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

Subversion Repositories iicmb

[/] [iicmb/] [trunk/] [src_tb/] [iicmb_m_tb.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sshuv2
 
2
--==============================================================================
3
--                                                                             |
4
--    Project: IIC Multiple Bus Controller (IICMB)                             |
5
--                                                                             |
6
--    Module:  Testbench for 'iicmb_m'.                                        |
7
--    Version:                                                                 |
8
--             1.0,   April 29, 2016                                           |
9
--                                                                             |
10
--    Author:  Sergey Shuvalkin, (sshuv2@opencores.org)                        |
11
--                                                                             |
12
--==============================================================================
13
--==============================================================================
14
-- Copyright (c) 2016, Sergey Shuvalkin                                        |
15
-- All rights reserved.                                                        |
16
--                                                                             |
17
-- Redistribution and use in source and binary forms, with or without          |
18
-- modification, are permitted provided that the following conditions are met: |
19
--                                                                             |
20
-- 1. Redistributions of source code must retain the above copyright notice,   |
21
--    this list of conditions and the following disclaimer.                    |
22
-- 2. Redistributions in binary form must reproduce the above copyright        |
23
--    notice, this list of conditions and the following disclaimer in the      |
24
--    documentation and/or other materials provided with the distribution.     |
25
--                                                                             |
26
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
27
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   |
28
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  |
29
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
30
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR         |
31
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF        |
32
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS    |
33
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN     |
34
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)     |
35
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  |
36
-- POSSIBILITY OF SUCH DAMAGE.                                                 |
37
--==============================================================================
38
 
39
 
40
library ieee;
41
use ieee.std_logic_1164.all;
42
 
43
library iicmb;
44
use iicmb.iicmb_pkg.all;
45
 
46
use work.test.all;
47
 
48
 
49
--==============================================================================
50
entity iicmb_m_tb is
51
end entity iicmb_m_tb;
52
--==============================================================================
53
 
54
--==============================================================================
55
architecture beh of iicmb_m_tb is
56
 
57
  constant c_f_clk   : real      := 100000.0; -- in kHz
58
  constant c_f_scl_0 : real      :=    100.0; -- in kHz
59
  constant c_f_scl_1 : real      :=    100.0; -- in kHz
60
  constant c_f_scl_2 : real      :=    100.0; -- in kHz
61
  constant c_f_scl_3 : real      :=    100.0; -- in kHz
62
  constant c_p_clk   : time      := integer(1000000000.0/c_f_clk) * 1 ps;
63
 
64
  constant c_bus_num : positive  := 1;
65
 
66
  ------------------------------------------------------------------------------
67
  component iicmb_m is
68
    generic
69
    (
70
      g_bus_num :       positive range 1 to 16 := 1;
71
      g_f_clk   :       real                   := 100000.0;
72
      g_f_scl_0 :       real                   :=    100.0;
73
      g_f_scl_1 :       real                   :=    100.0;
74
      g_f_scl_2 :       real                   :=    100.0;
75
      g_f_scl_3 :       real                   :=    100.0;
76
      g_f_scl_4 :       real                   :=    100.0;
77
      g_f_scl_5 :       real                   :=    100.0;
78
      g_f_scl_6 :       real                   :=    100.0;
79
      g_f_scl_7 :       real                   :=    100.0;
80
      g_f_scl_8 :       real                   :=    100.0;
81
      g_f_scl_9 :       real                   :=    100.0;
82
      g_f_scl_a :       real                   :=    100.0;
83
      g_f_scl_b :       real                   :=    100.0;
84
      g_f_scl_c :       real                   :=    100.0;
85
      g_f_scl_d :       real                   :=    100.0;
86
      g_f_scl_e :       real                   :=    100.0;
87
      g_f_scl_f :       real                   :=    100.0
88
    );
89
    port
90
    (
91
      clk         : in    std_logic;
92
      s_rst       : in    std_logic;
93
      busy        :   out std_logic;
94
      captured    :   out std_logic;
95
      bus_id      :   out std_logic_vector(3 downto 0);
96
      bit_state   :   out std_logic_vector(3 downto 0);
97
      byte_state  :   out std_logic_vector(3 downto 0);
98
      mcmd_wr     : in    std_logic;
99
      mcmd_id     : in    std_logic_vector(2 downto 0);
100
      mcmd_data   : in    std_logic_vector(7 downto 0);
101
      mrsp_wr     :   out std_logic;
102
      mrsp_id     :   out std_logic_vector(2 downto 0);
103
      mrsp_data   :   out std_logic_vector(7 downto 0);
104
      scl_i       : in    std_logic_vector(0 to g_bus_num - 1);
105
      sda_i       : in    std_logic_vector(0 to g_bus_num - 1);
106
      scl_o       :   out std_logic_vector(0 to g_bus_num - 1);
107
      sda_o       :   out std_logic_vector(0 to g_bus_num - 1)
108
    );
