-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asm
543 lines (456 loc) · 18.8 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
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
;==============================================================
; SMS defines
;==============================================================
VDPControl equ $bf
VdpData equ $be
VRAMWrite equ $4000
CRAMWrite equ $c000
ControlPortP1 equ $dc ; Control Port Player 1
SAT equ $3f00 ; starting location of SAT in VRAM
ply equ $c100 ; player Y (end of SAT buffer, which is $c000-c0ff)
plx equ $c101 ; player X
VDPStatus equ $c102 ; VDP Status Flags
input equ $c103 ; input from player 1 controller.
hspeed equ $08 ; player horizontal speed
scroll equ $c104 ; vdp scroll register buffer.
HScrollReg equ $08 ; horizontal scroll register
NextRowSrc equ $c105 ; store tilemap source row address (2 bytes)
NextColSrc equ $c107 ; store tilemap source col address (2 bytes)
NextRowDst equ $c109 ; store tilemap row address in VRAM (2 bytes)
NextColDst equ $c10b ; store tilemap col address in VRAM (2 bytes)
CurrentColScreen equ $c10d ; store screen column (0-32)
TileMapWidth equ $60 ; TileMap width
TileMapHeight equ $1c ; TileMap height
ScreenHeight equ $1c ; Screen height
ScreenWidth equ $20 ; Screen width
; Map of the sprite attribute table (sat) buffer.
; Contains sprites' vertical position (vpos), horizontal posi-
; tion (hpos) and character codes (cc).
plrvp equ $c000 ; first player vpos.
plrhp equ $c080 ; first player hpos.
plrcc equ $c081 ; first player cc.
endspr equ $c00c ; first unused sprite.
;==============================================================
; Boot section
;==============================================================
org $0000
di ; disable interrupts
im 1 ; Interrupt mode 1
jp main ; jump to main program
;==============================================================
; Do stuff when interrupts occurs
;==============================================================
org $0038
in a,(VDPControl) ; read status flags from VDP control
ld (VDPStatus),a ; save vdp status
reti ; return from interrupt
;==============================================================
; Pause button handler
;==============================================================
org $0066
retn ; Do nothing
;==============================================================
; Main program
;==============================================================
main:
ld sp,$dff0
;==============================================================
; Set up VDP registers
;==============================================================
ld hl,VDPInitData
ld b,VDPInitDataEnd-VDPInitData
ld c,VDPControl
otir
;==============================================================
; Clear VRAM
;==============================================================
; 1. Set VRAM write address to 0 by outputting $4000 ORed with $0000
ld hl,$0000 | VRAMWrite
call SetVDPAddress
; 2. Output 16KB of zeroes
ld bc,VRAMWrite ; Counter for 16KB of VRAM
call CopyToVDP
;==============================================================
; Load palette
;==============================================================
; Background
; 1. Set VRAM write address to CRAM (palette) address 0 (for palette index 0)
; by outputting $c000 ORed with $0000
ld hl,$0000 | CRAMWrite
call SetVDPAddress
; 2. Output colour data
ld hl,PaletteData
ld bc,PaletteDataEnd-PaletteData
call CopyToVDP
; Sprite
; 1. Set VRAM write address to CRAM (palette) address 0 (for palette index 0)
; by outputting $c000 ORed with $0000
ld hl,$0000 | CRAMWrite+16 ; 16 bg + 16 sprite colors
call SetVDPAddress
; 2. Output colour data
ld hl,SpritePaletteData
ld bc,SpritePaletteDataEnd-SpritePaletteData
call CopyToVDP
;==============================================================
; Load tiles (font)
;==============================================================
; Background
; 1. Set VRAM write address to tile index 0
; by outputting $4000 ORed with $0000
ld hl,$0000 | VRAMWrite
call SetVDPAddress
; 2. Output tile data
ld hl,TileData ; Location of tile data
ld bc,TileDataEnd-TileData ; Counter for number of bytes to write
call CopyToVDP
; Sprite
ld hl,$2000 | VRAMWrite
call SetVDPAddress
ld hl,SpriteTileData
ld bc,SpriteTileDataEnd-SpriteTileData
call CopyToVDP
;==============================================================
; Initialize background
;==============================================================
ld hl,0 ; initial tile source address
ld (NextColSrc),hl ; set source column
ld hl,0 ; initial tile destination address
ld (NextColDst),hl ; set destination column
; draw entire screen column by column
rept 32
call DrawColumn
ld a,2
call UpdateColumnIndex
endr
;==============================================================
; Initialize player position
;==============================================================
ld a,120
ld (ply),a ; set player Y (screen coordinates)
ld a,120
ld (plx),a ; set player X (screen coordinates)
;==============================================================
; Initialize scroll value
;==============================================================
ld a,0 ; set A = 0.
ld (scroll),a ; initial scroll value in buffer
ld a,0
ld (CurrentColScreen),a ; initial CurrentColScreen
;==============================================================
; Put main character in the SAT BUFFER
;==============================================================
ld hl,plrcc ; point to player cc in buffer.
ld de,PlayerN ; point to player graphics.
ld bc,PlayerNEnd-PlayerN ; Counter for number of bytes to write
call PlayerSprN ; set the char codes for player in buffer.
ld hl,endspr ; point to end of active sprites.
ld (hl),$d0 ; insert sprite terminator here.
call UpdateSATBuff ; update SAT buffer.
call LoadSAT ; load sat from buffer.
; Use VDP R0 bit5 to hide left char column (used to update tile when scrolling)
; https://www.smspower.org/Development/SMSOfficialDocs#VDPREGISTERS
ld a,%00100110
; ||||`---- Sprite Shift
; |||`----- Interrupt Enable
; ||`------ Left Column Blank
; |`------- Horizontal Scroll Inhibit (top 2 char rows)
; `-------- Vertical Scroll Inhibit (right 8 char columns)
out (VDPControl),a
ld a,$80 ; write to VDP R0
out (VDPControl),a
; Turn screen on
; https://www.smspower.org/Development/SMSOfficialDocs#VDPREGISTERS
ld a,%11100000
; |||| |`- Zoomed sprites -> 16x16 pixels
; |||| `-- Doubled sprites -> 2 tiles per sprite, 8x16
; |||`---- 30 row/240 line mode
; ||`----- 28 row/224 line mode
; |`------ VBlank interrupts
; `------- Enable display
out (VDPControl),a
ld a,$81 ; write to VDP R1
out (VDPControl),a
; This is the main loop.
Loop:
ei ; enable maskable interrupts (e.g. frame interrput)
halt ; stop CPU until interrupt occurs
call WaitVBlank
; Update vdp right when vblank begins!
call LoadSAT ; load sat from buffer.
call GetP1Keys ; read controller port.
CheckEndMap:
; reset nextColSrc if we are at the end of the map
ld hl,TileMapWidth
add hl,hl ; double tilemap width as tiles are made of 2 bytes (or 1 word)
ld bc,(nextColSrc)
sbc hl,bc
jr nz,CheckEndScreen
ld hl,0
ld (nextColSrc),hl
CheckEndScreen:
; reset nextColDst if we are at the end of the screen
ld a,ScreenWidth
add a
ld hl,(NextColDst)
ld b,l
sub b
jr nz,MovePlayerRight
ld hl,0
ld (NextColDst),a
MovePlayerRight:
; Test if player wants to move and scrolls background in the opposite direction to give sense of movement.
ld a,(input) ; read input from ram mirror.
bit 3,a ; is right key pressed?
jp nz,MovePlayerLeft ; no, then check for left movement
ld a,(scroll) ; get scroll buffer
sub hspeed ; sub constant speed to scroll
ld (scroll),a ; update scroll buffer
ld b,HScrollReg ; make the scroll happening
call SetRegister
; draw new column only if needed (in case the updated column is different than the one shown on screen)
ld a,(scroll) ; get updated scroll buffer
and %11111000 ; get top 5 bits
ld hl,(CurrentColScreen) ; get old CurrentColScreen
ld b,l
cp b
call nz,DrawColumn
ld a,2
call UpdateColumnIndex
; ; move the player to the right
; ld a,(plx) ; get player's hpos (x-coordinate)
; add a,hspeed ; add constant horizontal speed
; ld (plx),a ; update player x-coordinate
;
jp MovePlayerEnd ; exit move player part
MovePlayerLeft:
; Test if player wants to move and scrolls background in the opposite direction to give sense of movement.
bit 2,a ; is left key pressed?
jp nz,MovePlayerEnd ; no - end key check.
ld a,(scroll) ; Scroll background - update the horizontal scroll buffer.
add hspeed ; add constant speed to scroll
ld (scroll),a ; update scroll buffer.
ld b,HScrollReg ; make the scroll happening
call SetRegister
; ; move the player to the left
; ld a,(plx) ; get player's hpos (x-coordinate)
; sub a,hspeed ; add constant horizontal speed
; ld (plx),a ; update player x-coordinate
MovePlayerEnd:
; end of player movements
; Update player sprites in the buffer.
call UpdateSATBuff
ld a,(scroll) ; get updated scroll in VDP
and %11111000 ; get top 5 bits
ld (CurrentColScreen),a ; update CurrentColScreen
jp Loop ; jump back to start of main loop
;==============================================================
; Helpers
;==============================================================
SetVDPAddress:
; Sets the VDP address
; Parameters: hl = address
push af
ld a,l
out (VDPControl),a
ld a,h
out (VDPControl),a
pop af
ret
SetRegister:
; Write to target register.
; Parameters: a = byte to be loaded into vdp register, b = target register 0-10
out ($bf),a ; output command word 1/2.
ld a,$80 ; VDP register command byte
or b
out ($bf),a ; output command word 2/2.
ret
CopyToVDP:
; Copies data to the VDP
; Parameters: hl = data address, bc = data length
; Affects: a, hl, bc
ld a,(hl) ; Get data byte
out (VDPData),a
inc hl ; Point to next letter
dec bc
ld a,c
or b
jr nz,CopyToVDP
ret
DrawColumn:
; draw bg column from tilemap to VRAM (to be used for horizontal scrolling when player moves)
; The tilemap is usually stored in VRAM at location $3800, and
; takes up 1792 bytes (32×28×2 bytes).
; Affects: hl, bc, de
ld de,ScreenHeight ; go through all rows
; initialize first rows
ld hl,0 ; initial tile source address
ld (NextRowSrc),hl ; set source row
ld hl,$3800 ; initialize tile destination address
ld (NextRowDst),hl ; set destination row
; update row index with col index
ld bc,(NextColSrc) ; next col tile address
ld hl,(NextRowSrc)
add hl,bc ; add col index to row index
ld (NextRowSrc),hl
ld bc,(NextColDst) ; next col tile address
ld hl,(NextRowDst)
add hl,bc ; add col index to row index
ld (NextRowDst),hl
DrawColumnLoop:
; Set VRAM write address to tilemap destination index
ld hl,(NextRowDst)
ld a,l
out (VDPControl),a
ld a,h
or a,$40
out (VDPControl),a
; 2. Output tilemap data
ld hl,TileMapData ; Location of tile data
ld bc,(NextRowSrc)
add hl,bc
ld bc,2 ; Counter for number of bytes to write
call CopyToVDP
ld bc,$40 ; next row's tile address is always 64 bytes away
ld hl,(NextRowDst)
add hl,bc ; add 64 to the target address
ld (NextRowDst),hl ; store new VRAM row address
ld hl,TileMapWidth ; assuming map is a row-major matrix
add hl,hl ; double tilemap width as tiles are made of 2 bytes (or 1 word)
ld bc,(NextRowSrc)
add hl,bc ; add map's width ×2 (again, a constant) to the source address
ld (NextRowSrc),hl ; store new tilemap source row address
dec de
ld a,e
or d
jr nz,DrawColumnLoop
ret
UpdateColumnIndex:
; Update the column index
; Parameters: a = index offset
; Affects: a, hl, bc
; update source column index
ld hl,(NextColSrc)
ld b,0
ld c,a
add hl,bc
ld (NextColSrc),hl
; update destination column index
ld hl,(NextColDst)
ld b,0
ld c,a
add hl,bc
ld (NextColDst),hl
ret
LoadSAT:
; Load SPRITE ATTRIBUTE TABLE
; Load data into sprite attribute table (SAT) from the buffer.
ld hl,SAT | VRAMWrite ; point to start of SAT in vram.
call SetVDPAddress ; prepare vram to recieve data.
ld b,255 ; amount of bytes to output.
ld c,VdpData ; destination is vdp data port.
ld hl,plrvp
otir ; output buffer to vdp.
ret
UpdateSATBuff:
; Generate vpos, hpos and cc data for the sprites.
; Generate sat buffer data from player's x,y coordinates.
; INFO: we don't have to update characters' CC, only their XY positions
ld a,(ply) ; load player's current y-coordinate.
ld hl,plrvp ; buffer address of player vpos.
call PlayerSprY ; refresh buffer according to y.
ld a,(plx) ; load player's current x-coordinate.
ld hl,plrhp ; buffer address of player hpos.
call PlayerSprX ; refresh buffer according to x.
ret
PlayerSprY:
; player Y SPRITES' VERTICAL IN SAT Buffer
; Parameters: hl = SATY address, a = ply
; Affects: a, hl, b
rept 4 ; repeat 4 times as player is 4 tiles tall
inline ; use local name space (VASM does not have local name space like WLA-DX)
ld b,3 ; counter 3 as player is tile wide
.PlayerSprYLoop:
ld (hl),a ; write y pos to buffer
inc hl ; next Y coord
djnz .PlayerSprYLoop ; decrease B and jump back if NonZero
add a,8 ; add 8 (since next row is 8 pixels below)
einline ; end of local name space (http://eab.abime.net/showthread.php?t=88827)
endr ; end of rept
ret
; --------------------------------------------------------------
PlayerSprX:
; player X SPRITES' HORIZONTAL IN SAT Buffer
; Parameters: hl = SATX address, a = plx
; Affects: a, hl, b
ld c,a ; save initial X coordinate in C
rept 4 ; repeat 4 times as player is 4 tiles tall
inline ; use local name space (VASM does not have local name space like WLA-DX)
ld a,c ; start from the initial X coordinate saved in C
ld b,3 ; counter 3 as player is tile wide
.PlayerSprXLoop:
ld (hl),a ; write x pos to buffer
inc hl ; first SATX increase points to sprite number byte
inc hl ; second SATX increase point to next X coord byte
add a,8 ; add 8 (since next column is 8 pixels apart)
djnz .PlayerSprXLoop ; decrease B and jump back if NonZero
einline ; end of local name space (http://eab.abime.net/showthread.php?t=88827)
endr ; end of rept
ret
PlayerSprN:
; player N SPRITES' IN SAT Buffer
; Parameters: de = SAT buffer index, bc = data length, hl = pointer to 12 byte char codes block
; Affects: a, hl, bc, de
ld a,(de) ; Get data byte
ld (hl),a
inc de ; point to next sprite index
inc hl ; first increase points to X coord byte
inc hl ; second increase point to next sprite number byte
dec bc
ld a,c
or b
jr nz,PlayerSprN
ret
WaitVBlank:
; Wait for vertical blanking phase.
ld a,(VDPStatus) ; get VDP status flags
bit 7,a ; get frame interrupt
jp z,WaitVBlank ; keep looping if FI is set
res 7,a ; reset bit 7 of VDP status flags
ld (VDPStatus),a ; update VDP status flags
ret
GetP1Keys:
; Read player 1 keys (port $dc) into ram mirror (input).
; Affects: a, hl, bc, de
in a,(ControlPortP1) ; read player 1 input port $dc.
ld (input),a ; let variable mirror port status.
ret
;==============================================================
; Data
;==============================================================
; VDP initialisation data
VDPInitData:
db $04,$80,$00,$81,$ff,$82,$ff,$85,$ff,$86,$ff,$87,$00,$88,$00,$89,$ff,$8a
VDPInitDataEnd:
; Background assets
TileMapData:
include "assets/tilemap.inc"
TileMapDataEnd:
; SMS color: --BBGGRR (e.g. RGB yellow = 255,255,0 > %00001111 > $f)
; https://coolconversion.com/math/binary-octal-hexa-decimal/Convert_hex_number_3F_in_binary_
PaletteData:
include "assets/palette.inc"
PaletteDataEnd:
TileData:
include "assets/tiles.inc"
TileDataEnd:
; Sprite assets
SpritePaletteData:
include "assets/sprites_palette.inc"
SpritePaletteDataEnd:
SpriteTileData:
include "assets/player_tiles.inc"
SpriteTileDataEnd:
; Sprite Number
PlayerN:
db 0,1,2,3,4,5,6,7,8,9,10,11
PlayerNEnd: