forked from LemonHaze420/UEFAStriker96
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGENERAL.C
752 lines (549 loc) · 13.9 KB
/
GENERAL.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
/********************************************************************
* *
* *
* General functions , generic to any machine *
* *
* *
********************************************************************/
#include "incs.h"
#ifdef PSX
#include "..\..\global\striker.def" // defs for PSX striker
#include "..\..\striker\frontend\mod.def"
#include "..\..\striker\src\anim.def"
//#include "..\..\striker\movies\fruit\mod.def"
//#include "..\..\striker\movies\hotdog\mod.def"
#include "..\..\global\s01.h" // structs used by scene "s01" format
#include "sod.h"
#include "scene.h"
#include "datafile.h"
#include "sprite.h"
#include "frontend.h"
#include "gadget.h"
#include "hardware.h"
#include "polydraw.h" //psx structs
#include "font.h"
#include "text.h"
#include "plasma.h"
#include "render.h"
#include "anim.h"
//#include "test.h"
#include "mem.h"
#include "fvars.h"
#include "pad.h"
#include "camera.h"
#include "light.h"
#include "utils.h"
#include "io.h"
//#include "sound.h"
//#include "teamsel.h"
#include "options.h"
//#include "..\..\global\image.h"
#endif
/********************************************************************
* *
* Address list executor *
* *
********************************************************************/
#define MAX_PROCESS_NESTS 5
void ( *process_list[MAX_PROCESSES])(void);
void ( *store_process_list[ MAX_PROCESS_NESTS ][MAX_PROCESSES])(void);
WORD current_process_nest;
//UWORD current_process;
void init_process_executor()
{
WORD cnt;
// process_list[0]=0;
// current_process=0;
for ( cnt=0;cnt< MAX_PROCESSES; cnt++ )
{
process_list[cnt]=0;
}
current_process_nest=0;
}
void *add_process( void *addr )
{
WORD cnt;
for ( cnt=0;cnt<MAX_PROCESSES;cnt++ )
{
if ( process_list[cnt] == 0 )
{
process_list[ cnt ] = addr;
return &process_list[ cnt ];
}
}
display_error ( "add_process\nAll processes active");
return 0;
}
void store_processes()
{
WORD cnt;
//printf("storing processes to nest %d\n",current_process_nest);
// make a copy of all processes currently used
// *** NOTE - now, can be nested upto 5 levels ***
for ( cnt=0;cnt<MAX_PROCESSES;cnt++ )
{
store_process_list[ current_process_nest ][cnt] = process_list[cnt];
}
current_process_nest++;
if ( current_process_nest >= MAX_PROCESS_NESTS )
display_error( "Too many process nests" );
}
void restore_processes()
{
WORD cnt;
current_process_nest--;
for ( cnt=0;cnt<MAX_PROCESSES;cnt++ )
{
process_list[cnt] = store_process_list[ current_process_nest ][cnt];
}
}
void execute_processes()
{
void (*ptr)();
WORD cnt;
/*
Execute a list of addresses
*/
for( cnt=0;cnt<MAX_PROCESSES;cnt++ )
{
ptr = (void *)process_list[cnt];
if ( ptr )
(*ptr)();
// if ( ptr )
// {
// (*ptr)();
// cnt++;
// }
// else
// break;
}
}
void kill_process( ULONG *addr )
{
*addr = (ULONG)0;
}
/********************************************************************/
/********************************************************************
* *
* Random number generator *
* *
********************************************************************/
Pos random_number;
ULONG make_random_number( ULONG max_rand_num_size )
{
/* pass max size of number to generate */
ULONG b;
b = rand ();
random_number.l=b;
if ( max_rand_num_size )
b %= (max_rand_num_size+1);
return b;
}
/********************************************************************/
/********************************************************************
* *
* Memory *
* allocation/deallocation *
* *
********************************************************************/
static UBYTE *mh_ptr[4]; // heap pointer
static UBYTE *mh_ptr_save[4][128]; // saved heap pointer
static LONG mh_amount_used_save[4][128]; // Save amount used
static LONG mh_amount_avail[4]; // Amount allocated
static LONG mh_ptr_current[4];
LONG mh_amount_used[4]; // Amount currently allocated
LONG mh_max_used[4]; // Maximum amount allocated
BYTE *heap_area[4];
/* Initialise and clear heap to zero */
/* --------------------------------- */
/*
Returns:
0 - O.K.
1 - Error
*/
LONG my_heap_init(WORD heap_num, WORD init_ram )
{
BYTE *addr;
LONG heap_sizes[]={ HEAP0_SIZE, HEAP1_SIZE, HEAP2_SIZE, HEAP3_SIZE };
LONG amount;
amount = heap_sizes[ heap_num ];
/* Round up to DWORD */
amount = (amount+3)>>2;
amount = amount << 2;
mh_ptr_current[ heap_num ]=0;
mh_ptr[ heap_num ] = heap_area[ heap_num ]; // set to area reserved for heap in mem.s
mh_ptr_save[ heap_num ][ mh_ptr_current[ heap_num ] ] = NULL;
// clear ram?
if ( init_ram )
memset(mh_ptr[ heap_num ],0,amount);
mh_amount_avail[ heap_num ]=amount;
mh_amount_used[ heap_num ]=0;
mh_max_used[ heap_num ]=0;
return OK;
}
// save the current heap pointer
void my_heap_pushpointer( WORD heap_num )
{
mh_ptr_current[ heap_num ]++;
mh_ptr_save[ heap_num ][ mh_ptr_current[ heap_num ] ] = mh_ptr[ heap_num ];
mh_amount_used_save[ heap_num ][mh_ptr_current[ heap_num ] ]=mh_amount_used[ heap_num ];
//printf("pushed heap at %x\n",mh_ptr[ heap_num ] );
// printf("push hp=%d\n",mh_ptr_current[ heap_num ]);
}
// restore the heap pointer
// - a sort of very primitive free()
void my_heap_poppointer( WORD heap_num )
{
if ( mh_ptr_current[ heap_num ] <= 0 )
{
#if DEBUG
printf("Invalid save pointer '%d'\n",mh_ptr_current[ heap_num ] );
exit(1);
#endif
}
mh_ptr[ heap_num ] = mh_ptr_save[ heap_num ][ mh_ptr_current[ heap_num ] ]; // recover heap pointer
mh_ptr_save[ heap_num ][ mh_ptr_current[ heap_num ] ] = NULL; // clear saved heap pointer
mh_amount_used[ heap_num ]=mh_amount_used_save[ heap_num ][mh_ptr_current[ heap_num ] ]; // Restore amount used
// printf("pop hp=%d\n",mh_ptr_current[ heap_num ]);
mh_ptr_current[ heap_num ]--;
}
void zero( UBYTE *s, LONG many )
{
LONG *ls, lmany;
while( (LONG)s & 3 )
{
*s++ = 0;
many--;
}
ls = ( LONG *)s;
lmany = many>>2;
many -= ( lmany<<2);
while( lmany-- )
{
*ls++ = 0;
}
s = (UBYTE *)ls;
while( many-- )
{
*s++ = 0;
}
}
// ask how much you want in bytes
// it'll round it so it's divisible by four
// and return a pointer to it
void *my_malloc(WORD heap_num, LONG howmuch)
{
UBYTE *retp;
//printf("mem used=%d mem avail=%d\n",mh_amount_used,mh_amount_avail);
/* Round up to DWORD */
howmuch = (howmuch+3)>>2;
howmuch = howmuch << 2;
/* Update amount used */
mh_amount_used[ heap_num ] += howmuch;
/* Too much used ? */
if (mh_amount_used[ heap_num ] > mh_amount_avail[ heap_num ] )
{
mh_amount_used[ heap_num ] -= howmuch;
return NULL;
}
/* Keep track of max used */
if (mh_amount_used[ heap_num ] > mh_max_used[ heap_num ] )
mh_max_used[ heap_num ]=mh_amount_used[ heap_num ];
retp = mh_ptr[ heap_num ]; // return pointer to start of allocated lump
mh_ptr[ heap_num ] += howmuch; // advance heap pointer
zero( retp, howmuch );
// printf("amount used=%d\n",mh_amount_used);
return(retp);
}
/*********************************************************************
*
* This memory allocation/deallocation reserves a large chunk
* after the heap and allocates and deallocates within that
* area. Allocate mem starts at the bottom of the reserved chunk
* and further allocate_mems() allocate just after that.
* Use push() and pop() to store/restore the current memory pointer.
*
*********************************************************************/
void init_memory_alloc()
{
heap_area[0] = (BYTE *)&end_of_ram;
heap_area[1] = heap_area[0] + HEAP0_SIZE;
heap_area[2] = heap_area[1] + HEAP1_SIZE;
heap_area[3] = heap_area[2] + HEAP2_SIZE;
my_heap_init ( 0,YES ); // reset heap 0
my_heap_init ( 1,YES ); // reset heap 1
}
void *allocate_mem ( WORD heap_num, ULONG amount )
{
BYTE *addr;
ULONG addr_k, addr_rem, laddr;
addr=my_malloc ( heap_num, amount );
#if DEBUG
if ( amount==0 )
{
printf("ERROR trying to alloc ZERO bytes\n");
debug_print_mess( "error trying to alloc zero bytes", 0 );
}
#endif
#if DEBUG
if ( heap_num==1)
{
laddr = addr;
addr_k = ( laddr - 0x80000000 ) / 1024;
addr_rem = (laddr-0x80000000)%10;
printf("trying to allocate %d.%dk to addr %d.%dk\n",amount/1024,amount%10, addr_k, addr_rem );
}
#endif
#if DEBUG
if ( addr == 0 )
{
sprintf ( text_buffer, "*** Error , could not allocate %d bytes in heap %d ***\n",amount, heap_num);
debug_print_mess( text_buffer,0 );
}
else
{
// if ( amount > 1000 )
// printf("reserved 0x%x to 0x%x\n",addr, addr+amount );
}
#endif
return addr;
}
void push( WORD heap_num )
{
//Save the current memory pointer
my_heap_pushpointer( heap_num );
}
void pop( WORD heap_num )
{
//Restore the current memory pointer
my_heap_poppointer( heap_num );
}
ULONG read_free_addr( WORD heap_num )
{
return (ULONG)mh_ptr[ heap_num ];
}
WORD my_strcmp ( BYTE *s1, BYTE *s2 )
{
while ( *s1 && *s2 )
{
if ( *s1 != *s2 )
{
break;
}
s1++;
s2++;
}
if ( (*s1 == 0) && (*s2 == 0) )
{
return 0;
}
else
{
return 1;
}
}
ULONG get_heap_size( WORD heap_num )
{
return ( mh_amount_used[heap_num] );
}
ULONG get_heap_start( WORD heap_num )
{
return ( (ULONG )&heap_area[heap_num] );
}
BYTE *get_heap_end( WORD heap_num )
{
BYTE *addr = heap_area[heap_num];
return ( addr + mh_amount_used[heap_num] );
}
/*************************************************************
* *
* Bubble number sorter (up to 256 numbers) *
* For 64 bit numbers *
* *
* Pass - list of numbers to sort (long64 ) *
* - size of list *
* - Ascending or Descending *
* *
* Returns - WORD * indexed sort list *
* *
*************************************************************/
#define BUBBLE_LIST_SIZE 256
WORD bubble_index_list[ BUBBLE_LIST_SIZE ];
LONG64 bubble_sort_list64[ BUBBLE_LIST_SIZE ];
WORD *bubble_sort64( LONG64 *list_to_sort, WORD size, WORD order )
{
LONG64 *num_ptr1, *num_ptr2, store;
WORD *index_ptr1, *index_ptr2, swap,cnt;
//*** Order = Ascending or descending ****
/** Set up index list **/
for( cnt=0; cnt<BUBBLE_LIST_SIZE; cnt++)
{
bubble_index_list[cnt] = cnt;
}
for( cnt=0; cnt<size; cnt++)
{
bubble_sort_list64[cnt] = list_to_sort[cnt];
}
bubble_sort_list64[size]=0;
swap=YES;
if ( order == DESCENDING )
{
//*** Sort by descending order ****
while( swap )
{
swap = NO;
num_ptr1 = &bubble_sort_list64[0];
num_ptr2 = &bubble_sort_list64[1];
index_ptr1 = &bubble_index_list[0];
index_ptr2 = &bubble_index_list[1];
for( cnt=size-1; cnt>0; cnt-- )
{
if ( *num_ptr1 < *num_ptr2 )
{
store = *num_ptr1;
*num_ptr1 = *num_ptr2;
*num_ptr2 = store;
store = *index_ptr1;
*index_ptr1 = *index_ptr2;
*index_ptr2 = store;
swap = YES;
}
else
{
}
num_ptr1++;
num_ptr2++;
index_ptr1++;
index_ptr2++;
}
}
}
else
{
//*** Sort by ascending order ****
while( swap )
{
swap = NO;
num_ptr1 = &bubble_sort_list64[0];
num_ptr2 = &bubble_sort_list64[1];
index_ptr1 = &bubble_index_list[0];
index_ptr2 = &bubble_index_list[1];
for( cnt=0; cnt<size-1; cnt++ )
{
if ( *num_ptr1 > *num_ptr2 )
{
store = *num_ptr1;
*num_ptr1 = *num_ptr2;
*num_ptr2 = store;
store = *index_ptr1;
*index_ptr1 = *index_ptr2;
*index_ptr2 = store;
swap = YES;
}
else
{
}
num_ptr1++;
num_ptr2++;
index_ptr1++;
index_ptr2++;
}
}
}
return bubble_index_list;
}
/*************************************************************
* *
* Bubble number sorter (up to 256 numbers) *
* For 16 bit numbers *
* *
* Pass - list of numbers to sort (word ) *
* - size of list *
* - Ascending or Descending *
* *
* Returns - WORD * indexed sort list *
* *
*************************************************************/
WORD *bubble_sort16( WORD *list_to_sort, WORD size, WORD order )
{
LONG64 *num_ptr1, *num_ptr2, store;
WORD *index_ptr1, *index_ptr2, swap,cnt;
//*** Order = Ascending or descending ****
/** Set up index list **/
for( cnt=0; cnt<BUBBLE_LIST_SIZE; cnt++)
{
bubble_index_list[cnt] = cnt;
}
for( cnt=0; cnt<size; cnt++)
{
bubble_sort_list64[cnt] = list_to_sort[cnt];
}
bubble_sort_list64[size]=0;
swap=YES;
if ( order == DESCENDING )
{
//*** Sort by descending order ****
while( swap )
{
swap = NO;
num_ptr1 = &bubble_sort_list64[0];
num_ptr2 = &bubble_sort_list64[1];
index_ptr1 = &bubble_index_list[0];
index_ptr2 = &bubble_index_list[1];
for( cnt=size-1; cnt>0; cnt-- )
{
if ( *num_ptr1 < *num_ptr2 )
{
store = *num_ptr1;
*num_ptr1 = *num_ptr2;
*num_ptr2 = store;
store = *index_ptr1;
*index_ptr1 = *index_ptr2;
*index_ptr2 = store;
swap = YES;
}
else
{
}
num_ptr1++;
num_ptr2++;
index_ptr1++;
index_ptr2++;
}
}
}
else
{
//*** Sort by ascending order ****
while( swap )
{
swap = NO;
num_ptr1 = &bubble_sort_list64[0];
num_ptr2 = &bubble_sort_list64[1];
index_ptr1 = &bubble_index_list[0];
index_ptr2 = &bubble_index_list[1];
for( cnt=0; cnt<size-1; cnt++ )
{
if ( *num_ptr1 > *num_ptr2 )
{
store = *num_ptr1;
*num_ptr1 = *num_ptr2;
*num_ptr2 = store;
store = *index_ptr1;
*index_ptr1 = *index_ptr2;
*index_ptr2 = store;
swap = YES;
}
else
{
}
num_ptr1++;
num_ptr2++;
index_ptr1++;
index_ptr2++;
}
}
}
return bubble_index_list;
}