From 91d6861409abd785af1964593168e4e8d7b10421 Mon Sep 17 00:00:00 2001 From: meepingsnesroms Date: Sat, 20 Oct 2018 17:32:55 -0700 Subject: [PATCH] Audio is a little better, low and high options in settings dont seem to work though. --- src/emulator.c | 7 +++++ src/emulator.h | 2 +- src/hardwareRegisters.c | 2 ++ src/hardwareRegisters.h | 1 + src/hardwareRegistersAccessors.c.h | 41 ++++++++++++------------------ 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/emulator.c b/src/emulator.c index 5adc03d..7d06e66 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -184,6 +184,7 @@ uint64_t emulatorGetStateSize(){ size += sizeof(int32_t);//pwm1ClocksToNextSample size += sizeof(uint8_t) * 6;//pwm1Fifo[6] size += sizeof(uint8_t) * 2;//pwm1(Read/Write) + size += sizeof(int32_t);//pwm1LastSampleDelta size += sizeof(uint8_t) * 7;//palmMisc size += sizeof(uint32_t);//palmSdCard.command size += sizeof(uint8_t) * 2;//palmSdCard.response / palmSdCard.commandBitsRemaining @@ -306,6 +307,8 @@ bool emulatorSaveState(buffer_t buffer){ offset += sizeof(uint8_t); writeStateValueUint8(buffer.data + offset, pwm1WritePosition); offset += sizeof(uint8_t); + writeStateValueInt32(buffer.data + offset, pwm1LastSampleDelta); + offset += sizeof(int32_t); //misc writeStateValueBool(buffer.data + offset, palmMisc.powerButtonLed); @@ -453,6 +456,10 @@ bool emulatorLoadState(buffer_t buffer){ offset += sizeof(uint8_t); pwm1WritePosition = readStateValueUint8(buffer.data + offset); offset += sizeof(uint8_t); + blip_add_delta(palmAudioResampler, 0, -pwm1LastSampleDelta); + pwm1LastSampleDelta = readStateValueInt32(buffer.data + offset); + offset += sizeof(int32_t); + blip_add_delta(palmAudioResampler, 0, pwm1LastSampleDelta); //misc palmMisc.powerButtonLed = readStateValueBool(buffer.data + offset); diff --git a/src/emulator.h b/src/emulator.h index eb30f25..4c3efb6 100644 --- a/src/emulator.h +++ b/src/emulator.h @@ -122,7 +122,7 @@ typedef struct{ #define CRYSTAL_FREQUENCY 32768 #define AUDIO_AMPLITUDE 1000000 #define AUDIO_SAMPLES_PER_FRAME (AUDIO_SAMPLE_RATE / EMU_FPS) -#define AUDIO_END_OF_FRAME 1000000000//used to convert the variable timing of SYSCLK and CLK32 to a fixed location in the current frame 0<->AUDIO_END_OF_FRAME +#define AUDIO_END_OF_FRAME (235929600 / EMU_FPS)//smallest amount of time a second can be split into:(2.0 * (14.0 * (255 + 1.0) + 15 + 1.0)) * 32768 == 235929600, used to convert the variable timing of SYSCLK and CLK32 to a fixed location in the current frame 0<->AUDIO_END_OF_FRAME #define AUDIO_WAIT_FOR_SAMPLE INT32_MIN //emulator data, some are GUI interface variables, some should be left alone diff --git a/src/hardwareRegisters.c b/src/hardwareRegisters.c index 040a9a9..3f227ee 100644 --- a/src/hardwareRegisters.c +++ b/src/hardwareRegisters.c @@ -32,6 +32,7 @@ int32_t pwm1ClocksToNextSample; uint8_t pwm1Fifo[6]; uint8_t pwm1ReadPosition; uint8_t pwm1WritePosition; +int32_t pwm1LastSampleDelta; static void checkInterrupts(); @@ -1080,6 +1081,7 @@ void resetHwRegisters(){ memset(pwm1Fifo, 0x00, sizeof(pwm1Fifo)); pwm1ReadPosition = 0; pwm1WritePosition = 0; + pwm1LastSampleDelta = 0; memset(chips, 0x00, sizeof(chips)); //all chip selects are disabled at boot and CSA0 is mapped to 0x00000000 and covers the entire address range until CSA is set enabled diff --git a/src/hardwareRegisters.h b/src/hardwareRegisters.h index 0833e56..b2ed142 100644 --- a/src/hardwareRegisters.h +++ b/src/hardwareRegisters.h @@ -79,6 +79,7 @@ extern int32_t pwm1ClocksToNextSample; extern uint8_t pwm1Fifo[]; extern uint8_t pwm1ReadPosition; extern uint8_t pwm1WritePosition; +extern int32_t pwm1LastSampleDelta; //timing void beginClk32(); diff --git a/src/hardwareRegistersAccessors.c.h b/src/hardwareRegistersAccessors.c.h index 4a52a0b..9e29b43 100644 --- a/src/hardwareRegistersAccessors.c.h +++ b/src/hardwareRegistersAccessors.c.h @@ -86,20 +86,13 @@ int32_t pwm1FifoRunSample(int32_t now, int32_t clockOffset){ uint8_t clockDivider = 2 << (pwmc1 & 0x03); uint8_t repeat = 1 << (pwmc1 >> 2 & 0x03); int32_t audioStart = now + clockOffset; - int32_t audioNow = audioStart; - int32_t audioSampleDuration = usingClk32 ? audioGetFramePercentIncrementFromClk32s(period * prescaler * clockDivider) : audioGetFramePercentIncrementFromSysclks(period * prescaler * clockDivider); + //int32_t audioNow = audioStart; + int32_t audioSampleDuration = usingClk32 ? audioGetFramePercentIncrementFromClk32s(period * prescaler * clockDivider * repeat) : audioGetFramePercentIncrementFromSysclks(period * prescaler * clockDivider * repeat); int32_t audioDeew = dMin((double)sample / period, 1.0)/*dutyCycle*/ * AUDIO_AMPLITUDE; - for(uint8_t times = 0; times < repeat; times++){ - //At the beginning of a sample period cycle, the PWMO pin is set to 1 and the counter begins counting up from 0x00. - //The sample value is compared on each count of the prescaler clock. - //When the sample and count values match, the PWMO signal is cleared to 0. - //MC68VZ328UM.pdf - //debugLog("Audio sample played, dutyCycle:%f, start:%d, end:%d\n", dutyCycle, audioNow, audioNow + audioDutyCycle); - blip_add_delta(palmAudioResampler, audioNow, audioDeew); - blip_add_delta(palmAudioResampler, audioNow + audioSampleDuration, -audioDeew); - audioNow += audioSampleDuration; - } + blip_add_delta(palmAudioResampler, audioStart, audioDeew - pwm1LastSampleDelta); + //audioNow += audioSampleDuration; + pwm1LastSampleDelta = audioDeew; //remove used entry if(pwm1FifoEntrys() > 0) @@ -115,7 +108,12 @@ int32_t pwm1FifoRunSample(int32_t now, int32_t clockOffset){ registerArrayWrite16(PWMC1, pwmc1 | 0x0080);//set IRQ bit } - return audioNow - audioStart; + return audioSampleDuration; +} + +static inline void pwm1FifoFinished(int32_t now){ + blip_add_delta(palmAudioResampler, now, -pwm1LastSampleDelta); + pwm1LastSampleDelta = 0; } static inline void pwm1FifoWrite(uint8_t value){ @@ -130,16 +128,6 @@ static inline void pwm1FifoFlush(){ pwm1WritePosition = 0; } -static inline uint32_t pwm1FifoSampleDuration(){ - uint16_t pwmc1 = registerArrayRead16(PWMC1); - uint16_t period = registerArrayRead8(PWMP1) + 2;//max:257 - uint8_t prescaler = (pwmc1 >> 8 & 0x7F) + 1;//max:128 - uint8_t clockDivider = 2 << (pwmc1 & 0x03);//max:16 - uint8_t repeat = 1 << (pwmc1 >> 2 & 0x03);//max:8 - - return period * prescaler * clockDivider * repeat; -} - //register setters static inline void setCsa(uint16_t value){ chips[CHIP_A0_ROM].enable = value & 0x0001; @@ -644,8 +632,9 @@ static inline void samplePwm1(bool forClk32, double sysclks){ //use samples if(pwm1ClocksToNextSample <= 0){ + int32_t audioNow = audioGetFramePercentage(); + while(pwm1FifoEntrys() > 0 && pwm1ClocksToNextSample <= 0){ - int32_t audioNow = audioGetFramePercentage(); int32_t audioUsed; //switch out samples until waiting(pwm1ClocksToNextSample is positive) @@ -664,8 +653,10 @@ static inline void samplePwm1(bool forClk32, double sysclks){ } //all samples used and its still negative, wait for next sample - if(pwm1ClocksToNextSample <= 0) + if(pwm1ClocksToNextSample != AUDIO_WAIT_FOR_SAMPLE && pwm1ClocksToNextSample <= 0){ + pwm1FifoFinished(audioNow); pwm1ClocksToNextSample = AUDIO_WAIT_FOR_SAMPLE; + } } }