Professional Documents
Culture Documents
Lecture #12: Verilog: From Nand To Tetris
Lecture #12: Verilog: From Nand To Tetris
Verilog
From Nand to Tetris
www.nand2tetris.org
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 1
Outline
• Combinational
• Sequential module adder( input [3:0] A, B,
output cout,
output [3:0] S );
wire c0, c1, c2;
FA fa0( A[0], B[0], 1’b0, c0, S[0] );
FA fa1( A[1], B[1], c0, c1, S[1] );
FA fa2( A[2], B[2], c1, c2, S[2] );
FA fa3( A[3], B[3], c2, cout, S[3] );
endmodule
105
tangentsoft.net/elec/breadboard.html
No symbolic 104
execution or
testing 103
home.cogeco.ca 10
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 3
HDLs enabled logic level
simulation and testing
Number of Gates
Gate Level in Design
Description
107
Simulate
Test 106
Results
Manual 105
104
103
102
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 4
Designers began to use HDLs
for higher level design
Number of Gates
in Design
Behavioral Simulate 107
Test
Algorithm Results 106
105
Simulate
Register Test
Transfer Level Results 104
103
Simulate
Test
Gate Level Results 102
10
Manual
For us:
Behavioral == Truth table
RTL == Interface
GL == HDL
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 5
HDLs led to tools for
automatic translation
Number of Gates
in Design
10
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 7
Current Situation
C, C++, Behavioral RTL
MATLAB
SystemC Verilog, VHDL,
SystemVerilog
Structural RTL
Register Verilog, VHDL,
Transfer Level
SystemVerilog
Logic Synthesis
Gate Level
Simulators and other tools
are available at all levels but
not compilers from the
behavioral level to RTL
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 8
Data Type (Bit-vector)
Value Meaning
0 Logic zero
1 Logic one
X Unknown logic value
Z High impedance, floating
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 9
“wire” is used to denote a
hardware net
small_net
instruction
instruction
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 10
Bit literals Binary literals
n 8’b0000_0000
n 8’b0xx0_1xx1
Hexadecimal literals
4’b10_11
n 32’h0a34_def1
n 16’haxxx
Underscores
are ignored
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 11
Structural Verilog
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 12
A Verilog module has a
name and a port list
A B
module adder( A, B, cout, sum );
4 4 input [3:0] A;
input [3:0] B;
output cout;
adder output [3:0] sum;
// HDL modeling of
4
// adder functionality
cout sum endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 13
Alternate syntax
A B Traditional Verilog-1995 Syntax
(previous slide)
4 4
cout sum
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 14
A module can instantiate
other modules cout
a b
cin
A B FA
c
adder FA FA FA FA
cout S
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 15
Connecting modules
A B
adder FA FA FA FA
cout S
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 16
Alternative syntax
Connecting ports by ordered list
FA fa0( A[0], B[0], 1’b0, c0, S[0] );
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 17
Simple Behavior
• A module’s behavior can be described in many
different ways but it should not maCer from outside
Example: mux4
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 18
Mux4:
Gate-level structural Verilog
module mux4(input a,b,c,d, input [1:0] sel, output out);
wire [1:0] sel_b;
not not0( sel_b[0], sel[0] );
not not1( sel_b[1], sel[1] );
b d a c sel[1]sel[0]
wire n0, n1, n2, n3;
and and0( n0, c, sel[1] );
and and1( n1, a, sel_b[1] );
and and2( n2, d, sel[1]);
and and3( n3, b, sel_b[1]);
wire x0, x1; Looks
nor nor0( x0, n0, n1 ); familiar….
nor nor1( x1, n2, n3 );
wire y0, y1;
or or0( y0, x0, sel[0] );
or or1( y1, x1, sel_b[0] );
nand nand0( out, y0, y1 );
endmodule
out
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 19
Mux4:
Using continuous assignments to
generate combinational logic
Language defined
module mux4( input a, b, c, d operators
input [1:0] sel,
output out );
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 20
Mux4: Behavioral style
(Conditional Statement)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 21
mux4: Using “always block”
module mux4( input a, b, c, d
input [1:0] sel,
output out );
reg out, t0, t1; //notice reg!
endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 23
What happens if the case
statement is not complete?
module mux3( input a, b, c
input [1:0] sel,
output out );
reg out;
always @( * ) If sel = 3, mux will output
begin the previous value!
case ( sel )
2’d0 : out = a; Why?
2’d1 : out = b;
2’d2 : out = c; What have we done?!
endcase
end
endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 24
What happens if the case
statement is not complete?
module mux3( input a, b, c
input [1:0] sel,
output out );
reg out;
always @( * )
begin We CAN prevent creating
case ( sel ) state with a default
2’d0 : out = a;
statement
2’d1 : out = b;
2’d2 : out = c;
default: out = 1’bx;
endcase
end
endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 25
Parameterized mux4
module mux4 #( parameter WIDTH = 1 ) default value
( input[WIDTH-1:0] a, b, c, d
input [1:0] sel,
output[WIDTH-1:0] out );
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 26
Verilog Registers “reg”
• Wires are line names – they do not represent storage and
can be assigned only once
• Regs are imperaKve variables (as in C):
– “nonblocking” assignment r <= v
– can be assigned mulKple Kmes and holds values between
assignments
• Blocking Statements
– must be executed before the execuKon of the
statements that follow it in a sequenKal block.
• Nonblocking Statements
– Use whenever you want to make several register
assignments within the same Kme step without regard to
order or dependence upon each other
– resemble actual hardware more than blocking
assignments
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 27
flip-flops
module FF0 (input clk, input d,
output reg q); next_X D Q X
always @( posedge clk ) clk
begin
q <= d;
end
endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 28
flip-flops with reset
always @( posedge clk) reset
begin next_X D Q X
if (reset)
Q <= 0; clk
else if ( enable ) enable
Q <= D;
end synchronous reset
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 29
Sync vs. Async Resets
Advantages: Advantages:
• Flip-Flop less complex, smaller • reset has priority over other signals
• Circuit completely synchronous • occurs with or w/o clock
Disadvantages: Disadvantages:
• gets complex in combinaKonal logic • reset value must clear in less than a
• resets may not occur in same clock clock cycle
cycle • reset line sensiKve to glitches
• requires clock
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 30
Latches versus flip-flops
module latch module flipflop
( (
input clk, input clk, input
input d, d, output
output reg q reg q
); );
always @( clk or d ) always @( posedge clk )
begin begin
if ( clk ) q <= d;
q <= d; end
end Edge-triggered
endmodule
always block
endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 31
Register
endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 32
Register in terms of
Flipflops
module register2 module register2
( input clk, ( input clk,
input [1:0] d, input [1:0] d,
input en, input en,
output [1:0] q ); output [1:0] q
);
always @(posedge clk) FF ff0 (.clk(clk), .d(d[0]),
begin .en(en), .q(q[0]));
if (en)
q <= d; FF ff1 (.clk(clk), .d(d[1]),
end .en(en), .q(q[1]));
endmodule endmodule
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 33
Three abstraction levels for
functional descriptions
Behavioral
Algorithm V Abstract algorithmic
description
Manual
Low-level netlist
Gate Level V of primitive gates
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 34
Source: http://www.asic-world.com/verilog/
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 35
Hello, Verilog World!
Hello World program:
• Open your favorite text editor module main;
• Code to place inside of hello.v initial
• Compiling program: begin
– Open Command Prompt window $display("Hello, World!");
• Start, then type cmd, $finish ;
then hit Enter end
– Navigate to where hello.v is stored endmodule
– Type following command:
• iverilog –o hello hello.v
• Running program:
– vvp hello
• Output in console:
– Hello, Verilog World!
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 36
Testbench
How do we build/run chips like we did in HDL?
• Equivalent of Chip file (.hdl) == Verilog file of chip (.v)
• Simulator == vvp (command line)
• Test files (.tst) == Testbench (.v with test only)
//counter_tb.v //counter.v
module counter_tb; module counter(out, clk, reset);
parameter WIDTH = 8;
/* Make a reset that pulses once. */
reg reset = 0;
initial begin output [WIDTH-1 : 0] out;
# 17 reset = 1; input clk, reset;
# 11 reset = 0;
# 29 reset = 1; reg [WIDTH-1 : 0] out;
# 11 reset = 0;
wire clk, reset;
# 100 $stop;
end
always @(posedge clk)
/* Make a regular pulsing clock. */ out <= out + 1;
reg clk = 0;
always #5 clk = !clk; always @reset
wire [7:0] value;
if (reset)
counter c1 (value, clk, reset); assign out = 0;
else
initial deassign out;
$monitor("At time %t, value = %h (%0d)",
$time, value, value); endmodule // counter
endmodule // counter_tb
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 37
Testbench
• Compiling modules
• Running testbench
• Ending simulation
– type finish and hit Enter
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 38
Perspective • Waveform simulators + viewers:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 39
Perspective
• Other HDL (VHDL):
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 40