4 {-# LANGUAGE TypeOperators, TypeFamilies, FlexibleContexts #-}
7 import CLasH.HardwareTypes
8 import CLasH.Translator.Annotations
9 import qualified Prelude as P
13 \section{Polymorphic, Higher-Order CPU}
14 \subsection{Introduction}
17 \frametitle{Small Use Case}
19 \column{0.5\textwidth}
21 \includegraphics[width=4.75cm]{simpleCPU}
23 \column{0.5\textwidth}
25 \item Polymorphic, Higher-Order CPU
26 \item Use of state will be simple
30 \item Small "toy"-example of what can be done in \clash{}
31 \item Show what can be translated to Hardware
32 \item Put your hardware glasses on: each function will be a component
33 \item Use of state will be kept simple
36 \subsection{Type Definitions}
39 \frametitle{Type definitions}\pause
41 \column{0.5\textwidth}
43 \includegraphics[width=4.75cm]{simpleCPU}
45 \column{0.5\textwidth}
48 First we define some ALU types:
49 \begin{beamercolorbox}[sep=-2.5ex,rounded=true,shadow=true,vmode]{codebox}
51 type Op a = a -> a -> a
53 \end{beamercolorbox}\pause
55 And some Register types:
56 \begin{beamercolorbox}[sep=-2.5ex,rounded=true,shadow=true,vmode]{codebox}
66 type Word = SizedInt D12
71 \item The first type is already polymorphic in input / output type
72 \item State has to be of the State type to be recognized as such
75 \subsection{Polymorphic, Higher-Order ALU}
78 \frametitle{Simple ALU}
80 \includegraphics[width=5.25cm,trim=0mm 5.5cm 0mm 1cm, clip=true]{simpleCPU}
82 Abstract ALU definition:
83 \begin{beamercolorbox}[sep=-2.5ex,rounded=true,shadow=true,vmode]{codebox}
89 alu op1 op2 {-"{\color<2>[rgb]{1,0,0}"-}Low{-"}"-} a b = op1 a b
90 alu op1 op2 {-"{\color<2>[rgb]{1,0,0}"-}High{-"}"-} a b = op2 a b
94 \item Alu is both higher-order, and polymorphic
95 \item Two parameters are "compile time", others are "runtime"
96 \item We support pattern matching
99 \subsection{Register bank}
102 \frametitle{Register Bank}
104 \includegraphics[width=5.25cm,trim=0mm 0.4cm 0mm 6.2cm, clip=true]{simpleCPU}
109 CXT((NaturalT s ,PositiveT (s :+: D1),((s :+: D1) :>: s) ~ True )) => a -> RangedWord s ->
110 RangedWord s -> (RegState s a) -> (RegState s a, a )
113 A simple register bank:
114 \begin{beamercolorbox}[sep=-2.5ex,rounded=true,shadow=true,vmode]{codebox}
116 registers data_in rdaddr wraddr (State mem) =
117 ((State mem'), data_out)
119 data_out = mem!rdaddr
120 mem' = replace mem wraddr data_in
124 \item RangedWord runs from 0 to the upper bound
125 \item mem is statefull
126 \item We support guards
127 \item replace is a builtin function
130 \subsection{Simple CPU: ALU \& Register Bank}
133 \frametitle{Simple CPU}
134 Combining ALU and register bank:
135 \begin{beamercolorbox}[sep=-2.5ex,rounded=true,shadow=true,vmode]{codebox}
138 type Instruction = (Opcode, Word, RangedWord D9, RangedWord D9)
142 {-"{\color<2>[rgb]{1,0,0}"-}ANN(cpu TopEntity){-"}"-}
144 Instruction -> RegState D9 Word -> (RegState D9 Word, Word)
146 cpu (opc, d, rdaddr, wraddr) ram = (ram', alu_out)
148 alu_out = alu {-"{\color<3>[rgb]{1,0,0}"-}(+){-"}"-} {-"{\color<3>[rgb]{1,0,0}"-}(-){-"}"-} opc d ram_out
149 (ram',ram_out) = registers alu_out rdaddr wraddr ram
153 \uncover<2->{\item Annotation is used to indicate top-level component}
154 \uncover<3->{\item Instantiate actual operations}
157 \item We use the new Annotion functionality to indicate this is the top level. TopEntity is defined by us.
158 \item the primOp and vectOp frameworks are now supplied with real functionality, the plus (+) operations
159 \item No polymorphism or higher-order stuff is allowed at this level.
160 \item Functions must be specialized, and have primitives for input and output
165 ANN(initstate InitState)
166 initstate :: RegState D9 Word
167 initstate = State (copy (0 :: Word))
169 ANN(program TestInput)
170 program :: [Instruction]
172 [ (Low, 4, 0, 0) -- Write 4 to Reg0
173 , (Low, 3, 0, 1) -- Write 3+4 to Reg1
174 , (High,8, 1, 2) -- Write 8-7 to Reg2
177 run func state [] = []
178 run func state (i:input) = o:out
180 (state', o) = func i state
181 out = run func state' input
186 let istate = initstate
187 let output = run cpu istate input
188 mapM_ (\x -> putStr $ ("(" P.++ (show x) P.++ ")\n")) output