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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_top.vhd] - Blame information for rev 3

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Processor Top Entity >>                                                          #
3
-- # ********************************************************************************************* #
4
-- # This is the top entity of the NEORV32 Processor. Instantiate this unit in your own project    #
5
-- # and define all the configuration generics according to your needs. Alternatively, you can use #
6
-- # one of the alternative top entities provided in the "rtl\top_templates" folder.               #
7
-- # Check the processor's documentary for more information: doc\NEORV32.pdf                       #
8
-- # ********************************************************************************************* #
9
-- # BSD 3-Clause License                                                                          #
10
-- #                                                                                               #
11
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
12
-- #                                                                                               #
13
-- # Redistribution and use in source and binary forms, with or without modification, are          #
14
-- # permitted provided that the following conditions are met:                                     #
15
-- #                                                                                               #
16
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
17
-- #    conditions and the following disclaimer.                                                   #
18
-- #                                                                                               #
19
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
20
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
21
-- #    provided with the distribution.                                                            #
22
-- #                                                                                               #
23
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
24
-- #    endorse or promote products derived from this software without specific prior written      #
25
-- #    permission.                                                                                #
26
-- #                                                                                               #
27
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
28
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
29
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
30
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
31
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
32
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
33
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
34
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
35
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
36
-- # ********************************************************************************************* #
37
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
38
-- #################################################################################################
39
 
40
library ieee;
41
use ieee.std_logic_1164.all;
42
use ieee.numeric_std.all;
43
 
44
library neorv32;
45
use neorv32.neorv32_package.all;
46
 
47
entity neorv32_top is
48
  generic (
49
    -- General --
50
    CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
51
    HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
52
    BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
53
    -- RISC-V CPU Extensions --
54
    CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
55
    CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
56
    CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
57
    CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
58
    -- Memory configuration: Instruction memory --
59
    MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
60
    MEM_ISPACE_SIZE           : natural := 16*1024; -- total size of instruction memory space in byte
61
    MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
62
    MEM_INT_IMEM_SIZE         : natural := 16*1024; -- size of processor-internal instruction memory in bytes
63
    MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
64
    -- Memory configuration: Data memory --
65
    MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
66
    MEM_DSPACE_SIZE           : natural := 8*1024; -- total size of data memory space in byte
67
    MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
68
    MEM_INT_DMEM_SIZE         : natural := 8*1024; -- size of processor-internal data memory in bytes
69
    -- Memory configuration: External memory interface --
70
    MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
71
    MEM_EXT_REG_STAGES        : natural := 2;      -- number of interface register stages (0,1,2)
72
    MEM_EXT_TIMEOUT           : natural := 15;     -- cycles after which a valid bus access will timeout
73
    -- Processor peripherals --
74
    IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
75
    IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
76
    IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
77
    IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
78
    IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
79
    IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
80
    IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
81
    IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
82 3 zero_gravi
    IO_TRNG_USE               : boolean := false;  -- implement true random number generator (TRNG)?
83
    IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
84 2 zero_gravi
  );
85
  port (
86
    -- Global control --
87
    clk_i      : in  std_ulogic := '0'; -- global clock, rising edge
88
    rstn_i     : in  std_ulogic := '0'; -- global reset, low-active, async
89
    -- Wishbone bus interface (available if MEM_EXT_USE = true) --
90
    wb_adr_o   : out std_ulogic_vector(31 downto 0); -- address
91
    wb_dat_i   : in  std_ulogic_vector(31 downto 0) := (others => '0'); -- read data
92
    wb_dat_o   : out std_ulogic_vector(31 downto 0); -- write data
93
    wb_we_o    : out std_ulogic; -- read/write
94
    wb_sel_o   : out std_ulogic_vector(03 downto 0); -- byte enable
95
    wb_stb_o   : out std_ulogic; -- strobe
96
    wb_cyc_o   : out std_ulogic; -- valid cycle
97
    wb_ack_i   : in  std_ulogic := '0'; -- transfer acknowledge
98
    wb_err_i   : in  std_ulogic := '0'; -- transfer error
99
    -- GPIO (available if IO_GPIO_USE = true) --
100
    gpio_o     : out std_ulogic_vector(15 downto 0); -- parallel output
101
    gpio_i     : in  std_ulogic_vector(15 downto 0) := (others => '0'); -- parallel input
102
    -- UART (available if IO_UART_USE = true) --
103
    uart_txd_o : out std_ulogic; -- UART send data
104
    uart_rxd_i : in  std_ulogic := '0'; -- UART receive data
105
    -- SPI (available if IO_SPI_USE = true) --
106
    spi_sclk_o : out std_ulogic; -- serial clock line
107
    spi_mosi_o : out std_ulogic; -- serial data line out
108
    spi_miso_i : in  std_ulogic := '0'; -- serial data line in
109
    spi_csn_o  : out std_ulogic_vector(07 downto 0); -- SPI CS
110
    -- TWI (available if IO_TWI_USE = true) --
111
    twi_sda_io : inout std_logic := 'H'; -- twi serial data line
112
    twi_scl_io : inout std_logic := 'H'; -- twi serial clock line
113
    -- PWM (available if IO_PWM_USE = true) --
114
    pwm_o      : out std_ulogic_vector(03 downto 0);  -- pwm channels
115
    -- Interrupts (available if IO_CLIC_USE = true) --
116
    ext_irq_i  : in  std_ulogic_vector(01 downto 0) := (others => '0'); -- external interrupt request
117
    ext_ack_o  : out std_ulogic_vector(01 downto 0)  -- external interrupt request acknowledge
118
  );
