proper ym2612 save hopefully done
[picodrive.git] / Pico / sound / ym2612.c
index 34dabec..294d621 100644 (file)
@@ -889,7 +889,7 @@ static void chan_render_loop(chan_rend_context *ct, int *buffer, int length)
                {\r
                        int out = 0;\r
 \r
-                       if (ct->pack&0xf000) out = ((ct->op1_out>>16) + (ct->op1_out<<16>>16)) << ((ct->pack&0xf000)>>12); /* op1_out0 + op1_out1 */\r
+                       if (ct->pack&0xf000) out = ((ct->op1_out>>16) + ((ct->op1_out<<16)>>16)) << ((ct->pack&0xf000)>>12); /* op1_out0 + op1_out1 */\r
                        ct->op1_out <<= 16;\r
                        ct->op1_out |= (unsigned short)op_calc1(ct->phase1, eg_out, out);\r
                } else {\r
@@ -1087,12 +1087,25 @@ void chan_render_loop(chan_rend_context *ct, int *buffer, unsigned short length)
 \r
 static chan_rend_context crct;\r
 \r
+static void chan_render_prep(void)\r
+{\r
+       crct.eg_timer_add = ym2612.OPN.eg_timer_add;\r
+       crct.lfo_inc = ym2612.OPN.lfo_inc;\r
+}\r
+\r
+static void chan_render_finish(void)\r
+{\r
+       ym2612.OPN.eg_cnt = crct.eg_cnt;\r
+       ym2612.OPN.eg_timer = crct.eg_timer;\r
+       g_lfo_ampm = crct.pack >> 16; // need_save\r
+       ym2612.OPN.lfo_cnt = crct.lfo_cnt;\r
+}\r
+\r
 static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: stereo, ?, disabled, ?, pan_r, pan_l\r
 {\r
        crct.CH = &ym2612.CH[c];\r
        crct.mem = crct.CH->mem_value;          /* one sample delay memory */\r
        crct.lfo_cnt = ym2612.OPN.lfo_cnt;\r
-       crct.lfo_inc = ym2612.OPN.lfo_inc;\r
 \r
        flags &= 0x35;\r
 \r
@@ -1109,7 +1122,6 @@ static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: s
 \r
        crct.eg_cnt = ym2612.OPN.eg_cnt;                        /* envelope generator counter */\r
        crct.eg_timer = ym2612.OPN.eg_timer;\r
-       crct.eg_timer_add = ym2612.OPN.eg_timer_add;\r
 \r
        /* precalculate phase modulation incr */\r
        crct.phase1 = crct.CH->SLOT[SLOT1].phase;\r
@@ -1185,15 +1197,6 @@ static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: s
        else\r
                ym2612.slot_mask &= ~(0xf << (c*4));\r
 \r
-       // if this the last call, write back persistent stuff:\r
-       if ((ym2612.slot_mask >> ((c+1)*4)) == 0)\r
-       {\r
-               ym2612.OPN.eg_cnt = crct.eg_cnt;\r
-               ym2612.OPN.eg_timer = crct.eg_timer;\r
-               g_lfo_ampm = crct.pack >> 16;\r
-               ym2612.OPN.lfo_cnt = crct.lfo_cnt;\r
-       }\r
-\r
        return (crct.algo & 8) >> 3; // had output\r
 }\r
 \r
@@ -1212,10 +1215,10 @@ INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT, int fc, int kc)
                SLOT->ksr = ksr;\r
 \r
                /* calculate envelope generator rates */\r
-               if ((SLOT->ar + SLOT->ksr) < 32+62)\r
+               if ((SLOT->ar + ksr) < 32+62)\r
                {\r
-                       eg_sh  = eg_rate_shift [SLOT->ar  + SLOT->ksr ];\r
-                       eg_sel = eg_rate_select[SLOT->ar  + SLOT->ksr ];\r
+                       eg_sh  = eg_rate_shift [SLOT->ar  + ksr ];\r
+                       eg_sel = eg_rate_select[SLOT->ar  + ksr ];\r
                }\r
                else\r
                {\r
@@ -1225,18 +1228,18 @@ INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT, int fc, int kc)
 \r
                SLOT->eg_pack_ar = eg_inc_pack[eg_sel] | (eg_sh<<24);\r
 \r
