c8d1e9b6 |
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 |
c8d1e9b6 |
66 | struct DrZ80 drZ80; |
c8d1e9b6 |
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)); |
ee05564f |
91 | drZ80.z80_rebasePC=NULL; // unused, handled by xmap |
92 | drZ80.z80_rebaseSP=NULL; |
c8d1e9b6 |
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 |
ee05564f |
120 | drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0] << 1; |
d8f51995 |
121 | // drZ80 is locked in single bank |
ee05564f |
122 | drZ80.Z80SP_BASE = z80_read_map[0] << 1; |
123 | // drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ? |
c8d1e9b6 |
124 | #endif |
125 | #ifdef _USE_CZ80 |
126 | Cz80_Reset(&CZ80); |
127 | Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff); |
128 | Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff); |
129 | Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000); |
130 | #endif |
131 | Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled |
132 | } |
133 | |
ee05564f |
134 | // XXX TODO: should better use universal z80 save format |
c8d1e9b6 |
135 | PICO_INTERNAL void z80_pack(unsigned char *data) |
136 | { |
137 | #if defined(_USE_MZ80) |
138 | struct mz80context mz80; |
139 | *(int *)data = 0x00005A6D; // "mZ" |
140 | mz80GetContext(&mz80); |
141 | memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers |
142 | #elif defined(_USE_DRZ80) |
143 | *(int *)data = 0x015A7244; // "DrZ" v1 |
ee05564f |
144 | // drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE); |
145 | // drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE); |
c8d1e9b6 |
146 | memcpy(data+4, &drZ80, 0x54); |
147 | #elif defined(_USE_CZ80) |
148 | *(int *)data = 0x00007a43; // "Cz" |
149 | *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC); |
150 | memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80); |
151 | #endif |
152 | } |
153 | |
154 | PICO_INTERNAL void z80_unpack(unsigned char *data) |
155 | { |
156 | #if defined(_USE_MZ80) |
157 | if (*(int *)data == 0x00005A6D) { // "mZ" save? |
158 | struct mz80context mz80; |
159 | mz80GetContext(&mz80); |
160 | memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4); |
161 | mz80SetContext(&mz80); |
162 | } else { |
163 | z80_reset(); |
164 | z80_int(); |
165 | } |
166 | #elif defined(_USE_DRZ80) |
167 | if (*(int *)data == 0x015A7244) { // "DrZ" v1 save? |
ee05564f |
168 | int pc, sp; |
c8d1e9b6 |
169 | memcpy(&drZ80, data+4, 0x54); |
ee05564f |
170 | pc = (drZ80.Z80PC - drZ80.Z80PC_BASE) & 0xffff; |
171 | sp = (drZ80.Z80SP - drZ80.Z80SP_BASE) & 0xffff; |
c8d1e9b6 |
172 | // update bases |
ee05564f |
173 | drZ80.Z80PC_BASE = z80_read_map[pc >> Z80_MEM_SHIFT]; |
174 | if (drZ80.Z80PC & (1<<31)) { |
175 | elprintf(EL_STATUS|EL_ANOMALY, "bad PC in z80 save: %04x", pc); |
176 | drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0]; |
177 | } else { |
178 | drZ80.Z80PC_BASE <<= 1; |
179 | drZ80.Z80PC = drZ80.Z80PC_BASE + pc; |
180 | } |
181 | drZ80.Z80SP_BASE = z80_read_map[sp >> Z80_MEM_SHIFT]; |
182 | if (drZ80.Z80SP & (1<<31)) { |
183 | elprintf(EL_STATUS|EL_ANOMALY, "bad SP in z80 save: %04x", sp); |
184 | drZ80.Z80SP_BASE = z80_read_map[0]; |
185 | drZ80.Z80SP = drZ80.Z80SP_BASE + (1 << Z80_MEM_SHIFT); |
186 | } else { |
187 | drZ80.Z80SP_BASE <<= 1; |
188 | drZ80.Z80SP = drZ80.Z80SP_BASE + sp; |
189 | } |
c8d1e9b6 |
190 | } else { |
191 | z80_reset(); |
192 | drZ80.Z80IM = 1; |
193 | z80_int(); // try to goto int handler, maybe we won't execute trash there? |
194 | } |
195 | #elif defined(_USE_CZ80) |
196 | if (*(int *)data == 0x00007a43) { // "Cz" save? |
197 | memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80); |
198 | Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4)); |
199 | } else { |
200 | z80_reset(); |
201 | z80_int(); |
202 | } |
203 | #endif |
204 | } |
205 | |
206 | PICO_INTERNAL void z80_exit(void) |
207 | { |
208 | #if defined(_USE_MZ80) |
209 | mz80shutdown(); |
210 | #endif |
211 | } |
212 | |
213 | PICO_INTERNAL void z80_debug(char *dstr) |
214 | { |
215 | #if defined(_USE_DRZ80) |
216 | sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE); |
217 | #elif defined(_USE_CZ80) |
218 | sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W); |
219 | #endif |
220 | } |