1
0
mirror of https://github.com/mre/mos6502.git synced 2024-11-25 02:33:26 +00:00

Add Default impls

This commit is contained in:
Matthias Endler 2021-01-28 23:34:41 +01:00
parent 0749b1798b
commit 93a39980e4
3 changed files with 147 additions and 109 deletions

View File

@ -37,6 +37,12 @@ pub struct CPU {
pub memory: Memory,
}
impl Default for CPU {
fn default() -> Self {
Self::new()
}
}
impl CPU {
pub fn new() -> CPU {
CPU {
@ -554,7 +560,14 @@ impl CPU {
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);
@ -618,10 +631,13 @@ impl CPU {
self.registers.status.set_with_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);
}
@ -809,7 +825,9 @@ mod tests {
#[test]
fn decimal_subtract_test() {
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.subtract_with_carry(0x48);

View File

@ -54,9 +54,17 @@ pub struct Memory {
bytes: [u8; MEMORY_SIZE],
}
impl Default for Memory {
fn default() -> Self {
Self::new()
}
}
impl Memory {
pub fn new() -> Memory {
Memory { bytes: [0; MEMORY_SIZE] }
Memory {
bytes: [0; MEMORY_SIZE],
}
}
pub fn get_byte(&self, address: Address) -> u8 {
@ -101,7 +109,10 @@ mod tests {
fn test_memory_set_bytes() {
let mut memory = Memory::new();
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]

View File

@ -26,7 +26,7 @@
// POSSIBILITY OF SUCH DAMAGE.
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
#[derive(Copy, Clone)]
@ -88,15 +88,18 @@ impl Status {
})
}
pub fn new(StatusArgs { negative,
pub fn new(
StatusArgs {
negative,
overflow,
unused,
brk,
decimal_mode,
disable_interrupts,
zero,
carry }: StatusArgs) -> Status
{
carry,
}: StatusArgs,
) -> Status {
let mut out = Status::empty();
if negative {
@ -169,6 +172,12 @@ pub struct Registers {
pub status: Status,
}
impl Default for Registers {
fn default() -> Self {
Self::new()
}
}
impl Registers {
pub fn new() -> Registers {
// TODO akeeton: Revisit these defaults.