From 0c161a6cdd257ed8470dd9073c06aa0a8003390a Mon Sep 17 00:00:00 2001
From: "ol.sc" 
Date: Mon, 14 Sep 2009 19:58:06 +0000
Subject: [PATCH] Now that cc65 programs can run as SYS files themselves my
 ProDOS Loader ulimately has to change from a minimalistic shell replacement
 to a pure loader (without exit hook). This approach simplifies several
 things. However the "reboot after exit" option now present as a ProDOS Loader
 variant has to move into the cc65 program.
git-svn-id: svn://svn.cc65.org/cc65/trunk@4172 b7a2c559-68d2-44c3-8de9-860c34a00d81
---
 doc/apple2.sgml           |  1 +
 doc/apple2enh.sgml        |  1 +
 doc/funcref.sgml          |  2 ++
 include/apple2.h          |  3 +++
 libsrc/apple2/Makefile    |  1 +
 libsrc/apple2/crt0.s      |  8 ++------
 libsrc/apple2/reboot.s    | 21 +++++++++++++++++++++
 libsrc/apple2enh/Makefile |  1 +
 8 files changed, 32 insertions(+), 6 deletions(-)
 create mode 100644 libsrc/apple2/reboot.s
diff --git a/doc/apple2.sgml b/doc/apple2.sgml
index 22416013d..1abcf3eda 100644
--- a/doc/apple2.sgml
+++ b/doc/apple2.sgml
@@ -127,6 +127,7 @@ usage.
 
 - _dos_type
 - get_ostype
+- rebootafterexit
 
diff --git a/doc/apple2enh.sgml b/doc/apple2enh.sgml
index dac70e877..af7fb4a51 100644
--- a/doc/apple2enh.sgml
+++ b/doc/apple2enh.sgml
@@ -127,6 +127,7 @@ usage.- _dos_type
 - get_ostype
+- rebootafterexit
 - textframe
 - textframexy
 - videomode
diff --git a/doc/funcref.sgml b/doc/funcref.sgml
index 628362d87..54d23f552 100644
--- a/doc/funcref.sgml
+++ b/doc/funcref.sgml
@@ -67,6 +67,7 @@ function.
 
 - _dos_type
 - [
+]- rebootafterexit
 
@@ -75,6 +76,7 @@ function.- _dos_type
 - [
+]- rebootafterexit
 - textframe
 - textframexy
 - [
diff --git a/include/apple2.h b/include/apple2.h
index 1aee91e40..779f83b7a 100644
--- a/include/apple2.h
+++ b/include/apple2.h
@@ -140,6 +140,9 @@ extern unsigned char _dos_type;
 unsigned char get_ostype (void);
 /* Get the machine type. Returns one of the APPLE_xxx codes. */
 
+void rebootafterexit (void);
+/* Reboot machine after program termination has completed. */
+
 /* The following #defines will cause the matching functions calls in conio.h
  * to be overlaid by macros with the same names, saving the function call
  * overhead.
diff --git a/libsrc/apple2/Makefile b/libsrc/apple2/Makefile
index 2a067e21a..802c404f5 100644
--- a/libsrc/apple2/Makefile
+++ b/libsrc/apple2/Makefile
@@ -91,6 +91,7 @@ S_OBJS=	_scrsize.o	\
         randomize.o     \
         rdkey.o         \
      	read.o          \
+     	reboot.o        \
      	revers.o	\
 	rwcommon.o	\
 	syschdir.o	\
diff --git a/libsrc/apple2/crt0.s b/libsrc/apple2/crt0.s
index b0ffabf1c..565e5ffb1 100644
--- a/libsrc/apple2/crt0.s
+++ b/libsrc/apple2/crt0.s
@@ -2,7 +2,7 @@
 ; Startup code for cc65 (Apple2 version)
 ;
 
-        .export         _exit
+        .export         _exit, done, return
         .export         __STARTUP__ : absolute = 1      ; Mark as startup
         .import         zerobss
         .import    	initlib, donelib
@@ -165,10 +165,6 @@ init:   ldx     #zpspace-1
         bne     basic
         
         ; Check ProDOS system bit map
-        lda     $BF58           ; protection for pages $00 - $07
-        and     #%11110000      ; ignore protection of text pages
-        cmp     #%11000000      ; only zero and stack pages are protected
-        bne     basic
         lda     $BF6F           ; protection for pages $B8 - $BF
         cmp     #%00000001      ; exactly system global page is protected
         bne     basic
@@ -272,7 +268,7 @@ reset:  stx     SOFTEV
         sta     SOFTEV+1
         eor     #$A5
         sta     PWREDUP
-        rts
+return: rts
 
         ; Quit to ProDOS dispatcher
 quit:   jsr     $BF00           ; MLI call entry point
diff --git a/libsrc/apple2/reboot.s b/libsrc/apple2/reboot.s
new file mode 100644
index 000000000..9986371cf
--- /dev/null
+++ b/libsrc/apple2/reboot.s
@@ -0,0 +1,21 @@
+;
+; Oliver Schmidt, 14.09.2009
+;
+; void rebootafterexit (void);
+;
+
+        .constructor	initreboot
+        .export	        _rebootafterexit
+        .import         done, return
+
+_rebootafterexit := return
+
+        .segment        "INIT"
+
+initreboot:
+        ; Quit to PWRUP
+        lda     #<$FAA6
+        ldx     #>$FAA6
+        sta     done
+        stx     done+1
+        rts
diff --git a/libsrc/apple2enh/Makefile b/libsrc/apple2enh/Makefile
index deb046d87..11609b2ca 100644
--- a/libsrc/apple2enh/Makefile
+++ b/libsrc/apple2enh/Makefile
@@ -91,6 +91,7 @@ S_OBJS=	_scrsize.o	\
         randomize.o     \
         rdkey.o         \
      	read.o          \
+     	reboot.o        \
      	revers.o	\
 	rwcommon.o	\
 	syschdir.o	\
-- 
2.39.5
]