-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMicroprogram.hs
87 lines (73 loc) · 2.58 KB
/
Microprogram.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
{-# LANGUAGE TypeFamilies #-}
module Microprogram (
module Control.Monad,
Microprogram (..), Address (..), Value (..),
increment, fetchArgument, fetchNextOpcode, load, store, jump
) where
import Control.Monad
-- Type synonyms let us avoid the pain of converting values to addresses, yet improve readability
type Address = Int
type Value = Int
class Monad m => Machine m where
type State m
get :: m (State m)
put :: State m -> m ()
type Instruction m a = State m -> (a, State m)
execute :: Machine m => Instruction m a -> m a
execute instruction = do
s <- get
let (a, s') = instruction s
put s'
return a
-- class Machine m => VonNeumannMachine m where
-- data Memory m
-- data Register m
-- readMemory :: Address -> m Value
-- writeMemory :: Address -> Value -> m ()
-- readRegister :: Register m -> m Value
-- writeRegister :: Register m -> Value -> m ()
class Monad m => Microprogram m where
data Register m
pc, opcode :: Register m
readMemory :: Address -> m Value
writeMemory :: Address -> Value -> m ()
readRegister :: Register m -> m Value
writeRegister :: Register m -> Value -> m ()
-- Increment the value stored in a register
increment :: Microprogram m => Register m -> m ()
increment register = do
value <- readRegister register
writeRegister register (value + 1) -- TODO: change flags
-- Increment the program counter and fetch the value it points to; used for
-- fetching instruction opcodes and immediate arguments
fetchArgument :: Microprogram m => m Value
fetchArgument = do
increment pc
address <- readRegister pc
readMemory address
-- Fetch the next instruction opcode and store it in the opcode register
fetchNextOpcode :: Microprogram m => m ()
fetchNextOpcode = do
value <- fetchArgument
writeRegister opcode value
-- Load a register from memory from an address given by an immediate argument
load :: Microprogram m => Register m -> m ()
load register = do
address <- fetchArgument
value <- readMemory address
writeRegister register value
fetchNextOpcode
-- Store a register in memory at an address given by an immediate argument
store :: Microprogram m => Register m -> m ()
store register = do
address <- fetchArgument
value <- readRegister register
writeMemory address value
fetchNextOpcode
-- Unconditional jump to an address given by an immediate argument
jump :: Microprogram m => m ()
jump = do
address <- fetchArgument
writeRegister pc address
nextOpcode <- readMemory address
writeRegister opcode nextOpcode