PR#3
PREFIX /A2OSX.SRC
LOMEM $C00
INC 1
AUTO 6
					.LIST OFF
					.OP	65C02
					.OR $2000
					.TF	/A2OSX.DEV/BIN/ASM
*---------------------------------------
					.INB INC/MACROS.I
					.INB INC/A2OSX.I
					.INB INC/LIBSTR.I
*---------------------------------------
ZPPtr1				.EQ ZPBIN
ZPPtr2				.EQ ZPBIN+2
ZPPtr3				.EQ ZPBIN+4
ZPBlockPtr			.EQ ZPBIN+6
ZPSymbolPtr			.EQ ZPBIN+8
*---------------------------------------
ASM.T.AM			.EQ 0
ASM.T.R				.EQ 2
ASM.T.O				.EQ 4
*---------------------------------------
SRC.DO.MAXDEPTH		.EQ 8
SRC.IN.MAXDEPTH		.EQ 7
SRC.GLABEL.MAXLEN	.EQ 32
*---------------------------------------
SYM.GBLOCK.MAX		.EQ 96				96*256=24k
SYM.SBLOCK.MAX		.EQ 16				16*256=4k
SYM.PBLOCK.MAX		.EQ 32				32*256=8k
SYM.MBLOCK.MAX		.EQ 32				32*256=8k
*---------------------------------------
ERR.INV.ARGS		.EQ $80
ERR.SRC.INV.TYPE	.EQ $90
ERR.SRC.TOO.MANY.IN	.EQ $91
ERR.SRC.UNEXP.EOF	.EQ $92
ERR.OOM				.EQ $93
ERR.SYNTAX.ERROR	.EQ $A0
ERR.SYM.TOO.LONG	.EQ $A1
ERR.VAL.TOO.BIG		.EQ $A2
ERR.LINE.TOO.LONG	.EQ $A3
ERR.INVALID.LABEL	.EQ $A4
ERR.INVALID.DIRECTIVE	.EQ $A5
ERR.INVALID.OPCODE	.EQ $A6
ERR.INVALID.AM.SYN	.EQ $A7
ERR.INVALID.AM.4.OC	.EQ $A8
ERR.RANGE			.EQ $A9
ERR.UNDEF.SYMBOL	.EQ $AA
ERR.SYMBOL.REDEFINE	.EQ $AB
ERR.TOO.MANY.DO		.EQ $AC
ERR.MISSING.EXP		.EQ $B0
ERR.EXP.SYN.ERROR	.EQ $B1
ERR.INVALID.MACRO.DEF	.EQ $E0
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START			cld
					jmp (.1,x)
					.DA #$61			6502,Level 1 (65c02)
					.DA #1				BIN Layout Version 1
					.DA 0
					.DA CS.END-CS.START
					.DA DS.END-DS.START	Data Segment to Allocate
					.DA 0
					.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1					.DA CS.INIT
					.DA CS.RUN
					.DA CS.DOEVENT		
					.DA	CS.QUIT
L.LIBSTR			.DA LIBSTR
L.MSG.HELP			.DA MSG.HELP
L.MSG.SRCLINE		.DA MSG.SRCLINE
L.MSG.ERROR			.DA MSG.ERROR
L.MSG.SYMBOLS		.DA MSG.SYMBOLS
L.MSG.GSYMBOL		.DA MSG.GSYMBOL
L.MSG.LSYMBOL		.DA MSG.LSYMBOL
L.MSG.SUMMARY		.DA MSG.SUMMARY
L.MSG.PASS			.DA MSG.PASS
L.MSG.SRC.FILE		.DA MSG.SRC.FILE
L.MSG.OBJ.FILE		.DA MSG.OBJ.FILE
L.MSG.T.FILE		.DA MSG.T.FILE
L.SRC.AM.StrBuf		.DA SRC.AM.StrBuf
L.SRC.GLabel.Flags	.DA SRC.GLabel.Flags
L.SRC.GLabel.Len	.DA SRC.GLabel.Len
L.SRC.ELabel.Flags	.DA SRC.ELabel.Flags
L.SRC.ELabel.Len	.DA SRC.ELabel.Len
L.SRC.BUFFER		.DA SRC.BUFFER
L.ASM.T.FILENAME	.DA ASM.T.FILENAME
L.T.DIRECTIVES		.DA T.DIRECTIVES
J.DIRECTIVES		.DA DIR.AC
					.DA DIR.AS
					.DA DIR.AT
					.DA DIR.AZ
					.DA DIR.BS
					.DA DIR.DA
					.DA DIR.DO
					.DA DIR.DU
					.DA DIR.DU
					.DA DIR.ED
					.DA DIR.EL
					.DA DIR.EM
					.DA DIR.EN
					.DA DIR.EP
					.DA DIR.EQ
					.DA DIR.FI
					.DA DIR.HS
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.IN
					.DA DIR.LI
					.DA DIR.LI
					.DA DIR.MA
					.DA DIR.OP
					.DA DIR.OR
					.DA DIR.PG
					.DA DIR.PH
					.DA DIR.SE
					.DA DIR.TA
					.DA DIR.TF
					.DA DIR.TI
					.DA DIR.US
