1 |
30 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: addecrc.v
|
4 |
|
|
//
|
5 |
|
|
// Project: OpenArty, an entirely open SoC based upon the Arty platform
|
6 |
|
|
//
|
7 |
|
|
// Purpose: To (optionally) add a CRC to a stream of nibbles. The CRC
|
8 |
|
|
// is calculated from the stream.
|
9 |
|
|
//
|
10 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
11 |
|
|
// Gisselquist Technology, LLC
|
12 |
|
|
//
|
13 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
14 |
|
|
//
|
15 |
|
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
16 |
|
|
//
|
17 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
18 |
|
|
// modify it under the terms of the GNU General Public License as published
|
19 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
20 |
|
|
// your option) any later version.
|
21 |
|
|
//
|
22 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
23 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
24 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
25 |
|
|
// for more details.
|
26 |
|
|
//
|
27 |
|
|
// You should have received a copy of the GNU General Public License along
|
28 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
29 |
|
|
// target there if the PDF file isn't present.) If not, see
|
30 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
31 |
|
|
//
|
32 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
33 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
34 |
|
|
//
|
35 |
|
|
//
|
36 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
37 |
|
|
//
|
38 |
|
|
//
|
39 |
|
|
module addecrc(i_clk, i_ce, i_en, i_cancel, i_v, i_d, o_v, o_d);
|
40 |
|
|
localparam INVERT = 1; // Proper operation requires INVERT=1
|
41 |
|
|
input i_clk, i_ce, i_en, i_cancel;
|
42 |
|
|
input i_v;
|
43 |
|
|
input [3:0] i_d;
|
44 |
|
|
output reg o_v;
|
45 |
|
|
output reg [3:0] o_d;
|
46 |
|
|
|
47 |
|
|
reg [7:0] r_p;
|
48 |
|
|
reg [31:0] r_crc;
|
49 |
|
|
wire [3:0] lownibble;
|
50 |
|
|
wire [31:0] shifted_crc;
|
51 |
|
|
|
52 |
|
|
assign lownibble = r_crc[3:0] ^ i_d;
|
53 |
|
|
assign shifted_crc = { 4'h0, r_crc[31:4] };
|
54 |
|
|
|
55 |
|
|
initial o_v = 1'b0;
|
56 |
|
|
always @(posedge i_clk)
|
57 |
|
|
if (i_ce)
|
58 |
|
|
begin
|
59 |
|
|
if (((!i_v)&&(!o_v))||(i_cancel))
|
60 |
|
|
begin
|
61 |
|
|
r_crc <= (INVERT==0)? 32'h00 : 32'hffffffff;
|
62 |
|
|
r_p <= 8'hff;
|
63 |
|
|
end else if (i_v)
|
64 |
|
|
begin
|
65 |
|
|
o_v <= i_v;
|
66 |
|
|
r_p <= 8'hff;
|
67 |
|
|
o_d <= i_d;
|
68 |
|
|
|
69 |
|
|
`define CRCBIT8 32'hedb88320
|
70 |
|
|
`define CRCBIT4 32'h76dc4190
|
71 |
|
|
`define CRCBIT2 32'h3b6e20c8
|
72 |
|
|
`define CRCBIT1 32'h1db71064
|
73 |
|
|
|
74 |
|
|
// 0xedb88320 . 76dc4190 . 3b6e20c8 . 1db71064 . 0edb8832
|
75 |
|
|
case(lownibble)
|
76 |
|
|
4'h0: r_crc <= shifted_crc;
|
77 |
|
|
4'h1: r_crc <= shifted_crc ^ `CRCBIT1;
|
78 |
|
|
4'h2: r_crc <= shifted_crc ^ `CRCBIT2;
|
79 |
|
|
4'h3: r_crc <= shifted_crc ^ `CRCBIT2 ^ `CRCBIT1;
|
80 |
|
|
4'h4: r_crc <= shifted_crc ^ `CRCBIT4;
|
81 |
|
|
4'h5: r_crc <= shifted_crc ^ `CRCBIT4 ^ `CRCBIT1;
|
82 |
|
|
4'h6: r_crc <= shifted_crc ^ `CRCBIT4 ^ `CRCBIT2;
|
83 |
|
|
4'h7: r_crc <= shifted_crc ^ `CRCBIT4 ^ `CRCBIT2 ^ `CRCBIT1;
|
84 |
|
|
4'h8: r_crc <= shifted_crc ^ `CRCBIT8;
|
85 |
|
|
4'h9: r_crc <= shifted_crc ^ `CRCBIT8 ^ `CRCBIT1;
|
86 |
|
|
4'ha: r_crc <= shifted_crc ^ `CRCBIT8 ^ `CRCBIT2;
|
87 |
|
|
4'hb: r_crc <= shifted_crc ^ `CRCBIT8 ^ `CRCBIT2 ^ `CRCBIT1;
|
88 |
|
|
4'hc: r_crc <= shifted_crc ^ `CRCBIT8 ^ `CRCBIT4;
|
89 |
|
|
4'hd: r_crc <= shifted_crc ^ `CRCBIT8 ^ `CRCBIT4 ^ `CRCBIT1;
|
90 |
|
|
4'he: r_crc <= shifted_crc ^ `CRCBIT8 ^ `CRCBIT4 ^ `CRCBIT2;
|
91 |
|
|
4'hf: r_crc <= shifted_crc ^ `CRCBIT8 ^ `CRCBIT4 ^ `CRCBIT2 ^ `CRCBIT1;
|
92 |
|
|
|
93 |
|
|
default: r_crc <= { 4'h0, r_crc[31:4] };
|
94 |
|
|
endcase
|
95 |
|
|
end else begin
|
96 |
|
|
r_p <= { r_p[6:0], 1'b0 };
|
97 |
|
|
o_v <= (i_en)?r_p[7]:1'b0;
|
98 |
|
|
o_d <= r_crc[3:0] ^ ((INVERT==0)? 4'h0:4'hf);
|
99 |
|
|
r_crc <= { 4'h0, r_crc[31:4] };
|
100 |
|
|
end
|
101 |
|
|
end
|
102 |
|
|
|
103 |
|
|
endmodule
|
104 |
|
|
|