z80/ym2612 reset handling improved
[picodrive.git] / Pico / sound / ym2612.c
index 294d621..0c26739 100644 (file)
@@ -381,7 +381,7 @@ static const UINT8 dt_tab[4 * 32]={
        5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,\r
 /* FD=3 */\r
        2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,\r
-       8 , 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22\r
+       8 ,8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22\r
 };\r
 \r
 \r
@@ -1152,9 +1152,9 @@ static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: s
                        UINT32 fn;\r
                        int kc,fc;\r
 \r
+                       blk = block_fnum >> 11;\r
                        block_fnum = block_fnum*2 + lfo_fn_table_index_offset;\r
 \r
-                       blk = (block_fnum&0x7000) >> 12;\r
                        fn  = block_fnum & 0xfff;\r
 \r
                        /* keyscale code */\r
@@ -1203,10 +1203,14 @@ static int chan_render(int *buffer, int length, int c, UINT32 flags) // flags: s
 /* update phase increment and envelope generator */\r
 INLINE void refresh_fc_eg_slot(FM_SLOT *SLOT, int fc, int kc)\r
 {\r
-       int ksr;\r
+       int ksr, fdt;\r
 \r
        /* (frequency) phase increment counter */\r
-       SLOT->Incr = ((fc+SLOT->DT[kc])*SLOT->mul) >> 1;\r
+       fdt = fc+SLOT->DT[kc];\r
+       /* detect overflow */\r
+//     if (fdt < 0) fdt += fn_table[0x7ff*2] >> (7-blk-1);\r
+       if (fdt < 0) fdt += fn_table[0x7ff*2] >> 2;\r
+       SLOT->Incr = fdt*SLOT->mul >> 1;\r
 \r
        ksr = kc >> SLOT->KSR;\r
        if( SLOT->ksr != ksr )\r
@@ -1258,6 +1262,17 @@ INLINE void refresh_fc_eg_chan(FM_CH *CH)
        }\r
 }\r
 \r
+INLINE void refresh_fc_eg_chan_sl3(void)\r
+{\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
+}\r
+\r
 /* initialize time tables */\r
 static void init_timetables(const UINT8 *dttable)\r
 {\r
@@ -1428,7 +1443,6 @@ static void OPNSetPres(int pres)
 \r
        ym2612.OPN.eg_timer_add  = (1<<EG_SH) * ym2612.OPN.ST.freqbase;\r
 \r
-\r
        /* make time tables */\r
        init_timetables( dt_tab );\r
 \r
@@ -1505,8 +1519,8 @@ static int OPNWriteReg(int r, int v)
                switch( OPN_SLOT(r) ){\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
+                               UINT32 fn = (((UINT32)( (CH->fn_h)&7))<<8) + v;\r
+                               UINT8 blk = CH->fn_h>>3;\r
                                /* keyscale code */\r
                                CH->kcode = (blk<<2) | opn_fktable[fn >> 7];\r
                                /* phase increment counter */\r
@@ -1519,7 +1533,7 @@ static int OPNWriteReg(int r, int v)
                        }\r
                        break;\r
                case 1:         /* 0xa4-0xa6 : FNUM2,BLK */\r
-                       ym2612.OPN.ST.fn_h = v&0x3f;\r
+                       CH->fn_h = v&0x3f;\r
                        ret = 0;\r
                        break;\r
                case 2:         /* 0xa8-0xaa : 3CH FNUM1 */\r
@@ -1619,16 +1633,10 @@ int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty)
        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_sl3();\r
+       else\r
+               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
@@ -1660,6 +1668,8 @@ void YM2612Init_(int clock, int rate)
        ym2612.OPN.ST.clock = clock;\r
        ym2612.OPN.ST.rate = rate;\r
 \r
+       OPNSetPres( 6*24 );\r
+\r
        /* Extend handler */\r
        YM2612ResetChip_();\r
 }\r
@@ -1672,7 +1682,6 @@ void YM2612ResetChip_(void)
 \r
        memset(ym2612.REGS, 0, sizeof(ym2612.REGS));\r
 \r
-       OPNSetPres( 6*24 );\r
        set_timers( 0x30 ); /* mode 0 , timer reset */\r
        ym2612.REGS[0x27] = 0x30;\r
 \r
@@ -1771,11 +1780,11 @@ int YM2612Write_(unsigned int a, unsigned int v)
                                }\r
                                ret=0;\r
                                break;\r
+#endif\r
                        case 0x27:      /* mode, timer control */\r
                                set_timers( v );\r
                                ret=0;\r
                                break;\r
-#endif\r
                        case 0x28:      /* key on / off */\r
                                {\r
                                        UINT8 c;\r
@@ -1789,7 +1798,6 @@ int YM2612Write_(unsigned int a, unsigned int v)
                                        if(v&0x80) FM_KEYON(c,SLOT4); else FM_KEYOFF(c,SLOT4);\r
                                        break;\r
                                }\r
-#if 0\r
                        case 0x2a:      /* DAC data (YM2612) */\r
                                ym2612.dacout = ((int)v - 0x80) << 6;   /* level unknown (notaz: 8 seems to be too much) */\r
                                ret=0;\r
@@ -1799,7 +1807,6 @@ int YM2612Write_(unsigned int a, unsigned int v)
                                ym2612.dacen = v & 0x80;\r
                                ret=0;\r
                                break;\r
-#endif\r
                        default:\r
                                break;\r
                        }\r
@@ -1827,12 +1834,7 @@ int YM2612Write_(unsigned int a, unsigned int v)
                ret = OPNWriteReg(addr, v);\r
                break;\r
        }\r
-/*\r
-       if(ret) {\r
-               extern int Scanline;\r
-               dprintf("ymw [%i]", Scanline);\r
-       }\r
-*/\r
+\r
        return ret;\r
 }\r
 \r
@@ -1893,22 +1895,20 @@ typedef struct
        UINT32  eg_timer;\r
        UINT32  lfo_cnt;\r
        UINT16  lfo_ampm;\r
-       UINT8   fn_h;\r
-       UINT8   fn_h_sl3;\r
+       UINT16  unused2;\r
        UINT32  keyon_field;    // 20\r
        UINT32  kcode_fc_sl3_3;\r
+       UINT32  reserved[2];\r
 } ym_save_addon;\r
 \r
 typedef struct\r
 {\r
        UINT16  block_fnum[6];\r
        UINT16  block_fnum_sl3[3];\r
-       UINT16  unused;\r
+       UINT16  reserved[7];\r
 } ym_save_addon2;\r
 \r
 \r
-//static YM2612 check_ym;\r
-\r
 void YM2612PicoStateSave2(int tat, int tbt)\r
 {\r
        ym_save_addon_slot ss;\r
@@ -1917,25 +1917,8 @@ void YM2612PicoStateSave2(int tat, int tbt)
        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
+       memset(&sa2, 0, sizeof(sa2));\r
 \r
        // chans 1,2,3\r
        ptr = &ym2612.REGS[0x0b8];\r
@@ -1978,15 +1961,12 @@ void YM2612PicoStateSave2(int tat, int tbt)
        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
@@ -2028,7 +2008,6 @@ int YM2612PicoStateLoad2(int *tat, int *tbt)
                        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
@@ -2049,7 +2028,6 @@ int YM2612PicoStateLoad2(int *tat, int *tbt)
                        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
@@ -2068,64 +2046,6 @@ int YM2612PicoStateLoad2(int *tat, int *tbt)
                ym2612.OPN.SL3.fc[c] = fn_table[fn*2]>>(7-blk);\r
        }\r
 \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