L.T.LI				.DA T.LI
J.LI				.DA DIR.LI.CON
					.DA DIR.LI.COFF
					.DA DIR.LI.XON
					.DA DIR.LI.XOFF
					.DA DIR.LI.ON
					.DA DIR.LI.OFF
J.EXP.OP			.DA EXP.OP.EOR ^!|&<=>+-*/
					.DA EXP.OP.ORA
					.DA EXP.OP.ORA
					.DA EXP.OP.AND
					.DA EXP.OP.LOW
					.DA EXP.OP.EQU
					.DA EXP.OP.GRT
					.DA EXP.OP.ADD
					.DA EXP.OP.SUB
					.DA EXP.OP.MUL
					.DA EXP.OP.DIV	
					.DA 0
*---------------------------------------
CS.INIT				>LDYA L.LIBSTR
					>SYSCALL SYS.LoadLibYA
					sta hLIBSTR
					
					>SYSCALL SYS.GetArgC
					cmp #1
					beq .99					No arg, new file....
					
					lda #1
					>SYSCALL SYS.GetArgA
					>SYSCALL SYS.GetFullPathYA
					
					txa
					ldy #SRC.hFILENAME
					sta (pData),y
					

					jsr FIO.Init
					bcs .9
					
					jsr SYM.Init
					bcs .9
					
					jsr DIR.Init
					
					lda (pPs)
					ora #S.PS.F.EVENT
					sta (pPs)

					clc
					rts
					
.99					>PUSHW L.MSG.HELP
					>LIBCALL hLIBSTR,LIBSTR.PRINTF
					lda #ERR.INV.ARGS
					sec
.9					rts
*--------------------------------------
CS.RUN				ldy #bCANCEL
					lda (pData),y
					bne .99

					ldy #bSTOP
					lda (pData),y
					bne .8
					
					ldy #SRC.COUNT		root file is already opened?
					lda (pData),y
					bne .10 
					
					ldy #ASM.PASS
					lda (pData),y
					inc
					>PUSHA
					>PUSHW L.MSG.PASS
					>LIBCALL hLIBSTR,LIBSTR.PRINTF
					
					ldy #SRC.hFILENAME
					lda (pData),y
					
					jsr FIO.OpenFileA
					bcs .99
					
.10					jsr FIO.ReadLine
					bcc .2
					cmp #$4C			End Of File?
					bne .9
					
					jsr FIO.FileClose
					
					ldy #SRC.COUNT		end of root file ?
					lda (pData),y
					bne .8				no continue back to previous file
					
					ldy #ASM.PASS
					lda (pData),y		End of pass #2 ??
					bne .1
					inc
					sta (pData),y
					clc
					rts
					
.1					jsr SYM.Dump

					>PUSHW L.MSG.SUMMARY
					>LIBCALL hLIBSTR,LIBSTR.PRINTF
					
					lda #0				End of assembly, exit with no error
.99					sec		
					rts
					
.2					jsr SRC.PrintLine
					
					jsr SRC.ParseLine
					bcs .9
					
.8					clc
					rts

.9					pha
					jsr SRC.PrintLineErr
					pla
					pha
					>PUSHA
					>PUSHW SRC.LINENUM
					>PUSHW L.MSG.ERROR
					>LIBCALL hLIBSTR,LIBSTR.PRINTF
					pla
					sec
					rts
*--------------------------------------
CS.DOEVENT			ldy #S.EVT.hDEV		is Event from active IN device?
					lda (pEvent),y
					ldy #S.PS.hINDEV
					cmp (pPs),y
					bne .9
					lda (pEvent)
					and #S.EVT.F.KEY		is it a KEY event?
					beq .9
					ldy #S.EVT.DATAHI		is it an O or SAPPLE key ?
					lda (pEvent),y
					bne	.9
					ldy #S.EVT.DATALO	
					lda (pEvent),y
					cmp #$03				Ctrl-C
					bne .1
					
					lda #$FF
					ldy #bCANCEL
					sta (pData),y
					bra .8
					
