Verilog UBC
Verilog UBC
Verilog
Steve Wilton
Dept. of ECE
University of British Columbia
[email protected]
1
Hardware Description Languages
2
Hardware Description Languages
There are many different languages for modeling and simulating hardware.
– Verilog
– VHDL
– M-language (Mentor)
– AHDL (Altera)
– SystemC
– Aida (IBM / HaL)
– …. and many others
3
Verilog from 20,000 feet
Verilog Descriptions look like software programs:
• Block structure is a key principle
• Use hierarchy/modularity to manage complexity
C / Pascal Verilog
Procedures/Functions Modules
Procedure parameters Ports
Variables Wires / Regs
4
Verilog (or any HDL) View of the World
A design consists of a set of communicating modules
b
Ctrl Memory
c
bus d
a
Datapath
• There are graphical user inferfaces for Verilog, but we will not
use them
• Instead we will use the text method. Label the wires, and use
them as ‘parameters’ in the module calls.
5
Simple Gate:
6
Can describe a block with several outputs:
7
A one bit multiplexer can be described this way:
8
A Four Bit-Multiplexor:
9
Structural Descriptions:
IN1
U0 X U1 OUT1
IN2
Internal Signal
module my_gate(IN1, IN2, OUT1);
input IN1, IN2; Submodule name
output OUT1; (defined elsewhere)
wire X;
Instance Name
AND_G U0 (IN1, IN2, X);
NOT_G U1 (X, OUT1);
endmodule;
10
Bigger Structural Example
module system;
wire [7:0] bus_v1, const_s1; clkgen
wire [2:0] regSpec_s1, regSpecA_s1, regSpecB_s1;
wire [1:0] opcode_s1; patternsource
Phi2 Phi1
wire Phi1, Phi2, writeReg_s1,
ReadReg_s1,nextVector_s1
clkgen clkgen(Phi1, Phi2);
datapath datapath(Phi1, Phi2, regSpec_s1, bus_v1,
writeReg_s1, readReg_s1); controller
controller controller1(Phi1, Phi2, regSpec_s1, bus_v1,
readReg
writeReg
regSpec
bus
const_s1, writeReg_s1, readReg_s1,
opcode_s1, regSpecA_s1, regSpecB_s1,
nextVector_s1);
patternsource patternsource(Phi1, Phi2,nextVector_s1,
opcode_s1, regSpecA_s1, regSpecB_s1, datapath
const_s1);
11
Suppose we have defined:
{ C, S }
Is a 5 bit bus:
12
Behavioural Description of an Adder:
13
Behavioural Description of an Flip-Flop:
output Q;
Like a process in VHDL
reg Q;
Equivalent to a
“sensitivity list”
always @(posedge CLK or posedge RESET)
begin
if (RESET == 1)
Q <= 0;
else
Q <= D;
end
endmodule;
14
REG vs WIRE signals:
• Note: this does not mean a register will actually be used. You
can declare purely combinational blocks, where no register is to
be used. But, you still must declare the outputs of the always
block as REG.
15
Behavioural Description of a Comb. Block:
16
Activation List
But also enables subtle errors to enter into the design (as in VHDL)
17
Activation List Examples
always @(phi) vs always @(phi) vs always @(phi or in)
outA =in; if(phi) outB = in; if(phi) outC = in;
phi
in
outA
outB
outC
18
Initial Block
19
Wire vs. Reg
20
+ Delays in Verilog
21
+ Delays in Verilog
This code will wait 10 ticks after either input changes, then checks to see if
phi == 1, and then updates the output. If you wanted to sample the
input when it changed, and then update the output later, you need to
place the delay in a different place:
This code runs the code every time the inputs change, and just delays the
update of the output for 10 ticks.
22
+ Delays in Verilog
always
#100 out = in;
Since the always does not have an activation, it runs all the time.
As a result every 100 time ticks the output is updated with the
current version of the input.
23
Simple Example
phi1
phi2
clkGen
Sum_s1
A_v1
B_v1 SerAdd
Reset_s2
testAdd
24
// serAdd.v -- 2 phase serial adder module
module serAdd(Sum_s1, A_v1, B_v1, Reset_s2,
phi1, phi2);
output Sum_s1;
input A_v1, B_v1, phi1, phi2, Reset_s2;
reg Sum_s1;
reg A_s2, B_s2, Carry_s1, Carry_s2;
always @(phi1 or A_v1)
if (phi1)
A_s2 = A_v1;
always @(phi1 or B_v1)
if (phi1)
B_s2 = B_v1;
initial
begin
phi1 = 0;
phi2 = 0;
end
always
begin
#100
phi1 = 0;
#20
phi2 = 1;
#100
phi2 = 0;
#20
phi1 = 1;
end
endmodule
/*
The above clock generator will produce a clock with a period of 240 units of
simulation time.
*/
/* // test module for the adder
module testAdd; // top level
wire A_v1, B_v1;
reg Reset_s2;
serAdd serAdd(Sum_s1, A_v1, B_v1, Reset_s2, phi1, phi2);
/*
The serial adder takes inputs during phi1
and produces _s1 outputs during phi2.
The _s1 output corresponds to the addition of
the inputs at the previous falling edge of phi1
*/
clkGen clkGen(phi1,phi2);
From what I’ve told you, you don’t know enough Verilog to
thoroughly understand that code.
29