        tabs    8,8                     ;first tab is 8, then each tab is 8 ahead
        width   160                     ;prevent folding of long lines
        noproces                        ;disable fancy stuff, speed is what we want
;
        title   "Phunsy 2650 Micro Computer"
        stitle  "PHUNSY text Editor"
;       author: Frank Philipse
;       this editor was originally written in 1980 in hex using
;       for fun I made it suitable for the vax assembler
;       a few changes were made, probably no improvements
;       FP-2010-11-25 2014-03-22 (www.philipse.info)
;
        name    pheditor                ;the module name
;
pm      equ     01fffh                  ; page mask - this is to fool the VASC assembler
;
        INCLUDE "2650.inc"              ; 2650 definitions
        INCLUDE "ascii.inc"             ; ascii definitions
        INCLUDE "monitor.inc"           ; monitor definitions
        INCLUDE "phunsy.inc"            ; phunsy definitions
;
;                                       == some extra monitor definitions
monfree equ     00e50h                  ; some free monitor space (E50-EDF)
srout   equ     007ABh                  ; serial data out for printer
;
;                                       == editor definitions
TXTBBEG EQU  16384                      ; text buffer begin address (1Q 4000)
TXTBEND EQU  32768-1                    ; text buffer end address   (1Q 7FFF)
mxlnln  equ 60                          ; maximum line length
;
        section ram
        org     05E00H
;
RSLT    ds      16                      ;
OTPR    ds      8                       ;
INPR    ds      8                       ;
SLCT    ds      8                       ;
COND    ds      8                       ;
;
WRERES  ds      1                       ;
EPTRES  ds      1                       ;
RETRES  ds      1                       ;
INCRST  ds      1                       ;
NBITIN  ds      1                       ;
NBYTIN  ds      1                       ;
NBITOT  ds      1                       ; not addressed ?
NBYTOT  ds      1                       ;
;
SBF0    ds      64                      ;
SBF1    ds      64                      ; string buffers
SBF2    ds      64                      ;
;
TXTRES  ds      1                       ;
INSR    ds      2                       ; pointer of string buffers
ERRN    ds      2                       ; poiter to print specific error message
NRCHNL  ds      1                       ;
NRCHOL  ds      1                       ;
INPRS0  ds      1                       ;
INPRS1  ds      1                       ;
INPRS3  ds      1                       ;
NBLRES  ds      1                       ;
CMDV    ds      2                       ; command vector, branch address
CMDRES  ds      1                       ; temp storage command byte
RSBF    ds      3                       ; temp 3 byte value
RSPA    ds      2                       ;
RSPB    ds      2                       ;
MESC    ds      2                       ; used for print message
;
        section extrom                  ; external rom, program resides in common none-banked area
        ORG     03F00H                  ;
EXTCODE EQU     $                       ;
;
MOVUPA  LODA,R0 *CPORAC&pm              ;
        STRZ    R2                      ;
        STRZ    R3                      ;
        ANDI,R2 0F0H                    ;
MOVUPB  LODI,R1 003H                    ;
MOVUPC  LODA,R0 MCTA&pm,R1-             ;
        COMA,R0 CLAD&pm,R1              ;
        BCTR,GT MOVUPD                  ;
        BRNR,R1 MOVUPC                  ;
        BCTR,UN LOST                    ;
;
MOVUPD  BSTR,UN LOST                    ;
        LODI,R1 006H                    ;
MOVUPE  PPSL    WC+C                    ;
        LODA,R0 MCTA&pm,R1-             ;
        SUBI,R0 001H                    ;
        STRA,R0 MCTA&pm,R1              ;
        LODA,R0 MCTA&pm,R1-             ;
        ANDI,R0 03FH                    ;
        SUBI,R0 000H                    ;
        IORI,R0 0C0H                    ;
        STRA,R0 MCTA&pm,R1              ;
        LODA,R0 MCTA&pm,R1-             ;
        SUBI,R0 000H                    ;
        ANDI,R0 00FH                    ;
        STRA,R0 MCTA&pm,R1              ;
        BRNR,R1 MOVUPE                  ;
        CPSL    WC                      ;
        BCTR,UN MOVUPB                  ;
;
LOST    LODZ    R2                      ;
        IORA,R0 MCTA&pm                 ;
        WRTC,R0                         ;
        LODA,R1 *MCTA+1&pm              ;
        LODZ    R2                      ;
        IORA,R0 MCTB&pm                 ;
        WRTC,R0                         ;
        STRA,R1 *MCTB+1&pm              ;
        WRTC,R3                         ;
        RETC,UN                         ;
;
MOVDNA  LODA,R0 *CPORAC&pm              ;
        STRZ    R2                      ;
        STRZ    R3                      ;
        ANDI,R2 0F0H                    ;
MOVDNB  LODI,R1 003H                    ;
MOVDNC  LODA,R0 MCTA&pm,R1-             ;
        COMA,R0 LUAD&pm,R1              ;
        BCFR,EQ MOVDND                  ;
        BRNR,R1 MOVDNC                  ;
        BCTR,UN LOST                    ;
;
MOVDND  BSTR,UN LOST                    ;
        LODI,R1 006H                    ;
MOVDNE  SUBZ    R0                      ;
        PPSL    WC                      ;
        ADDA,R0 MCTA&pm,R1-             ;
        STRA,R0 MCTA&pm,R1              ;
        EORZ    R0                      ;
        ADDA,R0 MCTA&pm,R1-             ;
        IORI,R0 0C0H                    ;
        STRA,R0 MCTA&pm,R1              ;
        EORZ    R0                      ;
        ADDA,R0 MCTA&pm,R1-             ;
        ANDI,R0 00FH                    ;
        STRA,R0 MCTA&pm,R1              ;
        CPSL    WC                      ;
        BRNR,R1 MOVDNE                  ;
        BCTR,UN MOVDNB                  ;
;
STRCH   PPSL    RS                      ;
        STRZ    R1                      ;
        LODR,R2 *CPORAC                 ;
        LODZ    R2                      ;
        ANDI,R0 0F0H                    ;
        IORA,R0 MCTA&pm                 ;
        WRTC,R0                         ;
        STRA,R1 *MCTA+1&pm              ;
        WRTC,R2                         ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
LODCH   PPSL    RS                      ;
        LODR,R2 *CPORAC                 ;
        LODZ    R2                      ;
        ANDI,R0 0F0H                    ;
        IORA,R0 MCTA&pm                 ;
        WRTC,R0                         ;
        LODA,R0 *MCTA+1&pm              ;
        WRTC,R2                         ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
        BYTE    acon,2                  ;
CPORAC  WORD    cpores                  ;
;
;                                       == copied for printing only
PREXT   LODR,R2 PRBANK                  ;00AH,009H ;LODR,R2 508D
        WRTC,R2                         ;0B2H      ;WRTC,R2
        BSTR,UN *PRACON                 ;03BH,087H ;BSTR,UN *508E
        LODR,R2 *CPORAC                 ;00AH,0F7H ;LODR,R2 *5080
        WRTC,R2                         ;0B2H      ;WRTC,R2
        RETC,UN                         ;017H      ;RETC,UN
;
        BYTE    data,PRESTRZ-PRBANK     ;
PRBANK  BYTE    0                       ;
PRACON  WORD    srout                   ;508D acon to printer routine
PRESTRZ EQU     $
;
;       section extram                  ;external ram, ram resides in common none-banked area
        org     EXTCODE+0D0H
;                                       == text pointers, 16 x 3 bytes
txtptrs  equ     $                      ;
TVL0     ds      3                      ;  0 temp value
TVL1     ds      3                      ;  1
TVL2     ds      3                      ;  2
TVL3     ds      3                      ;  3
TVL4     ds      3                      ;  4
TVL5     ds      3                      ;  5
TVL6     ds      3                      ;  6
TVL7     ds      3                      ;  7
TVL8     ds      3                      ;  8
MDSP     ds      3                      ;  9
FBAD     ds      3                      ;  A first text buffer address
LBAD     ds      3                      ;  B last text buffer address
CLAD     ds      3                      ;  C current location
LUAD     ds      3                      ;  D last used, points to none printabe char (FF) behind text
MCTA     ds      3                      ;  E pointer A
MCTB     ds      3                      ;  F pointer B
;
TVL0B    equ     (TVL0-txtptrs)/3<<4
TVL0A    equ     (TVL0-txtptrs)/3
TVL1B    equ     (TVL1-txtptrs)/3<<4
TVL1A    equ     (TVL1-txtptrs)/3
TVL2B    equ     (TVL2-txtptrs)/3<<4
TVL2A    equ     (TVL2-txtptrs)/3
TVL3B    equ     (TVL3-txtptrs)/3<<4
TVL3A    equ     (TVL3-txtptrs)/3
TVL4B    equ     (TVL4-txtptrs)/3<<4
TVL4A    equ     (TVL4-txtptrs)/3
TVL5B    equ     (TVL5-txtptrs)/3<<4
TVL5A    equ     (TVL5-txtptrs)/3
TVL6B    equ     (TVL6-txtptrs)/3<<4
TVL6A    equ     (TVL6-txtptrs)/3
TVL7B    equ     (TVL7-txtptrs)/3<<4
TVL7A    equ     (TVL7-txtptrs)/3
TVL8B    equ     (TVL8-txtptrs)/3<<4
TVL8A    equ     (TVL8-txtptrs)/3
MDSPB    equ     (MDSP-txtptrs)/3<<4
MDSPA    equ     (MDSP-txtptrs)/3
FBADB    equ     (FBAD-txtptrs)/3<<4
FBADA    equ     (FBAD-txtptrs)/3
LBADB    equ     (LBAD-txtptrs)/3<<4
LBADA    equ     (LBAD-txtptrs)/3
CLADB    equ     (CLAD-txtptrs)/3<<4
CLADA    equ     (CLAD-txtptrs)/3
LUADB    equ     (LUAD-txtptrs)/3<<4
LUADA    equ     (LUAD-txtptrs)/3
MCTAB    equ     (MCTA-txtptrs)/3<<4
MCTAA    equ     (MCTA-txtptrs)/3
MCTBB    equ     (MCTB-txtptrs)/3<<4
MCTBA    equ     (MCTB-txtptrs)/3
;
        section edit
        ORG     04000H                  ;
;
EDITOR  BCTA,UN EDITR                   ;
;
        BYTE    titl,EDNAMEZ-EDNAME     ;
EDNAME  BYTE    "PHUNSY EDITOR v005 - 2014-04-05 - www.philipse.info"
EDNAMEZ equ     $
;
;                                       == set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
;                                         R0 ls 4 bits make RSPA pointer
;                                         R0 ms 4 bits make RSPB pointer
STRSP   BSTR,UN STRSPB
;                                       == Set RSPA only
STRSPA  PPSL    RS                      ;
        STRZ    R3                      ; save R0
        STRZ    R1                      ;
        ANDI,R1 00001111B               ;
        LODZ    R1                      ;
        ADDZ    R1                      ;
        ADDZ    R1                      ;
        ADDI,R0 LO txtptrs              ;
        STRA,R0 RSPA+1&pm               ;
        LODI,R0 HI txtptrs              ;
        STRA,R0 RSPA&pm                 ;
        LODZ    R3                      ; restore R0
        CPSL    RS                      ;
        RETC,UN                         ;
;                                       == Set RSPB only
STRSPB  PPSL    RS                      ;
        STRZ    R3                      ; save R0
        STRZ    R2                      ;
        RRR,R2                          ;
        RRR,R2                          ;
        RRR,R2                          ;
        RRR,R2                          ;
        ANDI,R2 00001111B               ;
        LODZ    R2                      ;
        ADDZ    R2                      ;
        ADDZ    R2                      ;
        ADDI,R0 LO txtptrs              ;
        STRA,R0 RSPB+1&pm               ;
        LODI,R0 HI txtptrs              ;
        STRA,R0 RSPB&pm                 ;
        LODZ    R3                      ; restore R0
        CPSL    RS                      ;
        RETC,UN                         ;
;
;                                       == move (A) to (B)
MOVRS   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
MOVRSA  PPSL    RS                      ;
        LODI,R3 3                       ;
MOVRSB  LODA,R0 *RSPA&pm,R3-            ;
        STRA,R0 *RSPB&pm,R3             ;
        BRNR,R3 MOVRSB                  ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
;                                       == compare (B) with (A)
COMRS   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
COMRSA  PPSL    RS                      ;
        LODI,R3 0FFH                    ; -1
COMRSB  COMI,R3 2                       ;
        BCTR,EQ COMRSC                  ;
        LODA,R0 *RSPA&pm,R3+            ;
        COMA,R0 *RSPB&pm,R3             ;
        BCTR,EQ COMRSB                  ;
COMRSC  CPSL    RS                      ;
        RETC,UN                         ;
;
;                                       == convert binary (A) to address (B)
BN2AD   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        PPSL    RS                      ;
        LODI,R3 2                       ;
        LODA,R0 *RSPA&pm,R3             ; ls byte
        STRA,R0 *RSPB&pm,R3             ;
        LODA,R0 *RSPA&pm,R3-            ; middle byte
        STRZ    R1                      ;
        LODA,R0 *RSPA&pm,R3-            ; ms byte
        STRZ    R2                      ;
        ANDI,R0 11111100B               ; check if value too high
        BCFR,EQ BN2ADA                  ; branch of so and set result to max
        LODZ    R1                      ;
        ANDI,R0 0C0H                    ; 
        ANDI,R2 003H                    ; ms byte 2 bits only
        RRL,R0                          ;
        RRL,R0                          ;
        RRL,R2                          ;
        RRL,R2                          ;
        IORZ    R2                      ;
        STRA,R0 *RSPB&pm,R3             ;
        LODZ    R1                      ;
        IORI,R0 0C0H                    ;
        STRA,R0 *RSPB&pm,R3+            ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
BN2ADA  LODI,R0 00FH                    ;
        STRA,R0 *RSPB&pm,R3             ;
        LODI,R0 0FFH                    ; bit 15 set too
        STRA,R0 *RSPB&pm,R3+            ;
        LODI,R0 0FFH                    ;
        STRA,R0 *RSPB&pm,R3+            ;
        RETC,UN                         ;
;
;                                       == convert address (A) to binary (B)
AD2BN   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        PPSL    RS                      ;
        LODI,R3 2                       ;
        LODA,R0 *RSPA&pm,R3             ; get low addr
        STRA,R0 *RSPB&pm,R3             ; move
        LODA,R0 *RSPA&pm,R3-            ; het high addr
        STRZ    R1                      ; to R1
        LODA,R0 *RSPA&pm,R3-            ; get bank
        RRR,R0                          ; 
        RRR,R0                          ;
        STRZ    R2                      ;
        ANDI,R0 003H                    ; leave ms bits
        STRA,R0 *RSPB&pm,R3             ; store to highest byte (2 bits)
        LODZ    R2                      ; 
        ANDI,R0 0C0H                    ; leave 2 ls bank bits
        ANDI,R1 03FH                    ; strip 2 address bits
        IORZ    R1                      ; combine
        STRA,R0 *RSPB&pm,R3+            ; store
        CPSL    RS                      ;
        RETC,UN                         ;
;
;                                       == move (A) to RSBF, clear *RSPA
TFRSP   LODI,R1 3                       ;
TFRSPA  LODA,R0 *RSPA&pm,R1-            ;
        STRA,R0 RSBF&pm,R1              ; temp 18 bit value
        BRNR,R1 TFRSPA                  ;
        EORZ    R0                      ;
        LODI,R1 3                       ;
TFRSPB  STRA,R0 *RSPB&pm,R1-            ;
        BRNR,R1 TFRSPB                  ;
        RETC,UN                         ;
