Skip to content

Commit

Permalink
uPD77C25: fix some mistakes, write dest register in ALU ops
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Jan 20, 2024
1 parent 915db64 commit c341bf5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/bin/upddisasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> Result<()> {
let mut pos = 0;
while let Ok(ins) = Instruction::decode(&mut fiter) {
println!("{:04X} {}", pos, ins);
pos += ins.len();
pos += 1;
}
Ok(())
}
28 changes: 16 additions & 12 deletions src/snes/cpu_upd77c25/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl CpuUpd77c25 {
fn execute_mul(&mut self) {
let k = self.regs.read(Register::K) as i32;
let l = self.regs.read(Register::L) as i32;
let result = k * l;
let result = k.wrapping_mul(l);
self.regs.write(Register::M, (result >> 15) as i16 as u16);
self.regs.write(Register::N, (result << 1) as i16 as u16);
}
Expand Down Expand Up @@ -266,10 +266,11 @@ impl CpuUpd77c25 {

fn op_op_alu(&mut self, instr: &InstructionOpRt) -> Result<()> {
// First operand
let (a, a_flags) = match instr.asl() {
ASL::ACCA => (self.regs.read(Register::ACCA), Flags::A),
ASL::ACCB => (self.regs.read(Register::ACCB), Flags::B),
let (a_reg, a_flags) = match instr.asl() {
ASL::ACCA => (Register::ACCA, Flags::A),
ASL::ACCB => (Register::ACCB, Flags::B),
};
let a = self.regs.read(a_reg);

// Second operand
let b = match instr.pselect() {
Expand Down Expand Up @@ -313,21 +314,28 @@ impl CpuUpd77c25 {
a_flags,
&[(Flag::Z, c == 0), (Flag::S0, c & 0x8000 == 0x8000)],
);
if self.regs.test_flag(a_flags, Flag::OV1) {
if !self.regs.test_flag(a_flags, Flag::OV1) {
self.regs.write_flags(
a_flags,
&[(Flag::S1, self.regs.test_flag(a_flags, Flag::S0))],
);
}

self.regs.write(a_reg, c);

self.regs.write_flags(
a_flags,
&match instr.alu() {
AluFunction::Nop => unreachable!(),
AluFunction::And | AluFunction::Cmp | AluFunction::Or | AluFunction::Xor => {
[(Flag::C, false), (Flag::OV0, false), (Flag::OV1, false)]
}
AluFunction::And
| AluFunction::Cmp
| AluFunction::Or
| AluFunction::Xor
| AluFunction::Shl2
| AluFunction::Shl4
| AluFunction::Xchg => [(Flag::C, false), (Flag::OV0, false), (Flag::OV1, false)],
AluFunction::Sub | AluFunction::Sbr | AluFunction::Dec => {
todo!();
let fcarry = a ^ b ^ c;
let foverflow = (a ^ c) & (a ^ b);
let ov0 = foverflow & 0x8000 == 0x8000;
Expand Down Expand Up @@ -355,10 +363,6 @@ impl CpuUpd77c25 {
(Flag::OV0, false),
(Flag::OV1, false),
],
AluFunction::Shl2 | AluFunction::Shl4 => {
[(Flag::C, false), (Flag::OV0, false), (Flag::OV1, false)]
}
AluFunction::Xchg => todo!(),
},
);

Expand Down
4 changes: 2 additions & 2 deletions src/snes/cpu_upd77c25/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ impl InstructionOpRt {
}

pub fn pselect(&self) -> PSelect {
PSelect::from_u32((self.opcode >> 20) >> 0x03)
PSelect::from_u32((self.opcode >> 20) & 0x03)
.expect(format!("Invalid PSelect in {:?}", self).as_str())
}

pub fn alu(&self) -> AluFunction {
AluFunction::from_u32((self.opcode >> 16) & 0x07)
AluFunction::from_u32((self.opcode >> 16) & 0x0F)
.expect(format!("Invalid ALU in {:?}", self).as_str())
}

Expand Down
3 changes: 2 additions & 1 deletion src/snes/cpu_upd77c25/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl fmt::Display for RegisterFile {

write!(
f,
"ACCA:{:04X} ACCB:{:04X} K:{:04X} L:{:04X} M:{:04X} N:{:04X} TR:{:04X} TRB:{:04X} SP:{:01X} PC:{:04X} DP:{:04X} RP:{:04X} SR:{:04X} ({}) Flg-A:{:02X} ({}) Flg-B:{:02X} ({})",
"ACCA:{:04X} ACCB:{:04X} K:{:04X} L:{:04X} M:{:04X} N:{:04X} TR:{:04X} TRB:{:04X} SP:{:01X} PC:{:04X} DP:{:04X} RP:{:04X} DR:{:04X} SR:{:04X} ({}) Flg-A:{:02X} ({}) Flg-B:{:02X} ({})",
self.acca,
self.accb,
self.k,
Expand All @@ -315,6 +315,7 @@ impl fmt::Display for RegisterFile {
self.pc,
self.dp,
self.rp,
self.dr,
self.sr,
sr,
self.flags[0],
Expand Down

0 comments on commit c341bf5

Please sign in to comment.