| 1 |
-- Copyright (c) 2021 Nuand LLC |
| 2 |
-- |
| 3 |
-- Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
-- of this software and associated documentation files (the "Software"), to deal |
| 5 |
-- in the Software without restriction, including without limitation the rights |
| 6 |
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
-- copies of the Software, and to permit persons to whom the Software is |
| 8 |
-- furnished to do so, subject to the following conditions: |
| 9 |
-- |
| 10 |
-- The above copyright notice and this permission notice shall be included in |
| 11 |
-- all copies or substantial portions of the Software. |
| 12 |
-- |
| 13 |
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
-- THE SOFTWARE. |
| 20 |
|
| 21 |
library ieee ; |
| 22 |
use ieee.std_logic_1164.all ; |
| 23 |
use ieee.numeric_std.all ; |
| 24 |
|
| 25 |
entity wishbone_master_tb is |
| 26 |
end entity ; |
| 27 |
|
| 28 |
architecture arch of wishbone_master_tb is |
| 29 |
constant ADDR_BITS : integer := 32; |
| 30 |
constant DATA_BITS : integer := 32; |
| 31 |
|
| 32 |
signal clock : std_logic := '1' ; |
| 33 |
signal reset : std_logic := '1' ; |
| 34 |
|
| 35 |
signal addr : std_logic_vector(ADDR_BITS-1 downto 0) := (others => '0'); |
| 36 |
signal din : std_logic_vector(DATA_BITS-1 downto 0) := (others => '0'); |
| 37 |
signal dout : std_logic_vector(DATA_BITS-1 downto 0) ; |
| 38 |
signal write : std_logic := '0' ; |
| 39 |
signal read : std_logic := '0' ; |
| 40 |
signal waitreq : std_logic ; |
| 41 |
signal readack : std_logic ; |
| 42 |
signal intr : std_logic ; |
| 43 |
|
| 44 |
signal arb_request : std_logic_vector(3 downto 0) ; |
| 45 |
signal arb_ack : std_logic_vector(3 downto 0) ; |
| 46 |
signal arb_granted : std_logic_vector(3 downto 0) ; |
| 47 |
|
| 48 |
procedure read_csr( |
| 49 |
signal clock : in std_logic ; |
| 50 |
signal addr : out std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 51 |
signal dout : in std_logic_vector(DATA_BITS-1 downto 0) ; |
| 52 |
signal read : out std_logic ; |
| 53 |
signal readack : in std_logic ; |
| 54 |
reg : in integer ; |
| 55 |
rv : out std_logic_vector(DATA_BITS-1 downto 0) |
| 56 |
) is |
| 57 |
begin |
| 58 |
addr <= std_logic_vector(to_unsigned(reg, addr'length)) ; |
| 59 |
read <= '1' ; |
| 60 |
wait until rising_edge(clock) and readack = '1' ; |
| 61 |
rv := std_logic_vector(dout) ; |
| 62 |
read <= '0' ; |
| 63 |
wait until rising_edge(clock) ; |
| 64 |
wait until rising_edge(clock) ; |
| 65 |
end procedure ; |
| 66 |
|
| 67 |
procedure write_csr( |
| 68 |
signal clock : in std_logic ; |
| 69 |
signal addr : out std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 70 |
signal din : out std_logic_vector(DATA_BITS-1 downto 0) ; |
| 71 |
signal write : out std_logic ; |
| 72 |
reg : in integer ; |
| 73 |
rv : std_logic_vector(DATA_BITS-1 downto 0) |
| 74 |
) is |
| 75 |
begin |
| 76 |
addr <= std_logic_vector(to_unsigned(reg, addr'length)) ; |
| 77 |
din <= std_logic_vector(rv) ; |
| 78 |
write <= '1' ; |
| 79 |
wait until rising_edge(clock) ; |
| 80 |
write <= '0' ; |
| 81 |
wait until rising_edge(clock) ; |
| 82 |
end procedure ; |
| 83 |
|
| 84 |
procedure nop( signal clock : in std_logic ; count : natural ) is |
| 85 |
begin |
| 86 |
for i in 1 to count loop |
| 87 |
wait until rising_edge(clock) ; |
| 88 |
end loop ; |
| 89 |
end procedure ; |
| 90 |
|
| 91 |
procedure clear_intr( |
| 92 |
signal clock : in std_logic ; |
| 93 |
signal addr : out std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 94 |
signal din : out std_logic_vector(DATA_BITS-1 downto 0) ; |
| 95 |
signal write : out std_logic |
| 96 |
) is |
| 97 |
begin |
| 98 |
addr <= std_logic_vector(to_unsigned(8, addr'length)) ; |
| 99 |
din <= std_logic_vector(to_unsigned(1, din'length)) ; |
| 100 |
write <= '1' ; |
| 101 |
wait until rising_edge(clock) ; |
| 102 |
write <= '0' ; |
| 103 |
wait until rising_edge(clock) ; |
| 104 |
end procedure ; |
| 105 |
|
| 106 |
procedure set_intr( |
| 107 |
signal clock : in std_logic ; |
| 108 |
signal addr : out std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 109 |
signal din : out std_logic_vector(DATA_BITS-1 downto 0) ; |
| 110 |
signal write : out std_logic |
| 111 |
) is |
| 112 |
begin |
| 113 |
addr <= std_logic_vector(to_unsigned(8, addr'length)) ; |
| 114 |
din <= std_logic_vector(to_unsigned(0, din'length)) ; |
| 115 |
write <= '1' ; |
| 116 |
wait until rising_edge(clock) ; |
| 117 |
write <= '0' ; |
| 118 |
wait until rising_edge(clock) ; |
| 119 |
end procedure ; |
| 120 |
|
| 121 |
signal wb_clk_i : std_logic := '0' ; |
| 122 |
signal wb_rst_i : std_logic ; |
| 123 |
|
| 124 |
signal wb_adr_o : std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 125 |
signal wb_dat_o : std_logic_vector(DATA_BITS-1 downto 0) ; |
| 126 |
signal wb_dat_i : std_logic_vector(DATA_BITS-1 downto 0) ; |
| 127 |
signal wb_we_o : std_logic ; |
| 128 |
signal wb_sel_o : std_logic ; |
| 129 |
signal wb_stb_o : std_logic ; |
| 130 |
signal wb_ack_i : std_logic ; |
| 131 |
signal wb_cyc_o : std_logic ; |
| 132 |
begin |
| 133 |
|
| 134 |
wb_clk_i <= not wb_clk_i after 3 ns ; |
| 135 |
process(wb_clk_i) |
| 136 |
variable idx : integer := 0; |
| 137 |
begin |
| 138 |
if(rising_edge(wb_clk_i)) then |
| 139 |
wb_ack_i <= wb_cyc_o; |
| 140 |
if (wb_cyc_o = '1' and wb_we_o = '0' ) then |
| 141 |
idx := idx + 1; |
| 142 |
wb_dat_i <= wb_dat_o(wb_dat_o'high-1 downto 0) & '0'; |
| 143 |
end if; |
| 144 |
end if; |
| 145 |
end process; |
| 146 |
|
| 147 |
|
| 148 |
clock <= not clock after 1 ns ; |
| 149 |
|
| 150 |
U_wishbone_master : entity work.wishbone_master |
| 151 |
port map ( |
| 152 |
clock => clock, |
| 153 |
reset => reset, |
| 154 |
|
| 155 |
addr => addr, |
| 156 |
din => din, |
| 157 |
dout => dout, |
| 158 |
write => write, |
| 159 |
read => read, |
| 160 |
waitreq => waitreq, |
| 161 |
readack => readack, |
| 162 |
intr => intr, |
| 163 |
|
| 164 |
wb_clk_i => wb_clk_i, |
| 165 |
wb_rst_i => wb_rst_i, |
| 166 |
|
| 167 |
wb_adr_o => wb_adr_o, |
| 168 |
wb_dat_o => wb_dat_o, |
| 169 |
wb_dat_i => wb_dat_i, |
| 170 |
wb_we_o => wb_we_o, |
| 171 |
wb_sel_o => wb_sel_o, |
| 172 |
wb_stb_o => wb_stb_o, |
| 173 |
wb_ack_i => wb_ack_i, |
| 174 |
wb_cyc_o => wb_cyc_o |
| 175 |
|
| 176 |
) ; |
| 177 |
|
| 178 |
tb : process |
| 179 |
variable ts : std_logic_vector(DATA_BITS-1 downto 0) := (others =>'0') ; |
| 180 |
begin |
| 181 |
arb_request <= ( others => '0' ) ; |
| 182 |
arb_ack <= ( others => '0' ) ; |
| 183 |
reset <= '1' ; |
| 184 |
nop( clock, 100 ) ; |
| 185 |
|
| 186 |
reset <= '0' ; |
| 187 |
nop( clock, 100 ) ; |
| 188 |
|
| 189 |
-- read CSR |
| 190 |
read_csr( clock, addr, dout, read, readack, 1000, ts ) ; |
| 191 |
nop( clock, 1000 ) ; |
| 192 |
|
| 193 |
-- NiOS request |
| 194 |
write_csr( clock, addr, din, write, 3, x"00000001" ) ; |
| 195 |
|
| 196 |
-- NiOS should be granted |
| 197 |
nop( clock, 100 ) ; |
| 198 |
read_csr( clock, addr, dout, read, readack, 1, ts ) ; |
| 199 |
|
| 200 |
-- NiOS ACK |
| 201 |
nop( clock, 1000 ) ; |
| 202 |
write_csr( clock, addr, din, write, 0, x"00000002" ) ; |
| 203 |
|
| 204 |
nop( clock, 1000 ) ; |
| 205 |
-- NiOS request #2 |
| 206 |
write_csr( clock, addr, din, write, 8, x"00000000" ) ; |
| 207 |
write_csr( clock, addr, din, write, 0, x"00000001" ) ; |
| 208 |
nop( clock, 1000 ) ; |
| 209 |
|
| 210 |
-- NiOS ACK #2 (with intr) |
| 211 |
nop( clock, 1000 ) ; |
| 212 |
write_csr( clock, addr, din, write, 8, x"00000001" ) ; |
| 213 |
nop( clock, 1000 ) ; |
| 214 |
write_csr( clock, addr, din, write, 0, x"00000002" ) ; |
| 215 |
|
| 216 |
nop( clock, 1000) ; |
| 217 |
-- Read the time back |
| 218 |
report "-- End of Simulation" severity failure ; |
| 219 |
end process ; |
| 220 |
|
| 221 |
end architecture ; |
| 222 |
|