33
CSE369, Spring 2021 L2: Verilog Basics Intro to Digital Design Verilog Basics Instructor: Clarice Larson “Advanced driver-assistance systems (ADAS) are quickly being integrated into almost all new automobiles. These systems often introduce automakers and Tier 1s with unique computing needs that the standard CPU or GPU may not be well suited for… First and foremost, FPGAs offer high customizability and flexibility… FPGA allows the designers the ability to customize their solutions specifically to their needs while differentiating from the competition. FPGAs allow for scalability… engineers can easily build on the FPGA design from previous generations without having to go through the hassle of spinning a new ASIC. Finally, quick time to market makes FPGAs a desirable solution for car manufacturers… https://www.allaboutcircuits.com/news/why-the-industry-is- demanding-fpgas-for-adas/ Why the Industry is Demanding FPGAs for Advanced Driver- Assistance Systems (ADAS)

L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Intro to Digital DesignVerilog BasicsInstructor: Clarice Larson

“Advanced driver-assistance systems (ADAS) are quickly being integrated into almost all new automobiles. These systems often introduce automakers and Tier 1s with unique computing needs that the standard CPU or GPU may not be well suited for…First and foremost, FPGAs offer high customizability and flexibility… FPGA allows the designers the ability to customize their solutions specifically to their needs while differentiating from the competition. FPGAs allow for scalability… engineers can easily build on the FPGA design from previous generations without having to go through the hassle of spinning a new ASIC.Finally, quick time to market makes FPGAs a desirable solution for car manufacturers…https://www.allaboutcircuits.com/news/why-the-industry-is-demanding-fpgas-for-adas/

Why the Industry is Demanding FPGAs for Advanced Driver-Assistance Systems (ADAS)

Page 2: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Practice Question:

v What is the MOST simplified Boolean Algebra expression for the following circuit?

2

Page 3: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Practice Question:

v Implement the Boolean expression B A + C with the fewest number of a single universal gate. What does your solution look like?

3

Page 4: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Administrivia

v Lab 1§ Everyone should have a kit and an assigned demo slot§ TAs will be available during lab hours for questions

• Please be flexible as we are trying a system to make lab hours as close to in-person as possible virtually. We may adjust as the quarter progresses.

§ NOTE: If you want to experiment with the breadboard, never hook ground directly to power!!

4

Page 5: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Lecture Outline

v Thinking About Hardwarev Verilog Basicsv Waveform Diagramsv Debugging in Verilog

5

Page 6: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog

v Programming language for describing hardware§ Simulate behavior before (wasting time) implementing§ Find bugs early

v Similar to C/C++/Java§ VHDL similar to ADA

v Modern version is “SystemVerilog”§ Superset of previous; cleaner and more efficient

6

Page 7: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Brainstorm: Describing Hardware

v How might Verilog be different than software programming languages?

v Starter questions§ What are the language primitives?

§ What does a “variable” mean in hardware?

§ Initialization?

§ “Program” execution?

7

Page 8: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog: Hardware Descriptive Language

v Although it looks like code:

8

v Keep the hardware in mind:

module myModule (F, A, B, C);output logic F;input logic A, B, C;logic AN, AB, AC;

nand gate1(AB,AN, B);nand gate2(AC, A, C);nand gate3( F,AB,AC);not not1(AN, A);

endmodule

Page 9: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Primitivesv Nets (wire): transmit value of connected source

§ Problematic if connected to two different voltage sources§ Can connect to many places (always possible to “split” wire)

v Variables (reg): variable voltage sources§ Can “drive” by assigning arbitrary values at any given time§ SystemVerilog: variable logic can be used as a net, too

v Logic Values§ 0 = zero, low, FALSE§ 1 = one, high, TRUE§ X = unknown, uninitialized, contention (conflict)§ Z = floating (disconnected), high impedance

9

Page 10: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Primitives

v Gates:

v Modules: “functions” in Verilog that define blocks§ Input: Signals passed from outside to inside of block§ Output: Signals passed from inside to outside of block

10

Gate Verilog Syntax

NOTa ~a

aANDb a & b

aORb a | b

aNANDb ~(a & b)

aNORb ~(a | b)

aXORb a ^ b

aXNORb ~(a ^ b)

Page 11: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Execution

v Physical wires transmit voltages (electrons) near-instantaneously§ Wires by themselves have no notion of sequential execution

v Gates and modules are constantly performing computations§ Can be hard to keep track of!

v In pure hardware, there is no notion of initialization§ A wire that is not driven by a voltage will naturally pick up a

voltage from the environment

11

Page 12: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Lecture Outline

v Thinking About Hardwarev Verilog Basicsv Waveform Diagramsv Debugging in Verilog

12

Page 13: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Using an FPGA

13

// Verilog code for 2-input multiplexer

module AOI (F, A, B, C, D);output F;input A, B, C, D;

assign F = ~((A & B) | (C & D));endmodule

module MUX2 (V, SEL, I, J); // 2:1multiplexeroutput V;input SEL, I, J;wire SELB, VB;

not G1 (SELB, SEL);AOI G2 (VB, I, SEL, SELB, J);not G3 (V, VB);

endmodule

// Verilog code for 2-input multiplexer

module AOI (F, A, B, C, D);output F;input A, B, C, D;

assign F = ~((A & B) | (C & D));endmodule

module MUX2 (V, SEL, I, J); // 2:1multiplexeroutput V;input SEL, I, J;wire SELB, VB;

not G1 (SELB, SEL);AOI G2 (VB, I, SEL, SELB, J);not G3 (V, VB);

endmodule

// Verilog code for 2-input multiplexer

module AOI (F, A, B, C, D);output F;input A, B, C, D;

assign F = ~((A & B) | (C & D));endmodule

module MUX2 (V, SEL, I, J); // 2:1multiplexeroutput V;input SEL, I, J;wire SELB, VB;

not G1 (SELB, SEL);AOI G2 (VB, I, SEL, SELB, J);not G3 (V, VB);

endmodule

// Verilog code for 2-input multiplexer

module AOI (F, A, B, C, D);output logic F;input logic A, B, C, D;

assign F = ~((A & B) | (C & D));endmodule

module MUX2 (V, SEL, I, J); // 2:1multiplexeroutput logic V;input logic SEL, I, J;logic SELB, VB;

not G1 (SELB, SEL);AOI G2 (VB, I, SEL, SELB, J);not G3 (V, VB);

endmodule

Verilog

FPGACADTools

0010101000101001010010010010011000101010001010110001010100101001010100010110001001010101010011110010010100001010100101010010010000101010101001010100101000101011010100101001010010100101001

Bitstream

Simulation

Page 14: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Structural Verilog

// Verilog code for AND-OR-INVERT gate

module AOI (F, A, B, C, D);output logic F;input logic A, B, C, D;

assign F = ~((A & B) | (C & D));endmodule

// end of Verilog code

14

Block Diagram:

AOI FCBA

D

Page 15: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Wires

15

// Verilog code for AND-OR-INVERT gate

module AOI (F, A, B, C, D);output logic F;input logic A, B, C, D;logic AB, CD, O; // necessary

assign AB = A & B;assign CD = C & D;assign O = AB | CD;assign F = ~O;

endmodule

Page 16: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Gate Level

16

// Verilog code for AND-OR-INVERT gate

module AOI (F, A, B, C, D);output logic F;input logic A, B, C, D;logic AB, CD, O; // necessary

and a1(AB, A, B);and a2(CD, C, D);or o1(O, AB, CD);not n1(F, O);

endmodule

assign AB = A & B;assign CD = C & D;assign O = AB | CD;assign F = ~O;

was:

Page 17: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Hierarchy// Verilog code for 2-input multiplexer

module AOI (F, A, B, C, D);output logic F;input logic A, B, C, D;

assign F = ~((A & B)|(C & D));endmodule

module MUX2 (V, SEL, I, J); // 2:1 multiplexeroutput logic V;input logic SEL, I, J;logic SELN, VN;

not G1 (SELN, SEL);AOI G2 (.F(VN), .A(I), .B(SEL), .C(SELN), .D(J));not G3 (V, VN);

endmodule17

2-input MUX

AOI VN VSELN

SELI

J

Page 18: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

TechnologyBreak

18

Page 19: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Lecture Outline

v Thinking About Hardwarev Verilog Basicsv Waveform Diagramsv Debugging in Verilog

19

Page 20: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Signals and Waveforms

v Signals transmitted over wires continuously§ Transmission is effectively instantaneous

(a wire can only contain one value at any given time)§ In digital system, a wire holds either a 0 (low voltage) or 1

(high voltage)

20

Stack multiple signals in same waveform diagramvertically (syncing times)

0 1

Page 21: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Signal Grouping

21

A group of wires when interpreted as a bit field is called a bus

X

“undefined” (unknown) signal

Page 22: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Circuit Timing Behaviorv Simple Model: Gates “react” after fixed delayv Example: Assume delay of all gates is 1 ns (= 3 ticks)v Draw the waveforms for D, E, and F

22

AB C

D E F

Page 23: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Circuit Timing: Hazards/Glitchesv Circuits can temporarily go to incorrect states!

§ Assume 1 ns delay (3 ticks) for all gates§ Draw the waveforms for A, B, C, and AE

23

Page 24: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Buses

24

// Verilog code for AND-OR-INVERT gate

module AOI (F, A, B, C, D);output logic F;input logic A, B, C, D;logic [2:0] w; // necessary

assign w[0] = A & B;assign w[1] = C & D;assign w[2] = w[0] | w[1];assign F = ~w[2];

endmodule

Just for illustration – this is bad coding style!

Page 25: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Signal Manipulation

v Bus definition: [n-1:0] is n-bit bus§ Good practice to follow bit numbering notation§ Access individual bit/wire using “array” syntax (e.g. bus[1])§ Can access sub-bus using similar notation (e.g. bus[4:2])

v Multi-bit constants: n'b#…#§ n is width, b is radix specifier (b for binary), #s are digits of number§ e.g. 4'd12, 4'b1100, 4'hC

v Concatenation: {sig, …, sig}§ Ordering matters; result will have combined widths of all signals

v Replication operator: {n{m}}§ repeats value m, n times

25

Page 26: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Practice Questionlogic [4:0] apple;logic [3:0] pear;

logic [9:0] orange;

assign apple = 5'd20;

assign pear = {1'b0, apple[2:1], apple[4]};

v What’s the value of pear?

v If we want orange to be the sign-extended version of apple, what is the appropriate Verilog statement?

26

Page 27: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Lecture Outline

v Thinking About Hardwarev Verilog Basicsv Waveform Diagramsv Debugging in Verilog

27

Page 28: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Testbenches

v Needed for simulation only!§ Mimic hardware

v ModelSim runs entirely on your computer§ Tries to simulate your FPGA environment without actually

using hardware – no physical signals available§ Must create fake inputs for FPGA’s physical connections

• e.g. LED, HEX, KEY, SW

§ Unnecessary when code is loaded onto FPGA

v Need to define both input signal combinations as well as their timing

28

Page 29: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Verilog Testbenches

module MUX2_testbench ();logic SEL, I, J; // variables remember valueslogic V; // acts as net for reading output

initial // build stimulus (test vectors)begin // start of "block" of codeSEL = 1; I = 0; J = 0; // t=0: S=1, I=0, J=0 -> V=0#10 I = 1; // t=10: S=1, I=1, J=0 -> V=1#10 SEL = 0; // t=20: S=0, I=1, J=0 -> V=0#10 J = 1; // t=30: S=0, I=1, J=1 -> V=1

end // end of "block" of code

MUX2 DUT (.V, .SEL, .I, .J);

initial // monitor response (print)$monitor("%d:\tSEL=%b,I=%b,J=%b\t->\tV=%b",$time,SEL,I,J,V);

endmodule29

“Device Under Test”, sometimes “Unit Under Test” (UUT)

No ports

Alternative to waveforms

Page 30: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Better Verilog Testbenchmodule MUX2_testbench ();logic SEL, I, J; // registers remember valueslogic V; // acts as net for reading output

int i;initial // build stimulus (test vectors)begin // start of "block" of codefor(i = 0; i < 8; i = i + 1) begin{SEL, I, J} = i; #10;

endend // end of "block" of code

MUX2 DUT (.V, .SEL, .I, .J);

endmodule

30

Page 31: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Debugging Circuits

v Complex circuits require careful debugging§ Test as you go; don’t wait until the end (system test)§ Every module should have a testbench (unit test)

1) Test all behaviors§ All combinations of inputs for small circuits, subcircuits

2) Identify any incorrect behaviors3) Examine inputs & outputs to find earliest place where value is

wrong§ Typically trace backwards from bad outputs§ Look at values at intermediate points in circuit

31

Page 32: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Hardware Debugging

v Simulation (ModelSim) is used to debug logic designand should be done thoroughly before touching FPGA

v If interfacing with other circuitry (e.g. breadboard), will also need to debug circuitry layout there§ Similar process, but with power sources (inputs) and

voltmeters (probe the wires)§ Often just a poor electrical connection somewhere

v Sometimes things simply fail§ All electrical components fail eventually, whether you caused

it to or not

32

Page 33: L2: Verilog Basics CSE369, Spring 2021 Intro to Digital Design

CSE369, Spring 2021L2: Verilog Basics

Summary

v Verilog is a hardware description language (HDL) used to program your FPGA§ Programmatic syntax used to describe the connections

between gates and registers

v Waveform diagrams used to track intermediate signals as information propagates through CL

v Hardware debugging is a critical skill§ Similar to debugging software, but using different tools

33