2 #include "sound/sn76496.h"
4 #define Z80_MEM_SHIFT 13
6 unsigned long z80_read_map [0x10000 >> Z80_MEM_SHIFT];
7 unsigned long z80_write_map[0x10000 >> Z80_MEM_SHIFT];
11 // memhandlers for mz80 core
12 unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }
13 void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }
15 // structures for mz80 core
16 static struct MemoryReadByte mz80_mem_read[]=
18 {0x0000,0xffff,mz80_read},
19 {(UINT32) -1,(UINT32) -1,NULL}
21 static struct MemoryWriteByte mz80_mem_write[]=
23 {0x0000,0xffff,mz80_write},
24 {(UINT32) -1,(UINT32) -1,NULL}
26 static struct z80PortRead mz80_io_read[] ={
27 {(UINT16) -1,(UINT16) -1,NULL}
29 static struct z80PortWrite mz80_io_write[]={
30 {(UINT16) -1,(UINT16) -1,NULL}
33 int mz80_run(int cycles)
35 int ticks_pre = mz80GetElapsedTicks(0);
37 return mz80GetElapsedTicks(0) - ticks_pre;
47 PICO_INTERNAL void z80_init(void)
50 struct mz80context z80;
54 // Modify the default context
58 z80.z80Base=Pico.zram;
59 z80.z80MemRead=mz80_mem_read;
60 z80.z80MemWrite=mz80_mem_write;
61 z80.z80IoRead=mz80_io_read;
62 z80.z80IoWrite=mz80_io_write;
67 memset(&drZ80, 0, sizeof(drZ80));
68 drZ80.z80_rebasePC=NULL; // unused, handled by xmap
69 drZ80.z80_rebaseSP=NULL;
70 drZ80.z80_read8 =(void *)z80_read_map;
71 drZ80.z80_read16 =NULL;
72 drZ80.z80_write8 =(void *)z80_write_map;
73 drZ80.z80_write16 =NULL;
74 drZ80.z80_irq_callback=NULL;
77 memset(&CZ80, 0, sizeof(CZ80));
79 Cz80_Set_ReadB(&CZ80, NULL); // unused (hacked in)
80 Cz80_Set_WriteB(&CZ80, NULL);
84 PICO_INTERNAL void z80_reset(void)
90 memset(&drZ80, 0, 0x54);
91 drZ80.Z80F = (1<<2); // set ZFlag
92 drZ80.Z80F2 = (1<<2); // set ZFlag
93 drZ80.Z80IX = 0xFFFF << 16;
94 drZ80.Z80IY = 0xFFFF << 16;
95 drZ80.Z80IM = 0; // 1?
96 drZ80.z80irqvector = 0xff0000; // RST 38h
97 drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0] << 1;
98 // drZ80 is locked in single bank
99 drZ80.Z80SP_BASE = ((PicoAHW & PAHW_SMS) ?
100 z80_read_map[0xc000 >> Z80_MEM_SHIFT] : z80_read_map[0]) << 1;
101 // drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?
105 Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);
106 Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);
107 Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);
111 // XXX TODO: should better use universal z80 save format
112 PICO_INTERNAL void z80_pack(unsigned char *data)
114 #if defined(_USE_MZ80)
115 struct mz80context mz80;
116 *(int *)data = 0x00005A6D; // "mZ"
117 mz80GetContext(&mz80);
118 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers
119 #elif defined(_USE_DRZ80)
120 *(int *)data = 0x015A7244; // "DrZ" v1
121 // drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
122 // drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
123 memcpy(data+4, &drZ80, 0x54);
124 #elif defined(_USE_CZ80)
125 *(int *)data = 0x00007a43; // "Cz"
126 *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);
127 memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80);
131 PICO_INTERNAL void z80_unpack(unsigned char *data)
133 #if defined(_USE_MZ80)
134 if (*(int *)data == 0x00005A6D) { // "mZ" save?
135 struct mz80context mz80;
136 mz80GetContext(&mz80);
137 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);
138 mz80SetContext(&mz80);
143 #elif defined(_USE_DRZ80)
144 if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?
146 memcpy(&drZ80, data+4, 0x54);
147 pc = (drZ80.Z80PC - drZ80.Z80PC_BASE) & 0xffff;
148 sp = (drZ80.Z80SP - drZ80.Z80SP_BASE) & 0xffff;
150 drZ80.Z80PC_BASE = z80_read_map[pc >> Z80_MEM_SHIFT];
151 if (drZ80.Z80PC & (1<<31)) {
152 elprintf(EL_STATUS|EL_ANOMALY, "bad PC in z80 save: %04x", pc);
153 drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0];
155 drZ80.Z80PC_BASE <<= 1;
156 drZ80.Z80PC = drZ80.Z80PC_BASE + pc;
158 drZ80.Z80SP_BASE = z80_read_map[sp >> Z80_MEM_SHIFT];
159 if (drZ80.Z80SP & (1<<31)) {
160 elprintf(EL_STATUS|EL_ANOMALY, "bad SP in z80 save: %04x", sp);
161 drZ80.Z80SP_BASE = z80_read_map[0];
162 drZ80.Z80SP = drZ80.Z80SP_BASE + (1 << Z80_MEM_SHIFT);
164 drZ80.Z80SP_BASE <<= 1;
165 drZ80.Z80SP = drZ80.Z80SP_BASE + sp;
170 z80_int(); // try to goto int handler, maybe we won't execute trash there?
172 #elif defined(_USE_CZ80)
173 if (*(int *)data == 0x00007a43) { // "Cz" save?
174 memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80);
175 Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));
183 PICO_INTERNAL void z80_exit(void)
185 #if defined(_USE_MZ80)
190 PICO_INTERNAL void z80_debug(char *dstr)
192 #if defined(_USE_DRZ80)
193 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);
194 #elif defined(_USE_CZ80)
195 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W);