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

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [rtl/] [riverlib/] [core/] [proc.vhd] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
-----------------------------------------------------------------------------
2
--! @file
3
--! @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
4
--! @author    Sergey Khabarov - sergeykhbr@gmail.com
5
--! @brief     CPU pipeline implementation.
6
------------------------------------------------------------------------------
7
 
8
library ieee;
9
use ieee.std_logic_1164.all;
10
library commonlib;
11
use commonlib.types_common.all;
12
--! RIVER CPU specific library.
13
library riverlib;
14
--! RIVER CPU configuration constants.
15
use riverlib.river_cfg.all;
16
 
17
 
18
entity Processor is
19
  port (
20
    i_clk : in std_logic;                                             -- CPU clock
21
    i_nrst : in std_logic;                                            -- Reset. Active LOW.
22
    -- Control path:
23
    i_req_ctrl_ready : in std_logic;                                  -- ICache is ready to accept request
24
    o_req_ctrl_valid : out std_logic;                                 -- Request to ICache is valid
25
    o_req_ctrl_addr : out std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);-- Requesting address to ICache
26
    i_resp_ctrl_valid : in std_logic;                                 -- ICache response is valid
27
    i_resp_ctrl_addr : in std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);-- Response address must be equal to the latest request address
28
    i_resp_ctrl_data : in std_logic_vector(31 downto 0);              -- Read value
29
    o_resp_ctrl_ready : out std_logic;
30
    -- Data path:
31
    i_req_data_ready : in std_logic;                                  -- DCache is ready to accept request
32
    o_req_data_valid : out std_logic;                                 -- Request to DCache is valid
33
    o_req_data_write : out std_logic;                                 -- Read/Write transaction
34
    o_req_data_size : out std_logic_vector(1 downto 0);               -- Size [Bytes]: 0=1B; 1=2B; 2=4B; 3=8B
35
    o_req_data_addr : out std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);-- Requesting address to DCache
36
    o_req_data_data : out std_logic_vector(RISCV_ARCH-1 downto 0);    -- Writing value
37
    i_resp_data_valid : in std_logic;                                 -- DCache response is valid
38
    i_resp_data_addr : in std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);-- DCache response address must be equal to the latest request address
39
    i_resp_data_data : in std_logic_vector(RISCV_ARCH-1 downto 0);    -- Read value
40
    o_resp_data_ready : out std_logic;
41
    -- External interrupt pin
42
    i_ext_irq : in std_logic;                                         -- PLIC interrupt accordingly with spec
43
    o_time : out std_logic_vector(63 downto 0);                       -- Timer in clock except halt state
44
    -- Debug interface:
45
    i_dport_valid : in std_logic;                                     -- Debug access from DSU is valid
46
    i_dport_write : in std_logic;                                     -- Write command flag
47
    i_dport_region : in std_logic_vector(1 downto 0);                 -- Registers region ID: 0=CSR; 1=IREGS; 2=Control
48
    i_dport_addr : in std_logic_vector(11 downto 0);                  -- Register idx
49
    i_dport_wdata : in std_logic_vector(RISCV_ARCH-1 downto 0);       -- Write value
50
    o_dport_ready : out std_logic;                                    -- Response is ready
51
    o_dport_rdata : out std_logic_vector(RISCV_ARCH-1 downto 0);      -- Response value
52
    -- Debug signals:
53
    i_istate : in std_logic_vector(1 downto 0);                       -- ICache state machine value
54
    i_dstate : in std_logic_vector(1 downto 0);                       -- DCache state machine value
55
    i_cstate : in std_logic_vector(1 downto 0)                        -- CacheTop state machine value
56
  );
57
end;
58
 
59
architecture arch_Processor of Processor is
60
 
61
    type FetchType is record
62
        req_fire : std_logic;
63
        valid : std_logic;
64
        pc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
65
        instr : std_logic_vector(31 downto 0);
66
        imem_req_valid : std_logic;
67
        imem_req_addr : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
68
        predict_miss : std_logic;
69
        pipeline_hold : std_logic;
70
        instr_buf : std_logic_vector(DBG_FETCH_TRACE_SIZE*64-1 downto 0);
71
    end record;
72
 
73
    type InstructionDecodeType is record
74
        pc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
75
        instr : std_logic_vector(31 downto 0);
76
        instr_valid : std_logic;
77
        memop_store : std_logic;
78
        memop_load : std_logic;
79
        memop_sign_ext : std_logic;
80
        memop_size : std_logic_vector(1 downto 0);
81
        rv32 : std_logic;                                    -- 32-bits instruction
82
        compressed : std_logic;                              -- C-extension
83
        unsigned_op : std_logic;                             -- Unsigned operands
84
        isa_type : std_logic_vector(ISA_Total-1 downto 0);
85
        instr_vec : std_logic_vector(Instr_Total-1 downto 0);
86
        exception : std_logic;
87
    end record;
88
 
89
    type ExecuteType is record
90
        valid : std_logic;
91
        instr : std_logic_vector(31 downto 0);
92
        pc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
93
        npc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
94
 
95
        radr1 : std_logic_vector(4 downto 0);
96
        radr2 : std_logic_vector(4 downto 0);
97
        res_addr : std_logic_vector(4 downto 0);
98
        res_data : std_logic_vector(RISCV_ARCH-1 downto 0);
99
        trap_ena : std_logic;                                 -- Trap pulse
100
        trap_code : std_logic_vector(4 downto 0);             -- bit[4] : 1=interrupt; 0=exception; bits[3:0]=code
101
        trap_pc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);-- trap on pc
102
        xret : std_logic;
103
        csr_addr : std_logic_vector(11 downto 0);
104
        csr_wena : std_logic;
105
        csr_wdata : std_logic_vector(RISCV_ARCH-1 downto 0);
106
 
107
        memop_sign_ext : std_logic;
108
        memop_load : std_logic;
109
        memop_store : std_logic;
110
        memop_size : std_logic_vector(1 downto 0);
111
        memop_addr : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
112
        pipeline_hold : std_logic;                            -- Hold pipeline from Execution stage
113
        breakpoint : std_logic;
114
        call : std_logic;
115
        ret : std_logic;
116
    end record;
117
 
118
    type MemoryType is record
119
        valid : std_logic;
120
        instr : std_logic_vector(31 downto 0);
121
        pc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
122
        pipeline_hold : std_logic;
123
    end record;
124
 
125
    type WriteBackType is record
126
        pc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
127
        wena : std_logic;
128
        waddr : std_logic_vector(4 downto 0);
129
        wdata : std_logic_vector(RISCV_ARCH-1 downto 0);
130
    end record;
131
 
132
    type IntRegsType is record
133
        rdata1 : std_logic_vector(RISCV_ARCH-1 downto 0);
134
        rdata2 : std_logic_vector(RISCV_ARCH-1 downto 0);
135
        dport_rdata : std_logic_vector(RISCV_ARCH-1 downto 0);
136
        ra : std_logic_vector(RISCV_ARCH-1 downto 0);       -- Return address
137
    end record;
138
 
139
    type CsrType is record
140
        rdata : std_logic_vector(RISCV_ARCH-1 downto 0);
141
        dport_rdata : std_logic_vector(RISCV_ARCH-1 downto 0);
142
        ie : std_logic;                                     -- Interrupt enable bit
143
        mtvec : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);-- Interrupt descriptor table
144
        mode : std_logic_vector(1 downto 0);                -- Current processor mode
145
    end record;
146
 
147
    --! 5-stages CPU pipeline
148
    type PipelineType is record
149
        f : FetchType;                            -- Fetch instruction stage
150
        d : InstructionDecodeType;                -- Decode instruction stage
151
        e : ExecuteType;                          -- Execute instruction
152
        m : MemoryType;                           -- Memory load/store
153
        w : WriteBackType;                        -- Write back registers value
154
    end record;
155
 
156
    type DebugType is record
157
        core_addr : std_logic_vector(11 downto 0);           -- Address of the sub-region register
158
        core_wdata : std_logic_vector(RISCV_ARCH-1 downto 0);-- Write data
159
        csr_ena : std_logic;                                 -- Region 0: Access to CSR bank is enabled.
160
        csr_write : std_logic;                               -- Region 0: CSR write enable
161
        ireg_ena : std_logic;                                -- Region 1: Access to integer register bank is enabled
162
        ireg_write : std_logic;                              -- Region 1: Integer registers bank write pulse
163
        npc_write : std_logic;                               -- Region 1: npc write enable
164
        halt : std_logic;                                    -- Halt signal is equal to hold pipeline
165
        clock_cnt : std_logic_vector(63 downto 0);           -- Number of clocks excluding halt state
166
        executed_cnt : std_logic_vector(63 downto 0);        -- Number of executed instruction
167
        break_mode : std_logic;                              -- Behaviour on EBREAK instruction: 0 = halt; 1 = generate trap
168
        br_fetch_valid : std_logic;                          -- Fetch injection address/instr are valid
169
        br_address_fetch : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0); -- Fetch injection address to skip ebreak instruciton only once
170
        br_instr_fetch : std_logic_vector(31 downto 0);      -- Real instruction value that was replaced by ebreak
171
    end record;
172
 
173
    signal ireg : IntRegsType;
174
    signal csr : CsrType;
175
    signal w : PipelineType;
176
    signal dbg : DebugType;
177
 
178
    signal wb_npc_predict : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
179
 
180
    signal wb_ireg_dport_addr : std_logic_vector(4 downto 0);
181
    signal wb_exec_dport_npc : std_logic_vector(BUS_ADDR_WIDTH-1 downto 0);
182
 
183
    signal w_fetch_pipeline_hold : std_logic;
184
    signal w_any_pipeline_hold : std_logic;
185
    signal w_exec_pipeline_hold : std_logic;
186
 
187
begin
188
 
189
    w_fetch_pipeline_hold <= w.e.pipeline_hold or w.m.pipeline_hold or dbg.halt;
190
    w_any_pipeline_hold <= w.f.pipeline_hold or w.e.pipeline_hold
191
                          or w.m.pipeline_hold  or dbg.halt;
192
    w_exec_pipeline_hold <= w.f.pipeline_hold or w.m.pipeline_hold or dbg.halt;
193
 
194
    wb_ireg_dport_addr <= dbg.core_addr(4 downto 0);
195
    wb_exec_dport_npc <= dbg.core_wdata(BUS_ADDR_WIDTH-1 downto 0);
196
 
197
    fetch0 : InstrFetch port map (
198
        i_clk => i_clk,
199
        i_nrst => i_nrst,
200
        i_pipeline_hold => w_fetch_pipeline_hold,
201
        i_mem_req_ready => i_req_ctrl_ready,
202
        o_mem_addr_valid => w.f.imem_req_valid,
203
        o_mem_addr => w.f.imem_req_addr,
204
        i_mem_data_valid => i_resp_ctrl_valid,
205
        i_mem_data_addr => i_resp_ctrl_addr,
206
        i_mem_data => i_resp_ctrl_data,
207
        o_mem_resp_ready => o_resp_ctrl_ready,
208
        i_e_npc => w.e.npc,
209
        i_predict_npc => wb_npc_predict,
210
        o_predict_miss => w.f.predict_miss,
211
        o_mem_req_fire => w.f.req_fire,
212
        o_valid => w.f.valid,
213
        o_pc => w.f.pc,
214
        o_instr => w.f.instr,
215
        o_hold => w.f.pipeline_hold,
216
        i_br_fetch_valid => dbg.br_fetch_valid,
217
        i_br_address_fetch => dbg.br_address_fetch,
218
        i_br_instr_fetch => dbg.br_instr_fetch,
219
        o_instr_buf => w.f.instr_buf);
220
 
221
    dec0 : InstrDecoder port map (
222
        i_clk => i_clk,
223
        i_nrst => i_nrst,
224
        i_any_hold => w_any_pipeline_hold,
225
        i_f_valid => w.f.valid,
226
        i_f_pc => w.f.pc,
227
        i_f_instr => w.f.instr,
228
        o_valid => w.d.instr_valid,
229
        o_pc => w.d.pc,
230
        o_instr => w.d.instr,
231
        o_memop_store => w.d.memop_store,
232
        o_memop_load => w.d.memop_load,
233
        o_memop_sign_ext => w.d.memop_sign_ext,
234
        o_memop_size => w.d.memop_size,
235
        o_unsigned_op => w.d.unsigned_op,
236
        o_rv32 => w.d.rv32,
237
        o_compressed => w.d.compressed,
238
        o_isa_type => w.d.isa_type,
239
        o_instr_vec => w.d.instr_vec,
240
        o_exception => w.d.exception);
241
 
