OpenCores
URL https://opencores.org/ocsvn/rio/rio/trunk

Subversion Repositories rio

[/] [rio/] [branches/] [2.0.0-development/] [rtl/] [vhdl/] [RioCommon.vhd] - Blame information for rev 45

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 magro732
-------------------------------------------------------------------------------
2
-- 
3
-- RapidIO IP Library Core
4
-- 
5
-- This file is part of the RapidIO IP library project
6
-- http://www.opencores.org/cores/rio/
7
-- 
8
-- Description
9
-- Contains commonly used types, functions, procedures and entities used in
10
-- the RapidIO IP library project.
11
-- 
12
-- To Do:
13
-- -
14
-- 
15
-- Author(s): 
16
-- - Magnus Rosenius, magro732@opencores.org 
17
-- 
18
-------------------------------------------------------------------------------
19
-- 
20
-- Copyright (C) 2013 Authors and OPENCORES.ORG 
21
-- 
22
-- This source file may be used and distributed without 
23
-- restriction provided that this copyright statement is not 
24
-- removed from the file and that any derivative work contains 
25
-- the original copyright notice and the associated disclaimer. 
26
-- 
27
-- This source file is free software; you can redistribute it 
28
-- and/or modify it under the terms of the GNU Lesser General 
29
-- Public License as published by the Free Software Foundation; 
30
-- either version 2.1 of the License, or (at your option) any 
31
-- later version. 
32
-- 
33
-- This source is distributed in the hope that it will be 
34
-- useful, but WITHOUT ANY WARRANTY; without even the implied 
35
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
36
-- PURPOSE. See the GNU Lesser General Public License for more 
37
-- details. 
38
-- 
39
-- You should have received a copy of the GNU Lesser General 
40
-- Public License along with this source; if not, download it 
41
-- from http://www.opencores.org/lgpl.shtml 
42
-- 
43
-------------------------------------------------------------------------------
44
 
45
 
46
-------------------------------------------------------------------------------
47
-- RioCommon library.
48
-------------------------------------------------------------------------------
49
library ieee;
50
use ieee.std_logic_1164.all;
51
use ieee.numeric_std.all;
52
use ieee.math_real.all;
53
use std.textio.all;
54
 
55
 
56
-------------------------------------------------------------------------------
57
-- RioCommon package description.
58
-------------------------------------------------------------------------------
59
package rio_common is
60
  -----------------------------------------------------------------------------
61
  -- Commonly used types.
62
  -----------------------------------------------------------------------------
63
  type Array1 is array (natural range <>) of
64
    std_logic;
65
  type Array2 is array (natural range <>) of
66
    std_logic_vector(1 downto 0);
67
  type Array3 is array (natural range <>) of
68
    std_logic_vector(2 downto 0);
69
  type Array4 is array (natural range <>) of
70
    std_logic_vector(3 downto 0);
71
  type Array5 is array (natural range <>) of
72
    std_logic_vector(4 downto 0);
73
  type Array8 is array (natural range <>) of
74
    std_logic_vector(7 downto 0);
75
  type Array9 is array (natural range <>) of
76
    std_logic_vector(8 downto 0);
77
  type Array10 is array (natural range <>) of
78
    std_logic_vector(9 downto 0);
79
  type Array16 is array (natural range <>) of
80
    std_logic_vector(15 downto 0);
81
  type Array32 is array (natural range <>) of
82
    std_logic_vector(31 downto 0);
83
  type Array34 is array (natural range <>) of
84
    std_logic_vector(33 downto 0);
85
 
86
  -----------------------------------------------------------------------------
87
  -- Commonly used constants.
88
  -----------------------------------------------------------------------------
89
 
90
  -- STYPE0 constants.
91
  constant STYPE0_PACKET_ACCEPTED : std_logic_vector(2 downto 0) := "000";
92
  constant STYPE0_PACKET_RETRY : std_logic_vector(2 downto 0) := "001";
93
  constant STYPE0_PACKET_NOT_ACCEPTED : std_logic_vector(2 downto 0) := "010";
94
  constant STYPE0_RESERVED : std_logic_vector(2 downto 0) := "011";
95
  constant STYPE0_STATUS : std_logic_vector(2 downto 0) := "100";
96
  constant STYPE0_VC_STATUS : std_logic_vector(2 downto 0) := "101";
97
  constant STYPE0_LINK_RESPONSE : std_logic_vector(2 downto 0) := "110";
98
  constant STYPE0_IMPLEMENTATION_DEFINED : std_logic_vector(2 downto 0) := "111";
99
 
100
  -- STYPE1 constants.
101
  constant STYPE1_START_OF_PACKET : std_logic_vector(2 downto 0) := "000";
102
  constant STYPE1_STOMP : std_logic_vector(2 downto 0) := "001";
103
  constant STYPE1_END_OF_PACKET : std_logic_vector(2 downto 0) := "010";
104
  constant STYPE1_RESTART_FROM_RETRY : std_logic_vector(2 downto 0) := "011";
105
  constant STYPE1_LINK_REQUEST : std_logic_vector(2 downto 0) := "100";
106
  constant STYPE1_MULTICAST_EVENT : std_logic_vector(2 downto 0) := "101";
107
  constant STYPE1_RESERVED : std_logic_vector(2 downto 0) := "110";
108
  constant STYPE1_NOP : std_logic_vector(2 downto 0) := "111";
109
 
110
  -- FTYPE constants.
111
  constant FTYPE_REQUEST_CLASS : std_logic_vector(3 downto 0) := "0010";
112
  constant FTYPE_WRITE_CLASS : std_logic_vector(3 downto 0) := "0101";
113
  constant FTYPE_STREAMING_WRITE_CLASS : std_logic_vector(3 downto 0) := "0110";
114
  constant FTYPE_MAINTENANCE_CLASS : std_logic_vector(3 downto 0) := "1000";
115
  constant FTYPE_RESPONSE_CLASS : std_logic_vector(3 downto 0) := "1101";
116
  constant FTYPE_DOORBELL_CLASS : std_logic_vector(3 downto 0) := "1010";
117
  constant FTYPE_MESSAGE_CLASS : std_logic_vector(3 downto 0) := "0010";
118
 
119
  -- TTYPE Constants
120
  constant TTYPE_MAINTENANCE_READ_REQUEST : std_logic_vector(3 downto 0) := "0000";
121
  constant TTYPE_MAINTENANCE_WRITE_REQUEST : std_logic_vector(3 downto 0) := "0001";
122
  constant TTYPE_MAINTENANCE_READ_RESPONSE : std_logic_vector(3 downto 0) := "0010";
123
  constant TTYPE_MAINTENANCE_WRITE_RESPONSE : std_logic_vector(3 downto 0) := "0011";
124
  constant TTYPE_NREAD_TRANSACTION : std_logic_vector(3 downto 0) := "0100";
125
  constant TTYPE_NWRITE_TRANSACTION : std_logic_vector(3 downto 0) := "0100";
126
 
127
  constant LINK_REQUEST_CMD_RESET_DEVICE : std_logic_vector(2 downto 0) := "011";
128
  constant LINK_REQUEST_CMD_INPUT_STATUS : std_logic_vector(2 downto 0) := "100";
129
 
130
  constant PACKET_NOT_ACCEPTED_CAUSE_UNEXPECTED_ACKID : std_logic_vector(4 downto 0) := "00001";
131
  constant PACKET_NOT_ACCEPTED_CAUSE_CONTROL_CRC : std_logic_vector(4 downto 0) := "00010";
132
  constant PACKET_NOT_ACCEPTED_CAUSE_NON_MAINTENANCE_STOPPED : std_logic_vector(4 downto 0) := "00011";
133
  constant PACKET_NOT_ACCEPTED_CAUSE_PACKET_CRC : std_logic_vector(4 downto 0) := "00100";
134
  constant PACKET_NOT_ACCEPTED_CAUSE_INVALID_CHARACTER : std_logic_vector(4 downto 0) := "00101";
135
  constant PACKET_NOT_ACCEPTED_CAUSE_NO_RESOURCES : std_logic_vector(4 downto 0) := "00110";
136
  constant PACKET_NOT_ACCEPTED_CAUSE_LOSS_DESCRAMBLER : std_logic_vector(4 downto 0) := "00111";
137
  constant PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR : std_logic_vector(4 downto 0) := "11111";
138
 
139
  -----------------------------------------------------------------------------
140
  -- Types used in simulations.
141
  -----------------------------------------------------------------------------
142
  type ByteArray is array (natural range <>) of
143
    std_logic_vector(7 downto 0);
144
  type HalfwordArray is array (natural range <>) of
145
    std_logic_vector(15 downto 0);
146
  type WordArray is array (natural range <>) of
147
    std_logic_vector(31 downto 0);
148
  type DoublewordArray is array (natural range <>) of
149
    std_logic_vector(63 downto 0);
150
 
151
  -- Type defining a RapidIO frame.
152
  type RioFrame is record
153
    length : natural range 0 to 69;
154
    payload : WordArray(0 to 68);
155
  end record;
156
  type RioFrameArray is array (natural range <>) of RioFrame;
157
 
158
  -- Type defining a RapidIO payload.
159
  type RioPayload is record
160
    length : natural range 0 to 133;
161
    data : HalfwordArray(0 to 132);
162
  end record;
163
 
164
  -----------------------------------------------------------------------------
165
  -- Crc5 calculation function.
166
  -- ITU, polynom=0x15.
167
  -----------------------------------------------------------------------------
168
  function Crc5(constant data : in std_logic_vector(18 downto 0);
169
                constant crc : in std_logic_vector(4 downto 0))
170
    return std_logic_vector;
171
 
172
  ---------------------------------------------------------------------------
173
  -- Create a RapidIO physical layer control symbol.
174
  ---------------------------------------------------------------------------
175
  function RioControlSymbolCreate(
176
    constant stype0 : in std_logic_vector(2 downto 0);
177
    constant parameter0 : in std_logic_vector(4 downto 0);
178
    constant parameter1 : in std_logic_vector(4 downto 0);
179
    constant stype1 : in std_logic_vector(2 downto 0);
180
    constant cmd : in std_logic_vector(2 downto 0))
181
    return std_logic_vector;
182
 
183
  -----------------------------------------------------------------------------
184
  -- Crc16 calculation function.
185
  -- CITT, polynom=0x1021.
186
  -----------------------------------------------------------------------------
187
  function Crc16(constant data : in std_logic_vector(15 downto 0);
188
                 constant crc : in std_logic_vector(15 downto 0))
189
    return std_logic_vector;
190
 
191
  ---------------------------------------------------------------------------
192
  -- Create a randomly initialized data array.
193
  ---------------------------------------------------------------------------
194
  procedure CreateRandomPayload(
195
    variable payload : out HalfwordArray(0 to 132);
196
    variable seed1 : inout positive;
197
    variable seed2 : inout positive);
198
  procedure CreateRandomPayload(
199
    variable payload : out DoublewordArray(0 to 31);
200
    variable seed1 : inout positive;
201
    variable seed2 : inout positive);
202
 
203
  ---------------------------------------------------------------------------
204
  -- Create a generic RapidIO frame.
205
  ---------------------------------------------------------------------------
206
  function RioFrameCreate(
207
    constant ackId : in std_logic_vector(4 downto 0);
208
    constant vc : in std_logic;
209
    constant crf : in std_logic;
210
    constant prio : in std_logic_vector(1 downto 0);
211
    constant tt : in std_logic_vector(1 downto 0);
212
    constant ftype : in std_logic_vector(3 downto 0);
213
    constant sourceId : in std_logic_vector(15 downto 0);
214
    constant destId : in std_logic_vector(15 downto 0);
215
    constant payload : in RioPayload)
216
    return RioFrame;
217
 
218
  ---------------------------------------------------------------------------
219
  -- Create a NWRITE RapidIO frame.
220
  ---------------------------------------------------------------------------
221
  function RioNwrite(
222
    constant wrsize : in std_logic_vector(3 downto 0);
223 45 magro732
    constant address : in std_logic_vector(28 downto 0);
224
    constant wdptr : in std_logic;
225
    constant xamsbs : in std_logic_vector(1 downto 0);
226
    constant dataLength : in natural range 1 to 32;
227
    constant data : in DoublewordArray(0 to 31))
228
    return RioPayload;
229
 
230
  ---------------------------------------------------------------------------
231
  -- Create a NWRITER RapidIO frame.
232
  ---------------------------------------------------------------------------
233
  function RioNwriteR(
234
    constant wrsize : in std_logic_vector(3 downto 0);
235 2 magro732
    constant tid : in std_logic_vector(7 downto 0);
236
    constant address : in std_logic_vector(28 downto 0);
237
    constant wdptr : in std_logic;
238
    constant xamsbs : in std_logic_vector(1 downto 0);
239
    constant dataLength : in natural range 1 to 32;
240
    constant data : in DoublewordArray(0 to 31))
241
    return RioPayload;
242
 
243
  ---------------------------------------------------------------------------
244
  -- Create a NREAD RapidIO frame.
245
  ---------------------------------------------------------------------------
246
  function RioNread(
247
    constant rdsize : in std_logic_vector(3 downto 0);
248
    constant tid : in std_logic_vector(7 downto 0);
249
    constant address : in std_logic_vector(28 downto 0);
250
    constant wdptr : in std_logic;
251
    constant xamsbs : in std_logic_vector(1 downto 0))
252
    return RioPayload;
253
 
254
  ---------------------------------------------------------------------------
255
  -- Create a RESPONSE RapidIO frame.
256
  ---------------------------------------------------------------------------
257
  function RioResponse(
258
    constant status : in std_logic_vector(3 downto 0);
259
    constant tid : in std_logic_vector(7 downto 0);
260
    constant dataLength : in natural range 0 to 32;
261
    constant data : in DoublewordArray(0 to 31))
262
    return RioPayload;
263
 
264
  ---------------------------------------------------------------------------
265
  -- Create a Maintenance RapidIO frame.
266
  ---------------------------------------------------------------------------
267
  function RioMaintenance(
268
    constant transaction : in std_logic_vector(3 downto 0);
269
    constant size : in std_logic_vector(3 downto 0);
270
    constant tid : in std_logic_vector(7 downto 0);
271
    constant hopCount : in std_logic_vector(7 downto 0);
272
    constant configOffset : in std_logic_vector(20 downto 0);
273
    constant wdptr : in std_logic;
274
    constant dataLength : in natural range 0 to 8;
275
    constant data : in DoublewordArray(0 to 7))
276
    return RioPayload;
277
 
278
  -----------------------------------------------------------------------------
279
  -- Function to convert a std_logic_vector to a string.
280
  -----------------------------------------------------------------------------
281
  function byteToString(constant byte : std_logic_vector(7 downto 0))
282
    return string;
283
 
284 40 magro732
  -----------------------------------------------------------------------------
285
  -- Function to print a std_logic_vector.
286
  -----------------------------------------------------------------------------
287
  function to_string(constant value : std_logic_vector)
288
    return string;
289
 
290 2 magro732
  ---------------------------------------------------------------------------
291
  -- Procedure to print to report file and output
292
  ---------------------------------------------------------------------------
293
  procedure PrintR( constant str : string );
294
 
295
  ---------------------------------------------------------------------------
296
  -- Procedure to print to spec file
297
  ---------------------------------------------------------------------------
298
  procedure PrintS( constant str : string );
299
 
300
  ---------------------------------------------------------------------------
301
  -- Procedure to Assert Expression
302
  ---------------------------------------------------------------------------
303
  procedure AssertE( constant exp: boolean; constant str : string );
304
 
305
  ---------------------------------------------------------------------------
306
  -- Procedure to Print Error
307
  ---------------------------------------------------------------------------
308
  procedure PrintE( constant str : string );
309
 
310
  ---------------------------------------------------------------------------
311 40 magro732
  -- Procedures for test control.
312 2 magro732
  ---------------------------------------------------------------------------
313 40 magro732
 
314
  procedure TestWarning(constant tag : in string);
315
  procedure TestError(constant tag : in string;
316
                      constant stopAtError : in boolean := true);
