-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsong_reader.v
More file actions
73 lines (63 loc) · 2.18 KB
/
Copy pathsong_reader.v
File metadata and controls
73 lines (63 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
`define SONG_WIDTH 5
`define NOTE_WIDTH 6
`define DURATION_WIDTH 6
// ----------------------------------------------
// Define State Assignments
// ----------------------------------------------
`define SWIDTH 3
`define PAUSED 3'b000
`define WAIT 3'b001
`define INCREMENT_ADDRESS 3'b010
`define RETRIEVE_NOTE 3'b011
`define NEW_NOTE_READY 3'b100
module song_reader(
input clk,
input reset,
input play,
input [1:0] song,
input note_done,
output wire song_done,
output wire [5:0] note,
output wire [5:0] duration,
output wire new_note
);
wire [`SONG_WIDTH-1:0] curr_note_num, next_note_num;
wire [`NOTE_WIDTH + `DURATION_WIDTH -1:0] note_and_duration;
wire [`SONG_WIDTH + 1:0] rom_addr = {song, curr_note_num};
wire [`SWIDTH-1:0] state;
reg [`SWIDTH-1:0] next;
// For identifying when we reach the end of a song
wire overflow;
dffr #(`SONG_WIDTH) note_counter (
.clk(clk),
.r(reset),
.d(next_note_num),
.q(curr_note_num)
);
dffr #(`SWIDTH) fsm (
.clk(clk),
.r(reset),
.d(next),
.q(state)
);
song_rom rom(.clk(clk), .addr(rom_addr), .dout(note_and_duration));
always @(*) begin
case (state)
`PAUSED: next = play ? `RETRIEVE_NOTE : `PAUSED;
`RETRIEVE_NOTE: next = play ? `NEW_NOTE_READY : `PAUSED;
`NEW_NOTE_READY: next = play ? `WAIT: `PAUSED;
`WAIT: next = !play ? `PAUSED
: (note_done ? `INCREMENT_ADDRESS
: `WAIT);
`INCREMENT_ADDRESS: next = (play && ~overflow) ? `RETRIEVE_NOTE
: `PAUSED;
default: next = `PAUSED;
endcase
end
assign {overflow, next_note_num} =
(state == `INCREMENT_ADDRESS) ? {1'b0, curr_note_num} + 1
: {1'b0, curr_note_num};
assign new_note = (state == `NEW_NOTE_READY);
assign {note, duration} = note_and_duration;
assign song_done = overflow;
endmodule