SlideShare a Scribd company logo
1 of 46
Dept of CSE, IIT Kharagpur 1
1
The Verilog Week
Debdeep Mukhopadhyay
Associate Professor
Dept of Computer Science and Engineering
NYU Shanghai and IIT Kharagpur
Dept of CSE, IIT Kharagpur 2
How it started!
• Gateway Design Automation
• Cadence purchased Gateway in 1989.
• Verilog was placed in the public domain.
• Open Verilog International (OVI) was
created to develop the Verilog Language
as IEEE standard.
Dept of CSE, IIT Kharagpur 3
The Verilog Language
• Originally a modeling language for a very efficient event-
driven digital logic simulator
• Later pushed into use as a specification language for
logic synthesis
• Now, one of the two most commonly-used languages in
digital hardware design (VHDL is the other)
• Virtually every chip (FPGA, ASIC, etc.) is designed in
part using one of these two languages
• Combines structural and behavioral modeling styles
Dept of CSE, IIT Kharagpur 4
Concurrency
• Verilog or any HDL has to have the power to
model concurrency which is natural to a piece of
hardware.
• There may be pieces of two hardware which are
even independent of each other.
• Verilog gives the following constructs for
concurrency:
– always
– assign
– module instantiation
– non-blocking assignments inside a sequential block
Dept of CSE, IIT Kharagpur 5
However…
• The simulating machine is sequential.
• no-matter how many processors I have, I can
write one more process which also have to be
simulated concurrently.
• So, the processes are scheduled sequentially so
that we have a feeling of parallelism:
– like the desktop of our PC where we may open an
editor, a netscape and so on. We have a feeling that
they are executed parallely, but in reality there is a
serialization involved.
Dept of CSE, IIT Kharagpur 6
Race…
• So, always blocks (for example) are parallel…
• Even though they are not actually.
• So, which always block gets executed first? My
simulation results will depend upon that!
• Standards do not say anything.
• It may be that the last always block is executed
first…
• and so we have race!
Dept of CSE, IIT Kharagpur 7
How do we overcome race?
• We shall see in the class…
• Try to think of a clean hardware and you
will not have race
Dept of CSE, IIT Kharagpur 8
Multiplexer Built From Primitives
module mux(f, a, b, sel);
output f;
input a, b, sel;
and g1(f1, a, nsel),
g2(f2, b, sel);
or g3(f, f1, f2);
notg4(nsel, sel);
endmodule a
b
sel
f
nsel
f1
f2
g1
g2
g3
g4
Verilog programs built from modules
Each module has
an interface
Module may contain
structure: instances of
primitives and other
modules
Dept of CSE, IIT Kharagpur 9
Multiplexer Built From Primitives
module mux(f, a, b, sel);
output f;
input a, b, sel;
and g1(f1, a, nsel),
g2(f2, b, sel);
or g3(f, f1, f2);
notg4(nsel, sel);
endmodule a
b
sel
f
nsel
f1
f2
g1
g2
g3
g4
Identifiers not
explicitly defined
default to wires
Dept of CSE, IIT Kharagpur 10
Multiplexer Built With Always
module mux(f, a, b, sel);
output f;
input a, b, sel;
reg f;
always @(a or b or sel)
if (sel) f = b;
else f = a;
endmodule a
b
sel
f
Modules may contain one
or more always blocks
Sensitivity list
contains signals
whose change
triggers the
execution of the
block
Dept of CSE, IIT Kharagpur 11
Multiplexer Built With Always
module mux(f, a, b, sel);
output f;
input a, b, sel;
reg f;
always @(a or b or sel)
if (sel) f = b;
else f = a;
endmodule a
b
sel
f
A reg behaves like memory:
holds its value until
imperatively assigned
otherwise
Body of an always
block contains
traditional imperative
code
Dept of CSE, IIT Kharagpur 12
Mux with Continuous Assignment
module mux(f, a, b, sel);
output f;
input a, b, sel;
assign f = sel ? b : a;
endmodule
a
b
sel
f
LHS is always set to the
value on the RHS
Any change on the right
causes re-evaluation
Dept of CSE, IIT Kharagpur 13
Identifiers in Verilog
• Any Sequence of letter, digits, dollar sign,
underscore.
• First character must be a letter or
underscore.
• It cannot be a dollar sign.
• Cannot use characters such as hyphen,
brackets, or # in verilog names
Dept of CSE, IIT Kharagpur 14
Verilog Logic Values
• Predefined logic value system or value set
: ‘0’, ‘1’ ,’x’ and ‘z’;
• ‘x’ means uninitialized or unknown logic
value
• ‘z’ means high impedance value.
Dept of CSE, IIT Kharagpur 15
Verilog Data Types
• Nets: wire, supply1, supply0
– wire:
i) Analogous to a wire in an ASIC.
ii) Cannot store or hold a value.
• Integer: used for the index variables of say
for loops. No hardware implication.
Dept of CSE, IIT Kharagpur 16
The reg Data Type
• Register Data Type: Comparable to a variable in a programming
language.
• Default initial value: ‘x’
• module reg_ex1;
reg Q; wire D;
always @(posedge clk) Q=D;
• A reg is not always equivalent to a hardware register, flipflop or
latch.
• module reg_ex2; // purely combinational
reg c;
always @(a or b) c=a|b;
endmodule
Dept of CSE, IIT Kharagpur 17
Difference between driving and
assigning
• Programming languages provide variables that
can contain arbitrary values of a particular type.
• They are implemented as simple memory
locations.
• Assigning to these variables is the simple
process of storing a value into the memory
location.
• Verilog reg operates in the same way. Previous
assignments have no effect on the final result.
Dept of CSE, IIT Kharagpur 18
Example
• module assignments;
reg R;
initial R<=#20 3;
initial begin
R=5; R=#35 2;
end
initial begin
R<=#100 1;
#15 R=4;
#220;
R=0;
end
endmodule
The variable R is shared by all the
concurrent blocks.
R takes the value that was last
assigned.
This is like a hardware register which
also stores the value that was last
loaded into them.
But a reg is not
necessarily a hardware
register.
Dept of CSE, IIT Kharagpur 19
Wire: helps to connect
• Consider a set of tristate drivers
connected to a common bus.
• The output of the wire depends on all the
outputs and not on the last one.
• To model connectivity, any value driven by
a device must be driven continuously onto
that wire, in parallel with the other driving
values.
Dept of CSE, IIT Kharagpur 20
Code
• module simple(A,B,C,sel,Z);
input A, B, C;
input [1:0] sel;
output Z;
reg Z;
always @(A or B or C or SEL)
begin
2’b00: Z=1’bz;
2’b01: Z=A;
2’b10: Z=B;
2’b11: Z=C;
endcase
end
endmodule
• module simple(A,B,C,sel,Z);
input A, B, C;
input [1:0] sel;
output Z;
assign Z=(SEL==2’b01)?A: 1’bz;
assign Z=(SEL==2’b10)?B:1’bz;
assign Z=(SEL==2’b11)?C:1’bz;
endmodule
Inferred as a multiplexer.
But we wanted drivers!
Dept of CSE, IIT Kharagpur 21
Numbers
• Format of integer constants:
Width’ radix value;
• Verilog keeps track of the sign if it is
assigned to an integer or assigned to
a parameter.
• Once verilog looses sign the designer has
to be careful.
Dept of CSE, IIT Kharagpur 22
Hierarchy
• Module interface provides the means to
interconnect two verilog modules.
• Note that a reg cannot be an input/ inout
port.
• A module may instantiate other modules.
Dept of CSE, IIT Kharagpur 23
Instantiating a Module
• Instances of
module mymod(y, a, b);
• Lets instantiate the module,
mymod mm1(y1, a1, b1); // Connect-by-position
mymod mm2(.a(a2), .b(b2), .y(c2)); // Connect-by-name
Dept of CSE, IIT Kharagpur 24
Sequential Blocks
• Sequential block is a group of statements
between a begin and an end.
• A sequential block, in an always statement
executes repeatedly.
• Inside an initial statement, it operates only once.
Dept of CSE, IIT Kharagpur 25
Procedures
• A Procedure is an always or initial
statement or a function.
• Procedural statements within a sequential
block executes concurrently with other
procedures.
Dept of CSE, IIT Kharagpur 26
Assignments
• module assignments
// continuous assignments
always // beginning of a procedure
begin //beginning of a sequential block
//….Procedural assignments
end
endmodule
• A Continuous assignment assigns a value to a wire like a real gate
driving a wire.
module holiday_1(sat, sun, weekend);
input sat, sun; output weekend;
// Continuous assignment
assign weekend = sat | sun;
endmodule
module holiday_2(sat, sun, weekend);
input sat, sun; output weekend;
reg weekend;
always @(sat or sun)
weekend = sat | sun; // Procedural
endmodule // assignment
Dept of CSE, IIT Kharagpur 27
Blocking and Nonblocking
Assignments
• Blocking procedural assignments must be
executed before the procedural flow can
pass to the subsequent statement.
• A Non-blocking procedural assignment is
scheduled to occur without blocking the
procedural flow to subsequent statements.
Dept of CSE, IIT Kharagpur 28
Nonblocking Statements are odd!
a = 1;
b = a;
c = b;
Blocking assignment:
a = b = c = 1
a <= 1;
b <= a;
c <= b;
Nonblocking assignment:
a = 1
b = old value of a
c = old value of b
Dept of CSE, IIT Kharagpur 29
Nonblocking Looks Like Latches
• RHS of nonblocking taken from latches
• RHS of blocking taken from wires
a = 1;
b = a;
c = b;
a <= 1;
b <= a;
c <= b;
1
a b c
“
”
a
b
c
1
“
”
Dept of CSE, IIT Kharagpur 30
Examples
• Blocking:
always @(A1 or B1 or C1 or M1)
begin
M1=#3(A1 & B1);
Y1= #1(M1|C1);
end
• Non-Blocking:
always @(A2 or B2 or C2 or M2)
begin
M2<=#3(A2 & B2);
Y2<=#1(M1 | C1);
end
Statement executed at
time t+3 causing Y1 to be
assigned at time t+4
Statement executed at
time t causing Y2 to be
assigned at time t+1.
Uses old values.
Statement executed at
time t causing M1 to be
assigned at t+3
Statement executed at
time t causing M2 to be
assigned at t+3
Dept of CSE, IIT Kharagpur 31
Order dependency of
Concurrent Statements
• Order of concurrent statements does not
affect how a synthesizer synthesizes a
circuit.
• It can affect simulation results.
Dept of CSE, IIT Kharagpur 32
Order dependency of
Concurrent Statements
• always @(posedge clock)
begin: CONCURR_1
Y1<=A;
end
• always @(posedge clock)
begin: CONCURR_2
if(Y1) Y2=B; else Y2=0;
end
B
A
clk
Y1
Y2
Can you figure out the possible mismatch of simulation with synthesis results?
Dept of CSE, IIT Kharagpur 33
Explanation of the mismatch
• The actual circuit is a concurrent process.
• The first and second flip flop are operating
parallel.
• However if the simulator simulates
CONCURR_1 block before CONCURR_2,
we have an error. Why?
• So, how do we solve the problem?
Dept of CSE, IIT Kharagpur 34
Solution
• always @(posedge clock)
begin
Y1<=A;
if(Y1==1)
Y2<=B;
else
Y2<=0;
end
With non-blocking assignments
the order of the assignments
is immaterial…
Dept of CSE, IIT Kharagpur 35
Parameterized Design
• module vector_and(z, a, b);
parameter cardinality = 1;
input [cardinality-1:0] a, b;
output [cardinality-1:0] z;
wire [cardinality-1:0] z = a & b;
endmodule
• We override these parameters when we instantiate the module as:
module Four_and_gates(OutBus, InBusA, InBusB);
input [3:0] InBusA, InBusB; output[3:0] OutBus;
Vector_And #(4) My_And(OutBus, InBusA, InBusB);
endmodule
Dept of CSE, IIT Kharagpur 36
Functions (cont’d)
• Function Declaration and Invocation
– Declaration syntax:
function <range_or_type> <func_name>;
<input declaration(s)>
<variable_declaration(s)>
begin // if more than one statement needed
<statements>
end // if begin used
endfunction
Dept of CSE, IIT Kharagpur 37
Function Examples
Controllable Shifter
module shifter;
`define LEFT_SHIFT 1'b0
`define RIGHT_SHIFT 1'b1
reg [31:0] addr, left_addr,
right_addr;
reg control;
initial
begin
…
end
always @(addr)begin
left_addr =shift(addr,
`LEFT_SHIFT);
right_addr
=shift(addr,`RIGHT_SHIFT);
end
function [31:0]shift;
input [31:0] address;
input control;
begin
shift = (control==`LEFT_SHIFT)
?(address<<1) : (address>>1);
end
endfunction
endmodule
Dept of CSE, IIT Kharagpur 38
How Are Simulators Used?
• Testbench generates stimulus and checks response
• Coupled to model of the system
• Pair is run simultaneously
Testbench System Model
Stimulus
Response
Result
checker
Dept of CSE, IIT Kharagpur 39
Looking back at our
multiplexer
• “Dataflow” Descriptions of Logic
//Dataflow description of mux
module mux2 (in0, in1, select, out);
input in0,in1,select;
output out;
assign out = (~select & in0)
| (select & in1);
endmodule // mux2
Alternative:
assign out = select ? in1 : in0;
Dept of CSE, IIT Kharagpur 40
TestBench of the Multiplexer
• Testbench
module testmux;
reg a, b, s;
wire f;
reg expected;
mux2 myMux (.select(s), .in0(a), .in1(b), .out(f));
initial
begin
s=0; a=0; b=1; expected=0;
#10 a=1; b=0; expected=1;
#10 s=1; a=0; b=1; expected=1;
end
initial
$monitor(
"select=%b in0=%b in1=%b out=%b, expected out=%b time=%d",
s, a, b, f, expected, $time);
endmodule // testmux
Dept of CSE, IIT Kharagpur 41
A Car Speed Controller
SLOW
FAST
STOP
MEDIUM
Accelerate
Brake
Accelerate
Brake
Accelerate
Brake
~Brake
(~Accelerate) & (~ Brake)
(~Accelerate) & (~ Brake)
Dept of CSE, IIT Kharagpur 42
Car Controller Coding
module fsm_car_speed_1(clk, keys, brake, accelerate,
speed);
input clk, keys, brake, accelerate;
output [1:0] speed;
reg [1:0] speed;
parameter stop = 2'b00,
slow = 2'b01,
mdium = 2'b10,
fast = 2'b11;
Dept of CSE, IIT Kharagpur 43
Car Controller (contd.)
always @(posedge clk or
negedge keys)
begin
if(!keys)
speed = stop;
else if(accelerate)
case(speed)
stop: speed = slow;
slow: speed = mdium;
mdium: speed = fast;
fast: speed = fast;
endcase
else if(brake)
case(speed)
stop: speed = stop;
slow: speed = stop;
mdium: speed = slow;
fast: speed = mdium;
endcase
else
speed = speed;
end
endmodule
Dept of CSE, IIT Kharagpur 44
A Better Way!
• We keep a separate control part where the
next state is calculated.
• The other part generates the output from
the next state.
• We follow this architecture in the coding of
any finite state machines, like ALU, etc. (
to be discussed later)
Dept of CSE, IIT Kharagpur 45
module fsm_car_speed_2(clk, keys,
brake, accelerate, speed);
input clk, keys, brake, accelerate;
output [1:0] speed;
reg [1:0] speed;
reg [1:0] newspeed;
parameter stop = 2'b00,
slow = 2'b01,
mdium = 2'b10,
fast = 2'b11;
always @(keys or brake or
accelerate or speed)
begin
case(speed)
stop:
if(accelerate)
newspeed = slow;
else
newspeed = stop;
slow:
if(brake)
newspeed = stop;
else if(accelerate)
newspeed = mdium;
else
newspeed = slow;
mdium:
if(brake)
newspeed = slow;
else if(accelerate)
newspeed = fast;
else
newspeed = mdium;
fast:
if(brake)
newspeed = mdium;
else
newspeed = fast;
default:
newspeed = stop;
endcase
end
always @(posedge clk or
negedge keys)
begin
if(!keys)
speed = stop;
else
speed = newspeed;
end
endmodule
Dept of CSE, IIT Kharagpur 46
Conclusion : Write codes which can
be translated into hardware !
The following cannot be translated into hardware( non - synthesizable):
• Initial blocks
– Used to set up initial state or describe finite testbench stimuli
– Don’t have obvious hardware component
• Delays
– May be in the Verilog source, but are simply ignored
• In short, write codes with a hardware in your mind. In other words
do not depend too much upon the tool to decide upon the resultant
hardware.
• Finally, remember that you are a better designer than the tool.

More Related Content

Similar to Ver1-iitkgp.ppt

VerilogTutorial-101702.ppt
VerilogTutorial-101702.pptVerilogTutorial-101702.ppt
VerilogTutorial-101702.pptPavanBhandari6
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waitingRoman Elizarov
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfsagar414433
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfsagar414433
 
vlsi design using verilog presentaion 1
vlsi design using verilog   presentaion 1vlsi design using verilog   presentaion 1
vlsi design using verilog presentaion 1MANDHASAIGOUD1
 
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 CertificationsGiacomo Veneri
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with EpsilonSina Madani
 
dokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfdokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfVelmathi Saravanan
 
3.FPGA for dummies: HDL languages
3.FPGA for dummies: HDL languages3.FPGA for dummies: HDL languages
3.FPGA for dummies: HDL languagesMaurizio Donna
 
Digital System Design-Switchlevel and Behavioural Modeling
Digital System Design-Switchlevel and Behavioural ModelingDigital System Design-Switchlevel and Behavioural Modeling
Digital System Design-Switchlevel and Behavioural ModelingIndira Priyadarshini
 
Kroening et al, v2c a verilog to c translator
Kroening et al, v2c   a verilog to c translatorKroening et al, v2c   a verilog to c translator
Kroening et al, v2c a verilog to c translatorsce,bhopal
 
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptxLecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptxwafawafa52
 
Session 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesSession 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesNirav Desai
 

Similar to Ver1-iitkgp.ppt (20)

Verilog
VerilogVerilog
Verilog
 
VerilogTutorial-101702.ppt
VerilogTutorial-101702.pptVerilogTutorial-101702.ppt
VerilogTutorial-101702.ppt
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waiting
 
Klee and angr
Klee and angrKlee and angr
Klee and angr
 
verilog ppt .pdf
verilog ppt .pdfverilog ppt .pdf
verilog ppt .pdf
 
Verilog Cheat sheet-2 (1).pdf
Verilog Cheat sheet-2 (1).pdfVerilog Cheat sheet-2 (1).pdf
Verilog Cheat sheet-2 (1).pdf
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdf
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdf
 
vlsi design using verilog presentaion 1
vlsi design using verilog   presentaion 1vlsi design using verilog   presentaion 1
vlsi design using verilog presentaion 1
 
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 Certifications
 
Vhdl new
Vhdl newVhdl new
Vhdl new
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
 
Verilog_ppt.pdf
Verilog_ppt.pdfVerilog_ppt.pdf
Verilog_ppt.pdf
 
dokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfdokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdf
 
3.FPGA for dummies: HDL languages
3.FPGA for dummies: HDL languages3.FPGA for dummies: HDL languages
3.FPGA for dummies: HDL languages
 
Digital System Design-Switchlevel and Behavioural Modeling
Digital System Design-Switchlevel and Behavioural ModelingDigital System Design-Switchlevel and Behavioural Modeling
Digital System Design-Switchlevel and Behavioural Modeling
 
Kroening et al, v2c a verilog to c translator
Kroening et al, v2c   a verilog to c translatorKroening et al, v2c   a verilog to c translator
Kroening et al, v2c a verilog to c translator
 
Coding style for good synthesis
Coding style for good synthesisCoding style for good synthesis
Coding style for good synthesis
 
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptxLecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
 
Session 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesSession 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfaces
 

Recently uploaded

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 

Recently uploaded (20)

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 

Ver1-iitkgp.ppt

  • 1. Dept of CSE, IIT Kharagpur 1 1 The Verilog Week Debdeep Mukhopadhyay Associate Professor Dept of Computer Science and Engineering NYU Shanghai and IIT Kharagpur
  • 2. Dept of CSE, IIT Kharagpur 2 How it started! • Gateway Design Automation • Cadence purchased Gateway in 1989. • Verilog was placed in the public domain. • Open Verilog International (OVI) was created to develop the Verilog Language as IEEE standard.
  • 3. Dept of CSE, IIT Kharagpur 3 The Verilog Language • Originally a modeling language for a very efficient event- driven digital logic simulator • Later pushed into use as a specification language for logic synthesis • Now, one of the two most commonly-used languages in digital hardware design (VHDL is the other) • Virtually every chip (FPGA, ASIC, etc.) is designed in part using one of these two languages • Combines structural and behavioral modeling styles
  • 4. Dept of CSE, IIT Kharagpur 4 Concurrency • Verilog or any HDL has to have the power to model concurrency which is natural to a piece of hardware. • There may be pieces of two hardware which are even independent of each other. • Verilog gives the following constructs for concurrency: – always – assign – module instantiation – non-blocking assignments inside a sequential block
  • 5. Dept of CSE, IIT Kharagpur 5 However… • The simulating machine is sequential. • no-matter how many processors I have, I can write one more process which also have to be simulated concurrently. • So, the processes are scheduled sequentially so that we have a feeling of parallelism: – like the desktop of our PC where we may open an editor, a netscape and so on. We have a feeling that they are executed parallely, but in reality there is a serialization involved.
  • 6. Dept of CSE, IIT Kharagpur 6 Race… • So, always blocks (for example) are parallel… • Even though they are not actually. • So, which always block gets executed first? My simulation results will depend upon that! • Standards do not say anything. • It may be that the last always block is executed first… • and so we have race!
  • 7. Dept of CSE, IIT Kharagpur 7 How do we overcome race? • We shall see in the class… • Try to think of a clean hardware and you will not have race
  • 8. Dept of CSE, IIT Kharagpur 8 Multiplexer Built From Primitives module mux(f, a, b, sel); output f; input a, b, sel; and g1(f1, a, nsel), g2(f2, b, sel); or g3(f, f1, f2); notg4(nsel, sel); endmodule a b sel f nsel f1 f2 g1 g2 g3 g4 Verilog programs built from modules Each module has an interface Module may contain structure: instances of primitives and other modules
  • 9. Dept of CSE, IIT Kharagpur 9 Multiplexer Built From Primitives module mux(f, a, b, sel); output f; input a, b, sel; and g1(f1, a, nsel), g2(f2, b, sel); or g3(f, f1, f2); notg4(nsel, sel); endmodule a b sel f nsel f1 f2 g1 g2 g3 g4 Identifiers not explicitly defined default to wires
  • 10. Dept of CSE, IIT Kharagpur 10 Multiplexer Built With Always module mux(f, a, b, sel); output f; input a, b, sel; reg f; always @(a or b or sel) if (sel) f = b; else f = a; endmodule a b sel f Modules may contain one or more always blocks Sensitivity list contains signals whose change triggers the execution of the block
  • 11. Dept of CSE, IIT Kharagpur 11 Multiplexer Built With Always module mux(f, a, b, sel); output f; input a, b, sel; reg f; always @(a or b or sel) if (sel) f = b; else f = a; endmodule a b sel f A reg behaves like memory: holds its value until imperatively assigned otherwise Body of an always block contains traditional imperative code
  • 12. Dept of CSE, IIT Kharagpur 12 Mux with Continuous Assignment module mux(f, a, b, sel); output f; input a, b, sel; assign f = sel ? b : a; endmodule a b sel f LHS is always set to the value on the RHS Any change on the right causes re-evaluation
  • 13. Dept of CSE, IIT Kharagpur 13 Identifiers in Verilog • Any Sequence of letter, digits, dollar sign, underscore. • First character must be a letter or underscore. • It cannot be a dollar sign. • Cannot use characters such as hyphen, brackets, or # in verilog names
  • 14. Dept of CSE, IIT Kharagpur 14 Verilog Logic Values • Predefined logic value system or value set : ‘0’, ‘1’ ,’x’ and ‘z’; • ‘x’ means uninitialized or unknown logic value • ‘z’ means high impedance value.
  • 15. Dept of CSE, IIT Kharagpur 15 Verilog Data Types • Nets: wire, supply1, supply0 – wire: i) Analogous to a wire in an ASIC. ii) Cannot store or hold a value. • Integer: used for the index variables of say for loops. No hardware implication.
  • 16. Dept of CSE, IIT Kharagpur 16 The reg Data Type • Register Data Type: Comparable to a variable in a programming language. • Default initial value: ‘x’ • module reg_ex1; reg Q; wire D; always @(posedge clk) Q=D; • A reg is not always equivalent to a hardware register, flipflop or latch. • module reg_ex2; // purely combinational reg c; always @(a or b) c=a|b; endmodule
  • 17. Dept of CSE, IIT Kharagpur 17 Difference between driving and assigning • Programming languages provide variables that can contain arbitrary values of a particular type. • They are implemented as simple memory locations. • Assigning to these variables is the simple process of storing a value into the memory location. • Verilog reg operates in the same way. Previous assignments have no effect on the final result.
  • 18. Dept of CSE, IIT Kharagpur 18 Example • module assignments; reg R; initial R<=#20 3; initial begin R=5; R=#35 2; end initial begin R<=#100 1; #15 R=4; #220; R=0; end endmodule The variable R is shared by all the concurrent blocks. R takes the value that was last assigned. This is like a hardware register which also stores the value that was last loaded into them. But a reg is not necessarily a hardware register.
  • 19. Dept of CSE, IIT Kharagpur 19 Wire: helps to connect • Consider a set of tristate drivers connected to a common bus. • The output of the wire depends on all the outputs and not on the last one. • To model connectivity, any value driven by a device must be driven continuously onto that wire, in parallel with the other driving values.
  • 20. Dept of CSE, IIT Kharagpur 20 Code • module simple(A,B,C,sel,Z); input A, B, C; input [1:0] sel; output Z; reg Z; always @(A or B or C or SEL) begin 2’b00: Z=1’bz; 2’b01: Z=A; 2’b10: Z=B; 2’b11: Z=C; endcase end endmodule • module simple(A,B,C,sel,Z); input A, B, C; input [1:0] sel; output Z; assign Z=(SEL==2’b01)?A: 1’bz; assign Z=(SEL==2’b10)?B:1’bz; assign Z=(SEL==2’b11)?C:1’bz; endmodule Inferred as a multiplexer. But we wanted drivers!
  • 21. Dept of CSE, IIT Kharagpur 21 Numbers • Format of integer constants: Width’ radix value; • Verilog keeps track of the sign if it is assigned to an integer or assigned to a parameter. • Once verilog looses sign the designer has to be careful.
  • 22. Dept of CSE, IIT Kharagpur 22 Hierarchy • Module interface provides the means to interconnect two verilog modules. • Note that a reg cannot be an input/ inout port. • A module may instantiate other modules.
  • 23. Dept of CSE, IIT Kharagpur 23 Instantiating a Module • Instances of module mymod(y, a, b); • Lets instantiate the module, mymod mm1(y1, a1, b1); // Connect-by-position mymod mm2(.a(a2), .b(b2), .y(c2)); // Connect-by-name
  • 24. Dept of CSE, IIT Kharagpur 24 Sequential Blocks • Sequential block is a group of statements between a begin and an end. • A sequential block, in an always statement executes repeatedly. • Inside an initial statement, it operates only once.
  • 25. Dept of CSE, IIT Kharagpur 25 Procedures • A Procedure is an always or initial statement or a function. • Procedural statements within a sequential block executes concurrently with other procedures.
  • 26. Dept of CSE, IIT Kharagpur 26 Assignments • module assignments // continuous assignments always // beginning of a procedure begin //beginning of a sequential block //….Procedural assignments end endmodule • A Continuous assignment assigns a value to a wire like a real gate driving a wire. module holiday_1(sat, sun, weekend); input sat, sun; output weekend; // Continuous assignment assign weekend = sat | sun; endmodule module holiday_2(sat, sun, weekend); input sat, sun; output weekend; reg weekend; always @(sat or sun) weekend = sat | sun; // Procedural endmodule // assignment
  • 27. Dept of CSE, IIT Kharagpur 27 Blocking and Nonblocking Assignments • Blocking procedural assignments must be executed before the procedural flow can pass to the subsequent statement. • A Non-blocking procedural assignment is scheduled to occur without blocking the procedural flow to subsequent statements.
  • 28. Dept of CSE, IIT Kharagpur 28 Nonblocking Statements are odd! a = 1; b = a; c = b; Blocking assignment: a = b = c = 1 a <= 1; b <= a; c <= b; Nonblocking assignment: a = 1 b = old value of a c = old value of b
  • 29. Dept of CSE, IIT Kharagpur 29 Nonblocking Looks Like Latches • RHS of nonblocking taken from latches • RHS of blocking taken from wires a = 1; b = a; c = b; a <= 1; b <= a; c <= b; 1 a b c “ ” a b c 1 “ ”
  • 30. Dept of CSE, IIT Kharagpur 30 Examples • Blocking: always @(A1 or B1 or C1 or M1) begin M1=#3(A1 & B1); Y1= #1(M1|C1); end • Non-Blocking: always @(A2 or B2 or C2 or M2) begin M2<=#3(A2 & B2); Y2<=#1(M1 | C1); end Statement executed at time t+3 causing Y1 to be assigned at time t+4 Statement executed at time t causing Y2 to be assigned at time t+1. Uses old values. Statement executed at time t causing M1 to be assigned at t+3 Statement executed at time t causing M2 to be assigned at t+3
  • 31. Dept of CSE, IIT Kharagpur 31 Order dependency of Concurrent Statements • Order of concurrent statements does not affect how a synthesizer synthesizes a circuit. • It can affect simulation results.
  • 32. Dept of CSE, IIT Kharagpur 32 Order dependency of Concurrent Statements • always @(posedge clock) begin: CONCURR_1 Y1<=A; end • always @(posedge clock) begin: CONCURR_2 if(Y1) Y2=B; else Y2=0; end B A clk Y1 Y2 Can you figure out the possible mismatch of simulation with synthesis results?
  • 33. Dept of CSE, IIT Kharagpur 33 Explanation of the mismatch • The actual circuit is a concurrent process. • The first and second flip flop are operating parallel. • However if the simulator simulates CONCURR_1 block before CONCURR_2, we have an error. Why? • So, how do we solve the problem?
  • 34. Dept of CSE, IIT Kharagpur 34 Solution • always @(posedge clock) begin Y1<=A; if(Y1==1) Y2<=B; else Y2<=0; end With non-blocking assignments the order of the assignments is immaterial…
  • 35. Dept of CSE, IIT Kharagpur 35 Parameterized Design • module vector_and(z, a, b); parameter cardinality = 1; input [cardinality-1:0] a, b; output [cardinality-1:0] z; wire [cardinality-1:0] z = a & b; endmodule • We override these parameters when we instantiate the module as: module Four_and_gates(OutBus, InBusA, InBusB); input [3:0] InBusA, InBusB; output[3:0] OutBus; Vector_And #(4) My_And(OutBus, InBusA, InBusB); endmodule
  • 36. Dept of CSE, IIT Kharagpur 36 Functions (cont’d) • Function Declaration and Invocation – Declaration syntax: function <range_or_type> <func_name>; <input declaration(s)> <variable_declaration(s)> begin // if more than one statement needed <statements> end // if begin used endfunction
  • 37. Dept of CSE, IIT Kharagpur 37 Function Examples Controllable Shifter module shifter; `define LEFT_SHIFT 1'b0 `define RIGHT_SHIFT 1'b1 reg [31:0] addr, left_addr, right_addr; reg control; initial begin … end always @(addr)begin left_addr =shift(addr, `LEFT_SHIFT); right_addr =shift(addr,`RIGHT_SHIFT); end function [31:0]shift; input [31:0] address; input control; begin shift = (control==`LEFT_SHIFT) ?(address<<1) : (address>>1); end endfunction endmodule
  • 38. Dept of CSE, IIT Kharagpur 38 How Are Simulators Used? • Testbench generates stimulus and checks response • Coupled to model of the system • Pair is run simultaneously Testbench System Model Stimulus Response Result checker
  • 39. Dept of CSE, IIT Kharagpur 39 Looking back at our multiplexer • “Dataflow” Descriptions of Logic //Dataflow description of mux module mux2 (in0, in1, select, out); input in0,in1,select; output out; assign out = (~select & in0) | (select & in1); endmodule // mux2 Alternative: assign out = select ? in1 : in0;
  • 40. Dept of CSE, IIT Kharagpur 40 TestBench of the Multiplexer • Testbench module testmux; reg a, b, s; wire f; reg expected; mux2 myMux (.select(s), .in0(a), .in1(b), .out(f)); initial begin s=0; a=0; b=1; expected=0; #10 a=1; b=0; expected=1; #10 s=1; a=0; b=1; expected=1; end initial $monitor( "select=%b in0=%b in1=%b out=%b, expected out=%b time=%d", s, a, b, f, expected, $time); endmodule // testmux
  • 41. Dept of CSE, IIT Kharagpur 41 A Car Speed Controller SLOW FAST STOP MEDIUM Accelerate Brake Accelerate Brake Accelerate Brake ~Brake (~Accelerate) & (~ Brake) (~Accelerate) & (~ Brake)
  • 42. Dept of CSE, IIT Kharagpur 42 Car Controller Coding module fsm_car_speed_1(clk, keys, brake, accelerate, speed); input clk, keys, brake, accelerate; output [1:0] speed; reg [1:0] speed; parameter stop = 2'b00, slow = 2'b01, mdium = 2'b10, fast = 2'b11;
  • 43. Dept of CSE, IIT Kharagpur 43 Car Controller (contd.) always @(posedge clk or negedge keys) begin if(!keys) speed = stop; else if(accelerate) case(speed) stop: speed = slow; slow: speed = mdium; mdium: speed = fast; fast: speed = fast; endcase else if(brake) case(speed) stop: speed = stop; slow: speed = stop; mdium: speed = slow; fast: speed = mdium; endcase else speed = speed; end endmodule
  • 44. Dept of CSE, IIT Kharagpur 44 A Better Way! • We keep a separate control part where the next state is calculated. • The other part generates the output from the next state. • We follow this architecture in the coding of any finite state machines, like ALU, etc. ( to be discussed later)
  • 45. Dept of CSE, IIT Kharagpur 45 module fsm_car_speed_2(clk, keys, brake, accelerate, speed); input clk, keys, brake, accelerate; output [1:0] speed; reg [1:0] speed; reg [1:0] newspeed; parameter stop = 2'b00, slow = 2'b01, mdium = 2'b10, fast = 2'b11; always @(keys or brake or accelerate or speed) begin case(speed) stop: if(accelerate) newspeed = slow; else newspeed = stop; slow: if(brake) newspeed = stop; else if(accelerate) newspeed = mdium; else newspeed = slow; mdium: if(brake) newspeed = slow; else if(accelerate) newspeed = fast; else newspeed = mdium; fast: if(brake) newspeed = mdium; else newspeed = fast; default: newspeed = stop; endcase end always @(posedge clk or negedge keys) begin if(!keys) speed = stop; else speed = newspeed; end endmodule
  • 46. Dept of CSE, IIT Kharagpur 46 Conclusion : Write codes which can be translated into hardware ! The following cannot be translated into hardware( non - synthesizable): • Initial blocks – Used to set up initial state or describe finite testbench stimuli – Don’t have obvious hardware component • Delays – May be in the Verilog source, but are simply ignored • In short, write codes with a hardware in your mind. In other words do not depend too much upon the tool to decide upon the resultant hardware. • Finally, remember that you are a better designer than the tool.