view TL1/TL1os9.asm @ 180:c3145f913f3d default tip

possible features
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 04 May 2019 11:17:42 +0900
parents 41d578d28d8c
children
line wrap: on
line source

*******TL/1*************
         nam   tl1
         ttl   TL1 compiler

         ifp1
         use   defsfile
         endc

* Module header definitions
tylg     set   Prgrm+Objct   
atrv     set   ReEnt+rev
rev      set   $00
edition  set   1

         mod   eom,name,tylg,atrv,start,size

TXTTOP EQU $FB7E

INDN   RMB 1
OUTDN  RMB 1
LB     RMB 2
GB     RMB 2
MHIGH  RMB 1
MOD    RMB 1
WT1    RMB 1
WT2    RMB 1
RNDH   RMB 1
RNDL   RMB 1
DREG   RMB 1 
DBUF   RMB 3
XR     RMB 2
YR     RMB 2
ZR     RMB 2
arg    equ .
PFTBEG RMB 2     prog/func table
PC     RMB 2
SREG   RMB 2
SP     RMB 2
PFMAX  RMB 2 
LSW    RMB 1 
SY     RMB 1
CH     RMB 1 
VAL    RMB 1 
GL     RMB 1     global 0xff / local 1
OPER   RMB 1 
GLL    RMB 1     left value g 0xff / local l 
AMODE  RMB 1
ACC    RMB 1
LSIZE  RMB 1     local variable size (including arguments )
GSIZE  RMB 1     global variable including array
TCOUNT RMB 1     1 search reserved word only, 5 search all local/global var/array, proc
TEND   RMB 2     table end (search start from here ) include local name
WEND   RMB 2     word end
PMODE  RMB 1     0x20 main,  1 proc, 0 ?
RSW    RMB 1     0 word lookup, 0xff word register mode in tlook
GEND   RMB 2     end of global name
SSW    RMB 1
runmod RMB 1
modofs RMB 2     module library offset

filepath rmb   2
parmptr  rmb   2
stdin    rmb   2
adr      rmb   2
work     rmb   2
bufsiz   equ   $100-1

**
* LIBRARY ADDRESSS TABLE
**
LIBR     equ   .
ioentry  rmb   $80
readbuff rmb   bufsiz+1

OBJSTART RMB 10


* OBJECT PG AREA

WTBLE    RMB $100+500
MSTACK   RMB $140 

OBJECT   RMB 2048        * NOP
RUB      equ 8

size   equ .

name     fcs   /TL1/
         fcb   edition


** OBJECT START
******
C      tst    <runmod
       beq    c2
       lbra   modend
c2     leas   OBJECT,u
VARPTR lda    INDN
       lbsr  close
       clra       os9 stdin
       sta   INDN
       inca
       sta   OUTDN
       LDX   <PC
       leay  ,x
OBJMP  JMP   OBJECT,u


start    LEAY OBJECT,u
         STY PC
         lda   ,x
         cmpa  #'-'
         bne   run
         leax  1,x
         lda   ,x+
         cmpa  #'c'
         bne   run
         lbsr  modsetup
         bra   fread 
run      ldy   #-2              17 xx xx
         sty   modofs
fread    clr   <stdin
         stx   <parmptr         save parameter pointer
         stu   <work            save parameter pointer
         lda   #READ.           read access mode
         os9   I$Open           open file
         lbcs  ferr             branch if error
         sta   <INDN            else save path to file
         stx   <parmptr         and updated parm pointer
         leax  readbuff,u       buffer 
         clr   ,x               buffer empty
         stx   <adr
         lbra  comp

**
COMP   CLRA
       STA OUTDN
       STA LSW
       STA AMODE
       STA PMODE
       STA LSIZE
       STA RSW
       STA PFMAX
       leax MSTACK,u
       STX SP
       LEAX 2,x
       STX PFTBEG
       CLRB
C1     STA ,X+
       DECB
       BNE C1
       INCA
       STA TCOUNT   = 1
       LDA #' '
       STA CH
**  copy reserved word table
       LEAX WTABLE,PCR
       pshs u
       leau WTBLE,u
       ldy #WTBLEND-WTABLE
tbl1   lda ,x+
       sta ,u+
       leay -1,y
       bne tbl1
       stu TEND
       puls u
** 
       LBSR CRLF
       BSR REG0
       LBSR WORD
       CMPA #$30 PROC?
       BNE  *+4
       BSR REGNAM  
       BSR REG0
       CMPA #$31 FUNC?
       BNE  *+4
       BSR REGNAM
       CLR LSIZE
       BSR REG0
       LBSR PROG 
       LDB  LSIZE
       STB  GSIZE
       LBSR STPOUT
       LDX TEND
       STX GEND
PLOOP  LDA  SY
       CMPA #$8F /END CODE
       LBEQ ENDL
       CMPA #$4
       BCC ERR4
       DECA
       STA  PMODE
       LDA VAL
       LBSR DEFPF
       LBSR PUTHS
       FCB $04
       FDB $AFE3    stx ,--s
       FDB $3086    leax a,x
       BSR REG0
       CLR LSIZE
       LBSR WORD
       CMPA #$37     (
       BNE PL1
       BSR REGNAM
       LDA #$3B       )
       LBSR CHECK
PL1    BSR PROG
       LDB PMODE
       CMPB #1
       BNE *+5
       LBSR RETP     generate return
       LDX GEND 
       STX TEND
       LDB #5
       STB TCOUNT
       BRA PLOOP
**
* REGIST ZERO
**
REG0   LDX TEND
       CLR ,X+
       STX TEND
       INC TCOUNT
       RTS
**
*  REGIST NAME
**
REGNAM COM RSW
       LBSR WORD
       LDB RSW
       BEQ *+5
ERR4   LBRA ERROR    define duplicate name 
       LDX TEND
       LDA LSIZE
       STA ,X
       INC LSIZE
       LDX WEND
       STX TEND
       BSR WORD1
       CMPA #$36    "["   array
       BNE REG2
       BSR WORD1
       BNE ERR4
       LDA VAL
       ADDA LSIZE
       BSR WORD1 
       LDA  #$3A    ":"
       LBSR CHECK
REG2   CMPA #$3C    "," get next word
       BEQ REGNAM
       RTS
**
* PROGRAM
**
PROG   CMPA #$32 VAR?
       BNE *+4
       BSR REGNAM    global variable
       BSR REG0      put mark
       CMPA #$33 ARRAY?
       BNE *+4
       BSR REGNAM
**
*  STATEMENT 
**
STAT   BSR SSTAT
       LDB SSW
       BEQ RTS1
       LBRA ERROR 
**
* STATEMENTS LIST
**
STLIST BSR SSTAT 
       LDB SSW
       BEQ STLIST
RTS1   RTS
**
* SINGLE STATEMENT
**
SSTAT  CLRA
       STA ACC 
       STA SSW 
       LDA SY
**
*  MULTIPLE STATEMENTS
**
SS1    CMPA #$34   BEGIN
       BCS SS2
       CMPA #$38   END
       BCC SS2
       ADDA #4
       PSHS A      wait for END
       BSR WORD1
       BSR STLIST
       PULS A
       CLR SSW
       LBRA CHECK
**
* STOP
**
SS2    CMPA #$50
       BNE SS3
       BSR STPOUT
WORD1  LBRA WORD
**
STPOUT LBSR PUTHSL
       FCB 3,$16
       FDB exit
       RTS
**
* RETURN
**
SS3    CMPA #$51
       BNE SS4
       LDB PMODE
       LBEQ ERR4
       PSHS B
       BSR WORD1
       PULS B
       DECB 
       BEQ RETP
       LBSR EXPR
RETP   LDD #$3590    puls x,pc
       LBSR PUTAB
       RTS
**
* PROC CALL
**
SS4    CMPA #2        proc
       LBEQ PFCALL
       CMPA #$E0
       BCS SS5
       LBRA PFCALL
**
* ASSIGNMENT STATEMENT 
**
SS5    CMPA #7
       BCS ASSIGN
       LBRA SS6
ASSIGN LDB GL 
       PSHS B
       LDB VAL
       PSHS B
       CMPA #5        local array
       BNE ASS1
       LBSR SUBSC1
       LDB ,S
       LDA LSW 
       BNE AS0
       LDA #$8B 
       LBSR PUTAB
       LDA  #1
       BRA   AS2
AS0    ADDB OPER 
       STB ,S
       CLR LSW
       BRA   AS1
ASS1   CMPA #6        global array
       BNE ASS2 
       LBSR DSUBSC 
       LDA #2
       BRA AS2 
ASS2    CMPA #4
       BEQ *+5 
       LBRA ERROR 
       LBSR WORD      local var
AS1    CLRA 
AS2    PSHS  A 
       LDA SY
       CMPA #$3C     ,
       BNE  *+9
       LBSR  WORD 
       BSR   ASSIGN
       BRA   AS3
       LDA   #$3D    :
       LBSR  CHECK
       LDA   #$27    =
       LBSR  CHECK
       LBSR  EXPR
AS3    PULS D
       STB   OPER     4--
       PULS B
       STB  GLL
       TSTA 
       BNE   AS4
*      LBSR  PUTX 
       LDA   #$A7 
       LDB   OPER 
       LBRA  PUTOFS
AS4      DECA 
       BNE   AS5
       LDD   #$3504          PULS B
       LBSR  PUTAB
*      LBSR  PUTX            STA B,X / STA B,Y
       LDD   #$A785           
       LBRA  PUTAB
AS5    LBSR  PUTHS
       FCB   4
       FDB   $3510           PULS X
       FDB   $A700           STA ,X
       RTS
**
** NON-STATEMENT
**
SSEND  CMPA #$A2       seek
       LBEQ  EXPR
       CMPA #$A3       seekr
       LBEQ  EXPR
       CMPA #$A4       position
       LBEQ  EXPR
SSEND1 COM SSW
       RTS
**
SS6    CMPA #$58 
       BCC SSEND 
       CMPA #$52
       BCS SSEND
       SUBA #$51
       PSHS A
       LBSR WORD
       PULS B         B keyword A next token
**
*  REPEAT UNTIL 
**
       DECB
       BNE SS7
       BSR ASTOUT
       LBSR STLIST
       LDA #$60
       LBSR CHECK
       LBSR EXPR
       LBSR PUTHS
       FCB 2
       FDB $2603
       CLR SSW
       BRA   MINOUT 
**
* FOR-TO
**
SS7    DECB
       BNE SS8
       CMPA #4        should be simple var
       BEQ *+5
       LBRA ERROR
       LDB VAL
       PSHS B
       LDB GL
       PSHS B
       LBSR ASSIGN
       LDA #$61       to
       LBSR CHECK
       CLR ACC
       LBSR EXPR
       LDA #$64       do
       LBSR CHECK
       INC  LSIZE
       LDA  #$A7      sta
       LDB  LSIZE
       LBSR PUTAB
       pshs b
       BSR ASTOUT
       LBSR STAT
       puls b
       lda  #$A6      lda
       LBSR PUTAB
       PULS D
       STA GLL
       STB OPER
       LDA #$A1
       LBSR PUTABX
       LBSR PUTHS
       FCB 2
       FDB $2305
       LDA #$6C
       LDB OPER
       LBSR PUTAB
MINOUT LBRA PULJMP
ASTOUT LBRA PSHDEF 
**
* WHILE-DO
**
SS8    DECB
       BNE SS9
       BSR ASTOUT
       LBSR EXPR
       LDA #$64
       LBSR CHECK
       LBSR PUTHS
       FCB 2
       FDB $2603
       BSR SLAOUT 
       LBSR STAT
       BSR PEROUT 
       BSR MINOUT
PLUOUT LBRA PULDEF
**
** IF—THEN
**
SS9    DECB
       BNE SS10
       LBSR EXPR
       LDA #$65 
       LBSR CHECK
       LBSR PUTHS
       FCB 2
       FDB $2603
       BSR SLAOUT 
       LBSR STAT
       LDA SY
       CMPA #$67   ELSE
       BNE PLUOUT
       BSR SLAOUT         lbra then
       BSR PEROUT      
       BSR PLUOUT      else:
       LBSR WORD
       LBSR STAT
       BRA PLUOUT      then:
SLAOUT LBRA PSHJMP
PEROUT LBRA STCHG
***
* CASE—OF
SS10   DECB
       BNE SS11
       LBSR EXPR
       LDA #$66
       LBSR CHECK
       CLRB
S10A   INCB
       PSHS B
       STB ACC 
       LBSR LEXPR 
       LDA #$81 
       LBSR AOPER
       LBSR PUTHS
       FCB 2
       FDB $2703
       BSR SLAOUT
       LBSR STAT
       BSR SLAOUT
       BSR PEROUT
       BSR PLUOUT
       PULS B
       LDA SY
       CMPA #$67     ELSE
       BNE S10A
       PSHS B 
       LBSR WORD
       LBSR STAT
       PULS A
       LBRA PLDFN 
**
* WRITE STATEMENT
**
SS11   LDA #$37
       LBSR CHECK
       LBSR EXPR    output channel number
       LBSR PUTHS
       FDB $0297
       FCB OUTDN
       LDA #$3D
       LBSR CHECK
WTLP   CLR ACC
       BSR WTERM
       LDA SY
       CMPA #$3C
       BNE WTEN
       LBSR WORD
       BRA WTLP
WTEN   LDA #$3B
       LBRA CHECK
**  write command argument
WTERM  CMPA #$6C      string
       BNE WR1
       LBSR PUTHSL
       FDB $0317
       FDB PUTSTR
       LDA CH
WR01   CMPA #'"'     copy until '"'
       BEQ WR02
       LBSR PUTA
       LBSR GETCH
       BRA WR01
WR02   CLRA          put 0 at end
       LBSR PUTA
       LBSR GETCH
       LBRA WORD
**
WR1    CMPA #$6B    CRLF
       BNE WR3
       LBSR WORD
       CMPA #$37    '('
       BEQ WR2
       LBSR PUTHSL
       FDB $0317
       FDB CRLF
       RTS
**
WR2    LBSR WEXPR
       BSR WTEN
       LBSR PUTHSL
       FDB $0317
       FDB CRLFA
       RTS
**
WR3    CMPA #$6A     SPACE
       BNE WR4
       LBSR SUBSC
       LBSR PUTHSL
       FDB $0317
       FDB SPACEA
       RTS
**
WR4    CMPA #$69     ASCII
       BNE WR5
       LBSR SUBSC
       LBSR PUTHSL
       FDB $0317
       FDB PUTCA
       RTS
**
WR5    CMPA #$26     #
       BNE WR6
       LBSR DSUBSC
       LBSR PUTPLB
       LBSR PUTHSL
       FDB $0317
       FDB PUTDA+1
       BRA WR66
**
WR6    LBSR EXPR
       LBSR PUTHSL
       FDB $0317
       FDB PUTDA
WR66   
RTS11  RTS
**
* use X for LB, OR use Y for GB BY 
**
*PUTX   equ RTS11     * no pointer load
**
PUTOFS PSHS D,X
       LDX <PC
       STA ,X+
       CLRA
       TST  <GLL
       BMI  PUTOFSX
       LDA  #$20
PUTOFSX STA ,S
       CMPB #32
       BGT  *+6
       CMPB #-32
       BGE  PUTOFS5
       LDA  #$80
       ORA  ,S
       STA  ,X+
       STB  ,X+
       BRA  PUTOFS8
PUTOFS5
       ANDB #$1F
       ORB   ,S
       STB   ,X+
PUTOFS8
       STX  <PC
       PULS D,X,PC

***
* PUT ACC A&B
**
PUTAB  BSR PUTA
       TFR B,A
**
* PUTX & PUTB
**
PUTABX equ PUTAB
*      PSHS D
*      BSR PUTX
*      PULS D 
*      BRA PUTAB
**
* PUT ACCA RS AN OBJECT
**
PUTA   PSHS X
       LBSR AOUT
       PULS X,PC
**
* PUTHS STRING
**
PUTHS  LDX ,S++
       LDB ,X+
PS1    LDA ,X+
       BSR PUTA
       DECB
       BNE PS1
       JMP ,X
**
** CHECK ACC
**
CHECK  CMPA SY
       BEQ WORD
**
* ERROR
**
ERROR  LBSR PUTSTR
       FDB $0D0A
       FCC "ERROR ",0
       LDX TEND
       LEAX 1,X
       NEG ,X
ER0    LDA ,X+
       LBSR PUTCA
       CMPX WEND
       BNE ER0
       lbra exit
**
* WORD DECORDER
**
WORD   BSR WORDS
       LDA SY
       RTS
WORDS  CLRB
       STB SY
       STB VAL
       LDA CH
** SKIP CONT  ,SPACE.;
WD1    CMPA #$21
       BCS SKIP
       CMPA #'.'
       BEQ SKIP
       CMPA #';'
       BNE WD2
SKIP   BSR GETCH
       BRA WD1
**
*  COMMENT
**
WD2    CMPA #'%'
       BNE WD3
       BSR GETCH
       CMPA #$20
       BCC *-4
       BRA WD1
**
* ASCC CONST
**
WD3    CMPA #'\''
       BNE WD4
       BSR GETCH
       STA VAL
       BSR GETCH
GETCH  PSHS X
       LBSR MEMIN
       STA CH
       PULS X,PC
* HEX CONSTANT
WD4    CMPA #'$'
       BNE WD5
WD40   BSR GETCH
       BSR TSTNA
       BEQ WD41
       BCC RTS2
       CMPA #'F'+1
       BCC RTS2
       SUBA #7
WD41   SUBA #'0'
       LDB VAL
       ASLB
       ASLB
       ASLB
       ASLB
       PSHS B
       ADDA ,S+
       STA VAL
       BRA WD40
**
* TEST ALPHA NUMERIC   Z=0 C=0 Not Number/Not Alpha
TSTNA  CMPA #'0'       Z=1 C=1 Number
       BCS NAF         Z=0 C=1 Not Number/Alpha
       CMPA #'9'+1
       BCS NT
       CMPA #'A'
       BCS NAF
       CMPA #'Z'+1
       BCS AT
NAF    CLRB CLEAR C
AT     LDB #-1
RTS2   RTS
NT     CLRB
       RTS
** DECIMAL CONSTANT
WD5    BSR TSTNA
       BNE WD6
WD50   SUBA #'0'
       PSHS A
       LDA VAL
       LDB #10
       MUL
       ADDB ,S+
       STB VAL
       BSR GETCH
       BSR TSTNA
       BEQ WD50
       RTS
* THE OTHER WORDS
WD6    LDX TEND
       PSHS A
       NEGA
       LEAX 1,X
       BSR STAONE
       PULS A
       BSR TSTNA   first word must alpha
       BCC TLOOK1
WD61   LDA CH
       BSR TSTNA   alpha numeric?
       BCS *+4
       BNE TLOOK1
       BSR STAONE
       BRA  WD61
STAONE STA ,X+     store to the table
       STX WEND
       BRA GETCH 
TLOOK1 LDA RSW     word end
       BEQ TLOOK   let's search
       COM RSW
       RTS
**
* WORD TABLE SEARCH
*
*  if not find then error
*  on return    X point last of word (VAL)
*          SY    7 larray 6 lvar 5 garray 4 gvar 3 func or proc 0 reserved word
*          VAL   word id or size
*          GL    1 local 0xff global
**
TLOOK  PSHS U
       LDA TCOUNT 
       STA SY
       LDX TEND 
S01    LDU WEND 
S02    LDA ,-U
       CMPA ,-X 
       BEQ S06
S03    TST ,X
       BEQ S05 
       BMI S04 
       LEAX -1,X
       BRA  S03 
S04    LEAX -1,X 
       BRA S01 
S05    DEC SY
       BNE S01
       LBRA ERROR
S06    TSTA 
       BPL S02
       LDB ,-X
       STB VAL
       LDA SY
       CMPA #1
       BNE *+6
       TFR B,A 
       BRA S07 
       LDB  #1
       CMPA #4 
       BCS  RTSS
       TST PMODE
       BEQ *+6
       CMPA #6
       BCS *+3 
       NEGB 
       STB GL
       ANDA #$FD
S07    STA SY
RTSS   PULS U,PC
**
* ARITHMATIC EXPRESSION
**
WEXPR  LBSR WORD
EXPR   BSR LEXPR
       BSR OLOAD 
RTE    RTS
** WORD * LEXPR
WLEXPR LBSR WORD
**
* LOGICAL EXPRESSION
**
LEXPR  BSR REXPR
LE1    LDY SY
       CMPA #$82
       BCS RTE
       CMPA #$8A+1
       BCC RTE
       PSHS A
       LBSR WORD
       BSR REXPR
       LDB LSW
       BNE LE2
       LBSR PUTHS
       FDB $0397 
       FCB WT1
       FCB $32
       PULS A
       ADDA #$10
       LDB #WT1
       LBSR PUTAB 
       BRA LE1
LE2    PULS A
       BSR OCORD 
       BRA LE1
**
* RELATIONAL EXPRESS 
**
REXPR  LBSR AEXPR
RE1    LDA SY
       CMPA #$21 
       BCS      RTE 
       CMPA #$30 
       BCC RTE
       PSHS A
       LBSR WORD 
       BSR AEXPR 
       LDA #$80 
       BSR AOPER 
       PULS A
       LBSR PUTHS
       FCB 6                      b?? *+3 ; clra ; bra *+2 ; lda #$ff
       FCB $3,$4F,$20,$02,$86,$FF
       BRA RE1
** ADDING OPERATFR
AOPER  LDB LSW
       BNE OCORD 
       PSHS A
       CMPA #$80 
       BEQ  *+5 
       BSR PUTPUL
       FCB $8C 
       BSR PUTPLB
       PULS A
       SUBA #$70
PUTA1  LBRA PUTA
** OUTPUT SAVED L-CC 
OLOAD  LDA LSW 
       BEQ RTE 
       LDA ACC
       BEQ OL1
       LDD #$3402  pshs a
       LBSR PUTAB 
OL1    LDA #$86
OCORD  PSHS A
       CLRA
       STA LSW
       COMA
       STA ACC
*      LDA AMODE       amode is sometime wrong?
*      CMPA #$20
*      BNE *+5
*      LBSR PUTX
       CLR LSW
       PULS A
       ADDA AMODE
       CMPA #$A6
       BEQ OCOFS
       CMPA #$E6
       BEQ OCOFS
       LDB OPER              imm case
       LBRA PUTAB
OCOFS  LDB OPER              index case
       LBRA PUTOFS
* PUT 'TAB:PULS A'
PUTPUL LBSR PUTHS
       FCB 4
       FCB $1f,$89,$35,2     tfr a,b ; puls a
RTE1   RTS 
* PUT 'PULS B'
PUTPLB LDA #$3504 puls b
       LBRA PUTAB
**
*  ADDING EXPRESSION
**
AEXPR  BSR MEXPR
AE1    LDA SY
       CMPA #$80
       BEQ  AE2
       CMPA #$8B 
       BNE RTE1
AE2    PSHS A
       LBSR WORD
       BSR MEXPR
       PULS A
       LBSR AOPER
       BRA AE1
**
*  MUTIPLYING EXPRESSION
**
MEXPR  BSR TERM 
ME1    LDA SY
       CMPA #$8E     *
       BEQ ME2
       CMPA #$8F     /
       BNE RTE1
ME2    PSHS A
       LBSR WORD
       BSR TERM
       LDB LSW 
       BEQ ME3
       LDA #$C6      LDB
       BSR OCORD
       FCB $8C       ???
ME3    BSR PUTPUL 
       PULS A
       CMPA #$8E 
       BHS ME4
       LBSR PUTHSL
       FCB 3
       FCB $17
       FDB MULT       should MUL inst
       BRA ME1
ME4    LBSR PUTHSL
       FCB 3
       FCB $17
       FDB DIV
       BRA ME1
**
* TERM
**
TERM   LDA SY
       BNE TM1
* SAVE L-COMMAND 
SLOAD  PSHS A
       LBSR OLOAD
       LDA VAL 
       STA OPER 
       LDA GL
       STA GLL
       PULS A
       STA AMODE 
       COM LSW 
       LBRA WORD 
* Uniary minus operator
TM1     CMPA #$80
        BNE TM02
        LBSR WORD
        BSR  TERM
        LDB  LSW
        BEQ  TM03
        NEG  <OPER
        RTS
TM03    LBSR PUTHS
        FCB  $2,$35,$2    puls a
        LDA  #$04     NEGA
        LBRA PUTA
* CONST TRUE & FALSE
TM02    CMPA #$A0  
        BEQ TM01
        CMPA #$A1
        BNE TM2
TM01    SUBA #$A1
        STA VAL
        CLRA
        BRA SLOAD
* SYSTEM VAR MHIGH & MOD
TM2       CMPA #$16
        BEQ *+6
        CMPA #$17
        BNE TM3
        STA VAL
        LDA #$10
        BRA SLOAD
* SIMPLE VARIABLE 
TM3       CMPA #4
        BNE TM4
        LDA #$20
        BRA SLOAD
* ( EXPTRSSION ) 
TM4       CMPA #$35
        BCS TM5
        CMPA #$38
        BCC TM5
        PSHS A
        LBSR WLEXPR
        PULS A
        ADDA #4
        LBRA CHECK
** FUNCTION CALL
TM5       CMPA #3
        BEQ *+6
        CMPA #$E0
        BCS TM6
        BSR OLP 
PFCALL LDA VAL
        PSHS A 
        LBSR WORD
        CMPA #$37 
        BNE PFC1 
        LDA LSIZE
        PSHS A 
*        INC LSIZE 
PFC2    INC LSIZE 
        LBSR WEXPR
        CLRB
        STB ACC 
        DECB
        STB    GLL
        LDA #$A7 
        LDB LSIZE 
        DECB
        LBSR PUTABX 
        LDA SY 
        CMPA #$3C 
        BEQ PFC2 
        PULS A
        STA LSIZE 
        LDA #$3B 
        LBSR CHECK 
PFC1   LDB #-1 
        STB ACC 
        LDA #$86 
        LDB LSIZE
       LBSR PUTAB
       PULS A
       CMPA #$C0
       BCC *+5
       LBRA CALPF
       LDX #LIBR
       SUBA #$C0
PFC3   BEQ PFC4
       LEAX 2,X
       DECA
       BRA PFC3
PFC4   LDA #$17
       LBSR PUTA
       LDD ,X
       LBRA PUTAB
**
OLP    LBSR OLOAD
       LDB ACC 
       BEQ RTS4
       LDD #$3402     pshs a
       LBSR PUTAB
       CLR ACC 
RTS4   RTS
** FUNCTION RND
TM6    CMPA #$70 
       BNE TM61 
       LBSR SUBSC
       LBSR PUTHSL
       FDB $0317
       FDB RND
       RTS
* FUNTION GET
TM61   CMPA #$71
       BNE TM62 
       LBSR SUBSC
       LBSR PUTHS
       FDB $0297
       FCB INDN
       LBSR PUTHSL
       FDB $0317
       FDB getchar
       RTS
* FUNCTION READ etc
TM62   CMPA #$72 
       BNE TM63
       LBSR SUBSC
       LBSR PUTHS
       FDB $0297
       FDB INDN
       LBSR PUTHSL
       FDB $0317
       FDB GETDA
       RTS
TM63   CMPA #$A2      seek
       BNE TM64
       LBSR DSUBSA
       tstb
       beq tmm3
       bra  aradr
TM64   CMPA #$A3      seekr
       BNE TM65
       LBSR DSUBSC
       bra tmm3
TM65   CMPA #$A4      position
       BNE TM66
       LBSR DSUBSA
       tstb
       beq tmm3
       bra  aradr
TM66   CMPA #$A5      open
       BNE TM67
       LBSR DSUBSC
       bra tmm3
TM67   CMPA #$A6      openm
       BNE TM7
       LBSR DSUBSA
       tstb
       beq tmm3
       bra  aradr
*                    ; pshs u; leau ?,[xy] ; stu <tm1 ; puls u
aradr  LBSR PUTHS
       FCB  2,$34,$40
       ldb  VAL
       lda  #$33     leau
       tst  GL
       beq aradr1
       addb #$20
aradr1 LBSR PUTAB
       LBSR PUTHS
       FCB  4,$DF,WT1,$35,$40
       LBSR WORD
       LDA  #$3B    ')'
       LBSR CHECK
tmm4   LBSR PUTHSL
       FDB  $0317    
       FDB  NONE
       RTS
tmm3   LBSR PUTHS
       FCB  6,$35,$04,$D7,WT2,$0F,WT1       ; puls b ; stb <tm2; clr <tm1
       bra  tmm4

* FUNCTION NOT ASL ET AL
TM7    CMPA #$40
       BCS TM8
       CMPA #$49+1
       BCC TM8
       PSHS A
       BSR SUBSC
       PULS A
       LBRA PUTA
* ARRAY
TM8    CMPA #$5
       BNE TM9
       LDB VAL
       PSHS B
       LDB GL
       PSHS B
       BSR SUBSC1
       PULS B
       STB GLL
       PULS B
       LDA LSW
       BEQ ARY1
       ADDB OPER 
       STB OPER
       LDA #$20 
       STA AMODE
       RTS 
ARY1   LDA #$8B
       LBSR PUTABX
       LBRA LDAAX

**   f(x,y)   y can be an array
DSUBSA LDA #$3C    ','
       BSR SUBS1
       LDA #$3B    ')'
       PSHS A
       LDA  SY
       CMPA #$5
       BNE  DSUB1
       leas 1,s
       ldb #1        b==1 array
       RTS
DSUB1  LBSR EXPR
       puls a
       lbsr CHECK    
       clrb
       rts

**   f(x,y)
DSUBSC LDA #$3C    ','
       BSR SUBS1
       LDA #$3B    ')'
       PSHS A
       BRA SUBS2
