-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
1496 lines (1398 loc) · 25.1 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
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
model tiny
.386
.code
org 100h
locals @@
@entry:
jmp @start
SavedVideoPage db ?
SavedVideoMode db ?
oldInt9Seg dw ?
oldInt9Off dw ?
@start:
call parseCommandLineArgs
cmp al, 0FFh
je @@failRet
call checkKeysValues
cmp al, 0FFh
je @@failRet
mov al, [KeyValues]
test al, al
jz @@argsParsed
call showHelp
@@failRet:
ret
@@argsParsed:
call fillArgs
call initialize
call makeSnake
call drawMap
mov [GameStatus], Game_PausedPause
@@loop:
mov ax, 0h
mov es, ax
mov cx, [Speed]
add cx, [word ptr es:046ch]
@@wait:
call offSound
cmp [GameStatus], Game_Over
je @@end
hlt
cmp cx, [word ptr es:046ch]
jne @@wait
cmp [GameStatus], Game_Running
jne @@paused
call makeTurn
inc [GameTurns]
call drawMap
jmp @@loop
@@paused:
cmp [GameStatus], Game_PausedOver
je @@pausedOver
cmp [GameStatus], Game_PausedHelp
je @@pausedHelp
call printPaused
jmp @@pauseLoop
@@pausedOver:
call printGameover
call playTune
jmp @@pauseLoop
@@pausedHelp:
call printHelp
@@pauseLoop:
mov dl, [GameStatus]
@@whilePausedStay:
cmp [GameStatus], Game_Over
je @@end
cmp dl, [GameStatus]
je @@whilePausedStay
jmp @@loop
@@end:
call restorePageAndMode
mov dx, [oldInt9Off]
mov ax, [oldInt9Seg]
mov ds, ax
mov ax, 2509h
int 21h
ret
newInt9 proc
push ax bx cx dx
cli
in al, 60h
cmp al, 01h
je @@escape
cmp al, 03bh
je @@helpKey
cmp al, 01ch
je @@enterKey
cmp al, 48h
je @@ifUp
cmp al, 4dh
je @@ifRight
cmp al, 50h
je @@ifDown
cmp al, 4bh
je @@ifLeft
cmp al, 0dh
je @@ifPlus
cmp al, 0ch
je @@ifMinus
cmp al, 4eh
je @@ifPlus
cmp al, 4ah
je @@ifMinus
jmp @@end
@@escape:
cmp [GameStatus], Game_Running
je @@setPause
mov [GameStatus], Game_Over
jmp @@end
@@setPause:
mov [GameStatus], Game_PausedPause
jmp @@end
@@helpKey:
cmp [GameStatus], Game_PausedOver
je @@end
mov [GameStatus], Game_PausedHelp
jmp @@end
@@enterKey:
cmp [GameStatus], Game_PausedOver
je @@setOver
cmp [GameStatus], Game_Running
je @@setPause
mov [GameStatus], Game_Running
jmp @@end
@@setOver:
mov [GameStatus], Game_Over
jmp @@end
@@ifUp:
mov bx, [HeadType]
cmp bx, MapObjectType_SnakePartDown
je @@reverse
mov ax, [HeadCoords]
call getMapObj
mov bx, MapObjectType_SnakePartUp
call setMapObj
jmp @@end
@@ifDown:
mov bx, [HeadType]
cmp bx, MapObjectType_SnakePartUp
je @@reverse
mov ax, [HeadCoords]
call getMapObj
mov bx, MapObjectType_SnakePartDown
call setMapObj
jmp @@end
@@ifLeft:
mov bx, [HeadType]
cmp bx, MapObjectType_SnakePartRight
je @@reverse
mov ax, [HeadCoords]
call getMapObj
mov bx, MapObjectType_SnakePartLeft
call setMapObj
jmp @@end
@@ifRight:
mov bx, [HeadType]
cmp bx, MapObjectType_SnakePartLeft
je @@reverse
mov ax, [HeadCoords]
call getMapObj
mov bx, MapObjectType_SnakePartRight
call setMapObj
jmp @@end
@@reverse:
call reverseSnake
jmp @@end
@@ifMinus:
inc [Speed]
jmp @@end
@@ifPlus:
cmp [Speed], 1
je @@end
dec [Speed]
jmp @@end
@@end:
mov al, 20h ;Send EOI (end of interrupt)
out 20h, al ; to the 8259A PIC.
pop dx cx bx ax
iret
endp
showHelp proc
mov ah, 09h
lea dx, HelpCmdText
int 21h
ret
endp
HelpCmdText db 'Usage: snake [optional keys]', 13, 10,' /? - show help', 13, 10,' /l <1..9> - starting snake length', 13, 10,' /f <1..20> - number of food pieces', 13, 10,' /s <0..2> - self-collision behaviour', 13, 10,' 0 - death', 13, 10,' 1 - override', 13, 10,' 2 - cutting tail$'
fillArgs proc
mov al, [KeyValues + 02h]
mov [SelfCollisionAllowed], al
mov al, [KeyValues + 01h]
mov [StartSnakeLength], al
mov al, [KeyValues + 03h]
mov [Food1Count], al
ret
endp
MaxSnakeStartLength equ 09h
StartSnakeLength db ?
Food1Count db 01h
checkKeysValues proc
xor ax, ax
mov al, [KeyValues + 01h]
cmp ax, MaxSnakeStartLength
jg @@endFail
xor al, al
cmp al, [KeyValues + 01h]
jne @@skipLength
mov al, 04h
mov [KeyValues + 01h], al
@@skipLength:
mov al, [KeyValues + 02h]
cmp al, 02h
jg @@endFail
mov al, 02h
cmp [KeyValues + 02h], al
jne @@skipCollision
mov al, 03h
mov [KeyValues + 02h], al
@@skipCollision:
xor al, al
cmp [KeyValues + 03h], al
jne @@skipFood
mov al, 01h
mov [KeyValues + 03h], al
@@skipFood:
ret
@@endFail:
mov al, 0FFh
ret
endp
;----------DFA states enum------------
WrongState db 00h
InitialState db 01h
SlashState db 02h
KeyWithValState db 03h
KeyOnlyState db 04h
DigitState db 05h
;----------DFA states enum------------
StatesCount equ 06d
KeysCount equ 04h
KeyValues db KeysCount dup (0)
LastKey db 05h
Sygma equ 016d
Alphabet db '/?lsf 0123456789'
Terminal db StatesCount dup (0)
traslateSymbol proc
push si cx
lea si, Alphabet
mov cx, Sygma
@@whileAlNotEqualToCsSi:
cmp al, [si]
je @@found
inc si
loop @@whileAlNotEqualToCsSi
mov al, 0FFh
jmp @@end
@@found:
sub si, offset Alphabet
mov ax, si
@@end:
pop cx si
ret
endp
DFA db Sygma * StatesCount dup (0)
initDFA proc
xor ax, ax
cld
lea si, DFA
add si, Sygma
;InitialState
mov al, [SlashState] ;by slash
mov [si], al
mov al, [InitialState] ;by whitespace
mov [si + 05h], al
add si, Sygma
;SlashState
mov al, [KeyOnlyState]
mov [si + 01h], al
mov di, si ;by keys with values
add di, 02h
mov cx, KeysCount - 01h
mov al, [KeyWithValState]
rep stosb
add si, Sygma
;KeyWithValState
mov al, [KeyWithValState] ;by whitespace
mov [si + KeysCount + 01h], al
mov di, si ;by digits
add di, KeysCount + 02h
mov al, [DigitState]
mov cx, 0ah
rep stosb
add si, Sygma
;KeyOnlyState
mov al, [InitialState]
mov [si + KeysCount + 01h], al
add si, Sygma
;DigitState
mov al, [InitialState] ;by whitespace
mov [si + KeysCount + 01h], al
mov di, si ;by digits
add di, KeysCount + 02h
mov al, [DigitState]
mov cx, 0ah
rep stosb
;
lea si, Terminal
mov al, 01h
mov [si + 01h], al ;q1, q4, q5
mov [si + 04h], al
mov [si + 05h], al
ret
endp
parseArgs proc
lea di, DFA
add di, Sygma
xor dx, dx
mov dl, 01h
mov si, 80h
xor cx, cx
mov cl, [si]
test cl, cl
jz @@end
inc si
@@loop:
lodsb
call traslateSymbol
cmp al, 0FFh
je @@endFail
call processSymbol ;
call getDFAstate ;to dx
mov al, dl
mov ah, Sygma
mul ah
lea di, DFA
add di, ax
loop @@loop
@@end:
lea bx, Terminal
add bx, dx
mov al, [bx]
test al, al
jz @@endFail
ret
@@endFail:
mov al, 0FFh
ret
endp
processSymbol proc ;al = symbol dx = state ;al <- Sygma if wrong
push ax bx cx
test al, al ;slash
jz @@end
cmp al, KeysCount + 01h ;whitespace
je @@end
cmp al, KeysCount
jg @@digit
cmp al, 01h
je @@help
dec al
mov [LastKey], al
jmp @@end
@@digit:
mov cx, ax
sub cx, KeysCount + 02h
xor bx, bx
mov bl, [LastKey]
add bx, offset KeyValues
mov al, [bx]
mov ah, 0Ah
mul ah
add ax, cx
mov [bx], al
jmp @@end
@@help:
mov [KeyValues], al
@@end:
pop cx bx ax
ret
endp
getDFAstate proc
push bx
xor dx, dx
mov bx, ax
add bx, di
mov dl, [bx]
pop bx
ret
endp
parseCommandLineArgs proc
call initDFA
call parseArgs
ret
endp
printHelp proc
push bx dx
mov dx, 0207h
lea bx, HelpTextLine1
call printLine
inc dh
lea bx, HelpTextline2
call printLine
inc dh
lea bx, HelpTextLine3
call printLine
inc dh
lea bx, HelpTextLine4
call printLine
inc dh
lea bx, HelpTextLine5
call printLine
pop dx bx
ret
endp
printPaused proc
push bx dx
mov dx, 0209h
lea bx, PauseTextLine1
call printLine
inc dh
lea bx, PauseTextline2
call printLine
inc dh
lea bx, PauseTextLine3
call printLine
pop dx bx
ret
endp
fillNumber proc ;bx = offset to end of insert, cx = number
push ax bx cx dx di
xor dx, dx
mov ax, cx
mov cx, 03h
mov di, 0Ah
@@loop:
div di
add dx, '0'
mov [bx], dl
dec bx
xor dx, dx
loop @@loop
pop di dx cx bx ax
ret
endp
printGameover proc
push ax bx cx dx
xor cx, cx
mov ax, [HeadCoords]
call getMapObj
mov [SnakeLengthAtFinish], cl
mov dx, 020fh
lea bx, EndTextLine1
call printLine
xor cx, cx
mov bx, offset EndTextLine2 + 009d
mov cl, [Food1Eaten]
call fillNumber
mov bx, offset EndTextLine2 + 020d
mov cl, [Food2Eaten]
call fillNumber
mov dx, 0407h
lea bx, EndTextLine2
call printLine
inc dh
mov bx, offset EndTextLine3 + 009d
mov cl, [GameTurns]
call fillNumber
lea bx, EndTextLine3
call printLine
inc dh
mov bx, offset EndTextLine4 + 010d
mov cl, [SnakeLengthAtFinish]
call fillNumber
lea bx, EndTextLine4
call printLine
inc dh
lea bx, EndTextline5
call printLine
pop dx cx bx ax
ret
endp
PauseTextLine1 db 'PAUSED$'
PauseTextLine2 db '[Press Enter to resume]$'
PauseTextLine3 db '[Press Escape to exit]$'
HelpTextLine1 db 'Use arrow keys to turn snake$'
HelpTextLine2 db 'Press Enter to pause/unpause$'
HelpTextLine3 db 'Use +/- to change speed $'
HelpTextLine4 db 'Try "/?" key to see keys$'
HelpTextLine5 db '[Press Enter to resume]$'
EndTextLine1 db 'GAME OVER$'
EndTextLine2 db 'Food1: ___ Food2: ___$'
EndTextLine3 db 'Turns: ___$'
EndTextLine4 db 'Length: ___$'
EndTextLine5 db '[Press Enter/Escape to exit]$'
Dir_North equ 0h
Dir_Easth equ 1h
Dir_South equ 2h
Dir_West equ 3h
Dir_Dx db 0, 1, 0, -1
Dir_Dy db -1, 0, 1, 0
;------------Enum object type------------
MapObjectType_None equ 0000h
MapObjectType_Food1 equ 0101h
MapObjectType_Food2 equ 0102h
MapObjectType_Food3 equ 0103h
MapObjectType_Wall1 equ 0201h
MapObjectType_Wall2 equ 0202h
MapObjectType_Wall3 equ 0203h
MapObjectType_Wall4 equ 0204h
MapObjectType_SnakePartLeft equ 0A00h
MapObjectType_SnakePartUp equ 0A01h
MapObjectType_SnakePartRight equ 0A02h
MapObjectType_SnakePartDown equ 0A03h
;------------Enum object type------------
Expires_Never equ 0
Food3Expires dw 0fh
;-------------Map Description----------------
MapObject struc
_Type dw MapObjectType_None
_Expires dw Expires_Never
MapObject ends
MapWidth equ 32d
MapHeight equ 20d
TilePxSize equ 10d
ScreenPxWidth equ 320d
ScreenPxHeight equ 200d
MapSize equ MapHeight * MapWidth
Map MapObject MapSize dup(<>)
;-------------Map Description----------------
Speed dw 1h
HeadCoords dw ?
HeadType dw ?
;-------------Game status enum---------------
Game_Running equ 0h
Game_PausedPause equ 10h
Game_PausedHelp equ 11h
Game_PausedOver equ 12h
Game_Over equ 20h
;-------------Game status enum---------------
;-------------Game statistics----------------
Food1Eaten db 0h
Food2Eaten db 0h
GameTurns db 0h
SnakeLengthAtFinish db 0h
;-------------Game statistics----------------
GameStatus db 0h
setMapObj proc ; ah = x, al = y, bx = type, cx = expires
push ax bx cx dx
mov dh, 0
mov dl, ah
mov ah, MapWidth
mul ah
add ax, dx
mov dx, type(MapObject)
mul dx
mov dx, bx
mov bx, ax
mov Map[bx]._Type, dx
mov Map[bx]._Expires, cx
pop dx cx bx ax
ret
endp
getMapObj proc ; ah = x, al = y ===> bx = type, cx = expires
push ax dx
mov dh, 0
mov dl, ah
mov ah, MapWidth
mul ah
add ax, dx
mov dx, type(MapObject)
mul dx
mov dx, bx
mov bx, ax
mov dx, Map[bx]._Type
mov cx, Map[bx]._Expires
mov bx, dx
pop dx ax
ret
endp
drawMap proc
push ax bx cx dx
mov ax, 0A000h
mov es, ax
mov ax, 0
@@whileAhLessThanWidth:
cmp ah, MapWidth
jae @@endAh
mov al, 0
@@whileAlLessThanHeight:
cmp al, MapHeight
jae @@endAl
call getMapObj
call drawMapObj
inc al
jmp @@whileAlLessThanHeight
@@endAl:
inc ah
jmp @@whileAhLessThanWidth
@@endAh:
pop dx cx bx ax
ret
endp
;----------Colors----------
SnakeColor dw 15d
Food1Color dw 48d
Food2Color dw 43d
Food3Color dw 41d
Wall1Color dw 240d
Wall2Color dw 11d
Wall3Color dw 80d
Wall4Color dw 32d
NoneColor dw 0d
;----------Colors----------
SnakeColorsCount equ 05h
SnakeColors dw 15d, 11d, 13d, 45d, 92d
CurrentSnakeColor db 0
drawMapObj proc ; ah = x, al = y, bx = type, cx = expires
push ax bx cx dx
cmp bh, 0Ah
jne @@checkIfFood1
mov dx, [SnakeColor]
call drawBox
jmp @@end
@@checkIfFood1:
cmp bx, MapObjectType_Food1
jne @@checkIfFood2
mov dx, [Food1Color]
call drawBox
jmp @@end
@@checkIfFood2:
cmp bx, MapObjectType_Food2
jne @@checkIfFood3
mov dx, [Food2Color]
call drawBox
jmp @@end
@@checkIfFood3:
cmp bx, MapObjectType_Food3
jne @@checkIfWall1
mov dx, [Food3Color]
call drawBox
jmp @@end
@@checkIfWall1:
cmp bx, MapObjectType_Wall1
jne @@checkIfWall2
mov dx, [Wall1Color]
call drawBox
jmp @@end
@@checkIfWall2:
cmp bx, MapObjectType_Wall2
jne @@checkIfWall3
mov dx, [Wall2Color]
call drawBox
jmp @@end
@@checkIfWall3:
cmp bx, MapObjectType_Wall3
jne @@checkIfWall4
mov dx, [Wall3Color]
call drawBox
jmp @@end
@@checkIfWall4:
cmp bx, MapObjectType_Wall4
jne @@ifNone
mov dx, [Wall4Color]
call drawBox
jmp @@end
@@ifNone:
mov dx, [NoneColor]
call drawBox
jmp @@end
@@end:
pop dx cx bx ax
ret
endp
MaxY equ (MapHeight - 1)
MaxX equ (MapWidth - 1)
makeSnake proc
push ax bx cx dx
mov ax, 0
@@whileAhLessThanWidth:
cmp ah, MapWidth
jae @@endAh
mov al, 0
@@whileAlLessThanHeight:
cmp al, MapHeight
jae @@endAl
mov cx, Expires_Never
cmp ah, 0
je @@edge
cmp ah, MaxX
je @@edge
cmp al, 0
je @@edge
cmp al, MaxY
je @@edge
jmp @@notEdge
@@edge:
cmp al, 0h
je @@isWall4
cmp ah, 0h
je @@isWall2
cmp ah, MaxX
je @@isWall3
mov bx, MapObjectType_Wall1
jmp @@done
@@isWall2:
mov bx, MapObjectType_Wall2
jmp @@done
@@isWall3:
mov bx, MapObjectType_Wall3
jmp @@done
@@isWall4:
mov bx, MapObjectType_Wall4
jmp @@done
@@notEdge:
mov bx, MapObjectType_None
@@done:
call setMapObj
@@next:
inc al
jmp @@whileAlLessThanHeight
@@endAl:
inc ah
jmp @@whileAhLessThanWidth
@@endAh:
mov ax, 0a07h
mov [HeadCoords], ax
mov [HeadType], MapObjectType_SnakePartRight
mov cl, [StartSnakeLength]
xor ch, ch
@@loop:
mov bx, MapObjectType_SnakePartRight
call setMapObj
dec ah
dec cx
jcxz @@end
jmp @@loop
@@end:
mov ax, 0B0Ah
mov bx, MapObjectType_Food1
mov cx, Expires_Never
call setMapObj
mov ax, 100Ah
mov bx, MapObjectType_Food2
mov cx, Expires_Never
call setMapObj
mov ax, 150Bh
mov bx, MapObjectType_Food3
mov cx, [Food3Expires]
call setMapObj
call setRemainingFood
pop dx cx bx ax
ret
endp
setRemainingFood proc
push ax bx cx dx
xor cx, cx
mov cl, [Food1Count]
dec cx
test cx, cx
jz @@end
@@loop:
push cx
xor cx, cx
mov bx, MapObjectType_Food1
call generateMapObjWhereEmpty
mov bx, MapObjectType_Food2
call generateMapObjWhereEmpty
pop cx
loop @@loop
@@end:
pop dx cx bx ax
ret
endp
initialize proc
push ax
call savePageAndMode
call setInt9Handler
xor ax, ax
mov ax, 13h
int 10h
pop ax
ret
endp
setInt9Handler proc
push ax bx es
mov ax, 3509h
int 21h
mov [oldInt9Seg], es
mov [oldInt9Off], bx
mov ax, 2509h
mov dx, offset newInt9
int 21h
pop es bx ax
ret
endp
savePageAndMode proc
push ax bx
mov ah,0fh
int 10h
mov [SavedVideoMode], al
mov [SavedVideoPage], bh
pop bx ax
ret
endp
restorePageAndMode proc
push ax
mov ah, 0
mov al, [SavedVideoMode]
int 10h
mov ah,05h
mov al, [SavedVideoPage]
int 10h
pop ax
ret
endp
BoxSize equ TilePxSize
YMultiplier equ (TilePxSize * ScreenPxWidth)
drawBox proc ; ah = x, al = y, dx = color, es = 0A000h
push ax bx cx dx di
push dx
mov cx, ax
mov ah, 0
mov dx, YMultiplier
mul dx
mov di, ax
mov al, ch
mov ah, TilePxSize
mul ah
add di, ax
pop ax ; color
mov dx, di ; starting di for stosb
mov bx, 0
@@whileBxLessThanBoxSize:
cmp bx, BoxSize
jae @@endWhile
push ax
cmp bx, 0
je @@edge
cmp bx, (BoxSize - 1)
je @@edge
jmp @@notEdge
@@edge:
mov ax, 0
@@notEdge:
mov cx, BoxSize
mov di, dx
cld
rep stosb
mov ax, 0
dec di
stosb
mov di, dx
stosb
inc bx
add dx, ScreenPxWidth
pop ax
jmp @@whileBxLessThanBoxSize
@@endWhile:
pop di dx cx bx ax
ret
endp
MaxSnakeLength dw 040d
makeTurn proc
pushf
push ax bx cx dx
cli
@@collide:
mov ax, [HeadCoords]
call getMapObj
cmp bh, 0Ah
je @@alive
mov [GameStatus], Game_PausedOver
jmp @@end
@@alive:
cmp cx, [MaxSnakeLength]
jb @@notWonYet
mov [GameStatus], Game_PausedOver
jmp @@end
@@notWonYet:
call getNextAx
call getMapObj
mov dx, bx
call onCollideWith
cmp di, 1
je @@collide
cmp [GameStatus], Game_PausedOver
je @@end
mov ax, [HeadCoords]
call getMapObj
call getNextAx
mov [HeadCoords], ax
mov HeadType, bx
inc cx
call setMapObj
call decreaseExpirations
@@end:
pop dx cx bx ax
popf
ret
endp
getNextAx proc ; ax = current coords, bx = snake part
push bx cx dx
cmp bx, MapObjectType_SnakePartLeft
je @@left
cmp bx, MapObjectType_SnakePartRight
je @@right
cmp bx, MapObjectType_SnakePartUp
je @@up
cmp bx, MapObjectType_SnakePartDown
je @@down
jmp @@end
@@left:
dec ah
jmp @@continue
@@right:
inc ah
jmp @@continue
@@up:
dec al
jmp @@continue
@@down:
inc al
jmp @@continue
@@continue:
call getMapObj
cmp bx, MapObjectType_Wall3
jne @@end
mov ah, 01h
@@end:
pop dx cx bx
ret
endp