 sttl System Configuration
 pag
*
* Configure the system
*

* equates

tstpat equ $99AA memory test pattern
tstloc equ (tfrseg<<12)+$f0 test position
useres equ $0001 user resident page
sysres equ $0000 system resident page

* code starts here

config ldx #tstpat setup pattern
 ldy #cormap+segmax setup page table pointer
 ldb #segmax-1 setup max page number
check bsr getpag map in a page
 stx tstloc write test pattern
 cmpx tstloc is memory there?
 bne nomem
 pshs b save count
check2 bsr getpag map in next page
 stb tstloc+1
 subb #1 dec count by 1
 bcc check2
 puls b
 bsr getpag reselect the page
 cmpb tstloc+1 is memory there?
 beq gotmem

* here if no memory

nomem clra set page number to zero

* here for memory found

gotmem sta 0,-y store into mem table
 subb #1 dec the count
 bcc check
 leax 0,y point to start of table

* setup system address space

syssup clr sysres,x clear out system and user pages
 clr useres,x
 ldy #sysmap+txtseg point to user segment
 ldb #NSPG set length
syssu2 bsr getnxt get next segment
 beq memfal crash if no memory!
 sta 0,y+ save in map
 decb dec the count
 bne syssu2
 clr corcnt set core count to zero
 ldy #cormap point to core map
syssu4 bsr getnxt get next page
 beq syssu5 no more?
 sta 0,y+ save in table
 inc corcnt bump core counter
 bra syssu4 repeat
syssu5 sty lstmem save last mem pointer
 ldx #sysmap point to system map
 ldy #datbox point to datbox
 bsr setmap set up map
 rts return

* map in a memory page

getpag pshs b save page index
 clra set up map entry
 lslb
 rola
 std datbox+((tfrseg*2)+0)*2
 addd #1
 std datbox+((tfrseg*2)+1)*2
 puls b restore page index
 tfr b,a return page map value in A
 rts return

* get next valid memory segment

getnxt cmpx #cormap+segmax end of map?
 beq getnx2
 lda 0,x+ get next page
 beq getnxt keep looking if zero
getnx2 rts return

* No memory panic

memfal bsr panic report panic
 fcc 'Not Enough Memory!',0

*
* Set up a DAT map
*  X - UniFLEX map (compressed form)
*  Y - DATBOX pointer ($F800, $F840, etc)
*
SAVEMAPS set 0
setmap pshs u save register
 if SAVEMAPS
 tfr y,d
 subd #datbox
 lsra
 rorb
 lsra
 rorb
 addd #datcopy
 tfr d,u
 endif
 ldb #segadr get segment count
10 pshs b save counter
 clra set up map entry
 ldb ,x+
 if SAVEMAPS
 stb ,u+ put in DAT copy
 endif
 cmpb #NOMEM check for Black Hole
 bne 15f
 ora #$40 set "illegal page" bit
15 lslb
 rola
 std ,y++ map $x000-$x7FF
 addd #1
 std ,y++ map $x800-$xFFF
 puls b restore counter
 decb dec the count
 bne 10b
 puls u,pc return