109
  end component iicmb_m;
110
  ------------------------------------------------------------------------------
111
 
112
  ------------------------------------------------------------------------------
113
  component wire_mdl is
114
    generic
115
    (
116
      g_resistance_0       :       real       := 1.0; -- In Ohms
117
      g_resistance_1       :       real       := 1.0; -- In Ohms
118
      g_capacitance        :       real       := 1.0; -- In pF
119
      g_initial_level      :       bit        := '0'
120
    );
121
    port
122
    (
123
      sig_in               : in    bit;
124
      sig_out              :   out real;
125
      sig_out_l            :   out bit
126
    );
127
  end component wire_mdl;
128
  ------------------------------------------------------------------------------
129
 
130
  signal   clk         : std_logic := '0';
131
  signal   s_rst       : std_logic := '1';
132
 
133
  signal   busy        : std_logic;
134
  signal   captured    : std_logic;
135
  signal   bus_id      : std_logic_vector(3 downto 0);
136
  signal   bit_state   : std_logic_vector(3 downto 0);
137
  signal   byte_state  : std_logic_vector(3 downto 0);
138
  signal   mcmd_wr     : std_logic;
139
  signal   mcmd_id     : std_logic_vector(2 downto 0);
140
  signal   mcmd_data   : std_logic_vector(7 downto 0);
141
  signal   mrsp_wr     : std_logic;
142
  signal   mrsp_id     : std_logic_vector(2 downto 0);
143
  signal   mrsp_data   : std_logic_vector(7 downto 0);
144
  signal   scl_o       : std_logic_vector(0 to c_bus_num - 1);
145
  signal   scl         : std_logic_vector(0 to c_bus_num - 1);
146
  signal   sda_o       : std_logic_vector(0 to c_bus_num - 1);
147
  signal   sda         : std_logic_vector(0 to c_bus_num - 1);
148
 
149
  type real_vector is array (natural range <>) of real;
150
  signal   scl_real    : real_vector(0 to c_bus_num - 1);
151
  signal   sda_real    : real_vector(0 to c_bus_num - 1);
152
  signal   scl_quant   : bit_vector(0 to c_bus_num - 1);
153
  signal   sda_quant   : bit_vector(0 to c_bus_num - 1);
154
  signal   scl_nquant  : bit_vector(0 to c_bus_num - 1);
155
  signal   sda_nquant  : bit_vector(0 to c_bus_num - 1);
156
 
157
begin
158
 
159
  clk <= not(clk) after c_p_clk / 2;
160
  s_rst <= '1', '0' after 113 ns;
161
 
162
  ------------------------------------------------------------------------------
163
  process
164
    procedure issue_command(a : std_logic_vector(2 downto 0)) is
165
    begin
166
      mcmd_wr   <= '1';
167
      mcmd_id   <= a;
168
      wait until rising_edge(clk);
169
      mcmd_wr   <= '0';
170
      wait until rising_edge(clk)and(mrsp_wr = '1');
171
    end procedure issue_command;
172
    procedure send_byte(a : std_logic_vector(7 downto 0)) is
173
    begin
174
      mcmd_wr   <= '1';
175
      mcmd_id   <= mcmd_write;
176
      mcmd_data <= a;
177
      wait until rising_edge(clk);
178
      mcmd_wr   <= '0';
179
      wait until rising_edge(clk)and(mrsp_wr = '1');
180
    end procedure send_byte;
181
  begin
182
    -- Initial delay:
183
    mcmd_wr   <= '0';
184
    mcmd_id   <= mcmd_set_bus;
185
    mcmd_data <= (others => '0');
186
    wait for 20000 ns;
187
 
188
    --
189
    issue_command(mcmd_start);
190
    send_byte(x"5A");
191
    issue_command(mcmd_start);
192
    send_byte(x"AA");
