2000-05-28 13:40:48 +00:00
;
; Startup code for cc65 (ATARI version)
;
; Contributing authors:
2013-05-09 11:56:54 +00:00
; Mark Keates
; Freddy Offenga
; Christian Groessler
; Stefan Haubenthal
2000-05-28 13:40:48 +00:00
;
2013-05-09 11:56:54 +00:00
.export __STARTUP__ : absolute = 1 ; Mark as startup
2016-06-13 18:40:01 +00:00
.export _ exit, s t a r t , e x c e x i t , S P _ s a v e
2000-12-03 23:50:16 +00:00
2013-05-09 11:56:54 +00:00
.import initlib, d o n e l i b
.import callmain, z e r o b s s
.import __RESERVED_MEMORY__
2016-03-07 00:28:55 +00:00
.import _ _ MAIN_ S T A R T _ _ , _ _ M A I N _ S I Z E _ _
2018-09-02 21:07:03 +00:00
.import _ _ LOWCODE_ R U N _ _ , _ _ L O W C O D E _ S I Z E _ _
2013-09-19 20:36:22 +00:00
.ifdef __ATARIXL__
2013-09-17 21:03:33 +00:00
.import __STACKSIZE__
2013-07-03 09:38:28 +00:00
.import sram_init
.import scrdev
.import findfreeiocb
2013-10-18 20:07:05 +00:00
.forceimport sramprep ; force inclusion of the "shadow RAM preparation" load chunk
2013-07-03 09:38:28 +00:00
.include " save_ a r e a . i n c "
2013-06-12 23:17:53 +00:00
.endif
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
.include " zeropage. i n c "
.include " atari. i n c "
2000-05-28 13:40:48 +00:00
; ------------------------------------------------------------------------
2013-05-09 11:56:54 +00:00
.segment " STARTUP"
2002-05-26 09:09:10 +00:00
2013-05-09 11:56:54 +00:00
rts ; fix for SpartaDOS / OS/A+
2014-08-23 18:05:36 +00:00
; They first call the entry point from AUTOSTRT; and
; then, the load address (this rts here).
2013-05-09 11:56:54 +00:00
; We point AUTOSTRT directly after the rts.
2000-05-28 13:40:48 +00:00
; Real entry point:
2013-09-13 16:28:17 +00:00
start :
2013-09-19 20:36:22 +00:00
.ifdef __ATARIXL__
2013-07-03 09:38:28 +00:00
jsr s r a m _ i n i t
2013-05-30 23:47:25 +00:00
.endif
2014-08-23 18:05:36 +00:00
; Clear the BSS data.
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
jsr z e r o b s s
2000-05-28 13:40:48 +00:00
2014-08-23 18:05:36 +00:00
; Set up the stack.
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
tsx
2013-09-19 20:40:20 +00:00
stx S P _ s a v e
2000-05-28 13:40:48 +00:00
2014-06-03 16:30:11 +00:00
.ifdef __ATARIXL__
2016-03-07 00:28:55 +00:00
lda #< ( _ _ M A I N _ S T A R T _ _ + _ _ M A I N _ S I Z E _ _ + _ _ S T A C K S I Z E _ _ )
ldx #> ( _ _ M A I N _ S T A R T _ _ + _ _ M A I N _ S I Z E _ _ + _ _ S T A C K S I Z E _ _ )
2014-06-03 16:30:11 +00:00
sta s p
2016-03-07 00:28:55 +00:00
stx s p + 1
2014-06-03 16:30:11 +00:00
.else
2013-05-30 00:30:19 +00:00
2014-08-23 18:05:36 +00:00
; Report the memory usage.
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
lda A P P M H I
2016-03-13 20:23:45 +00:00
ldx A P P M H I + 1
2013-07-03 09:38:28 +00:00
sta A P P M H I _ s a v e ; remember old APPMHI value
2016-03-13 20:23:45 +00:00
stx A P P M H I _ s a v e + 1
2013-05-09 11:56:54 +00:00
sec
lda M E M T O P
sbc #< _ _ R E S E R V E D _ M E M O R Y _ _
sta A P P M H I ; initialize our APPMHI value
2014-08-23 18:05:36 +00:00
sta s p ; set up runtime stack part 1
2013-05-09 11:56:54 +00:00
lda M E M T O P + 1
sbc #> _ _ R E S E R V E D _ M E M O R Y _ _
sta A P P M H I + 1
2014-08-23 18:05:36 +00:00
sta s p + 1 ; set up runtime stack part 2
2000-09-07 21:49:13 +00:00
2013-05-30 00:30:19 +00:00
.endif
2014-08-23 18:05:36 +00:00
; Call the module constructors.
2000-12-03 21:40:07 +00:00
2013-05-09 11:56:54 +00:00
jsr i n i t l i b
2000-12-03 21:40:07 +00:00
2014-08-23 18:05:36 +00:00
; Set the left margin to 0.
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
lda L M A R G N
2013-09-19 20:40:20 +00:00
sta L M A R G N _ s a v e
2013-05-09 11:56:54 +00:00
ldy #0
sty L M A R G N
2000-05-28 13:40:48 +00:00
2014-08-23 18:05:36 +00:00
; Set the keyboard to upper-/lower-case mode.
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
ldx S H F L O K
2013-09-19 20:40:20 +00:00
stx S H F L O K _ s a v e
2013-05-09 11:56:54 +00:00
sty S H F L O K
2000-09-07 21:49:13 +00:00
2014-08-23 18:05:36 +00:00
; Initialize the conio stuff.
2000-05-28 13:40:48 +00:00
2014-03-07 22:10:22 +00:00
dey ; Set Y to $FF
sty C H ; remove keypress which might be in the input buffer
2000-05-28 13:40:48 +00:00
2014-08-23 18:05:36 +00:00
; Push the command-line arguments; and, call main().
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
jsr c a l l m a i n
2000-05-28 13:40:48 +00:00
2014-08-23 18:05:36 +00:00
; Call the module destructors. This is also the exit() entry.
2000-05-28 13:40:48 +00:00
2016-06-13 18:40:01 +00:00
_exit : ldx S P _ s a v e
txs ; Restore stack pointer
2000-11-22 22:19:09 +00:00
2014-08-23 18:05:36 +00:00
; Restore the system stuff.
2000-05-28 13:40:48 +00:00
2016-06-13 18:40:01 +00:00
excexit : jsr d o n e l i b ; Run module destructors; 'excexit' is called from the exec routine
2000-05-28 13:40:48 +00:00
2014-08-23 18:05:36 +00:00
; Restore the left margin.
2000-05-28 13:40:48 +00:00
2013-09-19 20:40:20 +00:00
lda L M A R G N _ s a v e
2013-05-09 11:56:54 +00:00
sta L M A R G N
2000-05-28 13:40:48 +00:00
2014-08-23 18:05:36 +00:00
; Restore the kb mode.
2000-05-28 13:40:48 +00:00
2013-09-19 20:40:20 +00:00
lda S H F L O K _ s a v e
2013-05-09 11:56:54 +00:00
sta S H F L O K
2000-05-28 13:40:48 +00:00
2014-08-23 18:05:36 +00:00
; Restore APPMHI.
2000-09-07 21:49:13 +00:00
2013-07-03 09:38:28 +00:00
lda A P P M H I _ s a v e
2016-03-13 20:23:45 +00:00
ldx A P P M H I _ s a v e + 1
2013-05-09 11:56:54 +00:00
sta A P P M H I
2016-03-13 20:23:45 +00:00
stx A P P M H I + 1
2000-05-28 13:40:48 +00:00
2013-09-19 20:36:22 +00:00
.ifdef __ATARIXL__
2013-05-30 00:30:19 +00:00
; Atari XL target stuff...
2013-07-03 09:38:28 +00:00
lda P O R T B _ s a v e
sta P O R T B
lda R A M T O P _ s a v e
sta R A M T O P
lda M E M T O P _ s a v e
2016-03-13 20:23:45 +00:00
ldx M E M T O P _ s a v e + 1
2013-07-03 09:38:28 +00:00
sta M E M T O P
2016-03-13 20:23:45 +00:00
stx M E M T O P + 1
2013-06-12 23:17:53 +00:00
2014-08-23 18:05:36 +00:00
; Issue a GRAPHICS 0 call (copied'n'pasted from the TGI drivers), in
; order to restore screen memory to its default location just
2013-10-01 09:32:08 +00:00
; before the ROM.
2013-06-12 23:17:53 +00:00
2013-07-03 09:38:28 +00:00
jsr f i n d f r e e i o c b
2013-06-12 23:17:53 +00:00
; Reopen it in Graphics 0
lda #O P E N
sta I C C O M ,x
lda #O P N I N | O P N O T
sta I C A X 1 ,x
lda #0
sta I C A X 2 ,x
lda #< s c r d e v
sta I C B A L ,x
lda #> s c r d e v
sta I C B A H ,x
lda #3
sta I C B L L ,x
lda #0
sta I C B L H ,x
jsr C I O V _ o r g
2014-08-23 18:05:36 +00:00
; No error checking here, shouldn't happen(TM); and, no way to
2013-10-01 09:32:08 +00:00
; recover anyway.
2013-06-24 23:27:47 +00:00
lda #C L O S E
sta I C C O M ,x
jsr C I O V _ o r g
2013-06-12 23:17:53 +00:00
2013-05-30 00:30:19 +00:00
.endif
2014-08-23 18:05:36 +00:00
; Turn on the cursor.
2002-11-05 00:35:16 +00:00
2013-07-02 17:55:00 +00:00
ldx #0
2013-05-09 11:56:54 +00:00
stx C R S I N H
2002-11-05 00:35:16 +00:00
2014-08-23 18:05:36 +00:00
; Back to DOS.
2000-05-28 13:40:48 +00:00
2013-05-09 11:56:54 +00:00
rts
2000-05-28 13:40:48 +00:00
2013-02-12 22:39:38 +00:00
; *** end of main startup code
2011-06-13 16:42:45 +00:00
; ------------------------------------------------------------------------
2000-06-25 01:45:30 +00:00
2013-02-12 22:39:38 +00:00
.bss
2000-05-28 13:40:48 +00:00
2013-09-19 20:40:20 +00:00
SP_save : .res 1
2013-09-19 21:21:09 +00:00
SHFLOK_save : .res 1
2013-09-19 20:40:20 +00:00
LMARGN_save : .res 1
2013-09-19 20:36:22 +00:00
.ifndef __ATARIXL__
2013-07-03 09:38:28 +00:00
APPMHI_save : .res 2
2013-06-24 22:53:21 +00:00
.endif
2018-09-02 21:07:03 +00:00
; ------------------------------------------------------------------------
.segment " LOWCODE" ; have at least one (empty) segment of LOWCODE, so that the next line works even if the program doesn't make use of this segment
.assert ( _ _ LOWCODE_ R U N _ _ + _ _ L O W C O D E _ S I Z E _ _ < = $ 4 0 0 0 | | _ _ L O W C O D E _ R U N _ _ > $ 7 F F F | | _ _ L O W C O D E _ S I Z E _ _ = 0 ) , w a r n i n g , " ' l o w c o d e a r e a ' r e a c h e s i n t o $ 4 0 0 0 . . $ 7 F F F b a n k m e m o r y w i n d o w "
; check for LOWBSS_SIZE = 0 not needed since the only file which uses LOWBSS (irq.s) also uses LOWCODE
; check for LOWCODE_RUN > $7FFF is mostly for cartridges, where this segment is loaded high (into cart ROM)
; there is a small chance that if the user loads the program really high, LOWCODE is above $7FFF, but LOWBSS is below -- no warning emitted in this case