NEW
  AUTO 3,1
*---------------------------------------
FIO.Init.RTS	rts

FIO.Init		>LDYAI 256
				>SYSCALL GetMem
				bcs FIO.Init.RTS

				>STYA ZPLineBuf
				txa
				>STA.G SRC.hLineBuf

				>LDYAI 256
				>SYSCALL GetMem
				bcs FIO.Init.RTS

				>STYA ZPTmpBuf
				txa
				>STA.G SRC.hTmpBuf
				
				>LDYA L.ASM.6502
FIO.LOAD.CPU	>STYA ZPPtr2

				ldy #S.PS.hARGV
				lda (pPS),y
				>SYSCALL GetMemPtr

				>STYA ZPPtr1
				ldy #$ff
				ldx #$ff
				
.1				iny
				inx
				lda (ZPPtr1),y
				sta ASM.CPU.FILE,x
				bne .1

				lda #'.'
				sta ASM.CPU.FILE,x
				ldy #$ff

.2				iny
				inx
				lda (ZPPtr2),y
				jsr SRC.IsLetterOrDigit
				bcs .3
				
				sta ASM.CPU.FILE,x
				bra .2
				
.3				stz ASM.CPU.FILE,x
				
				>LDA.G ASM.hCPUFILE
				beq .4
				
				>SYSCALL FreeMem
				
				>STZ.G ASM.hCPUFILE
				
.4				>PUSHW L.MSG.CPU.FILE
				>PUSHW L.ASM.CPU.FILE
				>PUSHBI 2
				jsr OUT.Print
				
				>PUSHW L.ASM.CPU.FILE
				>PUSHBI	O.RDONLY
				>PUSHBI S.FI.T.BIN
				>PUSHWZ				Aux type
				>SYSCALL LoadFile
				bcs .9

				txa
				>STA.G ASM.hCPUFILE
				
				>SYSCALL GetMemPtr
				>STYA ZPPtr1

				ldy #ASM.T.AM
				lda (ZPPtr1),y
				clc
				adc ZPPtr1
				sta ZPAMPtr
				iny
				lda (ZPPtr1),y
				adc ZPPtr1+1
				sta ZPAMPtr+1
				iny
				lda (ZPPtr1),y
				clc
				adc ZPPtr1
				sta ZPRPtr
				iny
				lda (ZPPtr1),y
				adc ZPPtr1+1
				sta ZPRPtr+1
				iny
				lda (ZPPtr1),y
				clc
				adc ZPPtr1
				sta ZPOpsPtr
				iny
				lda (ZPPtr1),y
				adc ZPPtr1+1
				sta ZPOpsPtr+1
.9				rts
*---------------------------------------
FIO.OpenFile	>STYA ZPPtr1
				>PUSHW L.MSG.SRC.FILE
				>PUSHW ZPPtr1
				>PUSHBI 2
				jsr OUT.Print
				
				>LDA.G SRC.Depth
				cmp #IN.MAXDEPTH
				bne .1
				
				lda #E.SRC.TOO.MANY.IN
				sec
				rts
				
.1				>PUSHW ZPPtr1
				>PUSHW ZPTmpBuf
				>SYSCALL Stat
				bcs .99
				
				ldy #S.STAT.P.TYPE
				lda (ZPTmpBuf),y
				cmp #S.FI.T.TXT
				bne .2

				ldx #O.RDONLY+O.TEXT
				bra .3
				
.2				cmp #$FA			S-C/BAS?
				bne .98
				
				ldx #O.RDONLY
				
.3				>PUSHW ZPPtr1
				txa
				>PUSHA
				ldy #S.STAT.P.TYPE
				lda (ZPTmpBuf),y
				>PUSHA
				>PUSHWZ
				>SYSCALL FOpen
				bcs .99
			
				pha
				>INC.G SRC.Depth
				
				clc
				adc #SRC.hFILES-1
				tay
				pla
				sta (pData),y
				>LDA.G SRC.Depth
				clc
				adc #SRC.hFILETYPES-1
				tay

				phy
				ldy #S.STAT.P.TYPE
				lda (ZPTmpBuf),y
				ply
				sta (pData),y

				lda #0
				>STA.G SRC.LINENUM
				iny
				sta (pData),y
				
				clc
				rts
				
.98				lda #E.SRC.INV.TYPE
				sec
.99				rts
*---------------------------------------
FIO.ReadLine	>LDA.G SRC.Depth
				clc
				adc #SRC.hFILETYPES-1
				tay
				lda (pData),y
				bmi .10

				jsr FIO.GethFile
				>PUSHW ZPLineBuf
				>PUSHWI 256
				jsr FIO.ReadFromFile
				bcs .19 

				tya
				beq .13

				dey
				lda (ZPLineBuf),y
				eor #C.CR
				beq .22

				iny
.22				lda #0				replace ending $0D with $00
.13				sta (ZPLineBuf),y

				>INC.G SRC.LINENUM
				bne .19

				>INC.G SRC.LINENUM+1

.19				rts
*---------------------------------------
.10				jsr FIO.GethFile
				>PUSHW ZPTmpBuf
				>PUSHWI 3
				jsr FIO.ReadFromFile
				bcs .19 

				ldy #1
				lda (ZPTmpBuf),y
				pha
				iny
				lda (ZPTmpBuf),y
				>STA.G SRC.LINENUM+1
				pla
				>STA.G SRC.LINENUM


				jsr FIO.GethFile
				>PUSHW ZPTmpBuf

				lda #0					LENHI = 0
				>PUSHA
				
				lda (ZPTmpBuf)			LEN
				sec
				sbc #3
				bcc .9					LEN should be at least 3

				>PUSHA
				jsr FIO.ReadFromFile
				bcs .9

				>LDYA ZPTmpBuf
				>STYA ZPPtr1
				ldy #0
.1				lda (ZPPtr1)
				inc ZPPtr1
				bne .11

				inc ZPPtr1+1
.11				tax
				bmi .2

				sta (ZPLineBuf),y
				beq .8				Ending 00

				iny
				bne .1

				bra .99

.2				cmp #$C0			REPEAT char?
				bne .5

				lda (ZPPtr1)		Get Repeat Count
				inc ZPPtr1
				bne .3

				inc ZPPtr1+1

.3				tax

				lda (ZPPtr1)		Get Repeat Char
				inc ZPPtr1
				bne .4

				inc ZPPtr1+1

.4				sta (ZPLineBuf),y
				iny
				beq .99

				dex
				bne .4

				bra .1

.5				and #$3F			Compute blank count
				tax
				lda #$20

.6				sta (ZPLineBuf),y
				iny
				beq .99

				dex
				bne .6

				bra .1

.8				clc
.9				rts

.99				lda #E.LINE.TOO.LONG
				sec
				rts
*--------------------------------------
FIO.GethFile	>LDA.G SRC.Depth
				clc
				adc #SRC.hFILES-1
				tay
				lda (pData),y
				>PUSHA
				rts
*--------------------------------------
FIO.ReadFromFile
				>SYSCALL FRead
				bcs .9

				tax						$100 byte transfered ?
				beq .9

				lda #E.LINE.TOO.LONG
				sec
.9				rts
*---------------------------------------
FIO.FileClose	>LDA.G SRC.Depth
				beq .8
				
				dec
				sta (pData),y
				
				sec
				adc #SRC.hFILES-1
				tay
				lda (pData),y
				>SYSCALL FClose
				
.8				clc
				rts
*---------------------------------------
MAN
SAVE usr/src/bin/asm.s.fio
LOAD usr/src/bin/asm.s
ASM
