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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- This module describes the basic types and operations for our machine.
module Machine where
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as VM
import Prelude hiding (Word)
import Data.Word (Word32, Word16, Word8)
import Data.Bits (testBit)
import Data.IORef
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (MonadReader, ReaderT, ask)
import Control.Monad.Trans (MonadIO)
import Utils
-------------------------------------------------------------------------------
-- Base Types
type Long = Word32
type Word = Word16
type Byte = Word8
data Machine = Machine {
pc :: IORef Long,
sr :: IORef Word,
drs :: IORef (Long, Long, Long, Long, Long, Long, Long, Long),
ars :: IORef (Long, Long, Long, Long, Long, Long, Long),
usp :: IORef Long, -- this is a7 in user mode
ssp :: IORef Long, -- this is a7 in supermode
ram :: VM.IOVector Byte,
rom :: V.Vector Byte
}
-- Emulator is a monad which contains Machine and allows easy change of it.
newtype Emulator a = Emulator (ReaderT Machine IO a)
deriving (Monad, Applicative, Functor, MonadIO, MonadReader Machine)
with :: (Machine -> b) -> (b -> IO a) -> Emulator a
with field f = do
m <- ask
liftIO $ f (field m)
-------------------------------------------------------------------------------
-- Data and Address Registers Access
readD :: Int -> Emulator Long
readD 0 = with drs $ \rs -> do
(r,_,_,_,_,_,_,_) <- readIORef rs
return r
readD 1 = with drs $ \rs -> do
(_,r,_,_,_,_,_,_) <- readIORef rs
return r
readD 2 = with drs $ \rs -> do
(_,_,r,_,_,_,_,_) <- readIORef rs
return r
readD 3 = with drs $ \rs -> do
(_,_,_,r,_,_,_,_) <- readIORef rs
return r
readD 4 = with drs $ \rs -> do
(_,_,_,_,r,_,_,_) <- readIORef rs
return r
readD 5 = with drs $ \rs -> do
(_,_,_,_,_,r,_,_) <- readIORef rs
return r
readD 6 = with drs $ \rs -> do
(_,_,_,_,_,_,r,_) <- readIORef rs
return r
readD 7 = with drs $ \rs -> do
(_,_,_,_,_,_,_,r) <- readIORef rs
return r
readD _ = return $ error "Incorrect Data register read"
readA :: Int -> Emulator Long
readA 0 = with ars $ \rs -> do
(r,_,_,_,_,_,_) <- readIORef rs
return r
readA 1 = with ars $ \rs -> do
(_,r,_,_,_,_,_) <- readIORef rs
return r
readA 2 = with ars $ \rs -> do
(_,_,r,_,_,_,_) <- readIORef rs
return r
readA 3 = with ars $ \rs -> do
(_,_,_,r,_,_,_) <- readIORef rs
return r
readA 4 = with ars $ \rs -> do
(_,_,_,_,r,_,_) <- readIORef rs
return r
readA 5 = with ars $ \rs -> do
(_,_,_,_,_,r,_) <- readIORef rs
return r
readA 6 = with ars $ \rs -> do
(_,_,_,_,_,_,r) <- readIORef rs
return r
readA 7 = isSupervisor >>= \sup -> if sup
then with ssp $ \sp -> readIORef sp
else with usp $ \sp -> readIORef sp
readA _ = return $ error "Incorrect Address register read"
-------------------------------------------------------------------------------
-- Status Register Access
isTracing :: Emulator Bool
isTracing = with sr $ \sr -> do
sr <- readIORef sr
return $ testBit sr 0
isSupervisor :: Emulator Bool
isSupervisor = with sr $ \sr -> do
sr <- readIORef sr
return $ testBit sr 2
interruptLevel :: Emulator Int
interruptLevel = with sr $ \sr -> do
sr <- readIORef sr
return $ extractBits sr [5, 6, 7]
isExtend :: Emulator Bool
isExtend = with sr $ \sr -> do
sr <- readIORef sr
return $ testBit sr 11
isNegative :: Emulator Bool
isNegative = with sr $ \sr -> do
sr <- readIORef sr
return $ testBit sr 12
isZero :: Emulator Bool
isZero = with sr $ \sr -> do
sr <- readIORef sr
return $ testBit sr 13
isOverflow :: Emulator Bool
isOverflow = with sr $ \sr -> do
sr <- readIORef sr
return $ testBit sr 14
isCarry :: Emulator Bool
isCarry = with sr $ \sr -> do
sr <- readIORef sr
return $ testBit sr 15
-------------------------------------------------------------------------------
-- Memmory Access
getByte :: Long -> Emulator Byte
getByte a | a < 0x8 = with rom $ \rom -> return $ rom V.! fromIntegral a
| a < 0x7e0000 = with ram $ \ram ->
if VM.length ram >= fromIntegral a
then VM.unsafeRead ram (fromIntegral a)
else return 0xff
| a < 0x800000 = with rom $ \rom ->
return $ rom V.! (fromIntegral a - 0x7e0000)
| otherwise = return 0xff
-- TODO: only even addresses are allowed
getWord :: Long -> Emulator Word
getWord a = do
g <- getByte a
l <- getByte (a + 1)
return $ (fromIntegral g) * 256 + (fromIntegral l)
-- TODO: only even addresses are allowed
getLong :: Long -> Emulator Long
getLong a = do
g <- getWord a
l <- getWord (a + 2)
return $ (fromIntegral g) * 256 * 256 + (fromIntegral l)
setByte :: Long -> Byte -> Emulator ()
setByte a b | a < 0x8 = return ()
| a < 0x7e0000 = with ram $ \ram ->
VM.write ram (fromIntegral a) b
| otherwise = return ()
-- TODO: only even addresses are allowed
setWord :: Long -> Word -> Emulator ()
setWord a w = do
setByte a (fromIntegral (rem (fromIntegral w) 256))
setByte (a + 1) (fromIntegral (div (fromIntegral w) 256))
-- TODO: only even addresses are allowed
setLong :: Long -> Long -> Emulator ()
setLong a l = do
setWord a (fromIntegral (rem (fromIntegral l) 256 * 256))
setWord (a + 2) (fromIntegral (div (fromIntegral l) 256 * 256))
|