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)
62 {-# NOINLINE half_adder #-}
64 ( a `hwxor` b, a `hwand` b )
66 -- Combinatoric stateless full adder
67 -- (A, B, C) -> (S, C)
68 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
69 full_adder (a, b, cin) = (s, c)
71 (s1, c1) = half_adder(a, b)
72 (s, c2) = half_adder(s1, cin)
75 sfull_adder = stateless full_adder
79 -- [a] -> [b] -> ([s], cout)
80 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
82 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
83 ([s3, s2, s1, s0], c3)
85 (s0, c0) = full_adder (a0, b0, Low)
86 (s1, c1) = full_adder (a1, b1, c0)
87 (s2, c2) = full_adder (a2, b2, c1)
88 (s3, c3) = full_adder (a3, b3, c2)
90 -- Any number of bits adder
92 -- [a] -> [b] -> ([s], cout)
93 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
95 rec_adder ([], []) = ([], Low)
96 rec_adder ((a:as), (b:bs)) =
99 (rest, cin) = rec_adder (as, bs)
100 (s, cout) = full_adder (a, b, cin)
102 -- Four bit adder, using the continous adder below
103 -- [a] -> [b] -> ([s], cout)
104 --con_adder_4 as bs =
105 -- ([s3, s2, s1, s0], c)
107 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
109 -- Continuous sequential version
110 -- Stream a -> Stream b -> Stream (sum, cout)
111 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
113 -- Forward to con_adder_int, but supply an initial state
115 -- con_adder_int pin Low
117 -- Stream a -> Stream b -> state -> Stream (s, c)
118 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
119 --con_adder_int ((a,b):rest) cin =
120 -- (s, cout) : con_adder_int rest cout
122 -- (s, cout) = full_adder a b cin
124 -- vim: set ts=8 sw=2 sts=2 expandtab: