From bf5fbbb4b9c10cf1f47bc5c7da887adfc73a3a90 Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 16 Sep 2007 20:45:13 +0000 Subject: [PATCH] gen/cd frame loops merged git-svn-id: file:///home/notaz/opt/svn/PicoDrive@250 be3aeb3a-fb24-0410-a615-afba39da0efa --- Pico/Pico.c | 8 +-- Pico/PicoFrameHints.c | 70 +++++++++++++++++---- Pico/PicoInt.h | 5 +- Pico/cd/Pico.c | 134 +++------------------------------------- platform/gizmondo/emu.h | 2 +- platform/gp2x/emu.c | 51 ++++++++------- platform/gp2x/emu.h | 2 +- 7 files changed, 108 insertions(+), 164 deletions(-) diff --git a/Pico/Pico.c b/Pico/Pico.c index 73321bce..decd634a 100644 --- a/Pico/Pico.c +++ b/Pico/Pico.c @@ -199,7 +199,7 @@ PICO_INTERNAL int CheckDMA(void) return burn; } -static __inline void SekRun(int cyc) +static __inline void SekRunM68k(int cyc) { int cyc_do; SekCycleAim+=cyc; @@ -496,7 +496,7 @@ static int PicoFrameSimple(void) if (CheckIdle()) break; lines += lines_step; - SekRun(cycles_68k_block); + SekRunM68k(cycles_68k_block); PicoRunZ80Simple(line, lines); line=lines; @@ -541,7 +541,7 @@ static int PicoFrameSimple(void) // a gap between flags set and vint pv->pending_ints|=0x20; pv->status|=8; // go into vblank - SekRun(68+4); + SekRunM68k(68+4); // ---- V-Blanking period ---- // fix line counts @@ -568,7 +568,7 @@ static int PicoFrameSimple(void) while (sects) { lines += lines_step; - SekRun(cycles_68k_vblock); + SekRunM68k(cycles_68k_vblock); PicoRunZ80Simple(line, lines); line=lines; diff --git a/Pico/PicoFrameHints.c b/Pico/PicoFrameHints.c index e27484d4..2777440b 100644 --- a/Pico/PicoFrameHints.c +++ b/Pico/PicoFrameHints.c @@ -3,6 +3,8 @@ #define CYCLES_M68K_ASD 148 #define CYCLES_Z80_LINE 228 #define CYCLES_Z80_ASD 69 +#define CYCLES_S68K_LINE 795 +#define CYCLES_S68K_ASD 241 // pad delay (for 6 button pads) #define PAD_DELAY \ @@ -27,6 +29,25 @@ } \ } +// CPUS_RUN +#ifndef PICO_CD +#define CPUS_RUN(m68k_cycles,z80_cycles,s68k_cycles) \ + SekRunM68k(m68k_cycles); \ + Z80_RUN(z80_cycles); +#else +#define CPUS_RUN(m68k_cycles,z80_cycles,s68k_cycles) \ +{ \ + if ((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) { \ + SekRunPS(m68k_cycles, s68k_cycles); /* "better/perfect sync" */ \ + } else { \ + SekRunM68k(m68k_cycles); \ + if ((Pico_mcd->m.busreq&3) == 1) /* no busreq/no reset */ \ + SekRunS68k(s68k_cycles); \ + } \ + Z80_RUN(z80_cycles); \ +} +#endif + // Accurate but slower frame which does hints static int PicoFrameHints(void) { @@ -47,6 +68,9 @@ static int PicoFrameHints(void) } SekCyclesReset(); +#ifdef PICO_CD + SekCyclesResetS68k(); +#endif pv->status&=~0x88; // clear V-Int, come out of vblank @@ -54,8 +78,7 @@ static int PicoFrameHints(void) //dprintf("-hint: %i", hint); // This is to make active scan longer (needed for Double Dragon 2, mainly) - SekRun(CYCLES_M68K_ASD); - Z80_RUN(CYCLES_Z80_ASD); + CPUS_RUN(CYCLES_M68K_ASD, CYCLES_Z80_ASD, CYCLES_S68K_ASD); for (y=0;yreg[1]&0x20) { elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt); SekInterrupt(6); @@ -142,13 +176,20 @@ static int PicoFrameHints(void) sound_timers_and_dac(y); // get samples from sound chips - if ((y == 224) && PsndOut) - getSamples(y); +#ifndef PICO_CD + if (y == 224) +#endif + if (PsndOut) + getSamples(y); // Run scanline: if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); - SekRun(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD); - Z80_RUN(CYCLES_Z80_LINE - CYCLES_Z80_ASD); + CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD, + CYCLES_Z80_LINE - CYCLES_Z80_ASD, CYCLES_S68K_LINE - CYCLES_S68K_ASD); + +#ifdef PICO_CD + update_chips(); +#endif // PAL line count might actually be 313 according to Steve Snake, but that would complicate things. lines = Pico.m.pal ? 312 : 262; @@ -158,14 +199,20 @@ static int PicoFrameHints(void) Pico.m.scanline=(short)y; PAD_DELAY +#ifdef PICO_CD + check_cd_dma(); +#endif if(PicoOpt&1) sound_timers_and_dac(y); // Run scanline: if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); - SekRun(CYCLES_M68K_LINE); - Z80_RUN(CYCLES_Z80_LINE); + CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE); + +#ifdef PICO_CD + update_chips(); +#endif } // draw a frame just after vblank in alternative render mode @@ -177,4 +224,5 @@ static int PicoFrameHints(void) #undef PAD_DELAY #undef Z80_RUN +#undef CPUS_RUN diff --git a/Pico/PicoInt.h b/Pico/PicoInt.h index 6105f611..6be369ad 100644 --- a/Pico/PicoInt.h +++ b/Pico/PicoInt.h @@ -119,7 +119,10 @@ extern unsigned int SekCycleCntT; // total cycle counter, updated once per frame extern int SekCycleCntS68k; extern int SekCycleAimS68k; -#define SekCyclesResetS68k() {SekCycleCntS68k=SekCycleAimS68k=0;} +#define SekCyclesResetS68k() { \ + SekCycleCntS68k-=SekCycleAimS68k; \ + SekCycleAimS68k=0; \ +} #define SekCyclesDoneS68k() (SekCycleAimS68k-SekCyclesLeftS68k) // debug cyclone diff --git a/Pico/cd/Pico.c b/Pico/cd/Pico.c index ab0c333b..16a25dad 100644 --- a/Pico/cd/Pico.c +++ b/Pico/cd/Pico.c @@ -223,133 +223,17 @@ static __inline void update_chips(void) } -static int PicoFrameHintsMCD(void) +static __inline void getSamples(int y) { - struct PicoVideo *pv=&Pico.video; - int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample; - const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values - int skip=PicoSkipFrame || (PicoOpt&0x10); - int hint; // Hint counter - - if(Pico.m.pal) { // - //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488) - //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228 - lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well - line_sample = 68; - if(pv->reg[1]&8) lines_vis = 240; - } else { - //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488 - //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228 - lines = 262; - line_sample = 93; - } - - SekCyclesReset(); - SekCyclesResetS68k(); - //z80ExtraCycles = 0; - - if(PicoOpt&4) - z80CycleAim = 0; -// z80_resetCycles(); - - pv->status&=~0x88; // clear V-Int, come out of vblank - - hint=pv->reg[10]; // Load H-Int counter - //dprintf("-hint: %i", hint); - - for (y=0;y 25) Pico.m.padTHPhase[0]=0; - if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; - } - - check_cd_dma(); - - // H-Interrupts: - if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe - { - //dprintf("rhint:old @ %06x", SekPc); - hint=pv->reg[10]; // Reload H-Int counter - pv->pending_ints|=0x10; - if (pv->reg[0]&0x10) SekInterrupt(4); - //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt); - //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86)); - } - - // V-Interrupt: - if (y == lines_vis) - { - //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt); - pv->status|=0x88; // V-Int happened, go into vblank - SekRunM68k(128); SekCycleAim-=128; // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula) - /*if(Pico.m.z80Run && (PicoOpt&4)) { - z80CycleAim+=cycles_z80/2; - total_z80+=z80_run(z80CycleAim-total_z80); - z80CycleAim-=cycles_z80/2; - }*/ - pv->pending_ints|=0x20; - if(pv->reg[1]&0x20) SekInterrupt(6); - if(Pico.m.z80Run && (PicoOpt&4)) // ? - z80_int(); - //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0)); - } - - // decide if we draw this line -#if CAN_HANDLE_240_LINES - if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) ) -#else - if(!skip && y<224) -#endif - PicoLine(y); - - if(PicoOpt&1) - sound_timers_and_dac(y); - - // get samples from sound chips - if (y == 224 && PsndOut) { - int len = sound_render(0, PsndLen); - if (PicoWriteSound) PicoWriteSound(len); - // clear sound buffer - sound_clear(); - } - - // Run scanline: - //dprintf("m68k starting exec @ %06x", SekPc); - if (Pico.m.dma_xfers) SekCycleCnt+=CheckDMA(); - if ((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) { - SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync" - } else { - SekRunM68k(cycles_68k); - if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset - SekRunS68k(cycles_s68k); - } - - if ((PicoOpt&4) && Pico.m.z80Run) { - if (Pico.m.z80Run & 2) z80CycleAim+=cycles_z80; - else { - int cnt = SekCyclesDone() - z80startCycle; - cnt = (cnt>>1)-(cnt>>5); - //if (cnt > cycles_z80) printf("FIXME: z80 cycles: %i\n", cnt); - if (cnt > cycles_z80) cnt = cycles_z80; - Pico.m.z80Run |= 2; - z80CycleAim+=cnt; - } - total_z80+=z80_run(z80CycleAim-total_z80); - } - - update_chips(); - } + int len = sound_render(0, PsndLen); + if (PicoWriteSound) PicoWriteSound(len); + // clear sound buffer + sound_clear(); +} - // draw a frame just after vblank in alternative render mode - if (!PicoSkipFrame && (PicoOpt&0x10)) - PicoFrameFull(); - return 0; -} +#define PICO_CD +#include "../PicoFrameHints.c" PICO_INTERNAL int PicoFrameMCD(void) @@ -357,7 +241,7 @@ PICO_INTERNAL int PicoFrameMCD(void) if(!(PicoOpt&0x10)) PicoFrameStart(); - PicoFrameHintsMCD(); + PicoFrameHints(); return 0; } diff --git a/platform/gizmondo/emu.h b/platform/gizmondo/emu.h index e9b58c9c..defa5e62 100644 --- a/platform/gizmondo/emu.h +++ b/platform/gizmondo/emu.h @@ -53,7 +53,7 @@ char *emu_GetSaveFName(int load, int is_sram, int slot); int emu_check_save_file(int slot); void emu_set_save_cbs(int gz); void emu_forced_frame(void); -int emu_cd_check(char **bios_file); +int emu_cd_check(int *pregion); int find_bios(int region, char **bios_file); void scaling_update(void); diff --git a/platform/gp2x/emu.c b/platform/gp2x/emu.c index 3096edd9..96150138 100644 --- a/platform/gp2x/emu.c +++ b/platform/gp2x/emu.c @@ -151,7 +151,7 @@ int find_bios(int region, char **bios_file) /* checks if romFileName points to valid MegaCD image * if so, checks for suitable BIOS */ -int emu_cd_check(char **bios_file) +int emu_cd_check(int *pregion) { unsigned char buf[32]; pm_file *cd_f; @@ -183,17 +183,9 @@ int emu_cd_check(char **bios_file) printf("detected %s Sega/Mega CD image with %s region\n", type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA"); - if (PicoRegionOverride) { - region = PicoRegionOverride; - printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA"); - } - - if (bios_file == NULL) return type; - - if (find_bios(region, bios_file)) - return type; // CD and BIOS detected + if (pregion != NULL) *pregion = region; - return -1; // CD detected but load failed + return type; } int emu_ReloadRom(void) @@ -202,7 +194,7 @@ int emu_ReloadRom(void) char *used_rom_name = romFileName; char ext[5]; pm_file *rom; - int ret, cd_state; + int ret, cd_state, cd_region, cfg_loaded = 0; printf("emu_ReloadRom(%s)\n", romFileName); @@ -271,14 +263,30 @@ int emu_ReloadRom(void) Stop_CD(); // check for MegaCD image - cd_state = emu_cd_check(&used_rom_name); - if (cd_state > 0) { + cd_state = emu_cd_check(&cd_region); + if (cd_state > 0) + { + // valid CD image, check for BIOS.. + + // we need to have config loaded at this point + ret = emu_ReadConfig(1, 1); + if (!ret) emu_ReadConfig(0, 1); + cfg_loaded = 1; + + if (PicoRegionOverride) { + cd_region = PicoRegionOverride; + printf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA"); + } + if (!find_bios(cd_region, &used_rom_name)) { + // bios_help() ? + return 0; + } + PicoMCD |= 1; get_ext(used_rom_name, ext); - } else if (cd_state == -1) { - // bios_help() ? - return 0; - } else { + } + else + { if (PicoMCD & 1) Stop_CD(); PicoMCD &= ~1; } @@ -317,9 +325,10 @@ int emu_ReloadRom(void) } // load config for this ROM (do this before insert to get correct region) - ret = emu_ReadConfig(1, 1); - if (!ret) - emu_ReadConfig(0, 1); + if (!cfg_loaded) { + ret = emu_ReadConfig(1, 1); + if (!ret) emu_ReadConfig(0, 1); + } printf("PicoCartInsert(%p, %d);\n", rom_data, rom_size); if(PicoCartInsert(rom_data, rom_size)) { diff --git a/platform/gp2x/emu.h b/platform/gp2x/emu.h index 55bb53e7..6ae089df 100644 --- a/platform/gp2x/emu.h +++ b/platform/gp2x/emu.h @@ -53,7 +53,7 @@ char *emu_GetSaveFName(int load, int is_sram, int slot); int emu_check_save_file(int slot); void emu_set_save_cbs(int gz); void emu_forced_frame(void); -int emu_cd_check(char **bios_file); +int emu_cd_check(int *pregion); int find_bios(int region, char **bios_file); void scaling_update(void); -- 2.39.5