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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_wishbone.vhd] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - External Bus Interface (WISHBONE) >>                                             #
3
-- # ********************************************************************************************* #
4 35 zero_gravi
-- # The interface provides registers for all outgoing signals. If the host cancels a running      #
5 39 zero_gravi
-- # transfer, the Wishbone arbiter still waits some time for the bus system to ACK the transfer   #
6
-- # before the arbiter forces termination.                                                        #
7 23 zero_gravi
-- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
8 39 zero_gravi
-- # Even when all processor-internal memories and IO devices are disabled, the EXTERNAL address   #
9
-- # space ENDS at address 0xffff0000 (begin of internal BOOTROM address space).                   #
10 35 zero_gravi
-- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
11 39 zero_gravi
-- # All bus accesses from the CPU, which do not target the internal IO region / the internal      #
12
-- # bootlloader / the internal instruction or data memories (if implemented), are delegated via   #
13
-- # this Wishbone gateway to the external bus interface. Accessed peripherals can have a response #
14
-- # latency of up to neorv32_package.vhd:bus_timeout_c - 2 cycles.                                #
15
-- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
16 35 zero_gravi
-- # This interface supports classic/standard Wishbone transactions (WB_PIPELINED_MODE = false)    #
17
-- # and also pipelined transactions (WB_PIPELINED_MODE = true).                                   #
18 2 zero_gravi
-- # ********************************************************************************************* #
19
-- # BSD 3-Clause License                                                                          #
20
-- #                                                                                               #
21
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
22
-- #                                                                                               #
23
-- # Redistribution and use in source and binary forms, with or without modification, are          #
24
-- # permitted provided that the following conditions are met:                                     #
25
-- #                                                                                               #
26
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
27
-- #    conditions and the following disclaimer.                                                   #
28
-- #                                                                                               #
29
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
30
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
31
-- #    provided with the distribution.                                                            #
32
-- #                                                                                               #
33
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
34
-- #    endorse or promote products derived from this software without specific prior written      #
35
-- #    permission.                                                                                #
36
-- #                                                                                               #
37
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
38
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
39
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
40
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
41
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
42
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
43
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
44
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
45
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
46
-- # ********************************************************************************************* #
47
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
48
-- #################################################################################################
49
 
50
library ieee;
51
use ieee.std_logic_1164.all;
52
use ieee.numeric_std.all;
53
 
54
library neorv32;
55
use neorv32.neorv32_package.all;
56
 
57
entity neorv32_wishbone is
58
  generic (
59 39 zero_gravi
    WB_PIPELINED_MODE : boolean := false;  -- false: classic/standard wishbone mode, true: pipelined wishbone mode
60 23 zero_gravi
    -- Internal instruction memory --
61 35 zero_gravi
    MEM_INT_IMEM_USE  : boolean := true;   -- implement processor-internal instruction memory
62
    MEM_INT_IMEM_SIZE : natural := 8*1024; -- size of processor-internal instruction memory in bytes
63 23 zero_gravi
    -- Internal data memory --
64 35 zero_gravi
    MEM_INT_DMEM_USE  : boolean := true;   -- implement processor-internal data memory
65
    MEM_INT_DMEM_SIZE : natural := 4*1024  -- size of processor-internal data memory in bytes
66 2 zero_gravi
  );
