forked from open-ephys/rhythm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAC_output_scalable_HPF.v
409 lines (342 loc) · 11.3 KB
/
DAC_output_scalable_HPF.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: Intan Technologies, LLC
//
// Design Name: RHD2000 Rhythm Interface
// Module Name: DAC_output_scalable_HPF
// Project Name: Opal Kelly FPGA/USB RHD2000 Interface
// Target Devices:
// Tool versions:
// Description: Generates SPI control signals for Analog Devices AD5662 16-bit DAC
// with adjustable gain and noise suppression, and built-in digital
// one-pole high-pass filter.
//
// Dependencies:
//
// Revision: 1.3 (14 October 2013)
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module DAC_output_scalable_HPF #(
parameter ms_wait = 99,
parameter ms_clk1_a = 100,
parameter ms_clk11_a = 140
)
(
input wire reset,
input wire dataclk,
input wire [31:0] main_state,
input wire [5:0] channel,
input wire [15:0] DAC_input,
input wire DAC_en,
input wire [2:0] gain,
input wire [6:0] noise_suppress,
output reg DAC_SYNC,
output reg DAC_SCLK,
output reg DAC_DIN,
input wire [15:0] DAC_thrsh,
input wire DAC_thrsh_pol,
output wire DAC_thrsh_out,
input wire [15:0] HPF_coefficient,
input wire HPF_en
);
wire [15:0] DAC_input_twos_comp, DAC_input_offset, DAC_register, subtract_result, add_result;
reg [15:0] DAC_input_suppressed, DAC_input_scaled;
wire [10:0] noise_suppress_x_16;
wire [17:0] HPF_input, multiplier_in;
wire [18:0] multiplier_in_before_limit;
wire [15:0] HPF_output;
wire [31:0] HPF_new_state;
wire [35:0] multiplier_out;
reg [31:0] HPF_state;
wire positive_overflow, negative_overflow;
reg state_clk;
// Implement one-pole high-pass filter
// Convert DAC_input from unsigned (offset) representation to signed (two's complement)
// represention by inverting the MSB. And extend to 18 bits since we use an 18-bit multiplier.
assign HPF_input = {~DAC_input[15], DAC_input[14:0], 2'b00};
assign multiplier_in_before_limit = HPF_input + ~HPF_state[31:14] + 1; // HPF_input - HPF_state
assign negative_overflow = HPF_input[17] & ~HPF_state[31] & ~multiplier_in_before_limit[17]; // detect negative overflow from subtraction
assign positive_overflow = ~HPF_input[17] & HPF_state[31] & multiplier_in_before_limit[17]; // detect positive overflow from subtraction
assign multiplier_in = positive_overflow ? 18'h1ffff : (negative_overflow ? 18'h20000 : multiplier_in_before_limit[17:0]); // limit subtractor output
// Multiplier with two 18-bit signed inputs and 36-bit output
multiplier_18x18 multiplier_1 (
.clk (dataclk),
.a (multiplier_in),
.b ({1'b0, HPF_coefficient, 1'b0}),
.p (multiplier_out)
);
// In total: new state = A*state + B*sample = (1-B)*state + B*sample = state + B*(sample - state)
assign HPF_new_state = HPF_state + multiplier_out[34:3];
// HPF output = sample - state
assign HPF_output = HPF_en ? multiplier_in[17:2] : HPF_input[17:2];
always @(posedge state_clk) begin
if (reset) begin
HPF_state <= 32'b0;
end else begin
HPF_state <= HPF_new_state;
end
end
// End of high-pass filter
assign DAC_input_twos_comp = HPF_output;
// Convert back from signed (two's complement) representation to unsigned (offset)
// representation for thresholding.
assign DAC_input_offset = {~DAC_input_twos_comp[15], DAC_input_twos_comp[14:0]};
// Implement simple threshold comparator function on unscaled DAC input
assign DAC_thrsh_out = DAC_en ? (DAC_thrsh_pol ? (DAC_input_offset >= DAC_thrsh) : (DAC_input_offset <= DAC_thrsh)) : 1'b0;
// Now if the input is positive, subtract noise_suppress from it, limiting at zero.
// If the input is negative, add noise_suppress to it, also limiting at zero. This
// has the effect of chopping out the noise between +noise_suppress and -noise_suppress,
// which makes neural spikes much easier to hear on audio.
// (Note: noise_suppress is scaled up by 2^4 since very low values of this variable
// have negligible effect.)
assign noise_suppress_x_16 = {noise_suppress, 4'b0000};
assign subtract_result = DAC_input_twos_comp - noise_suppress_x_16;
assign add_result = DAC_input_twos_comp + noise_suppress_x_16;
always @(*) begin
case ({DAC_input_twos_comp[15], subtract_result[15], add_result[15]})
3'b000: DAC_input_suppressed <= subtract_result;
3'b001: DAC_input_suppressed <= subtract_result;
3'b010: DAC_input_suppressed <= 16'b0;
3'b011: DAC_input_suppressed <= 16'b0;
3'b100: DAC_input_suppressed <= 16'b0;
3'b101: DAC_input_suppressed <= add_result;
3'b110: DAC_input_suppressed <= 16'b0;
3'b111: DAC_input_suppressed <= add_result;
endcase
end
// Next, scale the input by a factor of 2^gain by left shifting, but preserving the
// sign and saturating if the scaling exceeds the range of a 16-bit signed number.
always @(*) begin
case (gain)
0: DAC_input_scaled <= DAC_input_suppressed;
1: DAC_input_scaled <= (DAC_input_suppressed[14] == {1{DAC_input_suppressed[15]}}) ?
{DAC_input_suppressed[15], DAC_input_suppressed[13:0], 1'b0} :
{DAC_input_suppressed[15], {15{~DAC_input_suppressed[15]}}};
2: DAC_input_scaled <= (DAC_input_suppressed[14:13] == {2{DAC_input_suppressed[15]}}) ?
{DAC_input_suppressed[15], DAC_input_suppressed[12:0], 2'b00} :
{DAC_input_suppressed[15], {15{~DAC_input_suppressed[15]}}};
3: DAC_input_scaled <= (DAC_input_suppressed[14:12] == {3{DAC_input_suppressed[15]}}) ?
{DAC_input_suppressed[15], DAC_input_suppressed[11:0], 3'b000} :
{DAC_input_suppressed[15], {15{~DAC_input_suppressed[15]}}};
4: DAC_input_scaled <= (DAC_input_suppressed[14:11] == {4{DAC_input_suppressed[15]}}) ?
{DAC_input_suppressed[15], DAC_input_suppressed[10:0], 4'b0000} :
{DAC_input_suppressed[15], {15{~DAC_input_suppressed[15]}}};
5: DAC_input_scaled <= (DAC_input_suppressed[14:10] == {5{DAC_input_suppressed[15]}}) ?
{DAC_input_suppressed[15], DAC_input_suppressed[9:0], 5'b00000} :
{DAC_input_suppressed[15], {15{~DAC_input_suppressed[15]}}};
6: DAC_input_scaled <= (DAC_input_suppressed[14:9] == {6{DAC_input_suppressed[15]}}) ?
{DAC_input_suppressed[15], DAC_input_suppressed[8:0], 6'b000000} :
{DAC_input_suppressed[15], {15{~DAC_input_suppressed[15]}}};
7: DAC_input_scaled <= (DAC_input_suppressed[14:8] == {7{DAC_input_suppressed[15]}}) ?
{DAC_input_suppressed[15], DAC_input_suppressed[7:0], 7'b0000000} :
{DAC_input_suppressed[15], {15{~DAC_input_suppressed[15]}}};
endcase
end
// Finally, convert back from signed (two's complement) representation to unsigned (offset)
// representation for input to the DAC. If DAC_en == 0, set the DAC to midrange (zero volts).
assign DAC_register = DAC_en ? {~DAC_input_scaled[15], DAC_input_scaled[14:0]} : 16'b1000000000000000;
// AD5662 16-bit DAC SPI output logic
// (See Analog Devices AD5662 datasheet for more information.)
always @(posedge dataclk) begin
if (reset) begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
state_clk <= 1'b0;
end else begin
state_clk <= 1'b0;
case (main_state)
ms_wait: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
ms_clk1_a: begin
case (channel)
0: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
state_clk <= 1'b1;
end
1: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
2: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
3: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
4: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
5: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
6: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
7: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
8: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
9: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
10: begin
DAC_SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
DAC_DIN <= 1'b0;
end
11: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
12: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
13: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
14: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
15: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
16: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
17: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
18: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= 1'b0;
end
19: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[15];
end
20: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[14];
end
21: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[13];
end
22: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[12];
end
23: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[11];
end
24: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[10];
end
25: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[9];
end
26: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[8];
end
27: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[7];
end
28: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[6];
end
29: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[5];
end
30: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[4];
end
31: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[3];
end
32: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[2];
end
33: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[1];
end
34: begin
DAC_SYNC <= 1'b0;
DAC_SCLK <= 1'b1;
DAC_DIN <= DAC_register[0];
end
endcase
end
ms_clk11_a: begin
DAC_SCLK <= 1'b0;
end
endcase
end
end
endmodule