forked from schoeberl/chisel-book
-
Notifications
You must be signed in to change notification settings - Fork 13
/
chisel-book-chinese.tex
5683 lines (4262 loc) · 278 KB
/
chisel-book-chinese.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[%draft,
10pt,
headinclude, footexclude,
% twoside, % this produces strange margins!
openright, % for new chapters
notitlepage,
cleardoubleempty,
headsepline,
pointlessnumbers,
bibtotoc, idxtotoc,
]{scrbook}
% iPad Air 2 resolution: 1536 x 2048
% 1.3333 ratio (4:3)
% LeeSeshia is 15,73 ? 20,96 cm, ratio 1.332
%\setlength{\paperwidth}{15.73cm} \setlength{\paperheight}{20.96cm}
\setlength{\paperwidth}{15.72cm} \setlength{\paperheight}{20.95cm}
%\typearea{calc} % without BCOR results to a DIV of 8 for 11pt
\typearea[1cm]{18} % TODO: check if 1 cm is the correct number
\usepackage{scrlayer-scrpage}
\setkomafont{pagehead}{\scshape\small}
\setkomafont{pagenumber}{\scshape\small}
\newif\ifbook
%\booktrue % comment out for the eBook version
\ifbook
\else
\cfoot{\hyperlink{contents}{{\textnormal{Contents}}}}
\fi
\usepackage{CJKutf8}
\usepackage{pslatex} % -- times instead of computer modern, especially for the plain article class
\usepackage[colorlinks=true,bookmarks=false]{hyperref}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{multirow}
\usepackage{cite}
\usepackage{dirtree}
\usepackage{pdfpages}
% For alignment on tables
\usepackage{dcolumn}
\newcommand{\cc}[1]{\multicolumn{1}{c}{#1}}
\newcommand{\code}[1]{{\small{\texttt{#1}}}}
\newcommand{\codefoot}[1]{{\footnotesize{\texttt{#1}}}}
% fatter TT font
\renewcommand*\ttdefault{txtt}
% another TT, suggested by Alex
% \usepackage{inconsolata}
% \usepackage[T1]{fontenc} % needed as well?
\usepackage[procnames]{listings}
\ifbook
\hypersetup{
linkcolor = black,
citecolor = black,
urlcolor = black,
colorlinks = black,
bookmarks=false,
}
\else
\hypersetup{
linkcolor = blue,
citecolor = blue,
urlcolor = blue,
colorlinks = true,
bookmarks=true,
}
\fi
% not really used
\newenvironment{comment}
{ \vspace{-0.1in}
\begin{quotation}
\noindent
\small \em
\rule{\linewidth}{0.5pt}\\
}
{
\\
\rule{\linewidth}{0.5pt}
\end{quotation}
}
\newcommand{\scale}{0.7}
\input{shared/chisel.tex}
\newcommand{\todo}[1]{{\emph{TODO: #1}}}
\newcommand{\martin}[1]{{\color{blue} Martin: #1}}
\newcommand{\myref}[2]{\href{#1}{#2}}
\ifbook
\renewcommand{\myref}[2]{{#2}{\footnote{\url{#1}}}}
\fi
% uncomment following for final submission
\renewcommand{\todo}[1]{}
\renewcommand{\martin}[1]{}
\begin{document}
\ifbook
\else
\includepdf{chisel-cover.pdf}
\newpage
\thispagestyle{empty}
~
\newpage
\fi
\begin{flushleft}
\pagestyle{empty}
\ \\
\vspace{1cm}
{\usekomafont{title}\mdseries\huge Digital Design with Chisel}
\cleardoublepage
\end{flushleft}
\newpage
\begin{flushleft}
\pagestyle{empty}
\ \\
\vspace{1cm}
{\usekomafont{title}\Huge Digital Design with Chisel\\
\bigskip
\bigskip
\bigskip
\bigskip
\bigskip
%{\large\itshape Beta Edition}\\
\bigskip
{\usekomafont{title}\huge Martin Schoeberl}
\medskip\\
%{\large\itshape [email protected]}
}
%\vspace{10cm} \emph{Version: \today}
\newpage
\end{flushleft}
\thispagestyle{empty}
\begin{flushleft}
{\small
%\lowertitleback{
Copyright \copyright{} 2016--2019 Martin Schoeberl
\medskip\\
\begin{tabular}{lp{.8\textwidth}}
\raisebox{-12pt}{\includegraphics[height=18pt]{figures/cc_by_sa}} &
This work is licensed under a Creative Commons Attribution-ShareAlike
4.0 International License.
\url{http://creativecommons.org/licenses/by-sa/4.0/}\\
\end{tabular}
%}
\medskip
Email: \url{[email protected]}\\
Visit the source at \url{https://github.com/schoeberl/chisel-book}
\medskip
Published 2019 by Kindle Direct Publishing,\\
\url{https://kdp.amazon.com/}
\medskip
\medskip
\textbf{Library of Congress Cataloging-in-Publication Data}
\medskip
Schoeberl, Martin
\begin{quote}
Digital Design with Chisel\\
Martin Schoeberl\\
Includes bibliographical references.\\ \todo{and index. \\}
ISBN 9781689336031
\end{quote}
\bigskip
Manufactured in the United States of America.
Typeset by Martin Schoeberl.}
\end{flushleft}
\frontmatter
\phantomsection
\hypertarget{contents}{}
\tableofcontents
\begingroup
\let\cleardoublepage\clearpage
\listoffigures
\listoftables
\lstlistoflistings
\endgroup
%\chapter*{Foreword}
%
%Here comes the text.
%
%\bigskip
%
%Jonathan Bachrach, designer of Chisel
\chapter{Preface}
% This text goes on the backside of the book, and in Amazon description
% 该文本在书本的背面,和亚马逊的介绍
This book is an introduction to digital design with the focus on using the hardware construction language Chisel. Chisel brings advances from software engineering, such as object-orientated and functional languages, into digital design.
This book addresses hardware designers and software engineers. Hardware designers, with knowledge of Verilog or VHDL,
can upgrade their productivity with a modern language for their next ASIC or FPGA design.
Software engineers, with knowledge of object-oriented and functional programming, can leverage their knowledge to program hardware, for example, FPGA accelerators executing in the cloud.
The approach of this book is to present small to medium-sized typical hardware components to explore digital design with Chisel.
这本书是一个专注于使用硬件构建语言chisel进行数字设计的导论。Chisel带来了软件工程的优势,像是面向对象和函数编程,进入数字设计。
这本书是给硬件设计师和软件工程师的。硬件设计师,有着Verilog或VHDL的知识,可以通过新型语言增加生产力,用于下一个ASIC或是FPDA设计。
软件工程师,有着面向对象语言和函数编程的知识,可以把这些知识用于生产硬件,例如,云FPGA。
这本书的方法是呈现小型到中型大小的典型硬件部分,去探索使用chisel做数字设计。
% about me, backside
%Martin Schoeberl is Associate Professor at the Technical University of Denmark, where he is teaching digital electronics and computer architecture. His research interest is on hard real-time systems, time-predictable computer architecture, and real-time Java. He has more than 100 publications in peer reviewed journals, conferences, and books.
%Martin has been four times at UC Berkeley on research stays, where he has picked up Chisel and was in close contact with the developers of Chisel. He lead the research project T-CREST where most of the components have been written in Chisel.
\section*{Acknowledgements}
I want to thank everyone who has worked on Chisel for creating such
a cool hardware construction language. Chisel is so joyful to use and
therefore worth writing a book about.
I am thankful to the whole Chisel community, which is so welcoming and friendly
and never tired to answer questions on Chisel.
I would also like to thank my students in the last years of an advanced computer
architecture course where most of them picked up Chisel for the final project.
Thank you for moving out of your comfort zone and taking up the journey of
learning and using a bleeding-edge hardware description language.
Many of your questions have helped to shape this book.
我想感谢所有在chisel上进行开发的人,他们创造了这样一个酷的硬件构建语言。chisel使用起来非常愉快,
于是写这样一本书很值得。
我很感谢整个chisel社区,非常对外欢迎友好,并且我从不对在chisel问问题感到疲惫。
我也想感谢去年在我的高级计算机架构课上的学生,他们大多数选择使用chisel作为最终项目。
感谢你们走出舒适区,走入了学习和使用前沿的硬件构建语言。
你们大多数的问题帮助规范了这本书。
\mainmatter
\chapter{Introduction}
\label{sec:intro}
This book is an introduction to digital system design using a modern hardware
construction language, \myref{https://chisel.eecs.berkeley.edu/}{Chisel}~\cite{chisel:dac2012}.
In this book, we focus on a higher abstraction level than usual in digital design books,
to enable you to build more complex, interacting digital systems in a shorter time.
这本书是一个使用现代硬件构建语言Chisel做数字系统设计的导论,
\myref{https://chisel.eecs.berkeley.edu/}{Chisel}~\cite{chisel:dac2012}。
在这本书,我们聚焦在比平常的硬件设计更高抽象层,
使在短时间搭建更为复杂和交互性的硬件系统变得可能。
This book and Chisel are targeting two groups of developers:
(1) hardware designers and (2) software programmers.
Hardware designers who are fluid in VHDL or Verilog and using other languages such as Python,
Java, or Tcl to generate hardware can move to a single hardware construction language
where hardware generation is part of the language and
Software programmers may become interested in hardware design,
e.g., as future chips from Intel will include programmable hardware to speed up programs.
It is perfectly fine to use Chisel as your first hardware description language.
这本书和Chisel的目标群体是两种开发者:
(1)硬件设计者 (2)软件编程者
硬件设计者,精通VHDL或是Verilog,使用其它语言,像是Python, Java, 或是TCL/TK去生成硬件,使其变成一个硬件建造语言,这样,生成硬件作为语言的一部分。
软件编程者,对硬件设计有兴趣的软件程序员,例如,像是Intel未来的芯片会添加可编程硬件用来提高程序速度。
Chisel brings advances in software engineering, such as object-orientated
and functional languages, into digital design.
Chisel does not only allow to express hardware at the register-transfer level
but allows you to write hardware generators.
Chisel将诸如面向对象和函数式语言之类的软件工程的优势引入数字系统设计。
Chisel不仅仅允许表示硬件在寄存器传输的抽象层,而且允许你编写硬件生成器。
Hardware is now commonly described with a hardware description language.
The time of drawing hardware components, even with CAD tools, is
over. Some high-level schematics can give an overview of the system but are
not intended to describe the system.
硬件现在普遍通过硬件描述语言进行描述。绘制硬件部件,甚至利用CAD工具的时代已经过去了。
一些高层次的原理图可以给你一个系统的整体概述,但它们并不用来描述系统。
The two most common hardware description languages are Verilog and VHDL.
Both languages are old, contain many legacies, and have a moving line of what
constructs of the language are synthesizable to hardware.
Do not get me wrong: VHDL and Verilog are perfectly able to describe a hardware
block that can be synthesized into an
\myref{https://en.wikipedia.org/wiki/Application-specific_integrated_circuit}{ASIC}.
For hardware design in Chisel, Verilog serves as an intermediate language
for testing and synthesis.
两个最常用的硬件描述语言是Verilog和VHDL。
这两个语言是古老的,包括大量规则,并且在综合到硬件的构建语言之间有一个变动的规则。
别理解错我的意思:VHDL和Verilog可以完美描述用于综合成\myref{https://en.wikipedia.org/wiki/Application-specific_integrated_circuit}{ASIC}的硬件模块。
对于Chisel的硬件设计,verilog充当一个测试和综合的中间语言。
This book is not a general introduction to hardware design and the fundamentals of it.
For an introduction of the basics in digital design, such as how to build a gate out of
CMOS transistors, refer to other digital design books.
However, this book intends to teach digital design at an abstraction level that is
current practice to describe ASICs or designs targeting
\myref{https://en.wikipedia.org/wiki/Field-programmable_gate_array}{FPGA}s.\footnote{As the author is more familiar with FPGAs
than ASICs as target technology, some design optimizations shown in this book are
targeting FPGA technology.}
As prerequisites for this book, we assume basic knowledge of
\myref{https://en.wikipedia.org/wiki/Boolean_algebra}{Boolean algebra} and the
\myref{https://en.wikipedia.org/wiki/Binary_number}{binary number system}.
Furthermore, some programming experience in any programming language
is assumed. No knowledge of Verilog or VHDL is needed.
Chisel can be your first programming language to describe digital hardware.
As the build process in the examples is based on \code{sbt} and \code{make}
basic knowledge of the command-line interface (CLI, also called terminal or
Unix shell) will be helpful.
这本书不是对硬件的设计和基础一般性介绍。
对有关数字设计基础知识的介绍,例如如何使用CMOS晶体管搭建一个门电路,你要参考其它数字设计书。
但是,这本书的目的是教你在一个抽象层中进行数字设计,作为当今描述ASIC或是设计
\myref{https://en.wikipedia.org/wiki/Field-programmable_gate_array}{FPGA}的例子。
\footnote{因为作者相比于ASIC,更熟悉FPGA一些,书中的一些优化是针对FPGA的技术}
作为这本书的前置需求,我们假设你有一些基本的\myref{https://en.wikipedia.org/wiki/Boolean_algebra}{Boolean algebra}和\myref{https://en.wikipedia.org/wiki/Binary_number}{binary number system}的知识。
更多的,一些任意编程语言的编程经验也是需要的。不需要Verilog或是VHDL的知识。
Chisel能够成为你的第一个编程语言用来描述数字硬件。
作为例子中的搭建过程是基于\code{sbt}和\code{make},基本的命令行界面知识(CLI,又称terminal或是Unix shell)会是有用的。
Chisel itself is not a big language. The basic constructs fit on
\myref{https://chisel.eecs.berkeley.edu/doc/chisel-cheatsheet3.pdf}{one page}
and can be learned within a few days.
Therefore, this book is not a big book, as well.
Chisel is for sure smaller than VHDL and Verilog, which carry many legacies.
The power of Chisel comes from the embedding of Chisel within
\myref{https://www.scala-lang.org/}{Scala}, which itself in an expressive language.
Chisel inherits the feature from Scala being ``a language that grows on you''~\cite{Scala}.
However, Scala is not the topic of this book.
Chisel用来本身不是一个大的语言。基本的部分在这里\myref{https://chisel.eecs.berkeley.edu/doc/chisel-cheatsheet3.pdf}{one page}, 可以在数日内学习。
于是,这本书也不是一本大书。Chisel应该是比VHDL和Verilog更小,
继承很多规则。Chisel的力量来自于它是嵌入在\myref{https://www.scala-lang.org/}{Scala}里的,Scala本身是一个有力的语言。
Chisel继承Scala作为``a language that grows on you''~\cite{Scala}的特性
但是,Scala不是本书的话题。
% We provide a short section on Scala for hardware designers, but
%A general introduction to Scala is better served by
The textbook by Odersky et al.~\cite{Scala} provides a general introduction
to Scala.
我们提供了一个短的章节给硬件设计者,但是Odersky~\cite{Scala}提供了一个更好的Scala导论。
This book is a tutorial in digital design and the Chisel language; it is not
a Chisel language reference, nor is it a book on complete chip design.
这本书是一个数字设计和Chisel语言的教学,不是Chisel语言的参考书籍也不是一本完整的芯片设计书
All code examples shown in this book are extracted from complete programs
that have been compiled and tested. Therefore, the code shall not contain
any syntax errors. The code examples are available from the
\myref{https://github.com/schoeberl/chisel-book}{GitHub repository}
of this book.
Besides showing Chisel code, we have also tried to show useful designs and
principles of good hardware description style.
所有的书中代码例子来自经过编译和测试过的程序。所以,代码不应该含有任何语法问题。
代码例子在本书的\myref{https://github.com/schoeberl/chisel-book}{GitHub repository}里。除了提供Chisel代码以外,我也试图提供有用的设计和好的硬件描述风格的规则。
This book is optimized for reading on a laptop or tablet (e.g., an iPad).
We include links to further reading in the running text, mostly to
\myref{https://en.wikipedia.org/}{Wikipedia} articles.
这本书在笔电或是平板上为阅读经过了优化。我们在字里行间提供了链接,大多数是\myref{https://en.wikipedia.org/}{Wikipedia}的文章。
\section{Installing Chisel and FPGA Tools}
Chisel is a Scala library, and the easiest way to install Chisel and Scala is
with \code{sbt}, the Scala build tool. Scala itself depends on the installation
of the \myref{https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html}{Java JDK 1.8}.
Chisel是一个Scala库,安装Chisel和Scala最简单的方法时通过\code{sbt}, Scala Build Tool。
Scala本身依赖于安装\myref{https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html}{Java JDK 1.8}。
On Mac OS X, with the packet manager \myref{https://brew.sh/}{Homebrew},
\code{sbt} is installed with:
在Mac OS X中,通过包管理软件\myref{https://brew.sh/}{Homebrew},sbt可以通过此命令安装
\begin{verbatim}
$ brew install sbt
\end{verbatim}
\noindent For Ubuntu, which is based on Debian, programs are usually installed from a
Debian file (.deb). However, as of the time of this writing \code{sbt} is not
available as a ready to install package. Therefore, the installation process
is a little bit more involved:
对于基于Debian的Ubuntu系统,软件通常通过.deb文件进行安装。
然而,当编写本文时,\code{sbt}还不能通过这种方法安装。
因此,安装过程有点麻烦:
\begin{verbatim}
echo "deb https://dl.bintray.com/sbt/debian /" | \
sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 \
--recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt
\end{verbatim}
Chisel and Scala can also be installed and used under Windows.
\code{sbt} can be installed with a Windows installer, see:
\myref{https://www.scala-sbt.org/1.x/docs/Installing-sbt-on-Windows.html}{Installing sbt on Windows}.
Chisel和Scala也支持Windows操作系统。
\code{sbt} 可以使用windows安装器安装,参照:
\myref{https://www.scala-sbt.org/1.x/docs/Installing-sbt-on-Windows.html}{在windows安装sbt}.
To build hardware for an FPGA, you need a synthesize tool. The two major
FPGA vendors, Intel\footnote{former Altera} and Xilinx, provide free versions of
their tools that cover small to medium-sized FPGAs. Those medium-sized
FPGAs are large enough to build multicore RISC style processors.
Intel provides the \myref{https://www.altera.com/products/design-software/fpga-design/quartus-prime/download.html}{Quartus Prime Lite Edition} and Xilinx the
\myref{https://www.xilinx.com/products/design-tools/vivado/vivado-webpack.html}{Vivado Design Suite, WebPACK Edition}.
为了构建FPGA硬件,你需要综合工具。两大主要的FPGA工具供应商,Intel\footnote{曾经是Altera}和Xilinx都提供了他们免费的工具,涵盖了小容量到中等容量大小的FPGA。
其中,中等规模的FPGA已经足够容纳多核RISC架构处理器。
Intel提供了\myref{https://www.altera.com/products/design-software/fpga-design/quartus-prime/download.html}{Quartus Prime Lite Edition}
和\myref{https://www.xilinx.com/products/design-tools/vivado/vivado-webpack.html}{Vivado Design Suite, WebPACK Edition}。
\section{Hello World}
Each book on a programming language shall start with a minimal example,
called the \emph{Hello World} example. Following code is the first approach:
每一本编程语言书都会从叫做\emph{Hello World}的最小例子开始。以下代码是第一种方法。
\shortlist{src/main/scala/HelloScala.scala}
\noindent Compiling and executing this short program with \code{sbt}
编译、执行这个简短的程序通过\code{sbt}:
\begin{chisel}
$ sbt "runMain HelloScala"
\end{chisel}
\noindent leads to the expected output of a Hello World program:
随后,Hello World程序输出了预期的内容:
\begin{chisel}
[info] Running HelloScala
Hello Chisel World!
\end{chisel}
\noindent However, is this Chisel? Is this hardware generated to print a string?
No, this is plain Scala code and not a representative Hello World
program for a hardware design.
然而,这就是Chisel吗?这个硬件被生成用于出书字符串吗?并不是这样,这里实际上是解释了Scala代码,并不代表着硬件设计的Hello World程序。
\section{Chisel Hello World}
What is then the equivalent of a Hello World program for a hardware design?
The minimal useful and visible design? A blinking LED is the hardware (or even
embedded software) version of Hello World. If a LED blinks, we are ready to
solve bigger problems!
那么Hello World程序等价的硬件设计是什么?是最小的可用并且可视的设计?一个闪烁的LED是硬件(或者嵌入式软件)版本的Hello World。
如果一个LED闪烁,我们就可以解决更大的问题。
\longlist{code/hello.txt}{A hardware Hello World in Chisel}{lst:chisel:hello}
Listing~\ref{lst:chisel:hello} shows a blinking LED, described in Chisel.
It is not important that you understand the details of this code example.
We will cover those in the following chapters. Just note that the circuit is
usually clocked with a high frequency, e.g., 50 MHz, and we need a counter
to derive timing in the Hz range to achieve a visible blinking. In the above
example, we count from 0 up to 25000000-1 and then toggle the blinking signal
(\code{blkReg := \textasciitilde blkReg}) and restart the counter (\code{cntReg := 0.U}).
That hardware then blinks the LED at 1~Hz.
F~\ref{fig:chisel:hello}展现的是一个通过Chisel描述的闪烁LED。你是否理解这个代码案例的细节是不重要的。我们将会介绍这些内容在以下的章节中。
仅仅需要注意这个电路通常有着高频率的时钟,例如50MHz,我们需要一个计数器来分频得到Hz级别的频率来实现可视化的闪烁。在这个例子中我们从0计数到25000000-1然后触发闪烁信号(\code{blkReg := \textasciitilde blkReg})
然后复位计数器 (\code{cntReg := 0.U})。这个硬件以1~Hz频率闪烁LED。
\section{An IDE for Chisel}
This book makes no assumptions about your programming environment or editor to use.
Learning of the basics should be easy with just using \code{sbt} at the command line
and an editor of your choice. In the tradition of other books, all commands that you
shall type in a shell/terminal/CLI are preceded by a \code{\$} character, which you
shall not type in. As an example, here is the Unix \code{ls} command, which lists files in
the current folder:
这本书没有假设你的编程环境和使用的编辑器。
在命令行中使用\code{sbt}同时使用一个你偏好的编辑器可以很容易的入门。在其他传统的书籍中,
所有你需要键入终端的命令之前会有一个\code{\$}字符,这个字符你不需要输入。一个例子,Unix的列出当前文件夹下的文件\code{ls}
命令:
\begin{verbatim}
$ ls
\end{verbatim}
That said, an integrated development environment (IDE), where a compiler is running in
the background, can speed up coding. As Chisel is a Scala library, all IDEs
that support Scala are also good IDEs for Chisel.
It is possible in \myref{https://www.eclipse.org/}{Eclipse} and
\myref{https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html}{IntelliJ}
to generate a project from the sbt project configuration in \code{build.sbt}.
You can create an Eclipse project via
也就是说,一个编译器后台运行的集成开发环境(IDE),可以加速编程。因为Chisel是一个Scala库,所有的IDEs都支持Scala,也支持Chisel。
在 \myref{https://www.eclipse.org/}{Eclipse} 和
\myref{https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html}{IntelliJ}中生成一个包含\code{build.sbt}配置文件的sbt工程是可行了。
你可以创建一个Eclipse工程通过
\begin{verbatim}
$ sbt eclipse
\end{verbatim}
and import that project into Eclipse.\footnote{This function needs the Eclipse plugin for sbt.}
In IntelliJ you can create a new project from existing sources and then select from \code{build.sbt}.
或者导入一个工程入Eclipse。\footnote{This function needs the Eclipse plugin for sbt.}
在InterlliJ中你可以创建一个工程从已经存在的源码中,然后选择\code{build.sbt}文件。
\myref{https://code.visualstudio.com/}{Visual Studio Code} with
\myref{https://marketplace.visualstudio.com/items?itemName=scalameta.metals/}{Scala Metals} requires
no setup beyond installing Visual Studio Code and the Scala Metals plugin. Scala Metals knows how to
set up everything including but not limited to code completion, Chisel source code download and
building based on \code{build.sbt}.
\myref{https://code.visualstudio.com/}{Visual Studio Code} 和
\myref{https://marketplace.visualstudio.com/items?itemName=scalameta.metals/}{Scala Metals}只需要
安装VS code和Scala Metals插件。Scala Metals知道如何去设置所有的东西不包括但不限于代码补全,Chisel源代码下载和基于\code{build.sbt}的配置。
\section{Source Access and eBook Features}
This book is open source and hosted at GitHub: \myref{https://github.com/schoeberl/chisel-book}{chisel-book}.
All Chisel code examples, shown in this book, are included in the repository.
The code compiles with a recent version of Chisel, and many examples also include a test bench.
We collect larger Chisel examples in the accompanying repository \myref{https://github.com/schoeberl/chisel-examples}{chisel-examples}. If you find an error or typo in the book, a GitHub pull request is the most convenient way to incorporate your improvement.
You can also provide feedback or comments for improvements by filing an issue on GitHub
or sending a plain, old school email.
这本书是在GitHub: \myref{https://github.com/schoeberl/chisel-book}{chisel-book}上开源。书中所有的Chisel例子都包含在该仓库中。
这些代码通过最近的Chisel版面编译,很多例子都包含了test bench。
我们收集了很多Chisel例子在\myref{https://github.com/schoeberl/chisel-examples}{chisel-examples}仓库里。如果你找到了错误或者笔误,可以提交pull request。
你可以在github上记录一个issue来提供反馈或者评论或者发送一个简单的学校邮件。
This book is freely available as a PDF eBook and in classical printed form.
The eBook version features links to further resources
and \myref{https://www.wikipedia.org/}{Wikipedia} entries.
We use Wikipedia entries for background information (e.g., binary number system)
that does not directly fit into this book.
We optimized the format of the eBook for reading on a tablet, such as an iPad.
这本书有免费的古典印刷体PDF电子版。
电子书提供了更多的资源和\myref{https://www.wikipedia.org/}{Wikipedia}入口。
我们使用Wikipedia来说明与本书部直接相关的背景知识(例如二进制数字系统)。
我们优化了电子书的排版,以方便便携式设备阅读,例如ipad。
\section{Further Reading}
Here a list of further reading for digital design and Chisel:
这儿例出了关于数字电路设计和Chisel更深层次的读物:
\begin{itemize}
\item \myref{http://www.cambridge.org/es/academic/subjects/engineering/circuits-and-systems/digital-design-systems-approach}{Digital Design: A Systems Approach}, by William J. Dally and R. Curtis Harting,
is a modern textbook on digital design. It is available in two versions: using Verilog or VHDL as a hardware description language.
\end{itemize}
\myref{http://www.cambridge.org/es/academic/subjects/engineering/circuits-and-systems/digital-design-systems-approach}{Digital Design: A Systems Approach}, 作者是William J. Dally 和 R. Curtis Harting,
,是一本现代的数字设计的教科书。它有两个版本,使用Verilog或是VHDL作为硬件描述语言。
\end{itemize}
The official Chisel documentation and further documents are available online:
Chisel官方文档和之后的文档都会更新到网上:
\begin{itemize}
\item The \myref{https://chisel.eecs.berkeley.edu/}{Chisel} home page is the official starting point to
download and learn Chisel.
\myref{https://chisel.eecs.berkeley.edu/}{Chisel}主页是开始安装和学习chisel的地方。
\item The \myref{https://github.com/ucb-bar/chisel-tutorial}{Chisel Tutorial} provides a ready setup
project containing small exercises with testers and solutions.
\myref{https://github.com/ucb-bar/chisel-tutorial}{Chisel Tutorial} 提供了一个准备安装的项目,
包括小的测试和解决答案的练习。
\item The \myref{https://github.com/freechipsproject/chisel3/wiki}{Chisel Wiki} contains
a short users guide to Chisel and links to further information.
\item \myref{https://github.com/freechipsproject/chisel3/wiki}{Chisel Wiki}包含了小的chisel的用户手册
和更多信息的链接。
\item The \myref{https://github.com/freechipsproject/chisel-testers}{Chisel Testers} are in
their repository that contains a Wiki documentation.
\item \myref{https://github.com/freechipsproject/chisel-testers}{Chisel Testers} 包括在他们的仓库,
包含了一个wiki文档。
\item The \myref{https://github.com/ucb-bar/generator-bootcamp}{Generator Bootcamp} is
a Chisel course focusing on hardware generators, as a Jupyter notebook
\item \myref{https://github.com/ucb-bar/generator-bootcamp}{Generator Bootcamp} 是chisel课程,专注在硬件生成器,
作为jupyter notebook。
\item A \myref{https://github.com/ccelio/chisel-style-guide}{Chisel Style Guide} by Christopher Celio.
\end{itemize}
\item \myref{https://github.com/ccelio/chisel-style-guide}{Chisel Style Guide},作者是Christopher Celio。
\end{itemize}
\section{Exercise}
Each chapter ends with a hands-on exercise. For the introduction exercise, we will use an
FPGA board to get one \myref{https://en.wikipedia.org/wiki/Light-emitting_diode}{LED} blinking.
As a first step clone (or fork) the \myref{https://github.com/schoeberl/chisel-examples}{chisel-examples}
repository from GitHub.
The Hello World example is in the folder \code{hello-world}, set up as
a minimal project. You can explore the Chisel code of the blinking LED
in \code{src/main/scala/Hello.scala}.
Compile the blinking LED with the following steps:
每个章节结尾都有手写的练习题。为了介绍练习,我们通常会使用一个FPGA板子上的一颗LED。
第一步clone或者fork\myref{https://github.com/schoeberl/chisel-examples}{chisel-examples}仓库。
Hello World例子在\code{hello-world}文件夹里,是一个最小的工程。你可以探索LED闪烁的Chisel代码在\code{src/main/scala/Hello.scala}。
按下下述步骤编译LED闪烁工程:
\begin{chisel}
$ git clone https://github.com/schoeberl/chisel-examples.git
$ cd chisel-examples/hello-world/
$ make
\end{chisel}
After some initial downloading of Chisel components, this will produce the Verilog file \code{Hello.v}.
Explore this Verilog file. You will see that it contains two inputs \code{clock} and \code{reset}
and one output \code{io\_led}. When you compare this Verilog file with the Chisel module,
you will notice that the Chisel module does not contain \code{clock} or \code{reset}.
Those signals are implicitly generated, and in most designs, it is convenient not to need to
deal with these low-level details. Chisel provides register components, and those
are connected automatically to \code{clock} and \code{reset} (if needed).
在Chisel部分下载完成之后,程序会产出一个Verilog文件\code{Hello.v}。
浏览这个Verilog文件,你会看到电路包含两个输入\code{clock}和\code{reset}
和一个输出\code{io\_led}。当你对比Verilog文件和Chisel模块时,你将会发现Chisel模块申明时不包含\code{clock}和\code{reset}。
这些信号是隐式生成的,在大部分设计中处理这些底层细节是不方便的。Chisel提供的寄存器如果需要,会自动连接\code{clock}和\code{reset}.
The next step is to set up an FPGA project file for the synthesize tool, assign the pins,
and compile\footnote{The real process is more elaborated with following steps: synthesizing the logic,
performing place and route, performing timing analysis, and generating a bitfile.
However, for the purpose of this introduction example we simply call it ``compile''
your code.} the Verilog code, and configure the FPGA with the resulting bitfile.
We cannot provide the details of these steps. Please consult the manual of
your Intel Quartus or Xilinx Vivado tool.
However, the examples repository contains some ready to use Quartus
projects in folder \code{quartus} for several popular FPGA boards (e.g., DE2-115).
If the repository contains support for your board, start Quartus, open the project,
compile it by pressing the \emph{Play} button, and configure the FPGA board
with the \emph{Programmer} button and one of the LEDs should blink.
下一步是建立FPGA工程文件,分配引脚,然后编译Verilog代码,最后利用生成的bit文件烧写进FPGA。
我们无法提供这些步骤的详细过程。请参考你的EDA工具手册。
然而,例程仓库里面包含了一些针对几个流行的FPGA的Quartus可用的工程在\code{quartus}文件夹中。
如果仓库支持的板子包括你的FPGA,启动Quartus,打开工程,编译,烧录,然后一个LED将会闪烁。
{\bf Gratulation! You managed to get your first design in Chisel running in an FPGA!}
{\bf 厉害了!你成功在FPGA上运行了你第一个Chisel电路!}
If the LED is not blinking, check the status of reset. On the DE2-115 configuration,
the reset input is connected to SW0.
如果LED没有闪烁,检查reset的状态。在DE2-115配置中,reset的输入连接SW0.
Now change the blinking frequency to a slower or a faster value and
rerun the build process. Blinking frequencies and also blinking patterns
communicate different ``emotions''. E.g., a slow blinking LED signals that
everything is ok, a fast blinking LED signals an alarm state.
Explore which frequencies express best those two different emotions.
现在改变闪烁的频率然后重新运行一遍构建工程。闪烁的频率和闪烁的方式可以预示不同的状态。例如一个缓慢的闪烁信代表一切正常,一个快速的闪烁信号代表一个报警。
探索哪个频率对应这两个不同的动作。
As a more challenging extension to the exercise, generate the following blinking pattern:
the LED shall be on for 200~ms every second. For this pattern, you might
decouple the change of the LED blinking from the counter reset.
You will need a second constant where you change the state of the
\code{blkReg} register. What kind of emotion does this pattern produce?
Is it alarming or more like a sign-of-live signal?
一个更具有挑战性的练习,生成一个下述的闪烁模式:
LED一秒中亮200~ms。对于这个模式,你可以会从计数器复位来分离LED的闪烁。你将会需要第二个常量来改变\code{blkReg}寄存器的状态。这个模式会产生什么种类的状态呢?
是警报还是更像一个正常运行的信号呢?
\chapter{Basic Components}
In this section, we introduce the basic components for digital design:
combinational circuits and flip-flops.
These essential elements can be combined to build larger, more interesting circuits.
在这个章节,我们介绍数字设计的组成部分。这些基本的组成部分可以被合并到更大,更有趣的电路中。
\section{Signal Types and Constants}
Chisel provides three data types to describe signals, combinational logic, and registers:
\code{Bits}, \code{UInt}, and \code{SInt}. \code{UInt} and \code{SInt} extend \code{Bits},
and all three types represent a vector of bits. \code{UInt} gives this vector of
bits the meaning of an unsigned integer and \code{SInt} of a signed
integer.\footnote{The type \codefoot{Bits} in the current version of Chisel is missing operations and
therefore not very useful for user code.}
Chisel uses \myref{https://en.wikipedia.org/wiki/Two\%27s\_complement}{two's complement}
as signed integer representation.
Here is the definition for different types, an 8-bit \code{Bits}, an 8-bit unsigned integer, and a 10-bit
signed integer:
Chisel提供了三种数据类型用来描述型号,组合逻辑,和寄存器:\code{Bits}, \code{UInt},和\code{SInt}。
\code{UInt}和code{SInt}是\code{Bits}的拓展,并且所有的三种类型代表bits的矢量。
\code{UInt}表示这个bits的矢量是一个无符号的整型,\code{SInt}表示一个有符号的整型。
\footnote{\codefoot{Bits}类型在本Chisel版本是遗失的操作,并且不是特别在使用者的代码中有用.}
Chisel使用\myref{https://en.wikipedia.org/wiki/Two\%27s\_complement}{two's complement}表示有符号数的整型。
以下是不同类型的定义,8位\code{Bits},8位无符号整型,和一个10位有符号整型:
\shortlist{code/types.txt}
\noindent The width of a vector of bits is defined by a Chisel width type (\code{Width}).
The following expression casts the Scala integer \code{n} to a Chisel \code{width},
which is used for the definition of the \code{Bits} vector:
Bits的矢量宽度被Chisel的width类型(\code{Width})定义。以下表示把scala的整型\code{n}转换成Chisel的\code{width},用于\code{Bits}矢量的定义。
\shortlist{code/n_w.txt}
\noindent Constants can be defined by using a Scala integer and converting it to a Chisel type:
常量可以通过Scala整型定义并把它转换成Chisel类型。定义一个为0的UInt常量,和定义一个为03的SInt常量。
\shortlist{code/constants.txt}
\noindent Constants can also be defined with a width, by using the Chisel width type:
常量也可以随着宽度被定义,使用Chisel的width类型。 定义一个4位的常量8。
\shortlist{code/const_width.txt}
\noindent If you find the notion of 8.U and 4.W a little bit funny, consider it as a variant of an integer
constant with a type. This notation is similar to 8L, representing a long integer constant in C, Java, and Scala.
当你发现8.U和4.W的表示方式有些有趣,你可以认为它是一个整型常量附带一个类型。这个表示方式类似于8L,代表一个C,Java或Scala中的长整型。
Chisel benefits from Scala's type inference and in many places type information can be left out.
The same is also valid for bit widths. In many cases, Chisel will automatically infer the correct width.
Therefore, a Chisel description of hardware is more concise and better readable than VHDL or
Verilog.
Chisel受益于Scala的类型推断,并且很多地方类型信息可以被省略。类似的也适用于位宽。
在很多时候,Chisel会自动推断正确的宽度。于是,chisel描述的硬件语言比VHDL或Verilog更加简洁和可读。
For constants defined in other bases than decimal, the constant is defined in a string with
a preceding \code{h} for hexadecimal (base 16), \code{o} for octal (base 8), and \code{b}
for binary (base 2). The following example shows the definition of constant 255 in different
bases. In this example we omit the bit width and Chisel infers the minimum width to fit
the constants in, in this case 8 bits.
对于以其它作为基底的十进制以外的常量,常量被定义为字符串,开头\code{h}是16进制,\code{o}是8进制,\code{b}是2进制。
以下的例子表明了常量255的定义,在不同的基底。在这个例子,我们省略了位宽,chisel推断了最小宽度用来表示常量,在这个例子是8位。(16进制表示255, 8进制表示255, 二进制表示255)
\shortlist{code/const_base.txt}
\noindent The above code shows how to use an underscore to group digits in the
string that represents a constant. The underscore is ignored.
以上代码也表示了如何使用下划线去群组数字来标识常量。下划线是被忽略的。
To represent logic values, Chisel defines the type \code{Bool}.
\code{Bool} can represent a \emph{true} or \emph{false} value.
The following code shows the definition of type \code{Bool} and the definition of
\code{Bool} constants, by converting the Scala Boolean constants \code{true}
and \code{false} to Chisel \code{Bool} constants.
为了表示逻辑值,Chisel定义了\code{Bool}类型。Bool可以表示\emph{true}或\emph{false}值。
以下的代码表示了\code{Bool}类型的定义以及\code{Bool}常量的定义,通过转换Scala Boolean常量\code{true}和\code{false}, 到Chisel的\code{Bool}类型。
\shortlist{code/bool.txt}
\section{Combinational Circuits}
Chisel uses \myref{https://en.wikipedia.org/wiki/Boolean_algebra}{Boolean algebra} operators,
as they are defined in C, Java, Scala, and several other programming languages,
to described combinational circuits: \code{\&} is the AND operator and \code{|} is
the OR operator.
Following line of code defines a circuit that combines signals \code{a} and \code{b} with \emph{and}
gates and combines the result with signal \code{c} with \emph{or} gates.
Chisel使用\myref{https://en.wikipedia.org/wiki/Boolean_algebra}{Boolean algebra}操作符,
和C,Java,Scala和可能很多其它编程语言中定义的一样,去描述组合电路。
以下代码定义了一个对\code{a}和\code{b}进行\emph{and}逻辑,然后把它的结果和\code{c}进行\emph{or}逻辑的电路:
\shortlist{code/logic.txt}
\begin{figure}
\centering
\includegraphics[scale=\scale]{figures/logic}
\caption{Logic for the expression \code{(a \& b) | c}.
The wires can be a single bit or multiple bits. The Chisel expression, and the schematics are the same.}
\label{fig:logic}
\end{figure}
Figure~\ref{fig:logic} shows the schematic of this combinatorial expression.
Note that this circuit may be for a vector of bits and not only single wires
that are combined with the AND and OR circuits.
Figure~\ref{fig:logic} 表明了这个组合逻辑的表达的草图。
注意到这个电路可能用来表示一个向量的bits,并不仅是单独的AND和OR组成的线路。
In this example, we do not define the type nor the width of signal \code{logic}.
Both are inferred from the type and width of the expression.
The standard logic operations in Chisel are:
在这个例子,我们不定义类型或是信号\code{logic}的宽度。两个都是从类型和表达式位宽推演过来的.
标准Chisel的逻辑操作是:
\shortlist{code/bool_ops.txt}
1. 按位与
2. 按位或
3. 按位异或
4. 按位取反(译者:这个和!有区别的,注意)
\noindent The arithmetic operations use the standard operators:
\noindent 算术操作使用标准操作符
\shortlist{code/arith_ops.txt}
算术操作使用标准操作符1. 加法 2. 减法 3. 相反数 4. 乘法 5. 除法 6. 余数:
\noindent The resulting width of the operation is the maximum width of the operators for
addition and subtraction, the sum of the two widths for the multiplication, and usually
the width of the numerator for divide and modulo operations.\footnote{The exact
details are available in the \myref{https://github.com/freechipsproject/firrtl/blob/master/spec/spec.pdf}{FIRRTL specification}.}
加法和减法操作结果的宽度是操作数的最大宽度,乘法操作结果的库纳杜是两个操作数的位宽加和, 除法和余数操作的结果是被除数的位宽。
\footnote{ \myref{https://github.com/freechipsproject/firrtl/blob/master/spec/spec.pdf}{FIRRTL specification}.}
A signal can also first be defined as a \code{Wire} of some type. Afterward, we can assign a
value to the wire with the \code{:=} update operator.
一个信号也可以先被定义为某种类型的\code{Wire}。随后,我们可以给这个wire赋一个值,使用\code{:=}。
\shortlist{code/wire.txt}
A single bit can be extracted as follows:
一个简单的bit可以被如下提取:
\shortlist{code/single_bit.txt}
\noindent A subfield can be extracted from end to start position:
\noindent 一个分割可以从终点到起点提取:
\shortlist{code/sub_field.txt}
\noindent Bit fields are concatenated with \code{Cat}.
\noindent bit域通过\code{Cat}合并.
\shortlist{code/concat.txt}
Table~\ref{tab:operators} shows the full list of operators
(see also \myref{https://github.com/freechipsproject/chisel3/wiki/Builtin-Operators}{builtin operators}).
The Chisel operator precedence is determined by the evaluation order of the circuit,
which follows the \myref{https://docs.scala-lang.org/tour/operators.html}{Scala operator precedence}.
If in doubt, it is always a good praxis to use parentheses.\footnote{The operator precedence in
Chisel is a side effect of the hardware elaboration when the tree of hardware nodes
is created by executing the Scala operators. The Scala operator precedence is similar but
not identical to Java/C. Verilog has the same operator precedence as C, but VHDL
has a different one. Verilog has precedence ordering for logic operations, but in VHDL
those operators have the same precedence and are evaluated from left to right.}
Table~\ref{tab:operators}表示了操作符号的全列表
(查看\myref{https://github.com/freechipsproject/chisel3/wiki/Builtin-Operators}{builtin operators})。
Chisel操作符优先级取决于电路的赋值顺序,遵守\myref{https://docs.scala-lang.org/tour/operators.html}{Scala operator precedence}。
如果有疑问的话,使用括号是一个好的习惯。\footnote{在chisel里操作符号的优先级是硬件优化的副产品,因为硬件节点数
是通过执行scala操作符创建的。Scala操作符的先后顺序与Java/C类似但是不等同。Verilog和C有着同样的操作符优先级,但是VHDL与之不同。
Verilog有逻辑上的操作符优先级,但是VHDL里,这些操作符的优先级是等同的,按照从左到右的顺序}
Table~\ref{tab:functions} shows various functions defined on and for Chisel data types.
表格~\ref{tab:functions} 表明了不同函数在根据chisel类型做的定义。
\begin{table}
\centering
\label{tab:operators}
\begin{tabular}{lll}
\toprule
Operator & Description & Data types \\
\midrule
\code{* / \%} & multiplication, division, modulus & UInt, SInt \\
\code{+ -} & addition, subtraction & UInt, SInt \\
\code{=== =/=} & equal, not equal & UInt, SInt, returns Bool \\
\code{> >= < <=} & comparison & UInt, SInt, returns Bool \\
\code{<< >>} & shift left, shift right (sign extend on SInt) & UInt, SInt \\
\code{\~} & NOT & UInt, SInt, Bool \\
\code{\& | \^} & AND, OR, XOR & UInt, SInt, Bool \\
\code{!} & logical NOT & Bool \\
\code{\&\& ||} & logical AND, OR & Bool \\
\bottomrule
\end{tabular}
\caption{Chisel defined hardware operators.}
\end{table}
\begin{table}
\centering
\label{tab:functions}
\begin{tabular}{lll}
\toprule
Function & Description & Data types \\
\midrule
\code{v.andR v.orR v.xorR} & AND, OR, XOR reduction & UInt, SInt, returns Bool \\
\code{v(n)} & extraction of a single bit & UInt, SInt \\
\code{v(end, start)} & bitfield extraction & UInt, SInt \\
\code{Fill(n, v)} & bitstring replication, n times & UInt, SInt \\
\code{Cat(a, b, ...)} & bit field concatenation & UInt, SInt \\
\bottomrule
\end{tabular}
\caption{Chisel defined hardware functions, invoked on \code{v}.}
\end{table}
\subsection{Multiplexer}
A \myref{https://en.wikipedia.org/wiki/Multiplexer}{multiplexer} is a circuit that selects between alternatives.
In the most basic form, it selects between two alternatives. Figure~\ref{fig:mux} shows
such a 2:1 multiplexer, or mux for short. Depending on the value of the
select signal (\code{sel}) signal \code{y} will represent signal \code{a} or
signal \code{b}.
\myref{https://en.wikipedia.org/wiki/Multiplexer}{复用器}是一个选择选项的电路。
在最基本的形式,它在二者选择其一。图2.2~\ref{fig:mux}表示一个二选一复用器,或是用mux简单表示。
取决于选择信号(\code{sel}),\code{y}会表示信号\code{a}或是信号\code{b}。
\begin{figure}
\centering
\includegraphics[scale=\scale]{figures/mux}
\caption{A basic 2:1 multiplexer.}