;
;                                       == convert decimal (A) to binary (B)
DC2BN   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
;                                       == convert decimal (A) to binary (B), A and B not changed
DC2BNA  PPSL    RS                      ;
        BSTR,UN TFRSP                   ; move (A) to RSBF, clear *RSPA
DC2BNB  LODA,R0 RSBF&pm,R1              ;
        COMI,R0 010H                    ;
        BCTR,LT DC2BNC                  ;
        SUBI,R0 010H                    ;
        STRA,R0 RSBF&pm,R1              ;
        LODI,R2 003H                    ;
        BSTR,UN DC2BNE                  ;
        BCTR,UN DC2BNB                  ;
;
DC2BNC  LODA,R0 RSBF&pm,R1              ;
        BCTR,EQ DC2BND                  ;
        SUBI,R0 001H                    ;
        STRA,R0 RSBF&pm,R1              ;
        LODI,R2 006H                    ;
        BSTR,UN DC2BNE                  ;
        BCTR,UN DC2BNC                  ;
;
DC2BND  ADDI,R1 001H                    ;
        COMI,R1 003H                    ;
        BCFR,EQ DC2BNB                  ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
DC2BNE  LODZ    R1                      ;
        BCTR,EQ DC2BNG                  ;
DC2BNF  ADDI,R2 006H                    ;
        BDRR,R0 DC2BNF                  ;
DC2BNG  LODI,R3 003H                    ;
        PPSL    WC                      ;
        CPSL    C                       ;
DC2BNH  LODA,R0 D2BDAT&pm,R2-           ;
        ADDA,R0 *RSPB&pm,R3-            ;
        STRA,R0 *RSPB&pm,R3             ;
        BRNR,R3 DC2BNH                  ;
        CPSL    WC                      ;
        RETC,UN                         ;
;
        BYTE    data,D2BDATZ-D2BDAT     ;
D2BDAT  BYTE    001H,086H,0A0H          ;
        BYTE    000H,027H,010H          ;
        BYTE    000H,003H,0E8H          ;
        BYTE    000H,000H,064H          ;
        BYTE    000H,000H,00AH          ;
        BYTE    000H,000H,001H          ;
D2BDATZ EQU     $
;
        BYTE    data,B2DDATZ-B2DDAT     ;
B2DDAT  BYTE    052H,042H,088H          ;
        BYTE    026H,021H,044H          ;
        BYTE    013H,010H,072H          ;
        BYTE    006H,055H,036H          ;
        BYTE    003H,027H,068H          ;
        BYTE    001H,063H,084H          ;
        BYTE    000H,081H,092H          ;
        BYTE    000H,040H,096H          ;
        BYTE    000H,020H,048H          ;
        BYTE    000H,010H,024H          ;
        BYTE    000H,005H,012H          ;
        BYTE    000H,002H,056H          ;
        BYTE    000H,001H,028H          ;
        BYTE    000H,000H,064H          ;
        BYTE    000H,000H,032H          ;
        BYTE    000H,000H,016H          ;
        BYTE    000H,000H,008H          ;
        BYTE    000H,000H,004H          ;
        BYTE    000H,000H,002H          ;
        BYTE    000H,000H,001H          ;
B2DDATZ EQU     $
;
;                                       == convert (A) binary to decimal (B)
;                                       binary value consists of 20 bits in 3 bytes
;                                       decimal consist of 3 bytes = 6 nibbles (max 999999)
BN2DC   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
BN2DCA  PPSL    RS                      ;
        BSTA,UN TFRSP                   ; move (A) to RSBF, clear *RSPA
        LODA,R0 RSBF&pm                 ; het ms byte of binary value
        ANDI,R0 11110000B               ; test if highest bits are not 0, meaniing overflow
        BCFR,EQ BN2DCH                  ; branch if overflow
        LODI,R1 B2DDATZ-B2DDAT          ; index to end of table
        LODI,R2 10000000B               ; set msb to 1 for shifting and testing
BN2DCB  RRL,R2                          ; shift bit for testin binary value
        LODZ    R1                      ; test if ready
        BCTR,GT BN2DCC                  ; not ready
        CPSL    RS                      ;
        EORZ    R0                      ; indicate no overflow
        RETC,UN                         ; done
;
BN2DCC  LODI,R3 0                       ;
        ADDI,R0 12                      ;
BN2DCD  SUBI,R0 24                      ; determine which of the 3 bytes to test
        BCFR,GT BN2DCE                  ;
        ADDI,R3 1                       ;
        BCTR,UN BN2DCD                  ;
;
BN2DCE  LODA,R0 RSBF&pm,R3              ; get byte (one of 3)
        ANDZ    R2                      ; test bit
        BCFR,EQ BN2DCF                  ; bit is 0
        SUBI,R1 3                       ; point to next 3 byte value in table
        BCTR,UN BN2DCB                  ; go for next bit
;
BN2DCF  LODI,R3 3                       ; 3 bytes to add decimal
        PPSL    WC                      ;
        CPSL    C                       ;
BN2DCG  LODA,R0 B2DDAT&pm,R1-           ;
        ADDI,R0 066H                    ;
        ADDA,R0 *RSPB&pm,R3-            ; perform addition
        DAR,R0                          ;
        STRA,R0 *RSPB&pm,R3             ;
        BRNR,R3 BN2DCG                  ;
        CPSL    WC                      ;
        TPSL    c                       ;
        BCFR,EQ BN2DCB                  ; no carry = no overflow, go check if ready or more bits
BN2DCH  LODI,R3 3                       ;
        LODI,R0 099H                    ; 99, also indicate overflow
BN2DCI  STRA,R0 *RSPB&pm,R3-            ; set value to maxx : 999999
        BRNR,R3 BN2DCI                  ;
        CPSL    RS                      ;
        RETC,UN                         ; done
;
;                                       == get decimal number to (B)
GETDC   BSTA,UN STRSPB                  ; set pointer RSPA (R0 lo)
GETDCA  PPSL    RS                      ;
        EORZ    R0                      ;
        STRZ    R1                      ;
        STRZ    R2                      ;
        STRZ    R3                      ;
        CPSL    RS                      ;
GETDCB  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BCTR,EQ GETDCB                  ;
        BCTR,LT GETDCC                  ;
        SUBI,R0 "0"                     ;
        COMI,R0 9                       ;
        BCFR,GT GETDCF                  ;
GETDCC  SUBI,R3 1                       ;
        LODI,R0 080H                    ;
        RETC,UN                         ;
;
GETDCD  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BCTR,LT GETDCE                  ;
        SUBI,R0 "0"                     ;
        COMI,R0 9                       ;
        BCFR,GT GETDCF                  ;
GETDCE  SUBI,R3 1                       ;
        BSTR,UN STRRS                   ;
        EORZ    R0                      ;
        RETC,UN                         ;
;
GETDCF  PPSL    RS+WC                   ;
        CPSL    C                       ;
        ANDI,R1 00001111B               ;
        RRL,R3                          ;
        RRL,R2                          ;
        RRL,R1                          ;
        RRL,R3                          ;
        RRL,R2                          ;
        RRL,R1                          ;
        RRL,R3                          ;
        RRL,R2                          ;
        RRL,R1                          ;
        RRL,R3                          ;
        RRL,R2                          ;
        RRL,R1                          ;
        IORZ    R3                      ;
        STRZ    R3                      ;
        CPSL    RS+WC                   ;
        BCTR,UN GETDCD                  ;
;
STRRS   PPSL    RS                      ;
        LODZ    R1                      ;
        LODI,R1 0                       ;
        STRA,R0 *RSPB&pm,R1             ;
        LODZ    R2                      ;
        STRA,R0 *RSPB&pm,R1+            ;
        LODZ    R3                      ;
        STRA,R0 *RSPB&pm,R1+            ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
;                                       == write decimal number (A) to screen
WRDEC   BSTA,UN STRSPA                  ; set pointers RSPA (R0 lo)
WRDECA  EORZ    R0                      ;
        STRZ    R1                      ; 6 counter
        STRZ    R2                      ; leading zero indicator
WRDECB  LODZ    R1                      ; 
        RRR,R0                          ; divide by 2
        ANDI,R0 01111111B               ; strip msb
        LODA,R0 *RSPA&pm,R0             ; get byte
        TMI,R1  00000001B               ; high or low byte?
        BCTR,EQ WRDECC                  ;
        RRR,R0                          ;
        RRR,R0                          ;
        RRR,R0                          ;
        RRR,R0                          ;
WRDECC  ANDI,R0 00001111B               ;
        BCFR,EQ WRDECD                  ;
        TMI,R2  00000001B               ;
        BCFR,EQ WRDECE                  ;
WRDECD  IORI,R0 "0"                     ; make ascii
        ZBSR    wrtzv                   ;
        LODI,R2 00000001B               ; indicate print digits from now on
WRDECE  COMI,R1 5                       ;
        RETC,EQ                         ;
        ADDI,R1 1                       ;
        COMI,R1 5                       ;
        BCFR,EQ WRDECB                  ;
        LODI,R2 00000001B               ; print last digit always
        BCTR,UN WRDECB                  ;
;
;                                       == (A) = (A) + (B)
ADDBN   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
ADDBNA  PPSL    RS+WC                   ;
        CPSL    C                       ;
        LODI,R3 003H                    ;
ADDBNB  LODA,R0 *RSPA&pm,R3-            ;
        ADDA,R0 *RSPB&pm,R3             ;
        STRA,R0 *RSPA&pm,R3             ;
        BRNR,R3 ADDBNB                  ;
        CPSL    RS+WC                   ;
        RETC,UN                         ;
;
;                                       == (A) = (A) - (B)
SUBBN   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
SUBBNA  PPSL    RS+WC+C                 ;
        LODI,R3 3                       ;
SUBBNB  LODA,R0 *RSPA&pm,R3-            ;
        SUBA,R0 *RSPB&pm,R3             ;
        STRA,R0 *RSPA&pm,R3             ;
        BRNR,R3 SUBBNB                  ;
        CPSL    RS+WC                   ;
        RETC,UN                         ;
;
;                                       == constants
        BYTE data,CONSTZ-CONSTS         ;
CONSTS  EQU  $
CNTBEG  EQU  ($-CONSTS)/3
        BYTE TXTBBEG>>16&0FFH, TXTBBEG>>8&0FFH, TXTBBEG&0FFH
CNTEND  EQU  ($-CONSTS)/3
        BYTE TXTBEND>>16&0FFH, TXTBEND>>8&0FFH, TXTBEND&0FFH
CN0     EQU  ($-CONSTS)/3
        BYTE 0,0,0                      ; 0
CN1     EQU  ($-CONSTS)/3
        BYTE 0,0,1                      ; 1
CN10    EQU  ($-CONSTS)/3
        BYTE 0,0,10                     ; 10
CONSTZ  EQU  $
;
;                                       == (B) = (A)constant
CONST   BSTA,UN STRSPB                  ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        ANDI,R0 00001111B               ; leave constants index
        PPSL    RS                      ;
        STRZ    R2                      ;
        ADDZ    R2                      ;
        ADDZ    R2                      ;
        STRZ    R2                      ;
        ADDI,R2 3                       ; start at ls byte
        LODI,R1 3                       ; start at ls byte and 3 counter
CONSTA  LODA,R0 CONSTS&pm,R2-           ;
        STRA,R0 *RSPB&pm,R1-            ;
        BRNR,R1 CONSTA                  ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
;                                       == store ram program
SRAMP   LODI,R1 RAMPRZ-RAMPR            ;
SRAMPA  LODA,R0 RAMPR&pm,R1-            ;
        STRA,R0 *ACEC&pm,R1             ; acon ext code
        BRNR,R1 SRAMPA                  ;
        RETC,UN                         ;
;
        BYTE    acon,2                  ;
ACEC    WORD    EXTCODE                 ;acon ext code, location of code in common bank ram
;
;                                       these are the routines in ram as assembled in section extrom
        BYTE    data,RAMPRZ-RAMPR       ;
RAMPR   BYTE    00CH,09FH,0B2H,0C2H     ;
        BYTE    0C3H,046H,0F0H,005H     ;
        BYTE    003H,00DH,05FH,0FAH     ;
        BYTE    0EDH,07FH,0F4H,019H     ;
        BYTE    004H,059H,076H,01BH     ;
        BYTE    02AH,03BH,028H,005H     ;
        BYTE    006H,077H,009H,00DH     ;
        BYTE    05FH,0FAH,0A4H,001H     ;
        BYTE    0CDH,07FH,0FAH,00DH     ;
        BYTE    05FH,0FAH,044H,03FH     ;
        BYTE    0A4H,000H,064H,0C0H     ;
        BYTE    0CDH,07FH,0FAH,00DH     ;
        BYTE    05FH,0FAH,0A4H,000H     ;
        BYTE    044H,00FH,0CDH,07FH     ;
        BYTE    0FAH,059H,05EH,075H     ;
        BYTE    008H,01BH,048H,002H     ;
        BYTE    06CH,01FH,0FAH,0B0H     ;
        BYTE    00DH,09FH,0FBH,002H     ;
        BYTE    06CH,01FH,0FDH,0B0H     ;
        BYTE    0CDH,09FH,0FEH,0B3H     ;
        BYTE    017H,00CH,09FH,0B2H     ;
        BYTE    0C2H,0C3H,046H,0F0H     ;
        BYTE    005H,003H,00DH,05FH     ;
        BYTE    0FAH,0EDH,07FH,0F7H     ;
        BYTE    098H,004H,059H,076H     ;
        BYTE    01BH,059H,03BH,057H     ;
        BYTE    005H,006H,0A0H,077H     ;
        BYTE    008H,08DH,05FH,0FAH     ;
        BYTE    0CDH,07FH,0FAH,020H     ;
        BYTE    08DH,05FH,0FAH,064H     ;
        BYTE    0C0H,0CDH,07FH,0FAH     ;
        BYTE    020H,08DH,05FH,0FAH     ;
        BYTE    044H,00FH,0CDH,07FH     ;
        BYTE    0FAH,075H,008H,059H     ;
        BYTE    061H,01BH,04DH,077H     ;
        BYTE    010H,0C1H,00AH,0A2H     ;
        BYTE    002H,044H,0F0H,06CH     ;
        BYTE    01FH,0FAH,0B0H,0CDH     ;
        BYTE    09FH,0FBH,0B2H,075H     ;
        BYTE    010H,017H,077H,010H     ;
        BYTE    00AH,090H,002H,044H     ;
        BYTE    0F0H,06CH,01FH,0FAH     ;
        BYTE    0B0H,00CH,09FH,0FBH     ;
        BYTE    0B2H,075H,010H,017H     ;
        BYTE    acon,2                  ;
        WORD    CPORES                  ; copy of port C
;
;                                       == print ext routine
        BYTE    00AH,009H               ;LODR,R2 508D
        BYTE    0B2H                    ;WRTC,R2
        BYTE    03BH,087H               ;BSTR,UN *508E
        BYTE    00AH,0F7H               ;LODR,R2 *5080
        BYTE    0B2H                    ;WRTC,R2
        BYTE    017H                    ;RETC,UN
;
        BYTE    data,3                  ;
        BYTE    0                       ;bank where printer routine resides
        WORD    srout                   ;serial out to printer acon
