 lib environment
 sttl Map Table Handlers
 pag
 name map_tables

 global getmap,fremap,chkmap

P_MAP set 0 Make non-zero to print map allocations, etc

*
* getmap
*
* Get a memory map entry
*    X - Task table entry
*

getmap pshs x,u,y save registers
 ldb MAXMAP any maps in system?
 lbeq getma6 no - exit
 ldu maptbl get map start
 clrb set counter
getma2 ldy 0,u++ check for 0 entry
 lbeq getma4
 incb bump entry counter
 cmpb MAXMAP end of map?
 bne getma2
 clrb see if this task already in table
 ldu maptbl
00 cmpx ,u++ check for match
 beq getma4 match - use entry
 incb bump
 cmpb MAXMAP end of maps?
 bne 00b
 ldb #1 OK - find a swapped out task
 ldu maptbl search task table
 leau 2,u skip system map
getma3 ldy 0,u++ get task entry
 lda tsmode,y
 bita #TCORE task in memory?
 beq givmap
 incb bump counter
 cmpb MAXMAP
 bne getma3
 ldb #1 -- look for a map to steal
 ldu maptbl
 leau 2,u skip system map
00 ldy 0,u++ check next entry
 lda tsstat,y
 cmpa #TTERM task terminated?
 beq givmap yes - he doesn't need a map
 incb bump counter
 cmpb MAXMAP any more maps?
 bne 00b
 ldb #1 -- look for a task that doesn't need a map
 ldu maptbl
 leau 2,u skip system map
00 ldy 0,u++ check next entry
 lda tsstat,y
 cmpa #TWAIT task sleeping?
 beq givmap yes - he doesn't need a map
 incb bump counter
 cmpb MAXMAP any more maps?
 bne 00b
 ldb #1 -- look for a task that doesn't need a map
 ldu maptbl
 leau 2,u skip system map
00 ldy 0,u++ check next entry
 lda tsstat,y
 cmpa #TSLEEP task sleeping?
 beq givmap yes - he doesn't need a map
 incb bump counter
 cmpb MAXMAP any more maps?
 bne 00b
 ldb lastmap last map # allocated
 incb
 cmpb MAXMAP legal map #?
 blo 00f
 ldb #1 no - start over with 1
00 stb lastmap save stolen map #
 pshs b save map #
 lslb
 ldu maptbl get map slot
 leau b,u
 ldy ,u++ get task entry
 puls b restore map #
 if 0
 ldx #nomapm point to message
 jmp blowup goodbye!
 endif
givmap lda tsmode,y invalidate map
 ora #TNOMAP
 sta tsmode,y
getma4
 if P_MAP
 pshs d
 ldd -2,u
 std oldval
 puls d
 endif
 stx -2,u mark entry
 lda tsmode,x mark map valid
 anda #!TNOMAP
 sta tsmode,x
getma6
 if P_MAP
 pshs d,x
 ldx #00f
 jsr Pdata
 lda 1,s
 jsr Phex
 ldx #01f
 jsr Pdata
 ldd oldval
 jsr Phex2
 ldx #02f
 jsr Pdata
 ldd 2,s
 jsr Phex2
 ldx #03f
 jsr Pdata
 ldd 4+6,s
 jsr Phex2
 puls d,x
 endif
 puls x,y,u,pc return with map in b
*
lastmap fcb 1 Last map # stolen
 if P_MAP
00 fcc $d,'Allocated map ',0
01 fcc ', Old Val = ',0
02 fcc ', New Val = ',0
03 fcc ', Called at: ',0
oldval fdb 0
 endif

*
* fremap
*
* Free the current map number.
*    B - Map # being released
*    X - Task table entry of task
*

fremap pshs d,x
 lda MAXMAP any maps in system
 beq 99f no - exit
 if P_MAP
 pshs d,x
 ldx #00f
 jsr Pdata
 lda 1,s
 jsr Phex
 ldx #01f
 jsr Pdata
 ldd 2,s
 jsr Phex2
 puls d,x
 endif
 ldx maptbl
 lslb word index
 leax b,x
 ldd 0,x check entry
 if P_MAP
 pshs d,x
 ldx #10f
 jsr Pdata
 ldd 0,s
 jsr Phex2
 puls d,x
 endif
 cmpd 2,s
 bne 99f
 clr ,x+
 clr ,x+ reset entry
99 puls d,x,pc return
 if P_MAP
00 fcc $d,'Release map ',0
01 fcc ', Task = ',0
10 fcc ', Map = ',0
*
Phex2 pshs d
 jsr Phex
 lda 1,s
 jsr Phex
 puls d,pc
 endif

*
* chkmap - Check if task has a map allocated
*
chkmap pshs d,x
 ldb MAXMAP any maps in system?
 beq 99f no - exit
 ldx utask get task pointer
 cmpx tsktab task 0 needs no map
 beq 99f
 lda tsmode,x map valid?
 bita #TNOMAP
 bne 01f no - must allocate one
 ldb umapno get current map #
 bne 99f
01 lbsr getmap allocate a map
 stb umapno
 stb urelod
99 puls d,x,pc restore registers

 if P_MAP
*
* Print memory map (and DAT copy)
*
DATcopy equ $E480 DAT copy in CPU board RAM
 global dmpmap
dmpmap pshs d,x,y,u
 ldx #00f
 jsr Pdata
 ldu utask
 lda umapno
 jsr Phex
 ldx #01f
 jsr Pdata
 lda tsmode,u
 jsr Phex
 ldx #02f
 jsr Pdata
 lda tsstat,u
 jsr Phex
 ldx #03f
 jsr Pdata
 lda utask
 jsr Phex
 lda utask+1
 jsr Phex
 ldx #10f
 jsr Pdata
 ldu #umem
 lbsr prtmap
 ldx #11f
 jsr Pdata
 ldu #DATcopy
 lda umapno
 ldb #16
 mul
 leau d,u
 lbsr prtmap
 ldx #12f
 jsr Pdata
 ldu maptbl
 ldb MAXMAP
50 ldy ,u++
 beq 55f
 ldx #13f
 jsr Pdata
 pshs y
 lda 0,s+
 jsr Phex
 lda ,s+
 jsr Phex
 ldx #14f
 jsr Pdata
 lda tsstat,y
 jsr Phex
 ldx #15f
 jsr Pdata
 lda tsmode,y
 jsr Phex
 bra 60f
55 ldx #16f
 jsr Pdata
60 decb
 bne 50b
99 puls d,x,y,u,pc
*
00 fcc $d,'Task abort (Core dump)'
 fcc ', Map = ',0
01 fcc ', Mode = ',0
02 fcc ', State = ',0
03 fcc ', Task = ',0
10 fcc $d,'Task Map: ',0
11 fcc $d,'DAT Map: ',0
12 fcc $d,'Map Tables:'
 fcc $d,'  Task    State    Mode'
 fcc $d,'  ====    =====    ====',0
13 fcc $d,'  ',0
14 fcc '      ',0
15 fcc '      ',0
16 fcc $d,'  ****    *****    ****',0
*
prtmap pshs d,x,y,u
 ldb #16 Size of Map
00 lda ,u+ get entry
 jsr Phex
 jsr Pspace
 decb
 bne 00b
99 puls d,x,y,u,pc
 endif
