Major progress on APU pulse channels & organization
Some checks failed
Cargo Build & Test / Rust project - latest (stable) (push) Failing after 9s
Some checks failed
Cargo Build & Test / Rust project - latest (stable) (push) Failing after 9s
This commit is contained in:
419
src/apu.rs
419
src/apu.rs
@@ -6,7 +6,64 @@ use iced::{
|
||||
};
|
||||
use tracing::debug;
|
||||
|
||||
pub enum None {}
|
||||
macro_rules! lut {
|
||||
($name:ident: [$ty:ty; $len:expr] = |$n:ident| $expr:expr) => {
|
||||
const $name: [$ty; $len] = {
|
||||
let mut table = [0; $len];
|
||||
let mut $n = 0;
|
||||
while $n < $len {
|
||||
table[$n] = $expr;
|
||||
$n += 1;
|
||||
}
|
||||
table
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct LengthTimerHigh(u8);
|
||||
impl Debug;
|
||||
length, set_length: 7, 3;
|
||||
u16;
|
||||
timer_high, set_timer_high: 2, 0;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct LengthCounter {
|
||||
current: u16,
|
||||
silenced: bool,
|
||||
}
|
||||
|
||||
impl LengthCounter {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current: 0,
|
||||
silenced: true,
|
||||
}
|
||||
}
|
||||
pub fn clock(&mut self) {
|
||||
if self.current == 0 {
|
||||
self.silenced = true;
|
||||
} else {
|
||||
self.current -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&mut self, length: u8) {
|
||||
// self.reg.0 = val;
|
||||
self.silenced = false;
|
||||
const LENGTH_LUT: [u16; 0x20] = [
|
||||
10, 254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14, 12, 16, 24, 18, 48, 20,
|
||||
96, 22, 192, 24, 72, 26, 16, 28, 32, 3,
|
||||
];
|
||||
self.current = LENGTH_LUT[length as usize] + 1; // I think?
|
||||
}
|
||||
|
||||
pub fn silenced(&self) -> bool {
|
||||
self.silenced
|
||||
}
|
||||
}
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
@@ -28,26 +85,23 @@ bitfield::bitfield! {
|
||||
shift, set_shift: 2, 0;
|
||||
}
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct LengthTimerHigh(u8);
|
||||
impl Debug;
|
||||
length, set_length: 7, 3;
|
||||
timer_high, set_timer_high: 2, 0;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct PulseChannel {
|
||||
enabled: bool,
|
||||
duty_vol: DutyVol,
|
||||
sweep: Sweep,
|
||||
timer_low: u8,
|
||||
length_timer_high: LengthTimerHigh,
|
||||
sweep_reload: bool,
|
||||
|
||||
cur_time: u16,
|
||||
cur_length: u8,
|
||||
counter: LengthCounter,
|
||||
|
||||
period: u16,
|
||||
period_timer: u16,
|
||||
cur: u8,
|
||||
sample: u8,
|
||||
|
||||
envelope_start: bool,
|
||||
envelope_counter: u8,
|
||||
envelope_divider: u8,
|
||||
}
|
||||
|
||||
impl PulseChannel {
|
||||
@@ -56,33 +110,55 @@ impl PulseChannel {
|
||||
enabled: false,
|
||||
duty_vol: DutyVol(0),
|
||||
sweep: Sweep(0),
|
||||
timer_low: 0,
|
||||
length_timer_high: LengthTimerHigh(0),
|
||||
cur_time: 0,
|
||||
cur_length: 0,
|
||||
sweep_reload: false,
|
||||
counter: LengthCounter::new(),
|
||||
period: 0,
|
||||
period_timer: 0,
|
||||
cur: 0,
|
||||
sample: 0,
|
||||
envelope_start: false,
|
||||
envelope_counter: 0,
|
||||
envelope_divider: 0,
|
||||
}
|
||||
}
|
||||
fn use_envelope(&self) -> bool {
|
||||
!self.duty_vol.const_vol()
|
||||
}
|
||||
fn volume(&self) -> u8 {
|
||||
self.duty_vol.volume()
|
||||
}
|
||||
fn timer(&self) -> u16 {
|
||||
self.timer_low as u16 | ((self.length_timer_high.timer_high() as u16) << 8)
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
// TODO
|
||||
self.cur_time = self.timer();
|
||||
self.cur_length = self.length_timer_high.length();
|
||||
self.cur = 0;
|
||||
pub fn write(&mut self, offset: u16, val: u8) {
|
||||
match offset {
|
||||
0x00 => {
|
||||
self.duty_vol.0 = val;
|
||||
self.envelope_start = true;
|
||||
}
|
||||
0x01 => {
|
||||
self.sweep.0 = val;
|
||||
self.sweep_reload = true;
|
||||
},
|
||||
0x02 => self.period = self.period & 0x700 | (val as u16),
|
||||
0x03 => {
|
||||
let reg = LengthTimerHigh(val);
|
||||
self.counter.write(reg.length());
|
||||
self.period = (self.period & 0xFF) | (reg.timer_high() << 8);
|
||||
self.period_timer = self.period;
|
||||
self.cur = 0;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn volume(&self) -> u8 {
|
||||
if self.duty_vol.const_vol() {
|
||||
self.duty_vol.volume()
|
||||
} else {
|
||||
self.envelope_counter
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clock(&mut self) {
|
||||
if self.cur_time == 0 {
|
||||
self.cur_time = self.timer();
|
||||
if !self.enabled {
|
||||
self.sample = 0;
|
||||
return;
|
||||
}
|
||||
if self.period_timer == 0 {
|
||||
self.period_timer = self.period;
|
||||
self.cur = (self.cur + 1) % 8;
|
||||
const DUTY: [[bool; 8]; 4] = [
|
||||
[false, true, false, false, false, false, false, false],
|
||||
@@ -96,30 +172,81 @@ impl PulseChannel {
|
||||
0x00
|
||||
};
|
||||
} else {
|
||||
self.cur_time -= 1;
|
||||
self.period_timer -= 1;
|
||||
}
|
||||
// TODO
|
||||
}
|
||||
pub fn cur_sample(&self) -> u8 {
|
||||
self.sample
|
||||
if self.enabled && !self.counter.silenced() {
|
||||
self.sample
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
pub fn q_frame_clock(&mut self) {
|
||||
if !self.duty_vol.length_counter_halt() && self.cur_length > 0 {
|
||||
self.cur_length -= 1;
|
||||
if !self.enabled {
|
||||
return;
|
||||
}
|
||||
if !self.duty_vol.length_counter_halt() {
|
||||
self.counter.clock();
|
||||
} else {
|
||||
self.counter.silenced = false;
|
||||
}
|
||||
if !self.duty_vol.const_vol() {
|
||||
if self.envelope_start || self.envelope_counter == 0 {
|
||||
self.envelope_counter = 0xF;
|
||||
self.envelope_divider = self.duty_vol.volume();
|
||||
self.envelope_start = false;
|
||||
} else if self.envelope_divider == 0 {
|
||||
self.envelope_divider = self.duty_vol.volume();
|
||||
self.envelope_counter -= 1;
|
||||
} else {
|
||||
self.envelope_divider -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn h_frame_clock(&mut self) {
|
||||
self.q_frame_clock();
|
||||
// if !self.duty_vol.length_counter_halt() && self.cur_length > 0 {
|
||||
// self.cur_length -= 1;
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn view<T>(&self) -> Element<'_, T> {
|
||||
text!(
|
||||
"
|
||||
Square Channel
|
||||
"
|
||||
"Square Channel
|
||||
Evelope Volume: {0:>3} ${0:02X}
|
||||
Constant Volume: {1}
|
||||
Length Counter - Halted: {2}
|
||||
Duty: {3:>3} ${3:02X}
|
||||
Sweep - Shift: {4:>3} ${4:02X}
|
||||
Sweep - Negate: {5}
|
||||
Sweep - Period: {6:>3} ${6:02X}
|
||||
Sweep - Enabled: {7}
|
||||
Period: {8:>3} ${8:04X}
|
||||
Length Counter - Reload Value: {9:>3} ${9:04X}
|
||||
Enabled: {10}
|
||||
Timer: {11:>3} ${11:04X}
|
||||
Duty Position: {12:>3} ${12:02X}
|
||||
Length Counter - Counter: {13:>3} ${13:02X}
|
||||
Envelope - Counter: {14:>3} ${14:02X}
|
||||
Envelope - Divider: {15:>3} ${15:02X}
|
||||
Output: {16:>3} ${16:02X}
|
||||
",
|
||||
self.duty_vol.volume(),
|
||||
self.duty_vol.const_vol(),
|
||||
self.duty_vol.length_counter_halt(),
|
||||
self.duty_vol.duty(),
|
||||
self.sweep.shift(),
|
||||
self.sweep.negate(),
|
||||
self.sweep.period(),
|
||||
self.sweep.enable(),
|
||||
self.period,
|
||||
0,
|
||||
self.enabled,
|
||||
self.period_timer,
|
||||
self.cur, // ?
|
||||
self.counter.current,
|
||||
self.envelope_counter,
|
||||
self.envelope_divider,
|
||||
self.cur_sample(),
|
||||
)
|
||||
.font(Font::MONOSPACE)
|
||||
.into()
|
||||
@@ -128,51 +255,59 @@ impl PulseChannel {
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct LengthCounter(u8);
|
||||
pub struct LengthCounterReg(u8);
|
||||
impl Debug;
|
||||
halt, set_halt: 7;
|
||||
value, set_value: 6, 0;
|
||||
}
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct LengthLoad(u8);
|
||||
impl Debug;
|
||||
load, set_load: 7, 3;
|
||||
timer_high, set_timer_high: 2, 0;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct TriangleChannel {
|
||||
enabled: bool,
|
||||
length: LengthCounter,
|
||||
timer_low: u8,
|
||||
length_load: LengthLoad,
|
||||
length: LengthCounterReg,
|
||||
reload: bool,
|
||||
counter: LengthCounter,
|
||||
|
||||
length_counter: u16,
|
||||
cur: u8,
|
||||
cur_time: u16,
|
||||
period: u16,
|
||||
period_timer: u16,
|
||||
sample: u8,
|
||||
}
|
||||
|
||||
impl TriangleChannel {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
length: LengthCounter(0),
|
||||
timer_low: 0,
|
||||
length_load: LengthLoad(0),
|
||||
length: LengthCounterReg(0),
|
||||
counter: LengthCounter::new(),
|
||||
reload: false,
|
||||
enabled: false,
|
||||
sample: 0,
|
||||
cur_time: 0,
|
||||
period: 0,
|
||||
period_timer: 0,
|
||||
cur: 0,
|
||||
length_counter: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn timer(&self) -> u16 {
|
||||
self.timer_low as u16 | ((self.length_load.timer_high() as u16) << 8)
|
||||
pub fn write(&mut self, offset: u16, val: u8) {
|
||||
match offset {
|
||||
0x00 => {
|
||||
self.length.0 = val;
|
||||
}
|
||||
0x01 => (),
|
||||
0x02 => self.period = self.period & 0x700 | (val as u16),
|
||||
0x03 => {
|
||||
// self.length_load.0 = val;
|
||||
// self.reload = true;
|
||||
let reg = LengthTimerHigh(val);
|
||||
self.counter.write(reg.length());
|
||||
self.period = (self.period & 0xFF) | (reg.timer_high() << 8);
|
||||
self.period_timer = self.period;
|
||||
self.cur = 0;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cur_sample(&self) -> u8 {
|
||||
@@ -180,9 +315,9 @@ impl TriangleChannel {
|
||||
}
|
||||
|
||||
pub fn clock(&mut self) {
|
||||
if self.length_counter > 0 && self.timer() > 0 {
|
||||
if self.cur_time == 0 {
|
||||
self.cur_time = self.timer();
|
||||
if self.length_counter > 0 && self.period > 0 {
|
||||
if self.period_timer == 0 {
|
||||
self.period_timer = self.period;
|
||||
const SAMPLES: [u8; 32] = [
|
||||
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7,
|
||||
8, 9, 10, 11, 12, 13, 14, 15,
|
||||
@@ -190,7 +325,7 @@ impl TriangleChannel {
|
||||
self.cur = (self.cur + 1) % SAMPLES.len() as u8;
|
||||
self.sample = SAMPLES[self.cur as usize];
|
||||
} else {
|
||||
self.cur_time -= 1;
|
||||
self.period_timer -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -227,10 +362,10 @@ Output: {10:>3} ${10:02X}
|
||||
",
|
||||
self.length.value(),
|
||||
self.length.halt(),
|
||||
self.timer(),
|
||||
self.period,
|
||||
0,
|
||||
self.enabled,
|
||||
self.cur_time,
|
||||
self.period_timer,
|
||||
self.cur,
|
||||
0,
|
||||
self.length_counter,
|
||||
@@ -242,26 +377,93 @@ Output: {10:>3} ${10:02X}
|
||||
}
|
||||
}
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct NoiseEnvelope(u8);
|
||||
impl Debug;
|
||||
length_counter_halt, set_length_counter_halt: 5;
|
||||
constant_volume, set_constant_volume: 4;
|
||||
volume, set_volume: 3, 0;
|
||||
}
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ModePeriod(u8);
|
||||
impl Debug;
|
||||
mode, set_mode: 7;
|
||||
period, set_period: 3, 0;
|
||||
}
|
||||
|
||||
bitfield::bitfield! {
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct LengthCounterLoad(u8);
|
||||
impl Debug;
|
||||
length, set_length: 7, 3;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct NoiseChannel {
|
||||
enabled: bool,
|
||||
sample: u8,
|
||||
evelope: NoiseEnvelope,
|
||||
mode_period: ModePeriod,
|
||||
length_counter_load: LengthCounterLoad,
|
||||
|
||||
shr: u16,
|
||||
count: u16,
|
||||
length_counter: u8,
|
||||
}
|
||||
|
||||
impl NoiseChannel {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
sample: 0,
|
||||
evelope: NoiseEnvelope(0),
|
||||
mode_period: ModePeriod(0),
|
||||
length_counter_load: LengthCounterLoad(0),
|
||||
shr: 1,
|
||||
count: 0,
|
||||
length_counter: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&mut self, offset: u16, val: u8) {
|
||||
match offset {
|
||||
0x00 => (),
|
||||
0x01 => (),
|
||||
0x02 => (),
|
||||
0x03 => (),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cur_sample(&self) -> u8 {
|
||||
self.sample
|
||||
if self.length_counter == 0 || self.shr & 1 == 1 {
|
||||
0
|
||||
} else {
|
||||
self.evelope.volume()
|
||||
}
|
||||
}
|
||||
pub fn clock(&mut self) {
|
||||
const PERIODS: &[u16] = &[
|
||||
4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4096,
|
||||
];
|
||||
if self.count == PERIODS[self.mode_period.period() as usize] / 2 {
|
||||
self.count = 0;
|
||||
let bit_0 = (self.shr & 0b1) << 14;
|
||||
let other = if self.mode_period.mode() {
|
||||
(self.shr & 0b100_0000) << 8
|
||||
} else {
|
||||
(self.shr & 0b10) << 13
|
||||
};
|
||||
self.shr = (self.shr >> 1) | (bit_0 ^ other);
|
||||
} else {
|
||||
self.count += 1;
|
||||
}
|
||||
}
|
||||
pub fn clock(&mut self) {}
|
||||
pub fn q_frame_clock(&mut self) {}
|
||||
pub fn h_frame_clock(&mut self) {}
|
||||
pub fn h_frame_clock(&mut self) {
|
||||
self.q_frame_clock();
|
||||
}
|
||||
|
||||
pub fn view<T>(&self) -> Element<'_, T> {
|
||||
text!("").font(Font::MONOSPACE).into()
|
||||
@@ -282,6 +484,16 @@ impl DeltaChannel {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&mut self, offset: u16, val: u8) {
|
||||
match offset {
|
||||
0x00 => (),
|
||||
0x01 => (),
|
||||
0x02 => (),
|
||||
0x03 => (),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cur_sample(&self) -> u8 {
|
||||
self.sample
|
||||
}
|
||||
@@ -373,48 +585,12 @@ impl APU {
|
||||
pub fn write_reg(&mut self, offset: u16, val: u8) {
|
||||
// println!("APU write: {offset:02X} <= {val:02X}");
|
||||
match offset {
|
||||
0x00 => self.pulse_1.duty_vol.0 = val,
|
||||
0x01 => self.pulse_1.sweep.0 = val,
|
||||
0x02 => self.pulse_1.timer_low = val,
|
||||
0x03 => {
|
||||
self.pulse_1.length_timer_high.0 = val;
|
||||
self.pulse_1.reset();
|
||||
}
|
||||
0x04 => self.pulse_2.duty_vol.0 = val,
|
||||
0x05 => self.pulse_2.sweep.0 = val,
|
||||
0x06 => self.pulse_2.timer_low = val,
|
||||
0x07 => {
|
||||
self.pulse_2.length_timer_high.0 = val;
|
||||
self.pulse_2.reset();
|
||||
}
|
||||
0x09 => (), // Unused, technically noise channel?
|
||||
0x08 => {
|
||||
self.triangle.length.0 = val;
|
||||
}
|
||||
0x0A => {
|
||||
self.triangle.timer_low = val;
|
||||
}
|
||||
0x0B => {
|
||||
self.triangle.length_load.0 = val;
|
||||
self.triangle.reload = true;
|
||||
}
|
||||
0x0D => (), // Unused, technically noise channel?
|
||||
0x0C | 0x0E | 0x0F => {
|
||||
// TODO: Noise channel
|
||||
}
|
||||
0x10 => {
|
||||
assert_eq!(val, 0x00);
|
||||
// TODO: implement this value
|
||||
}
|
||||
0x11 => {
|
||||
// TODO: load dmc counter with (val & 7F)
|
||||
}
|
||||
0x12 => {
|
||||
// TODO: DMC
|
||||
}
|
||||
0x13 => {
|
||||
// TODO: DMC
|
||||
}
|
||||
0x00..0x04 => self.pulse_1.write(offset - 0x00, val),
|
||||
0x04..0x08 => self.pulse_2.write(offset - 0x04, val),
|
||||
0x08..0x0C => self.triangle.write(offset - 0x08, val),
|
||||
0x0C..0x10 => self.noise.write(offset - 0x0C, val),
|
||||
0x10..0x14 => self.dmc.write(offset - 0x10, val),
|
||||
0x14 => (),
|
||||
0x15 => {
|
||||
self.dmc.enabled = val & 0b0001_0000 != 0;
|
||||
self.noise.enabled = val & 0b0000_1000 != 0;
|
||||
@@ -451,19 +627,6 @@ impl APU {
|
||||
}
|
||||
|
||||
fn gen_sample(&mut self) {
|
||||
macro_rules! lut {
|
||||
($name:ident: [$ty:ty; $len:expr] = |$n:ident| $expr:expr) => {
|
||||
const $name: [$ty; $len] = {
|
||||
let mut table = [0; $len];
|
||||
let mut $n = 0;
|
||||
while $n < $len {
|
||||
table[$n] = $expr;
|
||||
$n += 1;
|
||||
}
|
||||
table
|
||||
};
|
||||
};
|
||||
}
|
||||
lut!(P_LUT: [u8; 32] = |n| (95.52 / (8128.0 / n as f64 + 100.0) * 255.0) as u8);
|
||||
let pulse_out = P_LUT[(self.pulse_1.cur_sample() + self.pulse_2.cur_sample()) as usize];
|
||||
lut!(TND_LUT: [u8; 204] = |n| (163.67 / (24329.0 / n as f64 + 100.0) * 255.0) as u8);
|
||||
|
||||
Reference in New Issue
Block a user