From: notaz Date: Fri, 29 Sep 2017 23:23:13 +0000 (+0300) Subject: clean up dac code a bit X-Git-Tag: v1.92~21 X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f2cdbf551ad1a7f487b65b4754cbf7983e80b8a;p=picodrive.git clean up dac code a bit --- diff --git a/pico/debug.c b/pico/debug.c index e3ef83be..e780262e 100644 --- a/pico/debug.c +++ b/pico/debug.c @@ -366,31 +366,31 @@ void PDebugZ80Frame(void) } z80_resetCycles(); - emustatus &= ~1; + PsndStartFrame(); - if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) - PicoSyncZ80(line_sample*488); - if (ym2612.dacen && PsndDacLine <= line_sample) - PsndDoDAC(line_sample); + if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) + PicoSyncZ80(SekCycleCnt + line_sample * 488); if (PsndOut) PsndGetSamples(line_sample); - if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) { - PicoSyncZ80(224*488); + if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) { + PicoSyncZ80(SekCycleCnt + 224 * 488); z80_int(); } - if (ym2612.dacen && PsndDacLine <= 224) - PsndDoDAC(224); if (PsndOut) PsndGetSamples(224); // sync z80 - if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) - PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter - if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1) - PsndDoDAC(lines-1); + if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) { + SekCycleCnt += Pico.m.pal ? 151809 : 127671; // cycles adjusted for converter + PicoSyncZ80(SekCycleCnt); + } + if (PsndOut && ym2612.dacen && PsndDacLine < lines) + PsndDoDAC(lines - 1); + PsndDoPSG(lines - 1); timers_cycle(); + SekCycleAim = SekCycleCnt; } void PDebugCPUStep(void) diff --git a/pico/memory.c b/pico/memory.c index cb1b5ac3..fb5fa9be 100644 --- a/pico/memory.c +++ b/pico/memory.c @@ -932,7 +932,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) int scanline = get_scanline(is_from_z80); //elprintf(EL_STATUS, "%03i -> %03i dac w %08x z80 %i", PsndDacLine, scanline, d, is_from_z80); ym2612.dacout = ((int)d - 0x80) << 6; - if (PsndOut && ym2612.dacen && scanline >= PsndDacLine) + if (ym2612.dacen) PsndDoDAC(scanline); return 0; } @@ -1016,8 +1016,10 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) } case 0x2b: { /* DAC Sel (YM2612) */ int scanline = get_scanline(is_from_z80); - ym2612.dacen = d & 0x80; - if (d & 0x80) PsndDacLine = scanline; + if (ym2612.dacen != (d & 0x80)) { + ym2612.dacen = d & 0x80; + PsndDacLine = scanline; + } #ifdef __GP2X__ if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, scanline); #endif diff --git a/pico/pico_cmn.c b/pico/pico_cmn.c index 2de65d02..b39cfdb2 100644 --- a/pico/pico_cmn.c +++ b/pico/pico_cmn.c @@ -85,8 +85,7 @@ static int PicoFrameHints(void) } z80_resetCycles(); - PsndDacLine = 0; - emustatus &= ~1; + PsndStartFrame(); pv->status&=~0x88; // clear V-Int, come out of vblank @@ -145,8 +144,6 @@ static int PicoFrameHints(void) if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) PicoSyncZ80(cycles); - if (ym2612.dacen && PsndDacLine <= y) - PsndDoDAC(y); #ifdef PICO_CD if (PicoAHW & PAHW_MCD) pcd_sync_s68k(cycles, 0); @@ -230,11 +227,7 @@ static int PicoFrameHints(void) // get samples from sound chips if (y == 224 && PsndOut) - { - if (ym2612.dacen && PsndDacLine <= y) - PsndDoDAC(y); PsndGetSamples(y); - } // Run scanline: CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD); @@ -269,8 +262,8 @@ static int PicoFrameHints(void) cycles = SekCyclesDone(); if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) PicoSyncZ80(cycles); - if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1) - PsndDoDAC(lines-1); + if (PsndOut && ym2612.dacen && PsndDacLine < lines) + PsndDoDAC(lines - 1); #ifdef PICO_CD if (PicoAHW & PAHW_MCD) diff --git a/pico/pico_int.h b/pico/pico_int.h index 2249148f..551dcbc7 100644 --- a/pico/pico_int.h +++ b/pico/pico_int.h @@ -820,6 +820,7 @@ PICO_INTERNAL_ASM void wram_1M_to_2M(unsigned char *m); // sound/sound.c PICO_INTERNAL void PsndReset(void); +PICO_INTERNAL void PsndStartFrame(void); PICO_INTERNAL void PsndDoDAC(int line_to); PICO_INTERNAL void PsndClear(void); PICO_INTERNAL void PsndGetSamples(int y); diff --git a/pico/sms.c b/pico/sms.c index 8c44d51f..7dc796ef 100644 --- a/pico/sms.c +++ b/pico/sms.c @@ -258,6 +258,8 @@ void PicoFrameMS(void) int nmi; int y; + PsndStartFrame(); + nmi = (PicoPad[0] >> 7) & 1; if (!Pico.ms.nmi_state && nmi) z80_nmi(); diff --git a/pico/sound/sound.c b/pico/sound/sound.c index 69c1be03..8f88dd7c 100644 --- a/pico/sound/sound.c +++ b/pico/sound/sound.c @@ -14,15 +14,13 @@ #include "../cd/cue.h" #include "mix.h" -#define SIMPLE_WRITE_SOUND 0 - void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo; // master int buffer to mix to static int PsndBuffer[2*(44100+100)/50]; // dac -static unsigned short dac_info[312+4]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample +static unsigned short dac_info[312+4]; // pos in sample buffer // cdda output buffer short cdda_out_buffer[2*1152]; @@ -34,6 +32,7 @@ int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22 int PsndLen_exc_cnt=0; int PsndDacLine=0; short *PsndOut=NULL; // PCM data buffer +static int PsndLen_use; // timers int timer_a_next_oflow, timer_a_step; // in z80 cycles @@ -57,14 +56,12 @@ static void dac_recalculate(void) for(i=226; i != 225; i++) { if (i >= lines) i = 0; - len = 0; if(dac_cnt < 0) { - len=1; pos++; dac_cnt += lines; } dac_cnt -= PsndLen; - dac_info[i] = (pos<<4)|len; + dac_info[i] = pos; } } else @@ -86,24 +83,12 @@ static void dac_recalculate(void) len++; } dac_cnt += PsndLen; - dac_info[i] = (pos<<4)|len; - pos+=len; + pos += len; + dac_info[i] = pos; } - // last sample - for(len = 0, i = pos; i < PsndLen; i++) len++; - if (PsndLen_exc_add) len++; - dac_info[224] = (pos<<4)|len; } - mid = (dac_info[lines-1] & 0xfff0) + ((dac_info[lines-1] & 0xf) << 4); for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++) - dac_info[i] = mid; - //for(i=len=0; i < lines; i++) { - // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf); - // len+=dac_info[i]&0xf; - //} - //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0)); - //printf("len total: %i, last pos: %i\n", len, pos); - //exit(8); + dac_info[i] = dac_info[0]; } @@ -163,6 +148,21 @@ void PsndRerate(int preserve_state) } +PICO_INTERNAL void PsndStartFrame(void) +{ + // compensate for float part of PsndLen + PsndLen_use = PsndLen; + PsndLen_exc_cnt += PsndLen_exc_add; + if (PsndLen_exc_cnt >= 0x10000) { + PsndLen_exc_cnt -= 0x10000; + PsndLen_use++; + } + + PsndDacLine = 0; + emustatus &= ~1; + dac_info[224] = PsndLen_use; +} + PICO_INTERNAL void PsndDoDAC(int line_to) { int pos, pos1, len; @@ -172,12 +172,16 @@ PICO_INTERNAL void PsndDoDAC(int line_to) if (line_to >= 312) line_to = 311; + pos = dac_info[line_from]; + pos1 = dac_info[line_to + 1]; + len = pos1 - pos; + if (len <= 0) + return; + PsndDacLine = line_to + 1; - pos =dac_info[line_from]>>4; - pos1=dac_info[line_to]; - len = ((pos1>>4)-pos) + (pos1&0xf); - if (!len) return; + if (!PsndOut) + return; if (PicoOpt & POPT_EN_STEREO) { short *d = PsndOut + pos*2; @@ -260,17 +264,6 @@ static int PsndRender(int offset, int length) pprof_start(sound); -#if !SIMPLE_WRITE_SOUND - if (offset == 0) { // should happen once per frame - // compensate for float part of PsndLen - PsndLen_exc_cnt += PsndLen_exc_add; - if (PsndLen_exc_cnt >= 0x10000) { - PsndLen_exc_cnt -= 0x10000; - length++; - } - } -#endif - // PSG if (PicoOpt & POPT_EN_PSG) SN76496Update(PsndOut+offset, length, stereo); @@ -322,20 +315,16 @@ static int PsndRender(int offset, int length) // to be called on 224 or line_sample scanlines only PICO_INTERNAL void PsndGetSamples(int y) { -#if SIMPLE_WRITE_SOUND - if (y != 224) return; - PsndRender(0, PsndLen); - if (PicoWriteSound) - PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2)); - PsndClear(); -#else static int curr_pos = 0; + if (ym2612.dacen && PsndDacLine < y) + PsndDoDAC(y - 1); + if (y == 224) { if (emustatus & 2) curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2); - else curr_pos = PsndRender(0, PsndLen); + else curr_pos = PsndRender(0, PsndLen_use); if (emustatus & 1) emustatus |= 2; else emustatus &= ~2; @@ -343,28 +332,20 @@ PICO_INTERNAL void PsndGetSamples(int y) PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2)); // clear sound buffer PsndClear(); + PsndDacLine = 224; + dac_info[224] = 0; } else if (emustatus & 3) { emustatus|= 2; emustatus&=~1; curr_pos = PsndRender(0, PsndLen/2); } -#endif } PICO_INTERNAL void PsndGetSamplesMS(void) { int stereo = (PicoOpt & 8) >> 3; - int length = PsndLen; - -#if !SIMPLE_WRITE_SOUND - // compensate for float part of PsndLen - PsndLen_exc_cnt += PsndLen_exc_add; - if (PsndLen_exc_cnt >= 0x10000) { - PsndLen_exc_cnt -= 0x10000; - length++; - } -#endif + int length = PsndLen_use; // PSG if (PicoOpt & POPT_EN_PSG) @@ -382,3 +363,4 @@ PICO_INTERNAL void PsndGetSamplesMS(void) PsndClear(); } +// vim:shiftwidth=2:ts=2:expandtab diff --git a/pico/sound/ym2612.c b/pico/sound/ym2612.c index e712278b..0867f558 100644 --- a/pico/sound/ym2612.c +++ b/pico/sound/ym2612.c @@ -1679,6 +1679,7 @@ void YM2612ResetChip_(void) for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(i,0); /* DAC mode clear */ ym2612.dacen = 0; + ym2612.dacout = 0; ym2612.addr_A1 = 0; }