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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_bus.vhd] - Blame information for rev 14

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Bus Interface Unit >>                                                            #
3
-- # ********************************************************************************************* #
4 12 zero_gravi
-- # Instruction and data bus interfaces.                                                          #
5 2 zero_gravi
-- # ********************************************************************************************* #
6
-- # BSD 3-Clause License                                                                          #
7
-- #                                                                                               #
8
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
9
-- #                                                                                               #
10
-- # Redistribution and use in source and binary forms, with or without modification, are          #
11
-- # permitted provided that the following conditions are met:                                     #
12
-- #                                                                                               #
13
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
14
-- #    conditions and the following disclaimer.                                                   #
15
-- #                                                                                               #
16
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
17
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
18
-- #    provided with the distribution.                                                            #
19
-- #                                                                                               #
20
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
21
-- #    endorse or promote products derived from this software without specific prior written      #
22
-- #    permission.                                                                                #
23
-- #                                                                                               #
24
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
25
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
26
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
27
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
28
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
29
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
30
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
31
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
32
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
33
-- # ********************************************************************************************* #
34
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
35
-- #################################################################################################
36
 
37
library ieee;
38
use ieee.std_logic_1164.all;
39
use ieee.numeric_std.all;
40
 
41
library neorv32;
42
use neorv32.neorv32_package.all;
43
 
44
entity neorv32_cpu_bus is
45
  generic (
46 11 zero_gravi
    CPU_EXTENSION_RISCV_C : boolean := true; -- implement compressed extension?
47 14 zero_gravi
    BUS_TIMEOUT           : natural := 15    -- cycles after which a valid bus access will timeout
48 2 zero_gravi
  );
49
  port (
50
    -- global control --
51 12 zero_gravi
    clk_i          : in  std_ulogic; -- global clock, rising edge
52
    rstn_i         : in  std_ulogic; -- global reset, low-active, async
53
    ctrl_i         : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
54
    -- cpu instruction fetch interface --
55
    fetch_pc_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
56
    instr_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
57
    i_wait_o       : out std_ulogic; -- wait for fetch to complete
58
    --
59
    ma_instr_o     : out std_ulogic; -- misaligned instruction address
60
    be_instr_o     : out std_ulogic; -- bus error on instruction access
61
    -- cpu data access interface --
62
    addr_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result -> access address
63
    wdata_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- write data
64
    rdata_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
65
    mar_o          : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
66
    d_wait_o       : out std_ulogic; -- wait for access to complete
67
    --
68
    ma_load_o      : out std_ulogic; -- misaligned load data address
69
    ma_store_o     : out std_ulogic; -- misaligned store data address
70
    be_load_o      : out std_ulogic; -- bus error on load data access
71
    be_store_o     : out std_ulogic; -- bus error on store data access
72
    -- instruction bus --
73
    i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
74
    i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
75
    i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
76
    i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
77
    i_bus_we_o     : out std_ulogic; -- write enable
78
    i_bus_re_o     : out std_ulogic; -- read enable
79
    i_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
80
    i_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
81
    i_bus_err_i    : in  std_ulogic; -- bus transfer error
82
    i_bus_fence_o  : out std_ulogic; -- fence operation
83
    -- data bus --
84
    d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
85
    d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
86
    d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
87
    d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
88
    d_bus_we_o     : out std_ulogic; -- write enable
89
    d_bus_re_o     : out std_ulogic; -- read enable
90
    d_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
91
    d_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
92
    d_bus_err_i    : in  std_ulogic; -- bus transfer error
93
    d_bus_fence_o  : out std_ulogic  -- fence operation
94 2 zero_gravi
  );
95
end neorv32_cpu_bus;
96
 
97
architecture neorv32_cpu_bus_rtl of neorv32_cpu_bus is
98
 
99 12 zero_gravi
  -- data interface registers --
100 2 zero_gravi
  signal mar, mdo, mdi : std_ulogic_vector(data_width_c-1 downto 0);
101
 
102 12 zero_gravi
  -- data access --
103
  signal d_bus_wdata : std_ulogic_vector(data_width_c-1 downto 0); -- write data
104
  signal d_bus_rdata : std_ulogic_vector(data_width_c-1 downto 0); -- read data
105
  signal d_bus_ben   : std_ulogic_vector(3 downto 0); -- write data byte enable
106 2 zero_gravi
 
107
  -- misaligned access? --
108 12 zero_gravi
  signal d_misaligned, i_misaligned : std_ulogic;
109 2 zero_gravi
 
110 12 zero_gravi
  -- bus arbiter --
111
  type bus_arbiter_t is record
112
    rd_req    : std_ulogic; -- read access in progress
113
    wr_req    : std_ulogic; -- write access in progress
114
    err_align : std_ulogic; -- alignment error
