Add a D-Flip flop hardware model.
[matthijs/master-project/cλash.git] / Adders.hs
1 module Adders where
2 import Bits
3 import qualified Sim
4 import Language.Haskell.Syntax
5
6 mainIO f = Sim.simulateIO (Sim.stateless f) ()
7
8 -- This function is from Sim.hs, but we redefine it here so it can get inlined
9 -- by default.
10 stateless f = \i s -> (s, f i)
11
12 show_add f = do print ("Sum:   " ++ (displaysigs s)); print ("Carry: " ++ (displaysig c))
13   where
14     a = [High, High, High, High]
15     b = [Low, Low, Low, High]
16     (s, c) = f (a, b)
17
18 -- Not really an adder, but this is nice minimal hardware description
19 wire :: Bit -> Bit
20 wire a = a
21
22 -- Not really an adder either, but a slightly more complex example
23 inv :: Bit -> Bit
24 inv a = hwnot a
25
26 -- Not really an adder either, but a slightly more complex example
27 invinv :: Bit -> Bit
28 invinv a = hwnot (hwnot a)
29
30 -- Not really an adder either, but a slightly more complex example
31 dup :: Bit -> (Bit, Bit)
32 dup a = (a, a)
33
34 -- Not really an adder either, but a simple stateful example (D-flipflop)
35 dff :: Bit -> Bit -> (Bit, Bit)
36 dff d s = (s', q)
37   where
38     q = s
39     s' = d
40
41 -- Combinatoric stateless no-carry adder
42 -- A -> B -> S
43 no_carry_adder :: (Bit, Bit) -> Bit
44 no_carry_adder (a, b) = a `hwxor` b
45
46 -- Combinatoric stateless half adder
47 -- A -> B -> (S, C)
48 half_adder :: (Bit, Bit) -> (Bit, Bit)
49 half_adder (a, b) = 
50   ( a `hwxor` b, a `hwand` b )
51
52 -- Combinatoric stateless full adder
53 -- (A, B, C) -> (S, C)
54 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
55 full_adder (a, b, cin) = (s, c)
56   where
57     (s1, c1) = half_adder(a, b)
58     (s, c2)  = half_adder(s1, cin)
59     c        = c1 `hwor` c2
60
61 sfull_adder = stateless full_adder
62
63 -- Four bit adder
64 -- Explicit version
65 -- [a] -> [b] -> ([s], cout)
66 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
67
68 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
69   ([s3, s2, s1, s0], c3)
70   where
71     (s0, c0) = full_adder (a0, b0, Low)
72     (s1, c1) = full_adder (a1, b1, c0)
73     (s2, c2) = full_adder (a2, b2, c1)
74     (s3, c3) = full_adder (a3, b3, c2)
75
76 -- Any number of bits adder
77 -- Recursive version
78 -- [a] -> [b] -> ([s], cout)
79 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
80
81 rec_adder ([], []) = ([], Low)
82 rec_adder ((a:as), (b:bs)) = 
83   (s : rest, cout)
84   where
85     (rest, cin) = rec_adder (as, bs)
86     (s, cout) = full_adder (a, b, cin)
87
88 -- Four bit adder, using the continous adder below
89 -- [a] -> [b] -> ([s], cout)
90 --con_adder_4 as bs = 
91 --  ([s3, s2, s1, s0], c)
92 --  where
93 --    ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
94
95 -- Continuous sequential version
96 -- Stream a -> Stream b -> Stream (sum, cout)
97 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
98
99 -- Forward to con_adder_int, but supply an initial state
100 --con_adder pin =
101 --  con_adder_int pin Low
102
103 -- Stream a -> Stream b -> state -> Stream (s, c)
104 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
105 --con_adder_int ((a,b):rest) cin =
106 --  (s, cout) : con_adder_int rest cout
107 --  where
108 --    (s, cout) = full_adder a b cin
109
110 -- vim: set ts=8 sw=2 sts=2 expandtab: