forked from shikhin/tetranglix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tetranglix.asm
494 lines (378 loc) · 10.9 KB
/
tetranglix.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
; Modified by nanochess for compatibility with VirtualBox,
; to require only 286 and also now it's in color.
; 16 bits, starting at 0x7C00.
BITS 16
ORG 0x7C00
BSS EQU 0x0504 ; The byte at 0x0500 is also used, so align on next dword bound.
BSS_SIZE EQU 438
CUR_TETRAMINO EQU BSS ; 16 bytes.
ROT_TETRAMINO EQU BSS + 16 ; 16 bytes.
OFFSET EQU BSS + 32 ; 2 bytes.
STACK EQU BSS + 38 ; 4 bytes reserved in beginning, 400 bytes.
LEFT_SCANCODE EQU 0x4b
RIGHT_SCANCODE EQU 0x4d
UP_SCANCODE EQU 0x48
DOWN_SCANCODE EQU 0x50
SCORE_DIGITS EQU 5
CPU 286
; Entry point.
; cs:ip -> linear address (usually 0x7C00, but irrelevant because we are position independent).
start:
; Set up segments.
xor ax, ax
; Stack.
mov ss, ax
mov sp, 0xB800 ;why not
mov ds, ax
mov es, ax
; Clear direction flag.
cld
; Clear BSS
mov di, BSS
mov cx, di ;at least BSS_SIZE
rep stosb
; Set to mode 0x03, or 80x25 text mode (ah is zero from above).
mov al, 0x03
int 0x10
; Hide the hardware cursor.
mov ch, 0x26
mov ax, 0x103 ; Some BIOS crash without the 03.
int 0x10
mov es, sp
mov fs, sp
; White spaces on black background.
xor di, di
mov ax, 0x0F00
mov cx, ax ; At least 80x25x2.
rep stosw
call pop_check
; Detects if CUR_TETRAMINO at OFFSET is colliding with any thing.
; si -> OFFSET.
; Output:
; Carry set if colliding.
tetramino_collision_check:
lea bx, [bp + check_collision - tetramino_collision_check]
; Processes the current tetramino, calling bx per "tetramino pixel".
; bx -> where to call to; al contains tetramino pixel, di the address into stack.
tetramino_process:
pusha
; Gets the offset into stack (i.e., address) into di.
; si -> points at OFFSET.
; Output:
; si -> points at CUR_TETRAMINO.
; di -> address into stack.
; Trashes ax.
; Calculate first index into screen.
lodsw
aad 0x10
cmp byte [si-1], 0x10
sbb ah, ah
xchg bx, ax
lea di, [si + (STACK - OFFSET) + 0xFE + bx]
xchg bx, ax
mov si, CUR_TETRAMINO
mov cl, 0x10
.loop:
test cl, 0x13;0b1011
jnz .load_loop
; Go to next line in stack.
add di, 16 - 4
.load_loop:
lodsb
; Call wherever the caller wants us to go.
call bx
inc di
loop .loop
popa
ret
check_collision:
or al,al
jz .clear_carry
cmp di, STACK + 400
jae .colliding
cmp byte [di],0
.clear_carry:
clc
je .next_iter
; Colliding!
.colliding:
stc
mov cl, 1
.next_iter:
ret
; Used by the stack joining part.
merge:
or [di], al
ret
; All tetraminos in bitmap format.
tetraminos:
db 0xF0;0b11110000 ; I
db 0xE2;0b11100010 ; J
db 0x2E;0b00101110 ; L
db 0x66;0b01100110 ; O
db 0x36;0b00110110 ; S
db 0xE4;0b11100100 ; T
db 0x63;0b01100011 ; Z
pop_check:
pop bp ; Save some bytes.
.borders:
mov si, STACK - 3
mov ax, 0x0101
.borders_init:
mov [si], ax
mov [si + 2], ax
mov [si + 4], ax
add si, 16
cmp si, STACK + 400 - 3
jbe .borders_init
; Cleared dl implies "load new tetramino".
xor dl, dl
.event_loop:
mov si, OFFSET
; For some reason this doesn't work with BootOS over VirtualBox 5.1.22
%if 0
mov bx, [0x046C]
inc bx
inc bx ; Wait for 2 PIT ticks.
.busy_loop:
cmp [0x046C], bx
jne .busy_loop
%else
push dx
.busy_loop1:
mov ah,0x00
int 0x1a
cmp [0x1000],dx
je .busy_loop1
mov [0x1000],dx
.busy_loop2:
mov ah,0x00
int 0x1a
cmp [0x1000],dx
je .busy_loop2
mov [0x1000],dx
pop dx
xor cx,cx ; Or rotation doesn't work
%endif
; If we don't need to load a new tetramino, yayy!
test dl, dl
jnz .input
; Load a tetramino to CUR_TETRAMINO, from the compressed bitmap format.
.choose_tetramino:
in al,(0x40)
; Only 7 tetraminos, index as 1-7.
and ax, 7
je .choose_tetramino
; Get the address of the tetramino (in bitmap format).
cwd
mov di,ax
mov ah,al
; Load tetramino bitmap in dl.
mov dl, [cs:bp + di + (tetraminos - tetramino_collision_check) - 1]
shl dx, 4
; Convert from bitmap to array.
mov di, CUR_TETRAMINO
mov cl, 0x10
.loop_bitmap:
shl dx, 1
; If the bit we just shifted off was set, store number of tetramino.
sbb al, al
and al, ah
mov [di], al
inc di
loop .loop_bitmap
; Loaded.
mov dl, 6
mov word [si], dx
jmp .link_next_iter
; Check for input.
.input:
; Check for keystroke.
mov ah, 0x01
int 0x16
; If no keystroke, increment vertical offset.
jz .vertical_increment
; Clear the keyboard buffer.
xor ah, ah
int 0x16
.exit:
cmp ah, 0x01
jne .left
mov ax,0x0002 ; Clear screen
int 0x10
int 0x20 ; Return to bootOS
; Go left.
.left:
cmp ah, LEFT_SCANCODE
jne .right
dec byte [si]
jmp .call_bp
; Go right.
.right:
cmp ah, RIGHT_SCANCODE
jne .rotate
inc byte [si]
.call_bp:
xor ah, LEFT_SCANCODE ^ RIGHT_SCANCODE
call bp
jc .left
; Rotate it.
.rotate:
cmp ah, UP_SCANCODE
jne .vertical_increment
inc cx
.rotate_loop:
; Rotates CUR_TETRAMINO 90 degrees clock-wise.
; Output:
; CUR_TETRAMINO -> rotated tetramino.
pusha
push es
; Reset ES.
push ds
pop es
mov si, CUR_TETRAMINO
mov di, ROT_TETRAMINO + 3
push si
mov cl, 4
.loop:
mov ch, 4
.tetramino_line:
movsb
scasw
inc di
dec ch
jnz .tetramino_line
sub di, 4*4+1
loop .loop
pop di
mov cl, 4*4/2 ; CH would be zero, from above.
rep movsw
pop es
popa
loop .rotate_loop
call bp
; To restore, just rotate 3 more times.
mov cl, 3
jc .rotate_loop
.vertical_increment:
mov cl, 1
call upd_score
; Check if we can go below one byte, successfully.
inc byte [si + 1]
call bp
.link_next_iter:
jnc .next_iter
; If we can't, we need a new tetramino.
dec byte [si + 1]
je $ ; Game Over
cwd
; Joins the current tetramino to the stack, and any complete lines together.
; si -> OFFSET.
push es
push ds
pop es
lea bx, [bp + merge - tetramino_collision_check]
call tetramino_process
mov si, STACK + 15
std
.loop_lines:
push si
mov cl, 16
.line:
lodsb
test al, al
loopnz .line ; If it was a blank, exit loop to indicate failure.
jz .next_line
lea cx, [si - (STACK - 1)]
lea di, [si + 16]
rep movsb
mov cl, 64
call upd_score
.next_line:
pop si
add si, 16
cmp si, STACK + 15 + 400
jb .loop_lines
cld
pop es
jmp .borders
.next_iter:
; Display the stack.
push si
; Add 24 characters padding in the front.
mov ah, 0x0F
mov di, 48
mov si, STACK
.loop_stack_lines:
; Copy 32 characters.
mov cl, 16
.stack_line:
lodsb
mov ah,al
mov al,0xdb
; Store one character as two -- to make stack "squarish" on 80x25 display.
stosw
stosw
loop .stack_line
; Handle remaining 24 characters in row, and starting 24 in next row.
add di, 96
cmp di, (25 * 160) ; If we go beyond the last row, we're over.
jb .loop_stack_lines
pop si
; Displays CUR_TETRAMINO at current OFFSET.
; si -> OFFSET.
; Calculate first index into screen.
mov bx, [si]
mov al, 40
mul bh
mov cl, 12
add cl, bl
add ax, cx
; One character takes 2 bytes in video memory.
shl ax, 2
xchg di, ax
; Loops for 16 input characters.
mov cl, 0x10
mov si, CUR_TETRAMINO
mov ah, 0x0F
.loop_tetramino:
test cl, 0x13;0b1011
jnz .load_tetramino
; Since each tetramino input is 4x4, we must go to next line
; at every multiple of 4.
; Since we output 2 characters for one input char, cover offset of 8.
add di, (80 - 8) * 2
.load_tetramino:
lodsb
or al,al
mov ah,al
mov al,0xdb
; Output two characters for "squarish" output.
jne .load_tetramino2
mov ax, [es:di]
.load_tetramino2:
stosw
stosw
loop .loop_tetramino
jmp .event_loop
upd_score:
mov bx, SCORE_DIGITS * 2
.chk_score:
dec bx
dec bx
js $
mov al, '0'
xchg [fs:bx], al
or al, 0x30
cmp al, '9'
je .chk_score
inc ax
mov [fs:bx], al
loop upd_score
ret
; IT'S A SECRET TO EVERYBODY.
db "ShNoXgSo"
; Padding.
times 510 - ($ - $$) db 0
BIOS_signature:
dw 0xAA55