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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
|
/* *************************************
* Includes
* *************************************/
#include "System.h"
#include "Pad.h"
#include "Menu.h"
#include "Gfx.h"
#include "MemCard.h"
#include "EndAnimation.h"
#include "Timer.h"
/* *************************************
* Defines
* *************************************/
#define FILE_BUFFER_SIZE (128 << 10) // 128 KB
#define END_STACK_PATTERN (uint32_t) 0x18022015
#define BEGIN_STACK_ADDRESS (uint32_t*) 0x801FFF00
#define STACK_SIZE (6 << 10) // 6 KiB
#define I_MASK (*(volatile unsigned int*)0x1F801074)
#define I_STAT (*(volatile unsigned int*)0x1F801070)
/* *************************************
* Local Prototypes
* *************************************/
static void SystemCheckTimer(bool* timer, uint64_t* last_timer, uint8_t step);
static void SystemSetStackPattern(void);
static void ISR_RootCounter2(void);
/* *************************************
* Local Variables
* *************************************/
//Buffer to store any kind of files. It supports files up to 128 kB
static uint8_t file_buffer[FILE_BUFFER_SIZE];
//Global timer (called by interrupt)
static volatile uint64_t global_timer;
//Tells whether rand seed has been set
static bool rand_seed;
//Screen refresh flag (called by interrupt)
static volatile bool refresh_needed;
// Frames per second measurement
static volatile uint8_t fps;
static volatile uint8_t temp_fps;
//Timers
static bool one_second_timer;
static bool hundred_ms_timer;
static bool five_hundred_ms_timer;
//Emergency mode flag. Toggled on pad connected/disconnected
static bool emergency_mode;
//Critical section is entered (i.e.: when accessing fopen() or other BIOS functions
static volatile bool system_busy;
// When true, it draws a rectangle on top of all primitives with
// information for development purposes.
static bool devmenu_flag;
// Used for sine-like effect.
static unsigned char sine_counter;
/* *******************************************************************
*
* @name: void SystemInit(void)
*
* @author: Xavier Del Campo
*
* @brief: Calls main intialization routines.
*
* @remarks: To be called before main loop.
*
* *******************************************************************/
void SystemInit(void)
{
enum
{
RCNT2_100US_TICK_COUNTER = 0xA560
};
//Reset global timer
global_timer = 0;
//Reset 1 second timer
one_second_timer = 0;
//PSXSDK init
#ifdef SERIAL_INTERFACE
// PSX_INIT_SAVESTATE | PSX_INIT_CD flags are not needed
// when coming from OpenSend.
PSX_InitEx(0);
#else // SERIAL_INTERFACE
PSX_InitEx(PSX_INIT_SAVESTATE | PSX_INIT_CD);
#endif // SERIAL_INTERFACE
// SIO init
SerialInit();
//Graphics init
GsInit();
//Clear VRAM
GsClearMem();
//Set Video Resolution
#ifdef _PAL_MODE_
GsSetVideoMode(X_SCREEN_RESOLUTION, Y_SCREEN_RESOLUTION, VMODE_PAL);
#else
GsSetVideoMode(X_SCREEN_RESOLUTION, Y_SCREEN_RESOLUTION, VMODE_NTSC);
#endif //_PAL_MODE_
//SPU init
SsInit();
//Reset all user-handled timers
TimerReset();
//Pads init
PadInit();
//Set Drawing Environment
GfxInitDrawEnv();
//Set Display Environment
GfxInitDispEnv();
//Set VBlank Handler for screen refresh
SetVBlankHandler(&ISR_SystemDefaultVBlank);
//Set Primitive List
GfxSetDefaultPrimitiveList();
// Init memory card
MemCardInit();
//Initial value for system_busy
system_busy = false;
//Development menu flag
devmenu_flag = false;
//Emergency mode flag
emergency_mode = false;
GfxSetGlobalLuminance(NORMAL_LUMINANCE);
SystemSetStackPattern();
// Configure root counter 2 so that ISR_RootCounter2
// is executed every 100 us.
SetRCntHandler(&ISR_RootCounter2, 2, RCNT2_100US_TICK_COUNTER);
SystemEnableRCnt2Interrupt();
}
static volatile uint16_t u16_0_01seconds_cnt;
static volatile uint16_t u16_0_01seconds_cnt_prev;
/* *******************************************************************
*
* @name: void ISR_RootCounter2(void)
*
* @author: Xavier Del Campo
*
* @brief:
* Executed on RCnt2 ISR every 100 us.
*
* *******************************************************************/
void ISR_RootCounter2(void)
{
u16_0_01seconds_cnt++;
if ((int16_t)(u16_0_01seconds_cnt - 1000) >= (int16_t)(u16_0_01seconds_cnt_prev))
{
u16_0_01seconds_cnt_prev = u16_0_01seconds_cnt;
//~ DEBUG_PRINT_VAR(u16_0_01seconds_cnt_prev);
}
}
/* *******************************************************************
*
* @name: void SystemSetRandSeed(void)
*
* @author: Xavier Del Campo
*
* @brief:
* Calls srand() while avoiding multiple calls by setting internal
* variable rand_seed to true. Internal variable "global_timer" is
* used to generate the new seed.
*
* @remarks:
* It is recommended to call it once user has pressed any key.
*
* *******************************************************************/
void SystemSetRandSeed(void)
{
if (rand_seed == false)
{
rand_seed = true;
//Set random seed using global timer as reference
srand((unsigned int)global_timer ^ GetRCnt(2));
Serial_printf("Seed used: %d\n",(unsigned int)global_timer);
}
}
/* *******************************************************************
*
* @name: bool SystemIsRandSeedSet(void)
*
* @author: Xavier Del Campo
*
* @brief:
* Reportedly, returns whether rand seed has already been set.
*
* @remarks:
*
* @return:
* Reportedly, returns whether rand seed has already been set.
*
* *******************************************************************/
bool SystemIsRandSeedSet(void)
{
return rand_seed;
}
/* *******************************************************************
*
* @name: bool SystemRefreshNeeded(void)
*
* @author: Xavier Del Campo
*
* @brief:
*
* @remarks:
*
* @return:
* Returns whether VSync flag has been enabled.
*
* *******************************************************************/
bool SystemRefreshNeeded(void)
{
return refresh_needed;
}
/* *******************************************************************
*
* @name: void ISR_SystemDefaultVBlank(void)
*
* @author: Xavier Del Campo
*
* @brief:
*
* @remarks:
* Called from VSync interrupt. Called 50 times a second in PAL mode,
* 60 times a second in NTSC mode.
*
* *******************************************************************/
void ISR_SystemDefaultVBlank(void)
{
if (System1SecondTick())
{
fps = temp_fps;
temp_fps = 0;
}
refresh_needed = true;
}
/* *******************************************************************
*
* @name: void SystemAcknowledgeFrame(void)
*
* @author: Xavier Del Campo
*
* @brief:
*
* @remarks:
* Called by Game module in order to calculate frames per second.
*
* *******************************************************************/
void SystemAcknowledgeFrame(void)
{
temp_fps++;
}
/* *******************************************************************
*
* @name: void SystemAcknowledgeFrame(void)
*
* @author: Xavier Del Campo
*
* @brief:
* Creates a sine-like (more exactly, a sawtooth-like) effect and
* stores its value into a variable.
*
* @remarks:
* To be called only once, preferibly on SystemCyclic().
*
* *******************************************************************/
void SystemCalculateSine(void)
{
enum
{
SINE_EFFECT_STEP = 24,
SINE_EFFECT_MAX = 240
};
static bool sine_decrease = false;
if (sine_decrease == false)
{
if (sine_counter < SINE_EFFECT_MAX)
{
sine_counter += SINE_EFFECT_STEP;
}
else
{
sine_decrease = true;
}
}
else
{
if (sine_counter > SINE_EFFECT_STEP)
{
sine_counter -= SINE_EFFECT_STEP;
}
else
{
sine_decrease = false;
}
}
}
/* *******************************************************************
*
* @name: unsigned char SystemGetSineValue(void)
*
* @author: Xavier Del Campo
*
* @return:
* Returns a value which oscillates like a sine (more exactly, like
* a parabola) function to be used wherever you want.
*
* *******************************************************************/
unsigned char SystemGetSineValue(void)
{
return sine_counter;
}
/* *******************************************************************
*
* @name: void SystemIncreaseGlobalTimer(void)
*
* @author: Xavier Del Campo
*
* @brief:
* Increases internal variable responsible for time handling.
*
* @remarks:
* Usually called from ISR_SystemDefaultVBlank().
*
* *******************************************************************/
void SystemIncreaseGlobalTimer(void)
{
global_timer++;
}
/* *******************************************************************
*
* @name: volatile uint64_t SystemGetGlobalTimer(void)
*
* @author: Xavier Del Campo
*
* @brief: Returns internal global timer value.
*
* *******************************************************************/
volatile uint64_t SystemGetGlobalTimer(void)
{
return global_timer;
}
/* *******************************************************************
*
* @name: void SystemDisableScreenRefresh(void)
*
* @author: Xavier Del Campo
*
* @brief: Resets VBlank IRQ flag.
*
* *******************************************************************/
void SystemDisableScreenRefresh(void)
{
refresh_needed = false;
}
/* *******************************************************************
*
* @name: bool System1SecondTick(void)
*
* @author: Xavier Del Campo
*
* @return: bool variable with a 1-cycle-length pulse that gets
* set each second.
*
* *******************************************************************/
bool System1SecondTick(void)
{
return one_second_timer;
}
/* *******************************************************************
*
* @name: bool System100msTick(void)
*
* @author: Xavier Del Campo
*
* @return: bool variable with a 1-cycle-length pulse that gets
* set every 100 milliseconds.
*
* *******************************************************************/
bool System100msTick(void)
{
return hundred_ms_timer;
}
/* *******************************************************************
*
* @name bool System500msTick(void)
*
* @author: Xavier Del Campo
*
* @return: bool variable with a 1-cycle-length pulse that gets
* set every 500 milliseconds.
*
* *******************************************************************/
bool System500msTick(void)
{
return five_hundred_ms_timer;
}
/* *******************************************************************
*
* @name void SystemRunTimers(void)
*
* @author: Xavier Del Campo
*
* @brief: general timer handler
*
* @remarks: 1 second, 500 ms and 100 ms ticks get updated here.
*
* *******************************************************************/
void SystemRunTimers(void)
{
static uint64_t last_one_second_tick;
static uint64_t last_100_ms_tick;
static uint64_t last_500_ms_tick;
SystemCheckTimer(&one_second_timer, &last_one_second_tick, REFRESH_FREQUENCY);
#ifdef _PAL_MODE_
SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 2 /* 2 * 50 ms = 100 ms */);
SystemCheckTimer(&five_hundred_ms_timer, &last_500_ms_tick, 10 /* 10 * 50 ms = 500 ms */);
#else // _PAL_MODE_
SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 3);
#endif // _PAL_MODE_
}
/* ********************************************************************************
*
* @name void SystemCheckTimer(bool* timer, uint64_t* last_timer, uint8_t step)
*
* @author: Xavier Del Campo
*
* @brief: Checks if needed time step has been elapsed. If true, flag gets set.
*
* *******************************************************************************/
void SystemCheckTimer(bool* timer, uint64_t* last_timer, uint8_t step)
{
if (*timer)
{
*timer = false;
}
if (global_timer >= (*last_timer + step) )
{
*timer = true;
*last_timer = global_timer;
}
}
/* ****************************************************************************************
*
* @name bool SystemLoadFileToBuffer(char* fname, uint8_t* buffer, uint32_t szBuffer)
*
* @author: Xavier Del Campo
*
* @brief: Given an input path, it fills a buffer pointed to by "buffer" with
* maximum size "szBuffer" with data from CD-ROM.
*
* @return: true if file has been loaded successfully, false otherwise.
*
* ****************************************************************************************/
bool SystemLoadFileToBuffer(const char* fname, uint8_t* buffer, uint32_t szBuffer)
{
#ifdef SERIAL_INTERFACE
uint8_t fileSizeBuffer[sizeof (uint32_t)] = {0};
uint32_t i;
#else // SERIAL_INTERFACE
FILE *f;
#endif // SERIAL_INTERFACE
int32_t size = 0;
// Wait for possible previous operation from the GPU before entering this section.
while ( (SystemIsBusy()) || (GfxIsGPUBusy()) );
SystemDisableRCnt2Interrupt();
if (fname == NULL)
{
Serial_printf("SystemLoadFile: NULL fname!\n");
return false;
}
memset(buffer,0,szBuffer);
static char completeFileName[256];
snprintf(completeFileName, sizeof (completeFileName), "cdrom:\\%s;1", fname);
#ifdef SERIAL_INTERFACE
Serial_printf("#%s@", completeFileName);
SerialRead(fileSizeBuffer, sizeof (uint32_t) );
for (i = 0; i < sizeof (uint32_t); i++)
{
size |= fileSizeBuffer[i] << (i << 3); // (i << 3) == (i * 8)
}
SerialWrite(ACK_BYTE_STRING, 1);
for (i = 0; i < size; i += SERIAL_DATA_PACKET_SIZE)
{
uint32_t bytes_to_read;
// Read actual EXE data into proper RAM address.
if ( (i + SERIAL_DATA_PACKET_SIZE) >= size)
{
bytes_to_read = size - i;
}
else
{
bytes_to_read = SERIAL_DATA_PACKET_SIZE;
}
SerialRead(file_buffer + i, bytes_to_read);
SerialWrite(ACK_BYTE_STRING, sizeof (uint8_t)); // Write ACK
}
#else // SERIAL_INTERFACE
system_busy = true;
SystemDisableVBlankInterrupt();
Serial_printf("Opening %s...\n", completeFileName);
f = fopen((char*)completeFileName, "r");
if (f == NULL)
{
Serial_printf("SystemLoadFile: file could not be found!\n");
//File couldn't be found
return false;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
if (size > szBuffer)
{
Serial_printf("SystemLoadFile: Exceeds file buffer size (%d bytes)\n",size);
//Bigger than 128 kB (buffer's max size)
return false;
}
fseek(f, 0, SEEK_SET); //f->pos = 0;
fread(buffer, sizeof (char), size, f);
fclose(f);
SystemEnableVBlankInterrupt();
SystemEnableRCnt2Interrupt();
system_busy = false;
#endif // SERIAL_INTERFACE
Serial_printf("File \"%s\" loaded successfully!\n",completeFileName);
return true;
}
/* ****************************************************************************************
*
* @name bool SystemLoadFile(char*fname)
*
* @author: Xavier Del Campo
*
* @brief: Given an input file name, it loads its conents into internal buffer.
*
* @return: true if file has been loaded successfully, false otherwise.
*
* ****************************************************************************************/
bool SystemLoadFile(const char* fname)
{
return SystemLoadFileToBuffer(fname,file_buffer,sizeof (file_buffer));
}
/* ******************************************************************
*
* @name uint8_t* SystemGetBufferAddress(void)
*
* @author: Xavier Del Campo
*
* @return: Reportedly, returns internal buffer initial address.
*
* *****************************************************************/
uint8_t* SystemGetBufferAddress(void)
{
return file_buffer;
}
/* ******************************************************************
*
* @name void SystemClearFileBuffer(void)
*
* @author: Xavier Del Campo
*
* @return: Fills internal buffer with zeros
*
* *****************************************************************/
void SystemClearFileBuffer(void)
{
memset(file_buffer, 0, sizeof (file_buffer));
}
/* ******************************************************************
*
* @name uint32_t SystemRand(uint32_t min, uint32_t max)
*
* @author: Xavier Del Campo
*
* @return: random number between "min" and "max".
*
* @remarks: rand seed must be set before using this function, or
* you will predictable values otherwise!
*
* *****************************************************************/
uint32_t SystemRand(uint32_t min, uint32_t max)
{
if (rand_seed == false)
{
//~ Serial_printf("Warning: calling rand() before srand()\n");
}
return rand() % (max - min + 1) + min;
}
/* ***********************************************************************
*
* @name void SystemSetEmergencyMode(bool value)
*
* @author: Xavier Del Campo
*
* @brief: Sets emergency mode flag.
*
* @remarks: emergency mode is set once that a controller is unplugged.
*
* ***********************************************************************/
void SystemSetEmergencyMode(bool value)
{
emergency_mode = value;
}
/* ***********************************************************************
*
* @name bool SystemGetEmergencyMode(void)
*
* @author: Xavier Del Campo
*
* @return: returns emergency mode flag.
*
* ***********************************************************************/
bool SystemGetEmergencyMode(void)
{
return emergency_mode;
}
/* ***********************************************************************
*
* @name volatile bool SystemIsBusy(void)
*
* @author: Xavier Del Campo
*
* @return: returns system busy flag.
*
* ***********************************************************************/
volatile bool SystemIsBusy(void)
{
return system_busy;
}
/* ****************************************************************************
*
* @name bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz)
*
* @author: Xavier Del Campo
*
* @brief: checks if a certain value is contained in a buffer with size "sz".
*
* @return: true if value is contained inside buffer, false otherwise.
*
* ****************************************************************************/
bool SystemContains_u8(const uint8_t value, const uint8_t* const buffer, const size_t sz)
{
size_t i = 0;
for (i = 0; i < sz; i++)
{
if (buffer[i] == value)
{
return true;
}
}
return false;
}
/* ****************************************************************************
*
* @name bool SystemContains_u16(uint16_t value, uint16_t* buffer, size_t sz)
*
* @author: Xavier Del Campo
*
* @brief: checks if a certain value is contained in a buffer with size "sz".
* Variant for u16 variables.
*
* @return: true if value is contained inside buffer, false otherwise.
*
* ****************************************************************************/
bool SystemContains_u16(const uint16_t value, const uint16_t* const buffer, const size_t sz)
{
size_t i = 0;
for (i = 0; i < sz; i++)
{
if (buffer[i] == value)
{
return true;
}
}
return false;
}
/* ****************************************************************************************
*
* @name bool SystemArrayCompare(unsigned short* arr1, unsigned short* arr2, size_t sz)
*
* @author: Xavier Del Campo
*
* @brief: Reportedly, it compares two arrays "arr1" and "arr2", with size "sz".
*
* @return: true if they are equal, false otherwise.
*
* ****************************************************************************************/
bool SystemArrayCompare(const unsigned short* const arr1, const unsigned short* const arr2, const size_t sz)
{
size_t i;
for (i = 0; i < sz; i++)
{
if (arr1[i] != arr2[i])
{
return false;
}
}
return true;
}
/* ****************************************************************************************
*
* @name void SystemPrintStackPointerAddress(void)
*
* @author: Xavier Del Campo
*
* @brief: Prints stack usage in percentage via dprintf calls.
*
* ****************************************************************************************/
void SystemPrintStackPointerAddress(void)
{
#ifdef PSXSDK_DEBUG // Used to avoid unused variable warning
void* ptr = NULL;
fix16_t used_bytes = fix16_from_int((int)((void*)BEGIN_STACK_ADDRESS - (void*)&ptr));
fix16_t stackPercent = fix16_sdiv(used_bytes,fix16_from_int((int)STACK_SIZE));
stackPercent = fix16_smul(stackPercent, fix16_from_int((int)100));
Serial_printf("stackPercent: %d\n", stackPercent);
Serial_printf("Stack begin pointer: 0x%08X\n"
"Stack pointer address: 0x%08X\n"
"Used %d%% of stack size.\n"
"\tUsed bytes: %d\n",
(void*)BEGIN_STACK_ADDRESS,
(void*)&ptr,
fix16_to_int(stackPercent),
fix16_to_int(used_bytes) );
#endif // PSXSDK_DEBUG
}
/* ****************************************************************************************
*
* @name void SystemCheckStack(void)
*
* @author: Xavier Del Campo
*
* @brief: Compares stack top with expected byte pattern. If does not match, a stack
* overflow has been caused, and application returns to a safe state.
*
* ****************************************************************************************/
void SystemCheckStack(void)
{
const uint32_t* const ptrStack = BEGIN_STACK_ADDRESS - STACK_SIZE;
const uint32_t data = *ptrStack;
if (data != END_STACK_PATTERN)
{
Serial_printf("Stack overflow?\n");
while (1);
}
}
/* ****************************************************************************************
*
* @name void SystemSetStackPattern(void)
*
* @author: Xavier Del Campo
*
* @brief: Sets a determined byte pattern on stack top to detect possible stack
* overflow during execution.
*
* ****************************************************************************************/
void SystemSetStackPattern(void)
{
uint32_t* const ptrStack = BEGIN_STACK_ADDRESS - STACK_SIZE;
*ptrStack = END_STACK_PATTERN;
}
/* ****************************************************************************************
*
* @name int32_t SystemIndexOfStringArray(char* str, char** array)
*
* @author: Xavier Del Campo
*
* @brief: Finds string "str" inside an array of strings "array".
*
* @return Index for a string "str" inside "array". -1 if it could not be found.
*
* ****************************************************************************************/
int32_t SystemIndexOfStringArray(const char* str, const char* const* array)
{
int32_t i;
for (i = 0; array[i] != NULL; i++)
{
Serial_printf("String to find: %s\nEntry: %s\n", str, array[i]);
if (strcmp(str, array[i]) == 0)
{
Serial_printf("Match! Returning index %d...\n", i);
return i;
}
}
return -1;
}
/* ****************************************************************************************
*
* @name int32_t SystemIndexOf_U16(uint16_t value, uint16_t* array, uint32_t sz)
*
* @author: Xavier Del Campo
*
* @brief: For a uint16_t array, it returns index of a variable "value" inside an array.
*
* @return Index for a variable "value" inside "array". -1 if it could not be found.
*
* ****************************************************************************************/
int32_t SystemIndexOf_U16(const uint16_t value, const uint16_t* const array, const uint32_t sz)
{
int32_t i;
for (i = 0; i < sz; i++)
{
if (value == array[i])
{
return i;
}
}
return -1;
}
/* ****************************************************************************************
*
* @name int32_t SystemIndexOf_U8(uint8_t value, uint8_t* array, uint32_t from, uint32_t sz)
*
* @author: Xavier Del Campo
*
* @brief: For a uint8_t array, it returns index of a variable "value" inside an array.
* "from" and "size_t" can be used to determine initial/ending positions.
*
* @return Index for a variable "value" inside "array". -1 if it could not be found.
*
* ****************************************************************************************/
int32_t SystemIndexOf_U8(const uint8_t value, const uint8_t* const array, const uint32_t from, const uint32_t sz)
{
int32_t i;
for (i = from; i < sz; i++)
{
if (value == array[i])
{
return i;
}
}
return -1;
}
/* ****************************************************************************************
*
* @name volatile uint8_t SystemGetFPS(void)
*
* @author: Xavier Del Campo
*
* @return: Frames per second
*
* ****************************************************************************************/
volatile uint8_t SystemGetFPS(void)
{
return fps;
}
/* ****************************************************************************************
*
* @name void SystemCyclicHandler(void)
*
* @author: Xavier Del Campo
*
* @brief: It calls system handlers once an execution cycle has finished.
*
*
* ****************************************************************************************/
void SystemCyclicHandler(void)
{
UpdatePads();
SystemIncreaseGlobalTimer();
SystemRunTimers();
TimerHandler();
SystemDisableScreenRefresh();
MemCardHandler();
SystemCalculateSine();
SystemCheckStack();
}
/* ****************************************************************************************
*
* @name void SystemDisableVBlankInterrupt(void)
*
* @author: Xavier Del Campo
*
* @brief: Reportedly, this routine enables VBLANK interrupt flag.
*
* @remark: Used when critical timing is needed or GPU activity is not desired
* e.g.: when reading files from CD-ROM.
*
* ****************************************************************************************/
void SystemDisableVBlankInterrupt(void)
{
I_MASK &= ~1;
}
/* ****************************************************************************************
*
* @name void SystemEnableVBlankInterrupt(void)
*
* @author: Xavier Del Campo
*
* @brief: Reportedly, this routine enables VBLANK interrupt flag.
*
*
* ****************************************************************************************/
void SystemEnableVBlankInterrupt(void)
{
I_MASK |= 1;
}
/* ****************************************************************************************
*
* @name void SystemReturnToLoader(void)
*
* @author: Xavier Del Campo
*
* @brief: Deinitializes PSXSDK library and returns to OpenSend loader,
* located at memory address 0x801A0000
*
* ****************************************************************************************/
void SystemReturnToLoader(void)
{
Serial_printf("Returning to loader...\n");
EndAnimation();
PSX_DeInit();
__asm__("j 0x801A0000");
}
/* ****************************************************************************************
*
* @name void SystemDevMenuToggle(void)
*
* @author: Xavier Del Campo
*
* @brief: It toggles a flag called "devmenu_flag" which, if true, shows information on
* top of all drawn primitives for debugging/development purposes.
*
* ****************************************************************************************/
void SystemDevMenuToggle(void)
{
devmenu_flag = devmenu_flag? false: true;
}
/* ****************************************************************************************
*
* @name void SystemEnableRCnt2Interrupt(void)
*
* @author: Xavier Del Campo
*
* @brief: Enables bit 6 from I_MASK (0x1F801074)/IRQ6 RCNT2 (System clock / 8)
*
* ****************************************************************************************/
void SystemEnableRCnt2Interrupt(void)
{
I_MASK |= 1<<6;
}
/* ****************************************************************************************
*
* @name void SystemDisableRCnt2Interrupt(void)
*
* @author: Xavier Del Campo
*
* @brief: Disables bit 6 from I_MASK (0x1F801074)/IRQ6 RCNT2 (System clock / 8)
*
* ****************************************************************************************/
void SystemDisableRCnt2Interrupt(void)
{
I_MASK &= ~(1 << 6);
}
/* ****************************************************************************************
*
* @name void SystemDevMenu(void)
*
* @author: Xavier Del Campo
*
* @brief: Shows information on top of all drawn primitives for debugging/development purposes.
*
* ****************************************************************************************/
void SystemDevMenu(void)
{
enum
{
DEVMENU_BG_W = 256,
DEVMENU_BG_X = (X_SCREEN_RESOLUTION >> 1) - (DEVMENU_BG_W >> 1),
DEVMENU_BG_Y = 32,
DEVMENU_BG_H = 128,
DEVMENU_BG_R = 0,
DEVMENU_BG_G = 128,
DEVMENU_BG_B = 32,
};
enum
{
DEVMENU_TEXT_GAP = 8,
DEVMENU_PAD1_STATUS_TEXT_X = DEVMENU_BG_X + DEVMENU_TEXT_GAP,
DEVMENU_PAD1_STATUS_TEXT_Y = DEVMENU_BG_Y + DEVMENU_TEXT_GAP,
DEVMENU_PAD1_TYPE_TEXT_X = DEVMENU_PAD1_STATUS_TEXT_X,
DEVMENU_PAD1_TYPE_TEXT_Y = DEVMENU_PAD1_STATUS_TEXT_Y + DEVMENU_TEXT_GAP,
DEVMENU_PAD1_ID_TEXT_X = DEVMENU_PAD1_STATUS_TEXT_X,
DEVMENU_PAD1_ID_TEXT_Y = DEVMENU_PAD1_TYPE_TEXT_Y + DEVMENU_TEXT_GAP,
DEVMENU_PAD1_RAW_DATA_TEXT_X = DEVMENU_PAD1_ID_TEXT_X,
DEVMENU_PAD1_RAW_DATA_TEXT_Y = DEVMENU_PAD1_ID_TEXT_Y + DEVMENU_TEXT_GAP,
DEVMENU_PAD2_STATUS_TEXT_X = DEVMENU_PAD1_RAW_DATA_TEXT_X,
DEVMENU_PAD2_STATUS_TEXT_Y = DEVMENU_PAD1_RAW_DATA_TEXT_Y + (DEVMENU_TEXT_GAP << 1), // Leave a bigger gap here
DEVMENU_PAD2_TYPE_TEXT_X = DEVMENU_PAD2_STATUS_TEXT_X,
DEVMENU_PAD2_TYPE_TEXT_Y = DEVMENU_PAD2_STATUS_TEXT_Y + DEVMENU_TEXT_GAP,
DEVMENU_PAD2_ID_TEXT_X = DEVMENU_PAD2_TYPE_TEXT_X,
DEVMENU_PAD2_ID_TEXT_Y = DEVMENU_PAD2_TYPE_TEXT_Y + DEVMENU_TEXT_GAP,
DEVMENU_PAD2_RAW_DATA_TEXT_X = DEVMENU_PAD2_ID_TEXT_X,
DEVMENU_PAD2_RAW_DATA_TEXT_Y = DEVMENU_PAD2_ID_TEXT_Y + DEVMENU_TEXT_GAP,
DEVMENU_ROOTCNT2_TEXT_X = DEVMENU_PAD2_RAW_DATA_TEXT_X,
DEVMENU_ROOTCNT2_TEXT_Y = DEVMENU_PAD2_RAW_DATA_TEXT_Y + DEVMENU_TEXT_GAP,
};
if (devmenu_flag)
{
static const GsRectangle devMenuBg =
{
.x = DEVMENU_BG_X,
.y = DEVMENU_BG_Y,
.w = DEVMENU_BG_W,
.h = DEVMENU_BG_H,
.r = DEVMENU_BG_R,
.g = DEVMENU_BG_G,
.b = DEVMENU_BG_B,
.attribute = ENABLE_TRANS | TRANS_MODE(0)
};
GsSortRectangle((GsRectangle*)&devMenuBg);
FontPrintText( &SmallFont,
DEVMENU_PAD1_STATUS_TEXT_X,
DEVMENU_PAD1_STATUS_TEXT_Y,
"Pad1 connected = %d",
PadOneConnected() );
FontPrintText( &SmallFont,
DEVMENU_PAD1_TYPE_TEXT_X,
DEVMENU_PAD1_TYPE_TEXT_Y,
"Pad1 type = 0x%02X",
PadOneGetType() );
FontPrintText( &SmallFont,
DEVMENU_PAD1_ID_TEXT_X,
DEVMENU_PAD1_ID_TEXT_Y,
"Pad1 ID = 0x%02X",
PadOneGetID() );
FontPrintText( &SmallFont,
DEVMENU_PAD1_RAW_DATA_TEXT_X,
DEVMENU_PAD1_RAW_DATA_TEXT_Y,
"Pad1 raw data = 0x%04X",
PadOneGetRawData() );
FontPrintText( &SmallFont,
DEVMENU_PAD2_STATUS_TEXT_X,
DEVMENU_PAD2_STATUS_TEXT_Y,
"Pad2 connected = %d",
PadTwoConnected() );
FontPrintText( &SmallFont,
DEVMENU_PAD2_TYPE_TEXT_X,
DEVMENU_PAD2_TYPE_TEXT_Y,
"Pad2 type = 0x%02X",
PadTwoGetType() );
FontPrintText( &SmallFont,
DEVMENU_PAD2_ID_TEXT_X,
DEVMENU_PAD2_ID_TEXT_Y,
"Pad2 ID = 0x%02X",
PadTwoGetID() );
FontPrintText( &SmallFont,
DEVMENU_PAD2_RAW_DATA_TEXT_X,
DEVMENU_PAD2_RAW_DATA_TEXT_Y,
"Pad2 raw data = 0x%04X",
PadTwoGetRawData() );
/*FontPrintText( &SmallFont,
DEVMENU_ROOTCNT2_TEXT_X,
DEVMENU_ROOTCNT2_TEXT_Y,
"Timer2 = 0x%04X",
GetRCnt(2) );*/
FontPrintText( &SmallFont,
DEVMENU_ROOTCNT2_TEXT_X,
DEVMENU_ROOTCNT2_TEXT_Y,
"Timer2 = 0x%04X, timer2 = 0x%04X",
u16_0_01seconds_cnt, GetRCnt(2) );
}
}
void SystemGetFileBasename(const char* fileName, char* str, const size_t sz)
{
size_t i;
size_t j = 0;
const size_t len = strlen(fileName);
memset(str, 0, sz);
for (i = (len - 1); fileName[i] != '\\'; i--);
i++; // Skip '\\' character.
for (; fileName[i]; i++)
{
str[j++] = fileName[i];
}
}
|