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

Subversion Repositories neorv32

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

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 18 zero_gravi
-- # Instruction and data bus interfaces and physical memory protection (PMP).                     #
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 15 zero_gravi
    -- Physical memory protection (PMP) --
48
    PMP_USE               : boolean := false; -- implement physical memory protection?
49
    PMP_NUM_REGIONS       : natural := 4; -- number of regions (1..4)
50 18 zero_gravi
    PMP_GRANULARITY       : natural := 16 -- granularity (1=8B, 2=16B, 3=32B, ...)
51 2 zero_gravi
  );
52
  port (
53
    -- global control --
54 12 zero_gravi
    clk_i          : in  std_ulogic; -- global clock, rising edge
55
    rstn_i         : in  std_ulogic; -- global reset, low-active, async
56
    ctrl_i         : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
57
    -- cpu instruction fetch interface --
58
    fetch_pc_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
59
    instr_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
60
    i_wait_o       : out std_ulogic; -- wait for fetch to complete
61
    --
62
    ma_instr_o     : out std_ulogic; -- misaligned instruction address
63
    be_instr_o     : out std_ulogic; -- bus error on instruction access
64
    -- cpu data access interface --
65
    addr_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result -> access address
66
    wdata_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- write data
67
    rdata_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
68
    mar_o          : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
69
    d_wait_o       : out std_ulogic; -- wait for access to complete
70
    --
71
    ma_load_o      : out std_ulogic; -- misaligned load data address
72
    ma_store_o     : out std_ulogic; -- misaligned store data address
73
    be_load_o      : out std_ulogic; -- bus error on load data access
74
    be_store_o     : out std_ulogic; -- bus error on store data access
75 15 zero_gravi
    -- physical memory protection --
76
    pmp_addr_i     : in  pmp_addr_if_t; -- addresses
77
    pmp_ctrl_i     : in  pmp_ctrl_if_t; -- configs
78
    priv_mode_i    : in  std_ulogic_vector(1 downto 0); -- current CPU privilege level
79 12 zero_gravi
    -- instruction bus --
80
    i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
81
    i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
82
    i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
83
    i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
84
    i_bus_we_o     : out std_ulogic; -- write enable
85
    i_bus_re_o     : out std_ulogic; -- read enable
86
    i_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
87
    i_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
88
    i_bus_err_i    : in  std_ulogic; -- bus transfer error
89
    i_bus_fence_o  : out std_ulogic; -- fence operation
90
    -- data bus --
91
    d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
92
    d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
93
    d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
94
    d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
95
    d_bus_we_o     : out std_ulogic; -- write enable
96
    d_bus_re_o     : out std_ulogic; -- read enable
97
    d_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
98
    d_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
99
    d_bus_err_i    : in  std_ulogic; -- bus transfer error
100
    d_bus_fence_o  : out std_ulogic  -- fence operation
101 2 zero_gravi
  );
102
end neorv32_cpu_bus;
103
 
104
architecture neorv32_cpu_bus_rtl of neorv32_cpu_bus is
105
 
106 15 zero_gravi
  -- PMP modes --
107
  constant pmp_off_mode_c   : std_ulogic_vector(1 downto 0) := "00"; -- null region (disabled)
108
  constant pmp_tor_mode_c   : std_ulogic_vector(1 downto 0) := "01"; -- top of range
109
  constant pmp_na4_mode_c   : std_ulogic_vector(1 downto 0) := "10"; -- naturally aligned four-byte region
110
  constant pmp_napot_mode_c : std_ulogic_vector(1 downto 0) := "11"; -- naturally aligned power-of-two region (>= 8 bytes)
111
 
112
  -- PMP configuration register bits --
113
  constant pmp_cfg_r_c  : natural := 0; -- read permit
114
  constant pmp_cfg_w_c  : natural := 1; -- write permit
115
  constant pmp_cfg_x_c  : natural := 2; -- execute permit
116
  constant pmp_cfg_al_c : natural := 3; -- mode bit low
117
  constant pmp_cfg_ah_c : natural := 4; -- mode bit high
118
  constant pmp_cfg_l_c  : natural := 7; -- locked entry
119
 
120 12 zero_gravi
  -- data interface registers --
121 2 zero_gravi
  signal mar, mdo, mdi : std_ulogic_vector(data_width_c-1 downto 0);
122
 
123 12 zero_gravi
  -- data access --
124
  signal d_bus_wdata : std_ulogic_vector(data_width_c-1 downto 0); -- write data
125
  signal d_bus_rdata : std_ulogic_vector(data_width_c-1 downto 0); -- read data
126
  signal d_bus_ben   : std_ulogic_vector(3 downto 0); -- write data byte enable
127 2 zero_gravi
 
128
  -- misaligned access? --
129 12 zero_gravi
  signal d_misaligned, i_misaligned : std_ulogic;
130 2 zero_gravi
 
131 12 zero_gravi
  -- bus arbiter --
132
  type bus_arbiter_t is record
133
    rd_req    : std_ulogic; -- read access in progress
134
    wr_req    : std_ulogic; -- write access in progress
135
    err_align : std_ulogic; -- alignment error
136
    err_bus   : std_ulogic; -- bus access error
137 30 zero_gravi
    timeout   : std_ulogic_vector(index_size_f(bus_timeout_c)-1 downto 0);
138 12 zero_gravi
  end record;
139
  signal i_arbiter, d_arbiter : bus_arbiter_t;
140
 
141 15 zero_gravi
  -- physical memory protection --
142
  type pmp_addr34_t is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(data_width_c+1 downto 0);
143 16 zero_gravi
  type pmp_addr_t   is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(data_width_c-1 downto 0);
144 15 zero_gravi
  type pmp_t is record
145 16 zero_gravi
    addr_mask     : pmp_addr34_t; -- 34-bit physical address
146
    region_base   : pmp_addr_t; -- masked region base address for comparator
147
    region_i_addr : pmp_addr_t; -- masked instruction access base address for comparator
148
    region_d_addr : pmp_addr_t; -- masked data access base address for comparator
149
    i_match       : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for instruction interface
150
    d_match       : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for data interface
151
    if_fault      : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for fetch operation
152
    ld_fault      : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for load operation
153
    st_fault      : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for store operation
154 15 zero_gravi
  end record;
155
  signal pmp : pmp_t;
156
 
157
  -- pmp faults anybody? --
158
  signal if_pmp_fault : std_ulogic; -- pmp instruction access fault
159
  signal ld_pmp_fault : std_ulogic; -- pmp load access fault
160
  signal st_pmp_fault : std_ulogic; -- pmp store access fault
161
 
162 2 zero_gravi
begin
163
 
164 12 zero_gravi
  -- Data Interface: Access Address ---------------------------------------------------------
165 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
166
  mem_adr_reg: process(rstn_i, clk_i)
167
  begin
168 11 zero_gravi
    if rising_edge(clk_i) then
169 2 zero_gravi
      if (ctrl_i(ctrl_bus_mar_we_c) = '1') then
170 12 zero_gravi
        mar <= addr_i;
171 2 zero_gravi
      end if;
172
    end if;
173
  end process mem_adr_reg;
174
 
175 12 zero_gravi
  -- read-back for exception controller --
176
  mar_o <= mar;
177 2 zero_gravi
 
178 12 zero_gravi
  -- alignment check --
179
  misaligned_d_check: process(mar, ctrl_i)
180
  begin
181
    -- check data access --
182
    d_misaligned <= '0'; -- default
183
    case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
184
      when "00" => -- byte
185
        d_misaligned <= '0';
186
      when "01" => -- half-word
187
        if (mar(0) /= '0') then
188
          d_misaligned <= '1';
189
        end if;
190
      when others => -- word
191
        if (mar(1 downto 0) /= "00") then
192
          d_misaligned <= '1';
193
        end if;
194
    end case;
195
  end process misaligned_d_check;
196 2 zero_gravi
 
197
 
198 12 zero_gravi
  -- Data Interface: Write Data -------------------------------------------------------------
199 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
200
  mem_do_reg: process(clk_i)
201
  begin
202
    if rising_edge(clk_i) then
203
      if (ctrl_i(ctrl_bus_mdo_we_c) = '1') then
204
        mdo <= wdata_i;
205
      end if;
206
    end if;
207
  end process mem_do_reg;
208
 
209
  -- byte enable and output data alignment --
210
  byte_enable: process(mar, mdo, ctrl_i)
211
  begin
212
    case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
213
      when "00" => -- byte
214 12 zero_gravi
        d_bus_wdata(07 downto 00) <= mdo(07 downto 00);
215
        d_bus_wdata(15 downto 08) <= mdo(07 downto 00);
216
        d_bus_wdata(23 downto 16) <= mdo(07 downto 00);
217
        d_bus_wdata(31 downto 24) <= mdo(07 downto 00);
218
        d_bus_ben <= (others => '0');
219
        d_bus_ben(to_integer(unsigned(mar(1 downto 0)))) <= '1';
220 2 zero_gravi
      when "01" => -- half-word
221 12 zero_gravi
        d_bus_wdata(31 downto 16) <= mdo(15 downto 00);
222
        d_bus_wdata(15 downto 00) <= mdo(15 downto 00);
223 2 zero_gravi
        if (mar(1) = '0') then
224 12 zero_gravi
          d_bus_ben <= "0011"; -- low half-word
225 2 zero_gravi
        else
226 12 zero_gravi
          d_bus_ben <= "1100"; -- high half-word
227 2 zero_gravi
        end if;
228
      when others => -- word
229 12 zero_gravi
        d_bus_wdata <= mdo;
230
        d_bus_ben   <= "1111"; -- full word
231 2 zero_gravi
    end case;
232
  end process byte_enable;
233
 
234
 
235 12 zero_gravi
  -- Data Interface: Read Data --------------------------------------------------------------
236 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
237
  mem_out_buf: process(clk_i)
238
  begin
239
    if rising_edge(clk_i) then
240
      -- memory data in register (MDI) --
241
      if (ctrl_i(ctrl_bus_mdi_we_c) = '1') then
242 12 zero_gravi
        mdi <= d_bus_rdata;
243 2 zero_gravi
      end if;
244
    end if;
245
  end process mem_out_buf;
246
 
247 12 zero_gravi
  -- input data alignment and sign extension --
248 2 zero_gravi
  read_align: process(mdi, mar, ctrl_i)
249
    variable signed_v : std_ulogic;
250
  begin
251
    signed_v := not ctrl_i(ctrl_bus_unsigned_c);
252
    case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
253
      when "00" => -- byte
254
        case mar(1 downto 0) is
255
          when "00" =>
256
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(07)));
257
            rdata_o(07 downto 00) <= mdi(07 downto 00); -- byte 0
258
          when "01" =>
259
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(15)));
260
            rdata_o(07 downto 00) <= mdi(15 downto 08); -- byte 1
261
          when "10" =>
262
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(23)));
263
            rdata_o(07 downto 00) <= mdi(23 downto 16); -- byte 2
264
          when others =>
265
            rdata_o(31 downto 08) <= (others => (signed_v and mdi(31)));
266
            rdata_o(07 downto 00) <= mdi(31 downto 24); -- byte 3
267
        end case;
268
      when "01" => -- half-word
269
        if (mar(1) = '0') then
270
          rdata_o(31 downto 16) <= (others => (signed_v and mdi(15)));
271
          rdata_o(15 downto 00) <= mdi(15 downto 00); -- low half-word
272
        else
273
          rdata_o(31 downto 16) <= (others => (signed_v and mdi(31)));
274
          rdata_o(15 downto 00) <= mdi(31 downto 16); -- high half-word
275
        end if;
276
      when others => -- word
277
        rdata_o <= mdi; -- full word
278
    end case;
279
  end process read_align;
280
 
281
 
282 12 zero_gravi
  -- Instruction Interface: Check for Misaligned Access -------------------------------------
283 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
284 12 zero_gravi
  misaligned_i_check: process(ctrl_i, fetch_pc_i)
285 2 zero_gravi
  begin
286 12 zero_gravi
    -- check instruction access --
287
    i_misaligned <= '0'; -- default
288
    if (CPU_EXTENSION_RISCV_C = true) then -- 16-bit and 32-bit instruction accesses
289
      i_misaligned <= '0'; -- no alignment exceptions possible
290
    else -- 32-bit instruction accesses only
291
      if (fetch_pc_i(1) = '1') then -- PC(0) is always zero
292
        i_misaligned <= '1';
293
      end if;
294
    end if;
295
  end process misaligned_i_check;
296
 
297
 
298
  -- Instruction Fetch Arbiter --------------------------------------------------------------
299
  -- -------------------------------------------------------------------------------------------
300
  ifetch_arbiter: process(rstn_i, clk_i)
301
  begin
302 2 zero_gravi
    if (rstn_i = '0') then
303 12 zero_gravi
      i_arbiter.rd_req    <= '0';
304
      i_arbiter.wr_req    <= '0';
305
      i_arbiter.err_align <= '0';
306
      i_arbiter.err_bus   <= '0';
307
      i_arbiter.timeout   <= (others => '0');
308 2 zero_gravi
    elsif rising_edge(clk_i) then
309 12 zero_gravi
      i_arbiter.wr_req <= '0'; -- instruction fetch is read-only
310
 
311
      -- instruction fetch request --
312
      if (i_arbiter.rd_req = '0') then -- idle
313
        i_arbiter.rd_req    <= ctrl_i(ctrl_bus_if_c);
314
        i_arbiter.err_align <= i_misaligned;
315
        i_arbiter.err_bus   <= '0';
316 30 zero_gravi
        i_arbiter.timeout   <= std_ulogic_vector(to_unsigned(bus_timeout_c, index_size_f(bus_timeout_c)));
317 12 zero_gravi
      else -- in progress
318
        i_arbiter.timeout   <= std_ulogic_vector(unsigned(i_arbiter.timeout) - 1);
319
        i_arbiter.err_align <= (i_arbiter.err_align or i_misaligned)                                     and (not ctrl_i(ctrl_bus_ierr_ack_c));
320
        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));
321 28 zero_gravi
        --if (i_arbiter.err_align = '1') or (i_arbiter.err_bus = '1') then -- any error?
322
        --  if (ctrl_i(ctrl_bus_ierr_ack_c) = '1') then -- wait for controller to acknowledge error
323
        --    i_arbiter.rd_req <= '0';
324
        --  end if;
325
        if (i_bus_ack_i = '1') or (ctrl_i(ctrl_bus_ierr_ack_c) = '1') then -- wait for normal termination / CPU abort
326 23 zero_gravi
          i_arbiter.rd_req <= '0';
327 2 zero_gravi
        end if;
328
      end if;
329
    end if;
330 12 zero_gravi
  end process ifetch_arbiter;
331 2 zero_gravi
 
332 28 zero_gravi
  -- cancel bus access --
333
  i_bus_cancel_o <= i_arbiter.rd_req and ctrl_i(ctrl_bus_ierr_ack_c);
334 2 zero_gravi
 
335 12 zero_gravi
  -- wait for bus transaction to finish --
336
  i_wait_o <= i_arbiter.rd_req and (not i_bus_ack_i);
337 2 zero_gravi
 
338 12 zero_gravi
  -- output instruction fetch error to controller --
339
  ma_instr_o <= i_arbiter.err_align;
340
  be_instr_o <= i_arbiter.err_bus;
341 11 zero_gravi
 
342 12 zero_gravi
  -- instruction bus (read-only) --
343 31 zero_gravi
  i_bus_addr_o  <= fetch_pc_i(data_width_c-1 downto 2) & "00"; -- instruction access is always 4-byte aligned (even for compressed instructions)
344 12 zero_gravi
  i_bus_wdata_o <= (others => '0');
345
  i_bus_ben_o   <= (others => '0');
346
  i_bus_we_o    <= '0';
347 15 zero_gravi
  i_bus_re_o    <= ctrl_i(ctrl_bus_if_c) and (not i_misaligned) and (not if_pmp_fault); -- no actual read when misaligned or PMP fault
348 12 zero_gravi
  i_bus_fence_o <= ctrl_i(ctrl_bus_fencei_c);
349
  instr_o       <= i_bus_rdata_i;
350 2 zero_gravi
 
351
 
352 12 zero_gravi
  -- Data Access Arbiter --------------------------------------------------------------------
353 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
354 12 zero_gravi
  data_access_arbiter: process(rstn_i, clk_i)
355 2 zero_gravi
  begin
356 12 zero_gravi
    if (rstn_i = '0') then
357
      d_arbiter.rd_req    <= '0';
358
      d_arbiter.wr_req    <= '0';
359
      d_arbiter.err_align <= '0';
360
      d_arbiter.err_bus   <= '0';
361
      d_arbiter.timeout   <= (others => '0');
362
    elsif rising_edge(clk_i) then
363
 
364
      -- data access request --
365
      if (d_arbiter.wr_req = '0') and (d_arbiter.rd_req = '0') then -- idle
366
        d_arbiter.wr_req    <= ctrl_i(ctrl_bus_wr_c);
367
        d_arbiter.rd_req    <= ctrl_i(ctrl_bus_rd_c);
368
        d_arbiter.err_align <= d_misaligned;
369
        d_arbiter.err_bus   <= '0';
370 30 zero_gravi
        d_arbiter.timeout   <= std_ulogic_vector(to_unsigned(bus_timeout_c, index_size_f(bus_timeout_c)));
371 12 zero_gravi
      else -- in progress
372
        d_arbiter.timeout   <= std_ulogic_vector(unsigned(d_arbiter.timeout) - 1);
373
        d_arbiter.err_align <= (d_arbiter.err_align or d_misaligned)                                     and (not ctrl_i(ctrl_bus_derr_ack_c));
374
        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));
375 28 zero_gravi
        --if (d_arbiter.err_align = '1') or (d_arbiter.err_bus = '1') then -- any error?
376
        --  if (ctrl_i(ctrl_bus_derr_ack_c) = '1') then -- wait for controller to acknowledge error
377
        --    d_arbiter.wr_req <= '0';
378
        --    d_arbiter.rd_req <= '0';
379
        --  end if;
380
        if (d_bus_ack_i = '1') or (ctrl_i(ctrl_bus_derr_ack_c) = '1') then -- wait for normal termination / CPU abort
381 12 zero_gravi
          d_arbiter.wr_req <= '0';
382
          d_arbiter.rd_req <= '0';
383 2 zero_gravi
        end if;
384 12 zero_gravi
      end if;
385 2 zero_gravi
    end if;
386 12 zero_gravi
  end process data_access_arbiter;
387 2 zero_gravi
 
388 28 zero_gravi
  -- cancel bus access --
389
  d_bus_cancel_o <= (d_arbiter.wr_req or d_arbiter.rd_req) and ctrl_i(ctrl_bus_derr_ack_c);
390 2 zero_gravi
 
391 12 zero_gravi
  -- wait for bus transaction to finish --
392
  d_wait_o <= (d_arbiter.wr_req or d_arbiter.rd_req) and (not d_bus_ack_i);
393
 
394
  -- output data access error to controller --
395
  ma_load_o  <= d_arbiter.rd_req and d_arbiter.err_align;
396
  be_load_o  <= d_arbiter.rd_req and d_arbiter.err_bus;
397
  ma_store_o <= d_arbiter.wr_req and d_arbiter.err_align;
398
  be_store_o <= d_arbiter.wr_req and d_arbiter.err_bus;
399
 
400 15 zero_gravi
  -- data bus (read/write)--
401 12 zero_gravi
  d_bus_addr_o  <= mar;
402
  d_bus_wdata_o <= d_bus_wdata;
403
  d_bus_ben_o   <= d_bus_ben;
404 15 zero_gravi
  d_bus_we_o    <= ctrl_i(ctrl_bus_wr_c) and (not d_misaligned) and (not st_pmp_fault); -- no actual write when misaligned or PMP fault
405
  d_bus_re_o    <= ctrl_i(ctrl_bus_rd_c) and (not d_misaligned) and (not ld_pmp_fault); -- no actual read when misaligned or PMP fault
406 12 zero_gravi
  d_bus_fence_o <= ctrl_i(ctrl_bus_fence_c);
407
  d_bus_rdata   <= d_bus_rdata_i;
408
 
409
 
410 15 zero_gravi
  -- Physical Memory Protection (PMP) -------------------------------------------------------
411
  -- -------------------------------------------------------------------------------------------
412
  -- compute address masks --
413 17 zero_gravi
  pmp_masks: process(clk_i)
414 15 zero_gravi
  begin
415 17 zero_gravi
    if rising_edge(clk_i) then -- address configuration (not the actual address check!) has a latency of +1 cycles
416
      for r in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
417
        pmp.addr_mask(r) <= (others => '0'); -- default
418
        for i in PMP_GRANULARITY+1 to 33 loop
419
          if (i = PMP_GRANULARITY+1) then
420
            pmp.addr_mask(r)(i) <= '0';
421
          else -- current bit = not AND(all previous bits)
422
            pmp.addr_mask(r)(i) <= not (and_all_f(pmp_addr_i(r)(i-1 downto PMP_GRANULARITY)));
423
          end if;
424
        end loop; -- i
425
      end loop; -- r
426
    end if;
427 15 zero_gravi
  end process pmp_masks;
428
 
429
 
430 16 zero_gravi
  -- compute operands for comparator --
431
  pmp_prepare_check:
432
  for r in 0 to PMP_NUM_REGIONS-1 generate -- iterate over all regions
433
    -- ignore lowest 3 bits of access addresses -> minimal region size = 8 bytes
434
    pmp.region_i_addr(r) <= (fetch_pc_i(31 downto 3) & "000") and pmp.addr_mask(r)(33 downto 2);
435
    pmp.region_d_addr(r) <= (mar(31 downto 3) & "000")        and pmp.addr_mask(r)(33 downto 2);
436
    pmp.region_base(r)   <= pmp_addr_i(r)(33 downto 2)        and pmp.addr_mask(r)(33 downto 2);
437
  end generate; -- r
438 15 zero_gravi
 
439
 
440
  -- check for access address match --
441 16 zero_gravi
  pmp_addr_check: process (pmp)
442 15 zero_gravi
  begin
443
    for r in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
444
      -- instruction interface --
445 16 zero_gravi
      pmp.i_match(r) <= '0';
446
      if (pmp.region_i_addr(r)(31 downto PMP_GRANULARITY+2) = pmp.region_base(r)(31 downto PMP_GRANULARITY+2)) then
447 15 zero_gravi
        pmp.i_match(r) <= '1';
448
      end if;
449
      -- data interface --
450 16 zero_gravi
      pmp.d_match(r) <= '0';
451
      if (pmp.region_d_addr(r)(31 downto PMP_GRANULARITY+2) = pmp.region_base(r)(31 downto PMP_GRANULARITY+2)) then
452 15 zero_gravi
        pmp.d_match(r) <= '1';
453
      end if;
454
    end loop; -- r
455
  end process pmp_addr_check;
456
 
457
 
458
  -- check access type and regions's permissions --
459
  pmp_check_permission: process(pmp, pmp_ctrl_i, priv_mode_i)
460
  begin
461
    for r in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
462 29 zero_gravi
      if ((priv_mode_i = priv_mode_u_c) or (pmp_ctrl_i(r)(pmp_cfg_l_c) = '1')) and -- user privilege level or locked pmp entry -> enforce permissions also for machine mode
463 15 zero_gravi
          (pmp_ctrl_i(r)(pmp_cfg_ah_c downto pmp_cfg_al_c) /= pmp_off_mode_c) then -- active entry
464
        pmp.if_fault(r) <= pmp.i_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_x_c)); -- fetch access match no execute permission
465
        pmp.ld_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_r_c)); -- load access match no read permission
466
        pmp.st_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_w_c)); -- store access match no write permission
467
      else
468
        pmp.if_fault(r) <= '0';
469
        pmp.ld_fault(r) <= '0';
470
        pmp.st_fault(r) <= '0';
471
      end if;
472
    end loop; -- r
473
  end process pmp_check_permission;
474
 
475
 
476
  -- final PMP access fault signals --
477
  if_pmp_fault <= or_all_f(pmp.if_fault) when (PMP_USE = true) else '0';
478
  ld_pmp_fault <= or_all_f(pmp.ld_fault) when (PMP_USE = true) else '0';
479
  st_pmp_fault <= or_all_f(pmp.st_fault) when (PMP_USE = true) else '0';
480
 
481
 
482 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.