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 type ShifterState = (Bit, Bit, Bit, Bit)
43 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
50 -- Combinatoric stateless no-carry adder
52 no_carry_adder :: (Bit, Bit) -> Bit
53 no_carry_adder (a, b) = a `hwxor` b
55 -- Combinatoric stateless half adder
57 half_adder :: (Bit, Bit) -> (Bit, Bit)
59 ( a `hwxor` b, a `hwand` b )
61 -- Combinatoric stateless full adder
62 -- (A, B, C) -> (S, C)
63 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
64 full_adder (a, b, cin) = (s, c)
66 (s1, c1) = half_adder(a, b)
67 (s, c2) = half_adder(s1, cin)
70 sfull_adder = stateless full_adder
74 -- [a] -> [b] -> ([s], cout)
75 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
77 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
78 ([s3, s2, s1, s0], c3)
80 (s0, c0) = full_adder (a0, b0, Low)
81 (s1, c1) = full_adder (a1, b1, c0)
82 (s2, c2) = full_adder (a2, b2, c1)
83 (s3, c3) = full_adder (a3, b3, c2)
85 -- Any number of bits adder
87 -- [a] -> [b] -> ([s], cout)
88 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
90 rec_adder ([], []) = ([], Low)
91 rec_adder ((a:as), (b:bs)) =
94 (rest, cin) = rec_adder (as, bs)
95 (s, cout) = full_adder (a, b, cin)
97 -- Four bit adder, using the continous adder below
98 -- [a] -> [b] -> ([s], cout)
100 -- ([s3, s2, s1, s0], c)
102 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
104 -- Continuous sequential version
105 -- Stream a -> Stream b -> Stream (sum, cout)
106 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
108 -- Forward to con_adder_int, but supply an initial state
110 -- con_adder_int pin Low
112 -- Stream a -> Stream b -> state -> Stream (s, c)
113 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
114 --con_adder_int ((a,b):rest) cin =
115 -- (s, cout) : con_adder_int rest cout
117 -- (s, cout) = full_adder a b cin
119 -- vim: set ts=8 sw=2 sts=2 expandtab: