Fix some carry-related bugs and a typo in insn_alu_a.
[fpgaboy.git] / core / insn_alu8.v
CommitLineData
e7fe9dc2
JW
1`define INSN_ALU8 9'b010xxxxxx // 10 xxx yyy
2`define INSN_ALU8IMM 9'b011xxx110
3
3ad960bd 4`ifdef EXECUTE
f888201b
JW
5 `INSN_ALU8,`INSN_ALU8IMM: begin
6 if ((opcode[7:6] == 2'b11) && (cycle == 0)) begin // alu8imm
7 `EXEC_INC_PC
8 `EXEC_READ(`_PC + 1)
9 end else if ((opcode[2:0] == `INSN_reg_dHL) && (cycle == 0))
5c33c5c0 10 `EXEC_READ(`_HL)
5509558d 11 else begin
5c33c5c0
JW
12 `EXEC_NEWCYCLE
13 `EXEC_INC_PC
3ad960bd 14 case (opcode[2:0])
5509558d
JW
15 `INSN_reg_A: tmp <= `_A;
16 `INSN_reg_B: tmp <= `_B;
17 `INSN_reg_C: tmp <= `_C;
18 `INSN_reg_D: tmp <= `_D;
19 `INSN_reg_E: tmp <= `_E;
20 `INSN_reg_H: tmp <= `_H;
21 `INSN_reg_L: tmp <= `_L;
3ad960bd
JW
22 `INSN_reg_dHL: tmp <= rdata;
23 endcase
24 end
25 end
26`endif
27
28`ifdef WRITEBACK
f888201b
JW
29 `INSN_ALU8,`INSN_ALU8IMM: begin
30 if (((opcode[2:0] == `INSN_reg_dHL) || (opcode[7:6] == 2'b11)) && (cycle == 0)) begin
3ad960bd
JW
31 /* Sit on our asses. */
32 end else begin /* Actually do the computation! */
33 case (opcode[5:3])
34 `INSN_alu_ADD: begin
5509558d 35 `_A <= `_A + tmp;
dabe1f38 36 `_F <= { /* Z */ ((`_A + tmp) == 8'b0) ? 1'b1 : 1'b0,
3ad960bd 37 /* N */ 1'b0,
5509558d
JW
38 /* H */ (({1'b0,`_A[3:0]} + {1'b0,tmp[3:0]}) >> 4 == 1) ? 1'b1 : 1'b0,
39 /* C */ (({1'b0,`_A} + {1'b0,tmp}) >> 8 == 1) ? 1'b1 : 1'b0,
40 `_F[3:0]
3ad960bd
JW
41 };
42 end
43 `INSN_alu_ADC: begin
5509558d 44 `_A <= `_A + tmp + {7'b0,`_F[4]};
dabe1f38 45 `_F <= { /* Z */ ((`_A + tmp + {7'b0,`_F[4]}) == 8'b0) ? 1'b1 : 1'b0,
3ad960bd 46 /* N */ 1'b0,
5509558d
JW
47 /* H */ (({1'b0,`_A[3:0]} + {1'b0,tmp[3:0]} + {4'b0,`_F[4]}) >> 4 == 1) ? 1'b1 : 1'b0,
48 /* C */ (({1'b0,`_A} + {1'b0,tmp} + {8'b0,`_F[4]}) >> 8 == 1) ? 1'b1 : 1'b0,
49 `_F[3:0]
3ad960bd
JW
50 };
51 end
52 `INSN_alu_SUB: begin
5509558d
JW
53 `_A <= `_A - tmp;
54 `_F <= { /* Z */ (`_A == tmp) ? 1'b1 : 1'b0,
3ad960bd 55 /* N */ 1'b1,
5509558d
JW
56 /* H */ (tmp[3:0] > `_A[3:0]) ? 1'b1 : 1'b0,
57 /* C */ (tmp > `_A) ? 1'b1 : 1'b0,
58 `_F[3:0]
3ad960bd
JW
59 };
60 end
61 `INSN_alu_SBC: begin
5509558d 62 `_A <= `_A - (tmp + {7'b0,`_F[4]});
dabe1f38 63 `_F <= { /* Z */ ((`_A - (tmp + {7'b0,`_F[4]})) == 8'b0) ? 1'b1 : 1'b0,
3ad960bd 64 /* N */ 1'b1,
5509558d
JW
65 /* H */ (({1'b0,tmp[3:0]} + {4'b0,`_F[4]}) > {1'b0,`_A[3:0]}) ? 1'b1 : 1'b0,
66 /* C */ (({1'b0,tmp} + {8'b0,`_F[4]}) > {1'b0,`_A[7:0]}) ? 1'b1 : 1'b0,
67 `_F[3:0]
3ad960bd
JW
68 };
69 end
70 `INSN_alu_AND: begin
5509558d 71 `_A <= `_A & tmp;
25f4912c 72 `_F <= { /* Z */ ((`_A & tmp) == 8'b0) ? 1'b1 : 1'b0,
3ad960bd 73 3'b010,
5509558d 74 `_F[3:0]
3ad960bd
JW
75 };
76 end
77 `INSN_alu_OR: begin
5509558d 78 `_A <= `_A | tmp;
25f4912c 79 `_F <= { /* Z */ ((`_A | tmp) == 8'b0) ? 1'b1 : 1'b0,
3ad960bd 80 3'b000,
5509558d 81 `_F[3:0]
3ad960bd
JW
82 };
83 end
84 `INSN_alu_XOR: begin
5509558d 85 `_A <= `_A ^ tmp;
25f4912c 86 `_F <= { /* Z */ ((`_A ^ tmp) == 8'b0) ? 1'b1 : 1'b0,
3ad960bd 87 3'b000,
5509558d 88 `_F[3:0]
3ad960bd
JW
89 };
90 end
91 `INSN_alu_CP: begin
5509558d 92 `_F <= { /* Z */ (`_A == tmp) ? 1'b1 : 1'b0,
3ad960bd 93 /* N */ 1'b1,
5509558d
JW
94 /* H */ (tmp[3:0] > `_A[3:0]) ? 1'b1 : 1'b0,
95 /* C */ (tmp > `_A) ? 1'b1 : 1'b0,
96 `_F[3:0]
3ad960bd
JW
97 };
98 end
99 default:
100 $stop;
101 endcase
102 end
103 end
104`endif
This page took 0.044023 seconds and 4 git commands to generate.