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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_decompressor.vhd] - Blame information for rev 57

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2 49 zero_gravi
-- # << NEORV32 - CPU: Compressed Instructions Decoder (RISC-V "C" Extension) >>                   #
3 2 zero_gravi
-- # ********************************************************************************************* #
4
-- # BSD 3-Clause License                                                                          #
5
-- #                                                                                               #
6 49 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
7 2 zero_gravi
-- #                                                                                               #
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_illegal_o : out std_ulogic; -- is an illegal compressed instruction
48
    ci_instr32_o : out std_ulogic_vector(31 downto 0)  -- 32-bit decompressed instruction
49
  );
50
end neorv32_cpu_decompressor;
51
 
52
architecture neorv32_cpu_decompressor_rtl of neorv32_cpu_decompressor is
53
 
54
  -- compressed instruction layout --
55
  constant ci_opcode_lsb_c : natural :=  0;
56
  constant ci_opcode_msb_c : natural :=  1;
57
  constant ci_rd_3_lsb_c   : natural :=  2;
58
  constant ci_rd_3_msb_c   : natural :=  4;
59
  constant ci_rd_5_lsb_c   : natural :=  7;
60
  constant ci_rd_5_msb_c   : natural := 11;
61
  constant ci_rs1_3_lsb_c  : natural :=  7;
62
  constant ci_rs1_3_msb_c  : natural :=  9;
63
  constant ci_rs1_5_lsb_c  : natural :=  7;
64
  constant ci_rs1_5_msb_c  : natural := 11;
65
  constant ci_rs2_3_lsb_c  : natural :=  2;
66
  constant ci_rs2_3_msb_c  : natural :=  4;
67
  constant ci_rs2_5_lsb_c  : natural :=  2;
68
  constant ci_rs2_5_msb_c  : natural :=  6;
69
  constant ci_funct3_lsb_c : natural := 13;
70
  constant ci_funct3_msb_c : natural := 15;
71
 
72
begin
73
 
74
  -- Compressed Instruction Decoder ---------------------------------------------------------
75
  -- -------------------------------------------------------------------------------------------
76
  decompressor: process(ci_instr16_i)
77
    variable imm20_v : std_ulogic_vector(20 downto 0);
78
    variable imm12_v : std_ulogic_vector(12 downto 0);
79
  begin
80
    -- defaults --
81
    ci_illegal_o <= '0';
82
    ci_instr32_o <= (others => '0');
83
 
84
    -- 22-bit sign-extended immediate for J/JAL --
85
    imm20_v := (others => ci_instr16_i(12)); -- sign extension
86
    imm20_v(00):= '0';
87
    imm20_v(01):= ci_instr16_i(3);
88
    imm20_v(02):= ci_instr16_i(4);
89
    imm20_v(03):= ci_instr16_i(5);
90
    imm20_v(04):= ci_instr16_i(11);
91
    imm20_v(05):= ci_instr16_i(2);
92
    imm20_v(06):= ci_instr16_i(7);
93
    imm20_v(07):= ci_instr16_i(6);
94
    imm20_v(08):= ci_instr16_i(9);
95
    imm20_v(09):= ci_instr16_i(10);
96
    imm20_v(10):= ci_instr16_i(8);
97
    imm20_v(11):= ci_instr16_i(12);
98
 
99
    -- 12-bit sign-extended immediate for branches --
100
    imm12_v := (others => ci_instr16_i(12)); -- sign extension
101
    imm12_v(00):= '0';
102
    imm12_v(01):= ci_instr16_i(3);
103
    imm12_v(02):= ci_instr16_i(4);
104
    imm12_v(03):= ci_instr16_i(10);
105
    imm12_v(04):= ci_instr16_i(11);
106
    imm12_v(05):= ci_instr16_i(2);
107
    imm12_v(06):= ci_instr16_i(5);
108
    imm12_v(07):= ci_instr16_i(6);
109
    imm12_v(08):= ci_instr16_i(12);
110
 
111
    -- actual decoder --
112
    case ci_instr16_i(ci_opcode_msb_c downto ci_opcode_lsb_c) is
113
 
114
      when "00" => -- C0: Register-Based Loads and Stores
115
        case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
116
 
117
          when "000" => -- Illegal_instruction, C.ADDI4SPN
118
          -- ----------------------------------------------------------------------------------------------------------
119 36 zero_gravi
            if (ci_instr16_i(12 downto 2) = "00000000000") then -- "official illegal instruction"
120 2 zero_gravi
              ci_illegal_o <= '1';
121 36 zero_gravi
            else
122
              -- C.ADDI4SPN
123 2 zero_gravi
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
124
              ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00010"; -- stack pointer
125
              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);
126
              ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
127
              ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => '0'); -- zero extend
128
              ci_instr32_o(instr_imm12_lsb_c + 0)                        <= '0';
129
              ci_instr32_o(instr_imm12_lsb_c + 1)                        <= '0';
130
              ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(6);
131
              ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
132
              ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(11);
133
              ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(12);
134
              ci_instr32_o(instr_imm12_lsb_c + 6)                        <= ci_instr16_i(7);
135
              ci_instr32_o(instr_imm12_lsb_c + 7)                        <= ci_instr16_i(8);
136
              ci_instr32_o(instr_imm12_lsb_c + 8)                        <= ci_instr16_i(9);
137
              ci_instr32_o(instr_imm12_lsb_c + 9)                        <= ci_instr16_i(10);
138
            end if;
139
 
140 52 zero_gravi
          when "010" | "011" => -- C.LW / C.FLW
141 2 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
142 53 zero_gravi
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_load_c;
143 2 zero_gravi
            ci_instr32_o(21 downto 20)                                 <= "00";
144
            ci_instr32_o(22)                                           <= ci_instr16_i(6);
145
            ci_instr32_o(23)                                           <= ci_instr16_i(10);
146
            ci_instr32_o(24)                                           <= ci_instr16_i(11);
147
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
148
            ci_instr32_o(26)                                           <= ci_instr16_i(5);
149 6 zero_gravi
            ci_instr32_o(31 downto 27)                                 <= (others => '0');
150 2 zero_gravi
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_lw_c;
151
            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
152
            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
153 53 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '1') then -- C.FLW
154
              ci_illegal_o <= '1';
155
            end if;
156 2 zero_gravi
 
157 52 zero_gravi
          when "110" | "111" => -- C.SW / C.FSW
158 2 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
159 53 zero_gravi
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_store_c;
160 2 zero_gravi
            ci_instr32_o(08 downto 07)                                 <= "00";
161
            ci_instr32_o(09)                                           <= ci_instr16_i(6);
162
            ci_instr32_o(10)                                           <= ci_instr16_i(10);
163
            ci_instr32_o(11)                                           <= ci_instr16_i(11);
164
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
165
            ci_instr32_o(26)                                           <= ci_instr16_i(5);
166
            ci_instr32_o(31 downto 27)                                 <= (others => '0');
167
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sw_c;
168
            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
169
            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
170 53 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '1') then -- C.FSW
171
              ci_illegal_o <= '1';
172
            end if;
173 2 zero_gravi
 
174
          when others => -- undefined
175 49 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
176 2 zero_gravi
            ci_illegal_o <= '1';
177 49 zero_gravi
 
178 2 zero_gravi
        end case;
179
 
180
      when "01" => -- C1: Control Transfer Instructions, Integer Constant-Generation Instructions
181
 
182
        case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
183
          when "101" => -- C.J
184
          -- ----------------------------------------------------------------------------------------------------------
185
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jal_c;
186
            ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00000"; -- discard return address
187
            ci_instr32_o(19 downto 12)                                 <= imm20_v(19 downto 12);
188
            ci_instr32_o(20)                                           <= imm20_v(11);
189
            ci_instr32_o(30 downto 21)                                 <= imm20_v(10 downto 01);
190
            ci_instr32_o(31)                                           <= imm20_v(20);
191
 
192
          when "001" => -- C.JAL
193
          -- ----------------------------------------------------------------------------------------------------------
194
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jal_c;
195
            ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00001"; -- save return address to link register
196
            ci_instr32_o(19 downto 12)                                 <= imm20_v(19 downto 12);
197
            ci_instr32_o(20)                                           <= imm20_v(11);
198
            ci_instr32_o(30 downto 21)                                 <= imm20_v(10 downto 01);
199
            ci_instr32_o(31)                                           <= imm20_v(20);
200
 
201
          when "110" => -- C.BEQ
202
          -- ----------------------------------------------------------------------------------------------------------
203
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_branch_c;
204
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_beq_c;
205
            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);
206
            ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c)       <= "00000"; -- x0
207
            ci_instr32_o(07)                                           <= imm12_v(11);
208
            ci_instr32_o(11 downto 08)                                 <= imm12_v(04 downto 01);
209
            ci_instr32_o(30 downto 25)                                 <= imm12_v(10 downto 05);
210
            ci_instr32_o(31)                                           <= imm12_v(12);
211
 
212
          when "111" => -- C.BNEZ
213
          -- ----------------------------------------------------------------------------------------------------------
214
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_branch_c;
215
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_bne_c;
216
            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);
217
            ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c)       <= "00000"; -- x0
218
            ci_instr32_o(07)                                           <= imm12_v(11);
219
            ci_instr32_o(11 downto 08)                                 <= imm12_v(04 downto 01);
220
            ci_instr32_o(30 downto 25)                                 <= imm12_v(10 downto 05);
221
            ci_instr32_o(31)                                           <= imm12_v(12);
222
 
223
          when "010" => -- C.LI
224
          -- ----------------------------------------------------------------------------------------------------------
225
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
226
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
227
            ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00000"; -- x0
228
            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);
229
            ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
230
            ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
231
            ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
232
            ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
233
            ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
234
            ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
235
            ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(12);
236
            if (ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c) = "00000") then -- HINT
237
              ci_illegal_o <= '1';
238
            end if;
239
 
240
          when "011" => -- C.LUI / C.ADDI16SP
241
          -- ----------------------------------------------------------------------------------------------------------
242
            if (ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c) = "00010") then -- C.ADDI16SP
243
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
244
              ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
245
              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);
246
              ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00010"; -- stack pointer
247
              ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00010"; -- stack pointer
248
              ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
249 41 zero_gravi
              ci_instr32_o(instr_imm12_lsb_c + 0)                        <= '0';
250
              ci_instr32_o(instr_imm12_lsb_c + 1)                        <= '0';
251
              ci_instr32_o(instr_imm12_lsb_c + 2)                        <= '0';
252
              ci_instr32_o(instr_imm12_lsb_c + 3)                        <= '0';
253
              ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
254
              ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(2);
255
              ci_instr32_o(instr_imm12_lsb_c + 6)                        <= ci_instr16_i(5);
256
              ci_instr32_o(instr_imm12_lsb_c + 7)                        <= ci_instr16_i(3);
257
              ci_instr32_o(instr_imm12_lsb_c + 8)                        <= ci_instr16_i(4);
258
              ci_instr32_o(instr_imm12_lsb_c + 9)                        <= ci_instr16_i(12);
259 2 zero_gravi
 
260
            else -- C.LUI
261
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_lui_c;
262
              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);
263
              ci_instr32_o(instr_imm20_msb_c downto instr_imm20_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
264
              ci_instr32_o(instr_imm20_lsb_c + 0)                        <= ci_instr16_i(2);
265
              ci_instr32_o(instr_imm20_lsb_c + 1)                        <= ci_instr16_i(3);
266
              ci_instr32_o(instr_imm20_lsb_c + 2)                        <= ci_instr16_i(4);
267
              ci_instr32_o(instr_imm20_lsb_c + 3)                        <= ci_instr16_i(5);
268
              ci_instr32_o(instr_imm20_lsb_c + 4)                        <= ci_instr16_i(6);
269
              ci_instr32_o(instr_imm20_lsb_c + 5)                        <= ci_instr16_i(12);
270
            end if;
271
            if (ci_instr16_i(6 downto 2) = "00000") and (ci_instr16_i(12) = '0') then -- reserved
272
              ci_illegal_o <= '1';
273
            end if;
274
 
275
          when "000" => -- C.NOP (rd=0) / C.ADDI
276
          -- ----------------------------------------------------------------------------------------------------------
277
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
278
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
279
            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);
280
            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);
281
            ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
282
            ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
283
            ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
284
            ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
285
            ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
286
            ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
287
            ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(12);
288
 
289
          when "100" => -- C.SRLI, C.SRAI, C.ANDI, C.SUB, C.XOR, C.OR, C.AND, reserved
290
          -- ----------------------------------------------------------------------------------------------------------
291
            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);
292
            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);
293
            if (ci_instr16_i(11 downto 10) = "11") then -- register-register operation
294
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
295
              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);
296
              case ci_instr16_i(6 downto 5) is
297
                when "00" => -- C.SUB
298
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
299
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0100000";
300
                when "01" => -- C.XOR
301
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_xor_c;
302
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
303
                when "10" => -- C.OR
304
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_or_c;
305
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
306
                when others => -- C.AND
307
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_and_c;
308
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
309
              end case;
310
            else -- register-immediate operation
311
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
312
              case ci_instr16_i(11 downto 10) is
313
                when "00" => -- C.SRLI
314
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sr_c;
315
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
316
                  ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
317
                  ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
318
                  ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
319
                  ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
320
                  ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
321
                  ci_illegal_o <= ci_instr16_i(12);
322
                when "01" => -- C.SRAI
323
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sr_c;
324
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0100000";
325
                  ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
326
                  ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
327
                  ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
328
                  ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
329
                  ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
330
                  ci_illegal_o <= ci_instr16_i(12);
331
                when "10" => -- C.ANDI
332
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_and_c;
333
                  ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
334
                  ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
335
                  ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
336
                  ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
337
                  ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
338
                  ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
