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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu.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 - CPU Top Entity >>                                                                #
3
-- # ********************************************************************************************* #
4
-- # Top NEORV32 CPU:                                                                              #
5
-- # * neorv32_cpu_alu: Arithemtical/logical unit                                                  #
6
-- # * neorv32_cpu_ctrl: CPU control and CSR system                                                #
7
-- #   * neorv32_cpu_decompressor: Compressed instructions decoder                                 #
8
-- # * neorv32_cpu_bus: Memory/IO bus interface unit                                               #
9
-- # * neorv32_cpu_cp_muldiv: MULDIV co-processor                                                  #
10
-- # * neorv32_cpu_regfile: Data register file                                                     #
11
-- # ********************************************************************************************* #
12
-- # BSD 3-Clause License                                                                          #
13
-- #                                                                                               #
14
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
15
-- #                                                                                               #
16
-- # Redistribution and use in source and binary forms, with or without modification, are          #
17
-- # permitted provided that the following conditions are met:                                     #
18
-- #                                                                                               #
19
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
20
-- #    conditions and the following disclaimer.                                                   #
21
-- #                                                                                               #
22
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
23
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
24
-- #    provided with the distribution.                                                            #
25
-- #                                                                                               #
26
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
27
-- #    endorse or promote products derived from this software without specific prior written      #
28
-- #    permission.                                                                                #
29
-- #                                                                                               #
30
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
31
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
32
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
33
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
34
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
35
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
36
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
37
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
38
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
39
-- # ********************************************************************************************* #
40
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
41
-- #################################################################################################
42
 
43
library ieee;
44
use ieee.std_logic_1164.all;
45
use ieee.numeric_std.all;
46
 
47
library neorv32;
48
use neorv32.neorv32_package.all;
49
 
50
entity neorv32_cpu is
51
  generic (
52
    -- General --
53
    CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
54
    HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
55
    BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
56
    -- RISC-V CPU Extensions --
57
    CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
58
    CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
59
    CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
60
    CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
61
    -- Memory configuration: Instruction memory --
62
    MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
63
    MEM_ISPACE_SIZE           : natural := 8*1024; -- total size of instruction memory space in byte
64
    MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
65
    MEM_INT_IMEM_SIZE         : natural := 8*1024; -- size of processor-internal instruction memory in bytes
66
    MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
67
    -- Memory configuration: Data memory --
68
    MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
69
    MEM_DSPACE_SIZE           : natural := 4*1024; -- total size of data memory space in byte
70
    MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
71
    MEM_INT_DMEM_SIZE         : natural := 4*1024; -- size of processor-internal data memory in bytes
72
    -- Memory configuration: External memory interface --
73
    MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
74
    MEM_EXT_TIMEOUT           : natural := 15;     -- cycles after which a valid bus access will timeout
75
    -- Processor peripherals --
76
    IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
77
    IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
78
    IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
79
    IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
80
    IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
81
    IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
82
    IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
83
    IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
84 3 zero_gravi
    IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
85
    IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
86 2 zero_gravi
  );
87
  port (
88
    -- global control --
89
    clk_i       : in  std_ulogic; -- global clock, rising edge
90
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
91
    -- bus interface --
92
    bus_addr_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
93
    bus_rdata_i : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
94
    bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
95
    bus_ben_o   : out std_ulogic_vector(03 downto 0); -- byte enable
96
    bus_we_o    : out std_ulogic; -- write enable
97
    bus_re_o    : out std_ulogic; -- read enable
98
    bus_ack_i   : in  std_ulogic; -- bus transfer acknowledge
99
    bus_err_i   : in  std_ulogic; -- bus transfer error
100
    -- external interrupts --
101
    clic_irq_i  : in  std_ulogic; -- CLIC interrupt request
102
    mtime_irq_i : in  std_ulogic  -- machine timer interrupt
103
  );
104
end neorv32_cpu;
105
 
106
architecture neorv32_cpu_rtl of neorv32_cpu is
107
 
108
  -- local signals --
109
  signal ctrl        : std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
110
  signal alu_cmp     : std_ulogic_vector(1 downto 0); -- alu comparator result
111
  signal imm         : std_ulogic_vector(data_width_c-1 downto 0); -- immediate
112
  signal pc          : std_ulogic_vector(data_width_c-1 downto 0); -- current program counter
113
  signal pc_delayed  : std_ulogic_vector(data_width_c-1 downto 0); -- delayed program counter
114
  signal instr       : std_ulogic_vector(data_width_c-1 downto 0); -- new instruction
115
  signal rs1, rs2    : std_ulogic_vector(data_width_c-1 downto 0); -- source registers
116
  signal alu_res     : std_ulogic_vector(data_width_c-1 downto 0); -- alu result
117
  signal alu_add     : std_ulogic_vector(data_width_c-1 downto 0); -- alu adder result
118
  signal rdata       : std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
119
  signal alu_wait    : std_ulogic; -- alu is busy due to iterative unit
120
  signal bus_wait    : std_ulogic; -- wait for bus to finish operation
121
  signal csr_rdata   : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
122
  signal mar         : std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
123
  signal ma_instr    : std_ulogic; -- misaligned instruction address
124
  signal ma_load     : std_ulogic; -- misaligned load data address
125
  signal ma_store    : std_ulogic; -- misaligned store data address
126
  signal be_instr    : std_ulogic; -- bus error on instruction access
127
  signal be_load     : std_ulogic; -- bus error on load data access
128
  signal be_store    : std_ulogic; -- bus error on store data access
129
  signal bus_exc_ack : std_ulogic; -- bus exception error acknowledge
130
 
131
  -- co-processor interface --
132
  signal cp0_data,  cp1_data  : std_ulogic_vector(data_width_c-1 downto 0);
133
  signal cp0_valid, cp1_valid : std_ulogic;
134
 
135
begin
136
 
137
  -- Control Unit ---------------------------------------------------------------------------
138
  -- -------------------------------------------------------------------------------------------
139
  neorv32_cpu_control_inst: neorv32_cpu_control
140
  generic map (
141
    -- General --
142
    CLOCK_FREQUENCY           => CLOCK_FREQUENCY, -- clock frequency of clk_i in Hz
143
    HART_ID                   => HART_ID,         -- custom hardware thread ID
144
    BOOTLOADER_USE            => BOOTLOADER_USE,  -- implement processor-internal bootloader?
145
    -- RISC-V CPU Extensions --
146
    CPU_EXTENSION_RISCV_C     => CPU_EXTENSION_RISCV_C,     -- implement compressed extension?
147
    CPU_EXTENSION_RISCV_E     => CPU_EXTENSION_RISCV_E,     -- implement embedded RF extension?
148
    CPU_EXTENSION_RISCV_M     => CPU_EXTENSION_RISCV_M,     -- implement muld/div extension?
149
    CPU_EXTENSION_RISCV_Zicsr => CPU_EXTENSION_RISCV_Zicsr, -- implement CSR system?
150
    -- Memory configuration: Instruction memory --
151
    MEM_ISPACE_BASE           => MEM_ISPACE_BASE,   -- base address of instruction memory space
152
    MEM_ISPACE_SIZE           => MEM_ISPACE_SIZE,   -- total size of instruction memory space in byte
153
    MEM_INT_IMEM_USE          => MEM_INT_IMEM_USE,  -- implement processor-internal instruction memory
154
    MEM_INT_IMEM_SIZE         => MEM_INT_IMEM_SIZE, -- size of processor-internal instruction memory in bytes
155
    MEM_INT_IMEM_ROM          => MEM_INT_IMEM_ROM,  -- implement processor-internal instruction memory as ROM
156
    -- Memory configuration: Data memory --
157
    MEM_DSPACE_BASE           => MEM_DSPACE_BASE,   -- base address of data memory space
158
    MEM_DSPACE_SIZE           => MEM_DSPACE_SIZE,   -- total size of data memory space in byte
159
    MEM_INT_DMEM_USE          => MEM_INT_DMEM_USE,  -- implement processor-internal data memory
160
    MEM_INT_DMEM_SIZE         => MEM_INT_DMEM_SIZE, -- size of processor-internal data memory in bytes
161
    -- Memory configuration: External memory interface --
162
    MEM_EXT_USE               => MEM_EXT_USE,       -- implement external memory bus interface?
163
    -- Processor peripherals --
164
    IO_GPIO_USE               => IO_GPIO_USE,       -- implement general purpose input/output port unit (GPIO)?
165
    IO_MTIME_USE              => IO_MTIME_USE,      -- implement machine system timer (MTIME)?
166
    IO_UART_USE               => IO_UART_USE,       -- implement universal asynchronous receiver/transmitter (UART)?
167
    IO_SPI_USE                => IO_SPI_USE,        -- implement serial peripheral interface (SPI)?
168
    IO_TWI_USE                => IO_TWI_USE,        -- implement two-wire interface (TWI)?
169
    IO_PWM_USE                => IO_PWM_USE,        -- implement pulse-width modulation unit (PWM)?
170
    IO_WDT_USE                => IO_WDT_USE,        -- implement watch dog timer (WDT)?
171
    IO_CLIC_USE               => IO_CLIC_USE,       -- implement core local interrupt controller (CLIC)?
172 3 zero_gravi
    IO_TRNG_USE               => IO_TRNG_USE,       -- implement true random number generator (TRNG)?
173
    IO_DEVNULL_USE            => IO_DEVNULL_USE     -- implement dummy device (DEVNULL)?
174 2 zero_gravi
  )
175
  port map (
176
    -- global control --
177
    clk_i         => clk_i,       -- global clock, rising edge
178
    rstn_i        => rstn_i,      -- global reset, low-active, async
179
    ctrl_o        => ctrl,        -- main control bus
180
    -- status input --
181
    alu_wait_i    => alu_wait,    -- wait for ALU
182
    bus_wait_i    => bus_wait,    -- wait for bus
183
    -- data input --
184
    instr_i       => instr,       -- instruction
185
    cmp_i         => alu_cmp,     -- comparator status
186
    alu_add_i     => alu_add,     -- ALU.add result
187
    -- data output --
188
    imm_o         => imm,         -- immediate
189
    pc_o          => pc,          -- current PC
190
    alu_pc_o      => pc_delayed,  -- delayed PC for ALU
191
    -- csr interface --
192
    csr_wdata_i   => alu_res,     -- CSR write data
193
    csr_rdata_o   => csr_rdata,   -- CSR read data
194
    -- external interrupt --
195
    clic_irq_i    => clic_irq_i,  -- CLIC interrupt request
196
    mtime_irq_i   => mtime_irq_i, -- machine timer interrupt
197
    -- bus access exceptions --
198
    mar_i         => mar,         -- memory address register
199
    ma_instr_i    => ma_instr,    -- misaligned instruction address
200
    ma_load_i     => ma_load,     -- misaligned load data address
201
    ma_store_i    => ma_store,    -- misaligned store data address
202
    be_instr_i    => be_instr,    -- bus error on instruction access
203
    be_load_i     => be_load,     -- bus error on load data access
204
    be_store_i    => be_store,    -- bus error on store data access
205
    bus_exc_ack_o => bus_exc_ack  -- bus exception error acknowledge
206
  );
207
 
208
 
209
  -- Register File --------------------------------------------------------------------------
210
  -- -------------------------------------------------------------------------------------------
211
  neorv32_regfile_inst: neorv32_cpu_regfile
212
  generic map (
213
    CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E -- implement embedded RF extension?
214
  )
215
  port map (
216
    -- global control --
217
    clk_i  => clk_i,              -- global clock, rising edge
218
    ctrl_i => ctrl,               -- main control bus
219
    -- data input --
220
    mem_i  => rdata,              -- memory read data
221
    alu_i  => alu_res,            -- ALU result
222
    csr_i  => csr_rdata,          -- CSR read data
223
    pc_i   => pc,                 -- current pc
224
    -- data output --
225
    rs1_o  => rs1,                -- operand 1
226
    rs2_o  => rs2                 -- operand 2
227
  );
228
 
229
 
230
  -- ALU ------------------------------------------------------------------------------------
231
  -- -------------------------------------------------------------------------------------------
232
  neorv32_cpu_alu_inst: neorv32_cpu_alu
233
  generic map (
234
    CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
235
    CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M  -- implement mul/div extension?
236
  )
237
  port map (
238
    -- global control --
239
    clk_i       => clk_i,         -- global clock, rising edge
240
    rstn_i      => rstn_i,        -- global reset, low-active, async
241
    ctrl_i      => ctrl,          -- main control bus
242
    -- data input --
243
    rs1_i       => rs1,           -- rf source 1
244
    rs2_i       => rs2,           -- rf source 2
245
    pc_i        => pc,            -- current PC
246
    pc2_i       => pc_delayed,    -- delayed PC
247
    imm_i       => imm,           -- immediate
248
    csr_i       => csr_rdata,     -- csr read data
249
    -- data output --
250
    cmp_o       => alu_cmp,       -- comparator status
251
    add_o       => alu_add,       -- OPA + OPB
252
    res_o       => alu_res,       -- ALU result
253
    -- co-processor interface --
254
    cp0_data_i  => cp0_data,      -- co-processor 0 result
255
    cp0_valid_i => cp0_valid,     -- co-processor 0 result valid
256
    cp1_data_i  => cp1_data,      -- co-processor 1 result
257
    cp1_valid_i => cp1_valid,     -- co-processor 1 result valid
258
    -- status --
259
    wait_o      => alu_wait       -- busy due to iterative processing units
260
  );
261
 
262
 
263
  -- Co-Processor 0: MULDIV Unit ------------------------------------------------------------
264
  -- -------------------------------------------------------------------------------------------
265
  neorv32_cpu_cp_muldiv_inst_true:
266
  if (CPU_EXTENSION_RISCV_M = true) generate
267
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
268
    port map (
269
      -- global control --
270
      clk_i   => clk_i,           -- global clock, rising edge
271
      rstn_i  => rstn_i,          -- global reset, low-active, async
272
      ctrl_i  => ctrl,            -- main control bus
273
      -- data input --
274
      rs1_i   => rs1,             -- rf source 1
275
      rs2_i   => rs2,             -- rf source 2
276
      -- result and status --
277
      res_o   => cp0_data,        -- operation result
278
      valid_o => cp0_valid        -- data output valid
279
    );
280
  end generate;
281
 
282
  neorv32_cpu_cp_muldiv_inst_false:
283
  if (CPU_EXTENSION_RISCV_M = false) generate
284
    cp0_data  <= (others => '0');
285
    cp0_valid <= '0';
286
  end generate;
287
 
288
 
289
  -- Co-Processor 1: Not Implemented Yet ----------------------------------------------------
290
  -- -------------------------------------------------------------------------------------------
291
  cp1_data  <= (others => '0');
292
  cp1_valid <= '0';
293
 
294
 
295
  -- Bus Unit -------------------------------------------------------------------------------
296
  -- -------------------------------------------------------------------------------------------
297
  neorv32_cpu_bus_inst: neorv32_cpu_bus
298
  generic map (
299
    CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
300
    MEM_EXT_TIMEOUT       => MEM_EXT_TIMEOUT        -- cycles after which a valid bus access will timeout
301
  )
302
  port map (
303
    -- global control --
304
    clk_i       => clk_i,         -- global clock, rising edge
305
    rstn_i      => rstn_i,        -- global reset, low-active, async
306
    ctrl_i      => ctrl,          -- main control bus
307
    -- data input --
308
    wdata_i     => rs2,           -- write data
309
    pc_i        => pc,            -- current PC
310
    alu_i       => alu_res,       -- ALU result
311
    -- data output --
312
    instr_o     => instr,         -- instruction
313
    rdata_o     => rdata,         -- read data
314
    -- status --
315
    mar_o       => mar,           -- current memory address register
316
    ma_instr_o  => ma_instr,      -- misaligned instruction address
317
    ma_load_o   => ma_load,       -- misaligned load data address
318
    ma_store_o  => ma_store,      -- misaligned store data address
319
    be_instr_o  => be_instr,      -- bus error on instruction access
320
    be_load_o   => be_load,       -- bus error on load data access
321
    be_store_o  => be_store,      -- bus error on store data access
322
    bus_wait_o  => bus_wait,      -- wait for bus operation to finish
323
    exc_ack_i   => bus_exc_ack,   -- exception controller ACK
324
    -- bus system --
325
    bus_addr_o  => bus_addr_o,    -- bus access address
326
    bus_rdata_i => bus_rdata_i,   -- bus read data
327
    bus_wdata_o => bus_wdata_o,   -- bus write data
328
    bus_ben_o   => bus_ben_o,     -- byte enable
329
    bus_we_o    => bus_we_o,      -- write enable
330
    bus_re_o    => bus_re_o,      -- read enable
331
    bus_ack_i   => bus_ack_i,     -- bus transfer acknowledge
332
    bus_err_i   => bus_err_i      -- bus transfer error
333
  );
334
 
335
 
336
end neorv32_cpu_rtl;

powered by: WebSVN 2.1.0

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