This commit is contained in:
Daniel Bauer
2020-01-04 23:19:17 +01:00
parent 6ed5f0339a
commit 47043b42a2
3 changed files with 26 additions and 18 deletions

View File

@@ -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) { pub fn insert_cartridge(&mut self, cartridge: Cartridge) {
self.memory.insert_cartridge(cartridge); self.memory.insert_cartridge(cartridge);
} }
// Initializes the NES CPU programm pointer
pub fn start(&mut self) { pub fn start(&mut self) {
self.cpu.find_pc_addr(&self.memory); self.cpu.find_pc_addr(&self.memory);
} }
// Reset the CPU
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.clock_count = 0; self.clock_count = 0;
self.cpu.reset(&self.memory); self.cpu.reset(&self.memory);
} }
// A single clock on the NES
pub fn clock(&mut self) { pub fn clock(&mut self) {
self.clock_count += 1; self.clock_count += 1;
if self.clock_count % 3 == 0 { if self.clock_count % 3 == 0 {

View File

@@ -1,12 +1,29 @@
use crate::nes::{Memory,MemoryReader}; use crate::nes::{Memory,MemoryReader};
use crate::nes::types::*; use crate::nes::types::*;
pub mod instructions;
use instructions::{Instruction,Operation,AddrMode}; use instructions::{Instruction,Operation,AddrMode};
use core::fmt::{Debug,Formatter,Result}; use core::fmt::{Debug,Formatter,Result};
use log::{debug}; 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 struct Registers {
pub a: Byte, pub a: Byte,
pub x: Byte, pub x: Byte,
@@ -18,7 +35,6 @@ pub struct Registers {
impl Registers { impl Registers {
pub fn new() -> Registers { pub fn new() -> Registers {
// TODO true initial state of registers before reset?
Registers { Registers {
a: 0, a: 0,
x: 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 struct CPU {
pub regs: Registers, pub regs: Registers,
curr_op: Byte, // current operation curr_op: Byte, // current operation

View File

@@ -1,4 +1,3 @@
// Interface for devices that contain memory that can be accessed by CPU
use crate::nes::cartridge::Cartridge; use crate::nes::cartridge::Cartridge;
use crate::nes::types::*; 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 PPU_PHYS_RANGE: [Addr; 2] = [0x2000, 0x2007];
const CART_ADDR_RANGE: [Addr; 2] = [0x4020, 0xffff]; const CART_ADDR_RANGE: [Addr; 2] = [0x4020, 0xffff];
// NES memory: Contains data from RAM, cartridge...
pub struct NESMemory { pub struct NESMemory {
ram: [Byte; RAM_SIZE], // 2kb ram: [Byte; RAM_SIZE], // 2kb
cartridge: Option<Cartridge>, cartridge: Option<Cartridge>,