| 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 |
|
|
|
| 8 |
|
|
use work.pp_types.all;
|
| 9 |
|
|
use work.pp_constants.all;
|
| 10 |
|
|
use work.pp_csr.all;
|
| 11 |
|
|
|
| 12 |
|
|
--! @brief Unit decoding instructions and setting control signals apropriately.
|
| 13 |
|
|
entity pp_control_unit is
|
| 14 |
|
|
port(
|
| 15 |
|
|
-- Inputs, indices correspond to instruction word indices:
|
| 16 |
|
|
opcode : in std_logic_vector( 4 downto 0);
|
| 17 |
|
|
funct3 : in std_logic_vector( 2 downto 0);
|
| 18 |
|
|
funct7 : in std_logic_vector( 6 downto 0);
|
| 19 |
|
|
funct12 : in std_logic_vectoR(11 downto 0);
|
| 20 |
|
|
|
| 21 |
|
|
-- Control signals:
|
| 22 |
|
|
rd_write : out std_logic;
|
| 23 |
|
|
branch : out branch_type;
|
| 24 |
|
|
|
| 25 |
|
|
-- Exception signals:
|
| 26 |
|
|
decode_exception : out std_logic;
|
| 27 |
|
|
decode_exception_cause : out std_logic_vector(4 downto 0);
|
| 28 |
|
|
|
| 29 |
|
|
-- Control register signals:
|
| 30 |
|
|
csr_write : out csr_write_mode;
|
| 31 |
|
|
csr_imm : out std_logic; --! Indicating an immediate variant of the csrr* instructions.
|
| 32 |
|
|
|
| 33 |
|
|
-- Sources of operands to the ALU:
|
| 34 |
|
|
alu_x_src, alu_y_src : out alu_operand_source;
|
| 35 |
|
|
|
| 36 |
|
|
-- ALU operation:
|
| 37 |
|
|
alu_op : out alu_operation;
|
| 38 |
|
|
|
| 39 |
|
|
-- Memory transaction parameters:
|
| 40 |
|
|
mem_op : out memory_operation_type;
|
| 41 |
|
|
mem_size : out memory_operation_size
|
| 42 |
|
|
);
|
| 43 |
|
|
end entity pp_control_unit;
|
| 44 |
|
|
|
| 45 |
|
|
architecture behaviour of pp_control_unit is
|
| 46 |
|
|
begin
|
| 47 |
|
|
|
| 48 |
|
|
csr_imm <= funct3(2);
|
| 49 |
|
|
|
| 50 |
|
|
alu_control: entity work.pp_alu_control_unit
|
| 51 |
|
|
port map(
|
| 52 |
|
|
opcode => opcode,
|
| 53 |
|
|
funct3 => funct3,
|
| 54 |
|
|
funct7 => funct7,
|
| 55 |
|
|
alu_x_src => alu_x_src,
|
| 56 |
|
|
alu_y_src => alu_y_src,
|
| 57 |
|
|
alu_op => alu_op
|
| 58 |
|
|
);
|
| 59 |
|
|
|
| 60 |
|
|
decode_ctrl: process(opcode, funct3, funct12)
|
| 61 |
|
|
begin
|
| 62 |
|
|
case opcode is
|
| 63 |
|
|
when b"01101" => -- Load upper immediate
|
| 64 |
|
|
rd_write <= '1';
|
| 65 |
|
|
decode_exception <= '0';
|
| 66 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 67 |
|
|
branch <= BRANCH_NONE;
|
| 68 |
|
|
when b"00101" => -- Add upper immediate to PC
|
| 69 |
|
|
rd_write <= '1';
|
| 70 |
|
|
decode_exception <= '0';
|
| 71 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 72 |
|
|
branch <= BRANCH_NONE;
|
| 73 |
|
|
when b"11011" => -- Jump and link
|
| 74 |
|
|
rd_write <= '1';
|
| 75 |
|
|
decode_exception <= '0';
|
| 76 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 77 |
|
|
branch <= BRANCH_JUMP;
|
| 78 |
|
|
when b"11001" => -- Jump and link register
|
| 79 |
|
|
rd_write <= '1';
|
| 80 |
|
|
decode_exception <= '0';
|
| 81 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 82 |
|
|
branch <= BRANCH_JUMP_INDIRECT;
|
| 83 |
|
|
when b"11000" => -- Branch operations
|
| 84 |
|
|
rd_write <= '0';
|
| 85 |
|
|
decode_exception <= '0';
|
| 86 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 87 |
|
|
branch <= BRANCH_CONDITIONAL;
|
| 88 |
|
|
when b"00000" => -- Load instructions
|
| 89 |
|
|
rd_write <= '1';
|
| 90 |
|
|
decode_exception <= '0';
|
| 91 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 92 |
|
|
branch <= BRANCH_NONE;
|
| 93 |
|
|
when b"01000" => -- Store instructions
|
| 94 |
|
|
rd_write <= '0';
|
| 95 |
|
|
decode_exception <= '0';
|
| 96 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 97 |
|
|
branch <= BRANCH_NONE;
|
| 98 |
|
|
when b"00100" => -- Register-immediate operations
|
| 99 |
|
|
rd_write <= '1';
|
| 100 |
|
|
decode_exception <= '0';
|
| 101 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 102 |
|
|
branch <= BRANCH_NONE;
|
| 103 |
|
|
when b"01100" => -- Register-register operations
|
| 104 |
|
|
rd_write <= '1';
|
| 105 |
|
|
decode_exception <= '0';
|
| 106 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 107 |
|
|
branch <= BRANCH_NONE;
|
| 108 |
|
|
when b"00011" => -- Fence instructions, ignored
|
| 109 |
|
|
rd_write <= '0';
|
| 110 |
|
|
decode_exception <= '0';
|
| 111 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 112 |
|
|
branch <= BRANCH_NONE;
|
| 113 |
|
|
when b"11100" => -- System instructions
|
| 114 |
|
|
if funct3 = b"000" then
|
| 115 |
|
|
rd_write <= '0';
|
| 116 |
|
|
|
| 117 |
|
|
if funct12 = x"000" then
|
| 118 |
|
|
decode_exception <= '1';
|
| 119 |
|
|
decode_exception_cause <= CSR_CAUSE_SYSCALL;
|
| 120 |
|
|
branch <= BRANCH_NONE;
|
| 121 |
|
|
elsif funct12 = x"001" then
|
| 122 |
|
|
decode_exception <= '1';
|
| 123 |
|
|
decode_exception_cause <= CSR_CAUSE_BREAKPOINT;
|
| 124 |
|
|
branch <= BRANCH_NONE;
|
| 125 |
|
|
elsif funct12 = x"800" then
|
| 126 |
|
|
decode_exception <= '0';
|
| 127 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 128 |
|
|
branch <= BRANCH_SRET;
|
| 129 |
|
|
else
|
| 130 |
|
|
decode_exception <= '1';
|
| 131 |
|
|
decode_exception_cause <= CSR_CAUSE_INVALID_INSTR;
|
| 132 |
|
|
branch <= BRANCH_NONE;
|
| 133 |
|
|
end if;
|
| 134 |
|
|
else
|
| 135 |
|
|
rd_write <= '1';
|
| 136 |
|
|
decode_exception <= '0';
|
| 137 |
|
|
decode_exception_cause <= CSR_CAUSE_NONE;
|
| 138 |
|
|
branch <= BRANCH_NONE;
|
| 139 |
|
|
end if;
|
| 140 |
|
|
when others =>
|
| 141 |
|
|
rd_write <= '0';
|
| 142 |
|
|
decode_exception <= '1';
|
| 143 |
|
|
decode_exception_cause <= CSR_CAUSE_INVALID_INSTR;
|
| 144 |
|
|
branch <= BRANCH_NONE;
|
| 145 |
|
|
end case;
|
| 146 |
|
|
end process decode_ctrl;
|
| 147 |
|
|
|
| 148 |
|
|
decode_csr: process(opcode, funct3)
|
| 149 |
|
|
begin
|
| 150 |
|
|
if opcode = b"11100" then
|
| 151 |
|
|
case funct3 is
|
| 152 |
|
|
when b"001" | b"101" => -- csrrw/i
|
| 153 |
|
|
csr_write <= CSR_WRITE_REPLACE;
|
| 154 |
|
|
when b"010" | b"110" => -- csrrs/i
|
| 155 |
|
|
csr_write <= CSR_WRITE_SET;
|
| 156 |
|
|
when b"011" | b"111" => -- csrrc/i
|
| 157 |
|
|
csr_write <= CSR_WRITE_CLEAR;
|
| 158 |
|
|
when others =>
|
| 159 |
|
|
csr_write <= CSR_WRITE_NONE;
|
| 160 |
|
|
end case;
|
| 161 |
|
|
else
|
| 162 |
|
|
csr_write <= CSR_WRITE_NONE;
|
| 163 |
|
|
end if;
|
| 164 |
|
|
end process decode_csr;
|
| 165 |
|
|
|
| 166 |
|
|
decode_mem: process(opcode, funct3)
|
| 167 |
|
|
begin
|
| 168 |
|
|
case opcode is
|
| 169 |
|
|
when b"00000" => -- Load instructions
|
| 170 |
|
|
case funct3 is
|
| 171 |
|
|
when b"000" => -- lw
|
| 172 |
|
|
mem_size <= MEMOP_SIZE_BYTE;
|
| 173 |
|
|
mem_op <= MEMOP_TYPE_LOAD;
|
| 174 |
|
|
when b"001" => -- lh
|
| 175 |
|
|
mem_size <= MEMOP_SIZE_HALFWORD;
|
| 176 |
|
|
mem_op <= MEMOP_TYPE_LOAD;
|
| 177 |
|
|
when b"010" | b"110" => -- lw
|
| 178 |
|
|
mem_size <= MEMOP_SIZE_WORD;
|
| 179 |
|
|
mem_op <= MEMOP_TYPE_LOAD;
|
| 180 |
|
|
when b"100" => -- lbu
|
| 181 |
|
|
mem_size <= MEMOP_SIZE_BYTE;
|
| 182 |
|
|
mem_op <= MEMOP_TYPE_LOAD_UNSIGNED;
|
| 183 |
|
|
when b"101" => -- lhu
|
| 184 |
|
|
mem_size <= MEMOP_SIZE_HALFWORD;
|
| 185 |
|
|
mem_op <= MEMOP_TYPE_LOAD_UNSIGNED;
|
| 186 |
|
|
when others => -- FIXME: Treat others as lw.
|
| 187 |
|
|
mem_size <= MEMOP_SIZE_WORD;
|
| 188 |
|
|
mem_op <= MEMOP_TYPE_INVALID;
|
| 189 |
|
|
end case;
|
| 190 |
|
|
when b"01000" => -- Store instructions
|
| 191 |
|
|
case funct3 is
|
| 192 |
|
|
when b"000" =>
|
| 193 |
|
|
mem_op <= MEMOP_TYPE_STORE;
|
| 194 |
|
|
mem_size <= MEMOP_SIZE_BYTE;
|
| 195 |
|
|
when b"001" =>
|
| 196 |
|
|
mem_op <= MEMOP_TYPE_STORE;
|
| 197 |
|
|
mem_size <= MEMOP_SIZE_HALFWORD;
|
| 198 |
|
|
when b"010" =>
|
| 199 |
|
|
mem_op <= MEMOP_TYPE_STORE;
|
| 200 |
|
|
mem_size <= MEMOP_SIZE_WORD;
|
| 201 |
|
|
when others =>
|
| 202 |
|
|
mem_op <= MEMOP_TYPE_INVALID;
|
| 203 |
|
|
mem_size <= MEMOP_SIZE_WORD;
|
| 204 |
|
|
end case;
|
| 205 |
|
|
when others =>
|
| 206 |
|
|
mem_op <= MEMOP_TYPE_NONE;
|
| 207 |
|
|
mem_size <= MEMOP_SIZE_WORD;
|
| 208 |
|
|
end case;
|
| 209 |
|
|
end process decode_mem;
|
| 210 |
|
|
|
| 211 |
|
|
end architecture behaviour;
|