X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2Fmemory.c;h=3900208bf6475619df0e89b8f665251cf7ee8e53;hb=d1b8bcc63466f6982685df518db8fef399f9cf91;hp=88d43f0efed9ff58f3d6a6894091ab5f5ad075de;hpb=faf543cebe3f5e93d39f1fdb7e209ce32c954e56;p=picodrive.git diff --git a/pico/memory.c b/pico/memory.c index 88d43f0..3900208 100644 --- a/pico/memory.c +++ b/pico/memory.c @@ -49,7 +49,7 @@ static void xmap_set(uptr *map, int shift, int start_addr, int end_addr, for (i = start_addr >> shift; i <= end_addr >> shift; i++) { map[i] = addr >> 1; if (is_func) - map[i] |= (uptr)1 << (sizeof(addr) * 8 - 1); + map[i] |= MAP_FLAG; } } @@ -63,6 +63,17 @@ void cpu68k_map_set(uptr *map, int start_addr, int end_addr, const void *func_or_mh, int is_func) { xmap_set(map, M68K_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func); +#ifdef EMU_F68K + // setup FAME fetchmap + if (!is_func) + { + int shiftout = 24 - FAMEC_FETCHBITS; + int i = start_addr >> shiftout; + uptr base = (uptr)func_or_mh - (i << shiftout); + for (; i <= (end_addr >> shiftout); i++) + PicoCpuFM68k.Fetch[i] = base; + } +#endif } // more specialized/optimized function (does same as above) @@ -89,6 +100,17 @@ void cpu68k_map_all_ram(int start_addr, int end_addr, void *ptr, int is_sub) addr >>= 1; for (i = start_addr >> shift; i <= end_addr >> shift; i++) r8map[i] = r16map[i] = w8map[i] = w16map[i] = addr; +#ifdef EMU_F68K + // setup FAME fetchmap + { + M68K_CONTEXT *ctx = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k; + int shiftout = 24 - FAMEC_FETCHBITS; + i = start_addr >> shiftout; + addr = (uptr)ptr - (i << shiftout); + for (; i <= (end_addr >> shiftout); i++) + ctx->Fetch[i] = addr; + } +#endif } static u32 m68k_unmapped_read8(u32 a) @@ -126,19 +148,19 @@ void m68k_map_unmap(int start_addr, int end_addr) addr = (uptr)m68k_unmapped_read8; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_read8_map[i] = (addr >> 1) | (1 << 31); + m68k_read8_map[i] = (addr >> 1) | MAP_FLAG; addr = (uptr)m68k_unmapped_read16; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_read16_map[i] = (addr >> 1) | (1 << 31); + m68k_read16_map[i] = (addr >> 1) | MAP_FLAG; addr = (uptr)m68k_unmapped_write8; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_write8_map[i] = (addr >> 1) | (1 << 31); + m68k_write8_map[i] = (addr >> 1) | MAP_FLAG; addr = (uptr)m68k_unmapped_write16; for (i = start_addr >> shift; i <= end_addr >> shift; i++) - m68k_write16_map[i] = (addr >> 1) | (1 << 31); + m68k_write16_map[i] = (addr >> 1) | MAP_FLAG; } MAKE_68K_READ8(m68k_read8, m68k_read8_map) @@ -186,62 +208,119 @@ void cyclone_crashed(u32 pc, struct Cyclone *context) // ----------------------------------------------------------------- // memmap helpers -#ifndef _ASM_MEMORY_C -static -#endif -int PadRead(int i) +static u32 read_pad_3btn(int i, u32 out_bits) { - int pad,value,data_reg; - pad=~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU - data_reg=Pico.ioports[i+1]; + u32 pad = ~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU + u32 value; - // orr the bits, which are set as output - value = data_reg&(Pico.ioports[i+4]|0x80); + if (out_bits & 0x40) // TH + value = pad & 0x3f; // ?1CB RLDU + else + value = ((pad & 0xc0) >> 2) | (pad & 3); // ?0SA 00DU - if (PicoOpt & POPT_6BTN_PAD) - { - int phase = Pico.m.padTHPhase[i]; - - if(phase == 2 && !(data_reg&0x40)) { // TH - value|=(pad&0xc0)>>2; // ?0SA 0000 - return value; - } else if(phase == 3) { - if(data_reg&0x40) - value|=(pad&0x30)|((pad>>8)&0xf); // ?1CB MXYZ - else - value|=((pad&0xc0)>>2)|0x0f; // ?0SA 1111 - return value; - } + value |= out_bits & 0x40; + return value; +} + +static u32 read_pad_6btn(int i, u32 out_bits) +{ + u32 pad = ~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU + int phase = Pico.m.padTHPhase[i]; + u32 value; + + if (phase == 2 && !(out_bits & 0x40)) { + value = (pad & 0xc0) >> 2; // ?0SA 0000 + goto out; + } + else if(phase == 3) { + if (out_bits & 0x40) + return (pad & 0x30) | ((pad >> 8) & 0xf); // ?1CB MXYZ + else + return ((pad & 0xc0) >> 2) | 0x0f; // ?0SA 1111 + goto out; } - if(data_reg&0x40) // TH - value|=(pad&0x3f); // ?1CB RLDU - else value|=((pad&0xc0)>>2)|(pad&3); // ?0SA 00DU + if (out_bits & 0x40) // TH + value = pad & 0x3f; // ?1CB RLDU + else + value = ((pad & 0xc0) >> 2) | (pad & 3); // ?0SA 00DU - return value; // will mirror later +out: + value |= out_bits & 0x40; + return value; } -#ifndef _ASM_MEMORY_C +static u32 read_nothing(int i, u32 out_bits) +{ + return 0xff; +} + +typedef u32 (port_read_func)(int index, u32 out_bits); + +static port_read_func *port_readers[3] = { + read_pad_3btn, + read_pad_3btn, + read_nothing +}; + +static NOINLINE u32 port_read(int i) +{ + u32 data_reg = Pico.ioports[i + 1]; + u32 ctrl_reg = Pico.ioports[i + 4] | 0x80; + u32 in, out; + + out = data_reg & ctrl_reg; + out |= 0x7f & ~ctrl_reg; // pull-ups + + in = port_readers[i](i, out); + + return (in & ~ctrl_reg) | (data_reg & ctrl_reg); +} + +void PicoSetInputDevice(int port, enum input_device device) +{ + port_read_func *func; + + if (port < 0 || port > 2) + return; -static u32 io_ports_read(u32 a) + switch (device) { + case PICO_INPUT_PAD_3BTN: + func = read_pad_3btn; + break; + + case PICO_INPUT_PAD_6BTN: + func = read_pad_6btn; + break; + + default: + func = read_nothing; + break; + } + + port_readers[port] = func; +} + +NOINLINE u32 io_ports_read(u32 a) { u32 d; a = (a>>1) & 0xf; switch (a) { case 0: d = Pico.m.hardware; break; // Hardware value (Version register) - case 1: d = PadRead(0); break; - case 2: d = PadRead(1); break; + case 1: d = port_read(0); break; + case 2: d = port_read(1); break; + case 3: d = port_read(2); break; default: d = Pico.ioports[a]; break; // IO ports can be used as RAM } return d; } -static void NOINLINE io_ports_write(u32 a, u32 d) +NOINLINE void io_ports_write(u32 a, u32 d) { a = (a>>1) & 0xf; // 6 button gamepad: if TH went from 0 to 1, gamepad changes state - if (1 <= a && a <= 2 && (PicoOpt & POPT_6BTN_PAD)) + if (1 <= a && a <= 2) { Pico.m.padDelay[a - 1] = 0; if (!(Pico.ioports[a] & 0x40) && (d & 0x40)) @@ -252,7 +331,12 @@ static void NOINLINE io_ports_write(u32 a, u32 d) Pico.ioports[a] = d; } -#endif // _ASM_MEMORY_C +// lame.. +static int z80_cycles_from_68k(void) +{ + return z80_cycle_aim + + cycles_68k_to_z80(SekCyclesDone() - last_z80_sync); +} void NOINLINE ctl_write_z80busreq(u32 d) { @@ -262,14 +346,13 @@ void NOINLINE ctl_write_z80busreq(u32 d) { if (d) { - z80_cycle_cnt = cycles_68k_to_z80(SekCyclesDone()); + z80_cycle_cnt = z80_cycles_from_68k(); } else { - z80stopCycle = SekCyclesDone(); if ((PicoOpt&POPT_EN_Z80) && !Pico.m.z80_reset) { pprof_start(m68k); - PicoSyncZ80(z80stopCycle); + PicoSyncZ80(SekCyclesDone()); pprof_end_sub(m68k); } } @@ -295,13 +378,35 @@ void NOINLINE ctl_write_z80reset(u32 d) } else { - z80_cycle_cnt = cycles_68k_to_z80(SekCyclesDone()); + z80_cycle_cnt = z80_cycles_from_68k(); z80_reset(); } Pico.m.z80_reset = d; } } +static int get_scanline(int is_from_z80); + +static void psg_write_68k(u32 d) +{ + // look for volume write and update if needed + if ((d & 0x90) == 0x90 && PsndPsgLine < Pico.m.scanline) + PsndDoPSG(Pico.m.scanline); + + SN76496Write(d); +} + +static void psg_write_z80(u32 d) +{ + if ((d & 0x90) == 0x90) { + int scanline = get_scanline(1); + if (PsndPsgLine < scanline) + PsndDoPSG(scanline); + } + + SN76496Write(d); +} + // ----------------------------------------------------------------- #ifndef _ASM_MEMORY_C @@ -387,11 +492,14 @@ static void PicoWrite16_sram(u32 a, u32 d) EEPROM_write16(d); } else { - // XXX: hardware could easily use MSB too.. u8 *pm = (u8 *)(SRam.data - SRam.start + a); - if (*pm != (u8)d) { + if (pm[0] != (u8)(d >> 8)) { SRam.changed = 1; - *pm = (u8)d; + pm[0] = (u8)(d >> 8); + } + if (pm[1] != (u8)d) { + SRam.changed = 1; + pm[1] = (u8)d; } } } @@ -431,7 +539,7 @@ static void PicoWrite8_z80(u32 a, u32 d) } if ((a & 0x4000) == 0x0000) { // z80 RAM - SekCyclesBurn(2); // hack + SekCyclesBurnRun(2); // FIXME hack Pico.zram[a & 0x1fff] = (u8)d; return; } @@ -442,8 +550,7 @@ static void PicoWrite8_z80(u32 a, u32 d) } // TODO: probably other VDP access too? Maybe more mirrors? if ((a & 0x7ff9) == 0x7f11) { // PSG Sound - if (PicoOpt & POPT_EN_PSG) - SN76496Write(d); + psg_write_68k(d); return; } if ((a & 0x7f00) == 0x6000) // Z80 BANK register @@ -596,10 +703,21 @@ void PicoWrite16_io(u32 a, u32 d) // VDP area (0xc00000 - 0xdfffff) // TODO: verify if lower byte goes to PSG on word writes -static u32 PicoRead8_vdp(u32 a) +u32 PicoRead8_vdp(u32 a) { - if ((a & 0x00e0) == 0x0000) - return PicoVideoRead8(a); + if ((a & 0x00f0) == 0x0000) { + switch (a & 0x0d) + { + case 0x00: return PicoVideoRead8DataH(); + case 0x01: return PicoVideoRead8DataL(); + case 0x04: return PicoVideoRead8CtlH(); + case 0x05: return PicoVideoRead8CtlL(); + case 0x08: + case 0x0c: return PicoVideoRead8HV_H(); + case 0x09: + case 0x0d: return PicoVideoRead8HV_L(); + } + } elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc); return 0; @@ -617,8 +735,7 @@ static u32 PicoRead16_vdp(u32 a) static void PicoWrite8_vdp(u32 a, u32 d) { if ((a & 0x00f9) == 0x0011) { // PSG Sound - if (PicoOpt & POPT_EN_PSG) - SN76496Write(d); + psg_write_68k(d); return; } if ((a & 0x00e0) == 0x0000) { @@ -632,11 +749,8 @@ static void PicoWrite8_vdp(u32 a, u32 d) static void PicoWrite16_vdp(u32 a, u32 d) { - if ((a & 0x00f9) == 0x0010) { // PSG Sound - if (PicoOpt & POPT_EN_PSG) - SN76496Write(d); - return; - } + if ((a & 0x00f9) == 0x0010) // PSG Sound + psg_write_68k(d); if ((a & 0x00e0) == 0x0000) { PicoVideoWrite(a, d); return; @@ -735,14 +849,12 @@ PICO_INTERNAL void PicoMemSetup(void) { int i; // by default, point everything to first 64k of ROM - for (i = 0; i < M68K_FETCHBANK1; i++) + for (i = 0; i < M68K_FETCHBANK1 * 0xe0 / 0x100; i++) PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.rom - (i<<(24-FAMEC_FETCHBITS)); // now real ROM for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++) PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.rom; - // .. and RAM - for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++) - PicoCpuFM68k.Fetch[i] = (unsigned long)Pico.ram - (i<<(24-FAMEC_FETCHBITS)); + // RAM already set } #endif #ifdef EMU_M68K @@ -848,7 +960,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; } @@ -885,7 +997,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) timer_a_step = TIMER_A_TICK_ZCYCLES * (1024 - TAnew); if (ym2612.OPN.ST.mode & 1) { // this is not right, should really be done on overflow only - int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone()); + int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k(); timer_a_next_oflow = (cycles << 8) + timer_a_step; } elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, timer_a_next_oflow>>8); @@ -900,7 +1012,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) //ym2612.OPN.ST.TBT = 0; timer_b_step = TIMER_B_TICK_ZCYCLES * (256 - d); // 262800 if (ym2612.OPN.ST.mode & 2) { - int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone()); + int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k(); timer_b_next_oflow = (cycles << 8) + timer_b_step; } elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, timer_b_next_oflow>>8); @@ -908,7 +1020,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80) return 0; case 0x27: { /* mode, timer control */ int old_mode = ym2612.OPN.ST.mode; - int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone()); + int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k(); ym2612.OPN.ST.mode = d; elprintf(EL_YMTIMER, "st mode %02x", d); @@ -932,8 +1044,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 @@ -986,7 +1100,7 @@ static u32 ym2612_read_local_z80(void) static u32 ym2612_read_local_68k(void) { - int xcycles = cycles_68k_to_z80(SekCyclesDone()) << 8; + int xcycles = z80_cycles_from_68k() << 8; ym2612_read_local(); @@ -1081,7 +1195,22 @@ void PicoWrite16_32x(u32 a, u32 d) {} static unsigned char z80_md_vdp_read(unsigned short a) { - // TODO? + z80_subCLeft(2); + + if ((a & 0x00f0) == 0x0000) { + switch (a & 0x0d) + { + case 0x00: return PicoVideoRead8DataH(); + case 0x01: return PicoVideoRead8DataL(); + case 0x04: return PicoVideoRead8CtlH(); + case 0x05: return PicoVideoRead8CtlL(); + case 0x08: + case 0x0c: return get_scanline(1); // FIXME: make it proper + case 0x09: + case 0x0d: return Pico.m.rotate++; + } + } + elprintf(EL_ANOMALY, "z80 invalid r8 [%06x] %02x", a, 0xff); return 0xff; } @@ -1091,8 +1220,10 @@ static unsigned char z80_md_bank_read(unsigned short a) unsigned int addr68k; unsigned char ret; - addr68k = Pico.m.z80_bank68k<<15; - addr68k += a & 0x7fff; + z80_subCLeft(3); + + addr68k = Pico.m.z80_bank68k << 15; + addr68k |= a & 0x7fff; ret = m68k_read8(addr68k); @@ -1108,13 +1239,12 @@ static void z80_md_ym2612_write(unsigned int a, unsigned char data) static void z80_md_vdp_br_write(unsigned int a, unsigned char data) { - // TODO: allow full VDP access if ((a&0xfff9) == 0x7f11) // 7f11 7f13 7f15 7f17 { - if (PicoOpt & POPT_EN_PSG) - SN76496Write(data); + psg_write_z80(data); return; } + // at least VDP data writes hang my machine if ((a>>8) == 0x60) { @@ -1177,3 +1307,4 @@ static void z80_mem_setup(void) #endif } +// vim:shiftwidth=2:ts=2:expandtab