-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCPU.hs
422 lines (410 loc) · 14.5 KB
/
DCPU.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
{-# LANGUAGE TupleSections #-}
module DCPU where
import DCPU.Types
import Control.Applicative
import Control.Exception
import Control.Lens hiding ((<|), op)
import Control.Monad.State.Strict hiding (mapM_)
import Data.Bits
import Data.Foldable hiding (elem)
import Data.Int
import Data.Monoid
import Data.Sequence
import Data.Word
import System.CPUTime
import Prelude hiding (length, mapM_)
memPartA :: Word16 -> DCPUM Word16
memPartA n | n <= 0x07 = use . dcpuLens . Left . toEnum $ fromIntegral n
| n <= 0x0f = do
addr <- use . dcpuLens . Left . toEnum $ fromIntegral (n - 8)
use . dcpuLens $ Right addr
| n <= 0x17 = do
offset <- memPartA 0x1f
addr <- use . dcpuLens . Left . toEnum $ fromIntegral (n - 16)
use . dcpuLens $ Right (addr + offset)
| n == 0x18 = do
sp <- use . dcpuLens $ Left SP
dcpuLens (Left SP) += 1
use . dcpuLens $ Right sp
| n == 0x19 = do
sp <- use . dcpuLens $ Left SP
use . dcpuLens $ Right sp
| n == 0x1a = do
offset <- memPartA 0x1f
sp <- use . dcpuLens $ Left SP
use . dcpuLens $ Right (sp + offset)
| n == 0x1b = use . dcpuLens $ Left SP
| n == 0x1c = use . dcpuLens $ Left PC
| n == 0x1d = use . dcpuLens $ Left EX
| n == 0x1e = do
addr <- memPartA 0x1f
use . dcpuLens $ Right addr
| n == 0x1f = do
lift $ modify (+1)
pc <- use . dcpuLens $ Left PC
dcpuLens (Left PC) += 1
use . dcpuLens $ Right pc
| otherwise = return (n - 33)
memPartB :: Word16 -> DCPUM (Word16, Either RegCode Word16)
memPartB n | n <= 0x07 = let rc = Left . toEnum $ fromIntegral n in (, rc) <$> use (dcpuLens rc)
| n <= 0x0f = do
addr <- use . dcpuLens . Left . toEnum $ fromIntegral (n - 8)
uses (dcpuLens $ Right addr) (, Right addr)
| n <= 0x17 = do
offset <- memPartA 0x1f
addr <- use . dcpuLens . Left . toEnum $ fromIntegral (n - 16)
uses (dcpuLens . Right $ addr + offset) (, Right (addr + offset))
| n == 0x18 = do
dcpuLens (Left SP) -= 1
sp <- use . dcpuLens $ Left SP
uses (dcpuLens $ Right sp) (, Right sp)
| n == 0x19 = do
sp <- use . dcpuLens $ Left SP
uses (dcpuLens $ Right sp) (, Right sp)
| n == 0x1a = do
offset <- memPartA 0x1f
sp <- use . dcpuLens $ Left SP
uses (dcpuLens $ Right (sp + offset)) (, Right (sp + offset))
| n == 0x1b = (, Left SP) <$> use (dcpuLens $ Left SP)
| n == 0x1c = (, Left PC) <$> use (dcpuLens $ Left PC)
| n == 0x1d = (, Left EX) <$> use (dcpuLens $ Left EX)
| n == 0x1e = do
addr <- memPartA 0x1f
uses (dcpuLens $ Right addr) (, Right addr)
| n == 0x1f = do
pc <- memPartA 0x1f
uses (dcpuLens $ Right pc) (, Right pc)
| otherwise = return (n - 33, Left N)
sizeOfInstruction :: DCPUM Word16
sizeOfInstruction = do
pc <- use . dcpuLens $ Left PC
op <- use . dcpuLens $ Right pc
let f = (`elem` ([0x10..0x17] ++ [0x1a, 0x1e, 0x1f]))
o = op .&. 0x1F
a = f $ (op `shiftR` 5) .&. 0x1F
b = f $ op `shiftR` 10
return (1 + (if a then 1 else 0) + (if b && o /= 0 then 1 else 0))
interrupt :: Word16 -> DCPUM ()
interrupt msg = do
q <- use dcpuIsInterruptQueueing
if q
then dcpuInterruptQueue %= (msg <|)
else do
ia <- use . dcpuLens $ Left IA
unless (ia == 0) $ do
dcpuIsInterruptQueueing .= True
pc <- use . dcpuLens $ Left PC
a <- use . dcpuLens $ Left A
dcpuLens (Left SP) -= 1
sp1 <- use . dcpuLens $ Left SP
dcpuLens (Right sp1) .= pc
dcpuLens (Left SP) -= 1
sp2 <- use . dcpuLens $ Left SP
dcpuLens (Right sp2) .= a
dcpuLens (Left PC) .= ia
dcpuLens (Left A) .= msg
step :: DCPUM ()
step = do
timeToPause <- lift get
ago <- liftIO getCPUTime
let loop = do
now <- liftIO getCPUTime
if ago + timeToPause * 10 ^ 9 > now
then loop
else return ()
loop
lift $ put 0
pc <- use . dcpuLens $ Left PC
dcpuLens (Left PC) += 1
op <- use . dcpuLens $ Right pc
let opcode = op .&. 0x1F
bcode = (op `shiftR` 5) .&. 0x1F
acode = op `shiftR` 10
case opcode of
0x00 -> case bcode of
0x00 -> liftIO . throwIO $ DCPUUndefinedOperation opcode -- reserved for future expansion
0x01 -> do
lift $ modify (+ 3)
a <- memPartA acode
pc1 <- use . dcpuLens $ Left PC
dcpuLens (Left SP) -= 1
sp <- use . dcpuLens $ Left SP
dcpuLens (Right sp) .= pc1
dcpuLens (Left PC) .= a
0x02 -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x03 -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x04 -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x05 -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x06 -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x07 -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x08 -> do
lift $ modify (+ 4)
a <- memPartA acode
interrupt a
0x09 -> do
lift $ modify (+ 1)
(_, addr) <- memPartB acode
ia <- use . dcpuLens $ Left IA
dcpuLens addr .= ia
0x0a -> do
lift $ modify (+ 1)
a <- memPartA acode
dcpuLens (Left IA) .= a
0x0b -> do
lift $ modify (+ 1)
dcpuIsInterruptQueueing .= False
sp1 <- use . dcpuLens $ Left SP
dcpuLens (Left SP) += 1
a <- use . dcpuLens $ Right sp1
dcpuLens (Left A) .= a
sp2 <- use . dcpuLens $ Left SP
dcpuLens (Left SP) += 1
pc1 <- use . dcpuLens $ Right sp2
dcpuLens (Left PC) .= pc1
0x0c -> do
lift $ modify (+ 2)
a <- memPartA acode
dcpuIsInterruptQueueing .= (a /= 0)
0x0d -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x0e -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x0f -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x10 -> do
lift $ modify (+ 2)
noOfConnectedDevices <- uses dcpuHardware $ fromIntegral . length
dcpuLens (Left A) .= noOfConnectedDevices
0x11 -> do
lift $ modify (+ 4)
a <- memPartA acode
thisDevice <- preuse $ dcpuHardware . ix (fromIntegral a)
case thisDevice of
Nothing -> do
dcpuLens (Left A) .= 0
dcpuLens (Left B) .= 0
dcpuLens (Left C) .= 0
dcpuLens (Left X) .= 0
dcpuLens (Left Y) .= 0
Just device -> do
dcpuLens (Left B) .= fromIntegral (hardwareID device `shiftR` 16)
dcpuLens (Left A) .= fromIntegral (hardwareID device)
dcpuLens (Left C) .= hardwareVersion device
dcpuLens (Left Y) .= fromIntegral (hardwareManufacturer device `shiftR` 16)
dcpuLens (Left X) .= fromIntegral (hardwareManufacturer device)
0x12 -> do
lift $ modify (+ 4)
a <- memPartA acode
thisDevice <- preuse $ dcpuHardware . ix (fromIntegral a)
case thisDevice of
Nothing -> return ()
Just device -> hardwareInterrupt device
_ -> liftIO . throwIO $ DCPUUndefinedOperation opcode
0x01 -> do
lift $ modify (+ 1)
a <- memPartA acode
(_, addr) <- memPartB bcode
dcpuLens addr .= a
0x02 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= a + b
dcpuLens (Left EX) .= if (a + b) < a then 1 else 0
0x03 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= b - a
dcpuLens (Left EX) .= if a > b then -1 else 0
0x04 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= b * a
let a', b' :: Word32
a' = fromIntegral a
b' = fromIntegral b
dcpuLens (Left EX) .= fromIntegral ((a' * b') `shiftR` 16)
0x05 -> do
lift $ modify (+ 2)
a <- fromIntegral <$> memPartA acode :: DCPUM Int16
(b, addr) <- over _1 fromIntegral <$> memPartB bcode :: DCPUM (Int16, Either RegCode Word16)
dcpuLens addr .= fromIntegral (b * a)
let a', b' :: Int32
a' = fromIntegral a
b' = fromIntegral b
dcpuLens (Left EX) .= fromIntegral ((a' * b') `shiftR` 16)
0x06 -> do
lift $ modify (+ 3)
a <- memPartA acode
(b, addr) <- memPartB bcode
case a of
0 -> do
dcpuLens addr .= 0
dcpuLens (Left EX) .= 0
_ -> do
dcpuLens addr .= b `div` a
let a', b' :: Word32
a' = fromIntegral a
b' = fromIntegral b
dcpuLens (Left EX) .= fromIntegral (b' `shiftL` 16 `div` a')
0x07 -> do
lift $ modify (+ 3)
a <- fromIntegral <$> memPartA acode :: DCPUM Int16
(b, addr) <- over _1 fromIntegral <$> memPartB bcode :: DCPUM (Int16, Either RegCode Word16)
case a of
0 -> do
dcpuLens addr .= 0
dcpuLens (Left EX) .= 0
_ -> do
dcpuLens addr .= fromIntegral (b `div` a)
let a', b' :: Int32
a' = fromIntegral a
b' = fromIntegral b
dcpuLens (Left EX) .= fromIntegral (b' `shiftL` 16 `div` a')
0x08 -> do
lift $ modify (+ 3)
a <- memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= case a of
0 -> 0
_ -> b `mod` a
0x09 -> do
lift $ modify (+ 3)
a <- fromIntegral <$> memPartA acode :: DCPUM Int16
(b, addr) <- over _1 fromIntegral <$> memPartB bcode :: DCPUM (Int16, Either RegCode Word16)
dcpuLens addr .= case a of
0 -> 0
_ -> fromIntegral (b `rem` a)
0x0a -> do
lift $ modify (+ 3)
a <- memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= b .&. a
0x0b -> do
lift $ modify (+ 1)
a <- memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= b .|. a
0x0c -> do
lift $ modify (+ 1)
a <- memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= b `xor` a
0x0d -> do
lift $ modify (+ 1)
a <- fromIntegral <$> memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= b `shiftR` a
dcpuLens (Left EX) .= fromIntegral (fromIntegral b `shiftL` 16 `shiftR` a :: Word32)
0x0e -> do
lift $ modify (+ 1)
a <- fromIntegral <$> memPartA acode
(b, addr) <- over _1 fromIntegral <$> memPartB bcode :: DCPUM (Int16, Either RegCode Word16)
dcpuLens addr .= fromIntegral (b `shiftR` a)
dcpuLens (Left EX) .= fromIntegral (fromIntegral b `shiftL` 16 `shiftR` a :: Int32)
0x0f -> do
lift $ modify (+ 1)
a <- fromIntegral <$> memPartA acode
(b, addr) <- memPartB bcode
dcpuLens addr .= b `shiftL` a
dcpuLens (Left EX) .= fromIntegral (fromIntegral b `shiftL` a `shiftR` 16 :: Word32)
0x10 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, _) <- memPartB bcode
unless (a .&. b /= 0) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x11 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, _) <- memPartB bcode
unless (a .&. b == 0) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x12 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, _) <- memPartB bcode
unless (b == a) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x13 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, _) <- memPartB bcode
unless (b /= a) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x14 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, _) <- memPartB bcode
unless (b > a) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x15 -> do
lift $ modify (+ 2)
a <- fromIntegral <$> memPartA acode :: DCPUM Int16
(b, _) <- over _1 fromIntegral <$> memPartB bcode
unless (b > a) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x16 -> do
lift $ modify (+ 2)
a <- memPartA acode
(b, _) <- memPartB bcode
unless (b < a) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x17 -> do
lift $ modify (+ 2)
a <- fromIntegral <$> memPartA acode :: DCPUM Int16
(b, _) <- over _1 fromIntegral <$> memPartB bcode
unless (b < a) $ do
lift $ modify (+ 1)
skip <- sizeOfInstruction
dcpuLens (Left PC) += skip
0x18 -> liftIO . throwIO $ DCPUUndefinedOperation opcode -- spec is eerily missing
0x19 -> liftIO . throwIO $ DCPUUndefinedOperation opcode -- it's like someone stole these instructions from Notch's mind
0x1a -> do
lift $ modify (+ 3)
a <- memPartA acode
(b, addr) <- memPartB bcode
c <- use . dcpuLens $ Left EX
dcpuLens addr .= a + b + c
dcpuLens (Left EX) .= if foldMap (Sum . fromIntegral :: Word16 -> Sum Integer) [a, b, c] > Sum 0xffff then 1 else 0
0x1b -> do
lift $ modify (+ 3)
a <- memPartA acode
(b, addr) <- memPartB bcode
c <- use . dcpuLens $ Left EX
dcpuLens addr .= b - a + c
dcpuLens (Left EX) .= if b + c < a then -1 else 0
0x1c -> liftIO . throwIO $ DCPUUndefinedOperation opcode -- the spec theif strikes again!
0x1d -> liftIO . throwIO $ DCPUUndefinedOperation opcode -- someone call the police!
0x1e -> do
lift $ modify (+ 2)
a <- memPartA acode
(_, addr) <- memPartB bcode
dcpuLens addr .= a
dcpuLens (Left I) += 1
dcpuLens (Left J) += 1
0x1f -> do
lift $ modify (+ 2)
a <- memPartA acode
(_, addr) <- memPartB bcode
dcpuLens addr .= a
dcpuLens (Left I) -= 1
dcpuLens (Left J) -= 1
_ -> liftIO . throwIO $ DCPUUndefinedOperation opcode
hw <- use dcpuHardware
mapM_ hardwareRefresh hw
runDCPU :: DCPUM a
runDCPU = step >> runDCPU