    NAM DSKDR
***********************************************************
* Extracted from the book:
* "CP/68, an M6800 Operating System" by Jack E. Hemenway
*
* Scott LaBombard
* labomb@rochester.rr.com
* 02/29/2019
***********************************************************
*   
    ENT .RDSEC
    ENT .WTSEC
    ENT _INTDK
*
*   SINGLE SECTOR READ AND WRITE ROUTINES
*   FOR THE PERCOM DISK DRIVE SYSTEM
*
*   COPYRIGHT 1978 BY HEMENWAY ASSOCIATES INC
*   BOSTON MASS. 02111
*   ALL RIGHTS RESERVED
*
*
*
* FCB EQU'S
*
FCBEQT EQU 0        EQUIPMENT TABLE ADDRESS
FCBGDT EQU 2        GENERIC DEVICE TYPE
FCBSTA EQU 5        STATUS
FCBDTT EQU 6        DATA TRANSFER TYPE
FCBDBA EQU 7        DATA BUFFER ADDRESS
*
FCBDRV EQU 9        DRIVE NUMBER
FCBTRK EQU 10       TRACK NUMBER
FCBSCT EQU 11       SECTOR NUMBER
FCBFWD EQU 12       FWD LINK TRACK/SECTOR
FCBBAK EQU 14       BACK LINK TRACK/SECTOR
*
* BASE PAGE EQUATES
*
DRV    EQU 0        DESIRED TRACK (MS 2 BITS)
TRK    EQU 1        DESIRED TRACK
SCTR   EQU 2        DESIRED SECTOR
BAKLNK EQU 3        BACKWARD LINK
FWDLNK EQU 5        FORWARD LINK
BYTCNT EQU 7        SECTOR BYTE COUNT
ADDRES EQU 8        DATA ADDRESS VECTOR
TA     EQU $14      CONTINUATION ADDRESS
TW     EQU $16      ALTERNATE TARGET ADDRESS
*
* VECTORS INTO DISK DRIVERS
*
RDSECX EQU $C00C    READ A SECTOR
WTSECX EQU $C00F    WRITE A SECTOR
INITRK EQU $C027    INITIALIZE DRIVES
*
*
* INITIALIZE DRIVES
*
_INTDK JMP INITRK
*
*
*
* READ A SINGLE SECTOR
* A,B=FCBADR
* 
.RDSEC TABX         X:=FCBADR
    LDX FCBDBA,X
    STX TW          INIT DATA BUFFER ADDRESS
    TABX            X:=FCBADR
    BSR GETDTS      GET DRIVE,TRACK AND SECTOR
    PSHX            SAVE FCBADR
    JSR RDSECX      READ A SECTOR
    PULX            RESTORE FCBADR
    BCC RDSEC1      OK
*
* RE-SET ERROR CODES TO CP/68 VALUES
*
    CMP A #0        0 BECOMES 10
    BNE *+6
*
    LDA A #10
    BRA RDSEC0
*
    CMP A #1        1 BECOMES 18
    BNE *+6
*
    LDA A #18
    BRA RDSEC0
*
    LDA A #5        OTHERS BECOME 5
    BRA RDSEC0
*
RDSEC1 CLR A        NO ERRORS=0
*
RDSEC0 STA A FCBSTA,X  SET ERROR CODE
    RTS
*
* INIT BASE PAGE DRIVE,TRACK AND SECTOR
*
GETDTS LDA A FCBDRV,X  GET DRIVE #
    INC A           OFFSET +1
    ASL A           SHIFT TO BITS 7,6
    ASL A
    ASL A
    ASL A
    ASL A
    ASL A
    STA A DRV
    LDA A FCBTRK,X  GET TRACK
    STA A TRK
    LDA A FCBSCT,X  GET SECTOR
    DEC A           OFFSET -1
    STA A SCTR
    RTS
*
*
*
* WRITE A SINGLE SECTOR
* A,B=FCBADR
*
.WTSEC TABX         X:=FCBADR
    LDX FCBDBA,X
    STX TA          INIT DATA BUFFER ADDRESS
    TABX            X:=FCBADR
    BSR GETDTS      GET DRIVE,TRACK AND SECTOR
    CLR BYTCNT      SET FOR FULL SECTOR
* 
    LDA A #$FF
    STA A ADDRES
    STA A ADDRES+1
* 
    LDA A FCBBAK,X
    STA A BAKLNK
    LDA A FCBBAK+1
    STA A BAKLNK+1
*   
    PSHX            SAVE FCBADR
    LDX FCBFWD,X    GET FWD LINK
    STX FWDLNK
* 
    JSR WTSECX      WRITE A SECTOR
* 
    PULX            X:=RCADR
    BCC WTSEC1      OK
*
* RE-SET ERROR CODES TO CP/68 VALUES
* 
    CMP A #0        0 BECOMES 10
    BNE *+6
* 
    LDA A #10
    BRA WTSEC0
* 
    CMP A #1        1 BECOMES 18
    BNE *+6
*   
    LDA A #18
    BRA WTSEC0
*   
    LDA A #5        OTHERS BECOME 5
    BRA WTSEC0
*
WTSEC1 CLR A        NO ERRORS=0
*
WTSEC0 STA A FCBSTA,X
    RTS
    END
