-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathsi78c.c
4326 lines (3475 loc) · 102 KB
/
si78c.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
//
// Space Invaders 1978 in C
// Jason McSweeney
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ucontext.h>
#include <stdint.h>
#include <assert.h>
#include <SDL.h>
#include <assert.h>
#include <string.h>
#define OP_BLEND 0
#define OP_ERASE 1
#define OP_COLLIDE 2
#define OP_BLIT 3
#define SHOT_ACTIVE 0x80
#define SHOT_BLOWUP 0x1
#define BEAM_VBLANK 0x80
#define BEAM_MIDDLE 0x00
#define XR_MID 0x80
#define DIP3_SHIPS1 0x1
#define DIP5_SHIPS2 0x2
#define DIP6_BONUS 0x8
#define DIP7_COININFO 0x80
#define TILT_BIT 0x4
#define COIN_BIT 0x1
#define INIT 1
#define PROMPT 2
#define SHIELDS 4
#define ROL_SHOT_PICEND 0xf9
#define PLU_SHOT_PICEND 0xed
#define SQU_SHOT_PICEND 0xdb
#define PLAYER_ADDR 0x2010
#define PLAYER_SIZE 16
#define PLAYER_SHOT_ADDR 0x2020
#define PLAYER_SHOT_DATA_ADDR 0x2025
#define PLAYER_SHOT_DATA_SIZE 7
#define ROLLING_SHOT_ADDR 0x2030
#define ROLLING_SHOT_SIZE 16
#define PLUNGER_SHOT_ADDR 0x2040
#define PLUNGER_SHOT_SIZE 16
#define SQUIGGLY_SHOT_ADDR 0x2050
#define SQUIGGLY_SHOT_SIZE 16
#define SAUCER_ADDR 0x2083
#define SAUCER_SIZE 10
#define P1_ADDR 0x2100
#define P2_ADDR 0x2200
#define SPLASH_DESC_ADDR 0x20c5
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error "This code is Little Endian only."
#endif
struct Word
{
union {
uint16_t u16;
struct {
uint8_t l;
uint8_t h;
};
struct {
uint8_t y;
uint8_t x;
};
};
} __attribute__ ((packed)); // 4
typedef struct Word Word;
struct SprDesc
{
Word spr;
union {
Word pos; // pixel position
Word sc; // screen address
};
uint8_t n;
} __attribute__ ((packed)); // 5
typedef struct SprDesc SprDesc;
struct GameObjHeader
{
uint8_t TimerMSB;
uint8_t TimerLSB;
uint8_t TimerExtra;
Word Handler;
} __attribute__ ((packed)); // 5
typedef struct GameObjHeader GameObjHeader;
struct AShot
{
uint8_t Status;
uint8_t StepCnt;
uint8_t Track;
Word CFir;
uint8_t BlowCnt;
SprDesc Desc;
} __attribute__ ((packed)); // 11
typedef struct AShot AShot;
struct Mem {
uint8_t pad_01[3063]; // 0x0000
uint8_t MSG_TAITO_COP[9]; // 0x0bf7
uint8_t pad_02[3601]; // 0x0c00
uint8_t soundDelayKey[16]; // 0x1a11
uint8_t soundDelayValue[16]; // 0x1a21
uint8_t pad_03[112]; // 0x1a31
uint8_t ShotReloadRate[5]; // 0x1aa1
uint8_t MSG_GAME_OVER__PLAYER___[20]; // 0x1aa6
uint8_t MSG_1_OR_2PLAYERS_BUTTON[20]; // 0x1aba
uint8_t pad_04; // 0x1ace
uint8_t MSG_ONLY_1PLAYER__BUTTON[20]; // 0x1acf
uint8_t pad_05; // 0x1ae3
uint8_t MSG__SCORE_1__HI_SCORE_SCORE_2__[28]; // 0x1ae4
uint8_t pad_06[112]; // 0x1b00
uint8_t MSG_PLAY_PLAYER_1_[14]; // 0x1b70
uint8_t pad_07[66]; // 0x1b7e
uint8_t SPLASH_SHOT_OBJDATA[16]; // 0x1bc0
uint8_t pad_08[144]; // 0x1bd0
uint8_t PLAYER_SPRITES[48]; // 0x1c60
uint8_t pad_09[9]; // 0x1c90
uint8_t MSG__10_POINTS[10]; // 0x1c99
uint8_t MSG__SCORE_ADVANCE_TABLE_[21]; // 0x1ca3
uint8_t AReloadScoreTab[4]; // 0x1cb8
uint8_t MSG_TILT[4]; // 0x1cbc
uint8_t pad_10[28]; // 0x1cc0
uint8_t AlienShotExplodingSprite[6]; // 0x1cdc
uint8_t pad_11[24]; // 0x1ce2
uint8_t MSG_PLAY2[5]; // 0x1cfa
uint8_t pad_12[33]; // 0x1cff
uint8_t SHIELD_SPRITE[44]; // 0x1d20
uint8_t SauScrValueTab[4]; // 0x1d4c
uint8_t SauScrStrTab[4]; // 0x1d50
uint8_t pad_13[40]; // 0x1d54
uint8_t SpriteSaucerExp[24]; // 0x1d7c
uint8_t MSG__50[3]; // 0x1d94
uint8_t MSG_100[3]; // 0x1d97
uint8_t MSG_150[3]; // 0x1d9a
uint8_t MSG_300[3]; // 0x1d9d
uint8_t AlienScores[3]; // 0x1da0
uint8_t AlienStartTable[8]; // 0x1da3
uint8_t MSG_PLAY[4]; // 0x1dab
uint8_t MSG_SPACE__INVADERS[15]; // 0x1daf
uint8_t SCORE_ADV_SPRITE_LIST[17]; // 0x1dbe
uint8_t SCORE_ADV_MSG_LIST[17]; // 0x1dcf
uint8_t MSG____MYSTERY[10]; // 0x1de0
uint8_t MSG__30_POINTS[10]; // 0x1dea
uint8_t MSG__20_POINTS[10]; // 0x1df4
uint8_t pad_14[338]; // 0x1dfe
uint8_t MSG__1_OR_2_PLAYERS___[18]; // 0x1f50
uint8_t pad_15[46]; // 0x1f62
uint8_t MSG_INSERT__COIN[12]; // 0x1f90
uint8_t CREDIT_TABLE[4]; // 0x1f9c
uint8_t CREDIT_TABLE_COINS[9]; // 0x1fa0
uint8_t MSG_CREDIT_[7]; // 0x1fa9
uint8_t pad_16[67]; // 0x1fb0
uint8_t MSG_PUSH[4]; // 0x1ff3
uint8_t pad_17[9]; // 0x1ff7
// start of ram mirror
uint8_t waitOnDraw; // 0x2000
uint8_t pad_18; // 0x2001
uint8_t alienIsExploding; // 0x2002
uint8_t expAlienTimer; // 0x2003
uint8_t alienRow; // 0x2004
uint8_t alienFrame; // 0x2005
uint8_t alienCurIndex; // 0x2006
Word refAlienDelta; // 0x2007
Word refAlienPos; // 0x2009
Word alienPos; // 0x200b
uint8_t rackDirection; // 0x200d
uint8_t rackDownDelta; // 0x200e
uint8_t pad_19; // 0x200f
GameObjHeader playerHeader; // 0x2010
uint8_t playerAlive; // 0x2015
uint8_t expAnimateTimer; // 0x2016
uint8_t expAnimateCnt; // 0x2017
SprDesc playerDesc; // 0x2018
uint8_t nextDemoCmd; // 0x201d
uint8_t hidMessSeq; // 0x201e
uint8_t pad_20; // 0x201f
GameObjHeader plyrShotHeader; // 0x2020
uint8_t plyrShotStatus; // 0x2025
uint8_t blowUpTimer; // 0x2026
SprDesc playerShotDesc; // 0x2027
uint8_t shotDeltaYr; // 0x202c
uint8_t fireBounce; // 0x202d
uint8_t pad_21[2]; // 0x202e
GameObjHeader rolShotHeader; // 0x2030
AShot rolShotData; // 0x2035
GameObjHeader pluShotHeader; // 0x2040
AShot pluShotData; // 0x2045
GameObjHeader squShotHeader; // 0x2050
AShot squShotData; // 0x2055
uint8_t pad_22; // 0x2060
uint8_t collision; // 0x2061
SprDesc expAlien; // 0x2062
uint8_t playerDataMSB; // 0x2067
uint8_t playerOK; // 0x2068
uint8_t enableAlienFire; // 0x2069
uint8_t alienFireDelay; // 0x206a
uint8_t pad_23; // 0x206b
uint8_t temp206C; // 0x206c
uint8_t invaded; // 0x206d
uint8_t skipPlunger; // 0x206e
uint8_t pad_24; // 0x206f
uint8_t otherShot1; // 0x2070
uint8_t otherShot2; // 0x2071
uint8_t vblankStatus; // 0x2072
AShot aShot; // 0x2073
uint8_t alienShotDelta; // 0x207e
uint8_t shotPicEnd; // 0x207f
uint8_t shotSync; // 0x2080
uint8_t tmp2081; // 0x2081
uint8_t numAliens; // 0x2082
uint8_t saucerStart; // 0x2083
uint8_t saucerActive; // 0x2084
uint8_t saucerHit; // 0x2085
uint8_t saucerHitTime; // 0x2086
SprDesc saucerDesc; // 0x2087
uint8_t saucerDXr; // 0x208c
Word sauScore; // 0x208d
Word shotCount; // 0x208f
Word saucerTimer; // 0x2091
uint8_t waitStartLoop; // 0x2093
uint8_t soundPort3; // 0x2094
uint8_t changeFleetSnd; // 0x2095
uint8_t fleetSndCnt; // 0x2096
uint8_t fleetSndReload; // 0x2097
uint8_t soundPort5; // 0x2098
uint8_t extraHold; // 0x2099
uint8_t tilt; // 0x209a
uint8_t fleetSndHold; // 0x209b
uint8_t pad_25[36]; // 0x209c
// end of partial ram restore at 0x20c0
uint8_t isrDelay; // 0x20c0
uint8_t isrSplashTask; // 0x20c1
uint8_t splashAnForm; // 0x20c2
Word splashDelta; // 0x20c3
Word splashPos; // 0x20c5
Word splashPic; // 0x20c7
uint8_t splashPicSize; // 0x20c9
uint8_t splashTargetX; // 0x20ca
uint8_t splashReached; // 0x20cb
Word splashImRest; // 0x20cc
uint8_t twoPlayers; // 0x20ce
uint8_t aShotReloadRate; // 0x20cf
uint8_t pad_26[21]; // 0x20d0
Word playerExtras; // 0x20e5
Word playerStates; // 0x20e7
uint8_t gameTasksRunning; // 0x20e9
uint8_t coinSwitch; // 0x20ea
uint8_t numCoins; // 0x20eb
uint8_t splashAnimate; // 0x20ec
Word demoCmdPtr; // 0x20ed
uint8_t gameMode; // 0x20ef
uint8_t pad_27; // 0x20f0
uint8_t adjustScore; // 0x20f1
Word scoreDelta; // 0x20f2
Word HiScor; // 0x20f4
uint8_t pad_28[2]; // 0x20f6
Word P1Scor; // 0x20f8
uint8_t pad_29[2]; // 0x20fa
Word P2Scor; // 0x20fc
uint8_t pad_30[68]; // 0x20fe
// end of ram mirror at 0x2100
uint8_t p1ShieldBuffer[176]; // 0x2142
uint8_t pad_31[9]; // 0x21f2
uint8_t p1RefAlienDX; // 0x21fb
Word p1RefAlienPos; // 0x21fc
uint8_t p1RackCnt; // 0x21fe
uint8_t p1ShipsRem; // 0x21ff
uint8_t pad_32[66]; // 0x2200
uint8_t p2ShieldBuffer[176]; // 0x2242
uint8_t pad_33[9]; // 0x22f2
uint8_t p2RefAlienDX; // 0x22fb
Word p2RefAlienPos; // 0x22fc
uint8_t p2RackCnt; // 0x22fe
uint8_t p2ShipsRem; // 0x22ff
uint8_t pad_34[256]; // 0x2300
uint8_t vram[7168]; // 0x2400
// Technically the region below is supposed to be a mirror, but AFAICT,
// that property is not used by the SI code.
//
// The 'PLAy' animation does do some oob writes during DrawSprite,
// which effectively do nothing because they end up trying to write to ROM
//
// So, as a catchall, we just reserve this area up to the end of the address space.
uint8_t oob[49152]; // 0x4000
} __attribute ((packed));
typedef struct Mem Mem;
typedef struct PriCursor
{
uint8_t* src;
Word sc;
uint8_t* obj;
} PriCursor;
typedef struct ShieldBufferCursor
{
Word sc;
uint8_t* iter;
} ShieldBufferCursor;
typedef enum YieldReason
{
YIELD_INIT = 0,
YIELD_TIMESLICE,
YIELD_INTFIN,
YIELD_WAIT_FOR_START,
YIELD_PLAYER_DEATH,
YIELD_INVADED,
YIELD_TILT,
YIELD_UNKNOWN,
} YieldReason;
enum Keys {
KEYS_LEFT = 1,
KEYS_RIGHT = 2,
KEYS_START = 4,
KEYS_START2 = 8,
KEYS_FIRE = 16,
KEYS_COIN = 32,
KEYS_TILT = 64,
KEYS_DIP6 = 128,
KEYS_DIP7 = 256,
KEYS_SPECIAL1 = 512,
KEYS_SPECIAL2 = 1024,
KEYS_QUIT = 2048
};
#define KEY_LIST \
KEY_MAP('a', KEYS_LEFT); \
KEY_MAP('d', KEYS_RIGHT); \
KEY_MAP('1', KEYS_START); \
KEY_MAP('2', KEYS_START2); \
KEY_MAP('j', KEYS_FIRE); \
KEY_MAP('5', KEYS_COIN); \
KEY_MAP('t', KEYS_TILT); \
KEY_MAP('6', KEYS_DIP6); \
KEY_MAP('7', KEYS_DIP7); \
KEY_MAP('z', KEYS_SPECIAL1); \
KEY_MAP('x', KEYS_SPECIAL2); \
KEY_MAP(SDLK_ESCAPE, KEYS_QUIT);
#define TRUE 1
#define FALSE 0
static void do_logprintf(const char *file, unsigned line, const char* format, ...);
#define logprintf(...) { do_logprintf(__FILE__, __LINE__, __VA_ARGS__); }
#include "si78c_proto.h"
// Coordinate Systems
// ------------------
//
// Natural Units
// -------------
//
// For readability, this codebase uses the following coordinate system
// where possible.
//
// The origin is at the bottom left corner.
//
// X goes +ve towards the rhs of the screen.
// Y goes +ve towards the top of the screen.
//
// (0,256)
// ^
// |
// y |
// |
// |----------> (224,0)
// (0,0) x
//
// Game Units
// -------------
//
// The game uses two different coordinate systems, both of which
// fit into a 16-bit word, and come with an offset.
//
// pix - Pixel positions between 0x2000 and 0xffff
// sc - Screen RAM addresses between 0x2400 and 0x3fff
//
// pix coordinates are used to move and draw objects that require per pixel shifting,
// like the aliens and the bullets.
//
// sc coordinates are used for drawing text and other simple sprites, and also when
// blitting sprites after they have been shifted.
//
// The following macros are used to convert between natural units and game units.
#define xysc(x, y) ((x)*32 + (y)/8)
#define xytosc(x, y) u16_to_word(0x2400 + xysc((x),(y)))
#define xpix(x) ((x)+32)
#define xytopix(x, y) u16_to_word((xpix((x)) << 8) | (y))
#define STACK_SIZE 65536
#define CRED1 17152
#define CRED2 16384
static Mem m;
static uint8_t *rawmem = (uint8_t*) &m;
static int64_t ticks;
static int im;
static int irq_state;
static int irq_vector;
static uint16_t shift_data;
static uint8_t shift_count;
static ucontext_t frontend_ctx;
static ucontext_t main_ctx;
static ucontext_t int_ctx;
static ucontext_t *prev_ctx;
static ucontext_t *curr_ctx;
static uint8_t main_ctx_stack[STACK_SIZE];
static uint8_t int_ctx_stack[STACK_SIZE];
static YieldReason yield_reason;
static SDL_Window *window;
static SDL_Renderer *renderer;
static const int renderscale = 2;
static uint64_t keystate;
static int exited;
static uint8_t port1;
static uint8_t port2;
int main(int argc, char **argv)
{
init_renderer();
init_game();
int credit = 0;
size_t frame = -1;
while (1)
{
frame++;
input();
if (exited) break;
// preserves timing compatibility with MAME
if (frame == 1)
credit--;
// up to mid
credit += CRED1;
loop_core(&credit);
irq(0xcf);
// up to vblank
credit += CRED2;
loop_core(&credit);
irq(0xd7);
render();
}
fini_game();
fini_renderer();
return 0;
}
static void input()
{
SDL_Event event_buffer[64];
size_t num = 0;
while (num < 64)
{
int has = SDL_PollEvent(&event_buffer[num]);
if (!has) break;
num++;
}
for (size_t i = 0; i < num; ++i)
{
SDL_Event e = event_buffer[i];
if (e.type == SDL_QUIT)
{
e.type = SDL_KEYDOWN;
e.key.keysym.sym = SDLK_ESCAPE;
}
if (! (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP)) continue;
uint64_t mask = 0;
uint64_t f = e.type == SDL_KEYDOWN;
switch (e.key.keysym.sym)
{
#define KEY_MAP(x, y) case x: mask = y; break;
KEY_LIST
#undef KEY_MAP
}
keystate = (keystate & ~mask) | (-f & mask);
}
#define BIT(x) (!!(keystate & (x)))
port1 = (BIT(KEYS_RIGHT) << 6) |
(BIT(KEYS_LEFT) << 5) |
(BIT(KEYS_FIRE) << 4) |
(1 << 3) |
(BIT(KEYS_START) << 2) |
(BIT(KEYS_START2) << 1) |
(BIT(KEYS_COIN) << 0);
port2 = (BIT(KEYS_DIP7) << 7) |
(BIT(KEYS_RIGHT) << 6) |
(BIT(KEYS_LEFT) << 5) |
(BIT(KEYS_FIRE) << 4) |
(BIT(KEYS_DIP6) << 3) |
(BIT(KEYS_TILT) << 2);
exited = BIT(KEYS_QUIT);
}
static void init_renderer()
{
int rc = SDL_Init(SDL_INIT_EVERYTHING);
assert(rc == 0);
window = SDL_CreateWindow("si78c", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
224 * renderscale, 256 * renderscale, 0);
assert(window);
SDL_ShowCursor(SDL_DISABLE);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
assert(renderer);
}
static void fini_renderer()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
static void render()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
const uint8_t *iter = rawmem + 0x2400;
for (int y = 0; y < 224; ++y)
{
for (int xi = 0; xi < 32; ++xi)
{
uint8_t byte = *iter++;
for (int i = 0; i < 8; ++i)
{
int x = xi * 8 + i;
int on = (byte >> i) & 0x1;
if (on)
{
SDL_Rect rect;
rect.x = y * renderscale;
rect.y = (256 - x - 1) * renderscale;
rect.w = renderscale;
rect.h = renderscale;
SDL_RenderDrawRect(renderer, &rect);
}
}
}
}
SDL_RenderPresent(renderer);
}
static void loop_core(int *credit)
{
int allowed = 1;
while (*credit > 0)
*credit -= execute(allowed);
}
static void init_game()
{
assert(sizeof(m) == 0x10000);
load_rom(&m);
assert(checksum(&m) == 0x6dfbd7cc);
init_threads(YIELD_INIT);
}
static void fini_game()
{
}
static void init_threads(YieldReason entry_point)
{
int rc = getcontext(&main_ctx);
assert(rc == 0);
main_ctx.uc_stack.ss_sp = main_ctx_stack;
main_ctx.uc_stack.ss_size = STACK_SIZE;
main_ctx.uc_link = &frontend_ctx;
makecontext(&main_ctx, (void (*)()) run_main_ctx, 1, entry_point);
rc = getcontext(&int_ctx);
int_ctx.uc_stack.ss_sp = &int_ctx_stack;
int_ctx.uc_stack.ss_size = STACK_SIZE;
int_ctx.uc_link = &frontend_ctx;
makecontext(&int_ctx, run_int_ctx, 0);
prev_ctx = &main_ctx;
curr_ctx = &frontend_ctx;
}
static void run_main_ctx(YieldReason entry)
{
switch(entry)
{
case YIELD_INIT: reset(); break;
case YIELD_WAIT_FOR_START: WaitForStart(); break;
case YIELD_PLAYER_DEATH: player_death(0); break;
case YIELD_INVADED: on_invaded(); break;
case YIELD_TILT: on_tilt(); break;
default: assert(FALSE);
}
}
static void run_int_ctx()
{
while (1)
{
// 0xcf = RST 1 opcode (call 0x8)
// 0xd7 = RST 2 opcode (call 0x16)
if (irq_vector == 0xcf)
midscreen_int();
else if (irq_vector == 0xd7)
vblank_int();
enable_interrupts();
yield(YIELD_INTFIN);
}
}
static unsigned checksum(Mem *m)
{
assert(sizeof(*m) == 0x10000);
assert((uintptr_t) m % 4 == 0);
unsigned *ptr = (unsigned*) m;
size_t n = sizeof(*m) / 4;
unsigned sum = 0;
for (size_t i = 0; i < n; ++i)
sum += ptr[i];
return sum;
}
static void rom_load(void *mem, const char* name, size_t offset, size_t len)
{
char fbuf[256]; sprintf(fbuf, "inv1/%s", name);
FILE *romfile = fopen(fbuf, "r");
assert(romfile);
ssize_t rn = fread((char*) mem + offset, 1, len, romfile);
assert((size_t) rn == len);
fclose(romfile);
}
static void load_rom(void *mem)
{
rom_load(mem, "invaders.h", 0x0000, 0x0800);
rom_load(mem, "invaders.g", 0x0800, 0x0800);
rom_load(mem, "invaders.f", 0x1000, 0x0800);
rom_load(mem, "invaders.e", 0x1800, 0x0800);
}
static int execute(int allowed)
{
int64_t start = ticks;
ucontext_t *next = NULL;
switch (yield_reason)
{
case YIELD_INIT:
case YIELD_TIMESLICE:
next = prev_ctx;
break;
case YIELD_INTFIN:
next = &main_ctx;
break;
case YIELD_PLAYER_DEATH:
case YIELD_WAIT_FOR_START:
case YIELD_INVADED:
init_threads(yield_reason);
enable_interrupts();
next = &main_ctx;
break;
case YIELD_TILT:
init_threads(yield_reason);
next = &main_ctx;
break;
default:
assert(FALSE);
}
yield_reason = YIELD_UNKNOWN;
if (allowed && interrupted())
{
next = &int_ctx;
}
switch_to(next);
return ticks - start;
}
static void switch_to(ucontext_t *to)
{
co_switch(curr_ctx, to);
}
static void co_switch(ucontext_t *prev, ucontext_t *next)
{
prev_ctx = prev;
curr_ctx = next;
swapcontext(prev, next);
}
static void timeslice()
{
ticks += 30;
yield(YIELD_TIMESLICE);
}
static void yield(YieldReason reason)
{
yield_reason = reason;
switch_to(&frontend_ctx);
}
static uint8_t get_input(int64_t ticks, uint8_t port)
{
if (port == 1)
return port1;
if (port == 2)
return port2;
fatalerror("unknown port %d\n", port);
return 0;
}
static uint8_t read_port(uint8_t port)
{
if (port == 3)
return (shift_data << shift_count) >> 8;
uint8_t val = get_input(ticks, port);
return val;
}
static void write_port(uint16_t port, uint8_t v)
{
if (port == 2)
{
shift_count = v & 0x7;
}
else if (port == 4)
{
shift_data = (v << 8) | (shift_data >> 8);
}
timeslice();
}
static void enable_interrupts()
{
im = 1;
}
static void irq(uint8_t v)
{
irq_vector = v;
irq_state = 1;
}
static int interrupted()
{
// The two interrupts correspond to midscreen, and start of vblank.
// 0xcf = RST 1 opcode (call 0x8)
// 0xd7 = RST 2 opcode (call 0x16)
if (irq_state && im)
{
assert(irq_vector == 0xcf || irq_vector == 0xd7);
irq_state = 0;
im = 0;
return TRUE;
}
return FALSE;
}
static void fatalerror(const char* format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
fflush(stdout);
fflush(stderr);
exit(1);
}
static inline Word u16_to_word(uint16_t u)
{
Word w; w.u16 = u; return w;
}
static inline Word u8_u8_to_word(uint8_t h, uint8_t l)
{
return u16_to_word((h << 8) | l);
}
static inline uint16_t ptr_to_u16(uint8_t *ptr)
{
return (uint16_t) (ptr - (uint8_t*) &m);
}
static inline Word ptr_to_word(uint8_t *ptr)
{
return u16_to_word(ptr_to_u16(ptr));
}
static inline uint8_t* u16_to_ptr(uint16_t u)
{
return ((uint8_t*) &m) + u;
}
static inline uint8_t* word_to_ptr(Word w)
{
return u16_to_ptr(w.u16);
}
static int is_godmode()
{
uint16_t addr = 0x060f;
uint8_t nops[] = {0,0,0};
return memcmp(((uint8_t*) &m) + addr, nops, 3) == 0;
}
static uint8_t* rompos(uint8_t* ram)
{
return ram - 0x500;
}
static uint8_t bcd_add(uint8_t bcd, uint8_t a, uint8_t *carry)
{
// Add the given number to the given bcd value, as per ADI / ADDB etc
int q = bcd + a + *carry;
*carry = (q >> 8) & 0x1;
int aux = (bcd ^ q ^ a) & 0x10;
bcd = q;
// Adjust the result back into bcd as per DAA
uint8_t w = bcd;
if (aux || ((bcd & 0xf) > 9)) w += 6;
if ((*carry) || (bcd > 0x99)) w += 0x60;
*carry |= bcd > 0x99;
bcd = w;
return bcd;
}
static void do_logprintf(const char *file, unsigned line, const char* format, ...)
{
fprintf(stdout, "%s:%d: ", file, line);
va_list ap;
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
fflush(stdout);
}
static void DebugMessage(Word sc, uint8_t* msg, uint8_t n)
{
uint16_t raw = sc.u16 - 0x2400;
uint16_t x = raw / 32;
uint16_t y = (raw % 32) * 8;
static const char *alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<> =*...............?......-";
char sbuf[256];
for (size_t i = 0; i < n; ++i)
sbuf[i] = alpha[msg[i]];
sbuf[n] = '\0';
logprintf("Print Message %04x %d (%d,%d) \"%s\"\n", ptr_to_word(msg).u16, n, x, y, sbuf);
}
// GAMECODE START
// The entry point on reset or power up
static void reset()
{
// xref 0000
main_init(0);
}
// Executed via interrupt when the beam hits the middle of the screen
static void midscreen_int()
{
// xref 0008
// xref 008c
m.vblankStatus = BEAM_MIDDLE;
if (m.gameTasksRunning == 0) return;
if (!m.gameMode && !(m.isrSplashTask & 0x1)) return;
// xref 00a5
// Run game objects but skip over first entry (player)
RunGameObjs(u16_to_ptr(PLAYER_SHOT_ADDR));
CursorNextAlien();
}
// Executed via interrupt when the beam hits the end of the screen
static void vblank_int()
{