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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_busswitch.vhd] - Blame information for rev 36

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

Line No. Rev Author Line
1 12 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Bus Switch >>                                                                    #
3
-- # ********************************************************************************************* #
4
-- # Allows to access a single peripheral bus ("p_bus") by two controller busses. Controller port  #
5
-- # A ("ca_bus") has priority over controller port B ("cb_bus").                                  #
6
-- # ********************************************************************************************* #
7
-- # BSD 3-Clause License                                                                          #
8
-- #                                                                                               #
9
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
10
-- #                                                                                               #
11
-- # Redistribution and use in source and binary forms, with or without modification, are          #
12
-- # permitted provided that the following conditions are met:                                     #
13
-- #                                                                                               #
14
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
-- #    conditions and the following disclaimer.                                                   #
16
-- #                                                                                               #
17
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
19
-- #    provided with the distribution.                                                            #
20
-- #                                                                                               #
21
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
-- #    endorse or promote products derived from this software without specific prior written      #
23
-- #    permission.                                                                                #
24
-- #                                                                                               #
25
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
-- # ********************************************************************************************* #
35
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
36
-- #################################################################################################
37
 
38
library ieee;
39
use ieee.std_logic_1164.all;
40
use ieee.numeric_std.all;
41
 
42
library neorv32;
43
use neorv32.neorv32_package.all;
44
 
45
entity neorv32_busswitch is
46
  generic (
47
    PORT_CA_READ_ONLY : boolean := false; -- set if controller port A is read-only
48
    PORT_CB_READ_ONLY : boolean := false  -- set if controller port B is read-only
49
  );
50
  port (
51
    -- global control --
52
    clk_i           : in  std_ulogic; -- global clock, rising edge
53
    rstn_i          : in  std_ulogic; -- global reset, low-active, async
54
    -- controller interface a --
55
    ca_bus_addr_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
56
    ca_bus_rdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
57
    ca_bus_wdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
58
    ca_bus_ben_i    : in  std_ulogic_vector(03 downto 0); -- byte enable
59
    ca_bus_we_i     : in  std_ulogic; -- write enable
60
    ca_bus_re_i     : in  std_ulogic; -- read enable
61
    ca_bus_cancel_i : in  std_ulogic; -- cancel current bus transaction
62
    ca_bus_ack_o    : out std_ulogic; -- bus transfer acknowledge
63
    ca_bus_err_o    : out std_ulogic; -- bus transfer error
64
    -- controller interface b --
65
    cb_bus_addr_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
66
    cb_bus_rdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
67
    cb_bus_wdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
68
    cb_bus_ben_i    : in  std_ulogic_vector(03 downto 0); -- byte enable
69
    cb_bus_we_i     : in  std_ulogic; -- write enable
70
    cb_bus_re_i     : in  std_ulogic; -- read enable
71
    cb_bus_cancel_i : in  std_ulogic; -- cancel current bus transaction
72
    cb_bus_ack_o    : out std_ulogic; -- bus transfer acknowledge
73
    cb_bus_err_o    : out std_ulogic; -- bus transfer error
74
    -- peripheral bus --
75 36 zero_gravi
    p_bus_src_o     : out std_ulogic; -- access source: 0 = A, 1 = B
76 12 zero_gravi
    p_bus_addr_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
77
    p_bus_rdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
78
    p_bus_wdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
79
    p_bus_ben_o     : out std_ulogic_vector(03 downto 0); -- byte enable
80
    p_bus_we_o      : out std_ulogic; -- write enable
81
    p_bus_re_o      : out std_ulogic; -- read enable
82
    p_bus_cancel_o  : out std_ulogic; -- cancel current bus transaction
83
    p_bus_ack_i     : in  std_ulogic; -- bus transfer acknowledge
84
    p_bus_err_i     : in  std_ulogic  -- bus transfer error
85
  );
86
end neorv32_busswitch;
87
 
88
architecture neorv32_busswitch_rtl of neorv32_busswitch is
89
 
90
  -- access buffer --
91
  signal ca_rd_req_buf : std_ulogic;
92
  signal ca_wr_req_buf : std_ulogic;
93
  signal cb_rd_req_buf : std_ulogic;
94
  signal cb_wr_req_buf : std_ulogic;
95
 
96
  -- access requests --
97
  signal ca_req_current  : std_ulogic;
98
  signal cb_req_current  : std_ulogic;
99
  signal ca_req_buffered : std_ulogic;
100
  signal cb_req_buffered : std_ulogic;
101
 
102
  -- internal bus lines --
103
  signal ca_bus_ack : std_ulogic;
104
  signal cb_bus_ack : std_ulogic;
105
  signal ca_bus_err : std_ulogic;
106
  signal cb_bus_err : std_ulogic;
107
  signal p_bus_we   : std_ulogic;
108
  signal p_bus_re   : std_ulogic;
109
 
110
  -- access arbiter --
111
  type arbiter_state_t is (IDLE, BUSY, RETIRE, BUSY_SWITCHED, RETIRE_SWITCHED);
