Академический Документы
Профессиональный Документы
Культура Документы
1
Verilog Symbols
• Verilog only uses standard ASCII symbols.
• No operators or anything else use special
symbols not found on a standard keyboard.
• Easy to enter Verilog code,
code but
– Leads to dual use of symbols
– Assigns
A i diff
different meaning
i to some ffamiliar
ili
symbols
2
Arithmetic Operators
Symbol Operation
* multiply
lti l
/ divide
+ add
- subtract
% modulus
** exponent
3
Multiplication
• Hardware multiplication algorithms have
improved, making using the multiply operator
practical.
• Synthesizers will select a suitable hardware
multiplier
l i li (modified
( difi d Boothh Array, most likely)
lik l )
from a “synthetic library” of scalable components.
• Thus
Th PROD <= M Multiplier
lti li * MMultiplicand
lti li d iis now
an acceptable coding style. “Beating the machine”
is tough.
tough
4
Division
Divide by 2n (fixed divisor, shift right) has
always
y been supported
pp for synthesis.
y This
will always work:
input [7:0] DIVIDEND;
wire [7:0] QUOTIENT;
assign QUOTIENT = DIVIDEND / 4;
/*Resolved power of two: 4 may be replaced by
a parameter but not by a variable.*/
variable. /
5
Integer Division
• Now integer division has some synthesis support.
• Q
Quotient = Dividend / Divisor mayy work in some
tools for any integers.
• Needs to be verified before use.
input [3:0] DIVISOR;
input [7:0] DIVIDEND;
reg [7:0] QUOTIENT;
always @(DIVIDEND or DIVISOR)
6
QUOTIENT = DIVIDEND / DIVISOR;
More on Division
• At best, integer division will work for both
2’s compliment and unsigned numbers.
• At worst, using a variable for divisor will
produce a compile error: entirely tool
dependent.
• Integer and real division can be freely used
in test fixtures and when using Verilog as a
general-purpose programming language.
7
Sign Bit
8
Assume 2’s Compliment
100102 = -14
Shift right one time: 01001 or 11001?
010012 = 9
110012 = -77
Division must extend sign bit.
This behavior always needs to be verified before depending
on it in a circuit.
9
Signed Variables
• Verilog 2001 introduced signed variables
and operators.
p
• No need to manually extend sign bits when
using them.
them
• More on signed variables shortly.
• Do
D need d to write
i code
d to manually
ll extendd
sign bit when using normal variables.
10
Modulus
• Modulus operator
p pproduce the remainder
from the division of two numbers. The result
g of the first operand.
takes the sign p
• Examples:
14 % 3 produces 2
9 %2 produces 1
-5 % 3 produces -2
15 % -2 produces 1
-5 % -3 produces -2 11
Integer Division
//Not guaranteed to work for synthesis!!!
input [7:0] DATA1,
DATA1 DATA2;
output [7:0] QUO, REM;
reg [7:0]
[ 0] QQUO,
O REM;
always @(DATA1 or DATA2) begin
QUO = DATA1 / DATA2;
REM = DATA1 % DATA2;
end 12
Floating Point
• Sorry, no such luck.
• Need to code up implement mantissa
mantissa,
exponent algorithms manually.
• Maybe someday.
someday
13
Addition
• Can be used freely on regs and wires.
• C<<= A + B works fine
fine.
• A suitable adder style will be chosen from
the universe of adder styles.
styles Synthesizer
will look at design constraints and make a
selection.
selection
• Generally two’s compliment is assumed.
14
Subtraction
No tricks here, just like addition.
always @(A or B) C = A – B;
15
Exponential
• Generally unsupported for synthesis, though
could be used in test fixtures.
• Part of Verilog 2001: not supported by all
simulators much less synthesizers.
simulators, synthesizers
• Example: NUM <= BASE ** EXPON;
• Obvious
Ob i hardware
h d limitation—numbers
li i i b get
large, need big registers for results.
16
Unary Arithmetic Operators
• The operators + and - also work as unary
operators. In this case, they have higher
precedence than the binary + and - .
• Examples:
-2 + 17 produces 15
+5 - 14 produces -9
b + -a is legal too, though annoying and
ridiculous.
17
Logical Operators
Symbol Operation
! logical NOT
&& l i l AND
logical
|| logical OR
18
Logical Operators
• Logical operators treat their operands as
either true or false ((logic
g 1 or logic
g 0).
)
• They return a 1-bit result
– 11’b1
b1, 11’b0
b0 or 1’bx
1 bx if results are ambiguous
• Thus (2’b01 && 2’b10) is true because both
operands are non-zero.
non zero Logic 1 is returned.
returned
• It is equivalent to writing
– (2’b01 != 0) && (2’b10 != 0)
19
Logical Operators
• L
Logical
i l operators
t always
l produce
d 1-bit
1 bit results
lt which
hi h
could be 0 (false), 1 (true), or x (ambiguous).
• Logical
i l operators accept variables
i bl or expressions
i as
operands.
• If an operand is equal to zero, it is consider as logical 0
(false) otherwise it is considered as logical 1 (true).
• Logical operators are usually used to form conditions
for conditional if statement or conditional assignment
operator.
20
Logical Operand
• The operands used in a logical operation
evaluate as zeros if and only y if each bit is
zero.
• Otherwise it is a one.
one
• Thus a logic AND of two operands will
result in a logic 1 result if both operands
have at least one bit set.
21
Not A Bitwise Comparison
reg [3:0] X, Y;
reg Z;
always @(X or Y) Z = X && Y;
iinitial
iti l begin
b i
X = 4'b1010; Y = 4'b0101; //Z = 1
#10 X = 4'hf; Y = 4'h1; //Z = 1
#10 X = 0; //Z = 0
end
22
Logical operators
Examples:
x=3 y=0 w = 2’b0x
!x produces 0
x && y produces 0 (1 AND 0)
x || y produces 1 (1 OR 0)
x && w produces x (1 AND x)
23
Logical Operator Usage
Normally used in testing true/false conditions:
if (IN1 && IN2) begin
if (SUM > 0 || PROD < 8’h0F) begin
if (STATE == RESET || STATE == IDLE)
MOTOR_CONTROL <= 8’b0;
else if (!OVERRIDE)
MOTOR_CONTROL <= MOTOR_CONTROL + 1;
24
Bit ise Operators
Bitwise
Symbol Operation
~ bitwise negation
& bitwise AND
~&& bit i NAND
bitwise
| bitwise OR
~| bitwise NOR
^ bitwise XOR
~^ or ^~ bitwise XNOR
25
Bitwise Operators
• Bitwise operators take each bit in one operand
and perform the operation with corresponding
bit in the other operand.
• If one operand is shorter, it will be extended
with zeros to match the length of the longer
operand.
• In bitwise operation, a z is treated as an x.
26
Bitwise Operators
X=AB+AB
Y= A+B
Will be
b representedd in
i Verilog
V il by:
b
28
Bit-Wise Negation vs. Logical Negation
Examples:
A =55’b10101;
b10101; B=4 4’b0;
b0;
!A evaluates to 0
~A
A evaluates
l to 01010
!B evaluates to 1
~B evaluates to 1111
29
Bitwise vs. Logical Operators
• Both perform standard Boolean operations.
– AND, OR, etc.
• Logical Operators are used for comparisons
and generate a true/false condition.
condition
• Bitwise operators can give multi-bit
answers to Boolean operations.
operations
30
One-bit Operands
• Logical and bitwise operators perform identically
on single bit operands.
• It’s still good policy and style to use logical
operators when testing conditions and bitwise
operators whenh performing
f i Booleanl operations.
i
– if (!RST) begin //if RST == 0
– CLK = ~CLK;
CLK //Invert
//I t value
l off CLK
• Some authors recommend only using bitwise
operators never logical at all.
operators, all
31
https://www.synopsys.com/news/pubs/snug/boston2008/bresticker_paper.pdf
Reduction Operators
• Yet another form of Boolean operators, this
one,, like Logical
g Operators,
p , only
y producing
p g
a single bit output.
• Same symbols,
symbols different application.
application
• Reduction operators only operate on a
single operand.
operand
32
Reduction Operators
Symbol Operation
AND_RED
_
34
XOR Reduction
• XOR Reduction is just another way of saying
positive parity generator.
37
Exam 1
• Midterm exam 1 will be held on Tuesday,
October 6.
• Bring a blue book.
• There will be little to nothing to calculate.
calculate
However, you may bring a simple
calculator.
calculator
• No other electronic devices are permitted.
38
Exam 1
• No electronic devices
– This explicitly includes telephones, even if used
only as a watch or calculator.
• Homework/review will be held next time.
• Bring questions.
• There
Th will ill be
b no lab
l b exam day.
d LabL b reports
t
will be due the following week.
39
Relational Operators
Symbol Operation
40
Relational Operators
• Relational operators are used in comparison
expressions.
p
• If the relation is true, it evaluates to logical
1 and if it is false it evaluates to logical 0.
• If any of the operands contains x or z,
z the
relation evaluates to x.
41
Relational Operators
Examples:
if x = 4 ; y = 8 ; w = 3
3’b11z
b11z ; v = 2
2’b0x
b0x
x<y evaluates
l to Logical 1
y <= x evaluates to Logical 0
x > v evaluates to x
y >=
> w evaluates to x
42
Resolving Unknown Bits
• Relational operators do not resolve if either
operand
p contains an unknown bit.
• This is inconsistent with the way equality
operators work
work.
• Thus 1000 > 000x will return unknown,
even if the answer appears obvious.
obvious
• Just have to live with it.
43
Resolve Demo
module resolve;;
assign A = X > Y;
assign B = X === Y;
assign C = X == Y;
#A=xB=0C=0 44
Less Than or Equal/Non
Equal/Non-Blocking
Blocking
Assignment
45
Using Conditionals
Conditionals are used for testing.
testing
Example:
if (ENABLE && STATE <=
< MAX_STATE)
MAX STATE) //conditional
// diti l
RUN <= `TRUE; //assignment operator
else if (ENABLE)
RUN <= `FALSE;
else
RUN <= RUN;;
//VHDL designers note lack of ‘null’ keyword. 46
Shift Operators
Symbol Operation
>> right
g shift
<< left shift
Examples:
w = 6’b111111;
v = w >> 1 will
ill sett v to
t 011111
v = w << 1 will set v to 111110
v = w << 3 will set v to 111000
47
Shift Operators
• Note that shifts do not wrap around or sign
extend.
• Shifted bits just wind up in the bit bucket.
• Wrap or sign extend options must be added
manually.
• Fills
Fill with
i h zeros, from
f the
h right
i h or from
f the
h
left.
48
Shift Right
v <= w >> 1
Eight-bit Register
0 1 1 0 0 1 1 0
0
0 0 1 1 0 0 1 1
Initial value: 8’h66
Final value: 8’h33 Bit Bucket
49
Shift Hardware
• Shift operation implies a shift register: will
thus p
perform multiply/divide
py byy 2 in a
single clock cycle.
• Barrel shifters can also easily be coded
using shift operators, but the synthesized
hardware gets factorially more complicated.
complicated
51
Shift/Arithmetic Shift
module shiftest(DIST, DIN, DOUT1, DOUT2);
p signed
input g [3:0]
[ ] DIN;;
input [1:0] DIST;
output signed [3:0] DOUT1, DOUT2;
reg signed [3:0] DOUT1,
DOUT1 DOUT2;
initial begin
E = 8'ha5;;
#0 $strobe("%d A = %h B = %h C = %h D = %h", $time, A, B, C, D);
#1 E = 8'h5a;
$strobe("%d A = %h B = %h C = %h D = %h", $time, A, B, C, D);
end
endmodule 54
`timescale 1 ns / 1 ns
module shiftdemo;
parameter WIDTH = 8;
reg signed
i d [WIDTH - 1 : 0] A,
A BB, C,
C D,
D E
E;
initial begin
E = 8'ha5;
#0 $strobe ("E = %b", E);
$strobe("Hex: Time %d A = %h B = %h C = %h D = %h", $time, A, B, C, D);
$strobe("Decimal: Time %d A = %d B = %d C = %d D = %d", $time, A, B, C, D);
$strobe("Binary:
$strobe( Binary: Time %d A = %b B = %b C = %b D = %b
%b", $time,
$time A,
A B,
B C,
C D);
#1 E = 8'h5a;
$strobe ("E = %b", E);
$strobe("Hex: Time %d A = %h B = %h C = %h D = %h", $time, A, B, C, D);
$ b ("
$strobe("Decimal:
i l Timei %d A = %d B = %d C = %d D = %d"
%d", $$time,
i A
A, B, C
C, D);
)
$strobe("Binary: Time %d A = %b B = %b C = %b D = %b", $time, A, B, C, D); end
55
endmodule
Simulation Results
# E = 10100101
# Hex: A = 28 B = 28 C = f4 D = 14
# Decimal: A = 40 B = 40 C = -12 D = 20
# Binary:
y A = 00101000 B = 00101000 C = 11110100 D = 00010100
# E = 01011010
# Hex: A = d0 B = d0 C = 0b D = 0b
# Decimal: A = -48
48 B = -48
48 C = 11 D = 11
# Binary: A = 11010000 B = 11010000 C = 00001011 D = 00001011
56
Conclusions
• Shift left: signed and unsigned work the
same.
• Shift right: unsigned fills with zeros, signed
extends sign bit.
bit
• Negative numbers (- sign) only show up
when decimal numbers are usedused.
• That’s just for display.
57
Concatenation Operator {, }
• The
h concatenation
i operator (braces)
(b ) is
i usedd
to append multiple operands to produce one
operand.
• The operands must be sized.
sized They can be
sized constants, 4’h6, bit select, R[5], part-
select, R[5:2], scalar net, scalar register,
g
vector nets or vector registers.
58
Concatenation Operator {, }
• reg [4:0] A; reg [2:0] B; reg [3:0] C;
• Examples:
• A= 5’b10110 ; B=3’b000 ; C=4’b0101
62
Conditional Operator “ ?: ”
• Th
The conditional
diti l operator
t is
i usedd in
i expressions
i
and has the following syntax:
Y=Condition_Expr ? Expr_true_cond : Expr_false_cond
Condition Expr is first evaluated.
• The Condition_Expr
• If it is true, the Expr_true_cond is evaluated and its
value is assigned to Y.
• If the Condition_Expr is false, the Expr_false_cond is
evaluated and its value is assigned to Y.
63
Conditional Operator “ ?: ”
sel
sel1 sel0
v = ^ A & w >> 3;
// v = ^ 110110 & 111111 >> 3; // shift
// v = ^ 110110 & 000111; // bit-wise AND
// v = ^ 000110; // Reduction XOR
// v = 0; // Final result
68
Use Parenthesis to Clarify Logic
v = ^ A & w >> 3; //all but incomprehensible
v = ^(A & (w>>3));//easy to understand
The meaning
Th i off th
the secondd one
obvious. Without parenthesis, the
logic is identical but the meaning
is obscure.
69
Parenthesis Limitation
• Use of parenthesis can prevent the synthesizer
from reordering logic for timing optimization.
SUM <= A*B + C*D + E + F + G
SUM <= (A*B) + ((C*D) + (E+F) + G))
• Same Boolean logic, but first one allows operator
reordering to allow for different signal arrival
times Second is equally correct,
times. correct but may produce
worse results.
70