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

Subversion Repositories neorv32

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

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