asdf more trial and error, getting closer... trying to treat WAVE_FREQ as write-only

This commit is contained in:
lif 2024-01-21 00:55:52 -08:00
parent 36c1e52e29
commit 0cc0f52463
4 changed files with 63689 additions and 50605 deletions

View file

@ -31,9 +31,8 @@ fn main() {
.collect();
// gba psg has inverted wave channel polarity
for sample_pair in &mut bytes[0..*song_positions.iter().min().unwrap()] {
let upper = *sample_pair & 0xF0;
let lower = *sample_pair & 0x0F;
*sample_pair = (0xF0 - upper) | (0x0F - lower);
// don't worry about carries, both nybbles are NYBBLE_MAX
*sample_pair = 0xFF - *sample_pair;
}
consts.add_array("LSDPACK_DATA", "u8", &bytes);
consts.add_array("LSDPACK_SONG_POSITIONS", "usize", &song_positions);

114175
lsdpack.s

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,7 @@
use core::{fmt::Write, ops::Add};
use core::{
fmt::Write,
ops::{Add, BitXorAssign},
};
use gba::prelude::*;
use voladdress::{Safe, VolAddress};
@ -104,6 +107,7 @@ pub struct Lsdpack {
repeat_cmd: Option<(LsdpackCmd, bool)>,
repeat_cmd_counter: usize,
sample_pitch: u16,
wave_bank_switch: bool,
stopped: bool,
}
@ -118,6 +122,7 @@ impl Lsdpack {
repeat_cmd_counter: 0,
sample_pitch: 0,
stopped: false,
wave_bank_switch: false,
}
}
@ -147,17 +152,28 @@ impl Lsdpack {
//let lrvol = LEFT_RIGHT_VOLUME.read();
//LEFT_RIGHT_VOLUME.write(lrvol.with_wave_left(false).with_wave_right(false));
let wb = WAVE_BANK.read();
//let wb = WAVE_BANK.read();
//WAVE_BANK.write(WaveBank::new().with_enabled(false));
/*
WAVE_BANK.write(
WaveBank::new()
.with_enabled(true)
.with_two_banks(false)
.with_bank1(wb.bank1()),
);
*/
/*
let wave_ram_ptr = (WAVE_RAM.as_mut_ptr() as *mut u8);
for i in 0..16 {
unsafe {
wave_ram_ptr.add(i).write_volatile(self.data[ptr + i]);
}
}
// */
/*
//*
WAVE_RAM.index(0).write(u32::from_le_bytes(
TryFrom::try_from(&self.data[ptr..ptr + 4]).unwrap(),
));
@ -170,20 +186,26 @@ impl Lsdpack {
WAVE_RAM.index(3).write(u32::from_le_bytes(
TryFrom::try_from(&self.data[ptr + 12..ptr + 16]).unwrap(),
));
*/
//WAVE_BANK.write(WaveBank::new().with_enabled(true));
// */
self.wave_bank_switch ^= true;
WAVE_BANK.write(
WaveBank::new()
.with_enabled(true)
.with_two_banks(false)
.with_bank1(!wb.bank1()),
.with_bank1(self.wave_bank_switch),
);
//WAVE_BANK.write(WaveBank::new().with_enabled(true));
// SOUND_ENABLED.write(sound_enabled);
//LEFT_RIGHT_VOLUME.write(lrvol);
unsafe { WAVE_FREQ.cast() }.write(self.sample_pitch);
WAVE_FREQ.write(
WaveFrequency::new()
.with_enabled(true)
.with_length(self.sample_pitch & 0x7ff),
);
//unsafe { WAVE_FREQ.cast() }.write(self.sample_pitch);
//WAVE_FREQ.write(WaveFrequency::new().with_enabled(true).with_length(0x7e0));
}
#[link_section = ".iwram"]
@ -258,8 +280,16 @@ impl Lsdpack {
fn write_lsb<T>(&mut self, voladdr: VolAddress<T, Safe, Safe>) {
let voladdr = unsafe { voladdr.cast::<u16>() };
//voladdr.write((voladdr.read() & 0xFF00) | (self.next_byte() as u16));
let val = self.next_byte();
/*
if let Ok(mut logger) =
gba::mgba::MgbaBufferedLogger::try_new(gba::mgba::MgbaMessageLevel::Info)
{
writeln!(logger, "lsb: {}", val);
}
// */
unsafe {
(voladdr.as_mut_ptr() as *mut u8).write_volatile(self.next_byte());
(voladdr.as_mut_ptr() as *mut u8).write_volatile(val);
}
}
@ -267,10 +297,16 @@ impl Lsdpack {
fn write_msb<T>(&mut self, voladdr: VolAddress<T, Safe, Safe>) {
let voladdr = unsafe { voladdr.cast::<u16>() };
//voladdr.write((voladdr.read() & 0xFF) | ((self.next_byte() as u16) << 8));
let val = self.next_byte();
/*
if let Ok(mut logger) =
gba::mgba::MgbaBufferedLogger::try_new(gba::mgba::MgbaMessageLevel::Info)
{
writeln!(logger, "msb: {}", val);
}
// */
unsafe {
(voladdr.as_mut_ptr() as *mut u8)
.add(1)
.write_volatile(self.next_byte());
(voladdr.as_mut_ptr() as *mut u8).add(1).write_volatile(val);
}
}
@ -278,6 +314,13 @@ impl Lsdpack {
fn write_u16le<T>(&mut self, voladdr: VolAddress<T, Safe, Safe>) {
let voladdr = unsafe { voladdr.cast::<u16>() };
let halfword = u16::from_le_bytes([self.next_byte(), self.next_byte()]);
/*
if let Ok(mut logger) =
gba::mgba::MgbaBufferedLogger::try_new(gba::mgba::MgbaMessageLevel::Info)
{
writeln!(logger, "u16le: {}", halfword);
}
// */
voladdr.write(halfword);
}
@ -372,7 +415,10 @@ impl Lsdpack {
}
PitchPu0 => self.write_u16le(TONE1_FREQUENCY),
PitchPu1 => self.write_u16le(TONE2_FREQUENCY),
PitchWav => self.write_u16le(WAVE_FREQ),
PitchWav => {
self.sample_pitch = u16::from_le_bytes([self.next_byte(), self.next_byte()]);
unsafe { WAVE_FREQ.cast() }.write(self.sample_pitch);
}
SampleNext => self.write_next_samples(),
// general register writes:
@ -389,15 +435,41 @@ impl Lsdpack {
} //self.write_msb(TONE2_PATTERN),
Pu1PitchLsb => self.write_lsb(TONE2_FREQUENCY),
Pu1PitchMsb => self.write_msb(TONE2_FREQUENCY),
//WavOnOff => unsafe { WAVE_BANK.cast() }.write(self.next_byte()),
WavOnOff => WAVE_BANK.write(
WaveBank::new().with_enabled(self.next_byte() != 0), //.with_two_banks(true)
//.with_bank1(false),
),
WavOnOff => {
let val = self.next_byte();
self.wave_bank_switch = val & 0x40 != 0;
unsafe { WAVE_BANK.cast() }.write(val);
}
/*
WavOnOff => {
let val = self.next_byte();
if val == 0 {
WAVE_LEN_VOLUME.write(WaveLenVolume::new().with_volume(0))
} else {
panic!("perish")
}
/*
WAVE_BANK.write(
WaveBank::new()
.with_enabled(self.next_byte() != 0)
.with_two_banks(true)
.with_bank1(false),
);
*/
}
*/
WavLength => self.write_lsb(WAVE_LEN_VOLUME),
WavEnv => self.write_msb(WAVE_LEN_VOLUME),
WavPitchLsb => self.write_lsb(WAVE_FREQ),
WavPitchMsb => self.write_msb(WAVE_FREQ),
WavPitchLsb => {
self.sample_pitch &= 0xff00;
self.sample_pitch |= self.next_byte() as u16;
unsafe { WAVE_FREQ.cast() }.write(self.sample_pitch);
}
WavPitchMsb => {
self.sample_pitch &= 0xff;
self.sample_pitch |= (self.next_byte() as u16) << 8;
unsafe { WAVE_FREQ.cast() }.write(self.sample_pitch);
}
NoiLength => self.write_lsb(NOISE_LEN_ENV),
NoiEnv => {
self.next_byte();

View file

@ -86,7 +86,7 @@ extern "C" fn main() -> ! {
IME.write(true);
//TIMER0_RELOAD.write((((16777216 / 1) / 360) as u16).wrapping_neg());
TIMER0_RELOAD.write((((280896 / 1) / 6) as u16).wrapping_neg());
TIMER0_RELOAD.write((((280896 / 1) / 12) as u16).wrapping_neg());
TIMER0_CONTROL.write(
TimerControl::new()
.with_overflow_irq(true)