From 4707e929ef34488619e604293d29a49f34cf566e Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 19 Nov 2019 08:02:58 +0000 Subject: [PATCH] Switch to Mima.Format formatting --- app/MimaRun/PrintState.hs | 5 +++-- src/Mima/Instruction.hs | 9 ++++---- src/Mima/Util.hs | 46 --------------------------------------- 3 files changed, 8 insertions(+), 52 deletions(-) diff --git a/app/MimaRun/PrintState.hs b/app/MimaRun/PrintState.hs index be0b573..40aadc6 100644 --- a/app/MimaRun/PrintState.hs +++ b/app/MimaRun/PrintState.hs @@ -10,6 +10,7 @@ import qualified Data.Text as T import qualified Data.Text.IO as T import System.Console.ANSI +import Mima.Format.Common import Mima.Instruction import Mima.State import Mima.Util @@ -17,14 +18,14 @@ import Mima.Word printAddress :: Int -> MimaAddress -> IO () printAddress n addr = do - T.putStr $ toHexBytes addr + T.putStr $ toHex addr putStr " (" T.putStr $ T.justifyRight n ' ' $ toDec addr putStr ")" printWord :: Int -> MimaWord -> IO () printWord n word = do - T.putStr $ toHexBytes word + T.putStr $ toHex word putStr " (" T.putStr $ T.justifyRight n ' ' $ toDec word putStr ")" diff --git a/src/Mima/Instruction.hs b/src/Mima/Instruction.hs index e5521d0..dffd85e 100644 --- a/src/Mima/Instruction.hs +++ b/src/Mima/Instruction.hs @@ -11,6 +11,7 @@ module Mima.Instruction import qualified Data.Map.Strict as Map import qualified Data.Text as T +import Mima.Format.Common import Mima.Util import Mima.Word @@ -94,8 +95,8 @@ parseSmallInstruction mw = do 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) - <> " (" <> integralToHex 1 w <> ")" + Nothing -> Left $ "Unknown small opcode " <> toDec w <> " (" <> (fixWidthHex 1 $ toHex w) + <> ", " <> (fixWidthBin 4 $ toBin w) <> ")" parseLargeInstruction :: MimaWord -> Either T.Text Instruction parseLargeInstruction mw = do @@ -107,8 +108,8 @@ parseLargeInstruction mw = do 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) - <> " (" <> integralToHex 1 w <> ")" + Nothing -> Left $ "Unknown large opcode " <> toDec w <> " (" <> (fixWidthHex 1 $ toHex w) + <> ", " <> (fixWidthBin 4 $ toBin w) <> ")" instructionToWord :: Instruction -> MimaWord instructionToWord (SmallInstruction so lv) = wordFromSmallOpcode (smallOpcodeNr so) lv diff --git a/src/Mima/Util.hs b/src/Mima/Util.hs index e896009..551fd43 100644 --- a/src/Mima/Util.hs +++ b/src/Mima/Util.hs @@ -6,16 +6,9 @@ module Mima.Util ( -- * Formatting ToText(..) - , HexLike(..) - , groupByTwoChars - , integralToDec - , integralToHex ) where import qualified Data.Text as T -import Data.Word -import Data.Word.Odd -import qualified Numeric as N {- Formatting -} @@ -31,42 +24,3 @@ import qualified Numeric as N -- instead name the functions individually. class ToText a where toText :: a -> T.Text - --- | A class for number-like types that have a decimal and a --- hexadecimal representation. -class HexLike a where - toDec :: a -> T.Text - toHex :: a -> T.Text - - toHexBytes :: a -> T.Text - toHexBytes = T.intercalate " " . groupByTwoChars . toHex - -instance HexLike Word24 where - toHex = integralToHex 6 - toDec = T.pack . show - -instance HexLike Word20 where - toHex = integralToHex 5 - toDec = T.pack . show - -instance HexLike Word16 where - toHex = integralToHex 4 - toDec = T.pack . show - -instance HexLike Word4 where - toHex = integralToHex 1 - toDec = T.pack . show - - -groupByTwoChars :: T.Text -> [T.Text] -groupByTwoChars = reverse . helper . T.unpack . T.reverse - where - helper (c1:c2:cs) = T.pack [c2, c1] : helper cs - helper [c] = [T.singleton c] - helper [] = [] - -integralToDec :: (Integral a, Show a) => Int -> a -> T.Text -integralToDec digits a = T.justifyRight digits ' ' $ T.pack $ show a - -integralToHex :: (Integral a, Show a) => Int -> a -> T.Text -integralToHex digits a = T.justifyRight digits '0' $ T.pack $ N.showHex a ""