
*
* Disk boot code
*

*
*   disk bootstrap program
*

btsec    equ     1                   sector number of ipl record
seclen     equ     512                 disk sector length
btadr    equ     usrseg<<12         logical bootstrap address
realad    equ     (ramap<<4)&$F000   physical address of bootstrap
*
*   DMAF2 1791 disk controller definitions
*
cmmnd    equ     $E020               disk controller command register
status     equ     $E020               disk controller status register
track      equ     $E021               disk controller track register
sector     equ     $E022               disk controller sector register
data       equ     $E023               disk controller data register
*
*   DMAF2 1791 disk controller command definitions
*
frcint   equ     %11011000           force interrupt command
restore    equ     %00001001           restore command
read       equ     %10001100           single sector read command
*
*   DMAF2 1791 disk controller status bits
*
drdy       equ     %10000000           1 => drive not ready
skerr    equ     %00010000           1 => seek error
nrf        equ     %00001000           1 => no record found error
crc        equ     %00000100           1 => crc error
lsd        equ     %00000010           1 => lost data error
dbusy      equ     %00000001           1 => controller busy
*
*   DMAF2 drive select latch definitions
*
drive      equ     $E024               drive select latch (inverted)

dnsty    equ     %00100000           1 => double density selected
select     equ     %00000001           1 => drive zero selected
*
*   DMAF2 address select latch definitions
*
latch      equ     $E040               extended address latch

dmirq        equ     %00010000           1 => 1791 irq disabled
extaddr    equ     %00001111           extended address select lines
*
*   DMAF2 6844 DMA controller definitions
*
address    equ     $E000               DMA address register (zero)
dmcnt      equ     $E002               DMA count register (zero)
channel    equ     $E010               DMA channel control register
priority   equ     $E014               DMA priority control register
interrupt  equ     $E015               DMA interrupt control register
chain      equ     $E016               DMA data chain register
*
*   reset the stack and set up direct addressing
*
loadip           seti     irq,firq            disable irq and firq
 lda #8 set retry count
 ldb #!(dnsty+select) set density and drive select
           pshs    d,dp                 save the direct page register
           lda     #address>>8         load the msp of the address
           tfr     a,dp               set direct addressing
           setdp   $e0             and tell the assembler
*
*   quiesce the dma (in case it is active)
*
trybt      lda     #!0                 clear all relevant DMA registers
           sta     priority            stuff priority register
           sta     channel             clear channel control register
           sta     chain               and clear data chain register
           tst     channel             then read channel control
           tst     interrupt           and interrupt control registers
*
*   disable irq from the 1791 controller
*
           lda     #dmirq                disable irq requests
           sta     latch               stuff into DMAF2 address latch
 lda 1,s get density
           sta     drive               stuff into drive select latch
           lda     #frcint           load a force interrupt command
           bsr     diskcmd             send the command to te disk
*
*   read 1791 status latch and see if drive comes up ready
*
           ldy     #14000              allow up to 3 seconds for drive ready
dmchk      bsr     delay               delay 174 uSeconds
           lda     status              check 1791 drive ready status bit
           bpl     dorest              if ready, go do the restore
           leay    -1,y                decrement delay count
           bne     dmchk               loop if not so
           bra     loadxit
*
*   restore the drive and wait for it to complete
*
dorest     lda     #restore            load a restore command
           bsr     diskcmd             and attempt to restore the drive
waitrst    lda     status              pick up 1791 status register
           bita    #dbusy              check for the busy bit
           bne     waitrst             loop until busy goes away
           bita    #skerr            test for seek error
           bne     loadxit
*
*   set up the dma count and address registers
*
           lda     #dmirq                disable irq from 1791
           sta     latch               set the address latch
           ldd     #!realad           load real boot address
           std     address             stuff into DMA address register
           ldd     #!seclen            load sector length
           std     dmcnt               stuff into DMA count register
*
*   set up the dma mode and control registers
*
           ldd     #!1                 set up steal mode
           sta     channel             store into channel control register
           stb     priority            stuff into priority register
*
*   now fire up 1791 controller to do a single sector read
*
           ldd     #btsec            load boot sector number
           std     track               set track number to zero
           lda     #read               load single sector read
           bsr     diskcmd             and fire up the controller
*
*   wait for the command to complete
*
           ldx     #50000              allow up to 500 mS delay
wfboot     tst     channel             test channel control register
           bpl     checkit             if positive, boot is complete
           leax    -1,x                decrement the wait counter
           bne     wfboot              loop waiting for boot to complete
           lda     status              pick up status byte
*
*   load has failed - first shut down the DMA
*
loadxit    pshs    a                   push the offending status
           lda     #!0                 turn off all channels
           sta     priority            by clearing the priority register
           tst     channel             read channel register then
           tst     interrupt           clear any interrupt indication
*
*   then shut down the 1791 and exit
*
           lda     #frcint           force an interrupt
           sta     cmmnd             write into the command register
 dec 1,s dec retry count
 beq rlex
 puls a get status
 lda 1,s get density
 eora #dnsty toggle density
 sta 1,s set on stack
 jmp trybt retry
rlex       lda     #!dnsty           deselect all drives
           sta     drive               to turn off the lights
           tsta                        set zero false to indicate failure
 puls a clean stack
 puls u,dp,pc
*
*   boot has completed - check for any sicknesses along the way
*
checkit    lda     status              pick up disk controller status
           bita    #nrf+crc+lsd+drdy   test for error conditions
           bne     loadxit             exit if error on boot sector
 lda 1,s get density
           sta     drive               for the internal boot
           ldx     #btadr            load bootstrap address
           clra                        set equal to show boot complete
 puls d,dp,pc
*
*   send a command to the 1791 and wait for it to respond
*
diskcmd    sta     cmmnd             store the disk command
delay      lda     #32                 set up a delay counter
del        deca                        decrement the delay count
           bne     del                 hang in there for the count
           rts

           setdp   0

