]> git.sur5r.net Git - cc65/blob - libsrc/lynx/crt0.s
Lynx changes by Karri Kaksonen.
[cc65] / libsrc / lynx / crt0.s
1 ; ***
2 ; CC65 Lynx Library
3 ;
4 ; Originally by Bastian Schick
5 ; http://www.geocities.com/SiliconValley/Byte/4242/lynx/
6 ;
7 ; Ported to cc65 (http://www.cc65.org) by
8 ; Shawn Jefferson, June 2004
9 ;
10 ; ***
11 ;
12 ; Startup code for cc65 (Lynx version).  Based on Atari 8-bit startup
13 ; code structure.  The C stack is located at the end of the RAM memory
14 ; segment and grows downward.  Bastian Schick's executable header is put
15 ; on the front of the fully linked binary (see EXEHDR segment.)
16 ;
17
18         .include        "lynx.inc"
19         .export         _exit
20         .export         __STARTUP__ : absolute = 1      ; Mark as startup
21
22         .import         callirq, initlib, donelib
23         .import         zerobss
24         .import         callmain
25         .import         _main
26         .import         __BSS_LOAD__
27         .import         __INTERRUPTOR_COUNT__
28         .import         __RAM_START__, __RAM_SIZE__
29
30         .include        "zeropage.inc"
31         .include        "extzp.inc"
32
33
34 ; ------------------------------------------------------------------------
35 ; EXE header (BLL header)
36
37         .segment "EXEHDR"
38         .word   $0880
39         .dbyt   __RAM_START__
40         .dbyt   __BSS_LOAD__ - __RAM_START__ + 10
41         .byte   $42,$53
42         .byte   $39,$33
43
44
45 ; ------------------------------------------------------------------------
46 ; Mikey and Suzy init data, reg offsets and data
47
48         .rodata
49
50 SuzyInitReg:    .byte $28,$2a,$04,$06,$92,$83,$90
51 SuzyInitData:   .byte $7f,$7f,$00,$00,$24,$f3,$01
52
53 MikeyInitReg:   .byte $00,$01,$08,$09,$20,$28,$30,$38,$44,$50,$8a,$8b,$8c,$92,$93
54 MikeyInitData:  .byte $9e,$18,$68,$1f,$00,$00,$00,$00,$00,$ff,$1a,$1b,$04,$0d,$29
55
56
57 ; ------------------------------------------------------------------------
58 ; Actual code
59
60         .segment "STARTUP"
61
62 ; set up system
63
64         sei
65         cld
66         ldx     #$FF
67         txs
68
69 ; init bank switching
70
71         lda     #$C
72         sta     MAPCTL                  ; $FFF9
73
74 ; disable all timer interrupts
75
76         lda     #$80
77         trb     TIM0CTLA
78         trb     TIM1CTLA
79         trb     TIM2CTLA
80         trb     TIM3CTLA
81         trb     TIM5CTLA
82         trb     TIM6CTLA
83         trb     TIM7CTLA
84
85 ; disable TX/RX IRQ, set to 8E1
86
87         lda     #%11101
88         sta     SERCTL
89
90 ; clear all pending interrupts
91
92         lda     INTSET
93         sta     INTRST
94
95 ; setup the stack
96
97         lda     #<(__RAM_START__ + __RAM_SIZE__)
98         sta     sp
99         lda     #>(__RAM_START__ + __RAM_SIZE__)
100         sta     sp+1
101
102 ; Init Mickey
103
104         ldx     #.sizeof(MikeyInitReg)-1
105 mloop:  ldy     MikeyInitReg,x
106         lda     MikeyInitData,x
107         sta     $fd00,y
108         dex
109         bpl     mloop
110
111 ; these are RAM-shadows of read only regs
112
113         ldx     #$1b
114         stx     __iodat
115         dex                             ; $1A
116         stx     __iodir
117         ldx     #$d
118         stx     __viddma
119
120 ; Init Suzy
121
122         ldx     #.sizeof(SuzyInitReg)-1
123 sloop:  ldy     SuzyInitReg,x
124         lda     SuzyInitData,x
125         sta     $fc00,y
126         dex
127         bpl     sloop
128
129         lda     #$24
130         sta     __sprsys
131
132 ; Clear the BSS data
133
134         jsr     zerobss
135
136 ; If we have IRQ functions, set the IRQ vector
137 ; as Lynx is a console there is not much point in releasing the IRQ
138
139         lda     #<__INTERRUPTOR_COUNT__
140         beq     NoIRQ1
141         lda     #<IRQStub
142         ldx     #>IRQStub
143         sei
144         sta     INTVECTL
145         stx     INTVECTH
146         cli
147
148 ; Call module constructors
149
150 NoIRQ1: jsr     initlib
151
152 ; Push arguments and call main
153
154         jsr     callmain
155
156 ; Call module destructors. This is also the _exit entry.
157
158 _exit:  jsr     donelib         ; Run module destructors
159
160 ; Endless loop
161
162 noret:  bra     noret
163
164
165         .segment "CODE"
166 IRQStub:
167         phy
168         phx
169         pha
170         cld
171         jsr     callirq
172         lda     INTSET
173         sta     INTRST
174         pla
175         plx
176         ply
177         rti
178