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

Subversion Repositories iicmb

[/] [iicmb/] [trunk/] [src_tb/] [iicmb_m_sq_tb.vhd] - Blame information for rev 5

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_sq'.                                     |
7
--    Version:                                                                 |
8
--             1.0,   April 29, 2016                                           |
9 5 sshuv2
--             1.1,   May   10, 2016 Changed i2c_slave_model instance          |
10
--                                   parameter interface                       |
11 2 sshuv2
--                                                                             |
12
--    Author:  Sergey Shuvalkin, (sshuv2@opencores.org)                        |
13
--                                                                             |
14
--==============================================================================
15
--==============================================================================
16
-- Copyright (c) 2016, Sergey Shuvalkin                                        |
17
-- All rights reserved.                                                        |
18
--                                                                             |
19
-- Redistribution and use in source and binary forms, with or without          |
20
-- modification, are permitted provided that the following conditions are met: |
21
--                                                                             |
22
-- 1. Redistributions of source code must retain the above copyright notice,   |
23
--    this list of conditions and the following disclaimer.                    |
24
-- 2. Redistributions in binary form must reproduce the above copyright        |
25
--    notice, this list of conditions and the following disclaimer in the      |
26
--    documentation and/or other materials provided with the distribution.     |
27
--                                                                             |
28
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
29
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   |
30
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  |
31
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
32
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR         |
33
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF        |
34
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS    |
35
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN     |
36
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)     |
37
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  |
38
-- POSSIBILITY OF SUCH DAMAGE.                                                 |
39
--==============================================================================
40
 
41
 
42
library ieee;
43
use ieee.std_logic_1164.all;
44
use ieee.numeric_std.all;
45
 
46
library iicmb;
47
use iicmb.iicmb_pkg.all;
48
 
49
use work.test.all;
50
 
51
 
52
--==============================================================================
53
entity iicmb_m_sq_tb is
54
end entity iicmb_m_sq_tb;
55
--==============================================================================
56
 
57
--==============================================================================
58
architecture beh of iicmb_m_sq_tb is
59
 
60
  constant c_f_clk   : real      := 100000.0; -- in kHz
61
  constant c_f_scl_0 : real      :=    100.0; -- in kHz
62
  constant c_f_scl_1 : real      :=    100.0; -- in kHz
63
  constant c_f_scl_2 : real      :=    400.0; -- in kHz
64
  constant c_f_scl_3 : real      :=    100.0; -- in kHz
65
  constant c_p_clk   : time      := integer(1000000000.0/c_f_clk) * 1 ps; -- Period of 'clk' in ps.
66
 
67
  constant c_bus_num : positive  := 4;
68
 
69
  ------------------------------------------------------------------------------
70
  component iicmb_m_sq is
71
    generic
72
    (
73
      g_bus_num     :       positive range 1 to 16 := 1;
74
      g_f_clk       :       real                   := 100000.0;
75
      g_f_scl_0     :       real                   :=    100.0;
76
      g_f_scl_1     :       real                   :=    100.0;
77
      g_f_scl_2     :       real                   :=    100.0;
78
      g_f_scl_3     :       real                   :=    100.0;
79
      g_f_scl_4     :       real                   :=    100.0;
80
      g_f_scl_5     :       real                   :=    100.0;
81
      g_f_scl_6     :       real                   :=    100.0;
82
      g_f_scl_7     :       real                   :=    100.0;
83
      g_f_scl_8     :       real                   :=    100.0;
84
      g_f_scl_9     :       real                   :=    100.0;
85
      g_f_scl_a     :       real                   :=    100.0;
86
      g_f_scl_b     :       real                   :=    100.0;
87
      g_f_scl_c     :       real                   :=    100.0;
88
      g_f_scl_d     :       real                   :=    100.0;
89
      g_f_scl_e     :       real                   :=    100.0;
90
      g_f_scl_f     :       real                   :=    100.0;
91
      g_cmd         :       seq_cmd_type_array     := c_empty_array
92
    );
93
    port
94
    (
95
      clk           : in    std_logic;
96
      s_rst         : in    std_logic;
97
      cs_start      : in    std_logic;
98
      cs_busy       :   out std_logic;
99
      cs_status     :   out std_logic_vector(2 downto 0);
100
      scl_i         : in    std_logic_vector(0 to g_bus_num - 1);
101
      sda_i         : in    std_logic_vector(0 to g_bus_num - 1);
102
      scl_o         :   out std_logic_vector(0 to g_bus_num - 1);
103
      sda_o         :   out std_logic_vector(0 to g_bus_num - 1)
104
    );
105
  end component iicmb_m_sq;
106
  ------------------------------------------------------------------------------
107
 
108
  ------------------------------------------------------------------------------
109
  component wire_mdl is
110
    generic
111
    (
112
      g_resistance_0       :       real       := 1.0; -- In Ohms
113
      g_resistance_1       :       real       := 1.0; -- In Ohms
114
      g_capacitance        :       real       := 1.0; -- In pF
115
      g_initial_level      :       bit        := '0'
116
    );
117
    port
118
    (
119
      sig_in               : in    bit;
120
      sig_out              :   out real;
121
      sig_out_l            :   out bit
122
    );
123
  end component wire_mdl;
124
  ------------------------------------------------------------------------------
125
 
126
  ------------------------------------------------------------------------------
127
  component i2c_slave_model is
128
    generic
129
    (
130 4 sshuv2
      I2C_ADR : integer
131 2 sshuv2
    );
132
    port
133
    (
134
      scl     : inout std_logic;
135
      sda     : inout std_logic
136
    );
137
  end component i2c_slave_model;
138
  ------------------------------------------------------------------------------
139
 
140
  ------------------------------------------------------------------------------
