Skip to content

Commit d3f2b12

Browse files
committed
many more instructions + bugfixes
1 parent 3b91847 commit d3f2b12

File tree

12 files changed

+364
-122
lines changed

12 files changed

+364
-122
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "Gameboy-logs"]
88
path = Gameboy-logs
99
url = https://github.com/wheremyfoodat/Gameboy-logs.git
10+
[submodule "gameboy-doctor"]
11+
path = gameboy-doctor
12+
url = https://github.com/robert/gameboy-doctor

gameboy-doctor

Submodule gameboy-doctor added at 289b892

src/conditions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ impl fmt::Display for Condition {
1919
impl Condition {
2020
pub fn holds(&self, cpu: &CPU) -> bool {
2121
match self {
22-
Condition::C => cpu.registers.get_flag(Flag::C),
23-
Condition::Z => cpu.registers.get_flag(Flag::Z),
24-
Condition::NC => !cpu.registers.get_flag(Flag::C),
25-
Condition::NZ => !cpu.registers.get_flag(Flag::Z),
22+
Condition::C => cpu.registers.read_flag(Flag::C),
23+
Condition::Z => cpu.registers.read_flag(Flag::Z),
24+
Condition::NC => !cpu.registers.read_flag(Flag::C),
25+
Condition::NZ => !cpu.registers.read_flag(Flag::Z),
2626
}
2727
}
2828
}

src/cpu.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,28 @@ impl CPU {
7272
));
7373
res
7474
}
75+
76+
pub fn gbdoctor_string(machine: &Machine) -> String {
77+
let cpu = &machine.cpu;
78+
let mut res = String::new();
79+
res.push_str(&format!("A:{:02X} ", cpu.registers.read_a()));
80+
res.push_str(&format!("F:{:02X} ", cpu.registers.read_f()));
81+
res.push_str(&format!("B:{:02X} ", cpu.registers.read_b()));
82+
res.push_str(&format!("C:{:02X} ", cpu.registers.read_c()));
83+
res.push_str(&format!("D:{:02X} ", cpu.registers.read_d()));
84+
res.push_str(&format!("E:{:02X} ", cpu.registers.read_e()));
85+
res.push_str(&format!("H:{:02X} ", cpu.registers.read_h()));
86+
res.push_str(&format!("L:{:02X} ", cpu.registers.read_l()));
87+
res.push_str(&format!("SP:{:04X} ", cpu.registers.sp));
88+
let pc = cpu.registers.pc;
89+
res.push_str(&format!("PC:{:04X} ", pc));
90+
res.push_str(&format!(
91+
"PCMEM:{:02X},{:02X},{:02X},{:02X}",
92+
machine.read_u8(pc),
93+
machine.read_u8(pc + 1),
94+
machine.read_u8(pc + 2),
95+
machine.read_u8(pc + 3)
96+
));
97+
res
98+
}
7599
}

src/instruction/decode.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub fn decode_instruction_at_address(
8383
Instruction::LD_r8_u8(R8::E, machine.read_u8(address + 1)),
8484
2,
8585
),
86+
0x1F => (Instruction::RRA, 1),
8687

8788
0x20 => (
8889
Instruction::JR_cc_i8(Condition::NZ, machine.read_u8(address + 1) as i8),
@@ -96,6 +97,10 @@ pub fn decode_instruction_at_address(
9697
0x23 => (Instruction::INC_r16(R16::HL), 1),
9798
0x24 => (Instruction::INC_r8(R8::H), 1),
9899
0x25 => (Instruction::DEC_r8(R8::H), 1),
100+
0x26 => (
101+
Instruction::LD_r8_u8(R8::H, machine.read_u8(address + 1)),
102+
2,
103+
),
99104
0x28 => (
100105
Instruction::JR_cc_i8(Condition::Z, machine.read_u8(address + 1) as i8),
101106
2,
@@ -118,6 +123,7 @@ pub fn decode_instruction_at_address(
118123
),
119124
0x32 => (Instruction::LD_mHLdec_A, 1),
120125
0x33 => (Instruction::INC_r16(R16::SP), 1),
126+
0x35 => (Instruction::DEC_mHL, 1),
121127
0x38 => (
122128
Instruction::JR_cc_i8(Condition::C, machine.read_u8(address + 1) as i8),
123129
2,
@@ -180,9 +186,16 @@ pub fn decode_instruction_at_address(
180186
0x6E => (Instruction::LD_r8_mr16(R8::L, R16::HL), 1),
181187
0x6F => (Instruction::LD_r8_r8(R8::L, R8::A), 1),
182188

189+
0x70 => (Instruction::LD_mr16_r8(R16::HL, R8::B), 1),
190+
0x71 => (Instruction::LD_mr16_r8(R16::HL, R8::C), 1),
191+
0x72 => (Instruction::LD_mr16_r8(R16::HL, R8::D), 1),
183192
0x73 => (Instruction::LD_mr16_r8(R16::HL, R8::E), 1),
193+
0x74 => (Instruction::LD_mr16_r8(R16::HL, R8::H), 1),
194+
0x75 => (Instruction::LD_mr16_r8(R16::HL, R8::L), 1),
184195
0x77 => (Instruction::LD_mr16_r8(R16::HL, R8::A), 1),
185196
0x78 => (Instruction::LD_r8_r8(R8::A, R8::B), 1),
197+
0x79 => (Instruction::LD_r8_r8(R8::A, R8::C), 1),
198+
0x7A => (Instruction::LD_r8_r8(R8::A, R8::D), 1),
186199
0x7B => (Instruction::LD_r8_r8(R8::A, R8::E), 1),
187200
0x7C => (Instruction::LD_r8_r8(R8::A, R8::H), 1),
188201
0x7D => (Instruction::LD_r8_r8(R8::A, R8::L), 1),
@@ -197,20 +210,23 @@ pub fn decode_instruction_at_address(
197210
0x9F => (Instruction::SBC_A_A, 1),
198211

199212
0xA5 => (Instruction::AND_L, 1),
200-
0xA8 => (Instruction::XOR_r8(R8::B), 1),
201-
0xA9 => (Instruction::XOR_r8(R8::C), 1),
202-
0xAA => (Instruction::XOR_r8(R8::D), 1),
203-
0xAB => (Instruction::XOR_r8(R8::E), 1),
204-
0xAC => (Instruction::XOR_r8(R8::H), 1),
205-
0xAD => (Instruction::XOR_r8(R8::L), 1),
206-
0xAF => (Instruction::XOR_r8(R8::A), 1),
213+
0xA8 => (Instruction::XOR_A_r8(R8::B), 1),
214+
0xA9 => (Instruction::XOR_A_r8(R8::C), 1),
215+
0xAA => (Instruction::XOR_A_r8(R8::D), 1),
216+
0xAB => (Instruction::XOR_A_r8(R8::E), 1),
217+
0xAC => (Instruction::XOR_A_r8(R8::H), 1),
218+
0xAD => (Instruction::XOR_A_r8(R8::L), 1),
219+
0xAE => (Instruction::XOR_A_mHL, 1),
220+
0xAF => (Instruction::XOR_A_r8(R8::A), 1),
207221

208222
0xB0 => (Instruction::OR_r8(R8::B), 1),
209223
0xB1 => (Instruction::OR_r8(R8::C), 1),
210224
0xB2 => (Instruction::OR_r8(R8::D), 1),
211225
0xB3 => (Instruction::OR_r8(R8::E), 1),
212226
0xB4 => (Instruction::OR_r8(R8::H), 1),
213227
0xB5 => (Instruction::OR_r8(R8::L), 1),
228+
0xB6 => (Instruction::OR_A_mHL, 1),
229+
0xB7 => (Instruction::OR_r8(R8::A), 1),
214230
0xB9 => (Instruction::CP_A_r8(R8::C), 1),
215231
0xBB => (Instruction::CP_A_r8(R8::E), 1),
216232
0xBE => (Instruction::CP_A_mHL, 1),
@@ -229,7 +245,7 @@ pub fn decode_instruction_at_address(
229245
3,
230246
),
231247
0xC5 => (Instruction::PUSH_r16(R16::BC), 1),
232-
0xC6 => (Instruction::ADD_A_u8(machine.read_u8(address + 1)), 1),
248+
0xC6 => (Instruction::ADD_A_u8(machine.read_u8(address + 1)), 2),
233249
0xC8 => (Instruction::RET_cc(Condition::Z), 1),
234250
0xC9 => (Instruction::RET, 1),
235251
0xCB => match machine.read_u8(address + 1) {
@@ -239,15 +255,24 @@ pub fn decode_instruction_at_address(
239255
0x13 => (Instruction::RL_r8(R8::E), 2),
240256
0x14 => (Instruction::RL_r8(R8::H), 2),
241257
0x15 => (Instruction::RL_r8(R8::L), 2),
258+
0x19 => (Instruction::RR_r8(R8::C), 2),
259+
0x1A => (Instruction::RR_r8(R8::D), 2),
260+
0x1F => (Instruction::RR_r8(R8::A), 2),
261+
262+
0x38 => (Instruction::SRL_r8(R8::B), 2),
263+
242264
0x78 => (Instruction::BIT_u3_r8(7, R8::B), 2),
243265
0x79 => (Instruction::BIT_u3_r8(7, R8::C), 2),
244266
0x7A => (Instruction::BIT_u3_r8(7, R8::D), 2),
245267
0x7B => (Instruction::BIT_u3_r8(7, R8::E), 2),
246268
0x7C => (Instruction::BIT_u3_r8(7, R8::H), 2),
269+
247270
_ => {
271+
println!(
272+
"TODO: CB-prefixed opcode 0x{:02x}",
273+
machine.read_u8(address + 1)
274+
);
248275
(Instruction::Prefix, 2) // 1 for prefix, 1 for extension?
249-
// println!("TODO: CB-prefixed opcode 0x{:x}", slice[1]);
250-
// todo!()
251276
}
252277
},
253278
0xCC => (
@@ -260,8 +285,11 @@ pub fn decode_instruction_at_address(
260285
),
261286
0xCE => (Instruction::ADC_A_u8(machine.read_u8(address + 1)), 2),
262287

288+
0xD0 => (Instruction::RET_cc(Condition::NC), 1),
289+
0xD1 => (Instruction::POP_r16(R16::DE), 1),
263290
0xD5 => (Instruction::PUSH_r16(R16::DE), 1),
264-
0xD8 => (Instruction::RET_C, 1),
291+
0xD6 => (Instruction::SUB_A_u8(machine.read_u8(address + 1)), 2),
292+
0xD8 => (Instruction::RET_cc(Condition::C), 1),
265293
0xD9 => (Instruction::RETI, 1),
266294

267295
0xE0 => (Instruction::LD_FFu8_A(machine.read_u8(address + 1)), 2),
@@ -273,6 +301,7 @@ pub fn decode_instruction_at_address(
273301
Instruction::LD_mu16_A(Immediate16::from_memory(machine, address + 1)),
274302
3,
275303
),
304+
0xEE => (Instruction::XOR_A_u8(machine.read_u8(address + 1)), 2),
276305

277306
0xF0 => (Instruction::LD_A_FFu8(machine.read_u8(address + 1)), 2),
278307
0xF1 => (Instruction::POP_r16(R16::AF), 1),

src/instruction/display.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ impl DecodedInstruction {
7474
Instruction::RL_r8(r8) => {
7575
format!("RL {}", r8)
7676
}
77-
Instruction::XOR_r8(r8) => {
78-
format!("XOR {}", r8)
77+
Instruction::RR_r8(r8) => {
78+
format!("RRL {}", r8)
79+
}
80+
Instruction::XOR_A_r8(r8) => {
81+
format!("XOR A, {}", r8)
7982
}
8083
Instruction::ADC_A_r8(r8) => {
8184
format!("ADC A, {}", r8)
@@ -116,6 +119,7 @@ impl DecodedInstruction {
116119
Instruction::RET => String::from("RET"),
117120
Instruction::RETI => String::from("RETI"),
118121
Instruction::RLA => String::from("RLA"),
122+
Instruction::RRA => String::from("RRA"),
119123
Instruction::SBC_A_A => String::from("SBC A, A"),
120124
Instruction::SBC_A_C => String::from("SBC A, C"),
121125
Instruction::LD_A_mHLinc => String::from("LD A [HL+]"),
@@ -138,7 +142,24 @@ impl DecodedInstruction {
138142
Instruction::CALL_cc_u16(cc, imm16) => {
139143
format!("CALL {}, 0x{:04X}", cc, imm16.as_u16())
140144
}
141-
Instruction::RET_C => String::from("RET C"),
145+
Instruction::SUB_A_u8(u8) => {
146+
format!("SUB A, {:02X}", u8)
147+
}
148+
Instruction::XOR_A_u8(u8) => {
149+
format!("XOR A, 0x{:02X}", u8)
150+
}
151+
Instruction::XOR_A_mHL => {
152+
format!("XOR A, [HL]")
153+
}
154+
Instruction::SRL_r8(r8) => {
155+
format!("SRL {}", r8)
156+
}
157+
Instruction::OR_A_mHL => {
158+
format!("OR A, [HL]")
159+
}
160+
Instruction::DEC_mHL => {
161+
format!("DEC [HL]")
162+
}
142163
}
143164
}
144165
}

0 commit comments

Comments
 (0)