ECE448 Lecture16 Fixed Point VHDL 2008
ECE448 Lecture16 Fixed Point VHDL 2008
Lecture 16
Fixed-Point Representation
of Real Numbers
in VHDL 2008
ECE 448 – FPGA and ASIC Design with VHDL George Mason University
Acknowledgments
This set of slides is partially based on course materials
developed by
Dr. David Hwang - Harvard University (formerly GMU)
Mr. Amos Zaslavsky - Technion –
Israel Institute of Technology
Pseudocode:
Julia Set Fractal for c=−0.5+0.5·i
Each value of Z0=z0x+i·z0y, in the pseudocode below, corresponds to one pixel of the display region.
The plotted region should have the following limits 5
zn+1 = zn2 + zn + c
6
Refresher on Complex Numbers
a = ax + ay∙i Square root of -1
7
The quadratic recurrence equation
z = z2 + z + c
where
z = zx + zy∙i
c = cx + cy∙i
8
Pseudocode
In the pseudocode below, z0=z0x+i·z0y, corresponds to one
pixel of the display region.
-2 ≤ z0x=Re(z0) < 2
-1.5 < z0y=Im(z0) ≤ 1.5
-2 z0x=-2..2 2-step 2
0 x=0..640 639 640
1.5 0
-1.5+step 479
-1.5 480 9
Pseudocode (with complex numbers)
for z0y = -1.5+step to 1.5, step 3/480 do
for z0x = -2 to 2-step, step 4/640 do
{
iteration = 0
z = z0x + z0y∙i
while( |z| < 2) && (iteration < MAX_ITER) )
{
z = z2 + z + c // the quadratic recurrence equation
iteration++
}
if |z| < 2
pixel corresponding to z0x, z0y belongs to a fractal
else
pixel corresponding to z0x, z0y does not belong to a fractal
} // next zx0
} // next zy0
}
10
Pseudocode (with real numbers)
for z0y = -1.5+step to 1.5, step 3/480 do
for z0x = -2 to 2-step, step 4/640 do
{
iteration = 0
zx = z0x
zy = z0y
while( (zx2 + zy2 < 4) && (iteration < MAX_ITER) )
{
zxtemp = zx2 – zy2 + zx + cx
zytemp = 2 · zx · zy + zy + cy
zx = zxtemp
zy = zytemp
iteration++
}
11
Recommended Representation
of Real Numbers
• You are expected to use the Q4.28
fixed-point representation
– 4 integer bits
– 28 fractional bits
14
Unsigned
Fixed-Point Representation
K -1
X = å
i =- L
xi × 2i X = xK-1 xK-2 … x1 x0 . x-1 x-2 … x-L
16
Unsigned Binary Examples
17
Maximum Representable Range
18
Unsigned Binary Maximum Representable Range
Examples
Q7.0 à rule of thumb: 0 ≤ X < 128 (K=7)
min: 0000000 = (0)10
max: 1111111 = (27-1) / 20 = 12710
Q2.3 à rule of thumb: 0 ≤ X < 4 (K=2)
min: 00.000 = (0)10
max: 11.111 = (25-1) / 23 = 3.875
Q1.4 à rule of thumb: 0 ≤ X < 2 (K=1)
min: 0.0000 = (0)10
max: 1.1111 = (25-1) / 24 = 1.9375
Q0.7 à rule of thumb: 0 ≤ X < 1 (K=0)
min: .0000000 = (0)10
max: .1111111 = (27-1) / 27 = 0.9921875
19
Signed
Fixed-Point Representation
X = xK-1 ・ X =+
(-2K-1) å x ×2
i =- L
i
i
Q7.0 examples:
positive: 0100101. = 3710
negative: 1001011. = -64 + 8 + 2 + 1 = (-53)10
Q2.3 examples:
positive: 01.001 = 9/8 = (1.125)10
negative: 11.100 = (-16+8+4)/8 = (-0.5)10
Q1.4 examples:
positive: 0.1001 = 9/16 = (0.5625)10
negative: 1.1100 = (-16+8+4)/16 = (-0.25)10
22
Maximum Representable Range
• QK.L signed number has decimal range:
• Minimum: -2K-1
which is obtained when xi=1 for i=K-1 and xi=0 otherwise
• Minimum value is the largest negative value
• Maximum: 2K-1-2-L = (2N-1-1) / 2L
which is obtained when xi=0 for i=K-1 and xi=1 otherwise
• Maximum value is the largest positive value
• Exact representable range: -2K-1 ≤ X ≤ 2K-1-2-L
• Range is asymmetric
• Rule of thumb: range of number X in QK.L notation
• -2K-1 ≤ X < 2K-1
• The number of integer bits K largely determines the maximum
representable range
23
Two's Complement Maximum Representable Range
Examples
24
Two's Complement in a nutshell
• Values starting with '0' are non-negative
• Values starting with '1' are negative
• To obtain decimal value, invert all bits, add LSB, mod by 2K
• Example Q4.0: i.e. 1110 = -(0001 + 0001) mod 24 = -0010 = (-2)10
• Example Q4.2: i.e. 11.00 = -(00.11 + 00.01) mod 22= (-01.00) = (-1)10
• The decimal value 0 is not redundant
• Can only be represented by all zeros, i.e. 0000
• For a two's complement number QK.L
• Exact representable range: -2K-1 ≤ X ≤ 2K-1-2-L
• Range is asymmetric
25
Fixed-Point Representation
in VHDL-93
K -1
X = å x ×2
i =- L
i
i
X = xK-1 xK-2 … x1 x0 . x-1 x-2 … x-L
• IEEE-1076 1987
• IEEE-1076 1993 ← most commonly
supported by CAD tools
• IEEE-1076 2000 (minor changes)
• IEEE-1076 2002 (minor changes)
• IEEE-1076 2008 ← supported by the
new generation of CAD tools, such as
Xilinx Vivado
29
30
31
32
33
34
35
36
37
43
44
Example:
45
Basic Fixed Point Operations
-- Std libraries
library ieee;
use ieee.std_logic_1164.all;
46
Basic Fixed Point Operations
entity Fixed_Test is
Port (
A_F : in sfixed(3 downto -28);
B_F : in sfixed(3 downto -28);
OPCODE : in std_logic_vector(1 downto 0);
S_F : out sfixed(3 downto -28)
);
end Fixed_Test;
47
Basic Fixed Point Operations
architecture Dataflow of Fixed_Test is
signal ADD_F : sfixed(4 downto -28);
signal SUB_F : sfixed(4 downto -28);
signal MUL_F : sfixed(7 downto -56);
begin
-- Addition :
-- Addition between 2 Fixed point number, each Q4.28
will
-- yield a number that is Q5.28
ADD_F <= A_F + B_F;
-- Subtraction
-- Subtraction between 2 Fixed point numbers, each
with
-- Q4.28 gives a Q5.28 number
SUB_F <= A_F - B_F;
48
Basic Fixed Point Operations
-- Multiplication
-- Multiplication between 2 Fixed point numbers, each
with
-- Q4.28 gives Q8.56 number as result
MUL_F <= A_F * B_F;
end Dataflow;
49
Setting the Representation
for Display During Simulation
50
Setting the Representation
for Display During Simulation
51
Basic Fixed Point Operations
Simulation
Addition
52
Basic Fixed Point Operations
Simulation
Subtraction
53
Basic Fixed Point Operations
Simulation
Multiplication
54
Important Recommendations!
For Vivado 2019.1, in order to use the types ufixed and sfixed:
56
Single iteration of the Julia fractal set
calculations
57