devicedir.o     \
                dir.o           \
                diskcmd.o       \
+               diskinit.o      \
                exehdr.o        \
                filedes.o       \
                filename.o      \
 
 ;
 
         .export         _getdevicedir
-        .import         opencmdchannel, closecmdchannel
-        .import         writefndiskcmd, readdiskerror
-        .import         isdisk, fnunit, fncmd, devicestr
+        .import         diskinit, devicestr, fnunit
         .import         popa, popax
         .importzp       ptr2, ptr3
 
         sta     ptr2
         stx     ptr2+1
 
-; Save device
+; Check device readiness
 
         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
+        jsr     diskinit
+        beq     size
+        jsr     __mappederrno
+        bne     fail            ; Branch always
 
 ; Check for sufficient buf size
 
-        lda     ptr3+1
+size:   lda     ptr3+1
         bne     okay            ; Buf >= 256
         lda     ptr3
         cmp     #3
         bcs     okay            ; Buf >= 3
-
-erange: lda     #<ERANGE
+        lda     #<ERANGE
         jsr     __directerrno
-        bne     fail            ; Branch always
-
-oserr:  jsr     __mappederrno
-
 fail:   lda     #0              ; Return NULL
         tax
         rts
 
 ; Copy device string representation into buf
 
-okay:   lda     fnunit
+okay:   lda     fnunit          ; Set by diskinit
         jsr     devicestr       ; Returns 0 in A
         sta     __oserror       ; Clear _oserror
 
 
--- /dev/null
+;
+; Oliver Schmidt, 2012-10-17
+;
+
+        .export         diskinit
+        .import         opencmdchannel, closecmdchannel
+        .import         writefndiskcmd, readdiskerror
+        .import         isdisk, fnunit, fncmd
+
+;------------------------------------------------------------------------------
+; diskinit
+
+.proc   diskinit
+
+; Save device
+
+        sta     fnunit
+
+; Check for disk device
+
+        tax
+        jsr     isdisk
+        bcc     open
+        lda     #9              ; "Ilegal device"
+        rts
+
+; Open channel
+
+open:   jsr     opencmdchannel
+        bne     done
+
+; 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
+
+done:   rts
+
+.endproc
 
 ;
 
         .export         __syschdir
-        .import         curunit, initcwd
+        .import         diskinit, fnunit, curunit, initcwd
         .importzp       ptr1, tmp1, tmp2
 
 ;--------------------------------------------------------------------------
 
         iny
         lda     (ptr1),y
-        beq     done
+        beq     init
         jsr     getdigit
         bcs     err
         stx     tmp1            ; First digit
         lda     (ptr1),y
         bne     err
 
+; Check device readiness
+
+init:   txa
+        jsr     diskinit
+        bne     done
+
 ; Success, update cwd
 
-done:   stx     curunit
+        lda     fnunit          ; Set by diskinit
+        sta     curunit
         jmp     initcwd         ; Returns with A = 0
 
+; Return with error in A
+
 err:    lda     #9              ; "Ilegal device"
-        rts
+done:   rts
 
 .endproc