SUBSC  LDA #$3B    ')'
SUBS1  PSHS A
       LBSR WORD
       LDA #$37    '('
       LBSR CHECK
SUBS2  LBSR EXPR
       PULS A 
       LBRA CHECK
SUBSC1 LBSR WORD
       LDA #$36     '['
       LBSR CHECK
       LBSR LEXPR
       LDB LSW
       BEQ SBS5
       LDB AMODE
       BEQ SBS5
       LBSR OLOAD
SBS5   LDA #$3A     ']'
       LBRA CHECK
* MEM FUNCTION
TM9    CMPA #6
       BNE TM10
       LBSR PUTHS       ;    leas -1,s
       FCB 2,$32,$7F
       BSR DSUBSC
       LBSR PUTHS       ;    sta 1,s; lda [,s++]
       FDB $04,$a7,$61,$a8,$f1
       RTS
** FOR EXPANTION
TM10   LBRA ERROR
**
* ADDRESS DEPENDENT CODE
* GENARATION
**
* SET PRC—FUNC TABLE
**
SETPFT PSHS A 
       LDB #3
       MUL
       ADDD PFTBEG
       STD XR 
       PULS A,PC
**
TWICE  LBSR PUTSTR
       FCB $0D,$0A
       FCC "TWICE!",0
       lbra exit 
**
* DEF PROC-FUNC
**
DEFPF  BSR SETPFT
       LDX XR
       TST ,X
       BNE TWICE
       COM ,X
       LDX 1,X
       STX YR 
       LDX XR 
       LEAX 1,X
       BSR RPCST 
       LDX YR
DP1    BEQ RT10
       LDX ,X
       STX ZR
       LDX YR
       BSR RPCST 
       LDX ZR
       STX YR
       BRA DP1
RPCST  LDD PC
       pshs x
       subd ,s++
       subd #2
       STD ,X
       RTS
**
* CALL PORC-FUNC
**
CALPF  BSR SETPFT
       INCA
       CMPA PFMAX
       BCS *+4
       STA PFMAX
       LDA #$17
       BSR AOUT
       LDX XR
       LDD 1,X
       LDX PC
       BSR STAABX 
       LDX XR
       TST ,X
       BNE PC2ADD
       LEAX 1,X
       BSR PCST
       BRA PC2ADD
**
* PUL-DEF N TIMES
**
PLDFN  PSHS A
       BSR PULDEF
       DEC ,S
       BNE *-4
       PULS A,PC
**
* PULL AND DEFINE
**
PULDEF BSR PULSTK
PCST   LDD PC
STAABX STD ,X
RT10   RTS
**
JMPOUT LDA #$16
AOUT   LDX PC
       STA ,X
       BRA INCPC1
**
PULSTK LDX SP
       LEAX 2,x
       STX SP
       LDD ,X
       LDX ,X
       RTS
**
*  PUL STACK & Jump
**
PULJMP BSR JMPOUT
       BSR PULSTK
PCST2  LDX PC
       pshs x
       subd ,s++
       subd #2
       std  ,x
INCPC  LEAX 1,X
INCPC1 LEAX 1,X
SETPC  STX PC
       RTS
**
* PUSH STACK & JUMP
**
PSHJMP BSR JMPOUT
       BSR PSHDEF
PC2ADD LDX PC
       BRA INCPC
**
* PUSH STRCK & DEFINE
**
PSHDEF LDX SP
       BSR PCST
       LEAX -2,X
       STX SP
       RTS
**
OUTPC3 BSR AOUT 
       LDD PC
       ADDD #3
       BRA PCST2
**
* LDA R,X
LDAAX  LDA #$B7
       BSR OUTPC3
       LDA #$A6
LA1    BSR AOUT 
       BRA INCPC1
**
* STA B,X
STABX  LDA #$F7
       BSR OUTPC3
       LDA #$A7
       BRA LA1
**
* STACK TOP CHANGE
**
STCHG  PSHS U,Y
       LDU SP
       PULU X,Y
       STX ,--U
       STY ,--U
       PULS U,Y,PC
**
* PUTHSL output with address calculation
*   only working on 3 byte 16/17 (LBRA/LBSR)
**
PUTHSL  LDX ,S++
       LDB ,X+
       LDA ,X+
       LBSR PUTA
       LDD ,X++
       leay 0,pcr
       leay d,y
       exg  d,y
       addd modofs
       subd pc
       ldy  pc
       std  ,y++
       sty  pc
       JMP ,X
**
**
* END OF LOAD
**
ENDL   LDX PFTBEG
       LDA PFMAX
EL1    BEQ EL
       TST ,X
       BEQ UDERR
EL2    LEAX 3,X
       DECA
       BRA EL1
UDERR  PSHS A
       STA ZR
       SUBA PFMAX
       NEGA
       PSHS A
       LBSR PUTSTR
       FDB $0D0A
       FCC "UNDEF",0
       PULS A
       LBSR PUTCA
       PULS A
       LDX ZR
       BRA EL2
EL     LDX PC
       LBRA C

modsetup
       pshs  x,y,u
       lda   #$ff
       sta   <runmod
       ldy   <pc
       ldd   #$87CD
       std   ,y
       leay  4,y        skip size 
       ldd   #$0d       module name offset
       std   ,y++
       ldd   #$1180     type language
       std   ,y++
       ldd   #$1a00     attribue rev
       leay  5,y
       *  put module name
       ldx   ,s
       bsr   mkmodnam
       tfr   y,d
       leax  crt0top,pcr
       pshs  x
       subd  ,s++
       subd  #2
       std   modofs
       leau  crt0top,pc
       ldx   #LIBEND-crt0top
libcpy lda   ,u+
       sta   ,y+
       leax  -1,x
       bne   libcpy
       sty   <PC
       puls   x,y,u,pc

mkmodnam pshs x,y,u
m7     tfr    x,u
       lda    ,x+
       lbeq   ERROR    * no file name
       cmpa   #' '
       bne    m2
       bra    m7
m0     lda    ,x+
m2     cmpa   #' '
       ble    m1
       cmpa   #'/'
       beq    m8
       cmpa   #':'
       bne    m0
m8     tfr    x,u
       bra    m0
m1     lda    ,u+
       cmpa   #' '
       ble    m3
       cmpa   #'.'
       beq    m3
       sta    ,y+
       bra    m1
m3     lda    -1,y
       ora    #$80
       sta    -1,y
       clr    ,y+
       sty    2,s
       puls  x,y,u,pc

modend leax   OBJECT,u         * header setup
       ldd    <PC
       pshs   x
       subd   ,s++
       addd   #3
       std    2,x              module size
       * header parity
       clra
       ldb    #8
m4     eora   ,x+
       decb
       bne    m4
       eora   #$ff
       sta    ,x+              header parity
       leay   crt0,pcr
       ldd    modofs
       addd   #11
       leay   d,y
       tfr    y,d
       pshs   x
       subd   ,s++
       std    ,x++             start address offset
       clra 
       ldb    GSIZE
       addd   #OBJSTART+100
       std    ,x             global size
       * mod CRC
       pshs   d,u
       leax   OBJECT,u
       ldu    <PC
       lda    #$ff
       sta    ,u
       sta    1,u
       sta    2,u
       tfr    u,d
       pshs   x
       subd   ,s++
       tfr    d,y
       os9    F$CRC
       com    ,u
       com    1,u
       com    2,u

       * write to file
       ldu    2,s
       leax   OBJECT,u
       ldd    4,x
       leax   d,x
       ldd    #$27f         10 01111111      
       os9    I$Create
       lbcs    ERROR
       sta    ,s
       ldu    2,s
       leax   OBJECT,u
       ldy    2,x           module size
       lda    ,s
       os9    I$Write
       lbcs    ERROR
       puls   d,u
       clrb
       os9    F$Exit          all end

**********************
* ADVANCE WORD
**
WTABLE FCB 0 END MARK 
       FCB $30,-'P'
       FCC "ROC" 
       FCB $31,-'F'
       FCC "UNC" 
       FCB $32,-'V'
       FCC "AR"
       FCB $33,-'A'
       FCC "RRAY"
       FCB $34,-'B'
       FCC "EGIN"
       FCB $35,-';'
       FCB $36,-'['
       FCB $37,-'('
       FCB $34,-'{'
       FCB $38,-'E'
       FCC "ND"
       FCB $39,-'=' 
       FCB $3A,-']'
       FCB $3B,-')'
       FCB $38,-'}'
       FCB $3C,-','
       FCB $3D,-':'
       FCB $50,-'S'
       FCC "TOP"  
       FCB $51,-'R'
       FCC "ETURN"
       FCB $55,-'I'
       FCC "F"
       FCB $65,-'T'  
       FCC "HEN"
       FCB $53,-'F'  
       FCC "OR"  
       FCB $61,-'T'  
       FCC "O"
       FCB $52,-'R'
       FCC "EPEAT"
       FCB $60,-'U'
       FCC "NTIL"
       FCB $54,-'W'  
       FCC "HILE"
       FCB $64,-'D'  
       FCC "O"
       FCB $56,-'C'  
       FCC "ASE"
       FCB $66,-'O'  
       FCC "F"  
       FCB $67,-'E'  
       FCC "LSE"
       FCB $57,-'W'
       FCC "RITE"
       FCB $69,-'A'
       FCC "SCII"  
       FCB $6A,-'S'
       FCC "PACE"
       FCB $6B,-'C'  
       FCC "RLF"  
       FCB $6C,-'"'
       FCB $8B,-'+'
       FCB $80,-'-'
       FCB $8E,-'*'  
       FCB $8F,-'/'
       FCB $82,-'S'
       FCC "BC"
       FCB $84,-'A'
       FCC "ND"
       FCB $88,-'E'
       FCC "OR"
       FCB $89,-'A'  
       FCC "DC"
       FCB $8A,-'O'  
       FCC "R"
       FCB $22,-'>'  
       FCB $25,-'<'
       FCB $26,-'#'
       FCB $27,-'=' 
       FCB $2D,-'L','T'
       FCB $2E,-'G','T'
       FCB $40,-'N','E','G'
       FCB $43,-'N','O','T'
       FCB,$43,-'C','O','M'
       FCB $44,-'L','S','R'
       FCB $46,-'R','O','R'
       FCB $47,-'A','S','R'
       FCB $48,-'A','S','L'
       FCB $49,-'R','O','L'
       FCB $06,-'M','E','M'
       FCB $A0,-'T'
       FCC "RUE"
       FCB $A1,-'F'
       FCC "ALSE"
       FCB $16,-'M'
       FCC "HIGH"
       FCB $17,-'M'
       FCC "OD"
       FCB $70,-'R','N','D'
       FCB $71,-'G','E','T'
       FCB $72,-'R'
       FCC "EAD"
       FCB $A2,-'S'
       FCC "EEK"
       FCB $A3,-'S'
       FCC "EEKR"
       FCB $A4,-'P'
       FCC "OSITION"
       FCB $A5,-'O'
       FCC "PEN"
       FCB $A6,-'O'
       FCC "PENM"
WTBLEND 

******
* SUPORTING ROUTINES
* & I/0 CONTROL
**

crt0top

**
* RND FUNCTION
**
RND    PSHS A
       LDA RNDL
       LDB #125
       MUL
       ADDD #1
       STA RNDL
       PSHS A
       LDA RNDH
       LDB #125
       MUL
       ADDA ,S+
       STA RNDH
       PULS B
       MUL
       INCA
NONE
       RTS
**
* DVISITION SET MOD 
**
DIV    STB WT1
       BEQ ERDIV 
       TFR A,B
       CLRA
       STA WT2
DV0    INC WT2
       ASL WT1
       BCC DV0
DV1    ROR WT1
       CMPB WT1
       BCS DV2
       SUBB WT1
       ORCC #1
       BRA *+4
DV2    ANDCC #$FE CLR C
       ROLA
       DEC WT2
       BNE DV1
       STB MOD
       TSTA
       RTS
**
ERDIV  STB OUTDN
       LBSR CRLF
       LBSR PUTSTR
       FCC "ERR DIV 0",0
MONIT  lbra exit
**
MULT   MUL
       STA MHIGH
       TFR B,A
       RTS
**
* PUT A IN DECIMAL
**
PUTDA  CLRB
PUTDR  STB DREG
       PSHS X
       LEAX -2,S
       LEAS -6,S
       CLR 1,X
       LDB #3
P0     PSHS B
       LDB #10
       BSR DIV
       ADDB #$30
       STB ,X
       LEAX -1,X
       PULS B
       DECB
       BNE P0
       COM 3,X
       LDA #'0'
       LDB #4
P1     LEAX 1,X
       DECB
       CMPA ,X
       BEQ P1
       COM 4,S
       LDA DREG
       PSHS B
       SUBA ,S+
       BCS PRX
       BSR SPACEA
PRX    LDA ,X
       BEQ P4
       LBSR PUTCA 
       LEAX 1,X 
       BRA PRX 
P4     LEAS 6,S
CL1    PULS X,PC
** 
* GET IN A DECIMAL
** 
GETDA  CLRA
       PSHS A
       LBSR GETCA 
       CMPA #RUB 
       BNE GD1 
       PULS A
       LDB #10 
       LBSR DIV
       BRA GETDA+1 
GD1    SUBA #'0'
       BCS GD2
       CMPA #10
       BCC GD2 
       STA DBUF 
       PULS A
       LDB #10 
       MUL
       ADDB DBUF 
       TFR B,A
       BRA GETDA+1 
GD2    PULS A,PC
**
* SPACE A TIMES 
**
SPACEA BEQ CL1
       PSHS A
       LDA #' '
       LBSR PUTCA
       PULS A
       DECA
       BRA SPACEA
**
* STR OUT
**
PUTSTR PSHS X
       LDX 2,S
STR1   LDA ,X+
       BEQ STR2
       LBSR PUTCA
       BRA STR1
STR2   STX 2,S
       PULS X,PC
**
* CRLF
**
CRLF   LDA #$0D
       BSR PUTCA 
       LDA #$0A
       BRA PUTCA
**
* CRLF A TIMES
**
CRLFA  BEQ CL1
       PSHS A
       BSR CRLF 
       PULS A
       DECA
       BRA CRLFA

 
Exit     lbsr        setecho
*        ldx         <work
*        leax        readbuff,x
*        ldb         #1
*        lbsr         getline
*        lbsr         getpoll
*        lda        <stdin        
*        os9        I$Close      

        clrb
        os9        F$Exit
*       no return



err     ldb    #1
L0049
        bra     Exit


PUTCA   tfr         a,b
putchar                        * Output one character in B register.
        PSHS        X,Y
        BRA         OUTCH1

close
         lda   <INDN        else get path
         os9   I$Close          and close it
         bcs   L0049            branch if error
         rts

MEMIN
        PSHS        A,B,X,Y
        ldx         <adr
        lda         ,x+
        bne         GETCA1
        LDA         INDN
        LEAX        readbuff,u
        LDY         #bufsiz
        OS9         I$Read
        BCC         GETCA0
        lda         #'/'
        ldx         <adr
        bra         GETCA1
GETCA0  LEAX        readbuff,u
        tfr         y,d
        clr         d,x      eof
        lda         ,x+
GETCA1  stx         <adr
        sta         ,s
        PULS        A,B,X,Y,PC

GETCA   bsr         getchar
        tfr         b,a
        rts

getchar                        * Input one character into B register.
        PSHS        A,B,X,Y
GETCH0
        LDA         INDN
        LEAX        ,S
        LDY         #1
        OS9         I$Read
        BCS         GETCH0
        PULS        A,B,X,Y,PC
putcr                          * Output a newline.
        LDB         #C$CR
        bsr         putchar
        LDB         #C$LF
        PSHS        X,Y
OUTCH1  PSHS        A,B
        LEAX        1,S
        LDA         OUTDN
        LDY         #1
        OS9         I$Write
        PULS        A,B,X,Y,PC
getpoll
        PSHS        X,Y,D
        LDA         #0
        LDB         #SS.Ready
        OS9         I$GetStt
        CMPB        #$F6       Not Ready
        BNE         RSENSE
        CLRB
        PULS        X,Y,D,PC
RSENSE
        ORCC        #1        set carry to indicate ready
RNSENSE
        PULS        X,Y,D,PC

ferr     clr   <OUTDN
         ldx   <parmptr
floop    ldb   ,x+
         beq   ferr1    
         cmpb   #$0d
         beq    floop
         lbsr   putchar
         bra   floop
ferr1    lbsr   putstr
         fcc  " is not found",0
         lbra L0049

getline                        * Input line at address in X, length in B.
        PSHS        A,B,X,Y
        clr         ,s
GETLN0
        ldy         ,s
        lda         INDN   
        OS9         I$ReadLn
        BCS         GETLN0
        LEAY        -1,Y
GETLN1  STY         ,S
        PULS        A,B,X,Y,PC
putline                        * Output string at address in X, length in B.
        PSHS        A,B,X,Y
        CLRA
        TFR         D,Y
        lda         OUTDN
        OS9         I$WritLn
        PULS        A,B,X,Y,PC

setecho lda          #1
        bra          sss
noecho  clra
sss     leas         -128,s
        leax        ,s
        pshs         a
        clra  
        ldb          #SS.Opt
        OS9         I$GetStt
        bcs         err2
        LDA         ,s
        STA         PD.EKO-PD.OPT,X
setopts
        ldb         #SS.Opt         
        clra        
        OS9         I$SetStt
err2
        puls        a
        leas        128,s
        rts


delay   PSHS        D,X  * address **$21** 
                         * On input the D register contains the number of timer 
                         * ticks to wait. Each timer tick is 20ms
        TFR         D,X
        OS9         F$Sleep
        PULS        D,X,PC

crt0    stx         <arg
        leax        OBJSTART,u
        leay        ,x
        *   compiled code follows

LIBEND   equ *


         emod
eom      equ   *
         end