DDCArv Ch4
DDCArv Ch4
Computer Architecture
Sarah Harris & David Harris
Chapter 4:
Hardware Description
Languages
Chapter 4 :: Topics
• Introduction
• Combinational Logic
• Delays
• Sequential Logic
• Combinational Logic w/ Always
• Blocking & Nonblocking Assignments
• Finite State Machines
• Parameterized Modules
• Testbenches
Introduction
Introduction
• Hardware description language (HDL):
– Specifies logic function only
– Computer-aided design (CAD) tool produces or
synthesizes the optimized gates
• Most commercial designs built using HDLs
• Two leading HDLs:
– SystemVerilog
• Developed in 1984 by Gateway Design Automation
• IEEE standard (1364) in 1995
• Extended in 2005 (IEEE STD 1800-2009)
– VHDL 2008
• Developed in 1981 by the Department of Defense
• IEEE standard (1076) in 1987
• Updated in 2008 (IEEE STD 1076-2008)
Digital Design & Computer Architecture Hardware Description Languages 4
HDL to Gates
• Simulation
– Inputs applied to circuit
– Outputs checked for correctness
– Millions of dollars saved by debugging in simulation
instead of hardware
• Synthesis
– Transforms HDL code into a netlist describing the hardware
(i.e., a list of gates and the wires connecting them)
IMPORTANT:
When using an HDL, think of the hardware
the HDL should produce, then write the
appropriate idiom that implies that
hardware.
a
SystemVerilog
Verilog
b y
Module
Module
c
a
SystemVerilog
Verilog
b y
Module
Module
c
Synthesis:
b
c y
un5_y y
un8_y
Digital Design & Computer Architecture Hardware Description Languages 11
SystemVerilog Syntax
• Case sensitive
– Example: reset and Reset are not the same signal.
• No names that start with numbers
– Example: 2mux is an invalid name
• Whitespace ignored
• Comments:
– // single line comment
– /* multiline
comment */
Combinational
Logic
Bitwise Operators
SystemVerilog:
module gates(input logic [3:0] a, b,
output logic [3:0] y1, y2, y3, y4, y5);
/* Five different two-input logic
gates acting on 4 bit busses */
assign y1 = a & b; // AND Synthesis:
assign y2 = a | b; // OR
assign y3 = a ^ b; // XOR
assign y4 = ~(a & b); // NAND
assign y5 = ~(a | b); // NOR
endmodule
Synthesis:
Synthesis:
g s
mux2
s
[7:4] [7:4]
d0[3:0] y[3:0]
[7:4]
d1[3:0]
msbmux
Synthesis:
en
[3:0] [3:0] [3:0] [3:0]
a[3:0] y[3:0]
y_1[3:0]
Delays
Delays
SystemVerilog:
module example(input logic a, b, c,
output logic y);
logic ab, bb, cb, n1, n2, n3; Delays are for
assign #1 {ab, bb, cb} = ~{a, b, c}; simulation only! They
assign #2 n1 = ab & bb & cb; do not determine the
assign #2 n2 = a & bb & cb; delay of your
assign #2 n3 = a & bb & c; hardware.
assign #4 y = n1 | n2 | n3;
endmodule
Simulation
Sequential Logic
Sequential Logic
• SystemVerilog uses idioms to describe
latches, flip-flops and FSMs
• Other coding styles may simulate correctly
but produce incorrect hardware
endmodule
Synthesis:
// synchronous reset
always_ff @(posedge clk)
if (reset) q <= 4'b0;
else q <= d;
endmodule
Synthesis:
clk
[3:0] [3:0] [3:0] [3:0]
d[3:0] D[3:0] Q[3:0] q[3:0]
reset R
q[3:0]
// asynchronous reset
always_ff @(posedge clk, posedge reset)
if (reset) q <= 4'b0;
else q <= d;
endmodule
Synthesis:
clk
[3:0] [3:0] [3:0] [3:0]
d[3:0] D[3:0] Q[3:0] q[3:0]
reset R
q[3:0]
endmodule Synthesis:
always_latch
if (clk) q <= d;
Synthesis:
endmodule
[3:0] [3:0]
lat
d[3:0] D[3:0] Q[3:0] [3:0] [3:0]
q[3:0]
clk C
q[3:0]
Warning: We don’t use latches in this text. But you might write code that
inadvertently implies a latch. Check synthesized hardware – if it has latches
in it that you didn’t intend to create, there’s an error.
• Flip-flop: always_ff
• Latch: always_latch (don’t use)
Combinational Logic
using always
if/else and case/casez
Statements that must be inside always
statements:
– if / else
– case, casez
Blocking and
Nonblocking
Assignments
Blocking vs. Nonblocking Assignment
• <= is nonblocking assignment
– Occurs simultaneously with others
• = is blocking assignment
– Occurs in order it appears in file
// Good synchronizer using // Bad synchronizer using
// nonblocking assignments // blocking assignments
module syncgood(input logic clk, module syncbad(input logic clk,
input logic d, input logic d,
output logic q); output logic q);
logic n1; logic n1;
always_ff @(posedge clk) always_ff @(posedge clk)
begin begin
n1 <= d; // nonblocking n1 = d; // blocking
q <= n1; // nonblocking q = n1; // blocking
end end
endmodule endmodule
CLK
M next
next
k state k N
inputs state
state output
outputs
logic
logic
Mealy FSM
CLK
M next
next
k state k state N
inputs state
output
outputs
logic
logic
S 0
S 1
// state register
S 2
always_ff @(posedge clk, posedge reset)
if (reset) state <= S0;
else state <= nextstate;
S 0
// next state logic
always_comb
case (state) S 1
S0: nextstate = S1;
S1: nextstate = S2;
S2: nextstate = S0;
default: nextstate = S0; CLK
endcase M next
next k k N
inputs state
state state output
outputs
logic
// output logic logic
Moore FSM
Reset
0 1
S0 S1 S2
0 0 1
1 0 0
1
Mealy FSM
Reset
0/0
S0 S1
1/0 0/0
1/1
Parameterized
Modules
Parameterized Module
2:1 mux:
module mux2
#(parameter width = 8) // name and default value
(input logic [width-1:0] d0, d1,
input logic s,
output logic [width-1:0] y);
assign y = s ? d1 : d0;
endmodule
Testbenches
Testbenches
• HDL that tests another module: device
under test (dut)
• Not synthesizeable
• Types:
– Simple
– Self-checking
– Self-checking with testvectors
Assign Compare
Inputs Outputs to
Expected
// generate clock
always // no sensitivity list, so it always executes
begin
clk = 1; #5; clk = 0; #5;
end
initial
begin
$readmemb("example.tv", testvectors);
vectornum = 0; errors = 0;
reset = 1; #22; reset = 0;
end