1
0
mirror of https://github.com/mre/mos6502.git synced 2025-02-21 00:29:26 +00:00

Add add_with_carry().

This commit is contained in:
Andrew Keeton 2014-09-26 05:49:13 -04:00
parent 8deafa2644
commit 6436fef0a0

View File

@ -1,69 +1,141 @@
struct Processor { mod Registers {
accumulator: u8, // Each status flag should be 0 or 1.
index_x: u8, pub struct Status {
index_y: u8, pub carry: u8,
stack_pointer: u8, pub zero: u8,
program_counter: u16, pub interrupt: u8,
status: u8, pub decimal_mode: u8,
} pub brk: u8,
pub unused: u8,
pub overflow: u8,
pub sign: u8
}
impl Processor { impl Status {
} pub fn to_byte(&self) -> u8 {
self.carry << 0
#[deriving(PartialEq)] | self.zero << 1
#[deriving(Eq)] | self.interrupt << 2
#[deriving(PartialOrd)] | self.decimal_mode << 3
#[deriving(Ord)] | self.brk << 4
struct Address(u16); | self.unused << 5
| self.overflow << 6
impl Address { | self.sign << 7
fn to_int(&self) -> u16 {
match *self {
Address(address_) => address_
} }
} }
fn min() -> Address { Address(0x0100) } pub struct Registers {
fn max() -> Address { Address(0x01ff) } pub accumulator: i8,
pub index_x: i8,
pub index_y: i8,
pub stack_pointer: u8,
pub program_counter: u16,
pub status: Status
}
fn is_valid(&self) -> bool { impl Registers {
Address::min() <= *self && *self <= Address::max()
} }
} }
struct Memory { mod Address {
bytes: [u8,.. 256] #[deriving(PartialEq)]
#[deriving(Eq)]
#[deriving(PartialOrd)]
#[deriving(Ord)]
pub struct Address(u16);
impl Address {
/* TODO akeeton: Hide struct Address(u16) "constructor."
pub fn new(address_: u16) -> Address {
Address(address_)
}
*/
pub fn to_int(&self) -> u16 {
match *self {
Address(address_) => address_
}
}
pub fn min() -> Address { Address(0x0100) }
pub fn max() -> Address { Address(0x01ff) }
pub fn is_valid(&self) -> bool {
Address::min() <= *self && *self <= Address::max()
}
}
} }
impl Memory { mod Memory {
fn address_to_byte_offset(address: &Address) -> uint { use Address::Address;
(address.to_int() - Address::min().to_int()) as uint
pub struct Memory {
bytes: [u8,.. 256]
} }
fn get_byte(&self, address: &Address) -> u8 { impl Memory {
if !address.is_valid() fn address_to_byte_offset(address: &Address) -> uint {
{ (address.to_int() - Address::min().to_int()) as uint
fail!("Invalid address.");
} }
else
{ pub fn get_byte(&self, address: &Address) -> u8 {
return self.bytes[Memory::address_to_byte_offset(address)]; if !address.is_valid()
{
fail!("Invalid address.");
}
else
{
return self.bytes[Memory::address_to_byte_offset(address)];
}
}
// Sets the byte at the given address to the given value and returns the
// previous value at the address.
pub fn set_byte(&mut self, address: &Address, value: u8) -> u8 {
if !address.is_valid()
{
fail!("Invalid address.");
}
else
{
let old_value = self.get_byte(address);
self.bytes[Memory::address_to_byte_offset(address)] = value;
return old_value;
}
} }
} }
}
// Sets the byte at the given address to the given value and returns the mod Machine {
// previous value at the address. use Registers::Registers;
fn set_byte(&mut self, address: &Address, value: u8) -> u8 { use Address::Address;
if !address.is_valid() use Memory::Memory;
{
fail!("Invalid address.");
}
else
{
let old_value = self.get_byte(address);
self.bytes[Memory::address_to_byte_offset(address)] = value;
return old_value; struct Machine {
registers: Registers,
memory: Memory
}
impl Machine {
// TODO akeeton: Implement binary-coded decimal.
pub fn add_with_carry(&mut self, value: i8) {
let a: int = self.registers.accumulator as int;
let c: int = self.registers.status.carry as int;
let a_new_full: int = a + c + value as int;
let a_new: i8 = a_new_full as i8;
self.registers.accumulator = a_new;
self.registers.status.carry = if a_new_full == a_new as int { 0 } else { 1 };
self.registers.status.zero = if a_new == 0 { 1 } else { 0 };
self.registers.status.sign = if a_new < 0 { 1 } else { 0 };
self.registers.status.overflow =
if (a < 0 && value < 0 && a_new >= 0)
|| (a > 0 && value > 0 && a_new <= 0)
{ 1 }
else
{ 0 }
} }
} }
} }