;
RAMPRZ EQU     $
;
;                                       ; move text up for insertion of new text ???
MOVUP   LODI,R0 MCTAB!LUADA             ; 0EDH
        BSTA,UN MOVRS                   ; move (A) to (B)    MCTA=LUAD
        LODI,R0 MCTBB!LUADA             ; 0FDH
        BSTA,UN MOVRS                   ; move (A) to (B)    MCTB=LUAD
        LODI,R0 MDSPB!MCTBA             ; 09FH
        BSTR,UN ADMRS                   ; (RSPA) += (RSPB) (address values)
        LODI,R0 LBADB!MCTBA             ; 0BFH
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCTA,GT *ERRN                   ;
        LODI,R0 LUADB!MCTBA             ; 0DFH
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFA,GT *ERRN                   ;
        LODI,R0 LUADB!MCTBA             ; 0DFH
        BSTA,UN MOVRS                   ; move (A) to (B)
        PPSL    RS                      ;
        BSTA,UN MOVUPA                  ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
MOVDN   LODI,R0 MCTAB!CLADA             ; 0ECH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 MCTBB!CLADA             ; 0FCH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 MDSPB!MCTAA             ; 09EH
        BSTR,UN ADMRS                   ; (RSPA) += (RSPB) (address values)
        PPSL    RS                      ;
        BSTA,UN MOVDNA                  ;
        CPSL    RS                      ;
        LODI,R0 LUADB!MCTBA             ; 0DFH
        BSTA,UN MOVRS                   ; move (A) to (B)
        RETC,UN                         ;
;
;                                       == (RSPA) += (RSPB) (address values)
ADMRS   BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        PPSL    RS+WC                   ;
        CPSL    C                       ;
        LODI,R3 3                       ;
        LODA,R0 *RSPA&pm,R3-            ;
        ADDA,R0 *RSPB&pm,R3             ;
        STRA,R0 *RSPA&pm,R3             ;
        LODA,R0 *RSPB&pm,R3-            ;
        ANDI,R0 03FH                    ;
        ADDA,R0 *RSPA&pm,R3             ;
        IORI,R0 0C0H                    ;
        STRA,R0 *RSPA&pm,R3             ;
        LODA,R0 *RSPA&pm,R3-            ;
        ADDA,R0 *RSPB&pm,R3             ;
        ANDI,R0 00FH                    ;
        STRA,R0 *RSPA&pm,R3             ;
        CPSL    RS+WC                   ;
        RETC,UN                         ;
;
;                                       == address (RSPA)++
ADMCT   SUBZ    R0                      ; clear R0 and set carry
        PPSL    RS+WC                   ;
        LODI,R1 3                       ; 
        ADDA,R0 *RSPA&pm,R1-            ;
        STRA,R0 *RSPA&pm,R1             ;
        EORZ    R0                      ;
        ADDA,R0 *RSPA&pm,R1-            ;
        IORI,R0 0C0H                    ;
        STRA,R0 *RSPA&pm,R1             ;
        EORZ    R0                      ;
        ADDA,R0 *RSPA&pm,R1-            ;
        ANDI,R0 00001111B               ;
        STRA,R0 *RSPA&pm,R1             ;
        CPSL    RS+WC                   ;
        RETC,UN                         ;
;
;                                       == address (RSPA)--
SBMCT   PPSL    RS+WC+C                 ;
        LODI,R1 3                       ;
        LODA,R0 *RSPA&pm,R1-            ;
        SUBI,R0 1                       ;
        STRA,R0 *RSPA&pm,R1             ;
        LODA,R0 *RSPA&pm,R1-            ;
        ANDI,R0 03FH                    ;
        SUBI,R0 0                       ;
        IORI,R0 0C0H                    ;
        STRA,R0 *RSPA&pm,R1             ;
        LODA,R0 *RSPA&pm,R1-            ;
        SUBI,R0 0                       ;
        ANDI,R0 00FH                    ;
        STRA,R0 *RSPA&pm,R1             ;
        CPSL    RS+WC                   ;
        RETC,UN                         ;
;
        BYTE    acon,2                  ;
IRAC    WORD    inpram                  ; keyboard input buffer
;
;                                       == Process char from input buf (R2 index, R1 number of chars, R3-- if valid char)
PROCH   LODZ    R2                      ;
        COMZ    R1                      ;
        BCFR,EQ PROCHB                  ;
PROCHA  COMZ    R0                      ;
        PPSL    CC1                     ; set cc negative
        RETC,UN                         ;
;
PROCHB  LODA,R0 *IRAC&pm,R2+            ; keyboard input buffer
        COMI,R0 "~"                     ;
        BCTR,GT PROCHA                  ; branch if invalid character
        COMI,R0 " "                     ; space
        BCFR,LT PROCHC                  ; branch if control
        SUBI,R3 1                       ; ?
PROCHC  COMI,R0 " "                     ; cc pos if valid char
        RETC,UN                         ;
;
;                                       == 
CLEND   LODZ    R2                      ;
        STRZ    R3                      ;
CLENDA  LODZ    R3                      ;
        COMZ    R1                      ;
        RETC,EQ                         ;
        LODI,R0 " "                     ; 020H
        STRA,R0 *IRAC&pm,R3+            ; keyboard input buffer
        ZBSR    wrtzv                   ;
        BCTR,UN CLENDA                  ;
;
RTCUR   LODZ    R3                      ;
        COMZ    R2                      ;
        RETC,EQ                         ;
        LODI,R0 008H                    ; 008H
        ZBSR    wrtzv                   ;
        BDRR,R3 RTCUR                   ;
        RETC,UN                         ;
;
;                                       == go to begin of line, r2 holds # spaces
GTBOL   LODZ    R2                      ;
        RETC,EQ                         ;
GTBOLA  LODI,R0 bs                      ;
        ZBSR    wrtzv                   ;
        BDRR,R2 GTBOLA                  ;
        RETC,UN                         ;
;
;                                       == input keyboard to input buffer, R1 holds number of characters
INPUT   LODI,R2 0                       ; set input buffer index to 0
INPUTA  BSTR,UN CLEND                   ;
INPUTB  BSTR,UN RTCUR                   ;
INPUTC  STRA,R2 *IRAC&pm                ; keyboard input buffer
;
;                                       ==
INPUTD  LODA,R2 *IRAC&pm                ; keyboard input buffer
INPUTE  ZBSR    redzv                   ;
        COMI,R0 "~"                     ; ascii ?
        BCTR,GT INPUTD                  ; no 
        COMI,R0 cr                      ; cr ?
        BCFR,EQ INPUTF                  ; yes
        BSTR,UN CLEND                   ; clear rest of line
        BSTR,UN RTCUR                   ;
        LODI,R0 cr                      ;
        STRA,R0 *IRAC&pm,R2+            ; keyboard input buffer
        RETC,UN                         ;
;
INPUTF  COMI,R0 24                      ; control X, clear line
        BCFR,EQ INPUTG                  ;
        BSTR,UN GTBOL                   ;
        BCTR,UN INPUT                   ;
;
INPUTG  COMI,R0 5                       ; control E, go to begin of line
        BCFR,EQ INPUTI                  ;
        BSTR,UN GTBOL                   ;
INPUTH  BCTR,UN INPUTC                  ;
;
INPUTI  COMI,R0 6                       ; control F, skip word
        BCFR,EQ INPUTJ                  ;
PROWD   BSTA,UN PROCH                   ; Process char from input buf (R2 index, R1 number of chars, R3-- if valid char)
        BCFR,EQ PROWDB                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN PROWD                   ;
;
PROWDA  BSTA,UN PROCH                   ; Process char from input buf (R2 index, R1 number of chars, R3-- if valid char)
PROWDB  BCTR,EQ PROWDC                  ;
        BCTR,LT INPUTC                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN PROWDA                  ;
;
PROWDC  SUBI,R2 001H                    ;
        BCTR,UN INPUTH                  ;
;
INPUTJ  COMI,R0 bs                      ; control H, back space
        BCFR,EQ INPUTK                  ;
        COMI,R2 000H                    ;
        BCTA,EQ INPUTD                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN PROWDC                  ;
;
INPUTK  COMI,R0 21                      ; control U, move cursor right
        BCFR,EQ INPUTL                  ;
        BSTA,UN PROCH                   ; Process char from input buf (R2 index, R1 number of chars, R3-- if valid char)
        BCTA,LT INPUTD                  ;
        ZBSR    wrtzv                   ;
        BCTA,UN INPUTC                  ;
;
INPUTL  COMI,R0 28                      ; control \, right shift
        BCFR,EQ INPUTM                  ;
;
;                                       == right shift
RISHF   LODZ    R1                      ;
        STRZ    R3                      ;
RISHFA  LODZ    R3                      ;
        COMZ    R2                      ;
        BCTR,EQ RISHFB                  ;
        LODA,R0 *IRAC&pm,R3             ; keyboard input buffer
        STRA,R0 *IRAC&pm,R3+            ; keyboard input buffer
        SUBI,R3 002H                    ;
        BCTR,UN RISHFA                  ;
;
RISHFB  LODI,R0 020H                    ;
        STRA,R0 *IRAC&pm,R3+            ; keyboard input buffer
        BCTR,UN DISHF                   ;
;
INPUTM  COMI,R0 29                      ; control ], left shift
        BCFR,EQ INPUTN                  ;
;                                       == left shift
LESHF   LODZ    R1                      ;
        STRZ    R3                      ;
        LODI,R0 020H                    ;
        STRA,R0 *IRAC&pm,R3+            ; keyboard input buffer
        LODZ    R2                      ;
        STRZ    R3                      ;
LESHFA  LODZ    R3                      ;
        COMZ    R1                      ;
        BCTR,EQ DISHF                   ;
        ADDI,R3 002H                    ;
        LODA,R0 *IRAC&pm,R3             ; keyboard input buffer
        STRA,R0 *IRAC&pm,R3-            ; keyboard input buffer
        BCTR,UN LESHFA                  ;
;
DISHF   BSTA,UN PROCH                   ; Process char from input buf (R2 index, R1 number of chars, R3-- if valid char)
        BCTR,LT DISHFA                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN DISHF                   ;
;
DISHFA  LODZ    R2                      ;
        STRZ    R3                      ;
        LODA,R2 *IRAC&pm                ; keyboard input buffer
        BCTA,UN INPUTB                  ;
;
INPUTN  COMI,R0 " "                     ; space
        RETC,LT                         ; return if undefined control
        STRZ    R3                      ;
        LODZ    R2                      ;
        COMZ    R1                      ;
        BCTR,EQ INPUTO                  ;
        LODZ    R3                      ;
        STRA,R0 *IRAC&pm,R2+            ; keyboard input buffer
        ZBSR    wrtzv                   ;
INPUTO  BCTA,UN INPUTC                  ;
;
        byte    acon,2
MSGAC   WORD    MSGTXT
;
;                                       == print error
PRERR   LODI,R1 0                       ; 1st word
        LODI,R2 1                       ; one word
;                                       == print message
PRMES   LODR,R0 MSGAC                   ;
        STRA,R0 MESC&pm                 ;
        LODR,R0 MSGAC+1                 ;
        STRA,R0 MESC+1&pm               ;
        LODI,R3 0                       ;
PRMESA  LODZ    R1                      ;
        BCTR,EQ PRMESC                  ;
PRMESB  BSTR,UN PRMESE                  ;
        BCFR,EQ PRMESB                  ;
        SUBI,R1 1                       ;
        BCTR,UN PRMESA                  ;
;
PRMESC  BSTR,UN PRMESE                  ;
        BCTR,EQ PRMESD                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN PRMESC                  ;
;
PRMESD  ZBSR    wrtzv                   ;
        BDRR,R2 PRMESC                  ;
        RETC,UN                         ;
;
PRMESE  LODA,R0 *MESC&pm,R3             ;
        BIRR,R3 PRMESF                  ;
        LODA,R3 MESC&pm                 ;
        ADDI,R3 1                       ;
        STRA,R3 MESC&pm                 ;
        LODI,R3 0                       ;
PRMESF  COMI,R0 020H                    ;
        RETC,UN                         ;
;
        BYTE    acon,2                  ;
SRAC    WORD    upstre                  ; pointer to screen margins in monitor ram area
;
EDITR   BSTA,UN SRAMP                   ; store ram program in common bank ram area
        BSTA,UN ichcur                  ; move change cursor routine to monitor ram area
        LODI,R0 com                     ; set compares unsigned
        LPSL                            ;
        LODI,R0 0                       ;
        LPSU                            ;
        EORZ    R0                      ;
        ZBSR    wrtzv                   ; reset screen, this sets the margins to max.
;                                         set welcome text at the center of the screen
        LODI,R0 2                       ;
        LODA,R0 *SRAC&pm,R0             ; get bottom screen margin
        ADDI,R0 1                       ;
        RRR,R0                          ; divide by 2 to go to line 15
        ANDI,R0 01111111B               ;
        STRZ    R2                      ;
        LODI,R0 3                       ;
        LODA,R0 *SRAC&pm,R0             ; get right screen margin
        ADDI,R0 1                       ;
        SUBI,R0 EDNAMEZ-EDNAME          ;
        RRR,R0                          ; divide by 2
        ANDI,R0 01111111B               ;
        STRZ    R3                      ;
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        LODI,R1 0FFH                    ; -1
        LODI,R2 EDNAMEZ-EDNAME          ;
EDITRA  LODA,R0 EDNAME&pm,R1+           ;
        ZBSR    wrtzv                   ;
        BDRR,R2 EDITRA                  ;
;
BFSRT   LODI,R0 TVL0B!CNTBEG            ;
        BSTA,UN CONST                   ;
        LODI,R0 FBADB!TVL0A             ;
        BSTA,UN BN2AD                   ;
        LODI,R0 TVL0B!CNTEND            ;
        BSTA,UN CONST                   ;
        LODI,R0 LBADB!TVL0A             ;
        BSTA,UN BN2AD                   ;
;
BFSRTB  BSTA,UN CURLU                   ; clear 6 lines and set cursor upper left screen
        LODI,R1 1                       ; BUFFER START:
        LODI,R2 2                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R0 TVL0B!FBADA             ; 00AH
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        EORZ    R0                      ; TVL0
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        EORZ    R0                      ; TVL0
        BSTA,UN WRDEC                   ; write decimal number (A) to screen
        LODI,R1 " "                     ;
        LODI,R2 4                       ;
        ZBSR    wrtdx                   ; write R2 x character from R1 to output
        LODI,R1 3                       ; END:
        LODI,R2 1                       ; one word
        BSTA,UN PRMES                   ; print message
        LODI,R0 TVL0B!LBADA             ; 00BH last buffer address
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        EORZ    R0                      ; TVL0
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        EORZ    R0                      ; TVL0
        BSTA,UN WRDEC                   ; write decimal number (A) to screen
        ZBSR    wrtcr                   ;
        LODI,R1 4                       ; CHANGE? (Y/N)
        LODI,R2 2                       ;
        BSTA,UN PRMES                   ; print message
;
BFSRTC  ZBSR    redzv                   ;
        COMI,R0 "N"                     ;
        BCTA,EQ FDEND                   ;
        COMI,R0 "Y"                     ;
        BCFR,EQ BFSRTC                  ;
        ZBSR    wrtcr                   ;
BFSRTD  LODI,R1 2                       ; BUFFER START:
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R1 6                       ; 6 characters
        BSTA,UN INPUT                   ; input keyboard to input buffer, R1 holds number of characters
BFSRTE  COMI,R0 cr                      ;
        BCTR,EQ BFSRTF                  ;
        BSTA,UN INPUTD                  ;
        BCTR,UN BFSRTE                  ;
;
BFSRTF  EORZ    R0                      ;
        STRA,R0 *IRAC&pm                ; keyboard input buffer
        BSTA,UN GETDC                   ; get decimal number to (B)
        BCTR,LT BFSRTG                  ;
        EORZ    R0                      ; TVL0
        BSTA,UN DC2BN                   ; convert decimal (A) to binary (B)
        LODI,R0 FBADB!TVL0A             ; 0A0H
        BSTA,UN BN2AD                   ; convert binary (A) to address (B)
BFSRTG  ZBSR    wrtcr                   ;
        LODI,R1 3                       ; END:
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R1 6                       ; 6 chars
        BSTA,UN INPUT                   ;
BFSRTH  COMI,R0 cr                      ;
        BCTR,EQ BFSRTI                  ;
        BSTA,UN INPUTD                  ;
        BCTR,UN BFSRTH                  ;
;
BFSRTI  EORZ    R0                      ;
        STRA,R0 *IRAC&pm                ; keyboard input buffer
        BSTA,UN GETDC                   ; get decimal number to (B)
        BCTR,LT BFSRTJ                  ;
        EORZ    R0                      ; TVL0
        BSTA,UN DC2BN                   ; convert decimal (A) to binary (B)
        LODI,R0 LBADB!TVL0A             ; 0B0H
        BSTA,UN BN2AD                   ; convert binary (A) to address (B)
BFSRTJ  ZBSR    wrtcr                   ;
        LODI,R0 LBADB!FBADA             ; 0BAH
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCTA,LT BFSRTB                  ; start all over if (B) < (A)
        LODI,R1 1                       ; BUFFER
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        BSTA,UN PRERR                   ; print ERROR
        ZBSR    wrtcr                   ;
        LODI,R2 2                       ; 3rd line
        LODI,R3 0                       ; left most position
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        BCTA,UN BFSRTD                  ; start over getting buffer data from user
;
;                                       == clear 6 lines and set cursor upper left screen
curlu   lodi,r2 6                       ;
;                                       == clear R2 lines and set cursor upper left screen 
curlua  lodi,r3 0                       ;
        bsta,un chcur                   ; change cursor position, R2 vertical, R3 horizontal
        lodi,r0 cleol                   ; clear to end of line
        zbsr    wrtzv                   ;
        bdrr,r2 curlua                  ;
;                                       == clear 1st line and set cursor upper left screen
CURLUB  EORZ    R0                      ;
        STRZ    R2                      ;
        STRZ    R3                      ;
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        LODI,R0 cleol                   ; clear to end of line
        ZBRR    wrtzv                   ;
;
;                                       == find end of text in text edit area
FDEND   BSTR,UN CURLU                   ; clear 6 lines and set cursor upper left screen
        LODI,R0 CLADB!FBADA             ; 0CAH
        BSTA,UN MOVRS                   ; move (A) to (B)
;
        LODI,R0 MCTAB!FBADA             ;
        BSTA,UN MOVRS                   ; move (A) to (B)
FDENDQ  LODI,R0 TVL0B!MCTAA             ; store address of 1st char of line
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R3 mxlnln+1                ; line length max incl cr
FDENDA  LODZ    R3                      ; char counter 0?
        BCTR,EQ FDENDP                  ;
        BSTA,UN LODCH                   ; get char from text (MCTA)
        STRZ    R1                      ;
        COMI,R0 "~"                     ;
        BCTR,GT FDENDC                  ;
        COMI,R0 cr                      ;
        BCTR,EQ FDENDB                  ;
        COMI,R0 " "                     ;
        BCTR,LT FDENDC                  ;
        SUBI,R3 1                       ;
FDENDB  LODI,R0 LBADB!MCTAA             ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCTR,EQ FDENDC                  ; end reached
        BSTA,UN ADMCT                   ; address (RSPA)++
        COMI,R1 cr                      ;
        BCTR,EQ FDENDQ                  ;
        BCTR,UN FDENDA                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
;
FDENDP  LODI,R0 MCTAB!TVL0A             ;
        BSTA,UN MOVRS                   ; move (A) to (B)
;
FDENDC  LODI,R0 LUADB!MCTAA             ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 0FFH                    ; indicates end of text
        BSTA,UN STRCH                   ; store char to text (MCTA)
;
;                                       == get new user command
CMDRT   lodi,r2 4                       ; 4 lines
        bsta,un curlua                  ; clear R2 lines and set cursor upper left screen
        ZBSR    wrtcr                   ;
        LODI,R1 0FFH                    ; print commands to lines 2 and 3 (-1 mag niet blijkbaar)
        LODI,R2 CMDHALF-CMDDAT          ;
CMDRTA  LODA,R0 CMDDAT&pm,R1+           ;
        ZBSR    wrtzv                   ;
        BDRR,R2 CMDRTA                  ;
        ZBSR    wrtcr                   ; next line
        LODI,R1 CMDHALF-CMDDAT-1        ;
        LODI,R2 CMDDATE-CMDHALF         ;
CMDRTB  LODA,R0 CMDDAT&pm,R1+           ;
        ZBSR    wrtzv                   ;
        BDRR,R2 CMDRTB                  ;
        bsta,un curlub                  ; clear 1st line and set cursor upper left screen (again?)
        LODI,R1 6                       ; BYTES USED:
        LODI,R2 2                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R0 TVL1B!FBADA             ;
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        LODI,R0 TVL0B!LUADA             ;
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        LODI,R0 TVL1B!TVL0A             ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        EORZ    R0                      ;
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        BSTA,UN WRDECA                  ;
        LODI,R1 " "                     ;
        LODI,R2 4                       ;
        ZBSR    wrtdx                   ; write R2 x character from R1 to output
        LODI,R1 8                       ; LEFT:
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R0 TVL1B!LUADA             ; 01DH
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        LODI,R0 TVL0B!LBADA             ; 00BH
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        LODI,R0 TVL1B!TVL0A             ; 010H
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        EORZ    R0                      ; TVL0
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        BSTA,UN WRDECA                  ; 
        LODI,R1 " "                     ;
        LODI,R2 4                       ;
        ZBSR    wrtdx                   ; write R2 x character from R1 to output
        LODI,R1 9                       ; COMMAND:
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
CMDRTC  ZBSR    redzv                   ; Get command from user (A..Z)
        STRZ    R1                      ;
        SUBI,R1 "A"                     ;
        COMI,R1 "Z"-"A"                 ; 25
        BCTR,GT CMDRTC                  ; wait for A..Z
        STRA,R1 CMDRES&pm               ; store command letter from user
        LODI,R2 CMDDATE-CMDDAT          ; index to end of comamnd data
CMDRTE  COMA,R0 CMDDAT&pm,R2-           ; find capital letter
        BCTR,EQ CMDRTG                  ; found valid command
        BRNR,R2 CMDRTE                  ; try next if any left
        BCTR,UN CMDRTH                  ; no valid command, continue anyway
;
CMDRTF  LODA,R0 CMDDAT&pm,R2+           ; write full command word to screen
        COMI,R0 " "                     ;
        BCTR,EQ CMDRTI                  ;
CMDRTG  ZBSR    wrtzv                   ;
        BCTR,UN CMDRTF                  ;
;
CMDRTH  ZBSR    wrtzv                   ; write none command letter
CMDRTI  RRL,R1                          ; command - "A" x 2
        LODA,R0 AACN&pm,R1              ; get high address byte from table
        STRA,R0 CMDV&pm                 ; store command vector
        LODA,R0 AACN&pm,R1+             ; low address byte too
        STRA,R0 CMDV+1&pm               ;
        BSTA,UN CLRUS                   ; 
        BCTA,UN *CMDV                   ; jump to command
;
        BYTE    text,CMDDATE-CMDDAT     ;
CMDDAT  byte    "Append "
        byte    "Begin "
        byte    "Count "
        byte    "Delete "
        byte    "End "
        byte    "Find "
        byte    "Go "
        byte    "Higher "
CMDHALF EQU     $
        byte    "Insert "
        byte    "Lower "
        byte    "Modify "
        byte    "Print "
        byte    "Quit "
        byte    "Run "
        byte    "Start "
        byte    "Tape "
CMDDATE equ     $
;
        BYTE    acon,2*26               ;
AACN    WORD    APDRT                   ; A
        WORD    BEGRT                   ; B
        WORD    CNTRT                   ; C
        WORD    DELRT                   ; D
        WORD    ENDRT                   ; E
        WORD    FINRT                   ; F
        WORD    GINRT                   ; G
        WORD    HIGRT                   ; H
        WORD    GINRT                   ; I
        WORD    CMDRT                   ; J
        WORD    CMDRT                   ; K
        WORD    LOWRT                   ; L
        WORD    MODRT                   ; M
        WORD    CMDRT                   ; N
        WORD    CMDRT                   ; O
        WORD    PRTRT                   ; P
        WORD    QUITR                   ; Q
        WORD    LOTEL                   ; R
        WORD    STRTR                   ; S
        WORD    CMDRT                   ; T not implemented
        WORD    CMDRT                   ; U
        WORD    CMDRT                   ; V
        WORD    CMDRT                   ; W
        WORD    CMDRT                   ; X
        WORD    CMDRT                   ; Y
        WORD    CMDRT                   ; Z
;
QUITR   ZBSR    redzv                   ; get keyboard entry
        COMI,R0 cr                      ; cr ?
        BCFA,EQ CMDRT                   ; no, ignore command
QUITRU  EORZ    R0                      ; clear R0
        ZBSR    wrtzv                   ; clear screen
        BCTA,UN monml                   ; return to monitor
;
;                                       == Start (restart editor / change text buffer location)
STRTR   ZBSR    redzv                   ; input key from user
        COMI,R0 cr                      ; cr?
        BCFA,EQ CMDRT                   ; No, get new command
        BCTA,UN EDITOR                  ; Yes, restart editor
;
;                                       == go down R3 lines in text
;                                       returns number of lines done in R3
DOXLN   ADDI,R3 1                       ;
DOXLNA  LODI,R0 FBADB!MCTAA             ; 0AEH
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,GT DOXLNB                  ;
        BSTA,UN SBMCT                   ; address (RSPA)--
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMI,R0 cr                      ;
        BCFR,EQ DOXLNA                  ;
        BDRR,R3 DOXLNA                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        RETC,UN                         ;
;
DOXLNB  SUBI,R3 1                       ;
        RETC,UN                         ;
;
;                                       == go up R3 lines in text
UPXLNA  BSTA,UN ADMCT                   ; address (RSPA)++
UPXLN   LODI,R0 LUADB!MCTAA             ; 0DEH
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,LT UPXLNB                  ;
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMI,R0 cr                      ;
        BCFR,EQ UPXLNA                  ;
        BDRR,R3 UPXLNA                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
UPXLNB  RETC,UN                         ;
;
;                                       == increment and display number
CRDNR   LODI,R0 cr                      ;
        ZBSR    wrtzv                   ;
        LODI,R0 lf                      ;
        ZBSR    wrtzv                   ;
        LODI,R0 TVL1B!TVL0A             ;
        BSTA,UN ADDBN                   ; (A) = (A) + (B)
        LODI,R0 TVL3B!TVL0A             ;
        BSTA,UN COMRS                   ;
        BCFR,LT CRDNRA                  ;
        ZBSR    wrtsp                   ; write space first if less the 10
CRDNRA  LODI,R0 TVL2B!TVL0A             ;
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        LODI,R0 TVL2A                   ;
        BSTA,UN WRDEC                   ; write decimal number (A) to screen
        ZBSR    wrtsp                   ;
        RETC,UN                         ;
;
DISPL   LODI,R0 TVL0B!CN0               ;
        BSTA,UN CONST                   ;
        LODI,R0 TVL1B!CN1               ;
        BSTA,UN CONST                   ;
        LODI,R0 TVL3B!CN10              ;
        BSTA,UN CONST                   ;
;
        LODI,R2 6                       ;
        LODI,R3 0                       ;
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        LODI,R0 cleol                   ;
        ZBSR    wrtzv                   ;
        LODI,R3 2                       ;
        LODA,R0 *SRAC&pm,R3             ; get number of lines of screen (31)
        SUBI,R0 6                       ; skip 1st 6 lines
        RRR,R0                          ; divide by 2
        ANDI,R0 07FH                    ; strip msb
        STRZ    R3                      ; 
        BSTA,UN DOXLN                   ; go down R3 lines in text
        LODZ    R3                      ; number of lines it actualy went down
        STRA,R0 NBLRES&pm               ; save temp
        BCTR,EQ DISPLC                  ;
DISPLB  BSTA,UN CRDNR                   ; increment and display line number
        BDRR,R3 DISPLB                  ;
DISPLC  LODI,R1 2                       ;
        LODA,R0 *SRAC&pm,R1             ;
        SUBI,R0 6                       ;
        LODA,R1 NBLRES&pm               ;
        SUBZ    R1                      ;
        STRZ    R3                      ;
DISPLD  BSTA,UN CRDNR                   ; increment and display line number
DISPLE  LODI,R0 LUADB!MCTAA             ; 0DEH
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,LT DISPLG                  ;
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMI,R0 cr                      ;
        BCTR,EQ DISPLF                  ;
        ZBSR    wrtzv                   ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        BCTR,UN DISPLE                  ;
;
DISPLF  BSTA,UN ADMCT                   ; address (RSPA)++
DISPLG  BDRR,R3 DISPLD                  ;
        LODI,R0 cleol                   ;
        ZBSR    wrtzv                   ;
        BSTR,UN CEDLN                   ; calculate line number from the top of edit line in the middle R2
        LODI,R3 2                       ;
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        LODI,R0 ">"                     ;
        ZBSR    wrtzv                   ;
        RETC,UN                         ;
;
;                                       == calculate line number from the top of edit line in the middle R2
CEDLN   LODI,R2 2                       ; loenre
        LODA,R0 *SRAC&pm,R2             ;
        STRZ    R2                      ; number of lines (32-1)
        SUBI,R2 6                       ; subtract to lines to het text lines (25)
        RRR,R2                          ; divide by 2 (12)
        ANDI,R2 07FH                    ;
        ADDI,R2 7                       ; 19
        RETC,UN                         ;
;
BEGRT   ZBSR    redzv                   ;
        COMI,R0 cr                      ;
        BCFR,EQ ENDRTB                  ;
        LODI,R0 MCTAB!FBADA             ; 0EAH
        BSTA,UN MOVRS                   ; move (A) to (B)
        BCTR,UN ENDRTA                  ;
;
ENDRT   ZBSR    redzv                   ;
        COMI,R0 cr                      ;
        BCFR,EQ ENDRTB                  ;
        LODI,R0 MCTAB!LUADA             ; 0EDH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R3 1                       ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
ENDRTA  LODI,R0 CLADB!MCTAA             ; 0CEH
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
ENDRTB  BCTA,UN CMDRT                   ;
;
;                                       == input linenumber from user
ILNNR   ZBSR    wrtcr                   ;
ILNNRA  LODI,R1 10                      ; LINE NUMBER:
        LODI,R2 2                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R1 2                       ;
        BCTR,UN INPKBA                  ;
;
INRLN   ZBSR    wrtcr                   ;
        LODI,R1 12                      ; NUMBER OF LINES:
        LODI,R2 3                       ;
        BSTA,UN PRMES                   ; print message
INPKB   LODI,R1 6                       ;
INPKBA  LODI,R2 0                       ;
        BSTA,UN INPUT                   ;
INPKBB  COMI,R0 cr                      ;
        RETC,EQ                         ;
        COMI,R0 esc                     ;
        BCTR,EQ INPKBC                  ;
        BSTA,UN INPUTD                  ;
        BCTR,UN INPKBB                  ;
;
INPKBC  CPSU    SP2+SP1+SP0             ; oops :)
        BCTA,UN CMDRT                   ;
;
LOWRT   ZBSR    wrtcr                   ;
        BSTR,UN INRLN                   ;
        LODI,R0 0ECH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        EORZ    R0                      ;
        STRA,R0 *IRAC&pm                ; keyboard input buffer
        BSTA,UN GETDC                   ; get decimal number to (B)
        BCFR,LT LOWRTA                  ;
        BSTA,UN CEDLN                   ; calculate line number from the top of edit line in the middle R2
        SUBI,R0 6                       ;
        BCTR,UN LOWRTD                  ;
;
LOWRTA  BSTA,UN DC2BNA                  ; convert decimal (A) to binary (B), A and B not changed
LOWRTB  LODI,R0 010H                    ;
        LODI,R3 0FAH                    ;
        BSTA,UN STNRDA                  ;
        BSTA,UN COMRSA                  ;
        BCFR,GT LOWRTC                  ;
        BSTA,UN SUBBNA                  ;
        LODI,R3 0FAH                    ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
        BRNR,R3 LOWRTE                  ;
        BCTR,UN LOWRTB                  ;
;
LOWRTC  EORZ    R0                      ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R3 002H                    ;
        LODA,R0 *RSPA&pm,R3             ;
LOWRTD  STRZ    R3                      ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
LOWRTE  LODI,R0 0CEH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
        BCTA,UN CMDRT                   ;
;
HIGRT   ZBSR    wrtcr                   ;
        BSTA,UN INRLN                   ;
        LODI,R0 0ECH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        EORZ    R0                      ;
        STRA,R0 *IRAC&pm                ; keyboard input buffer
        BSTA,UN GETDC                   ; get decimal number to (B)
        BCFR,LT HIGRTA                  ;
        BSTA,UN CEDLN                   ; calculate line number from the top of edit line in the middle R2
        SUBI,R0 006H                    ;
        BCTR,UN HIGRTD                  ;
;
HIGRTA  BSTA,UN DC2BNA                  ; convert decimal (A) to binary (B), A and B not changed
HIGRTB  LODI,R0 010H                    ;
        LODI,R3 0FAH                    ;
        BSTA,UN STNRDA                  ;
        BSTA,UN COMRSA                  ;
        BCFR,GT HIGRTC                  ;
        BSTA,UN SUBBNA                  ;
        LODI,R3 0FAH                    ;
        BSTA,UN UPXLN                   ; go up R3 lines in text
        BRNR,R3 HIGRTE                  ;
        BCTR,UN HIGRTB                  ;
;
HIGRTC  EORZ    R0                      ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R3 002H                    ;
        LODA,R0 *RSPA&pm,R3             ;
HIGRTD  STRZ    R3                      ;
        BSTA,UN UPXLN                   ; go up R3 lines in text
HIGRTE  LODI,R0 0CEH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
        BCTA,UN CMDRT                   ;
;
        BYTE    text,AGIDATZ-AGIDAT     ;
AGIDAT  BYTE    "CONTROL:",cr,lf        ;
        BYTE    "ESC; "                 ;
        BYTE    "SHIFT LEFT, RIGHT; "   ; arrows left right
        BYTE    "BACK; "                ; ctrl H, backspace
        BYTE    "RETURN; "              ; Return
        BYTE    "FORWARD;",cr,lf        ; ctrl U
        BYTE    "DELETE; "              ; ctrl X
        BYTE    "SKIP WORD; "           ; ctrl F
        BYTE    "GO TO BEGIN; "         ; ctrl E
        BYTE    "DOWN; "                ; down arrow
        BYTE    "UP. "                  ; up arrow
AGIDATZ equ     $
;
;                                       == print BUFFER ERROR to 6th line on screen
BFULL   LODI,R2 5                       ;
        LODI,R3 0                       ;
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        LODI,R1 1                       ; BUFFER
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        BSTA,UN PRERR                   ; print ERROR
        BCTA,UN CMDRT                   ; get new command
;
;
CLRUS   LODI,R3 6                       ; 6 times carriage return
CLRUSA  ZBSR    wrtcr                   ;
        BDRR,R3 CLRUSA                  ;
        LODI,R2 1                       ; vert
        LODI,R3 0                       ; hor
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        RETC,UN                         ;
;
;---------------
;                                       == go & insert
GINRT   BSTA,UN ILNNRA                  ;
        BSTA,UN CCLAD                   ; Calculate new current location in text from user line number input
        BCTR,UN AGIRT                   ;
;
;                                       == append
APDRT   ZBSR    redzv                   ;
        COMI,R0 cr                      ;
        BCFA,EQ CMDRT                   ;
        LODI,R0 CLADB!LUADA             ; 0CDH
        BSTA,UN MOVRS                   ; move (A) to (B)
;
;                                       == append, go, insert
AGIRT   ZBSR    wrtcr                   ;
        LODI,R1 0FFH                    ;
        LODI,R2 AGIDATZ-AGIDAT          ;
AGIRTA  LODA,R0 AGIDAT&pm,R1+           ;
        ZBSR    wrtzv                   ;
        BDRR,R2 AGIRTA                  ;
        ZBSR    wrtcr                   ;
        ZBSR    wrtcr                   ;
        LODI,R0 cr                      ; 00DH
        STRA,R0 INPRS0&pm               ;
        LODI,R0 mxlnln                  ; 60
        STRA,R0 INPRS1&pm               ;
        EORZ    R0                      ;
        STRA,R0 INPRS3&pm               ;
AGIRTB  LODA,R0 CMDRES&pm               ;
        COMI,R0 "I"-"A"                 ; 8 insert?
        BCFR,EQ AGIRTC                  ;
        LODI,R3 1                       ;
        BSTA,UN STNRD                   ;
        BSTA,UN MOVUP                   ;
        LODI,R0 MCTAB!CLADA             ; 0ECH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 cr                      ;
        BSTA,UN STRCH                   ; store char to text (MCTA)
AGIRTC  LODA,R0 INPRS0&pm               ;
        COMI,R0 cr                      ;
        BCFR,EQ AGIRTH                  ;
        LODI,R0 MCTAB!CLADA             ; 0ECH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 MCTAB!MCTAA             ; 0EEH
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODA,R0 CMDRES&pm               ;
        COMI,R0 "G"-"A"                 ; 6 Go?
        BCTR,EQ AGIRTD                  ;
        LODI,R3 1                       ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
AGIRTD  LODI,R3 0                       ;
AGIRTE  BSTA,UN LODCH                   ; get char from text (MCTA)
        COMI,R0 " "                     ;
        BCFR,EQ AGIRTG                  ;
        COMI,R3 mxlnln                  ; 03CH
        BCTR,EQ AGIRTF                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        ADDI,R3 1                       ;
        BCTR,UN AGIRTE                  ;
;
AGIRTF  LODI,R3 0                       ;
AGIRTG  STRA,R3 INPRS3&pm               ;
AGIRTH  LODI,R0 MCTAB!CLADA             ; 0ECH
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
        LODI,R0 MCTAB!CLADA             ; 0ECH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 CLADB!MCTAA             ; 0CEH
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R1 0                       ;
AGIRTI  BSTA,UN LODCH                   ; get char from text (MCTA)
        COMI,R0 "~"                     ;
        BCTR,GT AGIRTJ                  ;
        COMI,R0 " "                     ;
        BCTR,LT AGIRTJ                  ;
        COMI,R1 mxlnln                  ; 03CH
        BCTR,EQ AGIRTJ                  ;
        STRA,R0 *IRAC&pm,R1+            ; keyboard input buffer
        BSTA,UN ADMCT                   ; address (RSPA)++
        BCTR,UN AGIRTI                  ;
;
AGIRTJ  LODI,R0 " "                     ;
        COMI,R1 mxlnln                  ; 03CH
        BCTR,EQ AGIRTK                  ;
        STRA,R0 *IRAC&pm,R1+            ; keyboard input buffer
        BCTR,UN AGIRTJ                  ;
;
AGIRTK  LODA,R0 INPRS0&pm               ;
        COMI,R0 etx                     ; 003H
        BCTA,EQ CMDRT                   ;
        COMI,R0 esc                     ;
        BCTA,EQ CMDRT                   ;
        LODA,R1 INPRS1&pm               ;
        LODI,R2 0                       ;
AGIRTL  COMA,R2 INPRS3&pm               ;
        BCTR,EQ AGIRTM                  ;
        BSTA,UN PROCH                   ; Process char from input buf (R2 index, R1 number of chars, R3-- if valid char)
        ZBSR    wrtzv                   ;
        BCTR,UN AGIRTL                  ;
;
AGIRTM  BSTA,UN INPUTC                  ;
        STRA,R0 INPRS0&pm               ;
        STRA,R1 INPRS1&pm               ;
        STRA,R2 INPRS3&pm               ;
        COMI,R0 esc                     ; 01BH
        BCTA,EQ AGIRTH                  ;
;
INLIN   LODI,R3 61                      ; no call to this ?
INLINA  LODA,R0 *IRAC&pm,R3-            ; keyboard input buffer
        COMI,R0 " "                     ;
        BCTR,EQ INLINB                  ;
        COMI,R0 cr                      ;
        BCFR,EQ INLINC                  ;
INLINB  COMI,R3 1                       ;
        BCFR,EQ INLINA                  ;
        LODI,R3 0                       ;
INLINC  STRA,R3 NRCHNL&pm               ;
        LODI,R0 MCTAB!CLADA             ; 0ECH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 CLADB!MCTAA             ; 0CEH
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R3 0                       ;
INLIND  BSTA,UN LODCH                   ; get char from text (MCTA)
        COMI,R0 " "                     ;
        BCTR,LT INLINE                  ;
        COMI,R0 "~"                     ;
        BCTR,GT INLINE                  ;
        COMI,R3 128                     ;
        BCTR,EQ INLINE                  ;
        ADDI,R3 1                       ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        BCTR,UN INLIND                  ;
;
STNRD   LODI,R0 MDSPB!MCTAA             ; 09EH
STNRDA  BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODZ    R3                      ;
        PPSL    RS                      ;
        STRZ    R3                      ;
        EORZ    R0                      ;
        STRZ    R1                      ;
        STRZ    R2                      ;
        BCTA,UN STRRS                   ;
;
INLINE  STRA,R3 NRCHOL&pm               ;
        LODI,R0 HI BFULL                ;
        STRA,R0 ERRN&pm                 ;
        LODI,R0 LO BFULL                ;
        STRA,R0 ERRN+1&pm               ;
        LODI,R0 0DCH                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCTR,EQ INLINJ                  ;
        SUBA,R3 NRCHNL&pm               ;
        BCTR,GT INLINF                  ;
        BCTR,EQ INLING                  ;
        EORZ    R0                      ;
        SUBZ    R3                      ;
        STRZ    R3                      ;
        BSTR,UN STNRD                   ;
        BSTA,UN MOVUP                   ;
        BCTR,UN INLING                  ;
;
INLINF  BSTR,UN STNRD                   ;
        BSTA,UN MOVDN                   ;
INLING  BSTR,UN INLINH                  ;
        BCTR,UN WHNXT                   ;
;
INLINH  LODI,R0 0ECH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 0CEH                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R2 000H                    ;
        LODA,R3 NRCHNL&pm               ;
        RETC,EQ                         ;
INLINI  LODA,R0 *IRAC&pm,R2+            ; keyboard input buffer
        BSTA,UN STRCH                   ; store char to text (MCTA)
        BSTA,UN ADMCT                   ; address (RSPA)++
        BDRR,R3 INLINI                  ;
        RETC,UN                         ;
;
INLINJ  LODA,R0 INPRS0&pm               ;
        COMI,R0 01EH                    ;
        BCTR,EQ WHNXT                   ;
        LODA,R3 NRCHNL&pm               ;
        ADDI,R3 001H                    ;
        BSTA,UN STNRD                   ;
        BSTA,UN MOVUP                   ;
        BSTR,UN INLINH                  ;
        LODI,R0 00DH                    ;
        BSTA,UN STRCH                   ; store char to text (MCTA)
;
WHNXT   LODI,R0 0ECH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R3 001H                    ;
        LODA,R2 INPRS0&pm               ;
        COMI,R2 00DH                    ;
        BCTR,EQ WHNXTA                  ;
        COMI,R2 01EH                    ;
        BCFR,EQ WHNXTB                  ;
WHNXTA  BSTA,UN UPXLN                   ; go up R3 lines in text
        BCTR,UN WHNXTC                  ;
;
WHNXTB  COMI,R2 01FH                    ;
        BCFR,EQ WHNXTD                  ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
WHNXTC  LODI,R0 0CEH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
WHNXTD  COMI,R2 00DH                    ;
        BCTA,EQ AGIRTB                  ;
        BCTA,UN AGIRTH                  ;
;
;                                       == Calculate and go to new current location in text from user line number input
CCLAD   LODI,R0 MCTAB!CLADA             ;
        BSTA,UN MOVRS                   ; Move current location CLAD to MCTA
        EORZ    R0                      ; also for GETDC in TVL0
        STRA,R0 *IRAC&pm                ; keyboard input buffer
        BSTA,UN GETDC                   ; get decimal number to (B)
        RETC,LT                         ;
        BSTA,UN DC2BN                   ; convert decimal (A) to binary (B), A and B not changed
        LODI,R3 2                       ;
        LODA,R0 *RSPB&pm,R3             ; get low byte of entered value
        STRZ    R1                      ;
        BSTA,UN CEDLN                   ; calculate line number from the top of edit line in the middle R2
        SUBI,R2 6                       ;
        LODZ    R2                      ;
        SUBZ    R1                      ;
        STRZ    R3                      ;
        RETC,EQ                         ;
        BCTR,LT CCLADA                  ;
        BSTA,UN DOXLN                   ; go down R3 lines in text (uses MCTA)
        BCTR,UN CCLADB                  ;
;
CCLADA  EORZ    R0                      ;
        SUBZ    R3                      ;
        STRZ    R3                      ;
        BSTA,UN UPXLN                   ; go up R3 lines in text
CCLADB  LODI,R0 CLADB!MCTAA             ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        RETC,UN                         ;
;
DELRT   BSTA,UN ILNNR                   ;
        BSTA,UN CCLAD                   ; Calculate new current location in text from user line number input
        ZBSR    wrtcr                   ;
        BSTA,UN INRLN                   ;
        EORZ    R0                      ;
        STRA,R0 *IRAC&pm                ; keyboard input buffer
        BSTA,UN GETDC                   ; get decimal number to (B)
        BCFR,LT DELRTE                  ;
        EORZ    R0                      ;
        LODI,R3 001H                    ;
        BSTA,UN STNRDA                  ;
        BCTR,UN DELRTF                  ;
;
DELRTE  BSTA,UN DC2BNA                  ; convert decimal (A) to binary (B), A and B not changed
DELRTF  LODI,R0 010H                    ;
        LODI,R3 001H                    ;
        BSTA,UN STNRDA                  ;
        LODI,R0 020H                    ;
        LODI,R3 000H                    ;
        BSTA,UN STNRDA                  ;
DELRTG  LODI,R0 0EEH                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R3 001H                    ;
        BSTA,UN UPXLN                   ; go up R3 lines in text
        COMI,R3 001H                    ;
        BCTR,EQ DELRTH                  ;
        LODI,R0 010H                    ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        LODI,R0 020H                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,EQ DELRTG                  ;
DELRTH  LODI,R0 09EH                    ;
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        LODI,R0 00CH                    ;
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        LODI,R0 009H                    ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        LODI,R0 099H                    ;
        BSTA,UN BN2AD                   ; convert binary (A) to address (B)
        BSTA,UN MOVDN                   ;
        LODI,R0 0ECH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
        BCTA,UN CMDRT                   ;
;
;                                       == input string data
        BYTE    data,INSDATZ-INSDAT     ;
INSDAT  EQU     $
STR0DAT EQU     $-INSDAT
        BYTE    2                       ; line number screen
        BYTE    16,1                    ; STRING:
        WORD    SBF0                    ; string buffer 0
