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

Subversion Repositories neorv32

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

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 52 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '0') then -- C.LW
143
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_load_c;
144
            else -- C.FLW
145
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_flw_c;
146
            end if;
147 2 zero_gravi
            ci_instr32_o(21 downto 20)                                 <= "00";
148
            ci_instr32_o(22)                                           <= ci_instr16_i(6);
149
            ci_instr32_o(23)                                           <= ci_instr16_i(10);
150
            ci_instr32_o(24)                                           <= ci_instr16_i(11);
151
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
152
            ci_instr32_o(26)                                           <= ci_instr16_i(5);
153 6 zero_gravi
            ci_instr32_o(31 downto 27)                                 <= (others => '0');
154 2 zero_gravi
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_lw_c;
155
            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
156
            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
157
 
158 52 zero_gravi
          when "110" | "111" => -- C.SW / C.FSW
159 2 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
160 52 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '0') then -- C.SW
161
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_store_c;
162
            else -- C.FSW
163
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_fsw_c;
164
            end if;
165 2 zero_gravi
            ci_instr32_o(08 downto 07)                                 <= "00";
166
            ci_instr32_o(09)                                           <= ci_instr16_i(6);
167
            ci_instr32_o(10)                                           <= ci_instr16_i(10);
168
            ci_instr32_o(11)                                           <= ci_instr16_i(11);
169
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
170
            ci_instr32_o(26)                                           <= ci_instr16_i(5);
171
            ci_instr32_o(31 downto 27)                                 <= (others => '0');
172
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sw_c;
173
            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
174
            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
175
 
176
          when others => -- undefined
177 49 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
178 2 zero_gravi
            ci_illegal_o <= '1';
179 49 zero_gravi
 
180 2 zero_gravi
        end case;
181
 
182
      when "01" => -- C1: Control Transfer Instructions, Integer Constant-Generation Instructions
183
 
184
        case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
185
          when "101" => -- C.J
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)         <= "00000"; -- discard return address
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 "001" => -- C.JAL
195
          -- ----------------------------------------------------------------------------------------------------------
196
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jal_c;
197
            ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00001"; -- save return address to link register
198
            ci_instr32_o(19 downto 12)                                 <= imm20_v(19 downto 12);
199
            ci_instr32_o(20)                                           <= imm20_v(11);
200
            ci_instr32_o(30 downto 21)                                 <= imm20_v(10 downto 01);
201
            ci_instr32_o(31)                                           <= imm20_v(20);
202
 
203
          when "110" => -- C.BEQ
204
          -- ----------------------------------------------------------------------------------------------------------
205
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_branch_c;
206
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_beq_c;
207
            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);
208
            ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c)       <= "00000"; -- x0
209
            ci_instr32_o(07)                                           <= imm12_v(11);
210
            ci_instr32_o(11 downto 08)                                 <= imm12_v(04 downto 01);
211
            ci_instr32_o(30 downto 25)                                 <= imm12_v(10 downto 05);
212
            ci_instr32_o(31)                                           <= imm12_v(12);
213
 
214
          when "111" => -- C.BNEZ
215
          -- ----------------------------------------------------------------------------------------------------------
216
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_branch_c;
217
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_bne_c;
218
            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);
219
            ci_instr32_o(instr_rs2_msb_c downto instr_rs2_lsb_c)       <= "00000"; -- x0
220
            ci_instr32_o(07)                                           <= imm12_v(11);
221
            ci_instr32_o(11 downto 08)                                 <= imm12_v(04 downto 01);
222
            ci_instr32_o(30 downto 25)                                 <= imm12_v(10 downto 05);
223
            ci_instr32_o(31)                                           <= imm12_v(12);
224
 
225
          when "010" => -- C.LI
226
          -- ----------------------------------------------------------------------------------------------------------
227
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
228
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
229
            ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00000"; -- x0
230
            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);
231
            ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
232
            ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
233
            ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
234
            ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
235
            ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
236
            ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
237
            ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(12);
238
            if (ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c) = "00000") then -- HINT
239
              ci_illegal_o <= '1';
240
            end if;
241
 
242
          when "011" => -- C.LUI / C.ADDI16SP
243
          -- ----------------------------------------------------------------------------------------------------------
244
            if (ci_instr16_i(ci_rd_5_msb_c downto ci_rd_5_lsb_c) = "00010") then -- C.ADDI16SP
245
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
246
              ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
247
              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);
248
              ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00010"; -- stack pointer
249
              ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00010"; -- stack pointer
250
              ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
251 41 zero_gravi
              ci_instr32_o(instr_imm12_lsb_c + 0)                        <= '0';
252
              ci_instr32_o(instr_imm12_lsb_c + 1)                        <= '0';
253
              ci_instr32_o(instr_imm12_lsb_c + 2)                        <= '0';
254
              ci_instr32_o(instr_imm12_lsb_c + 3)                        <= '0';
255
              ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
256
              ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(2);
257
              ci_instr32_o(instr_imm12_lsb_c + 6)                        <= ci_instr16_i(5);
258
              ci_instr32_o(instr_imm12_lsb_c + 7)                        <= ci_instr16_i(3);
259
              ci_instr32_o(instr_imm12_lsb_c + 8)                        <= ci_instr16_i(4);
260
              ci_instr32_o(instr_imm12_lsb_c + 9)                        <= ci_instr16_i(12);
261 2 zero_gravi
 
262
            else -- C.LUI
263
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_lui_c;
264
              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);
265
              ci_instr32_o(instr_imm20_msb_c downto instr_imm20_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
266
              ci_instr32_o(instr_imm20_lsb_c + 0)                        <= ci_instr16_i(2);
267
              ci_instr32_o(instr_imm20_lsb_c + 1)                        <= ci_instr16_i(3);
268
              ci_instr32_o(instr_imm20_lsb_c + 2)                        <= ci_instr16_i(4);
269
              ci_instr32_o(instr_imm20_lsb_c + 3)                        <= ci_instr16_i(5);
270
              ci_instr32_o(instr_imm20_lsb_c + 4)                        <= ci_instr16_i(6);
271
              ci_instr32_o(instr_imm20_lsb_c + 5)                        <= ci_instr16_i(12);
272
            end if;
273
            if (ci_instr16_i(6 downto 2) = "00000") and (ci_instr16_i(12) = '0') then -- reserved
274
              ci_illegal_o <= '1';
275
            end if;
276
 
277
          when "000" => -- C.NOP (rd=0) / C.ADDI
278
          -- ----------------------------------------------------------------------------------------------------------
279
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
280
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
281
            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);
282
            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);
283
            ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
284
            ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
285
            ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
286
            ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
287
            ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
288
            ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
289
            ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(12);
290
 
291
          when "100" => -- C.SRLI, C.SRAI, C.ANDI, C.SUB, C.XOR, C.OR, C.AND, reserved
292
          -- ----------------------------------------------------------------------------------------------------------
293
            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);
294
            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);
295
            if (ci_instr16_i(11 downto 10) = "11") then -- register-register operation
296
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
297
              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);
298
              case ci_instr16_i(6 downto 5) is
299
                when "00" => -- C.SUB
300
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_subadd_c;
301
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0100000";
302
                when "01" => -- C.XOR
303
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_xor_c;
304
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
305
                when "10" => -- C.OR
306
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_or_c;
307
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
308
                when others => -- C.AND
309
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_and_c;
310
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
311
              end case;
312
            else -- register-immediate operation
313
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
314
              case ci_instr16_i(11 downto 10) is
315
                when "00" => -- C.SRLI
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) <= "0000000";
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 "01" => -- C.SRAI
325
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sr_c;
326
                  ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0100000";
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_illegal_o <= ci_instr16_i(12);
333
                when "10" => -- C.ANDI
334
                  ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_and_c;
335
                  ci_instr32_o(instr_imm12_msb_c downto instr_imm12_lsb_c)   <= (others => ci_instr16_i(12)); -- sign extend
336
                  ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
337
                  ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
338
                  ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
339
                  ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
340
                  ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
341
                  ci_instr32_o(instr_imm12_lsb_c + 5)                        <= ci_instr16_i(12);
342
                when others => -- register-register operation
343
                  NULL;
344
              end case;
345
            end if;
346
            if (ci_instr16_i(12 downto 10) = "111") then -- reserved / undefined
347
              ci_illegal_o <= '1';
348
            end if;
349
 
350
          when others => -- undefined
351 49 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
352 2 zero_gravi
            ci_illegal_o <= '1';
353 49 zero_gravi
 
354 2 zero_gravi
        end case;
355
 
356
      when "10" => -- C2: Stack-Pointer-Based Loads and Stores, Control Transfer Instructions
357
        case ci_instr16_i(ci_funct3_msb_c downto ci_funct3_lsb_c) is
358
 
359
          when "000" => -- C.SLLI
360
          -- ----------------------------------------------------------------------------------------------------------
361
            ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alui_c;
362
            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);
363
            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);
364
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sll_c;
365
            ci_instr32_o(instr_funct7_msb_c downto instr_funct7_lsb_c) <= "0000000";
366
            ci_instr32_o(instr_imm12_lsb_c + 0)                        <= ci_instr16_i(2);
367
            ci_instr32_o(instr_imm12_lsb_c + 1)                        <= ci_instr16_i(3);
368
            ci_instr32_o(instr_imm12_lsb_c + 2)                        <= ci_instr16_i(4);
369
            ci_instr32_o(instr_imm12_lsb_c + 3)                        <= ci_instr16_i(5);
370
            ci_instr32_o(instr_imm12_lsb_c + 4)                        <= ci_instr16_i(6);
371
            ci_illegal_o <= ci_instr16_i(12);
372
 
373 52 zero_gravi
          when "010" | "011" => -- C.LWSP / C.FLWSP
374 2 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
375 52 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '0') then -- C.LWSP
376
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_load_c;
377
            else -- C.FLWSP
378
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_flw_c;
379
            end if;
380 2 zero_gravi
            ci_instr32_o(21 downto 20)                                 <= "00";
381
            ci_instr32_o(22)                                           <= ci_instr16_i(4);
382
            ci_instr32_o(23)                                           <= ci_instr16_i(5);
383
            ci_instr32_o(24)                                           <= ci_instr16_i(6);
384
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
385
            ci_instr32_o(26)                                           <= ci_instr16_i(2);
386
            ci_instr32_o(27)                                           <= ci_instr16_i(3);
387
            ci_instr32_o(31 downto 28)                                 <= (others => '0');
388
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_lw_c;
389
            ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00010"; -- stack pointer
390
            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);
391
 
392 52 zero_gravi
          when "110" | "111" => -- C.SWSP / C.FSWSP
393 2 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
394 52 zero_gravi
            if (ci_instr16_i(ci_funct3_lsb_c) = '0') then -- C.SWSP
395
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_store_c;
396
            else -- C.FSWSP
397
              ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_fsw_c;
398
            end if;
399 2 zero_gravi
            ci_instr32_o(08 downto 07)                                 <= "00";
400
            ci_instr32_o(09)                                           <= ci_instr16_i(9);
401
            ci_instr32_o(10)                                           <= ci_instr16_i(10);
402
            ci_instr32_o(11)                                           <= ci_instr16_i(11);
403
            ci_instr32_o(25)                                           <= ci_instr16_i(12);
404
            ci_instr32_o(26)                                           <= ci_instr16_i(7);
405
            ci_instr32_o(27)                                           <= ci_instr16_i(8);
406
            ci_instr32_o(31 downto 28)                                 <= (others => '0');
407
            ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= funct3_sw_c;
408
            ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00010"; -- stack pointer
409
            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);
410
 
411
          when "100" => -- C.JR, C.JALR, C.MV, C.EBREAK, C.ADD
412
          -- ----------------------------------------------------------------------------------------------------------
413
            if (ci_instr16_i(12) = '0') then -- C.JR, C.MV
414
              if (ci_instr16_i(6 downto 2) = "00000") then -- C.JR
415
                ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jalr_c;
416
                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);
417
                ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00000"; -- discard return address
418
              else -- C.MV
419
                ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
420
                ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= "000";
421
                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);
422
                ci_instr32_o(instr_rs1_msb_c downto instr_rs1_lsb_c)       <= "00000"; -- x0
423
                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);
424
              end if;
425
            else -- C.EBREAK, C.JALR, C.ADD
426
              if (ci_instr16_i(6 downto 2) = "00000") then -- C.EBREAK, C.JALR
427
                if (ci_instr16_i(11 downto 7) = "00000") then -- C.EBREAK
428
                  ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c)   <= opcode_syscsr_c;
429
                  ci_instr32_o(instr_funct12_msb_c downto instr_funct12_lsb_c) <= "000000000001";
430
                else -- C.JALR
431
                  ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_jalr_c;
432
                  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);
433
                  ci_instr32_o(instr_rd_msb_c downto instr_rd_lsb_c)         <= "00001"; -- save return address to link register
434
                end if;
435
              else -- C.ADD
436
                ci_instr32_o(instr_opcode_msb_c downto instr_opcode_lsb_c) <= opcode_alu_c;
437
                ci_instr32_o(instr_funct3_msb_c downto instr_funct3_lsb_c) <= "000";
438
                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);
439
                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);
440
                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);
441
              end if;
442
            end if;
443
 
444
          when others => -- undefined
445 49 zero_gravi
          -- ----------------------------------------------------------------------------------------------------------
446 2 zero_gravi
            ci_illegal_o <= '1';
447 49 zero_gravi
 
448 2 zero_gravi
        end case;
449
 
450
      when others => -- not a compressed instruction
451 49 zero_gravi
      -- ----------------------------------------------------------------------------------------------------------
452 2 zero_gravi
        NULL;
453
 
454
    end case;
455
  end process decompressor;
456
 
457
 
458
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.