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

Subversion Repositories neorv32

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

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