Skip to content

Commit 21badbd

Browse files
committed
Add crc16 module
1 parent d5377ed commit 21badbd

File tree

4 files changed

+57
-67
lines changed

4 files changed

+57
-67
lines changed

modules/crc/Manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
files = [
2-
"crc12.v",
2+
"crc16.v",
33
]

modules/crc/crc12.v

Lines changed: 0 additions & 58 deletions
This file was deleted.

modules/crc/crc16.v

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

modules/crc/tb/crc12_tb.v renamed to modules/crc/tb/crc16_tb.v

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
`timescale 1ps / 1ps
22

3-
module crc12_tb();
3+
module crc16_tb();
44

55
localparam CLK_PERIOD = 2;
66
localparam SIM_TIME = 100;
@@ -10,14 +10,14 @@ reg arstn;
1010
reg en;
1111
reg [7:0] data;
1212

13-
wire [11:0] crc12;
13+
wire [15:0] crc16;
1414

15-
crc12 dut(
15+
crc16 dut(
1616
.clk (clk ),
1717
.arstn (arstn),
1818
.en (en ),
1919
.data (data ),
20-
.crc12 (crc12)
20+
.crc16 (crc16)
2121
);
2222

2323
task rst();
@@ -34,7 +34,6 @@ endtask
3434

3535
task data_gen();
3636
begin
37-
rst();
3837
repeat (5) begin
3938
en = 1;
4039
data = $urandom_range(0, 256);
@@ -53,13 +52,14 @@ initial begin
5352
end
5453

5554
initial begin
55+
rst();
5656
data_gen();
5757
end
5858

5959
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);
6363
end
6464

6565
initial begin

0 commit comments

Comments
 (0)