317
  procedure TestCompare(constant expression : in boolean;
318
                        constant tag : in string := "";
319
                        constant stopAtError : in boolean := true);
320
  procedure TestCompare(constant got : in std_logic;
321
                        constant expected : in std_logic;
322
                        constant tag : in string := "";
323
                        constant stopAtError : in boolean := true);
324
  procedure TestCompare(constant got : in std_logic_vector;
325
                        constant expected : in std_logic_vector;
326
                        constant tag : in string := "";
327
                        constant stopAtError : in boolean := true);
328
  procedure TestCompare(constant got : in natural;
329
                        constant expected : in natural;
330
                        constant tag : in string := "";
331
                        constant stopAtError : in boolean := true);
332
  procedure TestCompare(constant got : in time;
333
                        constant expected : in time;
334
                        constant tag : in string := "";
335
                        constant stopAtError : in boolean := true);
336
 
337
  procedure TestWait(signal waitSignal : in std_logic;
338
                     constant waitValue : in std_logic;
339
                     constant tag : in string := "";
340
                     constant waitTime : in time := 1 ms;
341
                     constant stopAtError : in boolean := true);
342
  procedure TestWait(signal waitSignal : in std_logic;
343
                     constant waitValue : in std_logic;
344
                     signal ackSignal : inout std_logic;
345
                     constant tag : in string := "";
346
                     constant waitTime : in time := 1 ms;
347
                     constant stopAtError : in boolean := true);
348
 
349 2 magro732
  procedure TestEnd;
350
 
351
end package;
352
 
353
-------------------------------------------------------------------------------
354
-- RioCommon package body description.
355
-------------------------------------------------------------------------------
356
package body rio_common is
357
  -----------------------------------------------------------------------------
358
  -- Crc5 calculation function.
359
  -- ITU, polynom=0x15.
360
  -----------------------------------------------------------------------------
361
  function Crc5(constant data : in std_logic_vector(18 downto 0);
362
                constant crc : in std_logic_vector(4 downto 0))
363
    return std_logic_vector is
364
    type crcTableType is array (0 to 31) of std_logic_vector(7 downto 0);
365
    constant crcTable : crcTableType := (
366
      x"00", x"15", x"1f", x"0a", x"0b", x"1e", x"14", x"01",
367
      x"16", x"03", x"09", x"1c", x"1d", x"08", x"02", x"17",
368
      x"19", x"0c", x"06", x"13", x"12", x"07", x"0d", x"18",
369
      x"0f", x"1a", x"10", x"05", x"04", x"11", x"1b", x"0e" );
370
    variable index : natural range 0 to 31;
371
    variable result : std_logic_vector(4 downto 0);
372
  begin
373
    result := crc;
374
    index := to_integer(unsigned(data(18 downto 14) xor result));
375
    result := crcTable(index)(4 downto 0);
376
    index := to_integer(unsigned(data(13 downto 9) xor result));
377
    result := crcTable(index)(4 downto 0);
378
    index := to_integer(unsigned(data(8 downto 4) xor result));
379
    result := crcTable(index)(4 downto 0);
380
    index := to_integer(unsigned((data(3 downto 0) & '0') xor result));
381
    return crcTable(index)(4 downto 0);
382
  end Crc5;
383
 
384
  ---------------------------------------------------------------------------
385
  -- Create a RapidIO physical layer control symbol.
386
  ---------------------------------------------------------------------------
387
  function RioControlSymbolCreate(
388
    constant stype0 : in std_logic_vector(2 downto 0);
389
    constant parameter0 : in std_logic_vector(4 downto 0);
390
    constant parameter1 : in std_logic_vector(4 downto 0);
391
    constant stype1 : in std_logic_vector(2 downto 0);
392
    constant cmd : in std_logic_vector(2 downto 0))
393
    return std_logic_vector is
394 37 magro732
    variable returnValue : std_logic_vector(23 downto 0);
395 2 magro732
    variable symbolData : std_logic_vector(18 downto 0);
396
  begin
397
    symbolData(18 downto 16) := stype0;
398
    symbolData(15 downto 11) := parameter0;
399
    symbolData(10 downto 6) := parameter1;
400
    symbolData(5 downto 3) := stype1;
401
    symbolData(2 downto 0) := cmd;
402
 
403 37 magro732
    returnValue(23 downto 5) := symbolData;
404
    returnValue(4 downto 0) := Crc5(symbolData, "11111");
405 2 magro732
 
406
    return returnValue;
407
  end function;
408
 
409
  -----------------------------------------------------------------------------
410
  -- Crc16 calculation function.
411
  -- CITT, polynom=0x1021.
412
  -----------------------------------------------------------------------------
413
  function Crc16(constant data : in std_logic_vector(15 downto 0);
414
                 constant crc : in std_logic_vector(15 downto 0))
415
    return std_logic_vector is
416
    type crcTableType is array (0 to 255) of std_logic_vector(15 downto 0);
417
    constant crcTable : crcTableType := (
418
      x"0000", x"1021", x"2042", x"3063", x"4084", x"50a5", x"60c6", x"70e7",
419
      x"8108", x"9129", x"a14a", x"b16b", x"c18c", x"d1ad", x"e1ce", x"f1ef",
420
      x"1231", x"0210", x"3273", x"2252", x"52b5", x"4294", x"72f7", x"62d6",
421
      x"9339", x"8318", x"b37b", x"a35a", x"d3bd", x"c39c", x"f3ff", x"e3de",
422
      x"2462", x"3443", x"0420", x"1401", x"64e6", x"74c7", x"44a4", x"5485",
423
      x"a56a", x"b54b", x"8528", x"9509", x"e5ee", x"f5cf", x"c5ac", x"d58d",
424
      x"3653", x"2672", x"1611", x"0630", x"76d7", x"66f6", x"5695", x"46b4",
425
      x"b75b", x"a77a", x"9719", x"8738", x"f7df", x"e7fe", x"d79d", x"c7bc",
426
      x"48c4", x"58e5", x"6886", x"78a7", x"0840", x"1861", x"2802", x"3823",
427
      x"c9cc", x"d9ed", x"e98e", x"f9af", x"8948", x"9969", x"a90a", x"b92b",
428
      x"5af5", x"4ad4", x"7ab7", x"6a96", x"1a71", x"0a50", x"3a33", x"2a12",
429
      x"dbfd", x"cbdc", x"fbbf", x"eb9e", x"9b79", x"8b58", x"bb3b", x"ab1a",
430
      x"6ca6", x"7c87", x"4ce4", x"5cc5", x"2c22", x"3c03", x"0c60", x"1c41",
431
      x"edae", x"fd8f", x"cdec", x"ddcd", x"ad2a", x"bd0b", x"8d68", x"9d49",
432
      x"7e97", x"6eb6", x"5ed5", x"4ef4", x"3e13", x"2e32", x"1e51", x"0e70",
433
      x"ff9f", x"efbe", x"dfdd", x"cffc", x"bf1b", x"af3a", x"9f59", x"8f78",
434
      x"9188", x"81a9", x"b1ca", x"a1eb", x"d10c", x"c12d", x"f14e", x"e16f",
435
      x"1080", x"00a1", x"30c2", x"20e3", x"5004", x"4025", x"7046", x"6067",
436
      x"83b9", x"9398", x"a3fb", x"b3da", x"c33d", x"d31c", x"e37f", x"f35e",
437
      x"02b1", x"1290", x"22f3", x"32d2", x"4235", x"5214", x"6277", x"7256",
438
      x"b5ea", x"a5cb", x"95a8", x"8589", x"f56e", x"e54f", x"d52c", x"c50d",
439
      x"34e2", x"24c3", x"14a0", x"0481", x"7466", x"6447", x"5424", x"4405",
440
      x"a7db", x"b7fa", x"8799", x"97b8", x"e75f", x"f77e", x"c71d", x"d73c",
441
      x"26d3", x"36f2", x"0691", x"16b0", x"6657", x"7676", x"4615", x"5634",
442
      x"d94c", x"c96d", x"f90e", x"e92f", x"99c8", x"89e9", x"b98a", x"a9ab",
443
      x"5844", x"4865", x"7806", x"6827", x"18c0", x"08e1", x"3882", x"28a3",
444
      x"cb7d", x"db5c", x"eb3f", x"fb1e", x"8bf9", x"9bd8", x"abbb", x"bb9a",
445
      x"4a75", x"5a54", x"6a37", x"7a16", x"0af1", x"1ad0", x"2ab3", x"3a92",
446
      x"fd2e", x"ed0f", x"dd6c", x"cd4d", x"bdaa", x"ad8b", x"9de8", x"8dc9",
447
      x"7c26", x"6c07", x"5c64", x"4c45", x"3ca2", x"2c83", x"1ce0", x"0cc1",
448
      x"ef1f", x"ff3e", x"cf5d", x"df7c", x"af9b", x"bfba", x"8fd9", x"9ff8",
449
      x"6e17", x"7e36", x"4e55", x"5e74", x"2e93", x"3eb2", x"0ed1", x"1ef0" );
450
    variable index : natural range 0 to 255;
451
    variable result : std_logic_vector(15 downto 0);
452
  begin
453
    result := crc;
454
    index := to_integer(unsigned(data(15 downto 8) xor result(15 downto 8)));
455
    result := crcTable(index) xor (result(7 downto 0) & x"00");
456
    index := to_integer(unsigned(data(7 downto 0) xor result(15 downto 8)));
457
    result := crcTable(index) xor (result(7 downto 0) & x"00");
458
    return result;
459
  end Crc16;
460
 
461
  ---------------------------------------------------------------------------
462
  -- Create a randomly initialized data array.
463
  ---------------------------------------------------------------------------
464
  procedure CreateRandomPayload(
465
    variable payload : out HalfwordArray(0 to 132);
466
    variable seed1 : inout positive;
467
    variable seed2 : inout positive) is
468
    variable rand: real;
469
    variable int_rand: integer;
470
    variable stim: std_logic_vector(7 downto 0);
471
  begin
472
    for i in payload'range loop
473
      uniform(seed1, seed2, rand);
474
      int_rand := integer(trunc(rand*256.0));
475
      payload(i)(7 downto 0) := std_logic_vector(to_unsigned(int_rand, 8));
476
      uniform(seed1, seed2, rand);
477
      int_rand := integer(trunc(rand*256.0));
478
      payload(i)(15 downto 8) := std_logic_vector(to_unsigned(int_rand, 8));
479
    end loop;
480
  end procedure;
481
 
482
  procedure CreateRandomPayload(
483
    variable payload : out DoublewordArray(0 to 31);
484
    variable seed1 : inout positive;
485
    variable seed2 : inout positive) is
486
    variable rand: real;
487
    variable int_rand: integer;
488
    variable stim: std_logic_vector(7 downto 0);
489
  begin
490
    for i in payload'range loop
491
      uniform(seed1, seed2, rand);
492
      int_rand := integer(trunc(rand*256.0));
493
      payload(i)(7 downto 0) := std_logic_vector(to_unsigned(int_rand, 8));
494
      uniform(seed1, seed2, rand);
495
      int_rand := integer(trunc(rand*256.0));
496
      payload(i)(15 downto 8) := std_logic_vector(to_unsigned(int_rand, 8));
497
      uniform(seed1, seed2, rand);
498
      int_rand := integer(trunc(rand*256.0));
499
      payload(i)(23 downto 16) := std_logic_vector(to_unsigned(int_rand, 8));
500
      uniform(seed1, seed2, rand);
501
      int_rand := integer(trunc(rand*256.0));
502
      payload(i)(31 downto 24) := std_logic_vector(to_unsigned(int_rand, 8));
503
      uniform(seed1, seed2, rand);
504
      int_rand := integer(trunc(rand*256.0));
505
      payload(i)(39 downto 32) := std_logic_vector(to_unsigned(int_rand, 8));
506
      uniform(seed1, seed2, rand);
507
      int_rand := integer(trunc(rand*256.0));
508
      payload(i)(47 downto 40) := std_logic_vector(to_unsigned(int_rand, 8));
509
      uniform(seed1, seed2, rand);
510
      int_rand := integer(trunc(rand*256.0));
511
      payload(i)(55 downto 48) := std_logic_vector(to_unsigned(int_rand, 8));
512
      uniform(seed1, seed2, rand);
513
      int_rand := integer(trunc(rand*256.0));
514
      payload(i)(63 downto 56) := std_logic_vector(to_unsigned(int_rand, 8));
515
    end loop;
516
  end procedure;
517
  ---------------------------------------------------------------------------
518
  -- Create a generic RapidIO frame.
519
  ---------------------------------------------------------------------------
520
  function RioFrameCreate(
521
    constant ackId : in std_logic_vector(4 downto 0);
522
    constant vc : in std_logic;
523
    constant crf : in std_logic;
524
    constant prio : in std_logic_vector(1 downto 0);
525
    constant tt : in std_logic_vector(1 downto 0);
526
    constant ftype : in std_logic_vector(3 downto 0);
527
    constant sourceId : in std_logic_vector(15 downto 0);
528
    constant destId : in std_logic_vector(15 downto 0);
529
    constant payload : in RioPayload) return RioFrame is
530
    variable frame : RioFrame;
531
    variable result : HalfwordArray(0 to 137);
532
    variable crc : std_logic_vector(15 downto 0) := x"ffff";
533
  begin
534
    -- Add the header and addresses.
535
    result(0) := ackId & "0" & vc & crf & prio & tt & ftype;
536
    result(1) := destId;
537
    result(2) := sourceId;
538
 
539
    -- Update the calculated CRC with the header contents.
540
    crc := Crc16("00000" & result(0)(10 downto 0), crc);
541
    crc := Crc16(result(1), crc);
542
    crc := Crc16(result(2), crc);
543
 
544
    -- Check if a single CRC should be added or two.
545
    if (payload.length <= 37) then
546
      -- Single CRC.
547
      for i in 0 to payload.length-1 loop
548
        result(i+3) := payload.data(i);
549
        crc := Crc16(payload.data(i), crc);
550
      end loop;
551
      result(payload.length+3) := crc;
552
 
553
      -- Check if paddning is needed to make the payload even 32-bit.
554
      if ((payload.length mod 2) = 1) then
555
        result(payload.length+4) := x"0000";
556
        frame.length := (payload.length+5) / 2;
557
      else
558
        frame.length := (payload.length+4) / 2;
559
      end if;
560
    else
561
      -- Double CRC.
562
      for i in 0 to 36 loop
563
        result(i+3) := payload.data(i);
564
        crc := Crc16(payload.data(i), crc);
565
      end loop;
566
 
567
      -- Add in-the-middle crc.
568
      result(40) := crc;
569
      crc := Crc16(crc, crc);
570
 
571
      for i in 37 to payload.length-1 loop
572
        result(i+4) := payload.data(i);
573
        crc := Crc16(payload.data(i), crc);
574
      end loop;
575
      result(payload.length+4) := crc;
576
 
577
      -- Check if paddning is needed to make the payload even 32-bit.
578
      if ((payload.length mod 2) = 0) then
579
        result(payload.length+5) := x"0000";
580
        frame.length := (payload.length+6) / 2;
581
      else
582
        frame.length := (payload.length+5) / 2;
583
      end if;
584
    end if;
585
 
586
    -- Update the result length.
587
    for i in 0 to frame.length-1 loop
588
      frame.payload(i) := result(2*i) & result(2*i+1);
589
    end loop;
590
 
591
    return frame;
592
  end function;
593
 
594
  -----------------------------------------------------------------------------
595
  -- 
596
  -----------------------------------------------------------------------------
597
  function RioNwrite(
598
    constant wrsize : in std_logic_vector(3 downto 0);
599
    constant address : in std_logic_vector(28 downto 0);
600
    constant wdptr : in std_logic;
601
    constant xamsbs : in std_logic_vector(1 downto 0);
602
    constant dataLength : in natural range 1 to 32;
603
    constant data : in DoublewordArray(0 to 31))
604
    return RioPayload is
605
    variable payload : RioPayload;
606
  begin
607
    payload.data(0)(15 downto 12) := "0100";
608
    payload.data(0)(11 downto 8) := wrsize;
609 45 magro732
    payload.data(0)(7 downto 0) := (others=>'0');
610
 
611
    payload.data(1) := address(28 downto 13);
612
 
613
    payload.data(2)(15 downto 3) := address(12 downto 0);
614
    payload.data(2)(2) := wdptr;
615
    payload.data(2)(1 downto 0) := xamsbs;
616
 
617
    for i in 0 to dataLength-1 loop
618
      payload.data(3+4*i) := data(i)(63 downto 48);
619
      payload.data(4+4*i) := data(i)(47 downto 32);
620
      payload.data(5+4*i) := data(i)(31 downto 16);
621
      payload.data(6+4*i) := data(i)(15 downto 0);
622
    end loop;
623
 
624
    payload.length := 3 + 4*dataLength;
625
 
626
    return payload;
627
  end function;
628
 
629
  -----------------------------------------------------------------------------
630
  -- 
631
  -----------------------------------------------------------------------------
632
  function RioNwriteR(
633
    constant wrsize : in std_logic_vector(3 downto 0);
634
    constant tid : in std_logic_vector(7 downto 0);
635
    constant address : in std_logic_vector(28 downto 0);
636
    constant wdptr : in std_logic;
637
    constant xamsbs : in std_logic_vector(1 downto 0);
638
    constant dataLength : in natural range 1 to 32;
639
    constant data : in DoublewordArray(0 to 31))
640
    return RioPayload is
641
    variable payload : RioPayload;
642
  begin
643
    payload.data(0)(15 downto 12) := "0101";
644
    payload.data(0)(11 downto 8) := wrsize;
645 2 magro732
    payload.data(0)(7 downto 0) := tid;
646
 
647
    payload.data(1) := address(28 downto 13);
648
 
649
    payload.data(2)(15 downto 3) := address(12 downto 0);
650
    payload.data(2)(2) := wdptr;
651
    payload.data(2)(1 downto 0) := xamsbs;
652
 
653
    for i in 0 to dataLength-1 loop
654
      payload.data(3+4*i) := data(i)(63 downto 48);
655
      payload.data(4+4*i) := data(i)(47 downto 32);
656
      payload.data(5+4*i) := data(i)(31 downto 16);
657
      payload.data(6+4*i) := data(i)(15 downto 0);
658
    end loop;
659
 
660
    payload.length := 3 + 4*dataLength;
661
 
662
    return payload;
663
  end function;
664
 
665
  -----------------------------------------------------------------------------
666
  -- 
667
  -----------------------------------------------------------------------------
668
  function RioNread(
669
    constant rdsize : in std_logic_vector(3 downto 0);
670
    constant tid : in std_logic_vector(7 downto 0);
671
    constant address : in std_logic_vector(28 downto 0);
672
    constant wdptr : in std_logic;
673
    constant xamsbs : in std_logic_vector(1 downto 0))
674
    return RioPayload is
675
    variable payload : RioPayload;
676
  begin
677
    payload.data(0)(15 downto 12) := "0100";
678
    payload.data(0)(11 downto 8) := rdsize;
679
    payload.data(0)(7 downto 0) := tid;
680
 
681
    payload.data(1) := address(28 downto 13);
682
 
683
    payload.data(2)(15 downto 3) := address(12 downto 0);
684
    payload.data(2)(2) := wdptr;
685
    payload.data(2)(1 downto 0) := xamsbs;
686
 
687
    payload.length := 3;
688
 
689
    return payload;
690
  end function;
691
 
692
  ---------------------------------------------------------------------------
693
  -- Create a RESPONSE RapidIO frame.
694
  ---------------------------------------------------------------------------
695
  function RioResponse(
696
    constant status : in std_logic_vector(3 downto 0);
697
    constant tid : in std_logic_vector(7 downto 0);
698
    constant dataLength : in natural range 0 to 32;
699
    constant data : in DoublewordArray(0 to 31))
700
    return RioPayload is
701
    variable payload : RioPayload;
702
  begin
703
    payload.data(0)(11 downto 8) := status;
704
    payload.data(0)(7 downto 0) := tid;
705
 
706
    if (dataLength = 0) then
707
      payload.data(0)(15 downto 12) := "0000";
708
      payload.length := 1;
709
    else
710
      payload.data(0)(15 downto 12) := "1000";
711
 
712
      for i in 0 to dataLength-1 loop
713
        payload.data(1+4*i) := data(i)(63 downto 48);
714
        payload.data(2+4*i) := data(i)(47 downto 32);
715
        payload.data(3+4*i) := data(i)(31 downto 16);
716
        payload.data(4+4*i) := data(i)(15 downto 0);
717
      end loop;
718
 
719
      payload.length := 1 + 4*dataLength;
720
    end if;
721
 
722
    return payload;
723
  end function;
724
 
725
  ---------------------------------------------------------------------------
726
  -- Create a Maintenance RapidIO frame.
727
  ---------------------------------------------------------------------------
728
  function RioMaintenance(
729
    constant transaction : in std_logic_vector(3 downto 0);
730
    constant size : in std_logic_vector(3 downto 0);
731
    constant tid : in std_logic_vector(7 downto 0);
732
    constant hopCount : in std_logic_vector(7 downto 0);
733
    constant configOffset : in std_logic_vector(20 downto 0);
734
    constant wdptr : in std_logic;
735
    constant dataLength : in natural range 0 to 8;
736
    constant data : in DoublewordArray(0 to 7))
737
    return RioPayload is
738
    variable payload : RioPayload;
739
  begin
740
    payload.data(0)(15 downto 12) := transaction;
741
    payload.data(0)(11 downto 8) := size;
742
    payload.data(0)(7 downto 0) := tid;
743
 
744
    payload.data(1)(15 downto 8) := hopCount;
745
    payload.data(1)(7 downto 0) := configOffset(20 downto 13);
746
 
747
    payload.data(2)(15 downto 3) := configOffset(12 downto 0);
748
    payload.data(2)(2) := wdptr;
749
    payload.data(2)(1 downto 0) := "00";
750
 
751
    if (dataLength = 0) then
752
      payload.length := 3;
753
    else
754
      for i in 0 to dataLength-1 loop
755
        payload.data(3+4*i) := data(i)(63 downto 48);
756
        payload.data(4+4*i) := data(i)(47 downto 32);
757
        payload.data(5+4*i) := data(i)(31 downto 16);
758
        payload.data(6+4*i) := data(i)(15 downto 0);
759
      end loop;
760
 
761
      payload.length := 3 + 4*dataLength;
762
    end if;
763
 
764
    return payload;
765
  end function;
766
 
767
  -----------------------------------------------------------------------------
768
  -- Function to convert a std_logic_vector to a string.
769
  -----------------------------------------------------------------------------
770
  function byteToString(constant byte : std_logic_vector(7 downto 0))
771
    return string is
772
    constant table : string(1 to 16) := "0123456789abcdef";
773
    variable returnValue : string(1 to 2);
774
  begin
775
    returnValue(1) := table(to_integer(unsigned(byte(7 downto 4))) + 1);
776
    returnValue(2) := table(to_integer(unsigned(byte(3 downto 0))) + 1);
777
    return returnValue;
778
  end function;
779
 
780 40 magro732
  -----------------------------------------------------------------------------
781
  -- Function to print std_logic_vector.
782
  -----------------------------------------------------------------------------
783
  function to_string(constant value : std_logic) return string is
784
    variable s : string(1 to 1);
785
  begin
786
    if (value = '0') then
787
      s(1) := '0';
788
    elsif (value = '1') then
789
      s(1) := '1';
790
    elsif (value = 'U') then
791
      s(1) := 'U';
792
    elsif (value = 'X') then
793
      s(1) := 'X';
794
    else
795
      s(1) := '?';
796
    end if;
797
    return s;
798
  end function;
799
  function to_string(constant value : std_logic_vector) return string is
