-
Notifications
You must be signed in to change notification settings - Fork 1
/
capture.v
288 lines (241 loc) · 7.03 KB
/
capture.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
`timescale 1ns / 1ps
module capture(input clock,
input reset,
// Capture interface
input tos_in,
input [31:0] frames,
input start,
output reg [31:0] frames_remaining,
output done,
output reg sync,
input [2:0] cs_word_sel,
output reg [31:0] cs_word,
// Memory interface
output cmd_clk,
output reg cmd_en,
output reg [2:0] cmd_instr,
output reg [5:0] cmd_bl,
output reg [29:0] cmd_byte_addr,
input cmd_empty,
input cmd_full,
output wr_clk,
output reg wr_en,
output [3:0] wr_mask,
output reg [31:0] wr_data,
input wr_full,
input wr_empty,
input [6:0] wr_count,
input wr_underrun,
input wr_error,
output rd_clk,
output rd_en,
input [31:0] rd_data,
input rd_full,
input rd_empty,
input [6:0] rd_count,
input rd_overflow,
input rd_error,
output [15:0] debug);
assign cmd_clk = clock;
assign wr_clk = clock;
assign rd_clk = clock;
assign wr_mask = 0;
assign rd_en = 0;
// Memory commands
localparam MEM_WRITE = 3'b000;
localparam MEM_READ = 3'b001;
localparam MEM_WRITE_AP = 3'b010;
localparam MEM_READ_AP = 3'b011;
localparam MEM_REFRESH = 3'b100;
// The address we're writing at
reg [29:0] address;
assign done = frames_remaining == 0;
// Recover the data and clock enable from the input
wire tos_ce;
wire tos_data;
clockrecovery cdr(clock, reset, tos_in, tos_ce, tos_data);
// Locate the syncwords
reg [7:0] syncword;
always @(posedge clock or posedge reset) begin
if (reset)
syncword <= 0;
else if (tos_ce)
syncword <= { syncword[6:0], tos_data };
end;
assign tos_bsync = (syncword == 8'b11101000
|| syncword == 8'b00010111);
assign tos_msync = (syncword == 8'b11100010
|| syncword == 8'b00011101);
assign tos_wsync = (syncword == 8'b11100100
|| syncword == 8'b00011011);
// Read the 32-bit words
reg [31:0] word;
reg word_complete;
wire tos_sync = tos_bsync || tos_msync || tos_wsync;
reg prev_bm; // Prev bit for Biphase Mark decide
reg bm_bit; // 1 if we're looking at the second bit of a pair
reg [4:0] bits; // Count of bits received so far
reg parity; // Running parity
// Combinatorial logic
wire decoded_bit = prev_bm ^ tos_data;
wire next_parity = parity ^ decoded_bit;
wire [31:0] next_word = { next_parity, word[29:0], decoded_bit };
// Output format is
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +-+-+-+-+---------------------------------------+-------+---+-+-+
// |P|C|S|V| sample | aux |syn|#|E|
// +-+-+-+-+---------------------------------------+-------+---+-+-+
//
// E = Parity error detected
// syn = Sync indicator - 00 = B, 01 = M, 10 = W
// aux = Auxiliary audio databits (used if 24-bit sample)
// sample = Audio sample
// V = Valid
// S = Subcode data
// C = Channel status information
// P = Parity (excluding top four bits)
localparam STATE_IDLE = 3'b000;
localparam STATE_BEGIN_CAPTURE = 3'b001;
localparam STATE_WAIT_WORD = 3'b010;
localparam STATE_WRITE_WORD = 3'b011;
reg [2:0] state;
reg [2:0] next_state;
// Capture control
always @(posedge clock or posedge reset) begin
if (reset)
state <= STATE_IDLE;
else
state <= next_state;
end
always @(*) begin
next_state = state;
case (state)
STATE_IDLE:
if (start)
next_state = STATE_BEGIN_CAPTURE;
STATE_BEGIN_CAPTURE:
next_state = STATE_WAIT_WORD;
STATE_WAIT_WORD:
if (frames_remaining == 0)
next_state = STATE_IDLE;
else if (word_complete)
next_state = STATE_WRITE_WORD;
STATE_WRITE_WORD:
next_state = STATE_WAIT_WORD;
endcase
end
integer n;
always @(posedge clock or posedge reset) begin
if (reset) begin
wr_en <= 1'b0;
cmd_en <= 1'b0;
frames_remaining <= 0;
end else begin
wr_en <= 1'b0;
cmd_en <= 1'b0;
case (next_state)
STATE_IDLE:
;
STATE_BEGIN_CAPTURE:
begin
frames_remaining <= frames;
address <= 0;
end
STATE_WAIT_WORD:
;
STATE_WRITE_WORD:
begin
// Put the word into the FIFO
for (n = 0; n < 32; n = n + 1) begin
wr_data[n] <= word[31 - n];
end
wr_en <= 1'b1;
// Now write the FIFO to memory
frames_remaining <= frames_remaining - 1;
cmd_instr <= MEM_WRITE;
cmd_byte_addr <= address;
cmd_bl <= 0;
cmd_en <= 1'b1;
address <= address + 29'h4;
end
endcase // case (next_state)
end // else: !if(reset)
end // always @ (posedge clock or posedge reset)
// Channel status
reg [191:0] channel_status;
reg [191:0] next_channel_status;
assign debug[0] = word_complete;
assign debug[1] = word[1];
assign debug[2] = tos_bsync;
assign debug[5:3] = cs_word_sel;
assign debug[15:6] = 0;
always @(posedge clock) begin
if (word_complete)
next_channel_status <= { next_channel_status[190:0], word[1] };
if (tos_bsync)
channel_status <= next_channel_status;
end // always @ (posedge clock)
always @(*) begin
case (cs_word_sel)
3'd0:
cs_word = channel_status[191:160];
3'd1:
cs_word = channel_status[159:128];
3'd2:
cs_word = channel_status[127:96];
3'd3:
cs_word = channel_status[95:64];
3'd4:
cs_word = channel_status[63:32];
3'd5:
cs_word = channel_status[31:0];
default:
cs_word = 0;
endcase
end
// Decoding
always @(posedge clock or posedge reset) begin
if (reset) begin
sync <= 1'b0;
word_complete <= 1'b0;
end else begin
word_complete <= 1'b0;
if (tos_ce) begin
if (tos_sync) begin
// When we detect sync, tos_data is the *first* bit of
// the actual data, so bm_bit must be one here
bm_bit <= 1'b1;
// Start the word with the sync indicator (the parity error
// is calculated as we receive data)
if (tos_bsync)
word <= 4'b0000;
else if (tos_msync)
word <= 4'b0010;
else if (tos_wsync)
word <= 4'b0001;
bits <= 4;
// No parity yet
parity <= 1'b0;
// We've locked sync
sync <= 1'b1;
end else if (sync) begin
bm_bit <= ~bm_bit;
if (bm_bit) begin
// Update word and parity
parity <= next_parity;
word <= next_word;
// If we've finished, unlock sync
bits <= bits + 1'b1;
if (bits == 5'b11111) begin
word_complete <= 1'b1;
sync <= 1'b0;
end
end
end // if (sync)
prev_bm <= tos_data;
end // if (tos_ce)
end // else: !if(reset)
end // always @ (posedge clock or posedge reset)
endmodule // capture