Use OddWords library

This commit is contained in:
Joscha 2019-11-08 18:33:08 +00:00
parent 63a32ff01a
commit 112a49a7b7
5 changed files with 120 additions and 292 deletions

View file

@ -9,7 +9,6 @@ module Mima.Instruction
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import Data.Word
import Mima.Util
import Mima.Word
@ -25,25 +24,25 @@ allSmallOpcodes :: [SmallOpcode]
allSmallOpcodes = [LDC, LDV, STV, ADD, AND, OR, XOR, EQL,
JMP, JMN, LDIV, STIV, CALL, LDVR, STVR]
smallOpcodeToWord32 :: SmallOpcode -> Word32
smallOpcodeToWord32 LDC = 0
smallOpcodeToWord32 LDV = 1
smallOpcodeToWord32 STV = 2
smallOpcodeToWord32 ADD = 3
smallOpcodeToWord32 AND = 4
smallOpcodeToWord32 OR = 5
smallOpcodeToWord32 XOR = 6
smallOpcodeToWord32 EQL = 7
smallOpcodeToWord32 JMP = 8
smallOpcodeToWord32 JMN = 9
smallOpcodeToWord32 LDIV = 10
smallOpcodeToWord32 STIV = 11
smallOpcodeToWord32 CALL = 12
smallOpcodeToWord32 LDVR = 13
smallOpcodeToWord32 STVR = 14
smallOpcodeNr :: SmallOpcode -> Opcode
smallOpcodeNr LDC = 0
smallOpcodeNr LDV = 1
smallOpcodeNr STV = 2
smallOpcodeNr ADD = 3
smallOpcodeNr AND = 4
smallOpcodeNr OR = 5
smallOpcodeNr XOR = 6
smallOpcodeNr EQL = 7
smallOpcodeNr JMP = 8
smallOpcodeNr JMN = 9
smallOpcodeNr LDIV = 10
smallOpcodeNr STIV = 11
smallOpcodeNr CALL = 12
smallOpcodeNr LDVR = 13
smallOpcodeNr STVR = 14
smallOpcodeMap :: Map.Map Word32 SmallOpcode
smallOpcodeMap = Map.fromList [(smallOpcodeToWord32 so, so) | so <- allSmallOpcodes]
smallOpcodeMap :: Map.Map Opcode SmallOpcode
smallOpcodeMap = Map.fromList [(smallOpcodeNr so, so) | so <- allSmallOpcodes]
data LargeOpcode = HALT | NOT | RAR | RET | LDRA | STRA
| LDSP | STSP | LDFP | STFP | ADC
@ -55,22 +54,21 @@ instance ToText LargeOpcode where
allLargeOpcodes :: [LargeOpcode]
allLargeOpcodes = [HALT, NOT, RAR, RET, LDRA, STRA, LDSP, STSP, LDFP, STFP, ADC]
largeOpcodeToWord32 :: LargeOpcode -> Word32
largeOpcodeToWord32 HALT = 0
largeOpcodeToWord32 NOT = 1
largeOpcodeToWord32 RAR = 2
largeOpcodeToWord32 RET = 3
largeOpcodeNr :: LargeOpcode -> Opcode
largeOpcodeNr HALT = 0
largeOpcodeNr NOT = 1
largeOpcodeNr RAR = 2
largeOpcodeNr RET = 3
largeOpcodeNr LDRA = 4
largeOpcodeNr STRA = 5
largeOpcodeNr LDSP = 6
largeOpcodeNr STSP = 7
largeOpcodeNr LDFP = 8
largeOpcodeNr STFP = 9
largeOpcodeNr ADC = 10
largeOpcodeToWord32 LDRA = 4
largeOpcodeToWord32 STRA = 5
largeOpcodeToWord32 LDSP = 6
largeOpcodeToWord32 STSP = 7
largeOpcodeToWord32 LDFP = 8
largeOpcodeToWord32 STFP = 9
largeOpcodeToWord32 ADC = 10
largeOpcodeMap :: Map.Map Word32 LargeOpcode
largeOpcodeMap = Map.fromList [(largeOpcodeToWord32 lo, lo) | lo <- allLargeOpcodes]
largeOpcodeMap :: Map.Map Opcode LargeOpcode
largeOpcodeMap = Map.fromList [(largeOpcodeNr lo, lo) | lo <- allLargeOpcodes]
data Instruction
= SmallInstruction !SmallOpcode !LargeValue
@ -89,7 +87,7 @@ parseSmallInstruction mw = do
-- Assumes that all bits not part of the opcode are zeroed. The opcode
-- uses the lowest four bits.
parseSmallOpcode :: Word32 -> Either T.Text SmallOpcode
parseSmallOpcode :: Opcode -> Either T.Text SmallOpcode
parseSmallOpcode w = case smallOpcodeMap Map.!? w of
Just oc -> pure oc
Nothing -> Left $ "Unknown small opcode " <> T.pack (show w)
@ -102,7 +100,7 @@ parseLargeInstruction mw = do
-- Assumes that all bits not part of the opcode are zeroed. The opcode
-- uses the lowest four bits.
parseLargeOpcode :: Word32 -> Either T.Text LargeOpcode
parseLargeOpcode :: Opcode -> Either T.Text LargeOpcode
parseLargeOpcode w = case largeOpcodeMap Map.!? w of
Just oc -> pure oc
Nothing -> Left $ "Unknown large opcode " <> T.pack (show w)