-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpsxbios.c
3340 lines (2846 loc) · 73 KB
/
psxbios.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
* [email protected], http://rschultz.ath.cx/code.php *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307 USA. *
***************************************************************************/
/*
* Internal emulated HLE BIOS.
*/
#include "psxbios.h"
#include "psxhw.h"
#include "gpu.h"
#include <zlib.h>
//We try to emulate bios :) HELP US :P
//#define PSXBIOS_LOG printf
#ifdef PSXBIOS_LOG
char *biosA0n[256] = {
// 0x00
"open", "lseek", "read", "write",
"close", "ioctl", "exit", "sys_a0_07",
"getc", "putc", "todigit", "atof",
"strtoul", "strtol", "abs", "labs",
// 0x10
"atoi", "atol", "atob", "setjmp",
"longjmp", "strcat", "strncat", "strcmp",
"strncmp", "strcpy", "strncpy", "strlen",
"index", "rindex", "strchr", "strrchr",
// 0x20
"strpbrk", "strspn", "strcspn", "strtok",
"strstr", "toupper", "tolower", "bcopy",
"bzero", "bcmp", "memcpy", "memset",
"memmove", "memcmp", "memchr", "rand",
// 0x30
"srand", "qsort", "strtod", "malloc",
"free", "lsearch", "bsearch", "calloc",
"realloc", "InitHeap", "_exit", "getchar",
"putchar", "gets", "puts", "printf",
// 0x40
"sys_a0_40", "LoadTest", "Load", "Exec",
"FlushCache", "InstallInterruptHandler", "GPU_dw", "mem2vram",
"SendGPUStatus", "GPU_cw", "GPU_cwb", "SendPackets",
"sys_a0_4c", "GetGPUStatus", "GPU_sync", "SystemError",
// 0x50
"SystemError", "LoadExec", "GetSysSp", "SystemError",
"_96_init()", "_bu_init()", "_96_remove()", "ReturnZero",
"ReturnZero", "ReturnZero", "ReturnZero", "dev_tty_init",
"dev_tty_open", "sys_a0_5d", "dev_tty_ioctl","dev_cd_open",
// 0x60
"dev_cd_read", "dev_cd_close", "dev_cd_firstfile", "dev_cd_nextfile",
"dev_cd_chdir", "dev_card_open", "dev_card_read", "dev_card_write",
"dev_card_close", "dev_card_firstfile", "dev_card_nextfile","dev_card_erase",
"dev_card_undelete","dev_card_format", "dev_card_rename", "dev_card_6f",
// 0x70
"_bu_init", "_96_init", "_96_remove", "ReturnZero",
"ReturnZero", "ReturnZero", "ReturnZero", "ReturnZero",
"_96_CdSeekL", "ReturnZero", "ReturnZero", "ReturnZero",
"_96_CdGetStatus", "sys_a0_7d", "_96_CdRead", "ReturnZero",
// 0x80
"ReturnZero", "sys_a0_81", "ReturnZero", "ReturnZero",
"ReturnZero", "_96_CdStop", "ReturnZero", "ReturnZero",
"ReturnZero", "ReturnZero", "ReturnZero", "ReturnZero",
"ReturnZero", "ReturnZero", "ReturnZero", "ReturnZero",
// 0x90
"sys_a0_90", "sys_a0_91", "sys_a0_92", "sys_a0_93",
"sys_a0_94", "sys_a0_95", "AddCDROMDevice", "AddMemCardDevide",
"DisableKernelIORedirection", "EnableKernelIORedirection", "SystemError", "SystemError",
"SetConf", "GetConf", "sys_a0_9e", "SetMem",
// 0xa0
"_boot", "SystemError", "EnqueueCdIntr", "DequeueCdIntr",
"sys_a0_a4", "ReadSector", "get_cd_status", "bufs_cb_0",
"bufs_cb_1", "bufs_cb_2", "bufs_cb_3", "_card_info",
"_card_load", "_card_auto", "bufs_cd_4", "sys_a0_af",
// 0xb0
"sys_a0_b0", "sys_a0_b1", "do_a_long_jmp", "sys_a0_b3",
"?? sub_function",
};
char *biosB0n[256] = {
// 0x00
"SysMalloc", "sys_b0_01", "sys_b0_02", "sys_b0_03",
"sys_b0_04", "sys_b0_05", "sys_b0_06", "DeliverEvent",
"OpenEvent", "CloseEvent", "WaitEvent", "TestEvent",
"EnableEvent", "DisableEvent", "OpenTh", "CloseTh",
// 0x10
"ChangeTh", "sys_b0_11", "InitPAD", "StartPAD",
"StopPAD", "PAD_init", "PAD_dr", "ReturnFromExecption",
"ResetEntryInt", "HookEntryInt", "SystemError", "SystemError",
"SystemError", "SystemError", "SystemError", "SystemError",
// 0x20
"UnDeliverEvent", "SystemError", "SystemError", "SystemError",
"sys_b0_24", "sys_b0_25", "sys_b0_26", "sys_b0_27",
"sys_b0_28", "sys_b0_29", "SystemError", "SystemError",
"sys_b0_2c", "sys_b0_2d", "sys_b0_2e", "sys_b0_2f",
// 0x30
"sys_b0_30", "sys_b0_31", "open", "lseek",
"read", "write", "close", "ioctl",
"exit", "sys_b0_39", "getc", "putc",
"getchar", "putchar", "gets", "puts",
// 0x40
"cd", "format", "firstfile", "nextfile",
"rename", "delete", "undelete", "AddDevice",
"RemoteDevice", "PrintInstalledDevices", "InitCARD", "StartCARD",
"StopCARD", "sys_b0_4d", "_card_write", "_card_read",
// 0x50
"_new_card", "Krom2RawAdd", "SystemError", "sys_b0_53",
"_get_errno", "_get_error", "GetC0Table", "GetB0Table",
"_card_chan", "sys_b0_59", "SystemError", "ChangeClearPAD",
"_card_status", "_card_wait",
};
char *biosC0n[256] = {
// 0x00
"InitRCnt", "InitException", "SysEnqIntRP", "SysDeqIntRP",
"get_free_EvCB_slot", "get_free_TCB_slot", "ExceptionHandler", "InstallExeptionHandler",
"SysInitMemory", "SysInitKMem", "ChangeClearRCnt", "SystemError",
"InitDefInt", "sys_c0_0d", "ReturnZero", "ReturnZero",
// 0x10
"ReturnZero", "ReturnZero", "InstallDevices", "FlushStfInOutPut",
"ReturnZero", "_cdevinput", "_cdevscan", "_circgetc",
"_circputc", "ioabort", "sys_c0_1a", "KernelRedirect",
"PatchAOTable",
};
#endif
//#define r0 (psxRegs.GPR.n.r0)
#define at (psxRegs.GPR.n.at)
#define v0 (psxRegs.GPR.n.v0)
#define v1 (psxRegs.GPR.n.v1)
#define a0 (psxRegs.GPR.n.a0)
#define a1 (psxRegs.GPR.n.a1)
#define a2 (psxRegs.GPR.n.a2)
#define a3 (psxRegs.GPR.n.a3)
#define t0 (psxRegs.GPR.n.t0)
#define t1 (psxRegs.GPR.n.t1)
#define t2 (psxRegs.GPR.n.t2)
#define t3 (psxRegs.GPR.n.t3)
#define t4 (psxRegs.GPR.n.t4)
#define t5 (psxRegs.GPR.n.t5)
#define t6 (psxRegs.GPR.n.t6)
#define t7 (psxRegs.GPR.n.t7)
#define t8 (psxRegs.GPR.n.t8)
#define t9 (psxRegs.GPR.n.t9)
#define s0 (psxRegs.GPR.n.s0)
#define s1 (psxRegs.GPR.n.s1)
#define s2 (psxRegs.GPR.n.s2)
#define s3 (psxRegs.GPR.n.s3)
#define s4 (psxRegs.GPR.n.s4)
#define s5 (psxRegs.GPR.n.s5)
#define s6 (psxRegs.GPR.n.s6)
#define s7 (psxRegs.GPR.n.s7)
#define k0 (psxRegs.GPR.n.k0)
#define k1 (psxRegs.GPR.n.k1)
#define gp (psxRegs.GPR.n.gp)
#define sp (psxRegs.GPR.n.sp)
#define fp (psxRegs.GPR.n.s8)
#define ra (psxRegs.GPR.n.ra)
#define pc0 (psxRegs.pc)
#define Ra0 ((char*)PSXM(a0))
#define Ra1 ((char*)PSXM(a1))
#define Ra2 ((char*)PSXM(a2))
#define Ra3 ((char*)PSXM(a3))
#define Rv0 ((char*)PSXM(v0))
#define Rsp ((char*)PSXM(sp))
typedef struct {
uint32_t desc;
int32_t status;
int32_t mode;
uint32_t fhandler;
} EvCB[32];
#define EvStUNUSED 0x0000
#define EvStWAIT 0x1000
#define EvStACTIVE 0x2000
#define EvStALREADY 0x4000
#define EvMdINTR 0x1000
#define EvMdNOINTR 0x2000
typedef struct {
int32_t status;
int32_t mode;
uint32_t reg[32];
uint32_t func;
} TCB;
typedef struct {
uint32_t _pc0;
uint32_t gp0;
uint32_t t_addr;
uint32_t t_size;
uint32_t d_addr;
uint32_t d_size;
uint32_t b_addr;
uint32_t b_size;
uint32_t S_addr;
uint32_t s_size;
uint32_t _sp,_fp,_gp,ret,base;
} EXEC;
struct DIRENTRY {
/* 0Ah-1Eh Filename in ASCII, terminated by 00h (max 20 chars, plus ending 00h) */
char name[20];
int32_t attr;
int32_t size;
uint32_t next;
int32_t head;
char system[4];
};
typedef struct {
char name[32];
uint32_t mode;
uint32_t offset;
uint32_t size;
uint32_t mcfile;
} FileDesc;
static uint32_t *jmp_int = NULL;
static int *pad_buf = NULL;
static char *pad_buf1 = NULL, *pad_buf2 = NULL;
static int pad_buf1len, pad_buf2len;//shadow add
static int pad_stopped = 0;
static uint32_t regs[35];
static EvCB *Event;
static EvCB *HwEV; // 0xf0
static EvCB *EvEV; // 0xf1
static EvCB *RcEV; // 0xf2
static EvCB *UeEV; // 0xf3
static EvCB *SwEV; // 0xf4
static EvCB *ThEV; // 0xff
static uint32_t *heap_addr = NULL;
static uint32_t *heap_end = NULL;
static uint32_t heap_size = 0;
static uint32_t SysIntRP[8];
static int CardState = -1;
static TCB Thread[8];
static int CurThread = 0;
static FileDesc FDesc[32];
static uint32_t card_active_chan;
/* To avoid any issues with different behaviour when using the libc's own strlen instead.
* We want to mimic the PSX's behaviour in this case for bufile. */
static size_t strlen_internal(char* p)
{
size_t size_of_array = 0;
while (*p++) size_of_array++;
return size_of_array;
}
INLINE void softCall(uint32_t pc) {
pc0 = pc;
ra = 0x80001000;
while (pc0 != 0x80001000) psxCpu->ExecuteBlock(0x80001000);
}
INLINE void softCall2(uint32_t pc) {
uint32_t sra = ra;
pc0 = pc;
ra = 0x80001000;
while (pc0 != 0x80001000) psxCpu->ExecuteBlock(0x80001000);
ra = sra;
}
INLINE void DeliverEvent(uint32_t ev, uint32_t spec) {
if (Event[ev][spec].status != EvStACTIVE) return;
// Event[ev][spec].status = EvStALREADY;
if (Event[ev][spec].mode == EvMdINTR) {
softCall2(Event[ev][spec].fhandler);
} else Event[ev][spec].status = EvStALREADY;
}
static unsigned interrupt_r26=0x8004E8B0;
INLINE void SaveRegs(void) {
memcpy(regs, psxRegs.GPR.r, 32*4);
regs[32] = psxRegs.GPR.n.lo;
regs[33] = psxRegs.GPR.n.hi;
regs[34] = psxRegs.pc;
}
INLINE void LoadRegs(void) {
memcpy(psxRegs.GPR.r, regs, 32*4);
psxRegs.GPR.n.lo = regs[32];
psxRegs.GPR.n.hi = regs[33];
}
/* Bu functions calls */
char ffile[64], *pfile;
int nfile;
static void buopen(int mcd, char *ptr, uint8_t cfg)
{
int i;
char *fptr = ptr;
strcpy(FDesc[1 + mcd].name, Ra0+5);
FDesc[1 + mcd].offset = 0;
FDesc[1 + mcd].mode = a1;
for (i=1; i<16; i++) {
fptr += 128;
if ((*fptr & 0xF0) != 0x50) continue;
if (strcmp(FDesc[1 + mcd].name, fptr+0xa)) continue;
FDesc[1 + mcd].mcfile = i;
v0 = 1 + mcd;
break;
}
if (a1 & 0x200 && v0 == -1) { /* FCREAT */
fptr = ptr;
for (i=1; i<16; i++) {
int j, xord, nblk = a1 >> 16;
char *pptr, *fptr2;
fptr += 128;
if ((*fptr & 0xF0) != 0xa0) continue;
FDesc[1 + mcd].mcfile = i;
fptr[0] = 0x51;
fptr[4] = 0x00;
fptr[5] = 0x20 * nblk;
fptr[6] = 0x00;
fptr[7] = 0x00;
strcpy(fptr+0xa, FDesc[1 + mcd].name);
pptr = fptr2 = fptr;
for(j=2; j<=nblk; j++) {
int k;
for(i++; i<16; i++) {
fptr2 += 128;
memset(fptr2, 0, 128);
fptr2[0] = j < nblk ? 0x52 : 0x53;
pptr[8] = i - 1;
pptr[9] = 0;
for (k=0, xord=0; k<127; k++) xord^= pptr[k];
pptr[127] = xord;
pptr = fptr2;
break;
}
/* shouldn't this return ENOSPC if i == 16? */
}
pptr[8] = pptr[9] = 0xff;
for (j=0, xord=0; j<127; j++) xord^= pptr[j];
pptr[127] = xord;
v0 = 1 + mcd;
/* just go ahead and resave them all */
enum MemcardNum mcd_num = (cfg == 0) ? MCD1 : MCD2;
sioMcdWrite(mcd_num, ptr, 128, 128 * 15);
break;
}
/* shouldn't this return ENOSPC if i == 16? */
}
}
#define buread(Ra1, mcd, length) { \
/*printf("read %d: %x,%x (%s)\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, length, Mcd##mcd##Data + 128 * FDesc[1 + mcd].mcfile + 0xa);*/ \
unsigned offset = 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
sioMcdRead(((mcd == 1) ? MCD1 : MCD2), (char*)Ra1, offset, length); \
if (FDesc[1 + mcd].mode & 0x8000) { \
DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
v0 = 0; } \
else v0 = length; \
FDesc[1 + mcd].offset += v0; \
}
#define buwrite(Ra1, mcd, length) { \
unsigned offset = 8192 * FDesc[1 + mcd].mcfile + FDesc[1 + mcd].offset; \
/*printf("write %d: %x,%x\n", FDesc[1 + mcd].mcfile, FDesc[1 + mcd].offset, length);*/ \
sioMcdWrite((mcd==1) ? MCD1 : MCD2, (const char*)Ra1, offset, length); \
FDesc[1 + mcd].offset += length; \
if (FDesc[1 + mcd].mode & 0x8000) { \
DeliverEvent(0x11, 0x2); /* 0xf0000011, 0x0004 */ \
DeliverEvent(0x81, 0x2); /* 0xf4000001, 0x0004 */ \
v0 = 0; } \
else v0 = length; \
}
#define bufile(mcd) { \
int i; \
const char *mcd_data = sioMcdDataPtr((mcd==1) ? MCD1 : MCD2); \
size_t size_of_name = strlen_internal(dir->name); \
while (nfile < 16) { \
int match=1; \
\
const char *ptr = mcd_data + 128 * (nfile + 1); \
nfile++; \
if ((*ptr & 0xF0) != 0x50) continue; \
/* Bug link files show up as free block. */ \
if (!ptr[0xa]) continue; \
ptr+= 0xa; \
if (pfile[0] == 0) { \
strncpy(dir->name, ptr, sizeof(dir->name) - 1); \
if (size_of_name < sizeof(dir->name)) dir->name[size_of_name] = '\0'; \
} else for (i=0; i<20; i++) { \
if (pfile[i] == ptr[i]) { \
dir->name[i] = ptr[i]; \
if (ptr[i] == 0) break; else continue; } \
if (pfile[i] == '?') { \
dir->name[i] = ptr[i]; continue; } \
if (pfile[i] == '*') { \
strcpy(dir->name+i, ptr+i); break; } \
match = 0; break; \
} \
/*printf("%d : %s = %s + %s (match=%d)\n", nfile, dir->name, pfile, ptr, match);*/ \
if (match == 0) { continue; } \
dir->size = 8192; \
v0 = _dir; \
break; \
} \
}
#define burename(mcd) { \
int i; \
enum MemcardNum mcd_num = (mcd == 1) ? MCD1 : MCD2; \
char *mcd_data = sioMcdDataPtr(mcd_num); \
for (i=1; i<16; i++) { \
int namelen, j, cxor = 0; \
char *ptr = mcd_data + 128 * i; \
if ((*ptr & 0xF0) != 0x50) continue; \
if (strcmp(Ra0+5, ptr+0xa)) continue; \
namelen = strlen(Ra1+5); \
memcpy(ptr+0xa, Ra1+5, namelen); \
memset(ptr+0xa+namelen, 0, 0x75-namelen); \
for (j=0; j<127; j++) cxor^= ptr[j]; \
ptr[127] = cxor; \
sioMcdWrite(mcd_num, NULL, 128 * i + 0xa, 0x76); \
v0 = 1; \
break; \
} \
}
#define budelete(mcd) { \
enum MemcardNum mcd_num = (mcd == 1) ? MCD1 : MCD2; \
char *mcd_data = sioMcdDataPtr(mcd_num); \
int i; \
for (i=1; i<16; i++) { \
char *ptr = mcd_data + 128 * i; \
if ((*ptr & 0xF0) != 0x50) continue; \
if (strcmp(Ra0+5, ptr+0xa)) continue; \
*ptr = (*ptr & 0xf) | 0xA0; \
sioMcdWrite(mcd_num, NULL, 128 * i, 1); \
/*printf("delete %s\n", ptr+0xa);*/ \
v0 = 1; \
break; \
} \
}
/* *
// *
// *
// System calls A0 */
/* Internally redirects to "FileRead(fd,tempbuf,1)".*/
/* For some strange reason, the returned character is sign-expanded; */
/* So if a return value of FFFFFFFFh could mean either character FFh, or error. */
void psxBios_getc(void) // 0x03, 0x35
{
void *pa1 = Ra1;
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x03]);
#endif
v0 = -1;
if (pa1) {
switch (a0) {
case 2: buread(pa1, 1, 1); break;
case 3: buread(pa1, 2, 1); break;
}
}
v0 = (int32_t)v0;
pc0 = ra;
}
/* Copy of psxBios_write, except size is 1. */
void psxBios_putc(void) // 0x09, 0x3B
{
void *pa1 = Ra1;
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x09]);
#endif
v0 = -1;
if (!pa1) {
pc0 = ra;
return;
}
if (a0 == 1) { // stdout
char *ptr = (char *)pa1;
v0 = a2;
while (a2 > 0) {
printf("%c", *ptr++); a2--;
}
pc0 = ra; return;
}
switch (a0) {
case 2: buwrite(pa1, 1, 1); break;
case 3: buwrite(pa1, 2, 1); break;
}
pc0 = ra;
}
void psxBios_todigit(void) // 0x0a
{
int c = a0;
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x0a]);
#endif
c &= 0xFF;
if (c >= 0x30 && c < 0x3A) {
c -= 0x30;
}
else if (c > 0x60 && c < 0x7B) {
c -= 0x20;
}
else if (c > 0x40 && c < 0x5B) {
c = c - 0x41 + 10;
}
else if (c >= 0x80) {
c = -1;
}
else
{
c = 0x0098967F;
}
v0 = c;
pc0 = ra;
}
void psxBios_abs(void) { // 0x0e
if ((int32_t)a0 < 0) v0 = -(int32_t)a0;
else v0 = a0;
pc0 = ra;
}
void psxBios_labs(void) { // 0x0f
psxBios_abs();
}
void psxBios_atoi(void) { // 0x10
int32_t n = 0, f = 0;
char *p = (char *)Ra0;
for (;;p++) {
switch (*p) {
case ' ': case '\t': continue;
case '-': f++;
case '+': p++;
}
break;
}
while (*p >= '0' && *p <= '9') {
n = n * 10 + *p++ - '0';
}
v0 = (f ? -n : n);
pc0 = ra;
}
void psxBios_atol(void) { // 0x11
psxBios_atoi();
}
void psxBios_setjmp(void) { // 13
uint32_t *jmp_buf= (uint32_t*)Ra0;
int i;
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x13]);
#endif
jmp_buf[0] = ra;
jmp_buf[1] = sp;
jmp_buf[2] = fp;
for (i=0; i<8; i++) // s0-s7
jmp_buf[3+i] = psxRegs.GPR.r[16+i];
jmp_buf[11] = gp;
v0 = 0; pc0 = ra;
}
void psxBios_longjmp(void) { //14
uint32_t *jmp_buf= (uint32_t*)Ra0;
int i;
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x14]);
#endif
ra = jmp_buf[0]; /* ra */
sp = jmp_buf[1]; /* sp */
fp = jmp_buf[2]; /* fp */
for (i=0; i<8; i++) // s0-s7
psxRegs.GPR.r[16+i] = jmp_buf[3+i];
gp = jmp_buf[11]; /* gp */
v0 = a1; pc0 = ra;
}
void psxBios_strcat(void) { // 0x15
char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s: %s, %s\n", biosA0n[0x15], Ra0, Ra1);
#endif
if (a0 == 0 || a1 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
while (*p1++);
--p1;
while ((*p1++ = *p2++) != '\0');
v0 = a0; pc0 = ra;
}
void psxBios_strncat(void) { // 0x16
char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
int32_t n = a2;
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s: %s (%x), %s (%x), %d\n", biosA0n[0x16], Ra0, a0, Ra1, a1, a2);
#endif
if (a0 == 0 || a1 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
while (*p1++);
--p1;
while ((*p1++ = *p2++) != '\0') {
if (--n < 0) {
*--p1 = '\0';
break;
}
}
v0 = a0; pc0 = ra;
}
void psxBios_strcmp(void) { // 0x17
char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
int32_t n=0;
if (a0 == 0 && a1 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
else if (a0 == 0 && a1 != 0)
{
v0 = -1;
pc0 = ra;
return;
}
else if (a0 != 0 && a1 == 0)
{
v0 = 1;
pc0 = ra;
return;
}
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s: %s (%x), %s (%x)\n", biosA0n[0x17], Ra0, a0, Ra1, a1);
#endif
while (*p1 == *p2++) {
n++;
if (*p1++ == '\0') {
v1=n-1;
a0+=n;
a1+=n;
v0 = 0;
pc0 = ra;
return;
}
}
v0 = (*p1 - *--p2);
v1 = n;
a0+=n;
a1+=n;
pc0 = ra;
}
void psxBios_strncmp(void) { // 0x18
char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
int32_t n = a2;
if (a0 == 0 && a1 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
else if (a0 == 0 && a1 != 0)
{
v0 = -1;
pc0 = ra;
return;
}
else if (a0 != 0 && a1 == 0)
{
v0 = 1;
pc0 = ra;
return;
}
#ifdef PSXBIOS_LOG
PSXBIOS_LOG("psxBios_%s: %s (%x), %s (%x), %d\n", biosA0n[0x18], Ra0, a0, Ra1, a1, a2);
#endif
while (--n >= 0 && *p1 == *p2++) {
if (*p1++ == '\0') {
v0 = 0;
pc0 = ra;
v1 = a2 - ((a2-n) - 1);
a0 += (a2-n) - 1;
a1 += (a2-n) - 1;
a2 = n;
return;
}
}
v0 = (n < 0 ? 0 : *p1 - *--p2);
pc0 = ra;
v1 = a2 - ((a2-n) - 1);
a0 += (a2-n) - 1;
a1 += (a2-n) - 1;
a2 = n;
}
void psxBios_strcpy(void) { // 0x19
char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
if (a0 == 0 || a1 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
while ((*p1++ = *p2++) != '\0');
v0 = a0; pc0 = ra;
}
void psxBios_strncpy(void) { // 0x1a
char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
int32_t n = a2, i;
if (a0 == 0 || a1 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
for (i = 0; i < n; i++) {
if ((*p1++ = *p2++) == '\0') {
while (++i < n) {
*p1++ = '\0';
}
v0 = a0; pc0 = ra;
return;
}
}
v0 = a0; pc0 = ra;
}
void psxBios_strlen(void) { // 0x1b
char *p = (char *)Ra0;
v0 = 0;
if (a0 == 0)
{
pc0 = ra;
return;
}
while (*p++) v0++;
pc0 = ra;
}
void psxBios_index(void) { // 0x1c
char *p = (char *)Ra0;
if (a0 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
do {
if (*p == a1) {
v0 = a0 + (p - (char *)Ra0);
pc0 = ra;
return;
}
} while (*p++ != '\0');
v0 = 0; pc0 = ra;
}
void psxBios_rindex(void) { // 0x1d
char *p = (char *)Ra0;
v0 = 0;
if (a0 == 0)
{
pc0 = ra;
return;
}
do {
if (*p == a1)
v0 = a0 + (p - (char *)Ra0);
} while (*p++ != '\0');
pc0 = ra;
}
void psxBios_strchr(void) { // 0x1e
psxBios_index();
}
void psxBios_strrchr(void) { // 0x1f
psxBios_rindex();
}
void psxBios_strpbrk(void) { // 0x20
char *p1 = (char *)Ra0, *p2 = (char *)Ra1, *scanp, c, sc;
while ((c = *p1++) != '\0') {
for (scanp = p2; (sc = *scanp++) != '\0';) {
if (sc == c) {
v0 = a0 + (p1 - 1 - (char *)Ra0);
pc0 = ra;
return;
}
}
}
/*
If there was no occurence, it returns 0 only if src[0]=00h,
otherwise returns the incoming "src" value.
(which is the SAME return value as when a occurence did occur on 1st character).
*/
if (p1[0] == 0x00)
{
v0 = 0;
}
else
{
v0 = a0;
}
pc0 = ra;
}
void psxBios_strspn(void) { // 0x21
char *p1, *p2;
for (p1 = (char *)Ra0; *p1 != '\0'; p1++) {
for (p2 = (char *)Ra1; *p2 != '\0' && *p2 != *p1; p2++);
if (*p2 == '\0') break;
}
v0 = p1 - (char *)Ra0; pc0 = ra;
}
void psxBios_strcspn(void) { // 0x22
char *p1, *p2;
for (p1 = (char *)Ra0; *p1 != '\0'; p1++) {
for (p2 = (char *)Ra1; *p2 != '\0' && *p2 != *p1; p2++);
if (*p2 != '\0') break;
}
v0 = p1 - (char *)Ra0; pc0 = ra;
}
void psxBios_strtok(void) { // 0x23
char *pcA0 = (char *)Ra0;
char *pcRet = strtok(pcA0, (char *)Ra1);
if(pcRet)
v0 = a0 + pcRet - pcA0;
else
v0 = 0;
pc0 = ra;
}
void psxBios_strstr(void) { // 0x24
char *p = (char *)Ra0, *p1, *p2;
while (*p != '\0') {
p1 = p;
p2 = (char *)Ra1;
while (*p1 != '\0' && *p2 != '\0' && *p1 == *p2) {
p1++; p2++;
}
if (*p2 == '\0') {
v0 = a0 + (p - (char *)Ra0);
pc0 = ra;
return;
}
p++;
}
v0 = 0; pc0 = ra;
}
void psxBios_toupper(void) { // 0x25
v0 = (int8_t)(a0 & 0xff);
if (v0 >= 'a' && v0 <= 'z') v0 -= 'a' - 'A';
pc0 = ra;
}
void psxBios_tolower(void) { // 0x26
v0 = (int8_t)(a0 & 0xff);
if (v0 >= 'A' && v0 <= 'Z') v0 += 'a' - 'A';
pc0 = ra;
}
void psxBios_bcopy(void) { // 0x27
char *p1 = (char *)Ra1, *p2 = (char *)Ra0;
v0 = a0;
if (a0 == 0 || a2 > 0x7FFFFFFF)
{
pc0 = ra;
return;
}
while ((int32_t)a2-- > 0) *p1++ = *p2++;
a2 = 0;
pc0 = ra;
}
void psxBios_bzero(void) { // 0x28
char *p = (char *)Ra0;
v0 = a0;
/* Same as memset here (See memset below) */
if (a1 > 0x7FFFFFFF || a1 == 0)
{
v0 = 0;
pc0 = ra;
return;
}
else if (a0 == 0)
{
pc0 = ra;
return;
}
while ((int32_t)a1-- > 0) *p++ = '\0';
a1 = 0;
pc0 = ra;
}
void psxBios_bcmp() { // 0x29
char *p1 = (char *)Ra0, *p2 = (char *)Ra1;
if (a0 == 0 || a1 == 0) { v0 = 0; pc0 = ra; return; }
while ((int32_t)a2-- > 0) {
if (*p1++ != *p2++) {
v0 = *p1 - *p2; // BUG: compare the NEXT byte
pc0 = ra;
return;
}
}
v0 = 0; pc0 = ra;
}