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

Subversion Repositories uart2bus

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

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

Line No. Rev Author Line
1 11 smuller
-----------------------------------------------------------------------------------------
2
-- uart test bench   
3
--
4
-----------------------------------------------------------------------------------------
5
use std.textio.all;
6
 
7
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
library work;
14
use work.uart2BusTop_pkg.all;
15
use work.helpers_pkg.all;
16
 
17
-----------------------------------------------------------------------------------------
18
-- test bench implementation 
19
entity uart2BusTop_txt_tb is
20
end uart2BusTop_txt_tb;
21
 
22
architecture behavior of uart2BusTop_txt_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
  signal intAccessReq   : std_logic;
73
  signal intAccessGnt   : std_logic;
74
  signal counter        : integer;
75
 
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
        intAccessReq => intAccessReq,
108
        intAccessGnt => intAccessGnt,
109
        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
    -- 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
    -- 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
      file     testTextFile : dataFile open READ_MODE is "../test.txt";
188
      variable charBuf      : character;
189
      variable data         : integer;
190
      variable tempLine     : line;
191
 
192
    begin
193
          -- default value of serial output 
194
      serIn <= '1';
195
      -- text mode simulation 
196
      write(tempLine, string'("Starting text mode simulation"));
197
      writeline(output, tempLine);
198
      wait until (clr = '0');
199
      wait until (rising_edge(clk));
200
      for index in 0 to 99 loop
201
        wait until (rising_edge(clk));
202
      end loop;
203
      while not endfile(testTextFile) loop
204
        -- transmit the byte in the command file one by one 
205
        read(testTextFile, charBuf);
206
        data := character'pos(charBuf);
207
        sendSerial(data, BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, serIn);
208
        wait for 800 us;
209
      end loop;
210
      wait;
211
    end process;
212
  end;

powered by: WebSVN 2.1.0

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