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_csr.all;
|
11 |
|
|
use work.pp_utilities.all;
|
12 |
|
|
|
13 |
|
|
entity pp_memory is
|
14 |
|
|
port(
|
15 |
|
|
clk : in std_logic;
|
16 |
|
|
reset : in std_logic;
|
17 |
|
|
stall : in std_logic;
|
18 |
|
|
|
19 |
|
|
-- Data memory inputs:
|
20 |
|
|
dmem_read_ack : in std_logic;
|
21 |
|
|
dmem_write_ack : in std_logic;
|
22 |
|
|
dmem_data_in : in std_logic_vector(31 downto 0);
|
23 |
|
|
|
24 |
|
|
-- Current PC value:
|
25 |
|
|
pc : in std_logic_vector(31 downto 0);
|
26 |
|
|
|
27 |
|
|
-- Destination register signals:
|
28 |
|
|
rd_write_in : in std_logic;
|
29 |
|
|
rd_write_out : out std_logic;
|
30 |
|
|
rd_data_in : in std_logic_vector(31 downto 0);
|
31 |
|
|
rd_data_out : out std_logic_vector(31 downto 0);
|
32 |
|
|
rd_addr_in : in register_address;
|
33 |
|
|
rd_addr_out : out register_address;
|
34 |
|
|
|
35 |
|
|
-- Control signals:
|
36 |
|
|
branch : in branch_type;
|
37 |
|
|
mem_op_in : in memory_operation_type;
|
38 |
|
|
mem_size_in : in memory_operation_size;
|
39 |
|
|
mem_op_out : out memory_operation_type;
|
40 |
|
|
|
41 |
|
|
-- Whether the instruction should be counted:
|
42 |
|
|
count_instr_in : in std_logic;
|
43 |
|
|
count_instr_out : out std_logic;
|
44 |
|
|
|
45 |
|
|
-- Exception signals:
|
46 |
|
|
exception_in : in std_logic;
|
47 |
|
|
exception_out : out std_logic;
|
48 |
|
|
exception_context_in : in csr_exception_context;
|
49 |
|
|
exception_context_out : out csr_exception_context;
|
50 |
|
|
|
51 |
|
|
-- CSR signals:
|
52 |
|
|
csr_addr_in : in csr_address;
|
53 |
|
|
csr_addr_out : out csr_address;
|
54 |
|
|
csr_write_in : in csr_write_mode;
|
55 |
|
|
csr_write_out : out csr_write_mode;
|
56 |
|
|
csr_data_in : in std_logic_vector(31 downto 0);
|
57 |
|
|
csr_data_out : out std_logic_vector(31 downto 0)
|
58 |
|
|
);
|
59 |
|
|
end entity pp_memory;
|
60 |
|
|
|
61 |
|
|
architecture behaviour of pp_memory is
|
62 |
|
|
signal mem_op : memory_operation_type;
|
63 |
|
|
signal mem_size : memory_operation_size;
|
64 |
|
|
|
65 |
|
|
signal rd_data : std_logic_vector(31 downto 0);
|
66 |
|
|
begin
|
67 |
|
|
|
68 |
|
|
mem_op_out <= mem_op;
|
69 |
|
|
|
70 |
|
|
pipeline_register: process(clk)
|
71 |
|
|
begin
|
72 |
|
|
if rising_edge(clk) then
|
73 |
|
|
if reset = '1' then
|
74 |
|
|
rd_write_out <= '0';
|
75 |
|
|
csr_write_out <= CSR_WRITE_NONE;
|
76 |
|
|
count_instr_out <= '0';
|
77 |
|
|
mem_op <= MEMOP_TYPE_NONE;
|
78 |
|
|
elsif stall = '0' then
|
79 |
|
|
mem_size <= mem_size_in;
|
80 |
|
|
rd_data <= rd_data_in;
|
81 |
|
|
rd_addr_out <= rd_addr_in;
|
82 |
|
|
|
83 |
|
|
if exception_in = '1' then
|
84 |
|
|
mem_op <= MEMOP_TYPE_NONE;
|
85 |
|
|
rd_write_out <= '0';
|
86 |
|
|
csr_write_out <= CSR_WRITE_REPLACE;
|
87 |
|
|
csr_addr_out <= CSR_EPC;
|
88 |
|
|
csr_data_out <= pc;
|
89 |
|
|
count_instr_out <= '0';
|
90 |
|
|
else
|
91 |
|
|
mem_op <= mem_op_in;
|
92 |
|
|
rd_write_out <= rd_write_in;
|
93 |
|
|
csr_write_out <= csr_write_in;
|
94 |
|
|
csr_addr_out <= csr_addr_in;
|
95 |
|
|
csr_data_out <= csr_data_in;
|
96 |
|
|
count_instr_out <= count_instr_in;
|
97 |
|
|
end if;
|
98 |
|
|
end if;
|
99 |
|
|
end if;
|
100 |
|
|
end process pipeline_register;
|
101 |
|
|
|
102 |
|
|
update_exception_context: process(clk)
|
103 |
|
|
begin
|
104 |
|
|
if rising_edge(clk) then
|
105 |
|
|
if reset = '1' then
|
106 |
|
|
exception_out <= '0';
|
107 |
|
|
else
|
108 |
|
|
exception_out <= exception_in or to_std_logic(branch = BRANCH_SRET);
|
109 |
|
|
|
110 |
|
|
if exception_in = '1' then
|
111 |
|
|
exception_context_out.status <= (
|
112 |
|
|
pim => exception_context_in.status.im,
|
113 |
|
|
im => exception_context_in.status.im,
|
114 |
|
|
pei => exception_context_in.status.ei,
|
115 |
|
|
ei => '0'
|
116 |
|
|
);
|
117 |
|
|
exception_context_out.cause <= exception_context_in.cause;
|
118 |
|
|
exception_context_out.badvaddr <= exception_context_in.badvaddr;
|
119 |
|
|
elsif branch = BRANCH_SRET then
|
120 |
|
|
exception_context_out.status <= (
|
121 |
|
|
pim => exception_context_in.status.pim,
|
122 |
|
|
im => exception_context_in.status.pim,
|
123 |
|
|
pei => exception_context_in.status.pei,
|
124 |
|
|
ei => exception_context_in.status.pei
|
125 |
|
|
);
|
126 |
|
|
exception_context_out.cause <= CSR_CAUSE_NONE;
|
127 |
|
|
exception_context_out.badvaddr <= (others => '0');
|
128 |
|
|
else
|
129 |
|
|
exception_context_out.status <= exception_context_in.status;
|
130 |
|
|
exception_context_out.cause <= CSR_CAUSE_NONE;
|
131 |
|
|
exception_context_out.badvaddr <= (others => '0');
|
132 |
|
|
end if;
|
133 |
|
|
end if;
|
134 |
|
|
end if;
|
135 |
|
|
end process update_exception_context;
|
136 |
|
|
|
137 |
|
|
rd_data_mux: process(rd_data, dmem_data_in, mem_op, mem_size)
|
138 |
|
|
begin
|
139 |
|
|
if mem_op = MEMOP_TYPE_LOAD or mem_op = MEMOP_TYPE_LOAD_UNSIGNED then
|
140 |
|
|
case mem_size is
|
141 |
|
|
when MEMOP_SIZE_BYTE =>
|
142 |
|
|
if mem_op = MEMOP_TYPE_LOAD_UNSIGNED then
|
143 |
|
|
rd_data_out <= std_logic_vector(resize(unsigned(dmem_data_in(7 downto 0)), rd_data_out'length));
|
144 |
|
|
else
|
145 |
|
|
rd_data_out <= std_logic_vector(resize(signed(dmem_data_in(7 downto 0)), rd_data_out'length));
|
146 |
|
|
end if;
|
147 |
|
|
when MEMOP_SIZE_HALFWORD =>
|
148 |
|
|
if mem_op = MEMOP_TYPE_LOAD_UNSIGNED then
|
149 |
|
|
rd_data_out <= std_logic_vector(resize(unsigned(dmem_data_in(15 downto 0)), rd_data_out'length));
|
150 |
|
|
else
|
151 |
|
|
rd_data_out <= std_logic_vector(resize(signed(dmem_data_in(15 downto 0)), rd_data_out'length));
|
152 |
|
|
end if;
|
153 |
|
|
when MEMOP_SIZE_WORD =>
|
154 |
|
|
rd_data_out <= dmem_data_in;
|
155 |
|
|
end case;
|
156 |
|
|
else
|
157 |
|
|
rd_data_out <= rd_data;
|
158 |
|
|
end if;
|
159 |
|
|
end process rd_data_mux;
|
160 |
|
|
|
161 |
|
|
end architecture behaviour;
|