A2osX/SYS/KERNEL.S.MEM.txt
2018-11-23 17:20:19 +01:00

815 lines
17 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NEW
PREFIX
AUTO 4,1
*--------------------------------------
ZPMemMgrSPtr .EQ ZPMEMMGR Pointer To Slot selected
ZPMemMgrTmp1 .EQ ZPMEMMGR+2
ZPMemMgrSize .EQ ZPMEMMGR+4
ZPSListID .EQ ZPMEMMGR+6
ZPSListIBlkPtr .EQ ZPMEMMGR+8
ZPSListDBlkPtr .EQ ZPMEMMGR+10
ZPSListKeyPtr .EQ ZPMEMMGR+12
ZPSListDataPtr .EQ ZPMEMMGR+14
*/--------------------------------------
* # GetMem0
* Y,A = Size Requested
* ## RETURN VALUE
* CC : success
* YA = PTR to Mem (ZERO Initialised)
* X = hMem
* CS :
* A = EC
*\--------------------------------------
K.GetMem0 ldx #S.MEM.F.INIT0
.HS 2C bit abs
*/--------------------------------------
* # GetMem
* Y,A = Size Requested
* ## RETURN VALUE
* CC : success
* YA = PTR to Mem (Uninitialised)
* X = hMem
* CS :
* A = EC
*\--------------------------------------
K.GetMem ldx #0
MEM.GetMem.YAX stx Mem.ReqFlags
sta ZPMemMgrSize+1
tya
bit #$0F 16 bytes aligned ?
beq .10 yes, request it
and #$F0 align on 16 bytes
clc
adc #$10
bcc .10
inc ZPMemMgrSize+1
.10 sta ZPMemMgrSize
>LDYAI Mem.Table
>STYA ZPMemMgrSPtr
ldx #0 Current slot=0
bra .4 skip slot 0, Check if some free slot to reuse first
.1 inx move to next slot
jsr Mem.NextSlot
lda (ZPMemMgrSPtr) Get Flags
bmi .4 in use ?
.2 lda Mem.ReqFlags Found an empty slot
and #S.MEM.F.ALIGN is request needs a page align ?
beq .3
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y get LO of PTR
bne .4 not page-aligned
.3 ldy #S.MEM.LEN
lda (ZPMemMgrSPtr),y get LEN of this block
cmp ZPMemMgrSize compare with requested size
bne .4
iny
lda (ZPMemMgrSPtr),y
cmp ZPMemMgrSize+1
beq .7 requested size matches slot size !!!
.4 cpx Mem.LastSlot any other slot to check?
bne .1 last one, create a new slot
*-------------- Create a New SLOT
.5 lda Mem.ReqFlags
and #S.MEM.F.ALIGN is request needs a page align ?
beq .6
lda Mem.Free target PTR will be page aligned ?
sec
sbc ZPMemMgrSize
beq .6 yes, allocate
tay no, reserve a free slot to fill gap
lda #0
jsr Mem.AddSlot X = new slot
bcs .9
lda #S.MEM.F.ALIGN Make sure marked FREE
sta (ZPMemMgrSPtr)
.6 >LDYA ZPMemMgrSize
jsr Mem.AddSlot X = new slot
bcs .9
*-------------- Reuse this SLOT
.7 lda Mem.ReqFlags get requested flags
ora #S.MEM.F.INUSE mark as in use
sta (ZPMemMgrSPtr)
and #S.MEM.F.INIT0
beq .8
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y ZPMemMgrSPtr already set
sta .12+1
iny
lda (ZPMemMgrSPtr),y
sta .12+2
lda ZPMemMgrSize
eor #$ff
tay y=not lo count
lda ZPMemMgrSize+1
eor #$ff a=not hi count
phx
ldx #0
.11 iny
bne .12
inc
beq .13
.12 stz $ffff,x
inx
bne .11
inc .12+2
bra .11
.13 plx
.8 lda #0
ldy #S.MEM.BIN
sta (ZPMemMgrSPtr),y
inc Mark this slot used by one process
ldy #S.MEM.REFCNT
sta (ZPMemMgrSPtr),y
ldy #S.PS.PID
lda (pPs),y
ldy #S.MEM.OWNERPID
sta (ZPMemMgrSPtr),y
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y
pha
iny
lda (ZPMemMgrSPtr),y A = HI PTR
ply Y = LO PTR
clc X = Current hMem
.9 rts
*--------------------------------------
Mem.ReqFlags .BS 1 store requested Flags
*--------------------------------------
* Mem.AddSlot
* In:
* Y,A = Requested size
* Out:
*--------------------------------------
Mem.AddSlot >STYA ZPMemMgrTmp1 save req size
ldx Mem.LastSlot
inx
beq .99 > 255 ERR:OUT OF SLOT
lda Mem.Free Compute base PTR=FREE-REQ size
sec
sbc ZPMemMgrTmp1
tay save new Mem.Free LO
lda Mem.Free+1
sbc ZPMemMgrTmp1+1
pha save new Mem.Free HI
cpy Mem.LoMem
sbc Mem.LoMem+1
bcc .98 ERR out of mem
jsr Mem.NextSlot X,Y unmodified
tya get back Mem.Free LO
ldy #S.MEM.PTR
sta Mem.Free set as system Mem.Free value
sta (ZPMemMgrSPtr),y store it as base address of new slot
iny
pla get back Mem.Free HI
sta Mem.Free+1
sta (ZPMemMgrSPtr),y
iny
lda ZPMemMgrTmp1 get requested size
sta (ZPMemMgrSPtr),y setup slot LEN
iny
lda ZPMemMgrTmp1+1
sta (ZPMemMgrSPtr),y
stx Mem.LastSlot mark this slot allocated
clc
rts
.98 pla discard new Mem.Free HI
.99 lda #E.OOM
sec
rts
*--------------------------------------
Mem.NextSlot lda ZPMemMgrSPtr
clc
adc #S.MEM
sta ZPMemMgrSPtr
bcc .8
inc ZPMemMgrSPtr+1
.8 rts
*/--------------------------------------
* # FreeMem
* A = hMem To Free
* ## RETURN VALUE
* none.
* (X unmodified)
*\--------------------------------------
K.FreeMem.BAD pha
ldy #S.PS.hARGV
lda (pPS),y
jsr K.GetMemPtr
>PUSHYA
pla
>PUSHA
>PUSHBI 3
phx
>LDYAI K.FreeMem.ERRBAD
>SYSCALL printf
plx
>DEBUGOA
sec
rts
K.FreeMem tay
beq K.FreeMem.BAD Slot=0, reserved by Kernel
cmp Mem.LastSlot
bcc .10
bne K.FreeMem.BAD
.10 jsr K.GetMemByID X unmodified
lda (ZPMemMgrSPtr)
bpl K.FreeMem.FREED In use ?
ldy #S.MEM.REFCNT
lda (ZPMemMgrSPtr),y Get count of those referencing this hMem
dec only one left ?
sta (ZPMemMgrSPtr),y
bne .8 no, must be a code segment loaded several times
lda (ZPMemMgrSPtr)
and #S.MEM.F.CODE CS: Any BINPATH to discard ?
sta (ZPMemMgrSPtr) Mark as FREE
beq .1
ldy #S.MEM.BIN
lda (ZPMemMgrSPtr),y
jsr K.GetMemByID X unmodified
lda (ZPMemMgrSPtr)
bpl * ***MUST BE ALLOCATED***
lda #0 mark BINPATH slot as free
sta (ZPMemMgrSPtr)
.1 lda Mem.LastSlot
beq .8
jsr K.GetMemByID X unmodified
lda (ZPMemMgrSPtr)
bmi .8 used, exit
.2 dec Mem.LastSlot free! get previous....
beq .80 empty list: go set Mem.Free=Mem.HiMem
lda Mem.LastSlot
jsr K.GetMemByID X unmodified
lda (ZPMemMgrSPtr)
bpl .2 free again! loop
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y set Mem.Free...
sta Mem.Free
iny
lda (ZPMemMgrSPtr),y
sta Mem.Free+1
.8 clc
rts
.80 >LDYA Mem.HiMem
>STYA Mem.Free
clc
rts
*--------------------------------------
K.FreeMem.FREED ldy #S.PS.hARGV
lda (pPS),y
jsr K.GetMemPtr
>PUSHYA
>PUSHBI 2
phx
>LDYAI K.FreeMem.ERRFREED
>SYSCALL printf
plx
>DEBUGOA
sec
rts
*--------------------------------------
K.FreeMem.ERRBAD .AZ "FreeMem:%h=Bad hMem,BIN=%s\r\n"
K.FreeMem.ERRFREED .AZ "FreeMem:hMem already freed,BIN=%s\r\n"
*/--------------------------------------
* # GetMemPtr
* A = hMem
* ## RETURN VALUE
* Y,A = PTR to MemBlock
* (X unmodified)
*\--------------------------------------
* Optimized for :
* Mem.Table is page aligned at $1800
* S.MEM is 8 bytes
*--------------------------------------
.DO Mem.Table=$1800
.ELSE
!!!!!WARNING!!!!!
.FIN
.DO S.MEM=8
.ELSE
!!!!!WARNING!!!!!
.FIN
*--------------------------------------
K.GetMemPtr sta ZPMemMgrSPtr
lda /Mem.Table/8
asl ZPMemMgrSPtr
rol
asl ZPMemMgrSPtr
rol
asl ZPMemMgrSPtr
rol
sta ZPMemMgrSPtr+1
ldy #S.MEM.PTR
lda (ZPMemMgrSPtr),y
pha
iny
lda (ZPMemMgrSPtr),y
ply
rts
*/--------------------------------------
* # GetMemByID
* A = hMem
* ## RETURN VALUE
* Y,A = ZPMemMgrSPtr = PTR to S.MEM
* (X unmodified)
*\--------------------------------------
K.GetMemByID sta ZPMemMgrSPtr
lda /Mem.Table/8
asl ZPMemMgrSPtr
rol
asl ZPMemMgrSPtr
rol
asl ZPMemMgrSPtr
rol
sta ZPMemMgrSPtr+1
ldy ZPMemMgrSPtr
rts
*/--------------------------------------
* # GetMemStat
* **In:**
* Y,A = Ptr to 24 bytes buffer
* ## RETURN VALUE
* Buffer filled with memory stats
*\--------------------------------------
K.GetMemStat >STYA ZPMemMgrTmp1
ldy #22
ldx #6
.1 lda DevMgr.Stat,x
sta (ZPMemMgrTmp1),y
dey
dex
bpl .1
sta SETREADAUX
ldy #14
jsr .2
sta CLRREADAUX
ldy #6
.2 ldx #6
.3 lda Mem.Table,x
sta (ZPMemMgrTmp1),y
dey
dex
bpl .3
rts
*/--------------------------------------
* # NewStr
* Create a new copy of this C-String
* Y,A = Ptr to source C-String
* ## RETURN VALUE
* CC : success
* Y,A = PTR to String
* X = hMem (PSTR)
* CS : error
* A = SYS error code
*\--------------------------------------
K.NewStr >STYA .1+1
>STYA .4+1
ldy #0
lda #0
.1 ldx $ffff,y
beq .2
iny
bne .1
inc
inc .1+2
bne .1
.2 iny Add one for ending 0
bne .3
inc
.3 jsr K.GetMem
bcs .9
>STYA .5+1
phy
pha
ldy #0
.4 lda $ffff,y
.5 sta $ffff,y
beq .8
iny
bne .4
inc .4+2
inc .5+2
bne .4
.8 pla
ply
clc
.9 rts
*/--------------------------------------
* # SListGetByID
* PUSHB = hSList
* PUSHW = KeyID
* PUSHW = Data Ptr
* PUSHW = Key Ptr
* ## RETURN VALUE
* X,Y = Next KeyID
*\--------------------------------------
K.SListGetByID >PULLW ZPSListKeyPtr
*/--------------------------------------
* # SListUpdateByID
* PUSHB = hSList
* PUSHW = KeyID
* PUSHW = Data Ptr
* ## RETURN VALUE
* A = Key Length
* X,Y = KeyID
*\--------------------------------------
K.SListUpdateByID
>PULLW ZPSListDataPtr
>PULLW ZPSListID
bra K.SListPullH
*/--------------------------------------
* # SListAdd
* PUSHB = hSList
* PUSHW = Key Ptr
* PUSHW = Data Ptr
* ## RETURN VALUE
* A = Key Length
* X,Y = KeyID
*\--------------------------------------
K.SListAdd
*/--------------------------------------
* # SListLookup
* PUSHB = hSList
* PUSHW = Key Ptr
* PUSHW = Data Ptr
* ## RETURN VALUE
* A = Key Length
* X,Y = KeyID
*\--------------------------------------
K.SListLookup
*--------------------------------------
>PULLW ZPSListDataPtr
>PULLW ZPSListKeyPtr
K.SListPullH >PULLA
*/--------------------------------------
* # SListNew
* ## RETURN VALUE
* A=hSList
*\--------------------------------------
K.SListNew
*/--------------------------------------
* # SListFree
* A=hSList
* ## RETURN VALUE
*\--------------------------------------
K.SListFree
*--------------------------------------
sta SETREADAUX
sta SETWRITEAUX
jsr X.SLIST
sta CLRREADAUX
sta CLRWRITEAUX
rts
*/--------------------------------------
* # GetStkObjProp
* A = hObject (AUX Memory)
* Y = Property Index
* ## RETURN VALUE
* Y,A = Property Value
*\--------------------------------------
K.GetStkObjProp phy
sta SETREADAUX
jsr K.GetMemPtr
>STYA ZPMemMgrTmp1
ply
lda (ZPMemMgrTmp1),y
pha
iny
lda (ZPMemMgrTmp1),y
ply
sta CLRREADAUX
rts
*/--------------------------------------
* # NewStkObj
* Y,A = Size Requested
* ## RETURN VALUE
* CC : success
* YA = PTR to Mem (Uninitialised)
* X = hMem
* CS :
* A = EC
*\--------------------------------------
K.NewStkObj ldx #SYS.GetMem
.HS 2C BIT ABS
*/--------------------------------------
* # FreeStkObj
* A = hMem To Free (AUX Memory)
* ## RETURN VALUE
* none.
* (X,Y unmodified)
*\--------------------------------------
K.FreeStkObj ldx #SYS.FreeMem
MEM.GoStkObj sta SETREADAUX
sta SETWRITEAUX
jsr .1
sta CLRREADAUX
sta CLRWRITEAUX
rts
.1 jmp (SYS.Kernel.JMP,x)
*/--------------------------------------
* # LoadStkObj
* Load a file in AUX memory (Stock Objects)
* PUSHW = AUXTYPE (Handled by....
* PUSHB = TYPE ...
* PUSHB = MODE ...
* PUSHW = PATH ...FOpen)
* ## RETURN VALUE
* Y,A = File Length
* X = hMem of Loaded Object in AUX mem
*\--------------------------------------
K.LoadStkObj >SYSCALL LoadFile
bcs .99
stx .8+1 Save MAIN hMem
>STYA A2L Save LEN temporarly
jsr K.NewStkObj
bcs .9
stx .80+1
>STYA A4L Save Destination Address in AUX
lda .8+1
jsr K.GetMemPtr
>STYA A1L Save MAIN base Ptr
pha Add Allocated LEN
tya
clc
adc A2L
sta A2L
pla
adc A2L+1
sta A2L+1 to get End address in A2L/H
sec Main To Aux
jsr AuxMove
.8 lda #$ff SELF MODIFIED
jsr K.FreeMem release MAIN memory
.80 lda #$ff Returns AUX hMem
rts
.9 pha
jsr .8
pla
sec
.99 rts
*--------------------------------------
MEM.GetKeyCharY sta CLRREADAUX
lda (ZPSListKeyPtr),y
sta SETREADAUX
beq MEM.IsDigit.9
MEM.IsIDValid cmp #'.'
beq MEM.IsDigit.8
cmp #'_'
beq MEM.IsDigit.8
MEM.IsDigitOrL cmp #'z'+1
bcs MEM.IsDigit.RTS
cmp #'a'
bcs MEM.IsDigit.8
MEM.IsDigitOrUC cmp #'Z'+1
bcs MEM.IsDigit.RTS
cmp #'A'
bcs MEM.IsDigit.8
MEM.IsDigit cmp #'9'+1
bcs MEM.IsDigit.RTS
cmp #'0'
bcs MEM.IsDigit.8
MEM.IsDigit.9 sec
MEM.IsDigit.RTS rts
MEM.IsDigit.8 clc
rts
*--------------------------------------
MEM.IsHexDigit jsr MEM.IsDigit
bcc .8
cmp #'A'
bcc MEM.IsDigit.9
cmp #'F'+1
bcc .1
cmp #'a'
bcc MEM.IsDigit.9
cmp #'f'+1
bcs MEM.IsDigit.RTS
eor #$20
.1
* clc
sbc #'A'-11 cc so A->10 (11-CC)
clc
.8 and #$0F
rts
*--------------------------------------
MEM.SetKeyCharY sta CLRWRITEAUX
sta (ZPSListKeyPtr),y
sta SETWRITEAUX
rts
*--------------------------------------
MEM.GetDataByte sta CLRREADAUX
lda (ZPSListDataPtr)
sta SETREADAUX
bra MEM.SetDataByte.1
*--------------------------------------
MEM.SetDataByte sta CLRWRITEAUX
sta (ZPSListDataPtr)
sta SETWRITEAUX
MEM.SetDataByte.1
inc ZPSListDataPtr
bne .8
inc ZPSListDataPtr+1
.8 rts
*--------------------------------------
MEM.TXTPTR.GetNext
sta CLRREADAUX
lda (TXTPTR)
sta SETREADAUX
beq .9
inc TXTPTR
bne .9
inc TXTPTR+1
.9 rts
*--------------------------------------
MEM.SPtr1PPtr2 >STYA ZPPtr1
>PULLW ZPPtr2
rts
*--------------------------------------
MEM.GetCharPtr1 lda (ZPPtr1)
beq MEM.GetCharPtr1.8
MEM.NextCharPtr1
inc ZPPtr1
bne MEM.GetCharPtr1.8
inc ZPPtr1+1 never Z
MEM.GetCharPtr1.8
rts
*--------------------------------------
MEM.PutCharPtr2 sta (ZPPtr2)
inc ZPPtr2
bne .8
inc ZPPtr2+1
.8 rts
*/--------------------------------------
* # GetPSStat
* **In:**
* Y,A = Ptr to K.PS.MAX*2+1 bytes buffer
* ## RETURN VALUE
* Buffer filled with PS stats
*\--------------------------------------
K.GetPSStat >STYA ZPPtr1
ldx #0
ldy #1
lda CORE.InKernelStats
sta (ZPPtr1),y
iny
.1 lda PS.Table.PID,x
beq .2
lda PS.Table.hPS,x
sta (ZPPtr1),y
iny
lda PS.Table.Stats,x
sta (ZPPtr1),y
iny
.2 inx
cpx #K.PS.MAX
bne .1
.9 tya
lsr
dec
sta (ZPPtr1)
rts
*--------------------------------------
* 6502/65C02/65816 OPCODES
* Bit 3 : 1 = absolute addressing
* Bit 2-1-0 : opcode + @ length
*--------------------------------------
* 0.1.2.3.4.5.6.7.8.9.A.B.C.D.E.F
BIN.65816
* TO DO : Make 2 different tables
* http://axis.llx.com/~nparker/a2/opcodes.html
*--------------------------------------
* 65R02 OPCODES
*--------------------------------------
* 0.1.2.3.4.5.6.7.8.9.A.B.C.D.E.F
BIN.65R02 .HS 1.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.B.B.B.3
.HS B.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.B.B.B.3
.HS 1.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.1.B.B.3
.HS 1.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.1.B.B.3
.HS 2.2.2.1.2.2.2.2.1.2.1.1.B.B.B.3
.HS 2.2.2.1.2.2.2.2.1.B.1.1.1.B.B.3
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.MEM
LOAD USR/SRC/SYS/KERNEL.S
ASM