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 mux2 :: Bit -> (Bit, Bit) -> Bit
23 -- Not really an adder, but this is nice minimal hardware description
27 -- Not really an adder either, but a slightly more complex example
31 -- Not really an adder either, but a slightly more complex example
33 invinv a = hwnot (hwnot a)
35 -- Not really an adder either, but a slightly more complex example
36 dup :: Bit -> (Bit, Bit)
39 -- Not really an adder either, but a simple stateful example (D-flipflop)
40 dff :: Bit -> Bit -> (Bit, Bit)
46 type ShifterState = (Bit, Bit, Bit, Bit)
47 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
54 -- Combinatoric stateless no-carry adder
56 no_carry_adder :: (Bit, Bit) -> Bit
57 no_carry_adder (a, b) = a `hwxor` b
59 -- Combinatoric stateless half adder
61 half_adder :: (Bit, Bit) -> (Bit, Bit)
63 ( a `hwxor` b, a `hwand` b )
65 -- Combinatoric stateless full adder
66 -- (A, B, C) -> (S, C)
67 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
68 full_adder (a, b, cin) = (s, c)
70 (s1, c1) = half_adder(a, b)
71 (s, c2) = half_adder(s1, cin)
74 sfull_adder = stateless full_adder
78 -- [a] -> [b] -> ([s], cout)
79 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
81 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
82 ([s3, s2, s1, s0], c3)
84 (s0, c0) = full_adder (a0, b0, Low)
85 (s1, c1) = full_adder (a1, b1, c0)
86 (s2, c2) = full_adder (a2, b2, c1)
87 (s3, c3) = full_adder (a3, b3, c2)
89 -- Any number of bits adder
91 -- [a] -> [b] -> ([s], cout)
92 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
94 rec_adder ([], []) = ([], Low)
95 rec_adder ((a:as), (b:bs)) =
98 (rest, cin) = rec_adder (as, bs)
99 (s, cout) = full_adder (a, b, cin)
101 -- Four bit adder, using the continous adder below
102 -- [a] -> [b] -> ([s], cout)
103 --con_adder_4 as bs =
104 -- ([s3, s2, s1, s0], c)
106 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
108 -- Continuous sequential version
109 -- Stream a -> Stream b -> Stream (sum, cout)
110 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
112 -- Forward to con_adder_int, but supply an initial state
114 -- con_adder_int pin Low
116 -- Stream a -> Stream b -> state -> Stream (s, c)
117 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
118 --con_adder_int ((a,b):rest) cin =
119 -- (s, cout) : con_adder_int rest cout
121 -- (s, cout) = full_adder a b cin
123 -- vim: set ts=8 sw=2 sts=2 expandtab: