Skip to content

Commit 910e966

Browse files
committed
sound: new ym2612 save states
irixxxx#166
1 parent 77141b0 commit 910e966

File tree

7 files changed

+387
-105
lines changed

7 files changed

+387
-105
lines changed

pico/memory.c

+37-11
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
* See COPYING file in the top-level directory.
99
*/
1010

11+
#include <assert.h>
1112
#include "pico_int.h"
1213
#include "memory.h"
14+
#include "state.h"
1315

1416
#include "sound/ym2612.h"
1517
#include "sound/sn76496.h"
@@ -1327,7 +1329,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
13271329
//elprintf(EL_STATUS, "%03i dac w %08x z80 %i", cycles, d, is_from_z80);
13281330
if (ym2612.dacen)
13291331
PsndDoDAC(cycles);
1330-
ym2612.dacout = ((int)d - 0x80) << 6;
1332+
ym2612.dacout = ((int)d - 0x80) << DAC_SHIFT;
13311333
return 0;
13321334
}
13331335
case 0x2b: { /* DAC Sel (YM2612) */
@@ -1374,10 +1376,11 @@ static u32 ym2612_read_local_68k(void)
13741376
return ym2612.OPN.ST.status;
13751377
}
13761378

1377-
void ym2612_pack_state(void)
1379+
int ym2612_pack_timers(void *buf, size_t size)
13781380
{
13791381
// timers are saved as tick counts, in 16.16 int format
13801382
int tac, tat = 0, tbc, tbt = 0, busy = 0;
1383+
size_t b = 0;
13811384
tac = 1024 - ym2612.OPN.ST.TA;
13821385
tbc = 256 - ym2612.OPN.ST.TB;
13831386
if (Pico.t.ym2612_busy > 0)
@@ -1388,18 +1391,18 @@ void ym2612_pack_state(void)
13881391
tbt = ((Pico.t.timer_b_step - Pico.t.timer_b_next_oflow) * ((1LL<<32)/TIMER_B_TICK_ZCYCLES+1))>>16;
13891392
elprintf(EL_YMTIMER, "save: timer a %i/%i", tat >> 16, tac);
13901393
elprintf(EL_YMTIMER, "save: timer b %i/%i", tbt >> 16, tbc);
1391-
1392-
#ifdef __GP2X__
1393-
if (PicoIn.opt & POPT_EXT_FM)
1394-
YM2612PicoStateSave2_940(tat, tbt);
1395-
else
1396-
#endif
1397-
YM2612PicoStateSave2(tat, tbt, busy);
1394+
assert(size >= 16);
1395+
save_u16(buf, &b, ym2612.OPN.ST.TA);
1396+
save_u16(buf, &b, ym2612.OPN.ST.TB);
1397+
save_u32(buf, &b, tat);
1398+
save_u32(buf, &b, tbt);
1399+
save_u32(buf, &b, busy);
1400+
return b;
13981401
}
13991402

1400-
void ym2612_unpack_state(void)
1403+
void ym2612_unpack_state_old(void)
14011404
{
1402-
int i, ret, tac, tat, tbc, tbt, busy = 0;
1405+
int i, ret, tat, tbt, busy = 0;
14031406
YM2612PicoStateLoad();
14041407

14051408
// feed all the registers and update internal state
@@ -1434,7 +1437,30 @@ void ym2612_unpack_state(void)
14341437
elprintf(EL_STATUS, "old ym2612 state");
14351438
return; // no saved timers
14361439
}
1440+
{
1441+
u8 tmp[16];
1442+
size_t b = 0;
1443+
save_u16(tmp, &b, ym2612.OPN.ST.TA);
1444+
save_u16(tmp, &b, ym2612.OPN.ST.TB);
1445+
save_u32(tmp, &b, tat);
1446+
save_u32(tmp, &b, tbt);
1447+
save_u32(tmp, &b, busy);
1448+
ym2612_unpack_timers(tmp, b);
1449+
}
1450+
}
14371451

1452+
void ym2612_unpack_timers(const void *buf, size_t size)
1453+
{
1454+
int tac, tat, tbc, tbt, busy;
1455+
size_t b = 0;
1456+
assert(size >= 16);
1457+
if (size < 16)
1458+
return;
1459+
ym2612.OPN.ST.TA = load_u16(buf, &b);
1460+
ym2612.OPN.ST.TB = load_u16(buf, &b);
1461+
tat = load_u32(buf, &b);
1462+
tbt = load_u32(buf, &b);
1463+
busy = load_u32(buf, &b);
14381464
Pico.t.ym2612_busy = cycles_68k_to_z80(busy);
14391465
tac = (1024 - ym2612.OPN.ST.TA) << 16;
14401466
tbc = (256 - ym2612.OPN.ST.TB) << 16;

pico/pico_int.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,9 @@ void cdda_start_play(int lba_base, int lba_offset, int lb_len);
904904
#define YM2612_NATIVE_RATE() (((Pico.m.pal?OSC_PAL:OSC_NTSC)/7 + 3*24) / (6*24))
905905

906906
void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new);
907-
void ym2612_pack_state(void);
908-
void ym2612_unpack_state(void);
907+
int ym2612_pack_timers(void *buf_, size_t size);
908+
void ym2612_unpack_timers(const void *buf_, size_t size);
909+
void ym2612_unpack_state_old(void);
909910

910911
#define TIMER_NO_OFLOW 0x70000000
911912

pico/sound/sound.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ void PsndRerate(int preserve_state)
170170
int ym2612_clock = Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7;
171171
int ym2612_rate = YM2612_NATIVE_RATE();
172172
int ym2612_init = !preserve_state;
173+
int state_size = 4096;
173174

174175
// don't init YM2612 if preserve_state and no parameter changes
175176
ym2612_init |= ymclock != ym2612_clock || ymopts != (PicoIn.opt & (POPT_DIS_FM_SSGEG|POPT_EN_FM_DAC));
@@ -179,10 +180,9 @@ void PsndRerate(int preserve_state)
179180
ymopts = PicoIn.opt & (POPT_DIS_FM_SSGEG|POPT_EN_FM_DAC);
180181

181182
if (preserve_state && ym2612_init) {
182-
state = malloc(0x204);
183-
if (state == NULL) return;
184-
ym2612_pack_state();
185-
memcpy(state, YM2612GetRegs(), 0x204);
183+
state = malloc(state_size);
184+
if (state)
185+
state_size = YM2612PicoStateSave3(state, state_size);
186186
}
187187

188188
if (PicoIn.AHW & PAHW_SMS) {
@@ -207,10 +207,8 @@ void PsndRerate(int preserve_state)
207207
PsndFMUpdate = YM2612UpdateONE;
208208
}
209209

210-
if (preserve_state && ym2612_init) {
211-
// feed it back it's own registers, just like after loading state
212-
memcpy(YM2612GetRegs(), state, 0x204);
213-
ym2612_unpack_state();
210+
if (state) {
211+
YM2612PicoStateLoad3(state, state_size);
214212
free(state);
215213
}
216214

0 commit comments

Comments
 (0)