| 1 | #include "pico_int.h" |
| 2 | #include "sound/sn76496.h" |
| 3 | |
| 4 | #define Z80_MEM_SHIFT 13 |
| 5 | |
| 6 | unsigned long z80_read_map [0x10000 >> Z80_MEM_SHIFT]; |
| 7 | unsigned long z80_write_map[0x10000 >> Z80_MEM_SHIFT]; |
| 8 | |
| 9 | void MEMH_FUNC z80_map_set(unsigned long *map, int start_addr, int end_addr, |
| 10 | void *func_or_mh, int is_func) |
| 11 | { |
| 12 | unsigned long addr = (unsigned long)func_or_mh; |
| 13 | int mask = (1 << Z80_MEM_SHIFT) - 1; |
| 14 | int i; |
| 15 | |
| 16 | if ((start_addr & mask) != 0 || (end_addr & mask) != mask) |
| 17 | elprintf(EL_STATUS|EL_ANOMALY, "z80_map_set: tried to map bad range: %04x-%04x", |
| 18 | start_addr, end_addr); |
| 19 | |
| 20 | if (addr & 1) { |
| 21 | elprintf(EL_STATUS|EL_ANOMALY, "z80_map_set: ptr is not aligned: %08lx", addr); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | for (i = start_addr >> Z80_MEM_SHIFT; i <= end_addr >> Z80_MEM_SHIFT; i++) |
| 26 | if (is_func) |
| 27 | map[i] = (addr >> 1) | (1 << (sizeof(addr) * 8 - 1)); |
| 28 | else |
| 29 | map[i] = (addr - (i << Z80_MEM_SHIFT)) >> 1; |
| 30 | } |
| 31 | |
| 32 | #ifdef _USE_MZ80 |
| 33 | |
| 34 | // memhandlers for mz80 core |
| 35 | unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); } |
| 36 | void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); } |
| 37 | |
| 38 | // structures for mz80 core |
| 39 | static struct MemoryReadByte mz80_mem_read[]= |
| 40 | { |
| 41 | {0x0000,0xffff,mz80_read}, |
| 42 | {(UINT32) -1,(UINT32) -1,NULL} |
| 43 | }; |
| 44 | static struct MemoryWriteByte mz80_mem_write[]= |
| 45 | { |
| 46 | {0x0000,0xffff,mz80_write}, |
| 47 | {(UINT32) -1,(UINT32) -1,NULL} |
| 48 | }; |
| 49 | static struct z80PortRead mz80_io_read[] ={ |
| 50 | {(UINT16) -1,(UINT16) -1,NULL} |
| 51 | }; |
| 52 | static struct z80PortWrite mz80_io_write[]={ |
| 53 | {(UINT16) -1,(UINT16) -1,NULL} |
| 54 | }; |
| 55 | |
| 56 | int mz80_run(int cycles) |
| 57 | { |
| 58 | int ticks_pre = mz80GetElapsedTicks(0); |
| 59 | mz80exec(cycles); |
| 60 | return mz80GetElapsedTicks(0) - ticks_pre; |
| 61 | } |
| 62 | |
| 63 | #endif |
| 64 | |
| 65 | #ifdef _USE_DRZ80 |
| 66 | struct DrZ80 drZ80; |
| 67 | #endif |
| 68 | |
| 69 | |
| 70 | PICO_INTERNAL void z80_init(void) |
| 71 | { |
| 72 | #ifdef _USE_MZ80 |
| 73 | struct mz80context z80; |
| 74 | |
| 75 | // z80 |
| 76 | mz80init(); |
| 77 | // Modify the default context |
| 78 | mz80GetContext(&z80); |
| 79 | |
| 80 | // point mz80 stuff |
| 81 | z80.z80Base=Pico.zram; |
| 82 | z80.z80MemRead=mz80_mem_read; |
| 83 | z80.z80MemWrite=mz80_mem_write; |
| 84 | z80.z80IoRead=mz80_io_read; |
| 85 | z80.z80IoWrite=mz80_io_write; |
| 86 | |
| 87 | mz80SetContext(&z80); |
| 88 | #endif |
| 89 | #ifdef _USE_DRZ80 |
| 90 | memset(&drZ80, 0, sizeof(drZ80)); |
| 91 | drZ80.z80_rebasePC=NULL; // unused, handled by xmap |
| 92 | drZ80.z80_rebaseSP=NULL; |
| 93 | drZ80.z80_read8 =(void *)z80_read_map; |
| 94 | drZ80.z80_read16 =NULL; |
| 95 | drZ80.z80_write8 =(void *)z80_write_map; |
| 96 | drZ80.z80_write16 =NULL; |
| 97 | drZ80.z80_irq_callback=NULL; |
| 98 | #endif |
| 99 | #ifdef _USE_CZ80 |
| 100 | memset(&CZ80, 0, sizeof(CZ80)); |
| 101 | Cz80_Init(&CZ80); |
| 102 | Cz80_Set_ReadB(&CZ80, NULL); // unused (hacked in) |
| 103 | Cz80_Set_WriteB(&CZ80, NULL); |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | PICO_INTERNAL void z80_reset(void) |
| 108 | { |
| 109 | #ifdef _USE_MZ80 |
| 110 | mz80reset(); |
| 111 | #endif |
| 112 | #ifdef _USE_DRZ80 |
| 113 | memset(&drZ80, 0, 0x54); |
| 114 | drZ80.Z80F = (1<<2); // set ZFlag |
| 115 | drZ80.Z80F2 = (1<<2); // set ZFlag |
| 116 | drZ80.Z80IX = 0xFFFF << 16; |
| 117 | drZ80.Z80IY = 0xFFFF << 16; |
| 118 | drZ80.Z80IM = 0; // 1? |
| 119 | drZ80.z80irqvector = 0xff0000; // RST 38h |
| 120 | drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0] << 1; |
| 121 | // drZ80 is locked in single bank |
| 122 | drZ80.Z80SP_BASE = ((PicoAHW & PAHW_SMS) ? |
| 123 | z80_read_map[0xc000 >> Z80_MEM_SHIFT] : z80_read_map[0]) << 1; |
| 124 | // drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ? |
| 125 | #endif |
| 126 | #ifdef _USE_CZ80 |
| 127 | Cz80_Reset(&CZ80); |
| 128 | Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff); |
| 129 | Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff); |
| 130 | Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000); |
| 131 | #endif |
| 132 | Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled |
| 133 | } |
| 134 | |
| 135 | // XXX TODO: should better use universal z80 save format |
| 136 | PICO_INTERNAL void z80_pack(unsigned char *data) |
| 137 | { |
| 138 | #if defined(_USE_MZ80) |
| 139 | struct mz80context mz80; |
| 140 | *(int *)data = 0x00005A6D; // "mZ" |
| 141 | mz80GetContext(&mz80); |
| 142 | memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers |
| 143 | #elif defined(_USE_DRZ80) |
| 144 | *(int *)data = 0x015A7244; // "DrZ" v1 |
| 145 | // drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE); |
| 146 | // drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE); |
| 147 | memcpy(data+4, &drZ80, 0x54); |
| 148 | #elif defined(_USE_CZ80) |
| 149 | *(int *)data = 0x00007a43; // "Cz" |
| 150 | *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC); |
| 151 | memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80); |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | PICO_INTERNAL void z80_unpack(unsigned char *data) |
| 156 | { |
| 157 | #if defined(_USE_MZ80) |
| 158 | if (*(int *)data == 0x00005A6D) { // "mZ" save? |
| 159 | struct mz80context mz80; |
| 160 | mz80GetContext(&mz80); |
| 161 | memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4); |
| 162 | mz80SetContext(&mz80); |
| 163 | } else { |
| 164 | z80_reset(); |
| 165 | z80_int(); |
| 166 | } |
| 167 | #elif defined(_USE_DRZ80) |
| 168 | if (*(int *)data == 0x015A7244) { // "DrZ" v1 save? |
| 169 | int pc, sp; |
| 170 | memcpy(&drZ80, data+4, 0x54); |
| 171 | pc = (drZ80.Z80PC - drZ80.Z80PC_BASE) & 0xffff; |
| 172 | sp = (drZ80.Z80SP - drZ80.Z80SP_BASE) & 0xffff; |
| 173 | // update bases |
| 174 | drZ80.Z80PC_BASE = z80_read_map[pc >> Z80_MEM_SHIFT]; |
| 175 | if (drZ80.Z80PC & (1<<31)) { |
| 176 | elprintf(EL_STATUS|EL_ANOMALY, "bad PC in z80 save: %04x", pc); |
| 177 | drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0]; |
| 178 | } else { |
| 179 | drZ80.Z80PC_BASE <<= 1; |
| 180 | drZ80.Z80PC = drZ80.Z80PC_BASE + pc; |
| 181 | } |
| 182 | drZ80.Z80SP_BASE = z80_read_map[sp >> Z80_MEM_SHIFT]; |
| 183 | if (drZ80.Z80SP & (1<<31)) { |
| 184 | elprintf(EL_STATUS|EL_ANOMALY, "bad SP in z80 save: %04x", sp); |
| 185 | drZ80.Z80SP_BASE = z80_read_map[0]; |
| 186 | drZ80.Z80SP = drZ80.Z80SP_BASE + (1 << Z80_MEM_SHIFT); |
| 187 | } else { |
| 188 | drZ80.Z80SP_BASE <<= 1; |
| 189 | drZ80.Z80SP = drZ80.Z80SP_BASE + sp; |
| 190 | } |
| 191 | } else { |
| 192 | z80_reset(); |
| 193 | drZ80.Z80IM = 1; |
| 194 | z80_int(); // try to goto int handler, maybe we won't execute trash there? |
| 195 | } |
| 196 | #elif defined(_USE_CZ80) |
| 197 | if (*(int *)data == 0x00007a43) { // "Cz" save? |
| 198 | memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80); |
| 199 | Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4)); |
| 200 | } else { |
| 201 | z80_reset(); |
| 202 | z80_int(); |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | PICO_INTERNAL void z80_exit(void) |
| 208 | { |
| 209 | #if defined(_USE_MZ80) |
| 210 | mz80shutdown(); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | PICO_INTERNAL void z80_debug(char *dstr) |
| 215 | { |
| 216 | #if defined(_USE_DRZ80) |
| 217 | sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE); |
| 218 | #elif defined(_USE_CZ80) |
| 219 | sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W); |
| 220 | #endif |
| 221 | } |