A2osX/SYS/KERNEL.S.MEM.txt

447 lines
9.5 KiB
Plaintext
Raw Normal View History

2015-03-14 21:48:35 +00:00
PR#3
2015-06-03 18:30:57 +00:00
PREFIX /A2OSX.SRC
2015-03-14 21:48:35 +00:00
NEW
INC 1
AUTO 6
.LIST OFF
.OP 65C02
*--------------------------------------
* !!!!!!!! DO NOT USE VCPU16 !!!!!!!!
* !!!!!! DO NOT USE ZPQuickPTRs !!!!!
*--------------------------------------
2015-06-03 18:30:57 +00:00
ZPMemMgrSPtr .EQ ZPMemMgr Pointer To Slot selected
ZPMemMgrTmp1 .EQ ZPMemMgr+2
ZPMemMgrTmp2 .EQ ZPMemMgr+4
ZPMemMgrTmp3 .EQ ZPMemMgr+6
2015-03-14 21:48:35 +00:00
*--------------------------------------
* TODO :
2015-06-03 18:30:57 +00:00
* - Create a 256 bytes table
2015-03-14 21:48:35 +00:00
* mapping hMem's to real memory blocks to help
* Garbage collector move blocks
2015-06-03 18:30:57 +00:00
* - mem.ID=0 always used! useful to test if hMem=0
2015-03-14 21:48:35 +00:00
*--------------------------------------
2015-06-03 18:30:57 +00:00
S.MemMgrInit >LDYAI MemMgr.MHiMem
>STYA MemMgr.HiMem
>STYA MemMgr.Free
2015-03-14 21:48:35 +00:00
2015-06-03 18:30:57 +00:00
>LDYAI MemMgr.MLoMem
>STYA MemMgr.LoMem
2015-03-14 21:48:35 +00:00
2015-06-03 18:30:57 +00:00
stz MemMgr.LastSlot Reserve Slot #0
sta SETREADAUX
sta SETWRITEAUX
>LDYAI MemMgr.XHiMem
>STYA MemMgr.HiMem
>STYA MemMgr.Free
>LDYAI MemMgr.XLoMem
>STYA MemMgr.LoMem
stz MemMgr.LastSlot Reserve Slot #0
sta CLRREADAUX
sta CLRWRITEAUX
2015-03-14 21:48:35 +00:00
clc
rts
*--------------------------------------
* PUBLIC
*--------------------------------------
* S.GetMem
* In:
* PULLB = FLAGS
* PULLW = Size Requested
* Out:
* YA = PTR to Mem
* X = S.MEM.ID
* CS on err
*--------------------------------------
2015-06-03 18:30:57 +00:00
S.GetMem >PULLB MemMgr.ReqFlags store requested flags
and #S.MEM.F.AUX
beq .11
sta SETREADAUX Aux Mem requested,switch to AUX
sta SETWRITEAUX
.11 >PULLW MemMgr.ReqSize Store requested size
lda MemMgr.ReqSize align REQS to 16 bytes
2015-03-14 21:48:35 +00:00
and #$0F
beq .10
2015-06-03 18:30:57 +00:00
lda MemMgr.ReqSize
2015-03-14 21:48:35 +00:00
and #$F0
clc
adc #$10
2015-06-03 18:30:57 +00:00
sta MemMgr.ReqSize
2015-03-14 21:48:35 +00:00
bcc .10
2015-06-03 18:30:57 +00:00
inc MemMgr.ReqSize+1
.10 >LDYAI MemMgr.Table
>STYA ZPMemMgrSPtr
ldx #0 Current slot=0
2015-03-15 22:00:40 +00:00
2015-06-03 18:30:57 +00:00
lda MemMgr.LastSlot Check if some free slot to reuse first
beq .41
sta MemMgr.SlotCnt
.1 inx move to next slot
jsr MemMgr.NextSlot
2015-03-14 21:48:35 +00:00
lda (ZPMemMgrSPtr) Get Flags
bmi .4 in use ?
2015-06-03 18:30:57 +00:00
.2 lda MemMgr.ReqFlags Found an empty slot
2015-03-14 21:48:35 +00:00
and #S.MEM.F.ALIGN is request needs a page align ?
beq .3
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y get LO of PTR
bne .4 not page-aligned
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
.3 ldy #S.MEM.LEN
lda (ZPMemMgrSPtr),y get LEN of this block
2015-06-03 18:30:57 +00:00
cmp MemMgr.ReqSize compare with requested size
2015-03-14 21:48:35 +00:00
bne .4
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
iny
lda (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
cmp MemMgr.ReqSize+1
beq .7 requested size matches slot size !!!
.4 dec MemMgr.SlotCnt any other slot to check?
bne .1 last one, create a new slot
*-------------- Create a New SLOT
.41 lda MemMgr.ReqFlags
and #S.MEM.F.ALIGN is request needs a page align ?
beq .6
lda MemMgr.Free target PTR will be page aligned ?
sec
sbc MemMgr.ReqSize
beq .6 yes, allocate
tay no, reserve a free slot to fill gap
lda #0
jsr MemMgr.AddSlot X = new slot
bcs .9
.6 >LDYA MemMgr.ReqSize
jsr MemMgr.AddSlot X = new slot
bcs .9
2015-03-14 21:48:35 +00:00
*-------------- Reuse this SLOT
2015-06-03 18:30:57 +00:00
.7 lda MemMgr.ReqFlags get requested flags
2015-03-14 21:48:35 +00:00
ora #S.MEM.F.INUSE mark as in use
sta (ZPMemMgrSPtr)
2015-03-15 22:00:40 +00:00
2015-06-03 18:30:57 +00:00
and #S.MEM.F.INIT0
beq .8
jsr MemMgr.Init0
.8 lda #0
2015-03-14 21:48:35 +00:00
ldy #S.MEM.BIN
sta (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
inc Mark this slot used by one process
2015-03-14 21:48:35 +00:00
ldy #S.MEM.REFCNT
sta (ZPMemMgrSPtr),y
2015-03-15 22:00:40 +00:00
2015-03-14 21:48:35 +00:00
ldy #S.PS.ID
2015-06-03 18:30:57 +00:00
lda (pPs),y
2015-03-14 21:48:35 +00:00
ldy #S.MEM.OWNERPID
sta (ZPMemMgrSPtr),y
2015-03-15 22:00:40 +00:00
2015-03-14 21:48:35 +00:00
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y
pha
iny
lda (ZPMemMgrSPtr),y A = HI PTR
ply Y = LO PTR
clc X = Current hMem
2015-03-15 22:00:40 +00:00
2015-06-03 18:30:57 +00:00
.9 bit MemMgr.ReqFlags
bpl .99
sta CLRREADAUX
sta CLRWRITEAUX
.99 rts
*--------------------------------------
* MemMgr.AddSlot
* In:
* Y,A = Requested size
* Out:
*--------------------------------------
MemMgr.AddSlot >STYA ZPMemMgrTmp1 save req size
ldx MemMgr.LastSlot
inx
beq .99 > 255 ERR:OUT OF SLOT
lda MemMgr.Free Compute base PTR=FREE-REQ size
2015-03-14 21:48:35 +00:00
sec
2015-06-03 18:30:57 +00:00
sbc ZPMemMgrTmp1
sta ZPMemMgrTmp2 store new MemMgr.Free in ZPMemMgrTmp2
lda MemMgr.Free+1
sbc ZPMemMgrTmp1+1
sta ZPMemMgrTmp2+1
lda ZPMemMgrTmp2 ensure new MemMgr.Free is not lower
sec than LoMem
sbc MemMgr.LoMem
lda ZPMemMgrTmp2+1
sbc MemMgr.LoMem+1
bcc .98 ERR out of mem
jsr MemMgr.NextSlot
2015-03-15 22:00:40 +00:00
2015-06-03 18:30:57 +00:00
ldy #S.MEM.PTR
lda ZPMemMgrTmp2 get new MemMgr.Free
sta MemMgr.Free set as system MemMgr.Free value
sta (ZPMemMgrSPtr),y store is as base address of new slot
iny
lda ZPMemMgrTmp2+1
sta MemMgr.Free+1
2015-03-14 21:48:35 +00:00
sta (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
iny
lda ZPMemMgrTmp1 get requested size
sta (ZPMemMgrSPtr),y setup slot LEN
iny
lda ZPMemMgrTmp1+1
2015-03-14 21:48:35 +00:00
sta (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
stx MemMgr.LastSlot mark this slot allocated
2015-03-14 21:48:35 +00:00
clc
rts
2015-06-03 18:30:57 +00:00
.98 lda #MemMgr.ERROOM
sec
2015-03-14 21:48:35 +00:00
rts
2015-06-03 18:30:57 +00:00
.99 lda #MemMgr.ERROOH
sec
rts
*--------------------------------------
MemMgr.NextSlot lda ZPMemMgrSPtr
clc
adc #S.MEM.SIZE
sta ZPMemMgrSPtr
bcc .8
inc ZPMemMgrSPtr+1
.8 rts
*--------------------------------------
MemMgr.Init0 ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y ZPMemMgrSPtr already set
sta ZPMemMgrTmp1 set ZPMemMgrTmp1 to Mem Ptr
iny
lda (ZPMemMgrSPtr),y
sta ZPMemMgrTmp1+1
ldy #S.MEM.LEN
lda (ZPMemMgrSPtr),y
sta ZPMemMgrTmp2 set ZPMemMgrTmp2 to mem size
iny
lda (ZPMemMgrSPtr),y
sta ZPMemMgrTmp2+1
lda #0
.1 ldy ZPMemMgrTmp2
bne .2
ldy ZPMemMgrTmp2+1
beq .3
dec ZPMemMgrTmp2+1
.2 dec ZPMemMgrTmp2
sta (ZPMemMgrTmp1)
inc ZPMemMgrTmp1
bne .1
inc ZPMemMgrTmp1+1
bra .1
.3 rts
*--------------------------------------
MemMgr.SlotCnt .BS 1
MemMgr.ReqFlags .BS 1 store requested Flags
MemMgr.ReqSize .BS 2 store requested Size
2015-03-14 21:48:35 +00:00
*--------------------------------------
* S.FreeMemA
* In:
* A = hMem To Free
* Out:
* X,Y unmodified
*--------------------------------------
2015-06-03 18:30:57 +00:00
S.FreeMemA phy
2015-03-14 21:48:35 +00:00
phx
2015-06-03 18:30:57 +00:00
tax
beq .8 Slot=0, reserved by Kernel
2015-03-14 21:48:35 +00:00
jsr S.GetMemByIDA
lda (ZPMemMgrSPtr)
2015-03-15 22:00:40 +00:00
bpl * In use ?
2015-03-14 21:48:35 +00:00
2015-03-15 22:00:40 +00:00
ldy #S.MEM.REFCNT
2015-03-14 21:48:35 +00:00
lda (ZPMemMgrSPtr),y Get count of those referencing this hMem
dec only one left ?
sta (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
bne .8 no, must be a code segment loaded several times
2015-03-14 21:48:35 +00:00
ldy #S.MEM.BIN
lda (ZPMemMgrSPtr),y Any BINPATH to discard ?
2015-06-03 18:30:57 +00:00
beq .1
2015-03-14 21:48:35 +00:00
pha save hMem
>PUSHW ZPMemMgrSPtr
pla
jsr S.GetMemByIDA
lda (ZPMemMgrSPtr)
2015-06-03 18:30:57 +00:00
bpl * ***MUST BE ALLOCATED***
2015-03-14 21:48:35 +00:00
2015-06-03 18:30:57 +00:00
lda #0 mark BINPATH slot as free
2015-03-14 21:48:35 +00:00
sta (ZPMemMgrSPtr)
>PULLW ZPMemMgrSPtr
2015-06-03 18:30:57 +00:00
.1 lda #0 mark this slot as free
2015-03-14 21:48:35 +00:00
sta (ZPMemMgrSPtr)
2015-06-03 18:30:57 +00:00
.2 lda MemMgr.LastSlot
beq .8
2015-03-14 21:48:35 +00:00
jsr S.GetMemByIDA
lda (ZPMemMgrSPtr)
2015-06-03 18:30:57 +00:00
bmi .8
2015-03-14 21:48:35 +00:00
ldy #S.MEM.LEN
lda (ZPMemMgrSPtr),y give back LEN bytes
clc
2015-06-03 18:30:57 +00:00
adc MemMgr.Free to MemMgr.Free
sta MemMgr.Free
2015-03-14 21:48:35 +00:00
iny
lda (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
adc MemMgr.Free+1
sta MemMgr.Free+1
dec MemMgr.LastSlot discard last slot
bne .2 no slot left, exit
.8 plx
2015-03-14 21:48:35 +00:00
ply
clc
rts
*--------------------------------------
2015-06-03 18:30:57 +00:00
* S.GetMemPtrA
* In:
* A = hMem
* Out:
* Y,A = PTR to MemBlock
* X unmodified
2015-03-14 21:48:35 +00:00
*--------------------------------------
2015-06-03 18:30:57 +00:00
S.GetMemPtrA jsr S.GetMemByIDA
2015-03-14 21:48:35 +00:00
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
pha
2015-03-14 21:48:35 +00:00
iny
lda (ZPMemMgrSPtr),y
2015-06-03 18:30:57 +00:00
ply
2015-03-14 21:48:35 +00:00
rts
*--------------------------------------
* S.GetMemByIDA
* In:
* A = hMem
* Out:
* Y,A = ZPMemMgrSPtr = PTR to S.MEM
* X unmodified
*--------------------------------------
S.GetMemByIDA stz ZPMemMgrSPtr+1
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
asl
rol ZPMemMgrSPtr+1
asl
rol ZPMemMgrSPtr+1
asl
rol ZPMemMgrSPtr+1
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
clc
2015-06-03 18:30:57 +00:00
adc #MemMgr.Table
2015-03-14 21:48:35 +00:00
sta ZPMemMgrSPtr
tay
lda ZPMemMgrSPtr+1
2015-06-03 18:30:57 +00:00
adc /MemMgr.Table
2015-03-14 21:48:35 +00:00
sta ZPMemMgrSPtr+1
rts
*--------------------------------------
* In:
* A = hMem to BINPATH (PSTR)
* Out:
* Y,A = ZPMemMgrSPtr = PTR to S.MEM
* X = hMem
*--------------------------------------
S.GetMemByNameA jsr S.GetMemPtrA
bcs *
2015-06-03 18:30:57 +00:00
S.GetMemByNameYA
2015-03-14 21:48:35 +00:00
>STYA ZPMemMgrTmp1 save BINPATH
>LDYAI MemMgr.Table
>STYA ZPMemMgrTmp2
2015-03-14 21:48:35 +00:00
ldx #0
2015-06-03 18:30:57 +00:00
.1 cpx MemMgr.LastSlot
beq .9
lda (ZPMemMgrTmp2)
2015-03-14 21:48:35 +00:00
bpl .6 In Use?
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
ldy #S.MEM.BIN any BIN PATH in this slot?
lda (ZPMemMgrTmp2),y
2015-03-14 21:48:35 +00:00
beq .6
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
jsr S.GetMemPtrA get pathname
bcs *
>STYA ZPMemMgrTmp3
2015-06-03 18:30:57 +00:00
lda (ZPMemMgrTmp1)
cmp (ZPMemMgrTmp3)
bne .6
tay
2015-06-03 18:30:57 +00:00
2015-03-14 21:48:35 +00:00
.2 lda (ZPMemMgrTmp1),y
cmp (ZPMemMgrTmp3),y
2015-03-14 21:48:35 +00:00
bne .6
dey
2015-03-14 21:48:35 +00:00
bne .2
2015-06-03 18:30:57 +00:00
>LDYA ZPMemMgrTmp2
2015-03-14 21:48:35 +00:00
clc
rts
2015-06-03 18:30:57 +00:00
.6 lda ZPMemMgrTmp2
2015-03-14 21:48:35 +00:00
clc
adc #S.MEM.SIZE
sta ZPMemMgrTmp2
bcc .7
inc ZPMemMgrTmp2+1
.7 inx
2015-06-03 18:30:57 +00:00
bra .1
.9 sec
2015-03-14 21:48:35 +00:00
rts
*--------------------------------------
2015-06-03 18:30:57 +00:00
S.SetLoMem
clc
2015-03-14 21:48:35 +00:00
rts
*--------------------------------------
2015-06-03 18:30:57 +00:00
S.GarbageCollector
2015-03-14 21:48:35 +00:00
clc
rts
*--------------------------------------
MAN
SAVE SYS/KERNEL.S.MEM
LOAD SYS/KERNEL.S
ASM