STR1DAT EQU     $-INSDAT                ;
        BYTE    1                       ; line number screen
        BYTE    15,2                    ; OLD STRING:
        WORD    SBF1                    ; string buffer 1
STR2DAT EQU     $-INSDAT                ;
        BYTE    3                       ; line number screen
        BYTE    17,2                    ; NEW STRING:
        WORD    SBF2                    ; string buffer 2
INSDATZ equ     $
;
;                                       == input string
INSTR   STRA,R3 INSR&pm                 ; save R3
        LODA,R0 INSDAT&pm,R3            ;
        STRZ    R2                      ; line number
        LODI,R3 0                       ; 1st position
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        LODA,R3 INSR&pm                 ; restore R3
        LODA,R0 INSDAT&pm,R3+           ;
        STRZ    R1                      ;
        LODA,R0 INSDAT&pm,R3+           ;
        STRZ    R2                      ;
        LODA,R0 INSDAT&pm,R3+           ;
        STRA,R0 INSR&pm                 ;
        LODA,R0 INSDAT&pm,R3+           ;
        STRA,R0 INSR+1&pm               ;
        BSTA,UN PRMES                   ; print message
        ZBSR    wrtcr                   ;
        LODI,R3 mxlnln+1                ; 62
INSTRA  LODA,R0 *INSR&pm,R3-            ;
        STRA,R0 *IRAC&pm,R3             ; keyboard input buffer
        BRNR,R3 INSTRA                  ;
        LODA,R0 *IRAC&pm                ; keyboard input buffer
        BCTR,EQ INSTRB                  ;
        COMI,R0 mxlnln                  ; 60
        BCTR,GT INSTRB                  ;
        STRZ    R3                      ;
        LODA,R0 *IRAC&pm,R3+            ; keyboard input buffer
        COMI,R0 cr                      ;
        BCTR,EQ INSTRF                  ;
INSTRB  LODI,R1 mxlnln                  ; 60
        LODI,R2 0                       ;
        BSTA,UN INPUT                   ;
INSTRC  COMI,R0 cr                      ;
        BCFR,EQ INSTRE                  ;
        LODI,R3 mxlnln+1                ; 62
INSTRD  LODA,R0 *IRAC&pm,R3-            ; keyboard input buffer
        STRA,R0 *INSR&pm,R3             ;
        BRNR,R3 INSTRD                  ;
        RETC,UN                         ;
;
INSTRE  COMI,R0 esc                     ;
        BCFR,EQ INSTRG                  ;
        CPSU    SP2+SP1+SP0             ; oops :)
        BCTA,UN CMDRT                   ;
;
INSTRF  BSTR,UN INSTRH                  ;
INSTRG  BSTA,UN INPUTC                  ;
        BCTR,UN INSTRC                  ;
;
INSTRH  LODI,R1 mxlnln                  ; 03CH
        LODI,R2 000H                    ;
        STRA,R2 *IRAC&pm                ; keyboard input buffer
INSTRI  LODA,R0 *IRAC&pm,R2+            ; keyboard input buffer
        COMI,R0 "~"                     ;
        BCTR,GT INSTRJ                  ;
        COMI,R0 " "                     ;
        BCTR,LT INSTRJ                  ;
        COMI,R2 mxlnln                  ; 03CH
        BCTR,GT INSTRJ                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN INSTRI                  ;
;
INSTRJ  SUBI,R2 1                       ;
        BSTA,UN CLEND                   ;
        LODI,R2 0                       ;
        BSTA,UN RTCUR                   ;
        RETC,UN                         ;
;
FINRT   LODI,R3 STR0DAT                 ;
        BSTA,UN INSTR                   ;
        COMI,R2 1                       ;
        BCFA,GT CMDRT                   ;
        ZBSR    wrtcr                   ;
        ZBSR    wrtcr                   ;
        LODI,R0 MCTAB!FBADA             ; 0EAH start at begin of text
        BSTA,UN MOVRS                   ; move (A) to (B)
FINRTA  LODI,R0 LUADB!MCTAA             ; 0DEH check if end of text reached
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,LT FINRTB                  ;
        LODI,R3 0                       ;
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMA,R0 SBF0&pm,R3+             ;
        BCTR,EQ FINRTC                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        BCTR,UN FINRTA                  ;
;
FINRTB  LODI,R1 19                      ; NOT FOUND
        LODI,R2 2                       ;
        BSTA,UN PRMES                   ; print message
        BCTA,UN CMDRT                   ;
;
FINRTC  LODI,R0 TVL8B!MCTAA             ; 08EH
        BSTA,UN MOVRS                   ; move (A) to (B)
FINRTD  LODA,R0 SBF0&pm,R3+             ;
        COMI,R0 cr                      ;
        BCTR,EQ FINRTF                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMA,R0 SBF0&pm,R3              ;
        BCTR,EQ FINRTD                  ;
FINRTE  LODI,R0 MCTAB!TVL8A             ; 0E8H
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 LUADB!MCTAA             ; 0DEH
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        BSTA,UN ADMCT                   ; address (RSPA)++
        BCTA,UN FINRTA                  ;
;
FINRTF  LODI,R3 0                       ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
        LODI,R0 0CEH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
        lodi,r2 4                       ; changed
        LODI,R3 0                       ;
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        lodi,r0 cleol                   ; changed
        ZBSR    wrtzv                   ;
        LODI,R1 21                      ; CONTINUE? (Y/N)
        LODI,R2 2                       ;
        BSTA,UN PRMES                   ; print message
        ZBSR    redzv                   ;
        STRZ    R1                      ;
        COMI,R1 "N"                     ;
        BCTA,EQ CMDRT                   ;
        BCTA,UN FINRTE                  ;
;
;                                       == Modify
MODRT   LODI,R3 STR1DAT                 ;
        BSTA,UN INSTR                   ;
        COMI,R2 1                       ;
        BCFA,GT CMDRT                   ;
        LODI,R3 STR2DAT                 ;
        BSTA,UN INSTR                   ;
        ZBSR    wrtcr                   ;
        LODI,R0 TVL7B!CLADA             ; 07CH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 TVL0B!CN0               ; clear modified counter
        BSTA,UN CONST                   ;
        LODI,R0 TVL1B!CN0               ; clear skipped counter
        BSTA,UN CONST                   ;
        LODI,R0 TVL2B!CN1               ; counter increment 1
        BSTA,UN CONST                   ;
        LODI,R0 MCTAB!FBADA             ; 0EAH
        BSTA,UN MOVRS                   ; move (A) to (B)
MODRTB  LODI,R0 LUADB!MCTAA             ; 0DEH
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,LT MODRTD                  ;
        LODI,R3 0                       ;
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMA,R0 SBF1&pm,R3+             ;
        BCTR,EQ MODRTE                  ;
MODRTC  BSTA,UN ADMCT                   ; address (RSPA)++
        BCTR,UN MODRTB                  ;
;
MODRTD  LODI,R0 CLADB!TVL7A             ; 0C7H
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R1 23                      ; MODIFIED:
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R0 TVL0B!TVL0A             ;
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        BSTA,UN WRDECA                  ;
        LODI,R1 " "                     ;
        LODI,R2 04                      ;
        ZBSR    wrtdx                   ; write R2 x character from R1 to output
        LODI,R1 24                      ; SKIPPED:
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        LODI,R0 TVL1B!TVL1A             ; 011H
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        BSTA,UN WRDECA                  ;
        LODI,R0 MCTAB!CLADA             ; 0ECH
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
        BCTA,UN CMDRT                   ;
;
MODRTE  LODI,R0 08EH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
MODRTF  LODA,R0 SBF1&pm,R3+             ;
        COMI,R0 00DH                    ;
        BCTR,EQ MODRTH                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMA,R0 SBF1&pm,R3              ;
        BCTR,EQ MODRTF                  ;
MODRTG  LODI,R0 0E8H                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 0DEH                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        BCTA,UN MODRTC                  ;
;
MODRTH  LODI,R0 0C8H                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R3 000H                    ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
        LODA,R3 SBF1&pm                 ;
        SUBA,R3 SBF2&pm                 ;
        BCTA,LT MODRTL                  ;
        BCTR,EQ MODRTI                  ;
        BSTA,UN STNRD                   ;
        BSTA,UN MOVDN                   ;
        LODI,R0 087H                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,GT MODRTI                  ;
        LODI,R0 077H                    ;
        BSTA,UN AD2BN                   ; convert address (A) to binary (B)
        LODI,R0 097H                    ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        LODI,R0 077H                    ;
        BSTA,UN BN2AD                   ; convert binary (A) to address (B)
MODRTI  LODI,R0 0ECH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 0CEH                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R1 000H                    ;
MODRTJ  LODA,R0 SBF2&pm,R1+             ;
        COMI,R0 00DH                    ;
        BCTR,EQ MODRTK                  ;
        BSTA,UN STRCH                   ; store char to text (MCTA)
        BSTA,UN ADMCT                   ; address (RSPA)++
        BCTR,UN MODRTJ                  ;
;
MODRTK  LODI,R0 020H                    ;
        BSTA,UN ADDBN                   ; (A) = (A) + (B)
        BCTA,UN MODRTB                  ;
;
MODRTL  EORZ    R0                      ;
        SUBZ    R3                      ;
        STRZ    R3                      ;
        BSTA,UN STNRD                   ;
        LODI,R0 0EEH                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values

        LODI,R1 000H                    ;
MODRTM  BSTA,UN LODCH                   ; get char from text (MCTA)
        COMI,R0 07EH                    ;
        BCTR,GT MODRTN                  ;
        COMI,R0 020H                    ;
        BCTR,LT MODRTN                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        ADDI,R1 001H                    ;
        BCTR,UN MODRTM                  ;
;
MODRTN  LODZ    R1                      ;
        ADDZ    R3                      ;
        COMI,R0 03CH                    ;
        BCTR,GT MODRTP                  ;
        LODI,R0 HI MODRTP               ;
        STRA,R0 ERRN&pm                 ;
        LODI,R0 LO MODRTP               ;
        STRA,R0 ERRN+1&pm               ;
        BSTA,UN MOVUP                   ;
        LODI,R0 087H                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,GT MODRTO                  ;
        LODI,R0 097H                    ;
        BSTA,UN ADMRS                   ; (RSPA) += (RSPB) (address values)
MODRTO  BCTA,UN MODRTI                  ;
;
MODRTP  LODI,R0 021H                    ;
        BSTA,UN ADDBN                   ; (A) = (A) + (B)
        BCTA,UN MODRTG                  ;
;
CNTRT   LODI,R3 STR0DAT                 ;
        BSTA,UN INSTR                   ;
        COMI,R2 1                       ;
        BSFA,GT CNTRTI                  ;
        ZBSR    wrtcr                   ;
        ZBSR    wrtcr                   ;
        EORZ    R0                      ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODI,R0 001H                    ;
        LODI,R1 006H                    ;
CNTRTA  STRA,R0 *RSPA&pm,R1-            ;
        EORZ    R0                      ;
        BRNR,R1 CNTRTA                  ;
        LODI,R0 0EAH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
CNTRTB  LODI,R0 0DEH                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,LT CNTRTD                  ;
        LODI,R3 000H                    ;
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMA,R0 SBF0&pm,R3+             ;
        BCTR,EQ CNTRTE                  ;
CNTRTC  BSTA,UN ADMCT                   ; address (RSPA)++
        BCTR,UN CNTRTB                  ;
;
CNTRTD  LODI,R1 25                      ; COUNTED:
        LODI,R2 1                       ;
        BSTA,UN PRMES                   ; print message
        EORZ    R0                      ;
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        BSTA,UN WRDECA                  ;
        BCTA,UN CMDRT                   ;
;
CNTRTE  LODI,R0 08EH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
CNTRTF  LODA,R0 SBF0&pm,R3+             ;
        COMI,R0 00DH                    ;
        BCTR,EQ CNTRTH                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        BSTA,UN LODCH                   ; get char from text (MCTA)
        COMA,R0 SBF0&pm,R3              ;
        BCTR,EQ CNTRTF                  ;
        LODI,R0 0E8H                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
CNTRTG  LODI,R0 0DEH                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        BCTR,UN CNTRTC                  ;
;
CNTRTH  LODI,R0 010H                    ;
        BSTA,UN ADDBN                   ; (A) = (A) + (B)
        BCTR,UN CNTRTG                  ;
;
CNTRTI  LODI,R0 00DH                    ;
        LODI,R3 002H                    ;
        STRA,R0 SBF0&pm,R3              ;
        RETC,UN                         ;
;
PRTRT   BSTA,UN ILNNR                   ;
        BSTA,UN CCLAD                   ; Calculate new current location in text from user line number input
        ZBSR    wrtcr                   ;
        BSTA,UN INRLN                   ;
        EORZ    R0                      ;
        STRA,R0 *IRAC&pm                ; keyboard input buffer
        LODI,R0 040H                    ;
        BSTA,UN GETDC                   ; get decimal number to (B)
        BCTA,LT CMDRT                   ;
        BSTA,UN DISPL                   ;
        LODI,R0 044H                    ;
        BSTA,UN DC2BN                   ; convert decimal (A) to binary (B)
        LODI,R0 050H                    ;
        LODI,R3 001H                    ;
        BSTA,UN STNRDA                  ;
        LODI,R0 030H                    ;
        LODI,R3 000H                    ;
        BSTA,UN STNRDA                  ;
        LODI,R0 0ECH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
PRTRTB  LODI,R0 0DEH                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,LT PRTRTD                  ;
        BSTA,UN LODCH                   ; get char from text (MCTA)
        STRZ    R1                      ;
        WRTE,R0 127                     ; added for emulator output
        BSTA,UN PREXT                   ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        COMI,R1 00DH                    ;
        BCFR,EQ PRTRTB                  ;
        LODI,R0 00AH                    ;
        WRTE,R0 127                     ; added for emulator output
        BSTA,UN PREXT                   ;
        LODI,R0 0CEH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        ZBSR    chizv                   ;
        COMI,R0 07FH                    ;
        BCTR,EQ PRTRTC                  ;
        ZBSR    redzv                   ;
        COMI,R0 01BH                    ;
        BCTR,EQ PRTRTD                  ;
PRTRTC  LODI,R0 054H                    ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        LODI,R0 034H                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCFR,EQ PRTRTB                  ;
PRTRTD  LODI,R0 0CEH                    ;
        BSTA,UN MOVRS                   ; move (A) to (B)
        BSTA,UN DISPL                   ;
        BCTA,UN CMDRT                   ;
;
;                                       == is this used?
STEXT   COMI,R0 cr                      ;
        BCTR,EQ STEXTA                  ;
        COMI,R0 " "                     ;
        RETC,LT                         ;
        COMI,R0 "~"                     ;
        RETC,GT                         ;
STEXTA  STRA,R0 TXTRES&pm               ;
        LODI,R0 LBADB!LUADA             ; 0BDH
        BSTA,UN COMRS                   ; compare (B) with (A)
        RETC,EQ                         ;
        RETC,GT                         ;
        LODI,R0 MCTAB!LUADA             ; 0EDH
        BSTA,UN MOVRS                   ; move (A) to (B)
        LODI,R0 LUADB!MCTAA             ; 0DEH
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        LODA,R0 TXTRES&pm               ;
        BSTA,UN STRCH                   ; store char to text (MCTA)
        BSTA,UN ADMCT                   ; address (RSPA)++
        LODI,R0 0FFH                    ; end indication
        BSTA,UN STRCH                   ;  store char to text (MCTA)
        BCTA,UN MOVRSA                  ; move pointers ...
;
        BYTE    text,MSGTXTZ-MSGTXT
MSGTXT  BYTE    "ERROR "                ;  0
        BYTE    "BUFFER "               ;  1
        BYTE    "START: "               ;  2
        BYTE    "END: "                 ;  3
        BYTE    "CHANGE? (Y/N) "        ;  4
        BYTE    "BYTES USED: "          ;  6
        BYTE    "LEFT: "                ;  8
        BYTE    "COMMAND: "             ;  9
        BYTE    "LINE NUMBER: "         ; 10
        BYTE    "NUMBER OF LINES: "     ; 12
        BYTE    "OLD STRING: "          ; 15
        BYTE    "NEW STRING: "          ; 17
        BYTE    "NOT FOUND "            ; 19
        BYTE    "CONTINUE? (Y/N) "      ; 21
        BYTE    "MODIFIED: "            ; 23
        BYTE    "SKIPPED: "             ; 24
        BYTE    "COUNTED: "             ; 25
MSGTXTZ EQU     $
;
;                                       == change cursor position, R2 vertical position, R3 horizontal position
chcur   bctr,un *mfacon
        byte    acon,2
mfacon  word    monfree                 ;pointer to free monitor memory space
;
ichcur  lodr,r1 chcurx-1
ichcura loda,r0 chcurx&pm,r1-
        stra,r0 *mfacon&pm,r1
        brnr,r1 ichcura
        retc,un
;
        byte    data,chcurxe-chcurx
chcurx  byte    00CH,00EH,0EFH          ;loda,r0 curres
        byte    0CCH,08EH,0F4H          ;stra,r0 *curs
        byte    003H                    ;lodz    r3
        byte    077H,010H               ;ppsl    rs
        byte    03FH,006H,0B5H          ;bsta,un stcrh
        byte    075H,010H               ;cpsl    rs
        byte    002H                    ;lodz    r2
        byte    077H,010H               ;ppsl    rs
        byte    03FH,006H,0DEH          ;bsta,un stcrv
        byte    01FH,006H,042H          ;bcta,un wcharg
chcurxe equ     $
;
; LOTEL - logical test language
; 2014-04-05
; don't remember how this works. It was used as a logical test language. It could test simple electronic printed circuit boards
; it was possible to output digital signals and check responses
; on errors a kind of checksum was build that could indicate a specific error on the electronic circuit
;
;       org     05400H
LOTEL   BCTA,UN LOTELA                  ;
;
        BYTE    titl,LTEXTZ-LTEXT       ;
LTEXT   byte    "PHUNSY LOTEL v001 - J.F.P. PHILIPSE - 1981-06-11"
LTEXTZ  EQU     $
;
        BYTE    text,LTLCDTE-LTLCDT     ; 176 bytes long
;
LTLCDT  BYTE    "TMIN",    080H+0
        BYTE    "TMAX",    080H+3
        BYTE    "SET",     080H+6
        BYTE    "CLEAR",   080H+9
        BYTE    "INCOND",  080H+12
        BYTE    "TESTOUT", 080H+15
        BYTE    "TESTIN",  080H+18
        BYTE    "DISPOUT", 080H+21
        BYTE    "DISPIN",  080H+24
        BYTE    "DISPTIME",080H+27
        BYTE    "PULSE",   080H+30
        BYTE    "INPUTS",  080H+33
        BYTE    "OUTPUTS", 080H+36
        BYTE    "END",     080H+39
        BYTE    "DISPRST", 080H+42
        BYTE    "ERRCODE", 080H+45
        BYTE    "REM",     080H+48
        BYTE    "PRINT",   080H+51
        BYTE    "DELAY",   080H+54
        BYTE    "UPDATE",  080H+57
        BYTE    " "
LTLCDTE equ     $
;
LTLBR   BCTA,UN TMINR                   ;
        BCTA,UN TMAXR                   ;
        BCTA,UN SETRT                   ;
        BCTA,UN CLRRT                   ;
        BCTA,UN INCND                   ;
        BCTA,UN TESTO                   ;
        BCTA,UN TESTI                   ;
        BCTA,UN DSPOT                   ;
        BCTA,UN DSPIN                   ;
        BCTA,UN DSPTM                   ;
        BCTA,UN PULSR                   ;
        BCTA,UN SINPS                   ;
        BCTA,UN SOUTS                   ;
        BCTA,UN CMDRT                   ;
        BCTA,UN DSPRS                   ;
        BCTA,UN CODER                   ;
        BCTA,UN REMRT                   ;
        BCTA,UN PRINT                   ;
        BCTA,UN DELAY                   ;
        BCTA,UN UPDTR                   ;
;
LOTELA  ZBSR    redzv                   ;
        COMI,R0 00DH                    ;
        BCFA,EQ CMDRT                   ;
        LODI,R2 007H                    ;
        LODI,R3 000H                    ;
        BSTA,UN chcur                   ; change cursor position, R2 vertical, R3 horizontal
        LODI,R0 006H                    ;
        ZBSR    wrtzv                   ;
        EORZ    R0                      ;
        STRZ    R1                      ;
LOTELB  STRA,R0 RSLT&pm,R1              ;
        BIRR,R1 LOTELB                  ;
        LODI,R0 0EAH                    ;
        BSTA,UN MOVRS                   ;
LOTELC  ZBSR    chizv                   ;
        COMI,R0 07FH                    ;
        BCTR,EQ LOTELD                  ;
        ZBSR    redzv                   ;
        COMI,R0 01BH                    ;
        BCTA,EQ CMDRT                   ;
LOTELD  LODI,R0 0DEH                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCTA,GT TXERR                   ;
        LODI,R2 000H                    ;
        LODI,R3 03CH                    ;
LOTELE  BSTA,UN LODCH                   ; get char from text (MCTA)
        BCTA,LT TXERR                   ;
        STRA,R0 *IRAC&pm,R2+            ; keyboard input buffer
        COMI,R0 00DH                    ;
        BCTR,EQ LOTELF                  ;
        BSTA,UN ADMCT                   ; address (RSPA)++
        BDRR,R3 LOTELE                  ;
        BCTA,UN TXERR                   ;
;
LOTELF  BSTA,UN ADMCT                   ; address (RSPA)++
        NOP                             ;
        NOP                             ;
        NOP                             ;
        EORZ    R0                      ;
        STRA,R0 *IRAC&pm                ; keyboard input buffer
LOTELG  LODA,R1 *IRAC&pm                ; keyboard input buffer
        LODI,R2 0FFH                    ;
LOTELH  STRA,R1 *IRAC&pm                ; keyboard input buffer
LOTELI  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BCTR,EQ LOTELI                  ;
        BCTR,UN LOTELK                  ;
;
LOTELJ  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
LOTELK  COMI,R0 041H                    ;
        BCTA,LT TXERR                   ;
        COMI,R0 05AH                    ;
        BCTA,GT TXERR                   ;
        COMA,R0 LTLCDT&pm,R2+           ;
        BCFR,EQ LOTELL                  ;
        LODA,R0 LTLCDT&pm,R2+           ;
        BCTR,LT LOTELM                  ;
        SUBI,R2 001H                    ;
        BCTR,UN LOTELJ                  ;
;
LOTELL  LODA,R0 LTLCDT&pm,R2+           ;
        BCTR,LT LOTELH                  ;
        COMI,R0 021H                    ;
        BCTA,LT TXERR                   ;
        BCTR,UN LOTELL                  ;
;
LOTELM  ANDI,R0 07FH                    ;
        STRZ    R3                      ;
        BXA     LTLBR,R3                ;
;
PRINT   BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        COMI,R0 03AH                    ; :
        BCFR,EQ PRINTA                  ;
        ZBSR    wrtcr                   ;
        BCTA,UN LOTELG                  ;
;
PRINTA  COMI,R0 00DH                    ;
        BCFR,EQ PRINTB                  ;
        ZBSR    wrtcr                   ;
        BCTA,UN LOTELC                  ;
;
PRINTB  ZBSR    wrtzv                   ;
        BCTR,UN PRINT                   ;
;
REMRT   BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        COMI,R0 03AH                    ;
        BCTA,EQ LOTELG                  ;
        COMI,R0 00DH                    ;
        BCTA,EQ LOTELC                  ;
        BCTR,UN REMRT                   ;
;
CMRET   BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BCTR,EQ CMRET                   ;
        COMI,R0 03AH                    ;
        BCTA,EQ LOTELG                  ;
        COMI,R0 00DH                    ;
        BCTA,EQ LOTELC                  ;
TXERR   ZBSR    wrtcr                   ;
        BSTA,UN PRERR                   ; print ERROR
        ZBSR    wrtcr                   ;
        LODI,R3 001H                    ;
        BSTA,UN DOXLN                   ; go down R3 lines in text
        LODI,R0 0CEH                    ;
        BSTA,UN MOVRS                   ;
        BSTR,UN WRLIN                   ;
        LODA,R1 *IRAC&pm                ; keyboard input buffer
        LODZ    R1                      ;
        BCTR,EQ TXERRB                  ;
TXERRA  SUBI,R1 001H                    ;
        BCTR,EQ TXERRB                  ;
        ZBSR    wrtsp                   ;
        BCTR,UN TXERRA                  ;
;
TXERRB  LODI,R0 05EH                    ;
        ZBSR    wrtzv                   ;
        ZBSR    wrtcr                   ;
        BCTA,UN CMDRT                   ;
;
WRLIN   LODI,R1 000H                    ;
WRLINA  LODA,R0 *IRAC&pm,R1+            ; keyboard input buffer
        COMI,R0 020H                    ;
        BCTR,LT WRLINB                  ;
        COMI,R0 07EH                    ;
        BCTR,GT WRLINB                  ;
        COMI,R1 03DH                    ;
        BCFR,LT WRLINB                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN WRLINA                  ;
;
WRLINB  ZBRR    wrtcr                   ;
;
OTPRT   PPSL    RS                      ;
        LODI,R0 0D4H                    ;
        STRA,R0 WRERES&pm               ;
        LODI,R0 017H                    ;
        STRA,R0 RETRES&pm               ;
        LODI,R1 008H                    ;
OTPRTA  LODA,R0 OTPR&pm,R1-             ;
        STRA,R1 EPTRES&pm               ;
        BSTA,UN WRERES                  ;
        LODZ    R1                      ;
        BCFR,EQ OTPRTA                  ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
INRTN   PPSL    RS                      ;
        LODI,R0 054H                    ;
        STRA,R0 WRERES&pm               ;
        LODI,R0 017H                    ;
        STRA,R0 RETRES&pm               ;
        LODI,R1 007H                    ;
INRTNA  STRA,R1 EPTRES&pm               ;
        BSTA,UN WRERES                  ;
        STRA,R0 INPR&pm,R1              ;
        SUBI,R1 001H                    ;
        BCFR,LT INRTNA                  ;
        CPSL    RS                      ;
        RETC,UN                         ;
;
SINPS   LODI,R2 0FFH                    ;
        BCTR,UN SIOPS                   ;
;
SOUTS   LODI,R2 001H                    ;
SIOPS   EORZ    R0                      ;
        BSTA,UN GETDC                   ; get decimal number to (B)
        STRA,R3 *IRAC&pm                ; keyboard input buffer
        BCTA,LT TXERR                   ;
        BSTA,UN DC2BNA                  ; convert decimal (A) to binary (B), A and B not changed
        LODI,R1 0FFH                    ;
        LODA,R0 *RSPB&pm,R1+            ;
        IORA,R0 *RSPB&pm,R1+            ;
        BCFA,EQ TXERR                   ;
        LODA,R0 *RSPB&pm,R1+            ;
        BCTA,EQ TXERR                   ;
        COMI,R0 040H                    ;
        BCTA,GT TXERR                   ;
        STRA,R0 NBITIN&pm,R2+           ;
        STRZ    R1                      ;
        RRR,R0                          ;
        RRR,R0                          ;
        RRR,R0                          ;
        ANDI,R0 00FH                    ;
        ANDI,R1 00FH                    ;
        BCTR,EQ SIOPSA                  ;
        ADDI,R0 001H                    ;
SIOPSA  STRA,R0 NBITIN&pm,R2+           ;
        BCTA,UN CMRET                   ;
;
TMINR   LODI,R0 044H                    ;
        BCTR,UN TIMSR                   ;
;
TMAXR   LODI,R0 055H                    ;
TIMSR   BSTA,UN GETDC                   ; get decimal number to (B)
        STRA,R3 *IRAC&pm                ; keyboard input buffer
        BCTA,LT TXERR                   ;
        BSTA,UN DC2BNA                  ; convert decimal (A) to binary (B), A and B not changed
        BCTA,UN CMRET                   ;
;
GTIOS   EORZ    R0                      ;
        LODI,R3 008H                    ;
GTIOSA  STRA,R0 SLCT&pm,R3-             ;
        BRNR,R3 GTIOSA                  ;
GTIOSB  EORZ    R0                      ;
        BSTA,UN GETDC                   ; get decimal number to (B)
        STRA,R3 *IRAC&pm                ; keyboard input buffer
        RETC,LT                         ;
        BSTA,UN DC2BNA                  ; convert decimal (A) to binary (B), A and B not changed
        LODI,R3 000H                    ;
        LODA,R0 *RSPB&pm,R3             ;
        IORA,R0 *RSPB&pm,R3+            ;
        BCTR,EQ GTIOSC                  ;
        EORZ    R0                      ;
        RETC,UN                         ;
;
GTIOSC  LODA,R0 *RSPB&pm,R3+            ;
        RETC,EQ                         ;
        COMA,R0 NBITIN&pm,R1            ;
        RETC,GT                         ;
        SUBI,R0 001H                    ;
        STRZ    R2                      ;
        LODI,R3 000H                    ;
GTIOSD  COMI,R2 008H                    ;
        BCTR,LT GTIOSE                  ;
        ADDI,R3 001H                    ;
        SUBI,R2 008H                    ;
        BCTR,UN GTIOSD                  ;
;
GTIOSE  LODI,R0 001H                    ;
GTIOSF  COMI,R2 000H                    ;
        BCTR,EQ GTIOSG                  ;
        RRL,R0                          ;
        SUBI,R2 001H                    ;
        BCTR,UN GTIOSF                  ;
;
GTIOSG  IORA,R0 SLCT&pm,R3              ;
        STRA,R0 SLCT&pm,R3              ;
        BCTA,UN GTIOSB                  ;
;
CLRRT   EORZ    R0                      ;
        BCTR,UN CLSRT                   ;
;
SETRT   LODI,R0 0FFH                    ;
CLSRT   LODI,R3 008H                    ;
CLSRTA  STRA,R0 COND&pm,R3-             ;
        BRNR,R3 CLSRTA                  ;
        LODI,R1 002H                    ;
        BSTA,UN GTIOS                   ;
        BCFA,LT TXERR                   ;
        LODI,R3 008H                    ;
CLSRTB  LODA,R0 SLCT&pm,R3-             ;
        BCFR,EQ CLSRTD                  ;
        BRNR,R3 CLSRTB                  ;
        LODI,R3 008H                    ;
        LODI,R0 0FFH                    ;
CLSRTC  STRA,R0 SLCT&pm,R3-             ;
        BRNR,R3 CLSRTC                  ;
CLSRTD  LODA,R3 NBYTOT&pm               ;
        BCTA,EQ TXERR                   ;
CLSRTE  LODI,R0 0FFH                    ;
        EORA,R0 SLCT&pm,R3-             ;
        ANDA,R0 OTPR&pm,R3              ;
        STRZ    R2                      ;
        LODA,R0 SLCT&pm,R3              ;
        ANDA,R0 COND&pm,R3              ;
        IORZ    R2                      ;
        STRA,R0 OTPR&pm,R3              ;
        BRNR,R3 CLSRTE                  ;
        BSTA,UN OTPRT                   ;
        BCTR,UN DELAYA                  ;
