-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.tex
355 lines (290 loc) · 7.38 KB
/
demo.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
\documentclass[10pt]{beamer}
\usetheme[progressbar=frametitle]{metropolis}
\usepackage{appendixnumberbeamer}
\usepackage{minted}
\usepackage{booktabs}
\usepackage[scale=2]{ccicons}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usepackage{xspace}
\newcommand{\themename}{\textbf{\textsc{metropolis}}\xspace}
\title{Clash - Haskell as an HDL}
\subtitle{A quick introduction}
% \date{\today}
\date{}
\author{Martijn Bastiaan <[email protected]>}
\institute{\includegraphics[height=1.2cm]{img/logo-fpga-innovation-crop.pdf}}
\titlegraphic{\hfill}
\begin{document}
\maketitle
%\begin{frame}{Table of contents}
% \setbeamertemplate{section in toc}[sections numbered]
% \tableofcontents[hideallsubsections]
%\end{frame}
\begin{frame}[fragile]{QBayLogic and Clash}
\begin{itemize}
\item \textbf{Clash}: high-level synthesis using Haskell
\item Started as a research project in 2009
\begin{itemize}
\item at the University of Twente (the Netherlands)
\item 20+ publications since then
\item Spawned many related research topics
\end{itemize}
\item \textbf{QBayLogic B.V.} founded in 2016
\item I joined in 2017 as software engineer
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Clash}
Clash $\approx$ Haskell without:
\begin{itemize}
\item General recursion
\item Types sized at runtime (e.g., lists)
\begin{itemize}
\item Lack of lists is amortized by \textbf{vectors}
\end{itemize}
\end{itemize}
Clash offers:
\begin{itemize}
\item Strong typing guarantees
\item Very high-level features without typical overhead
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Clash}
In this presentation we're going to build a very small CPU..
\begin{itemize}
\item .. with \emph{n} registers
\item .. operating on arbitrary \emph{number types}
\item .. executing one instruction per cycle
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Clash}
\begin{center}
\includegraphics[width=0.8\textwidth]{img/CPU.pdf}
\end{center}
\end{frame}
\begin{frame}[fragile]{Clash}
\begin{center}
\includegraphics[width=0.8\textwidth]{img/CPU-instrs.pdf}
\end{center}
\end{frame}
\begin{frame}[fragile]{Instruction set}
\begin{minted}{haskell}
data Instruction n numType
\end{minted}
\pause
\begin{minted}{haskell}
= Multiply (Reg n) (Reg n) (Reg n)
\end{minted}
\pause
\begin{minted}{haskell}
| Add (Reg n) (Reg n) (Reg n)
\end{minted}
\pause
\begin{minted}{haskell}
| Absolute (Reg n) (Reg n)
\end{minted}
\pause
\begin{minted}{haskell}
| Literal numType (Reg n)
\end{minted}
\pause
\begin{minted}{haskell}
| Jump Int8
\end{minted}
\pause
\begin{minted}{haskell}
type Reg n = Index n
\end{minted}
\end{frame}
\begin{frame}[fragile]{Clash}
\begin{center}
\includegraphics[width=0.8\textwidth]{img/CPU-decode.pdf}
\end{center}
\end{frame}
\begin{frame}[fragile]{Decoded instruction set}
Decoded ALU instructions are much simpler than the original instruction set:
\begin{minted}{haskell}
data ALUInstruction
= Add'
| Multiply'
| Absolute'
\end{minted}
\pause
Same goes for the the program counter instruction set:
\begin{minted}{haskell}
data PCInstruction
= Jump' Int8
| Succ
\end{minted}
\end{frame}
\begin{frame}[fragile]{Decoder}
\begin{minted}{haskell}
decode regs (Add x y z) =
(Add', Succ, regs !! x, regs !! y, z)
\end{minted}
\pause
\begin{minted}{haskell}
decode regs (Multiply x y z) =
(Multiply', Succ, regs !! x, regs !! y, z)
\end{minted}
\pause
\begin{minted}{haskell}
decode regs (Absolute x z) =
(Absolute', Succ, regs !! x, undefined, z)
\end{minted}
\pause
\begin{minted}{haskell}
decode regs (Literal x z) =
(Add', Succ, x, 0, z)
\end{minted}
\pause
\begin{minted}{haskell}
decode regs (Jump z) =
(undefined, Jump' z, undefined, undefined, 0)
\end{minted}
\end{frame}
\begin{frame}[fragile]{Clash}
\begin{center}
\includegraphics[width=0.8\textwidth]{img/CPU-ALU-PC.pdf}
\end{center}
\end{frame}
\begin{frame}[fragile]{ALU}
The ALU takes an ALU instruction and executes it:
\begin{minted}{haskell}
alu Add' x y = x + y
alu Multiply' x y = x * y
alu Absolute' x _ = abs x
\end{minted}
\pause
And the program counter does the same:
\begin{minted}{haskell}
nextPC pc Succ = pc + 1
nextPC _ (Jump' n) = n
\end{minted}
\end{frame}
\begin{frame}[fragile]{Clash}
\begin{center}
\includegraphics[width=0.8\textwidth]{img/CPU-misc.pdf}
\end{center}
\end{frame}
\begin{frame}[fragile]{Processor}
\begin{minted}{haskell}
proc instrs (regs, pc) _ = ((regs'', pc'), result)
where
\end{minted}
\pause
\begin{minted}{haskell}
(aluInstruction, pcInstruction, x, y, wrAddr) =
decode regs (instrs !! pc)
\end{minted}
\pause
\begin{minted}{haskell}
result = alu aluInstruction x y
\end{minted}
\pause
\begin{minted}{haskell}
regs' = replace wrAddr result regs
regs'' = replace 0 0 regs'
\end{minted}
\pause
\begin{minted}{haskell}
pc' = nextPC pc pcInstruction
\end{minted}
\end{frame}
\section{Simulation}
\begin{frame}[fragile]{Simulation: 16 bits}
\begin{minted}{haskell}
instrs :: Vec 4 (Instruction 3 Int16)
instrs = Literal 5 1
:> Literal 7 2
:> Add 1 2 1
:> Jump 2
:> Nil
regs :: Vec 3 Int16
regs = 0 :> 0 :> 0 :> Nil
results =
mealy (proc instrs) regs (pure ())
\end{minted}
\end{frame}
\begin{frame}[fragile]{Simulation: 16 bits}
\begin{verbatim}
$ clashi Proc.hs
*Proc> showX $ sampleN 8 results
[5, 7, 12, X, 19, X, 26, X]
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Simulation: vectors}
\begin{minted}{haskell}
instance (KnownNat n, Num a) => Num (Vec n a) where
(+) = zipWith (+)
(*) = zipWith (*)
(-) = zipWith (-)
abs = map abs
\end{minted}
\end{frame}
\begin{frame}[fragile]{Simulation: vectors}
\begin{minted}{haskell}
instrsVec :: Vec 4 (Instruction 3 (Vec 2 Int16))
instrsVec
= Literal (5 :> 6 :> Nil) 1
:> Literal (7 :> 8 :> Nil) 2
:> Add 1 2 1
:> Jump 2
:> Nil
regsVec :: Vec 3 (Vec 2 Int16)
regsVec = repeat (0 :> 0 :> Nil)
resultsVec =
mealy (proc instrs) regs (pure ())
\end{minted}
\end{frame}
\begin{frame}[fragile]{Simulation: vectors}
\begin{verbatim}
$ clashi Proc.hs
*Proc> showX $ sampleN 8 resultsVec
[<5,6>, <7,8>, <12,14>, X, <19,22>, X, <26,30>, X]
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Simulation: matrices}
\begin{minted}{haskell}
-- | Dot product on vectors
dot as bs = sum $ zipWith (*) as bs
\end{minted}
\begin{minted}{haskell}
-- | Matrix is a 2D "array"
type Matrix m n a = Vec m (Vec n a)
\end{minted}
\begin{minted}{haskell}
-- | Num instance for matrices
instance (.., Num a) => Num (Matrix m n a) where
(+) = zipWith (+)
(-) = zipWith (-)
a*b = transpose (zipWith dot a (transpose b))
abs = map abs
\end{minted}
\end{frame}
\begin{frame}[fragile]{Conclusion}
We've built a small CPU that handles
\begin{itemize}
\item Integers, floats, vectors, ..
\item .. any other ``Num instances''
\end{itemize}
\pause
We've used our own datatypes to:
\begin{itemize}
\item define an internal instruction set
\item \emph{use} that IS as an assembly-type language in simulation
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\vfill
\begin{center}
{\Huge Q\&A}
\end{center}
\vfill
\begin{center}
\includegraphics[width=0.6\textwidth]{img/logo-fpga-innovation-crop.pdf} \\
Martijn Bastiaan <[email protected]> \\
http://clash-lang.org/
\end{center}
\end{frame}
\end{document}