Skip to content

Commit

Permalink
Run and clock the APU, add verbose output + colors
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Nov 26, 2023
1 parent 2a69b66 commit eadd734
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
66 changes: 65 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default-run = "siena"
[dependencies]
anyhow = { version = "1.0.75", features = ["backtrace"] }
clap = { version = "4.4.6", features = ["derive"] }
colored = "2.0.4"
dbg_hex = "0.1.1"
hex-literal = "0.4.1"
itertools = "0.11.0"
Expand Down
5 changes: 3 additions & 2 deletions src/bin/siena/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::{stdin, Read};

use anyhow::Result;
use clap::Parser;
use colored::*;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;

Expand Down Expand Up @@ -79,15 +80,15 @@ fn main() -> Result<()> {
let eventpump = SDLEventPump::new();
let cart = Cartridge::load(&f);
println!("Cartridge: {}", &cart);
let bus = Mainbus::<SDLRenderer>::new(cart, args.bustrace, display, joypads);
let bus = Mainbus::<SDLRenderer>::new(cart, args.bustrace, display, joypads, args.verbose);

let reset = bus.read16(0xFFFC);
let mut cpu = Cpu65816::<Mainbus<SDLRenderer>>::new(bus, reset);

let mut eventpoll = 0;
'mainloop: loop {
if args.verbose {
println!("{}", cpu.dump_state());
println!("{}", cpu.dump_state().green());
}

if args.pause {
Expand Down
21 changes: 20 additions & 1 deletion src/snes/apu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod apubus;

use anyhow::Result;
use colored::*;

use crate::snes::bus::{Address, BusMember};
use crate::snes::cpu_spc700::cpu::{CpuSpc700, SpcAddress};
use crate::tickable::{Tickable, Ticks};

Expand All @@ -17,6 +19,9 @@ pub struct Apu {

/// Master ticks received that we can spend.
spc_master_credit: Ticks,

/// Print instructions
verbose: bool,
}

impl Apu {
Expand All @@ -34,11 +39,12 @@ impl Apu {
0x00, 0x00, 0xC0, 0xFF,
];

pub fn new() -> Self {
pub fn new(verbose: bool) -> Self {
Self {
cpu: CpuSpc700::<Apubus>::new(Apubus::new(&Self::IPL_BIN), Self::IPL_ENTRYPOINT),
spc_cycles_taken: 0,
spc_master_credit: 0,
verbose,
}
}
}
Expand All @@ -52,6 +58,9 @@ impl Tickable for Apu {

while self.spc_master_credit >= Self::SPC_MASTER_FACTOR {
if self.spc_cycles_taken == 0 {
if self.verbose {
println!("{}", self.cpu.dump_state().red());
}
self.spc_cycles_taken += self.cpu.step()?;
}
self.spc_cycles_taken -= 1;
Expand All @@ -61,3 +70,13 @@ impl Tickable for Apu {
Ok(())
}
}

impl BusMember<Address> for Apu {
fn read(&self, fulladdr: Address) -> Option<u8> {
None
}

fn write(&mut self, fulladdr: Address, val: u8) -> Option<()> {
None
}
}
7 changes: 7 additions & 0 deletions src/snes/bus/mainbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use dbg_hex::dbg_hex;

use crate::frontend::Renderer;
use crate::snes::apu::Apu;
use crate::snes::bus::{Address, Bus, BusMember, ADDRESS_MASK};
use crate::snes::cartridge::Cartridge;
use crate::snes::joypad::{Joypad, JOYPAD_COUNT};
Expand Down Expand Up @@ -36,6 +37,8 @@ where
/// Controllers
joypads: [Joypad; JOYPAD_COUNT],

pub apu: Apu,

/// Picture Processing Unit
pub ppu: PPU<TRenderer>,

Expand Down Expand Up @@ -225,6 +228,7 @@ where
trace: BusTrace,
renderer: TRenderer,
joypads: [Joypad; JOYPAD_COUNT],
apu_verbose: bool,
) -> Self {
Self {
cartridge,
Expand All @@ -235,6 +239,7 @@ where
joypads,

ppu: PPU::<TRenderer>::new(renderer),
apu: Apu::new(apu_verbose),

memsel: 0,
wmadd: Cell::new(0),
Expand Down Expand Up @@ -749,6 +754,8 @@ where
TRenderer: Renderer,
{
fn tick(&mut self, ticks: Ticks) -> Result<()> {
self.apu.tick(ticks)?;

// This ratio is not based on anything that makes sense yet
for _ in 0..(ticks * 8) {
self.ppu.tick(1)?;
Expand Down

0 comments on commit eadd734

Please sign in to comment.