| 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 |
library altera_mf ; |
| 26 |
use altera_mf.altera_mf_components.all ; |
| 27 |
|
| 28 |
|
| 29 |
entity wishbone_master is |
| 30 |
generic ( |
| 31 |
ADDR_BITS : integer := 32 ; |
| 32 |
DATA_BITS : integer := 32 |
| 33 |
) ; |
| 34 |
|
| 35 |
port ( |
| 36 |
-- Control signals |
| 37 |
clock : in std_logic ; |
| 38 |
reset : in std_logic ; |
| 39 |
|
| 40 |
-- Memory mapped interface |
| 41 |
addr : in std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 42 |
din : in std_logic_vector(DATA_BITS-1 downto 0) ; |
| 43 |
dout : out std_logic_vector(DATA_BITS-1 downto 0) ; |
| 44 |
write : in std_logic ; |
| 45 |
read : in std_logic ; |
| 46 |
waitreq : out std_logic ; |
| 47 |
readack : out std_logic ; |
| 48 |
intr : out std_logic ; |
| 49 |
|
| 50 |
-- Wishbone signals |
| 51 |
wb_clk_i : in std_logic ; |
| 52 |
wb_rst_i : in std_logic ; |
| 53 |
|
| 54 |
wb_adr_o : out std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 55 |
wb_dat_o : out std_logic_vector(DATA_BITS-1 downto 0) ; |
| 56 |
wb_dat_i : in std_logic_vector(DATA_BITS-1 downto 0) ; |
| 57 |
wb_we_o : out std_logic ; |
| 58 |
wb_sel_o : out std_logic ; |
| 59 |
wb_stb_o : out std_logic ; |
| 60 |
wb_ack_i : in std_logic ; |
| 61 |
wb_cyc_o : out std_logic |
| 62 |
) ; |
| 63 |
end entity ; |
| 64 |
|
| 65 |
architecture arch of wishbone_master is |
| 66 |
|
| 67 |
signal av2wb_wfull : std_logic ; |
| 68 |
signal wb_empty : std_logic ; |
| 69 |
signal wb_empty_r : std_logic ; |
| 70 |
signal wb_bus : std_logic_vector((ADDR_BITS + DATA_BITS) downto 0) ; |
| 71 |
|
| 72 |
signal wb2av_wfull : std_logic ; |
| 73 |
signal av_empty : std_logic ; |
| 74 |
signal av_bus : std_logic_vector(DATA_BITS-1 downto 0) ; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
type av_fsm_t is (INIT, WAIT_FOR_ACK) ; |
| 79 |
|
| 80 |
type av_state_t is record |
| 81 |
state : av_fsm_t; |
| 82 |
read_op : std_logic ; |
| 83 |
waitreq : std_logic ; |
| 84 |
readack : std_logic ; |
| 85 |
adr : std_logic_vector(ADDR_BITS-1 downto 0) ; |
| 86 |
din : std_logic_vector(DATA_BITS-1 downto 0) ; |
| 87 |
dout : std_logic_vector(DATA_BITS-1 downto 0) ; |
| 88 |
we : std_logic ; |
| 89 |
wreq : std_logic ; |
| 90 |
req_r : std_logic ; |
| 91 |
end record; |
| 92 |
|
| 93 |
function AV_NULL_STATE return av_state_t is |
| 94 |
variable rv : av_state_t ; |
| 95 |
begin |
| 96 |
rv.state := INIT; |
| 97 |
rv.read_op := '0'; |
| 98 |
rv.waitreq := '0'; |
| 99 |
rv.readack := '0'; |
| 100 |
|
| 101 |
rv.adr := ( others => '0' ); |
| 102 |
rv.din := ( others => '0' ); |
| 103 |
rv.dout := ( others => '0' ); |
| 104 |
rv.we := '0'; |
| 105 |
|
| 106 |
rv.wreq := '0'; |
| 107 |
rv.req_r := '0'; |
| 108 |
return rv ; |
| 109 |
end function; |
| 110 |
|
| 111 |
signal av_current : av_state_t ; |
| 112 |
signal av_future : av_state_t ; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
type wb_fsm_t is (INIT, WAIT_FOR_REQ) ; |
| 117 |
|
| 118 |
type wb_state_t is record |
| 119 |
state : wb_fsm_t; |
| 120 |
sel : std_logic ; |
| 121 |
end record; |
| 122 |
|
| 123 |
function WB_NULL_STATE return wb_state_t is |
| 124 |
variable rv : wb_state_t ; |
| 125 |
begin |
| 126 |
rv.state := INIT ; |
| 127 |
rv.sel := '0' ; |
| 128 |
return rv ; |
| 129 |
end function; |
| 130 |
|
| 131 |
|
| 132 |
signal wb_current : wb_state_t ; |
| 133 |
signal wb_future : wb_state_t ; |
| 134 |
|
| 135 |
begin |
| 136 |
|
| 137 |
av_sync_proc : process(clock, reset) |
| 138 |
begin |
| 139 |
if (reset = '1' ) then |
| 140 |
av_current <= AV_NULL_STATE ; |
| 141 |
elsif (rising_edge(clock) ) then |
| 142 |
av_current <= av_future; |
| 143 |
end if; |
| 144 |
end process; |
| 145 |
|
| 146 |
av_comb_proc : process( all ) |
| 147 |
begin |
| 148 |
av_future <= av_current; |
| 149 |
|
| 150 |
av_future.din <= ( others => '0' ); |
| 151 |
av_future.adr <= ( others => '0' ); |
| 152 |
av_future.req_r <= '0'; |
| 153 |
av_future.wreq <= '0'; |
| 154 |
av_future.readack <= '0'; |
| 155 |
|
| 156 |
case(av_current.state) is |
| 157 |
when INIT => |
| 158 |
if (read = '1' or write = '1' ) then |
| 159 |
if (av_current.req_r = '0' ) then |
| 160 |
av_future.din <= din; |
| 161 |
av_future.adr <= addr; |
| 162 |
av_future.we <= write; |
| 163 |
av_future.wreq <= '1'; |
| 164 |
|
| 165 |
av_future.waitreq <= '1'; |
| 166 |
av_future.state <= WAIT_FOR_ACK; |
| 167 |
|
| 168 |
end if; |
| 169 |
av_future.req_r <= '1'; |
| 170 |
end if; |
| 171 |
|
| 172 |
when WAIT_FOR_ACK => |
| 173 |
if (av_empty = '0' ) then |
| 174 |
av_future.req_r <= '1'; |
| 175 |
av_future.state <= INIT; |
| 176 |
av_future.waitreq <= '0'; |
| 177 |
av_future.dout <= av_bus; |
| 178 |
if (av_current.we = '0' ) then |
| 179 |
av_future.readack <= '1'; |
| 180 |
end if; |
| 181 |
end if; |
| 182 |
|
| 183 |
when others => |
| 184 |
av_future <= AV_NULL_STATE; |
| 185 |
end case; |
| 186 |
end process; |
| 187 |
|
| 188 |
wb_sync_proc : process(clock, reset) |
| 189 |
begin |
| 190 |
if (reset = '1' ) then |
| 191 |
wb_current <= WB_NULL_STATE ; |
| 192 |
elsif (rising_edge(wb_clk_i) ) then |
| 193 |
wb_current <= wb_future; |
| 194 |
end if; |
| 195 |
end process; |
| 196 |
|
| 197 |
wb_comb_proc : process( all ) |
| 198 |
begin |
| 199 |
wb_future <= wb_current; |
| 200 |
|
| 201 |
wb_future.sel <= '0'; |
| 202 |
|
| 203 |
case(wb_current.state) is |
| 204 |
when INIT => |
| 205 |
if (wb_empty = '0' ) then |
| 206 |
wb_future.sel <= '1'; |
| 207 |
wb_future.state <= WAIT_FOR_REQ; |
| 208 |
end if; |
| 209 |
when WAIT_FOR_REQ => |
| 210 |
if (wb_ack_i = '1') then |
| 211 |
wb_future.state <= INIT; |
| 212 |
else |
| 213 |
wb_future.sel <= '1'; |
| 214 |
end if; |
| 215 |
when others => |
| 216 |
wb_future <= WB_NULL_STATE; |
| 217 |
end case; |
| 218 |
end process; |
| 219 |
|
| 220 |
intr <= '0'; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
U_av2wb: dcfifo |
| 226 |
generic map ( |
| 227 |
lpm_width => (1+ADDR_BITS+DATA_BITS), |
| 228 |
lpm_widthu => 4, |
| 229 |
lpm_numwords => 16, |
| 230 |
lpm_showahead => "ON" |
| 231 |
) |
| 232 |
port map ( |
| 233 |
aclr => reset, |
| 234 |
|
| 235 |
wrclk => clock, |
| 236 |
wrreq => av_current.wreq and not av2wb_wfull, |
| 237 |
data => av_current.we & av_current.adr & av_current.din, |
| 238 |
|
| 239 |
wrfull => av2wb_wfull, |
| 240 |
wrempty => open, |
| 241 |
wrusedw => open, |
| 242 |
|
| 243 |
rdclk => wb_clk_i, |
| 244 |
rdreq => not wb_empty, |
| 245 |
q => wb_bus, |
| 246 |
|
| 247 |
rdfull => open, |
| 248 |
rdempty => wb_empty, |
| 249 |
rdusedw => open |
| 250 |
); |
| 251 |
|
| 252 |
|
| 253 |
wb_adr_o <= wb_bus(ADDR_BITS+DATA_BITS-1 downto DATA_BITS); |
| 254 |
wb_dat_o <= wb_bus(DATA_BITS-1 downto 0); |
| 255 |
wb_we_o <= wb_bus(wb_bus'high); |
| 256 |
wb_cyc_o <= not wb_empty; |
| 257 |
wb_sel_o <= wb_current.sel; |
| 258 |
wb_stb_o <= '1'; |
| 259 |
|
| 260 |
U_wb2av: dcfifo |
| 261 |
generic map ( |
| 262 |
lpm_width => 32, |
| 263 |
lpm_widthu => 4, |
| 264 |
lpm_numwords => 16, |
| 265 |
lpm_showahead => "ON" |
| 266 |
) |
| 267 |
port map ( |
| 268 |
aclr => reset, |
| 269 |
|
| 270 |
wrclk => wb_clk_i, |
| 271 |
wrreq => wb_ack_i and not wb2av_wfull, |
| 272 |
data => wb_dat_i, |
| 273 |
|
| 274 |
wrfull => wb2av_wfull, |
| 275 |
wrempty => open, |
| 276 |
wrusedw => open, |
| 277 |
|
| 278 |
rdclk => clock, |
| 279 |
rdreq => not av_empty, |
| 280 |
q => av_bus, |
| 281 |
|
| 282 |
rdfull => open, |
| 283 |
rdempty => av_empty, |
| 284 |
rdusedw => open |
| 285 |
); |
| 286 |
dout <= av_current.dout; |
| 287 |
readack <= av_current.readack; |
| 288 |
waitreq <= av_current.waitreq; |
| 289 |
|
| 290 |
end architecture ; |