NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/csh
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/MLI.I
				.INB INC/MLI.E.I
*--------------------------------------
				.INB USR/SRC/BIN/CSH.E
*--------------------------------------
* Zero Page Segment, up to 32 bytes
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPFileBuf		.BS 2
ZPFileBufPtr	.BS 2
ZPFileBufPtrBak	.BS 2

ZPCSHConst		.BS 2
ZPCSHData		.BS 2
ZPCSHStack		.BS 2

ZPVarID			.BS 2

ZPVarDef		.EQ *
ZPVarType		.BS 2
ZPVarSizeOf		.BS 2
ZPVarDataPtr	.BS 2

ZPhMacro		.BS 1

ZPPtr1			.BS 2
ZPPtr2			.BS 2


ArgIndex		.BS 1

ZPPtr3			.BS 2

ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #0					S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #64					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.MSG.GREETINGS	.DA MSG.GREETINGS
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.ECHOCRLF	.DA MSG.ECHOCRLF
L.MSG.DEBUG		.DA MSG.DEBUG
L.MSG.ERR		.DA MSG.ERR
L.MSG.DBGVAR	.DA MSG.DBGVAR
				.INB USR/SRC/BIN/CSH.R
				.DA 0
*--------------------------------------
* Called once at process creation
* Put code for loading LIB here
*--------------------------------------
CS.INIT			clc
CS.INIT.RTS		rts
*--------------------------------------
* Called until exit with CS
* if RUN exits with CC, RN entered again
*--------------------------------------
CS.RUN			>PUSHW L.MSG.GREETINGS
				>PUSHB #K.VER
				>PUSHB /K.VER
				>PUSHBI 2
				>SYSCALL printf
				bcs CS.INIT.RTS

				jsr CS.RUN.ARGS
				bcs CS.INIT.RTS

				jsr CSH.Init
				bcs CS.INIT.RTS

.1				>SLEEP

				>LDA.G bDebug
				bpl .2
				jsr PrintDebugMsg
.2				>LDA.G bTrace
				bpl .3
				>LDYA ZPFileBufPtr
				jsr PrintTraceMsg

.3				jsr CSH.Run
				bcc .1
				cmp #MLI.E.EOF
				beq .8

				pha
				jsr PrintErrMsg
				pla
				sec
				rts

.8				lda #0					Exit Code = Success
				sec
				rts
*--------------------------------------
CS.RUN.ARGS		inc ArgIndex	

				lda ArgIndex
				>SYSCALL ARGV
				bcs .7
		
				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #'-'
				bne .4

				ldy #1
				lda (ZPPtr1),y

				ldx #OptionVars-OptionList-1

.1				cmp OptionList,x
				beq .2

				dex
				bpl .1

				bra .90

.2				ldy OptionVars,x
				lda #$ff
				sta (pData),y
				bra CS.RUN.ARGS

.4				>LDA.G hFileBuf
				bne .90

				>PUSHW ZPPtr1
				>PUSHBI	O.RDONLY
				>PUSHBI S.FI.T.TXT
				>PUSHWZ					Aux type
				>SYSCALL LoadTxtFile
				bcs .9

				txa
				>STA.G hFileBuf
				>SYSCALL GetMemPtr
				>STYA ZPFileBuf
				>STYA ZPFileBufPtr
				bra CS.RUN.ARGS

.7				>LDA.G hFileBuf
				beq .90

				clc
.9				rts

.90				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL printf

				lda #E.SYN
				sec						QUIT Process
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			jsr CSH.Quit
				clc
				rts
*--------------------------------------
PrintErrMsg		lda ZPhMacro
				beq .10

				lda ZPFileBufPtrBak
				sta ZPFileBufPtr
				
				lda ZPFileBufPtrBak+1
				sta ZPFileBufPtr+1
				
.10				>LDYA ZPFileBuf
				>STYA ZPPtr1
				stz ZPPtr2
				stz ZPPtr2+1			Line counter

.1				inc ZPPtr2
				bne .2
				inc ZPPtr2+1

.2				>LDYA ZPPtr1
				>STYA ZPPtr3			save line start
.20				lda (ZPPtr1)
				beq .4					EoF

				inc ZPPtr1
				bne .3
				inc ZPPtr1+1
.3				cmp #C.CR
				bne .20					Scan until EoL
				ldx ZPPtr1
				cpx ZPFileBufPtr
				lda ZPPtr1+1
				sbc ZPFileBufPtr+1
				bcc .1					not this line....

.4				>LDA.G bTrace
				bmi .5
				
				jsr PrintTraceMsg.3

.5				lda ZPPtr3
				cmp ZPFileBufPtr
				bne .6
				lda ZPPtr3+1
				cmp ZPFileBufPtr+1 
				beq .8
.6				>PUSHBI '-'
				ldy #S.PS.hStdErr
				lda (pPS),y
				>SYSCALL fputc
				inc ZPPtr3
				bne .5
				inc ZPPtr3+1
				bra .5
				
.8				ldy #S.PS.hStdErr
				lda (pPS),y
				>PUSHA
				>PUSHW L.MSG.ERR
				>PUSHW ZPPtr2		Line counter
				>PUSHBI 2
				>SYSCALL fprintf

				rts
*--------------------------------------
PrintDebugMsg	>LDYA pStack
				>STYA ZPPtr2
				>PUSHW L.MSG.DEBUG
				
				>PUSHW ZPPtr2
				
				>PUSHBI 2
				>SYSCALL printf
				rts
*--------------------------------------
PrintTraceMsg	>STYA ZPPtr3

PrintTraceMsg.3	>PUSHBI '>'
				ldy #S.PS.hStdErr
				lda (pPS),y
				>SYSCALL fputc
				
				ldy #$ff

.1				iny
				lda (ZPPtr3),y
				beq .8
				
				cmp #C.CR
				beq .8
				
				phy
				>PUSHA
				ldy #S.PS.hStdErr
				lda (pPS),y
				>SYSCALL fputc
				ply
				bra .1
				
.8 				ldy #S.PS.hStdErr
				lda (pPS),y
				>PUSHA
				
				>PUSHW L.MSG.ECHOCRLF
				>PUSHBI 0
				>SYSCALL fprintf

				rts
*--------------------------------------
				.INB USR/SRC/BIN/CSH.S.CORE
				.INB USR/SRC/BIN/CSH.S.EXEC
*--------------------------------------
CS.END
*--------------------------------------
* Initialized DATA
*--------------------------------------
MSG.Greetings	.AZ "\r\nCSH-Shell %d.%d\r\n\r\n"
MSG.USAGE		.AS "Usage : CSH <option> file\r\n"
				.AS "   -D : Debug Mode\r\n"
				.AS "   -T : Trace On"
MSG.ECHOCRLF	.AZ "\r\n"
MSG.DEBUG		.AZ "pStack=%H"
MSG.ERR			.AZ "^\r\nLine #%D:"
MSG.DBGVAR		.AZ "K=%H, T=%H, S=%H, P=%H, V=%L\r\n"
*--------------------------------------
OptionList		.AS "DdTt"
OptionVars		.DA #bDebug,#bDebug,#bTrace,#bTrace
*--------------------------------------
				.INB USR/SRC/BIN/CSH.I
*--------------------------------------
				.DUMMY
				.OR 0
DS.START		.INB USR/SRC/BIN/CSH.D
bDebug			.BS 1
bTrace			.BS 1
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/CSH.S
ASM
