Fix comparisons

This commit is contained in:
Balt 2024-04-04 17:31:21 +00:00
parent a52d53a8d4
commit 8e6479abe6
2 changed files with 12 additions and 11 deletions

View File

@ -5,7 +5,7 @@
[![Latest version](https://img.shields.io/crates/v/r6502.svg)](https://crates.io/crates/r6502)
[![License](https://img.shields.io/crates/l/r6502.svg)](https://github.com/balt-dev/r6502/blob/trunk/LICENSE-MIT)
[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
![Maintenance](https://img.shields.io/maintenance/as-is/2024?color=gold)
![Maintenance](https://img.shields.io/maintenance/passively-developed/2024?color=ok)
# r6502
### Yet another MOS 6502 emulator.
@ -17,6 +17,8 @@ Designed to support `no-std` and not require an allocator nor any unsafe code, a
The API of this crate shies away from implementing interrupt handling,
instead having you step the emulator one opcode at a time and handle them yourself.
Note that this does not emulate cycle-by-cycle, and as such may not be 100% accurate.
## Feature Flags
The following feature flags exist:

View File

@ -405,7 +405,7 @@ impl<R: ReadCallback, W: WriteCallback> Emulator<R, W> {
self.state.status.set(Status::NEGATIVE, (sum as i8) < 0);
self.state.accumulator = sum;
} else {
self.state.accumulator = self.adc(rhs);
self.state.accumulator = self.add(self.state.accumulator, rhs);
}
})?,
SBC => opcode.address_mode.map(|mode| {
@ -440,7 +440,7 @@ impl<R: ReadCallback, W: WriteCallback> Emulator<R, W> {
self.state.status.set(Status::ZERO, diff == 0);
self.state.accumulator = diff;
} else {
self.state.accumulator = self.adc(!rhs);
self.state.accumulator = self.add(self.state.accumulator, !rhs);
}
})?,
INC => opcode.address_mode.map(|mode| {
@ -628,26 +628,25 @@ impl<R: ReadCallback, W: WriteCallback> Emulator<R, W> {
/// Drives CMP, CPX, and CPY
fn compare(&mut self, lhs: u8, mode: AddressMode) {
let rhs = self.byte(mode);
let diff = lhs.wrapping_sub(rhs);
self.state.status.set(Status::ZERO, diff == 0);
self.state.status.set(Status::CARRY, (diff as i8) >= 0);
self.state.status.set(Status::NEGATIVE, (diff as i8) < 0);
self.state.status.set(Status::ZERO, lhs == rhs);
self.state.status.set(Status::CARRY, lhs >= rhs);
self.state.status.set(Status::NEGATIVE, (lhs.wrapping_sub(rhs) as i8) < 0);
}
/// Computes binary add with carry
fn adc(&mut self, rhs: u8) -> u8 {
let sum = u16::from(self.state.accumulator)
fn add(&mut self, lhs: u8, rhs: u8) -> u8 {
let sum = u16::from(lhs)
+ u16::from(rhs)
+ u16::from(self.state.status.contains(Status::CARRY));
let acc = u16::from(self.state.accumulator);
let lhs = u16::from(lhs);
let rhs = u16::from(rhs);
self.state.status.set(Status::CARRY, sum > 0xFF);
self.state
.status
.set(Status::OVERFLOW, !(acc ^ rhs) & (acc ^ sum) & 0x80 != 0);
.set(Status::OVERFLOW, !(lhs ^ rhs) & (lhs ^ sum) & 0x80 != 0);
let sum = sum as u8;