242
    exec0 : InstrExecute port map (
243
        i_clk => i_clk,
244
        i_nrst => i_nrst,
245
        i_pipeline_hold => w_exec_pipeline_hold,
246
        i_d_valid => w.d.instr_valid,
247
        i_d_pc => w.d.pc,
248
        i_d_instr => w.d.instr,
249
        i_wb_done => w.m.valid,
250
        i_memop_store => w.d.memop_store,
251
        i_memop_load => w.d.memop_load,
252
        i_memop_sign_ext => w.d.memop_sign_ext,
253
        i_memop_size => w.d.memop_size,
254
        i_unsigned_op => w.d.unsigned_op,
255
        i_rv32 => w.d.rv32,
256
        i_compressed => w.d.compressed,
257
        i_isa_type => w.d.isa_type,
258
        i_ivec => w.d.instr_vec,
259
        i_ie => csr.ie,
260
        i_mtvec => csr.mtvec,
261
        i_mode => csr.mode,
262
        i_break_mode => dbg.break_mode,
263
        i_unsup_exception => w.d.exception,
264
        i_ext_irq => i_ext_irq,
265
        i_dport_npc_write => dbg.npc_write,
266
        i_dport_npc => wb_exec_dport_npc,
267
        o_radr1 => w.e.radr1,
268
        i_rdata1 => ireg.rdata1,
269
        o_radr2 => w.e.radr2,
270
        i_rdata2 => ireg.rdata2,
271
        o_res_addr => w.e.res_addr,
272
        o_res_data => w.e.res_data,
273
        o_pipeline_hold => w.e.pipeline_hold,
274
        o_xret => w.e.xret,
275
        o_csr_addr => w.e.csr_addr,
276
        o_csr_wena => w.e.csr_wena,
277
        i_csr_rdata => csr.rdata,
278
        o_csr_wdata => w.e.csr_wdata,
279
        o_trap_ena => w.e.trap_ena,
280
        o_trap_code => w.e.trap_code,
281
        o_trap_pc => w.e.trap_pc,
282
        o_memop_sign_ext => w.e.memop_sign_ext,
283
        o_memop_load => w.e.memop_load,
284
        o_memop_store => w.e.memop_store,
285
        o_memop_size => w.e.memop_size,
286
        o_memop_addr => w.e.memop_addr,
287
        o_valid => w.e.valid,
288
        o_pc => w.e.pc,
289
        o_npc => w.e.npc,
290
        o_instr => w.e.instr,
291
        o_breakpoint => w.e.breakpoint,
292
        o_call => w.e.call,
293
        o_ret => w.e.ret);
294
 
295
    mem0 : MemAccess port map (
296
        i_clk => i_clk,
297
        i_nrst => i_nrst,
298
        i_e_valid => w.e.valid,
299
        i_e_pc => w.e.pc,
300
        i_e_instr => w.e.instr,
301
        i_res_addr => w.e.res_addr,
302
        i_res_data => w.e.res_data,
303
        i_memop_sign_ext => w.e.memop_sign_ext,
304
        i_memop_load => w.e.memop_load,
305
        i_memop_store => w.e.memop_store,
306
        i_memop_size => w.e.memop_size,
307
        i_memop_addr => w.e.memop_addr,
308
        o_waddr => w.w.waddr,
309
        o_wena => w.w.wena,
310
        o_wdata => w.w.wdata,
311
        i_mem_req_ready => i_req_data_ready,
312
        o_mem_valid => o_req_data_valid,
313
        o_mem_write => o_req_data_write,
314
        o_mem_sz => o_req_data_size,
315
        o_mem_addr => o_req_data_addr,
316
        o_mem_data => o_req_data_data,
317
        i_mem_data_valid => i_resp_data_valid,
318
        i_mem_data_addr => i_resp_data_addr,
319
        i_mem_data => i_resp_data_data,
320
        o_mem_resp_ready => o_resp_data_ready,
321
        o_hold => w.m.pipeline_hold,
322
        o_valid => w.m.valid,
323
        o_pc => w.m.pc,
324
        o_instr => w.m.instr);
325
 
326
    predic0 : BranchPredictor port map (
327
        i_clk => i_clk,
328
        i_nrst => i_nrst,
329
        i_req_mem_fire => w.f.req_fire,
330
        i_resp_mem_valid => i_resp_ctrl_valid,
331
        i_resp_mem_addr => i_resp_ctrl_addr,
332
        i_resp_mem_data => i_resp_ctrl_data,
333
        i_f_predic_miss => w.f.predict_miss,
334
        i_e_npc => w.e.npc,
335
        i_ra => ireg.ra,
336
        o_npc_predict => wb_npc_predict);
337
 
338
 
339
    iregs0 : RegIntBank port map (
340
        i_clk => i_clk,
341
        i_nrst => i_nrst,
342
        i_radr1 => w.e.radr1,
343
        o_rdata1 => ireg.rdata1,
344
        i_radr2 => w.e.radr2,
345
        o_rdata2 => ireg.rdata2,
346
        i_waddr => w.w.waddr,
347
        i_wena => w.w.wena,
348
        i_wdata => w.w.wdata,
349
        i_dport_addr => wb_ireg_dport_addr,
350
        i_dport_ena => dbg.ireg_ena,
351
        i_dport_write => dbg.ireg_write,
352
        i_dport_wdata => dbg.core_wdata,
353
        o_dport_rdata => ireg.dport_rdata,
354
        o_ra => ireg.ra);   -- Return address
355
 
356
    csr0 : CsrRegs port map (
357
        i_clk => i_clk,
358
        i_nrst => i_nrst,
359
        i_xret => w.e.xret,
360
        i_addr => w.e.csr_addr,
361
        i_wena => w.e.csr_wena,
362
        i_wdata => w.e.csr_wdata,
363
        o_rdata => csr.rdata,
364
        i_break_mode => dbg.break_mode,
365
        i_breakpoint => w.e.breakpoint,
366
        i_trap_ena => w.e.trap_ena,
367
        i_trap_code => w.e.trap_code,
368
        i_trap_pc => w.e.trap_pc,
369
        o_ie => csr.ie,
370
        o_mode => csr.mode,
371
        o_mtvec => csr.mtvec,
372
        i_dport_ena => dbg.csr_ena,
373
        i_dport_write => dbg.csr_write,
374
        i_dport_addr => dbg.core_addr,
375
        i_dport_wdata => dbg.core_wdata,
376
        o_dport_rdata => csr.dport_rdata);
377
 
378
 
379
    dbg0 : DbgPort port map (
380
        i_clk => i_clk,
381
        i_nrst => i_nrst,
382
        i_dport_valid => i_dport_valid,
383
        i_dport_write => i_dport_write,
384
        i_dport_region => i_dport_region,
385
        i_dport_addr => i_dport_addr,
386
        i_dport_wdata => i_dport_wdata,
387
        o_dport_ready => o_dport_ready,
388
        o_dport_rdata => o_dport_rdata,
389
        o_core_addr => dbg.core_addr,
390
        o_core_wdata => dbg.core_wdata,
391
        o_csr_ena => dbg.csr_ena,
392
        o_csr_write => dbg.csr_write,
393
        i_csr_rdata => csr.dport_rdata,
394
        o_ireg_ena => dbg.ireg_ena,
395
        o_ireg_write => dbg.ireg_write,
396
        o_npc_write => dbg.npc_write,
397
        i_ireg_rdata => ireg.dport_rdata,
398
        i_pc => w.e.pc,
399
        i_npc => w.e.npc,
400
        i_e_valid => w.e.valid,
401
        i_e_call => w.e.call,
402
        i_e_ret => w.e.ret,
403
        i_m_valid => w.m.valid,
404
        o_clock_cnt => dbg.clock_cnt,
405
        o_executed_cnt => dbg.executed_cnt,
406
        o_halt => dbg.halt,
407
        i_ebreak => w.e.breakpoint,
408
        o_break_mode => dbg.break_mode,
409
        o_br_fetch_valid => dbg.br_fetch_valid,
410
        o_br_address_fetch => dbg.br_address_fetch,
411
        o_br_instr_fetch => dbg.br_instr_fetch,
412
        i_istate => i_istate,
413
        i_dstate => i_dstate,
414
        i_cstate => i_cstate,
415
        i_instr_buf => w.f.instr_buf);
416
 
417
    o_req_ctrl_valid <= w.f.imem_req_valid;
418
    o_req_ctrl_addr <= w.f.imem_req_addr;
419
    o_time <= dbg.clock_cnt;
420
 
421
end;

powered by: WebSVN 2.1.0

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