-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainLoop.s
964 lines (732 loc) · 17.6 KB
/
mainLoop.s
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
.data
frameCounter: .word 0 # simple timer
playerMovementDirection : .word 0
playerX : .word 0
playerY : .word 0
playerSpeed: .word 0
currentScore: .word 0
highScore: .word 0
# Yayy, we've just doubled our memory need (320 > 256 => 1 byte is not enough)
bulletsCoord: .skip 80 # alocate 40 bytes for 20 bullets (2 bytes for position x, 2 bytes for y)
enemiesCoord: .skip 120 # 20 enemies on the screen in the same time is enough (2 bytes per coordonate + one for health)
enemyBulletsCoord: .skip 80 # 6 bullets on the same time on screen (2bytes for Xcoord 2bytes for Ycoord)
enemyNumber: .word 0
shootOrder: .skip 60 # shooting order containing 30 items
currentShootPos: .word 0
isAlive: .word 0
recordesListed: .word 0
menuActiveButton: .word 0
infoButtonColor: .word 0
startButtonColor: .word 0
currentStage: .word 0
deathEnemies: .word 0
currentNumberOfEnemies: .word 0
isBetweenStages: .word 0
framesBeforeShoot: .word 0
spritePointer: .word 0
topScores: .skip 20 # 10 highscores
topScoresNumber: .word 0
.text
highScoreText: .asciz "Top Scores"
playButtonText: .asciz "Play"
infoButtonText: .asciz "Scoreboard"
stageInfoText: .asciz "Stage cleared"
noRecordsText: .asciz "No records available"
exitRecordsButton: .asciz " Great!"
#logoTitleText: .asciz "(totally not)"
logoTitleText: .asciz "Space Invaders"
.section .mainLoop
mainLoop:
#get into graphics mode 13
mov $0x13, %ax
int $0x10
mov $0, topScoresNumber
jmp mainMenuRender
#_________________________________________main menu______________________________________
renderTheBasicScreen:
push %bp
mov %sp, %bp
mov $120, %di
mov $120, %si
mov $0, %dx
push $90
push $5
lea logoSprite, %bx
mov %bx, spritePointer
call drawSprite
add $4, %sp
mov $20, %di
mov $20, %si
mov $0, %dx
push $100
push $0
lea enemySprite, %bx
mov %bx, spritePointer
call drawSprite
add $4, %sp
mov $20, %di
mov $20, %si
mov $0, %dx
push $185
push $10
lea enemySprite, %bx
mov %bx, spritePointer
call drawSprite
add $4, %sp
mov $20, %di
mov $20, %si
mov $0, %dx
push $45
push $135
lea enemySprite, %bx
mov %bx, spritePointer
call drawSprite
add $4, %sp
mov $logoTitleText, %di
mov $12, %si
mov $14, %dx
call writeStringOnScreen
mov startButtonColor, %dx
mov $20, %di
mov $100, %si
push $10
push $153
call drawRect
add $4, %sp
mov $0, %dx
mov $15, %di
mov $90, %si
push $15
push $156
call drawRect
add $4, %sp
mov infoButtonColor, %dx
mov $20, %di
mov $100, %si
push $194
push $153
call drawRect
add $4, %sp
mov $0, %dx
mov $15, %di
mov $90, %si
push $199
push $156
call drawRect
add $4, %sp
mov $playButtonText, %di
mov $ 6, %si
mov $20, %dx
call writeStringOnScreen
mov $infoButtonText, %di
mov $ 26, %si
mov $20, %dx
call writeStringOnScreen
mov %bp, %sp
pop %bp
ret
mainMenuRender:
call clearWholeScreen
mov $3, startButtonColor
mov $12, infoButtonColor
mov $1, menuActiveButton
mov $0, recordesListed
call renderTheBasicScreen
jmp mainMenuRenderLoop
keyboardEventHandlerMenu:
mov $0, %ax
mov $0x00, %ah
int $0x16
jnz keyboardHandlerMenu
keyboardHandlerMenu:
mov recordesListed, %bx
cmp $0, %bx
jg keyboardHandlerForRecord
cmpb $'a', %al
je selectTheStartButton
cmpb $'d', %al
je selectInfoButton
cmpb $' ', %al
je choseThisOption
jmp mainMenuRenderLoop2
keyboardHandlerForRecord:
cmpb $' ', %al
je exitRecords
jmp mainMenuRenderLoop2
exitRecords:
call clearWholeScreen
call renderTheBasicScreen
mov $0, recordesListed
jmp mainMenuRenderLoop2
choseThisOption:
mov menuActiveButton, %ax
cmp $0, %ax
jl displayInfos
call clearWholeScreen
jmp gameInit
displayInfos:
mov $1, recordesListed
call clearWholeScreen
mov infoButtonColor, %dx
mov $13, %di
mov $65, %si
push $107
push $157
call drawRect
add $4, %sp
mov $exitRecordsButton, %di
mov $14, %si
mov $20, %dx
call writeStringOnScreen
cmp $0, highScore
je displayNoRecord
mov $highScoreText, %di
mov $14, %si
mov $2, %dx
call writeStringOnScreen
#mov highScore, %di
#mov $25, %si
#mov $2, %dx
#call writeNumberOnScreen
call writeTopScoresOnScreen
jmp mainMenuRenderLoop2
writeTopScoresOnScreen:
push %bp
mov %sp, %bp
call sortTopScores
mov $0, %di
mov $4, %dx
jmp writeTopScoresOnScreenLoop
writeTopScoresOnScreenLoop:
mov topScoresNumber, %ax
cmp %ax, %di
jge writeTopScoresOnScreenEnd
lea topScores, %bx
add %di, %bx
add %di, %bx
push %di
push %dx
mov (%bx), %di
mov $20, %si
call writeNumberOnScreen
pop %dx
pop %di
inc %di
add $2, %dx
jmp writeTopScoresOnScreenLoop
writeTopScoresOnScreenEnd:
mov %bp, %sp
pop %bp
ret
displayNoRecord:
mov $noRecordsText, %di
mov $10, %si
mov $2, %dx
call writeStringOnScreen
jmp mainMenuRenderLoop2
mainMenuRenderLoop:
mov $0, %ax
mov $0x01, %ah
int $0x16
jnz keyboardEventHandlerMenu
jmp mainMenuRenderLoop2
selectInfoButton:
mov $-1, menuActiveButton
mov $12, startButtonColor
mov $3, infoButtonColor
call renderTheBasicScreen
jmp mainMenuRenderLoop2
selectTheStartButton:
mov $1, menuActiveButton
mov $12, infoButtonColor
mov $3, startButtonColor
call renderTheBasicScreen
jmp mainMenuRenderLoop2
mainMenuRenderLoop2:
jmp mainMenuRenderLoop
#___________________________________end_main_menu_______________________________________________
gameInit:
mov $1, isAlive
mov $20, enemyNumber
mov $0, playerMovementDirection # making possible some sort of handmade multi key presses
mov $2, playerSpeed # default speed
mov $60, playerX
mov $150, playerY
mov $0, currentScore
mov $1, currentStage
mov $0, isBetweenStages
mov $6, currentNumberOfEnemies
mov currentNumberOfEnemies, %ax
mov %ax,deathEnemies
call initShootOrder
call initEnemiesLine
call initEnemyBullets
call drawEnemies
jmp update # start the game
update: # game main loop
mov isAlive, %ax
cmp $0, %ax
jle die # we've died, let's restart the thing
call clearScreenBuffer # clear the screen buffer
mov $0, %ax
mov $0x01, %ah
int $0x16
jnz keyboardHandler
updatePart2: # nothing to see here, just an update function doing it's stuff
call updatePlayer
call updateBullet
call updateEnemyBullet
call drawPlayer
call drawEnemies
call drawEnemyBullets
call drawBullets
mov isBetweenStages, %ax
#cmp $0, %ax
#jne skipDrawinEnemies
skipDrawinEnemies:
mov currentScore, %di
mov $40, %si
mov $1, %dx
call writeNumberOnScreen
#mov highScore, %di
#mov $3, %si
#mov $1, %dx
#call writeNumberOnScreen
call incrementFrameCounter # timer
call waitForFrame # wait for frame
jmp update
addScoreToList:
push %bp
mov %sp, %bp
mov topScoresNumber, %ax
lea topScores, %bx
add %ax, %bx
add %ax, %bx
mov currentScore, %ax
mov %ax, (%bx) #store the score
inc topScoresNumber
mov %bp, %sp
pop %bp
ret
die:
call clearWholeScreen
call addScoreToList
mov highScore, %ax
mov currentScore, %bx
cmp %ax, %bx
jle dieWithoutHighScore
mov %bx, highScore
dieWithoutHighScore:
jmp mainMenuRender
randomEnemyShoot:
push %bp
mov %sp, %bp
mov $0, %di
mov $-1, %cx
jmp shootLoop
enemyShootReturn:
mov %bp, %sp
pop %bp
ret
keyboardHandler:
mov $0, %ax
mov $0x00, %ah
int $0x16 # give the cpu another slap to clear the buffer ( TO DO: change to pasive pooling
jnz keyboardReader
jmp updatePart2
keyboardReader: # finally, we can get the keycode
cmp $'a', %al
je incrementPlayerX
cmp $'d', %al
je decrementPlayerX
cmp $' ', %al
je shoot
mov $0, playerMovementDirection
jmp updatePart2
shoot:
mov $0, %di
mov $1, %cx
shootLoop:
cmp $20, %di
je exitShootLoop
lea bulletsCoord, %bx #stard in the right momery point
#get the first byte
cmp $0, %cx
jge continueShootLoop
lea enemyBulletsCoord, %bx
continueShootLoop:
mov %di, %ax
mov $4, %si # jump in the good position
mul %si
add %ax, %bx
add $2, %bx
cmp $200, (%bx)
jge putHereEnemy # for enemy ammos
cmp $0, (%bx)
jle putHere
inc %di
jmp shootLoop
putHereEnemy:
push %bx
mov currentShootPos, %bx
mov (%bx), %bx
cmp $-1, %bx
jg putHereEnemyContinue
lea shootOrder, %bx
mov %bx, currentShootPos
mov (%bx), %bx
putHereEnemyContinue:
mov $2, %si
mov %bx, %ax
mov $0, %dx
mul %si
add $2, currentShootPos
lea enemiesCoord, %bx
add %ax, %bx
add %ax, %bx
add %ax, %bx
mov %bx, %si
mov (%bx), %bx
#mov %bx, currentScore
add $4, %si
mov (%si), %dx
sub $4, %si
#mov %dx, currentScore
cmp $0, %dx
jle enemyShootReturn
#sub $2, %bx
pop %bx
sub $2, %bx
push %bx
mov %si, %bx
mov (%bx), %ax
pop %bx
add $7, %ax # add half of the enemy size (10) - half of the bullet size (2.5 aprox 3)
mov %ax, (%bx)
add $2, %bx
push %bx
mov %si, %bx
add $2, %bx
mov (%bx), %ax
pop %bx
mov %ax, (%bx)
jmp enemyShootReturn
putHere:
sub $2, %bx
mov playerX, %ax
add $7, %ax # add half of the player size - half of the bullet size
mov %ax, (%bx)
add $2, %bx
mov playerY, %ax
mov %ax, (%bx)
jmp exitShootLoop
exitShootLoop:
jmp updatePart2
incrementPlayerX:
mov $1, playerMovementDirection
jmp updatePart2
decrementPlayerX:
mov $2, playerMovementDirection
jmp updatePart2
waitForFrame: # aproximate some kind of framerate (it will be changed to IRQ1 in a sunny day)
push %bp
mov %sp, %bp
mov $0, %ax
mov $0, %cx
mov $0x8235, %dx # aprox 30 fps
#mov $0x1046A, %dx # aprox 60 fps (too fast for ths small resolution pixel with screen)
mov $0x86, %ah
int $0x15 # not broken, don't fix it
mov %bp, %sp
pop %bp
ret
#___________________State_Management_And_Logic_Update_________________________
incrementFrameCounter:
push %bp
mov %sp, %bp
cmp $0, isBetweenStages
je incrementInGame # some sort of state machine
inc frameCounter
mov frameCounter, %ax
cmp $90, %ax # 3 second(ish)
jl skipBetweenStagesFrame
call clearWholeScreen
mov $0, isBetweenStages
mov $0, frameCounter
call startNextStage
skipBetweenStagesFrame:
jmp exitFrameCounter
incrementInGame: # game running
inc frameCounter
mov frameCounter, %ax
mov framesBeforeShoot, %si
cmp %si, %ax
je enemyDoSomething
continueIncrementInGame:
mov $3, %si
cmp $30, %ax # 30 frames per second => 1 second(ish) every 30 frame
je incrementScoreByTime
jmp exitFrameCounter
enemyDoSomething:
mov $0, frameCounter
call randomEnemyShoot
jmp continueIncrementInGame
exitFrameCounter:
mov %bp, %sp
pop %bp
ret
incrementScoreByTime:
inc currentScore
jmp exitFrameCounter
writeNumberOnScreen:
push %bp
mov %sp, %bp
mov %dx, %cx
push %di
updateScoreLoop:
dec %si
pop %ax
cmp $0, %ax
je finishUpdateScore
push %ax
push %cx
mov %si, %ax
mov %al, %dl # column number
mov %cl, %dh # row number
mov $0, %bh # select main page
mov $0x2, %ah # cursor position
int $0x10
pop %cx
pop %ax
mov $10, %di
mov $0, %dx
div %di
push %ax
push %cx
mov %dx, %ax
add $48, %al # convert in asci
mov $0x0C, %bl # color selection
mov $0, %bh # again page
mov $0x0E, %ah # teletype
int $0x10
pop %cx
jmp updateScoreLoop
finishUpdateScore:
mov %bp, %sp
pop %bp
ret
updatePlayer:
push %bp
mov %sp, %bp
mov playerMovementDirection, %ax
cmp $1, %ax
je moveLeft
cmp $2, %ax
je moveRight
updatePlayerFinish:
mov %bp, %sp
pop %bp
ret
moveLeft:
cmp $0, playerX
jle updatePlayerFinish # the memory on video card in "circular" so let's not teleport from one side of the screen to anothe
mov playerSpeed, %ax
sub %ax, playerX
jmp updatePlayerFinish
moveRight:
cmp $300, playerX
jge updatePlayerFinish
mov playerSpeed, %ax
add %ax, playerX
jmp updatePlayerFinish
#__________Update_Player_Bullets_Update_____________
updateBullet:
push %bp
mov %sp, %bp
mov $0, %di
updateBulletLoop:
cmp $20, %di
je endUpdateBullet
lea bulletsCoord, %bx
mov %di, %ax
mov $4, %si
mov $0, %dx
mul %si
add %ax, %bx # current bullet
add $2, %bx
cmp $0, (%bx)
jg updateThisBullet
nextBulletLoop:
inc %di
jmp updateBulletLoop
updateThisBullet:
sub $5, (%bx)
push %di
push %bx
mov %bx, %di
mov $6, %dx
call checkCollisions
cmp $-1, %ax
je bulletHit
bulletHitNext:
#add $4, %bx
#mov (%bx),%ax
#mov %ax, currentScore
pop %bx
pop %di
jmp nextBulletLoop
bulletHit:
add $2, %bx
cmp $0, (%bx)
jle bulletHitNext
dec (%bx)
mov $0, (%si)
mov (%bx), %ax
cmp $0, %ax
jg bulletHitNext
add $2, currentScore
dec deathEnemies
call drawEnemies
call tryNextStage
jmp bulletHitNext
endUpdateBullet:
mov %bp, %sp
pop %bp
ret
tryNextStage:
push %bp
mov %sp, %bp
mov deathEnemies, %ax
cmp $0, %ax
jg tryNextStageEnd
call initShootOrder2
mov $1, isBetweenStages
inc currentStage
mov $0, frameCounter
mov currentStage, %di
cmp $60, %di
je tryNextStage
mov $31, framesBeforeShoot
continueTryNextStage:
cmp $15, %di # cap fire rate at a minimum of 15
jle skipTheDecrement
dec framesBeforeShoot
skipTheDecrement:
mov $stageInfoText, %di
mov $13, %si
mov $10, %dx
call writeStringOnScreen
tryNextStageEnd:
mov %bp, %sp
pop %bp
ret
startNextStage:
push %bp
mov %sp, %bp
mov $12, currentNumberOfEnemies
mov currentNumberOfEnemies, %ax
mov %ax, deathEnemies #next stage
call initEnemiesLine
call drawEnemies
mov %bp, %sp
pop %bp
ret
#_____________End_Player_Bullets_Update____________
#____________Update_Enemy_Bullets__________________
updateEnemyBullet:
push %bp
mov %sp, %bp
mov $0, %di
updateEnemyBulletLoop:
cmp $20, %di
je endUpdateEnemyBullet
lea enemyBulletsCoord, %bx
mov %di, %ax
mov $4, %si
mov $0, %dx
mul %si
add %ax, %bx # current bullet
add $2, %bx
cmp $200, (%bx)
jl updateThisEnemyBullet
nextEnemyBulletLoop:
inc %di
jmp updateEnemyBulletLoop
updateThisEnemyBullet:
add $5, (%bx)
push %di
push %bx
mov %bx, %di
call enemyCheckCollisions
cmp $-1, %ax
je enemybulletHit
enemybulletHitNext:
pop %bx
pop %di
jmp nextEnemyBulletLoop
enemybulletHit:
mov $200, (%bx)
mov $-1, isAlive # oups, we've died
jmp enemybulletHitNext
endUpdateEnemyBullet:
mov %bp, %sp
pop %bp
ret
#___________End_Update_Enemy_Bullets_______________
#______________Sort_Top_Scores________________________
sortTopScores:
push %bp
mov %sp, %bp
mov $0, %di # first index
sortBigLoop:
mov topScoresNumber, %ax
cmp %ax, %di
jge sortTopScoresEnd
mov %di, %si
inc %si # from index1 +1 to topScores.....
jmp sortSmallLoop
sortSmallLoop:
mov topScoresNumber, %ax
cmp %ax, %si
jge sortSmallLoopEnd
push %di
push %si
lea topScores, %bx
add %di, %bx
add %di, %bx
mov (%bx), %di
mov $0, %bx
lea topScores, %bx
add %si, %bx
add %si, %bx
mov (%bx), %si
cmp %si, %di
jl switchTheNumbers
pop %si
pop %di
inc %si
jmp sortSmallLoop
switchTheNumbers:
mov %si, %ax
mov %di, %cx
pop %si
pop %di
lea topScores, %bx
add %si, %bx
add %si, %bx
mov %cx, (%bx)
lea topScores, %bx
add %di, %bx
add %di, %bx
mov %ax, (%bx)
inc %si
jmp sortSmallLoop
sortSmallLoopEnd:
inc %di
jmp sortBigLoop
sortTopScoresEnd:
mov %bp, %sp
pop %bp
ret
#______________End_Sort_Top_Scores_________________
#_____________End_State_Management_And_Logic_Update____________________________