4 import Language.Haskell.Syntax
5 import qualified Data.TypeLevel as TypeLevel
6 import qualified Data.Param.FSVec as FSVec
8 mainIO f = Sim.simulateIO (Sim.stateless f) ()
10 -- This function is from Sim.hs, but we redefine it here so it can get inlined
12 stateless :: (i -> o) -> (i -> () -> ((), o))
13 stateless f = \i s -> (s, f i)
15 show_add f = do print ("Sum: " ++ (displaysigs s)); print ("Carry: " ++ (displaysig c))
17 a = [High, High, High, High]
18 b = [Low, Low, Low, High]
21 mux2 :: Bit -> (Bit, Bit) -> Bit
25 -- Not really an adder, but this is nice minimal hardware description
29 bus :: (TypeLevel.Pos len) => BitVec len -> BitVec len
32 bus_4 :: BitVec TypeLevel.D4 -> BitVec TypeLevel.D4
36 inv_n :: (Pos len) => BitVec len -> BitVec len
42 inv_n_rec :: vec -> vec
44 instance (Pos len) => Inv (BitVec len) where
51 instance Inv (BitVec D0) where
54 -- Not really an adder either, but a slightly more complex example
56 inv a = let r = hwnot a in r
58 -- Not really an adder either, but a slightly more complex example
60 invinv a = hwnot (hwnot a)
62 -- Not really an adder either, but a slightly more complex example
63 dup :: Bit -> (Bit, Bit)
66 -- Not really an adder either, but a simple stateful example (D-flipflop)
67 dff :: Bit -> Bit -> (Bit, Bit)
73 type ShifterState = (Bit, Bit, Bit, Bit)
74 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
75 shifter i (a, b, c, d) =
80 {-# NOINLINE shifter_en #-}
81 shifter_en :: Bit -> Bit-> ShifterState -> (ShifterState, Bit)
82 shifter_en High i (a, b, c, d) =
87 shifter_en Low i s@(a, b, c, d) =
90 -- Two multiplexed shifters
91 type ShiftersState = (ShifterState, ShifterState)
92 shifters :: Bit -> Bit -> ShiftersState -> (ShiftersState, Bit)
93 shifters sel i (sa, sb) =
96 (sa', outa) = shifter_en sel i sa
97 (sb', outb) = shifter_en (hwnot sel) i sb
99 out = if sel == High then outa else outb
101 -- Combinatoric stateless no-carry adder
103 no_carry_adder :: (Bit, Bit) -> Bit
104 no_carry_adder (a, b) = a `hwxor` b
106 -- Combinatoric stateless half adder
108 half_adder :: (Bit, Bit) -> (Bit, Bit)
109 {-# NOINLINE half_adder #-}
111 ( a `hwxor` b, a `hwand` b )
113 -- Combinatoric stateless full adder
114 -- (A, B, C) -> (S, C)
115 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
116 full_adder (a, b, cin) = (s, c)
118 (s1, c1) = half_adder(a, b)
119 (s, c2) = half_adder(s1, cin)
122 sfull_adder = stateless full_adder
126 -- [a] -> [b] -> ([s], cout)
127 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
129 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
130 ([s3, s2, s1, s0], c3)
132 (s0, c0) = full_adder (a0, b0, Low)
133 (s1, c1) = full_adder (a1, b1, c0)
134 (s2, c2) = full_adder (a2, b2, c1)
135 (s3, c3) = full_adder (a3, b3, c2)
137 -- Any number of bits adder
139 -- [a] -> [b] -> ([s], cout)
140 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
142 rec_adder ([], []) = ([], Low)
143 rec_adder ((a:as), (b:bs)) =
146 (rest, cin) = rec_adder (as, bs)
147 (s, cout) = full_adder (a, b, cin)
149 -- Four bit adder, using the continous adder below
150 -- [a] -> [b] -> ([s], cout)
151 --con_adder_4 as bs =
152 -- ([s3, s2, s1, s0], c)
154 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
156 -- Continuous sequential version
157 -- Stream a -> Stream b -> Stream (sum, cout)
158 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
160 -- Forward to con_adder_int, but supply an initial state
162 -- con_adder_int pin Low
164 -- Stream a -> Stream b -> state -> Stream (s, c)
165 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
166 --con_adder_int ((a,b):rest) cin =
167 -- (s, cout) : con_adder_int rest cout
169 -- (s, cout) = full_adder a b cin
171 -- vim: set ts=8 sw=2 sts=2 expandtab: