Preface¶
Who this book is for¶
Nobody draws a modern processor gate by gate. A chip with billions of transistors is described in text, simulated until the designers believe it works, and only then handed to tools that turn the description into gates, wires and finally silicon. The skill of writing that description is the subject of this book. It starts from the idea that makes the whole practice possible, the register transfer level (RTL), then teaches the two industrial hardware description languages, Verilog and VHDL, side by side, and ends with the open RISC-V instruction set, which is small enough to understand completely and real enough to run in shipping products.
The book is written for students of computer architecture, digital design and embedded systems. It assumes that you already know logic gates, flip-flops, binary numbers and two's complement arithmetic, and nothing more. No earlier programming in a hardware language is needed. If you have written software, one habit will have to be put aside very early: in hardware, everything happens at once. Many of the mistakes that beginners make come from reading hardware descriptions as if they were programs, and the early chapters return to this point again and again.
What is in the book¶
The chapters follow a single line of thought, from the abstraction to the language to a complete machine.
| Chapters | Topic | What you will be able to do |
|---|---|---|
| 1 and 2 | Register transfer level: registers, the clock, microoperations, buses, memory, datapath and control | Read and write register-transfer statements, and turn a small specification into a table of microoperations and a control schedule |
| 3, 4 and 5 | Verilog: modules, signals, combinational and sequential logic, state machines, an adder and an ALU, testbenches | Write synthesizable Verilog for common blocks and prove it correct with a self-checking testbench |
| 6 and 7 | VHDL: entities, architectures, processes, numeric types, generics, testbenches | Write the same designs in VHDL and translate between the two languages |
| 8, 9 and 10 | RISC-V: the instruction formats, assembly programming and tracing, the single-cycle datapath and the five-stage pipeline | Encode and decode instructions, trace programs by hand, and build a small RISC-V processor |
| Back matter | Glossary and further reading | Look up a term, and find where to read more |
The material was developed as a six-part course (register transfers, Verilog, VHDL, the RISC-V architecture, a hands-on RISC-V session and a set of ten assignments). In this book the lectures and the written notes of that course are merged into a single running text, the interactive demonstrations of the course are replaced by traces, tables and figures that you can follow on paper, and the ten assignments appear as exercises at the end of the chapters where their topics are taught. Answers are folded under each exercise; the phrase "Answer" opens one when you want it.
How to use it¶
Read with a pencil. Hardware cannot be learned by looking at code: it has to be predicted, run and compared. The method used throughout, and the one that the exercises ask you to follow, is predict first, then check. Before you read the result of a worked example, work out for yourself what each register will hold after each clock edge, and write it in a trace table. A prediction that can turn out wrong is the only thing that tells you whether you understand.
You will also want to run the code. All the tools you need are free:
- Icarus Verilog (
iverilog,vvp) simulates Verilog, and GTKWave shows the waveforms it records. - GHDL simulates VHDL.
- EDA Playground, in a web browser, runs Icarus Verilog and GHDL with nothing to set up.
- Yosys is an open-source synthesis tool; it shows what hardware a description really produces.
A typical session is iverilog -g2012 -o sim design.v design_tb.v followed by vvp sim, for Verilog, and ghdl -a design.vhd design_tb.vhd, ghdl -e design_tb, ghdl -r design_tb --vcd=wave.vcd, for VHDL. The listings are written to be read as well as run, so treat them as models of style, and type them in rather than pasting them where you can.
Conventions¶
Worked examples sit in boxes headed "Example", definitions in note boxes and common mistakes in warning boxes. Code is set in fixed-width type with the language named. A signal or register whose name ends in _n (for example rst_n) is active low: it does its job when it is 0. Numbers written in Verilog style, such as 8'hA5, give a width, a base and the digits; 0x introduces hexadecimal in assembly language and in prose. Register-transfer statements are written with the arrow of Morris Mano's notation, R2 ← R1; the code listings use the ASCII <= that the languages themselves use.
A note on the two languages¶
Verilog and VHDL are two spellings of the same ideas. Verilog (standardised as IEEE 1364, and now part of SystemVerilog, IEEE 1800) is terse and permissive. VHDL (IEEE 1076) is verbose and strict, and reads like a contract. Both are used in industry, and a working engineer is expected to read both. The book teaches Verilog first, because its brevity lets the central ideas show clearly, and then shows how every one of those ideas is written in VHDL. Chapter 7 closes with a table that translates the constructs one by one.
The two sibling books of this collection continue the story. The first turns the instruction set into a fast core, with pipelining, caches and on-chip interconnect. The second follows an RTL description through synthesis, placement, routing and signoff until it becomes the layout of a chip. This book ends exactly where the hardware description is complete and correct.