Print memory dump again

This commit is contained in:
Joscha 2019-11-07 16:46:21 +00:00
parent ef06f7b309
commit af70c1a02c
3 changed files with 63 additions and 7 deletions

View file

@ -1,11 +1,14 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module MimaRun where module MimaRun where
import Control.Monad import Control.Monad
import qualified Data.Text as T
import qualified Data.Text.IO as T import qualified Data.Text.IO as T
import Options.Applicative import Options.Applicative
import Mima.Instruction
import Mima.Load import Mima.Load
import Mima.State import Mima.State
import Mima.Util import Mima.Util
@ -68,6 +71,54 @@ runMima settings s =
Just e -> T.putStrLn $ toText e Just e -> T.putStrLn $ toText e
pure s' pure s'
dumpState :: Bool -> MimaState -> T.Text
dumpState sparse ms
= registerLegend
<> dumpRegisters ms
<> memoryLegend
<> dumpMemory sparse (msMemory ms)
<> footerLegend
showWord :: MimaWord -> T.Text
showWord w =
case wordToInstruction w of
Left _ -> wordToHexDec w
Right i -> wordToHexDec w <> ": " <> toText i
dumpRegisters :: MimaState -> T.Text
dumpRegisters MimaState{..}
= "IAR: " <> showAddressRegister msIAR <> " -> " <> showWord (readAt msIAR msMemory) <> "\n"
<> "ACC: " <> showWordRegister msACC <> "\n"
<> " RA: " <> showAddressRegister msRA <> " -> " <> showWord (readAt msRA msMemory) <> "\n"
<> " SP: " <> showAddressRegister msRA <> " -> " <> showWord (readAt msRA msMemory) <> "\n"
<> " FP: " <> showAddressRegister msRA <> " -> " <> showWord (readAt msRA msMemory) <> "\n"
where
showWordRegister w = wordToHex w <> " (" <> wordToDec w <> ")"
showAddressRegister lv =
" " <> largeValueToHex lv <> " ( " <> largeValueToDec lv <> ")"
registerLegend :: T.Text
registerLegend = "--------- Register -------------- Target word ---------------\n"
-- "IAR: 00000 ( 0) -> 800008 ( 8388616): JMP 8"
showMemoryLine :: MimaAddress -> MimaWord -> T.Text
showMemoryLine addr word = largeValueToHexDec addr <> " -> " <> showWord word <> "\n"
dumpMemory :: Bool -> MimaMemory -> T.Text
dumpMemory sparse mem =
let addresses = if sparse then sparseAddressRange mem else addressRange mem
memLines = map (\addr -> showMemoryLine addr $ readAt addr mem) addresses
in T.concat memLines
memoryLegend :: T.Text
memoryLegend = "--- Address ---------------- Word ---------------------------\n"
-- "00000 ( 0) -> 800008 ( 8388616): JMP 8"
-- "IAR: 00000 ( 0) -> 800008 ( 8388616): JMP 8"
footerLegend :: T.Text
footerLegend = "------------------------------------------------------\n"
-- "00000 ( 0) -> 800008 ( 8388616): JMP 8"
-- TODO exception handling -- TODO exception handling
main :: IO () main :: IO ()
main = do main = do
@ -80,10 +131,9 @@ main = do
s' <- if norun settings then pure s else runMima settings s s' <- if norun settings then pure s else runMima settings s
unless (quiet settings) $ do unless (quiet settings) $ do
putStrLn "\nDump of memory:" putStrLn ""
T.putStrLn $ "IP: " <> addrToHexDec (msIp s') <> " " putStrLn "Dump of MiMa state:"
<> "Acc: " <> wordToHexDec (msAcc s') T.putStr $ dumpState (sparse settings) s'
T.putStrLn $ memoryToText (sparse settings) (msMemory s')
putStrLn "" putStrLn ""
forM_ (memoryDump settings) $ \path -> do forM_ (memoryDump settings) $ \path -> do

View file

@ -84,7 +84,7 @@ instance ToText Instruction where
| otherwise = T.justifyLeft 4 ' ' (toText oc) <> " " <> smallValueToDec sv | otherwise = T.justifyLeft 4 ' ' (toText oc) <> " " <> smallValueToDec sv
wordToInstruction :: MimaWord -> Either T.Text Instruction wordToInstruction :: MimaWord -> Either T.Text Instruction
wordToInstruction mw = if getLargeOpcode mw == 0xF wordToInstruction mw = if getSmallOpcode mw == 0xF
then parseLargeInstruction mw then parseLargeInstruction mw
else parseSmallInstruction mw else parseSmallInstruction mw
@ -99,7 +99,7 @@ parseSmallOpcode :: Word32 -> Either T.Text SmallOpcode
parseSmallOpcode w = case smallOpcodeMap Map.!? w of parseSmallOpcode w = case smallOpcodeMap Map.!? w of
Just oc -> pure oc Just oc -> pure oc
Nothing -> Left $ "Unknown small opcode " <> T.pack (show w) Nothing -> Left $ "Unknown small opcode " <> T.pack (show w)
<> " (" <> toHex 2 w <> ")" <> " (" <> toHex 1 w <> ")"
parseLargeInstruction :: MimaWord -> Either T.Text Instruction parseLargeInstruction :: MimaWord -> Either T.Text Instruction
parseLargeInstruction mw = do parseLargeInstruction mw = do
@ -112,4 +112,4 @@ parseLargeOpcode :: Word32 -> Either T.Text LargeOpcode
parseLargeOpcode w = case largeOpcodeMap Map.!? w of 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)
<> " (" <> toHex 2 w <> ")" <> " (" <> toHex 1 w <> ")"

View file

@ -7,6 +7,9 @@ module Mima.State
MimaMemory MimaMemory
, readAt , readAt
, writeAt , writeAt
-- ** Querying
, addressRange
, sparseAddressRange
-- ** Converting -- ** Converting
, wordsToMemory , wordsToMemory
, memoryToWords , memoryToWords
@ -36,6 +39,9 @@ addressRange (MimaMemory m) =
Nothing -> [] Nothing -> []
Just maxAddr -> [minBound..maxAddr] Just maxAddr -> [minBound..maxAddr]
sparseAddressRange :: MimaMemory -> [MimaAddress]
sparseAddressRange (MimaMemory m) = Map.keys m
wordsToMemory :: [MimaWord] -> MimaMemory wordsToMemory :: [MimaWord] -> MimaMemory
wordsToMemory = MimaMemory wordsToMemory = MimaMemory
. Map.filter (/= zeroBits) . Map.filter (/= zeroBits)