-               eg_sh  = eg_rate_shift [SLOT->d1r + SLOT->ksr];\r
-               eg_sel = eg_rate_select[SLOT->d1r + SLOT->ksr];\r
+               eg_sh  = eg_rate_shift [SLOT->d1r + ksr];\r
+               eg_sel = eg_rate_select[SLOT->d1r + ksr];\r
 \r
                SLOT->eg_pack_d1r = eg_inc_pack[eg_sel] | (eg_sh<<24);\r
 \r
-               eg_sh  = eg_rate_shift [SLOT->d2r + SLOT->ksr];\r
-               eg_sel = eg_rate_select[SLOT->d2r + SLOT->ksr];\r
+               eg_sh  = eg_rate_shift [SLOT->d2r + ksr];\r
+               eg_sel = eg_rate_select[SLOT->d2r + ksr];\r
 \r
                SLOT->eg_pack_d2r = eg_inc_pack[eg_sel] | (eg_sh<<24);\r
 \r
-               eg_sh  = eg_rate_shift [SLOT->rr  + SLOT->ksr];\r
-               eg_sel = eg_rate_select[SLOT->rr  + SLOT->ksr];\r
+               eg_sh  = eg_rate_shift [SLOT->rr  + ksr];\r
+               eg_sel = eg_rate_select[SLOT->rr  + ksr];\r
 \r
                SLOT->eg_pack_rr = eg_inc_pack[eg_sel] | (eg_sh<<24);\r
        }\r
@@ -1290,6 +1293,7 @@ static void reset_channels(FM_CH *CH)
                        CH[c].SLOT[s].state= EG_OFF;\r
                        CH[c].SLOT[s].volume = MAX_ATT_INDEX;\r
                }\r
+               CH[c].mem_value = CH[c].op1_out = 0;\r
        }\r
        ym2612.slot_mask = 0;\r
 }\r
@@ -1478,17 +1482,17 @@ static int OPNWriteReg(int r, int v)
                set_ar_ksr(CH,SLOT,v);\r
                break;\r
 \r
-       case 0x60:      /* bit7 = AM ENABLE, DR */\r
+       case 0x60:      /* bit7 = AM ENABLE, DR | depends on ksr */\r
                set_dr(SLOT,v);\r
                if(v&0x80) CH->AMmasks |=   1<<OPN_SLOT(r);\r
                else       CH->AMmasks &= ~(1<<OPN_SLOT(r));\r
                break;\r
 \r
-       case 0x70:      /*     SR */\r
+       case 0x70:      /*     SR | depends on ksr */\r
                set_sr(SLOT,v);\r
                break;\r
 \r
-       case 0x80:      /* SL, RR */\r
+       case 0x80:      /* SL, RR | depends on ksr */\r
                set_sl_rr(SLOT,v);\r
                break;\r
 \r
@@ -1499,7 +1503,7 @@ static int OPNWriteReg(int r, int v)
 \r
        case 0xa0:\r
                switch( OPN_SLOT(r) ){\r
-               case 0:         /* 0xa0-0xa2 : FNUM1 */\r
+               case 0:         /* 0xa0-0xa2 : FNUM1 | depends on fn_h (below) */\r
                        {\r
                                UINT32 fn = (((UINT32)( (ym2612.OPN.ST.fn_h)&7))<<8) + v;\r
                                UINT8 blk = ym2612.OPN.ST.fn_h>>3;\r
@@ -1527,7 +1531,7 @@ static int OPNWriteReg(int r, int v)
                                ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];\r
                                /* phase increment counter */\r
                                ym2612.OPN.SL3.fc[c] = fn_table[fn*2]>>(7-blk);\r
-                               ym2612.OPN.SL3.block_fnum[c] = fn;\r
+                               ym2612.OPN.SL3.block_fnum[c] = (blk<<11) | fn;\r
                                ym2612.CH[2].SLOT[SLOT1].Incr=-1;\r
                        }\r
                        break;\r
