VHDL-verification
Package to ease directed testing of HDL entities
Constants | Signals | Processes
uart_emu_arch Architecture Reference

Architecture is based on just a single process. More...

Detailed Description

Architecture is based on just a single process.

An assertion has also been added to check that generics are assigned supported values.

Definition at line 49 of file uart_emu.vhd.

Processes

get_values  ( )
 Decode uart_input signal and write it to OUTPUT_FILE.

Constants

LINE_FEED  std_logic_vector ( 7 downto 0 ) := x " 0A "

Signals

rxparity  std_logic
expectedparity  std_logic

Member Function Documentation

◆ get_values()

get_values ( )
Process

Decode uart_input signal and write it to OUTPUT_FILE.

Assuming the line is in the rest value ('1') when we are outside of the process, when we enter the process it should be because the line changed to the start bit ('0'). From that point, get all the data values and the parity, performing some sanity checks. Please note that this process does not check for signal stability (we will not raise any errors if the signals change a bit sooner or later than BIT_DURATION), we just measure the signal at half the bit time, as a real UART receiver would do.

Definition at line 79 of file uart_emu.vhd.

get_values: process
79 
80  file f : text;
81  variable l : line;
82  variable rxdata : std_logic_vector(DATA_BITS-1 downto 0);
83 
84  begin
85 
86  -- A transaction start when the line goes to a strong low level
87  wait until uart_input = '0';
88  if VERBOSE then
89  report "uart_emu: uart transaction begin";
90  end if;
91 
92  -- Offset half a bit so we measure at the center of each bit
93  wait for BIT_DURATION/2;
94 
95  -- Raise an error if we are not in the start bit
96  if (uart_input /= '0') then
97  report "uart_emu: wrong value for start bit: expected: '0', actual: " & std_logic'image(uart_input) severity failure;
98  end if;
99 
100  -- Get all data bits
101  for i in 0 to DATA_BITS-1 loop
102  wait for BIT_DURATION;
103  rxdata(i) := uart_input;
104  if VERBOSE then
105  report "uart_emu: sampled bit " & integer'image(i) & ", got value: " & std_logic'image(uart_input);
106  end if;
107  end loop;
108 
109  -- Print received data. If DATA_BITS = 8, print it also in ASCII
110  if DATA_BITS = 8 then
111  report "uart_emu: received data: " & slv2string(rxdata) & " (bin), " & slv2hexstring(rxdata) & " (hex), " & ascii(to_integer(unsigned(rxdata))) & " (ASCII)";
112  else
113  report "uart_emu: received data: " & slv2string(rxdata) & " (bin), " & slv2hexstring(rxdata) & " (hex)";
114  end if;
115 
116  -- If DATA_BITS = 8, write received data to output file. Data is always
117  -- prepared to be written to the output file, no matter if we have parity
118  -- errors or not.
119  --
120  -- We write the received char into the line variable, and not directly
121  -- into the file, unless we got a newline char (line feed, \n). Only when
122  -- we receive a newline char, we commit the line to the file.
123  --
124  -- This would probably be more convenient if we wrote to a pipe instead
125  -- of writing to a file, but I am not sure if that would work on Windows.
126  --
127  -- Since the file output seems to be buffered, normally it would not be
128  -- actually written to the log until we close the file, which should
129  -- happen automatically at the end of the simulation. But in that case
130  -- we would get all the text at the same time, instead of line by line as
131  -- the simulation progresses. So we will open and close the file each
132  -- time we have a complete line.
133  if DATA_BITS = 8 then
134  if rxdata /= LINE_FEED then
135  write (l, ascii(to_integer(unsigned(rxdata))));
136  else
137  file_open(f, OUTPUT_FILE, append_mode);
138  writeline (f, l);
139  file_close(f);
140  end if;
141  end if;
142 
143  -- If parity is even or odd, get parity bit
144  if PARITY /= "none" then
145  wait for BIT_DURATION;
146  rxparity <= uart_input;
147  end if;
148 
149  -- Calculate expected parity
150  if PARITY = "even" then
151  expectedparity <= xor_reduce(rxdata);
152  elsif PARITY = "odd" then
153  expectedparity <= xnor_reduce(rxdata);
154  else
155  expectedparity <= '-';
156  end if;
157 
158  -- Check if received parity matches expected, report an error if not
159  if PARITY /= "none" then
160  if rxparity /= expectedparity then
161  report "uart_emu: parity error! received parity bit: " & std_logic'image(rxparity) & " expected: " & std_logic'image(expectedparity)
162  severity error;
163  end if;
164  end if;
165 
166  -- Wait for stop bits, and raise errors if the value is incorrect.
167  -- We always will have at least one stop bit
168  wait for BIT_DURATION;
169  if (uart_input /= '1') then
170  report "uart_emu: wrong value for stop bit: expected: '1', actual: " & std_logic'image(uart_input) severity failure;
171  end if;
172 
173  -- But sometimes we will have a second stop bit
174  if STOP_BITS = 2 then
175  wait for BIT_DURATION;
176  if (uart_input /= '1') then
177  report "uart_emu: wrong value for second stop bit: expected: '1', actual: " & std_logic'image(uart_input) severity failure;
178  end if;
179  end if;
180 
181  -- We could do a last half-bit shift before we exit the process, but since
182  -- we will wait for an event on uart_input when we enter the process again,
183  -- that half-bit wait will be performed nevertheless.
184  -- Also, by not manualy waiting for a half-bit, we avoid missing the start
185  -- bit event on uart_input in the case of receiving a new transaction just
186  -- after the last stop bit.
187 
188  if VERBOSE then
189  report "uart_emu: uart transaction end";
190  end if;
191 
192  end process;
193 
in uart_inputstd_logic
Connect here the TX signal from your design.
Definition: uart_emu.vhd:42
PARITYstring := "none"
Can be either "even", "odd", or "none".
Definition: uart_emu.vhd:36
STOP_BITSinteger := 1
Can be either 1 or 2.
Definition: uart_emu.vhd:39
DATA_BITSinteger := 8
Number of data bits in the UART word.
Definition: uart_emu.vhd:35
VERBOSEboolean := false
Log beginning and end of transactions, as well as individual bit values.
Definition: uart_emu.vhd:34
BIT_DURATIONtime := 104 us
Duration of each bit, depends on baudrate (it is actually 1 second / baudrate)
Definition: uart_emu.vhd:37

The documentation for this class was generated from the following file: