git @ Cat's Eye Technologies ZOWIE / b3cd714
Implement references. Chris Pressey 6 months ago
2 changed file(s) with 27 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
00 module Language.ZOWIE.Machine where
11
2 import Language.ZOWIE.Registers
32 import Language.ZOWIE.State
3 import Language.ZOWIE.Registers (readAddr, writeAddr)
44
55
6 getValue :: State -> Reference -> IO Value
7 getValue _ (Immediate v) =
8 return v
9 getValue state (Direct addr) =
10 readAddr state addr
11 getValue state (Indirect addr) = do
12 addr' <- readAddr state addr
13 val <- readAddr state addr'
14 return val
15
16 setValue :: State -> Reference -> Value -> IO State
17 setValue _ (Immediate _) _ =
18 error "Cannot set the value of an immediate reference"
19 setValue state (Direct addr) value =
20 writeAddr state addr value
21 setValue state (Indirect addr) value = do
22 addr' <- readAddr state addr
23 state' <- writeAddr state addr' value
24 return state'
25
626 run s = s
77
88 type Memory = Map.Map Addr Value
99
10 data Instruction = Immediate Addr Value
11 | Direct Addr Addr
12 | Indirect Addr Addr Value
10 data Reference = Immediate Value
11 | Direct Addr
12 | Indirect Addr
13 deriving (Show, Ord, Eq)
14
15 data Instruction = Mov Reference Reference
1316 deriving (Show, Ord, Eq)
1417
1518 data State = State {