-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOU.hs
73 lines (62 loc) · 2.25 KB
/
IOU.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
-- ----------------------------------------------------------------- --
-- --
-- MonadChip8.hs : CHIP-8 Emulator --
-- --
-- 2016/09/28 Jay Kumogata(twitter: jay1905) --
-- --
-- ----------------------------------------------------------------- --
module IOU (
IOU,
initIOU,
ioDelay,
ioSound,
ioKey,
ioEsc,
) where
import Data.Bits
import Data.Int
import Data.Word
import Data.Array
import Control.Monad
import Text.Printf
import System.IO
-- ------------------------------------------------------------ --
-- I/O Resources --
-- ------------------------------------------------------------ --
data IOU = IOU {
-- Delay Timer
ioDelay :: Word8
-- Sound Timer
, ioSound :: Word8
-- Key State : FEDCBA9876543210
, ioKey :: Word16
, ioEsc :: Word8
}
-- ----------------------------------------------------------------- --
-- Initialze IOU --
-- ----------------------------------------------------------------- --
initIOU :: IO IOU
initIOU = do
return IOU {
-- Delay Timer
ioDelay = 0x00
-- Sound Timer
, ioSound = 0x00
-- Key State : FEDCBA9876543210
, ioKey = 0x0000
, ioEsc = 0x00
}
-- ----------------------------------------------------------------- --
-- Show state of IOU --
-- ----------------------------------------------------------------- --
instance Show IOU where
show iou = unlines $ [reg8,reg16 ] where
reg8 = concatMap (showReg 2) [("Delay", ioDelay ),
("Sound", ioSound ),
("Esc", ioEsc )]
reg16 = concatMap (showReg 4) [("Key", ioKey)]
showReg n (s,f) = s ++ " : 0x" ++ toHex n (toEnum $ fromEnum $ f iou) ++ " "
toHex :: Int -> Int -> String
toHex n = reverse . map ((tbl!!).(`mod`16)) . take n . iterate (`div`16)
where tbl="0123456789ABCDEF"
-- End of IOU.hs