193
    issue_command(mcmd_read_ack);
194
    issue_command(mcmd_read_nak);
195
    issue_command(mcmd_stop);
196
 
197
    -- Halt:
198
    mcmd_wr   <= '0';
199
    mcmd_id   <= mcmd_set_bus;
200
    mcmd_data <= (others => '0');
201
    wait;
202
  end process;
203
  ------------------------------------------------------------------------------
204
 
205
  ------------------------------------------------------------------------------
206
  dut : iicmb_m
207
    generic map
208
    (
209
      g_bus_num   => c_bus_num,
210
      g_f_clk     => c_f_clk,
211
      g_f_scl_0   => c_f_scl_0,
212
      g_f_scl_1   => c_f_scl_1,
213
      g_f_scl_2   => c_f_scl_2,
214
      g_f_scl_3   => c_f_scl_3
215
    )
216
    port map
217
    (
218
      clk         => clk,
219
      s_rst       => s_rst,
220
      busy        => busy,
221
      captured    => captured,
222
      bus_id      => bus_id,
223
      bit_state   => bit_state,
224
      byte_state  => byte_state,
225
      mcmd_wr     => mcmd_wr,
226
      mcmd_id     => mcmd_id,
227
      mcmd_data   => mcmd_data,
228
      mrsp_wr     => mrsp_wr,
229
      mrsp_id     => mrsp_id,
230
      mrsp_data   => mrsp_data,
231
      scl_i       => to_stdlogicvector(scl_quant),
232
      sda_i       => to_stdlogicvector(sda_quant),
233
      scl_o       => scl_o,
234
      sda_o       => sda_o
235
    );
236
  ------------------------------------------------------------------------------
237
 
238
  --****************************************************************************
239
  bus_gen:
240
  for i in 0 to c_bus_num - 1 generate
241
    scl(i) <= '0' when (scl_o(i) = '0') else 'Z';
242
    sda(i) <= '0' when (sda_o(i) = '0') else 'Z';
243
 
244
    ----------------------------------------------------------------------------
245
    wire_mdl_inst_0 : wire_mdl
246
      generic map
247
      (
248
        g_resistance_0       => 40.0,
249
        g_resistance_1       => 4000.0,
250
        g_capacitance        => 200.0, -- In pF
251
        g_initial_level      => '1'
252
      )
253
      port map
254
      (
255
        sig_in               => scl_nquant(i),
256
        sig_out              => scl_real(i),
257
        sig_out_l            => scl_quant(i)
258
      );
259
    ----------------------------------------------------------------------------
260
 
261
    ----------------------------------------------------------------------------
262
    wire_mdl_inst_1 : wire_mdl
263
      generic map
264
      (
265
        g_resistance_0       => 40.0,
266
        g_resistance_1       => 4000.0,
267
        g_capacitance        => 200.0, -- In pF
268
        g_initial_level      => '1'
269
      )
270
      port map
271
      (
272
        sig_in               => sda_nquant(i),
273
        sig_out              => sda_real(i),
274
        sig_out_l            => sda_quant(i)
275
      );
276
    ----------------------------------------------------------------------------
277
  end generate bus_gen;
278
  --****************************************************************************
279
 
280
  scl <= (others => 'H'); -- Pull up
281
  sda <= (others => 'H'); -- Pull up
282
 
283
  scl_nquant <= to_bitvector(to_x01(scl));
284
  sda_nquant <= to_bitvector(to_x01(sda));
285
 
286
  ------------------------------------------------------------------------------
287
  process(clk)
288
  begin
289
    if rising_edge(clk) then
290
      if (s_rst = '1') then
291
      else
292
        if (mrsp_wr = '1') then
293
          case (mrsp_id) is
294
            when mrsp_done     => print_string("Response: Done" & newline);
295
            when mrsp_nak      => print_string("Response: Nak" & newline);
296
            when mrsp_arb_lost => print_string("Response: Arbitration lost" & newline);
297
            when mrsp_error    => print_string("Response: Error" & newline);
298
            when mrsp_byte     => print_string("Response: Byte received: " & "0x" & to_string(mrsp_data, "X") & newline);
299
            when others        => print_string("Undefined response!!!" & newline);
300
          end case;
301
        end if;
302
      end if;
303
    end if;
304
  end process;
305
 
306
end architecture beh;
307
--==============================================================================
308
 

powered by: WebSVN 2.1.0

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