forked from EdouardBERGE/snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
691 lines (598 loc) · 13.3 KB
/
snake.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
RELEASE_DSK = 1
RELEASE_TAPE = 1
RELEASE_AMSDOS = 1
org #100
run #100
di
ld bc,#7F00+%10001101 : out (c),c ; MODE 1 !!!
ld bc,#FA7E : out (c),0 ; Floppy MOTOR OFF
ld hl,mycode
ld de,#1000
;**************************************************
;**************************************************
; official LZ48 decrunch routine, modified to jump
; into application after decrunch
;**************************************************
;**************************************************
LZ48_decrunch
ldi
ld b,0
nextsequence
ld a,(hl)
inc hl
cp #10
jr c,lzunpack ; no literal bytes
ld ixl,a
and #f0
rrca
rrca
rrca
rrca
cp 15 ; more bytes for literal length?
jr nz,copyliteral
getadditionallength
ld c,(hl) ; get additional literal length byte
inc hl
add a,c ; compute literal length total
jr nc,lengthNC
inc b
lengthNC
inc c
jr z,getadditionallength ; if last literal length byte was 255, we have more bytes to process
copyliteral
ld c,a
ldir
ld a,ixl
and #F
lzunpack
add 3
cp 18 ; more bytes for match length?
jr nz,readoffset
getadditionallengthbis
ld c,(hl) ; get additional match length byte
inc hl
add a,c ; compute match length size total
jr nc,lengthNCbis
inc b
lengthNCbis
inc c
jr z,getadditionallengthbis ; if last match length byte was 255, we have more bytes to process
readoffset
ld c,a
; read encoded offset
ld a,(hl)
inc a
;ret z ; LZ48 end with zero offset
jp z,init
inc hl
push hl
; source=dest-copyoffset
; A != 0 here
neg
ld l,a
ld h,#ff
add hl,de
copykey
ldir
pop hl
jr nextsequence
mycode
;**************************************************
;**************************************************
; game code relocated and crunched
;**************************************************
;**************************************************
org #1000,$
lz48
;*******************************************
; print routines
;*******************************************
SCREEN_WIDTH equ 64
DISPLAY_REFERENCE equ #C000
displaylineadr defw DISPLAY_REFERENCE
displaycharadr defw DISPLAY_REFERENCE
macro locate posx,posy
ld hl,DISPLAY_REFERENCE+{posy}*SCREEN_WIDTH
ld (displaylineadr),hl
ld hl,DISPLAY_REFERENCE+{posx}+{posy}*SCREEN_WIDTH
ld (displaycharadr),hl
mend
; A=char to display
displaychar
ld h,0
add a
ld l,a
add hl,hl
add hl,hl
ld de,minifonte
add hl,de
ex hl,de ; sprite char ADR
ld hl,(displaycharadr)
ld bc,#808
displaycharline
ld a,(de) ; donnee du caractere
inc e
ld (hl),a
ld a,h
add c
ld h,a
djnz displaycharline
ld hl,(displaycharadr)
inc hl
ld (displaycharadr),hl
ret
; a=hex value
displayhexa
push af
rrca : rrca : rrca : rrca : and 15
call displaychar
pop af
and 15
call displaychar
ret
; hl=string adr
; 255 -> EOS
; 254 -> CR
displaystring
ld a,(hl)
cp 255
ret z
push hl
call displaychar
pop hl
inc hl
jr displaystring
align 8
minifonte
incbin 'minifonte.bin'
;****************************************************
; initialisation CRTC, colors, PPI
;****************************************************
crtc_data defb 63,32,42,#8E,38,0,32,34,0,7,0,0,#30,00
init
xor a
ld bc,#F782 ; PPI output A, output C
out (c),c
ld d,a
ld e,a
ld b,#bc
ld hl,crtc_data
; loop is useless when we have memory and a good cruncher
repeat 14
out (c),a : inc b : inc b : outi : inc a : dec b
rend
ld sp,0
ld bc,4096
.razscreen
push de
push de
push de
push de
cpi
jp pe,.razscreen
;****************************************************
; draw white borders around the entire screen
;****************************************************
ld hl,#C000 : ld de,64 : exx : ld hl,#C000+63 : ld de,64 : exx : ld b,0
.vert ld (hl),%10001000 : res 6,h : ld (hl),%10001000 : set 6,h : add hl,de : exx : ld (hl),%10001 : res 6,h : ld (hl),%10001000 : set 6,h : add hl,de : exx : djnz .vert
ld hl,#C000 : ld de,#FFFF-63 : ld b,64 : ld a,d
.horiz ld (hl),a : ld (de),a : res 6,h : res 6,d : ld (hl),a : ld (de),a : set 6,h : set 6,d : inc l : inc e : djnz .horiz
call player.init
ld bc,#7F03 : out (c),c : ld a,64+11 : out (c),a
dec c : out (c),c : ld a,64+21 : out (c),a
dec c : out (c),c : ld a,64+31 : out (c),a
dec c : out (c),c : ld a,64+20 : out (c),a
set 4,c : out (c),c : out (c),a
;****************************************************
; display some messages for startup screen
;****************************************************
locate 32-4,12
ld hl,str_title
call displaystring
locate 32-12,14
ld hl,str_player1
call displaystring
locate 32-11,15
ld hl,str_nbplayer2
call displaystring
locate 32-11,16
ld hl,str_nbplayer3
call displaystring
locate 32-11,17
ld hl,str_nbplayer4
call displaystring
locate 32-12,18
ld hl,str_any
call displaystring
locate 32-14,30
ld hl,str_copy
call displaystring
.waitnbplayer
call scankeyboard
ld hl,keyboardbuffer : ld b,10 : ld de,.nbplayertab
.testnbplayer
ld a,(hl) : cp #FF : jr nz,.nbplayerok
inc hl : inc de : djnz .testnbplayer
jr .waitnbplayer
; keyboard line pressed gives the number of players
; player1 keyboard lines 0,1
; player2 keyboard line 7
; player3 keyboard line 5
; player4 keyboard line 4
.nbplayertab defb 1,1,1,1,4,3,1,2,1,1
.nbplayerok
ld a,(de)
ld (player.manage+1),a
; raz messages before play
ld hl,#C000+64
inc l
ld a,30
.raznbline
exa : ld a,8
.raznbplayer
push hl
ld de,hl : inc e
ld bc,61
ld (hl),0 : ldir
pop hl
ld bc,#800
add hl,bc
dec a
jr nz,.raznbplayer
ld bc,64-#4000
add hl,bc
exa
dec a
jr nz,.raznbline
ld bc,#7F01 : out (c),c : ld a,64+4 : out (c),a
;-----------------------------------------------------
main
;-----------------------------------------------------
; wait VBL and NO-VBL because the game is consumming
; less than 3% of CPU with one player...
;-----------------------------------------------------
ld b,#F5
.vbl in a,(c) : rra : jr nc,.vbl
.novbl in a,(c) : rra : jr c,.novbl
call player.manage
call scankeyboard
;-------------------------------------------------
; change angle modifier according to keyboard
;-------------------------------------------------
ld a,(keyboardbuffer) : bit 1,a : jr nz,.skipleft ; cursor
ld a,1 : ld (player.coordz+1),a
jr .endkey
.skipleft
ld a,(keyboardbuffer+1) : rra : jr c,.endkey ; cursor
ld a,#FF : ld (player.coordz+1),a
.endkey
ld a,(keyboardbuffer+7) : bit 6,a : jr nz,.skipleft2 ; C
ld a,1 : ld (player.coordz+1+14),a
jr .endkey2
.skipleft2
ld a,(keyboardbuffer+7) : bit 7,a : jr nz,.endkey2 ; X
ld a,#FF : ld (player.coordz+1+14),a
.endkey2
ld a,(keyboardbuffer+5) : bit 5,a : jr nz,.skipleft3 ; J
ld a,1 : ld (player.coordz+1+28),a
jr .endkey3
.skipleft3
ld a,(keyboardbuffer+5) : bit 4,a : jr nz,.endkey3 ; H
ld a,#FF : ld (player.coordz+1+28),a
.endkey3
ld a,(keyboardbuffer+4) : bit 2,a : jr nz,.skipleft4 ; J
ld a,1 : ld (player.coordz+1+42),a
jr .endkey4
.skipleft4
ld a,(keyboardbuffer+4) : bit 3,a : jr nz,.endkey4 ; H
ld a,#FF : ld (player.coordz+1+42),a
.endkey4
;------------------------------------
; this counter let us know where
; snakes leave holes
;------------------------------------
ld hl,.scronch+1 : inc (hl)
;-----------------------------------
; loop each player
;-----------------------------------
ld ix,player.coordz
ld a,(player.manage+1)
ld yh,a
.pushdisplay
push ix
ld a,(ix+13) : or a : jr z,.nochange
ld a,(ix+10) : or a : jr z,.nochange
ld e,(ix+2+1)
ld d,(ix+6+1)
;--------------------------------------------------------------
; according to player coordinates, get screen adress and masks
;--------------------------------------------------------------
call prepare_plot
push af ; backup pixel mask
and #FF ; pen 3
ld e,a
exa
or e
.scronch ld e,0
rl e : rl e : jr nc,.display : rl e : jr nc,.display ; <192 is ok
and #F ; pen 2
.display
ld c,a ; backup color
ld (hl),a ; push pixel on screen
;
res 6,h ; => transcription to #8000 page
pop af ; retrieve back pixel mask
ld d,a
ld e,(hl)
and e
cp d ; is it PEN 3 == collision?
jr nz,.notdead
ld (ix+13),0
jr .nochange
.notdead
; E still contains the 2 bits of the pixel
ld a,d : and #F : ld d,a ; we keep only low bits which represent high bit of the mask
ld a,e : and d : cp d ; is the pixel using PEN 2?
jr nz,.nopoint
;---------------------------
; increase score only if
; the previous position is
; not a score position
;---------------------------
ld a,(ix+11)
or a
jr nz,.nopoint
ld (ix+11),1
inc (ix+12) ; score++
jr .afternopoint
.nopoint
ld (ix+11),0
.afternopoint
;---------------------------------
; apply the pixel to collision map
;---------------------------------
ld a,(hl)
or c
ld (hl),a
.nochange
pop ix
ld de,player.coordz_end-player.coordz
add ix,de
dec yh
jr nz,.pushdisplay
;*** all dead? ***
ld ix,player.coordz
ld a,(player.manage+1)
ld b,a
xor a
.testdead
or (ix+13)
add ix,de
djnz .testdead
or a
jp nz,main
;-----------------------------------------------------
; display score of active players
;-----------------------------------------------------
ld bc,#7F01 : out (c),c : ld a,64+31 : out (c),a
ld hl,#C000+15*64+26
ld ix,player.coordz
exx
ld a,(player.manage+1)
ld b,a : ld c,0
ld de,player.coordz_end-player.coordz
.affichescores
exx
push hl
ld (displaycharadr),hl
ld hl,str_player : call displaystring
exx : inc c : ld a,c : exx
call displayhexa
ld hl,displaycharadr : inc (hl) : inc (hl)
ld a,(ix+12)
call displayhexa
pop hl
ld de,64
add hl,de
exx
add ix,de
djnz .affichescores
ld bc,#F540
.wvbl in a,(c) : rra : jr nc,.wvbl
.wnovbl in a,(c) : rra : jr c,.wnovbl
dec c : jr nz,.wvbl
; press a key (scan all lines)
.wkey
call scankeyboard
ld hl,keyboardbuffer
ld bc,#AFF
ld a,c
.tkey and (hl) : inc hl : djnz .tkey
cp c : jr z,.wkey
jp init
; DE=player coordz
; return
; HL=screen adress
; A=background with mask applied
; A'=pixel mask
prepare_plot
ld a,d ; Y first
and %11111000
ld h,#C0>>3
ld l,a
add hl,hl : add hl,hl : add hl,hl
ld a,d
and %111 : add a : add a : add a
or h : ld h,a
; Y line adress in HL
ld a,e
srl a
srl a
add l
ld l,a ; there is never a carry with a screen that size
; HL contains pixel adress
ld a,e
and %11
add a
ld e,a
ld d,hi(pixel)
ld a,(de) ; get background mask
and (hl)
exa
inc e
ld a,(de) ; pixel mask
ret
player
.init
ld hl,player.coordz
ld de,player.coordz+1
ld bc,14*4-1
ld (hl),0
ldir
;
ld ix,player.coordz
ld (ix+0),64 ; to the right
ld (ix+1),1
ld (ix+2+1),20
ld (ix+6+1),20
ld (ix+13),1
ld bc,player.coordz_end-player.coordz
add ix,bc
ld (ix+0),192 ; to the left
ld (ix+1),1
ld (ix+2+1),236
ld (ix+6+1),236
ld (ix+13),1
add ix,bc
ld (ix+0),128 ; downward
ld (ix+1),1
ld (ix+2+1),236
ld (ix+6+1),20
ld (ix+13),1
add ix,bc
;ld (ix+0),0 ; upward
ld (ix+1),1
ld (ix+2+1),20
ld (ix+6+1),236
ld (ix+13),1
ret
;---------------------------------------------------
; for each player, apply sinus to position according
; to the current angle. Also apply current direction
; to the current angle
;---------------------------------------------------
.manage
ld b,0
ld de, .coordz_end-.coordz
ld ix,.coordz
.manaloop
exx
ld a,(ix+0) : add (ix+1) : ld e,a : ld (ix+0),a
ld d,hi(sinus)
;********
ld a,(de)
ld c,a
add a
sbc a
ld b,a
ld hl,(ix+2)
ld (ix+4),hl ; set previous value
ld a,h
add hl,bc
sub h : ld (ix+10),a ; monitor coordinates changes
ld (ix+2),hl
;********
ld a,192
add e
ld e,a
ld a,(de)
ld c,a
add a
sbc a
ld b,a
ld hl,(ix+6)
ld (ix+8),hl ; set previous value
ld a,h
add hl,bc
sub h : or (ix+10) : ld (ix+10),a ; monitor coordinates changes
ld (ix+6),hl
exx
add ix,de
djnz .manaloop
ret
.coordz
defb 0,0 ; ang,increment
defw 0,0,0,0 ; x,y,prevx,prevy
defb 0,0 ; change/previouspixeltype
defb 0 ; score
defb 0 ; alive!
.coordz_end
nop 14 ; player 2
nop 14 ; player 3
nop 14 ; player 4
;******************************************
scankeyboard
;******************************************
ld bc,#F40E ; PPI register 14
out (c),c
ld bc,#F6C0 ; we want to select a register
out (c),c
out (c),0 ; finish operation
ld bc,#F792 ; PPI configuration input A, output C
out (c),c
ld hl,keyboardbuffer
ld de,#40F4 ; d=#40 counter / e=#F4 read port
; loop is useless when we have memory and good cruncher
repeat 10,x
if x==1
dec b
ld a,b
else
ld b,a
endif
out (c),d ; write keyboard line
ld b,e
ini ; read value to (hl++)
if x<10
inc d
endif
rend
ld bc,#F782 ; PPI output A, output C
out (c),c
ret
keyboardbuffer: DEFS 10,#C9 ; initialized to "ret" to icnrease compression ratio
charset '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-:.! =/\\[]_^<>@()',0
str_title defb 'SNAKE v1.0',255
str_mauvais defb 'GAME OVER',255
str_nbplayer2 defb 'Press X/C for 2 players',255
str_nbplayer3 defb 'Press H/J for 3 players',255
str_nbplayer4 defb 'Press I/O for 4 players',255
str_any defb 'Any other key to play solo',255
str_player1 defb 'Player 1 uses cursor keys',255
str_copy defb 'Resistance game department 2021',255
str_player defb 'Player ',255
charset
align 256
sinus
ang=0
repeat 256
defb sin(ang)*127
ang=ang+360/256
rend
pixel
defb %01110111,%10001000
defb %10111011,%01000100
defb %11011101,%00100010
defb %11101110,%00010001
lzclose
org $
lafin
if RELEASE_DSK
save"snake.bin",#100,lafin-#100,DSK,"snake.dsk"
endif
if RELEASE_TAPE
save"snake.bin",#100,lafin-#100,TAPE,"snake.cdt"
endif
if RELEASE_AMSDOS
save"snake.bin",#100,lafin-#100,AMSDOS
endif