Académique Documents
Professionnel Documents
Culture Documents
An Embedded Systems
Approach Using Verilog
Chapter 3
Numeric Basics
Portions of this work are from the book, Digital Design: An Embedded
Systems Approach Using Verilog, by Peter J. Ashenden, published by Morgan
Kaufmann Publishers, Copyright 2007 Elsevier Inc. All rights reserved.
Verilog
Numeric Basics
unsigned integers
signed integers
fixed-point real numbers
floating-point real numbers
complex numbers
Verilog
Unsigned Integers
Verilog
Binary Representation
Decimal: base 10
Binary: base 2
x xn 1 2 n 1 xn 2 2 n 2 x0 2 0
Digital Design Chapter 3 Numeric Basics
Verilog
Binary Representation
To represent x: 0 x N 1, need
log2N bits
Computers use
0: 000000; 2n 1: 111111
Verilog
Verilog
Verilog
x0
x1
y0
y1
xn 1
yn 1
yn
wire [3:0] x;
wire [7:0] y;
ym 2
ym 1
Verilog
y0
y1
x0
x1
yn 1
yn
xn 1
assign x = y[3:0];
ym 2
ym 1
Verilog
Unsigned Addition
1 1 0 0 1
1 0 1 0 1 1 1 1 0 0
0 0 1 1 0 1 0 0 1 0
0 1 0 0 1
1 1 1 0 1
1 1 1 0 0 0 1 1 1 0
1 0 0 1 1 0
carry
bits
overflow
10
Verilog
Addition Circuits
Half adder
c1 x0 y0
xi
yi
ci
si
ci+1
si xi yi ci
ci 1 xi yi xi yi ci
Full adder
11
Verilog
Ripple-Carry Adder
overflow
Worst-case delay
from x0, y0 to sn
carry must ripple through intervening
stages, affecting sum bits
Digital Design Chapter 3 Numeric Basics
12
Verilog
Carry kill:
ki xi yi
Carry propagate: pi xi yi
Carry generate:
g i xi yi
xi
yi
ci
si
ci+1
Adder equations
si pi ci
ci 1 g i pi ci
13
Verilog
Fast-Carry-Chain Adder
yi
xi
yi
+V
pi
ci+1
Xilinx FPGAs
include this
structure
gi
ci
pi
ki
ci+1
si
ci
si
14
Verilog
Carry Lookahead
ci 1 g i pi ci
c1 g 0 p0 c0
c2 g1 p1 g 0 p0 c0 g1 p1 g 0 p1 p0 c0
c3 g 2 p2 g1 p2 p1 g 0 p2 p1 p0 c0
c4 g 3 p3 g 2 p3 p2 g1
p3 p2 p1 g 0 p3 p2 p1 p0 c0
Digital Design Chapter 3 Numeric Basics
15
Verilog
Carry-Lookahead Adder
y3
g3
c4
p3
y2
g2
p2
x1
y1
g1
p1
x0
y0
g0
carry-lookahead generator
p3
s3
x2
c3
p2
s2
c2
p1
s1
c1
p0
c0
p0
s0
16
Verilog
17
Verilog
Adders in Verilog
wire [7:0] a, b, s;
...
assign s = a + b;
wire [8:0] tmp_result;
wire
c;
...
assign tmp_result = {1'b0, a} + {1'b0, b};
assign c
= tmp_result[8];
assign s
= tmp_result[7:0];
assign {c, s} = {1'b0, a} + {1'b0, b};
assign {c, s} = a + b;
Digital Design Chapter 3 Numeric Basics
18
Verilog
Unsigned Subtraction
As in decimal
b:
x:
y:
1 0 1 0 0 1 1 0
0 1 0 0 1 0 1 0
d:
0 1 0 1 1 1 0 0
0 1 0 1 1 0 0 0
borrow
bits
19
Verilog
Subtraction Circuits
b1 x0 y0
bi 1 xi yi xi yi bi
xi
yi
bi
si
bi+1
20
Verilog
Adder/Subtracter Circuits
Addition
Subtraction
si xi yi ci
di xi yi bi
ci 1 xi yi xi yi ci
bi 1 xi yi xi yi bi
21
Verilog
Adder/Subtracter Circuits
xn1
x1 x0
yn1
y1
y0
add/sub
xn1 x1 x0
ovf/unf
cn
sn1
sn1/dn1
yn1
adder
s1
s1/d1
y1
y0
c0
s0
s0/d0
depends on constraints
Digital Design Chapter 3 Numeric Basics
22
Verilog
Subtraction in Verilog
module adder_subtracter ( output [11:0]
output
input [11:0]
input
assign {ovf_unf, s} = !mode ? (x + y)
endmodule
s,
ovf_unf,
x, y,
mode );
: (x - y);
23
Verilog
ci 1 xi ci
24
Verilog
Increment/Decrement in Verilog
assign s = x + 1;
// increment x
assign s = x - 1;
// decrement x
Automatically resized
25
Verilog
Equality Comparison
x0
y0
x1
y1
In Verilog, x == y gives
a bit result
xn1
yn1
eq
assign eq = x == y;
26
Verilog
Inequality Comparison
xn1
yn1
xn2
yn2
x1 > y1
x1
y1
x0
y0
x0 > y0
Digital Design Chapter 3 Numeric Basics
27
Verilog
28
Verilog
Scaling by Power of 2
x xn 1 2 n 1 xn 2 2 n 2 x0 2 0
2 k x xn 1 2 k n 1 xn 2 2 k n 2 x0 2 k 02 k 1 (0)20
29
Verilog
Scaling by Power of 2
x xn 1 2 n 1 xn 2 2 n 2 x0 2 0
x / 2 k xn 1 2 n 1 k xn 2 2 n 2 k xk 20 xk 1 2 1 x0 2 k
30
Verilog
Scaling in Verilog
s = 000100112 = 1910
s = 000100112 = 1910
assign y = s << 2;
assign y = s >> 2;
y = 010011002 = 7610
y = 0001002 = 410
31
Verilog
Unsigned Multiplication
xy x yn 1 2 n 1 yn 2 2 n 2 y0 2 0
yn 1 x 2 n 1 yn 2 x 2 n 2 y0 x 2 0
32
Verilog
xn1 xn2
Unsigned
Multiplication
xn1
cn
sn1 s2 s1
y0
y0
c0
adder
s0
cn
x0
y1
y0
c0
adder
sn1 s2 s1
s0
xn1 xn2
xn2 x1 x0
xn1
x1
x0
y2
cn
yn1 yn2
y1
y0
c0
adder
sn1 s2 s1
s0
xn1 xn2
xn1
cn
sn1
p2n2
xn2 x1 x0
x1
x0
yn1
p2n1
x1
xn1
x0
xn1 xn2
x1
yn1 yn2
y1
y0
c0
adder
s2
s1
s0
pn+1
pn
pn1
p2
p1
p0
33
Verilog
Product Size
(2 n 1)( 2 n 1) 2 2 n 2 n 2 n 1 2 2 n 2 n 1 1
requires n + m bits
// implicit resizing
34
Verilog
Division, remainder
35
Verilog
Gray Codes
Code
Segment
Code
0000
1100
0001
1101
0011
10
1111
0010
11
1110
0110
12
1010
0111
13
1011
0101
14
1001
0100
15
1000
36
Verilog
Signed Integers
37
Verilog
2s-Complement Representation
x xn 1 2 n 1 xn 2 2 n 2 x0 2 0
Most-negative number
Most-positive number
10000 = 2n1
01111 = +2n1 1
xn1 = 1 negative,
xn1 = 0 non-negative
n 2
0
n 1
Since 2 2 2 1
Digital Design Chapter 3 Numeric Basics
38
Verilog
2s-Complement Examples
00110101
10110101
00000000
11111111
10000000
01111111
=
=
=
=
0
1
128
+127
39
Verilog
wire
[11:0] s1;
wire signed [11:0] s2;
...
assign s2 = $signed(s1);
// s1 is known to be
// less than 2**11
...
assign s1= $unsigned(s2); // s2 is known to be nonnegative
Digital Design Chapter 3 Numeric Basics
40
Verilog
41
Verilog
42
Verilog
43
Verilog
sign extension
x0
x1
y0
y1
xn 1
yn 1
yn
ym 2
ym 1
44
Verilog
Signed Negation
Note that xi 1 xi
x 1 (1 xn 1 )2 n 1 (1 xn 2 )2 n 2 (1 x0 )20 1
2 n 1 xn 1 2 n 1 2 n 2 xn 2 2 n 2 20 x0 20 1
( xn 1 2 n 1 xn 2 2 n 2 x0 20 )
2 n 1 (2 n 2 2 0 ) 1
x 2 n 1 2 n 1 x
E.g., 43 is 00101011
so 43 is 11010100 + 1 = 11010101
Digital Design Chapter 3 Numeric Basics
45
Verilog
Signed Negation
E.g., assign y = x;
Digital Design Chapter 3 Numeric Basics
46
Verilog
Signed Addition
x xn 1 2 n 1 xn 20
x y ( xn 1 yn 1 )2
y yn 1 2 n 1 yn 20
n 1
xn 20 yn 20
yields cn1
47
Verilog
1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
72:
49:
0 1 0 0 1 0 0 0
0 0 1 1 0 0 0 1
63:
32:
1 1 0 0 0 0 0 1
1 1 1 0 0 0 0 0
42:
8:
1 1 0 1 0 1 1 0
0 0 0 0 1 0 0 0
121:
0 1 1 1 1 0 0 1
95:
1 0 1 0 0 0 0 1
34:
1 1 0 1 1 1 1 0
no overflow
no overflow
0 1 0 0 1 0 0 0
72:
105:
0 1 0 0 1 0 0 0
0 1 1 0 1 0 0 1
1 0 1 1 0 0 0 1
positive overflow
no overflow
1 0 0 0 0 0 0 0
63:
96:
1 1 1 1 1 0 0 0
1 1 0 0 0 0 0 1
1 0 1 0 0 0 0 0
42:
8:
0 0 1 0 1 0 1 0
1 1 1 1 1 0 0 0
0 1 1 0 0 0 0 1
34:
0 0 1 0 0 0 1 0
negative overflow
no overflow
48
Verilog
49
Verilog
Signed Subtraction
x y x ( y ) x y 1
x1 x0
yn1
y1
y0
add/sub
unsigned
ovf/und
xn1 x1 x0
cn
cn1
signed
ovf
sn1
sn1/dn1
yn1
adder
s1
s1/d1
y1
y0
c0
s0
s0/d0
50
Verilog
Increment, decrement
Comparison
same as unsigned
=, same as unsigned
>, compare sign bits using xn1 yn1
Multiplication
51
Verilog
Multiplying by 2k
Dividing by 2k
52
Verilog
Fixed-Point Numbers
Fixed-point numbers
53
Verilog
Positional Notation
In decimal
10 .24 10 110 1 0 10 0 2 10 1 4 10 2
In binary
54
Verilog
Unsigned Fixed-Point
x xm1 2m1 x0 20 x1 21 x f 2 f
Range: 0 to 2m 2f
Precision: 2f
m may be 0, giving fractions only
e.g., m= 2: 0.0001001101
55
Verilog
Signed Fixed-Point
x xm1 2m1 x0 20 x1 21 x f 2 f
56
Verilog
In DSP
57
Verilog
Fixed-Point in Verilog
58
Verilog
Fixed-Point Operations
e.g., addition:
x y (x 2 y 2 ) / 2
f
10-bit
adder
x0
b4
y0
b3
b4
b5
y7
y8
y9
s0
c4
x7
x8
x9
a3
a7
a6
a5
a4
s7
s8
s9
c3
c4
c5
59
Verilog
Floating-Point Numbers
radix
exponent
60
Verilog
m bits
s exponent
mantissa
exponent2e1 1
x M 2 (1 s) 1.mantissa 2
E
61
Verilog
Floating-Point Range
Largest value
Range: 2
2e1 2
x 2
2e1
62
Verilog
Floating-Point Precision
m bits of precision
63
Verilog
Example Formats
e = 8, m = 23
range 1.2 1038 to 1.7 1038
precision 7 decimal digits
Application-specific, 22 bits
e = 5, m = 16
range 6.1 105 to 6.6 104
precision 5 decimal digits
Digital Design Chapter 3 Numeric Basics
64
Verilog
Denormal Numbers
x M 2 (1 s) 0.mantissa 2
E
Mantissa = 000...0
x M 2 (1 s) 0.0 2
E
2e1 1
0.0
65
Verilog
Infinity
Can be used in subsequent calculations,
avoiding need for overflow check
Not-a-Number (NaN)
Indicates illegal or undefined result
66
Verilog
Floating-Point Operations
E.g., addition
67
Verilog
Summary
Unsigned: x xn 1 2 n 1 xn 2 2 n 2 x0 20
Signed: x xn 1 2 n 1 xn 2 2 n 2 x0 20
Octal and Hex short-hand
Operations: resize, arithmetic, compare
Arithmetic circuits trade off
speed/area/power
Fixed- and floating-point non-integers
Gray codes for position encoding
Digital Design Chapter 3 Numeric Basics
68