From 0cdd25d921202030745069dbccd16ef856e16750 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Sun, 14 Feb 2021 21:01:33 +0300 Subject: Example commands added. --- src/Suem.hs | 84 +++++++++++++++++++++++-------------------------------------- 1 file changed, 32 insertions(+), 52 deletions(-) (limited to 'src/Suem.hs') diff --git a/src/Suem.hs b/src/Suem.hs index 0602196..3ca635b 100644 --- a/src/Suem.hs +++ b/src/Suem.hs @@ -4,37 +4,8 @@ import qualified Data.Vector.Unboxed as V import qualified Data.ByteString as B import Data.Word import Data.Bits - - -data Registers = Registers { - pc :: Word32, - sr :: Word16, - drs :: [Word32], -- d0 to d7 - ars :: [Word32], -- a0 to a6 - usp :: Word32, -- this is a7 in user mode - ssp :: Word32 -- this is a7 in supermode -} - -data Machine = Machine { - regs :: Registers, - ram :: V.Vector Word8, - rom :: V.Vector Word8 -} - -getByte :: Machine -> Int -> Word8 -getByte m a | a < 0x8 = rom m V.! a - | a < 0x7e0000 = if V.length (ram m) >= a then ram m V.! a - else 0xff - | a < 0x800000 = rom m V.! (a - 0x7e0000) - | otherwise = 0xff - -getWord :: Machine -> Int -> Word16 -- TODO: only even addresses are allowed -getWord m a = (fromIntegral $ getByte m a) * 256 + - (fromIntegral $ getByte m (a + 1)) - -getLong :: Machine -> Int -> Word32 -- TODO: only even addresses are allowed -getLong m a = (fromIntegral $ getWord m a) * 256 * 256 + - (fromIntegral $ getWord m (a + 2)) +import Machine +import Commands data ConfigSocket = ConfigInet String | ConfigUnix String @@ -51,31 +22,40 @@ data Config = Config Int -- frequence (Maybe ConfigSocket) (Maybe ConfigSocket) -doCommand :: Word16 -> Machine -> Machine -doCommand cmd m = case cmd .&. 0xf000 of - 0 -> if testBit cmd 7 - then let rega = (shiftR cmd 9) .&. 0x7 in - m - else m - 0x1000 -> m - 0x2000 -> m - 0x3000 -> m - 0x4000 -> m - 0x5000 -> m - 0x6000 -> m - 0x7000 -> m - 0x8000 -> m - 0x9000 -> m - 0xb000 -> m - 0xc000 -> m - 0xd000 -> m - 0xe000 -> m - _ -> error "Bad command" +boolToInt :: Bool -> Int +boolToInt True = 1 +boolToInt False = 0 + +toBits :: Word16 -> [Int] +toBits x = map (boolToInt . testBit x) [0..(finiteBitSize x-1)] + +fromBits :: [Int] -> Int +fromBits = foldl (\a b -> 2 * a + b) 0 . reverse +doCommand :: [Int] -> Machine -> Machine +--doCommand [0,1,0,0,1,1,1,0, 0,1,1,0,0,0,0,0] = doReset +doCommand [0,1,0,0,1,1,1,0, 0,1,1,0,0,0,0,1] = doNothing +--doCommand [0,1,0,0,1,1,1,0, 0,1,1,0,0,0,1,0] = doStop +--doCommand [0,1,0,0,1,1,1,0, 0,1,1,0,0,0,1,1] = doRTE +--doCommand [0,1,0,0,1,1,1,0, 0,1,1,0,0,1,0,1] = doRTS +--doCommand [0,1,0,0,1,1,1,0, 0,1,1,0,0,1,1,0] = doTrapV +--doCommand [0,1,0,0,1,1,1,0, 0,1,1,0,0,1,1,1] = doRTR +--doCommand [0,1,0,0,1,0,1,0, 1,1,1,1,1,1,0,0] = doIllegal +--doCommand [0,1,0,0,1,0,1,0, 1,1,a,b,c,d,e,f] = +-- doTAS (fromBits [a,b,c]) (fromBits [d,e,f]) +--doCommand [0,1,0,0,1,0,1,0, a,b,c,d,e,f,g,h] = +-- doTST (fromBits [a,b]) (fromBits [c,d,e]) (fromBits [f,g,h]) +--doCommand [0,1,0,0,1,0,1,0, 0,1,0,0,a,b,c,d] = +-- doTrap (fromBits [a,b,c,d]) +--doCommand [0,1,0,0,1,0,1,0, 0,1,0,1,0,a,b,c] = +-- doLink (fromBits [a,b,c]) +doCommand [0,1,0,0,1,0,1,0, 0,1,0,1,1,a,b,c] = + doUnlink (fromBits [a,b,c]) +doCommand _ = error "Bad command." runMachine :: Machine -> IO () runMachine m = do - runMachine $ doCommand (getWord m $ fromIntegral $ pc $ regs m) m + runMachine $ doCommand (toBits $ getWord m $ fromIntegral $ pc $ regs m) m makeMachine :: V.Vector Word8 -> Int -> Machine makeMachine romData ramSize = Machine rs rd romData -- cgit v1.2.3