-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALU_inst.v
More file actions
130 lines (108 loc) · 3.64 KB
/
Copy pathALU_inst.v
File metadata and controls
130 lines (108 loc) · 3.64 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
module ALUinst(
input [3:0] op,
input [3:0] subop,
input [2:0] state,
output reg u,
output reg op0,
output reg op1,
output reg ze,
output reg sw,
output reg lt,
output reg eq,
output reg gt,
output reg Aw,
output reg wr,
output reg re,
output reg load_acc,
output reg reg_wr_en,
output reg sel_reg // 1 = route reg_rd_data to ALU.in2
);
always @(*) begin
// defaults: all off
u = 0; op0 = 0; op1 = 0; ze = 0; sw = 0;
lt = 0; eq = 0; gt = 0;
Aw = 0; wr = 0; re = 0; load_acc = 0;
reg_wr_en = 0; sel_reg = 0;
case (state)
// ========== FETCH1: load PC into address register ==========
3'b000: begin
Aw = 1;
end
// ========== FETCH2: read instruction from memory ==========
3'b001: begin
re = 1;
end
// ========== FETCH3: IR is latched, PC inc ==========
3'b010: begin
// cpu.v handles IR load and PC increment
end
// ========== EXEC1 ==========
3'b011: begin
case (op)
// ---- memory operations: set address ----
4'b0001, // LDA
4'b0011, // ADD
4'b0100, // SUB
4'b0101, // AND
4'b0110, // XOR
4'b0111, // OR
4'b0010: // STA
Aw = 1;
// ---- register-only ALU ----
4'b1000: // NOT
begin u=0; op0=1; op1=1; ze=0; load_acc=1; end
4'b1001: // INC
begin u=1; op0=1; op1=0; ze=0; load_acc=1; end
4'b1010: // DEC
begin u=1; op0=1; op1=1; ze=0; load_acc=1; end
// ---- branches (set condition selects) ----
4'b1100: // JEQ
begin lt=0; eq=1; gt=0; end
4'b1101: // JLT
begin lt=1; eq=0; gt=0; end
4'b1110: // JGT
begin lt=0; eq=0; gt=1; end
// ---- NOP / register sub-ops ----
4'b0000: begin
if (subop == 4'b0001) // ACC_TO_REG
reg_wr_en = 1;
else if (subop == 4'b0010) // REG_TO_ACC
begin sel_reg=1; u=1; op0=0; op1=0; ze=1; load_acc=1; end
end
endcase
end
// ========== EXEC2: memory read/write ==========
3'b100: begin
case (op)
4'b0001, // LDA
4'b0011, // ADD
4'b0100, // SUB
4'b0101, // AND
4'b0110, // XOR
4'b0111: // OR
re = 1; // read operand data
4'b0010: // STA
wr = 1; // write ACC to memory
endcase
end
// ========== EXEC3: latch ALU result (for mem-based ops) ==========
3'b101: begin
case (op)
4'b0001: // LDA: ACC = 0 + mem[addr]
begin u=1; op0=0; op1=0; ze=1; load_acc=1; end
4'b0011: // ADD
begin u=1; op0=0; op1=0; ze=0; load_acc=1; end
4'b0100: // SUB
begin u=1; op0=0; op1=1; ze=0; load_acc=1; end
4'b0101: // AND
begin u=0; op0=0; op1=0; ze=0; load_acc=1; end
4'b0110: // XOR
begin u=0; op0=0; op1=1; ze=0; load_acc=1; end
4'b0111: // OR
begin u=0; op0=1; op1=0; ze=0; load_acc=1; end
// HLT: handled by cpu.v (check opcode in state 101)
endcase
end
endcase
end
endmodule