Compare commits
No commits in common. "a77d0a0f62aa62032a3bc21c7f37f25e3775d5b1" and "5d6df46a2d6ab4f1457f3d8b288e69250521a5fc" have entirely different histories.
a77d0a0f62
...
5d6df46a2d
52
src/apu.rs
52
src/apu.rs
|
@ -5,7 +5,7 @@ use types::ch1::{Sweep, SweepDirection};
|
|||
use types::ch3::Volume as Ch3Volume;
|
||||
use types::ch4::{CounterWidth, Frequency as Ch4Frequency, PolynomialCounter};
|
||||
use types::common::{EnvelopeDirection, FrequencyHigh, SoundDuty, VolumeEnvelope};
|
||||
use types::{ChannelControl, SoundOutput};
|
||||
use types::{ChannelControl, FrameSequencerState, SoundOutput};
|
||||
|
||||
pub mod gen;
|
||||
mod types;
|
||||
|
@ -26,7 +26,8 @@ pub struct Apu {
|
|||
ch4: Channel4,
|
||||
|
||||
// Frame Sequencer
|
||||
div_prev: Option<u16>,
|
||||
frame_seq_state: FrameSequencerState,
|
||||
div_prev: Option<u8>,
|
||||
|
||||
prod: Option<SampleProducer<f32>>,
|
||||
sample_counter: u64,
|
||||
|
@ -94,30 +95,41 @@ impl BusIo for Apu {
|
|||
|
||||
impl Apu {
|
||||
pub(crate) fn tick(&mut self, div: u16) {
|
||||
use FrameSequencerState::*;
|
||||
self.sample_counter += SAMPLE_INCREMENT;
|
||||
|
||||
// Length Control (256Hz)
|
||||
if self.falling_edge(13, div) {
|
||||
self.handle_length();
|
||||
}
|
||||
// the 5th bit of the high byte
|
||||
let bit_5 = (div >> 13 & 0x01) as u8;
|
||||
|
||||
// Sweep (128Hz)
|
||||
if self.falling_edge(14, div) {
|
||||
self.handle_sweep();
|
||||
}
|
||||
if let Some(0x01) = self.div_prev {
|
||||
if bit_5 == 0x00 {
|
||||
// Falling Edge, step the Frame Sequencer
|
||||
self.frame_seq_state.step();
|
||||
|
||||
// Volume Envelope (64Hz)
|
||||
if self.falling_edge(15, div) {
|
||||
self.handle_volume();
|
||||
match self.frame_seq_state {
|
||||
Step0Length => self.handle_length(),
|
||||
Step2LengthAndSweep => {
|
||||
self.handle_length();
|
||||
self.handle_sweep();
|
||||
}
|
||||
Step4Length => self.handle_length(),
|
||||
Step6LengthAndSweep => {
|
||||
self.handle_length();
|
||||
self.handle_sweep();
|
||||
}
|
||||
Step7VolumeEnvelope => self.handle_volume(),
|
||||
Step1Nothing | Step3Nothing | Step5Nothing => {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
self.div_prev = Some(div);
|
||||
|
||||
self.ch1.clock();
|
||||
self.ch2.clock();
|
||||
self.ch3.clock();
|
||||
self.ch4.clock();
|
||||
|
||||
self.div_prev = Some(bit_5);
|
||||
|
||||
if self.sample_counter >= SM83_CLOCK_SPEED {
|
||||
self.sample_counter %= SM83_CLOCK_SPEED;
|
||||
|
||||
|
@ -164,8 +176,7 @@ impl Apu {
|
|||
|
||||
if self.ctrl.enabled {
|
||||
// Frame Sequencer reset to Step 0
|
||||
// TODO: With the current implementation of the frame sequencer,
|
||||
// what does this even mean?
|
||||
self.frame_seq_state = Default::default();
|
||||
|
||||
// Square Duty units are reset to first step
|
||||
self.ch1.duty_pos = 0;
|
||||
|
@ -327,13 +338,6 @@ impl Apu {
|
|||
&mut self.ch4.current_volume,
|
||||
);
|
||||
}
|
||||
|
||||
fn falling_edge(&self, bit: u8, div: u16) -> bool {
|
||||
match self.div_prev {
|
||||
Some(p) => (p >> bit & 0x01) == 0x01 && (div >> bit & 0x01) == 0x00,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
|
|
|
@ -427,6 +427,41 @@ pub(crate) mod common {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) enum FrameSequencerState {
|
||||
Step0Length,
|
||||
Step1Nothing,
|
||||
Step2LengthAndSweep,
|
||||
Step3Nothing,
|
||||
Step4Length,
|
||||
Step5Nothing,
|
||||
Step6LengthAndSweep,
|
||||
Step7VolumeEnvelope,
|
||||
}
|
||||
|
||||
impl FrameSequencerState {
|
||||
pub(crate) fn step(&mut self) {
|
||||
use FrameSequencerState::*;
|
||||
|
||||
*self = match *self {
|
||||
Step0Length => Step1Nothing,
|
||||
Step1Nothing => Step2LengthAndSweep,
|
||||
Step2LengthAndSweep => Step3Nothing,
|
||||
Step3Nothing => Step4Length,
|
||||
Step4Length => Step5Nothing,
|
||||
Step5Nothing => Step6LengthAndSweep,
|
||||
Step6LengthAndSweep => Step7VolumeEnvelope,
|
||||
Step7VolumeEnvelope => Step0Length,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for FrameSequencerState {
|
||||
fn default() -> Self {
|
||||
Self::Step0Length
|
||||
}
|
||||
}
|
||||
|
||||
bitfield! {
|
||||
pub struct SoundOutput(u8);
|
||||
impl Debug;
|
||||
|
|
|
@ -104,10 +104,10 @@ impl Cpu {
|
|||
Instruction::execute(self, instruction)
|
||||
}
|
||||
|
||||
/// Perform the [`Cpu::fetch()`] [`Cpu::decode()`] [`Cpu::execute()`]
|
||||
/// Perform the [`Cpu::fetch()`] [`Cpu::decode(opcode)`] [`Cpu::execute(instr)`]
|
||||
/// routine.
|
||||
///
|
||||
/// Handle HALT and interrupts.
|
||||
/// Handle HALT state and interrupts.
|
||||
pub fn step(&mut self) -> Cycle {
|
||||
// Log instructions
|
||||
// if self.reg.pc > 0xFF {
|
||||
|
|
|
@ -1531,8 +1531,7 @@ impl Instruction {
|
|||
|
||||
/// Set program counter to Address.
|
||||
///
|
||||
/// This is explicitly meant to emulate the exact behaviour of [`Instruction::JP`], [`Instruction::JR`]
|
||||
/// [`Instruction::RET`], [`Instruction::RETI`]
|
||||
/// This is explicitly meant to emulate the exact behaviour of JP, JR RET, RETI and CALL
|
||||
/// (4 cycles)
|
||||
fn jump(cpu: &mut Cpu, addr: u16) {
|
||||
cpu.set_register_pair(RegisterPair::PC, addr);
|
||||
|
|
|
@ -75,7 +75,7 @@ fn main() -> Result<()> {
|
|||
});
|
||||
|
||||
let mut start = Instant::now();
|
||||
let frame_time = Duration::from_secs_f64(1.0 / 59.73); // 59.73 Hz on Host
|
||||
let frame_time = Duration::from_secs_f64(1.0 / 60.0); // 60 Hz on Host
|
||||
let mut cycle_count: Cycle = Default::default();
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
|
|
139
src/ppu.rs
139
src/ppu.rs
|
@ -231,23 +231,20 @@ impl Ppu {
|
|||
fn draw(&mut self, _cycle: u32) {
|
||||
use FetcherState::*;
|
||||
|
||||
let mut iter = self.obj_buffer.iter_mut();
|
||||
let default = &mut None;
|
||||
let iter = &mut self.obj_buffer.iter();
|
||||
|
||||
let obj_attr = loop {
|
||||
match iter.next() {
|
||||
Some(attr_opt) => {
|
||||
if let Some(attr) = attr_opt {
|
||||
if attr.x <= (self.x_pos + 8) {
|
||||
self.fetch.back.reset();
|
||||
self.fetch.back.pause();
|
||||
self.fifo.pause();
|
||||
match iter.flatten().next() {
|
||||
Some(attr) => {
|
||||
if attr.x <= (self.x_pos + 8) {
|
||||
self.fetch.back.reset();
|
||||
self.fetch.back.pause();
|
||||
self.fifo.pause();
|
||||
|
||||
break attr_opt;
|
||||
}
|
||||
break Some(*attr);
|
||||
}
|
||||
}
|
||||
None => break default,
|
||||
None => break None,
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -255,10 +252,10 @@ impl Ppu {
|
|||
match self.fetch.obj.state {
|
||||
TileNumber => {
|
||||
self.fetch.obj.tile.with_id(attr.tile_index);
|
||||
self.fetch.obj.next(SleepOne);
|
||||
self.fetch.obj.next(ToLowByteSleep);
|
||||
}
|
||||
SleepOne => self.fetch.obj.next(TileLow),
|
||||
TileLow => {
|
||||
ToLowByteSleep => self.fetch.obj.next(TileLowByte),
|
||||
TileLowByte => {
|
||||
let obj_size = self.ctrl.obj_size();
|
||||
|
||||
let addr = PixelFetcher::get_obj_addr(&attr, &self.pos, obj_size);
|
||||
|
@ -266,10 +263,10 @@ impl Ppu {
|
|||
let byte = self.read_byte(addr);
|
||||
self.fetch.obj.tile.with_low_byte(byte);
|
||||
|
||||
self.fetch.obj.next(SleepTwo);
|
||||
self.fetch.obj.next(ToHighByteSleep);
|
||||
}
|
||||
SleepTwo => self.fetch.obj.next(TileHigh),
|
||||
TileHigh => {
|
||||
ToHighByteSleep => self.fetch.obj.next(TileHighByte),
|
||||
TileHighByte => {
|
||||
let obj_size = self.ctrl.obj_size();
|
||||
|
||||
let addr = PixelFetcher::get_obj_addr(&attr, &self.pos, obj_size);
|
||||
|
@ -277,10 +274,10 @@ impl Ppu {
|
|||
let byte = self.read_byte(addr + 1);
|
||||
self.fetch.obj.tile.with_high_byte(byte);
|
||||
|
||||
self.fetch.obj.next(SleepThree);
|
||||
self.fetch.obj.next(ToFifoSleep);
|
||||
}
|
||||
SleepThree => self.fetch.obj.next(ToFifoOne),
|
||||
ToFifoOne => {
|
||||
ToFifoSleep => self.fetch.obj.next(SendToFifoOne),
|
||||
SendToFifoOne => {
|
||||
// Load into Fifo
|
||||
let (high, low) = self
|
||||
.fetch
|
||||
|
@ -292,12 +289,13 @@ impl Ppu {
|
|||
let tbpp = Pixels::from_bytes(high, low);
|
||||
|
||||
let palette_kind = attr.flags.palette();
|
||||
|
||||
let end = Pixels::PIXEL_COUNT - self.fifo.obj.len();
|
||||
let start = Pixels::PIXEL_COUNT - end;
|
||||
|
||||
let x_flip = attr.flags.x_flip();
|
||||
|
||||
let pixel_count = (attr.x - self.x_pos) as usize;
|
||||
let start = self.fifo.obj.len();
|
||||
|
||||
for i in start..pixel_count {
|
||||
for i in start..Pixels::PIXEL_COUNT {
|
||||
let x = if x_flip { 7 - i } else { i };
|
||||
|
||||
let priority = attr.flags.priority();
|
||||
|
@ -314,11 +312,11 @@ impl Ppu {
|
|||
|
||||
self.fetch.back.resume();
|
||||
self.fifo.resume();
|
||||
let _ = std::mem::take(obj_attr);
|
||||
self.obj_buffer.remove(&attr);
|
||||
|
||||
self.fetch.obj.next(ToFifoTwo);
|
||||
self.fetch.obj.next(SendToFifoTwo);
|
||||
}
|
||||
ToFifoTwo => self.fetch.obj.reset(),
|
||||
SendToFifoTwo => self.fetch.obj.reset(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -348,31 +346,31 @@ impl Ppu {
|
|||
self.fetch.back.tile.with_id(id);
|
||||
|
||||
// Move on to the Next state in 2 T-cycles
|
||||
self.fetch.back.next(SleepOne);
|
||||
self.fetch.back.next(ToLowByteSleep);
|
||||
}
|
||||
SleepOne => self.fetch.back.next(TileLow),
|
||||
TileLow => {
|
||||
ToLowByteSleep => self.fetch.back.next(TileLowByte),
|
||||
TileLowByte => {
|
||||
let addr = self.fetch.bg_byte_addr(&self.ctrl, &self.pos);
|
||||
|
||||
let low = self.read_byte(addr);
|
||||
self.fetch.back.tile.with_low_byte(low);
|
||||
|
||||
self.fetch.back.next(SleepTwo);
|
||||
self.fetch.back.next(ToHighByteSleep);
|
||||
}
|
||||
SleepTwo => self.fetch.back.next(TileHigh),
|
||||
TileHigh => {
|
||||
ToHighByteSleep => self.fetch.back.next(TileHighByte),
|
||||
TileHighByte => {
|
||||
let addr = self.fetch.bg_byte_addr(&self.ctrl, &self.pos);
|
||||
|
||||
let high = self.read_byte(addr + 1);
|
||||
self.fetch.back.tile.with_high_byte(high);
|
||||
|
||||
self.fetch.back.next(SleepThree);
|
||||
self.fetch.back.next(ToFifoSleep);
|
||||
}
|
||||
SleepThree => self.fetch.back.next(ToFifoOne),
|
||||
ToFifoOne => {
|
||||
self.fetch.back.next(ToFifoTwo);
|
||||
ToFifoSleep => self.fetch.back.next(SendToFifoOne),
|
||||
SendToFifoOne => {
|
||||
self.fetch.back.next(SendToFifoTwo);
|
||||
}
|
||||
ToFifoTwo => {
|
||||
SendToFifoTwo => {
|
||||
if let Ok(()) = self.fetch.send_to_fifo(&mut self.fifo) {
|
||||
self.fetch.x_pos += 1;
|
||||
self.fetch.back.next(TileNumber);
|
||||
|
@ -383,12 +381,12 @@ impl Ppu {
|
|||
}
|
||||
|
||||
if self.fifo.is_enabled() {
|
||||
if self.x_pos == 0 && self.scanline_start {
|
||||
if self.x_pos == 0 && !self.fifo.back.is_empty() && self.scanline_start {
|
||||
self.to_discard = self.pos.scroll_x % 8;
|
||||
self.scanline_start = false;
|
||||
}
|
||||
|
||||
if self.to_discard > 0 && !self.fifo.back.is_empty() {
|
||||
if self.to_discard > 0 {
|
||||
let _ = self.fifo.back.pop_front();
|
||||
self.to_discard -= 1;
|
||||
|
||||
|
@ -610,34 +608,67 @@ impl<'a> From<&'a [u8; 4]> for ObjectAttribute {
|
|||
|
||||
#[derive(Debug)]
|
||||
struct ObjectBuffer {
|
||||
inner: [Option<ObjectAttribute>; OBJECT_LIMIT],
|
||||
buf: [Option<ObjectAttribute>; OBJECT_LIMIT],
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl ObjectBuffer {
|
||||
fn iter(&self) -> std::slice::Iter<'_, Option<ObjectAttribute>> {
|
||||
self.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a ObjectBuffer {
|
||||
type Item = &'a Option<ObjectAttribute>;
|
||||
|
||||
type IntoIter = std::slice::Iter<'a, Option<ObjectAttribute>>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.buf.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a mut ObjectBuffer {
|
||||
type Item = &'a Option<ObjectAttribute>;
|
||||
|
||||
type IntoIter = std::slice::Iter<'a, Option<ObjectAttribute>>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.buf.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectBuffer {
|
||||
fn is_full(&self) -> bool {
|
||||
self.len == OBJECT_LIMIT
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.inner = [Default::default(); 10];
|
||||
self.buf = [Default::default(); 10];
|
||||
self.len = 0;
|
||||
}
|
||||
|
||||
fn add(&mut self, attr: ObjectAttribute) {
|
||||
self.inner[self.len] = Some(attr);
|
||||
self.buf[self.len] = Some(attr);
|
||||
self.len += 1;
|
||||
}
|
||||
|
||||
fn iter_mut(&mut self) -> std::slice::IterMut<'_, Option<ObjectAttribute>> {
|
||||
self.inner.iter_mut()
|
||||
fn remove(&mut self, attr: &ObjectAttribute) {
|
||||
let maybe_index = self.buf.iter().position(|maybe_attr| match maybe_attr {
|
||||
Some(other_attr) => attr == other_attr,
|
||||
None => false,
|
||||
});
|
||||
|
||||
if let Some(i) = maybe_index {
|
||||
self.buf[i] = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ObjectBuffer {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inner: [Default::default(); OBJECT_LIMIT],
|
||||
buf: [Default::default(); OBJECT_LIMIT],
|
||||
len: Default::default(),
|
||||
}
|
||||
}
|
||||
|
@ -872,13 +903,13 @@ impl WindowLineCounter {
|
|||
#[derive(Debug, Clone, Copy)]
|
||||
enum FetcherState {
|
||||
TileNumber,
|
||||
SleepOne,
|
||||
TileLow,
|
||||
SleepTwo,
|
||||
TileHigh,
|
||||
SleepThree,
|
||||
ToFifoOne,
|
||||
ToFifoTwo,
|
||||
ToLowByteSleep,
|
||||
TileLowByte,
|
||||
ToHighByteSleep,
|
||||
TileHighByte,
|
||||
ToFifoSleep,
|
||||
SendToFifoOne,
|
||||
SendToFifoTwo,
|
||||
}
|
||||
|
||||
impl Default for FetcherState {
|
||||
|
|
Loading…
Reference in New Issue