From: notaz Date: Sun, 1 Jun 2008 22:35:08 +0000 (+0000) Subject: ym2612 full saves wip X-Git-Tag: v1.85~472 X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d2721b08bb50fd1b112befa4b9f1a918dbd1ca03;p=picodrive.git ym2612 full saves wip git-svn-id: file:///home/notaz/opt/svn/PicoDrive@479 be3aeb3a-fb24-0410-a615-afba39da0efa --- diff --git a/Pico/Area.c b/Pico/Area.c index 4f4fa568..040154ed 100644 --- a/Pico/Area.c +++ b/Pico/Area.c @@ -145,8 +145,7 @@ static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile) SCAN_VAR(Pico.m ,"misc") SCAN_VAR(Pico.video,"video") - // notaz: save/load z80, YM2612, sn76496 states instead of Pico.s (which is unused anyway) - if(PicoOpt&7) { + if (PicoOpt&7) { if((PmovAction&3)==1) z80_pack(cpu_z80); ret = SCAN_VAR(cpu_z80,"cpu_z80") // do not unpack if we fail to load z80 state @@ -155,9 +154,10 @@ static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile) else z80_unpack(cpu_z80); } } - if(PicoOpt&3) + if (PicoOpt&3) ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff - if(PicoOpt&1) { + if (PicoOpt&1) { + if((PmovAction&3)==1) ym2612_pack_state(); ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line if((PmovAction&3)==2) ym2612_unpack_state(); // reload YM2612 state from it's regs } diff --git a/Pico/Memory.c b/Pico/Memory.c index dbf0118a..e4a251b8 100644 --- a/Pico/Memory.c +++ b/Pico/Memory.c @@ -796,7 +796,7 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80) { //elprintf(EL_STATUS, "timer a set %i", TAnew); ym2612.OPN.ST.TA = TAnew; - //ym2612.OPN.ST.TAC = (1024-TAnew)*18; + ym2612.OPN.ST.TAC = (1024-TAnew)*18; //ym2612.OPN.ST.TAT = 0; // timer_a_step = timer_a_offset = 16466 * (1024 - TAnew); @@ -812,8 +812,7 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80) if (ym2612.OPN.ST.TB != d) { //elprintf(EL_STATUS, "timer b set %i", d); ym2612.OPN.ST.TB = d; - //ym2612.OPN.ST.TBC = (256-d)<<4; - //ym2612.OPN.ST.TBC *= 18; + ym2612.OPN.ST.TBC = (256-d) * 288; //ym2612.OPN.ST.TBT = 0; timer_b_step = timer_b_offset = 262800 * (256 - d); // 262881 if (ym2612.OPN.ST.mode & 2) { @@ -912,21 +911,38 @@ u32 ym2612_read_local_68k(void) return ym2612.OPN.ST.status; } -// TODO: new ym2612 savestates, also save timers +void ym2612_pack_state(void) +{ + // TODO timers +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) + /*YM2612PicoStateSave2_940(0, 0)*/; + else +#endif + YM2612PicoStateSave2(0, 0); +} + void ym2612_unpack_state(void) { - int i; + int i, ret, tat, tbt; YM2612PicoStateLoad(); // feed all the registers and update internal state - for (i = 0x20; i < 0xC0; i++) { + for (i = 0x20; i < 0xB8; i++) { ym2612_write_local(0, i, 0); ym2612_write_local(1, ym2612.REGS[i], 0); } - for (i = 0x30; i < 0xC0; i++) { + for (i = 0x30; i < 0xB8; i++) { ym2612_write_local(2, i, 0); ym2612_write_local(3, ym2612.REGS[i|0x100], 0); } + +#ifdef __GP2X__ + if (PicoOpt & POPT_EXT_FM) + /*ret = YM2612PicoStateLoad2_940(&tat, &tbt)*/; + else +#endif + ret = YM2612PicoStateLoad2(&tat, &tbt); } // ----------------------------------------------------------------- diff --git a/Pico/PicoInt.h b/Pico/PicoInt.h index 12d8e2ae..06f4c645 100644 --- a/Pico/PicoInt.h +++ b/Pico/PicoInt.h @@ -476,6 +476,7 @@ extern int timer_a_next_oflow, timer_a_step, timer_a_offset; // in z80 cycles extern int timer_b_next_oflow, timer_b_step, timer_b_offset; void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new); +void ym2612_pack_state(void); void ym2612_unpack_state(void); #define timers_cycle() \ diff --git a/Pico/cd/Area.c b/Pico/cd/Area.c index 1a4d271a..a402d4ea 100644 --- a/Pico/cd/Area.c +++ b/Pico/cd/Area.c @@ -121,8 +121,10 @@ PICO_INTERNAL int PicoCdSaveState(void *file) } if (PicoOpt&3) CHECKED_WRITE(CHUNK_PSG, 28*4, sn76496_regs); - if (PicoOpt&1) + if (PicoOpt&1) { + ym2612_pack_state(); CHECKED_WRITE(CHUNK_FM, 0x200+4, ym2612_regs); + } if (PicoAHW & PAHW_MCD) { diff --git a/Pico/sound/ym2612.c b/Pico/sound/ym2612.c index 4b74826c..625864c3 100644 --- a/Pico/sound/ym2612.c +++ b/Pico/sound/ym2612.c @@ -1087,12 +1087,25 @@ void chan_render_loop(chan_rend_context *ct, int *buffer, unsigned short length) static chan_rend_context crct; +static void chan_render_prep(void) +{ + crct.eg_timer_add = ym2612.OPN.eg_timer_add; + crct.lfo_inc = ym2612.OPN.lfo_inc; +} + +static void chan_render_finish(void) +{ + ym2612.OPN.eg_cnt = crct.eg_cnt; + ym2612.OPN.eg_timer = crct.eg_timer; + g_lfo_ampm = crct.pack >> 16; // need_save + ym2612.OPN.lfo_cnt = crct.lfo_cnt; +} + static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: stereo, ?, disabled, ?, pan_r, pan_l { crct.CH = &ym2612.CH[c]; crct.mem = crct.CH->mem_value; /* one sample delay memory */ crct.lfo_cnt = ym2612.OPN.lfo_cnt; - crct.lfo_inc = ym2612.OPN.lfo_inc; flags &= 0x35; @@ -1109,7 +1122,6 @@ static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: s crct.eg_cnt = ym2612.OPN.eg_cnt; /* envelope generator counter */ crct.eg_timer = ym2612.OPN.eg_timer; - crct.eg_timer_add = ym2612.OPN.eg_timer_add; /* precalculate phase modulation incr */ crct.phase1 = crct.CH->SLOT[SLOT1].phase; @@ -1185,15 +1197,6 @@ static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: s else ym2612.slot_mask &= ~(0xf << (c*4)); - // if this the last call, write back persistent stuff: - if ((ym2612.slot_mask >> ((c+1)*4)) == 0) - { - ym2612.OPN.eg_cnt = crct.eg_cnt; - ym2612.OPN.eg_timer = crct.eg_timer; - g_lfo_ampm = crct.pack >> 16; - ym2612.OPN.lfo_cnt = crct.lfo_cnt; - } - return (crct.algo & 8) >> 3; // had output } @@ -1212,10 +1215,10 @@ INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT, int fc, int kc) SLOT->ksr = ksr; /* calculate envelope generator rates */ - if ((SLOT->ar + SLOT->ksr) < 32+62) + if ((SLOT->ar + ksr) < 32+62) { - eg_sh = eg_rate_shift [SLOT->ar + SLOT->ksr ]; - eg_sel = eg_rate_select[SLOT->ar + SLOT->ksr ]; + eg_sh = eg_rate_shift [SLOT->ar + ksr ]; + eg_sel = eg_rate_select[SLOT->ar + ksr ]; } else { @@ -1225,18 +1228,18 @@ INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT, int fc, int kc) SLOT->eg_pack_ar = eg_inc_pack[eg_sel] | (eg_sh<<24); - eg_sh = eg_rate_shift [SLOT->d1r + SLOT->ksr]; - eg_sel = eg_rate_select[SLOT->d1r + SLOT->ksr]; + eg_sh = eg_rate_shift [SLOT->d1r + ksr]; + eg_sel = eg_rate_select[SLOT->d1r + ksr]; SLOT->eg_pack_d1r = eg_inc_pack[eg_sel] | (eg_sh<<24); - eg_sh = eg_rate_shift [SLOT->d2r + SLOT->ksr]; - eg_sel = eg_rate_select[SLOT->d2r + SLOT->ksr]; + eg_sh = eg_rate_shift [SLOT->d2r + ksr]; + eg_sel = eg_rate_select[SLOT->d2r + ksr]; SLOT->eg_pack_d2r = eg_inc_pack[eg_sel] | (eg_sh<<24); - eg_sh = eg_rate_shift [SLOT->rr + SLOT->ksr]; - eg_sel = eg_rate_select[SLOT->rr + SLOT->ksr]; + eg_sh = eg_rate_shift [SLOT->rr + ksr]; + eg_sel = eg_rate_select[SLOT->rr + ksr]; SLOT->eg_pack_rr = eg_inc_pack[eg_sel] | (eg_sh<<24); } @@ -1634,12 +1637,14 @@ int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty) /* mix to 32bit dest */ // flags: stereo, ?, disabled, ?, pan_r, pan_l + chan_render_prep(); if (ym2612.slot_mask & 0x00000f) active_chs |= chan_render(buffer, length, 0, stereo|((pan&0x003)<<4)) << 0; if (ym2612.slot_mask & 0x0000f0) active_chs |= chan_render(buffer, length, 1, stereo|((pan&0x00c)<<2)) << 1; if (ym2612.slot_mask & 0x000f00) active_chs |= chan_render(buffer, length, 2, stereo|((pan&0x030) )) << 2; if (ym2612.slot_mask & 0x00f000) active_chs |= chan_render(buffer, length, 3, stereo|((pan&0x0c0)>>2)) << 3; if (ym2612.slot_mask & 0x0f0000) active_chs |= chan_render(buffer, length, 4, stereo|((pan&0x300)>>4)) << 4; if (ym2612.slot_mask & 0xf00000) active_chs |= chan_render(buffer, length, 5, stereo|((pan&0xc00)>>6)|(ym2612.dacen<<2)) << 5; + chan_render_finish(); return active_chs; // 1 if buffer updated } @@ -1864,6 +1869,124 @@ int YM2612PicoTick_(int n) void YM2612PicoStateLoad_(void) { reset_channels( &ym2612.CH[0] ); + ym2612.slot_mask = 0xffffff; +} + +typedef struct +{ + UINT32 state_phase; + INT16 volume; +} ym_save_addon_slot; + +typedef struct +{ + UINT32 magic; + UINT8 address; + UINT8 status; + UINT8 addr_A1; + UINT8 unused; + int TAT; + int TBT; + UINT32 eg_cnt; + UINT32 eg_timer; + UINT32 lfo_cnt; + UINT16 lfo_ampm; + UINT16 unused2; +} ym_save_addon; + +void YM2612PicoStateSave2(int tat, int tbt) +{ + ym_save_addon_slot ss; + ym_save_addon sa; + unsigned char *ptr; + int c, s; + + // chans 1,2,3 + ptr = &ym2612.REGS[0x0b8]; + for (c = 0; c < 3; c++) + { + for (s = 0; s < 4; s++) { + ss.state_phase = (ym2612.CH[c].SLOT[s].state << 29) | (ym2612.CH[c].SLOT[s].phase >> 3); + ss.volume = ym2612.CH[c].SLOT[s].volume; + memcpy(ptr, &ss, 6); + ptr += 6; + } + } + // chans 4,5,6 + ptr = &ym2612.REGS[0x1b8]; + for (; c < 6; c++) + { + for (s = 0; s < 4; s++) { + ss.state_phase = (ym2612.CH[c].SLOT[s].state << 29) | (ym2612.CH[c].SLOT[s].phase >> 3); + ss.volume = ym2612.CH[c].SLOT[s].volume; + memcpy(ptr, &ss, 6); + ptr += 6; + } + } + // other things + ptr = &ym2612.REGS[0x100]; + sa.magic = 0x41534d59; // 'YMSA' + sa.address = ym2612.OPN.ST.address; + sa.status = ym2612.OPN.ST.status; + sa.addr_A1 = ym2612.addr_A1; + sa.unused = 0; + sa.TAT = tat; + sa.TBT = tbt; + sa.eg_cnt = ym2612.OPN.eg_cnt; + sa.eg_timer = ym2612.OPN.eg_timer; + sa.lfo_cnt = ym2612.OPN.lfo_cnt; + sa.lfo_ampm = g_lfo_ampm; + sa.unused2 = 0; + memcpy(ptr, &sa, sizeof(sa)); // 0x30 max +} + +int YM2612PicoStateLoad2(int *tat, int *tbt) +{ + ym_save_addon_slot ss; + ym_save_addon sa; + unsigned char *ptr; + int c, s; + + ptr = &ym2612.REGS[0x100]; + memcpy(&sa, ptr, sizeof(sa)); // 0x30 max + if (sa.magic != 0x41534d59) return -1; + + ym2612.OPN.ST.address = sa.address; + ym2612.OPN.ST.status = sa.status; + ym2612.addr_A1 = sa.addr_A1; + ym2612.OPN.eg_cnt = sa.eg_cnt; + ym2612.OPN.eg_timer = sa.eg_timer; + ym2612.OPN.lfo_cnt = sa.lfo_cnt; + g_lfo_ampm = sa.lfo_ampm; + if (tat != NULL) *tat = sa.TAT; + if (tbt != NULL) *tbt = sa.TBT; + + // chans 1,2,3 + ptr = &ym2612.REGS[0x0b8]; + for (c = 0; c < 3; c++) + { + for (s = 0; s < 4; s++) { + memcpy(&ss, ptr, 6); + ym2612.CH[c].SLOT[s].state = ss.state_phase >> 29; + ym2612.CH[c].SLOT[s].phase = ss.state_phase << 3; + ym2612.CH[c].SLOT[s].volume = ss.volume; + ptr += 6; + } + } + // chans 4,5,6 + ptr = &ym2612.REGS[0x1b8]; + for (; c < 6; c++) + { + for (s = 0; s < 4; s++) { + memcpy(&ss, ptr, 6); + ym2612.CH[c].SLOT[s].state = ss.state_phase >> 29; + ym2612.CH[c].SLOT[s].phase = ss.state_phase << 3; + ym2612.CH[c].SLOT[s].volume = ss.volume; + ptr += 6; + } + } + + return 0; } #ifndef EXTERNAL_YM2612 diff --git a/Pico/sound/ym2612.h b/Pico/sound/ym2612.h index a1124de4..07f41989 100644 --- a/Pico/sound/ym2612.h +++ b/Pico/sound/ym2612.h @@ -22,15 +22,15 @@ typedef signed int INT32; /* signed 32bit */ typedef struct { INT32 *DT; /* #0x00 detune :dt_tab[DT] */ - UINT8 ar; /* #0x04 attack rate */ + UINT8 ar; /* #0x04 attack rate */ UINT8 d1r; /* #0x05 decay rate */ UINT8 d2r; /* #0x06 sustain rate */ - UINT8 rr; /* #0x07 release rate */ + UINT8 rr; /* #0x07 release rate */ UINT32 mul; /* #0x08 multiple :ML_TABLE[ML] */ /* Phase Generator */ - UINT32 phase; /* #0x0c phase counter */ - UINT32 Incr; /* #0x10 phase step */ + UINT32 phase; /* #0x0c phase counter | need_save */ + UINT32 Incr; /* #0x10 phase step | need_save */ UINT8 KSR; /* #0x14 key scale rate :3-KSR */ UINT8 ksr; /* #0x15 key scale rate :kcode>>(3-KSR) */ @@ -38,15 +38,15 @@ typedef struct UINT8 key; /* #0x16 0=last key was KEY OFF, 1=KEY ON */ /* Envelope Generator */ - UINT8 state; /* #0x17 phase type: EG_OFF=0, EG_REL, EG_SUS, EG_DEC, EG_ATT */ - UINT16 tl; /* #0x18 total level: TL << 3 */ - INT16 volume; /* #0x1a envelope counter */ - UINT32 sl; /* #0x1c sustain level:sl_table[SL] */ + UINT8 state; /* #0x17 phase type: EG_OFF=0, EG_REL, EG_SUS, EG_DEC, EG_ATT | need_save */ + UINT16 tl; /* #0x18 total level: TL << 3 */ + INT16 volume; /* #0x1a envelope counter | need_save */ + UINT32 sl; /* #0x1c sustain level:sl_table[SL] */ - UINT32 eg_pack_ar; /* #0x20 (attack state) */ + UINT32 eg_pack_ar; /* #0x20 (attack state) */ UINT32 eg_pack_d1r; /* #0x24 (decay state) */ UINT32 eg_pack_d2r; /* #0x28 (sustain state) */ - UINT32 eg_pack_rr; /* #0x2c (release state) */ + UINT32 eg_pack_rr; /* #0x2c (release state) */ } FM_SLOT; @@ -55,7 +55,7 @@ typedef struct FM_SLOT SLOT[4]; /* four SLOTs (operators) */ UINT8 ALGO; /* algorithm */ - UINT8 FB; /* feedback shift */ + UINT8 FB; /* feedback shift */ INT32 op1_out; /* op1 output for feedback */ INT32 mem_value; /* delayed sample (MEM) value */ @@ -64,7 +64,7 @@ typedef struct UINT8 ams; /* channel AMS */ UINT8 kcode; /* key code: */ - UINT32 fc; /* fnum,blk:adjusted to sample rate */ + UINT32 fc; /* fnum,blk:adjusted to sample rate */ UINT32 block_fnum; /* current blk/fnum value for this slot (can be different betweeen slots of one channel in 3slot mode) */ /* LFO */ @@ -77,17 +77,17 @@ typedef struct int clock; /* master clock (Hz) */ int rate; /* sampling rate (Hz) */ double freqbase; /* 08 frequency base */ - UINT8 address; /* 10 address register */ - UINT8 status; /* 11 status flag */ + UINT8 address; /* 10 address register | need_save */ + UINT8 status; /* 11 status flag | need_save */ UINT8 mode; /* mode CSM / 3SLOT */ UINT8 fn_h; /* freq latch */ int TA; /* timer a */ int TAC; /* timer a maxval */ - int TAT; /* timer a ticker */ + int TAT; /* timer a ticker | need_save */ UINT8 TB; /* timer b */ UINT8 pad[3]; int TBC; /* timer b maxval */ - int TBT; /* timer b ticker */ + int TBT; /* timer b ticker | need_save */ /* local time tables */ INT32 dt_tab[8][32];/* DeTune table */ } FM_ST; @@ -112,12 +112,12 @@ typedef struct FM_3SLOT SL3; /* 3 slot mode state */ UINT32 pan; /* fm channels output mask (bit 1 = enable) */ - UINT32 eg_cnt; /* #0xb38 global envelope generator counter */ - UINT32 eg_timer; /* #0xb3c global envelope generator counter works at frequency = chipclock/64/3 */ - UINT32 eg_timer_add; /* #0xb40 step of eg_timer */ + UINT32 eg_cnt; /* #0xb38 global envelope generator counter | need_save */ + UINT32 eg_timer; /* #0xb3c global envelope generator counter works at frequency = chipclock/64/3 | need_save */ + UINT32 eg_timer_add; /* #0xb40 step of eg_timer */ /* LFO */ - UINT32 lfo_cnt; + UINT32 lfo_cnt; /* need_save */ UINT32 lfo_inc; UINT32 lfo_freq[8]; /* LFO FREQ table */ @@ -126,13 +126,13 @@ typedef struct /* here's the virtual YM2612 */ typedef struct { - UINT8 REGS[0x200]; /* registers (for save states) */ - INT32 addr_A1; /* address line A1 */ + UINT8 REGS[0x200]; /* registers (for save states) */ + INT32 addr_A1; /* address line A1 | need_save */ FM_CH CH[6]; /* channel state (0x168 bytes each)? */ /* dac output (YM2612) */ - int dacen; + int dacen; INT32 dacout; FM_OPN OPN; /* OPN state */ @@ -156,6 +156,8 @@ int YM2612PicoTick_(int n); void YM2612PicoStateLoad_(void); void *YM2612GetRegs(void); +void YM2612PicoStateSave2(int tat, int tbt); +int YM2612PicoStateLoad2(int *tat, int *tbt); #ifndef __GP2X__ #define YM2612Init YM2612Init_