works even better now

This commit is contained in:
Yehowshua Immanuel 2025-02-19 18:28:08 -05:00
parent f9248057f9
commit 003a1c8545
2 changed files with 47 additions and 33 deletions

View file

@ -58,25 +58,32 @@ getRs1 instr = bitCoerce $ slice d19 d15 (pack instr)
decodeRType :: Insn -> Opcode decodeRType :: Insn -> Opcode
decodeRType insn = decodeRType insn =
case funct3 of case opcode of
0x00 -> ADD (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0b0110011 ->
0x00 -> SUB (RTypeFields opcode rd funct3 rs1 rs2 funct7) case funct3 of
0x04 -> XOR (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0x00 -> case funct7 of
0x06 -> OR (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0x00 -> ADD (RTypeFields opcode rd funct3 rs1 rs2 funct7)
0x07 -> AND (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0x20 -> SUB (RTypeFields opcode rd funct3 rs1 rs2 funct7)
0x01 -> SLL (RTypeFields opcode rd funct3 rs1 rs2 funct7) _ -> Unimplemented
0x05 -> SRL (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0x04 -> XOR (RTypeFields opcode rd funct3 rs1 rs2 funct7)
0x05 -> SRA (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0x06 -> OR (RTypeFields opcode rd funct3 rs1 rs2 funct7)
0x02 -> SLT (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0x07 -> AND (RTypeFields opcode rd funct3 rs1 rs2 funct7)
0x03 -> SLTU (RTypeFields opcode rd funct3 rs1 rs2 funct7) 0x01 -> SLL (RTypeFields opcode rd funct3 rs1 rs2 funct7)
_ -> Unimplemented 0x05 -> case funct7 of
0x00 -> SRL (RTypeFields opcode rd funct3 rs1 rs2 funct7)
0x20 -> SRA (RTypeFields opcode rd funct3 rs1 rs2 funct7)
_ -> Unimplemented
0x02 -> SLT (RTypeFields opcode rd funct3 rs1 rs2 funct7)
0x03 -> SLTU (RTypeFields opcode rd funct3 rs1 rs2 funct7)
_ -> Unimplemented
_ -> Unimplemented
where where
opcode = getOpcode insn opcode = getOpcode insn
rd = getRd insn rd = getRd insn
funct3 = getFunct3 insn funct3 = getFunct3 insn
rs1 = getRs1 insn rs1 = getRs1 insn
rs2 = getRs2 insn rs2 = getRs2 insn
funct7 = getFunct7 insn funct7 = getFunct7 insn
decodeIType :: Insn -> Opcode decodeIType :: Insn -> Opcode
decodeIType insn = case opcode of decodeIType insn = case opcode of
@ -122,11 +129,14 @@ decodeIType insn = case opcode of
imm = getImm12 insn imm = getImm12 insn
decodeSType :: Insn -> Opcode decodeSType :: Insn -> Opcode
decodeSType insn = case funct3 of decodeSType insn =
0x0 -> SB (STypeFields opcode funct3 rs1 rs2 imm12) -- Store Byte case opcode of
0x1 -> SH (STypeFields opcode funct3 rs1 rs2 imm12) -- Store Halfword 0b0100011 -> case funct3 of
0x2 -> SW (STypeFields opcode funct3 rs1 rs2 imm12) -- Store Word 0x0 -> SB (STypeFields opcode funct3 rs1 rs2 imm12) -- Store Byte
_ -> Unimplemented 0x1 -> SH (STypeFields opcode funct3 rs1 rs2 imm12) -- Store Halfword
0x2 -> SW (STypeFields opcode funct3 rs1 rs2 imm12) -- Store Word
_ -> Unimplemented
_ -> Unimplemented
where where
opcode = getOpcode insn opcode = getOpcode insn
funct3 = getFunct3 insn funct3 = getFunct3 insn
@ -135,14 +145,17 @@ decodeSType insn = case funct3 of
imm12 = getImm12SType insn imm12 = getImm12SType insn
decodeBType :: Insn -> Opcode decodeBType :: Insn -> Opcode
decodeBType insn = case funct3 of decodeBType insn =
0x0 -> BEQ (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if equal case opcode of
0x1 -> BNE (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if not equal 0b1100011 -> case funct3 of
0x4 -> BLT (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if less than 0x0 -> BEQ (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if equal
0x5 -> BGE (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if greater or equal 0x1 -> BNE (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if not equal
0x6 -> BLTU (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if less than (unsigned) 0x4 -> BLT (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if less than
0x7 -> BGEU (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if greater or equal (unsigned) 0x5 -> BGE (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if greater or equal
_ -> Unimplemented 0x6 -> BLTU (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if less than (unsigned)
0x7 -> BGEU (BTypeFields opcode funct3 rs1 rs2 imm13) -- Branch if greater or equal (unsigned)
_ -> Unimplemented
_ -> Unimplemented
where where
opcode = getOpcode insn opcode = getOpcode insn
funct3 = getFunct3 insn funct3 = getFunct3 insn
@ -170,7 +183,8 @@ getImm21JType instr = bitCoerce $ imm20 ++# imm10_1 ++# imm11 ++# imm19_12 ++# z
zero = 0 :: BitVector 1 -- LSB always zero for J-type zero = 0 :: BitVector 1 -- LSB always zero for J-type
decodeJType :: Insn -> Opcode decodeJType :: Insn -> Opcode
decodeJType insn = case getOpcode insn of decodeJType insn =
case opcode of
0b1101111 -> JAL (JTypeFields opcode rd imm21) -- JAL 0b1101111 -> JAL (JTypeFields opcode rd imm21) -- JAL
_ -> Unimplemented _ -> Unimplemented
where where

View file

@ -79,6 +79,6 @@ simulation args = do
InitializedPeripherals ram -> do InitializedPeripherals ram -> do
let initState = machineInit $ Machine.Peripherals ram let initState = machineInit $ Machine.Peripherals ram
sim <- simulationLoop 5 initState sim <- simulationLoop 15 initState
teardownPeripherals teardownPeripherals
return $ Success sim return $ Success sim