psxbios: Fix out-of-bounds issue.
[pcsx_rearmed.git] / libpcsxcore / psxbios.c
1 /***************************************************************************
2  *   Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team              *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
18  ***************************************************************************/
19
20 /*
21  * Internal simulated HLE BIOS.
22  */
23
24 // TODO: implement all system calls, count the exact CPU cycles of system calls.
25
26 #include "psxbios.h"
27 #include "psxhw.h"
28 #include "gpu.h"
29 #include <zlib.h>
30
31 #undef SysPrintf
32 #define SysPrintf if (Config.PsxOut) printf
33
34 char *biosA0n[256] = {
35 // 0x00
36         "open",         "lseek",        "read",         "write",
37         "close",        "ioctl",        "exit",         "sys_a0_07",
38         "getc",         "putc",         "todigit",      "atof",
39         "strtoul",      "strtol",       "abs",          "labs",
40 // 0x10
41         "atoi",         "atol",         "atob",         "setjmp",
42         "longjmp",      "strcat",       "strncat",      "strcmp",
43         "strncmp",      "strcpy",       "strncpy",      "strlen",
44         "index",        "rindex",       "strchr",       "strrchr",
45 // 0x20
46         "strpbrk",      "strspn",       "strcspn",      "strtok",
47         "strstr",       "toupper",      "tolower",      "bcopy",
48         "bzero",        "bcmp",         "memcpy",       "memset",
49         "memmove",      "memcmp",       "memchr",       "rand",
50 // 0x30
51         "srand",        "qsort",        "strtod",       "malloc",
52         "free",         "lsearch",      "bsearch",      "calloc",
53         "realloc",      "InitHeap",     "_exit",        "getchar",
54         "putchar",      "gets",         "puts",         "printf",
55 // 0x40
56         "sys_a0_40",            "LoadTest",                                     "Load",         "Exec",
57         "FlushCache",           "InstallInterruptHandler",      "GPU_dw",       "mem2vram",
58         "SendGPUStatus",        "GPU_cw",                                       "GPU_cwb",      "SendPackets",
59         "sys_a0_4c",            "GetGPUStatus",                         "GPU_sync",     "sys_a0_4f",
60 // 0x50
61         "sys_a0_50",            "LoadExec",                             "GetSysSp",             "sys_a0_53",
62         "_96_init()",           "_bu_init()",                   "_96_remove()", "sys_a0_57",
63         "sys_a0_58",            "sys_a0_59",                    "sys_a0_5a",    "dev_tty_init",
64         "dev_tty_open",         "sys_a0_5d",                    "dev_tty_ioctl","dev_cd_open",
65 // 0x60
66         "dev_cd_read",          "dev_cd_close",                 "dev_cd_firstfile",     "dev_cd_nextfile",
67         "dev_cd_chdir",         "dev_card_open",                "dev_card_read",        "dev_card_write",
68         "dev_card_close",       "dev_card_firstfile",   "dev_card_nextfile","dev_card_erase",
69         "dev_card_undelete","dev_card_format",          "dev_card_rename",      "dev_card_6f",
70 // 0x70
71         "_bu_init",                     "_96_init",             "_96_remove",           "sys_a0_73",
72         "sys_a0_74",            "sys_a0_75",    "sys_a0_76",            "sys_a0_77",
73         "_96_CdSeekL",          "sys_a0_79",    "sys_a0_7a",            "sys_a0_7b",
74         "_96_CdGetStatus",      "sys_a0_7d",    "_96_CdRead",           "sys_a0_7f",
75 // 0x80
76         "sys_a0_80",            "sys_a0_81",    "sys_a0_82",            "sys_a0_83",
77         "sys_a0_84",            "_96_CdStop",   "sys_a0_86",            "sys_a0_87",
78         "sys_a0_88",            "sys_a0_89",    "sys_a0_8a",            "sys_a0_8b",
79         "sys_a0_8c",            "sys_a0_8d",    "sys_a0_8e",            "sys_a0_8f",
80 // 0x90
81         "sys_a0_90",            "sys_a0_91",    "sys_a0_92",            "sys_a0_93",
82         "sys_a0_94",            "sys_a0_95",    "AddCDROMDevice",       "AddMemCardDevide",
83         "DisableKernelIORedirection",           "EnableKernelIORedirection", "sys_a0_9a", "sys_a0_9b",
84         "SetConf",                      "GetConf",              "sys_a0_9e",            "SetMem",
85 // 0xa0
86         "_boot",                        "SystemError",  "EnqueueCdIntr",        "DequeueCdIntr",
87         "sys_a0_a4",            "ReadSector",   "get_cd_status",        "bufs_cb_0",
88         "bufs_cb_1",            "bufs_cb_2",    "bufs_cb_3",            "_card_info",
89         "_card_load",           "_card_auto",   "bufs_cd_4",            "sys_a0_af",
90 // 0xb0
91         "sys_a0_b0",            "sys_a0_b1",    "do_a_long_jmp",        "sys_a0_b3",
92         "?? sub_function",
93 };
94
95 char *biosB0n[256] = {
96 // 0x00
97         "SysMalloc",            "sys_b0_01",    "sys_b0_02",    "sys_b0_03",
98         "sys_b0_04",            "sys_b0_05",    "sys_b0_06",    "DeliverEvent",
99         "OpenEvent",            "CloseEvent",   "WaitEvent",    "TestEvent",
100         "EnableEvent",          "DisableEvent", "OpenTh",               "CloseTh",
101 // 0x10
102         "ChangeTh",                     "sys_b0_11",    "InitPAD",              "StartPAD",
103         "StopPAD",                      "PAD_init",             "PAD_dr",               "ReturnFromExecption",
104         "ResetEntryInt",        "HookEntryInt", "sys_b0_1a",    "sys_b0_1b",
105         "sys_b0_1c",            "sys_b0_1d",    "sys_b0_1e",    "sys_b0_1f",
106 // 0x20
107         "UnDeliverEvent",       "sys_b0_21",    "sys_b0_22",    "sys_b0_23",
108         "sys_b0_24",            "sys_b0_25",    "sys_b0_26",    "sys_b0_27",
109         "sys_b0_28",            "sys_b0_29",    "sys_b0_2a",    "sys_b0_2b",
110         "sys_b0_2c",            "sys_b0_2d",    "sys_b0_2e",    "sys_b0_2f",
111 // 0x30
112         "sys_b0_30",            "sys_b0_31",    "open",                 "lseek",
113         "read",                         "write",                "close",                "ioctl",
114         "exit",                         "sys_b0_39",    "getc",                 "putc",
115         "getchar",                      "putchar",              "gets",                 "puts",
116 // 0x40
117         "cd",                           "format",               "firstfile",    "nextfile",
118         "rename",                       "delete",               "undelete",             "AddDevice",
119         "RemoteDevice",         "PrintInstalledDevices", "InitCARD", "StartCARD",
120         "StopCARD",                     "sys_b0_4d",    "_card_write",  "_card_read",
121 // 0x50
122         "_new_card",            "Krom2RawAdd",  "sys_b0_52",    "sys_b0_53",
123         "_get_errno",           "_get_error",   "GetC0Table",   "GetB0Table",
124         "_card_chan",           "sys_b0_59",    "sys_b0_5a",    "ChangeClearPAD",
125         "_card_status",         "_card_wait",
126 };
127
128 char *biosC0n[256] = {
129 // 0x00
130         "InitRCnt",                       "InitException",              "SysEnqIntRP",          "SysDeqIntRP",
131         "get_free_EvCB_slot", "get_free_TCB_slot",      "ExceptionHandler",     "InstallExeptionHandler",
132         "SysInitMemory",          "SysInitKMem",                "ChangeClearRCnt",      "SystemError",
133         "InitDefInt",             "sys_c0_0d",                  "sys_c0_0e",            "sys_c0_0f",
134 // 0x10
135         "sys_c0_10",              "sys_c0_11",                  "InstallDevices",       "FlushStfInOutPut",
136         "sys_c0_14",              "_cdevinput",                 "_cdevscan",            "_circgetc",
137         "_circputc",              "ioabort",                    "sys_c0_1a",            "KernelRedirect",
138         "PatchAOTable",
139 };
140
141 //#define r0 (psxRegs.GPR.n.r0)
142 #define at (psxRegs.GPR.n.at)
143 #define v0 (psxRegs.GPR.n.v0)
144 #define v1 (psxRegs.GPR.n.v1)
145 #define a0 (psxRegs.GPR.n.a0)
146 #define a1 (psxRegs.GPR.n.a1)
147 #define a2 (psxRegs.GPR.n.a2)
148 #define a3 (psxRegs.GPR.n.a3)
149 #define t0 (psxRegs.GPR.n.t0)
150 #define t1 (psxRegs.GPR.n.t1)
151 #define t2 (psxRegs.GPR.n.t2)
152 #define t3 (psxRegs.GPR.n.t3)
153 #define t4 (psxRegs.GPR.n.t4)
154 #define t5 (psxRegs.GPR.n.t5)
155 #define t6 (psxRegs.GPR.n.t6)
156 #define t7 (psxRegs.GPR.n.t7)
157 #define t8 (psxRegs.GPR.n.t8)
158 #define t9 (psxRegs.GPR.n.t9)
159 #define s0 (psxRegs.GPR.n.s0)
160 #define s1 (psxRegs.GPR.n.s1)
161 #define s2 (psxRegs.GPR.n.s2)
162 #define s3 (psxRegs.GPR.n.s3)
163 #define s4 (psxRegs.GPR.n.s4)
164 #define s5 (psxRegs.GPR.n.s5)
165 #define s6 (psxRegs.GPR.n.s6)
166 #define s7 (psxRegs.GPR.n.s7)
167 #define k0 (psxRegs.GPR.n.k0)
168 #define k1 (psxRegs.GPR.n.k1)
169 #define gp (psxRegs.GPR.n.gp)
170 #define sp (psxRegs.GPR.n.sp)
171 #define fp (psxRegs.GPR.n.s8)
172 #define ra (psxRegs.GPR.n.ra)
173 #define pc0 (psxRegs.pc)
174
175 #define Ra0 ((char *)PSXM(a0))
176 #define Ra1 ((char *)PSXM(a1))
177 #define Ra2 ((char *)PSXM(a2))
178 #define Ra3 ((char *)PSXM(a3))
179 #define Rv0 ((char *)PSXM(v0))
180 #define Rsp ((char *)PSXM(sp))
181
182 typedef struct {
183         u32 desc;
184         s32 status;
185         s32 mode;
186         u32 fhandler;
187 } EvCB[32];
188
189 #define EvStUNUSED      0x0000
190 #define EvStWAIT        0x1000
191 #define EvStACTIVE      0x2000
192 #define EvStALREADY 0x4000
193
194 #define EvMdINTR        0x1000
195 #define EvMdNOINTR      0x2000
196
197 /*
198 typedef struct {
199         s32 next;
200         s32 func1;
201         s32 func2;
202         s32 pad;
203 } SysRPst;
204 */
205
206 typedef struct {
207         s32 status;
208         s32 mode;
209         u32 reg[32];
210         u32 func;
211 } TCB;
212
213 typedef struct {                   
214         u32 _pc0;
215         u32 gp0;
216         u32 t_addr;
217         u32 t_size;
218         u32 d_addr;
219         u32 d_size;
220         u32 b_addr;
221         u32 b_size;
222         u32 S_addr;
223         u32 s_size;
224         u32 _sp, _fp, _gp, ret, base;
225 } EXEC;
226
227 struct DIRENTRY {
228         char name[20];
229         s32 attr;
230         s32 size;
231         u32 next;
232         s32 head;
233         char system[4];
234 };
235
236 typedef struct {
237         char name[32];
238         u32  mode;
239         u32  offset;
240         u32  size;
241         u32  mcfile;
242 } FileDesc;
243
244 static u32 *jmp_int = NULL;
245 static int *pad_buf = NULL;
246 static char *pad_buf1 = NULL, *pad_buf2 = NULL;
247 static int pad_buf1len, pad_buf2len;
248
249 static u32 regs[35];
250 static EvCB *Event;
251 static EvCB *HwEV; // 0xf0
252 static EvCB *EvEV; // 0xf1
253 static EvCB *RcEV; // 0xf2
254 static EvCB *UeEV; // 0xf3
255 static EvCB *SwEV; // 0xf4
256 static EvCB *ThEV; // 0xff
257 static u32 *heap_addr = NULL;
258 static u32 *heap_end = NULL;
259 static u32 SysIntRP[8];
260 static int CardState = -1;
261 static TCB Thread[8];
262 static int CurThread = 0;
263 static FileDesc FDesc[32];
264 static u32 card_active_chan;
265
266 boolean hleSoftCall = FALSE;
267
268 static inline void softCall(u32 pc) {
269         pc0 = pc;
270         ra = 0x80001000;
271
272         hleSoftCall = TRUE;
273
274         while (pc0 != 0x80001000) psxCpu->ExecuteBlock();
275
276         hleSoftCall = FALSE;
277 }
278
279 static inline void softCall2(u32 pc) {
280         u32 sra = ra;
281         pc0 = pc;
282         ra = 0x80001000;
283
284         hleSoftCall = TRUE;
285
286         while (pc0 != 0x80001000) psxCpu->ExecuteBlock();
287         ra = sra;
288
289         hleSoftCall = FALSE;
290 }
291
292 static inline void DeliverEvent(u32 ev, u32 spec) {
293         if (Event[ev][spec].status != EvStACTIVE) return;
294
295 //      Event[ev][spec].status = EvStALREADY;
296         if (Event[ev][spec].mode == EvMdINTR) {
297                 softCall2(Event[ev][spec].fhandler);
298         } else Event[ev][spec].status = EvStALREADY;
299 }
300
301 static inline void SaveRegs() {
302         memcpy(regs, psxRegs.GPR.r, 32*4);
303         regs[32] = psxRegs.GPR.n.lo;
304         regs[33] = psxRegs.GPR.n.hi;
305         regs[34] = psxRegs.pc;
306 }
307
308 static inline void LoadRegs() {
309         memcpy(psxRegs.GPR.r, regs, 32*4);
310         psxRegs.GPR.n.lo = regs[32];
311         psxRegs.GPR.n.hi = regs[33];
312 }
313
314 /*                                           *
315 //                                           *
316 //                                           *
317 //               System calls A0             */
318
319
320 #define buread(Ra1, mcd, length) { \
321         SysPrintf("read %d: %x,%x (%s)\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, a2, Mcd##mcd##Data + 128 * FDesc[1 + mcd].mcfile + 0xa); \
322         ptr = Mcd##mcd##Data + 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
323         memcpy(Ra1, ptr, length); \
324         DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
325         DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
326         if (FDesc[1 + mcd].mode & 0x8000) v0 = 0; \
327         else v0 = length; \
328         FDesc[1 + mcd].offset += v0; \
329 }
330
331 #define buwrite(Ra1, mcd, length) { \
332         u32 offset =  + 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
333         SysPrintf("write %d: %x,%x\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, a2); \
334         ptr = Mcd##mcd##Data + offset; \
335         memcpy(ptr, Ra1, length); \
336         DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
337         DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
338         FDesc[1 + mcd].offset += length; \
339         if (FDesc[1 + mcd].mode & 0x8000) v0 = 0; \
340         else v0 = length; \
341 }
342
343
344 /* Internally redirects to "FileRead(fd,tempbuf,1)".*/
345 /* For some strange reason, the returned character is sign-expanded; */
346 /* So if a return value of FFFFFFFFh could mean either character FFh, or error. */
347 /* TODO FIX ME : Properly implement this behaviour */
348 void psxBios_getc(void) // 0x03, 0x35
349 {
350         char *ptr;
351         void *pa1 = Ra1;
352 #ifdef PSXBIOS_LOG
353         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x03]);
354 #endif
355         v0 = -1;
356
357         if (pa1) {
358                 switch (a0) {
359                         case 2: buread(pa1, 1, 1); break;
360                         case 3: buread(pa1, 2, 1); break;
361                 }
362         }
363
364         pc0 = ra;
365 }
366
367 /* Copy of psxBios_write, except size is 1. */
368 void psxBios_putc(void) // 0x09, 0x3B
369 {
370         char *ptr;
371         void *pa1 = Ra1;
372 #ifdef PSXBIOS_LOG
373         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x09]);
374 #endif
375         v0 = -1;
376         if (!pa1) {
377                 pc0 = ra;
378                 return;
379         }
380
381         if (a0 == 1) { // stdout
382                 char *ptr = (char *)pa1;
383
384                 v0 = a2;
385                 while (a2 > 0) {
386                         printf("%c", *ptr++); a2--;
387                 }
388                 pc0 = ra; return;
389         }
390
391         switch (a0) {
392                 case 2: buwrite(pa1, 1, 1); break;
393                 case 3: buwrite(pa1, 2, 1); break;
394         }
395
396         pc0 = ra;
397 }
398
399 void psxBios_todigit(void) // 0x0a
400 {
401         int c = a0;
402 #ifdef PSXBIOS_LOG
403         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x0a]);
404 #endif
405         c &= 0xFF;
406         if (c >= 0x30 && c < 0x3A) {
407                 c -= 0x30;
408         }
409         else if (c > 0x60 && c < 0x7B) {
410                 c -= 0x20;
411         }
412         else if (c > 0x40 && c < 0x5B) {
413                 c = c - 0x41 + 10;
414         }
415         else if (c >= 0x80) {
416                 c = -1;
417         }
418         else
419         {
420                 c = 0x0098967F;
421         }
422         v0 = c;
423         pc0 = ra;
424 }
425
426 void psxBios_abs() { // 0x0e
427         if ((s32)a0 < 0) v0 = -(s32)a0;
428         else v0 = a0;
429         pc0 = ra;
430 }
431
432 void psxBios_labs() { // 0x0f
433         psxBios_abs();
434 }
435
436 void psxBios_atoi() { // 0x10
437         s32 n = 0, f = 0;
438         char *p = (char *)Ra0;
439
440         for (;;p++) {
441                 switch (*p) {
442                         case ' ': case '\t': continue;
443                         case '-': f++;
444                         case '+': p++;
445                 }
446                 break;
447         }
448
449         while (*p >= '0' && *p <= '9') {
450                 n = n * 10 + *p++ - '0';
451         }
452
453         v0 = (f ? -n : n);
454         pc0 = ra;
455 }
456
457 void psxBios_atol() { // 0x11
458         psxBios_atoi();
459 }
460
461 void psxBios_setjmp() { // 0x13
462         u32 *jmp_buf = (u32 *)Ra0;
463         int i;
464
465 #ifdef PSXBIOS_LOG
466         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x13]);
467 #endif
468
469         jmp_buf[0] = ra;
470         jmp_buf[1] = sp;
471         jmp_buf[2] = fp;
472         for (i = 0; i < 8; i++) // s0-s7
473                 jmp_buf[3 + i] = psxRegs.GPR.r[16 + i];
474         jmp_buf[11] = gp;
475
476         v0 = 0; pc0 = ra;
477 }
478
479 void psxBios_longjmp() { // 0x14
480         u32 *jmp_buf = (u32 *)Ra0;
481         int i;
482
483 #ifdef PSXBIOS_LOG
484         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x14]);
485 #endif
486
487         ra = jmp_buf[0]; /* ra */
488         sp = jmp_buf[1]; /* sp */
489         fp = jmp_buf[2]; /* fp */
490         for (i = 0; i < 8; i++) // s0-s7
491                 psxRegs.GPR.r[16 + i] = jmp_buf[3 + i];
492         gp = jmp_buf[11]; /* gp */
493
494         v0 = a1; pc0 = ra;
495 }
496
497 void psxBios_strcat() { // 0x15
498         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
499
500 #ifdef PSXBIOS_LOG
501         PSXBIOS_LOG("psxBios_%s: %s, %s\n", biosA0n[0x15], Ra0, Ra1);
502 #endif
503         if (a0 == 0 || a1 == 0)
504         {
505                 v0 = 0;
506                 pc0 = ra;
507                 return;
508         }
509         while (*p1++);
510         --p1;
511         while ((*p1++ = *p2++) != '\0');
512
513         v0 = a0; pc0 = ra;
514 }
515
516 void psxBios_strncat() { // 0x16
517         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
518         s32 n = a2;
519
520 #ifdef PSXBIOS_LOG
521         PSXBIOS_LOG("psxBios_%s: %s (%x), %s (%x), %d\n", biosA0n[0x16], Ra0, a0, Ra1, a1, a2);
522 #endif
523         if (a0 == 0 || a1 == 0)
524         {
525                 v0 = 0;
526                 pc0 = ra;
527                 return;
528         }
529         while (*p1++);
530         --p1;
531         while ((*p1++ = *p2++) != '\0') {
532                 if (--n < 0) {
533                         *--p1 = '\0';
534                         break;
535                 }
536         }
537
538         v0 = a0; pc0 = ra;
539 }
540
541 void psxBios_strcmp() { // 0x17
542         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
543
544 #ifdef PSXBIOS_LOG
545         PSXBIOS_LOG("psxBios_%s: %s (%x), %s (%x)\n", biosA0n[0x17], Ra0, a0, Ra1, a1);
546 #endif
547
548         while (*p1 == *p2++) {
549                 if (*p1++ == '\0') {
550                         v0 = 0;
551                         pc0 = ra;
552                         return;
553                 }
554         }
555
556         v0 = (*p1 - *--p2);
557         pc0 = ra;
558 }
559
560 void psxBios_strncmp() { // 0x18
561         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
562         s32 n = a2;
563
564 #ifdef PSXBIOS_LOG
565         PSXBIOS_LOG("psxBios_%s: %s (%x), %s (%x), %d\n", biosA0n[0x18], Ra0, a0, Ra1, a1, a2);
566 #endif
567
568         while (--n >= 0 && *p1 == *p2++) {
569                 if (*p1++ == '\0') {
570                         v0 = 0;
571                         pc0 = ra;
572                         return;
573                 }
574         }
575
576         v0 = (n < 0 ? 0 : *p1 - *--p2);
577         pc0 = ra;
578 }
579
580 void psxBios_strcpy() { // 0x19
581         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
582         while ((*p1++ = *p2++) != '\0');
583
584         v0 = a0; pc0 = ra;
585 }
586
587 void psxBios_strncpy() { // 0x1a
588         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
589         s32 n = a2, i;
590
591         for (i = 0; i < n; i++) {
592                 if ((*p1++ = *p2++) == '\0') {
593                         while (++i < n) {
594                                 *p1++ = '\0';
595                         }
596                         v0 = a0; pc0 = ra;
597                         return;
598                 }
599         }
600
601         v0 = a0; pc0 = ra;
602 }
603
604 void psxBios_strlen() { // 0x1b
605         char *p = (char *)Ra0;
606         v0 = 0;
607         while (*p++) v0++;
608         pc0 = ra;
609 }
610
611 void psxBios_index() { // 0x1c
612         char *p = (char *)Ra0;
613
614         do {
615                 if (*p == a1) {
616                         v0 = a0 + (p - (char *)Ra0);
617                         pc0 = ra;
618                         return;
619                 }
620         } while (*p++ != '\0');
621
622         v0 = 0; pc0 = ra;
623 }
624
625 void psxBios_rindex() { // 0x1d
626         char *p = (char *)Ra0;
627
628         v0 = 0;
629
630         do {
631                 if (*p == a1)
632                         v0 = a0 + (p - (char *)Ra0);
633         } while (*p++ != '\0');
634
635         pc0 = ra;
636 }
637
638 void psxBios_strchr() { // 0x1e
639         psxBios_index();
640 }
641
642 void psxBios_strrchr() { // 0x1f
643         psxBios_rindex();
644 }
645
646 void psxBios_strpbrk() { // 0x20
647         char *p1 = (char *)Ra0, *p2 = (char *)Ra1, *scanp, c, sc;
648
649         while ((c = *p1++) != '\0') {
650                 for (scanp = p2; (sc = *scanp++) != '\0';) {
651                         if (sc == c) {
652                                 v0 = a0 + (p1 - 1 - (char *)Ra0);
653                                 pc0 = ra;
654                                 return;
655                         }
656                 }
657         }
658
659         // BUG: return a0 instead of NULL if not found
660         v0 = a0; pc0 = ra;
661 }
662
663 void psxBios_strspn() { // 0x21
664         char *p1, *p2;
665
666         for (p1 = (char *)Ra0; *p1 != '\0'; p1++) {
667                 for (p2 = (char *)Ra1; *p2 != '\0' && *p2 != *p1; p2++);
668                 if (*p2 == '\0') break;
669         }
670
671         v0 = p1 - (char *)Ra0; pc0 = ra;
672 }
673
674 void psxBios_strcspn() { // 0x22
675         char *p1, *p2;
676
677         for (p1 = (char *)Ra0; *p1 != '\0'; p1++) {
678                 for (p2 = (char *)Ra1; *p2 != '\0' && *p2 != *p1; p2++);
679                 if (*p2 != '\0') break;
680         }
681
682         v0 = p1 - (char *)Ra0; pc0 = ra;
683 }
684
685 void psxBios_strtok() { // 0x23
686         char *pcA0 = (char *)Ra0;
687         char *pcRet = strtok(pcA0, (char *)Ra1);
688         if (pcRet)
689                 v0 = a0 + pcRet - pcA0;
690         else
691                 v0 = 0;
692         pc0 = ra;
693 }
694
695 void psxBios_strstr() { // 0x24
696         char *p = (char *)Ra0, *p1, *p2;
697
698         while (*p != '\0') {
699                 p1 = p;
700                 p2 = (char *)Ra1;
701
702                 while (*p1 != '\0' && *p2 != '\0' && *p1 == *p2) {
703                         p1++; p2++;
704                 }
705
706                 if (*p2 == '\0') {
707                         v0 = a0 + (p - (char *)Ra0);
708                         pc0 = ra;
709                         return;
710                 }
711
712                 p++;
713         }
714
715         v0 = 0; pc0 = ra;
716 }
717
718 void psxBios_toupper() { // 0x25
719         v0 = (s8)(a0 & 0xff);
720         if (v0 >= 'a' && v0 <= 'z') v0 -= 'a' - 'A';
721         pc0 = ra;
722 }
723
724 void psxBios_tolower() { // 0x26
725         v0 = (s8)(a0 & 0xff);
726         if (v0 >= 'A' && v0 <= 'Z') v0 += 'a' - 'A';
727         pc0 = ra;
728 }
729
730 void psxBios_bcopy() { // 0x27
731         char *p1 = (char *)Ra1, *p2 = (char *)Ra0;
732         while ((s32)a2-- > 0) *p1++ = *p2++;
733
734         pc0 = ra;
735 }
736
737 void psxBios_bzero() { // 0x28
738         char *p = (char *)Ra0;
739         while ((s32)a1-- > 0) *p++ = '\0';
740
741         pc0 = ra;
742 }
743
744 void psxBios_bcmp() { // 0x29
745         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
746
747         if (a0 == 0 || a1 == 0) { v0 = 0; pc0 = ra; return; }
748
749         while ((s32)a2-- > 0) {
750                 if (*p1++ != *p2++) {
751                         v0 = *p1 - *p2; // BUG: compare the NEXT byte
752                         pc0 = ra;
753                         return;
754                 }
755         }
756
757         v0 = 0; pc0 = ra;
758 }
759
760 void psxBios_memcpy() { // 0x2a
761         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
762         while ((s32)a2-- > 0) *p1++ = *p2++;
763
764         v0 = a0; pc0 = ra;
765 }
766
767 void psxBios_memset() { // 0x2b
768         char *p = (char *)Ra0;
769         while ((s32)a2-- > 0) *p++ = (char)a1;
770
771         a2 = 0;
772         v0 = a0; pc0 = ra;
773 }
774
775 void psxBios_memmove() { // 0x2c
776         char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
777
778         if (p2 <= p1 && p2 + a2 > p1) {
779                 a2++; // BUG: copy one more byte here
780                 p1 += a2;
781                 p2 += a2;
782                 while ((s32)a2-- > 0) *--p1 = *--p2;
783         } else {
784                 while ((s32)a2-- > 0) *p1++ = *p2++;
785         }
786
787         v0 = a0; pc0 = ra;
788 }
789
790 void psxBios_memcmp() { // 0x2d
791         psxBios_bcmp();
792 }
793
794 void psxBios_memchr() { // 0x2e
795         char *p = (char *)Ra0;
796
797         while ((s32)a2-- > 0) {
798                 if (*p++ != (s8)a1) continue;
799                 v0 = a0 + (p - (char *)Ra0 - 1);
800                 pc0 = ra;
801                 return;
802         }
803
804         v0 = 0; pc0 = ra;
805 }
806
807 void psxBios_rand() { // 0x2f
808         u32 s = psxMu32(0x9010) * 1103515245 + 12345;
809         v0 = (s >> 16) & 0x7fff;
810         psxMu32ref(0x9010) = SWAPu32(s);
811         pc0 = ra;
812 }
813
814 void psxBios_srand() { // 0x30
815         psxMu32ref(0x9010) = SWAPu32(a0);
816         pc0 = ra;
817 }
818
819 static u32 qscmpfunc, qswidth;
820
821 static inline int qscmp(char *a, char *b) {
822         u32 sa0 = a0;
823
824         a0 = sa0 + (a - (char *)PSXM(sa0));
825         a1 = sa0 + (b - (char *)PSXM(sa0));
826
827         softCall2(qscmpfunc);
828
829         a0 = sa0;
830         return (s32)v0;
831 }
832
833 static inline void qexchange(char *i, char *j) {
834         char t;
835         int n = qswidth;
836
837         do {
838                 t = *i;
839                 *i++ = *j;
840                 *j++ = t;
841         } while (--n);
842 }
843
844 static inline void q3exchange(char *i, char *j, char *k) {
845         char t;
846         int n = qswidth;
847
848         do {
849                 t = *i;
850                 *i++ = *k;
851                 *k++ = *j;
852                 *j++ = t;
853         } while (--n);
854 }
855
856 static void qsort_main(char *a, char *l) {
857         char *i, *j, *lp, *hp;
858         int c;
859         unsigned int n;
860
861 start:
862         if ((n = l - a) <= qswidth)
863                 return;
864         n = qswidth * (n / (2 * qswidth));
865         hp = lp = a + n;
866         i = a;
867         j = l - qswidth;
868         while (TRUE) {
869                 if (i < lp) {
870                         if ((c = qscmp(i, lp)) == 0) {
871                                 qexchange(i, lp -= qswidth);
872                                 continue;
873                         }
874                         if (c < 0) {
875                                 i += qswidth;
876                                 continue;
877                         }
878                 }
879
880 loop:
881                 if (j > hp) {
882                         if ((c = qscmp(hp, j)) == 0) {
883                                 qexchange(hp += qswidth, j);
884                                 goto loop;
885                         }
886                         if (c > 0) {
887                                 if (i == lp) {
888                                         q3exchange(i, hp += qswidth, j);
889                                         i = lp += qswidth;
890                                         goto loop;
891                                 }
892                                 qexchange(i, j);
893                                 j -= qswidth;
894                                 i += qswidth;
895                                 continue;
896                         }
897                         j -= qswidth;
898                         goto loop;
899                 }
900
901                 if (i == lp) {
902                         if (lp - a >= l - hp) {
903                                 qsort_main(hp + qswidth, l);
904                                 l = lp;
905                         } else {
906                                 qsort_main(a, lp);
907                                 a = hp + qswidth;
908                         }
909                         goto start;
910                 }
911
912                 q3exchange(j, lp -= qswidth, i);
913                 j = hp -= qswidth;
914         }
915 }
916
917 void psxBios_qsort() { // 0x31
918         qswidth = a2;
919         qscmpfunc = a3;
920         qsort_main((char *)Ra0, (char *)Ra0 + a1 * a2);
921
922         pc0 = ra;
923 }
924
925 void psxBios_malloc() { // 0x33
926         unsigned int *chunk, *newchunk = NULL;
927         unsigned int dsize = 0, csize, cstat;
928         int colflag;
929 #ifdef PSXBIOS_LOG
930         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x33]);
931 #endif
932
933         // scan through heap and combine free chunks of space
934         chunk = heap_addr;
935         colflag = 0;
936         while(chunk < heap_end) {
937                 // get size and status of actual chunk
938                 csize = ((u32)*chunk) & 0xfffffffc;
939                 cstat = ((u32)*chunk) & 1;
940
941                 // it's a free chunk
942                 if(cstat == 1) {
943                         if(colflag == 0) {
944                                 newchunk = chunk;
945                                 dsize = csize;
946                                 colflag = 1;                    // let's begin a new collection of free memory
947                         }
948                         else dsize += (csize+4);        // add the new size including header
949                 }
950                 // not a free chunk: did we start a collection ?
951                 else {
952                         if(colflag == 1) {                      // collection is over
953                                 colflag = 0;
954                                 *newchunk = SWAP32(dsize | 1);
955                         }
956                 }
957
958                 // next chunk
959                 chunk = (u32*)((uptr)chunk + csize + 4);
960         }
961         // if neccessary free memory on end of heap
962         if (colflag == 1)
963                 *newchunk = SWAP32(dsize | 1);
964
965         chunk = heap_addr;
966         csize = ((u32)*chunk) & 0xfffffffc;
967         cstat = ((u32)*chunk) & 1;
968         dsize = (a0 + 3) & 0xfffffffc;
969
970         // exit on uninitialized heap
971         if (chunk == NULL) {
972                 SysPrintf("malloc %x,%x: Uninitialized Heap!\n", v0, a0);
973                 v0 = 0;
974                 pc0 = ra;
975                 return;
976         }
977
978         // search an unused chunk that is big enough until the end of the heap
979         while ((dsize > csize || cstat == 0) && chunk < heap_end ) {
980                 chunk = (u32*)((uptr)chunk + csize + 4);
981                 csize = ((u32)*chunk) & 0xfffffffc;
982                 cstat = ((u32)*chunk) & 1;
983         }
984
985         // catch out of memory
986         if(chunk >= heap_end) { SysPrintf("malloc %x,%x: Out of memory error!\n", v0, a0); v0 = 0; pc0 = ra; return; }
987         
988         // allocate memory
989         if(dsize == csize) {
990                 // chunk has same size
991                 *chunk &= 0xfffffffc;
992         }
993         else {
994                 // split free chunk
995                 *chunk = SWAP32(dsize);
996                 newchunk = (u32*)((uptr)chunk + dsize + 4);
997                 *newchunk = SWAP32(((csize - dsize - 4) & 0xfffffffc) | 1);
998         }
999
1000         // return pointer to allocated memory
1001         v0 = ((unsigned long)chunk - (unsigned long)psxM) + 4;
1002         v0|= 0x80000000;
1003         SysPrintf ("malloc %x,%x\n", v0, a0);
1004         pc0 = ra;
1005 }
1006
1007 void psxBios_free() { // 0x34
1008
1009 #ifdef PSXBIOS_LOG
1010         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x34]);
1011 #endif
1012
1013         SysPrintf("free %x: %x bytes\n", a0, *(u32*)(Ra0-4));
1014
1015         *(u32*)(Ra0-4) |= 1;    // set chunk to free
1016         pc0 = ra;
1017 }
1018
1019 void psxBios_calloc() { // 0x37
1020         void *pv0;
1021 #ifdef PSXBIOS_LOG
1022         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x37]);
1023 #endif
1024
1025         a0 = a0 * a1;
1026         psxBios_malloc();
1027         pv0 = Rv0;
1028         if (pv0)
1029                 memset(pv0, 0, a0);
1030 }
1031
1032 void psxBios_realloc() { // 0x38
1033         u32 block = a0;
1034         u32 size = a1;
1035 #ifdef PSXBIOS_LOG
1036         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x38]);
1037 #endif
1038
1039         a0 = block;
1040         /* If "old_buf" is zero, executes malloc(new_size), and returns r2=new_buf (or 0=failed). */
1041         if (block == 0)
1042         {
1043                 psxBios_malloc();
1044         }
1045         /* Else, if "new_size" is zero, executes free(old_buf), and returns r2=garbage. */
1046         else if (size == 0)
1047         {
1048                 psxBios_free();
1049         }
1050         /* Else, executes malloc(new_size), bcopy(old_buf,new_buf,new_size), and free(old_buf), and returns r2=new_buf (or 0=failed). */
1051         /* Note that it is not quite implemented this way here. */
1052         else
1053         {
1054                 psxBios_free();
1055                 a0 = size;
1056                 psxBios_malloc();
1057         }
1058 }
1059
1060
1061 /* InitHeap(void *block , int n) */
1062 void psxBios_InitHeap() { // 0x39
1063         unsigned int size;
1064
1065 #ifdef PSXBIOS_LOG
1066         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x39]);
1067 #endif
1068
1069         if (((a0 & 0x1fffff) + a1)>= 0x200000) size = 0x1ffffc - (a0 & 0x1fffff);
1070         else size = a1;
1071
1072         size &= 0xfffffffc;
1073
1074         heap_addr = (u32 *)Ra0;
1075         heap_end = (u32 *)((u8 *)heap_addr + size);
1076         *heap_addr = SWAP32(size | 1);
1077
1078         SysPrintf("InitHeap %x,%x : %x %x\n",a0,a1, (int)((uptr)heap_addr-(uptr)psxM), size);
1079
1080         pc0 = ra;
1081 }
1082
1083 void psxBios_getchar() { //0x3b
1084         v0 = getchar(); pc0 = ra;
1085 }
1086
1087 void psxBios_printf() { // 0x3f
1088         char tmp[1024];
1089         char tmp2[1024];
1090         u32 save[4];
1091         char *ptmp = tmp;
1092         int n=1, i=0, j;
1093         void *psp;
1094
1095         psp = PSXM(sp);
1096         if (psp) {
1097                 memcpy(save, psp, 4 * 4);
1098                 psxMu32ref(sp) = SWAP32((u32)a0);
1099                 psxMu32ref(sp + 4) = SWAP32((u32)a1);
1100                 psxMu32ref(sp + 8) = SWAP32((u32)a2);
1101                 psxMu32ref(sp + 12) = SWAP32((u32)a3);
1102         }
1103
1104         while (Ra0[i]) {
1105                 switch (Ra0[i]) {
1106                         case '%':
1107                                 j = 0;
1108                                 tmp2[j++] = '%';
1109 _start:
1110                                 switch (Ra0[++i]) {
1111                                         case '.':
1112                                         case 'l':
1113                                                 tmp2[j++] = Ra0[i]; goto _start;
1114                                         default:
1115                                                 if (Ra0[i] >= '0' && Ra0[i] <= '9') {
1116                                                         tmp2[j++] = Ra0[i];
1117                                                         goto _start;
1118                                                 }
1119                                                 break;
1120                                 }
1121                                 tmp2[j++] = Ra0[i];
1122                                 tmp2[j] = 0;
1123
1124                                 switch (Ra0[i]) {
1125                                         case 'f': case 'F':
1126                                                 ptmp += sprintf(ptmp, tmp2, (float)psxMu32(sp + n * 4)); n++; break;
1127                                         case 'a': case 'A':
1128                                         case 'e': case 'E':
1129                                         case 'g': case 'G':
1130                                                 ptmp += sprintf(ptmp, tmp2, (double)psxMu32(sp + n * 4)); n++; break;
1131                                         case 'p':
1132                                         case 'i': case 'u':
1133                                         case 'd': case 'D':
1134                                         case 'o': case 'O':
1135                                         case 'x': case 'X':
1136                                                 ptmp += sprintf(ptmp, tmp2, (unsigned int)psxMu32(sp + n * 4)); n++; break;
1137                                         case 'c':
1138                                                 ptmp += sprintf(ptmp, tmp2, (unsigned char)psxMu32(sp + n * 4)); n++; break;
1139                                         case 's':
1140                                                 ptmp += sprintf(ptmp, tmp2, (char*)PSXM(psxMu32(sp + n * 4))); n++; break;
1141                                         case '%':
1142                                                 *ptmp++ = Ra0[i]; break;
1143                                 }
1144                                 i++;
1145                                 break;
1146                         default:
1147                                 *ptmp++ = Ra0[i++];
1148                 }
1149         }
1150         *ptmp = 0;
1151
1152         if (psp)
1153                 memcpy(psp, save, 4 * 4);
1154
1155         SysPrintf("%s", tmp);
1156
1157         pc0 = ra;
1158 }
1159
1160 void psxBios_format() { // 0x41
1161         if (strcmp(Ra0, "bu00:") == 0 && Config.Mcd1[0] != '\0')
1162         {
1163                 CreateMcd(Config.Mcd1);
1164                 LoadMcd(1, Config.Mcd1);
1165                 v0 = 1;
1166         }
1167         else if (strcmp(Ra0, "bu10:") == 0 && Config.Mcd2[0] != '\0')
1168         {
1169                 CreateMcd(Config.Mcd2);
1170                 LoadMcd(2, Config.Mcd2);
1171                 v0 = 1;
1172         }
1173         else
1174         {
1175                 v0 = 0;
1176         }
1177         pc0 = ra;
1178 }
1179
1180 /*
1181  *      long Load(char *name, struct EXEC *header);
1182  */
1183
1184 void psxBios_Load() { // 0x42
1185         EXE_HEADER eheader;
1186         void *pa1;
1187
1188 #ifdef PSXBIOS_LOG
1189         PSXBIOS_LOG("psxBios_%s: %s, %x\n", biosA0n[0x42], Ra0, a1);
1190 #endif
1191
1192         pa1 = Ra1;
1193         if (pa1 && LoadCdromFile(Ra0, &eheader) == 0) {
1194                 memcpy(pa1, ((char*)&eheader)+16, sizeof(EXEC));
1195                 v0 = 1;
1196         } else v0 = 0;
1197
1198         pc0 = ra;
1199 }
1200
1201 /*
1202  *      int Exec(struct EXEC *header , int argc , char **argv);
1203  */
1204
1205 void psxBios_Exec() { // 43
1206         EXEC *header = (EXEC*)Ra0;
1207         u32 tmp;
1208
1209 #ifdef PSXBIOS_LOG
1210         PSXBIOS_LOG("psxBios_%s: %x, %x, %x\n", biosA0n[0x43], a0, a1, a2);
1211 #endif
1212
1213         header->_sp = sp;
1214         header->_fp = fp;
1215         header->_sp = sp;
1216         header->_gp = gp;
1217         header->ret = ra;
1218         header->base = s0;
1219
1220         if (header->S_addr != 0) {
1221                 tmp = header->S_addr + header->s_size;
1222                 sp = tmp;
1223                 fp = sp;
1224         }
1225
1226         gp = header->gp0;
1227
1228         s0 = a0;
1229
1230         a0 = a1;
1231         a1 = a2;
1232
1233         ra = 0x8000;
1234         pc0 = header->_pc0;
1235 }
1236
1237 void psxBios_FlushCache() { // 44
1238 #ifdef PSXBIOS_LOG
1239         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x44]);
1240 #endif
1241
1242         pc0 = ra;
1243 }
1244
1245 void psxBios_GPU_dw() { // 0x46
1246         int size;
1247         s32 *ptr;
1248
1249 #ifdef PSXBIOS_LOG
1250         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x46]);
1251 #endif
1252
1253         GPU_writeData(0xa0000000);
1254         GPU_writeData((a1<<16)|(a0&0xffff));
1255         GPU_writeData((a3<<16)|(a2&0xffff));
1256         size = (a2*a3+1)/2;
1257         ptr = (s32*)PSXM(Rsp[4]);  //that is correct?
1258         do {
1259                 GPU_writeData(SWAP32(*ptr));
1260                 ptr++;
1261         } while(--size);
1262
1263         pc0 = ra;
1264 }  
1265
1266 void psxBios_mem2vram() { // 0x47
1267         int size;
1268
1269         GPU_writeData(0xa0000000);
1270         GPU_writeData((a1<<16)|(a0&0xffff));
1271         GPU_writeData((a3<<16)|(a2&0xffff));
1272         size = (a2*a3+1)/2;
1273         GPU_writeStatus(0x04000002);
1274         psxHwWrite32(0x1f8010f4,0);
1275         psxHwWrite32(0x1f8010f0,psxHwRead32(0x1f8010f0)|0x800);
1276         psxHwWrite32(0x1f8010a0,Rsp[4]);//might have a buggy...
1277         psxHwWrite32(0x1f8010a4,((size/16)<<16)|16);
1278         psxHwWrite32(0x1f8010a8,0x01000201);
1279
1280         pc0 = ra;
1281 }
1282
1283 void psxBios_SendGPU() { // 0x48
1284         GPU_writeStatus(a0);
1285         gpuSyncPluginSR();
1286         pc0 = ra;
1287 }
1288
1289 void psxBios_GPU_cw() { // 0x49
1290         GPU_writeData(a0);
1291         pc0 = ra;
1292 }
1293
1294 void psxBios_GPU_cwb() { // 0x4a
1295         s32 *ptr = (s32*)Ra0;
1296         int size = a1;
1297         while(size--) {
1298                 GPU_writeData(SWAP32(*ptr));
1299                 ptr++;
1300         }
1301
1302         pc0 = ra;
1303 }
1304    
1305 void psxBios_GPU_SendPackets() { //4b:  
1306         GPU_writeStatus(0x04000002);
1307         psxHwWrite32(0x1f8010f4,0);
1308         psxHwWrite32(0x1f8010f0,psxHwRead32(0x1f8010f0)|0x800);
1309         psxHwWrite32(0x1f8010a0,a0);
1310         psxHwWrite32(0x1f8010a4,0);
1311         psxHwWrite32(0x1f8010a8,0x010000401);
1312         pc0 = ra;
1313 }
1314
1315 void psxBios_sys_a0_4c() { // 0x4c GPU relate
1316         psxHwWrite32(0x1f8010a8,0x00000401);
1317         GPU_writeData(0x0400000);
1318         GPU_writeData(0x0200000);
1319         GPU_writeData(0x0100000);
1320         v0 = 0x1f801814;
1321         pc0 = ra;
1322 }
1323
1324 void psxBios_GPU_GetGPUStatus() { // 0x4d
1325         v0 = GPU_readStatus();
1326         pc0 = ra;
1327 }
1328
1329 #undef s_addr
1330
1331 void psxBios_LoadExec() { // 51
1332         EXEC *header = (EXEC*)PSXM(0xf000);
1333         u32 s_addr, s_size;
1334
1335 #ifdef PSXBIOS_LOG
1336         PSXBIOS_LOG("psxBios_%s: %s: %x,%x\n", biosA0n[0x51], Ra0, a1, a2);
1337 #endif
1338         s_addr = a1; s_size = a2;
1339
1340         a1 = 0xf000;    
1341         psxBios_Load();
1342
1343         header->S_addr = s_addr;
1344         header->s_size = s_size;
1345
1346         a0 = 0xf000; a1 = 0; a2 = 0;
1347         psxBios_Exec();
1348 }
1349
1350 void psxBios__bu_init() { // 70
1351 #ifdef PSXBIOS_LOG
1352         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x70]);
1353 #endif
1354
1355         DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
1356         DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
1357
1358         pc0 = ra;
1359 }
1360
1361 void psxBios__96_init() { // 71
1362 #ifdef PSXBIOS_LOG
1363         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x71]);
1364 #endif
1365
1366         pc0 = ra;
1367 }
1368
1369 void psxBios__96_remove() { // 72
1370 #ifdef PSXBIOS_LOG
1371         PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x72]);
1372 #endif
1373
1374         pc0 = ra;
1375 }
1376
1377 void psxBios_SetMem() { // 9f
1378         u32 new = psxHu32(0x1060);
1379
1380 #ifdef PSXBIOS_LOG
1381         PSXBIOS_LOG("psxBios_%s: %x, %x\n", biosA0n[0x9f], a0, a1);
1382 #endif
1383
1384         switch(a0) {
1385                 case 2:
1386                         psxHu32ref(0x1060) = SWAP32(new);
1387                         psxMu32ref(0x060) = a0;
1388                         SysPrintf("Change effective memory : %d MBytes\n",a0);
1389                         break;
1390
1391                 case 8:
1392                         psxHu32ref(0x1060) = SWAP32(new | 0x300);
1393                         psxMu32ref(0x060) = a0;
1394                         SysPrintf("Change effective memory : %d MBytes\n",a0);
1395         
1396                 default:
1397                         SysPrintf("Effective memory must be 2/8 MBytes\n");
1398                 break;
1399         }
1400
1401         pc0 = ra;
1402 }
1403
1404 void psxBios__card_info() { // ab
1405 #ifdef PSXBIOS_LOG
1406         PSXBIOS_LOG("psxBios_%s: %x\n", biosA0n[0xab], a0);
1407 #endif
1408         u32 ret;
1409         card_active_chan = a0;
1410
1411         switch (card_active_chan) 
1412         {
1413         case 0x00: case 0x01: case 0x02: case 0x03:
1414                 ret = Config.Mcd1[0] ? 0x2 : 0x8;
1415                 break;
1416         case 0x10: case 0x11: case 0x12: case 0x13:
1417                 ret = Config.Mcd2[0] ? 0x2 : 0x8;
1418                 break;
1419         default:
1420 #ifdef PSXBIOS_LOG
1421                 PSXBIOS_LOG("psxBios_%s: UNKNOWN PORT 0x%x\n", biosA0n[0xab], card_active_chan);
1422 #endif
1423                 ret = 0x11;
1424                 break;
1425         }
1426         
1427 //      DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
1428 //      DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
1429         DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
1430         DeliverEvent(0x81, ret); // 0xf4000001, 0x0004
1431
1432         v0 = 1; pc0 = ra;
1433 }
1434
1435 void psxBios__card_load() { // ac
1436 #ifdef PSXBIOS_LOG
1437         PSXBIOS_LOG("psxBios_%s: %x\n", biosA0n[0xac], a0);
1438 #endif
1439
1440         card_active_chan = a0;
1441
1442 //      DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
1443         DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
1444
1445         v0 = 1; pc0 = ra;
1446 }
1447
1448 /* System calls B0 */
1449
1450 void psxBios_SetRCnt() { // 02
1451 #ifdef PSXBIOS_LOG
1452         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x02]);
1453 #endif
1454
1455         a0&= 0x3;
1456         if (a0 != 3) {
1457                 u32 mode=0;
1458
1459                 psxRcntWtarget(a0, a1);
1460                 if (a2&0x1000) mode|= 0x050; // Interrupt Mode
1461                 if (a2&0x0100) mode|= 0x008; // Count to 0xffff
1462                 if (a2&0x0010) mode|= 0x001; // Timer stop mode
1463                 if (a0 == 2) { if (a2&0x0001) mode|= 0x200; } // System Clock mode
1464                 else         { if (a2&0x0001) mode|= 0x100; } // System Clock mode
1465
1466                 psxRcntWmode(a0, mode);
1467         }
1468         pc0 = ra;
1469 }
1470
1471 void psxBios_GetRCnt() { // 03
1472 #ifdef PSXBIOS_LOG
1473         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x03]);
1474 #endif
1475
1476         a0&= 0x3;
1477         if (a0 != 3) v0 = psxRcntRcount(a0);
1478         else v0 = 0;
1479         pc0 = ra;
1480 }
1481
1482 void psxBios_StartRCnt() { // 04
1483 #ifdef PSXBIOS_LOG
1484         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x04]);
1485 #endif
1486
1487         a0&= 0x3;
1488         if (a0 != 3) psxHu32ref(0x1074)|= SWAP32((u32)((1<<(a0+4))));
1489         else psxHu32ref(0x1074)|= SWAPu32(0x1);
1490         v0 = 1; pc0 = ra;
1491 }
1492
1493 void psxBios_StopRCnt() { // 05
1494 #ifdef PSXBIOS_LOG
1495         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x05]);
1496 #endif
1497
1498         a0&= 0x3;
1499         if (a0 != 3) psxHu32ref(0x1074)&= SWAP32((u32)(~(1<<(a0+4))));
1500         else psxHu32ref(0x1074)&= SWAPu32(~0x1);
1501         pc0 = ra;
1502 }
1503
1504 void psxBios_ResetRCnt() { // 06
1505 #ifdef PSXBIOS_LOG
1506         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x06]);
1507 #endif
1508
1509         a0&= 0x3;
1510         if (a0 != 3) {
1511                 psxRcntWmode(a0, 0);
1512                 psxRcntWtarget(a0, 0);
1513                 psxRcntWcount(a0, 0);
1514         }
1515         pc0 = ra;
1516 }
1517
1518
1519 /* gets ev for use with Event */
1520 #define GetEv() \
1521         ev = (a0 >> 24) & 0xf; \
1522         if (ev == 0xf) ev = 0x5; \
1523         ev*= 32; \
1524         ev+= a0&0x1f;
1525
1526 /* gets spec for use with Event */
1527 #define GetSpec() \
1528         spec = 0; \
1529         switch (a1) { \
1530                 case 0x0301: spec = 16; break; \
1531                 case 0x0302: spec = 17; break; \
1532                 default: \
1533                         for (i=0; i<16; i++) if (a1 & (1 << i)) { spec = i; break; } \
1534                         break; \
1535         }
1536
1537 void psxBios_DeliverEvent() { // 07
1538         int ev, spec;
1539         int i;
1540
1541         GetEv();
1542         GetSpec();
1543
1544 #ifdef PSXBIOS_LOG
1545         PSXBIOS_LOG("psxBios_%s %x,%x\n", biosB0n[0x07], ev, spec);
1546 #endif
1547
1548         DeliverEvent(ev, spec);
1549
1550         pc0 = ra;
1551 }
1552
1553 void psxBios_OpenEvent() { // 08
1554         int ev, spec;
1555         int i;
1556
1557         GetEv();
1558         GetSpec();
1559
1560 #ifdef PSXBIOS_LOG
1561         PSXBIOS_LOG("psxBios_%s %x,%x (class:%x, spec:%x, mode:%x, func:%x)\n", biosB0n[0x08], ev, spec, a0, a1, a2, a3);
1562 #endif
1563
1564         Event[ev][spec].status = EvStWAIT;
1565         Event[ev][spec].mode = a2;
1566         Event[ev][spec].fhandler = a3;
1567
1568         v0 = ev | (spec << 8);
1569         pc0 = ra;
1570 }
1571
1572 void psxBios_CloseEvent() { // 09
1573         int ev, spec;
1574
1575         ev   = a0 & 0xff;
1576         spec = (a0 >> 8) & 0xff;
1577
1578 #ifdef PSXBIOS_LOG
1579         PSXBIOS_LOG("psxBios_%s %x,%x\n", biosB0n[0x09], ev, spec);
1580 #endif
1581
1582         Event[ev][spec].status = EvStUNUSED;
1583
1584         v0 = 1; pc0 = ra;
1585 }
1586
1587 void psxBios_WaitEvent() { // 0a
1588         int ev, spec;
1589
1590         ev   = a0 & 0xff;
1591         spec = (a0 >> 8) & 0xff;
1592 #ifdef PSXBIOS_LOG
1593         PSXBIOS_LOG("psxBios_%s %x,%x\n", biosB0n[0x0a], ev, spec);
1594 #endif
1595         if (Event[ev][spec].status == EvStUNUSED)
1596         {
1597                 v0 = 0;
1598                 pc0 = ra;       
1599                 return;
1600         }
1601
1602         if (Event[ev][spec].status == EvStALREADY) 
1603         {
1604                 /* Callback events (mode=EvMdINTR) do never set the ready flag (and thus WaitEvent would hang forever). */
1605                 if (!(Event[ev][spec].mode == EvMdINTR)) Event[ev][spec].status = EvStACTIVE;
1606                 v0 = 1;
1607                 pc0 = ra;
1608                 return;
1609         }
1610
1611         v0 = 0;
1612         pc0 = ra;
1613 }
1614
1615 void psxBios_TestEvent() { // 0b
1616         int ev, spec;
1617
1618         ev   = a0 & 0xff;
1619         spec = (a0 >> 8) & 0xff;
1620
1621         if (Event[ev][spec].status == EvStALREADY) {
1622                 Event[ev][spec].status = EvStACTIVE; v0 = 1;
1623         } else v0 = 0;
1624
1625 #ifdef PSXBIOS_LOG
1626         PSXBIOS_LOG("psxBios_%s %x,%x: %x\n", biosB0n[0x0b], ev, spec, v0);
1627 #endif
1628
1629         pc0 = ra;
1630 }
1631
1632 void psxBios_EnableEvent() { // 0c
1633         int ev, spec;
1634
1635         ev   = a0 & 0xff;
1636         spec = (a0 >> 8) & 0xff;
1637
1638 #ifdef PSXBIOS_LOG
1639         PSXBIOS_LOG("psxBios_%s %x,%x\n", biosB0n[0x0c], ev, spec);
1640 #endif
1641
1642         Event[ev][spec].status = EvStACTIVE;
1643
1644         v0 = 1; pc0 = ra;
1645 }
1646
1647 void psxBios_DisableEvent() { // 0d
1648         int ev, spec;
1649
1650         ev   = a0 & 0xff;
1651         spec = (a0 >> 8) & 0xff;
1652
1653 #ifdef PSXBIOS_LOG
1654         PSXBIOS_LOG("psxBios_%s %x,%x\n", biosB0n[0x0d], ev, spec);
1655 #endif
1656
1657         Event[ev][spec].status = EvStWAIT;
1658
1659         v0 = 1; pc0 = ra;
1660 }
1661
1662 /*
1663  *      long OpenTh(long (*func)(), unsigned long sp, unsigned long gp);
1664  */
1665
1666 void psxBios_OpenTh() { // 0e
1667         int th;
1668
1669         for (th=1; th<8; th++)
1670         {
1671                 if (Thread[th].status == 0) break;
1672
1673         }
1674         if (th == 8) {
1675                 // Feb 2019 - Added out-of-bounds fix caught by cppcheck:
1676                 // When no free TCB is found, return 0xffffffff according to Nocash doc.
1677 #ifdef PSXBIOS_LOG
1678                 PSXBIOS_LOG("\t%s() WARNING! No Free TCBs found!\n", __func__);
1679 #endif
1680                 v0 = 0xffffffff;
1681                 pc0 = ra;
1682                 return;
1683         }
1684 #ifdef PSXBIOS_LOG
1685         PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x0e], th);
1686 #endif
1687
1688         Thread[th].status = 1;
1689         Thread[th].func    = a0;
1690         Thread[th].reg[29] = a1;
1691         Thread[th].reg[28] = a2;
1692
1693         v0 = th; pc0 = ra;
1694 }
1695
1696 /*
1697  *      int CloseTh(long thread);
1698  */
1699
1700 void psxBios_CloseTh() { // 0f
1701         int th = a0 & 0xff;
1702
1703 #ifdef PSXBIOS_LOG
1704         PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x0f], th);
1705 #endif
1706
1707         if (Thread[th].status == 0) {
1708                 v0 = 0;
1709         } else {
1710                 Thread[th].status = 0;
1711                 v0 = 1;
1712         }
1713
1714         pc0 = ra;
1715 }
1716
1717 /*
1718  *      int ChangeTh(long thread);
1719  */
1720
1721 void psxBios_ChangeTh() { // 10
1722         int th = a0 & 0xff;
1723
1724 #ifdef PSXBIOS_LOG
1725 //      PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x10], th);
1726 #endif
1727
1728         if (Thread[th].status == 0 || CurThread == th) {
1729                 v0 = 0;
1730
1731                 pc0 = ra;
1732         } else {
1733                 v0 = 1;
1734
1735                 if (Thread[CurThread].status == 2) {
1736                         Thread[CurThread].status = 1;
1737                         Thread[CurThread].func = ra;
1738                         memcpy(Thread[CurThread].reg, psxRegs.GPR.r, 32*4);
1739                 }
1740
1741                 memcpy(psxRegs.GPR.r, Thread[th].reg, 32*4);
1742                 pc0 = Thread[th].func;
1743                 Thread[th].status = 2;
1744                 CurThread = th;
1745         }
1746 }
1747
1748 void psxBios_InitPAD() { // 0x12
1749 #ifdef PSXBIOS_LOG
1750         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x12]);
1751 #endif
1752
1753         pad_buf1 = (char*)Ra0;
1754         pad_buf1len = a1;
1755         pad_buf2 = (char*)Ra2;
1756         pad_buf2len = a3;
1757
1758         v0 = 1; pc0 = ra;
1759 }
1760
1761 void psxBios_StartPAD() { // 13
1762 #ifdef PSXBIOS_LOG
1763         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x13]);
1764 #endif
1765
1766         psxHwWrite16(0x1f801074, (unsigned short)(psxHwRead16(0x1f801074) | 0x1));
1767         psxRegs.CP0.n.Status |= 0x401;
1768         pc0 = ra;
1769 }
1770
1771 void psxBios_StopPAD() { // 14
1772 #ifdef PSXBIOS_LOG
1773         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x14]);
1774 #endif
1775
1776         pad_buf1 = NULL;
1777         pad_buf2 = NULL;
1778         pc0 = ra;
1779 }
1780
1781 void psxBios_PAD_init() { // 15
1782 #ifdef PSXBIOS_LOG
1783         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x15]);
1784 #endif
1785         if (!(a0 == 0x20000000 || a0 == 0x20000001))
1786         {
1787                 v0 = 0;
1788                 pc0 = ra;
1789                 return;
1790         }
1791         psxHwWrite16(0x1f801074, (u16)(psxHwRead16(0x1f801074) | 0x1));
1792         pad_buf = (int *)Ra1;
1793         *pad_buf = -1;
1794         psxRegs.CP0.n.Status |= 0x401;
1795         v0 = 2;
1796         pc0 = ra;
1797 }
1798
1799 void psxBios_PAD_dr() { // 16
1800 #ifdef PSXBIOS_LOG
1801         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x16]);
1802 #endif
1803
1804         v0 = -1; pc0 = ra;
1805 }
1806
1807 void psxBios_ReturnFromException() { // 17
1808         LoadRegs();
1809
1810         pc0 = psxRegs.CP0.n.EPC;
1811         if (psxRegs.CP0.n.Cause & 0x80000000) pc0 += 4;
1812
1813         psxRegs.CP0.n.Status = (psxRegs.CP0.n.Status & 0xfffffff0) |
1814                                                   ((psxRegs.CP0.n.Status & 0x3c) >> 2);
1815 }
1816
1817 void psxBios_ResetEntryInt() { // 18
1818 #ifdef PSXBIOS_LOG
1819         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x18]);
1820 #endif
1821
1822         jmp_int = NULL;
1823         pc0 = ra;
1824 }
1825
1826 void psxBios_HookEntryInt() { // 19
1827 #ifdef PSXBIOS_LOG
1828         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x19]);
1829 #endif
1830
1831         jmp_int = (u32*)Ra0;
1832         pc0 = ra;
1833 }
1834
1835 void psxBios_UnDeliverEvent() { // 0x20
1836         int ev, spec;
1837         int i;
1838
1839         GetEv();
1840         GetSpec();
1841
1842 #ifdef PSXBIOS_LOG
1843         PSXBIOS_LOG("psxBios_%s %x,%x\n", biosB0n[0x20], ev, spec);
1844 #endif
1845
1846         if (Event[ev][spec].status == EvStALREADY &&
1847                 Event[ev][spec].mode == EvMdNOINTR)
1848                 Event[ev][spec].status = EvStACTIVE;
1849
1850         pc0 = ra;
1851 }
1852
1853 char ffile[64], *pfile;
1854 int nfile;
1855 static void buopen(int mcd, u8 *ptr, u8 *cfg)
1856 {
1857         int i;
1858         u8 *fptr = ptr;
1859
1860         strcpy(FDesc[1 + mcd].name, Ra0+5);
1861         FDesc[1 + mcd].offset = 0;
1862         FDesc[1 + mcd].mode   = a1;
1863
1864         for (i=1; i<16; i++) {
1865                 fptr += 128;
1866                 if ((*fptr & 0xF0) != 0x50) continue;
1867                 if (strcmp(FDesc[1 + mcd].name, fptr+0xa)) continue;
1868                 FDesc[1 + mcd].mcfile = i;
1869                 SysPrintf("open %s\n", fptr+0xa);
1870                 v0 = 1 + mcd;
1871                 break;
1872         }
1873         if (a1 & 0x200 && v0 == -1) { /* FCREAT */
1874                 fptr = ptr;
1875                 for (i=1; i<16; i++) {
1876                         int j, xor, nblk = a1 >> 16;
1877                         u8 *pptr, *fptr2;
1878
1879                         fptr += 128;
1880                         if ((*fptr & 0xF0) != 0xa0) continue;
1881
1882                         FDesc[1 + mcd].mcfile = i;
1883                         fptr[0] = 0x51;
1884                         fptr[4] = 0x00;
1885                         fptr[5] = 0x20 * nblk;
1886                         fptr[6] = 0x00;
1887                         fptr[7] = 0x00;
1888                         strcpy(fptr+0xa, FDesc[1 + mcd].name);
1889                         pptr = fptr2 = fptr;
1890                         for(j=2; j<=nblk; j++) {
1891                                 int k;
1892                                 for(i++; i<16; i++) {
1893                                         fptr2 += 128;
1894                                         
1895                                         memset(fptr2, 0, 128);
1896                                         fptr2[0] = j < nblk ? 0x52 : 0x53;
1897                                         pptr[8] = i - 1;
1898                                         pptr[9] = 0;
1899                                         for (k=0, xor=0; k<127; k++) xor^= pptr[k];
1900                                         pptr[127] = xor;
1901                                         pptr = fptr2;
1902                                         break;
1903                                 }
1904                                 /* shouldn't this return ENOSPC if i == 16? */
1905                         }
1906                         pptr[8] = pptr[9] = 0xff;
1907                         for (j=0, xor=0; j<127; j++) xor^= pptr[j];
1908                         pptr[127] = xor;
1909                         SysPrintf("openC %s %d\n", ptr, nblk);
1910                         v0 = 1 + mcd;
1911                         /* just go ahead and resave them all */
1912                         SaveMcd(cfg, ptr, 128, 128 * 15);
1913                         break;
1914                 }
1915                 /* shouldn't this return ENOSPC if i == 16? */
1916         }
1917 }
1918
1919 /*
1920  *      int open(char *name , int mode);
1921  */
1922
1923 void psxBios_open() { // 0x32
1924         int i;
1925         char *ptr;
1926         void *pa0 = Ra0;
1927
1928 #ifdef PSXBIOS_LOG
1929         PSXBIOS_LOG("psxBios_%s: %s,%x\n", biosB0n[0x32], Ra0, a1);
1930 #endif
1931
1932         v0 = -1;
1933
1934         if (pa0) {
1935                 if (!strncmp(pa0, "bu00", 4)) {
1936                         buopen(1, Mcd1Data, Config.Mcd1);
1937                 }
1938
1939                 if (!strncmp(pa0, "bu10", 4)) {
1940                         buopen(2, Mcd2Data, Config.Mcd2);
1941                 }
1942         }
1943
1944         pc0 = ra;
1945 }
1946
1947 /*
1948  *      int lseek(int fd , int offset , int whence);
1949  */
1950
1951 void psxBios_lseek() { // 0x33
1952 #ifdef PSXBIOS_LOG
1953         PSXBIOS_LOG("psxBios_%s: %x, %x, %x\n", biosB0n[0x33], a0, a1, a2);
1954 #endif
1955
1956         switch (a2) {
1957                 case 0: // SEEK_SET
1958                         FDesc[a0].offset = a1;
1959                         v0 = a1;
1960 //                      DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
1961 //                      DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
1962                         break;
1963
1964                 case 1: // SEEK_CUR
1965                         FDesc[a0].offset+= a1;
1966                         v0 = FDesc[a0].offset;
1967                         break;
1968         }
1969
1970         pc0 = ra;
1971 }
1972
1973
1974 /*
1975  *      int read(int fd , void *buf , int nbytes);
1976  */
1977
1978 void psxBios_read() { // 0x34
1979         char *ptr;
1980         void *pa1 = Ra1;
1981
1982 #ifdef PSXBIOS_LOG
1983         PSXBIOS_LOG("psxBios_%s: %x, %x, %x\n", biosB0n[0x34], a0, a1, a2);
1984 #endif
1985
1986         v0 = -1;
1987
1988         if (pa1) {
1989                 switch (a0) {
1990                         case 2: buread(pa1, 1, a2); break;
1991                         case 3: buread(pa1, 2, a2); break;
1992                 }
1993         }
1994                 
1995         pc0 = ra;
1996 }
1997
1998 /*
1999  *      int write(int fd , void *buf , int nbytes);
2000  */
2001
2002 void psxBios_write() { // 0x35/0x03
2003         char *ptr;
2004         void *pa1 = Ra1;
2005
2006 #ifdef PSXBIOS_LOG
2007         PSXBIOS_LOG("psxBios_%s: %x,%x,%x\n", biosB0n[0x35], a0, a1, a2);
2008 #endif
2009
2010         v0 = -1;
2011         if (!pa1) {
2012                 pc0 = ra;
2013                 return;
2014         }
2015
2016         if (a0 == 1) { // stdout
2017                 char *ptr = pa1;
2018
2019                 v0 = a2;
2020                 while (a2 > 0) {
2021                         SysPrintf("%c", *ptr++); a2--;
2022                 }
2023                 pc0 = ra; return;
2024         }
2025
2026         switch (a0) {
2027                 case 2: buwrite(pa1, 1, a2); break;
2028                 case 3: buwrite(pa1, 2, a2); break;
2029         }
2030
2031         pc0 = ra;
2032 }
2033
2034 /*
2035  *      int close(int fd);
2036  */
2037
2038 void psxBios_close() { // 0x36
2039 #ifdef PSXBIOS_LOG
2040         PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x36], a0);
2041 #endif
2042
2043         v0 = a0;
2044         pc0 = ra;
2045 }
2046
2047 void psxBios_putchar() { // 3d
2048         SysPrintf("%c", (char)a0);
2049         pc0 = ra;
2050 }
2051
2052 void psxBios_puts() { // 3e/3f
2053         SysPrintf("%s", Ra0);
2054         pc0 = ra;
2055 }
2056
2057 char ffile[64], *pfile;
2058 int nfile;
2059
2060 #define bufile(mcd) { \
2061         while (nfile < 16) { \
2062                 int match=1; \
2063  \
2064                 ptr = Mcd##mcd##Data + 128 * (nfile + 1); \
2065                 nfile++; \
2066                 if ((*ptr & 0xF0) != 0x50) continue; \
2067                 /* Bug link files show up as free block. */ \
2068                 if (!ptr[0xa]) continue; \
2069                 ptr+= 0xa; \
2070                 if (pfile[0] == 0) { \
2071                         strncpy(dir->name, ptr, sizeof(dir->name)); \
2072                         dir->name[sizeof(dir->name) - 1] = '\0'; \
2073                 } else for (i=0; i<20; i++) { \
2074                         if (pfile[i] == ptr[i]) { \
2075                                                                 dir->name[i] = ptr[i]; continue; } \
2076                         if (pfile[i] == '?') { \
2077                                 dir->name[i] = ptr[i]; continue; } \
2078                         if (pfile[i] == '*') { \
2079                                 strcpy(dir->name+i, ptr+i); break; } \
2080                         match = 0; break; \
2081                 } \
2082                 SysPrintf("%d : %s = %s + %s (match=%d)\n", nfile, dir->name, pfile, ptr, match); \
2083                 if (match == 0) { continue; } \
2084                 dir->size = 8192; \
2085                 v0 = _dir; \
2086                 break; \
2087         } \
2088 }
2089
2090 /*
2091  *      struct DIRENTRY* firstfile(char *name,struct DIRENTRY *dir);
2092  */
2093  
2094 void psxBios_firstfile() { // 42
2095         struct DIRENTRY *dir = (struct DIRENTRY *)Ra1;
2096         void *pa0 = Ra0;
2097         u32 _dir = a1;
2098         char *ptr;
2099         int i;
2100
2101 #ifdef PSXBIOS_LOG
2102         PSXBIOS_LOG("psxBios_%s: %s\n", biosB0n[0x42], Ra0);
2103 #endif
2104
2105         v0 = 0;
2106
2107         if (pa0) {
2108                 strcpy(ffile, pa0);
2109                 pfile = ffile+5;
2110                 nfile = 1;
2111                 if (!strncmp(pa0, "bu00", 4)) {
2112                         bufile(1);
2113                 } else if (!strncmp(pa0, "bu10", 4)) {
2114                         bufile(2);
2115                 }
2116         }
2117
2118         // firstfile() calls _card_read() internally, so deliver it's event
2119         DeliverEvent(0x11, 0x2);
2120
2121         pc0 = ra;
2122 }
2123
2124 /*
2125  *      struct DIRENTRY* nextfile(struct DIRENTRY *dir);
2126  */
2127
2128 void psxBios_nextfile() { // 43
2129         struct DIRENTRY *dir = (struct DIRENTRY *)Ra0;
2130         u32 _dir = a0;
2131         char *ptr;
2132         int i;
2133
2134 #ifdef PSXBIOS_LOG
2135         PSXBIOS_LOG("psxBios_%s: %s\n", biosB0n[0x43], dir->name);
2136 #endif
2137
2138         v0 = 0;
2139
2140         if (!strncmp(ffile, "bu00", 4)) {
2141                 bufile(1);
2142         }
2143
2144         if (!strncmp(ffile, "bu10", 4)) {
2145                 bufile(2);
2146         }
2147
2148         pc0 = ra;
2149 }
2150
2151 #define burename(mcd) { \
2152         for (i=1; i<16; i++) { \
2153                 int namelen, j, xor = 0; \
2154                 ptr = Mcd##mcd##Data + 128 * i; \
2155                 if ((*ptr & 0xF0) != 0x50) continue; \
2156                 if (strcmp(Ra0+5, ptr+0xa)) continue; \
2157                 namelen = strlen(Ra1+5); \
2158                 memcpy(ptr+0xa, Ra1+5, namelen); \
2159                 memset(ptr+0xa+namelen, 0, 0x75-namelen); \
2160                 for (j=0; j<127; j++) xor^= ptr[j]; \
2161                 ptr[127] = xor; \
2162                 SaveMcd(Config.Mcd##mcd, Mcd##mcd##Data, 128 * i + 0xa, 0x76); \
2163                 v0 = 1; \
2164                 break; \
2165         } \
2166 }
2167
2168 /*
2169  *      int rename(char *old, char *new);
2170  */
2171
2172 void psxBios_rename() { // 44
2173         void *pa0 = Ra0;
2174         void *pa1 = Ra1;
2175         char *ptr;
2176         int i;
2177
2178 #ifdef PSXBIOS_LOG
2179         PSXBIOS_LOG("psxBios_%s: %s,%s\n", biosB0n[0x44], Ra0, Ra1);
2180 #endif
2181
2182         v0 = 0;
2183
2184         if (pa0 && pa1) {
2185                 if (!strncmp(pa0, "bu00", 4) && !strncmp(pa1, "bu00", 4)) {
2186                         burename(1);
2187                 }
2188
2189                 if (!strncmp(pa0, "bu10", 4) && !strncmp(pa1, "bu10", 4)) {
2190                         burename(2);
2191                 }
2192         }
2193
2194         pc0 = ra;
2195 }
2196
2197
2198 #define budelete(mcd) { \
2199         for (i=1; i<16; i++) { \
2200                 ptr = Mcd##mcd##Data + 128 * i; \
2201                 if ((*ptr & 0xF0) != 0x50) continue; \
2202                 if (strcmp(Ra0+5, ptr+0xa)) continue; \
2203                 *ptr = (*ptr & 0xf) | 0xA0; \
2204                 SaveMcd(Config.Mcd##mcd, Mcd##mcd##Data, 128 * i, 1); \
2205                 SysPrintf("delete %s\n", ptr+0xa); \
2206                 v0 = 1; \
2207                 break; \
2208         } \
2209 }
2210
2211 /*
2212  *      int delete(char *name);
2213  */
2214
2215 void psxBios_delete() { // 45
2216         void *pa0 = Ra0;
2217         char *ptr;
2218         int i;
2219
2220 #ifdef PSXBIOS_LOG
2221         PSXBIOS_LOG("psxBios_%s: %s\n", biosB0n[0x45], Ra0);
2222 #endif
2223
2224         v0 = 0;
2225
2226         if (pa0) {
2227                 if (!strncmp(pa0, "bu00", 4)) {
2228                         budelete(1);
2229                 }
2230
2231                 if (!strncmp(pa0, "bu10", 4)) {
2232                         budelete(2);
2233                 }
2234         }
2235
2236         pc0 = ra;
2237 }
2238
2239 void psxBios_InitCARD() { // 4a
2240 #ifdef PSXBIOS_LOG
2241         PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x4a], a0);
2242 #endif
2243
2244         CardState = 0;
2245
2246         pc0 = ra;
2247 }
2248
2249 void psxBios_StartCARD() { // 4b
2250 #ifdef PSXBIOS_LOG
2251         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x4b]);
2252 #endif
2253
2254         if (CardState == 0) CardState = 1;
2255
2256         pc0 = ra;
2257 }
2258
2259 void psxBios_StopCARD() { // 4c
2260 #ifdef PSXBIOS_LOG
2261         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x4c]);
2262 #endif
2263
2264         if (CardState == 1) CardState = 0;
2265
2266         pc0 = ra;
2267 }
2268
2269 void psxBios__card_write() { // 0x4e
2270         void *pa2 = Ra2;
2271         int port;
2272
2273 #ifdef PSXBIOS_LOG
2274         PSXBIOS_LOG("psxBios_%s: %x,%x,%x\n", biosB0n[0x4e], a0, a1, a2);
2275 #endif
2276         /* Function also accepts sector 400h (a bug) */
2277         if (!(a1 <= 0x400))
2278         {
2279                 /* Invalid sectors */
2280                 v0 = 0; pc0 = ra;
2281                 return;
2282         }
2283         card_active_chan = a0;
2284         port = a0 >> 4;
2285
2286         if (pa2) {
2287                 if (port == 0) {
2288                         memcpy(Mcd1Data + a1 * 128, pa2, 128);
2289                         SaveMcd(Config.Mcd1, Mcd1Data, a1 * 128, 128);
2290                 } else {
2291                         memcpy(Mcd2Data + a1 * 128, pa2, 128);
2292                         SaveMcd(Config.Mcd2, Mcd2Data, a1 * 128, 128);
2293                 }
2294         }
2295
2296         DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
2297 //      DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
2298
2299         v0 = 1; pc0 = ra;
2300 }
2301
2302 void psxBios__card_read() { // 0x4f
2303         void *pa2 = Ra2;
2304         int port;
2305
2306 #ifdef PSXBIOS_LOG
2307         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x4f]);
2308 #endif
2309         /* Function also accepts sector 400h (a bug) */
2310         if (!(a1 <= 0x400))
2311         {
2312                 /* Invalid sectors */
2313                 v0 = 0; pc0 = ra;
2314                 return;
2315         }
2316         card_active_chan = a0;
2317         port = a0 >> 4;
2318
2319         if (pa2) {
2320                 if (port == 0) {
2321                         memcpy(pa2, Mcd1Data + a1 * 128, 128);
2322                 } else {
2323                         memcpy(pa2, Mcd2Data + a1 * 128, 128);
2324                 }
2325         }
2326
2327         DeliverEvent(0x11, 0x2); // 0xf0000011, 0x0004
2328 //      DeliverEvent(0x81, 0x2); // 0xf4000001, 0x0004
2329
2330         v0 = 1; pc0 = ra;
2331 }
2332
2333 void psxBios__new_card() { // 0x50
2334 #ifdef PSXBIOS_LOG
2335         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x50]);
2336 #endif
2337
2338         pc0 = ra;
2339 }
2340
2341 void psxBios_Krom2RawAdd() { // 0x51
2342         int i = 0;
2343
2344         const u32 table_8140[][2] = {
2345                 {0x8140, 0x0000}, {0x8180, 0x0762}, {0x81ad, 0x0cc6}, {0x81b8, 0x0ca8},
2346                 {0x81c0, 0x0f00}, {0x81c8, 0x0d98}, {0x81cf, 0x10c2}, {0x81da, 0x0e6a},
2347                 {0x81e9, 0x13ce}, {0x81f0, 0x102c}, {0x81f8, 0x1590}, {0x81fc, 0x111c},
2348                 {0x81fd, 0x1626}, {0x824f, 0x113a}, {0x8259, 0x20ee}, {0x8260, 0x1266},
2349                 {0x827a, 0x24cc}, {0x8281, 0x1572}, {0x829b, 0x28aa}, {0x829f, 0x187e},
2350                 {0x82f2, 0x32dc}, {0x8340, 0x2238}, {0x837f, 0x4362}, {0x8380, 0x299a},
2351                 {0x8397, 0x4632}, {0x839f, 0x2c4c}, {0x83b7, 0x49f2}, {0x83bf, 0x2f1c},
2352                 {0x83d7, 0x4db2}, {0x8440, 0x31ec}, {0x8461, 0x5dde}, {0x8470, 0x35ca},
2353                 {0x847f, 0x6162}, {0x8480, 0x378c}, {0x8492, 0x639c}, {0x849f, 0x39a8},
2354                 {0xffff, 0}
2355         };
2356
2357         const u32 table_889f[][2] = {
2358                 {0x889f, 0x3d68},  {0x8900, 0x40ec},  {0x897f, 0x4fb0},  {0x8a00, 0x56f4},
2359                 {0x8a7f, 0x65b8},  {0x8b00, 0x6cfc},  {0x8b7f, 0x7bc0},  {0x8c00, 0x8304},
2360                 {0x8c7f, 0x91c8},  {0x8d00, 0x990c},  {0x8d7f, 0xa7d0},  {0x8e00, 0xaf14},
2361                 {0x8e7f, 0xbdd8},  {0x8f00, 0xc51c},  {0x8f7f, 0xd3e0},  {0x9000, 0xdb24},
2362                 {0x907f, 0xe9e8},  {0x9100, 0xf12c},  {0x917f, 0xfff0},  {0x9200, 0x10734},
2363                 {0x927f, 0x115f8}, {0x9300, 0x11d3c}, {0x937f, 0x12c00}, {0x9400, 0x13344},
2364                 {0x947f, 0x14208}, {0x9500, 0x1494c}, {0x957f, 0x15810}, {0x9600, 0x15f54},
2365                 {0x967f, 0x16e18}, {0x9700, 0x1755c}, {0x977f, 0x18420}, {0x9800, 0x18b64},
2366                 {0xffff, 0}
2367         };
2368
2369         if (a0 >= 0x8140 && a0 <= 0x84be) {
2370                 while (table_8140[i][0] <= a0) i++;
2371                 a0 -= table_8140[i - 1][0];
2372                 v0 = 0xbfc66000 + (a0 * 0x1e + table_8140[i - 1][1]);
2373         } else if (a0 >= 0x889f && a0 <= 0x9872) {
2374                 while (table_889f[i][0] <= a0) i++;
2375                 a0 -= table_889f[i - 1][0];
2376                 v0 = 0xbfc66000 + (a0 * 0x1e + table_889f[i - 1][1]);
2377         } else {
2378                 v0 = 0xffffffff;
2379         }
2380
2381         pc0 = ra;
2382 }
2383
2384 void psxBios_GetC0Table() { // 56
2385 #ifdef PSXBIOS_LOG
2386         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x56]);
2387 #endif
2388
2389         v0 = 0x674; pc0 = ra;
2390 }
2391
2392 void psxBios_GetB0Table() { // 57
2393 #ifdef PSXBIOS_LOG
2394         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x57]);
2395 #endif
2396
2397         v0 = 0x874; pc0 = ra;
2398 }
2399
2400 void psxBios__card_chan() { // 0x58
2401 #ifdef PSXBIOS_LOG
2402         PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x58]);
2403 #endif
2404
2405         v0 = card_active_chan;
2406         pc0 = ra;
2407 }
2408
2409 void psxBios_ChangeClearPad() { // 5b
2410 #ifdef PSXBIOS_LOG
2411         PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x5b], a0);
2412 #endif  
2413
2414         pc0 = ra;
2415 }
2416
2417 /* System calls C0 */
2418
2419 /*
2420  * int SysEnqIntRP(int index , long *queue);
2421  */
2422
2423 void psxBios_SysEnqIntRP() { // 02
2424 #ifdef PSXBIOS_LOG
2425         PSXBIOS_LOG("psxBios_%s: %x\n", biosC0n[0x02] ,a0);
2426 #endif
2427
2428         SysIntRP[a0] = a1;
2429
2430         v0 = 0; pc0 = ra;
2431 }
2432
2433 /*
2434  * int SysDeqIntRP(int index , long *queue);
2435  */
2436
2437 void psxBios_SysDeqIntRP() { // 03
2438 #ifdef PSXBIOS_LOG
2439         PSXBIOS_LOG("psxBios_%s: %x\n", biosC0n[0x03], a0);
2440 #endif
2441
2442         SysIntRP[a0] = 0;
2443
2444         v0 = 0; pc0 = ra;
2445 }
2446
2447 void psxBios_ChangeClearRCnt() { // 0a
2448         u32 *ptr;
2449
2450 #ifdef PSXBIOS_LOG
2451         PSXBIOS_LOG("psxBios_%s: %x, %x\n", biosC0n[0x0a], a0, a1);
2452 #endif
2453
2454         ptr = (u32*)PSXM((a0 << 2) + 0x8600);
2455         v0 = *ptr;
2456         *ptr = a1;
2457
2458 //      psxRegs.CP0.n.Status|= 0x404;
2459         pc0 = ra;
2460 }
2461
2462 void psxBios_dummy() { 
2463 #ifdef PSXBIOS_LOG
2464         PSXBIOS_LOG("unk %x call: %x\n", pc0 & 0x1fffff, t1);
2465 #endif
2466         pc0 = ra; 
2467 }
2468
2469 void (*biosA0[256])();
2470 void (*biosB0[256])();
2471 void (*biosC0[256])();
2472
2473 #include "sjisfont.h"
2474
2475 void psxBiosInit() {
2476         u32 base, size;
2477         u32 *ptr; 
2478         int i;
2479         uLongf len;
2480
2481         for(i = 0; i < 256; i++) {
2482                 biosA0[i] = NULL;
2483                 biosB0[i] = NULL;
2484                 biosC0[i] = NULL;
2485         }
2486         biosA0[0x3e] = psxBios_puts;
2487         biosA0[0x3f] = psxBios_printf;
2488
2489         biosB0[0x3d] = psxBios_putchar;
2490         biosB0[0x3f] = psxBios_puts;
2491
2492         if (!Config.HLE) return;
2493
2494         for(i = 0; i < 256; i++) {
2495                 if (biosA0[i] == NULL) biosA0[i] = psxBios_dummy;
2496                 if (biosB0[i] == NULL) biosB0[i] = psxBios_dummy;
2497                 if (biosC0[i] == NULL) biosC0[i] = psxBios_dummy;
2498         }
2499
2500         biosA0[0x00] = psxBios_open;
2501         biosA0[0x01] = psxBios_lseek;
2502         biosA0[0x02] = psxBios_read;
2503         biosA0[0x03] = psxBios_write;
2504         biosA0[0x04] = psxBios_close;
2505         //biosA0[0x05] = psxBios_ioctl;
2506         //biosA0[0x06] = psxBios_exit;
2507         //biosA0[0x07] = psxBios_sys_a0_07;
2508         biosA0[0x08] = psxBios_getc;
2509         biosA0[0x09] = psxBios_putc;
2510         biosA0[0x0a] = psxBios_todigit;
2511         //biosA0[0x0b] = psxBios_atof;
2512         //biosA0[0x0c] = psxBios_strtoul;
2513         //biosA0[0x0d] = psxBios_strtol;
2514         biosA0[0x0e] = psxBios_abs;
2515         biosA0[0x0f] = psxBios_labs;
2516         biosA0[0x10] = psxBios_atoi;
2517         biosA0[0x11] = psxBios_atol;
2518         //biosA0[0x12] = psxBios_atob;
2519         biosA0[0x13] = psxBios_setjmp;
2520         biosA0[0x14] = psxBios_longjmp;
2521         biosA0[0x15] = psxBios_strcat;
2522         biosA0[0x16] = psxBios_strncat;
2523         biosA0[0x17] = psxBios_strcmp;
2524         biosA0[0x18] = psxBios_strncmp;
2525         biosA0[0x19] = psxBios_strcpy;
2526         biosA0[0x1a] = psxBios_strncpy;
2527         biosA0[0x1b] = psxBios_strlen;
2528         biosA0[0x1c] = psxBios_index;
2529         biosA0[0x1d] = psxBios_rindex;
2530         biosA0[0x1e] = psxBios_strchr;
2531         biosA0[0x1f] = psxBios_strrchr;
2532         biosA0[0x20] = psxBios_strpbrk;
2533         biosA0[0x21] = psxBios_strspn;
2534         biosA0[0x22] = psxBios_strcspn;
2535         biosA0[0x23] = psxBios_strtok;
2536         biosA0[0x24] = psxBios_strstr;
2537         biosA0[0x25] = psxBios_toupper;
2538         biosA0[0x26] = psxBios_tolower;
2539         biosA0[0x27] = psxBios_bcopy;
2540         biosA0[0x28] = psxBios_bzero;
2541         biosA0[0x29] = psxBios_bcmp;
2542         biosA0[0x2a] = psxBios_memcpy;
2543         biosA0[0x2b] = psxBios_memset;
2544         biosA0[0x2c] = psxBios_memmove;
2545         biosA0[0x2d] = psxBios_memcmp;
2546         biosA0[0x2e] = psxBios_memchr;
2547         biosA0[0x2f] = psxBios_rand;
2548         biosA0[0x30] = psxBios_srand;
2549         biosA0[0x31] = psxBios_qsort;
2550         //biosA0[0x32] = psxBios_strtod;
2551         biosA0[0x33] = psxBios_malloc;
2552         biosA0[0x34] = psxBios_free;
2553         //biosA0[0x35] = psxBios_lsearch;
2554         //biosA0[0x36] = psxBios_bsearch;
2555         biosA0[0x37] = psxBios_calloc;
2556         biosA0[0x38] = psxBios_realloc;
2557         biosA0[0x39] = psxBios_InitHeap;
2558         //biosA0[0x3a] = psxBios__exit;
2559         biosA0[0x3b] = psxBios_getchar;
2560         biosA0[0x3c] = psxBios_putchar; 
2561         //biosA0[0x3d] = psxBios_gets;
2562         //biosA0[0x40] = psxBios_sys_a0_40;
2563         //biosA0[0x41] = psxBios_LoadTest;
2564         biosA0[0x42] = psxBios_Load;
2565         biosA0[0x43] = psxBios_Exec;
2566         biosA0[0x44] = psxBios_FlushCache;
2567         //biosA0[0x45] = psxBios_InstallInterruptHandler;
2568         biosA0[0x46] = psxBios_GPU_dw;
2569         biosA0[0x47] = psxBios_mem2vram;
2570         biosA0[0x48] = psxBios_SendGPU;
2571         biosA0[0x49] = psxBios_GPU_cw;
2572         biosA0[0x4a] = psxBios_GPU_cwb;
2573         biosA0[0x4b] = psxBios_GPU_SendPackets;
2574         biosA0[0x4c] = psxBios_sys_a0_4c;
2575         biosA0[0x4d] = psxBios_GPU_GetGPUStatus;
2576         //biosA0[0x4e] = psxBios_GPU_sync;      
2577         //biosA0[0x4f] = psxBios_sys_a0_4f;
2578         //biosA0[0x50] = psxBios_sys_a0_50;
2579         biosA0[0x51] = psxBios_LoadExec;
2580         //biosA0[0x52] = psxBios_GetSysSp;
2581         //biosA0[0x53] = psxBios_sys_a0_53;
2582         //biosA0[0x54] = psxBios__96_init_a54;
2583         //biosA0[0x55] = psxBios__bu_init_a55;
2584         //biosA0[0x56] = psxBios__96_remove_a56;
2585         //biosA0[0x57] = psxBios_sys_a0_57;
2586         //biosA0[0x58] = psxBios_sys_a0_58;
2587         //biosA0[0x59] = psxBios_sys_a0_59;
2588         //biosA0[0x5a] = psxBios_sys_a0_5a;
2589         //biosA0[0x5b] = psxBios_dev_tty_init;
2590         //biosA0[0x5c] = psxBios_dev_tty_open;
2591         //biosA0[0x5d] = psxBios_sys_a0_5d;
2592         //biosA0[0x5e] = psxBios_dev_tty_ioctl;
2593         //biosA0[0x5f] = psxBios_dev_cd_open;
2594         //biosA0[0x60] = psxBios_dev_cd_read;
2595         //biosA0[0x61] = psxBios_dev_cd_close;
2596         //biosA0[0x62] = psxBios_dev_cd_firstfile;
2597         //biosA0[0x63] = psxBios_dev_cd_nextfile;
2598         //biosA0[0x64] = psxBios_dev_cd_chdir;
2599         //biosA0[0x65] = psxBios_dev_card_open;
2600         //biosA0[0x66] = psxBios_dev_card_read;
2601         //biosA0[0x67] = psxBios_dev_card_write;
2602         //biosA0[0x68] = psxBios_dev_card_close;
2603         //biosA0[0x69] = psxBios_dev_card_firstfile;
2604         //biosA0[0x6a] = psxBios_dev_card_nextfile;
2605         //biosA0[0x6b] = psxBios_dev_card_erase;
2606         //biosA0[0x6c] = psxBios_dev_card_undelete;
2607         //biosA0[0x6d] = psxBios_dev_card_format;
2608         //biosA0[0x6e] = psxBios_dev_card_rename;
2609         //biosA0[0x6f] = psxBios_dev_card_6f;
2610         biosA0[0x70] = psxBios__bu_init;
2611         biosA0[0x71] = psxBios__96_init;
2612         biosA0[0x72] = psxBios__96_remove;
2613         //biosA0[0x73] = psxBios_sys_a0_73;
2614         //biosA0[0x74] = psxBios_sys_a0_74;
2615         //biosA0[0x75] = psxBios_sys_a0_75;
2616         //biosA0[0x76] = psxBios_sys_a0_76;
2617         //biosA0[0x77] = psxBios_sys_a0_77;
2618         //biosA0[0x78] = psxBios__96_CdSeekL;
2619         //biosA0[0x79] = psxBios_sys_a0_79;
2620         //biosA0[0x7a] = psxBios_sys_a0_7a;
2621         //biosA0[0x7b] = psxBios_sys_a0_7b;
2622         //biosA0[0x7c] = psxBios__96_CdGetStatus;
2623         //biosA0[0x7d] = psxBios_sys_a0_7d;
2624         //biosA0[0x7e] = psxBios__96_CdRead;
2625         //biosA0[0x7f] = psxBios_sys_a0_7f;
2626         //biosA0[0x80] = psxBios_sys_a0_80;
2627         //biosA0[0x81] = psxBios_sys_a0_81;
2628         //biosA0[0x82] = psxBios_sys_a0_82;             
2629         //biosA0[0x83] = psxBios_sys_a0_83;
2630         //biosA0[0x84] = psxBios_sys_a0_84;
2631         //biosA0[0x85] = psxBios__96_CdStop;    
2632         //biosA0[0x86] = psxBios_sys_a0_86;
2633         //biosA0[0x87] = psxBios_sys_a0_87;
2634         //biosA0[0x88] = psxBios_sys_a0_88;
2635         //biosA0[0x89] = psxBios_sys_a0_89;
2636         //biosA0[0x8a] = psxBios_sys_a0_8a;
2637         //biosA0[0x8b] = psxBios_sys_a0_8b;
2638         //biosA0[0x8c] = psxBios_sys_a0_8c;
2639         //biosA0[0x8d] = psxBios_sys_a0_8d;
2640         //biosA0[0x8e] = psxBios_sys_a0_8e;
2641         //biosA0[0x8f] = psxBios_sys_a0_8f;
2642         //biosA0[0x90] = psxBios_sys_a0_90;
2643         //biosA0[0x91] = psxBios_sys_a0_91;
2644         //biosA0[0x92] = psxBios_sys_a0_92;
2645         //biosA0[0x93] = psxBios_sys_a0_93;
2646         //biosA0[0x94] = psxBios_sys_a0_94;
2647         //biosA0[0x95] = psxBios_sys_a0_95;
2648         //biosA0[0x96] = psxBios_AddCDROMDevice;
2649         //biosA0[0x97] = psxBios_AddMemCardDevide;
2650         //biosA0[0x98] = psxBios_DisableKernelIORedirection;
2651         //biosA0[0x99] = psxBios_EnableKernelIORedirection;
2652         //biosA0[0x9a] = psxBios_sys_a0_9a;
2653         //biosA0[0x9b] = psxBios_sys_a0_9b;
2654         //biosA0[0x9c] = psxBios_SetConf;
2655         //biosA0[0x9d] = psxBios_GetConf;
2656         //biosA0[0x9e] = psxBios_sys_a0_9e;
2657         biosA0[0x9f] = psxBios_SetMem;
2658         //biosA0[0xa0] = psxBios__boot;
2659         //biosA0[0xa1] = psxBios_SystemError;
2660         //biosA0[0xa2] = psxBios_EnqueueCdIntr;
2661         //biosA0[0xa3] = psxBios_DequeueCdIntr;
2662         //biosA0[0xa4] = psxBios_sys_a0_a4;
2663         //biosA0[0xa5] = psxBios_ReadSector;
2664         //biosA0[0xa6] = psxBios_get_cd_status;
2665         //biosA0[0xa7] = psxBios_bufs_cb_0;
2666         //biosA0[0xa8] = psxBios_bufs_cb_1;
2667         //biosA0[0xa9] = psxBios_bufs_cb_2;
2668         //biosA0[0xaa] = psxBios_bufs_cb_3;
2669         biosA0[0xab] = psxBios__card_info;
2670         biosA0[0xac] = psxBios__card_load;
2671         //biosA0[0axd] = psxBios__card_auto;
2672         //biosA0[0xae] = psxBios_bufs_cd_4;
2673         //biosA0[0xaf] = psxBios_sys_a0_af;
2674         //biosA0[0xb0] = psxBios_sys_a0_b0;
2675         //biosA0[0xb1] = psxBios_sys_a0_b1;
2676         //biosA0[0xb2] = psxBios_do_a_long_jmp
2677         //biosA0[0xb3] = psxBios_sys_a0_b3;
2678         //biosA0[0xb4] = psxBios_sub_function;
2679 //*******************B0 CALLS****************************
2680         //biosB0[0x00] = psxBios_SysMalloc;
2681         //biosB0[0x01] = psxBios_sys_b0_01;
2682         biosB0[0x02] = psxBios_SetRCnt;
2683         biosB0[0x03] = psxBios_GetRCnt;
2684         biosB0[0x04] = psxBios_StartRCnt;
2685         biosB0[0x05] = psxBios_StopRCnt;
2686         biosB0[0x06] = psxBios_ResetRCnt;
2687         biosB0[0x07] = psxBios_DeliverEvent;
2688         biosB0[0x08] = psxBios_OpenEvent;
2689         biosB0[0x09] = psxBios_CloseEvent;
2690         biosB0[0x0a] = psxBios_WaitEvent;
2691         biosB0[0x0b] = psxBios_TestEvent;
2692         biosB0[0x0c] = psxBios_EnableEvent;
2693         biosB0[0x0d] = psxBios_DisableEvent;
2694         biosB0[0x0e] = psxBios_OpenTh;
2695         biosB0[0x0f] = psxBios_CloseTh;
2696         biosB0[0x10] = psxBios_ChangeTh;
2697         //biosB0[0x11] = psxBios_psxBios_b0_11;
2698         biosB0[0x12] = psxBios_InitPAD;
2699         biosB0[0x13] = psxBios_StartPAD;
2700         biosB0[0x14] = psxBios_StopPAD;
2701         biosB0[0x15] = psxBios_PAD_init;
2702         biosB0[0x16] = psxBios_PAD_dr;
2703         biosB0[0x17] = psxBios_ReturnFromException;
2704         biosB0[0x18] = psxBios_ResetEntryInt;
2705         biosB0[0x19] = psxBios_HookEntryInt;
2706         //biosB0[0x1a] = psxBios_sys_b0_1a;
2707         //biosB0[0x1b] = psxBios_sys_b0_1b;
2708         //biosB0[0x1c] = psxBios_sys_b0_1c;
2709         //biosB0[0x1d] = psxBios_sys_b0_1d;
2710         //biosB0[0x1e] = psxBios_sys_b0_1e;
2711         //biosB0[0x1f] = psxBios_sys_b0_1f;
2712         biosB0[0x20] = psxBios_UnDeliverEvent;
2713         //biosB0[0x21] = psxBios_sys_b0_21;
2714         //biosB0[0x22] = psxBios_sys_b0_22;
2715         //biosB0[0x23] = psxBios_sys_b0_23;
2716         //biosB0[0x24] = psxBios_sys_b0_24;
2717         //biosB0[0x25] = psxBios_sys_b0_25;
2718         //biosB0[0x26] = psxBios_sys_b0_26;
2719         //biosB0[0x27] = psxBios_sys_b0_27;
2720         //biosB0[0x28] = psxBios_sys_b0_28;
2721         //biosB0[0x29] = psxBios_sys_b0_29;
2722         //biosB0[0x2a] = psxBios_sys_b0_2a;
2723         //biosB0[0x2b] = psxBios_sys_b0_2b;
2724         //biosB0[0x2c] = psxBios_sys_b0_2c;
2725         //biosB0[0x2d] = psxBios_sys_b0_2d;
2726         //biosB0[0x2e] = psxBios_sys_b0_2e;
2727         //biosB0[0x2f] = psxBios_sys_b0_2f;
2728         //biosB0[0x30] = psxBios_sys_b0_30;
2729         //biosB0[0x31] = psxBios_sys_b0_31;
2730         biosB0[0x32] = psxBios_open;
2731         biosB0[0x33] = psxBios_lseek;
2732         biosB0[0x34] = psxBios_read;
2733         biosB0[0x35] = psxBios_write;
2734         biosB0[0x36] = psxBios_close;
2735         //biosB0[0x37] = psxBios_ioctl;
2736         //biosB0[0x38] = psxBios_exit;
2737         //biosB0[0x39] = psxBios_sys_b0_39;
2738         //biosB0[0x3a] = psxBios_getc;
2739         //biosB0[0x3b] = psxBios_putc;
2740         biosB0[0x3c] = psxBios_getchar;
2741         //biosB0[0x3e] = psxBios_gets;
2742         //biosB0[0x40] = psxBios_cd;
2743         biosB0[0x41] = psxBios_format;
2744         biosB0[0x42] = psxBios_firstfile;
2745         biosB0[0x43] = psxBios_nextfile;
2746         biosB0[0x44] = psxBios_rename;
2747         biosB0[0x45] = psxBios_delete;
2748         //biosB0[0x46] = psxBios_undelete;
2749         //biosB0[0x47] = psxBios_AddDevice;
2750         //biosB0[0x48] = psxBios_RemoteDevice;
2751         //biosB0[0x49] = psxBios_PrintInstalledDevices;
2752         biosB0[0x4a] = psxBios_InitCARD;
2753         biosB0[0x4b] = psxBios_StartCARD;
2754         biosB0[0x4c] = psxBios_StopCARD;
2755         //biosB0[0x4d] = psxBios_sys_b0_4d;
2756         biosB0[0x4e] = psxBios__card_write;
2757         biosB0[0x4f] = psxBios__card_read;
2758         biosB0[0x50] = psxBios__new_card;
2759         biosB0[0x51] = psxBios_Krom2RawAdd;
2760         //biosB0[0x52] = psxBios_sys_b0_52;
2761         //biosB0[0x53] = psxBios_sys_b0_53;
2762         //biosB0[0x54] = psxBios__get_errno;
2763         //biosB0[0x55] = psxBios__get_error;
2764         biosB0[0x56] = psxBios_GetC0Table;
2765         biosB0[0x57] = psxBios_GetB0Table;
2766         biosB0[0x58] = psxBios__card_chan;
2767         //biosB0[0x59] = psxBios_sys_b0_59;
2768         //biosB0[0x5a] = psxBios_sys_b0_5a;
2769         biosB0[0x5b] = psxBios_ChangeClearPad;
2770         //biosB0[0x5c] = psxBios__card_status;
2771         //biosB0[0x5d] = psxBios__card_wait;
2772 //*******************C0 CALLS****************************
2773         //biosC0[0x00] = psxBios_InitRCnt;
2774         //biosC0[0x01] = psxBios_InitException;
2775         biosC0[0x02] = psxBios_SysEnqIntRP;
2776         biosC0[0x03] = psxBios_SysDeqIntRP;
2777         //biosC0[0x04] = psxBios_get_free_EvCB_slot;
2778         //biosC0[0x05] = psxBios_get_free_TCB_slot;
2779         //biosC0[0x06] = psxBios_ExceptionHandler;
2780         //biosC0[0x07] = psxBios_InstallExeptionHandler;
2781         //biosC0[0x08] = psxBios_SysInitMemory;
2782         //biosC0[0x09] = psxBios_SysInitKMem;
2783         biosC0[0x0a] = psxBios_ChangeClearRCnt; 
2784         //biosC0[0x0b] = psxBios_SystemError;
2785         //biosC0[0x0c] = psxBios_InitDefInt;
2786         //biosC0[0x0d] = psxBios_sys_c0_0d;
2787         //biosC0[0x0e] = psxBios_sys_c0_0e;
2788         //biosC0[0x0f] = psxBios_sys_c0_0f;
2789         //biosC0[0x10] = psxBios_sys_c0_10;
2790         //biosC0[0x11] = psxBios_sys_c0_11;
2791         //biosC0[0x12] = psxBios_InstallDevices;
2792         //biosC0[0x13] = psxBios_FlushStfInOutPut;
2793         //biosC0[0x14] = psxBios_sys_c0_14;
2794         //biosC0[0x15] = psxBios__cdevinput;
2795         //biosC0[0x16] = psxBios__cdevscan;
2796         //biosC0[0x17] = psxBios__circgetc;
2797         //biosC0[0x18] = psxBios__circputc;
2798         //biosC0[0x19] = psxBios_ioabort;
2799         //biosC0[0x1a] = psxBios_sys_c0_1a
2800         //biosC0[0x1b] = psxBios_KernelRedirect;
2801         //biosC0[0x1c] = psxBios_PatchAOTable;
2802 //************** THE END ***************************************
2803 /**/
2804         base = 0x1000;
2805         size = sizeof(EvCB) * 32;
2806         Event = (void *)&psxR[base]; base += size * 6;
2807         memset(Event, 0, size * 6);
2808         HwEV = Event;
2809         EvEV = Event + 32;
2810         RcEV = Event + 32 * 2;
2811         UeEV = Event + 32 * 3;
2812         SwEV = Event + 32 * 4;
2813         ThEV = Event + 32 * 5;
2814
2815         ptr = (u32 *)&psxM[0x0874]; // b0 table
2816         ptr[0] = SWAPu32(0x4c54 - 0x884);
2817
2818         ptr = (u32 *)&psxM[0x0674]; // c0 table
2819         ptr[6] = SWAPu32(0xc80);
2820
2821         memset(SysIntRP, 0, sizeof(SysIntRP));
2822         memset(Thread, 0, sizeof(Thread));
2823         Thread[0].status = 2; // main thread
2824
2825         jmp_int = NULL;
2826         pad_buf = NULL;
2827         pad_buf1 = NULL;
2828         pad_buf2 = NULL;
2829         pad_buf1len = pad_buf2len = 0;
2830         heap_addr = NULL;
2831         heap_end = NULL;
2832         CardState = -1;
2833         CurThread = 0;
2834         memset(FDesc, 0, sizeof(FDesc));
2835
2836         psxMu32ref(0x0150) = SWAPu32(0x160);
2837         psxMu32ref(0x0154) = SWAPu32(0x320);
2838         psxMu32ref(0x0160) = SWAPu32(0x248);
2839         strcpy((char *)&psxM[0x248], "bu");
2840 /*      psxMu32ref(0x0ca8) = SWAPu32(0x1f410004);
2841         psxMu32ref(0x0cf0) = SWAPu32(0x3c020000);
2842         psxMu32ref(0x0cf4) = SWAPu32(0x2442641c);
2843         psxMu32ref(0x09e0) = SWAPu32(0x43d0);
2844         psxMu32ref(0x4d98) = SWAPu32(0x946f000a);
2845 */
2846         // opcode HLE
2847         psxRu32ref(0x0000) = SWAPu32((0x3b << 26) | 4);
2848         psxMu32ref(0x0000) = SWAPu32((0x3b << 26) | 0);
2849         psxMu32ref(0x00a0) = SWAPu32((0x3b << 26) | 1);
2850         psxMu32ref(0x00b0) = SWAPu32((0x3b << 26) | 2);
2851         psxMu32ref(0x00c0) = SWAPu32((0x3b << 26) | 3);
2852         psxMu32ref(0x4c54) = SWAPu32((0x3b << 26) | 0);
2853         psxMu32ref(0x8000) = SWAPu32((0x3b << 26) | 5);
2854         psxMu32ref(0x07a0) = SWAPu32((0x3b << 26) | 0);
2855         psxMu32ref(0x0884) = SWAPu32((0x3b << 26) | 0);
2856         psxMu32ref(0x0894) = SWAPu32((0x3b << 26) | 0);
2857
2858         // initial stack pointer for BIOS interrupt
2859         psxMu32ref(0x6c80) = SWAPu32(0x000085c8);
2860
2861         // initial RNG seed
2862         psxMu32ref(0x9010) = SWAPu32(0xac20cc00);
2863
2864         // fonts
2865         len = 0x80000 - 0x66000;
2866         uncompress((Bytef *)(psxR + 0x66000), &len, font_8140, sizeof(font_8140));
2867         len = 0x80000 - 0x69d68;
2868         uncompress((Bytef *)(psxR + 0x69d68), &len, font_889f, sizeof(font_889f));
2869
2870         // memory size 2 MB
2871         psxHu32ref(0x1060) = SWAPu32(0x00000b88);
2872
2873         hleSoftCall = FALSE;
2874 }
2875
2876 void psxBiosShutdown() {
2877 }
2878
2879 #define psxBios_PADpoll(pad) { \
2880         PAD##pad##_startPoll(pad); \
2881         pad_buf##pad[0] = 0; \
2882         pad_buf##pad[1] = PAD##pad##_poll(0x42); \
2883         if (!(pad_buf##pad[1] & 0x0f)) { \
2884                 bufcount = 32; \
2885         } else { \
2886                 bufcount = (pad_buf##pad[1] & 0x0f) * 2; \
2887         } \
2888         PAD##pad##_poll(0); \
2889         i = 2; \
2890         while (bufcount--) { \
2891                 pad_buf##pad[i++] = PAD##pad##_poll(0); \
2892         } \
2893 }
2894
2895 void biosInterrupt() {
2896         int i, bufcount;
2897
2898 //      if (psxHu32(0x1070) & 0x1) { // Vsync
2899                 if (pad_buf != NULL) {
2900                         u32 *buf = (u32*)pad_buf;
2901
2902                         if (!Config.UseNet) {
2903                                 PAD1_startPoll(1);
2904                                 if (PAD1_poll(0x42) == 0x23) {
2905                                         PAD1_poll(0);
2906                                         *buf = PAD1_poll(0) << 8;
2907                                         *buf |= PAD1_poll(0);
2908                                         PAD1_poll(0);
2909                                         *buf &= ~((PAD1_poll(0) > 0x20) ? 1 << 6 : 0);
2910                                         *buf &= ~((PAD1_poll(0) > 0x20) ? 1 << 7 : 0);
2911                                 } else {
2912                                         PAD1_poll(0);
2913                                         *buf = PAD1_poll(0) << 8;
2914                                         *buf|= PAD1_poll(0);
2915                                 }
2916
2917                                 PAD2_startPoll(2);
2918                                 if (PAD2_poll(0x42) == 0x23) {
2919                                         PAD2_poll(0);
2920                                         *buf |= PAD2_poll(0) << 24;
2921                                         *buf |= PAD2_poll(0) << 16;
2922                                         PAD2_poll(0);
2923                                         *buf &= ~((PAD2_poll(0) > 0x20) ? 1 << 22 : 0);
2924                                         *buf &= ~((PAD2_poll(0) > 0x20) ? 1 << 23 : 0);
2925                                 } else {
2926                                         PAD2_poll(0);
2927                                         *buf |= PAD2_poll(0) << 24;
2928                                         *buf |= PAD2_poll(0) << 16;
2929                                 }
2930                         } else {
2931                                 u16 data;
2932
2933                                 PAD1_startPoll(1);
2934                                 PAD1_poll(0x42);
2935                                 PAD1_poll(0);
2936                                 data = PAD1_poll(0) << 8;
2937                                 data |= PAD1_poll(0);
2938
2939                                 if (NET_sendPadData(&data, 2) == -1)
2940                                         netError();
2941
2942                                 if (NET_recvPadData(&((u16*)buf)[0], 1) == -1)
2943                                         netError();
2944                                 if (NET_recvPadData(&((u16*)buf)[1], 2) == -1)
2945                                         netError();
2946                         }
2947                 }
2948                 if (Config.UseNet && pad_buf1 != NULL && pad_buf2 != NULL) {
2949                         psxBios_PADpoll(1);
2950
2951                         if (NET_sendPadData(pad_buf1, i) == -1)
2952                                 netError();
2953
2954                         if (NET_recvPadData(pad_buf1, 1) == -1)
2955                                 netError();
2956                         if (NET_recvPadData(pad_buf2, 2) == -1)
2957                                 netError();
2958                 } else {
2959                         if (pad_buf1) {
2960                                 psxBios_PADpoll(1);
2961                         }
2962
2963                         if (pad_buf2) {
2964                                 psxBios_PADpoll(2);
2965                         }
2966                 }
2967
2968         if (psxHu32(0x1070) & 0x1) { // Vsync
2969                 if (RcEV[3][1].status == EvStACTIVE) {
2970                         softCall(RcEV[3][1].fhandler);
2971 //                      hwWrite32(0x1f801070, ~(1));
2972                 }
2973         }
2974
2975         if (psxHu32(0x1070) & 0x70) { // Rcnt 0,1,2
2976                 int i;
2977
2978                 for (i = 0; i < 3; i++) {
2979                         if (psxHu32(0x1070) & (1 << (i + 4))) {
2980                                 if (RcEV[i][1].status == EvStACTIVE) {
2981                                         softCall(RcEV[i][1].fhandler);
2982                                 }
2983                                 psxHwWrite32(0x1f801070, ~(1 << (i + 4)));
2984                         }
2985                 }
2986         }
2987 }
2988
2989 void psxBiosException() {
2990         int i;
2991
2992         switch (psxRegs.CP0.n.Cause & 0x3c) {
2993                 case 0x00: // Interrupt
2994 #ifdef PSXCPU_LOG
2995 //                      PSXCPU_LOG("interrupt\n");
2996 #endif
2997                         SaveRegs();
2998
2999                         sp = psxMu32(0x6c80); // create new stack for interrupt handlers
3000
3001                         biosInterrupt();
3002
3003                         for (i = 0; i < 8; i++) {
3004                                 if (SysIntRP[i]) {
3005                                         u32 *queue = (u32 *)PSXM(SysIntRP[i]);
3006
3007                                         s0 = queue[2];
3008                                         softCall(queue[1]);
3009                                 }
3010                         }
3011
3012                         if (jmp_int != NULL) {
3013                                 int i;
3014
3015                                 psxHwWrite32(0x1f801070, 0xffffffff);
3016
3017                                 ra = jmp_int[0];
3018                                 sp = jmp_int[1];
3019                                 fp = jmp_int[2];
3020                                 for (i = 0; i < 8; i++) // s0-s7
3021                                          psxRegs.GPR.r[16 + i] = jmp_int[3 + i];
3022                                 gp = jmp_int[11];
3023
3024                                 v0 = 1;
3025                                 pc0 = ra;
3026                                 return;
3027                         }
3028                         psxHwWrite16(0x1f801070, 0);
3029                         break;
3030
3031                 case 0x20: // Syscall
3032 #ifdef PSXCPU_LOG
3033                         PSXCPU_LOG("syscall exp %x\n", a0);
3034 #endif
3035                         switch (a0) {
3036                                 case 1: // EnterCritical - disable irq's
3037                                         /* Fixes Medievil 2 not loading up new game, Digimon World not booting up and possibly others */
3038                                         v0 = (psxRegs.CP0.n.Status & 0x404) == 0x404;
3039                                         psxRegs.CP0.n.Status &= ~0x404;
3040                                         break;
3041
3042                                 case 2: // ExitCritical - enable irq's
3043                                         psxRegs.CP0.n.Status |= 0x404; 
3044                                         break;
3045                         }
3046                         pc0 = psxRegs.CP0.n.EPC + 4;
3047
3048                         psxRegs.CP0.n.Status = (psxRegs.CP0.n.Status & 0xfffffff0) |
3049                                                                   ((psxRegs.CP0.n.Status & 0x3c) >> 2);
3050                         return;
3051
3052                 default:
3053 #ifdef PSXCPU_LOG
3054                         PSXCPU_LOG("unknown bios exception!\n");
3055 #endif
3056                         break;
3057         }
3058
3059         pc0 = psxRegs.CP0.n.EPC;
3060         if (psxRegs.CP0.n.Cause & 0x80000000) pc0+=4;
3061
3062         psxRegs.CP0.n.Status = (psxRegs.CP0.n.Status & 0xfffffff0) |
3063                                                   ((psxRegs.CP0.n.Status & 0x3c) >> 2);
3064 }
3065
3066 #define bfreeze(ptr, size) { \
3067         if (Mode == 1) memcpy(&psxR[base], ptr, size); \
3068         if (Mode == 0) memcpy(ptr, &psxR[base], size); \
3069         base += size; \
3070 }
3071
3072 #define bfreezes(ptr) bfreeze(ptr, sizeof(ptr))
3073 #define bfreezel(ptr) bfreeze(ptr, sizeof(*ptr))
3074
3075 #define bfreezepsxMptr(ptr, type) { \
3076         if (Mode == 1) { \
3077                 if (ptr) psxRu32ref(base) = SWAPu32((s8 *)(ptr) - psxM); \
3078                 else psxRu32ref(base) = 0; \
3079         } else { \
3080                 if (psxRu32(base) != 0) ptr = (type *)(psxM + psxRu32(base)); \
3081                 else (ptr) = NULL; \
3082         } \
3083         base += sizeof(u32); \
3084 }
3085
3086 void psxBiosFreeze(int Mode) {
3087         u32 base = 0x40000;
3088
3089         bfreezepsxMptr(jmp_int, u32);
3090         bfreezepsxMptr(pad_buf, int);
3091         bfreezepsxMptr(pad_buf1, char);
3092         bfreezepsxMptr(pad_buf2, char);
3093         bfreezepsxMptr(heap_addr, u32);
3094         bfreezel(&pad_buf1len);
3095         bfreezel(&pad_buf2len);
3096         bfreezes(regs);
3097         bfreezes(SysIntRP);
3098         bfreezel(&CardState);
3099         bfreezes(Thread);
3100         bfreezel(&CurThread);
3101         bfreezes(FDesc);
3102         bfreezel(&card_active_chan);
3103 }