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