Switch to Mima.Format formatting

This commit is contained in:
Joscha 2019-11-19 08:02:58 +00:00
parent 81fee29490
commit 4707e929ef
3 changed files with 8 additions and 52 deletions

View file

@ -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 ")"

View file

@ -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

View file

@ -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 ""