From 2e2ba84d61b7925a70b770ed4e0a45cdf0d7eb90 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Sat, 27 Sep 2014 22:09:31 -0400 Subject: [PATCH] Fix modules. --- 6502emu/src/address.rs | 44 ++++++------- 6502emu/src/{machine/mod.rs => machine.rs} | 6 +- 6502emu/src/main.rs | 3 + 6502emu/src/memory.rs | 58 ++++++++--------- .../src/{registers/mod.rs => registers.rs} | 64 +++++++++---------- 5 files changed, 85 insertions(+), 90 deletions(-) rename 6502emu/src/{machine/mod.rs => machine.rs} (97%) rename 6502emu/src/{registers/mod.rs => registers.rs} (67%) diff --git a/6502emu/src/address.rs b/6502emu/src/address.rs index 55b8988..2303be1 100644 --- a/6502emu/src/address.rs +++ b/6502emu/src/address.rs @@ -25,31 +25,29 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -mod address { - #[deriving(PartialEq)] - #[deriving(Eq)] - #[deriving(PartialOrd)] - #[deriving(Ord)] - pub struct Address(u16); +#[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_) - } - */ +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() + 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() + } } diff --git a/6502emu/src/machine/mod.rs b/6502emu/src/machine.rs similarity index 97% rename from 6502emu/src/machine/mod.rs rename to 6502emu/src/machine.rs index 7e714cb..8ad0f1c 100644 --- a/6502emu/src/machine/mod.rs +++ b/6502emu/src/machine.rs @@ -25,9 +25,9 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -mod registers; -mod address; -mod memory; +use registers::Registers; +use address::Address; +use memory::Memory; struct Machine { registers: Registers, diff --git a/6502emu/src/main.rs b/6502emu/src/main.rs index 7ba4dc4..5e35af7 100644 --- a/6502emu/src/main.rs +++ b/6502emu/src/main.rs @@ -26,6 +26,9 @@ // POSSIBILITY OF SUCH DAMAGE. mod machine; +mod registers; +mod address; +mod memory; fn main() { let machine = machine::Machine::new(); diff --git a/6502emu/src/memory.rs b/6502emu/src/memory.rs index 1686f84..a7ab9c1 100644 --- a/6502emu/src/memory.rs +++ b/6502emu/src/memory.rs @@ -25,45 +25,41 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -mod address; +use address::Address; -mod memory { - use address::Address; +pub struct Memory { + bytes: [u8,.. 256] +} - pub struct Memory { - bytes: [u8,.. 256] +impl Memory { + fn address_to_byte_offset(address: &Address) -> uint { + (address.to_int() - Address::min().to_int()) as uint } - impl Memory { - fn address_to_byte_offset(address: &Address) -> uint { - (address.to_int() - Address::min().to_int()) as uint + pub fn get_byte(&self, address: &Address) -> u8 { + if !address.is_valid() + { + fail!("Invalid address."); } - - pub fn get_byte(&self, address: &Address) -> u8 { - if !address.is_valid() - { - fail!("Invalid address."); - } - else - { - return self.bytes[Memory::address_to_byte_offset(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; + // 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; - } + return old_value; } } } diff --git a/6502emu/src/registers/mod.rs b/6502emu/src/registers.rs similarity index 67% rename from 6502emu/src/registers/mod.rs rename to 6502emu/src/registers.rs index 5b89163..019e08c 100644 --- a/6502emu/src/registers/mod.rs +++ b/6502emu/src/registers.rs @@ -25,41 +25,39 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -mod registers { - // Each status flag should be 0 or 1. - pub struct Status { - pub carry: u8, - pub zero: u8, - pub interrupt: u8, - pub decimal_mode: u8, - pub brk: u8, - pub unused: u8, - pub overflow: u8, - pub sign: u8 - } +// Each status flag should be 0 or 1. +pub struct Status { + pub carry: u8, + pub zero: u8, + pub interrupt: u8, + pub decimal_mode: u8, + pub brk: u8, + pub unused: u8, + pub overflow: u8, + pub sign: u8 +} - impl Status { - pub fn to_byte(&self) -> u8 { - self.carry << 0 - | self.zero << 1 - | self.interrupt << 2 - | self.decimal_mode << 3 - | self.brk << 4 - | self.unused << 5 - | self.overflow << 6 - | self.sign << 7 - } +impl Status { + pub fn to_byte(&self) -> u8 { + self.carry << 0 + | self.zero << 1 + | self.interrupt << 2 + | self.decimal_mode << 3 + | self.brk << 4 + | self.unused << 5 + | self.overflow << 6 + | self.sign << 7 } +} - pub struct Registers { - pub accumulator: i8, - pub index_x: i8, - pub index_y: i8, - pub stack_pointer: u8, - pub program_counter: u16, - pub status: Status - } +pub struct Registers { + pub accumulator: i8, + pub index_x: i8, + pub index_y: i8, + pub stack_pointer: u8, + pub program_counter: u16, + pub status: Status +} - impl Registers { - } +impl Registers { } \ No newline at end of file