-
Notifications
You must be signed in to change notification settings - Fork 1
/
adder.sv
executable file
·61 lines (61 loc) · 2.47 KB
/
adder.sv
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
// SYNTHESIZABLE ADDER FOR N BIT SIGNED MAGNITUDE WITH F BITS OF FRACTION
// CREATED BY MEHDI SAFAEE, WINTER 2018-2019
`include "config.svh"
module adder
//##############################################################################################
//##############################################################################################
// PARAMETERs-----------------------------------------------------------------------------------
// INPUT AND OUTPUTS----------------------------------------------------------------------------
(
input logic [`N-1:0] a, b,
output logic [`N-1:0] c
);
// MODULES INSTANTIATIONS-----------------------------------------------------------------------
// VARIABLES -----------------------------------------------------------------------------------
logic [`N-1:0] result;
assign c = result;
// INITIALIZATIONS------------------------------------------------------------------------------
// MAIN-----------------------------------------------------------------------------------------
always @(a,b)
begin
if(a[`N-1] == b[`N-1])
begin
result[`N-2:0] = a[`N-2:0] + b[`N-2:0];
result[`N-1] = a[`N-1];
end
else if(a[`N-1] == 0 && b[`N-1] == 1)
begin
if( a[`N-2:0] > b[`N-2:0] )
begin
result[`N-2:0] = a[`N-2:0] - b[`N-2:0];
result[`N-1] = 0;
end
else
begin
result[`N-2:0] = b[`N-2:0] - a[`N-2:0];
if (result[`N-2:0] == 0)
result[`N-1] = 0;
else
result[`N-1] = 1;
end
end
else
begin
if( a[`N-2:0] > b[`N-2:0] )
begin
result[`N-2:0] = a[`N-2:0] - b[`N-2:0];
if (result[`N-2:0] == 0)
result[`N-1] = 0;
else
result[`N-1] = 1;
end
else
begin
result[`N-2:0] = b[`N-2:0] - a[`N-2:0];
result[`N-1] = 0;
end
end
end
//##############################################################################################
//##############################################################################################
endmodule