diff --git a/src/nes.rs b/src/nes.rs index 537ed49..b390640 100644 --- a/src/nes.rs +++ b/src/nes.rs @@ -32,19 +32,24 @@ impl NES { } } + // Insert a cartridge into the NES. This inserts the cartridge memory + // into the NES address range pub fn insert_cartridge(&mut self, cartridge: Cartridge) { self.memory.insert_cartridge(cartridge); } + // Initializes the NES CPU programm pointer pub fn start(&mut self) { self.cpu.find_pc_addr(&self.memory); } + // Reset the CPU pub fn reset(&mut self) { self.clock_count = 0; self.cpu.reset(&self.memory); } + // A single clock on the NES pub fn clock(&mut self) { self.clock_count += 1; if self.clock_count % 3 == 0 { diff --git a/src/nes/cpu.rs b/src/nes/cpu.rs index 617ddfe..ccc0079 100644 --- a/src/nes/cpu.rs +++ b/src/nes/cpu.rs @@ -1,12 +1,29 @@ use crate::nes::{Memory,MemoryReader}; use crate::nes::types::*; - -pub mod instructions; - use instructions::{Instruction,Operation,AddrMode}; use core::fmt::{Debug,Formatter,Result}; use log::{debug}; +pub mod instructions; + +// Base address of the stack in memory +pub const STACK_BASE_ADDR: Addr = 0x0100; + +// constants to extract LO and HI from a value. +pub const LO: Addr = 0x00FF; +pub const HI: Addr = 0xFF00; + +// CPU flags +pub const CARRY: Byte = 1 << 0; // 0000 0001 0x01 +pub const ZERO: Byte = 1 << 1; // 0000 0010 0x02 +pub const IRQ: Byte = 1 << 2; // 0000 0100 0x04 +pub const DECIMAL: Byte = 1 << 3; // 0000 1000 0x08 +pub const BREAK: Byte = 1 << 4; // 0001 0000 0x10 +pub const UNUSED: Byte = 1 << 5; // 0010 0000 0x20 +pub const OVERFLOW: Byte = 1 << 6; // 0100 0000 0x40 +pub const NEGATIVE: Byte = 1 << 7; // 1000 0000 0x80 + +// The NES CPU registers pub struct Registers { pub a: Byte, pub x: Byte, @@ -18,7 +35,6 @@ pub struct Registers { impl Registers { pub fn new() -> Registers { - // TODO true initial state of registers before reset? Registers { a: 0, x: 0, @@ -37,19 +53,6 @@ impl Debug for Registers { } } -pub const STACK_BASE_ADDR: Addr = 0x0100; -pub const LO: Addr = 0x00FF; -pub const HI: Addr = 0xFF00; - -pub const CARRY: Byte = 1 << 0; // 0000 0001 0x01 -pub const ZERO: Byte = 1 << 1; // 0000 0010 0x02 -pub const IRQ: Byte = 1 << 2; // 0000 0100 0x04 -pub const DECIMAL: Byte = 1 << 3; // 0000 1000 0x08 -pub const BREAK: Byte = 1 << 4; // 0001 0000 0x10 -pub const UNUSED: Byte = 1 << 5; // 0010 0000 0x20 -pub const OVERFLOW: Byte = 1 << 6; // 0100 0000 0x40 -pub const NEGATIVE: Byte = 1 << 7; // 1000 0000 0x80 - pub struct CPU { pub regs: Registers, curr_op: Byte, // current operation diff --git a/src/nes/memory.rs b/src/nes/memory.rs index ed99e46..1d1ccc3 100644 --- a/src/nes/memory.rs +++ b/src/nes/memory.rs @@ -1,4 +1,3 @@ -// Interface for devices that contain memory that can be accessed by CPU use crate::nes::cartridge::Cartridge; use crate::nes::types::*; @@ -9,6 +8,7 @@ const PPU_ADDR_RANGE: [Addr; 2] = [0x2000, 0x3fff]; const PPU_PHYS_RANGE: [Addr; 2] = [0x2000, 0x2007]; const CART_ADDR_RANGE: [Addr; 2] = [0x4020, 0xffff]; +// NES memory: Contains data from RAM, cartridge... pub struct NESMemory { ram: [Byte; RAM_SIZE], // 2kb cartridge: Option,