800
    variable s : string(1 to value'length);
801
    variable index : positive;
802
    variable i : natural;
803
  begin
804
    index := 1;
805
    for i in value'range loop
806
      if (value(i) = '0') then
807
        s(index) := '0';
808
      elsif (value(i) = '1') then
809
        s(index) := '1';
810
      elsif (value(i) = 'U') then
811
        s(index) := 'U';
812
      elsif (value(i) = 'X') then
813
        s(index) := 'X';
814
      else
815
        s(index) := '?';
816
      end if;
817
      index := index + 1;
818
    end loop;
819
    return s;
820
  end function;
821
 
822 2 magro732
  ---------------------------------------------------------------------------
823
  -- Procedure to print test report
824
  ---------------------------------------------------------------------------
825
  procedure PrintR( constant str : string ) is
826
    file reportFile  : text;
827
    variable reportLine, outputLine : line;
828
    variable fStatus: FILE_OPEN_STATUS;
829
  begin
830
    --Write report note to wave/transcript window
831
    report str severity NOTE;
832
  end PrintR;
833
 
834
  ---------------------------------------------------------------------------
835
  -- Procedure to print test spec
836
  ---------------------------------------------------------------------------
837
  procedure PrintS( constant str : string ) is
838
    file specFile  : text;
839
    variable specLine, outputLine : line;
840
    variable fStatus: FILE_OPEN_STATUS;
841
  begin
842
    --Write to spec file
843
    file_open(fStatus, specFile, "testspec.txt", append_mode);
844
    write(specLine, string'(str));
845
    writeline (specFile, specLine);
846
    file_close(specFile);
847
  end PrintS;
848
 
849
  ---------------------------------------------------------------------------
850
  -- Procedure to Assert Expression
851
  ---------------------------------------------------------------------------
852
  procedure AssertE( constant exp: boolean; constant str : string ) is
853
    file reportFile  : text;
854
    variable reportLine, outputLine : line;
855
    variable fStatus: FILE_OPEN_STATUS;
856
  begin
857
    if (not exp) then
858
      --Write to STD_OUTPUT
859
      report(str) severity error;
860
    else
861
      PrintR("Status: Passed");
862
      PrintS("Status: Passed");
863
    end if;
864
  end AssertE;
865
 
866
  ---------------------------------------------------------------------------
867
  -- Procedure to Print Error
868
  ---------------------------------------------------------------------------
869
  procedure PrintE( constant str : string ) is
870
    file reportFile  : text;
871
    variable reportLine, outputLine : line;
872
    variable fStatus: FILE_OPEN_STATUS;
873
  begin
874
    --Write to STD_OUTPUT
875
    report str severity error;
876
  end PrintE;
877
 
878
  ---------------------------------------------------------------------------
879 40 magro732
  -- Procedures to handle tests.
880 2 magro732
  ---------------------------------------------------------------------------
881 40 magro732
 
882
  procedure TestWarning(constant tag : in string) is
883
    variable writeBuffer : line;
884
  begin
885
    write(writeBuffer, now);
886
    write(writeBuffer, string'(":WARNING:"));
887
    write(writeBuffer, tag);
888
    writeline(OUTPUT, writeBuffer);
889
  end procedure;
890
 
891
  procedure TestError(constant tag : in string;
892
                      constant stopAtError : in boolean := true) is
893
    variable writeBuffer : line;
894
  begin
895
    write(writeBuffer, now);
896
    write(writeBuffer, string'(":FAILED:"));
897
    write(writeBuffer, tag);
898
    writeline(OUTPUT, writeBuffer);
899
 
900
    if (stopAtError) then
901
      std.env.stop(0);
902
    end if;
903
  end procedure;
904
 
905
  procedure TestCompare(constant expression : in boolean;
906
                        constant tag : in string := "";
907
                        constant stopAtError : in boolean := true) is
908
    variable writeBuffer : line;
909
  begin
910
    write(writeBuffer, now);
911
    if (not expression) then
912
      write(writeBuffer, string'(":FAILED:"));
913
    else
914
      write(writeBuffer, string'(":PASSED:"));
915
    end if;
916
    write(writeBuffer, tag);
917
    writeline(OUTPUT, writeBuffer);
918
 
919
    if (stopAtError) and (not expression) then
920
      std.env.stop(0);
921
    end if;
922
  end procedure;
923
 
924
  procedure TestCompare(constant got : in std_logic;
925
                        constant expected : in std_logic;
926
                        constant tag : in string := "";
927
                        constant stopAtError : in boolean := true) is
928
    variable writeBuffer : line;
929
  begin
930
    write(writeBuffer, now);
931
    if (expected /= got) then
932
      write(writeBuffer, string'(":FAILED:"));
933
      write(writeBuffer, tag);
934
      write(writeBuffer, ":got=" & to_string(got));
935
      write(writeBuffer, ":expected=" & to_string(expected));
936
    else
937
      write(writeBuffer, string'(":PASSED:"));
938
      write(writeBuffer, tag);
939
    end if;
940
    writeline(OUTPUT, writeBuffer);
941
 
942
    if (stopAtError) and (expected /= got) then
943
      std.env.stop(0);
944
    end if;
945
  end procedure;
946
 
947
  procedure TestCompare(constant got : in std_logic_vector;
948
                        constant expected : in std_logic_vector;
949
                        constant tag : in string := "";
950
                        constant stopAtError : in boolean := true) is
951
    variable writeBuffer : line;
952
  begin
953
    write(writeBuffer, now);
954
    if (expected /= got) then
955
      write(writeBuffer, string'(":FAILED:"));
956
      write(writeBuffer, tag);
957
      write(writeBuffer, ":got=" & to_string(got));
958
      write(writeBuffer, ":expected=" & to_string(expected));
959
    else
960
      write(writeBuffer, string'(":PASSED:"));
961
      write(writeBuffer, tag);
962
    end if;
963
    writeline(OUTPUT, writeBuffer);
964
 
965
    if (stopAtError) and (expected /= got) then
966
      std.env.stop(0);
967
    end if;
968
  end procedure;
969
 
970
  procedure TestCompare(constant got : in natural;
971
                        constant expected : in natural;
972
                        constant tag : in string := "";
973
                        constant stopAtError : in boolean := true) is
974
    variable writeBuffer : line;
975
  begin
976
    write(writeBuffer, now);
977
    if (expected /= got) then
978
      write(writeBuffer, string'(":FAILED:"));
979
      write(writeBuffer, tag);
980
      write(writeBuffer, ":got=" & integer'image(got));
981
      write(writeBuffer, ":expected=" & integer'image(expected));
982
    else
983
      write(writeBuffer, string'(":PASSED:"));
984
      write(writeBuffer, tag);
985
    end if;
986
    writeline(OUTPUT, writeBuffer);
987
 
988
    if (stopAtError) and (expected /= got) then
989
      std.env.stop(0);
990
    end if;
991
  end procedure;
992
 
993
  procedure TestCompare(constant got : in time;
994
                        constant expected : in time;
995
                        constant tag : in string := "";
996
                        constant stopAtError : in boolean := true) is
997
    variable writeBuffer : line;
998
  begin
999
    write(writeBuffer, now);
1000
    if (expected /= got) then
1001
      write(writeBuffer, string'(":FAILED:"));
1002
      write(writeBuffer, tag);
1003
      write(writeBuffer, string'(":got="));
1004
      write(writeBuffer, got);
1005
      write(writeBuffer, string'(":expected="));
1006
      write(writeBuffer, expected);
1007
    else
1008
      write(writeBuffer, string'(":PASSED:"));
1009
      write(writeBuffer, tag);
1010
    end if;
1011
    writeline(OUTPUT, writeBuffer);
1012
 
1013
    if (stopAtError) and (expected /= got) then
1014
      std.env.stop(0);
1015
    end if;
1016
  end procedure;
1017
 
1018
  procedure TestWait(signal waitSignal : in std_logic;
1019
                     constant waitValue : in std_logic;
1020
                     constant tag : in string := "";
1021
                     constant waitTime : in time := 1 ms;
1022
                     constant stopAtError : in boolean := true) is
1023
    variable writeBuffer : line;
1024
  begin
1025
    if (waitSignal /= waitValue) then
1026
      wait until waitSignal = waitValue for waitTime;
1027
      if (waitSignal /= waitValue) then
1028
        write(writeBuffer, now);
1029
        write(writeBuffer, string'(":FAILED:"));
1030
        write(writeBuffer, tag);
1031
        writeline(OUTPUT, writeBuffer);
1032
 
1033
        if (stopAtError) then
1034
          std.env.stop(0);
1035
        end if;
1036
      end if;
1037
    end if;
1038
  end procedure;
1039
 
1040
  procedure TestWait(signal waitSignal : in std_logic;
1041
                     constant waitValue : in std_logic;
1042
                     signal ackSignal : inout std_logic;
1043
                     constant tag : in string := "";
1044
                     constant waitTime : in time := 1 ms;
1045
                     constant stopAtError : in boolean := true) is
1046
    variable writeBuffer : line;
1047
  begin
1048
    if (waitSignal /= waitValue) then
1049
 
1050
      wait until waitSignal = waitValue for waitTime;
1051
 
1052
      if (waitSignal /= waitValue) then
1053
        write(writeBuffer, now);
1054
        write(writeBuffer, string'(":FAILED:"));
1055
        write(writeBuffer, tag);
1056
        writeline(OUTPUT, writeBuffer);
1057
 
1058
        if (stopAtError) then
1059
          std.env.stop(0);
1060
        end if;
1061
      end if;
1062
    end if;
1063
 
1064
    ackSignal <= not ackSignal;
1065
 
1066
    wait until waitSignal /= waitValue for waitTime;
1067
 
1068
    if (waitSignal = waitValue) then
1069
      write(writeBuffer, now);
1070
      write(writeBuffer, string'(":FAILED:"));
1071
      write(writeBuffer, tag);
1072
      writeline(OUTPUT, writeBuffer);
1073
 
1074
      if (stopAtError) then
1075
        std.env.stop(0);
1076
      end if;
1077
    end if;
1078
  end procedure;
1079
 
1080 2 magro732
  procedure TestEnd is
1081 40 magro732
    variable writeBuffer : line;
1082 2 magro732
  begin
1083 40 magro732
    write(writeBuffer, now);
1084
    write(writeBuffer, string'(":COMPLETED"));
1085
    writeline(OUTPUT, writeBuffer);
1086
    std.env.stop(0);
1087 2 magro732
  end TestEnd;
1088 40 magro732
 
1089 2 magro732
end rio_common;
1090
 
1091
 
1092
 
1093
-------------------------------------------------------------------------------
1094
-- Crc16CITT
1095
-- A CRC-16 calculator following the implementation proposed in the 2.2
1096
-- standard.
1097
-------------------------------------------------------------------------------
1098
library ieee;
1099
use ieee.std_logic_1164.all;
1100
 
1101
 
1102
-------------------------------------------------------------------------------
1103
-- Entity for Crc16CITT.
1104
-------------------------------------------------------------------------------
1105
entity Crc16CITT is
1106
  port(
1107
    d_i : in  std_logic_vector(15 downto 0);
1108
    crc_i : in std_logic_vector(15 downto 0);
1109
    crc_o : out std_logic_vector(15 downto 0));
1110
end entity;
1111
 
1112
 
1113
-------------------------------------------------------------------------------
1114
-- Architecture for Crc16CITT.
1115
-------------------------------------------------------------------------------
1116
architecture Crc16Impl of Crc16CITT is
1117
  signal d : std_logic_vector(0 to 15);
1118
  signal c : std_logic_vector(0 to 15);
1119
  signal e : std_logic_vector(0 to 15);
1120
  signal cc : std_logic_vector(0 to 15);
1121
begin
1122
 
1123
  -- Reverse the bit vector indexes to make them the same as in the standard.
1124
  d(15) <= d_i(0); d(14) <= d_i(1); d(13) <= d_i(2); d(12) <= d_i(3);
1125
  d(11) <= d_i(4); d(10) <= d_i(5); d(9) <= d_i(6); d(8) <= d_i(7);
1126
  d(7) <= d_i(8); d(6) <= d_i(9); d(5) <= d_i(10); d(4) <= d_i(11);
1127
  d(3) <= d_i(12); d(2) <= d_i(13); d(1) <= d_i(14); d(0) <= d_i(15);
1128
 
1129
  -- Reverse the bit vector indexes to make them the same as in the standard.
1130
  c(15) <= crc_i(0); c(14) <= crc_i(1); c(13) <= crc_i(2); c(12) <= crc_i(3);
1131
  c(11) <= crc_i(4); c(10) <= crc_i(5); c(9) <= crc_i(6); c(8) <= crc_i(7);
1132
  c(7) <= crc_i(8); c(6) <= crc_i(9); c(5) <= crc_i(10); c(4) <= crc_i(11);
1133
  c(3) <= crc_i(12); c(2) <= crc_i(13); c(1) <= crc_i(14); c(0) <= crc_i(15);
1134
 
1135
  -- Calculate the resulting crc.
1136
  e <= c xor d;
1137
  cc(0) <= e(4) xor e(5) xor e(8) xor e(12);
1138
  cc(1) <= e(5) xor e(6) xor e(9) xor e(13);
1139
  cc(2) <= e(6) xor e(7) xor e(10) xor e(14);
1140
  cc(3) <= e(0) xor e(7) xor e(8) xor e(11) xor e(15);
1141
  cc(4) <= e(0) xor e(1) xor e(4) xor e(5) xor e(9);
1142
  cc(5) <= e(1) xor e(2) xor e(5) xor e(6) xor e(10);
1143
  cc(6) <= e(0) xor e(2) xor e(3) xor e(6) xor e(7) xor e(11);
1144
  cc(7) <= e(0) xor e(1) xor e(3) xor e(4) xor e(7) xor e(8) xor e(12);
1145
  cc(8) <= e(0) xor e(1) xor e(2) xor e(4) xor e(5) xor e(8) xor e(9) xor e(13);
1146
  cc(9) <= e(1) xor e(2) xor e(3) xor e(5) xor e(6) xor e(9) xor e(10) xor e(14);
1147
  cc(10) <= e(2) xor e(3) xor e(4) xor e(6) xor e(7) xor e(10) xor e(11) xor e(15);
1148
  cc(11) <= e(0) xor e(3) xor e(7) xor e(11);
1149
  cc(12) <= e(0) xor e(1) xor e(4) xor e(8) xor e(12);
1150
  cc(13) <= e(1) xor e(2) xor e(5) xor e(9) xor e(13);
1151
  cc(14) <= e(2) xor e(3) xor e(6) xor e(10) xor e(14);
1152
  cc(15) <= e(3) xor e(4) xor e(7) xor e(11) xor e(15);
1153
 
1154
  -- Reverse the bit vector indexes to make them the same as in the standard.
1155
  crc_o(15) <= cc(0); crc_o(14) <= cc(1); crc_o(13) <= cc(2); crc_o(12) <= cc(3);
1156
  crc_o(11) <= cc(4); crc_o(10) <= cc(5); crc_o(9) <= cc(6); crc_o(8) <= cc(7);
1157
  crc_o(7) <= cc(8); crc_o(6) <= cc(9); crc_o(5) <= cc(10); crc_o(4) <= cc(11);
1158
  crc_o(3) <= cc(12); crc_o(2) <= cc(13); crc_o(1) <= cc(14); crc_o(0) <= cc(15);
1159
 
1160
end architecture;
1161
 
1162
 
1163
 
1164
-------------------------------------------------------------------------------
1165
-- MemoryDualPort
1166
-- Generic synchronous memory with one read/write port and one read port.
1167
-------------------------------------------------------------------------------
1168
 
1169
library ieee;
1170
use ieee.std_logic_1164.all;
1171
use ieee.numeric_std.all;
1172
 
1173
 
1174
-------------------------------------------------------------------------------
1175
-- Entity for MemoryDualPort.
1176
-------------------------------------------------------------------------------
1177
entity MemoryDualPort is
1178
  generic(
1179
    ADDRESS_WIDTH : natural := 1;
1180
    DATA_WIDTH : natural := 1);
1181
  port(
1182
    clkA_i : in std_logic;
1183
    enableA_i : in std_logic;
1184
    writeEnableA_i : in std_logic;
1185
    addressA_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
1186
    dataA_i : in std_logic_vector(DATA_WIDTH-1 downto 0);
1187
    dataA_o : out std_logic_vector(DATA_WIDTH-1 downto 0);
1188
 
1189
    clkB_i : in std_logic;
1190
    enableB_i : in std_logic;
1191
    addressB_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
1192
    dataB_o : out std_logic_vector(DATA_WIDTH-1 downto 0));
1193
end entity;
1194
 
1195
 
1196
-------------------------------------------------------------------------------
1197
-- Architecture for MemoryDualPort.
1198
-------------------------------------------------------------------------------
1199
architecture MemoryDualPortImpl of MemoryDualPort is
1200
  type MemoryType is array (natural range <>) of
1201
    std_logic_vector(DATA_WIDTH-1 downto 0);
1202
 
1203
  signal memory : MemoryType(0 to (2**ADDRESS_WIDTH)-1);
1204
 
1205
begin
1206
  process(clkA_i)
1207
  begin
1208
    if (clkA_i'event and clkA_i = '1') then
1209
      if (enableA_i = '1') then
1210
        if (writeEnableA_i = '1') then
1211
          memory(to_integer(unsigned(addressA_i))) <= dataA_i;
1212
        end if;
1213
 
1214
        dataA_o <= memory(to_integer(unsigned(addressA_i)));
1215
      end if;
1216
    end if;
1217
  end process;
1218
 
1219
  process(clkB_i)
1220
  begin
1221
    if (clkB_i'event and clkB_i = '1') then
1222
      if (enableB_i = '1') then
1223
        dataB_o <= memory(to_integer(unsigned(addressB_i)));
1224
      end if;
1225
    end if;
1226
  end process;
1227
 
1228
end architecture;
1229
 
1230
 
1231
 
1232
-------------------------------------------------------------------------------
1233
-- MemorySimpleDualPort
1234
-- Generic synchronous memory with one write port and one read port.
1235
-------------------------------------------------------------------------------
1236
 
1237
library ieee;
1238
use ieee.std_logic_1164.all;
1239
use ieee.numeric_std.all;
1240
 
1241
 
1242
-------------------------------------------------------------------------------
1243
-- Entity for MemorySimpleDualPort.
1244
-------------------------------------------------------------------------------
1245
entity MemorySimpleDualPort is
1246
  generic(
1247
    ADDRESS_WIDTH : natural := 1;
1248
    DATA_WIDTH : natural := 1);
1249
  port(
1250
    clkA_i : in std_logic;
1251
    enableA_i : in std_logic;
1252
    addressA_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
1253
    dataA_i : in std_logic_vector(DATA_WIDTH-1 downto 0);
1254
 
1255
    clkB_i : in std_logic;
1256
    enableB_i : in std_logic;
1257
    addressB_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
1258
    dataB_o : out std_logic_vector(DATA_WIDTH-1 downto 0));
1259
end entity;
1260
 
1261
 
1262
-------------------------------------------------------------------------------
1263
-- Architecture for MemorySimpleDualPort.
1264
-------------------------------------------------------------------------------
1265
architecture MemorySimpleDualPortImpl of MemorySimpleDualPort is
1266
  type MemoryType is array (natural range <>) of
1267
    std_logic_vector(DATA_WIDTH-1 downto 0);
1268
 
1269
  signal memory : MemoryType(0 to (2**ADDRESS_WIDTH)-1);
1270
 
1271
begin
1272
  process(clkA_i)
1273
  begin
1274
    if (clkA_i'event and clkA_i = '1') then
1275
      if (enableA_i = '1') then
1276
        memory(to_integer(unsigned(addressA_i))) <= dataA_i;
1277
      end if;
1278
    end if;
1279
  end process;
1280
 
1281
  process(clkB_i)
1282
  begin
1283
    if (clkB_i'event and clkB_i = '1') then
1284
      if (enableB_i = '1') then
1285
        dataB_o <= memory(to_integer(unsigned(addressB_i)));
1286
      end if;
1287
    end if;
1288
  end process;
1289
 
1290
end architecture;
1291
 
1292
 
1293
 
1294
-------------------------------------------------------------------------------
1295
-- MemorySinglePort
1296
-- Generic synchronous memory with one read/write port.
1297
-------------------------------------------------------------------------------
1298
 
1299
library ieee;
1300
use ieee.std_logic_1164.all;
1301
use ieee.numeric_std.all;
1302
 
1303
 
1304
-------------------------------------------------------------------------------
1305
-- Entity for MemorySinglePort.
1306
-------------------------------------------------------------------------------
1307
entity MemorySinglePort is
1308
  generic(
1309
    ADDRESS_WIDTH : natural := 1;
1310
    DATA_WIDTH : natural := 1);
1311
  port(
1312
    clk_i : in std_logic;
1313
    enable_i : in std_logic;
1314
    writeEnable_i : in std_logic;
1315
    address_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
1316
    data_i : in std_logic_vector(DATA_WIDTH-1 downto 0);
1317
    data_o : out std_logic_vector(DATA_WIDTH-1 downto 0));
1318
end entity;
1319
 
1320
 
1321
-------------------------------------------------------------------------------
1322
-- Architecture for MemorySinglePort.
1323
-------------------------------------------------------------------------------
1324
architecture MemorySinglePortImpl of MemorySinglePort is
1325
  type MemoryType is array (natural range <>) of
1326
    std_logic_vector(DATA_WIDTH-1 downto 0);
1327
 
1328
  signal memory : MemoryType(0 to (2**ADDRESS_WIDTH)-1);
1329
 
1330
begin
1331
  process(clk_i)
1332
  begin
1333
    if (clk_i'event and clk_i = '1') then
1334
      if (enable_i = '1') then
1335
        if (writeEnable_i = '1') then
1336
          memory(to_integer(unsigned(address_i))) <= data_i;
1337
        end if;
1338
 
1339
        data_o <= memory(to_integer(unsigned(address_i)));
1340
      end if;
1341
    end if;
1342
  end process;
1343
 
1344
end architecture;
1345
 
1346
 
1347
 
1348
 
1349
-------------------------------------------------------------------------------
1350
-- MemorySimpleDualPortAsync
1351
-- Generic memory with one synchronous write port and one asynchronous read port.
1352
-------------------------------------------------------------------------------
1353
 
1354
library ieee;
1355
use ieee.std_logic_1164.all;
1356
use ieee.numeric_std.all;
1357
 
1358
 
1359
-------------------------------------------------------------------------------
1360
-- Entity for MemorySimpleDualPortAsync.
1361
-------------------------------------------------------------------------------
1362
entity MemorySimpleDualPortAsync is
1363
  generic(
1364
    ADDRESS_WIDTH : natural := 1;
1365
    DATA_WIDTH : natural := 1;
1366
    INIT_VALUE : std_logic := 'U');
1367
  port(
1368
    clkA_i : in std_logic;
1369
    enableA_i : in std_logic;
1370
    addressA_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
1371
    dataA_i : in std_logic_vector(DATA_WIDTH-1 downto 0);
1372
 
1373
    addressB_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
1374
    dataB_o : out std_logic_vector(DATA_WIDTH-1 downto 0));
1375
end entity;
1376
 
1377
 
1378
-------------------------------------------------------------------------------
1379
-- Architecture for MemorySimpleDualPortAsync.
1380
-------------------------------------------------------------------------------
1381
architecture MemorySimpleDualPortAsyncImpl of MemorySimpleDualPortAsync is
1382
  type MemoryType is array (natural range <>) of
1383
    std_logic_vector(DATA_WIDTH-1 downto 0);
1384
 
1385
  signal memory : MemoryType(0 to (2**ADDRESS_WIDTH)-1) := (others=>(others=>INIT_VALUE));
1386
 
1387
begin
1388
  process(clkA_i)
1389
  begin
1390
    if (clkA_i'event and clkA_i = '1') then
1391
      if (enableA_i = '1') then
1392
        memory(to_integer(unsigned(addressA_i))) <= dataA_i;
1393
      end if;
1394
    end if;
1395
  end process;
1396
 
1397
  dataB_o <= memory(to_integer(unsigned(addressB_i)));
1398
 
1399
end architecture;
1400
 
1401
 
1402
 
1403
 
1404
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.