;
DELAY   EORZ    R0                      ;
        BSTA,UN GETDC                   ; get decimal number to (B)
        STRA,R3 *IRAC&pm                ; keyboard input buffer
        BCTA,LT TXERR                   ;
        BSTA,UN DC2BNA                  ; convert decimal (A) to binary (B), A and B not changed
        BSTR,UN MINDY                   ;
DELAYA  BCTA,UN CMRET                   ;
;
GTRES   LODI,R1 003H                    ;
        LODA,R0 *RSPA&pm,R1-            ;
        STRZ    R3                      ;
        LODA,R0 *RSPA&pm,R1-            ;
        STRZ    R2                      ;
        LODA,R0 *RSPA&pm,R1-            ;
        STRZ    R1                      ;
        RETC,UN                         ;
;
MINDY   BSTR,UN GTRES                   ;
MINDYA  LODZ    R1                      ;
        IORZ    R2                      ;
        IORZ    R3                      ;
        RETC,EQ                         ;
        PPSL    WC                      ;
        CPSL    C                       ;
        SUBI,R3 000H                    ;
        SUBI,R2 000H                    ;
        SUBI,R1 000H                    ;
        CPSL    WC                      ;
        LODI,R0 064H                    ;
MINDYB  BDRR,R0 MINDYB                  ;
        LODZ    R3                      ;
        LODZ    R3                      ;
        BCFR,EQ MINDYA                  ;
        LODI,R0 00FH                    ;
MINDYC  BDRR,R0 MINDYC                  ;
        BCTR,UN MINDYA                  ;
;
INCND   LODI,R1 000H                    ;
        BSTA,UN GTIOS                   ;
        BCFA,LT TXERR                   ;
        BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        COMI,R0 02CH                    ;
        BCFA,EQ TXERR                   ;
        LODA,R0 NBYTIN&pm               ;
        BCTA,EQ TXERR                   ;
        EORZ    R0                      ;
        LODI,R1 008H                    ;
INCNDA  STRA,R0 COND&pm,R1-             ;
        BRNR,R1 INCNDA                  ;
        LODI,R2 001H                    ;
INCNDB  LODZ    R2                      ;
        ANDA,R0 SLCT&pm,R1              ;
        BCTR,EQ INCNDD                  ;
INCNDC  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BCTR,EQ INCNDC                  ;
        STRZ    R3                      ;
        ANDI,R3 0FEH                    ;
        COMI,R3 030H                    ;
        BCFA,EQ TXERR                   ;
        TMI,R0  001H                    ;
        BCFR,EQ INCNDD                  ;
        LODZ    R2                      ;
        IORA,R0 COND&pm,R1              ;
        STRA,R0 COND&pm,R1              ;
INCNDD  RRL,R2                          ;
        TMI,R2  001H                    ;
        BCFR,EQ INCNDB                  ;
        ADDI,R1 001H                    ;
        COMA,R1 NBYTIN&pm               ;
        BCFR,EQ INCNDB                  ;
        LODI,R0 054H                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCTA,GT TXERR                   ;
        BSTA,UN MINDY                   ;
        LODI,R0 035H                    ;
        BSTA,UN MOVRS                   ;
        LODI,R0 043H                    ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        LODI,R0 033H                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        PPSL    RS                      ;
        BSTA,UN GTRES                   ;
        CPSL    RS                      ;
        LODI,R0 054H                    ; REDE,R0
        STRA,R0 WRERES&pm               ; write extended
        LODI,R0 017H                    ; RETC,UN
        STRA,R0 RETRES&pm               ; return location
INCNDE  LODI,R1 000H                    ;
        LODI,R2 007H                    ;
INCNDF  STRA,R2 EPTRES&pm               ; extended port
        BSTA,UN WRERES                  ; write extended
        STRA,R0 INPR&pm,R2              ;
        ANDA,R0 SLCT&pm,R2              ;
        EORA,R0 COND&pm,R2              ;
        IORZ    R1                      ;
        STRZ    R1                      ;
        SUBI,R2 001H                    ;
        BCFR,LT INCNDF                  ;
        IORZ    R0                      ;
        BCTR,EQ INCNDI                  ;
        PPSL    RS                      ;
        LODZ    R1                      ;
        IORZ    R2                      ;
        IORZ    R3                      ;
        CPSL    RS+C                    ;
        BCTR,EQ INCNDJ                  ;
        PPSL    RS+WC                   ;
        SUBI,R3 000H                    ;
        SUBI,R2 000H                    ;
        SUBI,R1 000H                    ;
        LODZ    R3                      ;
        CPSL    RS+WC                   ;
        LODI,R1 005H                    ;
INCNDG  BDRR,R1 INCNDG                  ;
        NOP                             ;
        IORZ    R0                      ;
        BCFR,EQ INCNDE                  ;
        LODI,R1 00FH                    ;
INCNDH  BDRR,R1 INCNDH                  ;
        BCTR,UN INCNDE                  ;
;
INCNDI  LODI,R0 001H                    ;
        BCTR,UN INCNDK                  ;
;
INCNDJ  LODI,R0 080H                    ;
INCNDK  STRA,R0 INCRST&pm               ;
        BSTA,UN STRRS                   ;
        LODI,R0 025H                    ;
        BSTA,UN MOVRS                   ;
        LODI,R0 032H                    ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        BCTA,UN CMRET                   ;
;
DSPTM   LODA,R0 INCRST&pm               ;
        BCTA,EQ TXERR                   ;
        LODI,R0 002H                    ;
        BSTA,UN BN2DC                   ; convert (A) binary to decimal (B)
        EORZ    R0                      ;
        BSTA,UN WRDEC                   ; write decimal number (A) to screen
        ZBSR    wrtsp                   ;
        LODI,R0 06DH                    ;
        ZBSR    wrtzv                   ;
        LODI,R0 053H                    ;
        ZBSR    wrtzv                   ;
        ZBSR    wrtcr                   ;
        BCTA,UN CMRET                   ;
;
TESTO   LODI,R1 002H                    ;
        BCTR,UN TESTR                   ;
;
TESTI   LODI,R1 000H                    ;
TESTR   BSTA,UN GTIOS                   ;
        BCFA,LT TXERR                   ;
        LODZ    R1                      ;
        BCTR,EQ TESTRA                  ;
        LODI,R2 008H                    ;
        BCTR,UN TESTRB                  ;
;
TESTRA  LODI,R2 010H                    ;
TESTRB  LODA,R0 INCRST&pm               ;
        BCTA,EQ TXERR                   ;
        BCTA,GT CMRET                   ;
        LODI,R3 008H                    ;
TESTRC  LODA,R0 SLCT&pm,R3-             ;
        IORA,R0 RSLT&pm,R2-             ;
        STRA,R0 RSLT&pm,R2              ;
        BRNR,R3 TESTRC                  ;
        BCTA,UN CMRET                   ;
;
DSPRS   LODA,R0 NBYTOT&pm               ;
        BCTA,EQ TXERR                   ;
        LODA,R0 NBYTIN&pm               ;
        BCTA,EQ TXERR                   ;
        EORZ    R0                      ;
        LODI,R1 040H                    ;
DSPRSA  STRA,R0 SBF2&pm,R1-             ;
        BRNR,R1 DSPRSA                  ;
        LODI,R3 000H                    ;
DSPRSB  LODA,R0 RSLT&pm,R3              ;
        BSTA,UN *wchexv                 ;
        LODZ    R1                      ;
        LODA,R1 SBF2&pm                 ;
        STRA,R0 SBF2&pm,R1+             ;
        LODZ    R2                      ;
        STRA,R0 SBF2&pm,R1+             ;
        STRA,R1 SBF2&pm                 ;
        ADDI,R3 001H                    ;
        COMA,R3 NBYTOT&pm               ;
        BCFR,EQ DSPRSB                  ;
        LODI,R3 000H                    ;
DSPRSC  LODA,R0 RSLT+8&pm,R3            ;
        BSTA,UN *wchexv                 ;
        LODZ    R1                      ;
        LODA,R1 SBF2&pm                 ;
        STRA,R0 SBF2&pm,R1+             ;
        LODZ    R2                      ;
        STRA,R0 SBF2&pm,R1+             ;
        STRA,R1 SBF2&pm                 ;
        ADDI,R3 001H                    ;
        COMA,R3 NBYTIN&pm               ;
        BCFR,EQ DSPRSC                  ;
        LODI,R0 00DH                    ;
        STRA,R0 SBF2&pm,R1+             ;
        LODI,R1 000H                    ;
DSPRSD  LODA,R0 SBF2&pm,R1+             ;
        COMI,R0 020H                    ;
        BCTR,LT DSPRSE                  ;
        ZBSR    wrtzv                   ;
        BCTR,UN DSPRSD                  ;
;
DSPRSE  ZBSR    wrtcr                   ;
        BCTA,UN CMRET                   ;
;
PULSR   LODI,R1 002H                    ;
        BSTA,UN GTIOS                   ;
        BCFA,LT TXERR                   ;
        BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        COMI,R0 02AH                    ;
        BCFA,EQ TXERR                   ;
        EORZ    R0                      ;
        BSTA,UN GETDC                   ; get decimal number to (B)
        BCTA,LT TXERR                   ;
        LODI,R0 010H                    ;
        LODI,R3 001H                    ;
        BSTA,UN STNRDA                  ;
        LODI,R0 034H                    ;
        LODI,R3 000H                    ;
        BSTA,UN STNRDA                  ;
PULSRA  BSTA,UN MINDY                   ;
        LODI,R0 030H                    ;
        BSTA,UN COMRS                   ; compare (B) with (A)
        BCTA,EQ CMRET                   ;
        LODI,R0 010H                    ;
        BSTA,UN SUBBN                   ; (A) = (A) - (B)
        LODI,R0 004H                    ;
        BSTA,UN STRSP                   ; set pointers RSPA (R0 lo) and RSPB (R0 hi) to 3byte values
        BSTR,UN PULSRB                  ;
        BSTA,UN MINDY                   ;
        BSTR,UN PULSRB                  ;
        BCTR,UN PULSRA                  ;
;
PULSRB  LODI,R1 008H                    ;
PULSRC  LODA,R0 OTPR&pm,R1-             ;
        EORA,R0 SLCT&pm,R1              ;
        STRA,R0 OTPR&pm,R1              ;
        BRNR,R1 PULSRC                  ;
        BCTA,UN OTPRT                   ;
;
UPDTR   BSTA,UN INRTN                   ;
        BCTA,UN CMRET                   ;
;
DSPIN   LODI,R1 000H                    ;
        BCTR,UN DSPIO                   ;
;
DSPOT   LODI,R1 002H                    ;
DSPIO   LODA,R0 NBITIN&pm,R1            ;
        BCTA,EQ TXERR                   ;
        STRZ    R2                      ;
        LODI,R3 001H                    ;
        LODZ    R1                      ;
        BCTR,EQ DSPIOA                  ;
        LODI,R1 000H                    ;
        BCTR,UN DSPIOB                  ;
;
DSPIOA  LODI,R1 008H                    ;
DSPIOB  LODA,R0 OTPR&pm,R1              ;
        ANDZ    R3                      ;
        BCTR,EQ DSPIOC                  ;
        LODI,R0 031H                    ;
        BCTR,UN DSPIOD                  ;
;
DSPIOC  LODI,R0 030H                    ;
DSPIOD  ZBSR    wrtzv                   ;
        SUBI,R2 001H                    ;
        BCTR,EQ DSPIOE                  ;
        RRL,R3                          ;
        TMI,R3  001H                    ;
        BCFR,EQ DSPIOB                  ;
        ADDI,R1 001H                    ;
        BCTR,UN DSPIOB                  ;
;
DSPIOE  COMI,R1 008H                    ;
        BCTR,LT DSPIOF                  ;
        LODI,R1 000H                    ;
        BCTR,UN DSPIOG                  ;
;
DSPIOF  LODI,R1 002H                    ;
DSPIOG  LODA,R0 NBITIN&pm,R1            ;
        COMI,R0 040H                    ;
        BCTR,EQ DSPIOH                  ;
        LODI,R0 00DH                    ;
        ZBSR    wrtzv                   ;
DSPIOH  LODA,R0 NBYTIN&pm,R1            ;
        STRZ    R2                      ;
        LODZ    R1                      ;
        BCTR,EQ DSPIOI                  ;
        LODI,R1 004H                    ;
DSPIOI  LODA,R0 TXTIO&pm,R1             ;
        BCTR,LT DSPIOJ                  ;
        ZBSR    wrtzv                   ;
        BIRR,R1 DSPIOI                  ;
DSPIOJ  LODI,R1 004H                    ;
DSPIOK  ZBSR    wrtsp                   ;
        BDRR,R1 DSPIOK                  ;
        SUBI,R2 001H                    ;
        BCTR,EQ DSPIOL                  ;
        LODI,R0 05EH                    ;
        ZBSR    wrtzv                   ;
        LODI,R1 007H                    ;
        BCTR,UN DSPIOK                  ;
;
DSPIOL  ZBSR    wrtcr                   ;
        BCTA,UN CMRET                   ;
;
        BYTE    text,8                  ;
TXTIO   BYTE    "IN ",0FFH              ;
        BYTE    "OUT",0FFH              ;
;
CODER   LODA,R0 NBYTIN&pm               ;
        BCTA,EQ TXERR                   ;
        LODA,R0 NBYTOT&pm               ;
        BCTA,EQ TXERR                   ;
CODERA  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BCTR,EQ CODERA                  ;
CODERB  LODI,R2 000H                    ;
        BCTR,UN CODERD                  ;
;
CODERC  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
CODERD  BSTA,UN *tehexv                 ;
        BCFA,GT TXERR                   ;
        TMI,R0  040H                    ;
        BCFR,EQ CODERE                  ;
        ADDI,R0 009H                    ;
CODERE  ANDI,R0 00FH                    ;
        RRL,R0                          ;
        RRL,R0                          ;
        RRL,R0                          ;
        RRL,R0                          ;
        STRZ    R1                      ;
        BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BSTA,UN *tehexv                 ;
        BCFA,GT TXERR                   ;
        TMI,R0  040H                    ;
        BCFR,EQ CODERF                  ;
        ADDI,R0 009H                    ;
CODERF  ANDI,R0 00FH                    ;
        IORZ    R1                      ;
        COMA,R0 RSLT&pm,R2              ;
        BCFR,EQ CODERI                  ;
        ADDI,R2 001H                    ;
        LODZ    R2                      ;
        COMI,R0 008H                    ;
        BCFR,LT CODERH                  ;
        SUBA,R0 NBYTOT&pm               ;
        BCFR,EQ CODERG                  ;
        LODI,R2 008H                    ;
CODERG  BCTA,UN CODERC                  ;
;
CODERH  SUBA,R0 NBYTIN&pm               ;
        COMI,R0 008H                    ;
        BCTR,EQ CODERL                  ;
        BCTR,UN CODERG                  ;
;
CODERI  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BSTA,UN *tehexv                 ;
        BCTR,GT CODERI                  ;
        BCTR,UN CODERK                  ;
;
CODERJ  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
CODERK  BCTR,EQ CODERJ                  ;
        COMI,R0 03DH                    ;
        BCTA,EQ REMRT                   ;
        BCTA,UN CODERB                  ;
;
CODERL  BSTA,UN *tinprv                 ; R0=inputbuffer(++index) and compared with space
        BSTA,UN *tehexv                 ;
        BCFR,LT CODERL                  ;
        COMI,R0 03DH                    ;
        BCTA,EQ PRINT                   ;
        BCTA,UN TXERR                   ;
;
        BYTE    titl,3
        BYTE    "END"
;
        END                             ;
