remove unused type
[wavedrum/sharc-disassembler.git] / SHARC / Types.hs
1 {-
2 This file is part of shark-disassembler.
3
4 Copyright (C) 2014 Ricardo Wurmus
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 -}
19
20 module SHARC.Types where
21
22 import SHARC.Word48
23
24 import Data.Word
25 import Data.Map (fromList, findWithDefault)
26 import Data.Bits ((.&.), (.|.), xor, testBit, shift, shiftR)
27 import Text.Printf (printf)
28
29 -- TODO: check other register assigments as well! They may need to be shifted first.
30
31
32 data Cond = Cond Word8
33 -- the 5 bit condition codes are at an odd position in the byte, so we
34 -- have to cut them out [ 00111110 ]
35 -- TODO: valid values go from 0 to 31, restrict!
36 mkCond :: Word48 -> Cond
37 mkCond w = Cond $ w64 `cutMask` 0x003E00000000
38 where
39 w64 = word48ToWord64 w
40
41 data TermCond = TermCond Word8
42 -- the 5 bit condition codes are at an odd position in the byte, so we
43 -- have to cut them out [ 00111110 ]
44 -- TODO: valid values go from 0 to 31, restrict!
45 mkTermCond :: Word48 -> TermCond
46 mkTermCond w = TermCond $ w64 `cutMask` 0x003E00000000
47 where
48 w64 = word48ToWord64 w
49
50 -- from table 1-4 (isr.pdf) "Conditional Execution Codes Summary"
51 condCodes = [
52 "EQ",
53 "NE",
54 "GT",
55 "LT",
56 "GE",
57 "LE",
58 "AC",
59 "NOT AC",
60 "AV",
61 "NOT AV",
62 "MV",
63 "NOT MV",
64 "MS",
65 "NOT MS",
66 "SV",
67 "NOT SV",
68 "SZ",
69 "NOT SZ",
70 "TF",
71 "NOT TF",
72 "FLAG0_IN",
73 "NOT FLAG0_IN",
74 "FLAG1_IN",
75 "NOT FLAG1_IN",
76 "FLAG2_IN",
77 "NOT FLAG2_IN",
78 "FLAG3_IN",
79 "NOT FLAG3_IN",
80 "BM",
81 "NOT BM",
82 "NOT ICE",
83 "TRUE" ]
84
85 condCodeMap = fromList $ zip [0..] condCodes
86 termCodeMap = fromList $ zip [0..] $ (init . init) condCodes ++ ["LCE", "FOREVER"]
87
88 instance Show Cond where
89 -- do not print if code is "TRUE" (31)
90 show (Cond n) = if n == 31 then ""
91 else "IF " ++ findWithDefault (printf "0x%02X" n) n condCodeMap ++ " "
92
93 instance Show TermCond where
94 show (TermCond n) = findWithDefault (printf "0x%02X" n) n termCodeMap
95
96
97 data Ireg = Ireg Word8
98 mkIreg :: Word8 -> Ireg
99 mkIreg n = Ireg $ n .&. 0x07
100 instance Show Ireg where
101 show (Ireg n) = printf "I%d" n
102
103 data Mreg = Mreg Word8
104 mkMreg :: Word8 -> Mreg
105 mkMreg n = Mreg $ n .&. 0x07
106 instance Show Mreg where
107 show (Mreg n) = printf "M%d" n
108
109 -- TODO: see table 1-8 and 1-11 in isr.pdf
110 -- 7 bit universal register
111 data Ureg = Ureg Word8
112 mkUreg :: Word8 -> Ureg
113 mkUreg n = Ureg $ n .&. 0xEF
114 instance Show Ureg where
115 show (Ureg n) = printf "0x%02X" n
116
117 -- 4 bit system register
118 data Sreg = Sreg Word8
119 mkSreg :: Word8 -> Sreg
120 mkSreg n = Sreg $ n .&. 0x0F
121 instance Show Sreg where
122 show (Sreg n) = printf "0x%01X" n
123
124 -- 4 bit data register
125 data Dreg = Dreg Word8
126 mkDreg :: Word8 -> Dreg
127 mkDreg n = Dreg $ n .&. 0x0F
128 instance Show Dreg where
129 show (Dreg n) = printf "R%d" n -- TODO: according to the docs this is R15-R0 *and* F15-F0
130
131 data Address24 = Address24 Word32 -- absolute 24 bit address
132 | RelAddress24 Word32 -- relative 24 bit address
133 data Address32 = Address32 Word32 -- absolute 32 bit address
134 | RelAddress32 Word32 -- relative 24 bit address
135
136 instance Show Address24 where
137 show (Address24 w) = printf "0x%06X" w
138 show (RelAddress24 w) = printf "(PC,%s)" w' -- two's complement
139 where w' = if w `testBit` 23 then '-' : show (w `xor` 0xFFFFFF) else show w
140
141 instance Show Address32 where
142 show (Address32 w) = printf "0x%08X" w
143 show (RelAddress32 w) = printf "(PC,%s)" w' -- two's complement
144 where w' = if w `testBit` 31 then '-' : show (w `xor` 0xFFFFFFFF) else show w
145
146
147 toWord32 :: (Word8, Word8, Word8, Word8) -> Word32
148 toWord32 (a,b,c,d) = fromIntegral a `shift` 24 .|.
149 fromIntegral b `shift` 16 .|.
150 fromIntegral c `shift` 8 .|.
151 fromIntegral d
152
153 data Update = PreModify -- 0, no update
154 | PostModify -- 1, with update
155
156 type PushPops = ([String], [String])
157
158 data Memory = Data -- data memory
159 | Prog -- programme memory
160 deriving Show
161 data AccessType = Read | Write -- memory acces
162 deriving Show
163 data WordAccess = NW | LW -- LW forces a long word access when address is in normal word address
164 deriving Show
165
166 data BranchType = Jump
167 | Call
168 deriving Show
169
170 data ReturnSource = Subroutine
171 | Interrupt
172 deriving Show
173
174 data BitOp = SET
175 | CLR
176 | TGL
177 | TST
178 | XOR
179 deriving Show