default_rte 
         STA $FF3E
         JSR JPRIMM
         .null "run-time error #"
erl = *+1
         ldx #255
erh = *+1
         lda #255
         JSR INT2STR
finalfinish 
          STA $FF3E
          JMP $8003

.if opt_trap
default_trap
      LDA #<default_rte
      STA traphandle0+1
      LDA #>default_rte
      STA traphandle0+2
      rts

linetrap
      jsr default_trap
      jmp 0
.endif

clrer     ldy #255
          sty erl
          sty erh
          rts

clr
.if opt_intfromdata . opt_strfromdata
          LDA #<datastart
          STA datapos
          LDA #>datastart
          STA datapos+1
.endif
.block
          jsr clrer
          iny
          sty $79   ;DS$
          lda #>strestatic
          sta r3h
          lda #<strestatic
          sta r3l
          lda #>0-strsdyn+strestatic
          sta r2h
          lda #<0-strsdyn+strestatic
          sta r2l
          ora r2h
          beq ll3

          lda #0
ll1       sta (r3l),y
          iny
          bne *+4
          inc r3h
          inc r2l
          bne ll1

          inc r2h
          bne ll1

ll3       lda #>strsstatic
          sta r3h
          lda #<strsstatic
          sta r3l
          ldy #0
          lda #>0-(strestatic-strsstatic)/2
          sta r2h
          lda #<0-(strestatic-strsstatic)/2
          sta r2l
          ora r2h
          beq ll3a

ll1a      lda #<lib_end
          sta (r3l),y
          iny
          lda #>lib_end
          sta (r3l),y
          iny
          bne *+4
          inc r3h
          inc r2l
          bne ll1a

          inc r2h
          bne ll1a

ll3a      LDA #<strsdyn
          STA strdcurre
          LDA #>strsdyn
          STA strdcurre+1
          rts
.bend

.if opt_i_GT_r
i_GT_r    BPL *+3
          DEX
          SBC $BD
          STA $BD
          TXA
.endif
.if opt_i_GT_i . opt_i_GT_r
i_GT_i
.block
          LDY #0
          SBC r3l
          BMI l1

          ORA $BC
          ORA $BD
          BEQ l1

          DEY
l1        TYA
          RTS
.bend
.endif

.if opt_i_LT_r
i_LT_r    BPL *+3
          DEX
          SBC $BD
          TXA
.endif
.if opt_i_LT_i . opt_i_LT_r
i_LT_i    LDY #255
          SBC r3l
          BMI *+3
          INY
          TYA
          RTS
.endif

.if opt_i_GE_r
i_GE_r    BPL *+3
          DEX
          SBC $BD
          TAX
.endif
.if opt_i_GE_i . opt_i_GE_r
i_GE_i    LDY #0
          SBC r3l
          BMI *+3
          DEY
          TYA
          RTS
.endif

.if opt_i_LE_r
i_LE_r    BPL *+3
          DEX
          SBC $BD
          STA $BD
          TXA
.endif
.if opt_i_LE_i . opt_i_LE_r
i_LE_i
.block
          LDY #255
          SBC r3l
          BMI l1

          ORA $BC
          ORA $BD
          BEQ l1

          INY
l1        TYA
          RTS
.bend
.endif

.if opt_i_EQ_r
i_EQ_r    TAY
          BPL *+3
          DEX
          TXA
.endif
.if opt_i_EQ_i . opt_i_EQ_r
i_EQ_i    LDX #0
          CPY $BD
          BNE i_EQ_i1

          CMP r3l
          BNE i_EQ_i1

          lda $bc
          CMP r3h
          BNE i_EQ_i1

          DEX
i_EQ_i1   TXA
          RTS
.endif

.if opt_i_NE_r
i_NE_r    TAY
          BPL *+3
          DEX
          TXA
.endif
.if opt_i_NE_i . opt_i_NE_r
i_NE_i    LDX #255
          CPY $BD
          BNE i_NE_i1

          CMP r3l
          BNE i_NE_i1

          lda $bc
          CMP r3h
          BNE i_NE_i1

          INX
i_NE_i1   TXA
          RTS
.endif

.if opt_power24
power24 .block      ;r3h:$15:$14^r3l->r5h:r5l:XR
         ldx #0
         stx r5h
         stx r5l
         inx
         lda r3l
         beq ll1

ll2      STX r1l
         LDA r5l
         STA r1h
         LDA r5h
         STA r4l
         LDA $14
         STA r2l
         LDA $15
         STA r2h
         lda r3h
         sta r4h
         JSR mul24
         ;LDA r0h
         STA r5h
         LDA r0l
         STA r5l
         DEC r3l
         BNE ll2
ll1      RTS
         .bend
.endif

.if opt_mul16
mul16    ldx #0     ;R1*R2->r0h(=AC):XR
         stx r0h

         .block
         ldy #16
ll1      lsr r1h
         ror r1l
         bcc ll2

         clc
         txa
         adc r2l
         tax
         lda r0h
         adc r2h
         sta r0h
ll2      asl r2l
         rol r2h
         dey
         bne ll1
         rts
         .bend
.endif

.if opt_multarray
multarray
         STX r2l
         STA r2h
         JSR mul16
         TXA
         CLC
         ADC r4l
         STA r4l
         LDA r0h
         ADC r4h
         STA r4h
         rts
.endif

.if opt_array
array
         LDX r4l
         INX
         BNE *+4
         INC r4h
         TXA
         ASL
         ROL r4h
         rts
.endif

.if opt_array3
array3
         INC r4l
         BNE *+4
         INC r4h
         lda r4l
         asl
         ldx r4h
         ROL r4h
         adc r4l
         tay
         txa
         adc r4h
         sta r4h
         tya
         rts
.endif

pr_int1 LDY #1
        STY r4h
        bne pr_int

.if opt_strS_i . opt_pu
strS_i0
     PHA
     jsr redir
     PLA
     ;jmp pr_int
.endif

pr_int  .block   ;print A:X
;used r3l,r3h,r0l,r1h
         ldy #0
         sty r1h
         ldy #32
         ora #0
         bpl l1

         pha
         ldy #"-"
         txa
         eor #$ff
         tax
         pla
         eor #$ff
         inx
         bne l1

         clc
         adc #1
l1       sta r0l
         .bend

         tya
         jsr xBSOUT
         ldy #<10000
         sty r3l
         ldy #>10000
         sty r3h
         jsr pr0
pr_int0
         ldy #<1000
         sty r3l
         ldy #>1000
         sty r3h
         jsr pr0

         ldy #0
         sty r3h
         ldy #100
         sty r3l
         jsr pr0
pr_int2
         lda #10
         sta r3l
         jsr pr0
         inc r1h
         txa
         tay
prd      tya
         bne prd2

         ldy r1h
         bne prd1
         rts

prd2     inc r1h
prd1     eor #$30
         jmp xBSOUT

pr0      ldy #255
         sec
prn      iny
         txa
         sbc r3l
         tax
         lda r0l
         sbc r3h
         sta r0l
         bcs prn

         txa
         adc r3l
         tax
         lda r0l
         adc r3h
         sta r0l
         bcs prd

xstringout0
         sta r2h
xstringout    ;print string at $bc-bd
.block
          ldy #0
          lda ($bc),y
          tax
rep       beq exit3

          iny
          lda ($bc),y
          jsr coutROM
          dex
          jmp rep
.bend

getvarval
          TSX
          LDA $104,X
          STA $BD
          LDA $103,X
          STA $BC
          LDY #1
          LDA ($BC),Y
          STA $104,X
          DEY
          LDA ($BC),Y
          STA $103,X
exit3     RTS

.if opt_strS_i
strS_i
     jsr strS_i0
strS_i1
     LDY #0
     LDA strdcurre
     STA r5l
     SEC
     ADC (strdcurre),y
.endif
.if opt_strS_i . opt_TIs
strS_i3
     STA strdcurre
     LDA strdcurre+1
     STA r5h
     ADC #0
     STA strdcurre+1
     ;JMP gc  ;gc is next!
.endif

.if opt_gc
.if opt_gc_fast
.include "gc2.s"
.endif
.ifeq opt_gc_fast
.include "gc.s"
.endif
.endif

.if opt_pr_real . opt_strS_i . opt_TIs . opt_pu
redir    ldy #0
         sty r4h
         tya
         sta (strdcurre),y
         rts
.endif

.if opt_getkey_I
getkeyint
        .block
        STA r0h
        lda $98
        beq ll1

        JSR BASIN
        jmp getkeyint0

ll1     JSR GETIN
        .bend
.endif

.if opt_getkey_I . opt_getkey_I0
getkeyint0
        LDY #0
        SEC
        SBC #48
        BCS *+3
        TYA
        CMP #10
        BCC *+3
        TYA
        STA (r0l),Y
        TYA
        INY
        STA (r0l),Y
        rts
.endif

.if opt_intfromkbd
intfromkbd
     .block
     STA r1h
     jsr strfromkbd0
     tya
     ldy #0
     sta r5h
     sta (strdcurre),y
     LDX strdcurre+1
     stx r3h
     LDX strdcurre
     stx r3l
     ldx r5h
     beq ll2

ll1  iny
     lda (r3l),y
     cmp #$3a
     bcs err

     cmp #32
     beq ll7

     cmp #"-"
     beq ll7

     cmp #"+"
     beq ll7

     cmp #$30
     bcc err     

ll7  dex
     bne ll1
ll2  jmp intfromdata0

err  sta $ff3e
     jsr JPRIMM
     .byte 13
     .text "?redo from start"
     .byte 13,0
     sta $ff3f
     .bend
     pla
     pla
.endif

.if opt_input
input_redo
     jmp 0

input0
     pla
     tay
     pla
     tax
     pha
     tya
     pha
     sec
     sbc #2
     sta input_redo+1
     bcs *+3
     dex
     stx input_redo+2 
     rts

input1
     LDA #"?"
     JSR coutROM
     LDA #32
     JSR coutROM
     LDA #0
     STA r5l
     rts
.endif

.if opt_strfromkbd . opt_intfromkbd
strfromkbd0
;returns: r5h,r2h,Y
;used: r5l,r2l,X
     .block
     lda #2
     bit r5l
     bne skip

     lda #4
     bit r5l
     beq noprompt

     sta $ff3e
     jsr JPRIMM
     .byte 13,63,63,32,0
     sta $ff3f
noprompt
     lda #$fb
     and r5l
     sta r5l
     ldy #1
     sty r2l
ll2  sta $ff3e
     jsr BASIN
     sta $ff3f
     sta r2h
     tax
     cmp #$d
     beq ll3

     lda #8
     ora r5l
     sta r5l
     cpx #","
     beq ll1

     cpx #32
     bne ll6

     lda r2l
     bne ll2

ll6  lda #0
     sta r2l
     txa
     sta (strdcurre),y
     iny
     bne ll2
;error
ll3  lda #8
     bit r5l
     bne ll5

     lda #2
     ora r5l
     sta r5l
skip pla
     pla
     rts

ll5  lsr
     ora r5l
     sta r5l
ll1  dey
     rts
     .bend

strfromkbd_extra
.block
     sta $ff3e
     lda r2h
     cmp #$d
     beq ll1

ll2  jsr BASIN
     cmp #$d
     bne ll2

     jsr JPRIMM
     .byte 13
     .text "?extra ignored"
     .byte 0
     lda #$d
ll1  jsr cout
     jmp coutROM0
     .bend
.endif

.if opt_intfromfile
intfromfile
     STA r1h
     jsr strfromfile0
     tya
     ldy #0
     sta (strdcurre),y
     LDX strdcurre+1
     stx r3h
     LDX strdcurre
     stx r3l
     jmp intfromdata0
.endif

.if opt_strfromfile . opt_intfromfile
strfromfile0
     .block
ll3  ldy #1
     sty r2l
ll2  sta $ff3e
     jsr BASIN
     ;bne error
     sta $ff3f
     sta r2h
     inc r5l
     cmp #32
     bne ll6

     ldx r2l
     bne ll2

ll6  ldx #0
     stx r2l

     cmp #$d
     beq ll1

     cmp #","
     beq ll1

     sta (strdcurre),y
     iny
     bne ll2
;error
ll1  dec r5l
     beq ll3

     dey
     rts
     .bend

strfromfile_extra
     .block
     sta $ff3e
     lda r2h
     cmp #$d
     beq ll1

ll2  jsr BASIN
     cmp #$d
     bne ll2

ll1  jmp coutROM0
     .bend
.endif

.if opt_strfromfile
strfromfile
     STA r4h
     jsr strfromfile0
     jmp strfromkbd1
.endif

.if opt_string
s_ASSIGN_s0
.block
     ldy #1
     lda (r3l),y
     tax
     dey
     lda (r3l),y
     cpx #>strsdyn
     bcc const
     bne s_ASSIGN_s1

     cmp #<strsdyn
     bcs s_ASSIGN_s1
const
     sta (r4l),y
     iny
     txa
     sta (r4l),y
     rts
.bend
.endif

.if opt_strfromdata
strfromdata
     STA r4h
     JSR commonfromdata
     jmp s_ASSIGN_s
.endif

.if opt_strfromdata . opt_string . opt_strfromfile . opt_strfromkbd
s_ASSIGN_s1
     LDY #0         ;it continues strfromdata
     LDA (r3l),Y
     TAX
     INY
     LDA (r3l),Y
     STA r3h
     STX r3l

s_ASSIGN_s           ;(R3)->(R4)
     LDY #0
     STY r5h
     LDA strdcurre
     STA (r4l),Y
     INY
     LDA strdcurre+1
     STA (r4l),Y
     DEY
     LDA (r3l),Y
     STA (strdcurre),Y
     TAX
     BNE ll2s
     BEQ ll7s

s_PLUS_s            ;(R4)+(R3)
     STA r4h
     LDA strdcurre  
     STA r5l
     LDA strdcurre+1
     STA r5h
     LDY #0
     LDA (r4l),Y
     STA (strdcurre),Y
     TAX
     BEQ ll0s

     TAY
ll1s LDA (r4l),Y
     STA (strdcurre),Y
     DEY
     BNE ll1s

ll0s CLC
     TXA
     ADC strdcurre
     STA strdcurre
     BCC *+4
     INC strdcurre+1
     LDA (r3l),y
     TAX
     CLC
     ADC (r5l),y
     STA (r5l),y
     TXA
     BEQ ll7s

ll2s .block
     TAY
loop LDA (r3l),y
     STA (strdcurre),y
     DEY
     BNE loop
     .bend
ll7s TXA
     JMP strfromkbd2
.endif

.if opt_s_assign_se
s_ASSIGN_se
     STA r2h
     LDY #0
     TXA
     STA (r2l),Y
     LDA $3F
     INY
     STA (r2l),Y
     rts
.endif

.if opt_s_assign_ti
s_ASSIGN_ti
.block
     STA r5h
     LDY #0
     sty r2h
     sty r1h
     sty r4l
     sty r4h
     LDA (r5l),Y
     cmp #6
     bne error14

     jsr get2digits
     sta r2l
     lda #60
     sta r1l
     jsr mul16
     stx r2l
     ;lda r0h
     sta r2h
     ldy $14
     jsr get2digits
     adc r2l
     sta r2l
     bcc *+4
     inc r2h
     jsr xmul24
     ;lda r0h
     sta r4h
     stx r2l
     lda r0l
     sta r2h
     ldy $14
     jsr get2digits
     adc r2l
     sta r2l
     bcc l1

     inc r2h
     bne *+4
     inc r4h
l1   jsr xmul24
     ;lda r0h
     sta $a3
     stx $a5
     lda r0l
     sta $a4
     rts

xmul24
     lda #60
     sta r1l
     jmp mul24

get2digits
     iny
     lda (r5l),y
     jsr checkdigit
     asl
     sta $14
     asl
     asl
     adc $14
     sta $14
     iny
     lda (r5l),y
     jsr checkdigit
     adc $14
     sty $14
     rts

checkdigit 
      eor #"0"
      cmp #0
      bcc error14

      cmp #9+1
      bcs error14
      rts
.bend
.endif

error14 lda #14  ;illegal quantity
      jmp traphandle2

.if opt_hex2dec
hex2dec  ;in: R4
         .block
         STA r4h
         ldy #0
         sty r3h
         sty r3l
         lda (r4l),y
         beq exit

         tay
         jsr hexconv
         sta r3l
         dey
         beq exit

         jsr hexconv
         asl
         asl
         asl
         asl
         ora r3l
         sta r3l
         dey
         beq exit

         jsr hexconv
         sta r3h
         dey
         beq exit

         jsr hexconv
         asl
         asl
         asl
         asl
         ora r3h
         sta r3h
exit     rts

hexconv  lda (r4l),y
         cmp #"9"+1
         bcs ll1

         sbc #"0"-1
         rts

ll1      sbc #"a"-10
         rts
         .bend
.endif

.if opt_hexconv
hexdigi2 LSR
         LSR
         LSR
         LSR
hexdigit EOR #$30
         .block
         CMP #$3A
         BCC ll1

         ADC #6
ll1      INY
         STA (strdcurre),Y
         RTS
         .bend

hexmain  LDA strdcurre
         STA r5l
         LDA strdcurre+1
         STA r5h
         LDY #0
         LDA #4
         STA (strdcurre),Y
         LDA r3h
         JSR hexdigi2
         LDA r3h
         AND #$F
         JSR hexdigit
         LDA r3l
         JSR hexdigi2
         LDA r3l
         AND #$F
         JSR hexdigit
         LDA #4
         JMP strfromkbd2
.endif

.if opt_str2dec . opt_intfromfile . opt_intfromkbd . opt_intfromdata
str2dec          ;IN: (R3); OUT: r0h:r4h:r4l
;used r2l
         .block
         jsr exit0
         ldx #0
         lda (r3l),y
         beq exit

         tay
ll7      lda (r3l),y
         cmp #" "
         bne ll8

         dey
         beq exit
         bne ll7

ll8      eor #"0"
         sta r4l
ll6      dey
         beq exit

         lda (r3l),y
         cmp #"-"
         beq sign

         cmp #"+"
         beq exit

         cmp #" "
         beq ll6

         eor #"0"
         beq ll4

         cmp #10
         bcs exit0

         sta r2l
         clc
ll5      lda tobin,x
         adc r4l
         sta r4l
         lda tobin+1,x
         adc r4h
         sta r4h
         lda tobin+2,x
         adc r0h
         sta r0h
         dec r2l
         bne ll5

ll4      inx
         inx
         inx
         cpx #21
         bne ll6

sign     lda r4l
         eor #$ff
         clc
         adc #1
         sta r4l

         lda r4h
         eor #$ff
         adc #0
         sta r4h

         lda r0h
         eor #$ff
         adc #0
         sta r0h
exit     rts

exit0    ldy #0
         sty r0h
         sty r4l
         sty r4h
         rts
         .bend
.endif

.if opt_midS_s_i . opt_midS_s_i_i
rightS_s_i  ;R3 - len, R2 - string
     STA r2h
     LDY #0
     LDA (r2l),Y
     SEC
     SBC r3l
     BCS *+4
     LDA #0
     STA r4l

midS_s_i   ;R4 - pos, R2 - string
     LDA strdcurre
     STA r5l
     LDA strdcurre+1
     STA r5h
     LDY #0
     LDA (r2l),Y
     SEC
     SBC r4l
     TAX
     BCC midS_s_i_i0

     LDA r4l
     CLC
     ADC r2l
     STA r2l
     BCC *+4
     INC r2h
     .block
ll3  INY
     LDA (r2l),y
     STA (strdcurre),y
     DEX
     BNE ll3
     .bend

midS_s_i1
     TYA
     LDY #0
     STA (r5l),Y
     BEQ strfromkbd2

leftS_s_i  ;R3 - len, R2 - string
     STA r2h
     LDA #0
     STA r4l

midS_s_i_i   ;R4 - pos, R3 - length, R2 - string
     LDA strdcurre
     STA r5l
     LDA strdcurre+1
     STA r5h
     LDY #0
     LDA (r2l),Y
     SEC
     SBC r4l
     TAX
     BCS midS_s_i_i1

midS_s_i_i0
     TYA
     STA (strdcurre),Y
     BEQ strfromkbd2

midS_s_i_i1
     LDA r3l
     BEQ midS_s_i_i0

     LDA r4l
     CLC
     ADC r2l
     STA r2l
     BCC *+4
     INC r2h
     .block
ll3  INY
     LDA (r2l),Y
     STA (strdcurre),Y
     DEC r3l
     BEQ midS_s_i1

     DEX
     BNE ll3
     BEQ midS_s_i1
     .bend
.endif

.if opt_strfromkbd
strfromkbd
     STA r4h
     jsr strfromkbd0
.endif

.if opt_strfromkbd . opt_strfromfile
strfromkbd1
     sty r5h
     ldy #1
     LDA strdcurre+1
     sta (r4l),y
     dey
     LDA strdcurre
     sta (r4l),y

     lda r5h
     sty r5h
     sta (strdcurre),y
.endif
.if opt_strfromkbd . opt_strfromfile . opt_strfromdata . opt_string . opt_hexconv . opt_chr . opt_getkey_S . opt_midS_s_i . opt_midS_s_i_i . opt_errs
strfromkbd2
     sec
     adc strdcurre
     sta strdcurre
     bcc *+4
     inc strdcurre+1
     jmp gc
.endif

.if opt_midS_s_i_s
midS_s_i_s    ;MID(R4,R3)=R1
     .block
     STA r4h
     DEC r3l
     LDY #1
     LDA (r4l),Y
     TAX
     DEY
     LDA (r4l),Y
     STX r4h
     STA r4l
     LDA r3l
     CLC
     ADC r4l
     STA r4l
     BCC *+4
     INC r4h
     LDA (r1l),Y
     BEQ ll1

     TAX
ll2  INY
     LDA (r1l),Y
     STA (r4l),Y
     DEX
     BNE ll2
ll1  RTS
     .bend
.endif

.if opt_chr
chrS_i 
     LDA strdcurre
     STA r5l
     LDA strdcurre+1
     STA r5h
     LDA r3l
     LDY #1
     bne chrS_i0   ;=jmp
.endif

.if opt_getkey_S
getkey1
     STA r4h
     sta $ff3e
     jsr GETIN
     tax           ;??
     BEQ *-4
     bne getkey0

getkey2
     sta $ff3e
     jsr GETIN
     tax            ;??
     bne getkey0

getkey5
     sta $ff3f
     LDY #0
     lda #<lib_end
     STA (r4l),Y
     INY
     lda #>lib_end
     STA (r4l),Y
     rts

getkey3
     STA r4h
     bne getkey2    ;=jmp

getkey0
     sta $ff3f
     tax
     LDY #0
     STY r5h
     LDA strdcurre
     STA (r4l),Y
     INY
     LDA strdcurre+1
     STA (r4l),Y
     txa
.endif

.if opt_chr . opt_getkey_S
chrS_i0
     STA (strdcurre),Y
     TYA
     DEY
     STA (strdcurre),Y
     LDA #1
     JMP strfromkbd2
.endif

.if opt_instr
instr     .block
          sty r2l
          ldy #0
          lda (r4l),y
          sec
          sbc r2l
          beq ll0
          bcc ll0

          sta r5l
          lda r4l
          sec
          adc r2l
          sta r4l
          bcc *+4
          inc r4h

          lda (r3l),y
          sta r2h
          lda r5l
          sec
          sbc r2h
          bcc ll0

          sty r5h
          sta r5l
          inc r3l
          bne ll2a

          inc r3h
ll2a      lda (r3l),y
          cmp (r4l),y
          beq ll1

ll4       lda r5l
          beq ll0

          inc r5h
          dec r5l
          inc r4l
          bne ll2a

          inc r4h
          bne ll2a

ll1       ldx r2h
          dex
          beq ll6

ll5       iny
          lda (r3l),y
          cmp (r4l),y
          bne ll3

          dex
          bne ll5

ll6       lda r2l
          sec
          adc r5h
          tay
ll0       lda #0
          rts

ll3       ldy #0
          beq ll4
          .bend
.endif

.if opt_s_GE_s . opt_s_LE_s . opt_s_GT_s . opt_s_LT_s
s_GT_s3
     sta r4h
     ldy #255
     sty r5l
     iny
     lda (r3l),y
     tax
     lda (r4l),y
     sta r1l
     rts
.endif

.if opt_s_LE_s
s_LE_s     ;(R4) <= (R3)
     .block
     jsr s_GT_s3
ll1  txa
     ora r1l
     beq s_GE_s1

     txa
     beq s_GE_s0

     dex
     lda r1l
     beq s_GE_s1

     dec r1l
     iny
     lda (r3l),y
     cmp (r4l),y
     bcc s_GE_s0
     beq ll1
     bne s_GE_s1
     .bend
.endif

.if opt_s_LT_s
s_LT_s    ;(R4) < (R3)
     .block
     jsr s_GT_s3 
ll1  txa
     beq s_GE_s0

     dex
     lda r1l
     beq s_GE_s1

     dec r1l
     iny
     lda (r3l),y
     cmp (r4l),y
     beq ll1
     bcc s_GE_s0
     bcs s_GE_s1
     .bend

.endif

.if opt_s_GE_s
s_GE_s    ;(R4) >= (R3)
     .block
     jsr s_GT_s3
ll1  txa
     ora r1l
     beq s_GE_s1

     txa
     beq s_GE_s1

     dex
     lda r1l
     beq s_GE_s0

     dec r1l
     iny
     lda (r3l),y
     cmp (r4l),y
     bcc s_GE_s1
     beq ll1
     .bend
.endif

.if opt_s_GE_s . opt_s_LE_s . opt_s_GT_s . opt_s_LT_s . opt_s_EQ_s . opt_s_NE_s
s_GE_s0
     inc r5l
s_GE_s1
     lda r5l
     rts
.endif

.if opt_s_GT_s
s_GT_s       ;(R4) > (R3)
     .block
     jsr s_GT_s3
ll1  lda r1l
     beq s_GE_s0

     dec r1l
     txa
     beq s_GE_s1

     dex
     iny
     lda (r3l),y
     cmp (r4l),y
     beq ll1
     bcs s_GE_s0
     bcc s_GE_s1
     .bend
.endif

.if opt_s_EQ_s . opt_s_NE_s
s_EQ_s    ;(R3) = (R4)
     .block
     sta r4h
     ldy #255
     sty r5l
     iny
     lda (r3l),y
     tax
     inx
ll1  lda (r3l),y
     cmp (r4l),y
     bne s_GE_s0

     iny
     dex
     bne ll1
     beq s_GE_s1
     .bend
.endif

.if opt_div16
div16  ; R3/R2 -> R3
.block
dividend = r3l
quotient = r3l
remainder = r1l
divisor = r2l
sign = r4l
        .block
        lda divisor
        ora divisor+1
        beq error20

        ldx #0
        lda dividend+1
        bpl l1

        inx
        jsr alt16
l1      lda divisor+1
        bpl l2

        dex
        eor #$ff
        sta divisor+1
        lda divisor
        eor #$ff
        tay
        iny
        sty divisor
        bne *+4
        inc divisor+1
l2      stx sign
.bend
        ldy #8
        lda #0
        clc
.block
l3	rol dividend+1
	rol
	ldx divisor+1
        bne l1

        cmp divisor
        bcc l1

l2      sbc divisor
l1      dey
        bne l3
.bend
        sta remainder
        tya
        rol dividend+1
        ldy #8
.block
l3      rol dividend
       	rol remainder
	rol
	cmp divisor+1
        bcc l1
        bne l2

        ldx remainder
        cpx divisor
        bcc l1

l2      tax
        lda remainder
        sbc divisor
        sta remainder
        txa
        sbc divisor+1
l1      dey
        bne l3
.bend
        rol dividend
.block
;the rounding code is commented
;        asl remainder
;        rol
;        sta remainder+1
;        cmp divisor+1
;        bcc l1
;        bne l2

;        lda remainder
;        cmp divisor
;        bcc l1

;l2      inc quotient
;        bne *+4
;        inc quotient+1

l1      ldx sign
        bne alt16

.bend
	rts
.bend
.endif
.if opt_div16 . opt_div24_16
error20 lda #20  ;division by zero
       jmp traphandle2
.endif

.if opt_div16 . opt_div24_16 . opt_div24
alt16   sec
.block
dividend_lo = r3l
dividend_mi = r3h
        lda dividend_lo
        eor #$ff
        adc #0
        sta dividend_lo
        lda dividend_mi
        eor #$ff
        adc #0
        sta dividend_mi
        rts
.bend
.endif

.if opt_div24_16
div24_16  ; r1l:r3h:r3l/R2 -> r1l:r3h:r3l
.block
dividend_lo = r3l
dividend_mi = r3h
dividend_hi = r1l ;should be after r3h?
;quotient = dividend
remainder = r0l
divisor = r2l
sign = r4l
        .block
        lda divisor
        ora divisor+1
        beq error20

        ldx #0
        ldy dividend_hi
        bpl l1

        inx
        jsr alt24
l1      lda divisor+1
        bpl l2

        dex
        eor #$ff
        sta divisor+1
        lda divisor
        eor #$ff
        tay
        iny
        sty divisor
        bne l2

        inc divisor+1
l2      stx sign
.bend
        ldy #8
        lda #0
        clc
.block
l3	rol dividend_hi
	rol
	ldx divisor+1
        bne l1

        cmp divisor
        bcc l1

l2      sbc divisor
l1      dey
        bne l3
.bend
        sta remainder
        tya
        rol dividend_hi
        ldy #8
.block
l3      rol dividend_mi
       	rol remainder
	rol
	cmp divisor+1
        bcc l1
        bne l2

        ldx remainder
        cpx divisor
        bcc l1

l2      tax
        lda remainder
        sbc divisor
        sta remainder
        txa
        sbc divisor+1
l1      dey
        bne l3
.bend
        rol dividend_mi
        ldy #8
.block
l3      rol dividend_lo
       	rol remainder
	rol
	cmp divisor+1
        bcc l1
        bne l2

        ldx remainder
        cpx divisor
        bcc l1

l2      tax
        lda remainder
        sbc divisor
        sta remainder
        txa
        sbc divisor+1
l1      dey
        bne l3
.bend
        rol dividend_lo
        sta remainder+1
.block
;the rounding code is missed
        ldx sign
        bne alt24
.bend
	rts
.bend
.endif

.if opt_div24_16 . opt_div24
alt24   sec
.block
dividend_lo = r3l
dividend_mi = r3h
dividend_hi = r1l
        jsr alt16
        lda dividend_hi
        eor #$ff
        adc #0
        sta dividend_hi
        rts
.bend
.endif

.if opt_div24
diverr24  jmp error20
div24   ; r1l:r3h:r3l/r5l:r2h:r2l -> r1l:r3h:r3l
.block
;free: r4h,r1h
dividend_lo = r3l
dividend_mi = r3h
dividend_hi = r1l ;should be after r3h?
;quotient = dividend
remainder_lo = r0l
remainder_mi = r0h
remainder_hi = r5h
divisor_hi = r5l
divisor_mi = r2h
divisor_lo = r2l
sign = r4l
        .block
        lda divisor_lo
        ora divisor_mi
        ora divisor_hi
        beq diverr24

        ldx #0
        ldy dividend_hi
        bpl l1

        inx
        jsr alt24
l1      ldy divisor_hi
        bpl l2

        dex
        lda divisor_lo
        eor #$ff
        adc #0
        sta divisor_lo
        lda divisor_mi
        eor #$ff
        adc #0
        sta divisor_mi
        tya
        eor #$ff
        adc #0
        sta divisor_hi
l2      stx sign
.bend
        clc
	lda divisor_hi
	ora divisor_mi
        beq ll1

        lda dividend_hi
        ldy #0
        sty dividend_hi
        beq ll2

ll1     ldy #8
        lda #0
.block
l3	rol dividend_hi
	rol
        cmp divisor_lo
        bcc l1

l2      sbc divisor_lo
l1      dey
        bne l3
.bend
        rol dividend_hi
ll2     sta remainder_lo
        tya
        ldy #8
.block
l3      rol dividend_mi
       	rol remainder_lo
	rol
	ldx divisor_hi
        bne l1

	cmp divisor_mi
        bcc l1
        bne l2

        ldx remainder_lo
        cpx divisor_lo
        bcc l1

l2      tax
        lda remainder_lo
        sbc divisor_lo
        sta remainder_lo
        txa
        sbc divisor_mi
l1      dey
        bne l3
.bend
        rol dividend_mi
        sta remainder_mi
        tya
        ldy #8
.block
l3      rol dividend_lo
       	rol remainder_lo
        rol remainder_mi
	rol
	cmp divisor_hi
        bcc l1
        bne l2

        ldx remainder_mi
	cpx divisor_mi
        bcc l1
        bne l2

        ldx remainder_lo
        cpx divisor_lo
        bcc l1

l2      tax
        lda remainder_lo
        sbc divisor_lo
        sta remainder_lo
        lda remainder_mi
        sbc divisor_mi
        sta remainder_mi
        txa
        sbc divisor_hi
l1      dey
        bne l3
.bend
        rol dividend_lo
        sta remainder_hi
.block
;the rounding code is missed
        ldx sign
        beq exit7
        jmp alt24
.bend
.bend
.endif

xBSOUT   ldy r4h
         bne ll2g

         pha
         lda (strdcurre),y
         tay
         iny
         pla
         sta (strdcurre),y
         tya
         ldy #0
         sta (strdcurre),y
exit7    rts

ll2g     jmp coutROM

.if opt_intfromdata . opt_strfromdata
commonfromdata
     LDA datapos+1
     sta r3h
     lda datapos
     sta r3l
     ldy #0
     lda (r3l),y
     sec
     adc datapos
     sta datapos
     bcc *+4
     inc datapos+1
     RTS
.endif

.if opt_intfromdata
intfromdata
     STA r1h
     JSR commonfromdata
.endif

.if opt_intfromdata . opt_intfromkbd . opt_intfromfile
intfromdata0
     JSR str2dec
     ldy #0
     lda r4l
     sta (r1l),y
     iny
     lda r4h
     sta (r1l),y
     rts
.endif

.if opt_open
openfile
     ldy #0
     lda ($af),y
     sta $ab
     inc $af
     bne *+4

     inc $b0
openfile0
     sta $ff3e
     jsr OPEN       ;hi RAM works ok - tested
     jmp xCHOUT0
     ;jmp coutROM0
.endif

.if opt_print2tab
print2tab
.block
     lda $99
     cmp #3
     beq ll3

     ldx #10
     lda #32
ll4  JSR coutROM
     dex
     bne ll4
     rts

ll3  lda $ca
     clc
     adc #10
     cmp #40
     bcc ll2

     sbc #40
     tay
     lda #13
     jsr coutROM
     tya
ll2  ldy #0
ll1  cmp #10
     bcc ll0

     sbc #10
     iny
     bne ll1

