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
17 mainIO f = Sim.simulateIO (Sim.stateless f) ()
19 -- This function is from Sim.hs, but we redefine it here so it can get inlined
21 stateless :: (i -> o) -> (i -> () -> ((), o))
22 stateless f = \i s -> (s, f i)
24 show_add f = do print ("Sum: " P.++ (displaysigs s)); print ("Carry: " P.++ (displaysig c))
26 a = [High, High, High, High]
27 b = [Low, Low, Low, High]
30 mux2 :: Bit -> (Bit, Bit) -> Bit
34 -- Not really an adder, but this is nice minimal hardware description
38 -- bus :: (TypeLevel.Pos len) => BitVec len -> BitVec len
41 -- bus_4 :: BitVec TypeLevel.D4 -> BitVec TypeLevel.D4
45 inv_n :: (Pos len) => BitVec len -> BitVec len
51 inv_n_rec :: vec -> vec
53 instance (Pos len) => Inv (BitVec len) where
60 instance Inv (BitVec D0) where
63 -- Not really an adder either, but a slightly more complex example
65 inv a = let r = hwnot a in r
67 -- Not really an adder either, but a slightly more complex example
69 invinv a = hwnot (hwnot a)
71 -- Not really an adder either, but a slightly more complex example
72 dup :: Bit -> (Bit, Bit)
75 -- Not really an adder either, but a simple stateful example (D-flipflop)
76 dff :: Bit -> Bit -> (Bit, Bit)
82 type ShifterState = (Bit, Bit, Bit, Bit)
83 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
84 shifter i (a, b, c, d) =
89 {-# NOINLINE shifter_en #-}
90 shifter_en :: Bit -> Bit-> ShifterState -> (ShifterState, Bit)
91 shifter_en High i (a, b, c, d) =
96 shifter_en Low i s@(a, b, c, d) =
99 -- Two multiplexed shifters
100 type ShiftersState = (ShifterState, ShifterState)
101 shifters :: Bit -> Bit -> ShiftersState -> (ShiftersState, Bit)
102 shifters sel i (sa, sb) =
105 (sa', outa) = shifter_en sel i sa
106 (sb', outb) = shifter_en (hwnot sel) i sb
108 out = if sel == High then outa else outb
110 -- Combinatoric stateless no-carry adder
112 no_carry_adder :: (Bit, Bit) -> Bit
113 no_carry_adder (a, b) = a `hwxor` b
115 -- Combinatoric stateless half adder
117 half_adder :: (Bit, Bit) -> (Bit, Bit)
118 {-# NOINLINE half_adder #-}
120 ( a `hwxor` b, a `hwand` b )
122 -- Combinatoric stateless full adder
123 -- (A, B, C) -> (S, C)
124 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
125 full_adder (a, b, cin) = (s, c)
127 (s1, c1) = half_adder(a, b)
128 (s, c2) = half_adder(s1, cin)
131 sfull_adder = stateless full_adder
135 -- [a] -> [b] -> ([s], cout)
136 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
138 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
139 ([s3, s2, s1, s0], c3)
141 (s0, c0) = full_adder (a0, b0, Low)
142 (s1, c1) = full_adder (a1, b1, c0)
143 (s2, c2) = full_adder (a2, b2, c1)
144 (s3, c3) = full_adder (a3, b3, c2)
146 -- Any number of bits adder
148 -- [a] -> [b] -> ([s], cout)
149 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
151 rec_adder ([], []) = ([], Low)
152 rec_adder ((a:as), (b:bs)) =
155 (rest, cin) = rec_adder (as, bs)
156 (s, cout) = full_adder (a, b, cin)
159 add, sub :: Int -> Int -> Int
179 functiontest :: TFVec D4 (TFVec D3 Bit) -> (TFVec D12 Bit, TFVec D3 Bit)
180 functiontest = \v -> let r = (concat v, head v) in r
184 maptest :: TFVec D4 Bit -> TFVec D4 Bit
185 maptest = \v -> let r = map xhwnot v in r
187 highordtest2 = \a b ->
191 op' :: Bit -> Bit -> Bit
197 -- Four bit adder, using the continous adder below
198 -- [a] -> [b] -> ([s], cout)
200 ([s3, s2, s1, s0], c)
202 ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (P.zip ((P.reverse as) P.++ lows) ((P.reverse bs) P.++ lows))
204 -- Continuous sequential version
205 -- Stream a -> Stream b -> Stream (sum, cout)
206 con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
208 -- Forward to con_adder_int, but supply an initial state
210 con_adder_int pin Low
212 -- Stream a -> Stream b -> state -> Stream (s, c)
213 con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
214 con_adder_int ((a,b):rest) cin =
215 (s, cout) : con_adder_int rest cout
217 (s, cout) = full_adder (a, b, cin)
219 -- vim: set ts=8 sw=2 sts=2 expandtab: