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

Subversion Repositories uart2bus

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

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

powered by: WebSVN 2.1.0

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