8b85ca1cca9c0656e4155bd0b6c391b4600b6db6
[calu.git] / 3a_asm / DTFormat.hs
1 module DTFormat where
2
3 import Numeric
4 import Data.Word
5 import Data.Char
6 import Text.Printf
7 import Text.Parsec
8 import Text.Parsec.String
9 import Text.Parsec.Combinator
10 import Text.Parsec.Expr
11 import Text.ParserCombinators.Parsec.Token
12 import Text.ParserCombinators.Parsec.Expr
13 import Control.Monad
14
15 data DT_State = NoState | InData | InText deriving (Show,Eq)
16
17 type Address = Word32
18 type Value = Word32
19 type Repeat = Word32
20 type ValueToParse = String
21 type Code = String
22 type Label = String
23 type Comment = String
24
25 data DTF =
26         DTF_Data Address Value Code Label Comment | -- 0;...
27         DTF_Instr Address Value Code Label Comment | -- 1;...
28         DTF_Comment Comment | -- 2;...
29         DTF_Label Label Comment Address | -- 3;...
30         -- types for intern processing
31         DTF_InstrToParse Address ValueToParse Code Label Comment |
32         DTF_SectionToDet Address Value Code Label Comment |
33         DTF_Org Address |
34         DTF_Define Label Value Comment |
35         DTF_Fill Repeat Value Label Code Comment |
36         DTF_State DT_State
37
38 instance Show (DTF) where
39         showsPrec n = showsDTF
40
41 showsDTF :: DTF -> ShowS
42 showsDTF (DTF_Data a v c l s) = (++) (datins "0" a v c l s)
43 showsDTF (DTF_Instr a v c l s) = (++) (datins "1" a v c l s)
44 showsDTF (DTF_Comment c) = (++) (printf "2;%s\n" c)
45 showsDTF (DTF_Label l c _) = (++) (printf "3;%s;%s\n" l c)
46 showsDTF (DTF_InstrToParse a v c l s) = (++) (printf "itp;%08x;%s;%s;%s;%s\n" a v c l s)
47 showsDTF (DTF_SectionToDet a v c l s) = (++) (datins "std" a v c l s)
48 showsDTF (DTF_Org a) = (++) (printf "org;%08x\n" a)
49 showsDTF (DTF_Define l a c) = (++) (printf "def;%s;%08x;%s\n" l a c)
50 showsDTF (DTF_Fill r v code l c) = (++) (printf "fill;%08x;%08x;%s;%s;%s\n" r v code l c)
51 showsDTF (DTF_State s) = (++) (printf "sta;%s\n" (show s))
52
53 datins :: String -> Address -> Value -> Code -> Label -> Comment -> String
54 datins = printf "%s;%08x;%08x;%s;%s;%s\n"
55
56 tobin :: Value -> String
57 tobin v = reverse $ take 32 $ reverse $ (['0' | _<-[0..32]]) ++ (showIntAtBase 2 (chr. ((+)0x30)) v "")
58
59 showsDTFBase :: DTF -> Int -> ShowS
60 showsDTFBase (DTF_Data a v c l s) 2 = (++) (datinsBin "0" a (tobin v) c l s)
61 showsDTFBase (DTF_Instr a v c l s) 2 = (++) (datinsBin "1" a (tobin v) c l s)
62 showsDTFBase d _ = showsDTF d
63
64 datinsBin :: String -> Address -> String -> Code -> Label -> Comment -> String
65 datinsBin = printf "%s;%08x;%s;%s;%s;%s\n"
66
67 -- datastructure for managing labels and defines
68 type Dict = (Address,[DictElem])
69 type DictElem = (String,Word32)
70
71 get_elem :: String -> [DictElem] -> Word32
72 get_elem s dict = case (lookup s dict) of
73                 Nothing -> error ("unknown label or define: \"" ++ s ++ "\"")
74                 Just z -> z
75
76 add_elem :: [DictElem] -> (String,Word32) -> [DictElem]
77 add_elem dic (s,w)
78         | s == "" = dic -- ignore empty string
79         | already_in =  error ("Label or define \"" ++ s ++ "\" already exists")
80         | otherwise = (s,w):dic
81         where
82         already_in = case (lookup s dic) of
83                 Just _ -> True
84                 Nothing -> False
85
86 -- some common functions
87 parseMySpaces :: Parser String
88 parseMySpaces = do
89         ret <- many $ oneOf "\t "
90         return $ ret