NEW
  AUTO 3,1
*--------------------------------------
* LoadLib
*  in :
*   Y,A = PTR To "LibName" C-String
* out :
*   A = hMem To loaded LIB
*--------------------------------------
K.LoadLib		jsr PS.Find.LIB
				bcs .99

				jsr BIN.Load			K.Buf256 = "/PATH/BIN"
				bcs .99

				>STYA .1+1

				txa						Pass hLib to LibLoad
				pha
				ldx #LIBMGR.LOAD

.1				jsr $ffff				SELF MODIFIED, Call LIB.LOAD function

				plx
				bcc .8
				
				pha
				txa
				
				jsr K.FreeMem

				pla
				sec
				rts

.8				txa
*				clc
.99				rts
*--------------------------------------
* UnloadLib
*  in :
*   A = hMem To Lib
* out :
*--------------------------------------
K.UnloadLib		pha
				jsr K.GetMemPtr
				>STYA .2+1

				ldy #S.MEM.REFCNT
				lda (ZPMemMgrSPtr),y
				dec
				bne .8

				ldx #LIBMGR.UNLOAD
.2				jsr $ffff				Self Modified, Call LIB.UNLOAD function
				pla
				jmp K.FreeMem

.8				sta (ZPMemMgrSPtr),y
				pla
*				clc
				rts
*/--------------------------------------
* # LoadDrv
* ## ASM
* **In:**
*  Y,A = PTR to "NAME.DRV [PARAM]" C-String
* ## RETURN VALUE
* none
*\--------------------------------------
K.LoadDrv		>STYA .2+1				Save "BIN\0ARGS\0\0"

				jsr PS.Find.DRV
				bcs .9

				jsr BIN.Load			K.Buf256 = "/PATH/BIN"
				bcs .9

				>STYA .3+1

				ldx #$ff
			
.1				inx						Skip "BIN"

.2				lda $ffff,x
				bne .1

				txa
				sec
				adc .2+1
				tay
				lda #0
				adc .2+2				Y,A = ARGS

.3				jsr $ffff				SELF MODIFIED, call Dev.Detect
				jsr BIN.Load.Free
				bcs .9

				lda #0					Make sure RC = 0 if success
*				clc
.9				rts
*--------------------------------------
* BIN.Load
*  in :
*   K.Buf256 = /BIN/PATH (CSTR)
* out :
*   Y,A = MEMPTR	
*   X = hMem of Code Segment
*--------------------------------------
BIN.Load		lda A2osX.FSID
				bne .11
				
				ldx #$ff
				
.10				inx
				lda K.Buf256,x
				beq .11
				
				cmp #'a'
				bcc .10

				cmp #'z'+1
				bcs .10

				eor #$20
				sta K.Buf256,x
				bra .10

.11				ldx #0

.1				cpx Mem.LastSlot
				beq BIN.Load.1

				inx						we will skip Slot 0...
				txa
				jsr MEM.GetMemByID
				lda (ZPMemMgrSPtr)
				bpl .1					In Use?

				ldy #S.MEM.BIN			any BIN PATH in this slot?	
				lda (ZPMemMgrSPtr),y
				beq .1

				jsr K.GetMemPtr			X unmodified, get pathname
				>STYA .3+1

				ldy #0
				
.2				lda K.Buf256,y
.3				cmp $ffff,y				SELF MODIFIED
				bne .1

				iny
				ora #0
				bne .2

				stx BIN.hMem			save hMem
				txa
				jsr MEM.GetMemByID
				jsr Mem.IncRefCnt
				clc
				jmp MEM.GetMEMPTR		Y,A=Ptr
*--------------------------------------
* STAT Already called by Filesearch in FindDRV, FindLIB
* STAT Already called by PS.Load (TXT,BIN,SYS)
*--------------------------------------
BIN.Load.1		>PUSHWI K.Buf256
				>PUSHBI	O.RDONLY
				>PUSHBI S.FI.T.BIN
				>LDYA K.S.STAT+S.STAT.P.AUXTYPE
				>STYA BIN.R.CS.Start
				>PUSHYA					Push AUXTYPE

				jsr K.LoadFile
				bcs .9					Error Loading file

				stx BIN.hMem			save hMem

				pha						YA=CODE+DATA size
				tya
*				clc
				adc BIN.R.CS.Start		Get AUXTYPE for actual Base Address
				sta BIN.R.End			compute Range End=AUXTYPE+FILELEN
				pla
				adc BIN.R.CS.Start+1
				sta BIN.R.End+1

				txa
				jsr K.GetMemPtr
				>STYA ZPPtr1			set ZPPtr1 -> New Code start

				jsr BIN.CompOfsYA		YA = actual load address

				jsr Mem.SetOwnerPS0		this CS is owned by PS 0

				ldy #H.BIN.CS.SIZE		get Code Len
				lda (ZPPtr1),y
				pha
				iny
				lda (ZPPtr1),y
				ply
				jsr BIN.CompCSEndYA
				jsr BIN.RelExe

				>LDYAI K.Buf256			get back bin path
				jsr K.strdup			make a copy of this string
				bcs BIN.Load.Free

				jsr Mem.SetOwnerPS0		Make BINPATH owned by PS0

				lda BIN.hMem			Keep X=new string hMem		
				jsr MEM.GetMemByID		X unmodified
				lda (ZPMemMgrSPtr) 
				ora #S.MEM.F.CODE		This is a code segment
				sta (ZPMemMgrSPtr)

				txa						Get Back Cmd line hMem
				ldy #S.MEM.BIN
				sta (ZPMemMgrSPtr),y
				ldx BIN.hMem			setup X = CS hMem
				clc
				jmp MEM.GetMEMPTR		...and Y,A=PTR to CS

.9				rts

BIN.Load.Free	php
				pha
				lda BIN.hMem
				jsr K.FreeMem
				pla
				plp
				rts						Discard Loaded Code, exits CS
*/--------------------------------------
* # InsDrv
* ## C
* `void * insdrv (void * src, void * crvcsstart, void * drvcssize, void * drvend);`
* ## ASM
* **In:**
* `>PUSHW DRV.END`
* `>PUSHW DRV.CS.SIZE`
* `>PUSHW DRV.CS.START`
* `>LDYA L.SRC`
* `>SYSCALL insdrv`
* ## RETURN VALUE
* Y,A = Ptr to installed driver
*\--------------------------------------
K.InsDrv		>STYA ZPPtr3			SRC PTR for move
				>PULLW BIN.R.CS.Start
				>PULLYA					DRV.CS.SIZE
				jsr BIN.CompCSEndYA
				>PULLA					BIN.R.End
				sta BIN.R.End
				sec
				sbc BIN.R.CS.Start
				tay

				>PULLA
				sta BIN.R.End+1
				sbc BIN.R.CS.Start+1
				>STYA BIN.R.Size
				jsr MEM.GetKrnlBuf		Y,A = CODE + DATA
				bcs .9					No More Room to load Driver

				>STYA ZPPtr1			Y,A = DST Buf for RelDrv...
				>STYA ZPPtr4			...for move...
				sty .7+1				...and for exit
				sta .8+1
				jsr BIN.CompOfsYA

				lda BIN.R.Size
				eor #$ff
				tax
				lda BIN.R.Size+1
				eor #$ff
				pha
				ldy #0
.1				inx
				bne .2
				pla
				inc
				beq .3
				pha
.2				lda (ZPPtr3),y
				sta (ZPPtr4),y
				iny
				bne .1
				inc ZPPtr3+1
				inc ZPPtr4+1
				bra .1
.3				jsr BIN.RelDrv			Relocate at Ptr1
.7				ldy #$ff				SELF MODIFIED
.8				lda #$ff				SELF MODIFIED
.9				rts
*--------------------------------------
BIN.RelExe		ldy #H.BIN.T+1
				lda (ZPPtr1),y
				cmp /H.BIN.T.BIN65
				beq .1

				cmp /H.BIN.T.DRV65
				beq .1

				lda #E.IBIN
				sec
				rts

.1				ldy #H.BIN.JMP			relocate Main JMP
				jsr BIN.RelAtPtr1Y

				ldy #16					skip Header, CS.SIZE & DS.SIZE... (16 bytes)
		
				.HS 2C					bit abs
*--------------------------------------
BIN.RelDrv		ldy #2					start after CLD JMP

.1				iny
				lda (ZPPtr1),y			HI in A
				beq .2					$00xx = end of table

				dey
				jsr BIN.RelAtPtr1Y

				iny
				bne .1
				
				inc ZPPtr1+1

				bra .1
*--------------------------------------
.2				jsr SHARED.AddYp12P1	add current offset in Y to Ptr
*										+ 1 to skip last 00 from beq .2
*										ZPPtr1=Current Code PTR
				lda BIN.R.CS.End
				clc
				adc BIN.R.Offset
				sta BIN.R.Stop
				lda BIN.R.CS.End+1
				adc BIN.R.Offset+1
				sta BIN.R.Stop+1

.3				lda (ZPPtr1)			get OPCODE
				lsr						/2
				tax
				lda BIN.65816,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 .5					no....
				and #7					save Opcode length...
				pha
				ldy #1
				jsr BIN.RelAtPtr1Y
				pla						get back Opcode length...

.5				tay						A = OPCODE length
				jsr SHARED.AddY2P1
		
.6				cmp BIN.R.Stop			A = ZPPtr1
				bne .3

				lda ZPPtr1+1
.7				cmp BIN.R.Stop+1
				bne .3					next opcode....

				clc						Make sure exit with CC
				rts
*--------------------------------------	YA = CS Size
BIN.CompCSEndYA	pha
				tya
				clc
				adc BIN.R.CS.Start
				sta BIN.R.CS.End		ZPPtr1 + CodeLen
				pla
				adc BIN.R.CS.Start+1
				sta BIN.R.CS.End+1
				rts
*-------------------------------------- YA = Target Address
BIN.CompOfsYA	pha
				tya
				sec
				sbc BIN.R.CS.Start
				sta BIN.R.Offset
				pla
				sbc BIN.R.CS.Start+1
				sta BIN.R.Offset+1
				rts
*--------------------------------------
BIN.RelAtPtr1Y	lda (ZPPtr1),y			Get LO
				tax						save LO in X
				iny						make Y point to HI
				lda (ZPPtr1),y			Get HI
				cpx BIN.R.CS.Start
				sbc BIN.R.CS.Start+1
				bcc .9					addr < BIN.R.CS.Start, out of range
				txa						Get back LO
				cpx BIN.R.End
				lda (ZPPtr1),y			Get HI
				sbc BIN.R.End+1
				bcs .9					addr > BIN.R.End, out of range
				txa						Get back LO
				dey
				clc
				adc BIN.R.Offset
				sta (ZPPtr1),y

				iny
				lda (ZPPtr1),y
				adc BIN.R.Offset+1
				sta (ZPPtr1),y
.9				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.bin
LOAD usr/src/sys/kernel.s
ASM
