mirror of
https://github.com/mre/mos6502.git
synced 2024-11-28 07:49:19 +00:00
Add new() methods and get main() running.
This commit is contained in:
parent
2e2ba84d61
commit
0b492a9702
@ -25,29 +25,21 @@
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#[deriving(PartialEq)]
|
||||
#[deriving(Eq)]
|
||||
#[deriving(PartialOrd)]
|
||||
#[deriving(Ord)]
|
||||
pub struct Address(u16);
|
||||
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Address(pub u16);
|
||||
|
||||
impl Address {
|
||||
/* TODO akeeton: Hide struct Address(u16) "constructor."
|
||||
pub fn new(address_: u16) -> Address {
|
||||
Address(address_)
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn to_int(&self) -> u16 {
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
match *self {
|
||||
Address(address_) => address_
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min() -> Address { Address(0x0100) }
|
||||
pub fn max() -> Address { Address(0x01ff) }
|
||||
pub fn get_page_number(&self) -> u8 {
|
||||
(self.to_u16() & 0xff00 >> 8) as u8
|
||||
}
|
||||
|
||||
pub fn is_valid(&self) -> bool {
|
||||
Address::min() <= *self && *self <= Address::max()
|
||||
pub fn get_offset(&self) -> u8 {
|
||||
(self.to_u16() & 0x00ff) as u8
|
||||
}
|
||||
}
|
||||
|
@ -26,15 +26,21 @@
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
use registers::Registers;
|
||||
use address::Address;
|
||||
use memory::Memory;
|
||||
|
||||
struct Machine {
|
||||
registers: Registers,
|
||||
memory: Memory
|
||||
pub struct Machine {
|
||||
pub registers: Registers,
|
||||
pub memory: Memory
|
||||
}
|
||||
|
||||
impl Machine {
|
||||
pub fn new() -> Machine {
|
||||
Machine{
|
||||
registers: Registers::new(),
|
||||
memory: Memory::new()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO akeeton: Implement binary-coded decimal.
|
||||
pub fn add_with_carry(&mut self, value: i8) {
|
||||
let a: int = self.registers.accumulator as int;
|
||||
|
@ -31,5 +31,10 @@ mod address;
|
||||
mod memory;
|
||||
|
||||
fn main() {
|
||||
let machine = machine::Machine::new();
|
||||
let mut machine = machine::Machine::new();
|
||||
|
||||
println!("A: {}", machine.registers.accumulator);
|
||||
println!("add_with_carry(1)");
|
||||
machine.add_with_carry(1);
|
||||
println!("A: {}", machine.registers.accumulator);
|
||||
}
|
||||
|
@ -27,39 +27,41 @@
|
||||
|
||||
use address::Address;
|
||||
|
||||
pub static MEMORY_ADDRESS_BEGIN: Address = Address(0x0000);
|
||||
pub static MEMORY_ADDRESS_END: Address = Address(0xffff);
|
||||
pub static STACK_ADDRESS_BEGIN: Address = Address(0x0100);
|
||||
pub static STACK_ADDRESS_END: Address = Address(0x01ff);
|
||||
|
||||
// static MEMORY_SIZE: uint = MEMORY_ADDRESS_END - MEMORY_ADDRESS_BEGIN + 1;
|
||||
pub struct Memory {
|
||||
bytes: [u8,.. 256]
|
||||
// Rust doesn't seem to like this:
|
||||
// bytes: [u8, ..MEMORY_SIZE]
|
||||
bytes: [u8, ..2^16]
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
fn address_to_byte_offset(address: &Address) -> uint {
|
||||
(address.to_int() - Address::min().to_int()) as uint
|
||||
pub fn new() -> Memory {
|
||||
Memory { bytes: [0, ..2^16] }
|
||||
}
|
||||
|
||||
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)];
|
||||
}
|
||||
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;
|
||||
let old_value = self.get_byte(address);
|
||||
self.bytes[Memory::address_to_byte_offset(address)] = value;
|
||||
|
||||
return old_value;
|
||||
}
|
||||
return old_value;
|
||||
}
|
||||
|
||||
fn address_to_byte_offset(address: &Address) -> uint {
|
||||
(STACK_ADDRESS_BEGIN.to_u16() - STACK_ADDRESS_END.to_u16()) as uint
|
||||
}
|
||||
|
||||
fn is_stack_address(address: &Address) -> bool {
|
||||
STACK_ADDRESS_BEGIN <= *address && *address <= STACK_ADDRESS_END
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,8 @@
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
use memory;
|
||||
|
||||
// Each status flag should be 0 or 1.
|
||||
pub struct Status {
|
||||
pub carry: u8,
|
||||
@ -48,6 +50,20 @@ impl Status {
|
||||
| self.overflow << 6
|
||||
| self.sign << 7
|
||||
}
|
||||
|
||||
pub fn new() -> Status {
|
||||
// TODO akeeton: Revisit these defaults.
|
||||
Status {
|
||||
carry: 0,
|
||||
zero: 0,
|
||||
interrupt: 0,
|
||||
decimal_mode: 0,
|
||||
brk: 0,
|
||||
unused: 1,
|
||||
overflow: 0,
|
||||
sign: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Registers {
|
||||
@ -60,4 +76,15 @@ pub struct Registers {
|
||||
}
|
||||
|
||||
impl Registers {
|
||||
pub fn new() -> Registers {
|
||||
// TODO akeeton: Revisit these defaults.
|
||||
Registers {
|
||||
accumulator: 0,
|
||||
index_x: 0,
|
||||
index_y: 0,
|
||||
stack_pointer: memory::STACK_ADDRESS_END.get_offset(),
|
||||
program_counter: 0,
|
||||
status: Status::new()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user