112
  type arbiter_t is record
113
    state     : arbiter_state_t;
114
    state_nxt : arbiter_state_t;
115
    bus_sel   : std_ulogic;
116
    re_trig   : std_ulogic;
117
    we_trig   : std_ulogic;
118
  end record;
119
  signal arbiter : arbiter_t;
120
 
121
begin
122
 
123
  -- Access Buffer --------------------------------------------------------------------------
124
  -- -------------------------------------------------------------------------------------------
125
  access_buffer: process(rstn_i, clk_i)
126
  begin
127
    if (rstn_i = '0') then
128
      ca_rd_req_buf <= '0';
129
      ca_wr_req_buf <= '0';
130
      cb_rd_req_buf <= '0';
131
      cb_wr_req_buf <= '0';
132
    elsif rising_edge(clk_i) then
133
 
134
      -- controller A requests --
135
      if (ca_rd_req_buf = '0') and (ca_wr_req_buf = '0') then -- idle
136
        ca_rd_req_buf <= ca_bus_re_i;
137
        ca_wr_req_buf <= ca_bus_we_i;
138
      elsif (ca_bus_cancel_i = '1') or -- controller cancels access
139
            (ca_bus_err = '1') or -- peripheral cancels access
140
            (ca_bus_ack = '1') then -- normal termination
141
        ca_rd_req_buf <= '0';
142
        ca_wr_req_buf <= '0';
143
      end if;
144
 
145
      -- controller B requests --
146
      if (cb_rd_req_buf = '0') and (cb_wr_req_buf = '0') then
147
        cb_rd_req_buf <= cb_bus_re_i;
148
        cb_wr_req_buf <= cb_bus_we_i;
149
      elsif (cb_bus_cancel_i = '1') or -- controller cancels access
150
            (cb_bus_err = '1') or -- peripheral cancels access
151
            (cb_bus_ack = '1') then -- normal termination
152
        cb_rd_req_buf <= '0';
153
        cb_wr_req_buf <= '0';
154
      end if;
155
 
156
    end if;
157
  end process access_buffer;
158
 
159
  -- any current requests? --
160
  ca_req_current <= (ca_bus_re_i or ca_bus_we_i) when (PORT_CA_READ_ONLY = false) else ca_bus_re_i;
161
  cb_req_current <= (cb_bus_re_i or cb_bus_we_i) when (PORT_CB_READ_ONLY = false) else cb_bus_re_i;
162
 
163
  -- any buffered requests? --
164
  ca_req_buffered <= (ca_rd_req_buf or ca_wr_req_buf) when (PORT_CA_READ_ONLY = false) else ca_rd_req_buf;
165
  cb_req_buffered <= (cb_rd_req_buf or cb_wr_req_buf) when (PORT_CB_READ_ONLY = false) else cb_rd_req_buf;
166
 
167
 
168
  -- Access Arbiter Sync --------------------------------------------------------------------
169
  -- -------------------------------------------------------------------------------------------
170
  -- for registers that require a specific reset state --
171
  arbiter_sync: process(rstn_i, clk_i)
172
  begin
173
    if (rstn_i = '0') then
174
      arbiter.state <= IDLE;
175
    elsif rising_edge(clk_i) then
176
      arbiter.state <= arbiter.state_nxt;
177
    end if;
178
  end process arbiter_sync;
179
 
180
 
181
  -- Peripheral Bus Arbiter -----------------------------------------------------------------
182
  -- -------------------------------------------------------------------------------------------
183
  arbiter_comb: process(arbiter, ca_req_current, cb_req_current, ca_req_buffered, cb_req_buffered,
184
                        ca_rd_req_buf, ca_wr_req_buf, cb_rd_req_buf, cb_wr_req_buf,
185
                        ca_bus_cancel_i, cb_bus_cancel_i, p_bus_ack_i, p_bus_err_i)
186
  begin
187
    -- arbiter defaults --
188
    arbiter.state_nxt <= arbiter.state;
189
    arbiter.bus_sel   <= '0';
190
    arbiter.we_trig   <= '0';
191
    arbiter.re_trig   <= '0';
192 36 zero_gravi
    --
193
    p_bus_src_o <= '0';
194 12 zero_gravi
 
195
    -- state machine --
196
    case arbiter.state is
197
 
198
      when IDLE => -- Controller a has full bus access
199
      -- ------------------------------------------------------------
200 36 zero_gravi
        p_bus_src_o <= '0'; -- access from port A
201 12 zero_gravi
        if (ca_req_current = '1') then -- current request?
202
          arbiter.bus_sel   <= '0';
203
          arbiter.state_nxt <= BUSY;
204
        elsif (ca_req_buffered = '1') then -- buffered request?
205
          arbiter.bus_sel   <= '0';
206
          arbiter.state_nxt <= RETIRE;
207
        elsif (cb_req_current = '1') then -- current request from controller b?
208
          arbiter.bus_sel   <= '1';
