]> git.sur5r.net Git - cc65/blob - libsrc/nes/crt0.s
NTSC/PAL update by Stefan Haubenthal.
[cc65] / libsrc / nes / crt0.s
1 ;
2 ; Startup code for cc65 (NES version)
3 ;
4 ; by Groepaz/Hitmen <groepaz@gmx.net>
5 ; based on code by Ullrich von Bassewitz <uz@cc65.org>
6 ;
7 ; This must be the *first* file on the linker command line
8 ;
9
10         .export         _exit
11         .import         initlib, donelib, callmain
12         .import         push0, _main, zerobss, copydata
13         .import         ppubuf_flush
14
15         ; Linker generated symbols
16         .import         __RAM_START__, __RAM_SIZE__
17         .import         __SRAM_START__, __SRAM_SIZE__
18         .import         __ROM0_START__, __ROM0_SIZE__
19         .import         __STARTUP_LOAD__,__STARTUP_RUN__, __STARTUP_SIZE__
20         .import         __CODE_LOAD__,__CODE_RUN__, __CODE_SIZE__
21         .import         __RODATA_LOAD__,__RODATA_RUN__, __RODATA_SIZE__
22
23         .include        "zeropage.inc"
24         .include        "nes.inc"
25
26
27 ; ------------------------------------------------------------------------
28 ; 16 bytes INES header
29
30 .segment        "HEADER"
31
32 ;    +--------+------+------------------------------------------+
33 ;    | Offset | Size | Content(s)                               |
34 ;    +--------+------+------------------------------------------+
35 ;    |   0    |  3   | 'NES'                                    |
36 ;    |   3    |  1   | $1A                                      |
37 ;    |   4    |  1   | 16K PRG-ROM page count                   |
38 ;    |   5    |  1   | 8K CHR-ROM page count                    |
39 ;    |   6    |  1   | ROM Control Byte #1                      |
40 ;    |        |      |   %####vTsM                              |
41 ;    |        |      |    |  ||||+- 0=Horizontal mirroring      |
42 ;    |        |      |    |  ||||   1=Vertical mirroring        |
43 ;    |        |      |    |  |||+-- 1=SRAM enabled              |
44 ;    |        |      |    |  ||+--- 1=512-byte trainer present  |
45 ;    |        |      |    |  |+---- 1=Four-screen mirroring     |
46 ;    |        |      |    |  |                                  |
47 ;    |        |      |    +--+----- Mapper # (lower 4-bits)     |
48 ;    |   7    |  1   | ROM Control Byte #2                      |
49 ;    |        |      |   %####0000                              |
50 ;    |        |      |    |  |                                  |
51 ;    |        |      |    +--+----- Mapper # (upper 4-bits)     |
52 ;    |  8-15  |  8   | $00                                      |
53 ;    | 16-..  |      | Actual 16K PRG-ROM pages (in linear      |
54 ;    |  ...   |      | order). If a trainer exists, it precedes |
55 ;    |  ...   |      | the first PRG-ROM page.                  |
56 ;    | ..-EOF |      | CHR-ROM pages (in ascending order).      |
57 ;    +--------+------+------------------------------------------+
58
59         .byte   $4e,$45,$53,$1a ; "NES"^Z
60         .byte   2               ; ines prg  - Specifies the number of 16k prg banks.
61         .byte   1               ; ines chr  - Specifies the number of 8k chr banks.
62         .byte   %00000011       ; ines mir  - Specifies VRAM mirroring of the banks.
63         .byte   %00000000       ; ines map  - Specifies the NES mapper used.
64         .byte   0,0,0,0,0,0,0,0 ; 8 zeroes
65
66
67 ; ------------------------------------------------------------------------
68 ; Place the startup code in a special segment.
69
70 .segment        "STARTUP"
71
72 start:
73
74 ; setup the CPU and System-IRQ
75
76         sei
77         cld
78         ldx     #0
79         stx     VBLANK_FLAG
80
81         stx     ringread
82         stx     ringwrite
83         stx     ringcount
84
85         txs
86
87         lda     #$20
88 @l:     sta     ringbuff,x
89         sta     ringbuff+$0100,x
90         sta     ringbuff+$0200,x
91         inx
92         bne     @l
93
94 ; Clear the BSS data
95
96         jsr     zerobss
97
98 ; initialize data
99         jsr     copydata
100
101 ; setup the stack
102
103         lda     #<(__SRAM_START__ + __SRAM_SIZE__)
104         sta     sp
105         lda     #>(__SRAM_START__ + __SRAM_SIZE__)
106         sta     sp+1            ; Set argument stack ptr
107
108 ; Call module constructors
109
110         jsr     initlib
111
112 ; Push arguments and call main()
113
114         jsr     callmain
115
116 ; Call module destructors. This is also the _exit entry.
117
118 _exit:  jsr     donelib         ; Run module destructors
119
120 ; Reset the NES
121
122         jmp start
123
124 ; ------------------------------------------------------------------------
125 ; System V-Blank Interupt
126 ; updates PPU Memory (buffered)
127 ; updates VBLANK_FLAG and tickcount
128 ; ------------------------------------------------------------------------
129
130 nmi:    pha
131         tya
132         pha
133         txa
134         pha
135
136         lda     #1
137         sta     VBLANK_FLAG
138
139         inc     tickcount
140         bne     @s
141         inc     tickcount+1
142
143 @s:     jsr     ppubuf_flush
144
145         ; reset the video counter
146         lda     #$20
147         sta     PPU_VRAM_ADDR2
148         lda     #$00
149         sta     PPU_VRAM_ADDR2
150
151         ; reset scrolling
152         sta     PPU_VRAM_ADDR1
153         sta     PPU_VRAM_ADDR1
154
155         pla
156         tax
157         pla
158         tay
159         pla
160
161 ; Interrupt exit
162
163 irq2:
164 irq1:
165 timerirq:
166 irq:
167         rti
168
169 ; ------------------------------------------------------------------------
170 ; hardware vectors
171 ; ------------------------------------------------------------------------
172
173 .segment "VECTORS"
174
175         .word   irq2        ; $fff4 ?
176         .word   irq1        ; $fff6 ?
177         .word   timerirq    ; $fff8 ?
178         .word   nmi         ; $fffa vblank nmi
179         .word   start       ; $fffc reset
180         .word   irq         ; $fffe irq / brk
181
182 ; ------------------------------------------------------------------------
183 ; character data
184 ; ------------------------------------------------------------------------
185
186 .segment "CHARS"
187
188         .include        "neschar.inc"
189
190