-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayrec.v
198 lines (160 loc) · 4.54 KB
/
playrec.v
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
parameter N = 8;
parameter W = 16;
parameter H = 8;
module filter1(input clock,
input signed [W-1:0]Xin,
output reg signed [W-1:0]Y);
reg signed [W+H-1:0] y[N];
reg signed [H-1:0] h[N];
assign h[0] = -2;
assign h[1] = 8;
assign h[2] = -35;
assign h[3] = 157;
assign h[4] = 157;
assign h[5] = -35;
assign h[6] = 8;
assign h[7] = -2;
assign Y = y[N-1]>>H;
always @(posedge clock)begin
y[0] <= h[0]*Xin;
for(int i = 1; i < N; i++)
y[i] <= y[i-1]+h[N-1-i]*Xin;
end
endmodule
module filter2(input clock,
input signed [W-1:0]Xin,
output reg signed [W-1:0]Y);
reg signed [W:0] y[N];
assign Y = y[N-1][W-1:0];
always @(posedge clock)begin
y[0] <= Xin;
y[1] <= y[0];
y[2] <= y[1];
y[3] <= y[2];
y[4] <= y[3];
y[5] <= y[4];
y[6] <= y[5];
y[7] <= (y[6]>>1)+Xin;
end
endmodule
module filter3(input clock,
input signed [W-1:0]Xin,
output reg signed [W-1:0]Y);
reg signed [W+H-1:0] y[N];
reg signed [H-1:0] h[N];
assign h[0] = -1;
assign h[1] = 5;
assign h[2] = 39;
assign h[3] = 86;
assign h[4] = 86;
assign h[5] = 39;
assign h[6] = 5;
assign h[7] = -1;
assign Y = y[N-1]>>H;
always @(posedge clock)begin
y[0] <= h[0]*Xin;
for(int i = 1; i < N; i++)
y[i] <= y[i-1]+h[N-1-i]*Xin;
end
endmodule
parameter N2 = 9;
module filter4(input clock,
input signed [W-1:0]Xin,
output reg signed [W-1:0]Y);
reg signed [W+H-1:0] y[N2];
reg signed [H-1:0] h[N2];
assign h[0] = 0;
assign h[1] = -4;
assign h[2] = -22;
assign h[3] = -50;
assign h[4] = 192;
assign h[5] = -50;
assign h[6] = -22;
assign h[7] = -4;
assign h[8] = 0;
assign Y = y[N2-1]>>H;
always @(posedge clock)begin
y[0] <= h[0]*Xin;
for(int i = 1; i < N2; i++)
y[i] <= y[i-1]+h[N2-1-i]*Xin;
end
endmodule
module playrec(
input CLOCK_50, reset,
output [21:0] ram_addr,
output [15:0] ram_data_in,
output ram_read, ram_write,
input [15:0] ram_data_out,
input ram_valid, ram_waitrq,
output [15:0] audio_out,
input [15:0] audio_in,
input audio_out_allowed, audio_in_available,
output write_audio_out, read_audio_in,
input play, record, pause,
input [1:0] speed,
input [2:0] sel
);
reg signed [W-1:0] Y1, Y2, Y3, Y4, Y;
filter1 filter (.clock(CLOCK_50), .Xin(audio_in), .Y(Y1));
filter2 echo (.clock(CLOCK_50), .Xin(audio_in), .Y(Y2));
filter3 hc1 (.clock(CLOCK_50), .Xin(audio_in), .Y(Y3));
filter4 hc (.clock(CLOCK_50), .Xin(audio_in), .Y(Y4));
always @(sel)begin
case(sel)
0: Y = audio_in;
1: Y = Y1;
2: Y = Y2;
3: Y = Y3;
4: Y = Y4;
default: Y = audio_in;
endcase
end
// FSM for moving audio data to/from dram
reg [2:0] st, streg;
localparam st_start = 0,
st_rc_audio_wait = 1,
st_rc_ram_nextaddr = 2,
st_rc_ram_wait = 3,
st_pl_ram_rd = 4,
st_pl_audio_wait = 5,
st_pl_ram_nextaddr = 6,
st_input_check = 7;
always @(*) begin
st = streg;
case(streg)
st_start: st = st_input_check;
st_input_check:
if(!pause)
if(play) st = st_pl_audio_wait;
else if(record) st = st_rc_audio_wait;
else st = st_start;
st_rc_audio_wait: if(audio_in_available) st = st_rc_ram_nextaddr;
st_rc_ram_nextaddr: st = st_rc_ram_wait;
st_rc_ram_wait: if(!ram_waitrq) st = st_input_check;
st_pl_audio_wait: if(audio_out_allowed) st = st_pl_ram_rd;
st_pl_ram_rd: if(!ram_waitrq && ram_valid) st = st_pl_ram_nextaddr; // Technically, this should be 2 states. The way it is, we might perform
// several reads from the same address, but it doesn't matter.
st_pl_ram_nextaddr: st = st_input_check;
endcase
end
always @(posedge CLOCK_50)
if(reset) streg <= st_input_check;
else streg <= st;
// Ram address counter
always @(posedge CLOCK_50)
if(reset) ram_addr <= 0;
else
case(streg)
st_start: ram_addr <= 0;
st_rc_ram_nextaddr: ram_addr <= ram_addr + 1;
st_pl_ram_nextaddr: ram_addr <= ram_addr + 1 + speed;
endcase
// Connect the audio controller
assign read_audio_in = (streg == st_rc_ram_nextaddr) || (streg == st_start && audio_in_available);
assign write_audio_out = (st == st_pl_ram_nextaddr);
// Connect the dram
assign ram_data_in = Y;
assign audio_out = ram_data_out;
assign ram_write = (streg == st_rc_ram_wait);
assign ram_read = (streg == st_pl_ram_rd);
endmodule