ll0  tya
     asl
     sta $bc
     asl
     asl
     adc $bc
     sta $ca
     rts
.bend
.endif

.if opt_close
close
     sta $ff3e
     jsr CLOSE
     ;jmp coutROM0  ;remove opt_close for xCHOUT0
     jmp xCHOUT0
.endif

.if opt_char
char
     sta $ff3e
     JSR PLOTW
     sta $ff3f
     jmp xstringout
.endif

.if opt_sys
sys0
    STA $15
sys
    sta $ff3e
    jsr $A7B8
    jmp coutROM0
.endif

.if opt_clrch
xCLRCH
    sta $ff3e
    jsr CLRCH
    jmp coutROM0
.endif

.if opt_chkin
xCHKIN
    sta $ff3e
    jsr CHKIN
    bcc xCHOUT0
.endif
.if opt_chout . opt_chkin
chkerr
    lda #3  ;file not open
    jmp traphandle2
.endif

.if opt_chout
xCHOUT
    sta $ff3e
    jsr CHOUT
    bcs chkerr
.endif
.if opt_chout . opt_chkin . opt_close . opt_DSs . opt_open . opt_collect
xCHOUT0
    lda #0
    sta $79
    jmp coutROM0
.endif

.if opt_pexpr
spc lda $99
    cmp #3
    bne tab1

    LDA #29
spc0
    jsr coutROM
    dex
    bne spc0
    rts

tab lda $99
    cmp #3
    beq tab0

tab1
    lda #32
    bne spc0

tab0
    TXA
    .block
    CMP $CA
    BCC ll1

ll2 CMP #40
    BCC ll0

    SBC #40
    TAY
    LDA #13
    JSR coutROM
    TYA
    JMP ll2

ll0 STA $CA
ll1 rts
    .bend
.endif

.if opt_pu
pu1
    ldy #2
    sty rombi_hi
    ldy #0
    sty rombi_lo
    sta r5h
    stx r5l
    jsr pustr0

pusc 
    LDA #";"

pusc0
    LDY #1
    STA (rombi_lo),Y
    INC rombi_lo
    rts

pucomma
    LDA #","
    bne pusc0

pustr
    STA r5h
    ldy #0

pustr0
.block
    lda (r5l),y
    and #$3f   ;limits the max string length to 63
    tax
    lda #$22   ;"
    iny
    sta (rombi_lo),y
    clc
ll2 dex
    bmi ll1

    lda (r5l),y
    iny
    sta (rombi_lo),y
    bcc ll2

ll1 iny
    lda #$22   ;"
    sta (rombi_lo),y
.bend

pu0 tya
    adc rombi_lo
    sta rombi_lo
    rts

puint
.block
    ldy #0
    lda (strdcurre),y
    tax
ll2 dex
    bmi pu0

    iny
    lda (strdcurre),y
    cmp #"-"
    bne *+4
    lda #$ab
    sta (rombi_lo),y
    clc
    bcc ll2
.bend

puall
    LDA #0
    LDY #1
    STA (rombi_lo),Y
    STA $3B
    iny
    STY $3C
    sta $ff3e
    jsr $AEF7
    jmp coutROM0
.endif

.if opt_pr_real
pr_real1 LDY #1
         STY r4h
         STA r0h

pr_real  .block   ;print r0h:r0l:X
;used r3l,r3h,r1l,r1h
         ldy #0
         sty r1h
         ldy #32
         lda r0h
         bpl l1

         ldy #"-"
         eor #$ff
         sta r0h
         lda r0l
         eor #$ff
         sta r0l
         txa
         eor #$ff
         tax
         inx
         bne l1

         inc r0l
         bne l1

         inc r0h
l1       tya
         jsr xBSOUT
         ldy #$40
         sty r3l
         ldy #$42
         sty r3h
         ldy #$f
         sty r1l
         jsr pr0

         ldy #$a0
         sty r3l
         ldy #$86
         sty r3h
         ldy #1
         sty r1l
         jsr pr0

         ldy #$10
         sty r3l
         ldy #$27
         sty r3h
         ldy #0
         sty r1l
         jsr pr0
         jmp pr_int0

pr0      ldy #255
         sec
prn      iny
         txa
         sbc r3l
         tax
         lda r0l
         sbc r3h
         sta r0l
         lda r0h
         sbc r1l
         sta r0h
         bcs prn

         txa
         adc r3l
         tax
         lda r0l
         adc r3h
         sta r0l
         lda r0h
         adc r1l
         sta r0h
         tya
         bne prd1

         ldy r1h
         bne prd2
         rts

prd1     inc r1h
prd2     eor #$30
         jmp xBSOUT
.bend
.endif

.if opt_mul24 . opt_power24
mul24          ;r4l:r1h:r1l*r4h:r2h:r2l->r0h(=AC):r0l:XR
         .block
         ldx #0
         stx r0l
         stx r0h
         ldy #24
ll1      lsr r4l
         ror r1h
         ror r1l
         bcc ll2

         clc
         txa
         adc r2l
         tax
         lda r0l
         adc r2h
         sta r0l
         lda r0h
         adc r4h
         sta r0h
ll2      asl r2l
         rol r2h
         rol r4h
         dey
         bne ll1
         rts
         .bend
.endif

.if opt_TIs
TIs      LDA $A5
         STA r3l
         LDA $A4
         STA r3h
         LDA $A3
         STA r1l
         LDA #60
         STA r2l
         LDA #0
         STA r2h
         JSR div24_16
         lda #>3600
         sta r2h
         lda #<3600
         sta r2l
         jsr div24_16
         lda r0l
         pha
         jsr redir
         sty r3h
         sty r0l
         iny
         sty r1h
         ldx r3l
         jsr pr_int2
         pla
         sta r3l
         lda r0h
         sta r3h
         lda #>60
         sta r2h
         lda #<60
         sta r2l
         jsr div16
         ldx r3l
         jsr pr_int2
         ldx r1l
         jsr pr_int2
         clc
         lda strdcurre
         sta r5l
         adc #6
         jmp strS_i3
.endif

.if opt_DSi
DSi      jsr DSs
         lda strestatic-3
         sta r2h
         lda strestatic-4
         sta r2l
         ldy #1
         lda (r2l),y
         and #$f
         sta r5l
         asl
         asl
         adc r5l
         asl
         sta r5l
         iny
         lda (r2l),y
         and #$f
         adc r5l
         tax
         lda #0
.endif

.if opt_DSs
exit     rts

DSs      lda $79
         bne exit
.block
         lda $277
         bne *+4
         lda #8
         STA $ae
         LDX #0
         STX $ab
         STX $ac
         ldy #$6f
         STY $ad
         sta $ff3e
         jsr OPEN
         bcs err2

         ldx #0
         jsr CHKIN
         bcs err1

         ldy #0
l2       lda $90
         bne l5

         jsr BASIN
         ;bne error2

         iny
         sta (strdcurre),y
         bne l2

