1
0
mirror of https://github.com/mre/mos6502.git synced 2024-12-01 11:51:51 +00:00

Fix modules.

This commit is contained in:
Andrew Keeton 2014-09-27 22:09:31 -04:00
parent 1f896fb4e9
commit 2e2ba84d61
5 changed files with 85 additions and 90 deletions

View File

@ -25,31 +25,29 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE. // POSSIBILITY OF SUCH DAMAGE.
mod address { #[deriving(PartialEq)]
#[deriving(PartialEq)] #[deriving(Eq)]
#[deriving(Eq)] #[deriving(PartialOrd)]
#[deriving(PartialOrd)] #[deriving(Ord)]
#[deriving(Ord)] pub struct Address(u16);
pub struct Address(u16);
impl Address { impl Address {
/* TODO akeeton: Hide struct Address(u16) "constructor." /* TODO akeeton: Hide struct Address(u16) "constructor."
pub fn new(address_: u16) -> Address { pub fn new(address_: u16) -> Address {
Address(address_) Address(address_)
} }
*/ */
pub fn to_int(&self) -> u16 { pub fn to_int(&self) -> u16 {
match *self { match *self {
Address(address_) => address_ 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 min() -> Address { Address(0x0100) }
pub fn max() -> Address { Address(0x01ff) }
pub fn is_valid(&self) -> bool {
Address::min() <= *self && *self <= Address::max()
}
} }

View File

@ -25,9 +25,9 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE. // POSSIBILITY OF SUCH DAMAGE.
mod registers; use registers::Registers;
mod address; use address::Address;
mod memory; use memory::Memory;
struct Machine { struct Machine {
registers: Registers, registers: Registers,

View File

@ -26,6 +26,9 @@
// POSSIBILITY OF SUCH DAMAGE. // POSSIBILITY OF SUCH DAMAGE.
mod machine; mod machine;
mod registers;
mod address;
mod memory;
fn main() { fn main() {
let machine = machine::Machine::new(); let machine = machine::Machine::new();

View File

@ -25,45 +25,41 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE. // POSSIBILITY OF SUCH DAMAGE.
mod address; use address::Address;
mod memory { pub struct Memory {
use address::Address; bytes: [u8,.. 256]
}
pub struct Memory { impl Memory {
bytes: [u8,.. 256] fn address_to_byte_offset(address: &Address) -> uint {
(address.to_int() - Address::min().to_int()) as uint
} }
impl Memory { pub fn get_byte(&self, address: &Address) -> u8 {
fn address_to_byte_offset(address: &Address) -> uint { if !address.is_valid()
(address.to_int() - Address::min().to_int()) as uint {
fail!("Invalid address.");
} }
else
pub fn get_byte(&self, address: &Address) -> u8 { {
if !address.is_valid() return self.bytes[Memory::address_to_byte_offset(address)];
{
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 // Sets the byte at the given address to the given value and returns the
// previous value at the address. // previous value at the address.
pub fn set_byte(&mut self, address: &Address, value: u8) -> u8 { pub fn set_byte(&mut self, address: &Address, value: u8) -> u8 {
if !address.is_valid() if !address.is_valid()
{ {
fail!("Invalid address."); fail!("Invalid address.");
} }
else else
{ {
let old_value = self.get_byte(address); let old_value = self.get_byte(address);
self.bytes[Memory::address_to_byte_offset(address)] = value; self.bytes[Memory::address_to_byte_offset(address)] = value;
return old_value; return old_value;
}
} }
} }
} }

View File

@ -25,41 +25,39 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE. // POSSIBILITY OF SUCH DAMAGE.
mod registers { // Each status flag should be 0 or 1.
// Each status flag should be 0 or 1. pub struct Status {
pub struct Status { pub carry: u8,
pub carry: u8, pub zero: u8,
pub zero: u8, pub interrupt: u8,
pub interrupt: u8, pub decimal_mode: u8,
pub decimal_mode: u8, pub brk: u8,
pub brk: u8, pub unused: u8,
pub unused: u8, pub overflow: u8,
pub overflow: u8, pub sign: u8
pub sign: u8 }
}
impl Status { impl Status {
pub fn to_byte(&self) -> u8 { pub fn to_byte(&self) -> u8 {
self.carry << 0 self.carry << 0
| self.zero << 1 | self.zero << 1
| self.interrupt << 2 | self.interrupt << 2
| self.decimal_mode << 3 | self.decimal_mode << 3
| self.brk << 4 | self.brk << 4
| self.unused << 5 | self.unused << 5
| self.overflow << 6 | self.overflow << 6
| self.sign << 7 | self.sign << 7
}
} }
}
pub struct Registers { pub struct Registers {
pub accumulator: i8, pub accumulator: i8,
pub index_x: i8, pub index_x: i8,
pub index_y: i8, pub index_y: i8,
pub stack_pointer: u8, pub stack_pointer: u8,
pub program_counter: u16, pub program_counter: u16,
pub status: Status pub status: Status
} }
impl Registers { impl Registers {
}
} }