improved irq handling Cz80_Exec() calls with lots of cycles
[picodrive.git] / Pico / sound / sound.c
CommitLineData
cc68a136 1// This is part of Pico Library\r
2\r
3// (c) Copyright 2004 Dave, All rights reserved.\r
b542be46 4// (c) Copyright 2006,2007 notaz, All rights reserved.\r
cc68a136 5// Free for non-commercial use.\r
6\r
7// For commercial use, separate licencing terms must be obtained.\r
8\r
9\r
10#include <string.h>\r
cc68a136 11#include "ym2612.h"\r
12#include "sn76496.h"\r
13\r
cc68a136 14#include "../PicoInt.h"\r
4f265db7 15#include "../cd/pcm.h"\r
16#include "mix.h"\r
cc68a136 17\r
4a32f01f 18void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
19\r
4f265db7 20// master int buffer to mix to\r
21static int PsndBuffer[2*44100/50];\r
cc68a136 22\r
cc68a136 23// dac\r
eff55556 24static unsigned short dac_info[312]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample\r
cc68a136 25\r
c9e1affc 26// cdda output buffer\r
27short cdda_out_buffer[2*1152];\r
28\r
cc68a136 29// for Pico\r
30int PsndRate=0;\r
31int PsndLen=0; // number of mono samples, multiply by 2 for stereo\r
7a93adeb 32int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60\r
33int PsndLen_exc_cnt=0;\r
cc68a136 34short *PsndOut=NULL; // PCM data buffer\r
35\r
cc68a136 36// sn76496\r
37extern int *sn76496_regs;\r
38\r
39\r
eff55556 40static void dac_recalculate(void)\r
cc68a136 41{\r
42 int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;\r
43\r
44 if(PsndLen <= lines) {\r
45 // shrinking algo\r
7a93adeb 46 dac_cnt = -PsndLen;\r
cc68a136 47 len=1; pos=0;\r
48 dac_info[225] = 1;\r
49\r
50 for(i=226; i != 225; i++) {\r
51 if (i >= lines) i = 0;\r
52 len = 0;\r
53 if(dac_cnt < 0) {\r
54 len=1;\r
55 pos++;\r
56 dac_cnt += lines;\r
57 }\r
58 dac_cnt -= PsndLen;\r
59 dac_info[i] = (pos<<4)|len;\r
60 }\r
61 } else {\r
62 // stretching\r
7a93adeb 63 dac_cnt = PsndLen;\r
cc68a136 64 pos=0;\r
65 for(i = 225; i != 224; i++) {\r
66 if (i >= lines) i = 0;\r
67 len=0;\r
68 while(dac_cnt >= 0) {\r
69 dac_cnt -= lines;\r
70 len++;\r
71 }\r
4f265db7 72 if (i == mid) // midpoint\r
cc68a136 73 while(pos+len < PsndLen/2) {\r
74 dac_cnt -= lines;\r
75 len++;\r
76 }\r
77 dac_cnt += PsndLen;\r
78 dac_info[i] = (pos<<4)|len;\r
79 pos+=len;\r
80 }\r
81 // last sample\r
82 for(len = 0, i = pos; i < PsndLen; i++) len++;\r
7a93adeb 83 if (PsndLen_exc_add) len++;\r
cc68a136 84 dac_info[224] = (pos<<4)|len;\r
85 }\r
7a93adeb 86 //for(i=len=0; i < lines; i++) {\r
87 // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);\r
88 // len+=dac_info[i]&0xf;\r
89 //}\r
90 //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));\r
91 //printf("len total: %i, last pos: %i\n", len, pos);\r
92 //exit(8);\r
cc68a136 93}\r
94\r
95\r
9d917eea 96PICO_INTERNAL void PsndReset(void)\r
cc68a136 97{\r
cc68a136 98 void *ym2612_regs;\r
99\r
cc68a136 100 // also clear the internal registers+addr line\r
101 ym2612_regs = YM2612GetRegs();\r
102 memset(ym2612_regs, 0, 0x200+4);\r
9a8ffeee 103 z80startCycle = z80stopCycle = 0;\r
7a93adeb 104\r
9d917eea 105 PsndRerate(0);\r
cc68a136 106}\r
107\r
108\r
109// to be called after changing sound rate or chips\r
9d917eea 110void PsndRerate(int preserve_state)\r
cc68a136 111{\r
5f8c85be 112 void *state = NULL;\r
7a93adeb 113 int target_fps = Pico.m.pal ? 50 : 60;\r
cc68a136 114\r
7a93adeb 115 // not all rates are supported in MCD mode due to mp3 decoder limitations\r
602133e1 116 if (PicoAHW & PAHW_MCD) {\r
4f265db7 117 if (PsndRate != 11025 && PsndRate != 22050 && PsndRate != 44100) PsndRate = 22050;\r
602133e1 118 PicoOpt |= POPT_EN_STEREO; // force stereo\r
4f265db7 119 }\r
120\r
7a93adeb 121 if (preserve_state) {\r
5f8c85be 122 state = malloc(0x200);\r
123 if (state == NULL) return;\r
124 memcpy(state, YM2612GetRegs(), 0x200);\r
7a93adeb 125 }\r
cc68a136 126 YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);\r
7a93adeb 127 if (preserve_state) {\r
128 // feed it back it's own registers, just like after loading state\r
5f8c85be 129 memcpy(YM2612GetRegs(), state, 0x200);\r
7a93adeb 130 YM2612PicoStateLoad();\r
c9e1affc 131 if ((PicoAHW & PAHW_MCD) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r
132 cdda_start_play();\r
7a93adeb 133 }\r
cc68a136 134\r
7a93adeb 135 if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state\r
cc68a136 136 SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);\r
7a93adeb 137 if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state\r
cc68a136 138\r
5f8c85be 139 if (state)\r
140 free(state);\r
141\r
cc68a136 142 // calculate PsndLen\r
7a93adeb 143 PsndLen=PsndRate / target_fps;\r
144 PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
145 PsndLen_exc_cnt=0;\r
cc68a136 146\r
147 // recalculate dac info\r
148 dac_recalculate();\r
4f265db7 149\r
602133e1 150 if (PicoAHW & PAHW_MCD)\r
4f265db7 151 pcm_set_rate(PsndRate);\r
7a93adeb 152\r
153 // clear all buffers\r
154 memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
c9e1affc 155 memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r
7a93adeb 156 if (PsndOut)\r
9d917eea 157 PsndClear();\r
4a32f01f 158\r
159 // set mixer\r
602133e1 160 PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;\r
ed367a3f 161\r
162 if (PicoAHW & PAHW_PICO)\r
163 PicoReratePico();\r
cc68a136 164}\r
165\r
166\r
167// This is called once per raster (aka line), but not necessarily for every line\r
9d917eea 168PICO_INTERNAL void Psnd_timers_and_dac(int raster)\r
cc68a136 169{\r
4f265db7 170 int pos, len;\r
602133e1 171 int do_dac = PsndOut && (PicoOpt&POPT_EN_FM) && *ym2612_dacen;\r
172// int do_pcm = PsndOut && (PicoAHW&1) && (PicoOpt&0x400);\r
4f265db7 173\r
174 // Our raster lasts 63.61323/64.102564 microseconds (NTSC/PAL)\r
175 YM2612PicoTick(1);\r
176\r
177 if (!do_dac /*&& !do_pcm*/) return;\r
178\r
179 pos=dac_info[raster], len=pos&0xf;\r
180 if (!len) return;\r
181\r
182 pos>>=4;\r
183\r
602133e1 184 if (do_dac)\r
185 {\r
4f265db7 186 short *d = PsndOut + pos*2;\r
187 int dout = *ym2612_dacout;\r
602133e1 188 if(PicoOpt&POPT_EN_STEREO) {\r
4f265db7 189 // some manual loop unrolling here :)\r
190 d[0] = dout;\r
191 if (len > 1) {\r
192 d[2] = dout;\r
7a93adeb 193 if (len > 2)\r
4f265db7 194 d[4] = dout;\r
4f265db7 195 }\r
cc68a136 196 } else {\r
4f265db7 197 short *d = PsndOut + pos;\r
198 d[0] = dout;\r
199 if (len > 1) {\r
200 d[1] = dout;\r
7a93adeb 201 if (len > 2)\r
4f265db7 202 d[2] = dout;\r
4f265db7 203 }\r
cc68a136 204 }\r
205 }\r
206\r
4f265db7 207#if 0\r
208 if (do_pcm) {\r
209 int *d = PsndBuffer;\r
210 d += (PicoOpt&8) ? pos*2 : pos;\r
211 pcm_update(d, len, 1);\r
212 }\r
213#endif\r
214}\r
cc68a136 215\r
c9e1affc 216// cdda\r
217static pm_file *cdda_stream = NULL;\r
218\r
219static void cdda_raw_update(int *buffer, int length)\r
220{\r
221 int ret, cdda_bytes;\r
222 if (cdda_stream == NULL) return;\r
223\r
224 cdda_bytes = length*4;\r
225 if (PsndRate <= 22050) cdda_bytes *= 2;\r
226 if (PsndRate < 22050) cdda_bytes *= 2;\r
227\r
228 ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream);\r
229 if (ret < cdda_bytes) {\r
230 memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);\r
231 cdda_stream = NULL;\r
232 return;\r
233 }\r
234\r
235 // now mix\r
236 switch (PsndRate) {\r
237 case 44100: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;\r
238 case 22050: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;\r
239 case 11025: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;\r
240 }\r
241}\r
242\r
243PICO_INTERNAL void cdda_start_play(void)\r
244{\r
245 int lba_offset, index, lba_length, i;\r
246\r
247 elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track);\r
248\r
249 index = Pico_mcd->scd.Cur_Track - 1;\r
250\r
251 lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1);\r
252 if (lba_offset < 0) lba_offset = 0;\r
253 lba_offset += Pico_mcd->TOC.Tracks[index].Offset;\r
254\r
255 // find the actual file for this track\r
256 for (i = index; i >= 0; i--)\r
257 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
258\r
259 if (Pico_mcd->TOC.Tracks[i].F == NULL) {\r
260 elprintf(EL_STATUS|EL_ANOMALY, "no track?!");\r
261 return;\r
262 }\r
263\r
264 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)\r
265 {\r
266 int pos1024 = 0;\r
267\r
268 lba_length = Pico_mcd->TOC.Tracks[i].Length;\r
269 for (i++; i < Pico_mcd->TOC.Last_Track; i++) {\r
270 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
271 lba_length += Pico_mcd->TOC.Tracks[i].Length;\r
272 }\r
273\r
274 if (lba_offset)\r
275 pos1024 = lba_offset * 1024 / lba_length;\r
276\r
277 mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);\r
278 return;\r
279 }\r
280\r
281 cdda_stream = Pico_mcd->TOC.Tracks[i].F;\r
282 PicoCDBufferFlush(); // buffering relies on fp not being touched\r
283 pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET);\r
0bccafeb 284 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_WAV)\r
285 {\r
286 // skip headers, assume it's 44kHz stereo uncompressed\r
287 pm_seek(cdda_stream, 44, SEEK_CUR);\r
288 }\r
c9e1affc 289}\r
290\r
cc68a136 291\r
9d917eea 292PICO_INTERNAL void PsndClear(void)\r
4f265db7 293{\r
7a93adeb 294 int len = PsndLen;\r
295 if (PsndLen_exc_add) len++;\r
602133e1 296 if (PicoOpt & POPT_EN_STEREO)\r
88b3d7c1 297 memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r
298 else {\r
299 short *out = PsndOut;\r
300 if ((int)out & 2) { *out++ = 0; len--; }\r
301 memset32((int *) out, 0, len/2);\r
302 if (len & 1) out[len-1] = 0;\r
303 }\r
cc68a136 304}\r
305\r
306\r
9d917eea 307PICO_INTERNAL int PsndRender(int offset, int length)\r
cc68a136 308{\r
85f8e929 309 int buf32_updated = 0;\r
4f265db7 310 int *buf32 = PsndBuffer+offset;\r
cc68a136 311 int stereo = (PicoOpt & 8) >> 3;\r
4f265db7 312 // emulating CD && PCM option enabled && PCM chip on && have enabled channels\r
602133e1 313 int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&\r
314 (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;\r
cc68a136 315 offset <<= stereo;\r
316\r
03a265e5 317#if !SIMPLE_WRITE_SOUND\r
7a93adeb 318 if (offset == 0) { // should happen once per frame\r
319 // compensate for float part of PsndLen\r
320 PsndLen_exc_cnt += PsndLen_exc_add;\r
321 if (PsndLen_exc_cnt >= 0x10000) {\r
322 PsndLen_exc_cnt -= 0x10000;\r
323 length++;\r
324 }\r
325 }\r
03a265e5 326#endif\r
7a93adeb 327\r
cc68a136 328 // PSG\r
602133e1 329 if (PicoOpt & POPT_EN_PSG)\r
cc68a136 330 SN76496Update(PsndOut+offset, length, stereo);\r
331\r
ef4eb506 332 if (PicoAHW & PAHW_PICO) {\r
333 PicoPicoPCMUpdate(PsndOut+offset, length, stereo);\r
334 return length;\r
335 }\r
336\r
cc68a136 337 // Add in the stereo FM buffer\r
602133e1 338 if (PicoOpt & POPT_EN_FM) {\r
85f8e929 339 buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);\r
3ec29f01 340 } else\r
fa1e5e29 341 memset32(buf32, 0, length<<stereo);\r
85f8e929 342\r
343//printf("active_chs: %02x\n", buf32_updated);\r
4f265db7 344\r
7a93adeb 345 // CD: PCM sound\r
85f8e929 346 if (do_pcm) {\r
4f265db7 347 pcm_update(buf32, length, stereo);\r
85f8e929 348 //buf32_updated = 1;\r
349 }\r
4f265db7 350\r
7a93adeb 351 // CD: CDDA audio\r
da42200b 352 // CD mode, cdda enabled, not data track, CDC is reading\r
602133e1 353 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&\r
354 !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r
c9e1affc 355 {\r
356 // note: only 44, 22 and 11 kHz supported, with forced stereo\r
357 int index = Pico_mcd->scd.Cur_Track - 1;\r
358\r
359 if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3)\r
360 mp3_update(buf32, length, stereo);\r
361 else\r
362 cdda_raw_update(buf32, length);\r
363 }\r
4f265db7 364\r
365 // convert + limit to normal 16bit output\r
4a32f01f 366 PsndMix_32_to_16l(PsndOut+offset, buf32, length);\r
cc68a136 367\r
7a93adeb 368 return length;\r
cc68a136 369}\r
370\r
371\r
c9e1affc 372// -----------------------------------------------------------------\r
373// z80 stuff\r
374\r
cc68a136 375\r
376#if defined(_USE_MZ80)\r
377\r
378// memhandlers for mz80 core\r
379unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }\r
380void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }\r
381\r
382// structures for mz80 core\r
383static struct MemoryReadByte mz80_mem_read[]=\r
384{\r
385 {0x0000,0xffff,mz80_read},\r
386 {(UINT32) -1,(UINT32) -1,NULL}\r
387};\r
388static struct MemoryWriteByte mz80_mem_write[]=\r
389{\r
390 {0x0000,0xffff,mz80_write},\r
391 {(UINT32) -1,(UINT32) -1,NULL}\r
392};\r
393static struct z80PortRead mz80_io_read[] ={\r
394 {(UINT16) -1,(UINT16) -1,NULL}\r
395};\r
396static struct z80PortWrite mz80_io_write[]={\r
397 {(UINT16) -1,(UINT16) -1,NULL}\r
398};\r
399\r
b542be46 400int mz80_run(int cycles)\r
401{\r
402 int ticks_pre = mz80GetElapsedTicks(0);\r
403 mz80exec(cycles);\r
404 return mz80GetElapsedTicks(0) - ticks_pre;\r
405}\r
406\r
cc68a136 407#elif defined(_USE_DRZ80)\r
408\r
b542be46 409struct DrZ80 drZ80;\r
cc68a136 410\r
411static unsigned int DrZ80_rebasePC(unsigned short a)\r
412{\r
413 drZ80.Z80PC_BASE = (unsigned int) Pico.zram;\r
414 return drZ80.Z80PC_BASE + a;\r
415}\r
416\r
417static unsigned int DrZ80_rebaseSP(unsigned short a)\r
418{\r
419 drZ80.Z80SP_BASE = (unsigned int) Pico.zram;\r
420 return drZ80.Z80SP_BASE + a;\r
421}\r
422\r
9d917eea 423static void DrZ80_irq_callback()\r
cc68a136 424{\r
9d917eea 425 drZ80.Z80_IRQ = 0; // lower irq when accepted\r
cc68a136 426}\r
9d917eea 427#endif\r
cc68a136 428\r
9d917eea 429#if defined(_USE_DRZ80) || defined(_USE_CZ80)\r
430static unsigned char z80_in(unsigned short p)\r
cc68a136 431{\r
9d917eea 432 elprintf(EL_ANOMALY, "Z80 port %04x read", p);\r
433 return 0xff;\r
cc68a136 434}\r
435\r
9d917eea 436static void z80_out(unsigned short p,unsigned char d)\r
cc68a136 437{\r
9d917eea 438 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);\r
cc68a136 439}\r
cc68a136 440#endif\r
441\r
9d917eea 442\r
cc68a136 443// z80 functionality wrappers\r
eff55556 444PICO_INTERNAL void z80_init(void)\r
cc68a136 445{\r
446#if defined(_USE_MZ80)\r
447 struct mz80context z80;\r
448\r
449 // z80\r
450 mz80init();\r
451 // Modify the default context\r
452 mz80GetContext(&z80);\r
4f265db7 453\r
cc68a136 454 // point mz80 stuff\r
455 z80.z80Base=Pico.zram;\r
456 z80.z80MemRead=mz80_mem_read;\r
457 z80.z80MemWrite=mz80_mem_write;\r
458 z80.z80IoRead=mz80_io_read;\r
459 z80.z80IoWrite=mz80_io_write;\r
4f265db7 460\r
cc68a136 461 mz80SetContext(&z80);\r
462\r
463#elif defined(_USE_DRZ80)\r
cc68a136 464 memset(&drZ80, 0, sizeof(struct DrZ80));\r
465 drZ80.z80_rebasePC=DrZ80_rebasePC;\r
466 drZ80.z80_rebaseSP=DrZ80_rebaseSP;\r
467 drZ80.z80_read8 =z80_read;\r
468 drZ80.z80_read16 =z80_read16;\r
469 drZ80.z80_write8 =z80_write;\r
470 drZ80.z80_write16 =z80_write16;\r
9d917eea 471 drZ80.z80_in =z80_in;\r
472 drZ80.z80_out =z80_out;\r
cc68a136 473 drZ80.z80_irq_callback=DrZ80_irq_callback;\r
a4221917 474\r
475#elif defined(_USE_CZ80)\r
476 memset(&CZ80, 0, sizeof(CZ80));\r
477 Cz80_Init(&CZ80);\r
478 Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (UINT32)Pico.zram); // main RAM\r
c6196c0f 479 Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (UINT32)Pico.zram); // mirror\r
b542be46 480 Cz80_Set_ReadB(&CZ80, (UINT8 (*)(UINT32 address))z80_read); // unused (hacked in)\r
a4221917 481 Cz80_Set_WriteB(&CZ80, z80_write);\r
9d917eea 482 Cz80_Set_INPort(&CZ80, z80_in);\r
483 Cz80_Set_OUTPort(&CZ80, z80_out);\r
cc68a136 484#endif\r
485}\r
486\r
eff55556 487PICO_INTERNAL void z80_reset(void)\r
cc68a136 488{\r
489#if defined(_USE_MZ80)\r
490 mz80reset();\r
491#elif defined(_USE_DRZ80)\r
492 memset(&drZ80, 0, 0x54);\r
493 drZ80.Z80F = (1<<2); // set ZFlag\r
494 drZ80.Z80F2 = (1<<2); // set ZFlag\r
495 drZ80.Z80IX = 0xFFFF << 16;\r
496 drZ80.Z80IY = 0xFFFF << 16;\r
497 drZ80.Z80IM = 0; // 1?\r
498 drZ80.Z80PC = drZ80.z80_rebasePC(0);\r
499 drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?\r
a4221917 500#elif defined(_USE_CZ80)\r
501 Cz80_Reset(&CZ80);\r
c6196c0f 502 Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);\r
503 Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);\r
504 Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);\r
cc68a136 505#endif\r
506 Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled\r
507}\r
508\r
cc68a136 509\r
eff55556 510PICO_INTERNAL void z80_pack(unsigned char *data)\r
cc68a136 511{\r
512#if defined(_USE_MZ80)\r
513 struct mz80context mz80;\r
514 *(int *)data = 0x00005A6D; // "mZ"\r
515 mz80GetContext(&mz80);\r
516 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers\r
517#elif defined(_USE_DRZ80)\r
518 *(int *)data = 0x015A7244; // "DrZ" v1\r
519 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);\r
520 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);\r
521 memcpy(data+4, &drZ80, 0x54);\r
a4221917 522#elif defined(_USE_CZ80)\r
523 *(int *)data = 0x00007a43; // "Cz"\r
c5b61ac2 524 *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);\r
525 memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80);\r
cc68a136 526#endif\r
527}\r
528\r
eff55556 529PICO_INTERNAL void z80_unpack(unsigned char *data)\r
cc68a136 530{\r
531#if defined(_USE_MZ80)\r
a4221917 532 if (*(int *)data == 0x00005A6D) { // "mZ" save?\r
cc68a136 533 struct mz80context mz80;\r
534 mz80GetContext(&mz80);\r
535 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);\r
536 mz80SetContext(&mz80);\r
537 } else {\r
538 z80_reset();\r
539 z80_int();\r
540 }\r
541#elif defined(_USE_DRZ80)\r
a4221917 542 if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?\r
cc68a136 543 memcpy(&drZ80, data+4, 0x54);\r
544 // update bases\r
545 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);\r
546 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);\r
547 } else {\r
548 z80_reset();\r
549 drZ80.Z80IM = 1;\r
550 z80_int(); // try to goto int handler, maybe we won't execute trash there?\r
551 }\r
a4221917 552#elif defined(_USE_CZ80)\r
c5b61ac2 553 if (*(int *)data == 0x00007a43) { // "Cz" save?\r
554 memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80);\r
555 Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));\r
556 } else {\r
a4221917 557 z80_reset();\r
558 z80_int();\r
559 }\r
cc68a136 560#endif\r
561}\r
562\r
eff55556 563PICO_INTERNAL void z80_exit(void)\r
cc68a136 564{\r
565#if defined(_USE_MZ80)\r
566 mz80shutdown();\r
567#endif\r
568}\r
569\r
1820b5a7 570#if 1 // defined(__DEBUG_PRINT) || defined(__GP2X__) || defined(__GIZ__)\r
eff55556 571PICO_INTERNAL void z80_debug(char *dstr)\r
cc68a136 572{\r
573#if defined(_USE_DRZ80)\r
0af33fe0 574 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);\r
a4221917 575#elif defined(_USE_CZ80)\r
576 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W);\r
cc68a136 577#endif\r
578}\r
579#endif\r