mirror of
https://github.com/mre/mos6502.git
synced 2024-11-25 02:33:26 +00:00
Merge pull request #4 from akeeton/merge-jm
Merge Johannes' changes into mine
This commit is contained in:
commit
d201bb4b61
2
.gitignore
vendored
2
.gitignore
vendored
@ -65,4 +65,4 @@ TAGS.vi
|
|||||||
\#*
|
\#*
|
||||||
\#*\#
|
\#*\#
|
||||||
src/.DS_Store
|
src/.DS_Store
|
||||||
tmp.*.rs
|
tmp.*.rs
|
5
6502emu/Cargo.toml
Normal file
5
6502emu/Cargo.toml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[package]
|
||||||
|
|
||||||
|
name = "6502emu"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["Andrew Keeton <andrewrkeeton@gmail.com>"]
|
89
6502emu/src/address.rs
Normal file
89
6502emu/src/address.rs
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
// Copyright (C) 2014 The 6502-rs Developers
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions
|
||||||
|
// are met:
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the distribution.
|
||||||
|
// 3. Neither the names of the copyright holders nor the names of any
|
||||||
|
// contributors may be used to endorse or promote products derived from this
|
||||||
|
// software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
// POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct Address(pub u16);
|
||||||
|
|
||||||
|
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct AddressDiff(pub u16);
|
||||||
|
|
||||||
|
pub enum AddressingMode {
|
||||||
|
Immediate,
|
||||||
|
Absolute,
|
||||||
|
ZeroPage,
|
||||||
|
Implied,
|
||||||
|
IndirectAbsolute,
|
||||||
|
AbsoluteIndexedX,
|
||||||
|
AbsoluteIndexedY,
|
||||||
|
ZeroPageIndexedX,
|
||||||
|
ZeroageIndexedY,
|
||||||
|
IndexedIndirect,
|
||||||
|
IndirectIndexed,
|
||||||
|
Relative,
|
||||||
|
Accumulator
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
Address(address_) => address_
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_uint(&self) -> uint {
|
||||||
|
self.to_u16() as uint
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_page_number(&self) -> u8 {
|
||||||
|
(self.to_u16() & 0xff00 >> 8) as u8
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_offset(&self) -> u8 {
|
||||||
|
(self.to_u16() & 0x00ff) as u8
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
// Copyright (C) 2014 The 6502-rs Developers
|
// Copyright (C) 2014 The 6502-rs Developers
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
// Redistribution and use in source and binary forms, with or without
|
// Redistribution and use in source and binary forms, with or without
|
||||||
// modification, are permitted provided that the following conditions
|
// modification, are permitted provided that the following conditions
|
||||||
// are met:
|
// are met:
|
||||||
@ -12,7 +12,7 @@
|
|||||||
// 3. Neither the names of the copyright holders nor the names of any
|
// 3. Neither the names of the copyright holders nor the names of any
|
||||||
// contributors may be used to endorse or promote products derived from this
|
// contributors may be used to endorse or promote products derived from this
|
||||||
// software without specific prior written permission.
|
// software without specific prior written permission.
|
||||||
//
|
//
|
||||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
@ -25,103 +25,23 @@
|
|||||||
// 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.
|
||||||
|
|
||||||
extern crate std;
|
|
||||||
|
|
||||||
// Abbreviations
|
|
||||||
//
|
|
||||||
// General
|
|
||||||
//
|
|
||||||
// M | `Memory location`
|
|
||||||
//
|
|
||||||
// Registers
|
|
||||||
//
|
|
||||||
// A | accumulator
|
|
||||||
// X | general purpose register
|
|
||||||
// Y | general purpose register
|
|
||||||
// NV-BDIZC | processor status flags -- see ProcessorStatus bitflags
|
|
||||||
// SP | stack pointer
|
|
||||||
// PC | program counter
|
|
||||||
//
|
|
||||||
|
|
||||||
pub bitflags! {
|
|
||||||
flags ProcessorStatus: u8 {
|
|
||||||
static N = 0b10000000, // Negative -- sometimes called S for "sign"
|
|
||||||
static V = 0b01000000, // oVerflow
|
|
||||||
static B = 0b00010000, // Brk
|
|
||||||
static D = 0b00001000, // Decimal mode active?
|
|
||||||
static I = 0b00000100, // Irq disabled?
|
|
||||||
static Z = 0b00000010, // Zero
|
|
||||||
static C = 0b00000001, // Carry
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub struct Machine {
|
|
||||||
pub A : u8,
|
|
||||||
pub X : u8,
|
|
||||||
pub Y : u8,
|
|
||||||
pub P : ProcessorStatus,
|
|
||||||
pub SP : u8,
|
|
||||||
pub PC : u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Machine {
|
|
||||||
pub fn new() -> Machine {
|
|
||||||
Machine {
|
|
||||||
A: 0,
|
|
||||||
X: 0,
|
|
||||||
Y: 0,
|
|
||||||
SP: 0,
|
|
||||||
P: ProcessorStatus::empty(),
|
|
||||||
PC: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
|
|
||||||
pub struct StackPointer(u8);
|
|
||||||
|
|
||||||
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
|
|
||||||
pub struct Addr(u16);
|
|
||||||
|
|
||||||
#[deriving(PartialEq, Eq, PartialOrd, Ord)]
|
|
||||||
pub struct AddrDiff(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<AddrDiff, Addr> for Addr {
|
|
||||||
fn add(&self, &AddrDiff(rhs): &AddrDiff) -> Addr {
|
|
||||||
let &Addr(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 Addr(lhs + rhs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub static STACK_POINTER_IN_MEMORY_LO: Addr = Addr(0x0100);
|
|
||||||
pub static STACK_POINTER_IN_MEMORY_HI: Addr = Addr(0x01FF);
|
|
||||||
|
|
||||||
pub fn stack_pointer_to_addr(StackPointer(x) : StackPointer) -> Addr
|
|
||||||
{
|
|
||||||
STACK_POINTER_IN_MEMORY_LO + AddrDiff(x as u16)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We can probably come up with a better way to represent address ranges
|
|
||||||
pub static IRQ_INTERRUPT_VECTOR_LO: Addr = Addr(0xFFFE);
|
|
||||||
pub static IRQ_INTERRUPT_VECTOR_HI: Addr = Addr(0xFFFE);
|
|
||||||
|
|
||||||
#[deriving(Show, PartialEq, Eq)]
|
#[deriving(Show, PartialEq, Eq)]
|
||||||
pub enum Instruction
|
pub enum Instruction
|
||||||
|
// Abbreviations
|
||||||
|
//
|
||||||
|
// General
|
||||||
|
//
|
||||||
|
// M | `Memory location`
|
||||||
|
//
|
||||||
|
// Registers
|
||||||
|
//
|
||||||
|
// A | accumulator
|
||||||
|
// X | general purpose register
|
||||||
|
// Y | general purpose register
|
||||||
|
// NV-BDIZC | processor status flags -- see ProcessorStatus bitflags
|
||||||
|
// SP | stack pointer
|
||||||
|
// PC | program counter
|
||||||
// i/o vars should be listed as follows:
|
// i/o vars should be listed as follows:
|
||||||
// NV BDIZC A X Y SP PC M
|
// NV BDIZC A X Y SP PC M
|
||||||
//
|
//
|
||||||
@ -182,21 +102,4 @@ pub enum Instruction
|
|||||||
, TXA // Transfer X to Accumulator..... | .. .....
|
, TXA // Transfer X to Accumulator..... | .. .....
|
||||||
, TXS // Transfer X to Stack pointer... | .. .....
|
, TXS // Transfer X to Stack pointer... | .. .....
|
||||||
, TYA // Transfer Y to Accumulator..... | .. .....
|
, TYA // Transfer Y to Accumulator..... | .. .....
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum AddressingMode {
|
|
||||||
Immediate,
|
|
||||||
Absolute,
|
|
||||||
ZeroPage,
|
|
||||||
Implied,
|
|
||||||
IndirectAbsolute,
|
|
||||||
AbsoluteIndexedX,
|
|
||||||
AbsoluteIndexedY,
|
|
||||||
ZeroPageIndexedX,
|
|
||||||
ZeroageIndexedY,
|
|
||||||
IndexedIndirect,
|
|
||||||
IndirectIndexed,
|
|
||||||
Relative,
|
|
||||||
Accumulator,
|
|
||||||
}
|
|
||||||
|
|
149
6502emu/src/machine.rs
Normal file
149
6502emu/src/machine.rs
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
// Copyright (C) 2014 The 6502-rs Developers
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions
|
||||||
|
// are met:
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the distribution.
|
||||||
|
// 3. Neither the names of the copyright holders nor the names of any
|
||||||
|
// contributors may be used to endorse or promote products derived from this
|
||||||
|
// software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
// POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
use util::{ BitFlag, Off, On };
|
||||||
|
use memory::Memory;
|
||||||
|
use registers::Registers;
|
||||||
|
|
||||||
|
pub struct Machine {
|
||||||
|
pub registers: Registers,
|
||||||
|
pub memory: Memory
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Machine {
|
||||||
|
pub fn new() -> Machine {
|
||||||
|
Machine{
|
||||||
|
registers: Registers::new(),
|
||||||
|
memory: Memory::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
*self = Machine::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO akeeton: Implement binary-coded decimal.
|
||||||
|
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();
|
||||||
|
let a_after: i8 = a_before + c_before as i8 + value;
|
||||||
|
|
||||||
|
assert_eq!(a_after as u8, a_before as u8 + c_before + value as u8);
|
||||||
|
|
||||||
|
let did_carry = (a_after as u8) < (a_before as u8);
|
||||||
|
let is_zero = a_after == 0;
|
||||||
|
let is_negative = a_after < 0;
|
||||||
|
let did_overflow =
|
||||||
|
(a_before < 0 && value < 0 && a_after >= 0)
|
||||||
|
|| (a_before > 0 && value > 0 && a_after <= 0);
|
||||||
|
|
||||||
|
self.registers.accumulator = a_after;
|
||||||
|
self.registers.status.carry = BitFlag::new(did_carry);
|
||||||
|
self.registers.status.zero = BitFlag::new(is_zero);
|
||||||
|
self.registers.status.negative = BitFlag::new(is_negative);
|
||||||
|
self.registers.status.overflow = BitFlag::new(did_overflow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_with_carry_test() {
|
||||||
|
{
|
||||||
|
let mut machine = Machine::new();
|
||||||
|
|
||||||
|
machine.add_with_carry(1);
|
||||||
|
assert_eq!(machine.registers.accumulator, 1);
|
||||||
|
assert_eq!(machine.registers.status.carry, Off);
|
||||||
|
assert_eq!(machine.registers.status.zero, Off);
|
||||||
|
assert_eq!(machine.registers.status.negative, Off);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
|
||||||
|
machine.add_with_carry(-1);
|
||||||
|
assert_eq!(machine.registers.accumulator, 0);
|
||||||
|
assert_eq!(machine.registers.status.carry, On);
|
||||||
|
assert_eq!(machine.registers.status.zero, On);
|
||||||
|
assert_eq!(machine.registers.status.negative, Off);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
|
||||||
|
machine.add_with_carry(1);
|
||||||
|
assert_eq!(machine.registers.accumulator, 2);
|
||||||
|
assert_eq!(machine.registers.status.carry, Off);
|
||||||
|
assert_eq!(machine.registers.status.zero, Off);
|
||||||
|
assert_eq!(machine.registers.status.negative, Off);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut machine = Machine::new();
|
||||||
|
|
||||||
|
machine.add_with_carry(127);
|
||||||
|
assert_eq!(machine.registers.accumulator, 127);
|
||||||
|
assert_eq!(machine.registers.status.carry, Off);
|
||||||
|
assert_eq!(machine.registers.status.zero, Off);
|
||||||
|
assert_eq!(machine.registers.status.negative, Off);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
|
||||||
|
machine.add_with_carry(-127);
|
||||||
|
assert_eq!(machine.registers.accumulator, 0);
|
||||||
|
assert_eq!(machine.registers.status.carry, On);
|
||||||
|
assert_eq!(machine.registers.status.zero, On);
|
||||||
|
assert_eq!(machine.registers.status.negative, Off);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
|
||||||
|
machine.registers.status.carry = Off;
|
||||||
|
machine.add_with_carry(-128);
|
||||||
|
assert_eq!(machine.registers.accumulator, -128);
|
||||||
|
assert_eq!(machine.registers.status.carry, Off);
|
||||||
|
assert_eq!(machine.registers.status.zero, Off);
|
||||||
|
assert_eq!(machine.registers.status.negative, On);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
|
||||||
|
machine.add_with_carry(127);
|
||||||
|
assert_eq!(machine.registers.accumulator, -1);
|
||||||
|
assert_eq!(machine.registers.status.carry, Off);
|
||||||
|
assert_eq!(machine.registers.status.zero, Off);
|
||||||
|
assert_eq!(machine.registers.status.negative, On);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut machine = Machine::new();
|
||||||
|
|
||||||
|
machine.add_with_carry(127);
|
||||||
|
assert_eq!(machine.registers.accumulator, 127);
|
||||||
|
assert_eq!(machine.registers.status.carry, Off);
|
||||||
|
assert_eq!(machine.registers.status.zero, Off);
|
||||||
|
assert_eq!(machine.registers.status.negative, Off);
|
||||||
|
assert_eq!(machine.registers.status.overflow, Off);
|
||||||
|
|
||||||
|
machine.add_with_carry(1);
|
||||||
|
assert_eq!(machine.registers.accumulator, -128);
|
||||||
|
assert_eq!(machine.registers.status.carry, Off);
|
||||||
|
assert_eq!(machine.registers.status.zero, Off);
|
||||||
|
assert_eq!(machine.registers.status.negative, On);
|
||||||
|
assert_eq!(machine.registers.status.overflow, On);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
// Copyright (C) 2014 The 6502-rs Developers
|
// Copyright (C) 2014 The 6502-rs Developers
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
// Redistribution and use in source and binary forms, with or without
|
// Redistribution and use in source and binary forms, with or without
|
||||||
// modification, are permitted provided that the following conditions
|
// modification, are permitted provided that the following conditions
|
||||||
// are met:
|
// are met:
|
||||||
@ -12,7 +12,7 @@
|
|||||||
// 3. Neither the names of the copyright holders nor the names of any
|
// 3. Neither the names of the copyright holders nor the names of any
|
||||||
// contributors may be used to endorse or promote products derived from this
|
// contributors may be used to endorse or promote products derived from this
|
||||||
// software without specific prior written permission.
|
// software without specific prior written permission.
|
||||||
//
|
//
|
||||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
@ -25,13 +25,17 @@
|
|||||||
// 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.
|
||||||
|
|
||||||
extern crate machine6502;
|
mod address;
|
||||||
|
mod machine;
|
||||||
use machine6502::defs::Machine;
|
mod memory;
|
||||||
|
mod registers;
|
||||||
|
mod util;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _q = Machine::new();
|
let mut machine = machine::Machine::new();
|
||||||
|
|
||||||
println!("Hello, 6502?");
|
println!("A: {}", machine.registers.accumulator);
|
||||||
|
println!("add_with_carry(1)");
|
||||||
|
machine.add_with_carry(1);
|
||||||
|
println!("A: {}", machine.registers.accumulator);
|
||||||
}
|
}
|
||||||
|
|
73
6502emu/src/memory.rs
Normal file
73
6502emu/src/memory.rs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
// Copyright (C) 2014 The 6502-rs Developers
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions
|
||||||
|
// are met:
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the distribution.
|
||||||
|
// 3. Neither the names of the copyright holders nor the names of any
|
||||||
|
// contributors may be used to endorse or promote products derived from this
|
||||||
|
// software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
// 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);
|
||||||
|
pub static STACK_ADDRESS_BEGIN: Address = Address(0x0100);
|
||||||
|
pub static STACK_ADDRESS_END: Address = Address(0x01FF);
|
||||||
|
pub static IRQ_INTERRUPT_VECTOR_LO: Address = Address(0xFFFE);
|
||||||
|
pub static IRQ_INTERRUPT_VECTOR_HI: Address = Address(0xFFFF);
|
||||||
|
|
||||||
|
|
||||||
|
// static MEMORY_SIZE: uint = MEMORY_ADDRESS_END - MEMORY_ADDRESS_BEGIN + 1;
|
||||||
|
pub struct Memory {
|
||||||
|
// Rust doesn't seem to like this:
|
||||||
|
// bytes: [u8, ..MEMORY_SIZE]
|
||||||
|
bytes: [u8, ..2^16]
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Memory {
|
||||||
|
pub fn new() -> Memory {
|
||||||
|
Memory { bytes: [0, ..2^16] }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_byte(&self, address: &Address) -> u8 {
|
||||||
|
self.bytes[address.to_uint()]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
let old_value = self.get_byte(address);
|
||||||
|
self.bytes[address.to_uint()] = value;
|
||||||
|
|
||||||
|
return old_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
93
6502emu/src/registers.rs
Normal file
93
6502emu/src/registers.rs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
// Copyright (C) 2014 The 6502-rs Developers
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions
|
||||||
|
// are met:
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the distribution.
|
||||||
|
// 3. Neither the names of the copyright holders nor the names of any
|
||||||
|
// contributors may be used to endorse or promote products derived from this
|
||||||
|
// software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
// POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
use memory::STACK_ADDRESS_END;
|
||||||
|
use util::{ BitFlag, Off, On };
|
||||||
|
|
||||||
|
pub struct Status {
|
||||||
|
pub carry: BitFlag,
|
||||||
|
pub zero: BitFlag,
|
||||||
|
pub disable_interrupts: BitFlag,
|
||||||
|
pub decimal_mode: BitFlag,
|
||||||
|
pub brk: BitFlag,
|
||||||
|
pub unused: BitFlag,
|
||||||
|
pub overflow: BitFlag,
|
||||||
|
pub negative: BitFlag
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Status {
|
||||||
|
pub fn to_byte(&self) -> u8 {
|
||||||
|
self.carry.to_bit() << 0
|
||||||
|
| self.zero.to_bit() << 1
|
||||||
|
| self.disable_interrupts.to_bit() << 2
|
||||||
|
| self.decimal_mode.to_bit() << 3
|
||||||
|
| self.brk.to_bit() << 4
|
||||||
|
| self.unused.to_bit() << 5
|
||||||
|
| self.overflow.to_bit() << 6
|
||||||
|
| self.negative.to_bit() << 7
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() -> Status {
|
||||||
|
// TODO akeeton: Revisit these defaults.
|
||||||
|
Status {
|
||||||
|
carry: Off,
|
||||||
|
zero: Off,
|
||||||
|
disable_interrupts: On,
|
||||||
|
decimal_mode: Off,
|
||||||
|
brk: Off,
|
||||||
|
unused: On,
|
||||||
|
overflow: Off,
|
||||||
|
negative: Off
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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: StackPointer,
|
||||||
|
pub program_counter: u16,
|
||||||
|
pub status: Status
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Registers {
|
||||||
|
pub fn new() -> Registers {
|
||||||
|
// TODO akeeton: Revisit these defaults.
|
||||||
|
Registers {
|
||||||
|
accumulator: 0,
|
||||||
|
index_x: 0,
|
||||||
|
index_y: 0,
|
||||||
|
stack_pointer: StackPointer(STACK_ADDRESS_END.get_offset()),
|
||||||
|
program_counter: 0,
|
||||||
|
status: Status::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
// Copyright (C) 2014 The 6502-rs Developers
|
// Copyright (C) 2014 The 6502-rs Developers
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
// Redistribution and use in source and binary forms, with or without
|
// Redistribution and use in source and binary forms, with or without
|
||||||
// modification, are permitted provided that the following conditions
|
// modification, are permitted provided that the following conditions
|
||||||
// are met:
|
// are met:
|
||||||
@ -12,7 +12,7 @@
|
|||||||
// 3. Neither the names of the copyright holders nor the names of any
|
// 3. Neither the names of the copyright holders nor the names of any
|
||||||
// contributors may be used to endorse or promote products derived from this
|
// contributors may be used to endorse or promote products derived from this
|
||||||
// software without specific prior written permission.
|
// software without specific prior written permission.
|
||||||
//
|
//
|
||||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
@ -25,5 +25,25 @@
|
|||||||
// 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.
|
||||||
|
|
||||||
pub mod defs;
|
#[deriving(PartialEq, Eq, Show)]
|
||||||
|
pub enum BitFlag {
|
||||||
|
Off,
|
||||||
|
On
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BitFlag {
|
||||||
|
pub fn new(is_set: bool) -> BitFlag {
|
||||||
|
if is_set {
|
||||||
|
On
|
||||||
|
} else {
|
||||||
|
Off
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_bit(&self) -> u8 {
|
||||||
|
match *self {
|
||||||
|
Off => 0,
|
||||||
|
On => 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
Cargo.toml
37
Cargo.toml
@ -1,37 +0,0 @@
|
|||||||
# Copyright (C) 2014 The 6502-rs Developers
|
|
||||||
# All rights reserved.
|
|
||||||
#
|
|
||||||
# Redistribution and use in source and binary forms, with or without
|
|
||||||
# modification, are permitted provided that the following conditions
|
|
||||||
# are met:
|
|
||||||
# 1. Redistributions of source code must retain the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer.
|
|
||||||
# 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer in the
|
|
||||||
# documentation and/or other materials provided with the distribution.
|
|
||||||
# 3. Neither the names of the copyright holders nor the names of any
|
|
||||||
# contributors may be used to endorse or promote products derived from this
|
|
||||||
# software without specific prior written permission.
|
|
||||||
#
|
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
# POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
[package]
|
|
||||||
|
|
||||||
name = "machine6502"
|
|
||||||
version = "0.0.1"
|
|
||||||
authors = ["The 6502-rs Developers"]
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "machine6502"
|
|
||||||
path = "src/machine6502/lib.rs"
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
|||||||
# Copyright (C) 2014 The 6502-rs Developers
|
|
||||||
# All rights reserved.
|
|
||||||
#
|
|
||||||
# Redistribution and use in source and binary forms, with or without
|
|
||||||
# modification, are permitted provided that the following conditions
|
|
||||||
# are met:
|
|
||||||
# 1. Redistributions of source code must retain the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer.
|
|
||||||
# 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer in the
|
|
||||||
# documentation and/or other materials provided with the distribution.
|
|
||||||
# 3. Neither the names of the copyright holders nor the names of any
|
|
||||||
# contributors may be used to endorse or promote products derived from this
|
|
||||||
# software without specific prior written permission.
|
|
||||||
#
|
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
||||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
# POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
[package]
|
|
||||||
name = "test_exe"
|
|
||||||
version = "0.0.1"
|
|
||||||
authors = [ "The 6502-rs Developers" ]
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "test_exe"
|
|
||||||
path = "main.rs"
|
|
||||||
|
|
||||||
[dependencies.machine6502]
|
|
||||||
path="../../"
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user