-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdesolcoda.asm
573 lines (533 loc) · 14.7 KB
/
desolcoda.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
572
573
CHEAT_SHOW_ROOM_NUMBER EQU 0
CHEAT_ALL_ACCESS EQU 0
CHEAT_ALL_INVENTORY EQU 0
CHEAT_HAVE_WEAPON EQU 0
CHEAT_HEALTH_999 EQU 0
ORG $5FB4 ; = 24500
start:
call ClearShadowScreen
call LBA07 ; Show titles and go to Menu
; Cheat code to get all door access codes
IF CHEAT_ALL_ACCESS = 1
LD HL,LDCA2
LD B,$48
start_1:
LD (HL),$01
INC HL
DJNZ start_1
ENDIF
; Cheat code to have all inventory items
IF CHEAT_ALL_INVENTORY = 1
LD HL,LDB9C
LD B,26
start_2:
LD (HL),$01
INC HL
DJNZ start_2
ENDIF
; Cheat code to have the weapon
IF CHEAT_HAVE_WEAPON = 1
ld a,$01
ld (LDCF7),a
ENDIF
IF CHEAT_HEALTH_999 = 1
ld hl,999
ld (LDB7A),hl
ENDIF
; call LB0A2 ; Inventory
; call LBBEC ; Info menu item, show Controls
; call LBADE ; New game
; call LBB7E ; Game start
; call LB9A2 ; Player is dead
; call LBD85 ; Final
; call LBF6F ; The End
; call ShowShadowScreen
; call WaitAnyKey
; call ClearShadowScreen
; call ShowShadowScreen
jp start
;----------------------------------------------------------------------------
DesolateStrsBeg:
INCLUDE "desolstrs.asm"
DesolateFontBeg:
INCLUDE "desolfont.asm"
DesolateDataBeg:
INCLUDE "desoldata.asm"
;----------------------------------------------------------------------------
DesolateCodeBeg:
ROM_BEEPER EQU $03B5 ; hl=pitch de=duration
; Sound for "Look" or "Shoot" action
SoundLookShoot:
ld hl,$0190
ld de,$0004
jp ROM_BEEPER
; Wait for any key
WaitAnyKey:
call ReadKeyboard
or a
jr nz,WaitAnyKey ; Wait for unpress
WaitAnyKey_1:
call ReadKeyboard
or a
jr z,WaitAnyKey_1 ; Wait for press
ret
; Wait until no key pressed - to put after ReadKeyboard calls to prevent double-reads of the same key
WaitKeyUp:
call ReadKeyboard
or a
jr nz,WaitKeyUp ; Wait for unpress
ret
; Source: http://www.breakintoprogram.co.uk/computers/zx-spectrum/keyboard
; Returns: A=key code, $00 no key; Z=0 for key, Z=1 for no key
; Key codes: Down=$01, Left=$02, Right=$03, Up=$04, Look/shoot=$05
; Inventory=$06, Escape=$07, Switch look/shoot=$08, Enter=$09, Menu=$0F
ReadKeyboard:
LD HL,ReadKeyboard_map ; Point HL at the keyboard list
LD D,8 ; This is the number of ports (rows) to check
LD C,$FE ; C is always FEh for reading keyboard ports
ReadKeyboard_0:
LD B,(HL) ; Get the keyboard port address from table
INC HL ; Increment to list of keys
IN A,(C) ; Read the row of keys in
AND $1F ; We are only interested in the first five bits
LD E,5 ; This is the number of keys in the row
ReadKeyboard_1:
SRL A ; Shift A right; bit 0 sets carry bit
JR NC,ReadKeyboard_2 ; If the bit is 0, we've found our key
INC HL ; Go to next table address
DEC E ; Decrement key loop counter
JR NZ,ReadKeyboard_1 ; Loop around until this row finished
DEC D ; Decrement row loop counter
JR NZ,ReadKeyboard_0 ; Loop around until we are done
xor a ; Clear A (no key found)
RET
ReadKeyboard_2:
LD A,(HL) ; We've found a key at this point; fetch the character code!
or a
RET
; Mapping:
; QAOP/1234/6789 - arrows, Space/B/M/N/Z/0/5 - look/shoot
; S/D - switch look/shoot, W/E - escape, U/I - inventory; G - menu, Enter=Enter
ReadKeyboard_map:
DB $FE, $00,$05,$00,$00,$00 ; Shift,"Z","X","C","V"
DB $FD, $01,$08,$08,$00,$0F ; "A","S","D","F","G"
DB $FB, $04,$07,$07,$00,$00 ; "Q","W","E","R","T"
DB $F7, $02,$03,$01,$04,$05 ; "1","2","3","4","5"
DB $EF, $06,$04,$01,$03,$02 ; "0","9","8","7","6"
DB $DF, $03,$02,$06,$06,$00 ; "P","O","I","U","Y"
DB $BF, $09,$00,$00,$00,$00 ; Enter,"L","K","J","H"
DB $7F, $05,$00,$05,$05,$05 ; Space,Sym,"M","N","B"
; ZX screen address list used to copy shadow screen lines on the ZX screen
ScreenAddrs:
DW $40C4,$42C4,$44C4,$46C4,$40E4,$42E4,$44E4,$46E4
DW $4804,$4A04,$4C04,$4E04,$4824,$4A24,$4C24,$4E24
DW $4844,$4A44,$4C44,$4E44,$4864,$4A64,$4C64,$4E64
DW $4884,$4A84,$4C84,$4E84,$48A4,$4AA4,$4CA4,$4EA4
DW $48C4,$4AC4,$4CC4,$4EC4,$48E4,$4AE4,$4CE4,$4EE4
DW $5004,$5204,$5404,$5604,$5024,$5224,$5424,$5624
DW $5044,$5244,$5444,$5644,$5064,$5264,$5464,$5664
DW $5084,$5284,$5484,$5684,$50A4,$52A4,$54A4,$56A4
; Compare HL and DE
CpHLDE:
push hl
or a
sbc hl,de
pop hl
ret
; Get shadow screen address using penCol in L86D7
; A = row 0..137
; (L86D7) = penCol 0..191
; Returns HL = address
; Clock timing: 175
GetScreenAddr:
push de
ld l,a
ld h,$00 ; now HL = A
add hl,hl ; now HL = A * 2
ld e,l
ld d,h ; now DE = A * 2
add hl,hl ; now HL = A * 4
add hl,de ; now HL = A * 6
add hl,hl ; now HL = A * 12
add hl,hl ; now HL = A * 24
ld de,ShadowScreen
add hl,de
ld a,(L86D7) ; get penCol
srl a ; shift right
srl a ;
srl a ; now A = 8px column
ld e,a
ld d,$00
add hl,de ; now HL = line address + column
pop de
ret
; Draw tile with mask 16x8 -> 16x16 on shadow screen - for Tileset2 tiles
; L = penRow; E = penCol; IX = tile address
DrawTileMasked:
ld a,e
ld (L86D7),a ; penCol
ld a,l ; penRow
ld b,8 ; 8 row pairs
call GetScreenAddr ; now HL = screen addr
DrawTileMasked_1:
; push bc
ld de,24-1
; Draw 1st line
ld a,(ix+$00) ; get mask
and (hl)
or (ix+$01)
ld (hl),a ; write 1st byte
inc hl
ld a,(ix+$02) ; get mask
and (hl)
or (ix+$03)
ld (hl),a ; write 2nd byte
add hl,de ; to the 2nd line
; Draw 2nd line
ld a,(ix+$04) ; get mask
and (hl)
or (ix+$05)
ld (hl),a ; write 1st byte
inc hl
ld a,(ix+$06) ; get mask
and (hl)
or (ix+$07)
ld (hl),a ; write 2nd byte
add hl,de ; to the next line
; pop bc
ld de,$0008
add ix,de
djnz DrawTileMasked_1
ret
; Draw string on shadow screen using FontProto
; HL = string addr
DrawString:
ld a,(hl)
inc hl
or a
ret z
call DrawChar
jr DrawString
; Draw decimal number HL in 5 digits
DrawNumber5:
ld bc,-10000
call DrawNumber_1
ld bc,-1000
call DrawNumber_1
; Draw decimal number HL in 3 digits
DrawNumber3:
ld bc,-100
call DrawNumber_1
ld c,-10
call DrawNumber_1
ld c,-1
DrawNumber_1:
ld a,'0'-1
DrawNumber_2:
inc a
add hl,bc
jr c,DrawNumber_2
sbc hl,bc
; jp DrawChar
;
; Draw character on the screen using FontProto
; A = character to show: $00-$1F space with A width; $20 space
DrawChar:
push hl
push bc
cp $20 ; $00-$1F ?
jr c,DrawChar_00 ; yes => set char width and process like space char
jr nz,DrawChar_0 ; not space char => jump
ld a,$03 ; space char gap size
DrawChar_00:
ld (DrawChar_width),a
jp DrawChar_fin
DrawChar_0:
cp $27 ; char less than apostroph?
jr nc,DrawChar_1
add a,$3A ; for '!', quotes, '#' '$' '%' '&'
jr DrawChar_2
DrawChar_1:
cp $2A ; char less than '*'?
jr nc,DrawChar_2
add a,$15 ; for apostroph, '(' ')' chars
DrawChar_2:
sub $2C ; font starts from ','
ld e,a ; calculating the symbol address
ld l,a ;
ld h,$00 ;
ld d,h ;
add hl,hl ; now hl = a * 2
add hl,hl ; now hl = a * 4
add hl,de ; now hl = a * 5
add hl,hl ; now hl = a * 10
add hl,de ; now hl = a * 11
ld de,FontProto
add hl,de ; now hl = addr of the symbol
ex de,hl ; now de=symbol addr
ld a,(L86D8) ; get penRow
ld (DrawChar_row),a
ld a,(de) ; get flag/width byte
inc de
bit 7,a ; lowered symbol?
jr z,DrawChar_3
ld hl,DrawChar_row
inc (hl) ; start on the next line
DrawChar_3:
and $0f ; keep width 1..8
add a,$02 ; gap 2px after the symbol
ld (DrawChar_width),a
ld a,(DrawChar_row)
call GetScreenAddr
push hl ; store addr on the screen
push de ; store symbol data addr
ld a,(L86D7) ; get penCol
and $07 ; shift 0..7
inc a
ld c,a
ld b,10 ; 10 lines
DrawChar_4: ; loop by lines
push bc ; save counter
ld a,(de)
inc de
DrawChar_5: ; loop for shift
dec c
jr z, DrawChar_6
srl a ; shift right
jr DrawChar_5
DrawChar_6:
or (hl)
ld (hl),a ; put on the screen
ld a,(DrawChar_row)
inc a
ld (DrawChar_row),a
call GetScreenAddr
pop bc ; restore counter and shift
djnz DrawChar_4
pop de ; restore symbol data addr
pop hl ; restore addr on the screen
ld a,(L86D7) ; get penCol
and $7 ; shift 0..7
ld b,a
ld a,(DrawChar_width)
add a,b
cp $08 ; shift + width <= 8 ?
jr c,DrawChar_fin ; yes => no need for 2nd pass
; Second pass
ld a,(L86D7) ; get penCol
and $07 ; shift 1..7
sub $08
neg ; a = 8 - shift; result is 1..7
inc a
ld c,a
ld a,(DrawChar_row)
add a,-10
ld (DrawChar_row),a
; call GetScreenAddr
inc hl
ld b,10 ; 10 lines
DrawChar_8: ; loop by lines
push bc ; save counter
ld a,(de)
inc de
DrawChar_9: ; loop for shift
dec c
jr z, DrawChar_A
sla a ; shift left
jr DrawChar_9
DrawChar_A:
or (hl)
ld (hl),a ; put on the screen
ld a,(DrawChar_row)
inc a
ld (DrawChar_row),a
call GetScreenAddr
inc hl
pop bc ; restore counter
djnz DrawChar_8
; All done, finalizing
DrawChar_fin:
ld hl,L86D7 ; penCol address
ld a,(DrawChar_width)
add a,(hl)
ld (hl),a ; updating penCol
pop bc
pop hl
ret
DrawChar_width: DB 0 ; Saved symbol width
DrawChar_row: DB 0 ; Saved current row number
;
ScreenThemeNite:
ld a,$05 ; attribute for dark/story mode
jr ScreenTheme_0
;
ScreenThemeLite:
ld a,$38 ; attribute for light/game mode
ScreenTheme_0:
ld hl,$58A2 ; ZX screen attribute address, top-left
ld c,18 ; height in attribute rows
ScreenTheme_1:
ld b,28 ; width of the area
ScreenTheme_2:
ld (hl),a
inc hl
djnz ScreenTheme_2
inc hl
inc hl
inc hl
inc hl
dec c
jr nz,ScreenTheme_1
ret
; Copy shadow screen 24*128=3072 bytes to ZX screen
; Second version, on LDIs
; Clock timing: 31 + (433+398+34) * 64 + 10 = 55401 (was 61279 on the DRAFT version with POPs/PUSHes)
ShowShadowScreen:
;31 Initializing
ld ix,ScreenAddrs ; table with ZX line addresses
ld hl,ShadowScreen ; shadow screen address
ld a,64 ; 64 line pairs = 128 lines
ShowShadowScreen_1: ; loop by A
;433 Copy the 1st line
ld e,(ix+$00)
ld d,(ix+$01) ; DE = start of first ZX screen line
push de
ldi ; byte 0
ldi ; byte 1
ldi ; byte 2
ldi ; byte 3
ldi ; byte 4
ldi ; byte 5
ldi ; byte 6
ldi ; byte 7
ldi ; byte 8
ldi ; byte 9
ldi ; byte 10
ldi ; byte 11
ldi ; byte 12
ldi ; byte 13
ldi ; byte 14
ldi ; byte 15
ldi ; byte 16
ldi ; byte 17
ldi ; byte 18
ldi ; byte 19
ldi ; byte 20
ldi ; byte 21
ldi ; byte 22
ldi ; byte 23
;398 Copy the 2nd line
pop de
inc d ; Next line on the ZX screen
ldi ; byte 0
ldi ; byte 1
ldi ; byte 2
ldi ; byte 3
ldi ; byte 4
ldi ; byte 5
ldi ; byte 6
ldi ; byte 7
ldi ; byte 8
ldi ; byte 9
ldi ; byte 10
ldi ; byte 11
ldi ; byte 12
ldi ; byte 13
ldi ; byte 14
ldi ; byte 15
ldi ; byte 16
ldi ; byte 17
ldi ; byte 18
ldi ; byte 19
ldi ; byte 20
ldi ; byte 21
ldi ; byte 22
ldi ; byte 23
;34 Continue the loop
inc ix
inc ix
dec a ; loop counter for line pairs
jp nz,ShowShadowScreen_1 ; continue the loop
;10 Finalization
ret
; Clear block on the shadow screen
; HL=row/col, DE=rows/cols
; columns are 8px wide; row=1..128, row=0..127; col=0..23, cols=1..24
ClearScreenBlock:
push bc
ld a,l ; column
ld c,h ; row
ld l,h ; row
ld h,$00
ld b,h
add hl,hl ; now HL = row * 2
add hl,bc ; now HL = row * 3
add hl,hl
add hl,hl
add hl,hl ; now HL = row * 24
ld c,a
add hl,bc ; now HL = row * 24 + col
ld bc,ShadowScreen
add hl,bc ; now HL = start address
ld c,24 ; line width in columns
xor a
; ld a,$55 ;DEBUG
ClearScreenBlock_1 ; loop by rows
push hl
ld b,e ; cols
ClearScreenBlock_2: ; loop by columns
ld (hl),a
inc hl
djnz ClearScreenBlock_2
pop hl
add hl,bc ; next line
dec d ; rows
jr nz,ClearScreenBlock_1
pop bc
ret
; 8-bit random number generator using Refresh Register (R)
; See http://www.cpcwiki.eu/index.php/Programming:Random_Number_Generator
GetRandomByte:
ld hl,(GetRandomByte_seed)
ld a,r
ld d,a
ld e,a
add hl,de
xor l
add a,a
xor h
ld l,a
ld (GetRandomByte_seed),hl
ret
GetRandomByte_seed: DEFW 12345
;
; Get random number 0..7
GetRandom8:
call GetRandomByte
and $07
ret
;
; Get random number 0..10 for door access codes
; value 10 is for '-' char and we made its probability lower by 1/3
GetRandom11:
call GetRandomByte
and $1F ; 0..31
GetRandom11_1:
cp 11 ; less than 11?
ret c ; yes => return 0..10
sub 11 ; 0..20, then 0..9
jr GetRandom11_1
;----------------------------------------------------------------------------
INCLUDE "desolcodb.asm"
;----------------------------------------------------------------------------
DesolateCodeEnd:
; Shadow screen, 192 x 140 pixels
; 12*2*(64*2+12) = 3360 bytes
ShadowScreen EQU $F2D0
;ShadowScreen:
; DEFS 3360,$00
IF DesolateCodeEnd > ShadowScreen
.ERROR DesolateCodeEnd overlaps ShadowScreen
ENDIF
;----------------------------------------------------------------------------
END