3a_asm: .ascii directive
[calu.git] / 3a_asm / DTFormat.hs
index 3a43eb8b854487ca6b4c18dbd13af4c44b0f5566..17b2f014c8bc737dab587f75dc2bfbcabff301ce 100644 (file)
@@ -1,6 +1,8 @@
 module DTFormat where
 
+import Numeric
 import Data.Word
+import Data.Char
 import Text.Printf
 import Text.Parsec
 import Text.Parsec.String
@@ -19,6 +21,7 @@ type ValueToParse = String
 type Code = String
 type Label = String
 type Comment = String
+type Ascii = String
 
 data DTF =
        DTF_Data Address Value Code Label Comment | -- 0;...
@@ -31,6 +34,7 @@ data DTF =
        DTF_Org Address |
        DTF_Define Label Value Comment |
        DTF_Fill Repeat Value Label Code Comment |
+       DTF_Ascii String Label Code Comment |
        DTF_State DT_State
 
 instance Show (DTF) where
@@ -46,11 +50,22 @@ showsDTF (DTF_SectionToDet a v c l s) = (++) (datins "std" a v c l s)
 showsDTF (DTF_Org a) = (++) (printf "org;%08x\n" a)
 showsDTF (DTF_Define l a c) = (++) (printf "def;%s;%08x;%s\n" l a c)
 showsDTF (DTF_Fill r v code l c) = (++) (printf "fill;%08x;%08x;%s;%s;%s\n" r v code l c)
+showsDTF (DTF_Ascii str l code c) = (++) (printf "ascii;%s;%s;%s;%s" str code l c)
 showsDTF (DTF_State s) = (++) (printf "sta;%s\n" (show s))
 
 datins :: String -> Address -> Value -> Code -> Label -> Comment -> String
 datins = printf "%s;%08x;%08x;%s;%s;%s\n"
 
+tobin :: Value -> String
+tobin v = reverse $ take 32 $ reverse $ (['0' | _<-[0..32]]) ++ (showIntAtBase 2 (chr. ((+)0x30)) v "")
+
+showsDTFBase :: DTF -> Int -> ShowS
+showsDTFBase (DTF_Data a v c l s) 2 = (++) (datinsBin "0" a (tobin v) c l s)
+showsDTFBase (DTF_Instr a v c l s) 2 = (++) (datinsBin "1" a (tobin v) c l s)
+showsDTFBase d _ = showsDTF d
+
+datinsBin :: String -> Address -> String -> Code -> Label -> Comment -> String
+datinsBin = printf "%s;%08x;%s;%s;%s;%s\n"
 
 -- datastructure for managing labels and defines
 type Dict = (Address,[DictElem])