1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - CPU Compressed Instructions (C-extension) Decoder >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
|
|
-- # BSD 3-Clause License #
|
5 |
|
|
-- # #
|
6 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
7 |
|
|
-- # #
|
8 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
9 |
|
|
-- # permitted provided that the following conditions are met: #
|
10 |
|
|
-- # #
|
11 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
12 |
|
|
-- # conditions and the following disclaimer. #
|
13 |
|
|
-- # #
|
14 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
15 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
16 |
|
|
-- # provided with the distribution. #
|
17 |
|
|
-- # #
|
18 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
19 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
20 |
|
|
-- # permission. #
|
21 |
|
|
-- # #
|
22 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
23 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
24 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
25 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
26 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
27 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
28 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
29 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
30 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
31 |
|
|
-- # ********************************************************************************************* #
|
32 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
33 |
|
|
-- #################################################################################################
|
34 |
|
|
|
35 |
|
|
library ieee;
|
36 |
|
|
use ieee.std_logic_1164.all;
|
37 |
|
|
use ieee.numeric_std.all;
|
38 |
|
|
|
39 |
|
|
library neorv32;
|
40 |
|
|
use neorv32.neorv32_package.all;
|
41 |
|
|
|
42 |
|
|
entity neorv32_cpu_decompressor is
|
43 |
|
|
port (
|
44 |
|
|
-- instruction input --
|
45 |
|
|
ci_instr16_i : in std_ulogic_vector(15 downto 0); -- compressed instruction input
|
46 |
|
|
-- instruction output --
|
47 |
|
|
ci_valid_o : out std_ulogic; -- is a compressed instruction
|
48 |
|
|
ci_illegal_o : out std_ulogic; -- is an illegal compressed instruction
|
49 |
|
|
ci_instr32_o : out std_ulogic_vector(31 downto 0) -- 32-bit decompressed instruction
|
50 |
|
|
);
|
51 |
|
|
end neorv32_cpu_decompressor;
|
52 |
|
|
|
53 |
|
|
architecture neorv32_cpu_decompressor_rtl of neorv32_cpu_decompressor is
|
54 |
|
|
|
55 |
|
|
-- compressed instruction layout --
|
56 |
|
|
constant ci_opcode_lsb_c : natural := 0;
|
57 |
|
|
constant ci_opcode_msb_c : natural := 1;
|
58 |
|
|
constant ci_rd_3_lsb_c : natural := 2;
|
59 |
|
|
constant ci_rd_3_msb_c : natural := 4;
|
60 |
|
|
constant ci_rd_5_lsb_c : natural := 7;
|
61 |
|
|
constant ci_rd_5_msb_c : natural := 11;
|
62 |
|
|
constant ci_rs1_3_lsb_c : natural := 7;
|
63 |
|
|
constant ci_rs1_3_msb_c : natural := 9;
|
64 |
|
|
constant ci_rs1_5_lsb_c : natural := 7;
|
65 |
|
|
constant ci_rs1_5_msb_c : natural := 11;
|
66 |
|
|
constant ci_rs2_3_lsb_c : natural := 2;
|
67 |
|
|
constant ci_rs2_3_msb_c : natural := 4;
|
68 |
|
|
constant ci_rs2_5_lsb_c : natural := 2;
|
69 |
|
|
constant ci_rs2_5_msb_c : natural := 6;
|
70 |
|
|
constant ci_funct3_lsb_c : natural := 13;
|
71 |
|
|
constant ci_funct3_msb_c : natural := 15;
|
72 |
|
|
|
73 |
|
|
begin
|
74 |
|
|
|
75 |
|
|
-- Compressed Instruction Decoder ---------------------------------------------------------
|
76 |
|
|
-- -------------------------------------------------------------------------------------------
|
77 |
|
|
decompressor: process(ci_instr16_i)
|
78 |
|
|
variable imm20_v : std_ulogic_vector(20 downto 0);
|
79 |
|
|
variable imm12_v : std_ulogic_vector(12 downto 0);
|
80 |
|
|
begin
|
81 |
|
|
-- defaults --
|
82 |
|
|
ci_illegal_o <= '0';
|
83 |
|
|
ci_instr32_o <= (others => '0');
|
84 |
|
|
|
85 |
|
|
-- 22-bit sign-extended immediate for J/JAL --
|
86 |
|
|
imm20_v := (others => ci_instr16_i(12)); -- sign extension
|
87 |
|
|
imm20_v(00):= '0';
|
88 |
|
|
imm20_v(01):= ci_instr16_i(3);
|
89 |
|
|
imm20_v(02):= ci_instr16_i(4);
|
90 |
|
|
imm20_v(03):= ci_instr16_i(5);
|
91 |
|
|
imm20_v(04):= ci_instr16_i(11);
|
92 |
|
|
imm20_v(05):= ci_instr16_i(2);
|
93 |
|
|
imm20_v(06):= ci_instr16_i(7);
|
94 |
|
|
imm20_v(07):= ci_instr16_i(6);
|
95 |
|
|
imm20_v(08):= ci_instr16_i(9);
|
96 |
|
|
imm20_v(09):= ci_instr16_i(10);
|
97 |
|
|
imm20_v(10):= ci_instr16_i(8);
|
98 |
|
|
imm20_v(11):= ci_instr16_i(12);
|
99 |
|
|
|
100 |
|
|
-- 12-bit sign-extended immediate for branches --
|
101 |
|
|
imm12_v := (others => ci_instr16_i(12)); -- sign extension
|
102 |
|
|
imm12_v(00):= '0';
|
103 |
|
|
imm12_v(01):= ci_instr16_i(3);
|
104 |
|
|
imm12_v(02):= ci_instr16_i(4);
|
105 |
|
|
imm12_v(03):= ci_instr16_i(10);
|
106 |
|
|
imm12_v(04):= ci_instr16_i(11);
|
107 |
|
|
imm12_v(05):= ci_instr16_i(2);
|
108 |
|
|
imm12_v(06):= ci_instr16_i(5);
|
109 |
|
|
imm12_v(07):= ci_instr16_i(6);
|
110 |
|
|
imm12_v(08):= ci_instr16_i(12);
|
111 |
|
|
|
112 |
|
|
-- actual decoder --
|
113 |
|
|
case ci_instr16_i(ci_opcode_msb_c downto ci_opcode_lsb_c) is
|
114 |
|
|
|
115 |
|
|
when "00" => -- C0: Register-Based Loads and Stores
|
116 |
|
|
case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
|
117 |
|
|
|
118 |
|
|
when "000" => -- Illegal_instruction, C.ADDI4SPN
|
119 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
120 |
|
|
if (ci_instr16_i(12 downto 2) = "00000000000") then -- "official" illegal instruction
|
121 |
|
|
ci_illegal_o <= '1';
|
122 |
|
|
|
123 |
|
|
else -- C.ADDI4SPN
|
124 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
|
125 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "00010"; -- stack pointer
|
126 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "01" & ci_instr16_i(ci_rd_3_msb_c downto ci_rd_3_lsb_c);
|
127 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
|
128 |
|
|
ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c) <= (others => '0'); -- zero extend
|
129 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 0) <= '0';
|
130 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 1) <= '0';
|
131 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 2) <= ci_instr16_i(6);
|
132 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 3) <= ci_instr16_i(5);
|
133 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 4) <= ci_instr16_i(11);
|
134 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 5) <= ci_instr16_i(12);
|
135 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 6) <= ci_instr16_i(7);
|
136 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 7) <= ci_instr16_i(8);
|
137 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 8) <= ci_instr16_i(9);
|
138 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 9) <= ci_instr16_i(10);
|
139 |
|
|
end if;
|
140 |
|
|
|
141 |
|
|
when "010" => -- C.LW
|
142 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
143 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_load_c;
|
144 |
|
|
ci_instr32_o(21 downto 20) <= "00";
|
145 |
|
|
ci_instr32_o(22) <= ci_instr16_i(6);
|
146 |
|
|
ci_instr32_o(23) <= ci_instr16_i(10);
|
147 |
|
|
ci_instr32_o(24) <= ci_instr16_i(11);
|
148 |
|
|
ci_instr32_o(25) <= ci_instr16_i(12);
|
149 |
|
|
ci_instr32_o(26) <= ci_instr16_i(5);
|
150 |
|
|
ci_instr32_o(31 downto 26) <= (others => '0');
|
151 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_lw_c;
|
152 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "01" & ci_instr16_i(ci_rs1_3_msb_c downto ci_rs1_3_lsb_c); -- x8 - x15
|
153 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "01" & ci_instr16_i(ci_rd_3_msb_c downto ci_rd_3_lsb_c); -- x8 - x15
|
154 |
|
|
|
155 |
|
|
when "110" => -- C.SW
|
156 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
157 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_store_c;
|
158 |
|
|
ci_instr32_o(08 downto 07) <= "00";
|
159 |
|
|
ci_instr32_o(09) <= ci_instr16_i(6);
|
160 |
|
|
ci_instr32_o(10) <= ci_instr16_i(10);
|
161 |
|
|
ci_instr32_o(11) <= ci_instr16_i(11);
|
162 |
|
|
ci_instr32_o(25) <= ci_instr16_i(12);
|
163 |
|
|
ci_instr32_o(26) <= ci_instr16_i(5);
|
164 |
|
|
ci_instr32_o(31 downto 27) <= (others => '0');
|
165 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sw_c;
|
166 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "01" & ci_instr16_i(ci_rs1_3_msb_c downto ci_rs1_3_lsb_c); -- x8 - x15
|
167 |
|
|
ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c) <= "01" & ci_instr16_i(ci_rs2_3_msb_c downto ci_rs2_3_lsb_c); -- x8 - x15
|
168 |
|
|
|
169 |
|
|
when others => -- undefined
|
170 |
|
|
ci_illegal_o <= '1';
|
171 |
|
|
end case;
|
172 |
|
|
|
173 |
|
|
when "01" => -- C1: Control Transfer Instructions, Integer Constant-Generation Instructions
|
174 |
|
|
|
175 |
|
|
case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
|
176 |
|
|
when "101" => -- C.J
|
177 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
178 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jal_c;
|
179 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "00000"; -- discard return address
|
180 |
|
|
ci_instr32_o(19 downto 12) <= imm20_v(19 downto 12);
|
181 |
|
|
ci_instr32_o(20) <= imm20_v(11);
|
182 |
|
|
ci_instr32_o(30 downto 21) <= imm20_v(10 downto 01);
|
183 |
|
|
ci_instr32_o(31) <= imm20_v(20);
|
184 |
|
|
|
185 |
|
|
when "001" => -- C.JAL
|
186 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
187 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jal_c;
|
188 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "00001"; -- save return address to link register
|
189 |
|
|
ci_instr32_o(19 downto 12) <= imm20_v(19 downto 12);
|
190 |
|
|
ci_instr32_o(20) <= imm20_v(11);
|
191 |
|
|
ci_instr32_o(30 downto 21) <= imm20_v(10 downto 01);
|
192 |
|
|
ci_instr32_o(31) <= imm20_v(20);
|
193 |
|
|
|
194 |
|
|
when "110" => -- C.BEQ
|
195 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
196 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_branch_c;
|
197 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_beq_c;
|
198 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "01" & ci_instr16_i(ci_rs1_3_msb_c downto ci_rs1_3_lsb_c);
|
199 |
|
|
ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c) <= "00000"; -- x0
|
200 |
|
|
ci_instr32_o(07) <= imm12_v(11);
|
201 |
|
|
ci_instr32_o(11 downto 08) <= imm12_v(04 downto 01);
|
202 |
|
|
ci_instr32_o(30 downto 25) <= imm12_v(10 downto 05);
|
203 |
|
|
ci_instr32_o(31) <= imm12_v(12);
|
204 |
|
|
|
205 |
|
|
when "111" => -- C.BNEZ
|
206 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
207 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_branch_c;
|
208 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_bne_c;
|
209 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "01" & ci_instr16_i(ci_rs1_3_msb_c downto ci_rs1_3_lsb_c);
|
210 |
|
|
ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c) <= "00000"; -- x0
|
211 |
|
|
ci_instr32_o(07) <= imm12_v(11);
|
212 |
|
|
ci_instr32_o(11 downto 08) <= imm12_v(04 downto 01);
|
213 |
|
|
ci_instr32_o(30 downto 25) <= imm12_v(10 downto 05);
|
214 |
|
|
ci_instr32_o(31) <= imm12_v(12);
|
215 |
|
|
|
216 |
|
|
when "010" => -- C.LI
|
217 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
218 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
|
219 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
|
220 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "00000"; -- x0
|
221 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
222 |
|
|
ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c) <= (others => ci_instr16_i(12)); -- sign extend
|
223 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 0) <= ci_instr16_i(2);
|
224 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 1) <= ci_instr16_i(3);
|
225 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 2) <= ci_instr16_i(4);
|
226 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 3) <= ci_instr16_i(5);
|
227 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 4) <= ci_instr16_i(6);
|
228 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 5) <= ci_instr16_i(12);
|
229 |
|
|
if (ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c) = "00000") then -- HINT
|
230 |
|
|
ci_illegal_o <= '1';
|
231 |
|
|
end if;
|
232 |
|
|
|
233 |
|
|
when "011" => -- C.LUI / C.ADDI16SP
|
234 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
235 |
|
|
if (ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c) = "00010") then -- C.ADDI16SP
|
236 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
|
237 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
|
238 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
239 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "00010"; -- stack pointer
|
240 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "00010"; -- stack pointer
|
241 |
|
|
ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c) <= (others => ci_instr16_i(12)); -- sign extend
|
242 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 00) <= '0';
|
243 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 01) <= '0';
|
244 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 02) <= '0';
|
245 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 03) <= '0';
|
246 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 04) <= ci_instr16_i(6);
|
247 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 05) <= ci_instr16_i(2);
|
248 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 06) <= ci_instr16_i(5);
|
249 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 07) <= ci_instr16_i(3);
|
250 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 08) <= ci_instr16_i(4);
|
251 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 09) <= ci_instr16_i(12);
|
252 |
|
|
|
253 |
|
|
else -- C.LUI
|
254 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_lui_c;
|
255 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
256 |
|
|
ci_instr32_o(instr_imm20_msb_c downto instr_imm20_lsb_c) <= (others => ci_instr16_i(12)); -- sign extend
|
257 |
|
|
ci_instr32_o(instr_imm20_lsb_c + 0) <= ci_instr16_i(2);
|
258 |
|
|
ci_instr32_o(instr_imm20_lsb_c + 1) <= ci_instr16_i(3);
|
259 |
|
|
ci_instr32_o(instr_imm20_lsb_c + 2) <= ci_instr16_i(4);
|
260 |
|
|
ci_instr32_o(instr_imm20_lsb_c + 3) <= ci_instr16_i(5);
|
261 |
|
|
ci_instr32_o(instr_imm20_lsb_c + 4) <= ci_instr16_i(6);
|
262 |
|
|
ci_instr32_o(instr_imm20_lsb_c + 5) <= ci_instr16_i(12);
|
263 |
|
|
end if;
|
264 |
|
|
if (ci_instr16_i(6 downto 2) = "00000") and (ci_instr16_i(12) = '0') then -- reserved
|
265 |
|
|
ci_illegal_o <= '1';
|
266 |
|
|
end if;
|
267 |
|
|
|
268 |
|
|
when "000" => -- C.NOP (rd=0) / C.ADDI
|
269 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
270 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
|
271 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
|
272 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= ci_instr16_i(ci_rs1_5_msb_c downto ci_rs1_5_lsb_c);
|
273 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
274 |
|
|
ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c) <= (others => ci_instr16_i(12)); -- sign extend
|
275 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 0) <= ci_instr16_i(2);
|
276 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 1) <= ci_instr16_i(3);
|
277 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 2) <= ci_instr16_i(4);
|
278 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 3) <= ci_instr16_i(5);
|
279 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 4) <= ci_instr16_i(6);
|
280 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 5) <= ci_instr16_i(12);
|
281 |
|
|
|
282 |
|
|
when "100" => -- C.SRLI, C.SRAI, C.ANDI, C.SUB, C.XOR, C.OR, C.AND, reserved
|
283 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
284 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "01" & ci_instr16_i(ci_rs1_3_msb_c downto ci_rs1_3_lsb_c);
|
285 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "01" & ci_instr16_i(ci_rs1_3_msb_c downto ci_rs1_3_lsb_c);
|
286 |
|
|
if (ci_instr16_i(11 downto 10) = "11") then -- register-register operation
|
287 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
|
288 |
|
|
ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c) <= "01" & ci_instr16_i(ci_rs2_3_msb_c downto ci_rs2_3_lsb_c);
|
289 |
|
|
case ci_instr16_i(6 downto 5) is
|
290 |
|
|
when "00" => -- C.SUB
|
291 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
|
292 |
|
|
ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0100000";
|
293 |
|
|
when "01" => -- C.XOR
|
294 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_xor_c;
|
295 |
|
|
ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
|
296 |
|
|
when "10" => -- C.OR
|
297 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_or_c;
|
298 |
|
|
ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
|
299 |
|
|
when others => -- C.AND
|
300 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_and_c;
|
301 |
|
|
ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
|
302 |
|
|
end case;
|
303 |
|
|
else -- register-immediate operation
|
304 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
|
305 |
|
|
case ci_instr16_i(11 downto 10) is
|
306 |
|
|
when "00" => -- C.SRLI
|
307 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sr_c;
|
308 |
|
|
ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
|
309 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 0) <= ci_instr16_i(2);
|
310 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 1) <= ci_instr16_i(3);
|
311 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 2) <= ci_instr16_i(4);
|
312 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 3) <= ci_instr16_i(5);
|
313 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 4) <= ci_instr16_i(6);
|
314 |
|
|
ci_illegal_o <= ci_instr16_i(12);
|
315 |
|
|
when "01" => -- C.SRAI
|
316 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sr_c;
|
317 |
|
|
ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0100000";
|
318 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 0) <= ci_instr16_i(2);
|
319 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 1) <= ci_instr16_i(3);
|
320 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 2) <= ci_instr16_i(4);
|
321 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 3) <= ci_instr16_i(5);
|
322 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 4) <= ci_instr16_i(6);
|
323 |
|
|
ci_illegal_o <= ci_instr16_i(12);
|
324 |
|
|
when "10" => -- C.ANDI
|
325 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_and_c;
|
326 |
|
|
ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c) <= (others => ci_instr16_i(12)); -- sign extend
|
327 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 0) <= ci_instr16_i(2);
|
328 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 1) <= ci_instr16_i(3);
|
329 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 2) <= ci_instr16_i(4);
|
330 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 3) <= ci_instr16_i(5);
|
331 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 4) <= ci_instr16_i(6);
|
332 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 5) <= ci_instr16_i(12);
|
333 |
|
|
when others => -- register-register operation
|
334 |
|
|
NULL;
|
335 |
|
|
end case;
|
336 |
|
|
end if;
|
337 |
|
|
if (ci_instr16_i(12 downto 10) = "111") then -- reserved / undefined
|
338 |
|
|
ci_illegal_o <= '1';
|
339 |
|
|
end if;
|
340 |
|
|
|
341 |
|
|
when others => -- undefined
|
342 |
|
|
ci_illegal_o <= '1';
|
343 |
|
|
end case;
|
344 |
|
|
|
345 |
|
|
when "10" => -- C2: Stack-Pointer-Based Loads and Stores, Control Transfer Instructions
|
346 |
|
|
case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
|
347 |
|
|
|
348 |
|
|
when "000" => -- C.SLLI
|
349 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
350 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
|
351 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= ci_instr16_i(ci_rs1_5_msb_c downto ci_rs1_5_lsb_c);
|
352 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rs1_5_msb_c downto ci_rs1_5_lsb_c);
|
353 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sll_c;
|
354 |
|
|
ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
|
355 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 0) <= ci_instr16_i(2);
|
356 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 1) <= ci_instr16_i(3);
|
357 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 2) <= ci_instr16_i(4);
|
358 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 3) <= ci_instr16_i(5);
|
359 |
|
|
ci_instr32_o(instr_imm12_lsb_c + 4) <= ci_instr16_i(6);
|
360 |
|
|
ci_illegal_o <= ci_instr16_i(12);
|
361 |
|
|
|
362 |
|
|
when "010" => -- C.LWSP
|
363 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
364 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_load_c;
|
365 |
|
|
ci_instr32_o(21 downto 20) <= "00";
|
366 |
|
|
ci_instr32_o(22) <= ci_instr16_i(4);
|
367 |
|
|
ci_instr32_o(23) <= ci_instr16_i(5);
|
368 |
|
|
ci_instr32_o(24) <= ci_instr16_i(6);
|
369 |
|
|
ci_instr32_o(25) <= ci_instr16_i(12);
|
370 |
|
|
ci_instr32_o(26) <= ci_instr16_i(2);
|
371 |
|
|
ci_instr32_o(27) <= ci_instr16_i(3);
|
372 |
|
|
ci_instr32_o(31 downto 28) <= (others => '0');
|
373 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_lw_c;
|
374 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "00010"; -- stack pointer
|
375 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
376 |
|
|
|
377 |
|
|
when "110" => -- C.SWSP
|
378 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
379 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_store_c;
|
380 |
|
|
ci_instr32_o(08 downto 07) <= "00";
|
381 |
|
|
ci_instr32_o(09) <= ci_instr16_i(9);
|
382 |
|
|
ci_instr32_o(10) <= ci_instr16_i(10);
|
383 |
|
|
ci_instr32_o(11) <= ci_instr16_i(11);
|
384 |
|
|
ci_instr32_o(25) <= ci_instr16_i(12);
|
385 |
|
|
ci_instr32_o(26) <= ci_instr16_i(7);
|
386 |
|
|
ci_instr32_o(27) <= ci_instr16_i(8);
|
387 |
|
|
ci_instr32_o(31 downto 28) <= (others => '0');
|
388 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sw_c;
|
389 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "00010"; -- stack pointer
|
390 |
|
|
ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c) <= ci_instr16_i(ci_rs2_5_msb_c downto ci_rs2_5_lsb_c);
|
391 |
|
|
|
392 |
|
|
when "100" => -- C.JR, C.JALR, C.MV, C.EBREAK, C.ADD
|
393 |
|
|
-- ----------------------------------------------------------------------------------------------------------
|
394 |
|
|
if (ci_instr16_i(12) = '0') then -- C.JR, C.MV
|
395 |
|
|
if (ci_instr16_i(6 downto 2) = "00000") then -- C.JR
|
396 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jalr_c;
|
397 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= ci_instr16_i(ci_rs1_5_msb_c downto ci_rs1_5_lsb_c);
|
398 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "00000"; -- discard return address
|
399 |
|
|
else -- C.MV
|
400 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
|
401 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= "000";
|
402 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
403 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= "00000"; -- x0
|
404 |
|
|
ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c) <= ci_instr16_i(ci_rs2_5_msb_c downto ci_rs2_5_lsb_c);
|
405 |
|
|
end if;
|
406 |
|
|
else -- C.EBREAK, C.JALR, C.ADD
|
407 |
|
|
if (ci_instr16_i(6 downto 2) = "00000") then -- C.EBREAK, C.JALR
|
408 |
|
|
if (ci_instr16_i(11 downto 7) = "00000") then -- C.EBREAK
|
409 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_syscsr_c;
|
410 |
|
|
ci_instr32_o(instr_funct12_msb_c downto instr_funct12_lsb_c) <= "000000000001";
|
411 |
|
|
else -- C.JALR
|
412 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jalr_c;
|
413 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= ci_instr16_i(ci_rs1_5_msb_c downto ci_rs1_5_lsb_c);
|
414 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= "00001"; -- save return address to link register
|
415 |
|
|
end if;
|
416 |
|
|
else -- C.ADD
|
417 |
|
|
ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
|
418 |
|
|
ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= "000";
|
419 |
|
|
ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
420 |
|
|
ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c) <= ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c);
|
421 |
|
|
ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c) <= ci_instr16_i(ci_rs2_5_msb_c downto ci_rs2_5_lsb_c);
|
422 |
|
|
end if;
|
423 |
|
|
end if;
|
424 |
|
|
|
425 |
|
|
when others => -- undefined
|
426 |
|
|
ci_illegal_o <= '1';
|
427 |
|
|
end case;
|
428 |
|
|
|
429 |
|
|
when others => -- not a compressed instruction
|
430 |
|
|
NULL;
|
431 |
|
|
|
432 |
|
|
end case;
|
433 |
|
|
end process decompressor;
|
434 |
|
|
|
435 |
|
|
-- is compressed instruction at all? --
|
436 |
|
|
ci_valid_o <= '0' when (ci_instr16_i(ci_opcode_msb_c downto ci_opcode_lsb_c) = "11") else '1';
|
437 |
|
|
|
438 |
|
|
|
439 |
|
|
end neorv32_cpu_decompressor_rtl;
|