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 :: (i -> o) -> (i -> () -> ((), o))
11 stateless f = \i s -> (s, f i)
13 show_add f = do print ("Sum: " ++ (displaysigs s)); print ("Carry: " ++ (displaysig c))
15 a = [High, High, High, High]
16 b = [Low, Low, Low, High]
19 -- Not really an adder, but this is nice minimal hardware description
23 -- Not really an adder either, but a slightly more complex example
27 -- Not really an adder either, but a slightly more complex example
29 invinv a = hwnot (hwnot a)
31 -- Not really an adder either, but a slightly more complex example
32 dup :: Bit -> (Bit, Bit)
35 -- Not really an adder either, but a simple stateful example (D-flipflop)
36 dff :: Bit -> Bit -> (Bit, Bit)
42 -- Combinatoric stateless no-carry adder
44 no_carry_adder :: (Bit, Bit) -> Bit
45 no_carry_adder (a, b) = a `hwxor` b
47 -- Combinatoric stateless half adder
49 half_adder :: (Bit, Bit) -> (Bit, Bit)
51 ( a `hwxor` b, a `hwand` b )
53 -- Combinatoric stateless full adder
54 -- (A, B, C) -> (S, C)
55 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
56 full_adder (a, b, cin) = (s, c)
58 (s1, c1) = half_adder(a, b)
59 (s, c2) = half_adder(s1, cin)
62 sfull_adder = stateless full_adder
66 -- [a] -> [b] -> ([s], cout)
67 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
69 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
70 ([s3, s2, s1, s0], c3)
72 (s0, c0) = full_adder (a0, b0, Low)
73 (s1, c1) = full_adder (a1, b1, c0)
74 (s2, c2) = full_adder (a2, b2, c1)
75 (s3, c3) = full_adder (a3, b3, c2)
77 -- Any number of bits adder
79 -- [a] -> [b] -> ([s], cout)
80 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
82 rec_adder ([], []) = ([], Low)
83 rec_adder ((a:as), (b:bs)) =
86 (rest, cin) = rec_adder (as, bs)
87 (s, cout) = full_adder (a, b, cin)
89 -- Four bit adder, using the continous adder below
90 -- [a] -> [b] -> ([s], cout)
92 -- ([s3, s2, s1, s0], c)
94 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
96 -- Continuous sequential version
97 -- Stream a -> Stream b -> Stream (sum, cout)
98 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
100 -- Forward to con_adder_int, but supply an initial state
102 -- con_adder_int pin Low
104 -- Stream a -> Stream b -> state -> Stream (s, c)
105 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
106 --con_adder_int ((a,b):rest) cin =
107 -- (s, cout) : con_adder_int rest cout
109 -- (s, cout) = full_adder a b cin
111 -- vim: set ts=8 sw=2 sts=2 expandtab: