psp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <string.h>
  4. #include <pspkernel.h>
  5. #include <pspiofilemgr.h>
  6. #include <pspdisplay.h>
  7. #include <psppower.h>
  8. #include <pspgu.h>
  9. #include "psp.h"
  10. #include "../common/lprintf.h"
  11. PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
  12. unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
  13. void *psp_screen = VRAM_FB0;
  14. static int current_screen = 0; /* front bufer */
  15. static SceUID logfd = -1;
  16. /* Exit callback */
  17. static int exit_callback(int arg1, int arg2, void *common)
  18. {
  19. sceKernelExitGame();
  20. return 0;
  21. }
  22. /* Callback thread */
  23. static int callback_thread(SceSize args, void *argp)
  24. {
  25. int cbid;
  26. lprintf("callback_thread started with id %i\n", sceKernelGetThreadId());
  27. cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  28. sceKernelRegisterExitCallback(cbid);
  29. sceKernelSleepThreadCB();
  30. return 0;
  31. }
  32. void psp_init(void)
  33. {
  34. int thid;
  35. lprintf("entered psp_init, threadId %i\n", sceKernelGetThreadId());
  36. thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
  37. if (thid >= 0)
  38. {
  39. sceKernelStartThread(thid, 0, 0);
  40. }
  41. /* video */
  42. sceDisplaySetMode(0, 480, 272);
  43. sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
  44. current_screen = 1;
  45. psp_screen = VRAM_FB0;
  46. /* gu */
  47. sceGuInit();
  48. sceGuStart(GU_DIRECT, guCmdList);
  49. sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
  50. sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
  51. sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
  52. sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
  53. sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2));
  54. sceGuViewport(2048, 2048, 480, 272);
  55. sceGuDepthRange(0xc350, 0x2710);
  56. sceGuScissor(0, 0, 480, 272);
  57. sceGuEnable(GU_SCISSOR_TEST);
  58. sceGuDepthMask(0xffff);
  59. sceGuDisable(GU_DEPTH_TEST);
  60. sceGuFrontFace(GU_CW);
  61. sceGuEnable(GU_TEXTURE_2D);
  62. sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
  63. sceGuAmbientColor(0xffffffff);
  64. sceGuColor(0xffffffff);
  65. sceGuFinish();
  66. sceGuSync(0, 0);
  67. sceDisplayWaitVblankStart();
  68. sceGuDisplay(GU_TRUE);
  69. /* input */
  70. sceCtrlSetSamplingCycle(0);
  71. sceCtrlSetSamplingMode(0);
  72. }
  73. void psp_finish(void)
  74. {
  75. sceGuTerm();
  76. //sceKernelSleepThread();
  77. sceKernelExitGame();
  78. }
  79. void psp_video_flip(int wait_vsync)
  80. {
  81. if (wait_vsync) sceDisplayWaitVblankStart();
  82. sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
  83. wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
  84. current_screen ^= 1;
  85. psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
  86. }
  87. void *psp_video_get_active_fb(void)
  88. {
  89. return current_screen ? VRAM_FB1 : VRAM_FB0;
  90. }
  91. void psp_video_switch_to_single(void)
  92. {
  93. psp_screen = VRAM_FB0;
  94. sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
  95. current_screen = 0;
  96. }
  97. void psp_msleep(int ms)
  98. {
  99. sceKernelDelayThread(ms * 1000);
  100. }
  101. unsigned int psp_pad_read(int blocking)
  102. {
  103. SceCtrlData pad;
  104. if (blocking)
  105. sceCtrlReadBufferPositive(&pad, 1);
  106. else sceCtrlPeekBufferPositive(&pad, 1);
  107. return pad.Buttons;
  108. }
  109. int psp_get_cpu_clock(void)
  110. {
  111. return scePowerGetCpuClockFrequencyInt();
  112. }
  113. int psp_set_cpu_clock(int clock)
  114. {
  115. int ret = scePowerSetClockFrequency(clock, clock, clock/2);
  116. if (ret != 0) lprintf("failed to set clock: %i\n", ret);
  117. return ret;
  118. }
  119. /* alt logging */
  120. #define LOG_FILE "log.log"
  121. void lprintf_f(const char *fmt, ...)
  122. {
  123. va_list vl;
  124. char buff[256];
  125. if (logfd < 0)
  126. {
  127. logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
  128. if (logfd < 0)
  129. return;
  130. }
  131. va_start(vl, fmt);
  132. vsnprintf(buff, sizeof(buff), fmt, vl);
  133. va_end(vl);
  134. sceIoWrite(logfd, buff, strlen(buff));
  135. //sceKernelDelayThread(200 * 1000);
  136. sceIoClose(logfd);
  137. logfd = -1;
  138. }