giz.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2007
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include "kgsdk/Framework.h"
  11. #include "kgsdk/Framework2D.h"
  12. #include "giz.h"
  13. #include "version.h"
  14. #define LOG_FILE "log.log"
  15. void *giz_screen = NULL;
  16. static FILE *logf = NULL;
  17. #if 0
  18. static int directfb_init(void);
  19. static void directfb_fini(void);
  20. #endif
  21. void lprintf(const char *fmt, ...)
  22. {
  23. va_list vl;
  24. if (logf == NULL)
  25. {
  26. logf = fopen(LOG_FILE, "r+");
  27. //logf = fopen(LOG_FILE, "a");
  28. if (logf == NULL)
  29. return;
  30. }
  31. fseek(logf, 0, SEEK_END);
  32. //if (strchr(fmt, '\n'))
  33. // fprintf(logf, "%lu: ", GetTickCount());
  34. va_start(vl, fmt);
  35. vfprintf(logf, fmt, vl);
  36. va_end(vl);
  37. fflush(logf);
  38. }
  39. static void giz_log_close(void)
  40. {
  41. if (logf != NULL)
  42. {
  43. fclose(logf);
  44. logf = NULL;
  45. }
  46. }
  47. void giz_init(HINSTANCE hInstance, HINSTANCE hPrevInstance)
  48. {
  49. int ret;
  50. lprintf("\n\nPicoDrive v" VERSION " (c) notaz, 2006-2008\n");
  51. lprintf("%s %s\n\n", __DATE__, __TIME__);
  52. ret = Framework_Init(hInstance, hPrevInstance);
  53. if (!ret)
  54. {
  55. lprintf("Framework_Init() failed\n");
  56. exit(1);
  57. }
  58. ret = Framework2D_Init();
  59. if (!ret)
  60. {
  61. lprintf("Framework2D_Init() failed\n");
  62. exit(1);
  63. }
  64. #if 0
  65. ret = directfb_init();
  66. if (ret != 0)
  67. {
  68. lprintf("directfb_init() failed\n");
  69. }
  70. #endif
  71. // test screen
  72. giz_screen = fb_lock(1);
  73. if (giz_screen == NULL)
  74. {
  75. lprintf("fb_lock() failed\n");
  76. exit(1);
  77. }
  78. lprintf("fb_lock() returned %p\n", giz_screen);
  79. fb_unlock();
  80. giz_screen = NULL;
  81. }
  82. void giz_deinit(void)
  83. {
  84. Framework2D_Close();
  85. Framework_Close();
  86. #if 0
  87. directfb_fini();
  88. #endif
  89. giz_log_close();
  90. }
  91. #define PAGE_SIZE 0x1000
  92. #define CACHE_SYNC_INSTRUCTIONS 0x002 /* discard all cached instructions */
  93. #define CACHE_SYNC_WRITEBACK 0x004 /* write back but don't discard data cache*/
  94. WINBASEAPI BOOL WINAPI CacheRangeFlush(LPVOID pAddr, DWORD dwLength, DWORD dwFlags);
  95. WINBASEAPI BOOL WINAPI VirtualCopy(LPVOID lpvDest, LPVOID lpvSrc, DWORD cbSize, DWORD fdwProtect);
  96. void cache_flush_d_inval_i(void *start_addr, void *end_addr)
  97. {
  98. int size = end_addr - start_addr;
  99. CacheRangeFlush(start_addr, size, CACHE_SYNC_WRITEBACK);
  100. CacheRangeFlush(start_addr, size, CACHE_SYNC_INSTRUCTIONS);
  101. }
  102. #if 0
  103. static void *mmap_phys(unsigned int addr, int pages)
  104. {
  105. void *mem;
  106. int ret;
  107. mem = VirtualAlloc(0, pages*PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS);
  108. if (mem == NULL)
  109. {
  110. lprintf("VirtualAlloc failed\n");
  111. return NULL;
  112. }
  113. ret = VirtualCopy(mem, (void *)addr, pages*PAGE_SIZE, PAGE_READWRITE | PAGE_NOCACHE);
  114. if (ret == 0)
  115. {
  116. lprintf("VirtualFree failed\n");
  117. VirtualFree(mem, 0, MEM_RELEASE);
  118. return NULL;
  119. }
  120. return mem;
  121. }
  122. static void munmap_phys(void *ptr)
  123. {
  124. VirtualFree(ptr, 0, MEM_RELEASE);
  125. }
  126. // FB
  127. static int directfb_initialized = 0;
  128. static int directfb_addrs[2] = { 0, 0 };
  129. static void *directfb_ptrs[2] = { NULL, NULL };
  130. static int directfb_sel = 0; // the one currently displayed
  131. static volatile unsigned int *memregs = NULL;
  132. /*static void xdump(void)
  133. {
  134. int i;
  135. for (i = 0; i < 0x1000/4; i += 4)
  136. {
  137. lprintf("%04x: %08x %08x %08x %08x\n", i*4, memregs[i],
  138. memregs[i+1], memregs[i+2], memregs[i+3]);
  139. }
  140. }*/
  141. static int directfb_init(void)
  142. {
  143. memregs = mmap_phys(0xac009000, 1);
  144. if (memregs == NULL)
  145. {
  146. lprintf("can't access hw regs\n");
  147. return -1;
  148. }
  149. // fake lock
  150. Framework2D_LockBuffer(1);
  151. // 0xAC00905C
  152. directfb_addrs[0] = memregs[0x5c>>2];
  153. lprintf("fb0 is at %08x\n", directfb_addrs[0]);
  154. Framework2D_UnlockBuffer();
  155. directfb_ptrs[0] = mmap_phys(directfb_addrs[0], (321*240*2+PAGE_SIZE-1) / PAGE_SIZE);
  156. if (directfb_ptrs[0] == NULL)
  157. {
  158. lprintf("failed to map fb0\n");
  159. goto fail0;
  160. }
  161. // use directx to discover other buffer
  162. xdump();
  163. Framework2D_Flip();
  164. lprintf("---\n");
  165. xdump();
  166. exit(1);
  167. //Framework2D_LockBuffer(1);
  168. directfb_addrs[1] = memregs[0x5c>>2] + 0x30000;
  169. lprintf("fb1 is at %08x\n", directfb_addrs[1]);
  170. //Framework2D_UnlockBuffer();
  171. directfb_ptrs[1] = mmap_phys(directfb_addrs[1], (321*240*2+PAGE_SIZE-1) / PAGE_SIZE);
  172. if (directfb_ptrs[1] == NULL)
  173. {
  174. lprintf("failed to map fb1\n");
  175. goto fail1;
  176. }
  177. directfb_initialized = 1;
  178. directfb_sel = 1;
  179. return 0;
  180. fail1:
  181. munmap_phys(directfb_ptrs[0]);
  182. fail0:
  183. munmap_phys((void *)memregs);
  184. return -1;
  185. }
  186. static void directfb_fini(void)
  187. {
  188. if (!directfb_initialized) return;
  189. munmap_phys(directfb_ptrs[0]);
  190. munmap_phys(directfb_ptrs[1]);
  191. munmap_phys((void *)memregs);
  192. }
  193. void *directfb_lock(int is_front)
  194. {
  195. int which;
  196. if (!directfb_initialized)
  197. // fall back to directx
  198. return Framework2D_LockBuffer(is_front);
  199. if (is_front)
  200. which = directfb_sel;
  201. else
  202. which = directfb_sel ^ 1; // return backbuffer when possible
  203. return directfb_ptrs[which];
  204. }
  205. void directfb_unlock(void)
  206. {
  207. if (!directfb_initialized)
  208. // fall back to directx
  209. Framework2D_UnlockBuffer();
  210. }
  211. void directfb_flip(void)
  212. {
  213. if (!directfb_initialized) {
  214. Framework2D_Flip();
  215. return;
  216. }
  217. directfb_sel ^= 1;
  218. // doesn't work
  219. memregs[0x5c>>2] = directfb_addrs[directfb_sel];
  220. }
  221. #endif