Browse Source

debug bgm player, sound code refactoring

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@598 be3aeb3a-fb24-0410-a615-afba39da0efa
notaz 15 years ago
parent
commit
7b3f44c6b6
12 changed files with 161 additions and 86 deletions
  1. 0 9
      pico/cd/pico.c
  2. 41 0
      pico/debug.c
  3. 1 0
      pico/debug.h
  4. 0 29
      pico/pico.c
  5. 9 15
      pico/pico_cmn.c
  6. 1 1
      pico/pico_int.h
  7. 30 1
      pico/sound/sound.c
  8. 5 1
      platform/common/emu.h
  9. 19 1
      platform/common/menu.c
  10. 42 25
      platform/gp2x/emu.c
  11. 1 0
      platform/gp2x/gp2x.h
  12. 12 4
      platform/psp/emu.c

+ 0 - 9
pico/cd/pico.c

@@ -225,15 +225,6 @@ static __inline void update_chips(void)
 }
 
 
-static __inline void getSamples(int y)
-{
-  int len = PsndRender(0, PsndLen);
-  if (PicoWriteSound) PicoWriteSound(len);
-  // clear sound buffer
-  PsndClear();
-}
-
-
 #define PICO_CD
 #include "../pico_cmn.c"
 

+ 41 - 0
pico/debug.c

@@ -2,6 +2,7 @@
 // (c) Copyright 2008 notaz, All rights reserved.
 
 #include "pico_int.h"
+#include "sound/ym2612.h"
 #include "debug.h"
 
 #define bit(r, x) ((r>>x)&1)
@@ -301,3 +302,43 @@ void PDebugDumpMem(void)
   }
 }
 
+void PDebugZ80Frame(void)
+{
+  int lines, line_sample;
+
+  if (Pico.m.pal) {
+    lines = 312;
+    line_sample = 68;
+  } else {
+    lines = 262;
+    line_sample = 93;
+  }
+
+  z80_resetCycles();
+  emustatus &= ~1;
+
+  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 (PsndOut)
+    PsndGetSamples(line_sample);
+
+  if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
+    PicoSyncZ80(224*488);
+    z80_int();
+  }
+  if (ym2612.dacen && PsndDacLine <= 224)
+    PsndDoDAC(224);
+  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);
+
+  timers_cycle();
+}
+

+ 1 - 0
pico/debug.h

@@ -5,4 +5,5 @@ void PDebugShowSpriteStats(unsigned short *screen, int stride);
 void PDebugShowPalette(unsigned short *screen, int stride);
 void PDebugShowSprite(unsigned short *screen, int stride, int which);
 void PDebugDumpMem(void);
+void PDebugZ80Frame(void);
 

+ 0 - 29
pico/pico.c

@@ -258,35 +258,6 @@ static __inline void SekRunM68k(int cyc)
 #endif
 }
 
-
-// to be called on 224 or line_sample scanlines only
-static __inline void getSamples(int y)
-{
-#if SIMPLE_WRITE_SOUND
-  if (y != 224) return;
-  PsndRender(0, PsndLen);
-  if (PicoWriteSound) PicoWriteSound(PsndLen);
-  PsndClear();
-#else
-  static int curr_pos = 0;
-
-  if(y == 224) {
-    if(emustatus & 2)
-         curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);
-    else curr_pos  = PsndRender(0, PsndLen);
-    if (emustatus&1) emustatus|=2; else emustatus&=~2;
-    if (PicoWriteSound) PicoWriteSound(curr_pos);
-    // clear sound buffer
-    PsndClear();
-  }
-  else if(emustatus & 3) {
-    emustatus|= 2;
-    emustatus&=~1;
-    curr_pos = PsndRender(0, PsndLen/2);
-  }
-#endif
-}
-
 #include "pico_cmn.c"
 
 int z80stopCycle;

+ 9 - 15
pico/pico_cmn.c

@@ -64,6 +64,7 @@ static int PicoFrameHints(void)
   SekCyclesResetS68k();
 #endif
   PsndDacLine = 0;
+  emustatus &= ~1;
 
   pv->status&=~0x88; // clear V-Int, come out of vblank
 
@@ -118,19 +119,15 @@ static int PicoFrameHints(void)
       }
     }
 
-#ifndef PICO_CD
     // get samples from sound chips
-    if (y == 32 && PsndOut)
-      emustatus &= ~1;
-    else if ((y == 224 || y == line_sample) && PsndOut)
+    if ((y == 224 || y == line_sample) && PsndOut)
     {
       if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
         PicoSyncZ80(SekCycleCnt);
       if (ym2612.dacen && PsndDacLine <= y)
         PsndDoDAC(y);
-      getSamples(y);
+      PsndGetSamples(y);
     }
-#endif
 
     // Run scanline:
     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
@@ -196,15 +193,12 @@ static int PicoFrameHints(void)
   }
 
   // get samples from sound chips
-#ifndef PICO_CD
-  if (y == 224)
-#endif
-    if (PsndOut)
-    {
-      if (ym2612.dacen && PsndDacLine <= y)
-        PsndDoDAC(y);
-      getSamples(y);
-    }
+  if (y == 224 && PsndOut)
+  {
+    if (ym2612.dacen && PsndDacLine <= y)
+      PsndDoDAC(y);
+    PsndGetSamples(y);
+  }
 
   // Run scanline:
   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());

+ 1 - 1
pico/pico_int.h

@@ -531,8 +531,8 @@ PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba);
 // sound/sound.c
 PICO_INTERNAL void PsndReset(void);
 PICO_INTERNAL void PsndDoDAC(int line_to);
-PICO_INTERNAL int  PsndRender(int offset, int length);
 PICO_INTERNAL void PsndClear(void);
+PICO_INTERNAL void PsndGetSamples(int y);
 // z80 functionality wrappers
 PICO_INTERNAL void z80_init(void);
 PICO_INTERNAL void z80_pack(unsigned char *data);

+ 30 - 1
pico/sound/sound.c

@@ -297,7 +297,7 @@ PICO_INTERNAL void PsndClear(void)
 }
 
 
-PICO_INTERNAL int PsndRender(int offset, int length)
+static int PsndRender(int offset, int length)
 {
   int  buf32_updated = 0;
   int *buf32 = PsndBuffer+offset;
@@ -361,6 +361,35 @@ PICO_INTERNAL int PsndRender(int offset, int length)
   return length;
 }
 
+// to be called on 224 or line_sample scanlines only
+PICO_INTERNAL void PsndGetSamples(int y)
+{
+#if SIMPLE_WRITE_SOUND
+  if (y != 224) return;
+  PsndRender(0, PsndLen);
+  if (PicoWriteSound) PicoWriteSound(PsndLen);
+  PsndClear();
+#else
+  static int curr_pos = 0;
+
+  if (y == 224)
+  {
+    if (emustatus & 2)
+         curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);
+    else curr_pos  = PsndRender(0, PsndLen);
+    if (emustatus&1) emustatus|=2; else emustatus&=~2;
+    if (PicoWriteSound) PicoWriteSound(curr_pos);
+    // clear sound buffer
+    PsndClear();
+  }
+  else if (emustatus & 3) {
+    emustatus|= 2;
+    emustatus&=~1;
+    curr_pos = PsndRender(0, PsndLen/2);
+  }
+#endif
+}
+
 
 // -----------------------------------------------------------------
 //                            z80 stuff

+ 5 - 1
platform/common/emu.h

@@ -69,7 +69,6 @@ void  emu_textOut16(int x, int y, const char *text);
 char *emu_makeRomId(void);
 void  emu_getGameName(char *str150);
 void  emu_findKeyBindCombos(void);
-void  emu_forcedFrame(int opts);
 void  emu_changeFastForward(int set_on);
 void  emu_RunEventsPico(unsigned int events);
 void  emu_DoTurbo(int *pad, int acts);
@@ -77,9 +76,14 @@ void  emu_packConfig(void);
 void  emu_unpackConfig(void);
 void  emu_shutdownMCD(void);
 
+/* not in common */
 extern const char * const keyNames[];
 void  emu_prepareDefaultConfig(void);
 void  emu_platformDebugCat(char *str);
+void  emu_forcedFrame(int opts);
+void  emu_startSound(void);
+void  emu_endSound(void);
+void  emu_waitSound(void);
 
 #ifdef __cplusplus
 } // extern "C"

+ 19 - 1
platform/common/menu.c

@@ -343,6 +343,20 @@ int me_process(menu_entry *entries, int count, menu_id id, int is_next)
 
 void SekStepM68k(void);
 
+static void mplayer_loop(void)
+{
+	emu_startSound();
+
+	while (1)
+	{
+		PDebugZ80Frame();
+		if (read_buttons_async(BTN_NORTH)) break;
+		emu_waitSound();
+	}
+
+	emu_endSound();
+}
+
 static void draw_text_debug(const char *str, int skip, int from)
 {
 	const char *p;
@@ -415,7 +429,7 @@ void debug_menu_loop(void)
 		}
 		menu_draw_end();
 
-		inp = read_buttons(BTN_EAST|BTN_SOUTH|BTN_WEST|BTN_L|BTN_R|BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT);
+		inp = read_buttons(BTN_EAST|BTN_SOUTH|BTN_WEST|BTN_NORTH|BTN_L|BTN_R|BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT);
 		if (inp & BTN_SOUTH) return;
 		if (inp & BTN_L) { mode--; if (mode < 0) mode = 3; }
 		if (inp & BTN_R) { mode++; if (mode > 3) mode = 0; }
@@ -423,6 +437,10 @@ void debug_menu_loop(void)
 		{
 			case 0:
 				if (inp & BTN_EAST) SekStepM68k();
+				if (inp & BTN_NORTH) {
+					while (inp & BTN_NORTH) inp = read_buttons_async(BTN_NORTH);
+					mplayer_loop();
+				}
 				if ((inp & (BTN_WEST|BTN_LEFT)) == (BTN_WEST|BTN_LEFT)) {
 					mkdir("dumps", 0777);
 					PDebugDumpMem();

+ 42 - 25
platform/gp2x/emu.c

@@ -665,6 +665,46 @@ static void updateSound(int len)
 		gp2x_sound_write(PsndOut, len<<1);
 }
 
+void emu_startSound(void)
+{
+	static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
+	int target_fps = Pico.m.pal ? 50 : 60;
+
+	PsndOut = NULL;
+
+	// prepare sound stuff
+	if (currentConfig.EmuOpt & 4)
+	{
+		int snd_excess_add;
+		if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
+				((PicoOpt&0x200) && crashed_940)) {
+			PsndRerate(Pico.m.frame_count ? 1 : 0);
+		}
+		snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
+		printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
+			PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
+		gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
+		gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
+		PicoWriteSound = updateSound;
+		update_volume(0, 0);
+		memset(sndBuffer, 0, sizeof(sndBuffer));
+		PsndOut = sndBuffer;
+		PsndRate_old = PsndRate;
+		PicoOpt_old  = PicoOpt;
+		pal_old = Pico.m.pal;
+	}
+}
+
+void emu_endSound(void)
+{
+}
+
+/* wait until we can write more sound */
+void emu_waitSound(void)
+{
+	// don't need to do anything, writes will block by themselves
+}
+
 
 static void SkipFrame(int do_audio)
 {
@@ -776,8 +816,7 @@ static void tga_dump(void)
 
 void emu_Loop(void)
 {
-	static int gp2x_old_clock = 200;
-	static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;
+	static int gp2x_old_clock = 200, EmuOpt_old = 0;
 	char fpsbuff[24]; // fps count c string
 	struct timeval tval; // timing
 	int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
@@ -821,29 +860,7 @@ void emu_Loop(void)
 	target_frametime = 1000000/target_fps;
 	reset_timing = 1;
 
-	// prepare sound stuff
-	if (currentConfig.EmuOpt & 4)
-	{
-		int snd_excess_add;
-		if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
-				((PicoOpt&0x200) && crashed_940)) {
-			PsndRerate(Pico.m.frame_count ? 1 : 0);
-		}
-		snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
-		printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
-			PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
-		gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
-		gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
-		PicoWriteSound = updateSound;
-		update_volume(0, 0);
-		memset(sndBuffer, 0, sizeof(sndBuffer));
-		PsndOut = sndBuffer;
-		PsndRate_old = PsndRate;
-		PicoOpt_old  = PicoOpt;
-		pal_old = Pico.m.pal;
-	} else {
-		PsndOut = NULL;
-	}
+	emu_startSound();
 
 	// prepare CD buffer
 	if (PicoAHW & PAHW_MCD) PicoCDBufferInit();

+ 1 - 0
platform/gp2x/gp2x.h

@@ -24,6 +24,7 @@ void gp2x_pd_clone_buffer2(void);
 void gp2x_start_sound(int rate, int bits, int stereo);
 void gp2x_sound_write(void *buff, int len);
 void gp2x_sound_volume(int l, int r);
+void gp2x_sound_sync(void);
 
 /* input */
 unsigned long gp2x_joystick_read(int allow_usb_joy);

+ 12 - 4
platform/psp/emu.c

@@ -609,7 +609,7 @@ static void sound_init(void)
 		lprintf("sceKernelCreateThread failed: %i\n", thid);
 }
 
-static void sound_prepare(void)
+void emu_startSound(void)
 {
 	static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
 	int ret, stereo;
@@ -648,7 +648,7 @@ static void sound_prepare(void)
 	}
 }
 
-static void sound_end(void)
+void emu_endSound(void)
 {
 	int i;
 	if (samples_done == 0)
@@ -667,6 +667,14 @@ static void sound_end(void)
 	sceAudio_5C37C0AE();
 }
 
+/* wait until we can write more sound */
+void emu_waitSound(void)
+{
+	// TODO: test this
+	while (!sound_thread_exit && samples_made - samples_done > samples_block * 4)
+		psp_msleep(10);
+}
+
 static void sound_deinit(void)
 {
 	sound_thread_exit = 1;
@@ -944,7 +952,7 @@ void emu_Loop(void)
 	PsndOut = NULL;
 	if (currentConfig.EmuOpt & 4)
 	{
-		sound_prepare();
+		emu_startSound();
 	}
 
 	sceDisplayWaitVblankStart();
@@ -1090,8 +1098,8 @@ void emu_Loop(void)
 	if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
 
 	if (PsndOut != NULL) {
+		emu_endSound();
 		PsndOut = NULL;
-		sound_end();
 	}
 
 	// save SRAM