-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrc.v
More file actions
331 lines (302 loc) · 9.96 KB
/
crc.v
File metadata and controls
331 lines (302 loc) · 9.96 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
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
`timescale 1ns / 1ps
module crc_validate(
input [479:0] udp_rx,
input [31:0] sending_crc,
input [15:0] crc_ceiling,
input udp_rx_start,
input clk,
output check_valid,
output check_passed,
output [31:0] checkCRC,
output [15:0] index,
output [1:0] state
);
//construct pseudo header, calculate checksum, and add to one's complement of header and data. If the result is zero, pass the check
reg [15:0] index;
reg [1:0] state;
reg [31:0] checkCRC;
reg [479:0] crc_to_check;
reg check_valid, check_passed;
reg [31:0] crc_polynomial = 32'h973afb51;
wire [15:0] crc_ceiling;
always @ (posedge clk) begin
if(udp_rx_start) begin
check_valid = 1'b0;
index = crc_ceiling-32;
state = 2'b1;
crc_to_check = udp_rx;
end else if (state == 2'd2) begin
checkCRC = ~(sending_crc + crc_to_check[31:0]);
check_passed = !checkCRC ? 1'b1 : 1'b0;
check_valid = 1'b1;
index = 8'b0;
state = 2'b0;
end else if (state == 2'b1 && index == 16'd31) begin
state = 2'd2;
end else if (crc_to_check[index] == 1'd0 && state == 2'b1) begin
index = index - 1;
end else if (state == 2'b1) begin
crc_to_check[index-:32] = crc_to_check[index-:32] ^ crc_polynomial;
end
end
endmodule
//CRC Receives the entire received packet at once, instead of streaming. CRC_Ceiling indicates the size of the received packet for indexing the udp_tx input
module crc_calculate(
input [479:0] udp_tx,
input [15:0] crc_ceiling,
input udp_tx_start,
input clk,
output [31:0] calculated_crc,
output check_valid,
output [15:0] index,
output [1:0] state,
output [479:0] crc_to_check
);
//construct pseudo header, calculate checksum, and add to one's complement of header and data. If the result is zero, pass the check
reg [15:0] index;
reg [1:0] state = 2'b0;
reg [479:0] crc_to_check;
wire udp_tx_start;
reg check_valid;
reg [31:0] calculated_crc;
reg [31:0] crc_polynomial = 32'h973afb51;
wire [15:0] crc_ceiling;
always @ (posedge clk) begin
if(udp_tx_start) begin
check_valid = 1'b0;
index = crc_ceiling;
state = 2'b1;
crc_to_check = udp_tx;
end else if (state == 2'd2) begin
calculated_crc = ~crc_to_check[31:0];
check_valid = 1'b1;
index = 8'b0;
state = 2'b0;
end else if (state == 2'b1 && index == 16'd31) begin
state = 2'd2;
end else if (crc_to_check[index] == 1'd0 && state == 2'b1) begin
index = index - 1;
end else if (state == 2'b1) begin
crc_to_check[index-:32] = crc_to_check[index-:32] ^ crc_polynomial;
end
end
endmodule
//construct pseudo header, calculate checksum, and perform one's complement. Append to header
module udpip_rx(
input [7:0] udp_rx,
input udp_rx_valid,
input udp_rx_first,
input udp_rx_last,
input crc_check,
input crc_valid,
input clk,
output [479:0] crc_in,
output [31:0] received_crc,
output crc_start,
output [15:0] crc_ceiling,
output [7:0] to_udp,
output to_udp_valid,
output to_udp_first,
output to_udp_last,
output [15:0] packet_ceiling,
output [2:0] state
);
//reg [207:0] input_buffer;
reg [511:0] input_buffer;
reg [15:0] packet_ceiling;
reg [15:0] crc_ceiling;
reg [2:0] state = 3'b0;
reg [6:0] index = 0;
reg [7:0] wait_counter = 0;
reg [7:0] output_counter = 0;
reg crc_start;
reg [479:0] crc_in;
reg [31:0] received_crc;
reg [7:0] to_udp;
reg to_udp_valid, to_udp_first, to_udp_last = 0;
always @ (posedge clk) begin
if(state == 3'b0 && udp_rx_first) begin
state = 3'b1;
//input_buffer = {input_buffer[199:0],udp_rx};
input_buffer = {input_buffer[503:0],udp_rx};
packet_ceiling = 16'd7;
$display("HIT_START");
end else if (state == 3'b1 && udp_rx_valid && udp_rx_last) begin
//input_buffer = {input_buffer[199:0],udp_rx};
input_buffer = {input_buffer[503:0],udp_rx};
packet_ceiling = packet_ceiling + 8;
state = 3'd2;
crc_start = 1'b1;
crc_in = input_buffer[511:32];
received_crc = input_buffer[31:0];
crc_ceiling = packet_ceiling;
end else if (state == 3'b1 && udp_rx_valid) begin
input_buffer = {input_buffer[503:0],udp_rx};
packet_ceiling = packet_ceiling + 8;
end else if (state == 3'd2 && wait_counter == 4) begin
wait_counter = wait_counter + 1;
crc_start = 1'b0;
end else if (state == 3'd2 && wait_counter<5) begin
wait_counter = wait_counter + 1;
end else if (state == 3'd2 && wait_counter == 5 && crc_valid) begin
state = crc_check ? 3'd3 : 3'b0;
end else if (state == 3'd3 && wait_counter == 5) begin
to_udp = input_buffer[packet_ceiling-:8];
wait_counter = 0;
to_udp_valid = 1'b1;
to_udp_first = 1'b1;
packet_ceiling = packet_ceiling-8;
end else if (state == 3'd3 && packet_ceiling>16'd39) begin
to_udp = input_buffer[packet_ceiling-:8];
packet_ceiling = packet_ceiling-8;
to_udp_valid = 1'b1;
to_udp_first = 1'b0;
end else if (state == 3'd3 && packet_ceiling==16'd39) begin
to_udp_last = 1'b1;
to_udp_valid = 1'b1;
to_udp = input_buffer[39:32];
packet_ceiling = 16'b0;
end else if (state == 3'd3 && packet_ceiling == 16'b0) begin
state = 3'b0;
to_udp_last = 1'b0;
to_udp_valid = 1'b0;
end
end
endmodule
//Check CRC and destination MAC-address, discarding if things don't match
module udpip_tx(
input [7:0] from_udp,
input from_udp_valid,
input from_udp_first,
input from_udp_last,
input crc_valid,
input [31:0] calculated_crc,
input clk,
output [479:0] crc_input,
output [15:0] crc_ceiling,
output crc_start,
output [7:0] udp_tx,
output udp_tx_valid,
output udp_tx_first,
output udp_tx_last,
output [2:0] state,
output [543:0] output_buffer,
output [31:0] crc_calculated,
output [7:0] wait_counter
);
reg [511:0] input_buffer = 512'b0;
reg [543:0] output_buffer = 544'b0;
reg [15:0] packet_ceiling;
reg [15:0] crc_ceiling;
reg [2:0] state = 3'b0;
reg [6:0] index = 0;
reg [7:0] wait_counter = 0;
reg crc_start;
reg [479:0] crc_input;
reg [7:0] udp_tx;
reg udp_tx_valid = 0, udp_tx_first = 0, udp_tx_last = 0;
wire [31:0] crc_calculated;
always @ (negedge clk) begin
if(state == 3'b0 && from_udp_first) begin
state = 3'b1;
input_buffer = {input_buffer[471:0],from_udp};
packet_ceiling = 16'd7;
//$display("BEGINNING");
end else if (state == 3'b1 && from_udp_valid && from_udp_last) begin
input_buffer = {input_buffer[503:0],from_udp};
packet_ceiling = packet_ceiling + 8;
crc_input = input_buffer;
crc_ceiling = packet_ceiling;
state = 3'd2;
crc_start = 1'b1;
//$display("ENDING");
end else if (state == 3'b1 && from_udp_valid) begin
input_buffer = {input_buffer[503:0],from_udp};
packet_ceiling = packet_ceiling + 8;
//$display("CONTINUING");
end else if (state == 3'd2 && wait_counter<5) begin
wait_counter = wait_counter + 1;
crc_start = 1'b0;
end else if (state == 3'd2 && wait_counter == 8'd5 && crc_valid) begin
state = 3'd3;
output_buffer = {input_buffer, calculated_crc};
packet_ceiling = packet_ceiling + 32;
end else if (state == 3'd3 && wait_counter > 0) begin
udp_tx = output_buffer[packet_ceiling-:8];
packet_ceiling = packet_ceiling - 8;
udp_tx_valid = 1'b1;
udp_tx_first = 1'b1;
wait_counter = 0;
end else if (state == 3'd3 && packet_ceiling > 7) begin
udp_tx = output_buffer[packet_ceiling-:8];
packet_ceiling = packet_ceiling - 8;
udp_tx_valid = 1'b1;
udp_tx_first = 1'b0;
end else if (state == 3'd3 && packet_ceiling == 7) begin
udp_tx = output_buffer[7:0];
udp_tx_valid = 1'b1;
udp_tx_last = 1'b1;
packet_ceiling = 0;
end else if (state == 3'd3 && packet_ceiling == 0) begin
udp_tx_valid = 1'b0;
udp_tx_last = 1'b0;
state = 3'b0;
end
end
endmodule
//Assemble the full CRC oriented transmitter
module crc_tx(
input [7:0] from_udp,
input from_udp_valid,
input from_udp_first,
input from_udp_last,
input clk,
output [7:0] udp_tx,
output udp_tx_valid,
output udp_tx_first,
output udp_tx_last
);
// Inter-module connection wires
wire crc_valid;
wire [31:0] calculated_crc;
wire [479:0] crc_input;
wire [15:0] crc_ceiling;
wire crc_start;
// Null wires for test bench control signals
wire [2:0] null_state;
wire [543:0] buffer_null;
wire [31:0] calculated_null;
wire [7:0] wait_null;
wire [15:0] index_null;
wire [1:0] state_null;
wire [479:0] check_null;
udpip_tx crc_prep(.from_udp(from_udp),.from_udp_valid(from_udp_valid),.from_udp_first(from_udp_first),.from_udp_last(from_udp_last),.crc_valid(crc_valid),.calculated_crc(calculated_crc),.clk(clk),.crc_input(crc_input),.crc_ceiling(crc_ceiling),.crc_start(crc_start),.udp_tx(udp_tx),.udp_tx_valid(udp_tx_valid),.udp_tx_first(udp_tx_first),.udp_tx_last(udp_tx_last),.state(null_state),.output_buffer(buffer_null),.crc_calculated(calculated_null),.wait_counter(wait_null));
crc_calculate crc_calc(.udp_tx(crc_input),.crc_ceiling(crc_ceiling),.udp_tx_start(crc_start),.clk(clk),.calculated_crc(calculated_crc),.check_valid(crc_valid),.index(index_null),.state(state_null),.crc_to_check(check_null));
endmodule
//Assemble the full CRC oriented receiver
module crc_rx(
input [7:0] udp_rx,
input udp_rx_valid,
input udp_rx_first,
input udp_rx_last,
input clk,
output [7:0] to_udp,
output to_udp_valid,
output to_udp_first,
output to_udp_last,
output [2:0] state_null,
output crc_valid_rx,
output crc_check_rx
);
wire crc_check_rx, crc_valid_rx, crc_start_rx;
wire [479:0] crc_in;
wire [31:0] received_crc_rx, crc_null;
wire [15:0] crc_ceiling_rx, ceiling_null, index_null;
wire [2:0] state_null;
wire [1:0] null_state;
udpip_rx crc_prep(.udp_rx(udp_rx),.udp_rx_valid(udp_rx_valid),.udp_rx_first(udp_rx_first),.udp_rx_last(udp_rx_last),.crc_check(crc_check_rx),.crc_valid(crc_valid_rx),.clk(clk),.crc_in(crc_in),.received_crc(received_crc_rx),.crc_start(crc_start_rx),.crc_ceiling(crc_ceiling_rx),.to_udp(to_udp),.to_udp_valid(to_udp_valid),.to_udp_first(to_udp_first),.to_udp_last(to_udp_last),.packet_ceiling(ceiling_null),.state(state_null));
crc_validate crc_vald(.sending_crc(received_crc_rx),.crc_ceiling(crc_ceiling_rx),.udp_rx_start(crc_start_rx),.udp_rx(crc_in),.clk(clk),.check_valid(crc_valid_rx),.check_passed(crc_check_rx),.checkCRC(crc_null),.index(index_null),.state(null_state));
always @ (posedge clk) begin
//$display("CRC SENDING: %b %b %b",to_udp,to_udp_first,to_udp_last);
end
endmodule