1 {-# LANGUAGE TemplateHaskell #-}
7 import qualified Prelude as P
8 import Prelude hiding (
9 null, length, head, tail, last, init, take, drop, (++), map, foldl, foldr,
10 zipWith, zip, unzip, concat, reverse, iterate )
12 import Language.Haskell.Syntax
14 import Data.Param.TFVec
15 import Data.RangedWord
19 mainIO f = Sim.simulateIO (Sim.stateless f) ()
21 -- This function is from Sim.hs, but we redefine it here so it can get inlined
23 stateless :: (i -> o) -> (i -> () -> ((), o))
24 stateless f = \i s -> (s, f i)
26 show_add f = do print ("Sum: " P.++ (displaysigs s)); print ("Carry: " P.++ (displaysig c))
28 a = [High, High, High, High]
29 b = [Low, Low, Low, High]
32 mux2 :: Bit -> (Bit, Bit) -> Bit
36 -- Not really an adder, but this is nice minimal hardware description
40 -- bus :: (TypeLevel.Pos len) => BitVec len -> BitVec len
43 -- bus_4 :: BitVec TypeLevel.D4 -> BitVec TypeLevel.D4
47 inv_n :: (Pos len) => BitVec len -> BitVec len
53 inv_n_rec :: vec -> vec
55 instance (Pos len) => Inv (BitVec len) where
62 instance Inv (BitVec D0) where
65 -- Not really an adder either, but a slightly more complex example
67 inv a = let r = hwnot a in r
69 -- Not really an adder either, but a slightly more complex example
71 invinv a = hwnot (hwnot a)
73 -- Not really an adder either, but a slightly more complex example
74 dup :: Bit -> (Bit, Bit)
77 -- Not really an adder either, but a simple stateful example (D-flipflop)
78 dff :: Bit -> Bit -> (Bit, Bit)
84 type ShifterState = (Bit, Bit, Bit, Bit)
85 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
86 shifter i (a, b, c, d) =
91 {-# NOINLINE shifter_en #-}
92 shifter_en :: Bit -> Bit-> ShifterState -> (ShifterState, Bit)
93 shifter_en High i (a, b, c, d) =
98 shifter_en Low i s@(a, b, c, d) =
101 -- Two multiplexed shifters
102 type ShiftersState = (ShifterState, ShifterState)
103 shifters :: Bit -> Bit -> ShiftersState -> (ShiftersState, Bit)
104 shifters sel i (sa, sb) =
107 (sa', outa) = shifter_en sel i sa
108 (sb', outb) = shifter_en (hwnot sel) i sb
110 out = if sel == High then outa else outb
112 -- Combinatoric stateless no-carry adder
114 no_carry_adder :: (Bit, Bit) -> Bit
115 no_carry_adder (a, b) = a `hwxor` b
117 -- Combinatoric stateless half adder
119 half_adder :: (Bit, Bit) -> (Bit, Bit)
120 {-# NOINLINE half_adder #-}
122 ( a `hwxor` b, a `hwand` b )
124 -- Combinatoric stateless full adder
125 -- (A, B, C) -> (S, C)
126 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
127 full_adder (a, b, cin) = (s, c)
129 (s1, c1) = half_adder(a, b)
130 (s, c2) = half_adder(s1, cin)
133 sfull_adder = stateless full_adder
137 -- [a] -> [b] -> ([s], cout)
138 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
140 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
141 ([s3, s2, s1, s0], c3)
143 (s0, c0) = full_adder (a0, b0, Low)
144 (s1, c1) = full_adder (a1, b1, c0)
145 (s2, c2) = full_adder (a2, b2, c1)
146 (s3, c3) = full_adder (a3, b3, c2)
148 -- Any number of bits adder
150 -- [a] -> [b] -> ([s], cout)
151 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
153 rec_adder ([], []) = ([], Low)
154 rec_adder ((a:as), (b:bs)) =
157 (rest, cin) = rec_adder (as, bs)
158 (s, cout) = full_adder (a, b, cin)
161 add, sub :: Int -> Int -> Int
181 functiontest :: TFVec D3 (TFVec D4 Bit) -> TFVec D12 Bit
182 functiontest = \v -> let r = concat v in r
184 functiontest2 :: SizedInt D8 -> SizedInt D7
185 functiontest2 = \a -> let r = Data.SizedInt.resize a in r
189 maptest :: TFVec D4 Bit -> TFVec D4 Bit
190 maptest = \v -> let r = map xhwnot v in r
192 highordtest2 = \a b ->
196 op' :: Bit -> Bit -> Bit
202 -- Four bit adder, using the continous adder below
203 -- [a] -> [b] -> ([s], cout)
205 ([s3, s2, s1, s0], c)
207 ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (P.zip ((P.reverse as) P.++ lows) ((P.reverse bs) P.++ lows))
209 -- Continuous sequential version
210 -- Stream a -> Stream b -> Stream (sum, cout)
211 con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
213 -- Forward to con_adder_int, but supply an initial state
215 con_adder_int pin Low
217 -- Stream a -> Stream b -> state -> Stream (s, c)
218 con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
219 con_adder_int ((a,b):rest) cin =
220 (s, cout) : con_adder_int rest cout
222 (s, cout) = full_adder (a, b, cin)
224 -- vim: set ts=8 sw=2 sts=2 expandtab: