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

Subversion Repositories uart2bus

[/] [uart2bus/] [trunk/] [vhdl/] [bench/] [uart2BusTop_bin_tb.vhd] - Blame information for rev 11

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

Line No. Rev Author Line
1 6 smuller
-----------------------------------------------------------------------------------------
2
-- uart test bench   
3
--
4
-----------------------------------------------------------------------------------------
5
use std.textio.all;
6 11 smuller
 
7 6 smuller
library ieee;
8
use ieee.std_logic_1164.all;
9
use ieee.std_logic_unsigned.all;
10
use ieee.numeric_std.all;
11
use ieee.std_logic_textio.all;
12
 
13 11 smuller
library work;
14
use work.uart2BusTop_pkg.all;
15
use work.helpers_pkg.all;
16
 
17 6 smuller
-----------------------------------------------------------------------------------------
18
-- test bench implementation 
19
entity uart2BusTop_bin_tb is
20
end uart2BusTop_bin_tb;
21
 
22
architecture behavior of uart2BusTop_bin_tb is
23
 
24
  procedure sendSerial(data : integer; baud : in real; parity : in integer; stopbit : in real; bitnumber : in integer; baudError : in real; signal txd : inout std_logic) is
25
 
26
    variable shiftreg : std_logic_vector(7 downto 0);
27
    variable bitTime  : time;
28
 
29
    begin
30
      bitTime := 1000 ms / (baud + baud * baudError / 100.0);
31
      shiftreg := std_logic_vector(to_unsigned(data, shiftreg'length));
32
      txd <= '0';
33
      wait for bitTime;
34
      for index in 0 to bitnumber loop
35
        txd <= shiftreg(index);
36
        wait for bitTime;
37
      end loop;
38
      txd <= '1';
39
      wait for stopbit * bitTime;
40
    end procedure;
41
 
42
  procedure recvSerial( signal rxd : in std_logic; baud : in real; parity : in integer; stopbit : in real; bitnumber : in integer; baudError : in real; signal data : inout std_logic_vector(7 downto 0)) is
43
 
44
    variable bitTime  : time;
45
 
46
    begin
47
      bitTime := 1000 ms / (baud + baud * baudError / 100.0);
48
      wait until (rxd = '0');
49
      wait for bitTime / 2;
50
      wait for bitTime;
51
      for index in 0 to bitnumber loop
52
        data <= rxd & data(7 downto 1);
53
        wait for bitTime;
54
      end loop;
55
      wait for stopbit * bitTime;
56
    end procedure;
57
 
58
  -- Inputs
59
  signal clr            : std_logic := '0';
60
  signal clk            : std_logic := '0';
61
  signal serIn          : std_logic := '0';
62
  signal intRdData      : std_logic_vector(7 downto 0) := (others => '0');
63
 
64
        -- Outputs
65
  signal serOut         : std_logic;
66
  signal intAddress     : std_logic_vector(7 downto 0);
67
  signal intWrData      : std_logic_vector(7 downto 0);
68
  signal intWrite       : std_logic;
69
  signal intRead        : std_logic;
70
  signal recvData       : std_logic_vector(7 downto 0);
71
  signal newRxData      : std_logic;
72 11 smuller
  signal intAccessReq   : std_logic;
73
  signal intAccessGnt   : std_logic;
74
  signal counter        : integer;
75 6 smuller
 
76
  constant BAUD_115200  : real := 115200.0;
77
  constant BAUD_38400   : real := 38400.0;
78
  constant BAUD_28800   : real := 28800.0;
79
  constant BAUD_19200   : real := 19200.0;
80
  constant BAUD_9600    : real := 9600.0;
81
  constant BAUD_4800    : real := 4800.0;
82
  constant BAUD_2400    : real := 2400.0;
83
  constant BAUD_1200    : real := 1200.0;
84
 
85
  constant NSTOPS_1     : real := 1.0;
86
  constant NSTOPS_1_5   : real := 1.5;
87
  constant NSTOPS_2     : real := 2.0;
88
 
89
  constant PARITY_NONE  : integer := 0;
90
  constant PARITY_EVEN  : integer := 1;
91
  constant PARITY_ODD   : integer := 2;
92
  constant PARITY_MARK  : integer := 3;
93
  constant PARITY_SPACE : integer := 4;
94
 
95
  constant NBITS_7      : integer := 6;
96
  constant NBITS_8      : integer := 7;
97
 
98
  begin
99
    -- Instantiate the Unit Under Test (UUT)
100
    uut : uart2BusTop
101
      port map
102
      (
103
        clr => clr,
104
        clk => clk,
105
        serIn => serIn,
106
        serOut => serOut,
107 11 smuller
        intAccessReq => intAccessReq,
108
        intAccessGnt => intAccessGnt,
109 6 smuller
        intRdData => intRdData,
110
        intAddress => intAddress,
111
        intWrData => intWrData,
112
        intWrite => intWrite,
113
        intRead => intRead
114
      );
115
 
116
    rfm : regFileModel
117
    port map
118
    (
119
      clr => clr,
120
      clk => clk,
121
      intRdData => intRdData,
122
      intAddress => intAddress,
123
      intWrData => intWrData,
124
      intWrite => intWrite,
125
      intRead => intRead);
126
 
127 11 smuller
    -- just to create a delay similar to simulate a bus arbitrer
128
    process (clr, clk)
129
    begin
130
      if (clr = '1') then
131
        intAccessGnt <= '0';
132
        counter <= 0;
133
      elsif (rising_edge(clk)) then
134
        if (counter = 0) then
135
          if ((intAccessReq = '1') and (intAccessGnt = '0')) then
136
            counter <= 500;
137
          end if;
138
          intAccessGnt <= '0';
139
        elsif (counter = 1) then
140
          counter <= counter - 1;
141
          intAccessGnt <= '1';
142
        else
143
          counter <= counter - 1;
144
        end if;
145
      end if;
146
    end process;
147
 
148 6 smuller
    -- clock generator - 25MHz clock 
149
    process
150
    begin
151
      clk <= '0';
152
      wait for 20 ns;
153
      clk <= '1';
154
      wait for 20 ns;
155
    end process;
156
 
157
    -- reset process definitions
158
    process
159
    begin
160
      clr <= '1';
161
      wait for 40 ns;
162
      clr <= '0';
163
      wait;
164
    end process;
165
 
166
    --------------------------------------------------------------------
167
    -- test bench receiver 
168
    process
169
 
170
    begin
171
      newRxData <= '0';
172
      recvData <= (others => '0');
173
      wait until (clr = '0');
174
      loop
175
        recvSerial(serOut, BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, recvData);
176
        newRxData <= '1';
177
        wait for 25 ns;
178
        newRxData <= '0';
179
      end loop;
180
    end process;
181
 
182
    --------------------------------------------------------------------
183
    -- uart transmit - test bench control 
184
    process
185
 
186
      type     dataFile is file of character;
187 11 smuller
      file     testBinaryFile : dataFile open READ_MODE is "../test.bin";
188 6 smuller
      variable charBuf        : character;
189
      variable fileLength     : integer;
190
      variable byteIndex      : integer;
191
      variable txLength       : integer;
192
      variable rxLength       : integer;
193
      variable tempLine       : line;
194
 
195
    begin
196
          -- default value of serial output 
197
      serIn <= '1';
198
      -- binary mode simulation 
199
      write(tempLine, string'("Starting binary mode simulation"));
200
      writeline(output, tempLine);
201
      wait until (clr = '0');
202
      wait until (rising_edge(clk));
203
      for index in 0 to 99 loop
204
        wait until (rising_edge(clk));
205
      end loop;
206
          -- in binary simulation mode the first two byte contain the file length (MSB first) 
207
      read(testBinaryFile, charBuf);
208
      fileLength := character'pos(charBuf);
209
      read(testBinaryFile, charBuf);
210
      fileLength := 256 * fileLength + character'pos(charBuf);
211
      write(tempLine, string'("File length: "));
212
      write(tempLine, fileLength);
213
      writeline(output, tempLine);
214
          -- send entire file to uart 
215
      byteIndex := 0;
216
      while (byteIndex < fileLength) loop
217
        -- each "record" in the binary starts with two bytes: the first is the number 
218
        -- of bytes to transmit and the second is the number of received bytes to wait 
219
        -- for before transmitting the next command. 
220
        read(testBinaryFile, charBuf);
221
        txLength := character'pos(charBuf);
222
        read(testBinaryFile, charBuf);
223
        rxLength := character'pos(charBuf);
224
        write(tempLine, string'("Executing command with "));
225
        write(tempLine, txLength);
226
        write(tempLine, string'(" tx bytes and "));
227
        write(tempLine, rxLength);
228
        write(tempLine, string'(" rx bytes"));
229
        writeline(output, tempLine);
230
        byteIndex := byteIndex + 2;
231
                -- transmit command 
232
        while (txLength > 0) loop
233
                  -- read next byte from file and transmit it 
234
          read(testBinaryFile, charBuf);
235
          byteIndex := byteIndex + 1;
236
          sendSerial(character'pos(charBuf), BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, serIn);
237
                  -- update tx_len
238
          txLength := txLength - 1;
239
        end loop;
240
                -- wait for received bytes
241
        while (rxLength > 0) loop
242
          wait until (newRxData = '1');
243
          wait until (newRxData = '0');
244
          rxLength := rxLength - 1;
245
        end loop;
246
        write(tempLine, string'("Command finished"));
247
        writeline(output, tempLine);
248
      end loop;
249
      wait;
250
    end process;
251
  end;

powered by: WebSVN 2.1.0

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