cue/bin finally implemented
[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
cc68a136 161}\r
162\r
163\r
164// This is called once per raster (aka line), but not necessarily for every line\r
9d917eea 165PICO_INTERNAL void Psnd_timers_and_dac(int raster)\r
cc68a136 166{\r
4f265db7 167 int pos, len;\r
602133e1 168 int do_dac = PsndOut && (PicoOpt&POPT_EN_FM) && *ym2612_dacen;\r
169// int do_pcm = PsndOut && (PicoAHW&1) && (PicoOpt&0x400);\r
4f265db7 170\r
171 // Our raster lasts 63.61323/64.102564 microseconds (NTSC/PAL)\r
172 YM2612PicoTick(1);\r
173\r
174 if (!do_dac /*&& !do_pcm*/) return;\r
175\r
176 pos=dac_info[raster], len=pos&0xf;\r
177 if (!len) return;\r
178\r
179 pos>>=4;\r
180\r
602133e1 181 if (do_dac)\r
182 {\r
4f265db7 183 short *d = PsndOut + pos*2;\r
184 int dout = *ym2612_dacout;\r
602133e1 185 if(PicoOpt&POPT_EN_STEREO) {\r
4f265db7 186 // some manual loop unrolling here :)\r
187 d[0] = dout;\r
188 if (len > 1) {\r
189 d[2] = dout;\r
7a93adeb 190 if (len > 2)\r
4f265db7 191 d[4] = dout;\r
4f265db7 192 }\r
cc68a136 193 } else {\r
4f265db7 194 short *d = PsndOut + pos;\r
195 d[0] = dout;\r
196 if (len > 1) {\r
197 d[1] = dout;\r
7a93adeb 198 if (len > 2)\r
4f265db7 199 d[2] = dout;\r
4f265db7 200 }\r
cc68a136 201 }\r
202 }\r
203\r
4f265db7 204#if 0\r
205 if (do_pcm) {\r
206 int *d = PsndBuffer;\r
207 d += (PicoOpt&8) ? pos*2 : pos;\r
208 pcm_update(d, len, 1);\r
209 }\r
210#endif\r
211}\r
cc68a136 212\r
c9e1affc 213// cdda\r
214static pm_file *cdda_stream = NULL;\r
215\r
216static void cdda_raw_update(int *buffer, int length)\r
217{\r
218 int ret, cdda_bytes;\r
219 if (cdda_stream == NULL) return;\r
220\r
221 cdda_bytes = length*4;\r
222 if (PsndRate <= 22050) cdda_bytes *= 2;\r
223 if (PsndRate < 22050) cdda_bytes *= 2;\r
224\r
225 ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream);\r
226 if (ret < cdda_bytes) {\r
227 memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);\r
228 cdda_stream = NULL;\r
229 return;\r
230 }\r
231\r
232 // now mix\r
233 switch (PsndRate) {\r
234 case 44100: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;\r
235 case 22050: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;\r
236 case 11025: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;\r
237 }\r
238}\r
239\r
240PICO_INTERNAL void cdda_start_play(void)\r
241{\r
242 int lba_offset, index, lba_length, i;\r
243\r
244 elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track);\r
245\r
246 index = Pico_mcd->scd.Cur_Track - 1;\r
247\r
248 lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1);\r
249 if (lba_offset < 0) lba_offset = 0;\r
250 lba_offset += Pico_mcd->TOC.Tracks[index].Offset;\r
251\r
252 // find the actual file for this track\r
253 for (i = index; i >= 0; i--)\r
254 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
255\r
256 if (Pico_mcd->TOC.Tracks[i].F == NULL) {\r
257 elprintf(EL_STATUS|EL_ANOMALY, "no track?!");\r
258 return;\r
259 }\r
260\r
261 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)\r
262 {\r
263 int pos1024 = 0;\r
264\r
265 lba_length = Pico_mcd->TOC.Tracks[i].Length;\r
266 for (i++; i < Pico_mcd->TOC.Last_Track; i++) {\r
267 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
268 lba_length += Pico_mcd->TOC.Tracks[i].Length;\r
269 }\r
270\r
271 if (lba_offset)\r
272 pos1024 = lba_offset * 1024 / lba_length;\r
273\r
274 mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);\r
275 return;\r
276 }\r
277\r
278 cdda_stream = Pico_mcd->TOC.Tracks[i].F;\r
279 PicoCDBufferFlush(); // buffering relies on fp not being touched\r
280 pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET);\r
281}\r
282\r
cc68a136 283\r
9d917eea 284PICO_INTERNAL void PsndClear(void)\r
4f265db7 285{\r
7a93adeb 286 int len = PsndLen;\r
287 if (PsndLen_exc_add) len++;\r
602133e1 288 if (PicoOpt & POPT_EN_STEREO)\r
88b3d7c1 289 memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r
290 else {\r
291 short *out = PsndOut;\r
292 if ((int)out & 2) { *out++ = 0; len--; }\r
293 memset32((int *) out, 0, len/2);\r
294 if (len & 1) out[len-1] = 0;\r
295 }\r
cc68a136 296}\r
297\r
298\r
9d917eea 299PICO_INTERNAL int PsndRender(int offset, int length)\r
cc68a136 300{\r
85f8e929 301 int buf32_updated = 0;\r
4f265db7 302 int *buf32 = PsndBuffer+offset;\r
cc68a136 303 int stereo = (PicoOpt & 8) >> 3;\r
4f265db7 304 // emulating CD && PCM option enabled && PCM chip on && have enabled channels\r
602133e1 305 int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&\r
306 (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;\r
cc68a136 307 offset <<= stereo;\r
308\r
03a265e5 309#if !SIMPLE_WRITE_SOUND\r
7a93adeb 310 if (offset == 0) { // should happen once per frame\r
311 // compensate for float part of PsndLen\r
312 PsndLen_exc_cnt += PsndLen_exc_add;\r
313 if (PsndLen_exc_cnt >= 0x10000) {\r
314 PsndLen_exc_cnt -= 0x10000;\r
315 length++;\r
316 }\r
317 }\r
03a265e5 318#endif\r
7a93adeb 319\r
cc68a136 320 // PSG\r
602133e1 321 if (PicoOpt & POPT_EN_PSG)\r
cc68a136 322 SN76496Update(PsndOut+offset, length, stereo);\r
323\r
324 // Add in the stereo FM buffer\r
602133e1 325 if (PicoOpt & POPT_EN_FM) {\r
85f8e929 326 buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);\r
3ec29f01 327 } else\r
fa1e5e29 328 memset32(buf32, 0, length<<stereo);\r
85f8e929 329\r
330//printf("active_chs: %02x\n", buf32_updated);\r
4f265db7 331\r
7a93adeb 332 // CD: PCM sound\r
85f8e929 333 if (do_pcm) {\r
4f265db7 334 pcm_update(buf32, length, stereo);\r
85f8e929 335 //buf32_updated = 1;\r
336 }\r
4f265db7 337\r
7a93adeb 338 // CD: CDDA audio\r
da42200b 339 // CD mode, cdda enabled, not data track, CDC is reading\r
602133e1 340 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&\r
341 !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r
c9e1affc 342 {\r
343 // note: only 44, 22 and 11 kHz supported, with forced stereo\r
344 int index = Pico_mcd->scd.Cur_Track - 1;\r
345\r
346 if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3)\r
347 mp3_update(buf32, length, stereo);\r
348 else\r
349 cdda_raw_update(buf32, length);\r
350 }\r
4f265db7 351\r
352 // convert + limit to normal 16bit output\r
4a32f01f 353 PsndMix_32_to_16l(PsndOut+offset, buf32, length);\r
cc68a136 354\r
7a93adeb 355 return length;\r
cc68a136 356}\r
357\r
358\r
c9e1affc 359// -----------------------------------------------------------------\r
360// z80 stuff\r
361\r
cc68a136 362\r
363#if defined(_USE_MZ80)\r
364\r
365// memhandlers for mz80 core\r
366unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }\r
367void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }\r
368\r
369// structures for mz80 core\r
370static struct MemoryReadByte mz80_mem_read[]=\r
371{\r
372 {0x0000,0xffff,mz80_read},\r
373 {(UINT32) -1,(UINT32) -1,NULL}\r
374};\r
375static struct MemoryWriteByte mz80_mem_write[]=\r
376{\r
377 {0x0000,0xffff,mz80_write},\r
378 {(UINT32) -1,(UINT32) -1,NULL}\r
379};\r
380static struct z80PortRead mz80_io_read[] ={\r
381 {(UINT16) -1,(UINT16) -1,NULL}\r
382};\r
383static struct z80PortWrite mz80_io_write[]={\r
384 {(UINT16) -1,(UINT16) -1,NULL}\r
385};\r
386\r
b542be46 387int mz80_run(int cycles)\r
388{\r
389 int ticks_pre = mz80GetElapsedTicks(0);\r
390 mz80exec(cycles);\r
391 return mz80GetElapsedTicks(0) - ticks_pre;\r
392}\r
393\r
cc68a136 394#elif defined(_USE_DRZ80)\r
395\r
b542be46 396struct DrZ80 drZ80;\r
cc68a136 397\r
398static unsigned int DrZ80_rebasePC(unsigned short a)\r
399{\r
400 drZ80.Z80PC_BASE = (unsigned int) Pico.zram;\r
401 return drZ80.Z80PC_BASE + a;\r
402}\r
403\r
404static unsigned int DrZ80_rebaseSP(unsigned short a)\r
405{\r
406 drZ80.Z80SP_BASE = (unsigned int) Pico.zram;\r
407 return drZ80.Z80SP_BASE + a;\r
408}\r
409\r
9d917eea 410static void DrZ80_irq_callback()\r
cc68a136 411{\r
9d917eea 412 drZ80.Z80_IRQ = 0; // lower irq when accepted\r
cc68a136 413}\r
9d917eea 414#endif\r
cc68a136 415\r
9d917eea 416#if defined(_USE_DRZ80) || defined(_USE_CZ80)\r
417static unsigned char z80_in(unsigned short p)\r
cc68a136 418{\r
9d917eea 419 elprintf(EL_ANOMALY, "Z80 port %04x read", p);\r
420 return 0xff;\r
cc68a136 421}\r
422\r
9d917eea 423static void z80_out(unsigned short p,unsigned char d)\r
cc68a136 424{\r
9d917eea 425 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);\r
cc68a136 426}\r
cc68a136 427#endif\r
428\r
9d917eea 429\r
cc68a136 430// z80 functionality wrappers\r
eff55556 431PICO_INTERNAL void z80_init(void)\r
cc68a136 432{\r
433#if defined(_USE_MZ80)\r
434 struct mz80context z80;\r
435\r
436 // z80\r
437 mz80init();\r
438 // Modify the default context\r
439 mz80GetContext(&z80);\r
4f265db7 440\r
cc68a136 441 // point mz80 stuff\r
442 z80.z80Base=Pico.zram;\r
443 z80.z80MemRead=mz80_mem_read;\r
444 z80.z80MemWrite=mz80_mem_write;\r
445 z80.z80IoRead=mz80_io_read;\r
446 z80.z80IoWrite=mz80_io_write;\r
4f265db7 447\r
cc68a136 448 mz80SetContext(&z80);\r
449\r
450#elif defined(_USE_DRZ80)\r
cc68a136 451 memset(&drZ80, 0, sizeof(struct DrZ80));\r
452 drZ80.z80_rebasePC=DrZ80_rebasePC;\r
453 drZ80.z80_rebaseSP=DrZ80_rebaseSP;\r
454 drZ80.z80_read8 =z80_read;\r
455 drZ80.z80_read16 =z80_read16;\r
456 drZ80.z80_write8 =z80_write;\r
457 drZ80.z80_write16 =z80_write16;\r
9d917eea 458 drZ80.z80_in =z80_in;\r
459 drZ80.z80_out =z80_out;\r
cc68a136 460 drZ80.z80_irq_callback=DrZ80_irq_callback;\r
a4221917 461\r
462#elif defined(_USE_CZ80)\r
463 memset(&CZ80, 0, sizeof(CZ80));\r
464 Cz80_Init(&CZ80);\r
465 Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (UINT32)Pico.zram); // main RAM\r
c6196c0f 466 Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (UINT32)Pico.zram); // mirror\r
b542be46 467 Cz80_Set_ReadB(&CZ80, (UINT8 (*)(UINT32 address))z80_read); // unused (hacked in)\r
a4221917 468 Cz80_Set_WriteB(&CZ80, z80_write);\r
9d917eea 469 Cz80_Set_INPort(&CZ80, z80_in);\r
470 Cz80_Set_OUTPort(&CZ80, z80_out);\r
cc68a136 471#endif\r
472}\r
473\r
eff55556 474PICO_INTERNAL void z80_reset(void)\r
cc68a136 475{\r
476#if defined(_USE_MZ80)\r
477 mz80reset();\r
478#elif defined(_USE_DRZ80)\r
479 memset(&drZ80, 0, 0x54);\r
480 drZ80.Z80F = (1<<2); // set ZFlag\r
481 drZ80.Z80F2 = (1<<2); // set ZFlag\r
482 drZ80.Z80IX = 0xFFFF << 16;\r
483 drZ80.Z80IY = 0xFFFF << 16;\r
484 drZ80.Z80IM = 0; // 1?\r
485 drZ80.Z80PC = drZ80.z80_rebasePC(0);\r
486 drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?\r
a4221917 487#elif defined(_USE_CZ80)\r
488 Cz80_Reset(&CZ80);\r
c6196c0f 489 Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);\r
490 Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);\r
491 Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);\r
cc68a136 492#endif\r
493 Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled\r
494}\r
495\r
cc68a136 496\r
eff55556 497PICO_INTERNAL void z80_pack(unsigned char *data)\r
cc68a136 498{\r
499#if defined(_USE_MZ80)\r
500 struct mz80context mz80;\r
501 *(int *)data = 0x00005A6D; // "mZ"\r
502 mz80GetContext(&mz80);\r
503 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers\r
504#elif defined(_USE_DRZ80)\r
505 *(int *)data = 0x015A7244; // "DrZ" v1\r
506 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);\r
507 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);\r
508 memcpy(data+4, &drZ80, 0x54);\r
a4221917 509#elif defined(_USE_CZ80)\r
510 *(int *)data = 0x00007a43; // "Cz"\r
c5b61ac2 511 *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);\r
512 memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80);\r
cc68a136 513#endif\r
514}\r
515\r
eff55556 516PICO_INTERNAL void z80_unpack(unsigned char *data)\r
cc68a136 517{\r
518#if defined(_USE_MZ80)\r
a4221917 519 if (*(int *)data == 0x00005A6D) { // "mZ" save?\r
cc68a136 520 struct mz80context mz80;\r
521 mz80GetContext(&mz80);\r
522 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);\r
523 mz80SetContext(&mz80);\r
524 } else {\r
525 z80_reset();\r
526 z80_int();\r
527 }\r
528#elif defined(_USE_DRZ80)\r
a4221917 529 if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?\r
cc68a136 530 memcpy(&drZ80, data+4, 0x54);\r
531 // update bases\r
532 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);\r
533 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);\r
534 } else {\r
535 z80_reset();\r
536 drZ80.Z80IM = 1;\r
537 z80_int(); // try to goto int handler, maybe we won't execute trash there?\r
538 }\r
a4221917 539#elif defined(_USE_CZ80)\r
c5b61ac2 540 if (*(int *)data == 0x00007a43) { // "Cz" save?\r
541 memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80);\r
542 Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));\r
543 } else {\r
a4221917 544 z80_reset();\r
545 z80_int();\r
546 }\r
cc68a136 547#endif\r
548}\r
549\r
eff55556 550PICO_INTERNAL void z80_exit(void)\r
cc68a136 551{\r
552#if defined(_USE_MZ80)\r
553 mz80shutdown();\r
554#endif\r
555}\r
556\r
1820b5a7 557#if 1 // defined(__DEBUG_PRINT) || defined(__GP2X__) || defined(__GIZ__)\r
eff55556 558PICO_INTERNAL void z80_debug(char *dstr)\r
cc68a136 559{\r
560#if defined(_USE_DRZ80)\r
0af33fe0 561 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);\r
a4221917 562#elif defined(_USE_CZ80)\r
563 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W);\r
cc68a136 564#endif\r
565}\r
566#endif\r