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