-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmetic Logic Unit
70 lines (60 loc) · 1.31 KB
/
Arithmetic Logic Unit
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
module ALU (ALUop, Ain, Bin, out, sta_out);
input [1:0] ALUop;
input [15:0] Ain, Bin;
output reg [15:0] out;
output reg [2:0] sta_out;
wire [15:0] s;
reg sub;
wire overflow;
//ALU substraction
always @(*)
case(ALUop)
2'b01: sub=1;
default: sub=0;
endcase
//if the operation is subtraction, go to Status block
AddSub #(16) alucore(Ain, Bin, sub, s,overflow);
//AND and =Bin logic
always @(*) begin
casex(ALUop)
2'b0x: out=s;
2'b10: out=(Ain&Bin);
default: out=Bin;
endcase
end
//assign the value for if result has overflow, is negative number and is zero
always@(*) begin
if(out==16'b0)
sta_out[2]=1;
else
sta_out[2]=0;
if(out[15]==1)
sta_out[1]=1;
else
sta_out[1]=0;
sta_out[0]= overflow;
end
endmodule
// a full adder to detect overflow
module AddSub(a, b, sub, s, ovf);
parameter n=8;
input [n-1:0] a,b;
input sub;
output [n-1:0]s;
output ovf;
wire c1, c2;
wire ovf= c1^c2;
Adder1 #(n-1) ai(a[n-2:0], b[n-2:0]^{n-1{sub}}, sub, c1, s[n-2:0]);
Adder1 #(1) as(a[n-1], b[n-1]^sub, c1, c2, s[n-1]);
endmodule
//half adder
module Adder1(a,b,cin,cout,s);
parameter n=8;
input [n-1:0] a,b;
input cin;
output [n-1:0] s;
output cout;
wire [n-1:0] s;
wire cout;
assign {cout,s}= a+b+cin;
endmodule