-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixed_addition_modified.sv
More file actions
78 lines (73 loc) · 2.52 KB
/
Fixed_addition_modified.sv
File metadata and controls
78 lines (73 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 18.09.2024 21:02:30
// Design Name:
// Module Name: Fixed_addition
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
`define A_E_WIDTH 8
`define B_E_WIDTH 8
`define A_M_WIDTH 7
`define B_M_WIDTH 7
`define ADD_SIGNAL 0
`define A_WIDTH (`A_E_WIDTH + `A_M_WIDTH + 1)
`define B_WIDTH (`B_E_WIDTH + `A_M_WIDTH + 1)
`define OUT_E_WIDTH (`A_E_WIDTH > `B_E_WIDTH)?(`A_E_WIDTH):(`B_E_WIDTH)
`define OUT_M_WIDTH (`A_E_WIDTH > `B_E_WIDTH)?(`A_M_WIDTH):(`B_M_WIDTH)
`define OUT_WIDTH `B_WIDTH
module Fixed_addition(
input [`A_WIDTH-1:0] a, input [`B_WIDTH-1:0] b, output reg [`OUT_WIDTH-1:0] p
);
// A's exponent and mantissa slicing
reg [`A_E_WIDTH-1:0] A_exp;
reg [`A_M_WIDTH-1:0] A_m;
reg [`B_E_WIDTH-1:0] B_exp;
reg [`B_M_WIDTH-1:0] B_m;
reg [`OUT_E_WIDTH-1:0] aligned_exp; // Use the correct width here
reg [`OUT_M_WIDTH-1:0] mant_a_shift;
reg [`OUT_M_WIDTH-1:0] mant_b_shift;
reg [`OUT_M_WIDTH-1:0] mant_f;
reg signed [7:0] exp_diff; // Signed to handle exponent differences
always @(*) begin
// Extract exponents and mantissas
A_exp = a[`A_E_WIDTH+`A_M_WIDTH-1:`A_M_WIDTH];
A_m = a[`A_M_WIDTH-1:0];
B_exp = b[`B_E_WIDTH+`B_M_WIDTH-1:`B_M_WIDTH];
B_m = b[`B_M_WIDTH-1:0];
// Compare actual exponents, not the widths
if (A_exp > B_exp) begin
exp_diff = A_exp - B_exp;
aligned_exp = A_exp;
mant_a_shift = A_m;
mant_b_shift = B_m >> exp_diff; // Shift B's mantissa by the exponent difference
end else begin
exp_diff = B_exp - A_exp;
aligned_exp = B_exp;
mant_a_shift = A_m >> exp_diff; // Shift A's mantissa by the exponent difference
mant_b_shift = B_m;
end
// Perform the addition or subtraction based on ADD_SIGNAL
if (`ADD_SIGNAL == 1)
mant_f = mant_a_shift + mant_b_shift;
else
if(mant_a_shift > mant_b_shift)
mant_f = mant_a_shift - mant_b_shift;
else
mant_f = mant_b_shift - mant_a_shift;
// Assign final result
p = {aligned_exp, mant_f[`OUT_M_WIDTH-1:0]};
end
endmodule