209
          arbiter.state_nxt <= BUSY_SWITCHED;
210
        elsif (cb_req_buffered = '1') then -- buffered request from controller b?
211
          arbiter.bus_sel   <= '1';
212
          arbiter.state_nxt <= RETIRE_SWITCHED;
213
        end if;
214
 
215
      when BUSY => -- transaction in progress
216
      -- ------------------------------------------------------------
217 36 zero_gravi
        p_bus_src_o     <= '0'; -- access from port A
218 12 zero_gravi
        arbiter.bus_sel <= '0';
219
        if (ca_bus_cancel_i = '1') or -- controller cancels access
220
           (p_bus_err_i = '1') or -- peripheral cancels access
221
           (p_bus_ack_i = '1') then -- normal termination
222
          arbiter.state_nxt <= IDLE;
223
        end if;
224
 
225
      when RETIRE => -- retire pending access
226
      -- ------------------------------------------------------------
227 36 zero_gravi
        p_bus_src_o     <= '0'; -- access from port A
228 12 zero_gravi
        arbiter.bus_sel <= '0';
229
        if (PORT_CA_READ_ONLY = false) then
230
          arbiter.we_trig <= ca_wr_req_buf;
231
        end if;
232
        arbiter.re_trig   <= ca_rd_req_buf;
233
        arbiter.state_nxt <= BUSY;
234
 
235
      when BUSY_SWITCHED => -- switched transaction in progress
236
      -- ------------------------------------------------------------
237 36 zero_gravi
        p_bus_src_o     <= '1'; -- access from port B
238 12 zero_gravi
        arbiter.bus_sel <= '1';
239
        if (cb_bus_cancel_i = '1') or -- controller cancels access
240
           (p_bus_err_i = '1') or -- peripheral cancels access
241
           (p_bus_ack_i = '1') then -- normal termination
242
          arbiter.state_nxt <= IDLE;
243
        end if;
244
 
245
      when RETIRE_SWITCHED => -- retire pending switched access
246
      -- ------------------------------------------------------------
247 36 zero_gravi
        p_bus_src_o     <= '1'; -- access from port B
248 12 zero_gravi
        arbiter.bus_sel <= '1';
249
        if (PORT_CB_READ_ONLY = false) then
250
          arbiter.we_trig <= cb_wr_req_buf;
251
        end if;
252
        arbiter.re_trig   <= cb_rd_req_buf;
253
        arbiter.state_nxt <= BUSY_SWITCHED;
254
 
255
    end case;
256
  end process arbiter_comb;
257
 
258
 
259
  -- Peripheral Bus Switch ------------------------------------------------------------------
260
  -- -------------------------------------------------------------------------------------------
261 36 zero_gravi
  p_bus_addr_o   <= ca_bus_addr_i   when (arbiter.bus_sel = '0')    else cb_bus_addr_i;
262
  p_bus_wdata_o  <= cb_bus_wdata_i  when (PORT_CA_READ_ONLY = true) else ca_bus_wdata_i when (PORT_CB_READ_ONLY = true) else
263
                    ca_bus_wdata_i  when (arbiter.bus_sel = '0')    else cb_bus_wdata_i;
264
  p_bus_ben_o    <= cb_bus_ben_i    when (PORT_CA_READ_ONLY = true) else ca_bus_ben_i   when (PORT_CB_READ_ONLY = true) else
265
                    ca_bus_ben_i    when (arbiter.bus_sel = '0')    else cb_bus_ben_i;
266
  p_bus_we       <= ca_bus_we_i     when (arbiter.bus_sel = '0')    else cb_bus_we_i;
267
  p_bus_re       <= ca_bus_re_i     when (arbiter.bus_sel = '0')    else cb_bus_re_i;
268
  p_bus_cancel_o <= ca_bus_cancel_i when (arbiter.bus_sel = '0')    else cb_bus_cancel_i;
269 12 zero_gravi
  p_bus_we_o     <= (p_bus_we or arbiter.we_trig);
270
  p_bus_re_o     <= (p_bus_re or arbiter.re_trig);
271
 
272
  ca_bus_rdata_o <= p_bus_rdata_i;
273
  cb_bus_rdata_o <= p_bus_rdata_i;
274
 
275
  ca_bus_ack     <= p_bus_ack_i and (not arbiter.bus_sel);
276
  cb_bus_ack     <= p_bus_ack_i and (    arbiter.bus_sel);
277
  ca_bus_ack_o   <= ca_bus_ack;
278
  cb_bus_ack_o   <= cb_bus_ack;
279
 
280
  ca_bus_err     <= p_bus_err_i and (not arbiter.bus_sel);
281
  cb_bus_err     <= p_bus_err_i and (    arbiter.bus_sel);
282
  ca_bus_err_o   <= ca_bus_err;
283
  cb_bus_err_o   <= cb_bus_err;
284
 
285
 
286
end neorv32_busswitch_rtl;

powered by: WebSVN 2.1.0

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