-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpacemuzx.asm
2994 lines (2522 loc) · 84.4 KB
/
pacemuzx.asm
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
; Pac-Man hardware emulation for the Sinclair ZX Spectrum (v1.6)
;
; https://simonowen.com/spectrum/pacemuzx/
debug: equ 0 ; non-zero for border stripes showing CPU use
colour: equ 1 ; non-zero for switchable colour/mono, zero for mono-only
; Memory maps
;
; Loading (normal paging R/5/2/0):
; a000-afff - emulation code
; b000-bfff - unshifted tiles+sprites
; c000-ffff - 16K Pac-Man ROM
;
; Emulation (special paging 0/1/2/3):
; 0000-3fff - 16K Pac-Man ROM
; 4000-50ff - Pac-Man display, I/O and working RAM
; 5100-7fff - unused
; 8000-9fff - 2nd half of sprite data
; a000-b1ff - emulation code
; b200-bfff - look-up tables
; c000-dfff - 8K sound table
; e000-ffff - first 8K of Pac-Man ROM (unpatched)
;
; Graphics (normal paging R3/5/2/7):
; 0000-3fff - Spectrum 48K ROM
; 4000-5aff - Spectrum display (normal)
; 5b00-5bef - screen data behind sprites (normal)
; 5bf0-9fff - pre-rotated sprite graphics
; a000-b1ff - emulation code
; b200-bfff - look-up tables
; c000-daff - Spectrum display (alt)
; db00-dbef - screen data behind sprites (alt)
; dbf0-ffff - pre-rotated tile graphics
default_attr: equ &07 ; default = white on black
kempston: equ 31 ; Kempston joystick in bits 4-0
divide: equ 227 ; DivIDE interface
border: equ 254 ; Border colour in bits 2-0
keyboard: equ 254 ; Keyboard matrix in bits 4-0
pac_footer: equ &4000 ; credit and fruit display
pac_chars: equ &4040 ; start of main Pac-Man display (skipping the score rows)
pac_header: equ &43c0 ; 64 bytes containing the score
pac_ypos: equ &4d08 ; Pac-Man y position (as viewed)
pac_xpos: equ &4d09 ; Pac-Man x position (as viewed)
pac_dir: equ &4d30 ; Pac-Man facing direction (0=right, 1=down, 2=left, 3=up)
; address of saved sprite block followed by the data itself
spr_save_2: equ &5b00
spr_save_3: equ spr_save_2+2+2+(3*12) ; attr address, data address, 3 bytes * 12 lines
spr_save_4: equ spr_save_3+2+2+(3*12)
spr_save_5: equ spr_save_4+2+2+(3*12)
spr_save_6: equ spr_save_5+2+2+(3*12)
spr_save_7: equ spr_save_6+2+2+(3*12)
spr_save_end: equ spr_save_7+2+2+(3*12)
; pre-shifted sprite graphics
spr_data_0: equ spr_save_end
spr_data_1: equ spr_data_0 + (76*2*12) ; 11111111 11110000
spr_data_2: equ spr_data_1 + (76*2*12) ; 01111111 11111000
spr_data_3: equ spr_data_2 + (76*2*12) ; 00111111 11111100
spr_data_4: equ spr_data_3 + (76*2*12) ; 00011111 11111110
spr_data_5: equ spr_data_4 + (76*2*12) ; 00001111 11111111
spr_data_6: equ spr_data_5 + (76*3*12) ; 00000111 11111111 10000000
spr_data_7: equ spr_data_6 + (76*3*12) ; 00000011 11111111 11000000
spr_data_end: equ spr_data_7 + (76*3*12) ; 00000001 11111111 11100000
; pre-shifted tile graphics
tile_data_0: equ &8000 + spr_data_0
tile_data_6: equ tile_data_0 + (192*1*6) ; 11111100
tile_data_4: equ tile_data_6 + (192*2*6) ; 00000011 11110000
tile_data_2: equ tile_data_4 + (192*2*6) ; 00001111 11000000
end_tile_data: equ tile_data_2 + (192*1*6) ; 00111111
; sound look-up table
sound_table: equ &c000
MACRO set_border, bordcol
IF debug
ld a,bordcol
out (border),a
ENDIF
ENDM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
org &a000
start: jr start2
; Dips at fixed address for easy poking
dip_5000: defb %11111111 ; c21tdrlu c=credit, 2=coin2, 1=coin1, t=rack test on/off ; down, right, left, up
; default: rack test off, nothing else signalled
dip_5040: defb %11111111 ; c--sdrlu c=cocktail/upright ; s=service mode on/off ; down, right, left, up (player 2)
; default: upright mode, service mode off, no controls pressed
dip_5080: defb %11001001 ; -dbbllcc d=hard/normal bb=bonus life at 10K/15K/20K/none ; ll=1/2/3/5 lives ; cc=freeplay/1coin1credit/1coin2credits/2coins1credit
; default: normal, bonus life at 10K, 3 lives, 1 coin 1 credit
start2: di
ld hl,loading_msg
call print_msg
ld a,(&5b5c) ; sysvar holding 128K paging
ex af,af' ; keep safe
ld a,%00000001 ; special paging, banks 0/1/2/3
ld bc,&1ffd ; +3 paging port
out (c),a ; attempt paging
ld a,(3) ; peek value in Pac-Man ROM
cp &ed ; as expected?
jr z,start3 ; if so, start up
ex af,af'
out (c),a ; restore 128K paging (disturbed due to partial address decoding)
ei
ld hl,plus2a3_msg
jp print_msg
; Next, move any data from load position to final location
start3: ld sp,new_stack
ld hl,&0000
ld de,&e000
ld bc,&2000
ldir ; copy 8K of ROM to &e000 (start of page 3)
call patch_rom ; patch the ROM while it's at the correct location
ld a,%00000100 ; +3 normal paging
ld bc,&1ffd
out (c),a ; restore R3/5/2/0, for ROM access at &c000
call chk_specnet ; check if Spectranet traps are enabled
jr z,no_specnet ; skip forwards if not
ld hl,specnet_msg ; prompt to disable traps
call print_msg
wait_specnet: ld bc,0 ; delay roughly half a second
delay: djnz $
djnz $
dec c
jr nz,delay
call chk_specnet ; check traps again
jr nz,wait_specnet ; jump back if they're still enabled
no_specnet:
ld a,%10000011 ; DivIDE page 3
out (divide),a ; page in at &2000, if present
ld hl,&c000
ld de,&2000
ld bc,&2000
ldir ; copy first 8K of ROM to DivIDE page 3
ld a,%10000000 ; DivIDE page 0
out (divide),a ; page in at &2000, if present
ld de,&2000
ld bc,&2000
ldir ; copy last 8K of ROM to DivIDE page 0
ld a,%01000000 ; MAPRAM
out (divide),a ; page 3 at &0000 (read-only), page 0 at &2000
ld a,(scr_page) ; normal display, page 7
ld bc,&7ffd
out (c),a ; page 7 at &c000
ld ixh,&80 ; write to alt screen
ld hl,load_tiles
ld de,tile_data_0
ld bc,&0480
ldir ; copy unshifted tile data
ld hl,load_sprites
ld de,spr_data_0
ld bc,&0720
ldir ; copy unshifted sprite data
call make_tables ; create all the look-up tables and pre-shift sprites
call page_rom ; page in sound table and ROM
call sound_init ; enable sound chip
call init_screens ; prepare both screens and sprite save areas
ld hl,&5000 ; Pac-Man I/O area
xor a
clear_io: ld (hl),a ; zero fill it
inc l
jr nz,clear_io
ld a,(dip_5000) ; set hardware dips to our defaults
ld (&5000),a
ld a,(dip_5040)
ld (&5040),a
ld a,&bf
in a,(keyboard)
bit 4,a ; Z if H pressed
ld a,(dip_5080)
jr nz,not_hard
and %10111111 ; set Hard difficulty
not_hard: ld (&5080),a
ld sp,&4c00 ; stack in spare RAM
jp 0 ; start the ROM! (and page in DivIDE, if present)
page_rom: push af
push bc
ld a,%00000001 ; +3 special paging, banks 0/1/2/3
ld bc,&1ffd
out (c),a
pop bc
pop af
ret
page_screen: push af
push bc
ld a,%00000100 ; +3 normal paging, R3/5/2/7
ld bc,&1ffd
out (c),a
pop bc
pop af
ret
patch_rom: ld a,&56 ; ED *56*
ld (&233c),a ; change IM 2 to IM 1
ld hl,&47ed
ld (&233f),hl ; change OUT (&00),A to LD I,A
ld (&3183),hl
ld a,&c3 ; JP nn
ld (&0038),a
ld hl,do_int_hook ; interrupt hook
ld (&0039),hl
ld hl,&04d6 ; SUB 4
ld (&3181),hl ; restore original instruction in patched bootleg ROMs
ld a,&01 ; to change &5000 writes to &5001, which is unused
ld (&0093),a
ld (&01d7),a
ld (&2347),a
ld (&238a),a
ld (&3194),a
ld (&3248),a
ld a,1 ; start clearing at &5001, to avoid DIP overwrite
ld (&230c),a
ld (&2353),a
ld a,7 ; shorten block clear after start adjustment above
ld (&230f),a
ld (&2357),a
ld a,&41 ; start clearing at &5041, to avoid DIP overwrite
ld (&2363),a
ld a,&3f ; shorten block clear after start adjustment above
ld (&2366),a
ld a,&b0 ; LSB of address in look-up table
ld (&3ffa),a ; skip memory test (actual code starts at &3000)
ld hl,&e0f6 ; change AND &1F to OR &E0 so ROM peeks are from unmodified copy of the ROM
ld (&2a2d),hl ; (used as random number source for blue ghost movements)
ld a,&cd ; CALL nn
ld (&2c62),a
ld hl,text_fix ; fix bit 7 being set in horizontal text screen writes
ld (&2c63),hl
ld a,&dc ; CALL C,nn
ld (&0353),a ; disable 1UP/2UP flashing to save cycles
ld (&035e),a
ret
; The ROM uses bit 7 of the display address to indicate text to the top/bottom lines, which
; is rotated compared to the main screen. It doesn't clear the bit before using the address,
; but due to RAM mirroring the arcade version still works. Our version requires a fix.
text_fix: ld e,(hl)
inc hl
ld d,(hl)
res 7,d
ret
; Do everything we need to update video/sound/input
;
do_int_hook: ld (old_stack+1),sp
ld sp,new_stack
push af
push bc
push de
push hl
ex af,af'
push af
exx
push bc
push de
push hl
push ix
call do_flip ; show last frame, page in new one
ld hl,&5062 ; sprite 1 x
inc (hl) ; offset 1 pixel left (mirrored)
ld hl,&5064 ; sprite 2 x
inc (hl)
set_border 1
call do_restore ; restore under the old sprites
set_border 2
call do_tiles ; update a portion of the background tiles
set_border 3
call do_input ; scan the joystick and DIP switches
call do_sound ; convert the sound to the AY chip
set_border 4
call flash_maze ; update maze colour if changed
call flash_pills ; flash the power pills
set_border 5
call do_save ; save under the new sprite positions
set_border 6
call do_sprites ; draw the 6 new masked sprites
set_border 7
call do_trim ; trim sprites at screen edge
set_border 0
ld hl,&5062 ; sprite 1 x
dec (hl) ; reverse change from above
ld hl,&5064 ; sprite 2 x
dec (hl)
call set_int_chain ; prepare interrupt handler chain
pop ix
pop hl
pop de
pop bc
exx
pop af
ex af,af'
pop hl
pop de
pop bc
pop af
old_stack: ld sp,0 ; self-modified by code above
int_chain: jp 0 ; address completed by set_int_chain
; Prepare the Pac-Man interrupt handler address for our return - does an IM2-style
; lookup to determine the address for normal Pac-Man interrupt processing
;
set_int_chain: ld a,i ; bus value originally written to port &00
ld l,a
ld h,&3f ; normal I value
ld a,(hl) ; handler low
inc hl
ld h,(hl) ; handler high
ld l,a
ld (int_chain+1),hl ; write into JP in interrupt handler
ret
; Flip to show the screen prepared during the last frame, and prepare to draw the next
;
do_flip: push af
push bc
ld a,(scr_page) ; current screen
xor %00001000 ; toggle active screen bit
ld (scr_page),a
ld bc,&7ffd
out (c),a ; activate
sub %00001000 ; set carry if we're viewing the normal screen
ld a,0
rra
ld ixh,a ; b7 holds b15 of drawing screen address
pop bc
pop af
ret
scr_page: defb %00000111 ; normal screen (page 5), page 7 at &c000
; Set the maze palette colour by detecting the attribute used for the maze white
; We also need to remove the ghost box door, as the real attribute wipe does.
;
flash_maze: ld a,(&4440) ; attribute of maze top-right
cp &1f ; white?
ld a,(attr_colour) ; current attribute setting
jr nz,maze_blue ; if not, draw as normal
xor %01000000 ; toggle bright
ld b,a ; save
IF colour
patch_and: and %00000111 ; keep basic colour
cp 7 ; white?
jr nz,not_white
ld b,&41 ; change to bright blue
not_white: cp 1 ; blue?
jr nz,not_blue
ld b,&07 ; change to white
not_blue:
ENDIF
ld a,&40 ; blank tile
ld (&420d),a ; clear left of ghost box door
ld (&41ed),a ; clear right of ghost box door
ld a,b
maze_blue:
call page_screen
attr_scr_lp: ld hl,&5800+5
ex af,af'
ld a,h
or ixh
ld h,a
ex af,af'
cp (hl) ; check current colour
jr z,attr_same ; skip fill if it's the correct colour
ld de,32-22
ld b,&18 ; 24 lines to fill
attr_fill_lp: ld c,22
attr_fill_lp2: ld (hl),a
inc l
dec c
jr nz,attr_fill_lp2
add hl,de
djnz attr_fill_lp
attr_same: jp page_rom
attr_colour: defb default_attr
; Set the power pill palette colour to the correct state by reading the 6 known
; pill locations, and reacting to changes in the attribute setting.
;
flash_pills: ld a,($4278) ; bottom attract screen pill location
cp &14 ; power pill?
jr z,attract_mode ; if so, we're in attract mode
ld a,(&4384) ; top-left
cp &14
ld a,(&4784)
ld hl,&4066 ; x=2, y=4
ld bc,&878f ; pre-ORed left byte data
ld de,&80c0 ; original right byte data
call z,pill_2
ld a,(&4064) ; top right
cp &14
ld a,(&4464)
ld hl,&4079 ; x=27, y=4
call z,pill_1
ld a,(&4398) ; bottom-left
cp &14
ld a,(&4798)
ld hl,&5046 ; x=2, y=24
ld bc,&878f
ld de,&80c0
call z,pill_2
ld a,(&4078) ; bottom-right
cp &14
ld a,(&4478)
ld hl,&5059 ; x=27, y=24
call z,pill_1
ret
attract_mode: ld a,($4678) ; bottom attract
ld hl,&504d ; x=11, y=24
call pill_1
ld a,(&4332) ; top attract
cp &14
ld a,(&4732)
ld bc,&0103
ld de,&e0f0
ld hl,&4ca8 ; x=5, y=18
call z,pill_2
ret
; Pill tile in 1 byte
pill_1: cp &9f
jr z,pill_1_on
cp &10
jr nz,pill_clear_1
pill_1_on:
ld a,%00000100 ; +3 normal paging, R3/5/2/7
ld bc,&1ffd
out (c),a
ld a,h
or ixh
ld h,a
ld de,&1e3f ; pill data for ends and middle
ld (hl),d
inc h
ld (hl),e
inc h
ld (hl),e
inc h
ld (hl),e
inc h
ld (hl),e
inc h
ld (hl),d
ld a,%00000001 ; +3 special paging, banks 0/1/2/3
ld bc,&1ffd
out (c),a ; page ROM
ret
; clear pill
pill_clear_1: ld a,%00000100 ; +3 normal paging, R3/5/2/7
ld bc,&1ffd
out (c),a
ld a,h
or ixh
ld h,a
xor a
ld (hl),a
inc h
ld (hl),a
inc h
ld (hl),a
inc h
ld (hl),a
inc h
ld (hl),a
inc h
ld (hl),a
ld a,%00000001 ; +3 special paging, banks 0/1/2/3
ld bc,&1ffd
out (c),a ; page ROM
ret
; Pill tile spanning 2 bytes
pill_2: cp &10
jr nz,pill_clear_2
call page_screen
ld a,h
or ixh
ld h,a
ld (hl),b
inc l
ld a,(hl)
or d
ld (hl),a
dec l
inc h
ld (hl),c
inc l
ld a,(hl)
or e
ld (hl),a
dec l
inc h
ld (hl),c
inc l
ld a,(hl)
or e
ld (hl),a
dec l
inc h
ld (hl),c
inc l
ld a,(hl)
or e
ld (hl),a
dec l
inc h
ld a,h
and %00000111
call z,blockdown_hl
ld (hl),c
inc l
ld a,(hl)
or e
ld (hl),a
dec l
inc h
ld (hl),b
inc l
ld a,(hl)
or d
ld (hl),a
dec l
inc h
ld a,%00000001 ; +3 special paging, banks 0/1/2/3
ld bc,&1ffd
out (c),a ; page ROM
ret
; clear pill
pill_clear_2: call page_screen
ld a,h
or ixh
ld h,a
ld de,&c00f ; mask
ld a,(hl)
and d
ld (hl),a
inc l
ld a,(hl)
and e
ld (hl),a
dec l
inc h
ld a,(hl)
and d
ld (hl),a
inc l
ld a,(hl)
and e
ld (hl),a
dec l
inc h
ld a,(hl)
and d
ld (hl),a
inc l
ld a,(hl)
and e
ld (hl),a
dec l
inc h
ld a,(hl)
and d
ld (hl),a
inc l
ld a,(hl)
and e
ld (hl),a
dec l
inc h
ld a,h
and %00000111
call z,blockdown_hl
ld a,(hl)
and d
ld (hl),a
inc l
ld a,(hl)
and e
ld (hl),a
dec l
inc h
ld a,(hl)
and d
ld (hl),a
inc l
ld a,(hl)
and e
ld (hl),a
ld a,%00000001 ; +3 special paging, banks 0/1/2/3
ld bc,&1ffd
out (c),a ; page ROM
ret
; Scan the input DIP switches for joystick movement and button presses
;
do_input: ld de,&ffff ; nothing pressed
ld a,&7f
in a,(keyboard)
bit 1,a ; Sym?
jr nz,not_sym
ld bc,&0501 ; 5 bits to check, first colour is blue
ld a,&fe
in a,(keyboard)
rra ; Shift?
jr nc,not_bright
set 6,c ; use bright version
not_bright:
ld a,&f7
in a,(keyboard)
inp_col_lp: rra
jr nc,got_colour ; 1-5 = blue/red/magenta/green/cyan
inc c
djnz inp_col_lp
ld a,&ef
in a,(keyboard)
bit 4,a ; 6 = yellow?
jr z,got_colour
inc c
bit 3,a ; 7 = white?
jr z,got_colour
rra
jp c,joy_done
got_colour: ld a,c
ld (attr_colour),a ; set to be picked up by flash_maze
ret
not_sym: ld a,&f7
in a,(keyboard)
cpl
and %00000111
jr z,not_123
rra
jr nc,not_1
res 5,e ; 1 = start 1
not_1: rra
jr nc,not_2
res 6,e ; 2 = start 2
not_2: rra
jr nc,not_123
res 5,d ; 3 = coin 1
not_123:
ld a,&fe
in a,(keyboard)
rra
jr c,no_shift
ld a,&fb
in a,(keyboard)
bit 4,a
jr nz,not_shift_t
res 4,d ; shift-t = rack test
not_shift_t:
; Shifted for Cursor keys
ld a,&f7
in a,(keyboard)
bit 4,a
jr nz,not_shift_5
res 1,d ; Shift-5 = left
not_shift_5:
ld a,&ef
in a,(keyboard)
cpl
and %00011100
jr z,read_joy
rra
rra
rra
jr nc,not_shift_8
res 2,d ; Shift-8 = right
not_shift_8: rra
jr nc,not_shift_7
res 0,d ; Shift-7 = up
not_shift_7: rra
jr nc,read_qaop
res 3,d ; Shift-6 = down
jr read_qaop
; Unshifted for Sinclair joystick
no_shift: ld a,&ef
in a,(keyboard)
cpl
and %00011111
jr z,not_67890
rra
jr nc,not_0
res 5,d ; 0 = coin 1
res 5,e ; 0 = start 1
not_0: rra
jr nc,not_9
res 0,d ; 9 = up
not_9: rra
jr nc,not_8
res 3,d ; 8 = down
not_8: rra
jr nc,not_7
res 2,d ; 7 = right
not_7: rra
jr nc,not_67890
res 1,d ; 6 = left
not_67890:
read_qaop: ld a,&fb
in a,(keyboard)
rra
jr c,not_q
res 0,d ; Q = up
not_q:
ld a,&fd
in a,(keyboard)
rra
jr c,not_a
res 3,d ; A = down
not_a:
ld a,&df
in a,(keyboard)
rra
jr c,not_p
res 2,d ; P = right
not_p: rra
jr c,not_o
res 1,d ; O = left
not_o:
ld a,&7f
in a,(keyboard)
rra
jr c,not_space
res 5,d ; space = coin 1
res 5,e ; space = start 1
not_space:
IF colour
ld a,&fe
in a,(keyboard)
bit 3,a ; C = colour
jp z,set_colour
ld a,&7f
in a,(keyboard)
bit 2,a ; M = mono
jp z,set_mono
ENDIF
; Kempston joystick
read_joy: in a,(kempston) ; read Kempston joystick
inc a
cp 2
jr c,not_fire ; ignore blank or invalid inputs
dec a
rra
jr nc,not_right
res 2,d ; right
not_right: rra
jr nc,not_left
res 1,d ; left
not_left: rra
jr nc,not_down
res 3,d ; down
not_down: rra
jr nc,not_up
res 0,d ; up
not_up: rra
jr nc,not_fire
res 5,d ; Fire = coin 1
res 5,e ; Fire = start 1
not_fire:
ld a,d ; dip including controls
cpl ; invert so set=pressed
and %00001111 ; keep only direction bits
jr z,joy_done ; skip if nothing pressed
ld c,a
neg
and c ; keep least significant set bit
cp c ; was it the only bit?
jr z,joy_done ; skip if so
ld a,(pac_dir) ; facing direction (r/d/l/u)
bit 1,a
rra
ld c,&07 ; position mask for tile offset
jr c,face_ud
face_lr: ld a,(pac_xpos) ; x position
jr z,face_r ; (from bit test above)
face_l: set 1,d ; release left
and c ; mask to tile offset
cp 5 ; entering junction?
jr c,joy_done ; if not we're done
ignore_ud: set 0,d ; release up
set 3,d ; release down
jr joy_done
face_r: set 2,d ; release right
and c
cp 4 ; entering junction?
jr c,ignore_ud ; if so, ignore up+down
jr joy_done
face_ud: ld a,(pac_ypos) ; y position
jr z,face_d ; (from bit test above)
face_u: set 0,d ; release up
and c
cp 4 ; entering junction?
jr nc,joy_done ; if not we're done
ignore_lr: set 1,d ; release left
set 2,d ; release right
jr joy_done
face_d: set 3,d ; release down
and c
cp 5 ; entering junction?
jr nc,ignore_lr ; if so, ignore left+right
joy_done: ld a,d
joy_multi: ld (&5000),a
ld a,e
ld (&5040),a
ret
; Check sprite visibility, returns carry if any visible, no-carry if all hidden
is_visible: ld a,&10 ; minimum x/y position to be visible
ld b,7 ; 7 sprites to check
ld hl,&5062
vis_lp: cp (hl)
ret c
inc l
cp (hl)
ret c
inc l
inc l
inc l
djnz vis_lp
ret
; Draw the background tile changes, in 1-5 steps over the 2 double-buffered screens
do_tiles: call is_visible ; set carry state for below
tile_state: ld a,ixh
bit 7,a ; alt screen? (don't disturb carry!)
jr c,tile_strips ; if any sprites are visible we'll draw in strips
jr nz,fulldraw_alt ; full screen draw (alt)
fulldraw_norm: ld b,28
ld de,pac_chars
ld hl,bak_chars1-pac_footer
add hl,de
call tile_comp
ld hl,bak_chars1
call do_fruit
ld hl,bak_chars1
call do_lives
ld hl,bak_chars1
call do_score1
ld hl,bak_chars1
jp do_score2
fulldraw_alt: ld b,28
ld de,pac_chars
ld hl,bak_chars2-pac_footer
add hl,de
call tile_comp
ld hl,bak_chars2
call do_fruit
ld hl,bak_chars2
call do_lives
ld hl,bak_chars2
call do_score1
ld hl,bak_chars2
jp do_score2
tile_strips: jp nz,strip_odd
strip_even: jp strip_0
strip_0: ld b,7
ld de,pac_chars+(32*7*0)
ld hl,bak_chars1-pac_footer