           ttl     System Configurator
*
.   CPU and Memory configurator
.
config:    proc

           aif     VERS!=3
mapmask    equ     $0F                 segment map mask
           aif     VERS==3
mapmask    equ     $00
           aif
testpat    equ     $99AA               memory test pattern
testloc    equ     (tfr_seg<<12)+$F0   segment test position
timer      equ     $E090               SBOX timer address
termdata   equ     $E005               terminal data register
useres     equ     (ramap>>8)^mapmask  user residence page
sysres     equ     (ramap&$FF)^mapmask system residence page
*
.   set up memory table pointers
.
           ldx     #testpat            set up test pattern
           ldy     #coremap+segmax     set up pointer and page table
           ldb     #segmax-1           load max page number
*
.   check a memory page to see if it responds
.
check      bsr     getpage             map in the memory page
           stx     testloc             write the test location
           cmpx    testloc             compare to see if memory is there
           bne     nomemory            if not, bypass the check
           pshs    b                   save the page number on the stack
*
.   check all lower pages for multiple responses
.
lower      bsr     getpage             map in the next page
           stb     testloc+1           store into the test location
           subb    #1                  decrement by one
           bcc     lower               loop until all done
           puls    b                   get back the page number
           bsr     getpage             reselect the page
           cmpb    testloc+1           see if memory is there
           beq     gotmem              if so, fine
*
.   stuff the byte into the memory map table
.
nomemory   clra                        set the page number to zero
gotmem     sta     0,-y                store into the memory table
           subb    #1                  decrement the memory value
           bcc     check               continue to build the table
           leax    0,y                 get pointer to start of table
*
.   set up for system address space
.
           lda     #cpu_xmem           initially set extended memory
           sta     cputype             stuff into cpu type flag
           clr     sysres,x            clear out system resident page
           clr     useres,x            and also user resident page
           ldy     #sysmap+textseg     point to user map segment
           ldb     #6                  load length required
getblok    bsr     getnext             get next segment
           beq     memfail             if no more, we in deep shit, boy
           sta     0,y+                stuff into the system map
           decb                        count off the pages
           bne     getblok
*
.   now construct the user memory map
.
           clr     corecnt             set initial core count to zero
           ldy     #coremap            point to the system core map
getcore    bsr     getnext             get next valid page
           beq     setimer             if no more, we are done
           sta     0,y+                stuff into the core map slot
           inc     corecnt             increment the map count
           bra     getcore             hang in there
setimer    sty     lastmem             store end of table pointer
*
.   rewrite the entire datbox from the system space
.
           ldx     #sysmap             point to the system map
           ldy     #datbox             then point to datbox
           ldb     #segadr             and load number of segments
move       lda     0,x+                pick up source byte
           sta     0,y+                stuff into target
           decb                        decrement byte count
           bne     move                loop until complete
*
.   initialize the timer if it is present
.
           aif     VERS!=3
           ldy     #timer              load the timer address
           lda     #1                  set timer 2 to external clock
           sta     1,y                 also select timer 1 slot
           clr     0,y                 timer on, external clock
           nop
           ldd     2,y                 get initial timer value
*
.   count off 200,000 machine cycles and check the timer
.
           ldd     2,y                 provide some delay here
           ldx     #24998              load delay count
delay      leax    -1,x                decrement the count
           bne     delay               hangeth in there
           subd    2,y                 subtract the timer value
           beq     timefail            if equal, more deep shit
           aif
*
.   attempt to figure out what kind of system we have
.
           lda     #cpu_time           indicate a timer is available
           cmpb    #16                 cleave first at 1 MHz boarder
           bhs     gotMHz              if higher, fine and dandy
           ora     #cpu_2MHz           indicate 2 MHz CPU clock
           cmpb    #11                 check for 60 cycle
           bhs     timset              if greater, then 60 cycle
           ora     #cpu_50Hz           if not, 50 cycle
           bra     timset
gotMHz     cmpb    #22                 check for 60 cycle
           bhi     timset              if greater, then 60 cycle
           ora     #cpu_50Hz           if not, 50 cycle
timset     ora     cputype             or into the CPU type flag
           sta     cputype             stuff back in CPU type flag
           rts
*
.   map in a memory page
.
getpage
**         cmpb    #IOPAGE             I/O PAGE???
**         bne     gpio
**         subb    #1                  drop by one to skip i/o
gpio       tfr     b,a                 get page number
           aif     VERS!=3
           eora    #mapmask            invert the page mask
           aif
           sta     datbox+tfr_seg      stuff into the dat box
           rts
*
.   get next valid memory segment
.
getnext    cmpx    #coremap+segmax     see if at end of map
           beq     getsex              if so, we are screwed
           lda     0,x+                then fetch the page number
           beq     getnext             if zero, continue looking
getsex     rts
*
.   UniFlex configuration Failures
.
memfail    bsr     Panic               give up right now
           fcb     "Not Enough Memory",etx
timefail   bsr     Panic               also give up right now
           fcb     "Timer Not Operational",etx

           end