141 4 sshuv2
  function get_slave_addr(n : natural) return natural is
142
    variable ret : unsigned(6 downto 0);
143 2 sshuv2
  begin
144 4 sshuv2
    ret := "010" & to_unsigned(n, 4);
145
    return to_integer(ret);
146 2 sshuv2
  end function get_slave_addr;
147
  ------------------------------------------------------------------------------
148
 
149
  signal   cs_start    : std_logic := '0';
150
  signal   cs_busy     : std_logic;
151
  signal   cs_status   : std_logic_vector(2 downto 0);
152
 
153
  signal   clk         : std_logic := '0';
154
  signal   s_rst       : std_logic := '1';
155
 
156
  signal   scl_o       : std_logic_vector(0 to c_bus_num - 1) := (others => '1');
157
  signal   scl         : std_logic_vector(0 to c_bus_num - 1) := (others => 'H');
158
  signal   sda_o       : std_logic_vector(0 to c_bus_num - 1) := (others => '1');
159
  signal   sda         : std_logic_vector(0 to c_bus_num - 1) := (others => 'H');
160
 
161
  type real_vector is array (natural range <>) of real;
162
  signal   scl_real    : real_vector(0 to c_bus_num - 1);
163
  signal   sda_real    : real_vector(0 to c_bus_num - 1);
164
  signal   scl_quant   : bit_vector(0 to c_bus_num - 1);
165
  signal   sda_quant   : bit_vector(0 to c_bus_num - 1);
166
  signal   scl_nquant  : bit_vector(0 to c_bus_num - 1) := (others => '1');
167
  signal   sda_nquant  : bit_vector(0 to c_bus_num - 1) := (others => '1');
168
  signal   irq         : std_logic;
169
 
170
begin
171
 
172
  clk <= not(clk) after c_p_clk / 2;
173
  s_rst <= '1', '0' after 113 ns;
174
 
175
  ------------------------------------------------------------------------------
176
  -- Generate signal to launch the sequencer
177
  process
178
  begin
179
    cs_start <= '0';
180
    wait for 2000 ns;
181
    wait until rising_edge(clk);
182
    cs_start <= '1';
183
    wait until rising_edge(clk);
184
    cs_start <= '0';
185
    wait;
186
  end process;
187
  ------------------------------------------------------------------------------
188
 
189
  ------------------------------------------------------------------------------
190
  dut : iicmb_m_sq
191
    generic map
192
    (
193
      g_bus_num   => c_bus_num,
194
      g_f_clk     => c_f_clk,
195
      g_f_scl_0   => c_f_scl_0,
196
      g_f_scl_1   => c_f_scl_1,
197
      g_f_scl_2   => c_f_scl_2,
198
      g_f_scl_3   => c_f_scl_3,
199
      g_cmd       =>
200
        (
201
          scmd_wait(1),                             -- Wait for 1 ms
202
          scmd_set_bus(1),                          -- Select bus #1
203
          scmd_write_byte("0100001", x"00", x"4A"), -- Write byte
204
          scmd_write_byte("0100001", x"01", x"67"), -- Write byte
205
          scmd_wait(1),                             -- Wait for 1 ms
206
          scmd_set_bus(2),                          -- Select bus #2
207
          scmd_write_byte("0100010", x"02", x"59"), -- Write byte
208
          scmd_write_byte("0100010", x"03", x"AB")  -- Write byte
209
        )
210
    )
211
    port map
212
    (
213
      clk         => clk,
214
      s_rst       => s_rst,
215
      cs_start    => cs_start,
216
      cs_busy     => cs_busy,
217
      cs_status   => cs_status,
218
      scl_i       => to_stdlogicvector(scl_quant),
219
      sda_i       => to_stdlogicvector(sda_quant),
220
      scl_o       => scl_o,
221
      sda_o       => sda_o
222
    );
223
  ------------------------------------------------------------------------------
224
 
225
  --****************************************************************************
226
  bus_gen:
227
  for i in 0 to c_bus_num - 1 generate
228
    scl(i) <= '0' when (scl_o(i) = '0') else 'Z';
229
    sda(i) <= '0' when (sda_o(i) = '0') else 'Z';
230
 
231
    ----------------------------------------------------------------------------
232
    wire_mdl_inst_0 : wire_mdl
233
      generic map
234
      (
235
        g_resistance_0       => 40.0,
236
        g_resistance_1       => 4000.0,
237
        g_capacitance        => 200.0, -- In pF
238
        g_initial_level      => '1'
239
      )
240
      port map
241
      (
242
        sig_in               => scl_nquant(i),
243
        sig_out              => scl_real(i),
244
        sig_out_l            => scl_quant(i)
245
      );
246
    ----------------------------------------------------------------------------
247
 
248
    ----------------------------------------------------------------------------
249
    wire_mdl_inst_1 : wire_mdl
250
      generic map
251
      (
252
        g_resistance_0       => 40.0,
253
        g_resistance_1       => 4000.0,
254
        g_capacitance        => 200.0, -- In pF
255
        g_initial_level      => '1'
256
      )
257
      port map
258
      (
259
        sig_in               => sda_nquant(i),
260
        sig_out              => sda_real(i),
261
        sig_out_l            => sda_quant(i)
262
      );
263
    ----------------------------------------------------------------------------
264
 
265
    ----------------------------------------------------------------------------
266
    i2c_slave_model_inst0 : i2c_slave_model
267
      generic map
268
      (
269 4 sshuv2
        I2C_ADR => get_slave_addr(i)
270 2 sshuv2
      )
271
      port map
272
      (
273
        scl     => scl(i),
274
        sda     => sda(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
end architecture beh;
287
--==============================================================================
288
 

powered by: WebSVN 2.1.0

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