-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.s
278 lines (242 loc) · 8.57 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
277
278
.include "includes/actors/actor_types.inc"
.include "includes/constants.inc"
.include "includes/header.inc"
.include "includes/variables.inc"
.include "includes/ram.inc"
.segment "CODE"
.include "includes/init.inc"
.include "includes/utils.inc"
.include "includes/controller.inc"
.include "includes/actors/actors.inc"
; Levels
.include "includes/levels/0_titlescreen.inc"
.include "includes/levels/between.inc"
.include "includes/levels/1_level.inc"
.include "includes/levels/2_level.inc"
; Audio
.include "lib/famistudio.s"
.include "audio/sfx.s"
.include "audio/titlescreen.s"
.include "audio/song1.s"
.include "audio/song2.s"
.proc DrawSprites
; Copy sprite data to OAM
lda #$02 ; Where OAM should start copying from $02 means it will copy from $0200-02FF
sta OAM_DMA_COPY ; Send the source address to initial DMA copy
rts
.endproc
.proc ShowText
bit PPU_STATUS
SetPPUAddresByPointer DrawTextPosPtr
ldy #0
WhileTextByteNotZero:
lda (DrawTextPtr), y
cmp $0
beq BreakLoop
sta DrawTextAsciiCode
ldx DrawTextAsciiCode
lda ASCIITable, x
sta PPU_DATA
iny
jmp WhileTextByteNotZero
BreakLoop:
rts
.endproc
; This is a subroutine and it is doooooope
; Subroutines are scoped - meaning labels defined in subroutines
; are local to that subroutine
.proc LoadPalettes
PPU_SETADDR $3F00
; This is a for loop
; We are looping through PaletteData and reading those
; bytes into the PPU_DATA register
; Also notice that we don't have to constantly update the PPU_ADDR
; we're writing into. You only need to set the head and then it will increment
; as you write. Not bad!
ldy #0 ; for i=0
bit PPU_STATUS
ReadPaletteBytes:
; x = PaletteData[y]
lda (PalettePointer),y
; ppu_data_write(x)
sta PPU_DATA
iny ; i++
cpy #32 ; if y == 32
; break
bne ReadPaletteBytes
; rts is required at the end of a subroutine
rts
.endproc
.proc LoadNametable
PPU_SETADDR $2000
; Copy the BackgroundPtr so we don't stomp over it while we're iterating
CopyPointer BackgroundPtr, BGDrawPointer
ldy #0 ; y stores the lower bit offset into the bg data arry
ldx #0 ; x stores the higher bit offset into the bg data array
GetChunk: ; We can only load 255 at a time, so we break it into 4 255 char chunks
ReadBytesInChunk:
lda (BGDrawPointer), y ; Read value from address BGDrawPointer + y
sta PPU_DATA
iny
cpy #255 ; Keep iterating until we've read all 255 bytes in the chunk
bne ReadBytesInChunk
inc BGDrawPointer+1 ;Increate the hi-byte in the pointer address by one, which will increase our offset by 255
inx ; increase x
cpx #4 ; iterate for 4 chunks (4 chunks of 255b = 1kb)
bne GetChunk
rts
.endproc
.proc LoadLevel
ldx #0
stx $2000 ; Disable NMI
stx $2001 ; Disable rendering
stx $4010 ; Disable DMC IRQs
bit PPU_STATUS ; Clear the VBlank flag as we don't know its state on boot
lda Level
cmp #0
bne :+
; SetPointer DrawTextPtr, NoText
; SetPointer DrawTextPosPtr, $2020
SetPointer BackgroundPtr, TitleScreen_BackgroundData
SetPointer LevelActorDataPointer, TitleScreen_ActorData
SetPointer PalettePointer, TitleScreen_PaletteData
lda #1
ldx #<music_data_titlescreen
ldy #>music_data_titlescreen
jsr famistudio_init
:
cmp #1
bne :+
;SetPointer DrawTextPtr, NoText
; SetPointer DrawTextPosPtr, $2020
SetPointer BackgroundPtr, LevelOne_BackgroundData
SetPointer LevelActorDataPointer, LevelOne_ActorData
SetPointer PalettePointer, LevelOne_PaletteData
lda #1
ldx #<music_data_song_1
ldy #>music_data_song_1
jsr famistudio_init
:
cmp #2
bne :+
SetPointer BackgroundPtr, Between_BackgroundData
SetPointer LevelActorDataPointer, Between_ActorData
SetPointer PalettePointer, Between_PaletteData
lda #1
ldx #<music_data_titlescreen
ldy #>music_data_titlescreen
jsr famistudio_init
:
cmp #3
bne :+
SetPointer BackgroundPtr, LevelTwo_BackgroundData
SetPointer LevelActorDataPointer, LevelTwo_ActorData
SetPointer PalettePointer, LevelTwo_PaletteData
lda #1
ldx #<music_data_song_2
ldy #>music_data_song_2
jsr famistudio_init
:
jsr Actor_ClearAll
jsr Actor_ClearOAMCache
jsr LoadPalettes
jsr LoadNametable
jsr Actor_LoadLevelActorData
jsr Actor_LoadSpriteData
;jsr ShowText
lda #%10010000 ; Enable NMI interrupts from PPU. Set BG to use 2nd pattern table
sta PPU_CTRL
; Disable PPU Scrolling. This is customary to do when drawing to prevent errant scrolling
; PPU_SCROLL is also a latch address, so we need to set it twice
lda #0
sta PPU_SCROLL ; X Scrolling
sta PPU_SCROLL ; Y Scrolling
lda #%00011110
sta PPU_MASK ; Setting the mask ensures we show the background
lda #0
jsr famistudio_music_play
rts
.endproc
; Code of PRG ROM
Reset:
INIT_NES
INIT_VARIABLES
lda #1
ldx #<sounds
ldy #>sounds
jsr famistudio_sfx_init
lda #0
sta Level
jsr LoadLevel
GameLoop:
jsr Controller_ReadButtons
jsr Controller_ButtonHandler
jsr Actor_RunAll
jsr Actor_CheckCollisions
jsr AdvanceLevel
WaitForVBlank:
lda IsDrawComplete
cmp #FALSE
beq WaitForVBlank
lda #FALSE
sta IsDrawComplete
jsr famistudio_update
jmp GameLoop
NMI:
; This tripped me up for a while
; We can get our registers clobbered
NMI_CacheRegisters
jsr NMI_ChangeLevel
NMI_FetchCachedRegisters
; This is our main draw code
; In our game all we draw are sprites, which
; is handled by a DMA copy from our OAM_COPY
; to OAM on the PPU. We don't scroll or mess with tiles
; so this is really it (except for text which we need to add)
jsr DrawSprites
ManageTime:
inc Frame
lda Frame
cmp #60
bne :+
lda #0
sta Frame
:
SkipDraw:
lda #TRUE
inc IsDrawComplete
NMI_FetchCachedRegisters
rti ; Return from Interrupt
IRQ:
rti ; Return from Interrupt
NoText:
.byte $0
TextMessage:
.byte "LIVES 03", $0
FromAdamMessage:
.byte "SCORE ", $0
ASCIITable:
; Position is ASCII Code. Value at position is Tile ID
; 0 1 2 3 4 5 6 7 8 9 10 11
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $00,$00,$00
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $00,$00,$00
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $26,$00,$00
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $00,$25,$00
.byte $01,$02,$03, $04,$05,$06, $07,$08,$09, $0a,$00,$00
.byte $00,$00,$00, $00,$27,$0b, $0c,$0d,$0e, $0f,$10,$11
.byte $12,$13,$14, $15,$16,$17, $18,$19,$1a, $1b,$1c,$1d
.byte $1e,$1f,$20, $21,$22,$24, $24,$00,$00, $00,$00,$00
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $00,$00,$00
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $00,$00,$00
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $00,$00,$00
.byte $00,$00,$00, $00,$00,$00, $00,$00,$00, $00,$00,$00
;Load CHAR_ROM pattern tables
.segment "CHARS"
.incbin "graphics/cloudworld.chr"
; $FFFA
.segment "VECTORS"
;On boot the NES jumps to $FFFA to find the addresses for the handlers
;This is called the VECTORS
.word NMI ; address of the NMI handler
.word Reset ; address of the RESET handler
.word IRQ ; address of the IRQ handler