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
|
/* *************************************
* Includes
* *************************************/
#include "System.h"
/* *************************************
* Defines
* *************************************/
#define FILE_BUFFER_SIZE 0x20014
#define SYSTEM_MAX_TIMERS 16
#define END_STACK_PATTERN (uint32_t) 0x18022015
#define BEGIN_STACK_ADDRESS (uint32_t*) 0x801FFF00
#define STACK_SIZE 0x1000
#define I_MASK (*(unsigned int*)0x1F801074)
/* *************************************
* Local Prototypes
* *************************************/
static void SystemCheckTimer(bool* timer, uint64_t* last_timer, uint8_t step);
static void SystemSetStackPattern(void);
static void SystemEnableVBlankInterrupt(void);
static void SystemDisableVBlankInterrupt(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;
//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;
//Timer array.
static TYPE_TIMER timer_array[SYSTEM_MAX_TIMERS];
/* *******************************************************************
*
* @name: void SystemInit(void)
*
* @author: Xavier Del Campo
*
* @brief: Calls main intialization routines.
*
* @remarks: To be called before main loop.
*
* *******************************************************************/
void SystemInit(void)
{
//Reset global timer
global_timer = 0;
//Reset 1 second timer
one_second_timer = 0;
//PSXSDK init
PSX_InitEx(PSX_INIT_SAVESTATE | PSX_INIT_CD);
//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
SystemResetTimers();
//Pads init
PadInit();
//Set Drawing Environment
GfxInitDrawEnv();
//Set Display Environment
GfxInitDispEnv();
//Set VBlank Handler for screen refresh
SetVBlankHandler(&ISR_SystemDefaultVBlank);
//Set Primitive List
GfxSetPrimitiveList();
// Init memory card
MemCardInit();
//Initial value for system_busy
system_busy = false;
GfxSetGlobalLuminance(NORMAL_LUMINANCE);
SystemSetStackPattern();
SetRCnt(RCntCNT2,0xFFFF,RCntSC);
StartRCnt(RCntCNT2);
}
/* *******************************************************************
*
* @name: void SystemInit(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);
dprintf("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)
{
refresh_needed = true;
SystemIncreaseGlobalTimer();
}
/* *******************************************************************
*
* @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: uint64_t SystemGetGlobalTimer(void)
*
* @author: Xavier Del Campo
*
* @brief: Returns internal global timer value.
*
* *******************************************************************/
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 == true)
{
*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(char* fname, uint8_t* buffer, uint32_t szBuffer)
{
FILE *f;
int32_t size;
// Wait for possible previous operation from the GPU before entering this section.
while( (SystemIsBusy() == true) || (GfxIsGPUBusy() == true) );
if(fname == NULL)
{
dprintf("SystemLoadFile: NULL fname!\n");
return false;
}
memset(buffer,0,szBuffer);
system_busy = true;
SystemDisableVBlankInterrupt();
f = fopen(fname, "r");
if(f == NULL)
{
dprintf("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)
{
dprintf("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();
system_busy = false;
dprintf("File \"%s\" loaded successfully!\n",fname);
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(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 SystemClearBuffer(void)
*
* @author: Xavier Del Campo
*
* @return: Fills internal buffer with zeros
*
* *****************************************************************/
void SystemClearBuffer(void)
{
memset(file_buffer, 0, sizeof(file_buffer));
}
/* ******************************************************************
*
* @name void SystemWaitCycles(uint32_t cycles)
*
* @author: Xavier Del Campo
*
* @return: halts program execution for n-"cycles"
*
* *****************************************************************/
void SystemWaitCycles(uint32_t cycles)
{
uint64_t currentTime = global_timer;
while(global_timer < (currentTime + cycles) );
}
/* ******************************************************************
*
* @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)
{
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(uint8_t value, uint8_t* buffer, 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(uint16_t value, uint16_t* buffer, size_t sz)
{
size_t i = 0;
for(i = 0; i < sz; i++)
{
if(buffer[i] == value)
{
return true;
}
}
return false;
}
/* ********************************************************************************************
*
* @name TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) )
*
* @author: Xavier Del Campo
*
* @brief: fills a TYPE_TIMER structure with input parameters
*
* @param: uint32_t t:
* Timeout value (1 unit = 10 ms)
* bool rf:
* Repeat flag
* void (*timer_callback)(void)
* Function to be called on timeout
*
* @return: pointer to TYPE_TIMER structure if filled correctly, NULL pointer otherwise.
*
* ********************************************************************************************/
TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) )
{
bool success = false;
uint8_t i;
if(t == 0)
{
dprintf("Cannot create timer with time == 0!\n");
return NULL;
}
for(i = 0; i < SYSTEM_MAX_TIMERS; i++)
{
if(timer_array[i].busy == false)
{
timer_array[i].Timeout_Callback = timer_callback;
timer_array[i].time = t;
timer_array[i].orig_time = t;
timer_array[i].repeat_flag = rf;
timer_array[i].busy = true;
success = true;
break;
}
}
if(success == false)
{
dprintf("Could not find any free timer!\n");
return NULL;
}
return &timer_array[i];
}
/* *******************************************
*
* @name void SystemResetTimers(void)
*
* @author: Xavier Del Campo
*
* @brief: reportedly, removes all timers.
*
* *******************************************/
void SystemResetTimers(void)
{
uint8_t i;
for(i = 0; i < SYSTEM_MAX_TIMERS; i++)
{
SystemTimerRemove(&timer_array[i]);
}
}
/* *****************************************************
*
* @name void SystemUserTimersHandler(void)
*
* @author: Xavier Del Campo
*
* @brief: reportedly, handles all available timers.
*
* @remarks: calls callback on timeout.
*
* *****************************************************/
void SystemUserTimersHandler(void)
{
uint8_t i;
for(i = 0; i < SYSTEM_MAX_TIMERS; i++)
{
if(timer_array[i].busy == true)
{
if(System100msTick() == true)
{
timer_array[i].time--;
if(timer_array[i].time == 0)
{
timer_array[i].Timeout_Callback();
if(timer_array[i].repeat_flag == true)
{
timer_array[i].time = timer_array[i].orig_time;
}
}
}
}
}
}
/* *********************************************************************
*
* @name void SystemUserTimersHandler(void)
*
* @author: Xavier Del Campo
*
* @brief: sets time left for TYPE_TIMER instance to initial value.
*
* @remarks: specially used when TYPE_TIMER.rf is enabled.
*
* *********************************************************************/
void SystemTimerRestart(TYPE_TIMER* timer)
{
timer->time = timer->orig_time;
dprintf("Time set to %d seconds, timer 0x%08X\n", timer->time, timer);
}
void SystemTimerRemove(TYPE_TIMER* timer)
{
timer->time = 0;
timer->orig_time = 0;
timer->Timeout_Callback = NULL;
timer->busy = false;
timer->repeat_flag = false;
}
bool SystemArrayCompare(unsigned short* arr1, unsigned short* arr2, size_t sz)
{
size_t i;
for(i = 0; i < sz; i++)
{
if(arr1[i] != arr2[i])
{
return false;
}
}
return true;
}
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));
dprintf("stackPercent: %d\n", stackPercent);
dprintf("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
}
void SystemCheckStack(void)
{
uint32_t * ptrStack = BEGIN_STACK_ADDRESS;
uint32_t data;
ptrStack -= STACK_SIZE;
data = (*ptrStack);
if(data != END_STACK_PATTERN)
{
dprintf("Stack overflow?\n");
while(1);
}
}
void SystemSetStackPattern(void)
{
uint32_t * ptrStack = BEGIN_STACK_ADDRESS;
ptrStack -= STACK_SIZE;
*ptrStack = END_STACK_PATTERN;
}
int32_t SystemIndexOfStringArray(char* str, char** array)
{
int32_t i;
for(i = 0; array[i] != NULL; i++)
{
dprintf("String to find: %s\nEntry: %s\n", str, array[i]);
if(strcmp(str, array[i]) == 0)
{
dprintf("Match! Returning index %d...\n", i);
return i;
}
}
return -1;
}
int32_t SystemIndexOf_U16(uint16_t value, uint16_t* array, uint32_t sz)
{
int32_t i;
for(i = 0; i < sz; i++)
{
if(value == array[i])
{
return i;
}
}
return -1;
}
int32_t SystemIndexOf_U8(uint8_t value, uint8_t* array, uint32_t from, uint32_t sz)
{
int32_t i;
for(i = from; i < sz; i++)
{
if(value == array[i])
{
return i;
}
}
return -1;
}
void SystemCyclicHandler(void)
{
if(UpdatePads() == false)
{
SystemSetEmergencyMode(true);
}
else
{
SystemSetEmergencyMode(false);
}
SystemRunTimers();
SystemUserTimersHandler();
SystemDisableScreenRefresh();
MemCardHandler();
SystemCheckStack();
}
void SystemDisableVBlankInterrupt(void)
{
I_MASK &= ~(0x0001);
}
void SystemEnableVBlankInterrupt(void)
{
I_MASK |= (0x0001);
}
|