@@ -1634,12 +1638,14 @@ int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty)
 \r
        /* mix to 32bit dest */\r
        // flags: stereo, ?, disabled, ?, pan_r, pan_l\r
+       chan_render_prep();\r
        if (ym2612.slot_mask & 0x00000f) active_chs |= chan_render(buffer, length, 0, stereo|((pan&0x003)<<4)) << 0;\r
        if (ym2612.slot_mask & 0x0000f0) active_chs |= chan_render(buffer, length, 1, stereo|((pan&0x00c)<<2)) << 1;\r
        if (ym2612.slot_mask & 0x000f00) active_chs |= chan_render(buffer, length, 2, stereo|((pan&0x030)   )) << 2;\r
        if (ym2612.slot_mask & 0x00f000) active_chs |= chan_render(buffer, length, 3, stereo|((pan&0x0c0)>>2)) << 3;\r
        if (ym2612.slot_mask & 0x0f0000) active_chs |= chan_render(buffer, length, 4, stereo|((pan&0x300)>>4)) << 4;\r
        if (ym2612.slot_mask & 0xf00000) active_chs |= chan_render(buffer, length, 5, stereo|((pan&0xc00)>>6)|(ym2612.dacen<<2)) << 5;\r
+       chan_render_finish();\r
 \r
        return active_chs; // 1 if buffer updated\r
 }\r
@@ -1863,26 +1869,264 @@ int YM2612PicoTick_(int n)
 \r
 void YM2612PicoStateLoad_(void)\r
 {\r
-#ifndef EXTERNAL_YM2612\r
-       int i, real_A1 = ym2612.addr_A1;\r
-\r
        reset_channels( &ym2612.CH[0] );\r
+       ym2612.slot_mask = 0xffffff;\r
+}\r
+\r
+/* rather stupid design because I wanted to fit in unused register "space" */\r
+typedef struct\r
+{\r
+       UINT32  state_phase;\r
+       INT16   volume;\r
+} ym_save_addon_slot;\r
+\r
+typedef struct\r
+{\r
+       UINT32  magic;\r
+       UINT8   address;\r
+       UINT8   status;\r
+       UINT8   addr_A1;\r
+       UINT8   unused;\r
+       int     TAT;\r
+       int     TBT;\r
+       UINT32  eg_cnt;         // 10\r
+       UINT32  eg_timer;\r
+       UINT32  lfo_cnt;\r
+       UINT16  lfo_ampm;\r
+       UINT8   fn_h;\r
+       UINT8   fn_h_sl3;\r
+       UINT32  keyon_field;    // 20\r
+       UINT32  kcode_fc_sl3_3;\r
+} ym_save_addon;\r
+\r
+typedef struct\r
+{\r
+       UINT16  block_fnum[6];\r
+       UINT16  block_fnum_sl3[3];\r
+       UINT16  unused;\r
+} ym_save_addon2;\r
+\r
 \r
-       // feed all the registers and update internal state\r
-       for(i = 0; i < 0x100; i++) {\r
-               YM2612Write_(0, i);\r
-               YM2612Write_(1, ym2612.REGS[i]);\r
+//static YM2612 check_ym;\r
+\r
+void YM2612PicoStateSave2(int tat, int tbt)\r
+{\r
+       ym_save_addon_slot ss;\r
+       ym_save_addon2 sa2;\r
+       ym_save_addon sa;\r
+       unsigned char *ptr;\r
+       int c, s;\r
+\r
+       refresh_fc_eg_chan( &ym2612.CH[0] );\r
+       refresh_fc_eg_chan( &ym2612.CH[1] );\r
+       if( (ym2612.OPN.ST.mode & 0xc0) )\r
+       {\r
+               /* 3SLOT MODE */\r
+               if( ym2612.CH[2].SLOT[SLOT1].Incr==-1)\r
+               {\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT1], ym2612.OPN.SL3.fc[1], ym2612.OPN.SL3.kcode[1] );\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT2], ym2612.OPN.SL3.fc[2], ym2612.OPN.SL3.kcode[2] );\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT3], ym2612.OPN.SL3.fc[0], ym2612.OPN.SL3.kcode[0] );\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT4], ym2612.CH[2].fc , ym2612.CH[2].kcode );\r
+               }\r
+       } else refresh_fc_eg_chan( &ym2612.CH[2] );\r
+       refresh_fc_eg_chan( &ym2612.CH[3] );\r
+       refresh_fc_eg_chan( &ym2612.CH[4] );\r
+       refresh_fc_eg_chan( &ym2612.CH[5] );\r
+\r
+       memset(&sa, 0, sizeof(sa));\r
+//memcpy(&check_ym, &ym2612, sizeof(ym2612));\r
+\r
+       // chans 1,2,3\r
+       ptr = &ym2612.REGS[0x0b8];\r
+       for (c = 0; c < 3; c++)\r
+       {\r
+               for (s = 0; s < 4; s++) {\r
+                       ss.state_phase = (ym2612.CH[c].SLOT[s].state << 29) | (ym2612.CH[c].SLOT[s].phase >> 3);\r
+                       ss.volume = ym2612.CH[c].SLOT[s].volume;\r
+                       if (ym2612.CH[c].SLOT[s].key)\r
+                               sa.keyon_field |= 1 << (c*4 + s);\r
+                       memcpy(ptr, &ss, 6);\r
+                       ptr += 6;\r
+               }\r
+               sa2.block_fnum[c] = ym2612.CH[c].block_fnum;\r
+       }\r
+       // chans 4,5,6\r
+       ptr = &ym2612.REGS[0x1b8];\r
+       for (; c < 6; c++)\r
+       {\r
+               for (s = 0; s < 4; s++) {\r
+                       ss.state_phase = (ym2612.CH[c].SLOT[s].state << 29) | (ym2612.CH[c].SLOT[s].phase >> 3);\r
+                       ss.volume = ym2612.CH[c].SLOT[s].volume;\r
+                       if (ym2612.CH[c].SLOT[s].key)\r
+                               sa.keyon_field |= 1 << (c*4 + s);\r
+                       memcpy(ptr, &ss, 6);\r
+                       ptr += 6;\r
+               }\r
+               sa2.block_fnum[c] = ym2612.CH[c].block_fnum;\r
        }\r
+       for (c = 0; c < 3; c++)\r
+       {\r
+               sa2.block_fnum_sl3[c] = ym2612.OPN.SL3.block_fnum[c];\r
+       }\r
+\r
+       memcpy(&ym2612.REGS[0], &sa2, sizeof(sa2)); // 0x20 max\r
+\r
+       // other things\r
+       ptr = &ym2612.REGS[0x100];\r
+       sa.magic = 0x41534d59; // 'YMSA'\r
+       sa.address = ym2612.OPN.ST.address;\r
+       sa.status  = ym2612.OPN.ST.status;\r
+       sa.addr_A1 = ym2612.addr_A1;\r
+       sa.unused  = 0;\r
+       sa.TAT     = tat;\r
+       sa.TBT     = tbt;\r
+       sa.eg_cnt  = ym2612.OPN.eg_cnt;\r
+       sa.eg_timer = ym2612.OPN.eg_timer;\r
+       sa.lfo_cnt  = ym2612.OPN.lfo_cnt;\r
+       sa.lfo_ampm = g_lfo_ampm;\r
+       sa.fn_h     = ym2612.REGS[0xa4] = ym2612.OPN.ST.fn_h;\r
+       sa.fn_h_sl3 = ym2612.REGS[0xac] = ym2612.OPN.SL3.fn_h;\r
+       memcpy(ptr, &sa, sizeof(sa)); // 0x30 max\r
+}\r
 \r
-       for(i = 0; i < 0x100; i++) {\r
-               YM2612Write_(2, i);\r
-               YM2612Write_(3, ym2612.REGS[i|0x100]);\r
+int YM2612PicoStateLoad2(int *tat, int *tbt)\r
+{\r
+       ym_save_addon_slot ss;\r
+       ym_save_addon2 sa2;\r
+       ym_save_addon sa;\r
+       unsigned char *ptr;\r
+       UINT32 fn;\r
+       UINT8 blk;\r
+       int c, s;\r
+\r
+       ptr = &ym2612.REGS[0x100];\r
+       memcpy(&sa, ptr, sizeof(sa)); // 0x30 max\r
+       if (sa.magic != 0x41534d59) return -1;\r
+\r
+       ptr = &ym2612.REGS[0];\r
+       memcpy(&sa2, ptr, sizeof(sa2));\r
+\r
+       ym2612.OPN.ST.address = sa.address;\r
+       ym2612.OPN.ST.status = sa.status;\r
+       ym2612.addr_A1 = sa.addr_A1;\r
+       ym2612.OPN.eg_cnt = sa.eg_cnt;\r
+       ym2612.OPN.eg_timer = sa.eg_timer;\r
+       ym2612.OPN.lfo_cnt = sa.lfo_cnt;\r
+       g_lfo_ampm = sa.lfo_ampm;\r
+       if (tat != NULL) *tat = sa.TAT;\r
+       if (tbt != NULL) *tbt = sa.TBT;\r
+\r
+       // chans 1,2,3\r
+       ptr = &ym2612.REGS[0x0b8];\r
+       for (c = 0; c < 3; c++)\r
+       {\r
+               for (s = 0; s < 4; s++) {\r
+                       memcpy(&ss, ptr, 6);\r
+                       ym2612.CH[c].SLOT[s].state = ss.state_phase >> 29;\r
+                       ym2612.CH[c].SLOT[s].phase = ss.state_phase << 3;\r
+                       ym2612.CH[c].SLOT[s].volume = ss.volume;\r
+                       ym2612.CH[c].SLOT[s].key = (sa.keyon_field & (1 << (c*4 + s))) ? 1 : 0;\r
+                       ym2612.CH[c].SLOT[s].ksr = (UINT8)-1;\r
+//ym2612.CH[c].SLOT[s].Incr = check_ym.CH[c].SLOT[s].Incr;\r
+                       ptr += 6;\r
+               }\r
+               ym2612.CH[c].SLOT[SLOT1].Incr=-1;\r
+               ym2612.CH[c].block_fnum = sa2.block_fnum[c];\r
+               fn = ym2612.CH[c].block_fnum & 0x7ff;\r
+               blk = ym2612.CH[c].block_fnum >> 11;\r
+               ym2612.CH[c].kcode= (blk<<2) | opn_fktable[fn >> 7];\r
+               ym2612.CH[c].fc = fn_table[fn*2]>>(7-blk);\r
+       }\r
+       // chans 4,5,6\r
+       ptr = &ym2612.REGS[0x1b8];\r
+       for (; c < 6; c++)\r
+       {\r
+               for (s = 0; s < 4; s++) {\r
+                       memcpy(&ss, ptr, 6);\r
+                       ym2612.CH[c].SLOT[s].state = ss.state_phase >> 29;\r
+                       ym2612.CH[c].SLOT[s].phase = ss.state_phase << 3;\r
+                       ym2612.CH[c].SLOT[s].volume = ss.volume;\r
+                       ym2612.CH[c].SLOT[s].key = (sa.keyon_field & (1 << (c*4 + s))) ? 1 : 0;\r
+                       ym2612.CH[c].SLOT[s].ksr = (UINT8)-1;\r
+//ym2612.CH[c].SLOT[s].Incr = check_ym.CH[c].SLOT[s].Incr;\r
+                       ptr += 6;\r
+               }\r
+               ym2612.CH[c].SLOT[SLOT1].Incr=-1;\r
+               ym2612.CH[c].block_fnum = sa2.block_fnum[c];\r
+               fn = ym2612.CH[c].block_fnum & 0x7ff;\r
+               blk = ym2612.CH[c].block_fnum >> 11;\r
+               ym2612.CH[c].kcode= (blk<<2) | opn_fktable[fn >> 7];\r
+               ym2612.CH[c].fc = fn_table[fn*2]>>(7-blk);\r
+       }\r
+       for (c = 0; c < 3; c++)\r
+       {\r
+               ym2612.OPN.SL3.block_fnum[c] = sa2.block_fnum_sl3[c];\r
+               fn = ym2612.OPN.SL3.block_fnum[c] & 0x7ff;\r
+               blk = ym2612.OPN.SL3.block_fnum[c] >> 11;\r
+               ym2612.OPN.SL3.kcode[c]= (blk<<2) | opn_fktable[fn >> 7];\r
+               ym2612.OPN.SL3.fc[c] = fn_table[fn*2]>>(7-blk);\r
        }\r
 \r
-       ym2612.addr_A1 = real_A1;\r
-#else\r
-       reset_channels( &ym2612.CH[0] );\r
+#if 0\r
+       refresh_fc_eg_chan( &ym2612.CH[0] );\r
+       refresh_fc_eg_chan( &ym2612.CH[1] );\r
+       if( (ym2612.OPN.ST.mode & 0xc0) )\r
+       {\r
+               /* 3SLOT MODE */\r
+               if( ym2612.CH[2].SLOT[SLOT1].Incr==-1)\r
+               {\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT1], ym2612.OPN.SL3.fc[1], ym2612.OPN.SL3.kcode[1] );\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT2], ym2612.OPN.SL3.fc[2], ym2612.OPN.SL3.kcode[2] );\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT3], ym2612.OPN.SL3.fc[0], ym2612.OPN.SL3.kcode[0] );\r
+                       refresh_fc_eg_slot(&ym2612.CH[2].SLOT[SLOT4], ym2612.CH[2].fc , ym2612.CH[2].kcode );\r
+               }\r
+       } else refresh_fc_eg_chan( &ym2612.CH[2] );\r
+       refresh_fc_eg_chan( &ym2612.CH[3] );\r
+       refresh_fc_eg_chan( &ym2612.CH[4] );\r
+       refresh_fc_eg_chan( &ym2612.CH[5] );\r
+\r
+       for (c = 0; c < 6; c++)\r
+       {\r
+               for (s = 0; s < 4; s++)\r
+               {\r
+                       int i;\r
+                       unsigned char *chk = ((unsigned char *) &check_ym.CH[c].SLOT[s]);\r
+                       unsigned char *res = ((unsigned char *) &ym2612.CH[c].SLOT[s]);\r
+\r
+                       for (i = 0; i < sizeof(check_ym.CH[0].SLOT[0]); i++)\r
+                       {\r
+                               if (i != 0x0c && chk[i] != res[i])\r
+                                       printf("ch[%i].slot[%i].%03x: %02x vs %02x\n", c, s, i, chk[i], res[i]);\r
+                       }\r
+               }\r
+\r
+               {\r
+                       int i;\r
+                       unsigned char *chk = ((unsigned char *) &check_ym.CH[c].ALGO);\r
+                       unsigned char *res = ((unsigned char *) &ym2612.CH[c].ALGO);\r
+\r
+                       for (i = 0; i < 8*4; i++)\r
+                       {\r
+                               if ((i < 4 || i > 0x0b) && chk[i] != res[i])\r
+                                       printf("ch[%i].%03x: %02x vs %02x\n", c, i, chk[i], res[i]);\r
+                       }\r
+               }\r
+       }\r
+\r
+       for (c = 0; c < sizeof(ym2612.OPN); c++)\r
+       {\r
+               unsigned char *chk = ((unsigned char *) &check_ym.OPN);\r
+               unsigned char *res = ((unsigned char *) &ym2612.OPN);\r
+\r
+               if (chk[c] != res[c])\r
+               {\r
+                       printf("OPN: %03x: %02x vs %02x\n", c, chk[c], res[c]);\r
+               }\r
+       }\r
 #endif\r
+\r
+       return 0;\r
 }\r
 \r
 #ifndef EXTERNAL_YM2612\r