File tree Expand file tree Collapse file tree 4 files changed +57
-67
lines changed Expand file tree Collapse file tree 4 files changed +57
-67
lines changed Original file line number Diff line number Diff line change 1
1
files = [
2
- "crc12 .v" ,
2
+ "crc16 .v" ,
3
3
]
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ // Description : x^16 + x^12 + x^5 + 1
2
+ // Algorithm : CRC-16-CCIT
3
+ // Polynomial : 0x1021
4
+ // Init value : 0xFFFF
5
+
6
+ module crc16 #(
7
+ parameter DATA_WIDHT = 8 ,
8
+ parameter CRC_WIDTH = 16
9
+ ) (
10
+ input wire clk,
11
+ input wire arstn,
12
+ input wire en,
13
+ input wire [DATA_WIDHT- 1 :0 ] data,
14
+ output reg [CRC_WIDTH- 1 :0 ] crc16
15
+ );
16
+
17
+ always @(posedge clk or negedge arstn) begin
18
+ if (~ arstn) begin
19
+ crc16 <= 16'hffff ;
20
+ end else begin
21
+ crc16 <= en ? crc16_byte(crc16, data) : crc16;
22
+ end
23
+ end
24
+
25
+ function [CRC_WIDTH- 1 :0 ] crc16_bit;
26
+ input [CRC_WIDTH- 1 :0 ] crc;
27
+ input data;
28
+ begin
29
+ crc16_bit = crc << 1 ;
30
+ crc16_bit[0 ] = crc[15 ] ^ data;
31
+ crc16_bit[5 ] = crc[15 ] ^ data ^ crc[4 ];
32
+ crc16_bit[12 ] = crc[15 ] ^ data ^ crc[11 ];
33
+ end
34
+ endfunction
35
+
36
+ function [CRC_WIDTH- 1 :0 ] crc16_byte;
37
+ input [CRC_WIDTH- 1 :0 ] crc;
38
+ input [DATA_WIDHT- 1 :0 ] data;
39
+ integer i;
40
+ begin
41
+ crc16_byte = crc;
42
+ for (i = DATA_WIDHT - 1 ; i >= 0 ; i = i - 1 ) begin
43
+ crc16_byte = crc16_bit(crc16_byte, data[i]);
44
+ end
45
+ end
46
+ endfunction
47
+
48
+ endmodule
Original file line number Diff line number Diff line change 1
1
`timescale 1ps / 1ps
2
2
3
- module crc12_tb ();
3
+ module crc16_tb ();
4
4
5
5
localparam CLK_PERIOD = 2 ;
6
6
localparam SIM_TIME = 100 ;
@@ -10,14 +10,14 @@ reg arstn;
10
10
reg en;
11
11
reg [7 :0 ] data;
12
12
13
- wire [11 :0 ] crc12 ;
13
+ wire [15 :0 ] crc16 ;
14
14
15
- crc12 dut (
15
+ crc16 dut (
16
16
.clk (clk ),
17
17
.arstn (arstn),
18
18
.en (en ),
19
19
.data (data ),
20
- .crc12 (crc12 )
20
+ .crc16 (crc16 )
21
21
);
22
22
23
23
task rst();
@@ -34,7 +34,6 @@ endtask
34
34
35
35
task data_gen();
36
36
begin
37
- rst();
38
37
repeat (5 ) begin
39
38
en = 1 ;
40
39
data = $urandom_range(0 , 256 );
@@ -53,13 +52,14 @@ initial begin
53
52
end
54
53
55
54
initial begin
55
+ rst();
56
56
data_gen();
57
57
end
58
58
59
59
initial begin
60
- $dumpfile ("crc12_tb .vcd" );
61
- $dumpvars (0 , crc12_tb );
62
- $monitor("time=%g, crc=0x%h" , $time , crc12 );
60
+ $dumpfile ("crc16_tb .vcd" );
61
+ $dumpvars (0 , crc16_tb );
62
+ $monitor("time=%g, crc=0x%h" , $time , crc16 );
63
63
end
64
64
65
65
initial begin
You can’t perform that action at this time.
0 commit comments