Add a full_adder version with stateful interface.
[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 -- Combinatoric stateless no-carry adder
35 -- A -> B -> S
36 no_carry_adder :: (Bit, Bit) -> Bit
37 no_carry_adder (a, b) = a `hwxor` b
38
39 -- Combinatoric stateless half adder
40 -- A -> B -> (S, C)
41 half_adder :: (Bit, Bit) -> (Bit, Bit)
42 half_adder (a, b) = 
43   ( a `hwxor` b, a `hwand` b )
44
45 -- Combinatoric stateless full adder
46 -- (A, B, C) -> (S, C)
47 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
48 full_adder (a, b, cin) = (s, c)
49   where
50     (s1, c1) = half_adder(a, b)
51     (s, c2)  = half_adder(s1, cin)
52     c        = c1 `hwor` c2
53
54 sfull_adder = stateless full_adder
55
56 -- Four bit adder
57 -- Explicit version
58 -- [a] -> [b] -> ([s], cout)
59 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
60
61 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
62   ([s3, s2, s1, s0], c3)
63   where
64     (s0, c0) = full_adder (a0, b0, Low)
65     (s1, c1) = full_adder (a1, b1, c0)
66     (s2, c2) = full_adder (a2, b2, c1)
67     (s3, c3) = full_adder (a3, b3, c2)
68
69 -- Any number of bits adder
70 -- Recursive version
71 -- [a] -> [b] -> ([s], cout)
72 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
73
74 rec_adder ([], []) = ([], Low)
75 rec_adder ((a:as), (b:bs)) = 
76   (s : rest, cout)
77   where
78     (rest, cin) = rec_adder (as, bs)
79     (s, cout) = full_adder (a, b, cin)
80
81 -- Four bit adder, using the continous adder below
82 -- [a] -> [b] -> ([s], cout)
83 --con_adder_4 as bs = 
84 --  ([s3, s2, s1, s0], c)
85 --  where
86 --    ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
87
88 -- Continuous sequential version
89 -- Stream a -> Stream b -> Stream (sum, cout)
90 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
91
92 -- Forward to con_adder_int, but supply an initial state
93 --con_adder pin =
94 --  con_adder_int pin Low
95
96 -- Stream a -> Stream b -> state -> Stream (s, c)
97 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
98 --con_adder_int ((a,b):rest) cin =
99 --  (s, cout) : con_adder_int rest cout
100 --  where
101 --    (s, cout) = full_adder a b cin
102
103 -- vim: set ts=8 sw=2 sts=2 expandtab: