4 #include "kgsdk/Framework.h"
5 #include "kgsdk/Framework2D.h"
9 #define LOG_FILE "log.log"
11 void *giz_screen = NULL;
12 static FILE *logf = NULL;
15 static int directfb_init(void);
16 static void directfb_fini(void);
19 void lprintf(const char *fmt, ...)
25 logf = fopen(LOG_FILE, "r+");
26 //logf = fopen(LOG_FILE, "a");
30 fseek(logf, 0, SEEK_END);
32 //if (strchr(fmt, '\n'))
33 // fprintf(logf, "%lu: ", GetTickCount());
35 vfprintf(logf, fmt, vl);
40 static void giz_log_close(void)
49 void giz_init(HINSTANCE hInstance, HINSTANCE hPrevInstance)
53 lprintf("\n\nPicoDrive v" VERSION " (c) notaz, 2006-2008\n");
54 lprintf("%s %s\n\n", __DATE__, __TIME__);
56 ret = Framework_Init(hInstance, hPrevInstance);
59 lprintf("Framework_Init() failed\n");
62 ret = Framework2D_Init();
65 lprintf("Framework2D_Init() failed\n");
69 ret = directfb_init();
72 lprintf("directfb_init() failed\n");
77 giz_screen = fb_lock(1);
78 if (giz_screen == NULL)
80 lprintf("fb_lock() failed\n");
83 lprintf("fb_lock() returned %p\n", giz_screen);
100 #define PAGE_SIZE 0x1000
102 #define CACHE_SYNC_INSTRUCTIONS 0x002 /* discard all cached instructions */
103 #define CACHE_SYNC_WRITEBACK 0x004 /* write back but don't discard data cache*/
105 WINBASEAPI BOOL WINAPI CacheRangeFlush(LPVOID pAddr, DWORD dwLength, DWORD dwFlags);
106 WINBASEAPI BOOL WINAPI VirtualCopy(LPVOID lpvDest, LPVOID lpvSrc, DWORD cbSize, DWORD fdwProtect);
108 void cache_flush_d_inval_i(void *start_addr, void *end_addr)
110 int size = end_addr - start_addr;
111 CacheRangeFlush(start_addr, size, CACHE_SYNC_WRITEBACK);
112 CacheRangeFlush(start_addr, size, CACHE_SYNC_INSTRUCTIONS);
117 static void *mmap_phys(unsigned int addr, int pages)
122 mem = VirtualAlloc(0, pages*PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS);
125 lprintf("VirtualAlloc failed\n");
129 ret = VirtualCopy(mem, (void *)addr, pages*PAGE_SIZE, PAGE_READWRITE | PAGE_NOCACHE);
132 lprintf("VirtualFree failed\n");
133 VirtualFree(mem, 0, MEM_RELEASE);
140 static void munmap_phys(void *ptr)
142 VirtualFree(ptr, 0, MEM_RELEASE);
146 static int directfb_initialized = 0;
147 static int directfb_addrs[2] = { 0, 0 };
148 static void *directfb_ptrs[2] = { NULL, NULL };
149 static int directfb_sel = 0; // the one currently displayed
150 static volatile unsigned int *memregs = NULL;
152 /*static void xdump(void)
155 for (i = 0; i < 0x1000/4; i += 4)
157 lprintf("%04x: %08x %08x %08x %08x\n", i*4, memregs[i],
158 memregs[i+1], memregs[i+2], memregs[i+3]);
162 static int directfb_init(void)
164 memregs = mmap_phys(0xac009000, 1);
167 lprintf("can't access hw regs\n");
172 Framework2D_LockBuffer(1);
175 directfb_addrs[0] = memregs[0x5c>>2];
176 lprintf("fb0 is at %08x\n", directfb_addrs[0]);
178 Framework2D_UnlockBuffer();
180 directfb_ptrs[0] = mmap_phys(directfb_addrs[0], (321*240*2+PAGE_SIZE-1) / PAGE_SIZE);
181 if (directfb_ptrs[0] == NULL)
183 lprintf("failed to map fb0\n");
187 // use directx to discover other buffer
193 //Framework2D_LockBuffer(1);
195 directfb_addrs[1] = memregs[0x5c>>2] + 0x30000;
196 lprintf("fb1 is at %08x\n", directfb_addrs[1]);
198 //Framework2D_UnlockBuffer();
200 directfb_ptrs[1] = mmap_phys(directfb_addrs[1], (321*240*2+PAGE_SIZE-1) / PAGE_SIZE);
201 if (directfb_ptrs[1] == NULL)
203 lprintf("failed to map fb1\n");
207 directfb_initialized = 1;
212 munmap_phys(directfb_ptrs[0]);
214 munmap_phys((void *)memregs);
218 static void directfb_fini(void)
220 if (!directfb_initialized) return;
222 munmap_phys(directfb_ptrs[0]);
223 munmap_phys(directfb_ptrs[1]);
224 munmap_phys((void *)memregs);
227 void *directfb_lock(int is_front)
231 if (!directfb_initialized)
232 // fall back to directx
233 return Framework2D_LockBuffer(is_front);
236 which = directfb_sel;
238 which = directfb_sel ^ 1; // return backbuffer when possible
240 return directfb_ptrs[which];
243 void directfb_unlock(void)
245 if (!directfb_initialized)
246 // fall back to directx
247 Framework2D_UnlockBuffer();
250 void directfb_flip(void)
252 if (!directfb_initialized) {
259 memregs[0x5c>>2] = directfb_addrs[directfb_sel];