-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.s
276 lines (235 loc) · 3.78 KB
/
main.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
.org $8000
.include 'ps2.s'
.include 'via.s'
.include 'lcd.s'
.include 'vdp.s'
.include 'tty.s'
.include 'monitor.s'
SNAKE_PAT = $7F
SNAKE_INIT_X = 20
SNAKE_INIT_Y = 10
HEAD = $50
HEAD_HI = $51
SNAKE_DIR = $52
SNAKE_DIR_HI = $53
DELAY = $54
RNG_SEED = $55
EMPTY = $56
EMPTY_HI = $57
DELAY_VAL = 10
JOY_UP = 1
JOY_DOWN = 2
JOY_LEFT = 4
JOY_RIGHT = 8
JOY_TRIG = 16
irq:
pha
lda VDP_REG ; read VDP status to clear 'F', the interrupt flag.
; Check the joystick
.check_right:
lda #JOY_RIGHT
and PORTA
bne .check_down
lda #1
sta SNAKE_DIR
stz SNAKE_DIR_HI
bra .wait_delay
.check_down:
lda #JOY_DOWN
and PORTA
bne .check_left
lda #40
sta SNAKE_DIR
stz SNAKE_DIR_HI
bra .wait_delay
.check_left:
lda #JOY_LEFT
and PORTA
bne .check_up
lda #-1
sta SNAKE_DIR
sta SNAKE_DIR_HI
bra .wait_delay
.check_up:
lda #JOY_UP
and PORTA
bne .wait_delay
lda #-40
sta SNAKE_DIR
lda #-1
sta SNAKE_DIR_HI
bra .wait_delay
.wait_delay
; Wait for delay
dec DELAY
bne .exit
lda #DELAY_VAL
sta DELAY
.move_snake:
; Move the snake in the SNAKE_DIR
; Low byte
clc
lda HEAD
adc SNAKE_DIR
sta HEAD
; High byte
lda HEAD_HI
adc SNAKE_DIR_HI
sta HEAD_HI
; Set up for a VRAM read to HEAD
lda HEAD
sta VDP_REG
lda HEAD_HI
sta VDP_REG
; Compare with ASCII space character
lda VDP_VRAM
cmp #' '
beq .continue
cmp #'*'
beq .eat_apple
jmp game_over
.eat_apple:
jsr rand_8
sta EMPTY
jsr rand_8
and #3
sta EMPTY_HI
cmp #3
bne .valid_vaddr
lda EMPTY
cmp #$C0
bcs .eat_apple ; try again
.valid_vaddr:
lda EMPTY
sta VDP_REG
lda EMPTY_HI
sta VDP_REG
lda VDP_VRAM
cmp #' '
bne .eat_apple
.empty_vaddr
lda EMPTY
sta VDP_REG
lda EMPTY_HI
ora #VDP_WRITE_VRAM_BIT
sta VDP_REG
lda #'*'
sta VDP_VRAM
; Set up for a VRAM write to HEAD
.continue:
lda HEAD
sta VDP_REG
lda HEAD_HI
ORA #VDP_WRITE_VRAM_BIT
sta VDP_REG
; Plop a new snake head into VRAM
lda #SNAKE_PAT
sta VDP_VRAM
.exit:
pla
rti
reset:
sei
; initialize stack pointer to $01FF
ldx #$ff
txs
jsr ps2_reset
jsr via_reset
jsr lcd_reset
jsr vdp_reset
jsr tty_reset
.clear_board:
vdp_write_vram VDP_NAME_TABLE_BASE
.top_border:
lda #3
sta VDP_VRAM
ldx #(VDP_COLS - 2)
lda #0
.top_border_horiz:
sta VDP_VRAM
dex
bne .top_border_horiz
lda #2
sta VDP_VRAM
ldy #(VDP_ROWS - 2)
.mid_board:
lda #1
sta VDP_VRAM
ldx #(VDP_COLS - 2)
lda #' '
.mid_board_horiz
sta VDP_VRAM
dex
bne .mid_board_horiz
lda #1
sta VDP_VRAM
dey
bne .mid_board
.bottom_border:
lda #5
sta VDP_VRAM
ldx #(VDP_COLS - 2)
lda #0
.bottom_border_horiz:
sta VDP_VRAM
dex
bne .bottom_border_horiz
lda #4
sta VDP_VRAM
.snake:
vdp_write_vram (VDP_NAME_TABLE_BASE + (5 * VDP_COLS) + 30)
lda #'*'
sta VDP_VRAM
vdp_write_vram (VDP_NAME_TABLE_BASE + (10 * VDP_COLS) + 20)
lda #SNAKE_PAT
sta VDP_VRAM
sta VDP_VRAM
; store the snake head's current location in HEAD (2 bytes)
lda #<(VDP_NAME_TABLE_BASE + (SNAKE_INIT_Y * VDP_COLS) + SNAKE_INIT_X + 1)
sta HEAD
lda #>(VDP_NAME_TABLE_BASE + (SNAKE_INIT_Y * VDP_COLS) + SNAKE_INIT_X + 1)
sta HEAD_HI
lda #1
sta SNAKE_DIR
stz SNAKE_DIR_HI
lda #DELAY_VAL
sta DELAY
lda #'H'
jsr putchar
.seed_rng:
lda RNG_SEED
bne .already_seeded
lda #42
sta RNG_SEED
.already_seeded:
cli
loop:
wai
bra loop
rand_8:
lda RNG_SEED
asl
bcc .no_eor
eor #$CF
.no_eor:
sta RNG_SEED
rts
game_over:
; Game over - write an X and wait for trigger, then restart
lda HEAD
sta VDP_REG
lda HEAD_HI
ORA #VDP_WRITE_VRAM_BIT
sta VDP_REG
lda #'X'
sta VDP_VRAM
.wait_for_trig:
lda #JOY_TRIG
and PORTA
bne .wait_for_trig
jmp reset
; Vector locations
.org $fffa
.word ps2_nmi
.word reset
.word irq