-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
219 lines (172 loc) · 4.14 KB
/
main.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
main:
call setVideoBufferSeg
cmp si, 0
jz @f
call print
ret
@@:
finit
mov ax, 0x13
int 0x10
call loadPalette
call initializeTimer
call initializeBall
call newTrajectory
mov al, 1
call unpackLevel
mov ax, word [bufferB] ; clear buffer B
mov es, ax
call clearBuffer
call drawBorder ; draw background elements
mov cx, 10000
mov word [paddle.x], 70
mov word [paddle.y], 20
postpone {
paddleDirection db 1
}
;jmp @f
.mainLoop:
push cs ; DS = CS
pop ds
push cx
push cx
mov cx, 1000
.pause:
;call tickWait
loop .pause
pop cx
.draw:
mov ax, word [bufferA] ; draw buffer B to buffer A
mov es, ax
mov ax, word [bufferB]
mov ds, ax
call drawBuffer
push cs ; DS = CS
pop ds
call drawLevel
mov ax, word [paddle.x]
mov bx, word [paddle.y]
call drawPaddle
mov ax, word [ball.x]
mov bx, word [ball.y]
call drawBall
mov ax, 0xA000 ; draw buffer A to screen
mov es, ax
mov ax, word [bufferA]
mov ds, ax
call drawBuffer
.update:
push cs
pop ds
call checkBallCollision
call ricochet
call nextPosition
call checkPaddleCollision
cmp ax, pside.LEFT
jne @f
mov byte [paddleDirection], 1
jmp .paddleCollisionEnd
@@:
cmp ax, pside.RIGHT
jne @f
mov byte [paddleDirection], -1
@@:
.paddleCollisionEnd:
mov al, byte [paddleDirection]
cbw
add word [paddle.x], ax
pop cx
;loop .mainLoop ; jmp out of range
loop .mainLoop.again
jmp .mainLoop.terminate
.mainLoop.again:
jmp .mainLoop
.mainLoop.terminate:
@@:
xor ax, ax
int 0x16
mov ax, 0x03
int 0x10
jmp cleanup
cleanup:
ret
initializeBall:
mov dword [ball.angle], 1.37 ; (pi)7/16
mov dword [ball.slope], 3.0
mov word [ball.x], 160
mov word [ball.y], 15
mov word [ball.xOrigin], 160
mov word [ball.yOrigin], 15
mov word [ball.direction], 11b
ret
; void initializeTimer(void)
; Set speaker timer to divide 1.19 MHz by 16384, giving a ~14 ms period.
initializeTimer:
push ax
cli
mov al, 0xB6
out 0x43, al
mov ax, 16384
out 0x42, al
mov al, ah
out 0x42, al
sti
pop ax
ret
; void tickWait(void)
; Busy-wait for 14 ms.
tickWait:
pusha
macro readCount {
cli ; disable interrupts
mov al, 10000000b ; read back count from speaker timer
out 0x43, al
in al, 0x40 ; load low byte of count
mov ah, al
in al, 0x40 ; load high byte of count
rol ax, 8
sti ; enable interrupts
}
readCount
mov bx, ax
.spinwait:
readCount
cmp ax, bx
je .spinwait
popa
ret
; void printBinary(byte n)
; al = n
printBinary:
pusha
mov bl, al
mov ah, 0x0E
mov cx, 8
.nextBit:
shl bl, 1
jc .one
.zero:
mov al, '0'
jmp .common
.one:
mov al, '1'
.common:
int 0x10
loop .nextBit
popa
ret
; void print(char* <si> str)
print:
push ax
push si
mov ah, 0x0E
.nextChar:
lodsb
cmp al, 0
jz .end
int 0x10
jmp .nextChar
.end:
pop si
pop ax
ret