2 * Copyright 2018 Jacob Lifshay
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 `timescale 1ns / 100ps
28 input [31:0] instruction,
34 output [31:0] immediate,
36 output `decode_action decode_action
39 assign funct7 = instruction[31:25];
40 assign funct3 = instruction[14:12];
41 assign rd = instruction[11:7];
42 assign rs1 = instruction[19:15];
43 assign rs2 = instruction[24:20];
44 assign opcode = instruction[6:0];
46 function [31:0] calculate_immediate(input [31:0] instruction, input [6:0] opcode);
53 // R-type: no immediate
54 calculate_immediate = 32'hXXXXXXXX;
63 calculate_immediate = {{20{instruction[31]}}, instruction[31:20]};
67 calculate_immediate = {{21{instruction[31]}}, instruction[30:25], instruction[11:7]};
70 calculate_immediate = {{20{instruction[31]}}, instruction[7], instruction[30:25], instruction[11:8], 1'b0};
74 calculate_immediate = {instruction[31:12], 12'b0};
77 calculate_immediate = {{12{instruction[31]}}, instruction[19:12], instruction[20], instruction[30:25], instruction[24:21], 1'b0};
82 // R4-type: no immediate
83 calculate_immediate = 32'hXXXXXXXX;
88 `opcode_reserved_10101,
91 `opcode_reserved_11010,
92 `opcode_reserved_11101,
96 calculate_immediate = 32'hXXXXXXXX;
98 calculate_immediate = 32'hXXXXXXXX;
103 assign immediate = calculate_immediate(instruction, opcode);
105 function `decode_action calculate_action(
111 input [31:0] immediate,
122 calculate_action = `decode_action_load;
124 calculate_action = `decode_action_trap_illegal_instruction;
127 `opcode_misc_mem: begin
128 if(funct3 == `funct3_fence) begin
129 if((immediate[11:8] == 0) & (rs1 == 0) & (rd == 0))
130 calculate_action = `decode_action_fence;
132 calculate_action = `decode_action_trap_illegal_instruction;
134 else if(funct3 == `funct3_fence_i) begin
135 if((immediate[11:0] == 0) & (rs1 == 0) & (rd == 0))
136 calculate_action = `decode_action_fence_i;
138 calculate_action = `decode_action_trap_illegal_instruction;
142 calculate_action = `decode_action_trap_illegal_instruction;
147 if(funct3 == `funct3_slli) begin
149 calculate_action = `decode_action_op_op_imm;
151 calculate_action = `decode_action_trap_illegal_instruction;
153 else if(funct3 == `funct3_srli_srai) begin
154 if(funct7 == 0 || funct7 == 7'h20)
155 calculate_action = `decode_action_op_op_imm;
157 calculate_action = `decode_action_trap_illegal_instruction;
160 calculate_action = `decode_action_op_op_imm;
165 calculate_action = `decode_action_lui_auipc;
172 calculate_action = `decode_action_store;
174 calculate_action = `decode_action_trap_illegal_instruction;
177 `opcode_branch: begin
185 calculate_action = `decode_action_branch;
187 calculate_action = `decode_action_trap_illegal_instruction;
191 if(funct3 == `funct3_jalr)
192 calculate_action = `decode_action_jalr;
194 calculate_action = `decode_action_trap_illegal_instruction;
197 calculate_action = `decode_action_jal;
199 `opcode_system: begin
201 `funct3_ecall_ebreak:
202 if((rs1 != 0) | (rd != 0) | ((immediate & ~32'b1) != 0))
203 calculate_action = `decode_action_trap_illegal_instruction;
205 calculate_action = `decode_action_trap_ecall_ebreak;
212 calculate_action = `decode_action_csr;
214 calculate_action = `decode_action_trap_illegal_instruction;
220 `opcode_48b_escape_0,
231 `opcode_reserved_10101,
233 `opcode_48b_escape_1,
234 `opcode_reserved_11010,
235 `opcode_reserved_11101,
237 `opcode_80b_escape: begin
238 calculate_action = `decode_action_trap_illegal_instruction;
241 calculate_action = `decode_action_trap_illegal_instruction;
246 assign decode_action = calculate_action(funct7,