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

Subversion Repositories potato

[/] [potato/] [trunk/] [src/] [pp_core.vhd] - Blame information for rev 34

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

Line No. Rev Author Line
1 2 skordal
-- The Potato Processor - A simple processor for FPGAs
2
-- (c) Kristian Klomsten Skordal 2014 - 2015 <kristian.skordal@wafflemail.net>
3 3 skordal
-- Report bugs and issues on <http://opencores.org/project,potato,bugtracker>
4 2 skordal
 
5
library ieee;
6
use ieee.std_logic_1164.all;
7
use ieee.numeric_std.all;
8
 
9
use work.pp_types.all;
10
use work.pp_constants.all;
11
use work.pp_utilities.all;
12
use work.pp_csr.all;
13
 
14
--! @brief The Potato Processor is a simple processor core for use in FPGAs.
15
--! @details
16
--! It implements the RV32I (RISC-V base integer subset) ISA with additional
17
--! instructions for manipulation of control and status registers from the
18
--! currently unpublished supervisor extension.
19
entity pp_core is
20
        generic(
21
                PROCESSOR_ID           : std_logic_vector(31 downto 0) := x"00000000"; --! Processor ID.
22
                RESET_ADDRESS          : std_logic_vector(31 downto 0) := x"00000000"  --! Address of the first instruction to execute.
23
        );
24
        port(
25
                -- Control inputs:
26
                clk       : in std_logic; --! Processor clock
27
                reset     : in std_logic; --! Reset signal
28
 
29
                timer_clk : in std_logic; --! Clock used for the timer/counter
30
 
31
                -- Instruction memory interface:
32
                imem_address : out std_logic_vector(31 downto 0); --! Address of the next instruction
33
                imem_data_in : in  std_logic_vector(31 downto 0); --! Instruction input
34
                imem_req     : out std_logic;
35
                imem_ack     : in  std_logic;
36
 
37
                -- Data memory interface:
38
                dmem_address   : out std_logic_vector(31 downto 0); --! Data address
39
                dmem_data_in   : in  std_logic_vector(31 downto 0); --! Input from the data memory
40
                dmem_data_out  : out std_logic_vector(31 downto 0); --! Ouptut to the data memory
41
                dmem_data_size : out std_logic_vector( 1 downto 0);  --! Size of the data, 1 = 8 bits, 2 = 16 bits, 0 = 32 bits. 
42
                dmem_read_req  : out std_logic;                      --! Data memory read request
43
                dmem_read_ack  : in  std_logic;                      --! Data memory read acknowledge
44
                dmem_write_req : out std_logic;                      --! Data memory write request
45
                dmem_write_ack : in  std_logic;                      --! Data memory write acknowledge
46
 
47
                -- Tohost/fromhost interface:
48
                fromhost_data     : in  std_logic_vector(31 downto 0); --! Data from the host/simulator.
49
                fromhost_write_en : in  std_logic;                     --! Write enable signal from the host/simulator.
50
                tohost_data       : out std_logic_vector(31 downto 0); --! Data to the host/simulator.
51
                tohost_write_en   : out std_logic;                     --! Write enable signal to the host/simulator.
52
 
53
                -- External interrupt input:
54
                irq : in std_logic_vector(7 downto 0) --! IRQ inputs.
55
        );
56
end entity pp_core;
57
 
58
architecture behaviour of pp_core is
59
 
60
        ------- Flush signals -------
61
        signal flush_if, flush_id, flush_ex : std_logic;
62
 
63
        ------- Stall signals -------
64
        signal stall_if, stall_id, stall_ex, stall_mem : std_logic;
65
 
66
        -- Signals used to determine if an instruction should be counted
67
        -- by the instret counter:
68
        signal if_count_instruction, id_count_instruction  : std_logic;
69
        signal ex_count_instruction, mem_count_instruction : std_logic;
70
        signal wb_count_instruction : std_logic;
71
 
72
        -- CSR read port signals:
73
        signal csr_read_data      : std_logic_vector(31 downto 0);
74
        signal csr_read_writeable : boolean;
75
 
76
        -- Status register outputs:
77
        signal status : csr_status_register;
78
        signal evec   : std_logic_vector(31 downto 0);
79
 
80
        -- Load hazard detected in the execute stage:
81
        signal load_hazard_detected : std_logic;
82
 
83
        -- Branch targets:
84
        signal exception_target, branch_target : std_logic_vector(31 downto 0);
85
        signal branch_taken, exception_taken   : std_logic;
86
 
87
        -- Register file read ports:
88
        signal rs1_address_p, rs2_address_p : register_address;
89
        signal rs1_address, rs2_address     : register_address;
90
        signal rs1_data, rs2_data           : std_logic_vector(31 downto 0);
91
 
92
        -- Data memory signals:
93
        signal dmem_address_p   : std_logic_vector(31 downto 0);
94
        signal dmem_data_size_p : std_logic_vector(1 downto 0);
95
        signal dmem_data_out_p  : std_logic_vector(31 downto 0);
96
        signal dmem_read_req_p  : std_logic;
97
        signal dmem_write_req_p : std_logic;
98
 
99
        -- Fetch stage signals:
100
        signal if_instruction, if_pc : std_logic_vector(31 downto 0);
101
        signal if_instruction_ready  : std_logic;
102
 
103
        -- Decode stage signals:
104
        signal id_funct3          : std_logic_vector(2 downto 0);
105
        signal id_rd_address      : register_address;
106
        signal id_rd_write        : std_logic;
107
        signal id_rs1_address     : register_address;
108
        signal id_rs2_address     : register_address;
109
        signal id_csr_address     : csr_address;
110
        signal id_csr_write       : csr_write_mode;
111
        signal id_csr_use_immediate : std_logic;
112
        signal id_shamt           : std_logic_vector(4 downto 0);
113
        signal id_immediate       : std_logic_vector(31 downto 0);
114
        signal id_branch          : branch_type;
115
        signal id_alu_x_src, id_alu_y_src : alu_operand_source;
116
        signal id_alu_op          : alu_operation;
117
        signal id_mem_op          : memory_operation_type;
118
        signal id_mem_size        : memory_operation_size;
119
        signal id_pc              : std_logic_vector(31 downto 0);
120
        signal id_exception       : std_logic;
121
        signal id_exception_cause : csr_exception_cause;
122
 
123
        -- Execute stage signals:
124
        signal ex_dmem_address   : std_logic_vector(31 downto 0);
125
        signal ex_dmem_data_size : std_logic_vector(1 downto 0);
126
        signal ex_dmem_data_out  : std_logic_vector(31 downto 0);
127
        signal ex_dmem_read_req  : std_logic;
128
        signal ex_dmem_write_req : std_logic;
129
        signal ex_rd_address     : register_address;
130
        signal ex_rd_data        : std_logic_vector(31 downto 0);
131
        signal ex_rd_write       : std_logic;
132
        signal ex_pc             : std_logic_vector(31 downto 0);
133
        signal ex_csr_address    : csr_address;
134
        signal ex_csr_write      : csr_write_mode;
135
        signal ex_csr_data       : std_logic_vector(31 downto 0);
136
        signal ex_branch         : branch_type;
137
        signal ex_mem_op         : memory_operation_type;
138
        signal ex_mem_size       : memory_operation_size;
139
        signal ex_exception_context : csr_exception_context;
140
 
141
        -- Memory stage signals:
142
        signal mem_rd_write    : std_logic;
143
        signal mem_rd_address  : register_address;
144
        signal mem_rd_data     : std_logic_vector(31 downto 0);
145
        signal mem_csr_address : csr_address;
146
        signal mem_csr_write   : csr_write_mode;
147
        signal mem_csr_data    : std_logic_vector(31 downto 0);
148
        signal mem_mem_op      : memory_operation_type;
149
 
150
        signal mem_exception         : std_logic;
151
        signal mem_exception_context : csr_exception_context;
152
 
153
        -- Writeback signals:
154
        signal wb_rd_address  : register_address;
155
        signal wb_rd_data     : std_logic_vector(31 downto 0);
156
        signal wb_rd_write    : std_logic;
157
        signal wb_csr_address : csr_address;
158
        signal wb_csr_write   : csr_write_mode;
159
        signal wb_csr_data    : std_logic_vector(31 downto 0);
160
 
161
        signal wb_exception         : std_logic;
162
        signal wb_exception_context : csr_exception_context;
163
 
164
begin
165
 
166
        stall_if <= stall_id;
167
        stall_id <= stall_ex;
168
        stall_ex <= load_hazard_detected or stall_mem;
169
        stall_mem <= to_std_logic(memop_is_load(mem_mem_op) and dmem_read_ack = '0')
170
                or to_std_logic(mem_mem_op = MEMOP_TYPE_STORE and dmem_write_ack = '0');
171
 
172 34 skordal
        flush_if <= (branch_taken or exception_taken) and not stall_if;
173
        flush_id <= (branch_taken or exception_taken) and not stall_id;
174
        flush_ex <= (branch_taken or exception_taken) and not stall_ex;
175 2 skordal
 
176
        ------- Control and status module -------
177
        csr_unit: entity work.pp_csr_unit
178
                        generic map(
179
                                PROCESSOR_ID => PROCESSOR_ID
180
                        ) port map(
181
                                clk => clk,
182
                                reset => reset,
183
                                timer_clk => timer_clk,
184
                                count_instruction => wb_count_instruction,
185
                                fromhost_data => fromhost_data,
186
                                fromhost_updated => fromhost_write_en,
187
                                tohost_data => tohost_data,
188
                                tohost_updated => tohost_write_en,
189
                                read_address => id_csr_address,
190
                                read_data_out => csr_read_data,
191
                                read_writeable => csr_read_writeable,
192
                                write_address => wb_csr_address,
193
                                write_data_in => wb_csr_data,
194
                                write_mode => wb_csr_write,
195
                                exception_context => wb_exception_context,
196
                                exception_context_write => wb_exception,
197
                                status_out => status,
198
                                evec_out => evec
199
                        );
200
 
201
        ------- Register file -------
202
        regfile: entity work.pp_register_file
203
                        port map(
204
                                clk => clk,
205
                                rs1_addr => rs1_address,
206
                                rs2_addr => rs2_address,
207
                                rs1_data => rs1_data,
208
                                rs2_data => rs2_data,
209
                                rd_addr => wb_rd_address,
210
                                rd_data => wb_rd_data,
211
                                rd_write => wb_rd_write
212
                        );
213
 
214
        rs1_address <= id_rs1_address when stall_ex = '0' else rs1_address_p;
215
        rs2_address <= id_rs2_address when stall_ex = '0' else rs2_address_p;
216
 
217
        store_previous_rsaddr: process(clk, stall_ex)
218
        begin
219
                if rising_edge(clk) and stall_ex = '0' then
220
                        rs1_address_p <= id_rs1_address;
221
                        rs2_address_p <= id_rs2_address;
222
                end if;
223
        end process store_previous_rsaddr;
224
 
225
        ------- Instruction Fetch (IF) Stage -------
226
        fetch: entity work.pp_fetch
227
                generic map(
228
                        RESET_ADDRESS => RESET_ADDRESS
229
                ) port map(
230
                        clk => clk,
231
                        reset => reset,
232
                        imem_address => imem_address,
233
                        imem_data_in => imem_data_in,
234
                        imem_req => imem_req,
235
                        imem_ack => imem_ack,
236
                        stall => stall_if,
237
                        flush => flush_if,
238
                        branch => branch_taken,
239
                        exception => exception_taken,
240
                        branch_target => branch_target,
241
                        evec => exception_target,
242
                        instruction_data => if_instruction,
243
                        instruction_address => if_pc,
244
                        instruction_ready => if_instruction_ready
245
                );
246
        if_count_instruction <= if_instruction_ready;
247
 
248
        ------- Instruction Decode (ID) Stage -------
249
        decode: entity work.pp_decode
250
                generic map(
251
                        RESET_ADDRESS => RESET_ADDRESS,
252
                        PROCESSOR_ID => PROCESSOR_ID
253
                ) port map(
254
                        clk => clk,
255
                        reset => reset,
256
                        flush => flush_id,
257
                        stall => stall_id,
258
                        instruction_data => if_instruction,
259
                        instruction_address => if_pc,
260
                        instruction_ready => if_instruction_ready,
261
                        instruction_count => if_count_instruction,
262
                        funct3 => id_funct3,
263
                        rs1_addr => id_rs1_address,
264
                        rs2_addr => id_rs2_address,
265
                        rd_addr => id_rd_address,
266
                        csr_addr => id_csr_address,
267
                        shamt => id_shamt,
268
                        immediate => id_immediate,
269
                        rd_write => id_rd_write,
270
                        branch => id_branch,
271
                        alu_x_src => id_alu_x_src,
272
                        alu_y_src => id_alu_y_src,
273
                        alu_op => id_alu_op,
274
                        mem_op => id_mem_op,
275
                        mem_size => id_mem_size,
276
                        count_instruction => id_count_instruction,
277
                        pc => id_pc,
278
                        csr_write => id_csr_write,
279
                        csr_use_imm => id_csr_use_immediate,
280
                        decode_exception => id_exception,
281
                        decode_exception_cause => id_exception_cause
282
                );
283
 
284
        ------- Execute (EX) Stage -------
285
        execute: entity work.pp_execute
286
                port map(
287
                        clk => clk,
288
                        reset => reset,
289
                        stall => stall_ex,
290
                        flush => flush_ex,
291
                        irq => irq,
292
                        dmem_address => ex_dmem_address,
293
                        dmem_data_size => ex_dmem_data_size,
294
                        dmem_data_out => ex_dmem_data_out,
295
                        dmem_read_req => ex_dmem_read_req,
296
                        dmem_write_req => ex_dmem_write_req,
297
                        rs1_addr_in => rs1_address,
298
                        rs2_addr_in => rs2_address,
299
                        rd_addr_in => id_rd_address,
300
                        rd_addr_out => ex_rd_address,
301
                        rs1_data_in => rs1_data,
302
                        rs2_data_in => rs2_data,
303
                        shamt_in => id_shamt,
304
                        immediate_in => id_immediate,
305
                        funct3_in => id_funct3,
306
                        pc_in => id_pc,
307
                        pc_out => ex_pc,
308
                        csr_addr_in => id_csr_address,
309
                        csr_addr_out => ex_csr_address,
310
                        csr_write_in => id_csr_write,
311
                        csr_write_out => ex_csr_write,
312
                        csr_value_in => csr_read_data,
313
                        csr_value_out => ex_csr_data,
314
                        csr_writeable_in => csr_read_writeable,
315
                        csr_use_immediate_in => id_csr_use_immediate,
316
                        alu_op_in => id_alu_op,
317
                        alu_x_src_in => id_alu_x_src,
318
                        alu_y_src_in => id_alu_y_src,
319
                        rd_write_in => id_rd_write,
320
                        rd_write_out => ex_rd_write,
321
                        rd_data_out => ex_rd_data,
322
                        branch_in => id_branch,
323
                        branch_out => ex_branch,
324
                        mem_op_in => id_mem_op,
325
                        mem_op_out => ex_mem_op,
326
                        mem_size_in => id_mem_size,
327
                        mem_size_out => ex_mem_size,
328
                        count_instruction_in => id_count_instruction,
329
                        count_instruction_out => ex_count_instruction,
330
                        status_in => status,
331
                        evec_in => evec,
332
                        evec_out => exception_target,
333
                        decode_exception_in => id_exception,
334
                        decode_exception_cause_in => id_exception_cause,
335
                        exception_out => exception_taken,
336
                        exception_context_out => ex_exception_context,
337
                        jump_out => branch_taken,
338
                        jump_target_out => branch_target,
339
                        mem_rd_write => mem_rd_write,
340
                        mem_rd_addr => mem_rd_address,
341
                        mem_rd_value => mem_rd_data,
342
                        mem_csr_addr => mem_csr_address,
343
                        mem_csr_value => mem_csr_data,
344
                        mem_csr_write => mem_csr_write,
345
                        mem_exception => mem_exception,
346
                        mem_exception_context => mem_exception_context,
347
                        wb_rd_write => wb_rd_write,
348
                        wb_rd_addr => wb_rd_address,
349
                        wb_rd_value => wb_rd_data,
350
                        wb_csr_addr => wb_csr_address,
351
                        wb_csr_value => wb_csr_data,
352
                        wb_csr_write => wb_csr_write,
353
                        wb_exception => wb_exception,
354
                        wb_exception_context => wb_exception_context,
355
                        mem_mem_op => mem_mem_op,
356
                        hazard_detected => load_hazard_detected
357
                );
358
 
359
        dmem_address <= ex_dmem_address when stall_mem = '0' else dmem_address_p;
360
        dmem_data_size <= ex_dmem_data_size when stall_mem = '0' else dmem_data_size_p;
361
        dmem_data_out <= ex_dmem_data_out when stall_mem = '0' else dmem_data_out_p;
362
        dmem_read_req <= ex_dmem_read_req when stall_mem = '0' else dmem_read_req_p;
363
        dmem_write_req <= ex_dmem_write_req when stall_mem = '0' else dmem_write_req_p;
364
 
365
        store_previous_dmem_address: process(clk, stall_mem)
366
        begin
367
                if rising_edge(clk) and stall_mem = '0' then
368
                        dmem_address_p <= ex_dmem_address;
369
                        dmem_data_size_p <= ex_dmem_data_size;
370
                        dmem_data_out_p <= ex_dmem_data_out;
371
                        dmem_read_req_p <= ex_dmem_read_req;
372
                        dmem_write_req_p <= ex_dmem_write_req;
373
                end if;
374
        end process store_previous_dmem_address;
375
 
376
        ------- Memory (MEM) Stage -------
377
        memory: entity work.pp_memory
378
                port map(
379
                        clk => clk,
380
                        reset => reset,
381
                        stall => stall_mem,
382
                        dmem_data_in => dmem_data_in,
383
                        dmem_read_ack => dmem_read_ack,
384
                        dmem_write_ack => dmem_write_ack,
385
                        pc => ex_pc,
386
                        rd_write_in => ex_rd_write,
387
                        rd_write_out => mem_rd_write,
388
                        rd_data_in => ex_rd_data,
389
                        rd_data_out => mem_rd_data,
390
                        rd_addr_in => ex_rd_address,
391
                        rd_addr_out => mem_rd_address,
392
                        branch => ex_branch,
393
                        mem_op_in => ex_mem_op,
394
                        mem_op_out => mem_mem_op,
395
                        mem_size_in => ex_mem_size,
396
                        count_instr_in => ex_count_instruction,
397
                        count_instr_out => mem_count_instruction,
398
                        exception_in => exception_taken,
399
                        exception_out => mem_exception,
400
                        exception_context_in => ex_exception_context,
401
                        exception_context_out => mem_exception_context,
402
                        csr_addr_in => ex_csr_address,
403
                        csr_addr_out => mem_csr_address,
404
                        csr_write_in => ex_csr_write,
405
                        csr_write_out => mem_csr_write,
406
                        csr_data_in => ex_csr_data,
407
                        csr_data_out => mem_csr_data
408
                );
409
 
410
        ------- Writeback (WB) Stage -------
411
        writeback: entity work.pp_writeback
412
                port map(
413
                        clk => clk,
414
                        reset => reset,
415
                        count_instr_in => mem_count_instruction,
416
                        count_instr_out => wb_count_instruction,
417
                        exception_ctx_in => mem_exception_context,
418
                        exception_ctx_out => wb_exception_context,
419
                        exception_in => mem_exception,
420
                        exception_out => wb_exception,
421
                        csr_write_in => mem_csr_write,
422
                        csr_write_out => wb_csr_write,
423
                        csr_data_in => mem_csr_data,
424
                        csr_data_out => wb_csr_data,
425
                        csr_addr_in => mem_csr_address,
426
                        csr_addr_out => wb_csr_address,
427
                        rd_addr_in => mem_rd_address,
428
                        rd_addr_out => wb_rd_address,
429
                        rd_write_in => mem_rd_write,
430
                        rd_write_out => wb_rd_write,
431
                        rd_data_in => mem_rd_data,
432
                        rd_data_out => wb_rd_data
433
                );
434
 
435
end architecture behaviour;
436
 

powered by: WebSVN 2.1.0

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