Store the RegisterBankState in a algbraic data type.
[matthijs/master-project/cλash.git] / Alu.hs
1 module Alu (main) where
2 import Bits
3 import qualified Sim
4
5 main = Sim.simulate exec program initial_state
6 mainIO = Sim.simulateIO exec initial_state
7
8 program = [
9             -- (addr, we, op)
10             (High, Low, High), -- z = r1 and t (0) ; t = r1 (1)
11             (Low, Low, Low), -- z = r0 or t (1); t = r0 (0)
12             (Low, High, DontCare), -- r0 = z (1)
13             (High, Low, High), -- z = r1 and t (0); t = r1 (1)
14             (High, High, DontCare) -- r1 = z (0)
15           ]
16
17 initial_state = (Regs Low High, (), Low, Low)
18
19 -- Register bank
20
21 type RegAddr = Bit
22 --type RegisterBankState = (Bit, Bit)
23 data RegisterBankState = Regs { r0, r1 :: Bit} deriving (Show)
24
25 register_bank :: 
26   (RegAddr, Bit, Bit) -> -- (addr, we, d)
27   RegisterBankState -> -- s
28   (RegisterBankState, Bit) -- (s', o)
29
30 register_bank (Low, Low, _) s = -- Read r0
31   (s, r0 s)
32
33 register_bank (High, Low, _) s = -- Read r1
34   (s, r1 s)
35
36 register_bank (addr, High, d) s = -- Write
37   (s', DontCare)
38   where
39     Regs r0 r1 = s
40     r0' = if addr == Low then d else r0
41     r1' = if addr == High then d else r1
42     s' = Regs r0' r1'
43
44 -- ALU
45
46 type AluState = ()
47 type AluOp = Bit
48
49 alu :: (AluOp, Bit, Bit) -> AluState -> (AluState, Bit)
50 alu (High, a, b) s = ((), a `hwand` b)
51 alu (Low, a, b) s = ((), a `hwor` b)
52
53 type ExecState = (RegisterBankState, AluState, Bit, Bit)
54 exec :: (RegAddr, Bit, AluOp) -> ExecState -> (ExecState, ())
55
56 -- Read & Exec
57 exec (addr, Low, op) s =
58   (s', ())
59   where
60     (reg_s, alu_s, t, z) = s
61     (reg_s', t') = register_bank (addr, Low, DontCare) reg_s
62     (alu_s', z') = alu (op, t', t) alu_s
63     s' = (reg_s', alu_s', t', z')
64
65 -- Write
66 exec (addr, High, op) s =
67   (s', ())
68   where
69     (reg_s, alu_s, t, z) = s
70     (reg_s', _) = register_bank (addr, High, z) reg_s
71     s' = (reg_s', alu_s, t, z)
72
73 -- vim: set ts=8 sw=2 sts=2 expandtab: