initial code for Nemesis' blip discovery
[picodrive.git] / Pico / sound / ym2612.c
index 41defb9..a619c45 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
@@ -1893,8 +1901,7 @@ 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
 } ym_save_addon;\r
@@ -1915,23 +1922,6 @@ 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
        memset(&sa2, 0, sizeof(sa2));\r
 \r
@@ -1976,15 +1966,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