X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2Fsound%2Fym2612.c;h=625864c32afcc5abbd056571560d7a0b184cd76b;hb=d2721b08bb50fd1b112befa4b9f1a918dbd1ca03;hp=144c334f60a3ae1fa22bc8ede83483a75804a79b;hpb=dca310c413517d278898b967cfe610574310acd8;p=picodrive.git diff --git a/Pico/sound/ym2612.c b/Pico/sound/ym2612.c index 144c334..625864c 100644 --- a/Pico/sound/ym2612.c +++ b/Pico/sound/ym2612.c @@ -116,7 +116,7 @@ #ifndef EXTERNAL_YM2612 #include // let it be 1 global to simplify things -static YM2612 ym2612; +YM2612 ym2612; #else extern YM2612 *ym2612_940; @@ -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); } @@ -1584,11 +1587,6 @@ static int OPNWriteReg(int r, int v) /* YM2612 local section */ /*******************************************************************************/ -int *ym2612_dacen; -INT32 *ym2612_dacout; -FM_ST *ym2612_st; - - /* Generate samples for YM2612 */ int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty) { @@ -1639,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 } @@ -1653,11 +1653,6 @@ int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty) /* initialize YM2612 emulator */ void YM2612Init_(int clock, int rate) { - // notaz - ym2612_dacen = &ym2612.dacen; - ym2612_dacout = &ym2612.dacout; - ym2612_st = &ym2612.OPN.ST; - memset(&ym2612, 0, sizeof(ym2612)); init_tables(); @@ -1728,9 +1723,6 @@ int YM2612Write_(unsigned int a, unsigned int v) } addr = ym2612.OPN.ST.address; -#ifndef EXTERNAL_YM2612 - ym2612.REGS[addr] = v; -#endif switch( addr & 0xf0 ) { @@ -1747,6 +1739,7 @@ int YM2612Write_(unsigned int a, unsigned int v) ym2612.OPN.lfo_inc = 0; } break; +#if 0 // handled elsewhere case 0x24: { // timer A High 8 int TAnew = (ym2612.OPN.ST.TA & 0x03)|(((int)v)<<2); if(ym2612.OPN.ST.TA != TAnew) { @@ -1781,6 +1774,7 @@ int YM2612Write_(unsigned int a, unsigned int v) set_timers( v ); ret=0; break; +#endif case 0x28: /* key on / off */ { UINT8 c; @@ -1794,6 +1788,7 @@ int YM2612Write_(unsigned int a, unsigned int v) if(v&0x80) FM_KEYON(c,SLOT4); else FM_KEYOFF(c,SLOT4); break; } +#if 0 case 0x2a: /* DAC data (YM2612) */ ym2612.dacout = ((int)v - 0x80) << 6; /* level unknown (notaz: 8 seems to be too much) */ ret=0; @@ -1803,6 +1798,7 @@ int YM2612Write_(unsigned int a, unsigned int v) ym2612.dacen = v & 0x80; ret=0; break; +#endif default: break; } @@ -1826,9 +1822,6 @@ int YM2612Write_(unsigned int a, unsigned int v) } addr = ym2612.OPN.ST.address | 0x100; -#ifndef EXTERNAL_YM2612 - ym2612.REGS[addr] = v; -#endif ret = OPNWriteReg(addr, v); break; @@ -1875,26 +1868,125 @@ int YM2612PicoTick_(int n) void YM2612PicoStateLoad_(void) { -#ifndef EXTERNAL_YM2612 - int i, real_A1 = ym2612.addr_A1; - reset_channels( &ym2612.CH[0] ); + ym2612.slot_mask = 0xffffff; +} + +typedef struct +{ + UINT32 state_phase; + INT16 volume; +} ym_save_addon_slot; - // feed all the registers and update internal state - for(i = 0; i < 0x100; i++) { - YM2612Write_(0, i); - YM2612Write_(1, ym2612.REGS[i]); +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 +} - for(i = 0; i < 0x100; i++) { - YM2612Write_(2, i); - YM2612Write_(3, ym2612.REGS[i|0x100]); +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; + } } - ym2612.addr_A1 = real_A1; -#else - reset_channels( &ym2612.CH[0] ); -#endif + return 0; } #ifndef EXTERNAL_YM2612