4 import Language.Haskell.Syntax
6 mainIO f = Sim.simulateIO (Sim.stateless f) ()
8 -- This function is from Sim.hs, but we redefine it here so it can get inlined
10 stateless f = \i s -> (s, f i)
12 show_add f = do print ("Sum: " ++ (displaysigs s)); print ("Carry: " ++ (displaysig c))
14 a = [High, High, High, High]
15 b = [Low, Low, Low, High]
18 -- Not really an adder, but this is nice minimal hardware description
22 -- Not really an adder either, but a slightly more complex example
26 -- Not really an adder either, but a slightly more complex example
28 invinv a = hwnot (hwnot a)
30 -- Not really an adder either, but a slightly more complex example
31 dup :: Bit -> (Bit, Bit)
34 -- Combinatoric stateless no-carry adder
36 no_carry_adder :: (Bit, Bit) -> Bit
37 no_carry_adder (a, b) = a `hwxor` b
39 -- Combinatoric stateless half adder
41 half_adder :: (Bit, Bit) -> (Bit, Bit)
43 ( a `hwxor` b, a `hwand` b )
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)
50 (s1, c1) = half_adder(a, b)
51 (s, c2) = half_adder(s1, cin)
54 sfull_adder = stateless full_adder
58 -- [a] -> [b] -> ([s], cout)
59 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
61 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
62 ([s3, s2, s1, s0], c3)
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)
69 -- Any number of bits adder
71 -- [a] -> [b] -> ([s], cout)
72 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
74 rec_adder ([], []) = ([], Low)
75 rec_adder ((a:as), (b:bs)) =
78 (rest, cin) = rec_adder (as, bs)
79 (s, cout) = full_adder (a, b, cin)
81 -- Four bit adder, using the continous adder below
82 -- [a] -> [b] -> ([s], cout)
84 -- ([s3, s2, s1, s0], c)
86 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
88 -- Continuous sequential version
89 -- Stream a -> Stream b -> Stream (sum, cout)
90 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
92 -- Forward to con_adder_int, but supply an initial state
94 -- con_adder_int pin Low
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
101 -- (s, cout) = full_adder a b cin
103 -- vim: set ts=8 sw=2 sts=2 expandtab: