eecs 470 lab 1 - verilog: hardware description language · verilog: hardwaredescriptionlanguage...
TRANSCRIPT
EECS 470 Lab 1Verilog: Hardware Description Language
Department of Electrical Engineering and Computer ScienceCollege of EngineeringUniversity of Michigan
Thursday, 05th September, 2019
(University of Michigan) Lab 1: Verilog 09-05-2019 1 / 60
Overview
EECS 470 Labs
Verilog
Verilog Flow Control
Testing
Project 1
Lab Assignment
(University of Michigan) Lab 1: Verilog 09-05-2019 2 / 60
EECS 470 Labs
Who?
I Contact InformationI Siying Feng - [email protected] Xueyang Liu - [email protected] Jielun Tan - [email protected] EECS 470 Piazza (click for link)
I Most of your project related questions should be asked here so thatother people can benefit from the answer.
I Reminder: Please do not post program code in public questions.
I See up-to-date office hours on the course website.
(University of Michigan) Lab 1: Verilog 09-05-2019 3 / 60
EECS 470 Labs
Where? When?
I Three Lab SectionsI 011 – Thursday 4:30 pm to 6:30 pm in 1620 BBBI 012 – Friday 10:00 am to 12:00 pm in 1620 BBBI 013 – Friday 12:30 pm to 2:30 pm in 1620 BBB
I These will become extra office hours after labs are over, for extraproject help.
I Please go to your assigned section: the class is very full.I If you are on the wait list, you are welcome to attend if there is space.I Please let enrolled students have first-priority for workstations.
(University of Michigan) Lab 1: Verilog 09-05-2019 4 / 60
EECS 470 Labs
What?
Lab 1 – Verilog: Hardware Description LanguageLab 2 – The Build SystemLab 3 – Writing Good TestbenchesLab 4 – Revision ControlLab 5 – ScriptingLab 6 – SystemVerilog
(University of Michigan) Lab 1: Verilog 09-05-2019 5 / 60
EECS 470 Labs
Projects
Individual Verilog Projects
Project 1 – Priority SelectorsProject 2 – Pipelined Multiplier, Integer Square RootProject 3 – Verisimple 5-stage Pipeline
NOTE: Your average of the individual projects must be over50% to receive a passing grade in this class.
Group Project
Project 4 – Out-of-Order Processor (35%)
(University of Michigan) Lab 1: Verilog 09-05-2019 6 / 60
EECS 470 Labs
Advice
I These projects will take a non-trivial amount of time, especially ifyou’re not a Verilog guru.
I You should start them early. Seriously. . .I Especially Project 3
(University of Michigan) Lab 1: Verilog 09-05-2019 7 / 60
EECS 470 Labs
Project 4
I RV32IM Instruction Set Architecture (with some caveats)I Groups of 4 to 5
I Start thinking about your groups nowI You’ll be spending hundreds of hours together this semester, so work
with people with whom you get along.I Heavy Workload
I 100 hours/member, minimumI 150 to 300 hours/member, more realisticallyI This is a lower bound, not an upper. . .
I Class is heavily loaded to the end of the term
(University of Michigan) Lab 1: Verilog 09-05-2019 8 / 60
EECS 470 Labs
Administrivia
I Homework 1 is due Wednesday , 11th September, 2019 6:00 PM (turnin via Gradescope)
I Project 1 is due Friday, 13th September, 2019 11:59 PM (turn in viasubmission script)
(University of Michigan) Lab 1: Verilog 09-05-2019 9 / 60
Verilog
Intro to Verilog
What is Verilog?
I Hardware Description Language - IEEE 1364-2005I Superseded by SystemVerilog - IEEE 1800-2009
I Two Forms1. Behavioral2. Structural
I It can be built into hardware. If you can’t think of at least one(inefficient) way to build it, it might not be good.
Why do I care?
I We use Behavioral Verilog to do computer architecture here.I Semiconductor Industry Standard (VHDL is also common, more so in
Europe)
(University of Michigan) Lab 1: Verilog 09-05-2019 10 / 60
Verilog
Intro to Verilog
What is Verilog?
I Hardware Description Language - IEEE 1364-2005I Superseded by SystemVerilog - IEEE 1800-2009
I Two Forms1. Behavioral2. Structural
I It can be built into hardware. If you can’t think of at least one(inefficient) way to build it, it might not be good.
Why do I care?
I We use Behavioral Verilog to do computer architecture here.I Semiconductor Industry Standard (VHDL is also common, more so in
Europe)
(University of Michigan) Lab 1: Verilog 09-05-2019 10 / 60
Verilog
Intro to Verilog
What is Verilog?
I Hardware Description Language - IEEE 1364-2005I Superseded by SystemVerilog - IEEE 1800-2009
I Two Forms1. Behavioral2. Structural
I It can be built into hardware. If you can’t think of at least one(inefficient) way to build it, it might not be good.
Why do I care?
I We use Behavioral Verilog to do computer architecture here.I Semiconductor Industry Standard (VHDL is also common, more so in
Europe)
(University of Michigan) Lab 1: Verilog 09-05-2019 10 / 60
Verilog
The Difference Between Behavioral and Structural Verilog
Behavioral Verilog
I Describes function of designI Abstractions
I Arithmetic operations(+,-,*,/)
I Logical operations(&,|,ˆ,~)
Structural Verilog
I Describes construction ofdesign
I No abstractionI Uses modules, corresponding
to physical devices, foreverything
Suppose we want to build an adder?
(University of Michigan) Lab 1: Verilog 09-05-2019 11 / 60
Verilog
Structural Verilog by Example
a◦b◦
w_1
w_2
w_0
cin◦◦s
◦cout
u0u1
u2
u3
u4
Figure: 1-bit Full Adder
(University of Michigan) Lab 1: Verilog 09-05-2019 12 / 60
Verilog
Structural Verilog by Example
module one_bit_adder(input wire a,b,cin,output wire sum,cout);wire w_0,w_1,w_2;xor u0(w_0,a,b);xor u1(sum,w_0,cin);and u2(w_1,w_0,cin);and u3(w_2,a,b);or u4(cout,w_1,w_2);
endmodule
(University of Michigan) Lab 1: Verilog 09-05-2019 13 / 60
Verilog
Behavioral Verilog by Example
module one_bit_adder(input wire a,b,cin,output wire sum,cout);assign sum = a ^ b ^ cin;assign cout = ((a ^ b) & cin) | a & b;
endmodule
(University of Michigan) Lab 1: Verilog 09-05-2019 14 / 60
Verilog
Behavioral Verilog by Example
module one_bit_adder(input logic a,b,cin,output logic sum,cout);
always_combbegin
sum = a ^ b ^ cin;cout = 1'b0;if ((a ^ b) & cin) | (a & b))
cout = 1'b1;end
endmodule
(University of Michigan) Lab 1: Verilog 09-05-2019 15 / 60
Verilog
Verilog Semantics
LexicalI Everything is case sensitive.I Type instances must start with A-Z,a-z,_. They may contain
A-Z,a-z,0-9,_,$.I Comments begin with // or are enclosed with /* and */.
(University of Michigan) Lab 1: Verilog 09-05-2019 16 / 60
Verilog
Data Types
Synthesizable Data Types
wires Also called nets
wire a_wire;wire [3:0] another_4bit_wire;
I Cannot hold state
logic Replaced reg in SystemVerilog
logic [7:0] an_8bit_register;reg a_register;
I Holds state, might turn into flip-flopsI Less confusing than using reg with combinational logic
(coming up. . . )
(University of Michigan) Lab 1: Verilog 09-05-2019 17 / 60
Verilog
Data Types
Unsynthesizable Data Types
integer Signed 32-bit variabletime Unsigned 64-bit variablereal Double-precision floating point variable
(University of Michigan) Lab 1: Verilog 09-05-2019 18 / 60
Verilog
Types of Values
Four State Logic
0 False, low1 True, highZ High-impedance, unconnected netX Unknown, invalid, don’t care
(University of Michigan) Lab 1: Verilog 09-05-2019 19 / 60
Verilog
Values
Literals/Constants
I Written in the format <bitwidth>’<base><constant>I Options for <base> are. . .
b Binaryo Octald Decimalh Hexadecimal
assign an_8bit_register = 8'b10101111;assign a_32bit_wire = 32'hABCD_EF01;assign a_4bit_logic = 4'hE;
(University of Michigan) Lab 1: Verilog 09-05-2019 20 / 60
Verilog
Verilog Operators
Arithmetic* Multiplication/ Division+ Addition- Subtraction% Modulus
** ExponentiationBitwise
~ Complement& And| Or
~| Norˆ Xor
~ˆ XnorLogical
! Complement&& And|| Or
Shift>> Logical right shift<< Logical left shift>>> Arithmetic right shift<<< Arithmetic left shift
Relational> Greater than>= Greater than or equal to< Less than<= Less than or equal to!= Inequality!== 4-state inequality== Equality=== 4-state equality
Special{,} Concatenation
{n{m}} Replication?: Ternary
(University of Michigan) Lab 1: Verilog 09-05-2019 21 / 60
Verilog
Setting Values
assign Statements
I One line descriptions of combinational logicI Left hand side must be a wire (SystemVerilog allows assign statements
on logic type)I Right hand side can be any one line verilog expressionI Including (possibly nested) ternary (?:)
Example
module one_bit_adder(input wire a,b,cin,output wire sum,cout);assign sum = a ^ b ^ cin;assign cout = ((a ^ b) & cin) | a & b;
endmodule
(University of Michigan) Lab 1: Verilog 09-05-2019 22 / 60
Verilog
Setting Values
always Blocks
I Contents of always blocks are executed whenever anything in thesensitivity list happens
I Two main types in this class. . .I always_comb
I implied sensitivity lists of every signal inside the blockI Used for combinational logic. Replaced always @*
I always_ff @(posedge clk)I sensitivity list containing only the positive transition of the clk signalI Used for sequential logic
I All left hand side signals need to be logic type.
(University of Michigan) Lab 1: Verilog 09-05-2019 23 / 60
Verilog
Always Block Examples
Combinational Block
always_combbegin
x = a + b;y = x + 8'h5;
end
Sequential Block
always_ff @(posedge clk)begin
x <= #1 next_x;y <= #1 next_y;
end
(University of Michigan) Lab 1: Verilog 09-05-2019 24 / 60
Verilog
Blocking vs. Nonblocking Assignments
Blocking Assignment
I Combinational BlocksI Each assignment is
processed in order, earlierassignments block later ones
I Uses the = operator
vs.
Nonblocking Assignment
I Sequential BlocksI All assignments occur
“simultaneously,” delays arenecessary for accuratesimulation
I Uses the <= operator
(University of Michigan) Lab 1: Verilog 09-05-2019 25 / 60
Verilog
Blocking vs. Nonblocking Assignment by Example
Blocking Examplealways_combbegin
x = new_val1;y = new_val2;sum = x + y;
end
I Behave exactly as expectedI Standard combinational logic
new_val1x
new_val2y
sum
Figure: Timing diagram for theabove example.
(University of Michigan) Lab 1: Verilog 09-05-2019 26 / 60
Verilog
Blocking vs. Nonblocking Assignment by Example
Nonblocking Examplealways_ff @(posedge clock)begin
x <= #1 new_val1;y <= #1 new_val2;sum <= #1 x + y;
end
I What changes betweenthese two examples?
I Nonblocking means thatsum lags a cycle behindthe other two signals
clocknew_val1
xnew_val2
ysum
Figure: Timing diagram for the above example.
(University of Michigan) Lab 1: Verilog 09-05-2019 27 / 60
Verilog
Blocking vs. Nonblocking Assignment by Example
Bad Examplealways_ff @(posedge clock)begin
x <= #1 y;z = x;
end
I z is updated after xI z updates on negedge
clock
clockreset
xyz
Figure: Timing diagram for theabove example.
(University of Michigan) Lab 1: Verilog 09-05-2019 28 / 60
Verilog
Synthesis Tips
LatchesI What is a latch?
I Memory device without a clock
I Generated by a synthesis tool when a net needs to hold state withoutbeing clocked (combinational logic)
I Generally bad, unless designed in intentionallyI Unnecessary in this class
(University of Michigan) Lab 1: Verilog 09-05-2019 29 / 60
Verilog
Synthesis Tips
LatchesI What is a latch?
I Memory device without a clock
I Generated by a synthesis tool when a net needs to hold state withoutbeing clocked (combinational logic)
I Generally bad, unless designed in intentionallyI Unnecessary in this class
(University of Michigan) Lab 1: Verilog 09-05-2019 29 / 60
Verilog
Synthesis Tips
LatchesI Always assign every variable on every pathI This code generates a latchI Why does this happen?
always_combbegin
if (cond)next_x = y;
end
(University of Michigan) Lab 1: Verilog 09-05-2019 30 / 60
Verilog
Synthesis Tips
Possible Solutions to Latchesalways_combbegin
next_x = x;if (cond)
next_x = y;end
always_combbegin
if (cond)next_x = y;
elsenext_x = x;
end
(University of Michigan) Lab 1: Verilog 09-05-2019 31 / 60
Verilog
Modules
Intro to ModulesI Basic organizational unit in VerilogI Can be reused
Module Example
module my_simple_mux(input wire select_in, a_in, b_in; //inputs listedoutput wire muxed_out); //outputs listedassign muxed_out = select_in ? b_in : a_in;
endmodule
(University of Michigan) Lab 1: Verilog 09-05-2019 32 / 60
Verilog
Modules
Writing Modules
I Inputs and outputs must be listed, including size and typeformat: <dir> <type> <[WIDTH-1:0]> <name>;e.g. output logic [31:0] addr;
I In module declaration line or after it, inside the module
Instantiating Modules
I Two methods of instantiation1. e.g. my_simple_mux m1(.a_in(a),.b_in(b),
.select_in(s),.muxed_out(m));2. e.g. my_simple_mux m1(a,b,s,m);
I The former is much safer. . .I Introspection (in testbenches): module.submodule.signal
(University of Michigan) Lab 1: Verilog 09-05-2019 33 / 60
Verilog
How to Design with Verilog
I Remember – Behavioral Verilog implies no specific hardware designI But, it has to be synthesizableI Better be able to build it somehow
(University of Michigan) Lab 1: Verilog 09-05-2019 34 / 60
Verilog
Keys to Synthesizability
Combinational Logic
I Avoid feedback (combinatorial loops)I Always blocks should
I Be always_comb blocksI Use the blocking assignment operator =
I All variables assigned on all pathsI Default valuesI if(...) paired with an else
(University of Michigan) Lab 1: Verilog 09-05-2019 35 / 60
Verilog
Keys to Synthesizability
Sequential Logic
I Avoid clock- and reset-gatingI Always blocks should
I Be always_ff @(posedge clock) blocksI Use the nonblocking assignment operator, with a delay <= #1
I No path should set a variable more than onceI Reset all variables used in the blockI //synopsys sync_set_reset “reset”
(University of Michigan) Lab 1: Verilog 09-05-2019 36 / 60
Verilog Flow Control
Flow Control
All Flow ControlI Can only be used inside procedural blocks (always, initial, task,
function)I Encapsulate multiline assignments with begin...endI Remember to assign on all paths
Synthesizable Flow Control
I if/elseI case
(University of Michigan) Lab 1: Verilog 09-05-2019 37 / 60
Verilog Flow Control
Flow Control
Unsythesizable Flow Control
I Useful in testbenchesI For example. . .
I forI whileI repeatI forever
(University of Michigan) Lab 1: Verilog 09-05-2019 38 / 60
Verilog Flow Control
Flow Control by Example
Synthesizable Flow Control Example
always_combbegin
if (muxy == 1'b0)y = a;
elsey = b;
end
The Ternary Alternative
wire y;assign y = muxy ? b : a;
(University of Michigan) Lab 1: Verilog 09-05-2019 39 / 60
Verilog Flow Control
Flow Control by Example
Casez Example
always_combbegin
casez(alu_op)3'b000: r = a + b;3'b001: r = a - b;3'b010: r = a * b;...3'b1??: r = a ^ b;
endcaseend
(University of Michigan) Lab 1: Verilog 09-05-2019 40 / 60
Testing
Testing
What is a test bench?I Provides inputs to one or more modulesI Checks that corresponding output makes senseI Basic building block of Verilog testing
Why do I care?
I Finding bugs in a single module is hard. . .I But not as hard as finding bugs after combining many modulesI Better test benches tend to result in higher project scores
(University of Michigan) Lab 1: Verilog 09-05-2019 41 / 60
Testing
Intro to Test Benches
Features of the Test BenchI Unsynthesized
I Remember unsynthesizable constructs? This is where they’re used.I In particular, unsynthesizable flow control is useful in testbenches (e.g.
for, while)I Programmatic
I Many programmatic, rather than hardware design, features are availablee.g. functions, tasks, classes (in SystemVerilog)
(University of Michigan) Lab 1: Verilog 09-05-2019 42 / 60
Testing
Anatomy of a Test Bench
A good test bench should, in order. . .1. Declare inputs and outputs for the module(s) being tested2. Instantiate the module (possibly under the name DUT for Device Under
Test)3. Setup a clock driver (if necessary)4. Setup a correctness checking function (if necessary/possible)5. Inside an initial block. . .
5.1 Assign default values to all inputs, including asserting any availablereset signal
5.2 $monitor or $display important signals5.3 Describe changes in input, using good testing practice
(University of Michigan) Lab 1: Verilog 09-05-2019 43 / 60
Testing
Unsythesizable Procedural Blocks
initial BlocksI Procedural blocks, just like alwaysI Contents are simulated once at the beginning of a simulationI Used to set values inside a test benchI Should only be used in test benches
(University of Michigan) Lab 1: Verilog 09-05-2019 44 / 60
Testing
Unsythesizable Procedural Blocks
initial Block Example
initialbegin
@(negedge clock);reset = 1'b1;in0 = 1'b0;in1 = 1'b1;@(negedge clock);reset = 1'b0;@(negedge clock);in0 = 1'b1;...
end
(University of Michigan) Lab 1: Verilog 09-05-2019 45 / 60
Testing
Tasks and Functions
task
I Reuse commonly repeatedcode
I Can have delays (e.g. #5)I Can have timing information
(e.g. @(negedge clock))I Might be synthesizable
(difficult, not recommended)
function
I Reuse commonly repeatedcode
I No delays, no timingI Can return values, unlike a
taskI Basically combinational logic
(University of Michigan) Lab 1: Verilog 09-05-2019 46 / 60
Testing
Tasks and Functions by Example
task Exampletask exit_on_error;
input [63:0] A, B, SUM;input C_IN, C_OUT;begin
$display("@@@ Incorrect at time %4.0f", $time);$display("@@@ Time:%4.0f clock:%b A:%h B:%h CIN:%b SUM:%h"
"COUT:%b", $time, clock, A, B, C_IN, SUM, C_OUT);$display("@@@ expected sum=%b", (A+B+C_IN) );$finish;
endendtask
(University of Michigan) Lab 1: Verilog 09-05-2019 47 / 60
Testing
Tasks and Functions by Example
function Examplefunction check_addition;
input wire [31:0] a, b;begin
check_addition = a + b;end
endfunction
assign c = check_addition(a,b);
(University of Michigan) Lab 1: Verilog 09-05-2019 48 / 60
Testing
Intro to System Tasks and Functions
I Just like regular tasks and functionsI But they introspect the simulationI Mostly these are used to print informationI Behave just like printf from C
(University of Michigan) Lab 1: Verilog 09-05-2019 49 / 60
Testing
List of System Tasks and Functions
$monitor Used in test benches. Prints every time an argumentchanges. Very bad for large projects.e.g. $monitor("format",signal,...)
$display Can be used in either test benches or design, but not aftersynthesis. Prints once. Not the best debugging techniquefor significant projects.e.g. $display("format",signal,...)
$strobe Like display, but prints at the end of the current simulationtime unit.e.g. $strobe("format",signal,...)
$time The current simulation time as a 64 bit integer.$reset Resets the simulation to the beginning.
$finish Exit the simulator, return to terminal.More available at ASIC World.
(University of Michigan) Lab 1: Verilog 09-05-2019 50 / 60
Testing
Test Benches by Example
Test Bench Setup
module testbench;logic clock, reset, taken, transition, prediction;
two_bit_predictor(.clock(clock),.reset(reset),.taken(taken),.transition(transition),.prediction(prediction));
alwaysbegin
clock = #5 ~clock;end
(University of Michigan) Lab 1: Verilog 09-05-2019 51 / 60
Testing
Test Benches by Example
Test Bench Test Casesinitialbegin
$monitor("Time:%4.0f clock:%b reset:%b taken:%b trans:%b""pred:%b", $time, clock, reset, taken,transition, prediction);
clock = 1'b1;reset = 1'b1;taken = 1'b1;transition = 1'b1;@(negedge clock);@(negedge clock);reset = 1'b0;@(negedge clock);taken = 1'b1;@(negedge clock);...$finish;
end(University of Michigan) Lab 1: Verilog 09-05-2019 52 / 60
Testing
Test Bench Tips
Remember to. . .I Initialize all module inputsI Then assert resetI Use @(negedge clock) when changing inputs to avoid race
conditions
(University of Michigan) Lab 1: Verilog 09-05-2019 53 / 60
Project 1
Project 1 Administrivia
Grading
I Objective GradingI 70 points possibleI Test cases automatically run
I Subjective GradingI 30 points possibleI Verilog style graded by hand
(University of Michigan) Lab 1: Verilog 09-05-2019 54 / 60
Project 1
Project 1 Administrivia
Submission Script
I Run from the directory above the directory your project is in. . .I For example:
$ pwd/afs/umich.edu/user/j/i/jieltan/$ cd classes/eecs470/projects$ lsproject1 project2 project3$ /afs/umich.edu/user/j/i/jieltan/Public/470submit -p 1project1
(University of Michigan) Lab 1: Verilog 09-05-2019 55 / 60
Project 1
Project 1 Hints
Hierarchical Design
I Used to expand modulesI Build a 64-bit adder out of 1-bit addersI Build a 4-bit and out of 2-bit ands
I No additional logic needed
I Project 1 Part C and DI Build a 4-bit priority selector out of 2-bit priority selectorsI Build a 4-bit rotating priority selector out of 2-bit priority selectors
(University of Michigan) Lab 1: Verilog 09-05-2019 56 / 60
Project 1
Project 1 Hints
Hierarchical Design
I Used to expand modulesI Build a 64-bit adder out of 1-bit addersI Build a 4-bit and out of 2-bit ands
I No additional logic neededI Project 1 Part C and D
I Build a 4-bit priority selector out of 2-bit priority selectorsI Build a 4-bit rotating priority selector out of 2-bit priority selectors
(University of Michigan) Lab 1: Verilog 09-05-2019 56 / 60
Project 1
Project 1 Hints
module and2(input [1:0] a,output logic x
);assign x=a[0] & a[1];
endmodule
module and4(input [3:0] in,output logic out
);logic [1:0] tmp;and2 left(.a(in[1:0]),.x(tmp[0]));and2 right(.a(in[3:2]),.x(tmp[1]));and2 top(.a(tmp),.x(out));
endmodule
(University of Michigan) Lab 1: Verilog 09-05-2019 57 / 60
Project 1
Project 1 Hints
and2
and2and2
xtmp[0]
a[0]
xtmp[1]
a[1]
a[1]
in[3]
a[0]
in[2]
a[1]
in[1]
a[0]
in[0]
x
out
and4
(University of Michigan) Lab 1: Verilog 09-05-2019 58 / 60
Project 1
Project 1 Questions
I Any questions about Project 1?
(University of Michigan) Lab 1: Verilog 09-05-2019 59 / 60
Lab Assignment
Lab Assignment
I Follow the tutorial, this is one of the most important documents inthis class. . .
I Assignment on the course website.I Submission: Place yourself on the help queue and we will check you off
(University of Michigan) Lab 1: Verilog 09-05-2019 60 / 60