forked from wiebow/tetris.c64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.asm
571 lines (479 loc) · 14 KB
/
blocks.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
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
// code concerning blocks
.const screenPointer = $fb // zero page pointer to a screen memory position
.const screenPointer2 = $fd // 2nd pointer to move data
// sets screen memory pointer to x and y column
// set X and Y register before calling this routine.
SetScreenPointer:
stx screenPointer // set low byte. use x immediately...
lda #4
sta screenPointer+1 // set hi byte
cpy #0 // at top of the screen?
beq !exit+ // then no change is needed
txa // get the current low byte of screen pointer
!loop:
clc
adc #40 // add a row (screen is 40 chars wide)
bcc !skip+ // no page boundery passed? then skip next instruction
inc screenPointer+1 // page boundery passed, increment screen memory hi byte
!skip:
dey // decrement the y count
bne !loop- // do next row if more needed
sta screenPointer // store the screen memory low byte
!exit:
rts
// this subroutine adjusts the screenPointer pointer so it
// points to the row exactly below it.
DownOneRow:
lda screenPointer // add 40 to the screen memory pointer
clc
adc #40
bcc !skip+ // skip next instruction if page boundery was not passed
inc screenPointer+1 // inc hi byte of the screen address
!skip:
sta screenPointer // store new lo byte
rts
// this subroutine adjusts the screenPointer pointer so it
// points to the row exactly above it.
UpOneRow:
lda screenPointer // dec 40 from the screen memory pointer
sec
sbc #40
bcs !skip+ // skip next instruction if page boundery was not passed
dec screenPointer+1 // dec hi byte of the screen address
!skip:
sta screenPointer // store new lo byte
rts
// prints a block on the screen
// x and y position but be set for the use of SetScreenPosition ...
// and SelectBlock must have been called before calling this subroutine
PrintBlock:
ldx blockXposition // print to the correct place on screen
ldy blockYposition
jsr SetScreenPointer
// get pointer to the start of block data
ldx currentFrame // this has been set by calling SelectBlock or AnimateBlock
lda frameArrayLo,x // get the lo byte
sta printLoop+1 // store in lda instruction
lda frameArrayHi,x // same for hi byte
sta printLoop+2 // and store
// print the block
ldx #$00 // reset the block data counter
ldy #$00 // reset the print counter
printLoop:
lda $1010,x // get block data. the adress is modified at the start of this subroutine
cmp #$20 // is it a space?
beq !skip+ // then skip printing it
sta (screenPointer),y // put it on the screen
!skip:
inx // inc the block data pointer
cpx #16 // done 16 characters? (4x4)
bne !skip+ // continue printing if not
rts
!skip:
iny // inc the print counter
cpy #$04 // each block is 4 characters wide, done for this row?
bne printLoop // continue this row
jsr DownOneRow // go down one row
ldy #$00 // reset the counter for a new row
jmp printLoop // do the next row
// Checks if there is space for a block to be printed.
// Set the position registers before calling this routine.
// A register is set according to outcome: 0 = no problem, 1 = no space
CheckBlockSpace:
ldx blockXposition
ldy blockYposition
jsr SetScreenPointer
// first, get pointer to the start of block data
ldx currentFrame
lda frameArrayLo,x // get the lo byte
sta spaceLoop+1 // store in lda instruction
lda frameArrayHi,x // same for hi byte
sta spaceLoop+2 // and store
// check the space
ldx #$00 // reset the block data counter
ldy #$00 // reset the print counter
spaceLoop:
lda $1010,x // get block data.
cmp #$20 // is it a space?
beq !skip+ // then skip the check it
// check the position where data must be printed
lda (screenPointer),y // load the data on this position
cmp #$20 // is it a space?
beq !skip+ // yes. no problem. continue check
lda #$01 // no space for block. set flag
rts
!skip:
inx // inc the block data pointer
cpx #16 // done 16 characters? (4x4)
bne !skip+ // continue printing if not
lda #$00 // all locations checked. done. clear flag
rts
!skip:
iny
cpy #$04
bne spaceLoop
jsr DownOneRow
ldy #$00
jmp spaceLoop
// erases a block on the screen
// same as PrintBlock but outputting spaces
EraseBlock:
ldx blockXposition
ldy blockYposition
jsr SetScreenPointer
// first, get pointer to the start of block data
ldx currentFrame // this has been set by calling SelectBlock or AnimateBlock
lda frameArrayLo,x // get the lo byte
sta eraseLoop+1 // store in lda instruction
lda frameArrayHi,x // same for hi byte
sta eraseLoop+2 // and store
// erase the block
ldx #$00 // reset the block data counter
ldy #$00 // reset the columns counter
eraseLoop:
lda $1010,x // get block data. the adress is modified at the start of this subroutine
cmp #$20 // is it a space?
beq !skip+ // then skip erasing it.
lda #$20 // use a space
sta (screenPointer),y // and erase this block character.
!skip:
inx // inc the block data pointer
cpx #16 // done 16 characters? (4x4)
bne !skip+ // continue printing if not
rts // done!
!skip:
iny // inc the columns counter
cpy #$04 // each block is 4 columns wide, done for this row?
bne eraseLoop // continue this row
jsr DownOneRow // go down one row
ldy #$00 // reset the counter for a new row
jmp eraseLoop // do the next row
// this subroutine will select a block.
// set .A register with block id before calling this subroutine
SelectBlock:
sta currentBlockID // store the block id
tax
lda blockFrameStart,x // get begin frame number for this block
sta currentFrame // and store it for display
sta firstFrame // and for AnimateBlock routine
lda blockFrameEnd,x // get last frame number for this block
sta lastFrame // and store it for AnimateBlock routine
rts
// this subroutine will advance the block animation forward or backwards...
// depending on the value of the A register. Set that before calling this subroutine.
// 0 = forward, clockwise
// 1 = backward, counter clockwise
// Also, SelectBlock must have been called so the animation settings are correct.
AnimateBlock:
cmp #1 // see if we need to move the animation
beq doBackward // forward or backward
doForward:
lda currentFrame // get the current frame number
cmp lastFrame // already done the last frame?
beq !skip+ // yes. go set to first frame
inc currentFrame // no. go one frame forward
rts // done!
!skip:
lda firstFrame // reset the frame
sta currentFrame // to the first frame
rts // done!
doBackward:
lda currentFrame // get the current frame.
cmp firstFrame // already at the first frame?
beq !skip+ // then reset to last frame
dec currentFrame // no. go back one frame
rts // done!
!skip:
lda lastFrame // reset the animation to
sta currentFrame // the last frame.
rts // done!
// this subroutine updates the block fall timer...
// and drops the block a row when needed.
// A register holds: 0: nothing happened ...
// 1: block fell, 2:block fell, new block needed.
DropBlock:
dec fallDelayTimer // update the delay timer
beq !skip+ // drop the block if 0 is reached
lda #$00 // return 0 = nothing happened
rts
!skip:
lda fallDelay // reset the block fall delay
sta fallDelayTimer
// drop the block
jsr EraseBlock // erase from screen
inc blockYposition // move 1 row down
jsr CheckBlockSpace // will it fit?
bne !skip+ // A is set to 1, so no.
jsr PrintBlock // yes. print it
lda #$01 // status is block fell
rts
!skip:
dec blockYposition // no. move back.
jsr PrintBlock // print it
lda #$02 // new block needed
rts
// selects a new random block
// it will take the id from the nextBlockID address.
// the next block will be again determined, and printed on the screen
// register A holds: 0 if all went well, 1 if new block overlaps screen data (game over!)
NewBlock:
// reset the block fall delay
lda fallDelay
sta fallDelayTimer
// nextBlockID was printed as the next block.
// set location to remove from screen
ldx #25 // erase block from 25,15
ldy #15
stx blockXposition // save the position
sty blockYposition
jsr SetScreenPointer
// select and save the ID
// because we will create the block later on
lda nextBlockID
pha
jsr SelectBlock // select it
jsr EraseBlock // remove it
// get the id of the NEW block
// and print it on the bottomright of the screen
jsr GetRandom
sta nextBlockID // save next block id
jsr SetScreenPointer // restore screenpointer to 25,15
// as set previously
lda nextBlockID
jsr SelectBlock
jsr PrintBlock // print it.
// done.
// create the new player block
ldx #15 // put new block on 15,0
ldy #00
stx blockXposition // save the position
sty blockYposition
jsr SetScreenPointer
pla // restore the player block id
sta currentBlockID // put in current id address
jsr SelectBlock // select it
jsr CheckBlockSpace // will it fit?
bne !skip+ // A is set to 1, so no
jsr PrintBlock // print it.
lda #$00 // notify all is well.
rts
!skip:
jsr PrintBlock // print it
lda #$01 // notify that it doesnt fit!!!
rts
// -------------------------------------------------
BlockLeft:
jsr EraseBlock // remove block on this position
dec blockXposition // alter block position
jsr CheckBlockSpace // will it fit?
beq !skip+ // yes. print it
inc blockXposition // no. move it back
!skip:
jsr PrintBlock
rts
BlockRight:
jsr EraseBlock
inc blockXposition
jsr CheckBlockSpace
beq !skip+
dec blockXposition
!skip:
jsr PrintBlock
rts
//player forces block down
BlockDown:
jsr EraseBlock
inc blockYposition
jsr CheckBlockSpace
beq !skip+
// block doesn't fit
dec blockYposition
jsr PrintBlock
lda #1 // we made block drop
sta fallDelayTimer // so create new one without delay
rts
!skip:
jsr PrintBlock
// moving the block down gives points
lda #1
sta addition+2
lda #0
sta addition+1
sta addition
jsr AddScore
jsr PrintScore
rts
BlockRotateCCW:
jsr EraseBlock // remove block on this position
lda #$01 // yes. 1 means counter clock wise
jsr AnimateBlock // rotate it
jsr CheckBlockSpace // will it fit?
beq !skip+ // yes, print it
lda #$00 // no
jsr AnimateBlock // turn it back
!skip:
jsr PrintBlock
rts
BlockRotateCW:
jsr EraseBlock
lda #$00
jsr AnimateBlock
jsr CheckBlockSpace
beq !skip+
lda #$01
jsr AnimateBlock
!skip:
jsr PrintBlock
rts
// ---------------------------------------------------------------------------------------------
// registers to store information in
blockXposition:
.byte 0 // current player block x position
blockYposition:
.byte 0 // current player block y position
currentBlockID:
.byte 0 // current block ID
nextBlockID:
.byte 0 // this is the next block to fall
currentFrame:
.byte 0 // frame of current block
firstFrame:
.byte 0 // first animation frame for current block
lastFrame:
.byte 0 // last animation frame for current block
fallDelay:
.byte 0 // delay between block drops for this level
fallDelayTimer:
.byte 0 // current timer for delay
// ---------------------------------------------------------------------------------------------
// arrays of block start and end animation frames.
// example: block 0 animation starts at frame 0 and ends at frame 3
// 0 1 2 3 4 5 6
blockFrameStart:
.byte 0,4, 8,12,14,16,18
blockFrameEnd:
.byte 3,7,11,13,15,17,18
// these lo and hi byte pointers refer to the block data adress values
frameArrayLo:
.byte <frame00, <frame01, <frame02, <frame03 // block 0
.byte <frame04, <frame05, <frame06, <frame07 // block 1
.byte <frame08, <frame09, <frame10, <frame11 // block 2
.byte <frame12, <frame13 // block 3
.byte <frame14, <frame15 // block 4
.byte <frame16, <frame17 // block 5
.byte <frame18 // block 6
frameArrayHi:
.byte >frame00, >frame01, >frame02, >frame03 // block 0
.byte >frame04, >frame05, >frame06, >frame07 // block 1
.byte >frame08, >frame09, >frame10, >frame11 // block 2
.byte >frame12, >frame13 // block 3
.byte >frame14, >frame15 // block 4
.byte >frame16, >frame17 // block 5
.byte >frame18 // block 6
// block0, 4 frames
frame00:
.text " II "
.text " I "
.text " I "
.text " "
frame01:
.text " I"
.text " III"
.text " "
.text " "
frame02:
.text " I "
.text " I "
.text " II "
.text " "
frame03:
.text " "
.text " III"
.text " I "
.text " "
// block1, 4 frames
frame04:
.text " G "
.text " GG "
.text " G "
.text " "
frame05:
.text " G "
.text " GGG"
.text " "
.text " "
frame06:
.text " G "
.text " GG"
.text " G "
.text " "
frame07:
.text " "
.text " GGG"
.text " G "
.text " "
// block2, 4 frames
frame08:
.text " HH "
.text " H "
.text " H "
.text " "
frame09:
.text " "
.text "HHH "
.text " H "
.text " "
frame10:
.text " H "
.text " H "
.text "HH "
.text " "
frame11:
.text "H "
.text "HHH "
.text " "
.text " "
// block3, 2 frames
frame12:
.text " X "
.text " XX "
.text " X "
.text " "
frame13:
.text " XX "
.text "XX "
.text " "
.text " "
// block4, 2 frames
frame14:
.text " H "
.text " HH "
.text " H "
.text " "
frame15:
.text "HH "
.text " HH "
.text " "
.text " "
//block5, 2 frames
frame16:
.byte 32,92,32,32
.byte 32,93,32,32
.byte 32,93,32,32
.byte 32,94,32,32
// .text " K "
// .text " K "
// .text " K "
// .text " K "
frame17:
.text " "
.byte 89,90,90,91
// .text "YZZK"
.text " "
.text " "
// block6, 1 frame
frame18:
.text " "
.text " JJ "
.text " JJ "
.text " "