.1					cmp #$13				Ctrl-S
					bne .8
					
					ldy #bSTOP
					lda (pData),y
					eor #$FF
					sta (pData),y
					
.8					clc
					rts
					
.9					sec
					rts
*--------------------------------------
CS.QUIT				jsr FIO.FileClose
					bne CS.QUIT 

					jsr SYM.Quit
					
					ldy #ASM.T.hMem
					lda (pData),y
					beq .1
					>SYSCALL SYS.FreeMemA
					
.1					ldy #SRC.hFILENAME
					lda (pData),y
					beq .8
					
					>SYSCALL SYS.FreeMemA
					
.8					lda hLIBSTR
					>SYSCALL SYS.UnloadLibA
					clc
					rts
*---------------------------------------
					.INB BIN/ASM.S.DIR
					.INB BIN/ASM.S.EXP
					.INB BIN/ASM.S.FIO
					.INB BIN/ASM.S.SRC
					.INB BIN/ASM.S.SYM
*---------------------------------------
CS.END
SRC.DIR.MAXLEN		.EQ 5
T.DIRECTIVES		.HS 02
					.AS "AC"
					.HS 02
					.AS "AS"
					.HS 02
					.AS "AT"
					.HS 02
					.AS "AZ"
					.HS 02
					.AS "BS"
					.HS 02
					.AS "DA"
					.HS 02
					.AS "DO"
					.HS 02
					.AS "DU"
					.HS 05
					.AS "DUMMY"
					.HS 02
					.AS "ED"
					.HS 04
					.AS "ELSE"
					.HS 02
					.AS "EM"
					.HS 02
					.AS "EN"
					.HS 02
					.AS "EP"
					.HS 02
					.AS "EQ"
					.HS 03
					.AS "FIN"
					.HS 02
					.AS "HS"
					.HS 02
					.AS "IN"
					.HS 03
					.AS "INB"
					.HS 04
					.AS "INB1"
					.HS 04
					.AS "INB2"
					.HS 04
					.AS "INB3"
					.HS 04
					.AS "INB4"
					.HS 04
					.AS "INB5"
					.HS 04
					.AS "INB6"
					.HS 04
					.AS "INB7"
					.HS 04
					.AS "INB8"
					.HS 04
					.AS "INB9"
					.HS 02
					.AS "LI"
					.HS 04
					.AS "LIST"
					.HS 02
					.AS "MA"
					.HS 02
					.AS "OP"
					.HS 02
					.AS "OR"
					.HS 02
					.AS "PG"
					.HS 02
					.AS "PH"
					.HS 02
					.AS "SE"
					.HS 02
					.AS "TA"
					.HS 02
					.AS "TF"
					.HS 02
					.AS "TI"
					.HS 02
					.AS "US"
					.HS 00
*---------------------------------------
T.LI				.HS 03
					.AS "CON"
					.HS 04
					.AS "COFF"
					.HS 03
					.AS "XON"
					.HS 04
					.AS "XOFF"
					.HS 02
					.AS "ON"
					.HS 03
					.AS "OFF"
					.HS 00
*---------------------------------------
LIBSTR				>PSTRING "libstr.o"
MSG.HELP			>CSTRING "A2osX-Macro Assembler (S-C MASM 3.0 Based)\nUsage : ASM <src file> [type TXT ($04) or S-C/BAS ($FA)]\n"
MSG.PASS			>CSTRING "Pass:#%d\n"
MSG.SRC.FILE		>CSTRING "Reading SRC File:%S\n"
MSG.OBJ.FILE		>CSTRING "Writing OBJ File:%S, Type=%02x\n"
MSG.T.FILE			>CSTRING "Loading CPU File:%S\n"
MSG.SRCLINE			>CSTRING "%H:        %05D-%s\n"
MSG.ERROR			>CSTRING "%05D-Error:$%h\n"
MSG.SYMBOLS			>CSTRING "Symbol Table:\n"
MSG.GSYMBOL			>CSTRING "$%H%H:%S"
MSG.LSYMBOL			>CSTRING " .%d:+$%h"
MSG.SUMMARY			>CSTRING "\nEnd Of Assembly.\n"
SRC.AM.RESERVED		>PSTRING "[](),"
SRC.MOD.RESERVED	>PSTRING "#/<>"
SRC.EXP.RESERVED	>PSTRING "^!|&<=>+-*/"
ASM.T.DEFAULT		.AS ".T.6502"
ASM.T.FILENAMELEN	.BS 1
ASM.T.FILENAME		.BS 65
hLIBSTR				.BS	1

FIO.hFileName		.BS 1
FIO.hFullPath		.BS 1

EXP.Operator		.BS	1
EXP.Modifier		.BS 1

SYM.iCurGBlock.Save	.BS 1
SYM.pCurGBlock.Save	.BS 1

SRC.LINENUM			.BS 2
SRC.Directive.ID	.BS 1
SRC.Keyword.ID		.BS 1
SRC.ACC				.BS 4
SRC.ACCTMP			.BS 4
SRC.ARG				.BS 4
SRC.AM.ID			.BS 1
SRC.AM.StrBuf		.BS 32
SRC.AM.tmpBuf		.BS 32
SRC.Exp				.BS 128
SRC.BufPtr			.BS 1
SRC.BufPtrSave		.BS 1
SRC.Buffer			.BS 256
SRC.GLabel.New		.BS 1
SRC.GLabel.Flags	.BS 1
SRC.GLabel.Value	.BS 4
SRC.GLabel.Len		.BS 1
SRC.GLabel.Name		.BS	SRC.GLABEL.MAXLEN
SRC.LLabel.ID		.BS 1
SRC.LLabel.Offset	.BS 1
SRC.ELabel.Flags	.BS 1
SRC.ELabel.Value	.BS 4
SRC.ELabel.Len		.BS 1
SRC.ELabel.Name		.BS	SRC.GLABEL.MAXLEN
SRC.FLabel.Flags	.BS 1
SRC.FLabel.Value	.BS 4
SRC.FLabel.Len		.BS 1
SRC.FLabel.Name		.BS	SRC.GLABEL.MAXLEN
*--------------------------------------
					.DUMMY
					.OR 0
DS.START				
bSTOP				.BS 1
bCANCEL				.BS 1

SRC.hFILENAME		.BS 1

SRC.COUNT			.BS 1
SRC.hREFNUMS		.BS SRC.IN.MAXDEPTH
SRC.hBUFFERS		.BS SRC.IN.MAXDEPTH
SRC.hFILETYPES		.BS SRC.IN.MAXDEPTH

DST.hFILENAME		.BS 1
DST.hREFNUM			.BS 1
DST.hBUFFER			.BS 1
DST.hFILETYPE		.BS 1

ASM.T.hMem			.BS 1

SYM.hCurSBlock		.BS 1
SYM.iCurSBlock		.BS	1
SYM.LastSBlock		.BS 1
SYM.hSBlocks		.BS SYM.SBLOCK.MAX

SYM.pCurGBlock		.BS 1
SYM.iCurGBlock		.BS	1
SYM.pLastGBlock		.BS 1
SYM.iLastGBlock		.BS 1
SYM.hGBlocks		.BS SYM.GBLOCK.MAX

SYM.hCurPBlock		.BS 1
SYM.iCurPBlock		.BS	1
SYM.LastPBlock		.BS	1
SYM.hPBlocks		.BS SYM.PBLOCK.MAX

SYM.hCurMBlock		.BS 1
SYM.iCurMBlock		.BS	1
SYM.LastMBlock		.BS	1
SYM.hMBlocks		.BS SYM.MBLOCK.MAX

SYM.DumpCount		.BS 1

DIR.Counter			.BS 2
DIR.Byte			.BS 1

ASM.PASS			.BS 1
ASM.MACRO.ON		.BS 1
ASM.DO.Count		.BS 1
ASM.DOELSE.Flag		.BS SRC.DO.MAXDEPTH
ASM.DO.ON			.BS 1
ASM.PH.ON			.BS 1
ASM.LI.ON			.BS 1
ASM.LI.CON			.BS 1
ASM.LI.XON			.BS 1
ASM.PC				.BS 4
ASM.PC.PH			.BS 4
ASM.PC.GLABEL		.BS 4
ASM.LOCAL.OFFSET	.BS 1
ASM.LOCAL.INVALID	.BS 1
ASM.SYM.ID			.BS 2
DS.END				.ED
*---------------------------------------
					.DO DS.END-DS.START>$FF
					ERROR:DS too big
					.FIN
*---------------------------------------
MAN
SAVE BIN/ASM.S
ASM
