Add more conversions

This commit is contained in:
Joscha 2019-11-09 23:37:22 +00:00
parent 803c826395
commit 21e72420dd
3 changed files with 21 additions and 14 deletions

View file

@ -5,6 +5,7 @@ module Mima.Instruction
, LargeOpcode(..) , LargeOpcode(..)
, Instruction(..) , Instruction(..)
, wordToInstruction , wordToInstruction
, instructionToWord
) where ) where
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
@ -105,3 +106,7 @@ parseLargeOpcode w = case largeOpcodeMap Map.!? w of
Just oc -> pure oc Just oc -> pure oc
Nothing -> Left $ "Unknown large opcode " <> T.pack (show w) Nothing -> Left $ "Unknown large opcode " <> T.pack (show w)
<> " (" <> integralToHex 1 w <> ")" <> " (" <> integralToHex 1 w <> ")"
instructionToWord :: Instruction -> MimaWord
instructionToWord (SmallInstruction so lv) = wordFromSmallOpcode (smallOpcodeNr so) lv
instructionToWord (LargeInstruction lo sv) = wordFromLargeOpcode (largeOpcodeNr lo) sv

View file

@ -11,11 +11,12 @@ module Mima.State
, addressRange , addressRange
, sparseAddressRange , sparseAddressRange
-- ** Converting -- ** Converting
, mapToMemory
, wordsToMemory , wordsToMemory
, memoryToWords , memoryToWords
-- * State -- * State
, MimaState(..) , MimaState(..)
, initialState , basicState
, AbortReason(..) , AbortReason(..)
, step , step
, run , run
@ -42,9 +43,11 @@ addressRange (MimaMemory m) =
sparseAddressRange :: MimaMemory -> [MimaAddress] sparseAddressRange :: MimaMemory -> [MimaAddress]
sparseAddressRange (MimaMemory m) = Map.keys m sparseAddressRange (MimaMemory m) = Map.keys m
mapToMemory :: Map.Map MimaAddress MimaWord -> MimaMemory
mapToMemory = MimaMemory . Map.filter (/= zeroBits)
wordsToMemory :: [MimaWord] -> MimaMemory wordsToMemory :: [MimaWord] -> MimaMemory
wordsToMemory = MimaMemory wordsToMemory = mapToMemory
. Map.filter (/= zeroBits)
. Map.fromAscList . Map.fromAscList
. zip [minBound..] . zip [minBound..]
@ -68,17 +71,8 @@ data MimaState = MimaState
, msMemory :: !MimaMemory , msMemory :: !MimaMemory
} deriving (Show) } deriving (Show)
-- | A possible initial MiMa state, where every register is basicState :: MimaMemory -> MimaState
-- zeroed. Thus, execution starts at address 0x00000. basicState = MimaState zeroBits zeroBits zeroBits zeroBits zeroBits
initialState :: MimaMemory -> MimaState
initialState mem = MimaState
{ msIAR = zeroBits
, msACC = zeroBits
, msRA = zeroBits
, msSP = zeroBits
, msFP = zeroBits
, msMemory = mem
}
data AbortReason = Halted | InvalidInstruction T.Text | InvalidNextIarAddress data AbortReason = Halted | InvalidInstruction T.Text | InvalidNextIarAddress
deriving (Show) deriving (Show)

View file

@ -13,6 +13,8 @@ module Mima.Word
, boolToWord , boolToWord
, largeValueToWord , largeValueToWord
, signedSmallValueToWord , signedSmallValueToWord
, wordFromSmallOpcode
, wordFromLargeOpcode
-- ** 'MimaWord' properties -- ** 'MimaWord' properties
, getSmallOpcode , getSmallOpcode
, getLargeOpcode , getLargeOpcode
@ -59,6 +61,12 @@ signedSmallValueToWord sv
| topBit sv = 0xFF0000 .|. fromIntegral sv | topBit sv = 0xFF0000 .|. fromIntegral sv
| otherwise = fromIntegral sv | otherwise = fromIntegral sv
wordFromSmallOpcode :: Opcode -> LargeValue -> MimaWord
wordFromSmallOpcode so lv = shiftL (fromIntegral so) 20 .|. fromIntegral lv
wordFromLargeOpcode :: Opcode -> SmallValue -> MimaWord
wordFromLargeOpcode lo sv = 0xF00000 .|. shiftL (fromIntegral lo) 16 .|. fromIntegral sv
getSmallOpcode :: MimaWord -> Opcode getSmallOpcode :: MimaWord -> Opcode
getSmallOpcode mw = fromIntegral $ shiftR mw 20 getSmallOpcode mw = fromIntegral $ shiftR mw 20