From d91fe6b87791d40de6c164a0c6d31e4f3757b30b Mon Sep 17 00:00:00 2001
From: "ol.sc" 
Date: Thu, 10 Sep 2009 09:59:26 +0000
Subject: [PATCH] ProDOS 8 requires an page-aligned 1kB IO-buffer for every
 open file. The implementation using posix_memalign() is universally correct
 but quite expensive. There I added support for overriding this implementation
 with maybe limited (i.e. to one open file) but cheaper custom
 implementations.
git-svn-id: svn://svn.cc65.org/cc65/trunk@4142 b7a2c559-68d2-44c3-8de9-860c34a00d81
---
 libsrc/apple2/Makefile    |  1 +
 libsrc/apple2/iobuf.s     | 15 +++++++++++++++
 libsrc/apple2/mli.s       | 10 ++++++++--
 libsrc/apple2/open.s      |  6 +++---
 libsrc/apple2enh/Makefile |  1 +
 5 files changed, 28 insertions(+), 5 deletions(-)
 create mode 100644 libsrc/apple2/iobuf.s
diff --git a/libsrc/apple2/Makefile b/libsrc/apple2/Makefile
index b4e449f05..70ab08df1 100644
--- a/libsrc/apple2/Makefile
+++ b/libsrc/apple2/Makefile
@@ -78,6 +78,7 @@ S_OBJS=	_scrsize.o	\
 	gotoy.o		\
         home.o          \
 	initcwd.o	\
+	iobuf.o         \
         joy_stddrv.o    \
      	kbhit.o		\
         mainargs.o      \
diff --git a/libsrc/apple2/iobuf.s b/libsrc/apple2/iobuf.s
new file mode 100644
index 000000000..fc412c241
--- /dev/null
+++ b/libsrc/apple2/iobuf.s
@@ -0,0 +1,15 @@
+;
+; Oliver Schmidt, 10.9.2009
+;
+; Default ProDOS 8 I/O buffer management
+; 
+
+        .export		iobuf_alloc, iobuf_free
+        .export		iobuf_init,  iobuf_done
+        .import		iobuf_nop
+        .import		_posix_memalign, _free
+
+iobuf_alloc := _posix_memalign
+iobuf_free  := _free
+iobuf_init  := iobuf_nop
+iobuf_done  := iobuf_nop
diff --git a/libsrc/apple2/mli.s b/libsrc/apple2/mli.s
index b5c2ad433..48e09e718 100644
--- a/libsrc/apple2/mli.s
+++ b/libsrc/apple2/mli.s
@@ -5,6 +5,8 @@
 ;
 
         .import         __dos_type
+        .import         iobuf_init, iobuf_done
+        .export         iobuf_nop
 
         .include        "mli.inc"
 
@@ -24,12 +26,16 @@ callmli:
         beq     oserr
 
         ; Call MLI and return
+        jsr     iobuf_init
         jsr     ENTRY
 call:   .byte   $00
         .addr   mliparam
-        rts
+        jmp     iobuf_done
 
         ; Load oserror code and return
 oserr:  lda     #$01		; "Invalid MLI function code number"
         sec
-        rts
\ No newline at end of file
+        ; Fall through
+
+iobuf_nop:
+        rts
diff --git a/libsrc/apple2/open.s b/libsrc/apple2/open.s
index 635b91e90..aef0f56e8 100644
--- a/libsrc/apple2/open.s
+++ b/libsrc/apple2/open.s
@@ -12,7 +12,7 @@
 
         .import		pushname, popname
         .import 	errnoexit, oserrexit
-        .import		_posix_memalign, _free
+        .import		iobuf_alloc, iobuf_free
         .import 	addysp, incsp4, incaxy, pushax, popax
 
         .include	"zeropage.inc"
@@ -70,7 +70,7 @@ found:  tya
         ldx	#>$0100
         jsr	pushax		; Preserves A
         ldx	#>$0400
-        jsr	_posix_memalign
+        jsr	iobuf_alloc
 	tay			; Save errno code
 
         ; Restore fdtab slot
@@ -206,7 +206,7 @@ freebuffer:
         ; Free I/O buffer
         lda	#$00
         ldx	fdtab + FD::BUFFER+1,y
-        jmp	_free
+        jmp	iobuf_free
 
 closeallfiles:
         ; All open files
diff --git a/libsrc/apple2enh/Makefile b/libsrc/apple2enh/Makefile
index d030d2436..88e88c6b7 100644
--- a/libsrc/apple2enh/Makefile
+++ b/libsrc/apple2enh/Makefile
@@ -78,6 +78,7 @@ S_OBJS=	_scrsize.o	\
 	gotoy.o		\
         home.o          \
 	initcwd.o	\
+	iobuf.o         \
         joy_stddrv.o    \
      	kbhit.o		\
         mainargs.o      \
-- 
2.39.5