5 import qualified Prelude as P
6 import Prelude hiding (
7 null, length, head, tail, last, init, take, drop, (++), map, foldl, foldr,
8 zipWith, zip, unzip, concat, reverse, iterate )
10 import Language.Haskell.Syntax
12 import Data.Param.TFVec
14 mainIO f = Sim.simulateIO (Sim.stateless f) ()
16 -- This function is from Sim.hs, but we redefine it here so it can get inlined
18 stateless :: (i -> o) -> (i -> () -> ((), o))
19 stateless f = \i s -> (s, f i)
21 show_add f = do print ("Sum: " P.++ (displaysigs s)); print ("Carry: " P.++ (displaysig c))
23 a = [High, High, High, High]
24 b = [Low, Low, Low, High]
27 mux2 :: Bit -> (Bit, Bit) -> Bit
31 -- Not really an adder, but this is nice minimal hardware description
35 -- bus :: (TypeLevel.Pos len) => BitVec len -> BitVec len
38 -- bus_4 :: BitVec TypeLevel.D4 -> BitVec TypeLevel.D4
42 inv_n :: (Pos len) => BitVec len -> BitVec len
48 inv_n_rec :: vec -> vec
50 instance (Pos len) => Inv (BitVec len) where
57 instance Inv (BitVec D0) where
60 -- Not really an adder either, but a slightly more complex example
62 inv a = let r = hwnot a in r
64 -- Not really an adder either, but a slightly more complex example
66 invinv a = hwnot (hwnot a)
68 -- Not really an adder either, but a slightly more complex example
69 dup :: Bit -> (Bit, Bit)
72 -- Not really an adder either, but a simple stateful example (D-flipflop)
73 dff :: Bit -> Bit -> (Bit, Bit)
79 type ShifterState = (Bit, Bit, Bit, Bit)
80 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
81 shifter i (a, b, c, d) =
86 {-# NOINLINE shifter_en #-}
87 shifter_en :: Bit -> Bit-> ShifterState -> (ShifterState, Bit)
88 shifter_en High i (a, b, c, d) =
93 shifter_en Low i s@(a, b, c, d) =
96 -- Two multiplexed shifters
97 type ShiftersState = (ShifterState, ShifterState)
98 shifters :: Bit -> Bit -> ShiftersState -> (ShiftersState, Bit)
99 shifters sel i (sa, sb) =
102 (sa', outa) = shifter_en sel i sa
103 (sb', outb) = shifter_en (hwnot sel) i sb
105 out = if sel == High then outa else outb
107 -- Combinatoric stateless no-carry adder
109 no_carry_adder :: (Bit, Bit) -> Bit
110 no_carry_adder (a, b) = a `hwxor` b
112 -- Combinatoric stateless half adder
114 half_adder :: (Bit, Bit) -> (Bit, Bit)
115 {-# NOINLINE half_adder #-}
117 ( a `hwxor` b, a `hwand` b )
119 -- Combinatoric stateless full adder
120 -- (A, B, C) -> (S, C)
121 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
122 full_adder (a, b, cin) = (s, c)
124 (s1, c1) = half_adder(a, b)
125 (s, c2) = half_adder(s1, cin)
128 sfull_adder = stateless full_adder
132 -- [a] -> [b] -> ([s], cout)
133 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
135 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
136 ([s3, s2, s1, s0], c3)
138 (s0, c0) = full_adder (a0, b0, Low)
139 (s1, c1) = full_adder (a1, b1, c0)
140 (s2, c2) = full_adder (a2, b2, c1)
141 (s3, c3) = full_adder (a3, b3, c2)
143 -- Any number of bits adder
145 -- [a] -> [b] -> ([s], cout)
146 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
148 rec_adder ([], []) = ([], Low)
149 rec_adder ((a:as), (b:bs)) =
152 (rest, cin) = rec_adder (as, bs)
153 (s, cout) = full_adder (a, b, cin)
156 add, sub :: Int -> Int -> Int
174 functiontest :: TFVec D4 Bit -> Bit
175 functiontest = \v -> let r = head v in r
177 highordtest2 = \a b ->
181 op' :: Bit -> Bit -> Bit
187 -- Four bit adder, using the continous adder below
188 -- [a] -> [b] -> ([s], cout)
190 ([s3, s2, s1, s0], c)
192 ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (P.zip ((P.reverse as) P.++ lows) ((P.reverse bs) P.++ lows))
194 -- Continuous sequential version
195 -- Stream a -> Stream b -> Stream (sum, cout)
196 con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
198 -- Forward to con_adder_int, but supply an initial state
200 con_adder_int pin Low
202 -- Stream a -> Stream b -> state -> Stream (s, c)
203 con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
204 con_adder_int ((a,b):rest) cin =
205 (s, cout) : con_adder_int rest cout
207 (s, cout) = full_adder (a, b, cin)
209 -- vim: set ts=8 sw=2 sts=2 expandtab: