Browse Source

minor psp fixes

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@297 be3aeb3a-fb24-0410-a615-afba39da0efa
notaz 16 years ago
parent
commit
110df09c8a

+ 1 - 1
cpu/fame/famec.c

@@ -18,7 +18,7 @@
 #define FAMEC_ROLL_INLINE
 //#define FAMEC_EMULATE_TRACE
 //#define FAMEC_CHECK_BRANCHES
-#define FAMEC_EXTRA_INLINE
+//#define FAMEC_EXTRA_INLINE
 // #define FAMEC_DEBUG
 //#define FAMEC_NO_GOTOS
 #define FAMEC_ADR_BITS  24

+ 1 - 1
platform/base_readme.txt

@@ -542,7 +542,7 @@ Problems / limitations
 Credits
 -------
 
-This emulator uses code from these people/projects:
+This emulator is made of the code from following people/projects:
 
 notaz
 GP2X, UIQ, PSP, Gizmondo ports, CPU core hacks,

+ 2 - 7
platform/common/lprintf.h

@@ -2,13 +2,8 @@
  #include <stdio.h>
  #define lprintf printf
 #elif defined(PSP)
- #ifdef LPRINTF_STDIO
-  #include <stdio.h>
-  #define lprintf printf
- #else
-  extern void lprintf_f(const char *fmt, ...);
-  #define lprintf lprintf_f
- #endif
+ extern void lprintf_f(const char *fmt, ...);
+ #define lprintf lprintf_f
 #else
  #include "giz.h"
 #endif

+ 7 - 3
platform/psp/Makefile

@@ -8,9 +8,9 @@ PSPSDK = $(shell psp-config --pspsdk-path)
 amalgamate = 0
 
 
-CFLAGS += -I../.. -I. -DNO_SYNC -DLPRINTF_STDIO
+CFLAGS += -I../.. -I. -DNO_SYNC
 CFLAGS += -Wall -Winline -G0
-CFLAGS += -DLPRINTF_STDIO
+#CFLAGS += -DLPRINTF_STDIO
 #CFLAGS += -fprofile-generate
 #CFLAGS += -fprofile-use
 #CFLAGS += -pg
@@ -83,6 +83,7 @@ EXTRA_TARGETS = EBOOT.PBP
 PSP_EBOOT_TITLE = PicoDrive
 PSP_EBOOT_ICON = data/icon.png
 #PSP_EBOOT_PIC1 = .png
+BUILD_PRX = 1
 
 CUSTOM_CLEAN = myclean
 
@@ -131,8 +132,11 @@ data/bg40.o: data/bg40.bin
 	bin2o -i $< $@ bgdatac40
 
 #
+ifndef UPDIR
+UPDIR = /media/disk/PSP/GAME/PicoDrive/
+endif
 up: EBOOT.PBP
-	@cp -v $^ /media/disk/PSP/GAME/PicoDrive/
+	@cp -v $^ $(UPDIR)
 
 
 # cleanup

+ 18 - 6
platform/psp/emu.c

@@ -508,7 +508,7 @@ static int sound_thread(SceSize args, void *argp)
 	{
 		if (samples_made - samples_done < samples_block) {
 			// wait for data (use at least 2 blocks)
-			lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
+			//lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
 			while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
 				ret = sceKernelWaitSema(sound_sem, 1, 0);
 			if (ret < 0) lprintf("sthr: sceKernelWaitSema: %i\n", ret);
@@ -523,8 +523,9 @@ static int sound_thread(SceSize args, void *argp)
 		snd_playptr  += samples_block;
 		if (snd_playptr >= sndBuffer_endptr)
 			snd_playptr = sndBuffer;
-		if (ret)
-			lprintf("sthr: outf: %i; pos %i/%i\n", ret, samples_done, samples_made);
+		// 1.5 kernel returns 0, newer ones return # of samples queued
+		if (ret < 0)
+			lprintf("sthr: sceAudio_E0727056: %08x; pos %i/%i\n", ret, samples_done, samples_made);
 
 		// shouln't happen, but just in case
 		if (samples_made - samples_done >= samples_block*3) {
@@ -543,6 +544,7 @@ static int sound_thread(SceSize args, void *argp)
 static void sound_init(void)
 {
 	SceUID thid;
+	int ret;
 
 	sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
 	if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
@@ -553,7 +555,8 @@ static void sound_init(void)
 	thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
 	if (thid >= 0)
 	{
-		sceKernelStartThread(thid, 0, 0);
+		ret = sceKernelStartThread(thid, 0, 0);
+		if (ret < 0) lprintf("sound_init: sceKernelStartThread returned %08x\n", ret);
 	}
 	else
 		lprintf("sceKernelCreateThread failed: %i\n", thid);
@@ -865,6 +868,7 @@ static void simpleWait(unsigned int until)
 
 void emu_Loop(void)
 {
+	static int mp3_init_done = 0;
 	char fpsbuff[24]; // fps count c string
 	unsigned int tval, tval_prev = 0, tval_thissec = 0; // timing
 	int frames_done = 0, frames_shown = 0, oldmodes = 0;
@@ -894,8 +898,16 @@ void emu_Loop(void)
 	target_frametime = Pico.m.pal ? (1000000<<8)/50 : (1000000<<8)/60+1;
 	reset_timing = 1;
 
-	// prepare CD buffer
-	if (PicoMCD & 1) PicoCDBufferInit();
+	if (PicoMCD & 1) {
+		// prepare CD buffer
+		PicoCDBufferInit();
+		// mp3...
+		if (!mp3_init_done) {
+			i = mp3_init();
+			mp3_init_done = 1;
+			if (i) { engineState = PGS_Menu; return; }
+		}
+	}
 
 	// prepare sound stuff
 	PsndOut = NULL;

+ 36 - 5
platform/psp/main.c

@@ -1,3 +1,4 @@
+#include <string.h>
 #include "psp.h"
 #include "emu.h"
 #include "menu.h"
@@ -7,17 +8,34 @@
 #include "../common/lprintf.h"
 #include "version.h"
 
-int main()
+#define GPROF 0
+#define GCOV 0
+
+#if GPROF
+#include <pspprof.h>
+#endif
+
+#if GCOV
+#include <stdio.h>
+#include <stdlib.h>
+
+void dummy(void)
 {
-	int mp3_ret;
+	engineState = atoi(romFileName);
+	setbuf(NULL, NULL);
+	getenv(NULL);
+}
+#endif
 
+int main()
+{
 	lprintf("\nPicoDrive v" VERSION " " __DATE__ " " __TIME__ "\n");
 	psp_init();
-
 	emu_ReadConfig(0, 0);
 	emu_Init();
 	menu_init();
-	mp3_ret = mp3_init();
+	// moved to emu_Loop(), after CPU clock change..
+	//mp3_init();
 
 	engineState = PGS_Menu;
 
@@ -26,7 +44,12 @@ int main()
 		switch (engineState)
 		{
 			case PGS_Menu:
+#if !GPROF
 				menu_loop();
+#else
+				strcpy(romFileName, currentConfig.lastRomFile);
+				engineState = PGS_ReloadRom;
+#endif
 				break;
 
 			case PGS_ReloadRom:
@@ -45,6 +68,9 @@ int main()
 
 			case PGS_Running:
 				emu_Loop();
+#if GPROF
+				goto endloop;
+#endif
 				break;
 
 			case PGS_Quit:
@@ -58,9 +84,14 @@ int main()
 
 	endloop:
 
-	if (mp3_ret == 0) mp3_deinit();
+	mp3_deinit();
 	emu_Deinit();
+#if GPROF
+	gprof_cleanup();
+#endif
+#if !GCOV
 	psp_finish();
+#endif
 
 	return 0;
 }

+ 3 - 2
platform/psp/menu.c

@@ -941,7 +941,7 @@ static void cd_menu_loop_options(void)
 	for (;;)
 	{
 		draw_cd_menu_options(menu_sel, &bios_names);
-		inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE, 0);
+		inp = wait_for_input(BTN_UP|BTN_DOWN|BTN_LEFT|BTN_RIGHT|BTN_X|BTN_CIRCLE|BTN_START, 0);
 		if (inp & BTN_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
 		if (inp & BTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
 		selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
@@ -958,11 +958,12 @@ static void cd_menu_loop_options(void)
 				}
 			}
 		}
-		if (inp & BTN_CIRCLE) { // toggleable options
+		if (inp & BTN_CIRCLE) // toggleable options
 			if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
 			    selected_id == MA_CDOPT_DONE) {
 				return;
 			}
+		if (inp & BTN_START) {
 			switch (selected_id) { // BIOS testers
 				case MA_CDOPT_TESTBIOS_USA:
 					if (emu_findBios(4, &bios)) { // test US

+ 23 - 7
platform/psp/mp3.c

@@ -11,6 +11,7 @@
 
 int mp3_last_error = 0;
 
+static int initialized = 0;
 static SceUID thread_job_sem = -1;
 static SceUID thread_busy_sem = -1;
 static int thread_exit = 0;
@@ -96,9 +97,14 @@ static int read_next_frame(int which_buffer)
 			continue; // bad frame
 		}
 
-		if (bytes_read - frame_offset < frame_size) {
+		if (bytes_read - frame_offset < frame_size)
+		{
 			lprintf("unfit, foffs=%i\n", mp3_src_pos - bytes_read);
 			mp3_src_pos -= bytes_read - frame_offset;
+			if (mp3_src_size - mp3_src_pos < frame_size) {
+				mp3_src_pos = mp3_src_size;
+				return 0; // EOF
+			}
 			sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
 			continue; // didn't fit, re-read..
 		}
@@ -195,6 +201,7 @@ int mp3_init(void)
 	}
 
 	mp3_last_error = 0;
+	initialized = 1;
 	return 0;
 
 fail3:
@@ -207,12 +214,15 @@ fail1:
 	sceAudiocodecReleaseEDRAM(mp3_codec_struct);
 fail:
 	mp3_last_error = ret;
+	initialized = 0;
 	return 1;
 }
 
 void mp3_deinit(void)
 {
-	lprintf("deinit\n");
+	lprintf("mp3_deinit, initialized=%i\n", initialized);
+
+	if (!initialized) return;
 	thread_exit = 1;
 	psp_sem_lock(thread_busy_sem);
 	psp_sem_unlock(thread_busy_sem);
@@ -229,6 +239,7 @@ void mp3_deinit(void)
 	sceKernelDeleteSema(thread_job_sem);
 	thread_job_sem = -1;
 	sceAudiocodecReleaseEDRAM(mp3_codec_struct);
+	initialized = 0;
 }
 
 // may overflow stack?
@@ -269,6 +280,7 @@ static int decode_thread(SceSize args, void *argp)
 }
 
 
+// might be called before initialization
 int mp3_get_bitrate(FILE *f, int size)
 {
 	int ret, retval = -1, sample_rate, bitrate;
@@ -276,7 +288,8 @@ int mp3_get_bitrate(FILE *f, int size)
 	char *fname = (char *)f;
 
 	/* make sure thread is not busy.. */
-	psp_sem_lock(thread_busy_sem);
+	if (thread_busy_sem >= 0)
+		psp_sem_lock(thread_busy_sem);
 
 	if (mp3_handle >= 0) sceIoClose(mp3_handle);
 	mp3_handle = sceIoOpen(fname, PSP_O_RDONLY, 0777);
@@ -288,19 +301,19 @@ int mp3_get_bitrate(FILE *f, int size)
 	mp3_src_pos = 0;
 	ret = read_next_frame(0);
 	if (ret <= 0) {
-		lprintf("read_next_frame() failed\n");
+		lprintf("read_next_frame() failed (%s)\n", fname);
 		goto end;
 	}
 	sample_rate = (mp3_src_buffer[0][2] & 0x0c) >> 2;
 	bitrate = mp3_src_buffer[0][2] >> 4;
 
 	if (sample_rate != 0) {
-		lprintf("unsupported samplerate\n");
+		lprintf("unsupported samplerate (%s)\n", fname);
 		goto end; // only 44kHz supported..
 	}
 	bitrate = bitrates[bitrate];
 	if (bitrate == 0) {
-		lprintf("unsupported bitrate\n");
+		lprintf("unsupported bitrate (%s)\n", fname);
 		goto end;
 	}
 
@@ -310,7 +323,8 @@ end:
 	if (mp3_handle >= 0) sceIoClose(mp3_handle);
 	mp3_handle = -1;
 	mp3_fname = NULL;
-	psp_sem_unlock(thread_busy_sem);
+	if (thread_busy_sem >= 0)
+		psp_sem_unlock(thread_busy_sem);
 	if (retval < 0) mp3_last_error = -1; // remember we had a problem..
 	return retval;
 }
@@ -322,6 +336,8 @@ void mp3_start_play(FILE *f, int pos)
 {
 	char *fname = (char *)f;
 
+	if (!initialized) return;
+
 	lprintf("mp3_start_play(%s) @ %i\n", fname, pos);
 	psp_sem_lock(thread_busy_sem);
 

+ 58 - 12
platform/psp/psp.c

@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
 
@@ -14,12 +15,15 @@
 #include "../common/lprintf.h"
 
 PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
+PSP_HEAP_SIZE_MAX();
 
 unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
 
 void *psp_screen = VRAM_FB0;
 static int current_screen = 0; /* front bufer */
 
+static SceUID main_thread_id = -1;
+
 #define ANALOG_DEADZONE 80
 
 /* Exit callback */
@@ -64,8 +68,10 @@ void psp_init(void)
 {
 	SceUID thid;
 
-	lprintf("running in %08x kernel\n", sceKernelDevkitVersion()),
-	lprintf("entered psp_init, threadId %08x, priority %i\n", sceKernelGetThreadId(),
+	main_thread_id = sceKernelGetThreadId();
+
+	lprintf("running on %08x kernel\n", sceKernelDevkitVersion()),
+	lprintf("entered psp_init, threadId %08x, priority %i\n", main_thread_id,
 		sceKernelGetThreadCurrentPriority());
 
 	thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
@@ -198,35 +204,75 @@ char *psp_get_status_line(void)
 }
 
 /* alt logging */
-#define LOG_FILE "log.log"
+#define LOG_FILE "log.txt"
+
+typedef struct _log_entry
+{
+	char buff[256];
+	struct _log_entry *next;
+} log_entry;
 
-static SceUID logfd = -1;
+static log_entry *le_root = NULL;
 
 void lprintf_f(const char *fmt, ...)
 {
 	va_list vl;
+
+#ifdef LPRINTF_STDIO
+	va_start(vl, fmt);
+	vprintf(fmt, vl);
+	va_end(vl);
+#else
+	static SceUID logfd = -1;
 	char buff[256];
+	log_entry *le, *le1;
 
 	if (logfd == -2) return; // disabled
 
-	if (logfd < 0)
+	va_start(vl, fmt);
+	vsnprintf(buff, sizeof(buff), fmt, vl);
+	va_end(vl);
+
+	// note: this is still unsafe code
+	if (main_thread_id != sceKernelGetThreadId())
 	{
-		logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
-		if (logfd < 0) {
-			logfd = -2;
-			return;
+		le = malloc(sizeof(*le));
+		if (le == NULL) return;
+		le->next = NULL;
+		strcpy(le->buff, buff);
+		if (le_root == NULL) le_root = le;
+		else {
+			for (le1 = le_root; le1->next != NULL; le1 = le1->next);
+			le1->next = le;
 		}
+		return;
 	}
 
-	va_start(vl, fmt);
-	vsnprintf(buff, sizeof(buff), fmt, vl);
-	va_end(vl);
+	logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
+	if (logfd < 0) {
+		logfd = -2;
+		return;
+	}
+
+	if (le_root != NULL)
+	{
+		le1 = le_root;
+		le_root = NULL;
+		sceKernelDelayThread(1000);
+		while (le1 != NULL) {
+			le = le1;
+			le1 = le->next;
+			sceIoWrite(logfd, le->buff, strlen(le->buff));
+			free(le);
+		}
+	}
 
 	sceIoWrite(logfd, buff, strlen(buff));
 
 	// make sure it gets flushed
 	sceIoClose(logfd);
 	logfd = -1;
+#endif
 }
 
 

+ 3 - 2
tools/gcda.c

@@ -35,6 +35,7 @@ static int is_good_path(char *path)
 		fclose(f);
 		return 1;
 	}
+	printf("not good path: %s\n", path);
 	return 0;
 }
 
@@ -68,7 +69,7 @@ readnext:
 		{
 			pos1 = search_gcda(buff + pos, l - pos);
 			if (pos1 < 0) {
-				fseek(f, -5, SEEK_CUR);
+				fseek(f, -6, SEEK_CUR);
 				goto readnext;
 			}
 			pos += pos1;
@@ -76,7 +77,7 @@ readnext:
 			while (pos > 0 && is_good_char(buff[pos-1])) pos--;
 
 			if (pos == 0) {
-				fseek(f, -16, SEEK_CUR);
+				fseek(f, -(sizeof(buff) + 16), SEEK_CUR);
 				goto readnext;
 			}