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