mirror of
https://github.com/mre/mos6502.git
synced 2025-02-17 17:30:43 +00:00
Add Default impls
This commit is contained in:
parent
0749b1798b
commit
93a39980e4
26
src/cpu.rs
26
src/cpu.rs
@ -37,6 +37,12 @@ pub struct CPU {
|
|||||||
pub memory: Memory,
|
pub memory: Memory,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for CPU {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CPU {
|
impl CPU {
|
||||||
pub fn new() -> CPU {
|
pub fn new() -> CPU {
|
||||||
CPU {
|
CPU {
|
||||||
@ -554,7 +560,14 @@ impl CPU {
|
|||||||
|
|
||||||
let mask = Status::PS_CARRY | Status::PS_OVERFLOW;
|
let mask = Status::PS_CARRY | Status::PS_OVERFLOW;
|
||||||
|
|
||||||
self.registers.status.set_with_mask( mask, Status::new(StatusArgs { carry: did_carry, overflow: did_overflow, ..StatusArgs::none() }),);
|
self.registers.status.set_with_mask(
|
||||||
|
mask,
|
||||||
|
Status::new(StatusArgs {
|
||||||
|
carry: did_carry,
|
||||||
|
overflow: did_overflow,
|
||||||
|
..StatusArgs::none()
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
self.load_accumulator(result);
|
self.load_accumulator(result);
|
||||||
|
|
||||||
@ -618,10 +631,13 @@ impl CPU {
|
|||||||
|
|
||||||
self.registers.status.set_with_mask(
|
self.registers.status.set_with_mask(
|
||||||
mask,
|
mask,
|
||||||
Status::new(StatusArgs { carry: did_carry, overflow: did_overflow, ..StatusArgs::none() }),
|
Status::new(StatusArgs {
|
||||||
|
carry: did_carry,
|
||||||
|
overflow: did_overflow,
|
||||||
|
..StatusArgs::none()
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
self.load_accumulator(result);
|
self.load_accumulator(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -809,7 +825,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn decimal_subtract_test() {
|
fn decimal_subtract_test() {
|
||||||
let mut cpu = CPU::new();
|
let mut cpu = CPU::new();
|
||||||
cpu.registers.status.or(Status::PS_DECIMAL_MODE | Status::PS_CARRY);
|
cpu.registers
|
||||||
|
.status
|
||||||
|
.or(Status::PS_DECIMAL_MODE | Status::PS_CARRY);
|
||||||
cpu.registers.accumulator = 0;
|
cpu.registers.accumulator = 0;
|
||||||
|
|
||||||
cpu.subtract_with_carry(0x48);
|
cpu.subtract_with_carry(0x48);
|
||||||
|
@ -54,9 +54,17 @@ pub struct Memory {
|
|||||||
bytes: [u8; MEMORY_SIZE],
|
bytes: [u8; MEMORY_SIZE],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Memory {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Memory {
|
impl Memory {
|
||||||
pub fn new() -> Memory {
|
pub fn new() -> Memory {
|
||||||
Memory { bytes: [0; MEMORY_SIZE] }
|
Memory {
|
||||||
|
bytes: [0; MEMORY_SIZE],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_byte(&self, address: Address) -> u8 {
|
pub fn get_byte(&self, address: Address) -> u8 {
|
||||||
@ -101,7 +109,10 @@ mod tests {
|
|||||||
fn test_memory_set_bytes() {
|
fn test_memory_set_bytes() {
|
||||||
let mut memory = Memory::new();
|
let mut memory = Memory::new();
|
||||||
memory.set_bytes(Address(0x0100), &[1, 2, 3, 4, 5]);
|
memory.set_bytes(Address(0x0100), &[1, 2, 3, 4, 5]);
|
||||||
assert_eq!(memory.get_slice(Address(0x00FF), AddressDiff(7)), &[0, 1, 2, 3, 4, 5, 0]);
|
assert_eq!(
|
||||||
|
memory.get_slice(Address(0x00FF), AddressDiff(7)),
|
||||||
|
&[0, 1, 2, 3, 4, 5, 0]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
// POSSIBILITY OF SUCH DAMAGE.
|
// POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
use address::{Address, AddressDiff};
|
use address::{Address, AddressDiff};
|
||||||
use memory::{STACK_ADDRESS_LO, STACK_ADDRESS_HI};
|
use memory::{STACK_ADDRESS_HI, STACK_ADDRESS_LO};
|
||||||
|
|
||||||
// Useful for constructing Status instances
|
// Useful for constructing Status instances
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@ -88,15 +88,18 @@ impl Status {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(StatusArgs { negative,
|
pub fn new(
|
||||||
|
StatusArgs {
|
||||||
|
negative,
|
||||||
overflow,
|
overflow,
|
||||||
unused,
|
unused,
|
||||||
brk,
|
brk,
|
||||||
decimal_mode,
|
decimal_mode,
|
||||||
disable_interrupts,
|
disable_interrupts,
|
||||||
zero,
|
zero,
|
||||||
carry }: StatusArgs) -> Status
|
carry,
|
||||||
{
|
}: StatusArgs,
|
||||||
|
) -> Status {
|
||||||
let mut out = Status::empty();
|
let mut out = Status::empty();
|
||||||
|
|
||||||
if negative {
|
if negative {
|
||||||
@ -169,6 +172,12 @@ pub struct Registers {
|
|||||||
pub status: Status,
|
pub status: Status,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Registers {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Registers {
|
impl Registers {
|
||||||
pub fn new() -> Registers {
|
pub fn new() -> Registers {
|
||||||
// TODO akeeton: Revisit these defaults.
|
// TODO akeeton: Revisit these defaults.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user