15CS202 Unitv
15CS202 Unitv
15CS202 Unitv
Language - Introduction
• HDL is a language that describes the hardware of digital
systems in a textual form.
• It resembles a programming language, but is specifically
oriented to describing hardware structures and behaviors.
• The main difference with the traditional programming
languages is HDL’s representation of extensive parallel
operations whereas traditional ones represents mostly serial
operations.
• The most common use of a HDL is to provide an alternative to
schematics.
HDL – Introduction (2)
e;
and
g1(e,A,B);
not g2(y,C);
or
Verilog – Gate Delays
• Sometimes it is necessary to specify the amount of delay
from the input to the output of gates.
• In Verilog, the delay is specified in terms of time units and
the symbol #.
• The association of a time unit with physical time is made
using timescale compiler directive.
• Compiler directive starts with the “backquote (`)” symbol.
`timescale 1ns/100ps
• The first number specifies the unit of measurement
for time delays.
• The second number specifies the precision for which the
delays are rounded off, in this case to 0.1ns.
Verilog – Module (4)
//Description of circuit with delay
module circuit_with_delay (A,B,C,x,y);
input
A,B,C;
output x,y;
wire e;
and #(30)
g1(e,A,B);
or #(20)
g3(x,e,y);
not #(10)
g2(y,C);
endmodule
Verilog – Module (5)
• In order to simulate a circuit with HDL, it is necessary to apply inputs to
the circuit for the simulator to generate an output response.
• An HDL description that provides the stimulus to a design is called a test
bench.
• The initial statement specifies inputs between the keyword begin and
end.
• Initially ABC=000 (A,B and C are each set to 1’b0 (one binary digit with a
value 0).
• $finish is a system task.
Verilog – Module (6)
module circuit_with_delay //Stimulus for simple circuit
(A,B,C,x,y); module stimcrct;
input A,B,C; reg A,B,C;
output x,y; wire x,y;
wire e; circuit_with_delay cwd(A,B,C,x,y);
and #(30) initial
g1(e,A,B); begin
or #(20)
A = 1'b0; B = 1'b0; C = 1'b0;
g3(x,e,y);
#100
not #(10) g2(y,C);
endmodule A = 1'b1; B = 1'b1; C = 1'b1;
#100 $finish;
end
endmodule
Verilog – Module (6)
Bitwise operators
– Bitwise NOT : ~
– Bitwise AND: &
– Bitwise OR: |
– Bitwise XOR:
^ ~^ or ^~
– Bitwise XNOR:
Verilog – Module (8)
Boolean Expressions:
• These are specified in Verilog HDL with a
continuous assignment statement consisting of the
keyword assign followed by a Boolean Expression.
• The earlier circuit can be specified using the
statement:
assign x = (A&B)|~C)
E.g. x = A + BC +
B’D y = B’C +
BC’D’
Verilog – Module (9)
UDP features ….
• UDP’s do not use the keyword module. Instead they are declared with
the keyword primitive.
• There can be only one output and it must be listed first in the port
list and declared with an output keyword.
• There can be any number of inputs. The order in which they are listed in
the input declaration must conform to the order in which they are given
values in the table that follows.
• The truth table is enclosed within the keywords table and endtable.
• The values of the inputs are listed with a colon (:). The output is always
the last entry in a row followed by a semicolon (;).
• It ends with the keyword endprimitive.
Verilog – Module (13)
//User defined primitive(UDP)
primitive crctp (x,A,B,C);
output x;
input A,B,C;
//Truth table for x(A,B,C) = Minterms (0,2,4,6,7)
table
// A B C : x (Note that this is only a
comment)
0 0 0 : 1;
0 0 1 : 0;
0 1 0 : 1;
0 1 1 : 0;
1 0 0 : 1; // Instantiate primitive
1 0 1 : 0; module declare_crctp;
1 1 0 : 1;
1 1 1 : 1; reg x,y,z;
endtable
endprimitive wire w;
crctp (w,x,y,z);
endmodule
Verilog – Module (14)
• A module can be described in any one (or a
combination) of the following modeling
techniques.
– Gate-level modeling using instantiation of primitive
gates and user defined modules.
• This describes the circuit by specifying the gates and how
they
are connected with each other.
– Dataflow modeling using continuous assignment
statements with the keyword assign.
• This is mostly used for describing combinational circuits.
– Behavioral modeling using procedural assignment
statements with keyword always.
• This is used to describe digital systems at a higher level of
abstraction.
Gate-Level Modeling
//Intermediate
carries
fulladder FA0 (S[0],C1,A[0],B[0],C0),
FA1 (S[1],C2,A[1],B[1],C1),
FA2
//Instantiate (S[2],C3,A[2],B[2],C2),
FA3
the full adder (S[3],C4,A[3],B[3],C3);
endmodule
2 to 4 Decoder
2 to 4 Decoder
• The procedural assignment statements inside the always block are executed
every time there is a change in any of the variable listed after the @ symbol.
(Note that there is no “;” at the end of always statement)
4-to-1 line
multiplexer
Behavioral Modeling (4)
//Behavioral description of 4-to-1 line mux
initial begin
A=0; B=0; #10 A=1; #20 A=0; B=1;
end
• The block is enclosed between begin and end.
At time=0, A and B are set to 0. 10 time units
later, A is changed to 1. 20 time units later (at
t=30) a is changed to 0 and B to 1.
Writing a Test Bench (2)
The stimulus model generates inputs for the design module by declaring
identifiers TA and TB as reg data type, and checks the output of the design
unit with the wire identifier TC. The local identifiers are then used to
instantiate the design module under test.
Writing a Test-Bench (5)
• The response to the stimulus generated by the initial and always blocks will
appear at the output of the simulator as timing diagrams.
• It is also possible to display numerical outputs using Verilog system tasks.
– $display – display one-time value of variables or strings with end-of-line
return,
– $write – same $display but without going to next line.
– $monitor – display variables whenever a value changes during
simulation run.
– $time – displays simulation time
– $finish – terminates the simulation
• The syntax for $display,$write and $monitor is of the form
Task-name (format-specification, argument list);
E.g. $display(%d %b %b, C,A,B);
$display(“time = %0d A = %b B=%b”,$time,A,B);
Writing a Test-Bench (6)
//Stimulus for mux2x1_df
module testmux;
reg TA,TB,TS; //inputs for mux
wire Y; //output from mux
mux2x1_df mx (TA,TB,TS,Y); // instantiate mux
initial begin
$monitor(”select=%b A=%b B=%b OUT=%b",TS,TA,TB,Y);
TS = 1; TA = 0; TB = 1;
#10 TA = 1; TB = 0;
#10 TS = 0;
#10 TA = 0; TB = 1;
end
endmodule
Writing a Test-Bench (7)
initial begin
clock = 1’b0;
repeat (30);
#10 clock =
~clock;
end
initial begin
clock = 1’b0;
#300
$finish;
end
always #10
clock = ~clock
Behavioral Modeling in SSD (3)
• The always statement can be controlled by delays that
wait for a certain time or by certain conditions to become
true or by events to occur.
• This type of statement is of the form:
always @ (event control expression)
Procedural assignment statements
• The event control expression specifies the condition that
must occur to activate the execution of the procedural
assignment statements.
• The variables in the left-hand side of the procedural
statements must be of the reg data type and must be
declared as such.
Behavioral Modeling in SSD (4)
• The statements within the block, after the event control
expression, execute sequentially and the execution
suspends after the last statement has executed.
• Then the always statement waits again for an event to
occur.
• Two kind of events:
– Level sensitive (E.g. in combinational circuits and in latches)
always @(A or B or Reset) will cause the execution of the
procedural statements in the always block if changes occur in A
or B or Reset.
– Edge-triggered (In synchronous sequential circuits, changes in
flip-
flops must occur only in response to a transition of a clock pulse.
always @(posedge clock or negedge reset)will cause
the execution of the procedural statements only if the clock goes
through a positive transition or if the reset goes through a
negative transition.
Behavioral Modeling in SSD (5)
• A procedural assignment is an assignment within an initial or
always statement.
• There are two kinds of procedural assignments: blocking and
non-blocking
– Blocking assignments (executed sequentially in the order they are
listed in a sequential block)
• B=A
• C=B+1
– Non-blocking assignments (evaluate the expressions on the right
hand side, but do not make the assignment to the left hand side until
all expressions are evaluated.
• B <= A
• C <= B + 1
Flip-Flops and Latches
• The D-latch is transparent and responds to a change in
data input with a change in output as long as control input
is enabled.
• It has two inputs, D and control, and one output Q. Since
Q is evaluated in a procedural statement it must be
declared as reg type.
• Latches respond to input signals so the two inputs are
listed without edge qualifiers in the event control
expression following the @ symbol in the always
statement.
• There is one blocking procedural assignment statement
and it specifies the transfer of input D to output Q if
control is true.
Flip-Flops and Latches
module D_latch(Q,D,control);
output Q;
input D,control;
reg Q;
always @(control or D)
if(control) Q = D; //Same as: if(control=1)
endmodule
Flip-Flops and Latches
always@(posedge CLK
or posedge RST)
if (RST) Q<=0;
else Q<=D;
endmodule
Sequential Circuit
Sequential Circuit (2)
Identify the
sequence 1101,
regardless of
where it occurs in
a longer sequence.
Sequence Recognizer (2)
Sequence Recognizer
module seq_recognizer(CLK,RST,X,Z);
input CLK,RST,X;
output Z;
reg [1:0]state, next_state;
parameter A=2’b00,B=2’b01,C=2’b10,D=2’b11;
reg Z;
always@(posedge CLK or posedge RST) begin
if(RST==1) state <= A;
else state <= next_state;
end
always@(X or state) begin
case(state)
A:if(X)next_state <= B; else next_state <= A;
B:if(X)next_state <= C; else next_state <= A;
C:if(X)next_state <= C; else next_state <= D;
D:if(X)next_state <= B; else next_state <= A;
endcase
end
always@(X or state) begin
case(state)
A:Z<=0;
B:Z<=0;
C:Z<=0;
D:Z<=X?1:0;
endcase
end
endmodule
HDL for Registers and Counters
4 bit Binary
Counter with
Parallel Load
HDL for Registers and Counters (8)
//Binary counter with parallel load
module counter (Count,Load,IN,CLK,Clr,A,CO);
input Count,Load,CLK,Clr;
input [3:0] IN; //Data input
output CO; //Output carry
output [3:0] A; //Data output
reg [3:0] A;
assign CO = Count & ~Load & (A == 4'b1111);
always @(posedge CLK or negedge Clr)
if (~Clr) A = 4'b0000;
else if (Load) A = IN;
else if (Count) A = A + 1'b1;
else A = A; // no change, default condition
endmodule
HDL for Registers and Counters (9)
4-bit Binary
Ripple
Counter
HDL for Registers and Counters (10)
//Ripple counter
module ripplecounter(A0,A1,A2,A3,Count,Reset);
output A0,A1,A2,A3;
input Count,Reset;
//Instantiate complementing flip-flop
CF F0 (A0,Count,Reset);
CF F1 (A1,A0,Reset); //Complementing flip-flop with delay
CF F2 //Input to D flip-flop = Q'
(A2,A1,Reset); CF module CF (Q,CLK,Reset);
F3 (A3,A2,Reset); output Q;
endmodule input CLK,Reset;
reg Q;
always@(negedge CLK or posedge Reset)
if(Reset) Q=1'b0;
else Q=#2 (~Q);//Delay of 2 time
units
endmodule
HDL for Registers and Counters (11)
• Sign extending a number: replicate the most significant bit the number
of times needed
– Example: 1000 10102 is the same as 1111 1111 1000 10102
Some arithmetic fundamentals
• Negating a two’s complement number: invert (NOT) all bits
and add 1
– Example: negative of 1000 1010 = 0111 0101 + 1 =
0111 0110 = 118
• Logical operations
– AND, OR, NOR, XOR perform logic function on a bit-by-bit basis
• Example: 1000 1010 AND 1101 0110 = 1000 0010
– Also arithmetic/logical shift left/right
Integer and floating point computation
• Most general-purpose ISAs specify separate integer and floating
point register files
– Operand representation formats differ
– Computation hardware differs
• Result is a split of the execution core into integer and floating
point sections
integer
ALU data
integer memory
register
P instruction file
integer
memory multiplier
C
flt pt
adder
flt pt
register
file
flt pt
multiplier
MIPS ALU requirements
I-Type: op Rs Rt Immed 16
b
(Carry
Out)
Sum
– 1-bit AND, 1-bit OR, 1-bit full add of a and b always calculated
– Operation input (2 bits) determines which of these passes through the MUX and
appears at Result
– CarryIn is from previous bit-slice
– CarryOut goes to next bit-slice
Creating a 32-bit ALU from 1-bit bit-slices
What should we set this to when Operation=10 (add)?
(LSB)
(MSB)
• All bits except bit 0 are set to zero through a new bit-slice input
called Less that goes to a 4th input on the bit-slice MUX
What do we input here?
Less 3
Less 3
Set
MUXes
Carry select addition
• A larger design
generated carry
propagated carry