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 -- Not really an adder either, but a simple stateful example (D-flipflop)
35 dff :: Bit -> Bit -> (Bit, Bit)
41 -- Combinatoric stateless no-carry adder
43 no_carry_adder :: (Bit, Bit) -> Bit
44 no_carry_adder (a, b) = a `hwxor` b
46 -- Combinatoric stateless half adder
48 half_adder :: (Bit, Bit) -> (Bit, Bit)
50 ( a `hwxor` b, a `hwand` b )
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)
57 (s1, c1) = half_adder(a, b)
58 (s, c2) = half_adder(s1, cin)
61 sfull_adder = stateless full_adder
65 -- [a] -> [b] -> ([s], cout)
66 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
68 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
69 ([s3, s2, s1, s0], c3)
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)
76 -- Any number of bits adder
78 -- [a] -> [b] -> ([s], cout)
79 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
81 rec_adder ([], []) = ([], Low)
82 rec_adder ((a:as), (b:bs)) =
85 (rest, cin) = rec_adder (as, bs)
86 (s, cout) = full_adder (a, b, cin)
88 -- Four bit adder, using the continous adder below
89 -- [a] -> [b] -> ([s], cout)
91 -- ([s3, s2, s1, s0], c)
93 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
95 -- Continuous sequential version
96 -- Stream a -> Stream b -> Stream (sum, cout)
97 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
99 -- Forward to con_adder_int, but supply an initial state
101 -- con_adder_int pin Low
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
108 -- (s, cout) = full_adder a b cin
110 -- vim: set ts=8 sw=2 sts=2 expandtab: