5 uptr z80_read_map [0x10000 >> Z80_MEM_SHIFT];
6 uptr z80_write_map[0x10000 >> Z80_MEM_SHIFT];
10 // memhandlers for mz80 core
11 unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }
12 void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }
14 // structures for mz80 core
15 static struct MemoryReadByte mz80_mem_read[]=
17 {0x0000,0xffff,mz80_read},
18 {(UINT32) -1,(UINT32) -1,NULL}
20 static struct MemoryWriteByte mz80_mem_write[]=
22 {0x0000,0xffff,mz80_write},
23 {(UINT32) -1,(UINT32) -1,NULL}
25 static struct z80PortRead mz80_io_read[] ={
26 {(UINT16) -1,(UINT16) -1,NULL}
28 static struct z80PortWrite mz80_io_write[]={
29 {(UINT16) -1,(UINT16) -1,NULL}
32 int mz80_run(int cycles)
34 int ticks_pre = mz80GetElapsedTicks(0);
36 return mz80GetElapsedTicks(0) - ticks_pre;
46 PICO_INTERNAL void z80_init(void)
49 struct mz80context z80;
53 // Modify the default context
57 z80.z80Base=Pico.zram;
58 z80.z80MemRead=mz80_mem_read;
59 z80.z80MemWrite=mz80_mem_write;
60 z80.z80IoRead=mz80_io_read;
61 z80.z80IoWrite=mz80_io_write;
66 memset(&drZ80, 0, sizeof(drZ80));
67 drZ80.z80_rebasePC=NULL; // unused, handled by xmap
68 drZ80.z80_rebaseSP=NULL;
69 drZ80.z80_read8 =(void *)z80_read_map;
70 drZ80.z80_read16 =NULL;
71 drZ80.z80_write8 =(void *)z80_write_map;
72 drZ80.z80_write16 =NULL;
73 drZ80.z80_irq_callback=NULL;
76 memset(&CZ80, 0, sizeof(CZ80));
78 Cz80_Set_ReadB(&CZ80, NULL); // unused (hacked in)
79 Cz80_Set_WriteB(&CZ80, NULL);
83 PICO_INTERNAL void z80_reset(void)
89 memset(&drZ80, 0, 0x54);
90 drZ80.Z80F = (1<<2); // set ZFlag
91 drZ80.Z80F2 = (1<<2); // set ZFlag
92 drZ80.Z80IX = 0xFFFF << 16;
93 drZ80.Z80IY = 0xFFFF << 16;
94 drZ80.Z80IM = 0; // 1?
95 drZ80.z80irqvector = 0xff0000; // RST 38h
96 drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0] << 1;
97 // drZ80 is locked in single bank
98 drZ80.Z80SP_BASE = ((PicoAHW & PAHW_SMS) ?
99 z80_read_map[0xc000 >> Z80_MEM_SHIFT] : z80_read_map[0]) << 1;
100 // drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?
104 Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);
105 Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);
106 Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);
110 // XXX TODO: should better use universal z80 save format
111 PICO_INTERNAL void z80_pack(unsigned char *data)
113 #if defined(_USE_MZ80)
114 struct mz80context mz80;
115 *(int *)data = 0x00005A6D; // "mZ"
116 mz80GetContext(&mz80);
117 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers
118 #elif defined(_USE_DRZ80)
119 *(int *)data = 0x015A7244; // "DrZ" v1
120 // drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
121 // drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
122 memcpy(data+4, &drZ80, 0x54);
123 #elif defined(_USE_CZ80)
124 *(int *)data = 0x00007a43; // "Cz"
125 *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);
126 memcpy(data+8, &CZ80, offsetof(cz80_struc, BasePC));
130 PICO_INTERNAL void z80_unpack(unsigned char *data)
132 #if defined(_USE_MZ80)
133 if (*(int *)data == 0x00005A6D) { // "mZ" save?
134 struct mz80context mz80;
135 mz80GetContext(&mz80);
136 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);
137 mz80SetContext(&mz80);
142 #elif defined(_USE_DRZ80)
143 if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?
145 memcpy(&drZ80, data+4, 0x54);
146 pc = (drZ80.Z80PC - drZ80.Z80PC_BASE) & 0xffff;
147 sp = (drZ80.Z80SP - drZ80.Z80SP_BASE) & 0xffff;
149 drZ80.Z80PC_BASE = z80_read_map[pc >> Z80_MEM_SHIFT];
150 if (drZ80.Z80PC & (1<<31)) {
151 elprintf(EL_STATUS|EL_ANOMALY, "bad PC in z80 save: %04x", pc);
152 drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0];
154 drZ80.Z80PC_BASE <<= 1;
155 drZ80.Z80PC = drZ80.Z80PC_BASE + pc;
157 drZ80.Z80SP_BASE = z80_read_map[sp >> Z80_MEM_SHIFT];
158 if (drZ80.Z80SP & (1<<31)) {
159 elprintf(EL_STATUS|EL_ANOMALY, "bad SP in z80 save: %04x", sp);
160 drZ80.Z80SP_BASE = z80_read_map[0];
161 drZ80.Z80SP = drZ80.Z80SP_BASE + (1 << Z80_MEM_SHIFT);
163 drZ80.Z80SP_BASE <<= 1;
164 drZ80.Z80SP = drZ80.Z80SP_BASE + sp;
169 z80_int(); // try to goto int handler, maybe we won't execute trash there?
171 #elif defined(_USE_CZ80)
172 if (*(int *)data == 0x00007a43) { // "Cz" save?
173 memcpy(&CZ80, data+8, offsetof(cz80_struc, BasePC));
174 Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));
182 PICO_INTERNAL void z80_exit(void)
184 #if defined(_USE_MZ80)
189 PICO_INTERNAL void z80_debug(char *dstr)
191 #if defined(_USE_DRZ80)
192 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);
193 #elif defined(_USE_CZ80)
194 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", (unsigned int)(CZ80.PC - CZ80.BasePC), CZ80.SP.W);