clean up dac code a bit
authornotaz <notasas@gmail.com>
Fri, 29 Sep 2017 23:23:13 +0000 (02:23 +0300)
committernotaz <notasas@gmail.com>
Fri, 6 Oct 2017 22:36:59 +0000 (01:36 +0300)
pico/debug.c
pico/memory.c
pico/pico_cmn.c
pico/pico_int.h
pico/sms.c
pico/sound/sound.c
pico/sound/ym2612.c

index e3ef83b..e780262 100644 (file)
@@ -366,31 +366,31 @@ void PDebugZ80Frame(void)
   }
 
   z80_resetCycles();
   }
 
   z80_resetCycles();
-  emustatus &= ~1;
+  PsndStartFrame();
 
 
-  if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
-    PicoSyncZ80(line_sample*488);
-  if (ym2612.dacen && PsndDacLine <= line_sample)
-    PsndDoDAC(line_sample);
+  if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
+    PicoSyncZ80(SekCycleCnt + line_sample * 488);
   if (PsndOut)
     PsndGetSamples(line_sample);
 
   if (PsndOut)
     PsndGetSamples(line_sample);
 
-  if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
-    PicoSyncZ80(224*488);
+  if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
+    PicoSyncZ80(SekCycleCnt + 224 * 488);
     z80_int();
   }
     z80_int();
   }
-  if (ym2612.dacen && PsndDacLine <= 224)
-    PsndDoDAC(224);
   if (PsndOut)
     PsndGetSamples(224);
 
   // sync z80
   if (PsndOut)
     PsndGetSamples(224);
 
   // sync z80
-  if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
-    PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
-  if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
-    PsndDoDAC(lines-1);
+  if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
+    SekCycleCnt += Pico.m.pal ? 151809 : 127671; // cycles adjusted for converter
+    PicoSyncZ80(SekCycleCnt);
+  }
+  if (PsndOut && ym2612.dacen && PsndDacLine < lines)
+    PsndDoDAC(lines - 1);
+  PsndDoPSG(lines - 1);
 
   timers_cycle();
 
   timers_cycle();
+  SekCycleAim = SekCycleCnt;
 }
 
 void PDebugCPUStep(void)
 }
 
 void PDebugCPUStep(void)
index cb1b5ac..fb5fa9b 100644 (file)
@@ -932,7 +932,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
     int scanline = get_scanline(is_from_z80);\r
     //elprintf(EL_STATUS, "%03i -> %03i dac w %08x z80 %i", PsndDacLine, scanline, d, is_from_z80);\r
     ym2612.dacout = ((int)d - 0x80) << 6;\r
     int scanline = get_scanline(is_from_z80);\r
     //elprintf(EL_STATUS, "%03i -> %03i dac w %08x z80 %i", PsndDacLine, scanline, d, is_from_z80);\r
     ym2612.dacout = ((int)d - 0x80) << 6;\r
-    if (PsndOut && ym2612.dacen && scanline >= PsndDacLine)\r
+    if (ym2612.dacen)\r
       PsndDoDAC(scanline);\r
     return 0;\r
   }\r
       PsndDoDAC(scanline);\r
     return 0;\r
   }\r
@@ -1016,8 +1016,10 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
         }\r
         case 0x2b: { /* DAC Sel  (YM2612) */\r
           int scanline = get_scanline(is_from_z80);\r
         }\r
         case 0x2b: { /* DAC Sel  (YM2612) */\r
           int scanline = get_scanline(is_from_z80);\r
-          ym2612.dacen = d & 0x80;\r
-          if (d & 0x80) PsndDacLine = scanline;\r
+          if (ym2612.dacen != (d & 0x80)) {\r
+            ym2612.dacen = d & 0x80;\r
+            PsndDacLine = scanline;\r
+          }\r
 #ifdef __GP2X__\r
           if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, scanline);\r
 #endif\r
 #ifdef __GP2X__\r
           if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, scanline);\r
 #endif\r
index 2de65d0..b39cfdb 100644 (file)
@@ -85,8 +85,7 @@ static int PicoFrameHints(void)
   }
 
   z80_resetCycles();
   }
 
   z80_resetCycles();
-  PsndDacLine = 0;
-  emustatus &= ~1;
+  PsndStartFrame();
 
   pv->status&=~0x88; // clear V-Int, come out of vblank
 
 
   pv->status&=~0x88; // clear V-Int, come out of vblank
 
@@ -145,8 +144,6 @@ static int PicoFrameHints(void)
 
       if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
         PicoSyncZ80(cycles);
 
       if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
         PicoSyncZ80(cycles);
-      if (ym2612.dacen && PsndDacLine <= y)
-        PsndDoDAC(y);
 #ifdef PICO_CD
       if (PicoAHW & PAHW_MCD)
         pcd_sync_s68k(cycles, 0);
 #ifdef PICO_CD
       if (PicoAHW & PAHW_MCD)
         pcd_sync_s68k(cycles, 0);
@@ -230,11 +227,7 @@ static int PicoFrameHints(void)
 
   // get samples from sound chips
   if (y == 224 && PsndOut)
 
   // get samples from sound chips
   if (y == 224 && PsndOut)
-  {
-    if (ym2612.dacen && PsndDacLine <= y)
-      PsndDoDAC(y);
     PsndGetSamples(y);
     PsndGetSamples(y);
-  }
 
   // Run scanline:
   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD);
 
   // Run scanline:
   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD);
@@ -269,8 +262,8 @@ static int PicoFrameHints(void)
   cycles = SekCyclesDone();
   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
     PicoSyncZ80(cycles);
   cycles = SekCyclesDone();
   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
     PicoSyncZ80(cycles);
-  if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
-    PsndDoDAC(lines-1);
+  if (PsndOut && ym2612.dacen && PsndDacLine < lines)
+    PsndDoDAC(lines - 1);
 
 #ifdef PICO_CD
   if (PicoAHW & PAHW_MCD)
 
 #ifdef PICO_CD
   if (PicoAHW & PAHW_MCD)
index 2249148..551dcbc 100644 (file)
@@ -820,6 +820,7 @@ PICO_INTERNAL_ASM void wram_1M_to_2M(unsigned char *m);
 \r
 // sound/sound.c\r
 PICO_INTERNAL void PsndReset(void);\r
 \r
 // sound/sound.c\r
 PICO_INTERNAL void PsndReset(void);\r
+PICO_INTERNAL void PsndStartFrame(void);\r
 PICO_INTERNAL void PsndDoDAC(int line_to);\r
 PICO_INTERNAL void PsndClear(void);\r
 PICO_INTERNAL void PsndGetSamples(int y);\r
 PICO_INTERNAL void PsndDoDAC(int line_to);\r
 PICO_INTERNAL void PsndClear(void);\r
 PICO_INTERNAL void PsndGetSamples(int y);\r
index 8c44d51..7dc796e 100644 (file)
@@ -258,6 +258,8 @@ void PicoFrameMS(void)
   int nmi;
   int y;
 
   int nmi;
   int y;
 
+  PsndStartFrame();
+
   nmi = (PicoPad[0] >> 7) & 1;
   if (!Pico.ms.nmi_state && nmi)
     z80_nmi();
   nmi = (PicoPad[0] >> 7) & 1;
   if (!Pico.ms.nmi_state && nmi)
     z80_nmi();
index 69c1be0..8f88dd7 100644 (file)
 #include "../cd/cue.h"\r
 #include "mix.h"\r
 \r
 #include "../cd/cue.h"\r
 #include "mix.h"\r
 \r
-#define SIMPLE_WRITE_SOUND 0\r
-\r
 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
 \r
 // master int buffer to mix to\r
 static int PsndBuffer[2*(44100+100)/50];\r
 \r
 // dac\r
 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
 \r
 // master int buffer to mix to\r
 static int PsndBuffer[2*(44100+100)/50];\r
 \r
 // dac\r
-static unsigned short dac_info[312+4]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample\r
+static unsigned short dac_info[312+4]; // pos in sample buffer\r
 \r
 // cdda output buffer\r
 short cdda_out_buffer[2*1152];\r
 \r
 // cdda output buffer\r
 short cdda_out_buffer[2*1152];\r
@@ -34,6 +32,7 @@ int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22
 int PsndLen_exc_cnt=0;\r
 int PsndDacLine=0;\r
 short *PsndOut=NULL; // PCM data buffer\r
 int PsndLen_exc_cnt=0;\r
 int PsndDacLine=0;\r
 short *PsndOut=NULL; // PCM data buffer\r
+static int PsndLen_use;\r
 \r
 // timers\r
 int timer_a_next_oflow, timer_a_step; // in z80 cycles\r
 \r
 // timers\r
 int timer_a_next_oflow, timer_a_step; // in z80 cycles\r
@@ -57,14 +56,12 @@ static void dac_recalculate(void)
     for(i=226; i != 225; i++)\r
     {\r
       if (i >= lines) i = 0;\r
     for(i=226; i != 225; i++)\r
     {\r
       if (i >= lines) i = 0;\r
-      len = 0;\r
       if(dac_cnt < 0) {\r
       if(dac_cnt < 0) {\r
-        len=1;\r
         pos++;\r
         dac_cnt += lines;\r
       }\r
       dac_cnt -= PsndLen;\r
         pos++;\r
         dac_cnt += lines;\r
       }\r
       dac_cnt -= PsndLen;\r
-      dac_info[i] = (pos<<4)|len;\r
+      dac_info[i] = pos;\r
     }\r
   }\r
   else\r
     }\r
   }\r
   else\r
@@ -86,24 +83,12 @@ static void dac_recalculate(void)
           len++;\r
         }\r
       dac_cnt += PsndLen;\r
           len++;\r
         }\r
       dac_cnt += PsndLen;\r
-      dac_info[i] = (pos<<4)|len;\r
-      pos+=len;\r
+      pos += len;\r
+      dac_info[i] = pos;\r
     }\r
     }\r
-    // last sample\r
-    for(len = 0, i = pos; i < PsndLen; i++) len++;\r
-    if (PsndLen_exc_add) len++;\r
-    dac_info[224] = (pos<<4)|len;\r
   }\r
   }\r
-  mid = (dac_info[lines-1] & 0xfff0) + ((dac_info[lines-1] & 0xf) << 4);\r
   for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)\r
   for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)\r
-    dac_info[i] = mid;\r
-  //for(i=len=0; i < lines; i++) {\r
-  //  printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);\r
-  //  len+=dac_info[i]&0xf;\r
-  //}\r
-  //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));\r
-  //printf("len total: %i, last pos: %i\n", len, pos);\r
-  //exit(8);\r
+    dac_info[i] = dac_info[0];\r
 }\r
 \r
 \r
 }\r
 \r
 \r
@@ -163,6 +148,21 @@ void PsndRerate(int preserve_state)
 }\r
 \r
 \r
 }\r
 \r
 \r
+PICO_INTERNAL void PsndStartFrame(void)\r
+{\r
+  // compensate for float part of PsndLen\r
+  PsndLen_use = PsndLen;\r
+  PsndLen_exc_cnt += PsndLen_exc_add;\r
+  if (PsndLen_exc_cnt >= 0x10000) {\r
+    PsndLen_exc_cnt -= 0x10000;\r
+    PsndLen_use++;\r
+  }\r
+\r
+  PsndDacLine = 0;\r
+  emustatus &= ~1;\r
+  dac_info[224] = PsndLen_use;\r
+}\r
+\r
 PICO_INTERNAL void PsndDoDAC(int line_to)\r
 {\r
   int pos, pos1, len;\r
 PICO_INTERNAL void PsndDoDAC(int line_to)\r
 {\r
   int pos, pos1, len;\r
@@ -172,12 +172,16 @@ PICO_INTERNAL void PsndDoDAC(int line_to)
   if (line_to >= 312)\r
     line_to = 311;\r
 \r
   if (line_to >= 312)\r
     line_to = 311;\r
 \r
+  pos  = dac_info[line_from];\r
+  pos1 = dac_info[line_to + 1];\r
+  len = pos1 - pos;\r
+  if (len <= 0)\r
+    return;\r
+\r
   PsndDacLine = line_to + 1;\r
 \r
   PsndDacLine = line_to + 1;\r
 \r
-  pos =dac_info[line_from]>>4;\r
-  pos1=dac_info[line_to];\r
-  len = ((pos1>>4)-pos) + (pos1&0xf);\r
-  if (!len) return;\r
+  if (!PsndOut)\r
+    return;\r
 \r
   if (PicoOpt & POPT_EN_STEREO) {\r
     short *d = PsndOut + pos*2;\r
 \r
   if (PicoOpt & POPT_EN_STEREO) {\r
     short *d = PsndOut + pos*2;\r
@@ -260,17 +264,6 @@ static int PsndRender(int offset, int length)
 \r
   pprof_start(sound);\r
 \r
 \r
   pprof_start(sound);\r
 \r
-#if !SIMPLE_WRITE_SOUND\r
-  if (offset == 0) { // should happen once per frame\r
-    // compensate for float part of PsndLen\r
-    PsndLen_exc_cnt += PsndLen_exc_add;\r
-    if (PsndLen_exc_cnt >= 0x10000) {\r
-      PsndLen_exc_cnt -= 0x10000;\r
-      length++;\r
-    }\r
-  }\r
-#endif\r
-\r
   // PSG\r
   if (PicoOpt & POPT_EN_PSG)\r
     SN76496Update(PsndOut+offset, length, stereo);\r
   // PSG\r
   if (PicoOpt & POPT_EN_PSG)\r
     SN76496Update(PsndOut+offset, length, stereo);\r
@@ -322,20 +315,16 @@ static int PsndRender(int offset, int length)
 // to be called on 224 or line_sample scanlines only\r
 PICO_INTERNAL void PsndGetSamples(int y)\r
 {\r
 // to be called on 224 or line_sample scanlines only\r
 PICO_INTERNAL void PsndGetSamples(int y)\r
 {\r
-#if SIMPLE_WRITE_SOUND\r
-  if (y != 224) return;\r
-  PsndRender(0, PsndLen);\r
-  if (PicoWriteSound)\r
-    PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
-  PsndClear();\r
-#else\r
   static int curr_pos = 0;\r
 \r
   static int curr_pos = 0;\r
 \r
+  if (ym2612.dacen && PsndDacLine < y)\r
+    PsndDoDAC(y - 1);\r
+\r
   if (y == 224)\r
   {\r
     if (emustatus & 2)\r
          curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
   if (y == 224)\r
   {\r
     if (emustatus & 2)\r
          curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
-    else curr_pos  = PsndRender(0, PsndLen);\r
+    else curr_pos  = PsndRender(0, PsndLen_use);\r
     if (emustatus & 1)\r
          emustatus |=  2;\r
     else emustatus &= ~2;\r
     if (emustatus & 1)\r
          emustatus |=  2;\r
     else emustatus &= ~2;\r
@@ -343,28 +332,20 @@ PICO_INTERNAL void PsndGetSamples(int y)
       PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
     // clear sound buffer\r
     PsndClear();\r
       PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
     // clear sound buffer\r
     PsndClear();\r
+    PsndDacLine = 224;\r
+    dac_info[224] = 0;\r
   }\r
   else if (emustatus & 3) {\r
     emustatus|= 2;\r
     emustatus&=~1;\r
     curr_pos = PsndRender(0, PsndLen/2);\r
   }\r
   }\r
   else if (emustatus & 3) {\r
     emustatus|= 2;\r
     emustatus&=~1;\r
     curr_pos = PsndRender(0, PsndLen/2);\r
   }\r
-#endif\r
 }\r
 \r
 PICO_INTERNAL void PsndGetSamplesMS(void)\r
 {\r
   int stereo = (PicoOpt & 8) >> 3;\r
 }\r
 \r
 PICO_INTERNAL void PsndGetSamplesMS(void)\r
 {\r
   int stereo = (PicoOpt & 8) >> 3;\r
-  int length = PsndLen;\r
-\r
-#if !SIMPLE_WRITE_SOUND\r
-  // compensate for float part of PsndLen\r
-  PsndLen_exc_cnt += PsndLen_exc_add;\r
-  if (PsndLen_exc_cnt >= 0x10000) {\r
-    PsndLen_exc_cnt -= 0x10000;\r
-    length++;\r
-  }\r
-#endif\r
+  int length = PsndLen_use;\r
 \r
   // PSG\r
   if (PicoOpt & POPT_EN_PSG)\r
 \r
   // PSG\r
   if (PicoOpt & POPT_EN_PSG)\r
@@ -382,3 +363,4 @@ PICO_INTERNAL void PsndGetSamplesMS(void)
   PsndClear();\r
 }\r
 \r
   PsndClear();\r
 }\r
 \r
+// vim:shiftwidth=2:ts=2:expandtab\r
index e712278..0867f55 100644 (file)
@@ -1679,6 +1679,7 @@ void YM2612ResetChip_(void)
        for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(i,0);\r
        /* DAC mode clear */\r
        ym2612.dacen = 0;\r
        for(i = 0x26 ; i >= 0x20 ; i-- ) OPNWriteReg(i,0);\r
        /* DAC mode clear */\r
        ym2612.dacen = 0;\r
+       ym2612.dacout = 0;\r
        ym2612.addr_A1 = 0;\r
 }\r
 \r
        ym2612.addr_A1 = 0;\r
 }\r
 \r