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
13 import Data.RangedWord
15 mainIO f = Sim.simulateIO (Sim.stateless f) ()
17 -- This function is from Sim.hs, but we redefine it here so it can get inlined
19 stateless :: (i -> o) -> (i -> () -> ((), o))
20 stateless f = \i s -> (s, f i)
22 show_add f = do print ("Sum: " P.++ (displaysigs s)); print ("Carry: " P.++ (displaysig c))
24 a = [High, High, High, High]
25 b = [Low, Low, Low, High]
28 mux2 :: Bit -> (Bit, Bit) -> Bit
32 -- Not really an adder, but this is nice minimal hardware description
36 -- bus :: (TypeLevel.Pos len) => BitVec len -> BitVec len
39 -- bus_4 :: BitVec TypeLevel.D4 -> BitVec TypeLevel.D4
43 inv_n :: (Pos len) => BitVec len -> BitVec len
49 inv_n_rec :: vec -> vec
51 instance (Pos len) => Inv (BitVec len) where
58 instance Inv (BitVec D0) where
61 -- Not really an adder either, but a slightly more complex example
63 inv a = let r = hwnot a in r
65 -- Not really an adder either, but a slightly more complex example
67 invinv a = hwnot (hwnot a)
69 -- Not really an adder either, but a slightly more complex example
70 dup :: Bit -> (Bit, Bit)
73 -- Not really an adder either, but a simple stateful example (D-flipflop)
74 dff :: Bit -> Bit -> (Bit, Bit)
80 type ShifterState = (Bit, Bit, Bit, Bit)
81 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
82 shifter i (a, b, c, d) =
87 {-# NOINLINE shifter_en #-}
88 shifter_en :: Bit -> Bit-> ShifterState -> (ShifterState, Bit)
89 shifter_en High i (a, b, c, d) =
94 shifter_en Low i s@(a, b, c, d) =
97 -- Two multiplexed shifters
98 type ShiftersState = (ShifterState, ShifterState)
99 shifters :: Bit -> Bit -> ShiftersState -> (ShiftersState, Bit)
100 shifters sel i (sa, sb) =
103 (sa', outa) = shifter_en sel i sa
104 (sb', outb) = shifter_en (hwnot sel) i sb
106 out = if sel == High then outa else outb
108 -- Combinatoric stateless no-carry adder
110 no_carry_adder :: (Bit, Bit) -> Bit
111 no_carry_adder (a, b) = a `hwxor` b
113 -- Combinatoric stateless half adder
115 half_adder :: (Bit, Bit) -> (Bit, Bit)
116 {-# NOINLINE half_adder #-}
118 ( a `hwxor` b, a `hwand` b )
120 -- Combinatoric stateless full adder
121 -- (A, B, C) -> (S, C)
122 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
123 full_adder (a, b, cin) = (s, c)
125 (s1, c1) = half_adder(a, b)
126 (s, c2) = half_adder(s1, cin)
129 sfull_adder = stateless full_adder
133 -- [a] -> [b] -> ([s], cout)
134 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
136 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
137 ([s3, s2, s1, s0], c3)
139 (s0, c0) = full_adder (a0, b0, Low)
140 (s1, c1) = full_adder (a1, b1, c0)
141 (s2, c2) = full_adder (a2, b2, c1)
142 (s3, c3) = full_adder (a3, b3, c2)
144 -- Any number of bits adder
146 -- [a] -> [b] -> ([s], cout)
147 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
149 rec_adder ([], []) = ([], Low)
150 rec_adder ((a:as), (b:bs)) =
153 (rest, cin) = rec_adder (as, bs)
154 (s, cout) = full_adder (a, b, cin)
157 add, sub :: Int -> Int -> Int
177 functiontest :: TFVec D4 (TFVec D3 Bit) -> TFVec D12 Bit
178 functiontest = \v -> let r = concat v in r
182 maptest :: TFVec D4 Bit -> TFVec D4 Bit
183 maptest = \v -> let r = map xhwnot v in r
185 highordtest2 = \a b ->
189 op' :: Bit -> Bit -> Bit
195 -- Four bit adder, using the continous adder below
196 -- [a] -> [b] -> ([s], cout)
198 ([s3, s2, s1, s0], c)
200 ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (P.zip ((P.reverse as) P.++ lows) ((P.reverse bs) P.++ lows))
202 -- Continuous sequential version
203 -- Stream a -> Stream b -> Stream (sum, cout)
204 con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
206 -- Forward to con_adder_int, but supply an initial state
208 con_adder_int pin Low
210 -- Stream a -> Stream b -> state -> Stream (s, c)
211 con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
212 con_adder_int ((a,b):rest) cin =
213 (s, cout) : con_adder_int rest cout
215 (s, cout) = full_adder (a, b, cin)
217 -- vim: set ts=8 sw=2 sts=2 expandtab: