5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
11 #include "kgsdk/Framework.h"
12 #include "kgsdk/Framework2D.h"
16 #define LOG_FILE "log.log"
18 void *giz_screen = NULL;
19 static FILE *logf = NULL;
22 static int directfb_init(void);
23 static void directfb_fini(void);
26 void lprintf(const char *fmt, ...)
32 logf = fopen(LOG_FILE, "r+");
33 //logf = fopen(LOG_FILE, "a");
37 fseek(logf, 0, SEEK_END);
39 //if (strchr(fmt, '\n'))
40 // fprintf(logf, "%lu: ", GetTickCount());
42 vfprintf(logf, fmt, vl);
47 static void giz_log_close(void)
56 void giz_init(HINSTANCE hInstance, HINSTANCE hPrevInstance)
60 lprintf("\n\nPicoDrive v" VERSION " (c) notaz, 2006-2008\n");
61 lprintf("%s %s\n\n", __DATE__, __TIME__);
63 ret = Framework_Init(hInstance, hPrevInstance);
66 lprintf("Framework_Init() failed\n");
69 ret = Framework2D_Init();
72 lprintf("Framework2D_Init() failed\n");
76 ret = directfb_init();
79 lprintf("directfb_init() failed\n");
84 giz_screen = fb_lock(1);
85 if (giz_screen == NULL)
87 lprintf("fb_lock() failed\n");
90 lprintf("fb_lock() returned %p\n", giz_screen);
107 #define PAGE_SIZE 0x1000
109 #define CACHE_SYNC_INSTRUCTIONS 0x002 /* discard all cached instructions */
110 #define CACHE_SYNC_WRITEBACK 0x004 /* write back but don't discard data cache*/
112 WINBASEAPI BOOL WINAPI CacheRangeFlush(LPVOID pAddr, DWORD dwLength, DWORD dwFlags);
113 WINBASEAPI BOOL WINAPI VirtualCopy(LPVOID lpvDest, LPVOID lpvSrc, DWORD cbSize, DWORD fdwProtect);
115 void cache_flush_d_inval_i(void *start_addr, void *end_addr)
117 int size = end_addr - start_addr;
118 CacheRangeFlush(start_addr, size, CACHE_SYNC_WRITEBACK);
119 CacheRangeFlush(start_addr, size, CACHE_SYNC_INSTRUCTIONS);
124 static void *mmap_phys(unsigned int addr, int pages)
129 mem = VirtualAlloc(0, pages*PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS);
132 lprintf("VirtualAlloc failed\n");
136 ret = VirtualCopy(mem, (void *)addr, pages*PAGE_SIZE, PAGE_READWRITE | PAGE_NOCACHE);
139 lprintf("VirtualFree failed\n");
140 VirtualFree(mem, 0, MEM_RELEASE);
147 static void munmap_phys(void *ptr)
149 VirtualFree(ptr, 0, MEM_RELEASE);
153 static int directfb_initialized = 0;
154 static int directfb_addrs[2] = { 0, 0 };
155 static void *directfb_ptrs[2] = { NULL, NULL };
156 static int directfb_sel = 0; // the one currently displayed
157 static volatile unsigned int *memregs = NULL;
159 /*static void xdump(void)
162 for (i = 0; i < 0x1000/4; i += 4)
164 lprintf("%04x: %08x %08x %08x %08x\n", i*4, memregs[i],
165 memregs[i+1], memregs[i+2], memregs[i+3]);
169 static int directfb_init(void)
171 memregs = mmap_phys(0xac009000, 1);
174 lprintf("can't access hw regs\n");
179 Framework2D_LockBuffer(1);
182 directfb_addrs[0] = memregs[0x5c>>2];
183 lprintf("fb0 is at %08x\n", directfb_addrs[0]);
185 Framework2D_UnlockBuffer();
187 directfb_ptrs[0] = mmap_phys(directfb_addrs[0], (321*240*2+PAGE_SIZE-1) / PAGE_SIZE);
188 if (directfb_ptrs[0] == NULL)
190 lprintf("failed to map fb0\n");
194 // use directx to discover other buffer
200 //Framework2D_LockBuffer(1);
202 directfb_addrs[1] = memregs[0x5c>>2] + 0x30000;
203 lprintf("fb1 is at %08x\n", directfb_addrs[1]);
205 //Framework2D_UnlockBuffer();
207 directfb_ptrs[1] = mmap_phys(directfb_addrs[1], (321*240*2+PAGE_SIZE-1) / PAGE_SIZE);
208 if (directfb_ptrs[1] == NULL)
210 lprintf("failed to map fb1\n");
214 directfb_initialized = 1;
219 munmap_phys(directfb_ptrs[0]);
221 munmap_phys((void *)memregs);
225 static void directfb_fini(void)
227 if (!directfb_initialized) return;
229 munmap_phys(directfb_ptrs[0]);
230 munmap_phys(directfb_ptrs[1]);
231 munmap_phys((void *)memregs);
234 void *directfb_lock(int is_front)
238 if (!directfb_initialized)
239 // fall back to directx
240 return Framework2D_LockBuffer(is_front);
243 which = directfb_sel;
245 which = directfb_sel ^ 1; // return backbuffer when possible
247 return directfb_ptrs[which];
250 void directfb_unlock(void)
252 if (!directfb_initialized)
253 // fall back to directx
254 Framework2D_UnlockBuffer();
257 void directfb_flip(void)
259 if (!directfb_initialized) {
266 memregs[0x5c>>2] = directfb_addrs[directfb_sel];