~ruther/vhdl-i2c

ref: 57bc4031f549c4c98b9f726cf3ca7681299aa521 vhdl-i2c/tb/i2c/address_generator_tb.vhd -rw-r--r-- 6.4 KiB
57bc4031 — Rutherther feat: store address, rw in address generator or detector 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
library ieee;
use ieee.std_logic_1164.all;

library i2c;
library vunit_lib;
context vunit_lib.vunit_context;

entity address_generator_tb is

  generic (
    runner_cfg : string);

end entity address_generator_tb;

architecture tb of address_generator_tb is
  signal clk : std_logic := '0';
  constant CLK_PERIOD : time := 10 ns;

  signal rst_n : std_logic := '0';

  signal sda : std_logic := 'H';
  signal slave_sda : std_logic := 'H';
  signal sda_enable : std_logic;

  signal scl : std_logic := 'H';
  signal not_scl : std_logic;

  signal scl_rising : std_logic := '0';
  signal scl_falling : std_logic := '0';
  signal scl_falling_delayed : std_logic := '0';

  signal start : std_logic := '0';
  signal done : std_logic;
  signal unexpected_sda, noack : std_logic;
  signal rw : std_logic := '0';
  signal address : std_logic_vector(6 downto 0);

  signal one : std_logic := '1';
  signal zero : std_logic := '0';
begin  -- architecture tb

  clk <= not clk after CLK_PERIOD/2;
  rst_n <= '1' after 2*CLK_PERIOD;

  sda <= 'H';
  sda <= '0' when sda_enable = '1' else 'Z';

  slave_sda <= '1' when sda = 'H' else sda;

  not_scl <= not scl;
  scl <= 'H';

  uut : entity i2c.address_generator
    port map (
      clk_i                 => clk,
      rst_in                => rst_n,
      start_i               => start,
      store_address_rw_i    => '1',
      address_i             => address,
      rw_i                  => rw,
      sda_i                 => slave_sda,
      sda_enable_o          => sda_enable,
      scl_rising_i          => scl_rising,
      scl_falling_delayed_i => scl_falling_delayed,
      unexpected_sda_o      => unexpected_sda,
      noack_o               => noack,
      done_o                => done);

  sda_stability_check: check_stable(clk, one, scl, not_scl, sda);

  main: process is
    procedure request_address_gen (
      constant address_i : in std_logic_vector(6 downto 0);
      constant rw_i      : in std_logic) is
      begin
        rw <= rw_i;
        address <= address_i;
        start <= '1';
        wait until falling_edge(clk);
        start <= '0';
      end procedure request_address_gen;

      procedure scl_fall is
      begin  -- procedure scl_fall
        wait until falling_edge(clk);
        wait until falling_edge(clk);
        wait until falling_edge(clk);
        wait until falling_edge(clk);
        scl <= '0';
        wait until falling_edge(clk);
        wait until falling_edge(clk);
        wait until falling_edge(clk);
        wait until falling_edge(clk);
      end procedure scl_fall;

      procedure scl_rise is
      begin  -- procedure scl_fall
        scl <= 'Z';
        wait until falling_edge(clk);
      end procedure scl_rise;

      procedure scl_pulse is
      begin  -- procedure scl_pulse
        scl_rise;
        scl_fall;
      end procedure scl_pulse;

      procedure validate_address_gen (
        constant address            : in std_logic_vector(6 downto 0);
        constant rw                 : in std_logic;
        constant ack                : in std_logic                    := '1';
        constant exp_noack          : in std_logic                    := '0';
        constant exp_unexpected_sda : in std_logic_vector(7 downto 0) := (others => '0')) is
      begin  -- procedure validate_address_gen
        check_equal(done, '0', "Wrong done before validation!");
        scl_fall;

        for i in 0 to 6 loop
          if address(6 - i) = '1' then
            check_equal(sda, 'H', "Sda should be high for index " & integer'image(i));
          else
            check_equal(sda, '0', "Sda should be low for index " & integer'image(i));
          end if;

          wait for 0 ns;
          if exp_unexpected_sda(7 - i) = '1' then
            sda <= '0';
          end if;

          scl_rise;

          check_equal(unexpected_sda, exp_unexpected_sda(7 - i));

          scl_fall;

          if exp_unexpected_sda(7 - i) = '1' then
            sda <= 'Z';
          end if;
          wait for 0 ns;

        end loop;  -- i

        if rw = '1' then
          check_equal(sda, 'H', "Sda should be high for rw");
        else
          check_equal(sda, '0', "Sda should be low for rw");
        end if;

        scl_rise;
        check_equal(unexpected_sda, exp_unexpected_sda(0));
        scl_fall;

        if ack = '1' then
          sda <= '0';
          wait until falling_edge(clk);
        end if;

        scl <= 'Z';
        wait for CLK_PERIOD/4;

        check_equal(noack, exp_noack, "Unexpected noack.");

        wait until falling_edge(clk);

        scl_fall;

        sda <= 'Z';

        check_equal(done, '1', "Not reporting done");

      end procedure validate_address_gen;
  begin  -- process main
    wait until rst_n = '1';
    wait until falling_edge(clk);

    check_equal(scl, 'H', "begin SCL not high");

    test_runner_setup(runner, runner_cfg);

    while test_suite loop
      if run("simple_read") then
        request_address_gen("1110101", '1');
        validate_address_gen("1110101", '1');
      elsif run("simple_write") then
        request_address_gen("0001010", '0');
        validate_address_gen("0001010", '0');
      elsif run("write_read") then
        request_address_gen("0001010", '0');
        validate_address_gen("0001010", '0');

        request_address_gen("1111010", '1');
        validate_address_gen("1111010", '1');
      elsif run("noack") then
        request_address_gen("0001010", '0');
        validate_address_gen("0001010", '0', ack => '0', exp_noack => '1');
      elsif run("sda_wrong") then
        request_address_gen("1111111", '0');
        validate_address_gen("1111111", '0', exp_unexpected_sda => "10101010");
      end if;
    end loop;

    test_runner_cleanup(runner);
  end process main;

  set_scl_rising: process is
  begin  -- process scl_rising
    wait until rising_edge(scl);
    scl_rising <= '1';
    wait until falling_edge(clk);
    wait until rising_edge(clk);
    scl_rising <= '0';
  end process set_scl_rising;

  set_scl_falling: process is
  begin  -- process scl_rising
    wait until falling_edge(scl);
    scl_falling <= '1';
    wait until rising_edge(clk);
    wait until falling_edge(clk);
    scl_falling <= '0';
  end process set_scl_falling;

  set_delayed_scl_falling: process is
  begin  -- process scl_rising
    wait until falling_edge(scl);
    wait until falling_edge(clk);
    scl_falling_delayed <= '1';
    wait until rising_edge(clk);
    wait until falling_edge(clk);
    scl_falling_delayed <= '0';
  end process set_delayed_scl_falling;

end architecture tb;
Do not follow this link