Remove the empty state of the alu function.
[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 AluOp = Bit
47
48 alu :: (AluOp, Bit, Bit) -> Bit
49 alu (High, a, b) = a `hwand` b
50 alu (Low, a, b) = a `hwor` b
51
52 type ExecState = (RegisterBankState, Bit, Bit)
53 exec :: (RegAddr, Bit, AluOp) -> ExecState -> (ExecState, ())
54
55 -- Read & Exec
56 exec (addr, Low, op) s =
57   (s', ())
58   where
59     (reg_s, t, z) = s
60     (reg_s', t') = register_bank (addr, Low, DontCare) reg_s
61     z' = alu (op, t', t)
62     s' = (reg_s', t', z')
63
64 -- Write
65 exec (addr, High, op) s =
66   (s', ())
67   where
68     (reg_s, t, z) = s
69     (reg_s', _) = register_bank (addr, High, z) reg_s
70     s' = (reg_s', t, z)
71
72 -- vim: set ts=8 sw=2 sts=2 expandtab: