Alu.hs now uses the new CLasH.HardwareTypes
[matthijs/master-project/cλash.git] / Alu.hs
1 module Alu where
2 import qualified Sim
3 import CLasH.HardwareTypes hiding (fst,snd)
4 import CLasH.Translator.Annotations
5 import qualified Prelude as P
6
7 fst (a, b) = a
8 snd (a, b) = b
9
10 main = Sim.simulate exec program initial_state
11 mainIO = Sim.simulateIO exec initial_state
12
13 dontcare = Low
14
15 newtype State s = State s deriving (P.Show)
16
17 program = [
18             -- (addr, we, op)
19             (High, Low, High), -- z = r1 and t (0) ; t = r1 (1)
20             (Low, Low, Low), -- z = r0 or t (1); t = r0 (0)
21             (Low, High, dontcare), -- r0 = z (1)
22             (High, Low, High), -- z = r1 and t (0); t = r1 (1)
23             (High, High, dontcare) -- r1 = z (0)
24           ]
25
26 --initial_state = (Regs Low High, Low, Low)
27 initial_state = State (State (0, 1), 0, 0)
28
29 type Word = SizedWord D4
30 -- Register bank
31 type RegAddr = Bit
32 type RegisterBankState = State (Word, Word)
33 --data RegisterBankState = Regs { r0, r1 :: Bit} deriving (Show)
34
35 register_bank :: 
36   RegAddr -- ^ Address
37   -> Bit -- ^ Write Enable
38   -> Word -- ^ Data
39   -> RegisterBankState -> -- State
40   (RegisterBankState, Word) -- (State', Output)
41
42 register_bank addr we d (State s) =
43   case we of
44     Low -> -- Read
45       let
46         o = case addr of Low -> fst s; High -> snd s
47       in (State s, o) -- Don't change state
48     High -> -- Write
49       let
50         (r0, r1) = s
51         r0' = case addr of Low -> d; High -> r0
52         r1' = case addr of High -> d; Low -> r1
53         s' = (r0', r1')
54       in (State s', 0) -- Don't output anything useful
55
56 -- ALU
57
58 type AluOp = Bit
59
60 alu :: AluOp -> Word -> Word -> Word
61 {-# NOINLINE alu #-}
62 --alu High a b = a `hwand` b
63 --alu Low a b = a `hwor` b
64 alu High a b = a P.+ b
65 alu Low a b = a P.- b
66
67 type ExecState = State (RegisterBankState, Word, Word)
68 exec :: (RegAddr, Bit, AluOp) -> ExecState -> (ExecState, Word)
69
70 {-# ANN exec TopEntity #-}
71 -- Read & Exec
72 exec (addr, we, op) (State s) =
73   (State s', z')
74   where
75     (reg_s, t, z) = s
76     (reg_s', t') = register_bank addr we z reg_s
77     z' = alu op t' t
78     s' = (reg_s', t', z')
79
80 -- vim: set ts=8 sw=2 sts=2 expandtab: