-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleAsmArchitecture.hs
65 lines (49 loc) · 1.41 KB
/
SimpleAsmArchitecture.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module SimpleAsmArchitecture(
Word,
word,
int,
word_size,
op_size,
obj_size,
wordJoin,
wordSplit
) where
word_size = op_size + obj_size
op_size = 2
obj_size = 3
complement = 10 ^ word_size
min_word = complement `div` 2
max_word = min_word - 1
newtype Word = Word { runWord :: Int } deriving (Eq)
tensComplement x = complement - x
word :: Int -> Word
word x = if x < 0
then Word . tensComplement . trunc . abs $ x
else Word . trunc $ x
where trunc = (`mod` complement)
int (Word x) = if x < min_word
then x
else negate . tensComplement $ x
wordJoin :: Int -> Int -> Word
wordJoin op obj = Word $ (abs op) * 10 ^ obj_size + (abs obj) `mod` 10^ obj_size
wordSplit :: Word -> (Int,Int)
wordSplit (Word x) = (x `div` 10 ^ obj_size, x `mod` 10 ^ obj_size)
inspect (Word x) = "Word { runWord = " ++ (show x) ++ " }"
instance Show Word where
show (Word x) = frmt $ show x
instance Num Word where
w1 + w2 = word $ (int w1) + (int w2)
w1 * w2 = word $ (int w1) * (int w2)
negate = word . negate . int
abs = word . abs . int
signum = word . signum . int
fromInteger = word . fromIntegral
instance Ord Word where
w1 `compare` w2 = int w1 `compare` int w2
frmt :: String -> String
frmt x
| len > word_size = drop (len - word_size) x
| len < word_size = pad word_size '0' x
| otherwise = x
where len = length x
pad p c x = replicate (p - length x) c ++ x