1 |
36 |
wfjm |
-- $Id: comlib.vhd 749 2016-03-20 22:09:03Z mueller $
|
2 |
2 |
wfjm |
--
|
3 |
35 |
wfjm |
-- Copyright 2007-2016 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
4 |
2 |
wfjm |
--
|
5 |
|
|
-- This program is free software; you may redistribute and/or modify it under
|
6 |
|
|
-- the terms of the GNU General Public License as published by the Free
|
7 |
|
|
-- Software Foundation, either version 2, or at your option any later version.
|
8 |
|
|
--
|
9 |
|
|
-- This program is distributed in the hope that it will be useful, but
|
10 |
|
|
-- WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
11 |
|
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
12 |
|
|
-- for complete details.
|
13 |
|
|
--
|
14 |
|
|
------------------------------------------------------------------------------
|
15 |
|
|
-- Package Name: comlib
|
16 |
|
|
-- Description: communication components
|
17 |
|
|
--
|
18 |
|
|
-- Dependencies: -
|
19 |
35 |
wfjm |
-- Tool versions: ise 8.2-14.7; viv 2014.4-2015.4; ghdl 0.18-0.33
|
20 |
2 |
wfjm |
-- Revision History:
|
21 |
|
|
-- Date Rev Version Comment
|
22 |
36 |
wfjm |
-- 2016-03-20 749 1.6.2 crc*_update*: leave return type unconstraint
|
23 |
35 |
wfjm |
-- 2016-03-13 744 1.6.1 crc16_update_tbl: work around XSim 2015.4 issue
|
24 |
27 |
wfjm |
-- 2014-09-27 595 1.6 add crc16 (using CRC-CCITT polynomial)
|
25 |
|
|
-- 2014-09-14 593 1.5 new iface for cdata2byte and byte2cdata
|
26 |
13 |
wfjm |
-- 2011-09-17 410 1.4 now numeric_std clean; use for crc8 'A6' polynomial
|
27 |
|
|
-- of Koopman et al.; crc8_update(_tbl) now function
|
28 |
12 |
wfjm |
-- 2011-07-30 400 1.3 added byte2word, word2byte
|
29 |
2 |
wfjm |
-- 2007-10-12 88 1.2.1 avoid ieee.std_logic_unsigned, use cast to unsigned
|
30 |
|
|
-- 2007-07-08 65 1.2 added procedure crc8_update_tbl
|
31 |
|
|
-- 2007-06-29 61 1.1.1 rename for crc8 SALT->INIT
|
32 |
|
|
-- 2007-06-17 58 1.1 add crc8
|
33 |
|
|
-- 2007-06-03 45 1.0 Initial version
|
34 |
|
|
------------------------------------------------------------------------------
|
35 |
|
|
|
36 |
|
|
library ieee;
|
37 |
|
|
use ieee.std_logic_1164.all;
|
38 |
13 |
wfjm |
use ieee.numeric_std.all;
|
39 |
2 |
wfjm |
|
40 |
|
|
use work.slvtypes.all;
|
41 |
|
|
|
42 |
|
|
package comlib is
|
43 |
|
|
|
44 |
12 |
wfjm |
component byte2word is -- 2 byte -> 1 word stream converter
|
45 |
|
|
port (
|
46 |
|
|
CLK : in slbit; -- clock
|
47 |
|
|
RESET : in slbit; -- reset
|
48 |
|
|
DI : in slv8; -- input data (byte)
|
49 |
|
|
ENA : in slbit; -- write enable
|
50 |
|
|
BUSY : out slbit; -- write port hold
|
51 |
|
|
DO : out slv16; -- output data (word)
|
52 |
|
|
VAL : out slbit; -- read valid
|
53 |
|
|
HOLD : in slbit; -- read hold
|
54 |
|
|
ODD : out slbit -- odd byte pending
|
55 |
|
|
);
|
56 |
|
|
end component;
|
57 |
|
|
|
58 |
|
|
component word2byte is -- 1 word -> 2 byte stream converter
|
59 |
|
|
port (
|
60 |
|
|
CLK : in slbit; -- clock
|
61 |
|
|
RESET : in slbit; -- reset
|
62 |
|
|
DI : in slv16; -- input data (word)
|
63 |
|
|
ENA : in slbit; -- write enable
|
64 |
|
|
BUSY : out slbit; -- write port hold
|
65 |
|
|
DO : out slv8; -- output data (byte)
|
66 |
|
|
VAL : out slbit; -- read valid
|
67 |
|
|
HOLD : in slbit; -- read hold
|
68 |
|
|
ODD : out slbit -- odd byte pending
|
69 |
|
|
);
|
70 |
|
|
end component;
|
71 |
|
|
|
72 |
27 |
wfjm |
constant c_cdata_escape : slv8 := "11001010"; -- char escape
|
73 |
|
|
constant c_cdata_fill : slv8 := "11010101"; -- char fill
|
74 |
|
|
constant c_cdata_xon : slv8 := "00010001"; -- char xon: ^Q = hex 11
|
75 |
|
|
constant c_cdata_xoff : slv8 := "00010011"; -- char xoff: ^S = hex 13
|
76 |
|
|
constant c_cdata_ec_xon : slv3 := "100"; -- escape code: xon
|
77 |
|
|
constant c_cdata_ec_xoff : slv3 := "101"; -- escape code: xoff
|
78 |
|
|
constant c_cdata_ec_fill : slv3 := "110"; -- escape code: fill
|
79 |
|
|
constant c_cdata_ec_esc : slv3 := "111"; -- escape code: escape
|
80 |
|
|
constant c_cdata_ed_pref : slv2 := "01"; -- edata: prefix
|
81 |
|
|
subtype c_cdata_edf_pref is integer range 7 downto 6; -- edata pref field
|
82 |
|
|
subtype c_cdata_edf_eci is integer range 5 downto 3; -- edata inv field
|
83 |
|
|
subtype c_cdata_edf_ec is integer range 2 downto 0; -- edata code field
|
84 |
|
|
|
85 |
2 |
wfjm |
component cdata2byte is -- 9bit comma,data -> byte stream
|
86 |
|
|
port (
|
87 |
|
|
CLK : in slbit; -- clock
|
88 |
|
|
RESET : in slbit; -- reset
|
89 |
27 |
wfjm |
ESCXON : in slbit; -- enable xon/xoff escaping
|
90 |
|
|
ESCFILL : in slbit; -- enable fill escaping
|
91 |
|
|
DI : in slv9; -- input data; bit 8 = comma flag
|
92 |
|
|
ENA : in slbit; -- input data enable
|
93 |
|
|
BUSY : out slbit; -- input data busy
|
94 |
2 |
wfjm |
DO : out slv8; -- output data
|
95 |
27 |
wfjm |
VAL : out slbit; -- output data valid
|
96 |
|
|
HOLD : in slbit -- output data hold
|
97 |
2 |
wfjm |
);
|
98 |
|
|
end component;
|
99 |
|
|
|
100 |
|
|
component byte2cdata is -- byte stream -> 9bit comma,data
|
101 |
|
|
port (
|
102 |
|
|
CLK : in slbit; -- clock
|
103 |
|
|
RESET : in slbit; -- reset
|
104 |
|
|
DI : in slv8; -- input data
|
105 |
27 |
wfjm |
ENA : in slbit; -- input data enable
|
106 |
|
|
ERR : in slbit; -- input data error
|
107 |
|
|
BUSY : out slbit; -- input data busy
|
108 |
|
|
DO : out slv9; -- output data; bit 8 = comma flag
|
109 |
|
|
VAL : out slbit; -- output data valid
|
110 |
|
|
HOLD : in slbit -- output data hold
|
111 |
2 |
wfjm |
);
|
112 |
|
|
end component;
|
113 |
|
|
|
114 |
|
|
component crc8 is -- crc-8 generator, checker
|
115 |
|
|
generic (
|
116 |
27 |
wfjm |
INIT: slv8 := "00000000"); -- initial state of crc register
|
117 |
2 |
wfjm |
port (
|
118 |
|
|
CLK : in slbit; -- clock
|
119 |
|
|
RESET : in slbit; -- reset
|
120 |
|
|
ENA : in slbit; -- update enable
|
121 |
|
|
DI : in slv8; -- input data
|
122 |
|
|
CRC : out slv8 -- crc code
|
123 |
|
|
);
|
124 |
|
|
end component;
|
125 |
|
|
|
126 |
27 |
wfjm |
component crc16 is -- crc-16 generator, checker
|
127 |
|
|
generic (
|
128 |
|
|
INIT: slv16 := (others=>'0')); -- initial state of crc register
|
129 |
|
|
port (
|
130 |
|
|
CLK : in slbit; -- clock
|
131 |
|
|
RESET : in slbit; -- reset
|
132 |
|
|
ENA : in slbit; -- update enable
|
133 |
|
|
DI : in slv8; -- input data
|
134 |
|
|
CRC : out slv16 -- crc code
|
135 |
|
|
);
|
136 |
|
|
end component;
|
137 |
|
|
|
138 |
36 |
wfjm |
-- Note: leave return type unconstraint ! A direction constraint return
|
139 |
|
|
-- type works fine in ghdl and ISim, but XSim will abort with an
|
140 |
|
|
-- run time error (there is indeed a mismatch, some simulators
|
141 |
|
|
-- tolerate this, some not, so never constrain a return type...).
|
142 |
2 |
wfjm |
|
143 |
36 |
wfjm |
function crc8_update (crc : in slv8; data : in slv8) return slv;
|
144 |
|
|
function crc8_update_tbl (crc : in slv8; data : in slv8) return slv;
|
145 |
27 |
wfjm |
|
146 |
36 |
wfjm |
function crc16_update (crc : in slv16; data : in slv8) return slv;
|
147 |
|
|
function crc16_update_tbl (crc : in slv16; data : in slv8) return slv;
|
148 |
|
|
|
149 |
12 |
wfjm |
end package comlib;
|
150 |
2 |
wfjm |
|
151 |
|
|
-- ----------------------------------------------------------------------------
|
152 |
|
|
|
153 |
|
|
package body comlib is
|
154 |
27 |
wfjm |
|
155 |
|
|
-- crc8_update and crc8_update_tbl implement the 'A6' polynomial of
|
156 |
|
|
-- Koopman and Chakravarty
|
157 |
|
|
-- x^8 + x^6 + x^3 + x^2 + 1 (0xa6)
|
158 |
|
|
-- see
|
159 |
|
|
-- http://dx.doi.org/10.1109%2FDSN.2004.1311885
|
160 |
|
|
-- http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf
|
161 |
|
|
--
|
162 |
36 |
wfjm |
function crc8_update (crc: in slv8; data: in slv8) return slv is
|
163 |
2 |
wfjm |
variable t : slv8 := (others=>'0');
|
164 |
13 |
wfjm |
variable n : slv8 := (others=>'0');
|
165 |
2 |
wfjm |
begin
|
166 |
|
|
|
167 |
|
|
t := data xor crc;
|
168 |
13 |
wfjm |
|
169 |
|
|
n(0) := t(5) xor t(4) xor t(2) xor t(0);
|
170 |
|
|
n(1) := t(6) xor t(5) xor t(3) xor t(1);
|
171 |
|
|
n(2) := t(7) xor t(6) xor t(5) xor t(0);
|
172 |
|
|
n(3) := t(7) xor t(6) xor t(5) xor t(4) xor t(2) xor t(1) xor t(0);
|
173 |
|
|
n(4) := t(7) xor t(6) xor t(5) xor t(3) xor t(2) xor t(1);
|
174 |
|
|
n(5) := t(7) xor t(6) xor t(4) xor t(3) xor t(2);
|
175 |
|
|
n(6) := t(7) xor t(3) xor t(2) xor t(0);
|
176 |
|
|
n(7) := t(4) xor t(3) xor t(1);
|
177 |
|
|
|
178 |
|
|
return n;
|
179 |
2 |
wfjm |
|
180 |
13 |
wfjm |
end function crc8_update;
|
181 |
2 |
wfjm |
|
182 |
36 |
wfjm |
function crc8_update_tbl (crc: in slv8; data: in slv8) return slv is
|
183 |
2 |
wfjm |
|
184 |
|
|
type crc8_tbl_type is array (0 to 255) of integer;
|
185 |
|
|
variable crc8_tbl : crc8_tbl_type := -- generated with gen_crc8_tbl
|
186 |
13 |
wfjm |
( 0, 77, 154, 215, 121, 52, 227, 174, -- 00-07
|
187 |
|
|
242, 191, 104, 37, 139, 198, 17, 92, -- 00-0f
|
188 |
|
|
169, 228, 51, 126, 208, 157, 74, 7, -- 10-17
|
189 |
|
|
91, 22, 193, 140, 34, 111, 184, 245, -- 10-1f
|
190 |
|
|
31, 82, 133, 200, 102, 43, 252, 177, -- 20-27
|
191 |
|
|
237, 160, 119, 58, 148, 217, 14, 67, -- 20-2f
|
192 |
|
|
182, 251, 44, 97, 207, 130, 85, 24, -- 30-37
|
193 |
|
|
68, 9, 222, 147, 61, 112, 167, 234, -- 30-3f
|
194 |
|
|
62, 115, 164, 233, 71, 10, 221, 144, -- 40-47
|
195 |
|
|
204, 129, 86, 27, 181, 248, 47, 98, -- 40-4f
|
196 |
|
|
151, 218, 13, 64, 238, 163, 116, 57, -- 50-57
|
197 |
|
|
101, 40, 255, 178, 28, 81, 134, 203, -- 50-5f
|
198 |
|
|
33, 108, 187, 246, 88, 21, 194, 143, -- 60-67
|
199 |
|
|
211, 158, 73, 4, 170, 231, 48, 125, -- 60-6f
|
200 |
|
|
136, 197, 18, 95, 241, 188, 107, 38, -- 70-70
|
201 |
|
|
122, 55, 224, 173, 3, 78, 153, 212, -- 70-7f
|
202 |
|
|
124, 49, 230, 171, 5, 72, 159, 210, -- 80-87
|
203 |
|
|
142, 195, 20, 89, 247, 186, 109, 32, -- 80-8f
|
204 |
|
|
213, 152, 79, 2, 172, 225, 54, 123, -- 90-97
|
205 |
|
|
39, 106, 189, 240, 94, 19, 196, 137, -- 90-9f
|
206 |
|
|
99, 46, 249, 180, 26, 87, 128, 205, -- a0-a7
|
207 |
|
|
145, 220, 11, 70, 232, 165, 114, 63, -- a0-af
|
208 |
|
|
202, 135, 80, 29, 179, 254, 41, 100, -- b0-b7
|
209 |
|
|
56, 117, 162, 239, 65, 12, 219, 150, -- b0-bf
|
210 |
|
|
66, 15, 216, 149, 59, 118, 161, 236, -- c0-c7
|
211 |
|
|
176, 253, 42, 103, 201, 132, 83, 30, -- c0-cf
|
212 |
|
|
235, 166, 113, 60, 146, 223, 8, 69, -- d0-d7
|
213 |
|
|
25, 84, 131, 206, 96, 45, 250, 183, -- d0-df
|
214 |
|
|
93, 16, 199, 138, 36, 105, 190, 243, -- e0-e7
|
215 |
|
|
175, 226, 53, 120, 214, 155, 76, 1, -- e0-ef
|
216 |
|
|
244, 185, 110, 35, 141, 192, 23, 90, -- f0-f7
|
217 |
|
|
6, 75, 156, 209, 127, 50, 229, 168 -- f0-ff
|
218 |
|
|
);
|
219 |
2 |
wfjm |
|
220 |
|
|
begin
|
221 |
|
|
|
222 |
13 |
wfjm |
return slv(to_unsigned(crc8_tbl(to_integer(unsigned(data xor crc))), 8));
|
223 |
2 |
wfjm |
|
224 |
13 |
wfjm |
end function crc8_update_tbl;
|
225 |
2 |
wfjm |
|
226 |
27 |
wfjm |
-- crc16_update and crc16_update_tbl implement the CCITT polynomial
|
227 |
|
|
-- x^16 + x^12 + x^5 + 1 (0x1021)
|
228 |
|
|
--
|
229 |
36 |
wfjm |
function crc16_update (crc: in slv16; data: in slv8) return slv is
|
230 |
27 |
wfjm |
variable n : slv16 := (others=>'0');
|
231 |
|
|
variable t : slv8 := (others=>'0');
|
232 |
|
|
begin
|
233 |
|
|
|
234 |
|
|
t := data xor crc(15 downto 8);
|
235 |
|
|
|
236 |
|
|
n(0) := t(4) xor t(0);
|
237 |
|
|
n(1) := t(5) xor t(1);
|
238 |
|
|
n(2) := t(6) xor t(2);
|
239 |
|
|
n(3) := t(7) xor t(3);
|
240 |
|
|
n(4) := t(4);
|
241 |
|
|
n(5) := t(5) xor t(4) xor t(0);
|
242 |
|
|
n(6) := t(6) xor t(5) xor t(1);
|
243 |
|
|
n(7) := t(7) xor t(6) xor t(2);
|
244 |
|
|
|
245 |
|
|
n(8) := t(7) xor t(3) xor crc(0);
|
246 |
|
|
n(9) := t(4) xor crc(1);
|
247 |
|
|
n(10) := t(5) xor crc(2);
|
248 |
|
|
n(11) := t(6) xor crc(3);
|
249 |
|
|
n(12) := t(7) xor t(4) xor t(0) xor crc(4);
|
250 |
|
|
n(13) := t(5) xor t(1) xor crc(5);
|
251 |
|
|
n(14) := t(6) xor t(2) xor crc(6);
|
252 |
|
|
n(15) := t(7) xor t(3) xor crc(7);
|
253 |
|
|
|
254 |
|
|
return n;
|
255 |
|
|
|
256 |
|
|
end function crc16_update;
|
257 |
|
|
|
258 |
36 |
wfjm |
function crc16_update_tbl (crc: in slv16; data: in slv8) return slv is
|
259 |
27 |
wfjm |
|
260 |
|
|
type crc16_tbl_type is array (0 to 255) of integer;
|
261 |
|
|
variable crc16_tbl : crc16_tbl_type :=
|
262 |
|
|
( 0, 4129, 8258, 12387, 16516, 20645, 24774, 28903,
|
263 |
|
|
33032, 37161, 41290, 45419, 49548, 53677, 57806, 61935,
|
264 |
|
|
4657, 528, 12915, 8786, 21173, 17044, 29431, 25302,
|
265 |
|
|
37689, 33560, 45947, 41818, 54205, 50076, 62463, 58334,
|
266 |
|
|
9314, 13379, 1056, 5121, 25830, 29895, 17572, 21637,
|
267 |
|
|
42346, 46411, 34088, 38153, 58862, 62927, 50604, 54669,
|
268 |
|
|
13907, 9842, 5649, 1584, 30423, 26358, 22165, 18100,
|
269 |
|
|
46939, 42874, 38681, 34616, 63455, 59390, 55197, 51132,
|
270 |
|
|
18628, 22757, 26758, 30887, 2112, 6241, 10242, 14371,
|
271 |
|
|
51660, 55789, 59790, 63919, 35144, 39273, 43274, 47403,
|
272 |
|
|
23285, 19156, 31415, 27286, 6769, 2640, 14899, 10770,
|
273 |
|
|
56317, 52188, 64447, 60318, 39801, 35672, 47931, 43802,
|
274 |
|
|
27814, 31879, 19684, 23749, 11298, 15363, 3168, 7233,
|
275 |
|
|
60846, 64911, 52716, 56781, 44330, 48395, 36200, 40265,
|
276 |
|
|
32407, 28342, 24277, 20212, 15891, 11826, 7761, 3696,
|
277 |
|
|
65439, 61374, 57309, 53244, 48923, 44858, 40793, 36728,
|
278 |
|
|
37256, 33193, 45514, 41451, 53516, 49453, 61774, 57711,
|
279 |
|
|
4224, 161, 12482, 8419, 20484, 16421, 28742, 24679,
|
280 |
|
|
33721, 37784, 41979, 46042, 49981, 54044, 58239, 62302,
|
281 |
|
|
689, 4752, 8947, 13010, 16949, 21012, 25207, 29270,
|
282 |
|
|
46570, 42443, 38312, 34185, 62830, 58703, 54572, 50445,
|
283 |
|
|
13538, 9411, 5280, 1153, 29798, 25671, 21540, 17413,
|
284 |
|
|
42971, 47098, 34713, 38840, 59231, 63358, 50973, 55100,
|
285 |
|
|
9939, 14066, 1681, 5808, 26199, 30326, 17941, 22068,
|
286 |
|
|
55628, 51565, 63758, 59695, 39368, 35305, 47498, 43435,
|
287 |
|
|
22596, 18533, 30726, 26663, 6336, 2273, 14466, 10403,
|
288 |
|
|
52093, 56156, 60223, 64286, 35833, 39896, 43963, 48026,
|
289 |
|
|
19061, 23124, 27191, 31254, 2801, 6864, 10931, 14994,
|
290 |
|
|
64814, 60687, 56684, 52557, 48554, 44427, 40424, 36297,
|
291 |
|
|
31782, 27655, 23652, 19525, 15522, 11395, 7392, 3265,
|
292 |
|
|
61215, 65342, 53085, 57212, 44955, 49082, 36825, 40952,
|
293 |
|
|
28183, 32310, 20053, 24180, 11923, 16050, 3793, 7920
|
294 |
|
|
);
|
295 |
|
|
|
296 |
|
|
variable ch : slv16 := (others=>'0');
|
297 |
35 |
wfjm |
variable cu : slv16 := (others=>'0');
|
298 |
27 |
wfjm |
variable t : slv8 := (others=>'0');
|
299 |
|
|
variable td : integer := 0;
|
300 |
|
|
|
301 |
|
|
begin
|
302 |
|
|
|
303 |
|
|
-- (crc<<8) ^ crc16_tbl[((crc>>8) ^ data) & 0x00ff]
|
304 |
|
|
ch := crc(7 downto 0) & "00000000";
|
305 |
|
|
t := data xor crc(15 downto 8);
|
306 |
|
|
td := crc16_tbl(to_integer(unsigned(t)));
|
307 |
36 |
wfjm |
return ch xor slv(to_unsigned(td, 16));
|
308 |
27 |
wfjm |
|
309 |
|
|
end function crc16_update_tbl;
|
310 |
|
|
|
311 |
2 |
wfjm |
end package body comlib;
|