]> git.sur5r.net Git - cc65/blob - libsrc/nes/crt0.s
Improved cbm_dir routines by Thomas Giesel.
[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 ; setup 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 data
98         jsr     copydata
99
100 ; setup the stack
101
102         lda     #<(__SRAM_START__ + __SRAM_SIZE__)
103         sta     sp
104         lda     #>(__SRAM_START__ + __SRAM_SIZE__)
105         sta     sp+1            ; Set argument stack ptr
106
107 ; Call module constructors
108
109         jsr     initlib
110
111 ; Push arguments and call main()
112
113         jsr     callmain
114
115 ; Call 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 Interupt
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 irq2:
163 irq1:
164 timerirq:
165 irq:
166         rti
167
168 ; ------------------------------------------------------------------------
169 ; hardware vectors
170 ; ------------------------------------------------------------------------
171
172 .segment "VECTORS"
173
174         .word   irq2        ; $fff4 ?
175         .word   irq1        ; $fff6 ?
176         .word   timerirq    ; $fff8 ?
177         .word   nmi         ; $fffa vblank nmi
178         .word   start       ; $fffc reset
179         .word   irq         ; $fffe irq / brk
180
181 ; ------------------------------------------------------------------------
182 ; character data
183 ; ------------------------------------------------------------------------
184
185 .segment "CHARS"
186
187         .include        "neschar.inc"
188
189