forked from TinyTapeout/tt08-verilog-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6508942
commit 6841e52
Showing
7 changed files
with
270 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
`default_nettype none | ||
|
||
module audio_engine( | ||
output wire audio, | ||
input wire clk, rst_n | ||
); | ||
|
||
parameter [6:0] NOTE_B1 = 7'd100; | ||
parameter [6:0] NOTE_D2 = 7'd84; | ||
parameter [6:0] NOTE_E2 = 7'd74; | ||
parameter [6:0] NOTE_F2 = 7'd70; | ||
parameter [6:0] NOTE_G2 = 7'd62; | ||
parameter [6:0] NOTE_A2 = 7'd55; | ||
parameter [6:0] NOTE_C3 = 7'd47; | ||
parameter [6:0] NOTE_D3 = 7'd42; | ||
parameter [6:0] NOTE_E3 = 7'd37; | ||
parameter [6:0] NOTE_F3 = 7'd35; | ||
parameter [6:0] NOTE_G3 = 7'd31; | ||
parameter [6:0] NOTE_A3 = 7'd28; | ||
|
||
function [6:0] seq_lut (input [6:0] timestamp); | ||
begin | ||
if (~timestamp[6]) begin | ||
if (~timestamp[4]) begin | ||
case (timestamp[3:0]) | ||
4'h0, | ||
4'h1, | ||
4'h3, | ||
4'h4, | ||
4'h6, | ||
4'h7, | ||
4'h9, | ||
4'hA: seq_lut = NOTE_C3; | ||
4'h2, | ||
4'h8, | ||
4'hE: seq_lut = NOTE_F2; | ||
4'h5, | ||
4'hB: seq_lut = NOTE_A2; | ||
4'hC: seq_lut = NOTE_D2; | ||
4'hD: seq_lut = NOTE_E2; | ||
4'hF: seq_lut = NOTE_G2; | ||
endcase | ||
end else begin | ||
case (timestamp[3:0]) | ||
4'h0, | ||
4'h1, | ||
4'h3, | ||
4'h4, | ||
4'h6, | ||
4'h7, | ||
4'h9, | ||
4'hA, | ||
4'hC, | ||
4'hD, | ||
4'hF: seq_lut = NOTE_G2; | ||
4'h2, | ||
4'h8, | ||
4'hE: seq_lut = NOTE_D2; | ||
4'h5, | ||
4'hB: seq_lut = NOTE_B1; | ||
endcase | ||
end | ||
end else begin | ||
if (~timestamp[2]) begin | ||
case (timestamp[1:0]) | ||
4'h0, | ||
4'h1: seq_lut = NOTE_A2; | ||
4'h2: seq_lut = NOTE_A3; | ||
4'h3: seq_lut = NOTE_F2; | ||
endcase | ||
end else begin | ||
if (~(timestamp[4] & timestamp[3])) begin | ||
case (timestamp[1:0]) | ||
4'h0: seq_lut = NOTE_C3; | ||
4'h1: seq_lut = NOTE_E3; | ||
4'h2, | ||
4'h3: seq_lut = NOTE_D3; | ||
endcase | ||
end else begin | ||
case (timestamp[1:0]) | ||
4'h0, | ||
4'h2: seq_lut = NOTE_D3; | ||
4'h1: seq_lut = NOTE_F3; | ||
4'h3: seq_lut = NOTE_C3; | ||
endcase | ||
end | ||
end | ||
end | ||
end | ||
endfunction | ||
|
||
wire synth_clk, seq_clk, seq_active; | ||
reg [4:0] seq_ctr; | ||
reg [6:0] seq_time; | ||
wire [6:0] seq_hp; | ||
|
||
reg [17:0] counter; | ||
always @(posedge clk or negedge rst_n) begin | ||
if (~rst_n) | ||
counter <= 17'd0; | ||
else | ||
counter <= counter + 1; | ||
end | ||
|
||
assign synth_clk = counter[10]; | ||
assign seq_clk = counter[17]; | ||
|
||
always @(posedge seq_clk or negedge rst_n) begin | ||
if (~rst_n) begin | ||
seq_ctr <= 5'd0; | ||
seq_time <= 8'd0; | ||
end else begin | ||
if (seq_ctr == 5'd19) begin | ||
seq_ctr <= 5'd0; | ||
seq_time <= seq_time + 1; | ||
end else | ||
seq_ctr <= seq_ctr + 5'd1; | ||
end | ||
end | ||
|
||
assign seq_hp = seq_lut(seq_time); | ||
assign seq_active = seq_ctr < 8'd10; | ||
|
||
freq_synth freq_synth1 ( | ||
.audio(audio), | ||
.synth_clk(synth_clk), .rst_n(rst_n), | ||
.hp(seq_hp), | ||
.active(seq_active) | ||
); | ||
|
||
wire _unused = 0; | ||
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
`default_nettype none | ||
|
||
module freq_synth( | ||
output wire audio, | ||
input wire synth_clk, rst_n, | ||
input wire [6:0] hp, | ||
input wire active | ||
); | ||
|
||
reg audio_reg; | ||
reg [6:0] hp_ctr; | ||
always @ (posedge synth_clk or negedge rst_n) begin | ||
if (~active | ~rst_n) begin | ||
audio_reg <= 1'd0; | ||
hp_ctr <= 1'd1; | ||
end else begin | ||
if (hp_ctr == hp) begin | ||
hp_ctr <= 1'd1; | ||
audio_reg <= ~audio_reg; | ||
end else | ||
hp_ctr <= hp_ctr + 1'd1; | ||
end | ||
end | ||
|
||
assign audio = audio_reg & active; | ||
|
||
wire _unused = 0; | ||
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Derived from TinyTapeout/vga-playground/blob/main/src/examples/common/hvsync_generator.v | ||
// Just some refactoring to maintain coding style | ||
|
||
`default_nettype none | ||
|
||
module vga_controller ( | ||
output reg [9:0] x, y, | ||
output reg h_sync, v_sync, | ||
output wire frame_active, | ||
input wire clk, rst_n | ||
); | ||
|
||
// declarations for TV-simulator sync parameters | ||
// horizontal constants | ||
parameter W_DISPLAY = 640; // horizontal display width | ||
parameter W_BACK = 48; // horizontal left border (back porch) | ||
parameter W_FRONT = 16; // horizontal right border (front porch) | ||
parameter W_SYNC = 96; // horizontal sync width | ||
// vertical constants | ||
parameter H_DISPLAY = 480; // vertical display height | ||
parameter H_TOP = 33; // vertical top border | ||
parameter H_BOTTOM = 10; // vertical bottom border | ||
parameter H_SYNC = 2; // vertical sync # lines | ||
// derived constants | ||
parameter W_SYNC_START = W_DISPLAY + W_FRONT; | ||
parameter W_SYNC_END = W_DISPLAY + W_FRONT + W_SYNC - 1; | ||
parameter W_MAX = W_DISPLAY + W_BACK + W_FRONT + W_SYNC - 1; | ||
parameter H_SYNC_START = H_DISPLAY + H_BOTTOM; | ||
parameter H_SYNC_END = H_DISPLAY + H_BOTTOM + H_SYNC - 1; | ||
parameter H_MAX = H_DISPLAY + H_TOP + H_BOTTOM + H_SYNC - 1; | ||
|
||
wire h_limit = (x == W_MAX) || rst_n; // set when x is maximum | ||
wire v_limit = (y == H_MAX) || rst_n; // set when y is maximum | ||
|
||
// horizontal position counter | ||
always @(posedge clk) begin | ||
h_sync <= (x >= W_SYNC_START && x <= W_SYNC_END); | ||
if(h_limit) | ||
x <= 0; | ||
else | ||
x <= x + 1; | ||
end | ||
|
||
// vertical position counter | ||
always @(posedge clk) begin | ||
v_sync <= (y >= H_SYNC_START && y <= H_SYNC_END); | ||
if(h_limit) | ||
if (v_limit) | ||
y <= 0; | ||
else | ||
y <= y + 1; | ||
end | ||
|
||
// frame_active is set when beam is in visible frame | ||
assign frame_active = (x < W_DISPLAY) && (y < H_DISPLAY); | ||
endmodule |