From 987c3066a3b76e84843f41d6bd7e602b6167af19 Mon Sep 17 00:00:00 2001
From: "ol.sc" <ol.sc@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Date: Sun, 7 Oct 2012 19:18:56 +0000
Subject: [PATCH] Introduced getdevicedir() - with implementation for the CBMs.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5840 b7a2c559-68d2-44c3-8de9-860c34a00d81
---
 include/device.h       |  2 +
 libsrc/cbm/Makefile    |  2 +
 libsrc/cbm/devicedir.s | 97 ++++++++++++++++++++++++++++++++++++++++++
 libsrc/cbm/devicestr.s | 35 +++++++++++++++
 libsrc/cbm/initcwd.s   | 27 +++---------
 5 files changed, 143 insertions(+), 20 deletions(-)
 create mode 100644 libsrc/cbm/devicedir.s
 create mode 100644 libsrc/cbm/devicestr.s

diff --git a/include/device.h b/include/device.h
index 37323e637..9d525e15a 100644
--- a/include/device.h
+++ b/include/device.h
@@ -55,6 +55,8 @@ unsigned char getfirstdevice (void);
 
 unsigned char __fastcall__ getnextdevice (unsigned char device);
 
+char* __fastcall__ getdevicedir (unsigned char device, char* buf, size_t size);
+
 
 
 /* End of device.h */
diff --git a/libsrc/cbm/Makefile b/libsrc/cbm/Makefile
index 1b1a6a4cb..fbcaeeeed 100644
--- a/libsrc/cbm/Makefile
+++ b/libsrc/cbm/Makefile
@@ -69,6 +69,8 @@ S_OBJS =	c_acptr.o	\
                 closedir.o      \
 		ctype.o  	\
 		cvline.o 	\
+                devicedir.o     \
+                devicestr.o     \
                 dir.o           \
                 diskcmd.o       \
                 exehdr.o        \
diff --git a/libsrc/cbm/devicedir.s b/libsrc/cbm/devicedir.s
new file mode 100644
index 000000000..93e1845c0
--- /dev/null
+++ b/libsrc/cbm/devicedir.s
@@ -0,0 +1,97 @@
+;
+; Oliver Schmidt, 2012-09-10
+;
+; char* __fastcall__ getdevicedir (unsigned char device, char* buf, size_t size);
+;
+
+        .export         _getdevicedir
+
+        .import         opencmdchannel, closecmdchannel
+        .import         writefndiskcmd, readdiskerror
+        .import         isdisk, fnunit, fncmd, devicestr
+        .import         popa, popax
+        .importzp       ptr2, ptr3
+
+        .include        "errno.inc"
+
+;------------------------------------------------------------------------------
+; _getdevicedir
+
+.proc   _getdevicedir
+
+; Save size
+
+        sta     ptr3
+        stx     ptr3+1
+
+; Save buf
+
+        jsr     popax
+        sta     ptr2
+        stx     ptr2+1
+
+; Save device
+
+        jsr     popa
+        sta     fnunit
+
+; Check for disk device
+
+        tax
+        jsr     isdisk
+        bcs     erange
+
+; Open channel
+
+        jsr     opencmdchannel
+        bne     oserr
+
+; Write command
+
+        lda     #'i'            ; Init command
+        sta     fncmd
+        jsr     writefndiskcmd
+        bne     close
+
+; Read error
+
+        ldx     fnunit
+        jsr     readdiskerror
+
+; Close channel
+
+close:  pha
+        ldx     fnunit
+        jsr     closecmdchannel
+        pla
+        bne     oserr
+
+; Check for sufficient buf size
+
+        lda     ptr3+1
+        bne     okay            ; Buf >= 256
+        lda     ptr3
+        cmp     #3
+        bcs     okay            ; Buf >= 3
+
+erange: lda     #<ERANGE
+        bne     errno           ; Branch always
+
+oserr:  jsr     __osmaperrno
+
+errno:  jsr     __seterrno      ; Returns 0 in A
+        tax                     ; Return NULL
+        rts
+
+; Copy device string representation into buf
+
+okay:   lda     fnunit
+        jsr     devicestr
+
+; Success, return buf
+
+        lda     ptr2
+        ldx     ptr2+1
+        rts
+
+.endproc
diff --git a/libsrc/cbm/devicestr.s b/libsrc/cbm/devicestr.s
new file mode 100644
index 000000000..15b4b6c4a
--- /dev/null
+++ b/libsrc/cbm/devicestr.s
@@ -0,0 +1,35 @@
+;
+; Stefan Haubenthal, 2005-06-08
+;
+; CBM device to string
+;
+
+	.export		devicestr
+	.import		pusha0, tosudiva0
+	.importzp	sreg, ptr1, ptr2
+
+	.macpack	generic
+
+;------------------------------------------------------------------------------
+; Convert unit number in A into string representation pointed to by ptr2.
+
+.proc	devicestr
+
+	jsr	pusha0
+	lda	#10
+	jsr	tosudiva0
+	ldy	#0
+	lda	sreg
+	beq	:+		; >=10
+	add	#'0'
+	sta	(ptr2),y
+	iny
+:	lda	ptr1		; rem
+	add	#'0'
+	sta	(ptr2),y
+	iny
+	lda	#0
+	sta	(ptr2),y
+	rts
+
+.endproc
diff --git a/libsrc/cbm/initcwd.s b/libsrc/cbm/initcwd.s
index 2df6dba85..5f43bc867 100644
--- a/libsrc/cbm/initcwd.s
+++ b/libsrc/cbm/initcwd.s
@@ -5,31 +5,18 @@
 ;
 
 	.export		initcwd
-	.import		__curunit, __cwd
-	.import		pusha0, tosudiva0
-	.importzp	sreg, ptr1
-
-	.macpack	generic
+	.import		__curunit, __cwd, devicestr
+	.importzp	ptr2
 
 	.segment	"INIT"
 
 .proc	initcwd
 
+	lda	#<__cwd
+	ldx	#>__cwd
+	sta	ptr2
+	stx	ptr2+1
 	lda	__curunit
-	jsr	pusha0
-	lda	#10
-	jsr	tosudiva0
-	ldx	#0
-	lda	sreg
-	beq	:+		; >=10
-	add	#'0'
-	sta	__cwd
-	inx
-:	lda	ptr1		; rem
-	add	#'0'
-	sta	__cwd,x
-	lda	#0
-	sta	__cwd+1,x
-	rts
+	jmp	devicestr
 
 .endproc