Skip to content

Commit

Permalink
SPC700: implement BRA and flag-conditional branch instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Nov 23, 2023
1 parent a51e648 commit ede7064
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
28 changes: 28 additions & 0 deletions src/snes/cpu_spc700/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ where
InstructionType::TCALL => self.op_tcall(instr),
InstructionType::BBC => self.op_bbx(instr, false),
InstructionType::BBS => self.op_bbx(instr, true),
InstructionType::BCC => self.op_branch(instr, !self.regs.test_flag(Flag::C)),
InstructionType::BCS => self.op_branch(instr, self.regs.test_flag(Flag::C)),
InstructionType::BNE => self.op_branch(instr, !self.regs.test_flag(Flag::Z)),
InstructionType::BEQ => self.op_branch(instr, self.regs.test_flag(Flag::Z)),
InstructionType::BPL => self.op_branch(instr, !self.regs.test_flag(Flag::N)),
InstructionType::BMI => self.op_branch(instr, self.regs.test_flag(Flag::N)),
InstructionType::BVC => self.op_branch(instr, !self.regs.test_flag(Flag::V)),
InstructionType::BVS => self.op_branch(instr, self.regs.test_flag(Flag::V)),
InstructionType::BRA => self.op_branch(instr, true),
_ => todo!(),
}
}
Expand Down Expand Up @@ -850,4 +859,23 @@ where
self.tick_bus(2)?;
Ok(())
}

/// (Conditional) branch instructions, except BBS/BBC
fn op_branch(&mut self, instr: &Instruction, take: bool) -> Result<()> {
if !take {
// Branch not taken
return Ok(());
}

// Branch taken
let newpc = self
.regs
.read(Register::PC)
.wrapping_add_signed(instr.imm8(0) as i8 as i16);
self.regs.write(Register::PC, newpc);

// Internal cycles
self.tick_bus(2)?;
Ok(())
}
}
18 changes: 9 additions & 9 deletions src/test/processortests_spc700.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ cpu_test!(instr_0c, 0x0c);
cpu_test!(instr_0d, 0x0d);
//cpu_test!(instr_0e, 0x0e);
//cpu_test!(instr_0f, 0x0f);
//cpu_test!(instr_10, 0x10);
cpu_test!(instr_10, 0x10);
cpu_test!(instr_11, 0x11);
cpu_test!(instr_12, 0x12);
cpu_test_no_trace!(instr_13, 0x13);
Expand Down Expand Up @@ -228,8 +228,8 @@ cpu_test_no_trace!(instr_29, 0x29);
//cpu_test!(instr_2c, 0x2c);
cpu_test!(instr_2d, 0x2d);
//cpu_test!(instr_2e, 0x2e);
//cpu_test!(instr_2f, 0x2f);
//cpu_test!(instr_30, 0x30);
cpu_test!(instr_2f, 0x2f);
cpu_test!(instr_30, 0x30);
cpu_test!(instr_31, 0x31);
cpu_test!(instr_32, 0x32);
cpu_test_no_trace!(instr_33, 0x33);
Expand Down Expand Up @@ -265,7 +265,7 @@ cpu_test!(instr_4c, 0x4c);
cpu_test!(instr_4d, 0x4d);
//cpu_test!(instr_4e, 0x4e);
//cpu_test!(instr_4f, 0x4f);
//cpu_test!(instr_50, 0x50);
cpu_test!(instr_50, 0x50);
cpu_test!(instr_51, 0x51);
cpu_test!(instr_52, 0x52);
cpu_test_no_trace!(instr_53, 0x53);
Expand Down Expand Up @@ -300,7 +300,7 @@ cpu_test_no_trace!(instr_69, 0x69);
cpu_test!(instr_6d, 0x6d);
//cpu_test!(instr_6e, 0x6e);
//cpu_test!(instr_6f, 0x6f);
//cpu_test!(instr_70, 0x70);
cpu_test!(instr_70, 0x70);
cpu_test!(instr_71, 0x71);
cpu_test!(instr_72, 0x72);
cpu_test_no_trace!(instr_73, 0x73);
Expand Down Expand Up @@ -334,7 +334,7 @@ cpu_test!(instr_8c, 0x8c);
cpu_test!(instr_8d, 0x8d);
cpu_test!(instr_8e, 0x8e);
cpu_test!(instr_8f, 0x8f);
//cpu_test!(instr_90, 0x90);
cpu_test!(instr_90, 0x90);
cpu_test!(instr_91, 0x91);
cpu_test!(instr_92, 0x92);
cpu_test_no_trace!(instr_93, 0x93);
Expand Down Expand Up @@ -369,7 +369,7 @@ cpu_test!(instr_ad, 0xad);
cpu_test!(instr_ae, 0xae);
// Wait during fetch
cpu_test_no_trace!(instr_af, 0xaf);
//cpu_test!(instr_b0, 0xb0);
cpu_test!(instr_b0, 0xb0);
cpu_test!(instr_b1, 0xb1);
cpu_test!(instr_b2, 0xb2);
cpu_test_no_trace!(instr_b3, 0xb3);
Expand Down Expand Up @@ -402,7 +402,7 @@ cpu_test!(instr_cc, 0xcc);
cpu_test!(instr_cd, 0xcd);
cpu_test!(instr_ce, 0xce);
//cpu_test!(instr_cf, 0xcf);
//cpu_test!(instr_d0, 0xd0);
cpu_test!(instr_d0, 0xd0);
cpu_test!(instr_d1, 0xd1);
cpu_test!(instr_d2, 0xd2);
cpu_test_no_trace!(instr_d3, 0xd3);
Expand Down Expand Up @@ -435,7 +435,7 @@ cpu_test!(instr_ec, 0xec);
cpu_test!(instr_ed, 0xed);
cpu_test!(instr_ee, 0xee);
//cpu_test!(instr_ef, 0xef);
//cpu_test!(instr_f0, 0xf0);
cpu_test!(instr_f0, 0xf0);
cpu_test!(instr_f1, 0xf1);
cpu_test!(instr_f2, 0xf2);
cpu_test_no_trace!(instr_f3, 0xf3);
Expand Down

0 comments on commit ede7064

Please sign in to comment.