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

Subversion Repositories neorv32

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

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

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