2022-08-23 17:01:26 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::{Rc, Weak};
|
2022-08-23 20:11:25 +00:00
|
|
|
use copystr::s3;
|
2022-08-23 17:01:26 +00:00
|
|
|
|
|
|
|
use crate::bus::Bus;
|
|
|
|
|
2022-08-23 20:11:25 +00:00
|
|
|
type InstrFn = fn(&mut CPU);
|
|
|
|
type AddrFn = fn(&mut CPU);
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Instruction
|
|
|
|
{
|
|
|
|
action: InstrFn,
|
|
|
|
addressing: AddrFn,
|
|
|
|
cycles: u8,
|
|
|
|
length: u8,
|
|
|
|
|
|
|
|
name: s3
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! instr
|
|
|
|
{
|
|
|
|
($instr: ident, $addr: ident, $cyc: literal, $len: literal) =>
|
|
|
|
{
|
|
|
|
Instruction
|
|
|
|
{
|
|
|
|
action: CPU::$instr,
|
|
|
|
addressing: CPU::$addr,
|
|
|
|
cycles: $cyc,
|
|
|
|
length: $len,
|
|
|
|
|
|
|
|
name: s3::new(stringify!($instr)).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 17:01:26 +00:00
|
|
|
pub struct CPU
|
|
|
|
{
|
2022-08-23 20:11:25 +00:00
|
|
|
pub cycle: u8,
|
|
|
|
total_cycles: u64,
|
|
|
|
pub absolute_addr: u16,
|
|
|
|
pub relative_addr: u8,
|
2022-08-23 17:01:26 +00:00
|
|
|
|
2022-08-23 20:11:25 +00:00
|
|
|
pub acc: u8,
|
|
|
|
pub x: u8,
|
|
|
|
pub y: u8,
|
|
|
|
pub p: u8,
|
|
|
|
pub sp: u8,
|
|
|
|
pub pc: u16,
|
2022-08-23 17:01:26 +00:00
|
|
|
|
2022-08-23 20:11:25 +00:00
|
|
|
pub bus: Weak<RefCell<Bus>>,
|
|
|
|
|
|
|
|
instruction_set: [Option<Instruction>; 256]
|
2022-08-23 17:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CPU
|
|
|
|
{
|
|
|
|
pub fn new(bus: &Rc<RefCell<Bus>>) -> CPU
|
|
|
|
{
|
2022-08-23 20:11:25 +00:00
|
|
|
const UNDEF_INSTR: Option<Instruction> = None;
|
|
|
|
let mut instr_set = [UNDEF_INSTR; 256];
|
|
|
|
|
|
|
|
instr_set[0x4C] = Option::Some(instr!(jmp, abs, 3, 3));
|
2022-08-23 21:12:31 +00:00
|
|
|
instr_set[0x86] = Option::Some(instr!(stx, zpg, 3, 2));
|
2022-08-23 20:11:25 +00:00
|
|
|
instr_set[0xA2] = Option::Some(instr!(ldx, imm, 2, 2));
|
|
|
|
|
2022-08-23 17:01:26 +00:00
|
|
|
CPU {
|
2022-08-23 20:11:25 +00:00
|
|
|
cycle: 0,
|
|
|
|
total_cycles: 0,
|
|
|
|
absolute_addr: 0,
|
|
|
|
relative_addr: 0,
|
|
|
|
|
2022-08-23 17:01:26 +00:00
|
|
|
acc: 0,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
p: 0,
|
|
|
|
sp: 0,
|
|
|
|
|
|
|
|
pc: 0,
|
|
|
|
|
2022-08-23 20:11:25 +00:00
|
|
|
bus: Rc::downgrade(bus),
|
|
|
|
|
|
|
|
instruction_set: instr_set
|
2022-08-23 17:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn powerup(&mut self)
|
|
|
|
{
|
|
|
|
self.p = 0x34;
|
|
|
|
|
|
|
|
self.acc = 0;
|
|
|
|
self.x = 0;
|
|
|
|
self.y = 0;
|
|
|
|
self.sp = 0xFD;
|
2022-08-23 17:40:48 +00:00
|
|
|
|
2022-08-23 20:11:25 +00:00
|
|
|
self.total_cycles = 0;
|
|
|
|
self.cycle = 6;
|
|
|
|
|
2022-08-23 17:40:48 +00:00
|
|
|
// TODO: This is just for the nestest.nes
|
|
|
|
self.pc = 0xC000;
|
2022-08-23 17:01:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:11:25 +00:00
|
|
|
pub fn cycle(&mut self)
|
|
|
|
{
|
|
|
|
self.total_cycles += 1;
|
|
|
|
|
|
|
|
if self.cycle > 0
|
|
|
|
{
|
|
|
|
self.cycle -= 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.execute();
|
2022-08-23 20:21:28 +00:00
|
|
|
|
|
|
|
self.cycle -= 1;
|
2022-08-23 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(&mut self)
|
2022-08-23 17:01:26 +00:00
|
|
|
{
|
|
|
|
let bus = self.bus.upgrade().unwrap();
|
|
|
|
let opcode: u8 = bus.borrow().read_cpu(self.pc);
|
2022-08-23 20:11:25 +00:00
|
|
|
let instr = self.instruction_set[opcode as usize].expect(&format!("Unimplemented opcode {:02X}", opcode));
|
2022-08-23 17:01:26 +00:00
|
|
|
|
2022-08-23 20:11:25 +00:00
|
|
|
print!("{:04X} ", self.pc);
|
|
|
|
for byte in 0..3
|
2022-08-23 17:01:26 +00:00
|
|
|
{
|
2022-08-23 20:11:25 +00:00
|
|
|
if byte < instr.length
|
|
|
|
{
|
|
|
|
print!("{:02X} ", bus.borrow().read_cpu(self.pc + byte as u16));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print!(" ");
|
|
|
|
}
|
2022-08-23 17:01:26 +00:00
|
|
|
}
|
2022-08-23 20:11:25 +00:00
|
|
|
|
|
|
|
print!(" {} ", instr.name.to_string().to_uppercase());
|
|
|
|
|
|
|
|
self.pc += 1;
|
|
|
|
|
|
|
|
(instr.addressing)(self);
|
|
|
|
(instr.action)(self);
|
|
|
|
|
|
|
|
self.cycle += instr.cycles;
|
|
|
|
|
|
|
|
println!("A:{:02X} X:{:02X} Y:{:02X} P:{:02X} SP:{:02X} CYC:{}", self.acc, self.x, self.y, self.p, self.sp, self.total_cycles);
|
2022-08-23 17:01:26 +00:00
|
|
|
}
|
|
|
|
}
|