-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalu.v
79 lines (73 loc) · 2.93 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
`timescale 1ns / 1ps
module alu (
input clk,
input wire [3:0] operand1, operand2, // ALU inputs
input wire [3:0] alu_op, // ALU operation control
output reg [3:0] result, // ALU result
output reg zero_flag=0, // Zero flag
output reg carry_flag=0, // Carry flag
output reg write_enable=0 // Write enable for the register file
);
always @(posedge clk)
begin
// Default values
carry_flag = 0;
zero_flag=0;
case (alu_op)
4'b0000: begin // Add
{carry_flag, result} = operand1 + operand2; // Add with carry-out
write_enable = 1; // Enable write-back for add operation
end
4'b0001: begin // Subtract
{carry_flag, result} = {1'b0, operand1} - operand2; // Subtract with borrow
//carry_flag = ~carry_flag; // Invert for borrow indication
write_enable = 1; // Enable write-back for subtract operation
end
4'b0010: begin // Multiply
result = operand1 * operand2;
write_enable = 1; // Enable write-back for multiply operation
end
4'b0011: begin // Divide
if (operand2 != 0) begin
result = operand1 / operand2;
end else begin
result = 4'b0000; // Handle divide-by-zero
end
write_enable = 1; // Enable write-back for divide operation
end
4'b0100: begin // AND
result = operand1 & operand2;
write_enable = 1; // Enable write-back for AND operation
end
4'b0101: begin // OR
result = operand1 | operand2;
write_enable = 1; // Enable write-back for OR operation
end
4'b0110: begin // XOR
result = operand1 ^ operand2;
write_enable = 1; // Enable write-back for XOR operation
end
4'b0111: begin // XNOR
result = ~(operand1 ^ operand2);
write_enable = 1; // Enable write-back for XNOR operation
end
4'b1000: begin // NOT
result = ~operand1;
write_enable = 1; // Enable write-back for NOT operation
end
4'b1001: begin // Right Shift
result = operand1 >> 1;
write_enable = 1; // Enable write-back for right shift operation
end
4'b1010: begin // Left Shift
result = operand1 << 1;
write_enable = 1; // Enable write-back for left shift operation
end
default: begin
result = 4'b0000;
write_enable = 0; // No write for undefined operations
end
endcase
zero_flag = (result == 4'b0000); // Set zero flag if result is zero
end
endmodule