001 Verilog-Intro
001 Verilog-Intro
• What is Verilog?
– Hardware Description Language(HDL)
• Why use Verilog?
– Because 60% of US companies use it.
Why Verilog?
• Why use an HDL?
– Describe complex designs (millions of gates)
– Input to synthesis tools (synthesizable subset)
– Design exploration with simulation
• Why not use a general purpose language
– Support for structure and instantiation
– Support for describing bit-level behavior
– Support for timing
– Support for concurrency
• Verilog vs. VHDL
– Verilog is relatively simple and close to C
– VHDL is complex and close to Ada
– Verilog has 60% of the world digital design market (larger share in US)
Why Verilog?
Why Verilog?
Verilog HDL and Verilog-XL
• Verilog HDL
– Hardware design language that allows you to
design circuit.
• Verilog-XL
– High speed, event-driven simulator that reads
Verilog HDL and simulates the behavior of
hardware.
Modern Project Methodology
Always Synthesis
inst1
inst2
inst3
ing
p
ap
m
Place and
Route
clb 1
clb 2
Introduction to
Verilog only
• Objectives
– Understand the design methodologies using Verilog
• Target audience
– have basic digital circuits design concept
– knowledge of VHDL for design of digital systems
– Verilog description for logic synthesis
• NOT in the talk
– a full coverage of Verilog
Contents
• Verilog HDL
– structured modeling
– RTL modeling
• Example combinational circuits
– structured description (net-list)
– RTL
• Example sequential circuits
– RTL
• FSM
– combinational circuits
– sequential circuits
Verilog history
• Gateway Design Automation
– Phil Moorby in 1984 and 1985
• Verilog-XL, "XL algorithm", 1986
– a very efficient method for doing gate-level simulation
• Verilog logic synthesizer, Synopsys, 1988
– the top-down design methodology is feasible
• Cadence Design Systems acquired Gateway
– December 1989
– a proprietary HDL
Verilog history
• Open Verilog International (OVI), 1991
– Language Reference Manual (LRM)
– making the language specification as vendor-
independent as possible.
• The IEEE 1364 working group, 1994
– to turn the OVI LRM into an IEEE standard.
• Verilog became an IEEE standard
– December, 1995.
Hardware Description Languages
• The functionality of hardware
– concurrency
– timing controls
• The implementation of hardware
– structure
– net-list
• ISP
– C. Gordon Bell and Alan Newell at Carnegie Mellon
University, 1972
– RTL (register transfer level)
Different Levels of Abstraction
• Algorithmic
– the function of the system
• RTL
– the data flow
– the control signals
– the storage element and clock
• Gate
– gate-level net-list
• Switch
– transistor-level net-list
Verilog for Digital System Design
• Structural description
– net-list using primitive gates and switches
– continuous assignment using Verilog operators
• RTL
– functional description
– timing controls and concurrency specification
– procedural blocks (always and initial)
– registers and latches
• C + timing controls + concurrency
Verilog Procedural
Descriptions
Verilog Variables
Tricky Delay
Initial Block
Verilog Usage
Different States
Procedural
Statements
Still a Problem?
Hierarchical structure and
Modules
• Represent the hierarchy of a design
– modules
• the basic building blocks
– ports
• the I/O pins in hardware
• input, output or inout
Event Driven Simulation
• Verilog is really a language for modeling event-driven systems
– Event : change in state
0 t t+1
Event ••• •••
queue
Events
– Simulation starts at t = 0
– Processing events generates new events
– When all events at time t have been processed simulation time advances to t+1
– Simulation stops when there are no more events in the queue
Modeling Structure: Modules
• The module is the basic building block in Verilog
– Modules can be interconnected to describe the structure of your digital system
– Modules start with keyword module and end with keyword endmodule
• •
• •
• •
endmodule endmodule
Modeling Structure: Ports
• Module Ports
– Similar to pins on a chip
– Provide a way to communicate with outside world
– Ports can be input, output or inout
endmodule
Modeling Structure: Instances
• Module instances
– Verilog models consist of a hierarchy of module instances
– In C++ speak: modules are classes and instances are objects
AND3
i0
i1
o
i2
wire temp;
• X: unknown
• Declaring a register
reg [<range>] <reg_name> [<reg_name>*];
• Declaring memory
reg [<range>] <memory_name> [<start_addr> : <end_addr>];
• Examples
reg r; // 1-bit reg variable
wire w1, w2; // 2 1-bit wire variable
reg [7:0] vreg; // 8-bit register
reg [7:0] memory [0:1023]; a 1 KB memory
Ports and Data Types
• Correct data types for ports
Module
net
inout
net
Structural Modeling
• Structural Verilog describes connections of
modules (netlist)
Verilog Simulator
Simulation Result
Sample Design
module fadder ( sum, cout, a, b , ci ); 1-bit full adder sum
// port declaration a
b
output sum, cout; ci
input a, b, ci;
reg sum, cout;
cout
// behavior description
always @( a or b or ci )
begin
sum = a ^ b ^ ci;
Simpler than VHDL
cout = ( a&b ) | ( b&ci ) | ( ci&a);
end Only Syntactical
endmodule Difference
Basic
Instructions
Lexical Conventions in
Verilog
Type of lexical tokens :
•Operators ( * )
•White space
•Comment
•Number ( * )
•String
•Identifier
•Keyword ( * ) Note : * will be discussed
Reg and Parameters
• Reg
– variables used in RTL description
– a wire, a storage device or a temporary variable
– reg : unsigned integer variables of varying bit width
– integer : 32-bit signed integer
– real : signed floating-point
– time : 64-bit unsigned integer
• Parameters
– run-time constants
Special Language Tokens
• $<identifier>: System tasks and functions
– $time
– $stop
– $finish
– $monitor
– $ps_waves
– $gr_waves
– $gr_regs
• #<delay specification>
– used in
• gate instances and procedural statements
• unnecessary in RTL specification
Modeling Structures
• Net-list
– structural description for the top level
• Continuous assignments (combination circuits)
– data flow specification for simple combinational
– Verilog operators
• Procedural blocks (RTL)
– always and initial blocks
• allow timing control and concurrency
– C-like procedure statements
• primitives (=truth table, state transition table)
• function and task (function and subroutine)
A full-adder
• module add (co, s, a, b, c)
input a, b ,c ;
output co, s ;
xor (n1, a, b) ;
xor (s, n1, c) ;
Simpler than VHDL
nand (n2, a, b) ;
nand (n3,n1, c) ; Only Syntactical
nand (co, n3,n2) ; Difference
endmodule
Verilog Primitives
• Basic logic gates only
– and
– or
– not
– buf
– xor
– nand
– nor
– xnor
– bufif1, bufif0
– notif1, notif0
Primitive Pins Are Expandable
• One output and variable number of inputs
nand (y, in1, in2) ;
• Conditional operator
assign z = ({s1,s0} == 2'b00) ? IA :
({s1,s0} == 2'b01) ? IB :
({s1,s0} == 2'b10) ? IC :
({s1,s0} == 2'b11) ? ID :
1'bx ;
‘0’
‘X’
‘1’ ‘Z’
0
Major Data Type Class
Nets
Registers
Parameters
Nets
Net data type represent physical
connections between structural entities.
A net must be driven by a driver, such as
a gate or a continuous assignment.
Verilog automatically propagates new
values onto a net when the drivers change
value.
Registers & Parameters
Registers represent abstract storage
elements.
A register holds its value until a new value is
assigned to it.
Registers are used extensively in behavior
modeling and in applying stimuli.
Parameters are not variables, they are
constants.
Assignments
Assignment : drive values into nets and registers.
Continuous Assignments – Any changes in the
RHS of continuous assignment are evaluated and
the LHS is update.
Example : (1) assign out = ~in;
(2) assign reg_out; = reg_in << shift
Assignments ( cont. )
Blocking procedural assignment.
rega = regb + regc;
Non-blocking procedural assignment.
rega <= regb * regc;
RTL
Modeling
RTL Modeling
• Describe the system at a high level of abstraction
• Specify a set of concurrently active procedural
blocks
– procedural blocks = digital circuits
• Procedural blocks
– initial blocks
• test-fixtures to generate test vectors
• initial conditions
– always blocks
• can be combinational circuits
• can imply latches or flip-flops
Procedural blocks have the following
components
• procedural assignment statements
• timing controls
• high-level programming language constructs
initial c always c
c statement c statement
c … c …
c … c …
c … c …
c … c …
c … c …
RTL Statements
– Procedural and RTL assignments
• reg & integer
• out = a + b ;
– begin . . . end block statements
• group statements
– if. . . else statements
– case statements
– for loops
– while loops
– forever loops
– disable statements
• disable a named block
Combinational Always Blocks
• A complete sensitivity list (inputs)
always @(a or b or c)
f = a&~c | b&c ;
• Simulation results
always @(a or b)
f = a&~c | b&c ;
• Parentheses
always @(a or b or c or d)
z=a+b+c+d; // z = (a+b) + (c+d) ;
Sequential Always Blocks
• Inferred latches (Incomplete branch specifications)
module infer_latch(D, enable, Q);
input D, enable;
output Q;
reg Q;
always @ (D or enable) begin
if (enable)
Q <= D;
end
endmodule
– the Q is not specified in a branch
• a latch like 74373
Combinational Circuit Design
• Outputs are functions of inputs
• Examples
– MUX
– decoder
– priority encoder
– adder
Multiplexor
• Net-list (gate-level)
module mux2_1 (out,a,b,sel) ;
output out ;
input a,b,sel ;
not (sel_, sel) ;
and (a1, a, sel_) ;
and (b1, b, sel) ;
or (out, a1, b1) ;
endmodule
Multiplexor
• Continuous assignment
module mux2_1 (out,a,b,sel) ;
output out ;
input a,b,sel ;
assign out = (a&~sel)|(b&sel) ;
endmodule
• RTL modeling
always @(a or b or sel)
if(sel)
out = b;
else
out = a;
Multiplexor
• 4-to-1 multiplexor
module mux4_1 (out, in0, in1, in2, in3, sel) ;
output out ;
input in0,in1,in2,in3 ;
input [1:0] sel ;
assign out = (sel == 2'b00) ? in0 :
(sel == 2'b01) ? in1 :
(sel == 2'b10) ? in2 :
(sel == 2'b11) ? in3 :
1'bx ;
endmodule
Multiplexor
module mux4_1 (out, in, sel) ;
output out ;
input [3:0] in ;
input [1:0] sel ;
reg out ;
always @(sel or in) begin
case(sel)
2’d0: out = in[0] ;
2’d1: out = in[1] ; out = in[sel] ;
2’d2: out = in[2] ;
2’d3: out = in[3] ;
default: 1’bx ;
endcase
end
endmodule
Decoder
• 3-to 8 decoder with an
case(sel)
enable control 3'b000 : o = 8'b1111_1110 ;
module decoder(o,enb_,sel) ; 3'b001 : o = 8'b1111_1101 ;
output [7:0] o ; 3'b010 : o = 8'b1111_1011 ;
3'b011 : o = 8'b1111_0111 ;
input enb_ ;
3'b100 : o = 8'b1110_1111 ;
input [2:0] sel ; 3'b101 : o = 8'b1101_1111 ;
reg [7:0] o ; 3'b110 : o = 8'b1011_1111 ;
always @ (enb_ or sel) 3'b111 : o = 8'b0111_1111 ;
if(enb_) default : o = 8'bx ;
endcase
o = 8'b1111_1111 ;
endmodule
else
Priority Encoder
always @ (d0 or d1 or d2 or d3)
if (d3 == 1)
{x,y,v} = 3’b111 ;
else if (d2 == 1)
{x,y,v} = 3’b101 ;
else if (d1 == 1)
{x,y,v} = 3’b011 ;
else if (d0 == 1)
{x,y,v} = 3’b001 ;
else
{x,y,v} = 3’bxx0 ;
Parity Checker
module parity_chk(data, parity); always @ (data)
input [0:7] data; begin: check_parity
output parity; reg partial;
integer n;
reg parity;
partial = data[0];
for ( n = 0; n <= 7; n = n + 1)
begin
partial = partial ^ data[n];
end
parity <= partial;
end
endmodule
Adder
• RTL modeling
module adder(c,s,a,b) ;
output c ;
output [7:0] s ;
input [7:0] a,b ;
Wire [8:0] temp;
assign {c,s} = a + b ;
endmodule
• Logic synthesis
– CLA adder for speed optimization
– ripple adder for area optimization
Tri-State
• The value z assign out = (sela)? a: 1’bz ;
always @ (sela or a)
if (sela)
out = a ;
else
out = 1’bz ;
• Another block
always @(selb or b)
if(selb)
out =b ;
else
out = 1’bz ;
Registers (Flip-flops) are implied
– a feedback path
– the state of the sequential circuits
– the state transition
synchronous circuits
asynchronous circuits
Finite State Machine
• Moore model
inputs next current
comb. state memory state comb. outputs
circuit elements circuit
• Mealy model
inputs next current
comb. state memory state comb. outputs
circuit elements circuit
Examples
– D flip-flop
– D latch
– register
– shifter
– counter
– pipeline
– FSM
Flip-Flop
• Synchronous clear
module d_ff (q,d,clk,clr_) ;
output q ;
input d,clk,clr_ ;
reg q ;
always @ (posedge clk)
if (~clr_) q=0;
else q=d;
endmodule
• Asynchronous clear
always @ (posedge clk or negedge clr_)
if (~clr_) q=0;
else q=d;
Register
module register (q,d,clk,clr_, set_) ;
output [7:0] q ;
input [7:0] d ;
input clk,clr_, set_ ;
reg [7:0] q ;
always @ (posedge clk or negedge clr_ or negedge set_)
if (~clr_)
q=0;
else if (~set_)
q = 8’b1111_1111 ;
else
q=d;
endmodule
D Latches
• D latch
always @ (enable or data)
if (enable)
q = data ;
• D latch with gated asynchronous data
always @ (enable or data or gate)
if (enable)
q = data & gate ;
D Latches
• D latch with gated ‘enable’
always @ (enable or d or gate)
if (enable & gate)
q=d;
• An example
a
n-sum
Dff sum
b
p out
Dff
c Dff d_c
C
HL
FL
Highway
Highway
FL
HL C
Traffic Light Controller Farmroad
Specifications
Traffic Light Controller
? Tabulation of Inputs and Outputs:
Input Signal Description
reset place FSM in initial state
C detect vehicle on farmroad
TS short time interval expired
TL long time interval expired
TS
S3: FY
TS/ST
TL + C/ST
S2
TL • C
Verilog Description
module traffic_light(HG, HY, HR, FG, FY, FR,ST_o,
tl, ts, clk, reset, c) ;
output HG, HY, HR, FG, FY, FR, ST_o;
input tl, ts, clk, reset, c ;
reg ST_o, ST ;
reg[0:1] state, next_state ;
parameter EVEN= 0, ODD=1 ;
parameter S0= 2'b00, S1=2'b01, S2=2'b10, S3=2'b11;
assign HG = (state == S0) ;
assign HY = (state == S1) ;
assign HR = ((state == S2)||(state == S3)) ;
assign FG = (state == S2) ;
assign FY = (state == S3) ;
assign FR = ((state == S0)||(state == S1)) ;
// flip-flops
always@ (posedge clk or posedge reset)
if(reset) // an asynchronous reset
begin
state = S0 ;
ST_o = 0 ;
end
else
begin
state = next_state ;
ST_o = ST ;
end
always@ (state or c or tl or ts)
case(state) // state transition
S0:
if(tl & c) TL + C
Reset
begin S0
next_state = S1 ; TL•C/ST TS/ST
TS
ST = 1 ;
S1 S3
end
TS/ST TS
else TL + C/ST
S2
begin
TL • C
next_state = S0 ;
ST = 0 ;
end
S1:
if (ts) begin
next_state = S2 ;
ST = 1 ;
end TL + C
else begin Reset
next_state = S1 ; S0
ST = 0 ; TL•C/ST TS/ST
end TS
S2: S1 S3
if(tl | !c) begin
TS/ST TS
next_state = S3 ;
TL + C/ST
ST = 1 ; S2
end
else begin TL • C
next_state = S2 ;
ST = 0 ;
end
S3:
if(ts)
begin
next_state = S0 ; TL + C
Reset
ST = 1 ; S0
end TL•C/ST TS/ST
TS
else
S1 S3
begin
TS/ST TS
next_state = S3 ; TL + C/ST
S2
ST = 0 ;
TL • C
end
endcase
endmodule
Efficient Modeling
Techniques
• Separate combinational and sequential
circuits
– always know your target circuits
• Separate structured circuits and random logic
– structured: data path, XORs, MUXs
– random logic: control logic, decoder, encoder
• Use parentheses control complex structure
• .....
VERILOG Coding Styles
Synthesizable
Behavioral
Register Transfer Level (RTL)
Structural
Behavioral
modeling
Conditional Instructions
(expression)?(true):(false) acc=(ir[7:0]==4’b0011) ?
0:255;
Loops
Module half_adder(x, y, s, c)
input x, y;
output s, c;
Continually drive
assign s = x ^ y; wire variables
assign c = x & y;
endmodule
Used to model
combinational logic
Module adder_4(a, b, ci, s, co)
or make
input [3:0] a, b;
input ci;
connections
output [3:0]s;
output co; between wires
assign {co, s} = a + b + ci;
endmodule
Behavior: Initial and Always
• Multiple statements per block
Procedural assignments
Timing control
• Initial blocks execute once
• at t = 0
• Always blocks execute continuously
• at t = 0 and repeatedly thereafter
initial always
begin begin
• •
• •
• •
end end
Behavior: Procedural assignments
• Blocking assignment =
Regular assignment inside procedural block
Assignment takes place immediately
LHS must be a register
always
begin
A = B; A = B, B= B
B = A;
end
always
begin
A <= B; swap A and B
B <= A;
end
Behavior:Timing Control
• Delay #
Used to delay statement by specified amount of simulation time
always
begin
#10 clk = 1;
#10 clk = 0;
end
• Event Control @
Delay execution until event occurs
Event may be single signal/expression change
Multiple events linked by or
case(opcode)
6’b001010: read_mem = 1;
6’b100011: alu_add = 1;
default:
begin
$display (“Unknown opcode %h”, opcode);
end
Could also use casez (treats z as don’t cares ) and casex ( treats z and x as don’t cares)
endcase
Behavior: Loop Statements
• Repeat
i = 0;
repeat (10)
begin
i = i + 1;
$display( “i = %d”, i);
end
• While
i = 0;
while (i < 10)
begin
i = i + 1;
$display( “i = %d”, i);
• For end
– If you mix sequential and combinational logic within the same always block
use nonblocking assignments
– Don’t mix blocking and nonblocking assignments in the same always block
– Don’t make assignments to same variable from more than one always block
WRONG RIGHT
d q q q1
clk
183 Verilog Synthesizable
coding rules
- Always keep in mind what sort of implementation your design could map to. If
you don’t know, chances are synthesis doesn’t either.
- The only allowed storage is instantiated dff
- No always @ posedge statements
- No case statements without default case
- No if statements without an else case
- If you assign to a net in one case it must be assigned to in all cases (no implicit storage)
- No loops
- No initial blocks
- Limited operators
- + and – are the only arithmetic operators allowed
- Try to avoid relational operators (>, ==) in favor of simpler logic
- Use assign statements when possible
System and Compiler
• System tasks
– $time - returns the current simulation time
– $display - similar to printf in C
– $stop - stops simultion
– $finish - ends simulation
– $readmemh - load memory array from text file in hex format
– Many more …
• Compiler directives
– A compiler directive is preceded by `
– `define - defines a compiler time constant or macro
– `ifdef, `else, `endif - conditional compilation
– `include - text inclusion
Verilog id le
Example: ASM 1
in p u t
0
Chart s1
0 1
in p u t
s2
0 1
in p u t
1011
1 0
in p u t
s3
o u tp u t
1 0
in p u t
Verilog Example (cont)
module sequence (dataIn, found, clock, reset);
//DataInternal Variables
reg [3:0] state;
reg [3:0] next_state;
wire found_comb;
//State Declarations
parameter idle = 4'b0001;
parameter s1 = 4'b0010;
parameter s2 = 4'b0100;
parameter s3 = 4'b1000;
//Next State Logic
always @(state or dataIn)
case (state)
idle:
if (dataIn)
next_state = s1;
else
Verilog s1:
next_state = idle;
if (dataIn)
Example next_state = s1;
else
next_state = s2;
s2:
if (dataIn)
next_state = s3;
else
next_state = idle;
s3:
if (dataIn)
next_state = s1;
else
next_state = s2;
default:
next_state = idle;
endcase // case(state)
//State Transition
always @(posedge clock)
if (reset == 1)
state <= idle;
else
Verilog
state <= next_state;
//Output Logic
Example assign found_comb = (state[3] & dataIn);
endmodule // sequence
Verilog Example :
Simulation
Verilog Example: Synthesis
• Top Level
multiplicand
Counter P Register B
Cout
Adder
multiplier
0 C Shift register A Shift register Q
product
OUT
Behavioral modeling
Learning behavioral modeling by exploring some examples
module DFF ( Din, Dout, Clock, Reset );
output Dout;
input Din, Clock, Reset;
reg Dout;
Din Dout
always @( negedge Reset or posedge Clock )
begin DFF
if ( !Reset ) Clock
Dout <= 1’b0;
else
Dout <= Din; Reset
end
endmodule
Behavioral modeling ( cont. )
• netlist:
not(n1, Illegal)
and(n2, n1, VL1)
...
Synthesizable Verilog #3 FSMs
parameter IDLE=0, SET_HOURS=1, SET_MINUTES=2;
reg [1:0] CURRENT_STATE, NEXT_STATE; // State
reg HOURS, MINS; // Outputs
parameter
parameter NN == 8;
8;
input
input [N-1:0]
[N-1:0] Q;Q;
output
output [N-1:0]
[N-1:0] D;D;
input
input load,
load, Clk;
Clk;
always
always @(posedge
@(posedge clk)
clk)
if
if (load)
(load)
QQ == #`dh
#`dh D;
D; RegLd
RegLd reg0(q0,
reg0(q0, d0,
d0, l,
l, clk);
clk);
endmodule
endmodule RegLd
RegLd #16
#16 reg1(q1,
reg1(q1, d1,
d1, l,
l, clk);
clk);
RegLd
RegLd reg2(q2,
reg2(q2, d2,
d2, l,
l, clk);
clk);
defparam reg2.N = 4;
defparam reg2.N = 4;
Sensitivity lists
always
always @(posedge
@(posedge clk
clk or
or negedge
negedge rst_)
rst_)
...
...
always
always @(a
@(a oror bb or
or c)
c)
if (opcode == 32’h52A0234E)
if (opcode == 32’h52A0234E)
aa == bb ^^ (~c);
(~c);
always
always @(posedge
@(posedge aa or
or posedge
posedge b)
b)
...
... !
Behavioral (1)
initial
initial begin
• simulation //
begin
// reset
reset everything
everything
end
– end
if
if (branch)
(branch)
dst
dst == #2
#2 br_addr;
br_addr;
end
end
Behavioral (2)
integer
integer sum,
sum, i;
i;
integer
integer opcodes
opcodes [31:0];
[31:0];
real
real average;
average;
initial
initial
for
for (i=0;
(i=0; i<32;
i<32; i=i+1)
i=i+1)
opcodes[i]
opcodes[i] == 0;
0;
always
always @(posedge
@(posedge clk)
clk) begin
begin
sum
sum == sum
sum ++ 1;
1;
! average
average == average
average ++ (c
(c // sum);
sum);
opcodes[d]
opcodes[d] == sum;
sum;
$display(“sum:
$display(“sum: %d, %d, avg:
avg: %f”,
%f”,
sum, average);
sum, average);
end
end
Verification of Design
Differences
between Tasks
and Functions
Tasks versus Functions in
Verilog
• Procedures/Subroutines/Functions in SW
programming languages
– The same functionality, in different places
• Verilog equivalence:
– Tasks and Functions
– Used in behavioral modeling
– Part of design hierarchy Hierarchical name
Differences between...
• Functions • Tasks
– Can enable (call) just – Can enable other tasks and
another function (not task) functions
– Execute in 0 simulation time – May execute in non-zero
– No timing control statements simulation time
allowed – May contain any timing
– At lease one input control statements
– Return only a single value – May have arbitrary input,
output, or inouts
– Do not return any value
Differences between… (cont’d)
• Both
– are defined in a module
– are local to the module
– can have local variables (registers, but not nets) and events
– contain only behavioral statements
– do not contain initial or always statements
– are called from initial or always statements or other tasks or
functions
Differences between… (cont’d)
• Tasks can be used for common Verilog code
• Function are used when the common code
– is purely combinational
– executes in 0 simulation time
– provides exactly one output
• Functions are typically used for conversions and
commonly used calculations
Tasks
Tasks
task <task_name>;
<I/O declarations>
<variable and event declarations>
begin // if more than one statement needed
<statement(s)>
end // if begin used!
endtask
Tasks (cont’d)
endmodule
Functions
Functions
• Semantics
– much like function in Pascal
– An internal implicit reg is declared inside the function
with the same name
– The return value is specified by setting that implicit reg
– <range_or_type> defines width and type of the implicit
reg
• type can be integer or real
• default bit width is 1
Function Examples
Parity Generator
always @(addr)begin
left_addr =shift(addr, `LEFT_SHIFT);
right_addr =shift(addr,`RIGHT_SHIFT);
end
Tasks and Functions Summary
Spring 2004
Figure 5-1
Block Diagram of an Iterative Circuit
5-2 Binary Adders
• Arithmetic Circuit
– a combinational circuit for arithmetic operations
Figure 5-4
Logic Diagram
of Full Adder
5-2 Binary Adders
Figure 5-6
Development of
a Carry Lookahead
Adder
5-3 Binary Subtraction
10 end borrow
– Subtraction 63
-72
--------
91 100-91
-9
Figure 5-7
Block Diagram of Binary
Adder-Subtracter
5-3 Binary Subtraction
• Complements
– 2 types:
• radix complement: r's complement
• diminished radix complement: (r-1)'s complement
– 2's & 1's for binary numbers
– 10's & 9's for decimal numbers
2) leaving all least significant 0's and the first 1 unchanged then
replacing 1's with 0's, 0's with 1's
• 2's comp of 1101100 ==> 0010100
• 2's complement of N is 2n – N
& the complement of the complement is 2n - (2n-N) = N
5-3 Binary Subtraction
• Subtraction with Ex 5-2) X=1010100 Y=1000011
Complements
– (M - N)
1) add 2's comp of the
subtrahend N to the minuend
M
M + (2n-N) = M - N + 2n
2) if M > N, the end cary is
discarded
3) if M < N, the result is 2n -
(N - M)
take the 2's complement of
the sum & place a minus sign
– avoid overflow problem to
accommodate the sum
5-4 Binary Adder-Subtractors
Table 5-3
Signed Binary Numbers
5-4 Binary Adder-Subtractors
Ex 5-4)Binary
– Signed Signed Binary Adding
Addition andUsing 2’s Complement
Subtraction
• S = A + B + C0 S = A + 11…1 + C0
= A - 1 + C0 (in 2’s complement)
if C0 = 0, S is a decrement of A
Incrementing
adding a fixed value(ex, 1) to an arithmetic variable
N-bit incrementer
S = A + 1 from S = A + B + C0 by setting B to 00..01 and
C0 to 0
5-6 Other Arithmetic Functions
• Incrementing( 중간 0 위치 내림 , X because of 3-bit incrementer)