           ttl     WD1000 Bootstrap
*
.   bootstrap for WD1000 UniFlex
.
wdboot:    proc

bootsec    equ     1                   sector number of bootstrap
bootcyl    equ     0                   cylinder number of bootstrap
bootadr    equ     userseg<<12         disk boot address

wdxx       equ     $E8E0               address of WD1000 controller

wddch      equ     %00010000           1 => WD DMA Channel Select
dmareq     equ     %00000010           1 => WD DMA Channel Request

*
.   WD-1000 Controller Board Registers
.
wd_dta     equ     wdxx+$000           Data Register
wd_err     equ     wdxx+$001           Error Register
wd_sec     equ     wdxx+$003           Sector Number
wd_cyl     equ     wdxx+$004           Cylinder Number (LSP, MSP)
wd_sdh     equ     wdxx+$006           Side, Drive, Head Select
wd_cmd     equ     wdxx+$007           Command Register
wd_sta     equ     wdxx+$007           Status Register


*
.   stuff contained in VIA
.
via29      equ     $E8A0               VIA containing drive sel, etc
ORB        equ     via29+0             output register B
ORA        equ     via29+1             output register A
DDRB       equ     via29+2             data dir reg A side
DDRA       equ     via29+3             data dir reg B side
PCR        equ     via29+12            periph control register
IFR        equ     via29+13            interrupt flag register
IER        equ     via29+14            interrupt enable register
drive_via  equ     ORA                 U29 via address

wd_res     equ     $E820               WDxx board reset address (read)


drvsel     equ     %10100000           Drive Zero, Head Zero select, ECC on
liteoff    equ     %00111000           Drive Three, Head Zero select
restore    equ     %00010110           Restore w/ 3.0 ms step rate
read       equ     %00100000           Read Sector, Programmed I/O Mode

*
.   set up direct addressing for the WD board
.
           pshs    dpr
           lda     #wdxx>>8            load DPR for short addressing
           tfr     a,dpr
           setdp   wdxx
*
.  initialize the VIA for this type of thing
.
           lda     #%11111111          set a side direction
           sta     DDRA
           lda     #%11000111          deselect floppy drives
           sta     ORA
           lda     #%00000000          set b side for all inputs
           sta     DDRB
           lda     #%11110101          set up CA CB control lines
           sta     PCR
           lda     #%01111111          do not enable VIA interrupts
           sta     IER
           sta     IFR                 clear any pending stat in IFR

*
.   reset WD-1000, select drive zero, wait for READY or TIMEOUT
.
           tst     wd_res              reset the WDxx board
           nop
           nop                         provide a microsecond or so delay
           lda     #drvsel             load up the drive select
           sta     wd_sdh              stuff into SDH register
           bsr     wdwait              wait for WD to respond
           bmi     loadfail            if busy, still sicko
           bita    #%01000000          check for drive ready
           beq     loadfail            if not, go fail load
*
.   issue a restore to the WD-1000 and wait for complete
.
dorest     lda     #restore
           bsr     wdcmd               send restore command to disk
           bmi     loadfail
           bita    #%00000001          check for error condition
           bne     wderror             if so, go read error and exit
*
.   set cylinder and sector registers, and read the boot block
.
           ldd     #bootcyl            load cylinder number of boot
           std     wd_cyl
           lda     #bootsec            load sector number of boot block
           sta     wd_sec
           lda     #read
           bsr     wdcmd               issue command to WD controller
           bmi     loadfail
           bita    #%00000001          check for an error condition
           bne     wderror
*
.   load the sector into the boot address
.
           bita    #%00001000          check for data request
           beq     loadfail
           ldx     #bootadr            point to boot address
           clrb
doload     lda     wd_dta              load data from WD register
           sta     0,x+
           lda     wd_dta              get second byte from WD-1000
           sta     0,x+
           decb                        decrement data word count
           bne     doload
           lda     #%01111111
           sta     IFR                 clear any pending stat in IFR
*
.   check to see the bootstrap data is valid
.
           clra                        set status byte to zero
           ldx     #bootadr            point back to bootstrap addr
           ldb     0,x                 pick up first byte of boot
           cmpb    #$16                check for long branch instr
           ret     dpr
*
.   boot has failed, turn off light and exit
.
wderror    lda     wd_err              load offending error status
           ora     #%00001000          set un-ambiguous condition flag
loadfail   clr     wd_res              reset the WD-1000 board
           lda     #%01111111
           sta     IFR                 clear any pending stat in IFR
           ldb     #liteoff            turn the drive light off
           stb     wd_sdh              stuff into drive register
           ret     dpr
*
.   wait for the WD to indicate not busy
.
wdcmd      sta     wd_cmd              stuff the command register
wdwait     ldy     #15220              load up the delay counter
wrwait     lda     wd_sta              read WD status register
           bpl     wrexit              exit if no longer busy
           clra
wrdly      inca                        increment delay counter
           bpl     wrdly
wrcnt      leay    -1,y                decrement delay counter
           bne     wrwait              loop if not ready
           scc     n                   exit with minus condition
wrexit     rts

           setdp   0

           end
