Add the type-alias Vector for TFVec to HardwareTypes, and don't export TFVec.TFVec...
[matthijs/master-project/cλash.git] / HighOrdAlu.hs
1 {-# LANGUAGE TemplateHaskell, ScopedTypeVariables, NoImplicitPrelude #-}
2
3 module HighOrdAlu where
4
5 import qualified Prelude as P
6 import CLasH.HardwareTypes
7 import CLasH.Translator.Annotations
8
9 constant :: NaturalT n => e -> Op n e
10 constant e a b = copy e
11
12 invop :: Op n Bit
13 invop a b = map hwnot a
14
15 andop :: (e -> e -> e) -> Op n e
16 andop f a b = zipWith f a b
17
18 -- Is any bit set?
19 --anyset :: (PositiveT n) => Op n Bit
20 anyset :: NaturalT n => (e -> e -> e) -> e -> Op n e
21 --anyset a b = copy undefined (a' `hwor` b')
22 anyset f s a b = constant (f a' b') a b
23   where 
24     a' = foldl f s a
25     b' = foldl f s b
26
27 xhwor = hwor
28
29 type Op n e = (Vector n e -> Vector n e -> Vector n e)
30 type Opcode = Bit
31
32 {-# ANN sim_input TestInput#-}
33 sim_input :: [(Opcode, Vector D4 (SizedInt D8), Vector D4 (SizedInt D8))]
34 sim_input = [ (High,  $(vectorTH ([4,3,2,1]::[SizedInt D8])), $(vectorTH ([1,2,3,4]::[SizedInt D8])))
35             , (High,  $(vectorTH ([4,3,2,1]::[SizedInt D8])), $(vectorTH ([1,2,3,4]::[SizedInt D8])))
36             , (Low,   $(vectorTH ([4,3,2,1]::[SizedInt D8])), $(vectorTH ([1,2,3,4]::[SizedInt D8]))) ]
37
38 {-# ANN actual_alu InitState #-}
39 initstate = High
40
41 alu :: Op n e -> Op n e -> Opcode -> Vector n e -> Vector n e -> Vector n e
42 alu op1 op2 opc a b =
43   case opc of
44     Low -> op1 a b
45     High -> op2 a b
46
47 {-# ANN actual_alu TopEntity #-}
48 actual_alu :: (Opcode, Vector D4 (SizedInt D8), Vector D4 (SizedInt D8)) -> Vector D4 (SizedInt D8)
49 --actual_alu = alu (constant Low) andop
50 actual_alu (opc, a, b) = alu (anyset (+) (0 :: SizedInt D8)) (andop (-)) opc a b
51
52 runalu = P.map actual_alu sim_input