339
                  ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(12);
340
                when others => -- register-register operation
341
                  NULL;
342
              end case;
343
            end if;
344
            if (ci_instr16_i(12 downto 10) = "111") then -- reserved / undefined
345
              ci_illegal_o <= '1';
346
            end if;
347
 
348
          when others => -- undefined
349 49 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
350 2 zero_gravi
            ci_illegal_o <= '1';
351 49 zero_gravi
 
352 2 zero_gravi
        end case;
353
 
354
      when "10" => -- C2: Stack-Pointer-Based Loads and Stores, Control Transfer Instructions
355
        case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
356
 
357
          when "000" => -- C.SLLI
358
          -- ----------------------------------------------------------------------------------------------------------
359
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
360
            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);
361
            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);
362
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sll_c;
363
            ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
364
            ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
365
            ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
366
            ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
367
            ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
368
            ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
369
            ci_illegal_o <= ci_instr16_i(12);
370
 
371 52 zero_gravi
          when "010" | "011" => -- C.LWSP / C.FLWSP
372 2 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
373 53 zero_gravi
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_load_c;
374 2 zero_gravi
            ci_instr32_o(21 downto 20)                                 <= "00";
375
            ci_instr32_o(22)                                           <= ci_instr16_i(4);
376
            ci_instr32_o(23)                                           <= ci_instr16_i(5);
377
            ci_instr32_o(24)                                           <= ci_instr16_i(6);
378
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
379
            ci_instr32_o(26)                                           <= ci_instr16_i(2);
380
            ci_instr32_o(27)                                           <= ci_instr16_i(3);
381
            ci_instr32_o(31 downto 28)                                 <= (others => '0');
382
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_lw_c;
383
            ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00010"; -- stack pointer
384
            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);
385 53 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '1') then -- C.FLWSP
386
              ci_illegal_o <= '1';
387
            end if;
388 2 zero_gravi
 
389 52 zero_gravi
          when "110" | "111" => -- C.SWSP / C.FSWSP
390 2 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
391 53 zero_gravi
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_store_c;
392 2 zero_gravi
            ci_instr32_o(08 downto 07)                                 <= "00";
393
            ci_instr32_o(09)                                           <= ci_instr16_i(9);
394
            ci_instr32_o(10)                                           <= ci_instr16_i(10);
395
            ci_instr32_o(11)                                           <= ci_instr16_i(11);
396
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
397
            ci_instr32_o(26)                                           <= ci_instr16_i(7);
398
            ci_instr32_o(27)                                           <= ci_instr16_i(8);
399
            ci_instr32_o(31 downto 28)                                 <= (others => '0');
400
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sw_c;
401
            ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00010"; -- stack pointer
402
            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);
403 53 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '1') then -- C.FSWSP
404
              ci_illegal_o <= '1';
405
            end if;
406 2 zero_gravi
 
407
          when "100" => -- C.JR, C.JALR, C.MV, C.EBREAK, C.ADD
408
          -- ----------------------------------------------------------------------------------------------------------
409
            if (ci_instr16_i(12) = '0') then -- C.JR, C.MV
410
              if (ci_instr16_i(6 downto 2) = "00000") then -- C.JR
411
                ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jalr_c;
412
                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);
413
                ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00000"; -- discard return address
414
              else -- C.MV
415
                ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
416
                ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= "000";
417
                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);
418
                ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00000"; -- x0
419
                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);
420
              end if;
421
            else -- C.EBREAK, C.JALR, C.ADD
422
              if (ci_instr16_i(6 downto 2) = "00000") then -- C.EBREAK, C.JALR
423
                if (ci_instr16_i(11 downto 7) = "00000") then -- C.EBREAK
424
                  ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c)   <= opcode_syscsr_c;
425
                  ci_instr32_o(instr_funct12_msb_c downto instr_funct12_lsb_c) <= "000000000001";
426
                else -- C.JALR
427
                  ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jalr_c;
428
                  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);
429
                  ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00001"; -- save return address to link register
430
                end if;
431
              else -- C.ADD
432
                ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
433
                ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= "000";
434
                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);
435
                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);
436
                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);
437
              end if;
438
            end if;
439
 
440
          when others => -- undefined
441 49 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
442 2 zero_gravi
            ci_illegal_o <= '1';
443 49 zero_gravi
 
444 2 zero_gravi
        end case;
445
 
446
      when others => -- not a compressed instruction
447 49 zero_gravi
      -- ----------------------------------------------------------------------------------------------------------
448 2 zero_gravi
        NULL;
449
 
450
    end case;
451
  end process decompressor;
452
 
453
 
454
end neorv32_cpu_decompressor_rtl;

powered by: WebSVN 2.1.0

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