119
end neorv32_top;
120
 
121
architecture neorv32_top_rtl of neorv32_top is
122
 
123
  -- reset generator --
124
  signal rstn_i_sync0 : std_ulogic;
125
  signal rstn_i_sync1 : std_ulogic;
126
  signal rstn_i_sync2 : std_ulogic;
127
  signal rstn_gen     : std_ulogic_vector(3 downto 0);
128
  signal ext_rstn     : std_ulogic;
129
  signal sys_rstn     : std_ulogic;
130
  signal wdt_rstn     : std_ulogic;
131
 
132
  -- clock generator --
133
  signal clk_div    : std_ulogic_vector(11 downto 0);
134
  signal clk_div_ff : std_ulogic_vector(11 downto 0);
135
  signal clk_gen    : std_ulogic_vector(07 downto 0);
136
  signal wdt_cg_en  : std_ulogic;
137
  signal uart_cg_en : std_ulogic;
138
  signal spi_cg_en  : std_ulogic;
139
  signal twi_cg_en  : std_ulogic;
140
  signal pwm_cg_en  : std_ulogic;
141
 
142
  -- cpu bus --
143
  signal cpu_addr  : std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
144
  signal cpu_rdata : std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
145
  signal cpu_wdata : std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
146
  signal cpu_ben   : std_ulogic_vector(03 downto 0); -- byte enable
147
  signal cpu_we    : std_ulogic; -- write enable
148
  signal cpu_re    : std_ulogic; -- read enable
149
  signal cpu_ack   : std_ulogic; -- bus transfer acknowledge
150
  signal cpu_err   : std_ulogic; -- bus transfer error
151
 
152
  -- io space access --
153
  signal io_acc  : std_ulogic;
154
  signal io_rden : std_ulogic;
155
  signal io_wren : std_ulogic;
156
 
157
  -- read-back busses -
158
  signal imem_rdata     : std_ulogic_vector(data_width_c-1 downto 0);
159
  signal imem_ack       : std_ulogic;
160
  signal dmem_rdata     : std_ulogic_vector(data_width_c-1 downto 0);
161
  signal dmem_ack       : std_ulogic;
162
  signal bootrom_rdata  : std_ulogic_vector(data_width_c-1 downto 0);
163
  signal bootrom_ack    : std_ulogic;
164
  signal wishbone_rdata : std_ulogic_vector(data_width_c-1 downto 0);
165
  signal wishbone_ack   : std_ulogic;
166
  signal wishbone_err   : std_ulogic;
167
  signal gpio_rdata     : std_ulogic_vector(data_width_c-1 downto 0);
168
  signal gpio_ack       : std_ulogic;
169
  signal mtime_rdata    : std_ulogic_vector(data_width_c-1 downto 0);
170
  signal mtime_ack      : std_ulogic;
171
  signal uart_rdata     : std_ulogic_vector(data_width_c-1 downto 0);
172
  signal uart_ack       : std_ulogic;
173
  signal spi_rdata      : std_ulogic_vector(data_width_c-1 downto 0);
174
  signal spi_ack        : std_ulogic;
175
  signal twi_rdata      : std_ulogic_vector(data_width_c-1 downto 0);
176
  signal twi_ack        : std_ulogic;
177
  signal pwm_rdata      : std_ulogic_vector(data_width_c-1 downto 0);
178
  signal pwm_ack        : std_ulogic;
179
  signal wdt_rdata      : std_ulogic_vector(data_width_c-1 downto 0);
180
  signal wdt_ack        : std_ulogic;
181
  signal clic_rdata     : std_ulogic_vector(data_width_c-1 downto 0);
182
  signal clic_ack       : std_ulogic;
183
  signal trng_rdata     : std_ulogic_vector(data_width_c-1 downto 0);
184
  signal trng_ack       : std_ulogic;
185 3 zero_gravi
  signal devnull_rdata  : std_ulogic_vector(data_width_c-1 downto 0);
186
  signal devnull_ack    : std_ulogic;
187 2 zero_gravi
 
188
  -- IRQs --
189
  signal mtime_irq : std_ulogic;
190
  signal clic_irq  : std_ulogic;
191
  signal clic_xirq : std_ulogic_vector(7 downto 0);
192
  signal clic_xack : std_ulogic_vector(7 downto 0);
193
  signal gpio_irq  : std_ulogic;
194
  signal wdt_irq   : std_ulogic;
195
  signal uart_irq  : std_ulogic;
196
  signal spi_irq   : std_ulogic;
197
  signal twi_irq   : std_ulogic;
198
 
199
begin
200
 
201
  -- Sanity Checks --------------------------------------------------------------------------
202
  -- -------------------------------------------------------------------------------------------
203
  sanity_check: process(clk_i)
204
  begin
205
    if rising_edge(clk_i) then
206
      -- internal bootloader memory --
207
      if (BOOTLOADER_USE = true) and (boot_size_c > boot_max_size_c) then
208
        assert false report "NEORV32 CONFIG ERROR! Boot ROM size out of range." severity error;
209
      end if;
210
 
211
      -- memory system - data/instruction fetch --
212
      if (MEM_EXT_USE = false) then
213
        if (MEM_INT_DMEM_USE = false) then
214
          assert false report "NEORV32 CONFIG ERROR! Core cannot fetch data without external memory interface and internal data memory." severity error;
215
        end if;
216
        if (MEM_INT_IMEM_USE = false) and (BOOTLOADER_USE = false) then
217
          assert false report "NEORV32 CONFIG ERROR! Core cannot fetch instructions without external memory interface, internal data memory and bootloader." severity error;
218
        end if;