115
    err_bus   : std_ulogic; -- bus access error
116 14 zero_gravi
    timeout   : std_ulogic_vector(index_size_f(BUS_TIMEOUT)-1 downto 0);
117 12 zero_gravi
  end record;
118
  signal i_arbiter, d_arbiter : bus_arbiter_t;
119
 
120 2 zero_gravi
begin
121
 
122 12 zero_gravi
  -- Data Interface: Access Address ---------------------------------------------------------
123 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
124
  mem_adr_reg: process(rstn_i, clk_i)
125
  begin
126 11 zero_gravi
    if rising_edge(clk_i) then
127 2 zero_gravi
      if (ctrl_i(ctrl_bus_mar_we_c) = '1') then
128 12 zero_gravi
        mar <= addr_i;
129 2 zero_gravi
      end if;
130
    end if;
131
  end process mem_adr_reg;
132
 
133 12 zero_gravi
  -- read-back for exception controller --
134
  mar_o <= mar;
135 2 zero_gravi
 
136 12 zero_gravi
  -- alignment check --
137
  misaligned_d_check: process(mar, ctrl_i)
138
  begin
139
    -- check data access --
140
    d_misaligned <= '0'; -- default
141
    case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
142
      when "00" => -- byte
143
        d_misaligned <= '0';
144
      when "01" => -- half-word
145
        if (mar(0) /= '0') then
146
          d_misaligned <= '1';
147
        end if;
148
      when others => -- word
149
        if (mar(1 downto 0) /= "00") then
150
          d_misaligned <= '1';
151
        end if;
152
    end case;
153
  end process misaligned_d_check;
154 2 zero_gravi
 
155
 
156 12 zero_gravi
  -- Data Interface: Write Data -------------------------------------------------------------
157 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
158
  mem_do_reg: process(clk_i)
159
  begin
160
    if rising_edge(clk_i) then
161
      if (ctrl_i(ctrl_bus_mdo_we_c) = '1') then
162
        mdo <= wdata_i;
163
      end if;
164
    end if;
165
  end process mem_do_reg;
166
 
167
  -- byte enable and output data alignment --
168
  byte_enable: process(mar, mdo, ctrl_i)
169
  begin
170
    case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
171
      when "00" => -- byte
172 12 zero_gravi
        d_bus_wdata(07 downto 00) <= mdo(07 downto 00);
173
        d_bus_wdata(15 downto 08) <= mdo(07 downto 00);
174
        d_bus_wdata(23 downto 16) <= mdo(07 downto 00);
175
        d_bus_wdata(31 downto 24) <= mdo(07 downto 00);
176
        d_bus_ben <= (others => '0');
177
        d_bus_ben(to_integer(unsigned(mar(1 downto 0)))) <= '1';
178 2 zero_gravi
      when "01" => -- half-word
179 12 zero_gravi
        d_bus_wdata(31 downto 16) <= mdo(15 downto 00);
180
        d_bus_wdata(15 downto 00) <= mdo(15 downto 00);
181 2 zero_gravi
        if (mar(1) = '0') then
182 12 zero_gravi
          d_bus_ben <= "0011"; -- low half-word
183 2 zero_gravi
        else
184 12 zero_gravi
          d_bus_ben <= "1100"; -- high half-word
185 2 zero_gravi
        end if;
186
      when others => -- word
187 12 zero_gravi
        d_bus_wdata <= mdo;
188
        d_bus_ben   <= "1111"; -- full word
189 2 zero_gravi
    end case;
190
  end process byte_enable;
191
 
192
 
193 12 zero_gravi
  -- Data Interface: Read Data --------------------------------------------------------------
194 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
195
  mem_out_buf: process(clk_i)
196
  begin
197
    if rising_edge(clk_i) then
198
      -- memory data in register (MDI) --
199
      if (ctrl_i(ctrl_bus_mdi_we_c) = '1') then
200 12 zero_gravi
        mdi <= d_bus_rdata;
201 2 zero_gravi
      end if;
202
    end if;
203
  end process mem_out_buf;
204
 
205 12 zero_gravi
  -- input data alignment and sign extension --
206 2 zero_gravi
  read_align: process(mdi, mar, ctrl_i)
207
    variable signed_v : std_ulogic;
208
  begin
209
    signed_v := not ctrl_i(ctrl_bus_unsigned_c);
210
    case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
211
      when "00" => -- byte
212
        case mar(1 downto 0) is
213
          when "00" =>
214
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(07)));
215
            rdata_o(07 downto 00) <= mdi(07 downto 00); -- byte 0
216
          when "01" =>
217
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(15)));
218
            rdata_o(07 downto 00) <= mdi(15 downto 08); -- byte 1
219
          when "10" =>
