add a hack for Decap Attack
[picodrive.git] / platform / gizmondo / giz.c
CommitLineData
cff531af 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 */
e5f426aa 8#include <windows.h>
9#include <stdio.h>
e5f426aa 10
e5f426aa 11#include "kgsdk/Framework.h"
12#include "kgsdk/Framework2D.h"
84100c0f 13#include "giz.h"
14#include "version.h"
e5f426aa 15
16#define LOG_FILE "log.log"
17
18void *giz_screen = NULL;
19static FILE *logf = NULL;
20
84100c0f 21#if 0
22static int directfb_init(void);
23static void directfb_fini(void);
24#endif
25
26void lprintf(const char *fmt, ...)
e5f426aa 27{
28 va_list vl;
29
30 if (logf == NULL)
31 {
84100c0f 32 logf = fopen(LOG_FILE, "r+");
33 //logf = fopen(LOG_FILE, "a");
e5f426aa 34 if (logf == NULL)
35 return;
36 }
84100c0f 37 fseek(logf, 0, SEEK_END);
e5f426aa 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
47static void giz_log_close(void)
48{
49 if (logf != NULL)
50 {
51 fclose(logf);
52 logf = NULL;
53 }
54}
55
56void giz_init(HINSTANCE hInstance, HINSTANCE hPrevInstance)
57{
58 int ret;
59
84100c0f 60 lprintf("\n\nPicoDrive v" VERSION " (c) notaz, 2006-2008\n");
e5f426aa 61 lprintf("%s %s\n\n", __DATE__, __TIME__);
62
63 ret = Framework_Init(hInstance, hPrevInstance);
64 if (!ret)
65 {
84100c0f 66 lprintf("Framework_Init() failed\n");
e5f426aa 67 exit(1);
68 }
69 ret = Framework2D_Init();
70 if (!ret)
71 {
84100c0f 72 lprintf("Framework2D_Init() failed\n");
e5f426aa 73 exit(1);
74 }
84100c0f 75#if 0
76 ret = directfb_init();
77 if (ret != 0)
78 {
79 lprintf("directfb_init() failed\n");
80 }
81#endif
ea8c405f 82
83 // test screen
84100c0f 84 giz_screen = fb_lock(1);
ea8c405f 85 if (giz_screen == NULL)
86 {
84100c0f 87 lprintf("fb_lock() failed\n");
ea8c405f 88 exit(1);
89 }
84100c0f 90 lprintf("fb_lock() returned %p\n", giz_screen);
91 fb_unlock();
ea8c405f 92 giz_screen = NULL;
e5f426aa 93}
94
95void giz_deinit(void)
96{
97 Framework2D_Close();
98 Framework_Close();
84100c0f 99#if 0
100 directfb_fini();
101#endif
e5f426aa 102
103 giz_log_close();
104}
105
84100c0f 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
112WINBASEAPI BOOL WINAPI CacheRangeFlush(LPVOID pAddr, DWORD dwLength, DWORD dwFlags);
113WINBASEAPI BOOL WINAPI VirtualCopy(LPVOID lpvDest, LPVOID lpvSrc, DWORD cbSize, DWORD fdwProtect);
114
115void 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
124static 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
147static void munmap_phys(void *ptr)
148{
149 VirtualFree(ptr, 0, MEM_RELEASE);
150}
151
152// FB
153static int directfb_initialized = 0;
154static int directfb_addrs[2] = { 0, 0 };
155static void *directfb_ptrs[2] = { NULL, NULL };
156static int directfb_sel = 0; // the one currently displayed
157static 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
169static 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
218fail1:
219 munmap_phys(directfb_ptrs[0]);
220fail0:
221 munmap_phys((void *)memregs);
222 return -1;
223}
224
225static 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
234void *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
250void directfb_unlock(void)
251{
252 if (!directfb_initialized)
253 // fall back to directx
254 Framework2D_UnlockBuffer();
255}
256
257void 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