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