wok-tiny view bootbricks/stuff/bricks.asm @ rev 182

Update bootpillman & bootsokoban
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Sep 24 17:53:05 2023 +0000 (7 months ago)
parents e05af9592e8b
children 5d44015ce878
line source
1 ;
2 ; Bricks game in one boot sector
3 ;
4 ; by Oscar Toledo G.
5 ;
6 ; Creation date: Nov/02/2019.
7 ;
9 cpu 8086
11 ;
12 ; Press Left Shift to start the game
13 ; Press Left Ctrl to move the paddle to the left
14 ; Press Left Alt to move the paddle to the right
15 ;
17 old_time: equ 16 ; Old time
18 ball_x: equ 14 ; X-coordinate of ball (8.8 fraction)
19 ball_y: equ 12 ; Y-coordinate of ball (8.8 fraction)
20 ball_xs: equ 10 ; X-speed of ball (8.8 fraction)
21 ball_ys: equ 8 ; Y-speed of ball (8.8 fraction)
22 beep: equ 6 ; Frame count to turn off sound
23 bricks: equ 4 ; Remaining bricks
24 score: equ 2 ; Current score
25 balls: equ 0 ; Remaining balls
27 ;
28 ; Start of the game
29 ;
30 start:
31 mov ax,0xb800 ; Address of video screen
32 mov ds,ax ; Setup DS
33 mov es,ax ; Setup ES
34 sub sp,32
35 cbw
36 push ax ; Reset score
37 mov al,4
38 push ax ; Balls remaining
39 mov bp,sp ; Setup stack frame for globals
40 mov al,0x02 ; Text mode 80x25x16 colors
41 int 0x10 ; Setup
42 ;
43 ; Start another level
44 ;
45 another_level:
46 mov word [bp+bricks],273 ; 273 bricks on screen
47 xor di,di
48 mov ax,0x01b1 ; Draw top border
49 mov cx,80
50 cld
51 rep stosw
52 mov cl,24 ; 24 rows
53 .1:
54 stosw ; Draw left border
55 mov ax,0x20 ; No bricks on this row
56 push cx
57 cmp cl,23
58 jnb .2
59 sub cl,15
60 jbe .2
61 mov al,0xdb ; Bricks on this row
62 mov ah,cl
63 .2:
64 mov cl,39 ; 39 bricks per row
65 .3:
66 stosw
67 stosw
68 inc ah ; Increase attribute color
69 cmp ah,0x08
70 jne .4
71 mov ah,0x01
72 .4:
73 loop .3
74 pop cx
76 mov ax,0x01b1 ; Draw right border
77 stosw
78 loop .1
80 ;
81 ; Start another ball
82 ;
83 mov di,0x0f4a ; Position of paddle
84 another_ball:
85 mov byte [bp+ball_x+1],0x28 ; Center X
86 mov byte [bp+ball_y+1],0x14 ; Center Y
87 xor ax,ax
88 mov [bp+ball_xs],ax ; Static on screen
89 mov [bp+ball_ys],ax
90 mov byte [bp+beep],0x01
92 mov si,0x0ffe ; Don't erase ball yet
93 game_loop:
94 call wait_frame ; Wait 1/18.2 secs.
96 and word [si],0x0000 ; Erase ball
98 call update_score ; Update score
100 mov ah,0x01 ; Check for keys
101 int 0x16
102 jz .0
103 cbw ; Eat key
104 int 0x16
105 cmp al,27 ; ESC key ?
106 je exit
107 .0:
108 mov ah,0x02 ; Read modifier keys
109 int 0x16
110 test al,0x04 ; Left ctrl
111 je .1
112 mov byte [di+6],0 ; Erase right side of paddle
113 mov byte [di+8],0
114 sub di,byte 4 ; Move paddle to left
115 cmp di,0x0f02 ; Limit
116 ja .1
117 mov di,0x0f02
118 .1:
119 test al,0x08 ; Left alt
120 je .2
121 xor ax,ax ; Erase left side of paddle
122 stosw
123 stosw ; DI increased automatically
124 cmp di,0x0f94 ; Limit
125 jb .2
126 mov di,0x0f94
127 .2:
128 test al,0x02 ; Left shift
129 je .15
130 mov ax,[bp+ball_xs] ; Ball moving?
131 add ax,[bp+ball_ys]
132 jne .15 ; Yes, jump
133 ; Setup movement of ball
134 mov word [bp+ball_xs],0xff40
135 mov word [bp+ball_ys],0xff80
136 .15:
137 mov ax,0x0adf ; Paddle graphic and color
138 push di
139 stosw ; Draw paddle
140 stosw
141 stosw
142 stosw
143 stosw
144 pop di
146 mov bx,[bp+ball_x] ; Draw ball
147 mov ax,[bp+ball_y]
148 call locate_ball ; Locate on screen
149 test byte [bp+ball_y],0x80 ; Y-coordinate half fraction?
150 mov ah,0x60 ; Interchange colors for smooth mov.
151 je .12
152 mov ah,0x06
153 .12: mov al,0xdc ; Graphic
154 mov [bx],ax ; Draw
155 push bx
156 pop si
158 .14:
159 mov bx,[bp+ball_x] ; Ball position
160 mov ax,[bp+ball_y]
161 add bx,[bp+ball_xs] ; Add movement speed
162 add ax,[bp+ball_ys]
163 push ax
164 push bx
165 call locate_ball ; Locate on screen
166 mov al,[bx]
167 cmp al,0xb1 ; Touching borders
168 jne .3
169 mov cx,5423 ; 1193180 / 220
170 call speaker ; Generate sound
171 pop bx
172 pop ax
173 cmp bh,0x4f
174 je .8
175 test bh,bh
176 jne .7
177 .8:
178 neg word [bp+ball_xs] ; Negate X-speed if it touches a side
179 .7:
180 test ah,ah
181 jnz .9
182 neg word [bp+ball_ys] ; Negate Y-speed if it touches a side
183 .9: jmp .14
185 .3:
186 cmp al,0xdf ; Touching paddle
187 jne .4
188 sub bx,di ; Subtract paddle position
189 sub bx,byte 4
190 mov cl,6 ; Multiply by 64
191 shl bx,cl
192 mov [bp+ball_xs],bx ; New X speed for ball
193 mov word [bp+ball_ys],0xff80 ; Update Y speed for ball
194 mov cx,2711 ; 1193180 / 440
195 call speaker ; Generate sound
196 pop bx
197 pop ax
198 jmp .14
200 .4:
201 cmp al,0xdb ; Touching brick
202 jne .5
203 mov cx,1355 ; 1193180 / 880
204 call speaker ; Generate sound
205 test bl,2 ; Aligned with brick?
206 jne .10 ; Yes, jump
207 dec bx ; Align
208 dec bx
209 .10: xor ax,ax ; Erase brick
210 mov [bx],ax
211 mov [bx+2],ax
212 inc word [bp+score] ; Increase score
213 neg word [bp+ball_ys] ; Negate Y speed (rebound)
214 pop bx
215 pop ax
216 dec word [bp+bricks] ; One brick less on screen
217 jne .14 ; Fully completed? No, jump.
218 jmp another_level ; Start another level
220 .5:
221 pop bx
222 pop ax
223 .6:
224 mov [bp+ball_x],bx ; Update ball position
225 mov [bp+ball_y],ax
226 cmp ah,0x19 ; Ball exited through bottom?
227 je ball_lost ; Yes, jump
228 jmp game_loop ; No, repeat game loop
230 ;
231 ; Ball lost
232 ;
233 ball_lost:
234 mov cx,10846 ; 1193180 / 110
235 call speaker ; Generate sound
237 and word [si],0 ; Erase ball
238 dec byte [bp+balls] ; One ball less
239 js .1 ; All finished? Yes, jump
240 jmp another_ball ; Start another ball
242 .1: call wait_frame.2 ; Turn off sound
243 stc
244 exit:
245 pushf
246 mov ax,0x0003 ; Text mode 80x25x16 colors
247 int 0x10 ; Setup
248 int 0x20 ; Exit to DOS / bootOS
249 popf
250 jc .1
251 int 0x19
252 .1:
253 jmp start
255 wait_frame:
256 .0:
257 hlt ; Wait for clock interrupt
258 mov ah,0x00 ; Read ticks
259 int 0x1a ; Call BIOS
260 cmp dx,[bp+old_time] ; Wait for change
261 je .0
262 mov [bp+old_time],dx
264 dec byte [bp+beep] ; Decrease time to turn off beep
265 jne .1
266 .2:
267 in al,0x61
268 and al,0xfc ; Turn off
269 out 0x61,al
270 .1:
272 ret
274 ;
275 ; Generate sound on PC speaker
276 ;
277 speaker:
278 mov al,0xb6 ; Setup timer 2
279 out 0x43,al
280 mov al,cl ; Low byte of timer count
281 out 0x42,al
282 mov al,ch ; High byte of timer count
283 out 0x42,al
284 in al,0x61
285 or al,0x03 ; Connect PC speaker to timer 2
286 out 0x61,al
287 mov byte [bp+beep],3 ; Duration
288 ret
290 ;
291 ; Locate ball on screen
292 ;
293 locate_ball:
294 mov al,0xa0
295 mul ah ; AH = Y coordinate (row)
296 mov bl,bh ; BH = X coordinate (column)
297 mov bh,0
298 shl bx,1
299 add bx,ax
300 ret
302 ;
303 ; Update score indicator (from bootRogue)
304 ;
305 update_score:
306 mov bx,0x0f98 ; Point to bottom right corner
307 mov ax,[bp+score]
308 call .1
309 mov al,[bp+balls]
310 .1:
311 xor cx,cx ; CX = Quotient
312 .2: inc cx
313 sub ax,10 ; Division by subtraction
314 jnc .2
315 add ax,0x0a3a ; Convert remainder to ASCII digit + color
316 call .3 ; Put on screen
317 xchg ax,cx
318 dec ax ; Quotient is zero?
319 jnz .1 ; No, jump to show more digits.
321 .3: mov [bx],ax
322 dec bx
323 dec bx
324 ret
326 times 510-($-$$) db 0x4f
327 db 0x55,0xaa ; Make it a bootable sector