220
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(23)));
221
            rdata_o(07 downto 00) <= mdi(23 downto 16); -- byte 2
222
          when others =>
223
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(31)));
224
            rdata_o(07 downto 00) <= mdi(31 downto 24); -- byte 3
225
        end case;
226
      when "01" => -- half-word
227
        if (mar(1) = '0') then
228
          rdata_o(31 downto 16) <= (others => (signed_v and mdi(15)));
229
          rdata_o(15 downto 00) <= mdi(15 downto 00); -- low half-word
230
        else
231
          rdata_o(31 downto 16) <= (others => (signed_v and mdi(31)));
232
          rdata_o(15 downto 00) <= mdi(31 downto 16); -- high half-word
233
        end if;
234
      when others => -- word
235
        rdata_o <= mdi; -- full word
236
    end case;
237
  end process read_align;
238
 
239
 
240 12 zero_gravi
  -- Instruction Interface: Check for Misaligned Access -------------------------------------
241 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
242 12 zero_gravi
  misaligned_i_check: process(ctrl_i, fetch_pc_i)
243 2 zero_gravi
  begin
244 12 zero_gravi
    -- check instruction access --
245
    i_misaligned <= '0'; -- default
246
    if (CPU_EXTENSION_RISCV_C = true) then -- 16-bit and 32-bit instruction accesses
247
      i_misaligned <= '0'; -- no alignment exceptions possible
248
    else -- 32-bit instruction accesses only
249
      if (fetch_pc_i(1) = '1') then -- PC(0) is always zero
250
        i_misaligned <= '1';
251
      end if;
252
    end if;
253
  end process misaligned_i_check;
254
 
255
 
256
  -- Instruction Fetch Arbiter --------------------------------------------------------------
257
  -- -------------------------------------------------------------------------------------------
258
  ifetch_arbiter: process(rstn_i, clk_i)
259
  begin
260 2 zero_gravi
    if (rstn_i = '0') then
261 12 zero_gravi
      i_arbiter.rd_req    <= '0';
262
      i_arbiter.wr_req    <= '0';
263
      i_arbiter.err_align <= '0';
264
      i_arbiter.err_bus   <= '0';
265
      i_arbiter.timeout   <= (others => '0');
266 2 zero_gravi
    elsif rising_edge(clk_i) then
267 12 zero_gravi
      i_arbiter.wr_req <= '0'; -- instruction fetch is read-only
268
 
269
      -- instruction fetch request --
270
      if (i_arbiter.rd_req = '0') then -- idle
271
        i_arbiter.rd_req    <= ctrl_i(ctrl_bus_if_c);
272
        i_arbiter.err_align <= i_misaligned;
273
        i_arbiter.err_bus   <= '0';
274 14 zero_gravi
        i_arbiter.timeout   <= std_ulogic_vector(to_unsigned(BUS_TIMEOUT, index_size_f(BUS_TIMEOUT)));
275 12 zero_gravi
      else -- in progress
276
        i_arbiter.timeout   <= std_ulogic_vector(unsigned(i_arbiter.timeout) - 1);
277
        i_arbiter.err_align <= (i_arbiter.err_align or i_misaligned)                                     and (not ctrl_i(ctrl_bus_ierr_ack_c));
278
        i_arbiter.err_bus   <= (i_arbiter.err_bus   or (not or_all_f(i_arbiter.timeout)) or i_bus_err_i) and (not ctrl_i(ctrl_bus_ierr_ack_c));
279
        if (i_arbiter.err_align = '1') or (i_arbiter.err_bus = '1') then -- any error?
280
          if (ctrl_i(ctrl_bus_ierr_ack_c) = '1') then -- wait for controller to acknowledge error
281
            i_arbiter.rd_req <= '0';
282 2 zero_gravi
          end if;
283 12 zero_gravi
        elsif (i_bus_ack_i = '1') then -- wait for normal termination
284
         i_arbiter.rd_req <= '0';
285 2 zero_gravi
        end if;
286
      end if;
287 12 zero_gravi
 
288
      -- cancel bus access --
289
      i_bus_cancel_o <= i_arbiter.rd_req and ctrl_i(ctrl_bus_ierr_ack_c);
290 2 zero_gravi
    end if;
291 12 zero_gravi
  end process ifetch_arbiter;
292 2 zero_gravi
 
293
 
294 12 zero_gravi
  -- wait for bus transaction to finish --
295
  i_wait_o <= i_arbiter.rd_req and (not i_bus_ack_i);
296 2 zero_gravi
 
297 12 zero_gravi
  -- output instruction fetch error to controller --
298
  ma_instr_o <= i_arbiter.err_align;
299
  be_instr_o <= i_arbiter.err_bus;
300 11 zero_gravi
 
