Final Project
Final Project
Final Project
UNIVERSITY OF TECHNOLOGY
FACULTY OF ELECTRICAL ENGINEERING
--------------o0o---------------
FINAL PROJECT
Microprocessor Lab
2
I. Specification
In this final project, we design a hardware to calculate cos(x) using MacLaurin series (the
formula below) with n = 8. The input value is in radian and represented in 32-bit floating
point number.
Floating point numbers is one way of representing real numbers in binary format. In
this project, our group used it to represent real numbers, all calculations are conducted
based on it. Floating point numbers consist of 3 parts: a one-bit sign (S), 8 bits of
exponent (E) and 23 bits of fraction (F or M), which is so called Mantissa. Figure 1
shows the structure of a 32-bit floating point number.
+ 8-bit exponent
+ 23-bit fraction
3
Bit 22 to 0 denote the fractional part.
2. Exception:
3. Example:
1. Algorithm
The addition of two floating point numbers has two different cases.
- Case1: When both the numbers have same sign i.e when both numbers are either
positive or negative. In this case, the MSB of both number are either 1 or 0.
4
- Case2: When both the numbers have different sign i.e when one number is positive and
other number is negative. In this case, the MSB iff one number is 1 and other is 0.
Enter two numbers A and B in 32 bit floating point (IEE 754 Floating point). Let man_a,
exp_a, man_b, exp_b represent mantissa and exponent of A and B, respectively.
Case1: When both numbers are of same sign
Step 1: Check exp_B > exp_A or not. If yes, swap A and B
Step 2: Check exp_A and exp_B are larger than 255 or not. If yes, set Exception = 1
=> End the process.
Step 3: Check exp_A or exp_B equal to ‘0’ or not. if yes, set the hidden bit of
mantissa to ‘0’.
Step 4: Calculate the different between the exponents of number A and B: diff =
exp_a - exp_2. If diff = 0 then there is no need to shifting the mantissa. If diff is more
then 0 then shift exp_b to the right by an amount of diff
Step 5: Amount of shifting is added to exp_b. New exponent value of exp_b = exp_b
+ diff. Now result is in normalize form because exp_a = exp_b;
Step 6: Check if both number have same sign or not. If ‘yes’, move to step 7. If not,
move to step 11
Step 7: Add the mantissa of 24 bits each including hidden bit new_man = man_a +
man_b
Step 8: Check if there is carry out in mantissa addition. If yes, then add ‘1’ to the
exponent value of either exp_a or new exp_b. After addition, shift the overall result of
mantissa addition to the right one by making MSB of new_man as ‘1 ‘ and dropping LSB
of mantissa
Step 9: If there is no carry out in step 7, then let exp_a in step 5 is the final exponent
and sign is neither a[31] or b[31]. Then move step
Case2: When both numbers are of same sign
Step 10: Take 2’s complement of man_b then add it to man_a to obtain new_man =
m1 + (2’s complement of man_b)
Step 11: Check if there is carry out of the mantissa addition. If yes, then discard the
carry and shift the result to the left until there is ‘1’ in MSB and also count the amount of
shifting.
Step 12: Subtract the amount of shifting from exponent either from exp_a and exp_b.
5
Step 13: if there is no carry out from the mantissa addition then MSB must be ‘1’ and
in this case simply replace new_man by 2’s complement
Step 14: Sign of the result is equal to the sign of the larger number.
Step 15: Concatenate sign, exponent and mantissa after calculating to obtain the final
result
2. Flow chart
6
IV. Multiplication of 32-bit floating point:
1. Algorithm:
The algorithm for floating point multiplication is explained through the flow chart in
Figure 2. Initially, we start with N1 and N2 which are represented in 32-bit floating point
number and assigned S1, F1, E1 and S2, F2, E2 as sign bit, mantissa and exponent
respectively.
7
- Step 5: Check for zero case:
If the result turn into zero (either one operand or both are zero), the final result turn
into zero (E = 0 and F = 0), if not, move to step 6
8
V. Architecture
The calculation of cosine using the MacLaurin series is a 15-state Finite State
Machine (FSM), using two multiplier and one adder/subtractor arranged:
The three D-type Flip Flops are for passing the multiplication factors to the
corresponding multiplier.
All constant values within the MacLaurin series are pre-encoded in 32-bit floating
point.
Assuming the to-be-given radian be x, when the enable flag asserts, the following
sequence occurs:
State 0: Calculates x^2, MacLaurin 2nd term and the MacLaurin sum up to 2nd
term by letting x as both factors of the 1st multiplier and one factor of the 2nd
1
multiplier be the constant of MacLaurin 2nd term (− );
2!
State 1: Stores the product of the 1st multiplier (x^2) and the MacLaurin sum
before updating the cosine value;
State 2: Updates the cosine value to be the MacLaurin sum up to 2nd term and
calculates x^4, MacLaurin 3rd term and the sum, by letting both factors of 1st as
the previously stored x^2 and replacing one factor of 2nd multiplier the
corresponding constant of the current term;
State 3: Similar to state 1;
State 4: Updates the cosine value to the stored sum in state 3 and calculates
MacLaurin 4th term, this time only change one factor of the 1st multiplier to the
stored product (x^4), keeping the other as x^2 and change the 2nd multiplier
one factor accordingly;
For state 5, 7, 9 and 11: repeats state 1;
For state 6, 8, 10, 12: repeats state 4;
State 13: Stores only the resultant sum up to MacLaurin 8th term;
9
State 14: Updates the cosine value and responds with an asserted ‘Done’ flag.
During the calculation, if NaN occurred in the answer, the designed FSM would:
return the cosine value as NaN, accompanied by the asserted ‘Done’ flag and’Except’
flag.
The module exposes four 32-bit registers: radian input at address 0, enable flag
assertion and reading of ‘Done’ and ‘Except’ flags at address 1 (known as control
register), and MacLaurin’s cosine value returned at address 2, while address 3 is
reserved.
1. Test case
10
2. Test bench
11