l5       jsr CLRCH
         sec
         lda #0
         jsr CLOSE
         sta $ff3f
         dey
         tya
         ldy #0
         sty r5h
         sta (strdcurre),y
         LDA strdcurre
         STA strestatic-4
         sec
         ADC (strdcurre),y
         STA strdcurre
         LDA strdcurre+1
         STA strestatic-3
         ADC #0
         sta $79
         STA strdcurre+1
         JMP gc

err1     lda #0
         jsr CLOSE
err2     jmp xCHOUT0
.bend
.endif

.if opt_pudef
pudef    STA r4h   ;generates error
         ldy #0
         lda (r4l),y
         beq puerr

         cmp #5
         bcs puerr
.block
         tax
l1       iny
         lda (r4l),y
         sta $4e6,y
         dex
         bne l1
         rts
.bend
.endif
.if opt_pudef . opt_joy
puerr    jmp error14
.endif

.if opt_joy
joy      dex
         cpx #2
         bcs puerr
.block
         sta $ff3e
         lda $bffb,x
         sta $ff08
         lda $ff08
         eor #$ff
         tax
         bmi l1

         asl
l1       and #$80
.bend
         sta r2l
         txa
         and #15
         tax
         lda $bff0,x
         sta $ff3f
         ora r2l
         tax
         lda #0
         rts
.endif

.if opt_sqr
sqrerr  jmp error14   ; x = (x + Value/x)/2

sqr     .block
        bmi sqrerr
;        beq d16

        sta $14
        sty 6
;        sty r1h   ;mi(x)
        stx 5
;        stx r4h   ;lo(x)
        ora 5
        ora 6
        beq ex1

        lda #4
        sta r4h
        lda #0
        sta r1h
loop    sta $15   ;hi(x)
        lda $14
        sta r1l
        lda 6
        sta r3h
        lda 5
        sta r3l
        lda r1h
        sta r2h
        lda r4h
        sta r2l
        jsr div24
        clc
        lda r4h
        tax
        adc r3l
        sta r4h
        lda r1h
        tay
        adc r3h
        sta r1h
        lda $15
        adc r1l
        lsr
        ror r1h
        ror r4h
        cmp $15
        bne loop

        cpy r1h
        bne loop

        cpx r4h
        bne loop

ex      lda r1h
        ldx r4h
ex1     rts
        .bend
.endif

.if opt_atn
atn     .block
        bmi lm
        beq lc

l2      ldx #2
l2x     lda #0
l2y     rts

lc      tya
        bne l2

        txa
        beq l2y

        cpx #15
        bcs l2

        ldx #1
        bne l2x  ;always

l3      ldx #$fe
l3x     lda #$ff
        rts

lm      cmp #$ff
        bne l3

        cpy #$ff
        bne l3

        cpx #$f2
        bcc l3

        ldx #$ff
        bne l3x  ;always
        .bend
.endif

.if opt_exp
experr  lda #15  ;overflow
        jmp traphandle2

exp     .block 
        bmi r0
        bne experr

        cpy #0
        bne experr

        cpx #16
        bcs experr

        lda exptabmi,x
        tay
        lda exptabhi,x
        pha
        lda exptablo,x
        tax
        pla
        rts
      
r0      lda #0
        tax
        tay
        rts
exptablo  .byte 1,3,7,20,55,$94,$93,$49,$a5,$a7,$a,$e2,$c3,$2d,$ac,$99
exptabmi  .byte 0,0,0,0,0,0,1,4,$b,$1f,$56,$e9,$7b,$c0,$59,$e1
exptabhi  .byte 0,0,0,0,0,0,0,0,0,0,0,0,2,6,$12,$31
        .bend
.endif

.if opt_log
log     .block
        bmi logerr

        cmp #$52
        bcc b16
        bne e16

        cpy #$3d
        bcc e15
        bne e16

        cpx #$83
        bcc e15

e16     LDA #0
        ldy #16
        rts        
        
b16     cmp #$1e
        bcc b15
        bne e15

        cpy #$41
        bcc e14
        bne e15

        cpx #$28
        bcc e14

e15     LDA #0
        ldy #15
        rts        

b15     cmp #$b
        bcc b14
        bne e14

        cpy #$21
        bcc e13
        bne e14

        cpx #$49
        bcc e13

e14     LDA #0
        ldy #14
        rts        

b14     cmp #4
        bcc b13
        bne e13

        cpy #$18
        bcc e12
        bne e13

        cpx #$32
        bcc e12

e13     LDA #0
        ldy #13
        rts

b13     cmp #1
        bcc b12
        bne e12

        lda #0
        cpy #$81
        bcc e11
        bne e12

        cpx #$9c
        bcc e11

e12     ldy #12
        rts

logerr  jmp error14

b12     lda #0
        cpy #$8d
        bcc b11
        bne e11

        cpx #$dc
        bcc e10

e11     ldy #11
        rts

b11     cpy #$34
        bcc b10
        bne e10

        cpx #$a8
        bcc e9

e10     ldy #10
        rts

b10     cpy #$13
        bcc b9
        bne e9

        cpx #$33
        bcc e8

e9      ldy #9
        rts

b9      cpy #7
        bcc b8
        bne e8

        cpx #$11
        bcc e7

e8      ldy #8
        rts

b8      cpy #2
        bcc b7
        bne e7

        cpx #$9a
        bcc e6

e7      ldy #7
        rts

e6      ldy #6
ex      rts

b7      ldy #6
        cpx #$f5
        bcs ex

        dey
        cpx #$5b
        bcs ex

        dey
        cpx #$22
        bcs ex

        dey
        cpx #$d
        bcs ex

        dey
        cpx #5
        bcs ex

b2      dey
        cpx #2
        bcs ex

        dex
        bmi logerr

        dey
        beq ex
        .bend
.endif

.if opt_collect
collect  ldy #1
         sty $ab
         stx $ae
         stx $277
         lda #$56   ;"V"
.endif
.if opt_collect . opt_scratch . opt_copy . opt_header
collect0 sta $200
         sta $ff3e
         jsr CLALL
         ldy #0
         sty $79
         sty $ac
         sty $af
         ldy #2
         sty $b0
         lda #$6f
         sta $ad
         jsr OPEN
         lda #0
         sec
         jsr CLOSE
         ;jmp coutROM0
         jmp xCHOUT0
.endif

.if opt_header
header   stx $ae
         stx $277
         ldy #0
         lda (r2l),y
         beq schlv_error
.block
         tax
l1       iny
         lda (r2l),y
         sta $201,y
         dex
         bne l1

         lda r3h
         beq l2

         sta $203,y
         lda #","
         sta $202,y
         lda r3l
         sta $204,y
         iny
         iny
         iny
l2
.bend
         lda #$4e   ;"N"
         bne copy0
.endif

.if opt_scratch
scratch  stx $ae
         stx $277
         ldy #0
         lda (r2l),y
         beq schlv_error
.block
         tax
l1       iny
         lda (r2l),y
         sta $201,y
         dex
         bne l1
.bend
         lda #$53   ;"S"
.endif
.if opt_scratch . opt_copy . opt_header
copy0    iny
         iny
         sty $ab
         ldx #":"
         stx $201
         bne collect0
.endif
.if opt_scratch . opt_copy . opt_header . opt_load . opt_verify
schlv_error jmp error14
.endif

.if opt_copy
copy     txa
.block
         bne l2

         ldx #8
         tya
         beq l2

         tax
l2       stx $ae
         stx $277
         ldy #0
         lda (r2l),y
         beq schlv_error

         tax
l1       iny
         lda (r2l),y
         sta $201,y
         dex
         bne l1

         iny
         lda #"="
         sta $201,y
         iny
         sty m1+1
         ldy #0
         lda (r3l),y
         beq schlv_error

         tax
l3       iny
         lda (r3l),y
m1       sta $201,y
         dex
         bne l3

         tya
         clc
         adc m1+1
         tay
         dey
.bend
         lda r4l   ;"C" or "R"
         bne copy0
.endif

.if opt_cat
cat      ldy #0
.block
         sty $3b
         txa
         beq l1

         eor #"0"  ;limits to u9 max, the next addresses too
         sta $201
         lda #$55   ;"U"
         sta $200
         ldx #2
l1       lda r2h
         beq l5

         lda (r2l),y
         beq l5

         sta r3l
         txa
         beq l3

         lda #","
         sta $202
         inx
l3       lda #34  ;"
         sta $200,x
l4       inx
         iny
         lda (r2l),y
         sta $200,x
         dec r3l
         bne l4

         lda #34  ;"
         inx
         sta $200,x
         inx
l5       lda #0
         sta $200,x
l2       lda #2
         sta $3c
         sta $ff3e
         jsr $c8bc   ;directory
         jmp coutROM0
.bend
.endif

.if opt_load
load     jsr load0
         lda #>postload-1
         pha
         lda #<postload-1
         pha
         lda #0
         jmp LOADSP
.endif
.if opt_load . opt_verify . opt_save
load0    sta $b0
         ldy #0
         lda ($af),y
         beq lv_error

         sta $ab
         inc $af
         bne *+4
         inc $b0
         ldx #1
         ldy prgbase
         sta $ff3e
         rts
.endif
.if opt_load . opt_verify . opt_sound . opt_key . opt_save
lv_error jmp error14
.endif

.if opt_verify
verify   jsr load0
         txa
         jsr LOADSP
         bcs lv_error
         ;jmp coutROM0
         jmp xCHOUT0
.endif

.if opt_save
save     jsr load0
         lda #$2b
         ldx $2d
         ldy $2e
         jsr SAVESP
         bcs lv_error
         ;jmp coutROM0
         jmp xCHOUT0
.endif

;.if  opt_sound . opt_key
;sk_error jmp error14
;.endif
.if opt_sound
sound    dex
.block
         stx $80
         cpx #3
         bcs lv_error

         cpx #2
         bne l1

         dex
l1       tya
         ora r2h
         beq l2

         lda r3h
         cmp #4
         bcs lv_error

l3       ;check run-stop
         LDA  $4FE,X
         ora  $4fc,x
         bne l3

l4       tya
         eor #$ff
         ;clc
         adc #1
         sei
         sta $4fc,x
         lda r2h
         eor #$ff
         adc #0
         sta $4fe,x
         lda r3l
         sta $ff0e,x
         lda snd1tab,x
         tax
         lda $ff10,x
         and #$fc
         ora r3h
         sta $ff10,x
         ldx $80
         lda snd2tab,x
         ora $ff11
         sta $ff11
         cli
         rts

l2       iny
         bne l4
.bend
.endif

.if opt_key
key      txa
         dex
         cpx #8
         bcs lv_error
.block
         eor #"0"
         sta $200
         lda #2
         sta $3c
         ldy #0
         sty $3b
         lda #","
         sta $201
         lda (r2l),y
         tax
l1       iny
         lda (r2l),y
         sta $202,y
         dex
         bne l1
.bend
         txa
         sta $204,y
         lda #34     ;"
         sta $202
         sta $203,y
         sta $ff3e
         jsr $b7a7
         jmp coutROM0
.endif

i_assign_i
         STA r2h
         LDY #0
         TXA
         STA (r2l),Y
         LDA r3l
         INY
         STA (r2l),Y
         rts

r_assign_r
         STA r2h
         LDY #0
         LDA r3l
         STA (r2l),Y
         LDA r3h
         INY
         STA (r2l),Y
         txa
         INY
         STA (r2l),Y
         rts

.if opt_color
color    dec r5l
         lda r5l
         and #15
         sta r5l
         tya
         asl
         asl
         asl
         asl
         ora r5l
         cpx #0
.block
         bne fg

         sta $ff15
         rts

fg       dex
         bne mc1

         sta $86
         sta $53b
         rts

mc1      dex
         bne mc2

         sta $85
         rts

mc2      dex
         bne bg

         sta $ff16
         rts

bg       dex
         bne err_rclr

         sta $ff19
         rts
.bend
.endif

.if opt_rclr
rclr     clc
         txa
.block
         bne fg

         lda $ff15
         bcc l1   ;=jmp

fg       dex
         bne mc1

         lda $86
         bcc l1      ;=jmp

mc1      dex
         bne mc2

         lda $85
         bcc l1      ;=jmp

mc2      dex
         bne bg

         lda $ff16
         bcc l1      ;=jmp

bg       dex
         bne err_rclr

         lda $ff19
l1       adc #1
         and #15
         tay
         txa
         rts
.bend
.endif
.if opt_rclr . opt_rlum . opt_color
err_rclr jmp error14
.endif

.if opt_rlum
rlum     clc
         txa
.block
         bne fg

         lda $ff15
         bcc l1   ;=jmp

fg       dex
         bne mc1

         lda $86
         bcc l1      ;=jmp

mc1      dex
         bne mc2

         lda $85
         bcc l1      ;=jmp

mc2      dex
         bne bg

         lda $ff16
         bcc l1      ;=jmp

bg       dex
         bne err_rclr

         lda $ff19
l1       and #$7f
         lsr
         lsr
         lsr
         lsr
         tay
         txa
         rts
.bend
.endif

.if opt_rgr
rgr      lda $83
.block
         bne gr

         tay
         rts

gr       ldy #1
         cmp #$20
         beq lx

         iny
         cmp #$60
         beq lx

         iny
         cmp #$a0
         beq lx

         iny
lx       lda #0
.bend
         rts
.endif

.if opt_errs
errS     
         tax
         LDA strdcurre
         STA r5l
         LDA strdcurre+1
         STA r5h
.block
         dex
         txa
         sta $ff3e
         jsr $8653
         ldy #0
l2       lda ($24),y
         php
         bpl l1

         eor #$80
l1       iny
         sta (strdcurre),y
         plp
         bpl l2

         sta $ff3f
         tya
         ldy #0
         sta (strdcurre),y
         jmp strfromkbd2
.bend
.endif

baseptr   .byte 0    ;for FN parameter stack
prgbase   .byte $10  ;$40 after graphics on
snd1tab   .byte 2,0
snd2tab   .byte $10,$20,$40
tobin     .byte 10,0,0
          .byte 100,0,0
          .byte $e8,3,0
          .byte $10,$27,0
          .byte $a0,$86,1
          .byte $40,$42,$f