301 12 zero_gravi
  -- instruction bus (read-only) --
302
  i_bus_addr_o  <= fetch_pc_i;
303
  i_bus_wdata_o <= (others => '0');
304
  i_bus_ben_o   <= (others => '0');
305
  i_bus_we_o    <= '0';
306
  i_bus_re_o    <= ctrl_i(ctrl_bus_if_c) and (not i_misaligned); -- no actual read when misaligned
307
  i_bus_fence_o <= ctrl_i(ctrl_bus_fencei_c);
308
  instr_o       <= i_bus_rdata_i;
309 2 zero_gravi
 
310
 
311 12 zero_gravi
  -- Data Access Arbiter --------------------------------------------------------------------
312 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
313 12 zero_gravi
  data_access_arbiter: process(rstn_i, clk_i)
314 2 zero_gravi
  begin
315 12 zero_gravi
    if (rstn_i = '0') then
316
      d_arbiter.rd_req    <= '0';
317
      d_arbiter.wr_req    <= '0';
318
      d_arbiter.err_align <= '0';
319
      d_arbiter.err_bus   <= '0';
320
      d_arbiter.timeout   <= (others => '0');
321
    elsif rising_edge(clk_i) then
322
 
323
      -- data access request --
324
      if (d_arbiter.wr_req = '0') and (d_arbiter.rd_req = '0') then -- idle
325
        d_arbiter.wr_req    <= ctrl_i(ctrl_bus_wr_c);
326
        d_arbiter.rd_req    <= ctrl_i(ctrl_bus_rd_c);
327
        d_arbiter.err_align <= d_misaligned;
328
        d_arbiter.err_bus   <= '0';
329 14 zero_gravi
        d_arbiter.timeout   <= std_ulogic_vector(to_unsigned(BUS_TIMEOUT, index_size_f(BUS_TIMEOUT)));
330 12 zero_gravi
      else -- in progress
331
        d_arbiter.timeout   <= std_ulogic_vector(unsigned(d_arbiter.timeout) - 1);
332
        d_arbiter.err_align <= (d_arbiter.err_align or d_misaligned)                                     and (not ctrl_i(ctrl_bus_derr_ack_c));
333
        d_arbiter.err_bus   <= (d_arbiter.err_bus   or (not or_all_f(d_arbiter.timeout)) or d_bus_err_i) and (not ctrl_i(ctrl_bus_derr_ack_c));
334
        if (d_arbiter.err_align = '1') or (d_arbiter.err_bus = '1') then -- any error?
335
          if (ctrl_i(ctrl_bus_derr_ack_c) = '1') then -- wait for controller to acknowledge error
336
            d_arbiter.wr_req <= '0';
337
            d_arbiter.rd_req <= '0';
338
          end if;
339
        elsif (d_bus_ack_i = '1') then -- wait for normal termination
340
          d_arbiter.wr_req <= '0';
341
          d_arbiter.rd_req <= '0';
342 2 zero_gravi
        end if;
343 12 zero_gravi
      end if;
344 2 zero_gravi
 
345 12 zero_gravi
      -- cancel bus access --
346
      d_bus_cancel_o <= (d_arbiter.wr_req or d_arbiter.rd_req) and ctrl_i(ctrl_bus_derr_ack_c);
347 2 zero_gravi
    end if;
348 12 zero_gravi
  end process data_access_arbiter;
349 2 zero_gravi
 
350
 
351 12 zero_gravi
  -- wait for bus transaction to finish --
352
  d_wait_o <= (d_arbiter.wr_req or d_arbiter.rd_req) and (not d_bus_ack_i);
353
 
354
  -- output data access error to controller --
355
  ma_load_o  <= d_arbiter.rd_req and d_arbiter.err_align;
356
  be_load_o  <= d_arbiter.rd_req and d_arbiter.err_bus;
357
  ma_store_o <= d_arbiter.wr_req and d_arbiter.err_align;
358
  be_store_o <= d_arbiter.wr_req and d_arbiter.err_bus;
359
 
360
  -- data bus --
361
  d_bus_addr_o  <= mar;
362
  d_bus_wdata_o <= d_bus_wdata;
363
  d_bus_ben_o   <= d_bus_ben;
364
  d_bus_we_o    <= ctrl_i(ctrl_bus_wr_c) and (not d_misaligned); -- no actual write when misaligned
365
  d_bus_re_o    <= ctrl_i(ctrl_bus_rd_c) and (not d_misaligned); -- no actual read when misaligned
366
  d_bus_fence_o <= ctrl_i(ctrl_bus_fence_c);
367
  d_bus_rdata   <= d_bus_rdata_i;
368
 
369
 
370 2 zero_gravi
end neorv32_cpu_bus_rtl;

powered by: WebSVN 2.1.0

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