; This routine selects a serial device, and aborts if the device is not
; present. This code is in Buddy format, and is useful for programs
; supplying multi-drive support.
; By Todd S. Elliott, 1997. Feel free to use without attribution.

temp   = $fa  ; one byte variable- contains the new user selected serial
              ; device. Usually is in range from 8 to 30.
dv     = $ba  ; Current device used by the Kernal disk/file routines
status = $90  ; The status byte used by Kernal disk/tape routines
secnd  = $ff93; Assigns a secondary address, low level disk routine
unlstn = $ffae; Severs the assigned device from further serial
              ; serial communications
listen = $ffb1; Commands the assigned device to accept further serial
              ; serial communications

change'serial'device =*
  lda dv
  pha; save original device number temporarily
  lda temp
  sta dv; create a new device number
  lda #$00
  sta status; clear the status register
  lda dv; get the current device number
  jsr listen; opens the device for listening
  lda #$0f; Secondary address (sa)
  ora #$f0; opens it
  jsr secnd; opens the channel with sa of 15
  lda status; check the status byte
  bpl +; branch if there is a device present

; obviously, some kind of error- restore original device
  jsr unlstn; severs the serial bus controls
  pla
  sta dv; restore the original serial device
  sec; sets the carry flag to indicate error
  rts; exits out with the restored device
+ jsr unlstn; severs the serial bus control
  pla
  clc; clears the carry flag to indicate no error
  rts; exits out with a successful device switch

