Add some hardware models using vectors (FSVec).
[matthijs/master-project/cλash.git] / Adders.hs
1 module Adders where
2 import Bits
3 import qualified Sim
4 import Language.Haskell.Syntax
5 import Data.TypeLevel
6 import qualified Data.Param.FSVec as FSVec
7
8 mainIO f = Sim.simulateIO (Sim.stateless f) ()
9
10 -- This function is from Sim.hs, but we redefine it here so it can get inlined
11 -- by default.
12 stateless :: (i -> o) -> (i -> () -> ((), o))
13 stateless f = \i s -> (s, f i)
14
15 show_add f = do print ("Sum:   " ++ (displaysigs s)); print ("Carry: " ++ (displaysig c))
16   where
17     a = [High, High, High, High]
18     b = [Low, Low, Low, High]
19     (s, c) = f (a, b)
20
21 mux2 :: Bit -> (Bit, Bit) -> Bit
22 mux2 Low (a, b) = a
23 mux2 High (a, b) = b
24
25 -- Not really an adder, but this is nice minimal hardware description
26 wire :: Bit -> Bit
27 wire a = a
28
29 bus :: (Pos len) => BitVec len -> BitVec len
30 bus v = v
31
32 bus_4 :: BitVec D4 -> BitVec D4
33 bus_4 v = v
34
35 {-
36 inv_n :: (Pos len) => BitVec len -> BitVec len
37 inv_n v =
38   --FSVec.map hwnot v
39   inv_n_rec v
40
41 class Inv vec where
42   inv_n_rec :: vec -> vec
43
44 instance (Pos len) => Inv (BitVec len) where
45   inv_n_rec v = 
46     h FSVec.+> t
47     where
48       h = FSVec.head v
49       t = FSVec.tail v
50
51 instance Inv (BitVec D0) where
52   inv_n_rec v = v
53 -}
54 -- Not really an adder either, but a slightly more complex example
55 inv :: Bit -> Bit
56 inv a = hwnot a
57
58 -- Not really an adder either, but a slightly more complex example
59 invinv :: Bit -> Bit
60 invinv a = hwnot (hwnot a)
61
62 -- Not really an adder either, but a slightly more complex example
63 dup :: Bit -> (Bit, Bit)
64 dup a = (a, a)
65
66 -- Not really an adder either, but a simple stateful example (D-flipflop)
67 dff :: Bit -> Bit -> (Bit, Bit)
68 dff d s = (s', q)
69   where
70     q = s
71     s' = d
72
73 type ShifterState = (Bit, Bit, Bit, Bit)
74 shifter :: Bit -> ShifterState -> (ShifterState, Bit)
75 shifter a s =
76   (s', o)
77   where
78     s' = (a, b, c, d)
79     (b, c, d, o) = s
80
81 -- Combinatoric stateless no-carry adder
82 -- A -> B -> S
83 no_carry_adder :: (Bit, Bit) -> Bit
84 no_carry_adder (a, b) = a `hwxor` b
85
86 -- Combinatoric stateless half adder
87 -- A -> B -> (S, C)
88 half_adder :: (Bit, Bit) -> (Bit, Bit)
89 {-# NOINLINE half_adder #-}
90 half_adder (a, b) = 
91   ( a `hwxor` b, a `hwand` b )
92
93 -- Combinatoric stateless full adder
94 -- (A, B, C) -> (S, C)
95 full_adder :: (Bit, Bit, Bit) -> (Bit, Bit)
96 full_adder (a, b, cin) = (s, c)
97   where
98     (s1, c1) = half_adder(a, b)
99     (s, c2)  = half_adder(s1, cin)
100     c        = c1 `hwor` c2
101
102 sfull_adder = stateless full_adder
103
104 -- Four bit adder
105 -- Explicit version
106 -- [a] -> [b] -> ([s], cout)
107 exp_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
108
109 exp_adder ([a3,a2,a1,a0], [b3,b2,b1,b0]) =
110   ([s3, s2, s1, s0], c3)
111   where
112     (s0, c0) = full_adder (a0, b0, Low)
113     (s1, c1) = full_adder (a1, b1, c0)
114     (s2, c2) = full_adder (a2, b2, c1)
115     (s3, c3) = full_adder (a3, b3, c2)
116
117 -- Any number of bits adder
118 -- Recursive version
119 -- [a] -> [b] -> ([s], cout)
120 rec_adder :: ([Bit], [Bit]) -> ([Bit], Bit)
121
122 rec_adder ([], []) = ([], Low)
123 rec_adder ((a:as), (b:bs)) = 
124   (s : rest, cout)
125   where
126     (rest, cin) = rec_adder (as, bs)
127     (s, cout) = full_adder (a, b, cin)
128
129 -- Four bit adder, using the continous adder below
130 -- [a] -> [b] -> ([s], cout)
131 --con_adder_4 as bs = 
132 --  ([s3, s2, s1, s0], c)
133 --  where
134 --    ((s0, _):(s1, _):(s2, _):(s3, c):_) = con_adder (zip ((reverse as) ++ lows) ((reverse bs) ++ lows))
135
136 -- Continuous sequential version
137 -- Stream a -> Stream b -> Stream (sum, cout)
138 --con_adder :: Stream (Bit, Bit) -> Stream (Bit, Bit)
139
140 -- Forward to con_adder_int, but supply an initial state
141 --con_adder pin =
142 --  con_adder_int pin Low
143
144 -- Stream a -> Stream b -> state -> Stream (s, c)
145 --con_adder_int :: Stream (Bit, Bit) -> Bit -> Stream (Bit, Bit)
146 --con_adder_int ((a,b):rest) cin =
147 --  (s, cout) : con_adder_int rest cout
148 --  where
149 --    (s, cout) = full_adder a b cin
150
151 -- vim: set ts=8 sw=2 sts=2 expandtab: