-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalu.v
85 lines (75 loc) · 2.52 KB
/
alu.v
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
79
80
81
82
83
84
85
// Computer Architecture (CO224) - Lab 05 - Part 1
// Design: ALU
// Group Number : 17
`timescale 1ns/100ps
/**********************************************************************************/
// Define the FORWARD module
module forwardForALU(DATA2, RESULT);
// Port declarations
input [7:0] DATA2;
output [7:0] RESULT;
assign #1 RESULT=DATA2;
endmodule // endmodule statement
/**********************************************************************************/
// Define the ADD module
module addForALU(DATA1, DATA2, RESULT);
// Port declarations
input [7:0] DATA1, DATA2;
output [7:0] RESULT;
assign #2 RESULT=DATA1 + DATA2;
endmodule // endmodule statement
/**********************************************************************************/
// Define the AND module
module andForALU(DATA1, DATA2, RESULT);
// Port declarations
input [7:0] DATA1, DATA2;
output [7:0] RESULT;
assign #1 RESULT=DATA1 & DATA2;
endmodule // endmodule statement
/**********************************************************************************/
// Define the OR module
module orForALU(DATA1, DATA2, RESULT);
// Port declarations
input [7:0] DATA1, DATA2;
output [7:0] RESULT;
assign #1 RESULT=DATA1 | DATA2;
endmodule // endmodule statement
/**********************************************************************************/
// ALU 8-bit module
module alu(DATA1, DATA2, SELECT, RESULT, ZERO);
// Port declarations, Declarations of wire & reg
input [7:0] DATA1, DATA2;
input [2:0] SELECT;
output reg [7:0] RESULT;
output reg ZERO;
wire [7:0] RESULT_FOR_FORWARD, RESULT_FOR_ADD, RESULT_FOR_AND, RESULT_FOR_OR;
// Instantiate modules
forwardForALU forward_1(DATA2, RESULT_FOR_FORWARD);
addForALU add_1(DATA1, DATA2, RESULT_FOR_ADD);
andForALU and_1(DATA1, DATA2, RESULT_FOR_AND);
orForALU or_1(DATA1, DATA2, RESULT_FOR_OR);
// Select everytime DATA1, DATA2, SELECT updates
always @(DATA1, DATA2, SELECT)
begin
// Select the function to implement using a case structure
case(SELECT)
3'b000: //FORWARD
assign RESULT=RESULT_FOR_FORWARD;
3'b001: //ADD
assign RESULT=RESULT_FOR_ADD;
3'b010: //AND
assign RESULT=RESULT_FOR_AND;
3'b011: //OR
assign RESULT=RESULT_FOR_OR;
default: RESULT=8'b00000000; //DEFAULT
endcase
end
always @(RESULT_FOR_ADD) begin
if (RESULT_FOR_ADD == 8'b00000000) begin
ZERO = 1'b1;
end
else begin
ZERO = 1'b0;
end
end
endmodule // endmodule statement