VHDL-verification
Package to ease directed testing of HDL entities
datawrite.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 --! @file
3 --! @author Hipolito Guzman-Miranda
4 --! @brief Writes circuit output to file
5 -------------------------------------------------------------------------------
6 
7 --! Use IEEE standard definitions library
8 library ieee;
9 --! Use std_logic* signal types
10 use ieee.std_logic_1164.all;
11 --! Allows use of arithmetical operations between integers and vectors
12 use ieee.numeric_std.all;
13 -- Allows writing strings to lines and lines to files
14 use STD.textio.all;
15 -- Allows converting std_logic_vector(s) to strings (BIN, HEX, OCT)
16 --use work.image_pkg.all;
17 -- Allows writing std_logic_vector(s) to line(s) in BIN, HEX, OCT and reading BIN, HEX, OCT vector(s) from line(s)
18 use ieee.std_logic_textio.all;
19 -- For print() function
20 use work.txt_util.all;
21 
22 --! @brief Writes circuit output data to file
23 --!
24 --! @detailed Creates (overwriting if it exists) a file at \c OUTPUT_FILE,
25 --! writing \c OUTPUT_NIBBLES chars in each line. Data will be written in hex
26 --! format: 4 bits per character (nibble). Reads \c DATA_WIDTH - bit input \c data
27 --! and internally reorders it to form complete lines. \c data is only sampled
28 --! when \c valid is active.
29 entity datawrite is
30  generic(
31  SIMULATION_LABEL : string := "datawrite"; --! Allow to separate messages from different instances in SIMULATION
32  VERBOSE : boolean := false; --! Print more internal details
33  DEBUG : boolean := false; --! Print debug info (developers only)
34  OUTPUT_FILE : string := "./output/datawrite_test.txt"; --! File where data will be stored
35  OUTPUT_NIBBLES : integer := 2; --! Hex chars on each output line
36  DATA_WIDTH : integer := 8 --! Width of input data
37  );
38  port(
39  clk : in std_logic; --! Will sample input on rising_edge of this clock
40  data : in std_logic_vector (DATA_WIDTH-1 downto 0); --! Data to write to file
41  valid : in std_logic; --! Active high, indicates data is valid
42  endsim : in std_logic --! Active high, tells the process to close its open files
43  );
44 end datawrite;
45 
46 --! @brief Architecture accumulates input data in a vector which will be written to the output file
47 architecture pack_lines_and_write of datawrite is
48 
49  constant NUM_CHUNKS : integer := 4*OUTPUT_NIBBLES / DATA_WIDTH; --! Each line in output file equals to NUM_CHUNKS data of DATA_WIDTH
50 
51  signal received_data : integer := 0;
52 
53 begin
54 
55  assert OUTPUT_NIBBLES > 0
56  report "datawrite(" & SIMULATION_LABEL & "): OUTPUT_NIBBLES must be a positive non-zero integer"
57  severity failure;
58 
59  assert DATA_WIDTH > 0
60  report "datawrite(" & SIMULATION_LABEL & "): DATA_WIDTH must be a positive non-zero integer"
61  severity failure;
62 
63  assert 4*OUTPUT_NIBBLES >= DATA_WIDTH
64  report "datawrite(" & SIMULATION_LABEL & "): DATA_WIDTH (" & integer'image(DATA_WIDTH) & ") bits must fit into OUTPUT_NIBBLES nibbles (4*" & integer'image(OUTPUT_NIBBLES) & ") bits"
65  severity failure;
66 
67  assert (4*OUTPUT_NIBBLES) mod DATA_WIDTH = 0
68  report "datawrite(" & SIMULATION_LABEL & "): An exact multiple of DATA_WIDTH (" & integer'image(DATA_WIDTH) & ") must fit into OUTPUT_NIBBLES nibbles (4*" & integer'image(OUTPUT_NIBBLES) & ") bits."
69  severity failure;
70 
71  --! @brief Accumulates data to form a line, when line is complete it gets written to the output file
72  --!
73  --! @detailed Also reports errors and checks for unexpected conditions
74  datawrite_read : process
75 
76  file file_pointer : text;
77  variable current_line : line;
78  variable expected_data : std_logic_vector (OUTPUT_NIBBLES*4-1 downto 0); -- Data read from file
79  variable current_data : std_logic_vector (OUTPUT_NIBBLES*4-1 downto 0); -- Data read from input
80  variable chunk_idx : integer range 0 to NUM_CHUNKS := 0; -- Points to current data chunk in line
81  variable error_count : integer := 0; -- Store differences between received and expected data
82  variable correct_count : integer := 0; -- Store number of correct data
83 
84  begin
85 
86  print ("datawrite(" & SIMULATION_LABEL & "): NUM_CHUNKS: " & integer'image(NUM_CHUNKS));
87  print ("datawrite(" & SIMULATION_LABEL & "): opening output file " & OUTPUT_FILE);
88  file_open(file_pointer, OUTPUT_FILE, WRITE_MODE);
89 
90  while (endsim /= '1') loop
91 
92  print (DEBUG, "datawrite(" & SIMULATION_LABEL & "): composing line");
93 
94  while (chunk_idx < NUM_CHUNKS and endsim /= '1') loop
95  wait until (rising_edge(clk) or endsim = '1');
96  if (valid = '1') then
97  print (DEBUG, "datawrite(" & SIMULATION_LABEL & "): chunk_idx: " & integer'image(chunk_idx));
98  current_data(DATA_WIDTH*(chunk_idx+1)-1 downto DATA_WIDTH*chunk_idx) := data; -- Put input data in the correct chunk
99  chunk_idx := chunk_idx + 1;
100  received_data <= received_data + 1;
101  end if;
102  end loop;
103 
104  if (chunk_idx /= NUM_CHUNKS and chunk_idx /= 0) then
105  print ("datawrite(" & SIMULATION_LABEL & "): warning: endsim received whilst line not completed. (chunk_idx = " & integer'image(chunk_idx) & "). Some data may be lost" );
106  end if;
107 
108  -- Avoid writing twice the last line
109  if (endsim /= '1') then
110  print (DEBUG, "datawrite(" & SIMULATION_LABEL & "): writing line");
111  hwrite(current_line, current_data);
112  writeline(file_pointer, current_line);
113  end if;
114 
115  current_data := (others => 'U');
116  chunk_idx := 0;
117 
118  end loop;
119 
120  print (VERBOSE, "datawrite(" & SIMULATION_LABEL & "): " & integer'image(received_data) & " data received");
121  print (VERBOSE, "datawrite(" & SIMULATION_LABEL & "): Closing output file");
122 
123  file_close(file_pointer);
124 
125  wait;
126 
127  end process datawrite_read;
128 
129 end pack_lines_and_write;
130 
in datastd_logic_vector( DATA_WIDTH- 1 downto 0)
Data to write to file.
Definition: datawrite.vhd:40
DATA_WIDTHinteger := 8
Width of input data.
Definition: datawrite.vhd:37
SIMULATION_LABELstring := "datawrite"
Allow to separate messages from different instances in SIMULATION.
Definition: datawrite.vhd:31
OUTPUT_FILEstring := "./output/datawrite_test.txt"
File where data will be stored.
Definition: datawrite.vhd:34
Writes circuit output data to file.
Definition: datawrite.vhd:29
in validstd_logic
Active high, indicates data is valid.
Definition: datawrite.vhd:41
VERBOSEboolean := false
Print more internal details.
Definition: datawrite.vhd:32
in endsimstd_logic
Active high, tells the process to close its open files.
Definition: datawrite.vhd:43
integer := 4* OUTPUT_NIBBLES/ DATA_WIDTH NUM_CHUNKS
Each line in output file equals to NUM_CHUNKS data of DATA_WIDTH.
Definition: datawrite.vhd:49
OUTPUT_NIBBLESinteger := 2
Hex chars on each output line.
Definition: datawrite.vhd:35
in clkstd_logic
Will sample input on rising_edge of this clock.
Definition: datawrite.vhd:39
Allows for text output in simulation.
Definition: txt_util.vhd:12
DEBUGboolean := false
Print debug info (developers only)
Definition: datawrite.vhd:33
_library_ ieeeieee
Use IEEE standard definitions library.
Definition: datagen.vhd:8