-
Notifications
You must be signed in to change notification settings - Fork 9
/
crt0.s
259 lines (198 loc) · 4 KB
/
crt0.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
; Startup code for cc65 and Shiru's NES library
; based on code by Groepaz/Hitmen <[email protected]>, Ullrich von Bassewitz <[email protected]>
FT_DPCM_OFF = $c000 ;$c000..$ffc0, 64-byte steps
FT_SFX_STREAMS = 4 ;number of sound effects played at once, 1..4
.define FT_DPCM_ENABLE 1 ;undefine to exclude all DMC code
.define FT_SFX_ENABLE 1 ;undefine to exclude all sound effects code
.export _exit,__STARTUP__:absolute=1
.import initlib,push0,popa,popax,_main,zerobss,copydata
; Linker generated symbols
.import __RAM_START__ ,__RAM_SIZE__
.import __ROM0_START__ ,__ROM0_SIZE__
.import __STARTUP_LOAD__,__STARTUP_RUN__,__STARTUP_SIZE__
.import __CODE_LOAD__ ,__CODE_RUN__ ,__CODE_SIZE__
.import __RODATA_LOAD__ ,__RODATA_RUN__ ,__RODATA_SIZE__
.import NES_MAPPER,NES_PRG_BANKS,NES_CHR_BANKS,NES_MIRRORING
.include "zeropage.inc"
FT_BASE_ADR =$0100 ;page in RAM, should be $xx00
.define FT_THREAD 1 ;undefine if you call sound effects in the same thread as sound update
.define FT_PAL_SUPPORT 1 ;undefine to exclude PAL support
.define FT_NTSC_SUPPORT 1 ;undefine to exclude NTSC support
PPU_CTRL =$2000
PPU_MASK =$2001
PPU_STATUS =$2002
PPU_OAM_ADDR=$2003
PPU_OAM_DATA=$2004
PPU_SCROLL =$2005
PPU_ADDR =$2006
PPU_DATA =$2007
PPU_OAM_DMA =$4014
PPU_FRAMECNT=$4017
DMC_FREQ =$4010
CTRL_PORT1 =$4016
CTRL_PORT2 =$4017
OAM_BUF =$0200
PAL_BUF =$01c0
.segment "ZEROPAGE"
NTSC_MODE: .res 1
FRAME_CNT1: .res 1
FRAME_CNT2: .res 1
VRAM_UPDATE: .res 1
NAME_UPD_ADR: .res 2
NAME_UPD_ENABLE: .res 1
PAL_UPDATE: .res 1
PAL_BG_PTR: .res 2
PAL_SPR_PTR: .res 2
SCROLL_X: .res 1
SCROLL_Y: .res 1
SCROLL_X1: .res 1
SCROLL_Y1: .res 1
PAD_STATE: .res 2 ;one byte per controller
PAD_STATEP: .res 2
PAD_STATET: .res 2
PPU_CTRL_VAR: .res 1
PPU_CTRL_VAR1: .res 1
PPU_MASK_VAR: .res 1
RAND_SEED: .res 2
FT_TEMP: .res 3
TEMP: .res 11
PAD_BUF =TEMP+1
PTR =TEMP ;word
LEN =TEMP+2 ;word
NEXTSPR =TEMP+4
SCRX =TEMP+5
SCRY =TEMP+6
SRC =TEMP+7 ;word
DST =TEMP+9 ;word
RLE_LOW =TEMP
RLE_HIGH =TEMP+1
RLE_TAG =TEMP+2
RLE_BYTE =TEMP+3
.segment "HEADER"
.byte $4e,$45,$53,$1a
.byte <NES_PRG_BANKS
.byte <NES_CHR_BANKS
.byte <NES_MIRRORING|(<NES_MAPPER<<4)
.byte <NES_MAPPER&$f0
.res 8,0
.segment "STARTUP"
start:
_exit:
sei
ldx #$ff
txs
inx
stx PPU_MASK
stx DMC_FREQ
stx PPU_CTRL ;no NMI
initPPU:
bit PPU_STATUS
@1:
bit PPU_STATUS
bpl @1
@2:
bit PPU_STATUS
bpl @2
clearPalette:
lda #$3f
sta PPU_ADDR
stx PPU_ADDR
lda #$0f
ldx #$20
@1:
sta PPU_DATA
dex
bne @1
clearVRAM:
txa
ldy #$20
sty PPU_ADDR
sta PPU_ADDR
ldy #$10
@1:
sta PPU_DATA
inx
bne @1
dey
bne @1
clearRAM:
txa
@1:
sta $000,x
sta $100,x
sta $200,x
sta $300,x
sta $400,x
sta $500,x
sta $600,x
sta $700,x
inx
bne @1
lda #4
jsr _pal_bright
jsr _pal_clear
jsr _oam_clear
jsr zerobss
jsr copydata
lda #<(__RAM_START__+__RAM_SIZE__)
sta sp
lda #>(__RAM_START__+__RAM_SIZE__)
sta sp+1 ; Set argument stack ptr
jsr initlib
lda #%10000000
sta <PPU_CTRL_VAR
sta PPU_CTRL ;enable NMI
lda #%00000110
sta <PPU_MASK_VAR
waitSync3:
lda <FRAME_CNT1
@1:
cmp <FRAME_CNT1
beq @1
detectNTSC:
ldx #52 ;blargg's code
ldy #24
@1:
dex
bne @1
dey
bne @1
lda PPU_STATUS
and #$80
sta <NTSC_MODE
jsr _ppu_off
lda #0
ldx #0
jsr _set_vram_update
ldx #<music_data
ldy #>music_data
lda <NTSC_MODE
jsr FamiToneInit
.if(FT_SFX_ENABLE)
ldx #<sounds_data
ldy #>sounds_data
jsr FamiToneSfxInit
.endif
lda #$fd
sta <RAND_SEED
sta <RAND_SEED+1
lda #0
sta PPU_SCROLL
sta PPU_SCROLL
jmp _main ;no parameters
.include "neslib.s"
.segment "RODATA"
music_data:
.include "music.s"
.if(FT_SFX_ENABLE)
sounds_data:
.include "sounds.s"
.endif
.segment "SAMPLES"
;.incbin "music_dpcm.bin"
.segment "VECTORS"
.word nmi ;$fffa vblank nmi
.word start ;$fffc reset
.word irq ;$fffe irq / brk
.segment "CHARS"
.incbin "tileset.chr"