#![allow(dead_code)]
use mos6502::{CPU_FREQ, CPU};
use memory::CPUBus;
struct LPFilter {
prev_out: i16
}
const AUDIO_LEVEL_MAX: i32 = 65536;
const LP_FACTOR: i32 = (0.815686 * AUDIO_LEVEL_MAX as f32) as i32;
const HP_FACTOR1: i32 = (0.996039 * AUDIO_LEVEL_MAX as f32) as i32;
const HP_FACTOR2: i32 = (0.999835 * AUDIO_LEVEL_MAX as f32) as i32;
impl LPFilter {
fn new() -> Self {
LPFilter { prev_out: 0 }
}
fn output(&mut self, input: i16) -> i16 {
let out = ((input as i32 - self.prev_out as i32)
* LP_FACTOR / AUDIO_LEVEL_MAX) as i16;
self.prev_out = out;
out
}
}
struct HPFilter {
prev_in: i16,
prev_out: i16,
hp_factor: i32
}
impl HPFilter {
fn new(hp_factor: i32) -> Self {
HPFilter {
prev_in: 0,
prev_out: 0,
hp_factor
}
}
fn output(&mut self, input: i16) -> i16 {
let out = (self.prev_out as i32 * self.hp_factor / AUDIO_LEVEL_MAX +
input as i32 - self.prev_in as i32) as i16;
self.prev_in = input;
self.prev_out = out;
out
}
}
pub trait Speaker {
fn queue(&mut self, sample: i16);
}
const QUARTER_FRAME_FREQ: u32 = 240;
pub const AUDIO_SAMPLE_FREQ: u32 = 44100;
const TRI_SEQ_TABLE: [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
];
const LEN_TABLE: [u8; 32] = [
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, 30,
];
const DUTY_TABLE: [u8; 4] = [
0b00000010,
0b00000110,
0b00011110,
0b11111001,
];
const PULSE_TABLE: [u16; 31] = [
0x0000, 0x02f8, 0x05df, 0x08b4, 0x0b78, 0x0e2b,
0x10cf, 0x1363, 0x15e9, 0x1860, 0x1ac9, 0x1d25,
0x1f75, 0x21b7, 0x23ee, 0x2618, 0x2837, 0x2a4c,
0x2c55, 0x2e54, 0x3049, 0x3234, 0x3416, 0x35ee,
0x37be, 0x3985, 0x3b43, 0x3cf9, 0x3ea7, 0x404d,
0x41ec
];
const NOISE_PERIOD_TABLE: [u16; 16] = [
4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068
];
const DMC_TABLE: [u16; 16] = [
214, 190, 170, 160, 143, 127, 113, 107, 95, 80, 71, 64, 53, 42, 36, 27,
];
const TND_TABLE: [u16; 203] = [
0x0000, 0x01b7, 0x036a, 0x051a, 0x06c6, 0x086f,
0x0a15, 0x0bb7, 0x0d56, 0x0ef2, 0x108a, 0x121f,
0x13b1, 0x1540, 0x16cc, 0x1855, 0x19da, 0x1b5d,
0x1cdd, 0x1e59, 0x1fd3, 0x214a, 0x22be, 0x2430,
0x259e, 0x270a, 0x2874, 0x29da, 0x2b3e, 0x2c9f,
0x2dfe, 0x2f5a, 0x30b4, 0x320b, 0x335f, 0x34b2,
0x3601, 0x374f, 0x389a, 0x39e2, 0x3b29, 0x3c6d,
0x3dae, 0x3eee, 0x402b, 0x4166, 0x429f, 0x43d6,
0x450a, 0x463d, 0x476d, 0x489c, 0x49c8, 0x4af2,
0x4c1b, 0x4d41, 0x4e65, 0x4f87, 0x50a8, 0x51c6,
0x52e3, 0x53fe, 0x5517, 0x562e, 0x5743, 0x5856,
0x5968, 0x5a78, 0x5b86, 0x5c93, 0x5d9d, 0x5ea6,
0x5fae, 0x60b3, 0x61b7, 0x62ba, 0x63bb, 0x64ba,
0x65b7, 0x66b3, 0x67ae, 0x68a7, 0x699e, 0x6a94,
0x6b88, 0x6c7b, 0x6d6d, 0x6e5d, 0x6f4b, 0x7038,
0x7124, 0x720e, 0x72f7, 0x73de, 0x74c4, 0x75a9,
0x768c, 0x776e, 0x784f, 0x792e, 0x7a0d, 0x7ae9,
0x7bc5, 0x7c9f, 0x7d78, 0x7e50, 0x7f26, 0x7ffc,
0x80d0, 0x81a3, 0x8274, 0x8345, 0x8414, 0x84e2,
0x85af, 0x867b, 0x8746, 0x880f, 0x88d8, 0x899f,
0x8a65, 0x8b2b, 0x8bef,