67
  port (
68
    -- global control --
69 39 zero_gravi
    clk_i     : in  std_ulogic; -- global clock line
70
    rstn_i    : in  std_ulogic; -- global reset line, low-active
71 2 zero_gravi
    -- host access --
72 39 zero_gravi
    src_i     : in  std_ulogic; -- access type (0: data, 1:instruction)
73
    addr_i    : in  std_ulogic_vector(31 downto 0); -- address
74
    rden_i    : in  std_ulogic; -- read enable
75
    wren_i    : in  std_ulogic; -- write enable
76
    ben_i     : in  std_ulogic_vector(03 downto 0); -- byte write enable
77
    data_i    : in  std_ulogic_vector(31 downto 0); -- data in
78
    data_o    : out std_ulogic_vector(31 downto 0); -- data out
79
    cancel_i  : in  std_ulogic; -- cancel current bus transaction
80
    lock_i    : in  std_ulogic; -- locked/exclusive bus access
81
    ack_o     : out std_ulogic; -- transfer acknowledge
82
    err_o     : out std_ulogic; -- transfer error
83
    priv_i    : in  std_ulogic_vector(1 downto 0); -- current CPU privilege level
84 2 zero_gravi
    -- wishbone interface --
85 39 zero_gravi
    wb_tag_o  : out std_ulogic_vector(2 downto 0); -- tag
86
    wb_adr_o  : out std_ulogic_vector(31 downto 0); -- address
87
    wb_dat_i  : in  std_ulogic_vector(31 downto 0); -- read data
88
    wb_dat_o  : out std_ulogic_vector(31 downto 0); -- write data
89
    wb_we_o   : out std_ulogic; -- read/write
90
    wb_sel_o  : out std_ulogic_vector(03 downto 0); -- byte enable
91
    wb_stb_o  : out std_ulogic; -- strobe
92
    wb_cyc_o  : out std_ulogic; -- valid cycle
93
    wb_lock_o : out std_ulogic; -- locked/exclusive bus access
94
    wb_ack_i  : in  std_ulogic; -- transfer acknowledge
95
    wb_err_i  : in  std_ulogic  -- transfer error
96 2 zero_gravi
  );
97
end neorv32_wishbone;
98
 
99
architecture neorv32_wishbone_rtl of neorv32_wishbone is
100
 
101 35 zero_gravi
  -- constants --
102 39 zero_gravi
  constant xbus_timeout_c : natural := bus_timeout_c/4;
103 35 zero_gravi
 
104 2 zero_gravi
  -- access control --
105 39 zero_gravi
  signal int_imem_acc : std_ulogic;
106
  signal int_dmem_acc : std_ulogic;
107
  signal int_boot_acc : std_ulogic;
108
  signal xbus_access  : std_ulogic;
109 2 zero_gravi
 
110 35 zero_gravi
  -- bus arbiter
111 38 zero_gravi
  type ctrl_state_t is (IDLE, BUSY, CANCELED, RESYNC);
112 35 zero_gravi
  type ctrl_t is record
113 39 zero_gravi
    state   : ctrl_state_t;
114
    we      : std_ulogic;
115
    rd_req  : std_ulogic;
116
    wr_req  : std_ulogic;
117
    adr     : std_ulogic_vector(31 downto 0);
118
    wdat    : std_ulogic_vector(31 downto 0);
119
    rdat    : std_ulogic_vector(31 downto 0);
120
    sel     : std_ulogic_vector(3 downto 0);
121
    ack     : std_ulogic;
122
    err     : std_ulogic;
123
    timeout : std_ulogic_vector(index_size_f(xbus_timeout_c)-1 downto 0);
124
    src     : std_ulogic;
125
    lock    : std_ulogic;
126
    priv    : std_ulogic_vector(1 downto 0);
127 35 zero_gravi
  end record;
128 36 zero_gravi
  signal ctrl    : ctrl_t;
129
  signal stb_int : std_ulogic;
130
  signal cyc_int : std_ulogic;
131 2 zero_gravi
 
132
begin
133
 
134 35 zero_gravi
  -- Sanity Checks --------------------------------------------------------------------------
135 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
136 39 zero_gravi
  -- max bus timeout latency lower than recommended --
137
  assert not (bus_timeout_c <= 32) report "NEORV32 PROCESSOR CONFIG ERROR: Bus timeout (neorv32_package.vhd:bus_timeout_c) should be >32 when using external bus interface." severity error;
138
  -- external memory iterface protocol + max timeout latency notifier (warning) --
139
  assert not (wb_pipe_mode_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: Implementing external memory interface using STANDARD Wishbone protocol with max latency = " & integer'image(bus_timeout_c) & " cycles." severity warning;
140
  assert not (wb_pipe_mode_c =  true) report "NEORV32 PROCESSOR CONFIG NOTE: Implementing external memory interface using PIEPLINED Wishbone protocol with max latency = " & integer'image(bus_timeout_c) & " cycles." severity warning;
141 2 zero_gravi
 
142
 
143
  -- Access Control -------------------------------------------------------------------------
144
  -- -------------------------------------------------------------------------------------------
145 39 zero_gravi
  -- access to processor-internal IMEM or DMEM? --
146
  int_imem_acc <= '1' when (addr_i(31 downto index_size_f(MEM_INT_IMEM_SIZE)) = imem_base_c(31 downto index_size_f(MEM_INT_IMEM_SIZE))) and (MEM_INT_IMEM_USE = true) else '0';
147
  int_dmem_acc <= '1' when (addr_i(31 downto index_size_f(MEM_INT_DMEM_SIZE)) = dmem_base_c(31 downto index_size_f(MEM_INT_DMEM_SIZE))) and (MEM_INT_DMEM_USE = true) else '0';
148
  -- access to processor-internal BOOTROM or IO devices? --
149
  int_boot_acc <= '1' when (addr_i(31 downto 16) = boot_rom_base_c(31 downto 16)) else '0'; -- hacky!
150 2 zero_gravi
  -- actual external bus access? --
151 39 zero_gravi
  xbus_access <= (not int_imem_acc) and (not int_dmem_acc) and (not int_boot_acc);
152 2 zero_gravi
 
153
  -- Bus Arbiter -----------------------------------------------------------------------------
154
  -- -------------------------------------------------------------------------------------------
155
  bus_arbiter: process(rstn_i, clk_i)
156
  begin
157
    if (rstn_i = '0') then
158 39 zero_gravi
      ctrl.state   <= IDLE;
159
      ctrl.we      <= '0';
160
      ctrl.rd_req  <= '0';
161
      ctrl.wr_req  <= '0';
162
      ctrl.adr     <= (others => '0');
163
      ctrl.wdat    <= (others => '0');
164
      ctrl.rdat    <= (others => '0');
165
      ctrl.sel     <= (others => '0');
166
      ctrl.timeout <= (others => '0');
167
      ctrl.ack     <= '0';
168
      ctrl.err     <= '0';
169
      ctrl.src     <= '0';
170
      ctrl.lock    <= '0';
171
      ctrl.priv    <= "00";
172 2 zero_gravi
    elsif rising_edge(clk_i) then
173 35 zero_gravi
      -- defaults --
174 39 zero_gravi
      ctrl.rdat    <= (others => '0');
175
      ctrl.ack     <= '0';
176
      ctrl.err     <= '0';
177
      ctrl.timeout <= std_ulogic_vector(to_unsigned(xbus_timeout_c, index_size_f(xbus_timeout_c)));
178 2 zero_gravi
 
179 35 zero_gravi
      -- state machine --
180
      case ctrl.state is
181 2 zero_gravi
 
182 35 zero_gravi
        when IDLE => -- waiting for host request
183
        -- ------------------------------------------------------------
184
          ctrl.rd_req <= '0';
185
          ctrl.wr_req <= '0';
186
          -- buffer all outgoing signals --
187
          ctrl.we   <= wren_i;
188
          ctrl.adr  <= addr_i;
189
          ctrl.wdat <= data_i;
190
          ctrl.sel  <= ben_i;
191 36 zero_gravi
          ctrl.src  <= src_i;
192 39 zero_gravi
          ctrl.lock <= lock_i;
193 36 zero_gravi
          ctrl.priv <= priv_i;
194 39 zero_gravi
          -- valid new or buffered read/write request --
195
          if ((xbus_access and (wren_i or ctrl.wr_req or rden_i or ctrl.rd_req)) = '1') then
196 35 zero_gravi
            ctrl.state <= BUSY;
197
          end if;
198 2 zero_gravi
 
199 35 zero_gravi
        when BUSY => -- transfer in progress
200
        -- ------------------------------------------------------------
201
          ctrl.rdat <= wb_dat_i;
202
          if (cancel_i = '1') then -- transfer canceled by host
203
            ctrl.state <= CANCELED;
204
          elsif (wb_err_i = '1') then -- abnormal bus termination
205
            ctrl.err   <= '1';
206
            ctrl.state <= CANCELED;
207
          elsif (wb_ack_i = '1') then -- normal bus termination
208
            ctrl.ack   <= '1';
209
            ctrl.state <= IDLE;
210
          end if;
211 2 zero_gravi
 
212 38 zero_gravi
        when CANCELED => -- wait for cycle to be completed either by peripheral or by timeout (ignore result of transfer)
213 35 zero_gravi
        -- ------------------------------------------------------------
214
          ctrl.wr_req <= ctrl.wr_req or wren_i; -- buffer new request
215
          ctrl.rd_req <= ctrl.rd_req or rden_i; -- buffer new request
216
          -- wait for bus.peripheral to ACK transfer (as "aborted" but still somehow "completed")
217
          -- or wait for a timeout and force termination
218
          ctrl.timeout <= std_ulogic_vector(unsigned(ctrl.timeout) - 1); -- timeout counter
219
          if (wb_ack_i = '1') or (or_all_f(ctrl.timeout) = '0') then
220 38 zero_gravi
            ctrl.state <= RESYNC;
221
          end if;
222
 
223
        when RESYNC => -- make sure transfer is done!
224
        -- ------------------------------------------------------------
225
          if (wb_ack_i = '0') then
226 35 zero_gravi
            ctrl.state <= IDLE;
227
          end if;
228 2 zero_gravi
 
229 35 zero_gravi
        when others => -- undefined
230
        -- ------------------------------------------------------------
231
          ctrl.state <= IDLE;
232 2 zero_gravi
 
233 35 zero_gravi
      end case;
234
    end if;
235
  end process bus_arbiter;
236 23 zero_gravi
 
237 35 zero_gravi
  -- host access --
238 39 zero_gravi
  data_o <= ctrl.rdat;
239
  ack_o  <= ctrl.ack;
240
  err_o  <= ctrl.err;
241 2 zero_gravi
 
242 35 zero_gravi
  -- wishbone interface --
243 36 zero_gravi
  wb_tag_o(0) <= '1' when (ctrl.priv = priv_mode_m_c) else '0'; -- privileged access when in machine mode
244 39 zero_gravi
  wb_tag_o(1) <= '0'; -- 0 = secure, 1 = non-secure
245
  wb_tag_o(2) <= ctrl.src; -- 0 = data access, 1 = instruction access
246 36 zero_gravi
 
247 39 zero_gravi
  wb_adr_o  <= ctrl.adr;
248
  wb_dat_o  <= ctrl.wdat;
249
  wb_we_o   <= ctrl.we;
250
  wb_sel_o  <= ctrl.sel;
251
  wb_lock_o <= ctrl.lock;
252
  wb_stb_o  <= stb_int when (WB_PIPELINED_MODE = true) else cyc_int;
253
  wb_cyc_o  <= cyc_int;
254 2 zero_gravi
 
255 39 zero_gravi
  stb_int <= '1' when (ctrl.state = BUSY) else '0';
256
  cyc_int <= '0' when (ctrl.state = IDLE) or (ctrl.state = RESYNC) else '1';
257 2 zero_gravi
 
258 35 zero_gravi
 
259 2 zero_gravi
end neorv32_wishbone_rtl;

powered by: WebSVN 2.1.0

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