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 Types.Data.Num.Decimal.Literals
15 import Data.Param.TFVec
16 import Data.RangedWord
20 mainIO f = Sim.simulateIO (Sim.stateless f) ()
22 -- This function is from Sim.hs, but we redefine it here so it can get inlined
24 stateless :: (i -> o) -> (i -> () -> ((), o))
25 stateless f = \i s -> (s, f i)
27 -- show_add f = do print ("Sum: " P.++ (displaysigs s)); print ("Carry: " P.++ (displaysig c))
29 -- a = [High, High, High, High]
30 -- b = [Low, Low, Low, High]
33 mux2 :: Bit -> (Bit, Bit) -> Bit
37 -- Not really an adder, but this is nice minimal hardware description
41 -- bus :: (TypeLevel.Pos len) => BitVec len -> BitVec len
44 -- bus_4 :: BitVec TypeLevel.D4 -> BitVec TypeLevel.D4
48 inv_n :: (Pos len) => BitVec len -> BitVec len
54 inv_n_rec :: vec -> vec
56 instance (Pos len) => Inv (BitVec len) where
63 instance Inv (BitVec D0) where
66 -- Not really an adder either, but a slightly more complex example
68 inv a = let r = hwnot a in r
70 -- Not really an adder either, but a slightly more complex example
72 invinv a = hwnot (hwnot a)
74 -- Not really an adder either, but a slightly more complex example
75 dup :: Bit -> (Bit, Bit)
78 -- Not really an adder either, but a simple stateful example (D-flipflop)
79 dff :: Bit -> Bit -> (Bit, Bit)
85 type ShifterState = (Bit, Bit, Bit, Bit)
86 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
87 shifter i (a, b, c, d) =
92 {-# NOINLINE shifter_en #-}
93 shifter_en :: Bit -> Bit-> ShifterState -> (ShifterState, Bit)
94 shifter_en High i (a, b, c, d) =
99 shifter_en Low i s@(a, b, c, d) =
102 -- Two multiplexed shifters
103 type ShiftersState = (ShifterState, ShifterState)
104 shifters :: Bit -> Bit -> ShiftersState -> (ShiftersState, Bit)
105 shifters sel i (sa, sb) =
108 (sa', outa) = shifter_en sel i sa
109 (sb', outb) = shifter_en (hwnot sel) i sb
111 out = if sel == High then outa else outb
113 -- Combinatoric stateless no-carry adder
115 no_carry_adder :: (Bit, Bit) -> Bit
116 no_carry_adder (a, b) = a `hwxor` b
118 -- Combinatoric stateless half adder
120 half_adder :: (Bit, Bit) -> (Bit, Bit)
121 {-# NOINLINE half_adder #-}
123 ( a `hwxor` b, a `hwand` b )
125 -- Combinatoric stateless full adder
126 -- (A, B, C) -> (S, C)
127 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
128 full_adder (a, b, cin) = (s, c)
130 (s1, c1) = half_adder(a, b)
131 (s, c2) = half_adder(s1, cin)
134 sfull_adder = stateless full_adder
138 -- [a] -> [b] -> ([s], cout)
139 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
141 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
142 ([s3, s2, s1, s0], c3)
144 (s0, c0) = full_adder (a0, b0, Low)
145 (s1, c1) = full_adder (a1, b1, c0)
146 (s2, c2) = full_adder (a2, b2, c1)
147 (s3, c3) = full_adder (a3, b3, c2)
149 -- Any number of bits adder
151 -- [a] -> [b] -> ([s], cout)
152 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
154 rec_adder ([], []) = ([], Low)
155 rec_adder ((a:as), (b:bs)) =
158 (rest, cin) = rec_adder (as, bs)
159 (s, cout) = full_adder (a, b, cin)
162 add, sub :: Int -> Int -> Int
182 functiontest :: TFVec D12 Bit -> TFVec D6 Bit
183 functiontest = \v -> let r = take d6 v in r
185 functiontest2 :: SizedInt D8 -> SizedInt D7
186 functiontest2 = \a -> let r = Data.SizedInt.resize a in r
190 maptest :: TFVec D4 Bit -> TFVec D4 Bit
191 maptest = \v -> let r = map xhwnot v in r
193 highordtest2 = \a b ->
197 op' :: Bit -> Bit -> Bit
203 -- Four bit adder, using the continous adder below
204 -- [a] -> [b] -> ([s], cout)
205 -- con_adder_4 as bs =
206 -- ([s3, s2, s1, s0], c)
208 -- ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (P.zip ((P.reverse as) P.++ lows) ((P.reverse bs) P.++ lows))
210 -- Continuous sequential version
211 -- Stream a -> Stream b -> Stream (sum, cout)
212 -- con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
214 -- Forward to con_adder_int, but supply an initial state
216 -- con_adder_int pin Low
218 -- Stream a -> Stream b -> state -> Stream (s, c)
219 -- con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
220 -- con_adder_int ((a,b):rest) cin =
221 -- (s, cout) : con_adder_int rest cout
223 -- (s, cout) = full_adder (a, b, cin)
225 -- vim: set ts=8 sw=2 sts=2 expandtab: