4 import Language.Haskell.Syntax
6 mainIO f = Sim.simulateIO (Sim.stateless f) ()
8 show_add f = do print ("Sum: " ++ (displaysigs s)); print ("Carry: " ++ (displaysig c))
10 a = [High, High, High, High]
11 b = [Low, Low, Low, High]
14 -- Not really an adder, but this is nice minimal hardware description
18 -- Not really an adder either, but a slightly more complex example
22 -- Not really an adder either, but a slightly more complex example
24 invinv a = hwnot (hwnot a)
26 -- Not really an adder either, but a slightly more complex example
27 dup :: Bit -> (Bit, Bit)
30 -- Combinatoric stateless no-carry adder
32 no_carry_adder :: (Bit, Bit) -> Bit
33 no_carry_adder (a, b) = a `hwxor` b
35 -- Combinatoric stateless half adder
37 half_adder :: (Bit, Bit) -> (Bit, Bit)
39 ( a `hwxor` b, a `hwand` b )
41 -- Combinatoric stateless full adder
42 -- (A, B, C) -> (S, C)
43 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
44 full_adder (a, b, cin) = (s, c)
46 (s1, c1) = half_adder(a, b)
47 (s, c2) = half_adder(s1, cin)
52 -- [a] -> [b] -> ([s], cout)
53 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
55 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
56 ([s3, s2, s1, s0], c3)
58 (s0, c0) = full_adder (a0, b0, Low)
59 (s1, c1) = full_adder (a1, b1, c0)
60 (s2, c2) = full_adder (a2, b2, c1)
61 (s3, c3) = full_adder (a3, b3, c2)
63 -- Any number of bits adder
65 -- [a] -> [b] -> ([s], cout)
66 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
68 rec_adder ([], []) = ([], Low)
69 rec_adder ((a:as), (b:bs)) =
72 (rest, cin) = rec_adder (as, bs)
73 (s, cout) = full_adder (a, b, cin)
75 -- Four bit adder, using the continous adder below
76 -- [a] -> [b] -> ([s], cout)
78 -- ([s3, s2, s1, s0], c)
80 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
82 -- Continuous sequential version
83 -- Stream a -> Stream b -> Stream (sum, cout)
84 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
86 -- Forward to con_adder_int, but supply an initial state
88 -- con_adder_int pin Low
90 -- Stream a -> Stream b -> state -> Stream (s, c)
91 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
92 --con_adder_int ((a,b):rest) cin =
93 -- (s, cout) : con_adder_int rest cout
95 -- (s, cout) = full_adder a b cin
97 -- vim: set ts=8 sw=2 sts=2 expandtab: