now using bus and new FetchResult type

This commit is contained in:
Yehowshua Immanuel 2025-03-04 23:37:33 -05:00
parent 4729d79b23
commit eb79210863
3 changed files with 32 additions and 11 deletions

View file

@ -32,7 +32,7 @@ data RISCVCPU = RISCVCPU
riscvCPUInit :: RISCVCPU
riscvCPUInit =
RISCVCPU
{ pc = 0 -- 0x8000_0000
{ pc = 0x8000_0000
, gpr = gprInit
, fpr = fprInit
, csr = csrInit

View file

@ -3,7 +3,9 @@
module Fetch(
fetchInstruction,
FetchResult(..)) where
fetchInstruction1,
FetchResult(..),
FetchResult1(..)) where
import Clash.Prelude
import Types(Mem, Addr, Insn)

View file

@ -19,11 +19,12 @@ import Bus(Peripherals(..))
import Cpu(
RISCVCPU(..),
riscvCPUInit)
import Fetch(fetchInstruction, FetchResult (Instruction, Misaligned))
import Fetch(fetchInstruction, FetchResult (Instruction, Misaligned), fetchInstruction1, FetchResult1(..))
import Isa.Decode(decode)
import Debug.Trace
import Types (Insn)
import Control.Monad.RWS (MonadState(put))
data Args = Args {
firmware :: FilePath
@ -39,9 +40,6 @@ data Machine = Machine
}
deriving (Generic, Show, Eq, NFDataX)
-- machine :: Machine
-- machine = machineInit
machine' :: Machine -> Machine
machine' machine =
let
@ -63,12 +61,33 @@ machine' machine =
opcode = decode insn
Misaligned addr -> undefined
debugInsn :: FetchResult1 -> String
debugInsn fetchResult =
case fetchResult of
Instruction1 insn ->
"Decoded instruction: " P.++ show opcode
P.++ " | Binary: " P.++ binaryInsn
P.++ " (" P.++ show insn P.++ ")"
where
binaryInsn = show (bitCoerce insn :: BitVector 32)
opcode = decode insn
InstructionException e -> show e
simulationLoop :: Int -> Machine -> IO [Machine]
simulationLoop 0 state = return [state]
simulationLoop n state = do
let newState = machine' state
rest <- simulationLoop (n - 1) newState
return (state : rest)
simulationLoop 0 machine = return [machine]
simulationLoop n machine = do
-- let newState = machine' machine
let machinePeripherals = peripherals machine
currPc = pc $ cpu machine
fetchResult <- fetchInstruction1 machinePeripherals currPc
putStrLn $ debugInsn fetchResult
let pc' = currPc + 4
cpu' = (cpu machine) { pc = pc' }
machine' = machine { cpu = cpu' }
-- let machine' = machine { cpu = cpu $ machine { pc = pc $ cpu machine + 4 } }
rest <- simulationLoop (n - 1) machine'
return (machine : rest)
simulation :: Args -> IO Simulation
simulation args = do