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 |
|
|
-- |
|
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 |
|
|
use ieee.numeric_std.all;
|
43 |
|
|
|
44 |
|
|
library iicmb;
|
45 |
|
|
use iicmb.iicmb_pkg.all;
|
46 |
|
|
|
47 |
|
|
use work.test.all;
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
--==============================================================================
|
51 |
|
|
entity iicmb_m_sq_tb is
|
52 |
|
|
end entity iicmb_m_sq_tb;
|
53 |
|
|
--==============================================================================
|
54 |
|
|
|
55 |
|
|
--==============================================================================
|
56 |
|
|
architecture beh of iicmb_m_sq_tb is
|
57 |
|
|
|
58 |
|
|
constant c_f_clk : real := 100000.0; -- in kHz
|
59 |
|
|
constant c_f_scl_0 : real := 100.0; -- in kHz
|
60 |
|
|
constant c_f_scl_1 : real := 100.0; -- in kHz
|
61 |
|
|
constant c_f_scl_2 : real := 400.0; -- in kHz
|
62 |
|
|
constant c_f_scl_3 : real := 100.0; -- in kHz
|
63 |
|
|
constant c_p_clk : time := integer(1000000000.0/c_f_clk) * 1 ps; -- Period of 'clk' in ps.
|
64 |
|
|
|
65 |
|
|
constant c_bus_num : positive := 4;
|
66 |
|
|
|
67 |
|
|
------------------------------------------------------------------------------
|
68 |
|
|
component iicmb_m_sq is
|
69 |
|
|
generic
|
70 |
|
|
(
|
71 |
|
|
g_bus_num : positive range 1 to 16 := 1;
|
72 |
|
|
g_f_clk : real := 100000.0;
|
73 |
|
|
g_f_scl_0 : real := 100.0;
|
74 |
|
|
g_f_scl_1 : real := 100.0;
|
75 |
|
|
g_f_scl_2 : real := 100.0;
|
76 |
|
|
g_f_scl_3 : real := 100.0;
|
77 |
|
|
g_f_scl_4 : real := 100.0;
|
78 |
|
|
g_f_scl_5 : real := 100.0;
|
79 |
|
|
g_f_scl_6 : real := 100.0;
|
80 |
|
|
g_f_scl_7 : real := 100.0;
|
81 |
|
|
g_f_scl_8 : real := 100.0;
|
82 |
|
|
g_f_scl_9 : real := 100.0;
|
83 |
|
|
g_f_scl_a : real := 100.0;
|
84 |
|
|
g_f_scl_b : real := 100.0;
|
85 |
|
|
g_f_scl_c : real := 100.0;
|
86 |
|
|
g_f_scl_d : real := 100.0;
|
87 |
|
|
g_f_scl_e : real := 100.0;
|
88 |
|
|
g_f_scl_f : real := 100.0;
|
89 |
|
|
g_cmd : seq_cmd_type_array := c_empty_array
|
90 |
|
|
);
|
91 |
|
|
port
|
92 |
|
|
(
|
93 |
|
|
clk : in std_logic;
|
94 |
|
|
s_rst : in std_logic;
|
95 |
|
|
cs_start : in std_logic;
|
96 |
|
|
cs_busy : out std_logic;
|
97 |
|
|
cs_status : out std_logic_vector(2 downto 0);
|
98 |
|
|
scl_i : in std_logic_vector(0 to g_bus_num - 1);
|
99 |
|
|
sda_i : in std_logic_vector(0 to g_bus_num - 1);
|
100 |
|
|
scl_o : out std_logic_vector(0 to g_bus_num - 1);
|
101 |
|
|
sda_o : out std_logic_vector(0 to g_bus_num - 1)
|
102 |
|
|
);
|
103 |
|
|
end component iicmb_m_sq;
|
104 |
|
|
------------------------------------------------------------------------------
|
105 |
|
|
|
106 |
|
|
------------------------------------------------------------------------------
|
107 |
|
|
component wire_mdl is
|
108 |
|
|
generic
|
109 |
|
|
(
|
110 |
|
|
g_resistance_0 : real := 1.0; -- In Ohms
|
111 |
|
|
g_resistance_1 : real := 1.0; -- In Ohms
|
112 |
|
|
g_capacitance : real := 1.0; -- In pF
|
113 |
|
|
g_initial_level : bit := '0'
|
114 |
|
|
);
|
115 |
|
|
port
|
116 |
|
|
(
|
117 |
|
|
sig_in : in bit;
|
118 |
|
|
sig_out : out real;
|
119 |
|
|
sig_out_l : out bit
|
120 |
|
|
);
|
121 |
|
|
end component wire_mdl;
|
122 |
|
|
------------------------------------------------------------------------------
|
123 |
|
|
|
124 |
|
|
------------------------------------------------------------------------------
|
125 |
|
|
component i2c_slave_model is
|
126 |
|
|
generic
|
127 |
|
|
(
|
128 |
4 |
sshuv2 |
I2C_ADR : integer
|
129 |
2 |
sshuv2 |
);
|
130 |
|
|
port
|
131 |
|
|
(
|
132 |
|
|
scl : inout std_logic;
|
133 |
|
|
sda : inout std_logic
|
134 |
|
|
);
|
135 |
|
|
end component i2c_slave_model;
|
136 |
|
|
------------------------------------------------------------------------------
|
137 |
|
|
|
138 |
|
|
------------------------------------------------------------------------------
|
139 |
4 |
sshuv2 |
function get_slave_addr(n : natural) return natural is
|
140 |
|
|
variable ret : unsigned(6 downto 0);
|
141 |
2 |
sshuv2 |
begin
|
142 |
4 |
sshuv2 |
ret := "010" & to_unsigned(n, 4);
|
143 |
|
|
return to_integer(ret);
|
144 |
2 |
sshuv2 |
end function get_slave_addr;
|
145 |
|
|
------------------------------------------------------------------------------
|
146 |
|
|
|
147 |
|
|
signal cs_start : std_logic := '0';
|
148 |
|
|
signal cs_busy : std_logic;
|
149 |
|
|
signal cs_status : std_logic_vector(2 downto 0);
|
150 |
|
|
|
151 |
|
|
signal clk : std_logic := '0';
|
152 |
|
|
signal s_rst : std_logic := '1';
|
153 |
|
|
|
154 |
|
|
signal scl_o : std_logic_vector(0 to c_bus_num - 1) := (others => '1');
|
155 |
|
|
signal scl : std_logic_vector(0 to c_bus_num - 1) := (others => 'H');
|
156 |
|
|
signal sda_o : std_logic_vector(0 to c_bus_num - 1) := (others => '1');
|
157 |
|
|
signal sda : std_logic_vector(0 to c_bus_num - 1) := (others => 'H');
|
158 |
|
|
|
159 |
|
|
type real_vector is array (natural range <>) of real;
|
160 |
|
|
signal scl_real : real_vector(0 to c_bus_num - 1);
|
161 |
|
|
signal sda_real : real_vector(0 to c_bus_num - 1);
|
162 |
|
|
signal scl_quant : bit_vector(0 to c_bus_num - 1);
|
163 |
|
|
signal sda_quant : bit_vector(0 to c_bus_num - 1);
|
164 |
|
|
signal scl_nquant : bit_vector(0 to c_bus_num - 1) := (others => '1');
|
165 |
|
|
signal sda_nquant : bit_vector(0 to c_bus_num - 1) := (others => '1');
|
166 |
|
|
signal irq : std_logic;
|
167 |
|
|
|
168 |
|
|
begin
|
169 |
|
|
|
170 |
|
|
clk <= not(clk) after c_p_clk / 2;
|
171 |
|
|
s_rst <= '1', '0' after 113 ns;
|
172 |
|
|
|
173 |
|
|
------------------------------------------------------------------------------
|
174 |
|
|
-- Generate signal to launch the sequencer
|
175 |
|
|
process
|
176 |
|
|
begin
|
177 |
|
|
cs_start <= '0';
|
178 |
|
|
wait for 2000 ns;
|
179 |
|
|
wait until rising_edge(clk);
|
180 |
|
|
cs_start <= '1';
|
181 |
|
|
wait until rising_edge(clk);
|
182 |
|
|
cs_start <= '0';
|
183 |
|
|
wait;
|
184 |
|
|
end process;
|
185 |
|
|
------------------------------------------------------------------------------
|
186 |
|
|
|
187 |
|
|
------------------------------------------------------------------------------
|
188 |
|
|
dut : iicmb_m_sq
|
189 |
|
|
generic map
|
190 |
|
|
(
|
191 |
|
|
g_bus_num => c_bus_num,
|
192 |
|
|
g_f_clk => c_f_clk,
|
193 |
|
|
g_f_scl_0 => c_f_scl_0,
|
194 |
|
|
g_f_scl_1 => c_f_scl_1,
|
195 |
|
|
g_f_scl_2 => c_f_scl_2,
|
196 |
|
|
g_f_scl_3 => c_f_scl_3,
|
197 |
|
|
g_cmd =>
|
198 |
|
|
(
|
199 |
|
|
scmd_wait(1), -- Wait for 1 ms
|
200 |
|
|
scmd_set_bus(1), -- Select bus #1
|
201 |
|
|
scmd_write_byte("0100001", x"00", x"4A"), -- Write byte
|
202 |
|
|
scmd_write_byte("0100001", x"01", x"67"), -- Write byte
|
203 |
|
|
scmd_wait(1), -- Wait for 1 ms
|
204 |
|
|
scmd_set_bus(2), -- Select bus #2
|
205 |
|
|
scmd_write_byte("0100010", x"02", x"59"), -- Write byte
|
206 |
|
|
scmd_write_byte("0100010", x"03", x"AB") -- Write byte
|
207 |
|
|
)
|
208 |
|
|
)
|
209 |
|
|
port map
|
210 |
|
|
(
|
211 |
|
|
clk => clk,
|
212 |
|
|
s_rst => s_rst,
|
213 |
|
|
cs_start => cs_start,
|
214 |
|
|
cs_busy => cs_busy,
|
215 |
|
|
cs_status => cs_status,
|
216 |
|
|
scl_i => to_stdlogicvector(scl_quant),
|
217 |
|
|
sda_i => to_stdlogicvector(sda_quant),
|
218 |
|
|
scl_o => scl_o,
|
219 |
|
|
sda_o => sda_o
|
220 |
|
|
);
|
221 |
|
|
------------------------------------------------------------------------------
|
222 |
|
|
|
223 |
|
|
--****************************************************************************
|
224 |
|
|
bus_gen:
|
225 |
|
|
for i in 0 to c_bus_num - 1 generate
|
226 |
|
|
scl(i) <= '0' when (scl_o(i) = '0') else 'Z';
|
227 |
|
|
sda(i) <= '0' when (sda_o(i) = '0') else 'Z';
|
228 |
|
|
|
229 |
|
|
----------------------------------------------------------------------------
|
230 |
|
|
wire_mdl_inst_0 : wire_mdl
|
231 |
|
|
generic map
|
232 |
|
|
(
|
233 |
|
|
g_resistance_0 => 40.0,
|
234 |
|
|
g_resistance_1 => 4000.0,
|
235 |
|
|
g_capacitance => 200.0, -- In pF
|
236 |
|
|
g_initial_level => '1'
|
237 |
|
|
)
|
238 |
|
|
port map
|
239 |
|
|
(
|
240 |
|
|
sig_in => scl_nquant(i),
|
241 |
|
|
sig_out => scl_real(i),
|
242 |
|
|
sig_out_l => scl_quant(i)
|
243 |
|
|
);
|
244 |
|
|
----------------------------------------------------------------------------
|
245 |
|
|
|
246 |
|
|
----------------------------------------------------------------------------
|
247 |
|
|
wire_mdl_inst_1 : wire_mdl
|
248 |
|
|
generic map
|
249 |
|
|
(
|
250 |
|
|
g_resistance_0 => 40.0,
|
251 |
|
|
g_resistance_1 => 4000.0,
|
252 |
|
|
g_capacitance => 200.0, -- In pF
|
253 |
|
|
g_initial_level => '1'
|
254 |
|
|
)
|
255 |
|
|
port map
|
256 |
|
|
(
|
257 |
|
|
sig_in => sda_nquant(i),
|
258 |
|
|
sig_out => sda_real(i),
|
259 |
|
|
sig_out_l => sda_quant(i)
|
260 |
|
|
);
|
261 |
|
|
----------------------------------------------------------------------------
|
262 |
|
|
|
263 |
|
|
----------------------------------------------------------------------------
|
264 |
|
|
i2c_slave_model_inst0 : i2c_slave_model
|
265 |
|
|
generic map
|
266 |
|
|
(
|
267 |
4 |
sshuv2 |
I2C_ADR => get_slave_addr(i)
|
268 |
2 |
sshuv2 |
)
|
269 |
|
|
port map
|
270 |
|
|
(
|
271 |
|
|
scl => scl(i),
|
272 |
|
|
sda => sda(i)
|
273 |
|
|
);
|
274 |
|
|
----------------------------------------------------------------------------
|
275 |
|
|
end generate bus_gen;
|
276 |
|
|
--****************************************************************************
|
277 |
|
|
|
278 |
|
|
scl <= (others => 'H'); -- Pull-up
|
279 |
|
|
sda <= (others => 'H'); -- Pull-up
|
280 |
|
|
|
281 |
|
|
scl_nquant <= to_bitvector(to_x01(scl));
|
282 |
|
|
sda_nquant <= to_bitvector(to_x01(sda));
|
283 |
|
|
|
284 |
|
|
end architecture beh;
|
285 |
|
|
--==============================================================================
|
286 |
|
|
|