add cpu decode cases
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 23 Nov 2018 15:23:49 +0000 (15:23 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 23 Nov 2018 15:23:49 +0000 (15:23 +0000)
cpu_decoder.py

index 9aebc4cf84dca9a50acca4ab3c6bbc4d975bd991..d2eff7d66aaf594b1bbe01be5371220a39702e4b 100644 (file)
@@ -89,7 +89,6 @@ def calculate_immediate(instruction, immediate):
 
     return ci
 
-
 class CPUDecoder(Module):
 
     def __init__(self):
@@ -114,32 +113,101 @@ class CPUDecoder(Module):
         ci = calculate_immediate(self.instruction, self.immediate)
         self.comb += Case(self.opcode, ci)
 
-"""
-        
-        function [31:0] calculate_immediate(input [31:0] instruction, input [6:0] opcode);
-        begin
-            case(opcode)
+        self.comb += self.calculate_action()
 
-            `opcode_custom_0,
-            `opcode_48b_escape_0,
-            `opcode_custom_1,
-            `opcode_64b_escape,
-            `opcode_reserved_10101,
-            `opcode_rv128_0,
-            `opcode_48b_escape_1,
-            `opcode_reserved_11010,
-            `opcode_reserved_11101,
-            `opcode_rv128_1,
-            `opcode_80b_escape:
-                // unknown
-                calculate_immediate = 32'hXXXXXXXX;
-            default:
-                calculate_immediate = 32'hXXXXXXXX;
-            endcase
-        end
-        endfunction
-        
-        assign immediate = calculate_immediate(instruction, opcode);
+    def calculate_store_action(self):
+        """ decode store action
+        """
+        c = {}
+        # load opcode
+        for op in [ funct3_sb, funct3_sh, funct3_sw, ]:
+            c[op] = self.decode_action.eq(decode_action_store)
+        # default
+        c["default"] = \
+            self.decode_action.eq(decode_action_trap_illegal_instruction)
+
+        return Case(self.funct3, c)
+
+    def calculate_load_action(self):
+        """ decode load action
+        """
+        c = {}
+        # load opcode
+        for op in [ funct3_lb, funct3_lbu, funct3_lh, funct3_lhu, funct3_lw, ]:
+            c[op] = self.decode_action.eq(decode_action_load)
+        # default
+        c["default"] = \
+            self.decode_action.eq(decode_action_trap_illegal_instruction)
+
+        return Case(self.funct3, c)
+
+    def calculate_op_action(self):
+        """ decode op action
+        """
+        c = {}
+        immz = Constant(0, 12)
+        regz = Constant(0, 5)
+        # fence
+        c[funct3_slli] = \
+            If((self.funct7 == Constant(0, 7)),
+                self.decode_action.eq(decode_action_op_op_imm)).\
+            Else(
+                self.decode_action.eq(decode_action_trap_illegal_instruction))
+        # fence.i
+        c[funct3_srli_srai] = \
+            If((self.funct7 == Constant(0, 7) | \
+               (self.funct7 == Constant(0x20, 7))),
+                self.decode_action.eq(decode_action_op_op_imm)).\
+            Else(
+                self.decode_action.eq(decode_action_trap_illegal_instruction))
+        # default
+        c["default"] = \
+            self.decode_action.eq(decode_action_op_op_imm)
+
+        return Case(self.funct3, c)
+
+    def calculate_misc_action(self):
+        """ decode misc mem action
+        """
+        c = {}
+        immz = Constant(0, 12)
+        regz = Constant(0, 5)
+        # fence
+        c[funct3_fence] = \
+            If((self.immediate[8:12] == immz) & (self.rs1 == regz) & \
+                                                   (self.rd == regz),
+                self.decode_action.eq(decode_action_fence)).\
+            Else(
+                self.decode_action.eq(decode_action_trap_illegal_instruction))
+        # fence.i
+        c[funct3_fence_i] = \
+            If((self.immediate[0:12] == immz) & (self.rs1 == regz) & \
+                                                    (self.rd == regz),
+                self.decode_action.eq(decode_action_fence_i)).\
+            Else(
+                self.decode_action.eq(decode_action_trap_illegal_instruction))
+        # default
+        c["default"] = \
+            self.decode_action.eq(decode_action_trap_illegal_instruction)
+
+        return Case(self.funct3, c)
+
+    def calculate_action(self):
+        """ calculate action
+        """
+        c = {}
+        # load opcode
+        c[opcode_load] = self.calculate_load_action()
+        c[opcode_misc_mem] = self.calculate_misc_action()
+        c[opcode_op_imm] = self.calculate_op_action()
+        c[opcode_op] = self.calculate_op_action()
+        c[opcode_lui] = self.decode_action.eq(decode_action_lui_auipc)
+        c[opcode_auipc] = self.decode_action.eq(decode_action_lui_auipc)
+        c[opcode_store] = self.calculate_store_action()
+
+        return Case(self.opcode, c)
+
+"""
         
         function `decode_action calculate_action(
             input [6:0] funct7,
@@ -151,68 +219,6 @@ class CPUDecoder(Module):
             input [6:0] opcode);
         begin
             case(opcode)
-            `opcode_load: begin
-                case(funct3)
-                `funct3_lb,
-                `funct3_lbu,
-                `funct3_lh,
-                `funct3_lhu,
-                `funct3_lw:
-                    calculate_action = `decode_action_load;
-                default:
-                    calculate_action = `decode_action_trap_illegal_instruction;
-                endcase
-            end
-            `opcode_misc_mem: begin
-                if(funct3 == `funct3_fence) begin
-                    if((immediate[11:8] == 0) & (rs1 == 0) & (rd == 0))
-                        calculate_action = `decode_action_fence;
-                    else
-                        calculate_action = `decode_action_trap_illegal_instruction;
-                end
-                else if(funct3 == `funct3_fence_i) begin
-                    if((immediate[11:0] == 0) & (rs1 == 0) & (rd == 0))
-                        calculate_action = `decode_action_fence_i;
-                    else
-                        calculate_action = `decode_action_trap_illegal_instruction;
-                end
-                else
-                begin
-                    calculate_action = `decode_action_trap_illegal_instruction;
-                end
-            end
-            `opcode_op_imm,
-            `opcode_op: begin
-                if(funct3 == `funct3_slli) begin
-                    if(funct7 == 0)
-                        calculate_action = `decode_action_op_op_imm;
-                    else
-                        calculate_action = `decode_action_trap_illegal_instruction;
-                end
-                else if(funct3 == `funct3_srli_srai) begin
-                    if(funct7 == 0 || funct7 == 7'h20)
-                        calculate_action = `decode_action_op_op_imm;
-                    else
-                        calculate_action = `decode_action_trap_illegal_instruction;
-                end
-                else begin
-                    calculate_action = `decode_action_op_op_imm;
-                end
-            end
-            `opcode_lui,
-            `opcode_auipc: begin
-                calculate_action = `decode_action_lui_auipc;
-            end
-            `opcode_store: begin
-                case(funct3)
-                `funct3_sb,
-                `funct3_sh,
-                `funct3_sw:
-                    calculate_action = `decode_action_store;
-                default:
-                    calculate_action = `decode_action_trap_illegal_instruction;
-                endcase
-            end
             `opcode_branch: begin
                 case(funct3)
                 `funct3_beq,