Intial import of some haskell programs.
[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 = ((Low, High), (), Low, Low)
18
19 --
20 --
21
22 type RegAddr = Bit
23 type RegisterBankState = (Bit, Bit)
24 register_bank :: 
25   (RegAddr, Bit, Bit) -> -- (addr, we, d)
26   RegisterBankState -> -- s
27   (RegisterBankState, Bit) -- (s', o)
28
29 register_bank (Low, Low, _) s = -- Read r0
30   (s, fst s)
31
32 register_bank (High, Low, _) s = -- Read r1
33   (s, snd s)
34
35 register_bank (addr, High, d) s = -- Write
36   (s', DontCare)
37   where
38     (r0, r1) = s
39     r0' = if addr == Low then d else r0
40     r1' = if addr == High then d else r1
41     s' = (r0', r1')
42
43 type AluState = ()
44 type AluOp = Bit
45
46 alu :: (AluOp, Bit, Bit) -> AluState -> (AluState, Bit)
47 alu (High, a, b) s = ((), a `hwand` b)
48 alu (Low, a, b) s = ((), a `hwor` b)
49
50 type ExecState = (RegisterBankState, AluState, Bit, Bit)
51 exec :: (RegAddr, Bit, AluOp) -> ExecState -> (ExecState, ())
52
53 -- Read & Exec
54 exec (addr, Low, op) s =
55   (s', ())
56   where
57     (reg_s, alu_s, t, z) = s
58     (reg_s', t') = register_bank (addr, Low, DontCare) reg_s
59     (alu_s', z') = alu (op, t', t) alu_s
60     s' = (reg_s', alu_s', t', z')
61
62 -- Write
63 exec (addr, High, op) s =
64   (s', ())
65   where
66     (reg_s, alu_s, t, z) = s
67     (reg_s', _) = register_bank (addr, High, z) reg_s
68     s' = (reg_s', alu_s, t, z)
69
70 -- vim: set ts=8 sw=2 sts=2 expandtab: