03_systemverilog
03_systemverilog
SystemVerilog
CIS 5710
Computer Organization & Design
This Unit: Digital Logic & Verilog
App App App • Transistors & fabrication
System software
• Digital logic basics
Mem CPU I/O • Focus on useful components
• Hardware design methods
• Introduction to Verilog
fetch
control
• Fetch: get insn, translate opcode into control
• Datapath: performs computation
• registers, ALUs, etc.
• Control: which computation to perform
• Routes data through datapath (which regs, which ALU op)
• Fetch ® Decode ® Execute “cycle” 4
Two Types of Components
datapath
fetch
control
• combinational/combinatorial
• stateless computation
• ALUs, muxes, control logic, arbitrary Boolean functions
• sequential
• storage: PC, insn/data memories, register file
• internally contain some combinational components
CIS 5710 | Prof Joseph Devietti 5
Example LC4 Datapath
©IBM
From slides © Krste Asanović, MIT
CIS 5710 | Prof Joseph Devietti 10
Complementary MOS (CMOS)
• Voltages as values
• Power (VDD) = “1”, Ground = “0” power (1)
12
Digital Building Blocks: Logic Gates
• Logic gates implement Boolean functions
• Basic gates: NOT, NAND, NOR
• Underlying CMOS transistors are naturally inverting ( = NOT)
NOT (Inverter) NAND NOR
A A
A A’ (AB)’ (A | B)’
B B
Programmable
connections
Out0
Out1
D
CIS 5710 | Prof Joseph Devietti 24
Adder
• Adder: adds/subtracts two binary integers in
two’s complement format
• Half adder: adds two 1-bit “integers”, no carry-in
• Full adder: adds three 1-bit “integers”, includes carry-in
• Ripple-carry adder: N chained full adders add 2 N-bit
integers
• To subtract: negate B input, set bit 0 carry-in to 1
25
Full Adder
• What is the logic for a full adder?
• Look at truth table CI
CI A B ® C0 S
® S
0 0 0 0 0 CI
0 0 1 ® 0 1 A
A S
0 1 0 ® 0 1 B FA
0 1 1 ® 1 0 B
1 0 0 ® 0 1 CO
1 0 1 ® 1 0
1 1 0 ® 1 0
1 1 1 ® 1 1 CO
A1 S1 A
FA +/- S
B1 B
+/–
…
AN-1 SN-1
FA
BN-1
+/–
• More later when we cover arithmetic!
CIS 5710 | Prof Joseph Devietti 27
FPGAs
Programmable Interconnect
30
Configurable Logic Blocks
• Each of the configurable logic blocks (or logic cells) contains some lookup
tables and one or more flip-flops.
• By setting the entries in the lookup tables (LUTs) these units can be
programmed to implement arbitrary logical functions on their inputs.
• http://en.wikipedia.org/wiki/Field-programmable_gate_array
• ZedBoard has 85K logic cells
AND
AND
XOR XOR
NAND
NAND
Programmable Interconnect
a tic is
st lys
a
an
netlist
SystemVerilog (wires, implementation
synthesis bitstream
code gates, (place & route)
FFs)
sim
ula
tio
n
S
A
O
B
• Draw pictures
• Use a program to draw wires, logic blocks, gates
• Supports hierarchical design (arbitrary nesting)
+ Good match for hardware which is inherently spatial
– Not scalable (except at very high levels of abstraction)
48
SystemVerilog HDL
• Verilog is a huge language
• Structural constructs at both gate and transistor level
• Precise timing specification and simulation
• Lots of behavioral constructs
• Object-oriented features
• A pre-processor
• VPI: Verilog Procedural Interface provides a C API for
simulators
• SystemVerilog Assertions for verification
• …
Structural
module mux2to1(input wire S,
input wire A, S
input wire B, A
output wire Out); Out
wire S_, AnS_, BnS;
not (S_, S); B
and (AnS_, A, S_);
and (BnS, B, S);
or (Out, AnS_, BnS);
endmodule
52
(Gate-Level) Structural Verilog
• Primitive “operators”: gates
• Specifically: and, or, xor, nand, nor, xnor, not, buf
• Can be multi-input: e.g., or (C, A, B, D) (C= A | B | D)
• “Operator” buf just repeats input signal (may amplify it)
Structural
module mux2to1(input wire S, S
input wire A,
input wire B, A
output wire Out); Out
wire S_, AnS_, BnS;
B
not (S_, S);
and (AnS_, A, S_);
and (BnS, B, S);
or (Out, AnS_, BnS);
endmodule
53
(Gate-Level) Behavioral Verilog
• Primitive “operators”: boolean operators
• Specifically: &, |, ^, ~
• Can be combined into expressions
• Can be mixed with structural Verilog
“Behavioral” (Synthesizable)
module mux2to1(input wire S, S
input wire A,
input wire B, A
output wire Out); Out
wire S_, AnS_, BnS;
B
assign S_ = ~S;
assign AnS_ = A & S_;
assign BnS = B & S;
assign Out = AnS_ | BnS;
endmodule
54
Wire Assignment
• Wire assignment:
• Connect combinational logic block or other wire to wire
input
• Order of statements not important, executed totally in
parallel
• When right-hand-side
“Behavioral” (Synthesizable) changes, it is re-evaluated and re-
assigned
module mux2to1(input wire S, S
input wire A,
• Designated by the keyword assign
input wire B, A
output wire Out); Out
wire S_, AnS_, BnS;
B
assign S_ = ~S;
assign AnS_ = A & S_;
assign BnS = B & S;
assign Out = AnS_ | BnS;
endmodule
55
Wire Assignment
• Assignment can be combined with declaration
wire c = a | b;
“Behavioral” (Synthesizable)
S
module mux2to1(input wire S,
input wire A, A
input wire B, Out
output wire Out);
wire S_ = ~S; B
wire AnS_ = A & S_;
wire BnS = B & S;
assign Out = AnS_ | BnS;
endmodule
56
(Gate-Level) Behavioral Verilog
• Primitive “operators”: boolean operators
• Specifically: &, |, ^, ~
• Can be combined into expressions
• Can be mixed with structural Verilog
“Behavioral” (Synthesizable)
module mux2to1(input wire S, S
input wire A,
A
input wire B, Out
output wire Out);
assign Out = (~S & A) | (S & B);B
endmodule
57
Best Way to do a Mux
• Verilog supports ?: conditional assignment
operator
• Much more useful (and common) in Verilog than in C/Java
“Behavioral” (Synthesizable)
module mux2to1(input wire SS,
input wire A,
A
input wire B, Out
output wire Out);
assign Out = S ? B B: A;
endmodule
58
Wires Are Not C-like Variables!
• Order of assignment doesn’t matter
• This works fine
module mux2to1(input wire S,
input wire A,
input wire B,
output wire Out);
assign Out = AnS_ | BnS;
assign BnS = B & S;
assign AnS_ = A & S_;
assign S_ = ~S;
endmodule
• Example:
module mux2to1(input wire S,
input wire [7:0] A,
Unlike C, array range is part
input wire [7:0] B,
of type, not variable!
output wire [7:0] Out);
assign Out = S ? B : A;
endmodule
• Operations
• Bit select: vec[3]
• Range select: vec[3:2]
60
• Concatenate: assign vec = {w, x, y, z};
Wire and Wire Vector Constants
wire [3:0] w = 4’b0101;
• The “4” is the number of bits
• The “b” means “binary” - “h” for hex, “o” for octal, “d” for
decimal
• The “0101” are the digits (in binary in this case)
wire [3:0] w = 4’d5; // same thing, effectively
• Here is a single wire constant
wire w = 1’b0;
62
Gate-Level Vector Operators
• Verilog also supports behavioral vector operators
65
• Why not use high-level operators?
Hierarchical Design using Modules
• Old-style interface specification
module mux2to1(Sel, A, B, Out);
input Sel, A, B;
output Out;
• Can also have inout: bidirectional wire (we will not use this)
• Recommended Alternative: Verilog 2001
interfaces
module mux2to1(input wire Sel, A, B, output
Out);
A and B share same type as Sel. Convenient, but dangerous!
• Declarations
• Internal wires, i.e., “locals”
• Wires also called “nets” or “signals”
66
wire S_, AnS_, BnS;
Verilog Module Example
module mux2to1(input wire Sel,
input wire A,
input wire B, S
output wire Out);
A
wire S_, AnS_, BnS; O
not (S_, Sel); B
and (AnS_, A, S_);
and (BnS, B, Sel);
or (Out, AnS_, BnS);
endmodule
68
Connections via Named Association
• ALWAYS specify module connections by name
• Like named parameters/keyword arguments in SDLs
• Helps keep the bugs away
• Bad example
mux2to1 mux0 (res, s, a, b);
• Good example
mux2to1 mux0 (.Sel(s), .A(a), .B(b), .Out(res));
• Also, order becomes irrelevant
mux2to1 mux1 (.A(a), .B(b), .Out(res), .Sel(s));
69
Generate construct
• basic metaprogramming to reduce repetition
• for loop must have a fixed bound
• think of this as “copy-and-paste”, not a SDL for
loopmodule mux2to1_4(input wire Sel,
input wire [3:0] A,
input wire [3:0] B,
output wire [3:0] Out);
genvar i;
for (i = 0; i < 4; i = i+1) begin
mux2to1 m(.Sel(Sel),.A(A[i]),.B(B[i]),
.Out(Out[i]));
end
endmodule
70
Per-Instance Module Parameters
• Module parameters: useful for defining varying bus
widths
module Nbit_mux2to1 (input wire Sel,
input wire [N-1:0] A,
input wire [N-1:0] B,
output wire [N-1:0] Out);
parameter N = 1;
assign Out = Sel ? B : A;
endmodule
72
Fancy: Generate and Parameters
module rca
#(parameter N = 4)
(input wire [N-1:0] a,
input wire [N-1:0] b,
output wire [N-1:0] s);
wire [N:0] carry;
assign carry[0] = 1'b0;
73
Wire Arrays
• Verilog supports multi-dimensional wire vectors
• Useful with generate loops when you need lots of buses
74
Verilog Pre-Processor
• Like the C pre-processor
• But uses ` (back-tick) instead of #
• Constants: `define
• No parameterized macros
• Use ` before expanding constant macro
`define letter_A 8’h41
wire w[7:0] = `letter_A;
• Conditional compilation: `ifdef, `endif
• File inclusion: `include
• Parameter vs `define
• A parameter is scoped to a module instance
• A `define is scoped to a file (potentially across modules) 75
Verilog Errata
• Wires have binary values: 0 or 1
• except when they don’t: x and z are undefined values
• No particular naming convention for modules and
their files
• Unlike, say, Java public classes
• No “imports” or “libraries”
• There are ways to do this, to integrate 3rd-party Intellectual
Property (IP) into your design. We won’t explore this.
76
Verilog testing constructs
• integer, reg types
• correspond to storage (unlike wire which is stateless)
• DO NOT use these outside of testing!
• we’ll see this later: reg will synthesize into a latch/FF
• delay statement
• #10; means “wait 10 cycles”
• for/while loops
• allow iterating over test inputs
• $display()
• printf-like output
• $display(“wire was %b", a);
• $finish
77
• ends the simulation
Sequential Logic
78
Two Types of Digital Circuits
• Combinational Logic
• Logic without state variables
• Examples: adders, multiplexers, decoders, encoders
• No clock involved
• Sequential Logic
• Logic with state variables
• State variables: latches, flip-flops, registers, memories
• Clocked
• State machines, multi-cycle arithmetic, processors
fetch
control
81
S-R Latch
• S-R (set-reset) latch
R Q
• Cross-coupled NOR gates
• Distinct inputs/outputs
Q’
S,R ® Q S
0,0 ® oldQ
0,1 ® 0 R Q
1,0 ® 1 SR
1,1 ® 0 S
D
Q
D Q
DL
E
83
Timing Diagrams
• Voltage {0,1} diagrams for different nodes in
system
• “Digitally stylized”: changes are vertical lines
(instantaneous?)
• Reality is analog, changes are continuous and smooth
E
• Timing diagram for a D latch
D
84
Triggering: Level vs. Edge
E
Q
86
FFWE: FF with Separate Write Enable
• FFWE: FF with separate write enable
• FF D(ata) input is MUX of D and Q, WE selects
D Q Q
FF D FFWE
WE
WE
• Bad idea: why not just AND the CLK and WE?
+Fewer gates
– Creates timing problems
§ Do not try to do logic on CLK in Verilog
§ No, really. Never do this. 87
N-bit Register
D0 Q0
FFWE
D1 Q1
n n
FFWE
D Q
DN-1 QN-1
FFWE
WE
89
Designing Sequential Logic
• key design rule: separate combinational logic from
sequential state elements
• Not enforced by Verilog, but a very good idea
• Possible exceptions: counters, shift registers
• We’ll give you a flip-flop module (see next slide)
• Edge-triggered, not a transparent latch
• Parameterized to create an n-bit register
• Example
Clock
use: state machine
State Combinational Output
Register Current Logic
State
Next State
90
Sequential Logic In Verilog
• How are state-holding variables specified in
Verilog?
• First instinct: structurally
• After all, real latches and flip-flops are made from gates…
reg out;
always @(posedge clock)
begin • reg: storage bit
if (reset) • always @ (): synthesizable
out = 0; behavioral sequential Verilog
else if (writeEnable) • Tricky: hard to know exactly what
out = in; it will synthesize to
end • We will give this to you,
endmodule don’t write your own
• “Creativity is a poor substitute for
knowing what you’re doing”
92
Verilog Register (Behavioral Magic)
• How do we specify state-holding constructs in Verilog?
module register (output wire [n-1:0] out,
input wire [n-1:0] in, input wire writeEnable,
input wire reset, input wire clock);
parameter n = 1;
• Ramifications:
• Never do logic operations on the clocks
• If you want to add a “write enable” to a flip-flop:
• Use a mux to route the old value back into it
• (or use the flip-flop with write enable we give you!)
• Do not just “and” the write-enable signal with the clock!
95
Testbenches
• A more effective way to test & debug designs
• In Java?
• Write test code in Java to test Java
• “Test harness”, “unit testing”
• For Verilog?
• Write test code in Verilog to test Verilog
• Verilog has advanced “behavioral” commands to facilitate
this:
• Delay for n units of time
• Full high-level constructs: if, while, sequential
assignment, ints
• Input/output: file I/O, output to display, etc. 96
Common Errors
• Tools are from a less civilized time
• More like C, less like Java
• Assume that you mean what you say
• Common errors:
• Not assigning a wire a value
• Assigning a wire a value more than once
• Implicit wire declarations (default to type “wire” 1-bit wide)
• Mis-matched wire assignment widths
• Combinational loops
• Avoid names such as:
• clock, clk, power, pwr, ground, gnd, vdd, vcc, init, reset, rst
• Some of these are “special” and will silently cause errors
97
Official Vivado Verilog Reference
98
List of Verilog keywords
always endspecify medium rnmos vectored
and endtable module rpmos wait
assign endtask nand rtran wand
automatic event negedge rtranif0 weak0
begin for nmos rtranif1 weak1
buf force nor scalared while
bufif0 forever noshow-cancelled* show-cancelled* wire
bufif1 fork not signed wor
case function notif0 small xnor from Chapter 7 of
casex generate notif1 specify xor Vivado Design Suite
casez genvar or specpa User Guide: Synthesis
cell* highz0 output strong0 UG901 (v2017.4)
cmos highz1 parameter strong1
config* if pmos supply0
deassign ifnone posedge supply1
default incdir* primitive table
defparam include* pull0 task
design* initial pull1 time
disable inout pullup* tran
edge input pulldown* tranif0
else instance* pulsestyle_ondetect* tranif1
end integer pulsestyle_onevent* tri
endcase join rcmos tri0
endconfig* larger real tri1
endfunction liblist* realtime triand
endgenerate library* reg trior
endmodule localparam release trireg
99
endprimitive macromodule repeat use*
Additional Verilog Resources
• Elements of Logic Design Style by Shing Kong,
2001
• Do’s, do-not’s, other tips
• http://www.cis.upenn.edu/~milom/elements-of-logic-design-
style/
101