219
      end if;
220
 
221
      -- memory system - address space --
222
      if (MEM_INT_IMEM_USE = true) and (MEM_INT_IMEM_SIZE > MEM_ISPACE_SIZE) then
223
        assert false report "NEORV32 CONFIG ERROR! Internal instruction memory (IMEM) cannot be greater than total instruction address space." severity error;
224
      end if;
225
      if (MEM_INT_DMEM_USE = true) and (MEM_INT_DMEM_SIZE > MEM_DSPACE_SIZE) then
226
        assert false report "NEORV32 CONFIG ERROR! Internal data memory (DMEM) cannot be greater than total data address space." severity error;
227
      end if;
228
      if (MEM_EXT_TIMEOUT <= 1) then
229
        assert false report "NEORV32 CONFIG ERROR! Invalid bus timeout. Internal components require 1 cycle delay." severity error;
230
      end if;
231
 
232
      -- clock --
233
      if (CLOCK_FREQUENCY = 0) then
234
        assert false report "NEORV32 CONFIG ERROR! Core clock frequency (CLOCK_FREQUENCY) not specified." severity error;
235
      end if;
236
 
237
      -- CSR system not implemented --
238
      if (CPU_EXTENSION_RISCV_Zicsr = false) then
239
        assert false report "NEORV32 CONFIG WARNING! No exception/interrupt/machine status features available when CPU_EXTENSION_RISCV_Zicsr = false." severity warning;
240
      end if;
241
      -- core local interrupt controller --
242
      if (CPU_EXTENSION_RISCV_Zicsr = false) and (IO_CLIC_USE = true) then
243
        assert false report "NEORV32 CONFIG ERROR! Core local interrupt controller (CLIC) cannot be used without >Zicsr< CPU extension." severity error;
244
      end if;
245
 
246
      -- memory layout notifier --
247
      if (MEM_ISPACE_BASE /= x"00000000") then
248
        assert false report "NEORV32 CONFIG WARNING! Non-default base address for instruction address space. Make sure this is sync with the linker script." severity warning;
249
      end if;
250
      if (MEM_DSPACE_BASE /= x"80000000") then
251
        assert false report "NEORV32 CONFIG WARNING! Non-default base address for data address space. Make sure this is sync with the linker script." severity warning;
252
      end if;
253
    end if;
254
  end process sanity_check;
255
 
256
 
257
  -- Reset Generator ------------------------------------------------------------------------
258
  -- -------------------------------------------------------------------------------------------
259
  reset_generator_sync: process(clk_i)
260
  begin
261
    -- make sure the external reset is free of metastability and has a minimal duration of 1 clock cycle
262
    if rising_edge(clk_i) then
263
      rstn_i_sync0 <= rstn_i;
264
      rstn_i_sync1 <= rstn_i_sync0;
265
      rstn_i_sync2 <= rstn_i_sync1;
266
    end if;
267
  end process reset_generator_sync;
268
 
269
  -- keep internal reset active for at least 4 clock cycles
270
  reset_generator: process(rstn_i_sync1, rstn_i_sync2, clk_i)
271
  begin
272
    if ((rstn_i_sync1 or rstn_i_sync2) = '0') then -- signal stable somehow?
273
      rstn_gen <= (others => '0');
274
    elsif rising_edge(clk_i) then
275
      rstn_gen <= rstn_gen(rstn_gen'left-1 downto 0) & '1';
276
    end if;
277
  end process reset_generator;
278
 
279
  ext_rstn <= rstn_gen(rstn_gen'left); -- the beautified external reset signal
280
  sys_rstn <= ext_rstn and wdt_rstn; -- system reset - can also be triggered by watchdog
281
 
282
 
283
  -- Clock Generator ------------------------------------------------------------------------
284
  -- -------------------------------------------------------------------------------------------
285
  clock_generator: process(sys_rstn, clk_i)
286
  begin
287
    if (sys_rstn = '0') then
288
      clk_div    <= (others => '0');
289
      clk_div_ff <= (others => '0');
290
    elsif rising_edge(clk_i) then
291
      -- anybody wanting some fresh clocks? --
292
      if ((wdt_cg_en or uart_cg_en or spi_cg_en or twi_cg_en or pwm_cg_en) = '1') then
293
        clk_div    <= std_ulogic_vector(unsigned(clk_div) + 1);
294
        clk_div_ff <= clk_div;
295
      end if;
296
    end if;
297
  end process clock_generator;
298
 
299
  -- clock enable select: rising edge detectors --
300
  clk_gen(clk_div2_c)    <= clk_div(0)  and (not clk_div_ff(0));  -- CLK/2
301
  clk_gen(clk_div4_c)    <= clk_div(1)  and (not clk_div_ff(1));  -- CLK/4
302
  clk_gen(clk_div8_c)    <= clk_div(2)  and (not clk_div_ff(2));  -- CLK/8
303
  clk_gen(clk_div64_c)   <= clk_div(5)  and (not clk_div_ff(5));  -- CLK/64
304
  clk_gen(clk_div128_c)  <= clk_div(6)  and (not clk_div_ff(6));  -- CLK/128
305
  clk_gen(clk_div1024_c) <= clk_div(9)  and (not clk_div_ff(9));  -- CLK/1024
306
  clk_gen(clk_div2048_c) <= clk_div(10) and (not clk_div_ff(10)); -- CLK/2048
307
  clk_gen(clk_div4096_c) <= clk_div(11) and (not clk_div_ff(11)); -- CLK/4096
308
 
309
 
310
  -- CPU ------------------------------------------------------------------------------------
311
  -- -------------------------------------------------------------------------------------------
312
  neorv32_cpu_inst: neorv32_cpu
313
  generic map (
314
    -- General --
315
    CLOCK_FREQUENCY           => CLOCK_FREQUENCY,   -- clock frequency of clk_i in Hz
316
    HART_ID                   => HART_ID,           -- custom hardware thread ID
317
    BOOTLOADER_USE            => BOOTLOADER_USE,    -- implement processor-internal bootloader?
318
    -- RISC-V CPU Extensions --
319
    CPU_EXTENSION_RISCV_C     => CPU_EXTENSION_RISCV_C,     -- implement compressed extension?
320
    CPU_EXTENSION_RISCV_E     => CPU_EXTENSION_RISCV_E,     -- implement embedded RF extension?
321
    CPU_EXTENSION_RISCV_M     => CPU_EXTENSION_RISCV_M,     -- implement muld/div extension?
322
    CPU_EXTENSION_RISCV_Zicsr => CPU_EXTENSION_RISCV_Zicsr, -- implement CSR system?
323
    -- Memory configuration: Instruction memory --
324
    MEM_ISPACE_BASE           => MEM_ISPACE_BASE,   -- base address of instruction memory space
325
    MEM_ISPACE_SIZE           => MEM_ISPACE_SIZE,   -- total size of instruction memory space in byte
326
    MEM_INT_IMEM_USE          => MEM_INT_IMEM_USE,  -- implement processor-internal instruction memory
327
    MEM_INT_IMEM_SIZE         => MEM_INT_IMEM_SIZE, -- size of processor-internal instruction memory in bytes
328
    MEM_INT_IMEM_ROM          => MEM_INT_IMEM_ROM,  -- implement processor-internal instruction memory as ROM
329
    -- Memory configuration: Data memory --
330
    MEM_DSPACE_BASE           => MEM_DSPACE_BASE,   -- base address of data memory space
331
    MEM_DSPACE_SIZE           => MEM_DSPACE_SIZE,   -- total size of data memory space in byte
332
    MEM_INT_DMEM_USE          => MEM_INT_DMEM_USE,  -- implement processor-internal data memory
333
    MEM_INT_DMEM_SIZE         => MEM_INT_DMEM_SIZE, -- size of processor-internal data memory in bytes
334
    -- Memory configuration: External memory interface --
335
    MEM_EXT_USE               => MEM_EXT_USE,       -- implement external memory bus interface?
336
    MEM_EXT_TIMEOUT           => MEM_EXT_TIMEOUT,   -- cycles after which a valid bus access will timeout
337
    -- Processor peripherals --
338
    IO_GPIO_USE               => IO_GPIO_USE,       -- implement general purpose input/output port unit (GPIO)?
339
    IO_MTIME_USE              => IO_MTIME_USE,      -- implement machine system timer (MTIME)?
340
    IO_UART_USE               => IO_UART_USE,       -- implement universal asynchronous receiver/transmitter (UART)?
341
    IO_SPI_USE                => IO_SPI_USE,        -- implement serial peripheral interface (SPI)?
342
    IO_TWI_USE                => IO_TWI_USE,        -- implement two-wire interface (TWI)?
343
    IO_PWM_USE                => IO_PWM_USE,        -- implement pulse-width modulation unit (PWM)?
344
    IO_WDT_USE                => IO_WDT_USE,        -- implement watch dog timer (WDT)?
345
    IO_CLIC_USE               => IO_CLIC_USE,       -- implement core local interrupt controller (CLIC)?
346 3 zero_gravi
    IO_TRNG_USE               => IO_TRNG_USE,       -- implement true random number generator (TRNG)?
347
    IO_DEVNULL_USE            => IO_DEVNULL_USE     -- implement dummy device (DEVNULL)?
348 2 zero_gravi
  )
349
  port map (
350
    -- global control --
351
    clk_i       => clk_i,        -- global clock, rising edge
352
    rstn_i      => sys_rstn,     -- global reset, low-active, async
353
    -- bus interface --
354
    bus_addr_o  => cpu_addr,     -- bus access address
355
    bus_rdata_i => cpu_rdata,    -- bus read data
356
    bus_wdata_o => cpu_wdata,    -- bus write data
357
    bus_ben_o   => cpu_ben,      -- byte enable
358
    bus_we_o    => cpu_we,       -- write enable
359
    bus_re_o    => cpu_re,       -- read enable
360
    bus_ack_i   => cpu_ack,      -- bus transfer acknowledge
361
    bus_err_i   => cpu_err,      -- bus transfer error
362
    -- external interrupts --
363
    clic_irq_i  => clic_irq,     -- CLIC interrupt request
364
    mtime_irq_i => mtime_irq     -- machine timer interrupt
365
  );
366
 
367
  -- CPU data input --
368
  cpu_rdata <= (imem_rdata or dmem_rdata or bootrom_rdata) or wishbone_rdata or (gpio_rdata or mtime_rdata or
369 3 zero_gravi
               uart_rdata or spi_rdata or twi_rdata or pwm_rdata or wdt_rdata or clic_rdata or trng_rdata or devnull_rdata);
370 2 zero_gravi
 
371
  -- CPU ACK input --
372
  cpu_ack <= (imem_ack or dmem_ack or bootrom_ack) or wishbone_ack or (gpio_ack or mtime_ack or
373 3 zero_gravi
              uart_ack or spi_ack or twi_ack or pwm_ack or wdt_ack or clic_ack or trng_ack or devnull_ack);
374 2 zero_gravi
 
375
  -- CPU bus error input --
376
  cpu_err <= wishbone_err;
377
 
378
 
379
  -- Processor-Internal Instruction Memory (IMEM) -------------------------------------------
380
  -- -------------------------------------------------------------------------------------------
381
  neorv32_int_imem_inst_true:
382
  if (MEM_INT_IMEM_USE = true) generate
383
    neorv32_int_imem_inst: neorv32_imem
384
    generic map (
385
      IMEM_BASE      => MEM_ISPACE_BASE,   -- memory base address
386
      IMEM_SIZE      => MEM_INT_IMEM_SIZE, -- processor-internal instruction memory size in bytes
387
      IMEM_AS_ROM    => MEM_INT_IMEM_ROM,  -- implement IMEM as read-only memory?
388
      BOOTLOADER_USE => BOOTLOADER_USE     -- implement and use bootloader?
389
    )
390
    port map (
391
      clk_i  => clk_i,      -- global clock line
392
      rden_i => cpu_re,     -- read enable
393
      wren_i => cpu_we,     -- write enable
394
      ben_i  => cpu_ben,    -- byte write enable
395
      upen_i => '1',        -- update enable
396
      addr_i => cpu_addr,   -- address
397
      data_i => cpu_wdata,  -- data in
398
      data_o => imem_rdata, -- data out
399
      ack_o  => imem_ack    -- transfer acknowledge
400
    );
401
  end generate;
402
 
403
  neorv32_int_imem_inst_false:
404
  if (MEM_INT_IMEM_USE = false) generate
405
    imem_rdata <= (others => '0');
406
    imem_ack   <= '0';
407
  end generate;
408
 
409
 
410
  -- Processor-Internal Data Memory (DMEM) --------------------------------------------------
411
  -- -------------------------------------------------------------------------------------------
412
  neorv32_int_dmem_inst_true:
413
  if (MEM_INT_DMEM_USE = true) generate
414
    neorv32_int_dmem_inst: neorv32_dmem
415
    generic map (
416
      DMEM_BASE => MEM_DSPACE_BASE,  -- memory base address
417
      DMEM_SIZE => MEM_INT_DMEM_SIZE -- processor-internal data memory size in bytes
418
    )
419
    port map (
420
      clk_i  => clk_i,      -- global clock line
421
      rden_i => cpu_re,     -- read enable
422
      wren_i => cpu_we,     -- write enable
423
      ben_i  => cpu_ben,    -- byte write enable
424
      addr_i => cpu_addr,   -- address
425
      data_i => cpu_wdata,  -- data in
426
      data_o => dmem_rdata, -- data out
427
      ack_o  => dmem_ack    -- transfer acknowledge
428
    );
429
  end generate;
430
 
431
  neorv32_int_dmem_inst_false:
432
  if (MEM_INT_DMEM_USE = false) generate
433
    dmem_rdata <= (others => '0');
434
    dmem_ack   <= '0';
435
  end generate;
436
 
437
 
438
  -- Processor-Internal Bootloader ROM (BOOTROM) --------------------------------------------
439
  -- -------------------------------------------------------------------------------------------
440
  neorv32_boot_rom_inst_true:
441
  if (BOOTLOADER_USE = true) generate
442
    neorv32_boot_rom_inst: neorv32_boot_rom
443
    port map (
444
      clk_i  => clk_i,         -- global clock line
445
      rden_i => cpu_re,        -- read enable
446
      addr_i => cpu_addr,      -- address
447
      data_o => bootrom_rdata, -- data out
448
      ack_o  => bootrom_ack    -- transfer acknowledge
449
    );
450
  end generate;
451
 
452
  neorv32_boot_rom_inst_false:
453
  if (BOOTLOADER_USE = false) generate
454
    bootrom_rdata <= (others => '0');
455
    bootrom_ack   <= '0';
456
  end generate;
457
 
458
 
459
  -- External Wishbone Gateway (WISHBONE) ---------------------------------------------------
460
  -- -------------------------------------------------------------------------------------------
461
  neorv32_wishbone_inst_true:
462
  if (MEM_EXT_USE = true) generate
463
    neorv32_wishbone_inst: neorv32_wishbone
464
    generic map (
465
      INTERFACE_REG_STAGES => MEM_EXT_REG_STAGES, -- number of interface register stages (0,1,2)
466
      -- Memory configuration: Instruction memory --
467
      MEM_ISPACE_BASE      => MEM_ISPACE_BASE,   -- base address of instruction memory space
468
      MEM_ISPACE_SIZE      => MEM_ISPACE_SIZE,   -- total size of instruction memory space in byte
469
      MEM_INT_IMEM_USE     => MEM_INT_IMEM_USE,  -- implement processor-internal instruction memory
470
      MEM_INT_IMEM_SIZE    => MEM_INT_IMEM_SIZE, -- size of processor-internal instruction memory in bytes
471
      -- Memory configuration: Data memory --
472
      MEM_DSPACE_BASE      => MEM_DSPACE_BASE,   -- base address of data memory space
473
      MEM_DSPACE_SIZE      => MEM_DSPACE_SIZE,   -- total size of data memory space in byte
474
      MEM_INT_DMEM_USE     => MEM_INT_DMEM_USE,  -- implement processor-internal data memory
475
      MEM_INT_DMEM_SIZE    => MEM_INT_DMEM_SIZE  -- size of processor-internal data memory in bytes
476
    )
477
    port map (
478
      -- global control --
479
      clk_i    => clk_i,          -- global clock line
480
      rstn_i   => sys_rstn,       -- global reset line, low-active
481
      -- host access --
482
      addr_i   => cpu_addr,       -- address
483
      rden_i   => cpu_re,         -- read enable
484
      wren_i   => cpu_we,         -- write enable
485
      ben_i    => cpu_ben,        -- byte write enable
486
      data_i   => cpu_wdata,      -- data in
487
      data_o   => wishbone_rdata, -- data out
488
      ack_o    => wishbone_ack,   -- transfer acknowledge
489
      err_o    => wishbone_err,   -- transfer error
490
      -- wishbone interface --
491
      wb_adr_o => wb_adr_o,       -- address
492
      wb_dat_i => wb_dat_i,       -- read data
493
      wb_dat_o => wb_dat_o,       -- write data
494
      wb_we_o  => wb_we_o,        -- read/write
495
      wb_sel_o => wb_sel_o,       -- byte enable
496
      wb_stb_o => wb_stb_o,       -- strobe
497
      wb_cyc_o => wb_cyc_o,       -- valid cycle
498
      wb_ack_i => wb_ack_i,       -- transfer acknowledge
499
      wb_err_i => wb_err_i        -- transfer error
500
    );
501
  end generate;
502
 
503
  neorv32_wishbone_inst_false:
504
  if (MEM_EXT_USE = false) generate
505
    wishbone_rdata <= (others => '0');
506
    wishbone_ack   <= '0';
507
    wishbone_err   <= '0';
508
    --
509
    wb_adr_o <= (others => '0');
510
    wb_dat_o <= (others => '0');
511
    wb_we_o  <= '0';
512
    wb_sel_o <= (others => '0');
513
    wb_stb_o <= '0';
514
    wb_cyc_o <= '0';
515
  end generate;
516
 
517
 
518
  -- IO Access? -----------------------------------------------------------------------------
519
  -- -------------------------------------------------------------------------------------------
520
  io_acc  <= '1' when (cpu_addr(data_width_c-1 downto index_size_f(io_size_c)) = io_base_c(data_width_c-1 downto index_size_f(io_size_c))) else '0';
521
  io_rden <= io_acc and cpu_re;
522
  io_wren <= io_acc and cpu_we;
523
 
524
 
525
  -- General Purpose Input/Output Port (GPIO) -----------------------------------------------
526
  -- -------------------------------------------------------------------------------------------
527
  neorv32_gpio_inst_true:
528
  if (IO_GPIO_USE = true) generate
529
    neorv32_gpio_inst: neorv32_gpio
530
    port map (
531
      -- host access --
532
      clk_i  => clk_i,      -- global clock line
533
      addr_i => cpu_addr,   -- address
534
      rden_i => io_rden,    -- read enable
535
      wren_i => io_wren,    -- write enable
536
      ben_i  => cpu_ben,    -- byte write enable
537
      data_i => cpu_wdata,  -- data in
538
      data_o => gpio_rdata, -- data out
539
      ack_o  => gpio_ack,   -- transfer acknowledge
540
      -- parallel io --
541
      gpio_o => gpio_o,
542
      gpio_i => gpio_i,
543
      -- interrupt --
544
      irq_o  => gpio_irq    -- pin-change interrupt
545
    );
546
  end generate;
547
 
548
  neorv32_gpio_inst_false:
549
  if (IO_GPIO_USE = false) generate
550
    gpio_rdata <= (others => '0');
551
    gpio_ack   <= '0';
552
    gpio_o     <= (others => '0');
553
    gpio_irq   <= '0';
554
  end generate;
555
 
556
 
557
  -- Core-Local Interrupt Controller (CLIC) -------------------------------------------------
558
  -- -------------------------------------------------------------------------------------------
559
  neorv32_clic_inst_true:
560
  if (IO_CLIC_USE = true) generate
561
    neorv32_clic_inst: neorv32_clic
562
    port map (
563
      -- host access --
564
      clk_i     => clk_i,      -- global clock line
565
      rden_i    => io_rden,    -- read enable
566
      wren_i    => io_wren,    -- write enable
567
      ben_i     => cpu_ben,    -- byte write enable
568
      addr_i    => cpu_addr,   -- address
569
      data_i    => cpu_wdata,  -- data in
570
      data_o    => clic_rdata, -- data out
571
      ack_o     => clic_ack,   -- transfer acknowledge
572
      -- cpu interrupt --
573
      cpu_irq_o => clic_irq,   -- trigger CPU's external IRQ
574
      -- external interrupt lines --
575
      ext_irq_i => clic_xirq,  -- IRQ, triggering on HIGH level
576
      ext_ack_o => clic_xack   -- acknowledge
577
    );
578
  end generate;
579
 
580
  -- CLIC interrupt channels and priority --
581
  clic_xirq(0) <= wdt_irq; -- highest priority
582
  clic_xirq(1) <= '0'; -- reserved
583
  clic_xirq(2) <= gpio_irq;
584
  clic_xirq(3) <= uart_irq;
585
  clic_xirq(4) <= spi_irq;
586
  clic_xirq(5) <= twi_irq;
587
  clic_xirq(6) <= ext_irq_i(0);
588
  clic_xirq(7) <= ext_irq_i(1); -- lowest priority
589
 
590
  ext_ack_o <= clic_xirq(7 downto 6); -- external interrupt request acknowledge
591
 
592
  neorv32_clic_inst_false:
593
  if (IO_CLIC_USE = false) generate
594
    clic_rdata <= (others => '0');
595
    clic_ack   <= '0';
596
    clic_irq   <= '0';
597
    clic_xack  <= (others => '0');
598
  end generate;
599
 
600
 
601
  -- Watch Dog Timer (WDT) ------------------------------------------------------------------
602
  -- -------------------------------------------------------------------------------------------
603
  neorv32_wdt_inst_true:
604
  if (IO_WDT_USE = true) generate
605
    neorv32_wdt_inst: neorv32_wdt
606
    port map (
607
      -- host access --
608
      clk_i       => clk_i,      -- global clock line
609
      rstn_i      => ext_rstn,   -- global reset line, low-active
610
      rden_i      => io_rden,    -- read enable
611
      wren_i      => io_wren,    -- write enable
612
      ben_i       => cpu_ben,    -- byte write enable
613
      addr_i      => cpu_addr,   -- address
614
      data_i      => cpu_wdata,  -- data in
615
      data_o      => wdt_rdata,  -- data out
616
      ack_o       => wdt_ack,    -- transfer acknowledge
617
      -- clock generator --
618
      clkgen_en_o => wdt_cg_en,  -- enable clock generator
619
      clkgen_i    => clk_gen,
620
      -- timeout event --
621
      irq_o       => wdt_irq,    -- timeout IRQ
622
      rstn_o      => wdt_rstn    -- timeout reset, low_active, use it as async!
623
    );
624
  end generate;
625
 
626
  neorv32_wdt_inst_false:
627
  if (IO_WDT_USE = false) generate
628
    wdt_rdata <= (others => '0');
629
    wdt_ack   <= '0';
630
    wdt_irq   <= '0';
631
    wdt_rstn  <= '1';
632
    wdt_cg_en <= '0';
633
  end generate;
634
 
635
 
636
  -- Machine System Timer (MTIME) -----------------------------------------------------------
637
  -- -------------------------------------------------------------------------------------------
638
  neorv32_mtime_inst_true:
639
  if (IO_MTIME_USE = true) generate
640
    neorv32_mtime_inst: neorv32_mtime
641
    port map (
642
      -- host access --
643
      clk_i     => clk_i,        -- global clock line
644
      addr_i    => cpu_addr,     -- address
645
      rden_i    => io_rden,      -- read enable
646
      wren_i    => io_wren,      -- write enable
647
      ben_i     => cpu_ben,      -- byte write enable
648
      data_i    => cpu_wdata,    -- data in
649
      data_o    => mtime_rdata,  -- data out
650
      ack_o     => mtime_ack,    -- transfer acknowledge
651
      -- interrupt --
652
      irq_o     => mtime_irq     -- interrupt request
653
    );
654
  end generate;
655
 
656
  neorv32_mtime_inst_false:
657
  if (IO_MTIME_USE = false) generate
658
    mtime_rdata <= (others => '0');
659
    mtime_ack   <= '0';
660
    mtime_irq   <= '0';
661
  end generate;
662
 
663
 
664
  -- Universal Asynchronous Receiver/Transmitter (UART) -------------------------------------
665
  -- -------------------------------------------------------------------------------------------
666
  neorv32_uart_inst_true:
667
  if (IO_UART_USE = true) generate
668
    neorv32_uart_inst: neorv32_uart
669
    port map (
670
      -- host access --
671
      clk_i       => clk_i,      -- global clock line
672
      addr_i      => cpu_addr,   -- address
673
      rden_i      => io_rden,    -- read enable
674
      wren_i      => io_wren,    -- write enable
675
      ben_i       => cpu_ben,    -- byte write enable
676
      data_i      => cpu_wdata,  -- data in
677
      data_o      => uart_rdata, -- data out
678
      ack_o       => uart_ack,   -- transfer acknowledge
679
      -- clock generator --
680
      clkgen_en_o => uart_cg_en, -- enable clock generator
681
      clkgen_i    => clk_gen,
682
      -- com lines --
683
      uart_txd_o  => uart_txd_o,
684
      uart_rxd_i  => uart_rxd_i,
685
      -- interrupts --
686
      uart_irq_o  => uart_irq    -- uart rx/tx interrupt
687
    );
688
  end generate;
689
 
690
  neorv32_uart_inst_false:
691
  if (IO_UART_USE = false) generate
692
    uart_rdata <= (others => '0');
693
    uart_ack   <= '0';
694
    uart_txd_o <= '0';
695
    uart_cg_en <= '0';
696
    uart_irq   <= '0';
697
  end generate;
698
 
699
 
700
  -- Serial Peripheral Interface (SPI) ------------------------------------------------------
701
  -- -------------------------------------------------------------------------------------------
702
  neorv32_spi_inst_true:
703
  if (IO_SPI_USE = true) generate
704
    neorv32_spi_inst: neorv32_spi
705
    port map (
706
      -- host access --
707
      clk_i       => clk_i,      -- global clock line
708
      addr_i      => cpu_addr,   -- address
709
      rden_i      => io_rden,    -- read enable
710
      wren_i      => io_wren,    -- write enable
711
      ben_i       => cpu_ben,    -- byte write enable
712
      data_i      => cpu_wdata,  -- data in
713
      data_o      => spi_rdata,  -- data out
714
      ack_o       => spi_ack,    -- transfer acknowledge
715
      -- clock generator --
716
      clkgen_en_o => spi_cg_en,  -- enable clock generator
717
      clkgen_i    => clk_gen,
718
      -- com lines --
719
      spi_sclk_o  => spi_sclk_o, -- SPI serial clock
720
      spi_mosi_o  => spi_mosi_o, -- SPI master out, slave in
721
      spi_miso_i  => spi_miso_i, -- SPI master in, slave out
722
      spi_csn_o   => spi_csn_o,  -- SPI CS
723
      -- interrupt --
724
      spi_irq_o   => spi_irq     -- transmission done interrupt
725
    );
726
  end generate;
727
 
728
  neorv32_spi_inst_false:
729
  if (IO_SPI_USE = false) generate
730
    spi_rdata  <= (others => '0');
731
    spi_ack    <= '0';
732
    spi_sclk_o <= '0';
733
    spi_mosi_o <= '0';
734
    spi_csn_o  <= (others => '1'); -- CSn lines are low-active
735
    spi_cg_en  <= '0';
736
    spi_irq    <= '0';
737
  end generate;
738
 
739
 
740
  -- Two-Wire Interface (TWI) ---------------------------------------------------------------
741
  -- -------------------------------------------------------------------------------------------
742
  neorv32_twi_inst_true:
743
  if (IO_TWI_USE = true) generate
744
    neorv32_twi_inst: neorv32_twi
745
    port map (
746
      -- host access --
747
      clk_i       => clk_i,      -- global clock line
748
      addr_i      => cpu_addr,   -- address
749
      rden_i      => io_rden,    -- read enable
750
      wren_i      => io_wren,    -- write enable
751
      ben_i       => cpu_ben,    -- byte write enable
752
      data_i      => cpu_wdata,  -- data in
753
      data_o      => twi_rdata,  -- data out
754
      ack_o       => twi_ack,    -- transfer acknowledge
755
      -- clock generator --
756
      clkgen_en_o => twi_cg_en,  -- enable clock generator
757
      clkgen_i    => clk_gen,
758
      -- com lines --
759
      twi_sda_io  => twi_sda_io, -- serial data line
760
      twi_scl_io  => twi_scl_io, -- serial clock line
761
      -- interrupt --
762
      twi_irq_o   => twi_irq     -- transfer done IRQ
763
    );
764
  end generate;
765
 
766
  neorv32_twi_inst_false:
767
  if (IO_TWI_USE = false) generate
768
    twi_rdata  <= (others => '0');
769
    twi_ack    <= '0';
770
--  twi_sda_io <= 'H';
771
--  twi_scl_io <= 'H';
772
    twi_cg_en  <= '0';
773
    twi_irq    <= '0';
774
  end generate;
775
 
776
 
777
  -- Pulse-Width Modulation Controller (PWM) ------------------------------------------------
778
  -- -------------------------------------------------------------------------------------------
779
  neorv32_pwm_inst_true:
780
  if (IO_PWM_USE = true) generate
781
    neorv32_pwm_inst: neorv32_pwm
782
    port map (
783
      -- host access --
784
      clk_i       => clk_i,      -- global clock line
785
      addr_i      => cpu_addr,   -- address
786
      rden_i      => io_rden,    -- read enable
787
      wren_i      => io_wren,    -- write enable
788
      ben_i       => cpu_ben,    -- byte write enable
789
      data_i      => cpu_wdata,  -- data in
790
      data_o      => pwm_rdata,  -- data out
791
      ack_o       => pwm_ack,    -- transfer acknowledge
792
      -- clock generator --
793
      clkgen_en_o => pwm_cg_en,  -- enable clock generator
794
      clkgen_i    => clk_gen,
795
      -- pwm output channels --
796
      pwm_o       => pwm_o
797
    );
798
  end generate;
799
 
800
  neorv32_pwm_inst_false:
801
  if (IO_PWM_USE = false) generate
802
    pwm_rdata <= (others => '0');
803
    pwm_ack   <= '0';
804
    pwm_cg_en <= '0';
805
    pwm_o     <= (others => '0');
806
  end generate;
807
 
808
 
809
  -- True Random Number Generator (TRNG) ----------------------------------------------------
810
  -- -------------------------------------------------------------------------------------------
811
  neorv32_trng_inst_true:
812
  if (IO_TRNG_USE = true) generate
813
    neorv32_trng_inst: neorv32_trng
814
    port map (
815
      -- host access --
816
      clk_i  => clk_i,      -- global clock line
817
      addr_i => cpu_addr,   -- address
818
      rden_i => io_rden,    -- read enable
819
      wren_i => io_wren,    -- write enable
820
      ben_i  => cpu_ben,    -- byte write enable
821
      data_i => cpu_wdata,  -- data in
822
      data_o => trng_rdata, -- data out
823
      ack_o  => trng_ack    -- transfer acknowledge
824
    );
825
  end generate;
826
 
827
  neorv32_trng_inst_false:
828
  if (IO_TRNG_USE = false) generate
829
    trng_rdata <= (others => '0');
830
    trng_ack   <= '0';
831
  end generate;
832
 
833
 
834 3 zero_gravi
  -- Dummy Device (DEVNULL) -----------------------------------------------------------------
835
  -- -------------------------------------------------------------------------------------------
836
  neorv32_devnull_inst_true:
837
  if (IO_DEVNULL_USE = true) generate
838
    neorv32_devnull_inst: neorv32_devnull
839
    port map (
840
      -- host access --
841
      clk_i  => clk_i,         -- global clock line
842
      addr_i => cpu_addr,      -- address
843
      rden_i => io_rden,       -- read enable
844
      wren_i => io_wren,       -- write enable
845
      ben_i  => cpu_ben,       -- byte write enable
846
      data_i => cpu_wdata,     -- data in
847
      data_o => devnull_rdata, -- data out
848
      ack_o  => devnull_ack    -- transfer acknowledge
849
    );
850
  end generate;
851
 
852
  neorv32_devnull_inst_false:
853
  if (IO_DEVNULL_USE = false) generate
854
    devnull_rdata <= (others => '0');
855
    devnull_ack   <= '0';
856
  end generate;
857
 
858
 
859 2 zero_gravi
end neorv32_top_rtl;

powered by: WebSVN 2.1.0

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