1
0
mirror of https://github.com/mre/mos6502.git synced 2024-06-13 15:29:33 +00:00

Integrate AddressDiff.

This commit is contained in:
Andrew Keeton 2014-09-28 20:59:09 -04:00
parent 957a3272b7
commit f7cf02fb9e
4 changed files with 37 additions and 4 deletions

View File

@ -28,6 +28,30 @@
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
pub struct Address(pub u16);
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
pub struct AddressDiff(pub u16);
// The idea here is that it doesn't make sense to add two addresses, but it
// does make sense to add an address and an "address-difference". (If this
// is too annoying to work with we should let it go.)
impl Add<AddressDiff, Address> for Address {
fn add(&self, &AddressDiff(rhs): &AddressDiff) -> Address {
let &Address(lhs) = self;
// We probably don't want to overflow when doing arithmetic in our own
// code.
debug_assert!({
match lhs.checked_add(&rhs) {
None => false,
_ => true
}
});
return Address(lhs + rhs);
}
}
impl Address {
pub fn to_u16(&self) -> u16 {
match *self {

View File

@ -47,7 +47,6 @@ impl Machine {
}
// TODO akeeton: Implement binary-coded decimal.
// TODO akeeton: Add test.
pub fn add_with_carry(&mut self, value: i8) {
let a_before: i8 = self.registers.accumulator;
let c_before: u8 = self.registers.status.carry.to_bit();

View File

@ -26,6 +26,8 @@
// POSSIBILITY OF SUCH DAMAGE.
use address::Address;
use address::AddressDiff;
use registers::StackPointer;
pub static MEMORY_ADDRESS_BEGIN: Address = Address(0x0000);
pub static MEMORY_ADDRESS_END: Address = Address(0xffff);
@ -57,7 +59,12 @@ impl Memory {
return old_value;
}
fn is_stack_address(address: &Address) -> bool {
pub fn is_stack_address(address: &Address) -> bool {
STACK_ADDRESS_BEGIN <= *address && *address <= STACK_ADDRESS_END
}
pub fn stack_pointer_to_address(&StackPointer(sp): &StackPointer) -> Address
{
STACK_ADDRESS_BEGIN + AddressDiff(sp as u16)
}
}

View File

@ -66,11 +66,14 @@ impl Status {
}
}
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
pub struct StackPointer(pub u8);
pub struct Registers {
pub accumulator: i8,
pub index_x: i8,
pub index_y: i8,
pub stack_pointer: u8,
pub stack_pointer: StackPointer,
pub program_counter: u16,
pub status: Status
}
@ -82,7 +85,7 @@ impl Registers {
accumulator: 0,
index_x: 0,
index_y: 0,
stack_pointer: STACK_ADDRESS_END.get_offset(),
stack_pointer: StackPointer(STACK_ADDRESS_END.get_offset()),
program_counter: 0,
status: Status::new()
}