Professional Documents
Culture Documents
EECS 470 Lab 1: Verilog: Hardware Description Language
EECS 470 Lab 1: Verilog: Hardware Description Language
Verilog
Testing
Project 1
Lab Assignment
Who?
I Contact Information
I Siying Feng - fengsy@umich.edu
I Xueyang Liu - marliu@umich.edu
I Jielun Tan - jieltan@umich.edu
I EECS 470 Piazza (click for link)
I Most of your project related questions should be asked here so that
other 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.
Where? When?
What?
Projects
Advice
Project 4
Administrivia
Intro to Verilog
What is Verilog?
I Hardware Description Language - IEEE 1364-2005
I Superseded by SystemVerilog - IEEE 1800-2009
I Two Forms
1. Behavioral
2. 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.
Intro to Verilog
What is Verilog?
I Hardware Description Language - IEEE 1364-2005
I Superseded by SystemVerilog - IEEE 1800-2009
I Two Forms
1. Behavioral
2. 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?
Intro to Verilog
What is Verilog?
I Hardware Description Language - IEEE 1364-2005
I Superseded by SystemVerilog - IEEE 1800-2009
I Two Forms
1. Behavioral
2. 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
a◦
u0 w_0
b◦ ◦s
u1
cin◦
w_1
u2
u4 ◦cout
w_2
u3
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
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
module one_bit_adder(
input logic a,b,cin,
output logic sum,cout);
always_comb
begin
sum = a ^ b ^ cin;
cout = 1'b0;
if ((a ^ b) & cin) | (a & b))
cout = 1'b1;
end
endmodule
Verilog Semantics
Lexical
I 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 */.
Data Types
Data Types
Types of Values
Values
Literals/Constants
I Written in the format <bitwidth>’<base><constant>
I Options for <base> are. . .
b Binary
o Octal
d Decimal
h Hexadecimal
Verilog Operators
Arithmetic Shift
* Multiplication >> Logical right shift
/ Division << Logical left shift
+ Addition >>> Arithmetic right shift
- Subtraction <<< Arithmetic left shift
% Modulus Relational
** Exponentiation > Greater than
Bitwise >= Greater than or equal to
~ Complement < Less than
& And <= Less than or equal to
| Or != Inequality
~| Nor !== 4-state inequality
ˆ Xor == Equality
~ˆ Xnor === 4-state equality
Logical Special
! Complement {,} Concatenation
&& And {n{m}} Replication
|| Or ?: Ternary
Setting Values
assign Statements
I One line descriptions of combinational logic
I Left hand side must be a wire (SystemVerilog allows assign statements
on logic type)
I Right hand side can be any one line verilog expression
I 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 the
sensitivity list happens
I Two main types in this class. . .
I always_comb
I implied sensitivity lists of every signal inside the block
I Used for combinational logic. Replaced always @*
I always_ff @(posedge clk)
I sensitivity list containing only the positive transition of the clk signal
I Used for sequential logic
I All left hand side signals need to be logic type.
Combinational Block
always_comb
begin
x = a + b;
y = x + 8'h5;
end
Sequential Block
Blocking Example
always_comb
begin
x = new_val1;
y = new_val2;
sum = x + y;
end
I Behave exactly as expected new_val1
I Standard combinational logic x
new_val2
y
sum
Bad Example
always_ff @(posedge clock)
begin
x <= #1 y;
z = x;
end
I z is updated after x clock
I z updates on negedge reset
clock x
y
z
Synthesis Tips
Latches
I What is a latch?
Synthesis Tips
Latches
I What is a latch?
I Memory device without a clock
I Generated by a synthesis tool when a net needs to hold state without
being clocked (combinational logic)
I Generally bad, unless designed in intentionally
I Unnecessary in this class
Synthesis Tips
Latches
I Always assign every variable on every path
I This code generates a latch
I Why does this happen?
always_comb
begin
if (cond)
next_x = y;
end
Synthesis Tips
always_comb
begin
if (cond)
next_x = y;
else
next_x = x;
end
Modules
Intro to Modules
I Basic organizational unit in Verilog
I Can be reused
Module Example
module my_simple_mux(
input wire select_in, a_in, b_in; //inputs listed
output wire muxed_out); //outputs listed
assign muxed_out = select_in ? b_in : a_in;
endmodule
Modules
Writing Modules
I Inputs and outputs must be listed, including size and type
format: <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 instantiation
1. 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
Keys to Synthesizability
Combinational Logic
I Avoid feedback (combinatorial loops)
I Always blocks should
I Be always_comb blocks
I Use the blocking assignment operator =
I All variables assigned on all paths
I Default values
I if(...) paired with an else
Keys to Synthesizability
Sequential Logic
I Avoid clock- and reset-gating
I Always blocks should
I Be always_ff @(posedge clock) blocks
I Use the nonblocking assignment operator, with a delay <= #1
I No path should set a variable more than once
I Reset all variables used in the block
I //synopsys sync_set_reset “reset”
Flow Control
Flow Control
always_comb
begin
if (muxy == 1'b0)
y = a;
else
y = b;
end
wire y;
assign y = muxy ? b : a;
Casez Example
always_comb
begin
casez(alu_op)
3'b000: r = a + b;
3'b001: r = a - b;
3'b010: r = a * b;
...
3'b1??: r = a ^ b;
endcase
end
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 modules
I Better test benches tend to result in higher project scores
initial Blocks
I Procedural blocks, just like always
I Contents are simulated once at the beginning of a simulation
I Used to set values inside a test bench
I Should only be used in test benches
initial
begin
@(negedge clock);
reset = 1'b1;
in0 = 1'b0;
in1 = 1'b1;
@(negedge clock);
reset = 1'b0;
@(negedge clock);
in0 = 1'b1;
...
end
task function
I Reuse commonly repeated I Reuse commonly repeated
code code
I Can have delays (e.g. #5) I No delays, no timing
I Can have timing information I Can return values, unlike a
(e.g. @(negedge clock)) task
I Might be synthesizable I Basically combinational logic
(difficult, not recommended)
task Example
task 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;
end
endtask
function Example
function check_addition;
input wire [31:0] a, b;
begin
check_addition = a + b;
end
endfunction
assign c = check_addition(a,b);
module testbench;
logic clock, reset, taken, transition, prediction;
two_bit_predictor(
.clock(clock),
.reset(reset),
.taken(taken),
.transition(transition),
.prediction(prediction));
always
begin
clock = #5 ~clock;
end
Remember to. . .
I Initialize all module inputs
I Then assert reset
I Use @(negedge clock) when changing inputs to avoid race
conditions
Project 1 Administrivia
Grading
I Objective Grading
I 70 points possible
I Test cases automatically run
I Subjective Grading
I 30 points possible
I Verilog style graded by hand
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
$ ls
project1 project2 project3
$ /afs/umich.edu/user/j/i/jieltan/Public/470submit -p 1
project1
Project 1 Hints
Hierarchical Design
I Used to expand modules
I Build a 64-bit adder out of 1-bit adders
I Build a 4-bit and out of 2-bit ands
I No additional logic needed
Project 1 Hints
Hierarchical Design
I Used to expand modules
I Build a 64-bit adder out of 1-bit adders
I Build a 4-bit and out of 2-bit ands
I No additional logic needed
I Project 1 Part C and D
I Build a 4-bit priority selector out of 2-bit priority selectors
I Build a 4-bit rotating priority selector out of 2-bit priority selectors
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
Project 1 Hints
out
and2
a[1] a[0]
tmp[1] tmp[0]
x x
and2 and2
a[1] a[0] a[1] a[0]
and4
Project 1 Questions
Lab Assignment