Load memory from list of words

This commit is contained in:
Joscha 2019-11-06 10:02:23 +00:00
parent 2843cfd462
commit d0f3985ff1

View file

@ -1,8 +1,13 @@
module Mima.State module Mima.State
( MimaMemory ( MimaMemory
, wordsToMemory
, readAt , readAt
, writeAt , writeAt
, MimaState(..) , MimaState(..)
, initialState
, AbortReason(..)
, ExecException(..)
, step
) where ) where
import Data.Bits import Data.Bits
@ -13,6 +18,10 @@ import Mima.Instruction
import Mima.Word import Mima.Word
newtype MimaMemory = MimaMemory (Map.Map MimaAddress MimaWord) newtype MimaMemory = MimaMemory (Map.Map MimaAddress MimaWord)
deriving (Show)
wordsToMemory :: [MimaWord] -> MimaMemory
wordsToMemory = MimaMemory . Map.fromAscList . zip [minBound..]
readAt :: MimaAddress -> MimaMemory -> MimaWord readAt :: MimaAddress -> MimaMemory -> MimaWord
readAt addr (MimaMemory m) = Map.findWithDefault zeroBits addr m readAt addr (MimaMemory m) = Map.findWithDefault zeroBits addr m
@ -26,10 +35,20 @@ data MimaState = MimaState
{ msIp :: !MimaAddress { msIp :: !MimaAddress
, msAcc :: !MimaWord , msAcc :: !MimaWord
, msMemory :: !MimaMemory , msMemory :: !MimaMemory
} deriving (Show)
initialState :: MimaMemory -> MimaState
initialState mem = MimaState
{ msIp = minBound
, msAcc = zeroBits
, msMemory = mem
} }
data AbortReason = Halted | InvalidInstruction T.Text | InvalidNextIpAddress data AbortReason = Halted | InvalidInstruction T.Text | InvalidNextIpAddress
deriving (Show)
data ExecException = ExecException MimaAddress MimaWord AbortReason data ExecException = ExecException MimaAddress MimaWord AbortReason
deriving (Show)
incrementIp :: MimaState -> Either ExecException MimaState incrementIp :: MimaState -> Either ExecException MimaState
incrementIp ms = incrementIp ms =