X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2Fsek.c;h=39168a9a22c82ce6eed3f1afd960a9a415ac0d78;hb=9b5713af95da3f72cb659d4d8017559aca593a31;hp=f085f6129a1896f74cc6224e22ea14a67e4e6569;hpb=1cfc5cc4ce06642b9bc45ca3b9d32793718e9455;p=picodrive.git diff --git a/pico/sek.c b/pico/sek.c index f085f61..39168a9 100644 --- a/pico/sek.c +++ b/pico/sek.c @@ -1,13 +1,14 @@ -// This is part of Pico Library - -// (c) Copyright 2004 Dave, All rights reserved. -// (c) Copyright 2006 notaz, All rights reserved. -// Free for non-commercial use. - -// For commercial use, separate licencing terms must be obtained. - +/* + * PicoDrive + * (c) Copyright Dave, 2004 + * (C) notaz, 2006-2009 + * + * This work is licensed under the terms of MAME license. + * See COPYING file in the top-level directory. + */ #include "pico_int.h" +#include "memory.h" int SekCycleCnt=0; // cycles done in this frame @@ -49,12 +50,13 @@ static void SekResetAck(void) static int SekUnrecognizedOpcode() { - unsigned int pc, op; + unsigned int pc; pc = SekPc; - op = PicoCpuCM68k.read16(pc); - elprintf(EL_ANOMALY, "Unrecognized Opcode %04x @ %06x", op, pc); - // see if we are not executing trash - if (pc < 0x200 || (pc > Pico.romsize+4 && (pc&0xe00000)!=0xe00000)) { + elprintf(EL_ANOMALY, "Unrecognized Opcode @ %06x", pc); + // see if we are still in a mapped region + pc &= 0x00ffffff; + if (map_flag_set(m68k_read16_map[pc >> M68K_MEM_SHIFT])) { + elprintf(EL_STATUS|EL_ANOMALY, "m68k crash @%06x", pc); PicoCpuCM68k.cycles = 0; PicoCpuCM68k.state_flags |= 1; return 1; @@ -138,13 +140,7 @@ PICO_INTERNAL int SekReset(void) if (Pico.rom==NULL) return 1; #ifdef EMU_C68K - PicoCpuCM68k.state_flags=0; - PicoCpuCM68k.osp=0; - PicoCpuCM68k.srh =0x27; // Supervisor mode - PicoCpuCM68k.irq=0; - PicoCpuCM68k.a[7]=PicoCpuCM68k.read32(0); // Stack Pointer - PicoCpuCM68k.membase=0; - PicoCpuCM68k.pc=PicoCpuCM68k.checkpc(PicoCpuCM68k.read32(4)); // Program Counter + CycloneReset(&PicoCpuCM68k); #endif #ifdef EMU_M68K m68k_set_context(&PicoCpuMM68k); // if we ever reset m68k, we always need it's context to be set @@ -189,13 +185,86 @@ PICO_INTERNAL void SekSetRealTAS(int use_real) #endif } +// Pack the cpu into a common format: +// XXX: rename +PICO_INTERNAL void SekPackCpu(unsigned char *cpu, int is_sub) +{ + unsigned int pc=0; + +#if defined(EMU_C68K) + struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k; + memcpy(cpu,context->d,0x40); + pc=context->pc-context->membase; + *(unsigned int *)(cpu+0x44)=CycloneGetSr(context); + *(unsigned int *)(cpu+0x48)=context->osp; + cpu[0x4c] = context->irq; + cpu[0x4d] = context->state_flags & 1; +#elif defined(EMU_M68K) + void *oldcontext = m68ki_cpu_p; + m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k); + memcpy(cpu,m68ki_cpu_p->dar,0x40); + pc=m68ki_cpu_p->pc; + *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR); + *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]; + cpu[0x4c] = CPU_INT_LEVEL>>8; + cpu[0x4d] = CPU_STOPPED; + m68k_set_context(oldcontext); +#elif defined(EMU_F68K) + M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k; + memcpy(cpu,context->dreg,0x40); + pc=context->pc; + *(unsigned int *)(cpu+0x44)=context->sr; + *(unsigned int *)(cpu+0x48)=context->asp; + cpu[0x4c] = context->interrupts[0]; + cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0; +#endif + + *(unsigned int *)(cpu+0x40)=pc; +} + +PICO_INTERNAL void SekUnpackCpu(const unsigned char *cpu, int is_sub) +{ +#if defined(EMU_C68K) + struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k; + CycloneSetSr(context, *(unsigned int *)(cpu+0x44)); + context->osp=*(unsigned int *)(cpu+0x48); + memcpy(context->d,cpu,0x40); + context->membase = 0; + context->pc = *(unsigned int *)(cpu+0x40); + CycloneUnpack(context, NULL); // rebase PC + context->irq = cpu[0x4c]; + context->state_flags = 0; + if (cpu[0x4d]) + context->state_flags |= 1; +#elif defined(EMU_M68K) + void *oldcontext = m68ki_cpu_p; + m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k); + m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44)); + memcpy(m68ki_cpu_p->dar,cpu,0x40); + m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40); + m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]=*(unsigned int *)(cpu+0x48); + CPU_INT_LEVEL = cpu[0x4c] << 8; + CPU_STOPPED = cpu[0x4d]; + m68k_set_context(oldcontext); +#elif defined(EMU_F68K) + M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k; + memcpy(context->dreg,cpu,0x40); + context->pc =*(unsigned int *)(cpu+0x40); + context->sr =*(unsigned int *)(cpu+0x44); + context->asp=*(unsigned int *)(cpu+0x48); + context->interrupts[0] = cpu[0x4c]; + context->execinfo &= ~FM68K_HALTED; + if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED; +#endif +} + /* idle loop detection, not to be used in CD mode */ #ifdef EMU_C68K -#include "cpu/Cyclone/tools/idle.h" +#include "cpu/cyclone/tools/idle.h" #endif -static int *idledet_addrs = NULL; +static unsigned short **idledet_ptrs = NULL; static int idledet_count = 0, idledet_bads = 0; int idledet_start_frame = 0; @@ -220,13 +289,13 @@ void SekRegisterIdleHit(unsigned int pc) void SekInitIdleDet(void) { - void *tmp = realloc(idledet_addrs, 0x200*4); + unsigned short **tmp = realloc(idledet_ptrs, 0x200*4); if (tmp == NULL) { - free(idledet_addrs); - idledet_addrs = NULL; + free(idledet_ptrs); + idledet_ptrs = NULL; } else - idledet_addrs = tmp; + idledet_ptrs = tmp; idledet_count = idledet_bads = 0; idledet_start_frame = Pico.m.frame_count + 360; #ifdef IDLE_STATS @@ -290,6 +359,9 @@ int SekIsIdleCode(unsigned short *dst, int bytes) int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop, void *ctx) { int is_main68k = 1; + u16 *target; + uptr v; + #if defined(EMU_C68K) struct Cyclone *cyc = ctx; is_main68k = cyc == &PicoCpuCM68k; @@ -301,19 +373,24 @@ int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop, void *ctx) elprintf(EL_IDLE, "idle: patch %06x %04x %04x %c %c #%i", pc, oldop, newop, (newop&0x200)?'n':'y', is_main68k?'m':'s', idledet_count); - if (pc > Pico.romsize && !(PicoAHW & PAHW_SVP)) { - if (++idledet_bads > 128) return 2; // remove detector + // XXX: probably shouldn't patch RAM too + v = m68k_read16_map[pc >> M68K_MEM_SHIFT]; + if (!(v & 0x80000000)) + target = (u16 *)((v << 1) + pc); + else { + if (++idledet_bads > 128) + return 2; // remove detector return 1; // don't patch } if (idledet_count >= 0x200 && (idledet_count & 0x1ff) == 0) { - void *tmp = realloc(idledet_addrs, (idledet_count+0x200)*4); - if (tmp == NULL) return 1; - idledet_addrs = tmp; + unsigned short **tmp = realloc(idledet_ptrs, (idledet_count+0x200)*4); + if (tmp == NULL) + return 1; + idledet_ptrs = tmp; } - if (pc < Pico.romsize) - idledet_addrs[idledet_count++] = pc; + idledet_ptrs[idledet_count++] = target; return 0; } @@ -328,7 +405,7 @@ void SekFinishIdleDet(void) #endif while (idledet_count > 0) { - unsigned short *op = (unsigned short *)&Pico.rom[idledet_addrs[--idledet_count]]; + unsigned short *op = idledet_ptrs[--idledet_count]; if ((*op & 0xfd00) == 0x7100) *op &= 0xff, *op |= 0x6600; else if ((*op & 0xfd00) == 0x7500)