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

Subversion Repositories neorv32

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

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