Load memory from list of words
This commit is contained in:
parent
2843cfd462
commit
d0f3985ff1
1 changed files with 19 additions and 0 deletions
|
|
@ -1,8 +1,13 @@
|
|||
module Mima.State
|
||||
( MimaMemory
|
||||
, wordsToMemory
|
||||
, readAt
|
||||
, writeAt
|
||||
, MimaState(..)
|
||||
, initialState
|
||||
, AbortReason(..)
|
||||
, ExecException(..)
|
||||
, step
|
||||
) where
|
||||
|
||||
import Data.Bits
|
||||
|
|
@ -13,6 +18,10 @@ import Mima.Instruction
|
|||
import Mima.Word
|
||||
|
||||
newtype MimaMemory = MimaMemory (Map.Map MimaAddress MimaWord)
|
||||
deriving (Show)
|
||||
|
||||
wordsToMemory :: [MimaWord] -> MimaMemory
|
||||
wordsToMemory = MimaMemory . Map.fromAscList . zip [minBound..]
|
||||
|
||||
readAt :: MimaAddress -> MimaMemory -> MimaWord
|
||||
readAt addr (MimaMemory m) = Map.findWithDefault zeroBits addr m
|
||||
|
|
@ -26,10 +35,20 @@ data MimaState = MimaState
|
|||
{ msIp :: !MimaAddress
|
||||
, msAcc :: !MimaWord
|
||||
, msMemory :: !MimaMemory
|
||||
} deriving (Show)
|
||||
|
||||
initialState :: MimaMemory -> MimaState
|
||||
initialState mem = MimaState
|
||||
{ msIp = minBound
|
||||
, msAcc = zeroBits
|
||||
, msMemory = mem
|
||||
}
|
||||
|
||||
data AbortReason = Halted | InvalidInstruction T.Text | InvalidNextIpAddress
|
||||
deriving (Show)
|
||||
|
||||
data ExecException = ExecException MimaAddress MimaWord AbortReason
|
||||
deriving (Show)
|
||||
|
||||
incrementIp :: MimaState -> Either ExecException MimaState
|
||||
incrementIp ms =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue