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