-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsw.ino
1576 lines (1247 loc) · 31.2 KB
/
jsw.ino
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
/***********************************************************
*
* JSW 48K implementation for ZPUino with VGA ZX interface
*
* (C) Alvaro Lopes 2012
*
* This code released under Creative Commons BY-NC-SA
*
* None of the additional files (ROM's and other resources) are
* allowed for commercial use.
*
* Based on information found in http://mdfs.net/Software/JSW/
* ROM extracted from http://www.worldofspectrum.org
*
* Original ZX Spectrum ROM used by permission:
* http://www.worldofspectrum.org/permits/amstrad-roms.txt
*
* " Amstrad have kindly given their permission for the
* redistribution of their copyrighted material but retain
* that copyright "
*
*
* Based on commented disassembly as seen below:
*
*****
***** Based on: Jet Set Willy JSW48 Game Engine Source
***** ======================================
***** JSW Copyright (C) 1984 Matthew Smith & Software Projects
*****
***** Commentary Copyright (C) 1985, 2004 J.G.Harston
***** See http://mdfs.net/Software/JSW/
***** The source is assembleable with ZMac
***** See http://mdfs.net/Software/Z80/ZMac
*****
***** This was originally a commented disassembly, but it became easier to type
***** up as a source file and create the disassembly from it.
*****
**********************************************************
*/
#include <SmallFS.h>
// Define this is you use the ZPUino simulator
//#define SIMULATION
#define STATIC static
#ifdef SIMULATION
#define JUMP_INVERT
#define LEFT_INVERT
#define RIGHT_INVERT
#define JUMP_PIN 25
#define LEFT_PIN 27
#define RIGHT_PIN 24
#else
// Hephaestus (megawing)
#define JUMP_PIN 36
#define JUMP_INVERT
#define LEFT_PIN 39
#define LEFT_INVERT
#define RIGHT_PIN 38
#define RIGHT_INVERT
/*#define JUMP_PIN WING_C_3
#define JUMP_INVERT
#define LEFT_PIN WING_C_5
#define LEFT_INVERT
#define RIGHT_PIN WING_C_1
#define RIGHT_INVERT
*/
#endif
/* Some debugging functions */
void printnibble(unsigned int c)
{
c&=0xf;
if (c>9)
Serial.write(c+'a'-10);
else
Serial.write(c+'0');
}
void printhexbyte(unsigned int c)
{
printnibble(c>>4);
printnibble(c);
}
void printhex(unsigned int c)
{
printhexbyte(c>>24);
printhexbyte(c>>16);
printhexbyte(c>>8);
printhexbyte(c);
}
// This converts a attrbuffer offset to a framebuffer offset
#define ATTRBUFFER_OFFSET_TO_FRAMEBUFFER_OFFSET(x) \
( ( (x) &0x1F) /* X offset */ + (((x)<<3) & ~0xFF) )
// Our main framebuffer
static unsigned char framebuffer[32*24*8];
static unsigned char pallete[32*24];
static unsigned int GAMEDELAY=50;
struct ROOM_s {
unsigned char def[128]; // OK
unsigned char name[32]; // OK
unsigned char background[9]; // OK
unsigned char floor[9]; // OK
unsigned char wall[9]; // OK
unsigned char nasty[9]; // OK
unsigned char slope[9]; // OK
unsigned char conveyor[9]; // OK
unsigned char conv_dir; // OK
unsigned char conv_psn[2]; // OK
unsigned char conv_num; // OK
unsigned char slope_dir; // OK
unsigned char slope_psn[2]; // OK
unsigned char slope_num; // OK
unsigned char border[3]; // OK
unsigned char object[8];
unsigned char left;
unsigned char right;
unsigned char up;
unsigned char down;
unsigned char willisp[3];
unsigned char instances[16];
} __attribute__((packed));
static struct ROOM_s ROOM;
static unsigned char guardian[66] ={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFB
};
static unsigned char ROPE[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // Rope X offsets
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,1,2,2,1,1,2,1,1,2,2,3,2,3,2,
3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, // Rope Y offsets
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
4,6,6,4,6,4,6,4,6,4,4,4,6,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
static unsigned char HERE; // Current room number
// Bitmaps for triangle characters
// -------------------------------
static const unsigned char TBITMAP[] ={
0xC0,0xF0,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,
0x00,0x00,0x00,0x00,0xC0,0xF0,0xFC,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFC,0xF0,0xC0,0x00,
0xFC,0xF0,0xC0,0x00,0x00,0x00,0x00,0x00};
const char AIR[] = "AIR";
const char MESSAGE[] =
"+++++ Press ENTER to Start +++++"
" JET-SET WILLY by Matthew Smith "
"\177 1984 SOFTWARE PROJECTS Ltd "
". . . . .Guide Willy to collect "
"all the items around the house "
"before Midnight so Maria will let "
"you get to your bed. . . . . . ."
"+++++ Press ENTER to Start +++++";
char ITEMS[] = "Items collected 000 Time 00:00 m";
const char GAME[] = "Game";
const char OVER[] = "Over";
char COLLECTED[] = "000";
char NOWTIME[] = " 7:00a";
char STARTTIME[] = " 7:00a";
// ====================
unsigned char TICKER=0;
unsigned char LIVES=0;
unsigned char FLASH=0;
// Current Willy state
// ===================
struct willy_state {
unsigned int YPOSN;
unsigned char DIRECTION; //L85D0;
unsigned char FALLING;
unsigned char FRAME;
unsigned int POSITION;
unsigned char JUMPING;
unsigned char ONROPE;
};
struct willy_state wstate;
struct willy_state save_wstate;
unsigned char REMAIN=0;
unsigned char STATUS=0;
unsigned char COUNTDOWN=0;
unsigned char MTICK=0;
unsigned char MFLAGS=0;
unsigned char TELEPORT;
unsigned char TEMP;
unsigned int willy_offset=0;
int newroom=1; // Set to 1 to load a new room.
static unsigned char sprite[32]; // Willy sprite, used across the code
const unsigned char MOONLIGHT[] = {
0x51,0x3C,0x33,0x51,0x3C,0x33,0x51,0x3C,0x33,0x51,0x3C,0x33,
0x51,0x3C,0x33,0x51,0x3C,0x33,0x51,0x3C,0x33,0x51,0x3C,0x33,
0x4C,0x3C,0x33,0x4C,0x3C,0x33,0x4C,0x39,0x2D,0x4C,0x39,0x2D,
0x51,0x40,0x2D,0x51,0x3C,0x33,0x51,0x3C,0x36,0x5B,0x40,0x36,
0x66,0x51,0x3C,0x51,0x3C,0x33,0x51,0x3C,0x33,0x28,0x3C,0x28,
0x28,0x36,0x2D,0x51,0x36,0x2D,0x51,0x36,0x2D,0x28,0x36,0x28,
0x28,0x3C,0x33,0x51,0x3C,0x33,0x26,0x3C,0x2D,0x4C,0x3C,0x2D,
0x28,0x40,0x33,0x51,0x40,0x33,0x2D,0x40,0x36,0x20,0x40,0x36,
0x3D,0x79,0x3D,0xFF }; // Terminated with &FF
const unsigned char RICHMAN[] = {
0x56,0x60,0x56,0x60,0x66,0x66,0x80,0x80,0x80,0x80,0x66,0x60,
0x56,0x60,0x56,0x60,0x66,0x60,0x56,0x4C,0x48,0x4C,0x48,0x4C,
0x56,0x56,0x56,0x56,0x56,0x56,0x56,0x56,0x40,0x40,0x40,0x40,
0x44,0x44,0x4C,0x4C,0x56,0x60,0x66,0x60,0x56,0x56,0x66,0x66,
0x51,0x56,0x60,0x56,0x51,0x51,0x60,0x60,0x40,0x40,0x40,0x40,
0x40,0x40,0x40,0x40 }; // 64 bytes long, just loops
#define VGABASE IO_SLOT(8)
#define COLUMNS 32
#define ROWS 24
#define RWIDTH 32*8
//SmallFSFile charmap;
SmallFSFile jswrom;
unsigned char *btarget;
static inline void blit8x8(unsigned char value, void*)
{
*btarget=value;
btarget+=COLUMNS;
}
static inline void putChar(unsigned char value)
{
jswrom.seek( (value*8) + 0x3C00,SEEK_SET);
jswrom.readCallback(8, &blit8x8, NULL);
btarget-=((8*COLUMNS)-1);
}
static inline void putString(const char *p)
{
while (*p) {
putChar(*p++);
}
}
STATIC void drawpower()
{
unsigned off = 21*COLUMNS + 20; // Line 21, column 20
memset( &pallete[off], 0x7, 10);
PRMESSAGE( &framebuffer[ ATTRBUFFER_OFFSET_TO_FRAMEBUFFER_OFFSET(off) ], "Powered by", 10);
off+=COLUMNS;
memset( &pallete[off], 0x5, 10);
PRMESSAGE( &framebuffer[ ATTRBUFFER_OFFSET_TO_FRAMEBUFFER_OFFSET(off) ], " ZPUino32 ", 10);
}
STATIC void updatepower()
{
unsigned off = 22*COLUMNS + 20; // Line 21, column 20
unsigned i;
for (i=10;i!=0;i--) {
pallete[off] = ( pallete[off] & 0xf8 ) | // Lose INK
(TICKER & 0x3) + 0x3;
off++;
}
}
STATIC void PRMATRIX(const unsigned char *source, unsigned char *dest)
{
// Print a 8x8 matrix on screen. This loop is fully unrolled.
*dest = *source++; dest+=COLUMNS;
*dest = *source++; dest+=COLUMNS;
*dest = *source++; dest+=COLUMNS;
*dest = *source++; dest+=COLUMNS;
*dest = *source++; dest+=COLUMNS;
*dest = *source++; dest+=COLUMNS;
*dest = *source++; dest+=COLUMNS;
*dest = *source;
}
/* Update conveyors on screen */
STATIC void UPDATECONV()
{
if (ROOM.conv_num==0)
return;
unsigned v = ROOM.conveyor[1];
if (ROOM.conv_dir==0) {
// Left
v<<=2;
v|=(v>>8);
ROOM.conveyor[1] = v;
} else {
v|=(v<<8);
v>>=2;
ROOM.conveyor[1] = v;
}
v = ROOM.conveyor[3];
if (ROOM.conv_dir==0) {
// opposite
v|=(v<<8);
v>>=2;
ROOM.conveyor[3] = v;
} else {
v<<=2;
v|=(v>>8);
ROOM.conveyor[3] = v;
}
}
/*
Draw a sprite (32x32) on the screen. Eventually check for collisions
*/
STATIC int drawsprite(const unsigned char *source, unsigned char*dest, int check_collision)
{
int i;
unsigned sprite_byte;
for (i=16;i!=0;i--) {
sprite_byte = *source;
if (check_collision) {
if (sprite_byte & (*dest)) {
return 1;
}
sprite_byte |= *dest;
}
*dest = sprite_byte;
dest++;
source++;
sprite_byte=*source;
if (check_collision) {
if (sprite_byte & (*dest)) {
return 1;
}
}
*dest = sprite_byte;
dest--;
dest+=COLUMNS;
source++;
}
return 0;
}
STATIC void loadscr(SmallFSFile &scr)
{
// SCR are tricky
unsigned i,j,z;
for (z=0;z<3;z++) {
unsigned char *fbptr= &framebuffer[COLUMNS*(z*8*8)];
for (j=0;j<8;j++) {
unsigned char *tbptr=fbptr;
for (i=0;i<8;i++) {
scr.read( tbptr, COLUMNS );
tbptr+=COLUMNS*8;
}
fbptr+=COLUMNS;
}
}
// Seek to pallete (not nedded, I think)
scr.seek( sizeof(framebuffer), SEEK_SET);
scr.read( &pallete[0], sizeof(pallete));
}
STATIC void opening()
{
SmallFSFile scr;
scr = SmallFS.open("ZPUINO");
if (scr.valid()) {
loadscr(scr);
// Wait for jump key
while (!( JUMP_INVERT digitalRead(JUMP_PIN))) {
delay(100);
}
}
}
void setup()
{
Serial.begin(115200);
REGISTER(VGABASE,0) = (unsigned)framebuffer;
REGISTER(VGABASE,1) = (unsigned)pallete;
Serial.println("JSW starting");
SmallFS.begin();
jswrom = SmallFS.open("JSW");
if (!jswrom.valid()) {
while (1) {
}
}
pinMode(JUMP_PIN,INPUT);
pinMode(RIGHT_PIN,INPUT);
pinMode(LEFT_PIN,INPUT);
opening();
}
void generic_copy_from_flash(unsigned offset, unsigned char *target, unsigned size)
{
jswrom.seek(offset, SEEK_SET);
jswrom.read( target, size);
}
void loop()
{
GAMESTART();
}
static inline void copy_attributes()
{
generic_copy_from_flash(0x9800,pallete,sizeof(pallete));
}
STATIC void PRMESSAGE(unsigned char *dest, const char *source, unsigned size)
{
btarget = dest;
while(size--) {
putChar(*source++);
}
}
void changeBG()
{
// TODO: implement this
}
STATIC int DRAWLIVES()
{
unsigned l = LIVES;
if (l==0)
return 0;
// TODO - use MTICK
if ((TICKER&0x3)!=0)
return 1;
unsigned spritenum = TICKER>>2;
spritenum *= 8;
spritenum &= 0x60; // Ensure 0, 1, 2 or 3
unsigned char *dest = &framebuffer[ 8*COLUMNS * 21 ]; // Line 21
generic_copy_from_flash(spritenum + 0x9D00, sprite, sizeof(sprite));
while (l--) {
drawsprite(sprite, dest, 0);
dest+=2;
}
}
STATIC void ROOMBLOCK(unsigned v, unsigned char *dest)
{
*dest = ROOM.background[ (v&0x3)*9 ];
}
STATIC void BUILDROOM()
{
unsigned char *rb = &ROOM.def[0];
unsigned char *attrptr = &pallete[0];
unsigned roombyte;
int i;
for (i=128;i!=0;i--) {
roombyte=*rb;
ROOMBLOCK(roombyte>>6, attrptr++);
ROOMBLOCK(roombyte>>4, attrptr++);
ROOMBLOCK(roombyte>>2, attrptr++);
ROOMBLOCK(roombyte>>0, attrptr++);
rb++;
}
i=ROOM.conv_num;
if (i!=0) {
unsigned start = (ROOM.conv_psn[0] +( ROOM.conv_psn[1]<<8)) - 0x5E00;
attrptr = &pallete[start];
while (i--) {
*attrptr++=ROOM.conveyor[0];
}
}
if (ROOM.slope_num==0)
return;
attrptr = &pallete[ ((unsigned)ROOM.slope_psn[0]+ ((unsigned)ROOM.slope_psn[1]<<8)) - 0x5E00];
int delta;
if ( ROOM.slope_dir & 1 ) {
delta = -31;
} else {
delta = -33;
}
for (i=ROOM.slope_num;i!=0;i--) {
*attrptr = ROOM.slope[0];
attrptr+=delta;
}
}
STATIC void PRMATRIX_if(unsigned val, const unsigned char *src, unsigned char *dest)
{
if (val == *src) {
PRMATRIX(src+1,dest);
}
}
STATIC void DRAWROOM()
{
/* Iterate through all attributes, apply bitmap */
unsigned int i,z;
unsigned char *attrptr = &pallete[0];
unsigned char *fbptr = &framebuffer[0];
for (i=0; i<512; i++) {
unsigned int val = *attrptr;
unsigned char *src = &ROOM.background[0];
// Unroll loop
PRMATRIX_if(val,src,fbptr);
PRMATRIX_if(val,src+9,fbptr);
PRMATRIX_if(val,src+18,fbptr);
PRMATRIX_if(val,src+27,fbptr);
PRMATRIX_if(val,src+36,fbptr);
PRMATRIX_if(val,src+45,fbptr);
fbptr++;
// Move to next line
if ((i%32)==31) {
fbptr+=(COLUMNS*7);
}
attrptr++;
}
}
#define DUMPROOM( dname, vname ) \
rdump( dname, (unsigned char*)&ROOM + offsetof(ROOM_s, vname), sizeof(ROOM.vname) );
STATIC void loadroom()
{
unsigned i;
generic_copy_from_flash( 0xC000 + ((unsigned)HERE*256), (unsigned char*)&ROOM, 256 );
unsigned char *gptr=&ROOM.instances[0];
unsigned char *dptr=guardian;
for (i=0;i<8;i++) {
unsigned guardian_number = *gptr++;
/*Serial.print("Read Guardian ");
printhexbyte(guardian_number);
Serial.println("");*/
guardian_number &= 0x7F; // Max. 127 guardians
guardian_number |= 0x1400;
guardian_number*=8;
// Now, guardian_number is address IN ROM. Copy parts
generic_copy_from_flash(guardian_number,&dptr[0],2);
/*
jswrom.seek(guardian_number,SEEK_SET);
jswrom.read( &dptr[0], 2);*/
//
// Copy the rest
jswrom.read( &dptr[2], 6);
// Set attr?
dptr[2] = *gptr++;
dptr+=8;
}
// Print room information
memset( &framebuffer[ 16*COLUMNS*8 ], 0x0, sizeof(framebuffer)-(16*COLUMNS*8) );
PRMESSAGE( &framebuffer[16*COLUMNS*8], (char*)ROOM.name, 32);
PRMESSAGE( &framebuffer[19*COLUMNS*8], (char*)ITEMS, 32);
// Update collected
PRMESSAGE( &framebuffer[19*COLUMNS*8 + 16], (char*)COLLECTED, 3);
drawpower();
memcpy(&save_wstate, &wstate, sizeof(wstate)); // Save willy state
}
STATIC void GAMESTART()
{
MTICK=FLASH=TICKER=COUNTDOWN=STATUS = 0;
wstate.FALLING=0; // Not falling.
LIVES=7;
HERE = 0x21;// The bathroom
wstate.POSITION=0x5DB4; // Position in attr buffer2
wstate.YPOSN = 0xD0;
COLLECTED[0]='0';
COLLECTED[1]='0';
COLLECTED[2]='0';
//REMAIN=OBJECTS[0];
int i;
// TODO : clear object flags
MFLAGS=0;
unsigned deltas[9];
wstate.POSITION=0x5DB4;
wstate.YPOSN = 0xD0;
copy_attributes();
do {
if (newroom) {
newroom=0;
loadroom();
}
i=0;
TICKER++;
//Serial.println("Tick");
#ifndef SIMULATION
delay( GAMEDELAY );
// Wait for VGA retrace
while ((REGISTER(VGABASE,0)&1) == 0) {}
while ((REGISTER(VGABASE,0)&1) != 0) {}
while ((REGISTER(VGABASE,0)&1) == 0) {}
while ((REGISTER(VGABASE,0)&1) != 0) {}
#else
delay(GAMEDELAY);
REGISTER(VGABASE,3) = 1; // Disable screen
#endif
DRAWLIVES();
bzero(framebuffer, 16*COLUMNS*8 ); // Only clear game-area
BUILDROOM();
DRAWROOM();
UPDATEGUARD();
UPDATECONV();
MOVEMENT();
if (wstate.YPOSN>=0xE1) {
go_up(); // NOTE: I think there's a bug here in some scenarios
continue;
}
UPDATEWILLY();
DRAWWILLY();
DRAWGUARD();
CHKOBJECTS();
updatepower();
#ifdef SIMULATION
REGISTER(VGABASE,3) = 0; // Enable screen
#endif
} while (1);
}
STATIC void DRAWWILLY()
{
int A,E,DE,C;
unsigned i;
unsigned char *plb,*plbd;
// willy_offset is used when we're on a stair.
A = ((wstate.YPOSN+willy_offset)>>1)*32;
plb = &framebuffer[A]; // plb now points to start of line
A = wstate.DIRECTION;
A &= 1;
E = A<<7;
A = wstate.FRAME;
A&=3;
A*=32;
E |= A;
DE=E;
DE |= 0x9D00;
generic_copy_from_flash(DE,sprite,sizeof(sprite));
plb += wstate.POSITION&0x1F;
for (i=0;i<32;i+=2) /* 32 bytes */
{
plbd = plb;
*plbd = *plbd | sprite[i];
plbd++;
*plbd = *plbd | sprite[i+1];
plb+=(COLUMNS); // Next line
}
}
STATIC void DRAWROPE()
{
}
STATIC void DRAWARROW(unsigned char *loc)
{
unsigned comp;
if (loc[0] & 0x80) {
loc[4]++;
comp = 0xF4;
} else {
loc[4]--;
comp = 0x2C;
}
if (loc[4]!=comp) {
//?
}
// 82XX -pixel line
unsigned ypos = loc[2];
ypos += loc[4];
unsigned yagain=loc[2];
yagain&=0x80;
yagain<<=1;
// OR 5C ?
yagain|=0x5C; // 5C00 / 5D00
yagain|= (ypos & 0xff);
loc[5] = 0;
// yagain shhould be willy attr
if ( ( pallete[yagain-0x5C00] & 0x7) == 0x07) {
// Willy
loc[5]--; // ?
}
pallete[yagain-0x5C00] |= 0x7;
yagain++;
}
STATIC void DRAWGUARD()
{
unsigned char *gptr = &guardian[0];
do {
unsigned i = *gptr;
/* Serial.print("Guardian: ");
printhexbyte(*gptr);
Serial.println("");*/
if (i==0xff)
return;
i&=7;
if (i==0) {
// No guardian
//Serial.println("No guard");
gptr+=8;
continue;
}
if (i==0x3) { /* Rope */
//Serial.println("Rope");
DRAWROPE();
gptr+=8;
continue;
}
if (i==0x4) { /* Arrow */
//Serial.println("Arrow");
DRAWARROW(gptr);
gptr+=8;
continue;
}
/* Normal */
/* Serial.print("Ypos ");
printhexbyte(gptr[3]);
Serial.println("");*/
unsigned ypos = gptr[3]>>1;
unsigned xpos = (unsigned)gptr[2];
unsigned attr;
attr = gptr[1]; // Get guardian attribute
attr&=0xf; // Keep b3-b0
attr+=0x38; // Move BRIGHT up to bit 6
attr&=0x47; // Keep INK and BRIGHT
xpos &= 0x1F; // No overflow
// Convert ypos.
//Serial.print("FB at xpos ");Serial.print(xpos);Serial.print(" ypos ");Serial.println(ypos);
xpos+=ypos*32;
unsigned yoffset = ((xpos/32)/8)*32;
//Serial.print("Yoff "); Serial.println(yoffset);
unsigned char *attrptr = &pallete[ (unsigned)(gptr[2]&0x1f) + yoffset];//( ((gptr[3]>>1)<<2)&(~0x1f) ) ];
// Merge attribute
attr = (*attrptr & 0x38 ) | attr;
// Two uppermost cells
*attrptr++ = attr;
*attrptr = attr;
attrptr+=31; // move to next line
*attrptr++ =attr;
*attrptr =attr;
// A guardian can use 2 or 3 lines. When (ypos/8)==0, only 2 lines are used.
if ((gptr[3]) & 0xe ) {
//Serial.println("Guardian 3 lines");
attrptr+=31;
*attrptr++=attr; // Fill the 3rd line
*attrptr=attr;
}
/* Draw the guardian */
unsigned offset = (( gptr[1] & gptr[0]) | gptr[2]) & 0xE0;
offset|=((unsigned)gptr[5]<<8);
/*
Serial.print("Sprite at 0x");
printhex(offset);
Serial.println("");
*/
/* Seek to sprite */
generic_copy_from_flash(offset,sprite,sizeof(sprite));
if ( drawsprite( sprite, &framebuffer[xpos], 1 ) ) {
//Serial.println("Kill: collision");
kill_willy();
return;
}
gptr+=8;
if (gptr>(&guardian[0]+sizeof(guardian)))
break;
} while (1);
}
STATIC void UPDATEGUARD()
{
unsigned char *gptr = &guardian[0];
do {
unsigned i = *gptr;
/*
Serial.print("UpdateG: ");
printhexbyte(*gptr);
Serial.println("");
*/
if (i==0xff)
return;
switch (i&0x03) {
case 0:
/* No need */
break;
case 1:
/* Horizontal */
/*
Serial.print("FRAME: ");
Serial.println( (i>>5) & 0x3 );
*/
/* topmost bit depicts direction. 0->left, 1->right */
if (i&0x80) {
i += 0x20;
i |= 0x80; /* ensure we're still moving right */
// update position/frame
*gptr=i;
if ((i & 0x60) == 0x00) { // 1 01 000 00b
// We overflowed.
// Get X position
unsigned xpos = gptr[2];
xpos &= 0x1F;
if (xpos==gptr[7]) { // Maximum X offset
gptr[0] = 0x61; // Clear direction, keep type (1). 0x61 == '0 11 000 01b'
} else {
// Increase X position
gptr[2] = gptr[2]+1;
}
}
} else {
// Left move
i-=0x20;
i&=0x7f; // Reset high bit, if we underflowed
*gptr = i;
if (i>0x60) { // 0 11 000 01 b
// Serial.println("Underflow");
unsigned xpos = gptr[2];
xpos&=0x1f;
if (xpos==gptr[6]) // Minimum X
{
*gptr=0x81; // Clear direction, keep type '0 00 000 01'
} else {
gptr[2] = gptr[2] - 1;
}
} else {
}
}
break;
case 2:
/*
Serial.println("Vertical");
Serial.print("FRAME: ");
Serial.println( (i>>5) & 0x3 );
*/
/* Vertical */
i ^= 0x8; // '0 00 010 00
*gptr=i;
//if (i&0x18 != 0x00) { // 00001'1000b
i+=0x20;
*gptr=i;
//}