PR#3 PREFIX /A2OSX.BUILD LOMEM $A00 INC 1 AUTO 6 *-------------------------------------- * K.LoadDrv.YA * in : * Y,A = PTR To Full Cmd Line PStrArray * out : * none *-------------------------------------- K.LoadDrv.YA >STYA K.LoadDrv.CmdArray >LDYAI ENV.DRV push ENVNAME=DRV >SYSCALL GetEnv.YA get value for ENV=DRV bcs .9 >PUSHYA Push $DRV value >PUSHW K.LoadDrv.CmdArray (ARG[0] = DRVNAME) jsr K.FileSearch.I find Driver in $DRV bcs .9 >LDYAI K.Buf256 jsr K.LoadBin.YA Y,A =filename full path bcs .9 >STYA pDrv stx .97+1 >LDYA K.LoadDrv.CmdArray >STYA ZPPtr1 .1 lda (ZPPtr1) Advance to ARG[1] beq .2 inc ZPPtr1 bne .1 inc ZPPtr1+1 bra .1 .2 >LDYA ZPPtr1 jsr pDrvJmp call Dev.Detect bcs .96 jsr BIN.InstallDRV bcs .96 jsr .96 Cleanup... lda #0 Make sure RC = 0 if success clc .9 rts .96 pha .97 lda #$ff jsr K.FreeMem.A pla rts *-------------------------------------- K.LoadDrv.CmdArray .BS 2 *-------------------------------------- * K.LoadLib.YA * in : * Y,A = PTR To Lib Name * out : * A = hMem To loaded LIB *-------------------------------------- K.LoadLib.YA >STYA K.LoadLib.Name SAVE LIBname for K.FileSearch >LDYAI ENV.LIB push ENVNAME=LIB >SYSCALL GetEnv.YA get value for ENV=LIB bcs .9 >PUSHYA Push $LIB value >PUSHW K.LoadLib.Name jsr K.FileSearch.I find libname in $LIB bcs .9 >LDYAI K.Buf256 jsr K.LoadBin.YA Y,A=filename full path bcs .9 stx .8+1 >STYA LIB.Jump+1 txa Pass hLib to LibLoad ldx #LIBMGR.LOAD jsr LIB.Jump Call LIB.LOAD function bcs .99 .8 lda #$ff hLib clc .9 rts .99 pha lda .8+1 jsr K.FreeMem.A pla sec rts *-------------------------------------- K.LoadLib.Name .BS 2 *-------------------------------------- * K.UnloadLib.A * in : * A = hMem To Lib * out : *-------------------------------------- K.UnloadLib.A pha jsr K.GetMemByID.A >STYA ZPPtr1 ldy #S.MEM.REFCNT lda (ZPPtr1),y Get count of those referencing this lib dec only one left ? beq .1 sta (ZPPtr1),y pla clc rts .1 ldy #S.MEM.PTR lda (ZPPtr1),y sta LIB.Jump+1 iny lda (ZPPtr1),y sta LIB.Jump+2 ldx #LIBMGR.UNLOAD jsr LIB.Jump Call LIB.UNLOAD function pla jmp K.FreeMem.A LIB.Jump jmp $ffff Self Modiied *-------------------------------------- * K.LoadBin.YA * in : * A = hMem of FilePath (PSTR) * out : * Y,A = MEMPTR * X = hMem of Code Segment *-------------------------------------- K.LoadBin.YA >STYA K.LoadBin.YA.Filename jsr MEM.GetMemByNameYA bcs K.LoadBin.YA.1 not already loaded >STYA ZPPtr1 Save base address ldy #S.MEM.REFCNT lda (ZPPtr1),y inc sta (ZPPtr1),y txa jmp K.GetMemPtr.A X=hMem from K.GetMemByNameA *-------------------------------------- K.LoadBin.YA.1 >PUSHWI K.S.STAT >PUSHW K.LoadBin.YA.Filename jsr K.STAT Look for AUXTYPE bcs .9 >LDYA K.S.STAT+S.STAT.P.AUXTYPE >STYA BIN.Relocate.Start >PUSHYA Push AUXTYPE >PUSHBI 6 S.FILEINFO.TYPE.BIN >PUSHBI SYS.FOpen.R >PUSHW K.LoadBin.YA.Filename jsr K.LoadFile .9 bcs .99 Error Loading file stx K.LoadBin.YA.hMem save hMem pha YA=CODE+DATA size tya * clc CC from bcs .99 adc BIN.Relocate.Start Get AUXTYPE for actual Base Address sta BIN.Relocate.End compute Range End=AUXTYPE+FILELEN pla adc BIN.Relocate.Start+1 sta BIN.Relocate.End+1 txa jsr K.GetMemPtr.A >STYA ZPPtr1 set ZPPtr1 -> Code start pha YA = actual load address tya sec sbc BIN.Relocate.Start sta BIN.Relocate.Offset Offset=ZPPtr1-AUXTYPE pla sbc BIN.Relocate.Start+1 sta BIN.Relocate.Offset+1 ldy #H.BIN.CODE.LEN get Code Len lda (ZPPtr1),y clc adc ZPPtr1 sta ZPPtr2 Make ZPPtr2 = ZPPtr1 + CodeLen iny lda (ZPPtr1),y adc ZPPtr1+1 sta ZPPtr2+1 jsr BIN.RelocateEXE >LDYA K.LoadBin.YA.Filename get back bin path >SYSCALL NewPStrYA make a copy of this string bcs .98 lda K.LoadBin.YA.hMem Keep X=hMem jsr K.GetMemByID.A X unmodified >STYA ZPPtr1 lda (ZPPtr1) ora #S.MEM.F.CODE This is a code segment sta (ZPPtr1) txa Get Back hMem ldy #S.MEM.BIN sta (ZPPtr1),y lda K.LoadBin.YA.hMem tax return hMEM to Caller... jsr K.GetMemPtr.A clc ...and Y,A=PTR to CS rts .98 pha lda K.LoadBin.YA.hMem jsr K.FreeMem.A Discard Loaded Code pla sec .99 rts *-------------------------------------- K.LoadBin.YA.Filename .BS 2 K.LoadBin.YA.hMem .BS 1 *-------------------------------------- * BIN.InstallDRV * In: * pDrv = .DRV File Loaded Address * Note: * K.LoadBin.YA called from K.LoadDrvYA * Already setup correctly pDrv, * BIN.Relocate.Start,End *-------------------------------------- BIN.InstallDRV ldy #H.BIN.DRV.CODE.O lda (pDrv),y clc adc BIN.Relocate.Start Advance start From AUXTYPE to AUXTYPE+DRV code offset sta BIN.Relocate.Start iny lda (pDrv),y adc BIN.Relocate.Start+1 sta BIN.Relocate.Start+1 lda DevMgr.Free Offset = Target DRV address-(AUXTYPE+DRV OFFSET) sec sbc BIN.Relocate.Start sta BIN.Relocate.Offset lda DevMgr.Free+1 sbc BIN.Relocate.Start+1 sta BIN.Relocate.Offset+1 lda BIN.Relocate.End Compute DRVLen=End-start sec sbc BIN.Relocate.Start sta BIN.InstallDRV.DRVLen lda BIN.Relocate.End+1 sbc BIN.Relocate.Start+1 sta BIN.InstallDRV.DRVLen+1 lda DevMgr.Free clc adc BIN.InstallDRV.DRVLen sta BIN.InstallDRV.DRVEnd tay Save DRVEnd LO lda DevMgr.Free+1 adc BIN.InstallDRV.DRVLen+1 A = DRVEnd HI sta BIN.InstallDRV.DRVEnd+1 bcs .9 we crossed $FFFF, out of mem cpy #DevMgr.HiMem sbc /DevMgr.HiMem bcs .9 No More Room to load Driver.... lda DevMgr.Count cmp #K.DEV.MAX bne .10 No Device Handle Left lda #DEVMGR.ERROOH sec rts .9 lda #DEVMGR.ERROOM sec rts .10 inc DevMgr.Count >SYSCALL GetDevByID.A >STYA ZPPtr2 setup Ptr2 to dest HEADER ldy #H.BIN.DRV.HEADER.O lda (pDrv),y clc adc pDrv sta ZPPtr1 iny lda (pDrv),y adc pDrv+1 sta ZPPtr1+1 set Ptr1 to Src HEADER ldy #S.DEV-1 .1 lda (ZPPtr1),y copy header to DevMgr.Table sta (ZPPtr2),y dey bpl .1 ldy #S.DEV.DRV.JMP+1 Setup Main JMP lda DevMgr.Free+1 sta (ZPPtr2),y tax dey lda DevMgr.Free sta (ZPPtr2),y sta ZPPtr2 set Ptr2 to Dest CODE stx ZPPtr2+1 ldy #H.BIN.DRV.CODE.O lda (pDrv),y clc adc pDrv sta ZPPtr1 iny lda (pDrv),y adc pDrv+1 sta ZPPtr1+1 set Ptr1 to Src CODE lda BIN.InstallDRV.DrvLen+1 eor #$ff pha lda BIN.InstallDRV.DrvLen eor #$ff tax ldy #0 .2 inx Move CODE in LC bne .3 pla inc beq .4 pha .3 lda (ZPPtr1),y sta (ZPPtr2),y iny bne .2 inc ZPPtr1+1 inc ZPPtr2+1 bra .2 .4 ldy #H.BIN.DRV.CODE.LEN lda DevMgr.Free Make Ptr1 = code start sta ZPPtr1 clc adc (pDrv),y sta ZPPtr2 iny lda DevMgr.Free+1 sta ZPPtr1+1 adc (pDrv),y sta ZPPtr2+1 Make ZPPtr2 = ZPPtr1 + CodeLen jsr BIN.RelocateDRV >LDYA BIN.InstallDRV.DrvEnd >STYA DevMgr.Free clc rts *-------------------------------------- BIN.InstallDRV.DrvLen .BS 2 BIN.InstallDRV.DrvEnd .BS 2 *-------------------------------------- * BIN.Relocate___ : * In : * ZPPtr1 = Ptr to Code * ZPPtr2 = End Of Code *-------------------------------------- BIN.Relocate.Start .BS 2 BIN.Relocate.End .BS 2 BIN.Relocate.Offset .BS 2 *-------------------------------------- BIN.RelocateEXE ldy #H.BIN.HEADER+1 lda (ZPPtr1),y cmp /H.BIN.HEADER.BIN65 beq .1 cmp /H.BIN.HEADER.DRV65 beq .1 lda #SYSMGR.ERRIBIN sec rts .1 ldy #H.BIN.JMP relocate Main JMP lda (ZPPtr1),y clc adc BIN.Relocate.Offset sta (ZPPtr1),y iny lda (ZPPtr1),y adc BIN.Relocate.Offset+1 sta (ZPPtr1),y ldy #H.BIN.EXE.REL.TABLE skip Header, CS.SIZE & DS.SIZE... (16 bytes) .HS 2C bit abs *-------------------------------------- BIN.RelocateDRV ldy #H.BIN.DRV.REL.TABLE .1 lda (ZPPtr1),y Start Relocate JMP table tax LO in X iny ora (ZPPtr1),y ORA with HI beq .2 $0000 = end of table dey txa get back LO clc adc BIN.Relocate.Offset sta (ZPPtr1),y iny lda (ZPPtr1),y adc BIN.Relocate.Offset+1 sta (ZPPtr1),y iny bra .1 *-------------------------------------- .2 tya add current offset in Y to Ptr sec + 1 to skip last 00 from beq .2 adc ZPPtr1 sta ZPPtr1 bcc .3 inc ZPPtr1+1 ZPPtr1=Current Code PTR .3 lda (ZPPtr1) get OPCODE lsr /2 tax lda BIN.OPCODES,x get OPCODE definition bcs .4 go get LO nibble lsr lsr move HI -> LO lsr lsr .4 and #$0f bit #$8 abs addressing? beq .6 no.... and #7 save Opcode length... pha ldy #1 lda (ZPPtr1),y Get LO tax save LO in X iny make Y point to HI cpx BIN.Relocate.Start lda (ZPPtr1),y Get HI sbc BIN.Relocate.Start+1 bcc .5 addr < BIN.Relocate.Start, out of range txa Get back LO cpx BIN.Relocate.End lda (ZPPtr1),y Get HI sbc BIN.Relocate.End+1 bcs .5 addr > BIN.Relocate.End, out of range txa Get back LO * clc CC from bcs .6 adc BIN.Relocate.Offset add Offset to abs address dey sta (ZPPtr1),y store relocated addr LO iny lda (ZPPtr1),y Get HI adc BIN.Relocate.Offset+1 sta (ZPPtr1),y store relocated addr HI .5 pla get back Opcode length... .6 clc A = OPCODE length adc ZPPtr1 sta ZPPtr1 bcc .7 inc ZPPtr1+1 .7 eor ZPPtr2 A = ZPPtr1 bne .3 lda ZPPtr1+1 eor ZPPtr2+1 bne .3 next opcode.... .8 rts *-------------------------------------- * 65C02 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.OPCODES .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 /A2OSX.SRC/SYS/KERNEL.S.BIN LOAD /A2OSX.SRC/SYS/KERNEL.S ASM