(*
    BSD 2-Clause License

    Copyright (c) 2019-2021, Anton Krotov
    All rights reserved.
*)

MODULE MSP430RTL;


CONST

    _mul*      =  0;
    _divmod*   =  1;
    _lsl*      =  2;
    _asr*      =  3;
    _ror*      =  4;
    _lsr*      =  5;
    _in*       =  6;
    _in2*      =  7;
    _set1*     =  8;
    _incl*     =  9;
    _excl*     = 10;
    _move*     = 11;
    _set*      = 12;
    _arrcpy*   = 13;
    _rot*      = 14;
    _strcmp*   = 15;
    _error*    = 16;
    _is*       = 17;
    _guard*    = 18;
    _guardrec* = 19;
    _length*   = 20;
    _new*      = 21;


    HP* = 15;

    LenIV* = 32;

    iv          = 10000H - LenIV * 2;
    bsl         = iv - 2;
    sp          = bsl - 2;
    empty_proc* = sp - 2;
    bits        = empty_proc - 272;
    bits_offs   = bits - 32;
    DataSize*   = iv - bits_offs;
    types       = bits_offs - 2;

    IntVectorSize* = LenIV * 2 + DataSize;

    VarSize*  = 4;

    StkReserve* = 40;

    trap = 2;


TYPE

    EMITPROC = PROCEDURE (n: INTEGER);


VAR

    ram*: INTEGER;

    rtl*: ARRAY 22 OF
              RECORD
                  label*: INTEGER;
                  used:   BOOLEAN
              END;

    Label, Word, Call: EMITPROC;


PROCEDURE Gen*;


    PROCEDURE Word1 (word: INTEGER);
    BEGIN
        Word(word)
    END Word1;


    PROCEDURE Word2 (word1, word2: INTEGER);
    BEGIN
        Word1(word1);
        Word1(word2)
    END Word2;


    PROCEDURE Word3 (word1, word2, word3: INTEGER);
    BEGIN
        Word1(word1);
        Word1(word2);
        Word1(word3)
    END Word3;


BEGIN
    (* _lsl (n, x: INTEGER): INTEGER *)
    IF rtl[_lsl].used THEN
        Label(rtl[_lsl].label);
        Word2(4115H, 2);              (*  MOV  2(SP), R5; R5 <- n  *)
        Word2(4114H, 4);              (*  MOV  4(SP), R4; R4 <- x  *)
        Word2(0F035H, 15);            (*  AND  #15, R5             *)
        Word1(2400H + 3);             (*  JZ   L1                  *)
                                      (*  L2:                      *)
        Word1(5404H);                 (*  ADD  R4, R4              *)
        Word1(8315H);                 (*  SUB  #1, R5              *)
        Word1(2000H + 400H - 3);      (*  JNZ  L2                  *)
                                      (*  L1:                      *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _asr (n, x: INTEGER): INTEGER *)
    IF rtl[_asr].used THEN
        Label(rtl[_asr].label);
        Word2(4115H, 2);              (*  MOV  2(SP), R5; R5 <- n  *)
        Word2(4114H, 4);              (*  MOV  4(SP), R4; R4 <- x  *)
        Word2(0F035H, 15);            (*  AND  #15, R5             *)
        Word1(2400H + 3);             (*  JZ   L1                  *)
                                      (*  L2:                      *)
        Word1(1104H);                 (*  RRA  R4                  *)
        Word1(8315H);                 (*  SUB  #1, R5              *)
        Word1(2000H + 400H - 3);      (*  JNZ  L2                  *)
                                      (*  L1:                      *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _ror (n, x: INTEGER): INTEGER *)
    IF rtl[_ror].used THEN
        Label(rtl[_ror].label);
        Word2(4115H, 2);              (*  MOV  2(SP), R5; R5 <- n  *)
        Word2(4114H, 4);              (*  MOV  4(SP), R4; R4 <- x  *)
        Word2(0F035H, 15);            (*  AND  #15, R5             *)
        Word1(2400H + 5);             (*  JZ   L1                  *)
        Word1(4406H);                 (*  MOV  R4, R6              *)
                                      (*  L2:                      *)
        Word1(1006H);                 (*  RRC  R6                  *)
        Word1(1004H);                 (*  RRC  R4                  *)
        Word1(8315H);                 (*  SUB  #1, R5              *)
        Word1(2000H + 400H - 4);      (*  JNZ  L2                  *)
                                      (*  L1:                      *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _lsr (n, x: INTEGER): INTEGER *)
    IF rtl[_lsr].used THEN
        Label(rtl[_lsr].label);
        Word2(4115H, 2);              (*  MOV  2(SP), R5; R5 <- n  *)
        Word2(4114H, 4);              (*  MOV  4(SP), R4; R4 <- x  *)
        Word2(0F035H, 15);            (*  AND  #15, R5             *)
        Word1(2400H + 4);             (*  JZ   L1                  *)
                                      (*  L2:                      *)
        Word1(0C312H);                (*  BIC  #1, SR              *)
        Word1(1004H);                 (*  RRC  R4                  *)
        Word1(8315H);                 (*  SUB  #1, R5              *)
        Word1(2000H + 400H - 4);      (*  JNZ  L2                  *)
                                      (*  L1:                      *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _set (b, a: INTEGER): SET *)
    IF rtl[_set].used THEN
        Label(rtl[_set].label);
        Word2(4114H, 2);              (*  MOV  2(SP), R4; R4 <- b  *)
        Word2(4115H, 4);              (*  MOV  4(SP), R5; R5 <- a  *)
        Word1(9504H);                 (*  CMP  R5, R4              *)
        Word1(3800H + 24);            (*  JL   L1                  *)
        Word2(9035H, 16);             (*  CMP  #16, R5             *)
        Word1(3400H + 21);            (*  JGE  L1                  *)
        Word1(9304H);                 (*  CMP  #0, R4              *)
        Word1(3800H + 19);            (*  JL   L1                  *)
        Word2(9034H, 16);             (*  CMP  #16, R4             *)
        Word1(3800H + 2);             (*  JL   L2                  *)
        Word2(4034H, 15);             (*  MOV  #15, R4             *)
                                      (*  L2:                      *)
        Word1(9305H);                 (*  CMP  #0, R5              *)
        Word1(3400H + 1);             (*  JGE  L3                  *)
        Word1(4305H);                 (*  MOV  #0, R5              *)
                                      (*  L3:                      *)
        Word1(8504H);                 (*  SUB  R5, R4              *)
        Word1(5404H);                 (*  ADD  R4, R4              *)
        Word2(5034H, bits_offs);      (*  ADD  bits_offs, R4       *)
        Word1(4424H);                 (*  MOV  @R4, R4             *)
        Word1(5505H);                 (*  ADD  R5, R5              *)
        Word1(5405H);                 (*  ADD  R4, R5              *)
        Word2(5035H, bits);           (*  ADD  bits, R5            *)
        Word1(4524H);                 (*  MOV  @R5, R4             *)
        Word1(4130H);                 (*  RET                      *)
                                      (*  L1:                      *)
        Word1(4304H);                 (*  MOV  #0, R4              *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _set1 (a: INTEGER): SET *)
    IF rtl[_set1].used THEN
        Label(rtl[_set1].label);
        Word2(4114H, 2);              (*  MOV  2(SP), R4; R4 <- a  *)
        Word2(0B034H, 0FFF0H);        (*  BIT  #0FFF0H, R4         *)
        Word1(2000H + 5);             (*  JNZ  L1                  *)
        Word1(5404H);                 (*  ADD  R4, R4              *)
        Word2(5034H, bits);           (*  ADD  bits, R4            *)
        Word1(4424H);                 (*  MOV  @R4, R4             *)
        Word1(4130H);                 (*  RET                      *)
                                      (*  L1:                      *)
        Word1(4304H);                 (*  MOV  #0, R4              *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _in2 (i, s: INTEGER): BOOLEAN *)
    IF rtl[_in2].used THEN
        Label(rtl[_in2].label);
        Word2(4114H, 2);               (*  MOV  2(SP), R4; R4 <- i  *)
        Word1(5404H);                  (*  ADD  R4, R4              *)
        Word2(5034H, bits);            (*  ADD  bits, R4            *)
        Word1(4424H);                  (*  MOV  @R4, R4             *)
        Word2(0F114H, 4);              (*  AND  4(SP), R4           *)
        Word1(2400H + 1);              (*  JZ   L1                  *)
        Word1(4314H);                  (*  MOV  #1, R4              *)
                                       (*  L1:                      *)
        Word1(4130H)                   (*  RET                      *)
    END;

    (* _in (s, i: INTEGER): BOOLEAN *)
    IF rtl[_in].used THEN
        Label(rtl[_in].label);
        Word2(4114H, 4);               (*  MOV  4(SP), R4; R4 <- i  *)
        Word2(0B034H, 0FFF0H);         (*  BIT  #0FFF0H, R4         *)
        Word1(2000H + 9);              (*  JNZ  L2                  *)
        Word1(5404H);                  (*  ADD  R4, R4              *)
        Word2(5034H, bits);            (*  ADD  bits, R4            *)
        Word1(4424H);                  (*  MOV  @R4, R4             *)
        Word2(0F114H, 2);              (*  AND  2(SP), R4           *)
        Word1(2400H + 3);              (*  JZ   L1                  *)
        Word1(4314H);                  (*  MOV  #1, R4              *)
        Word1(4130H);                  (*  RET                      *)
                                       (*  L2:                      *)
        Word1(4304H);                  (*  MOV  #0, R4              *)
                                       (*  L1:                      *)
        Word1(4130H)                   (*  RET                      *)
    END;

    (* _incl (VAR s: SET; i: INTEGER) *)
    IF rtl[_incl].used THEN
        Label(rtl[_incl].label);
        Word2(4114H, 4);          (*  MOV  4(SP), R4; R4 <- i  *)
        Word2(0B034H, 0FFF0H);    (*  BIT  #0FFF0H, R4         *)
        Word1(2000H + 8);         (*  JNZ  L1                  *)
        Word1(5404H);             (*  ADD  R4, R4              *)
        Word2(5034H, bits);       (*  ADD  bits, R4            *)
        Word1(4424H);             (*  MOV  @R4, R4             *)
        Word2(4115H, 2);          (*  MOV  2(SP), R5; R5 <- @s *)
        Word2(0D485H, 0);         (*  BIS  R4, 0(R5)           *)
                                  (*  L1:                      *)
        Word1(4130H)              (*  RET                      *)
    END;

    (* _excl (VAR s: SET; i: INTEGER) *)
    IF rtl[_excl].used THEN
        Label(rtl[_excl].label);
        Word2(4114H, 4);          (*  MOV  4(SP), R4; R4 <- i  *)
        Word2(0B034H, 0FFF0H);    (*  BIT  #0FFF0H, R4         *)
        Word1(2000H + 8);         (*  JNZ  L1                  *)
        Word1(5404H);             (*  ADD  R4, R4              *)
        Word2(5034H, bits);       (*  ADD  bits, R4            *)
        Word1(4424H);             (*  MOV  @R4, R4             *)
        Word2(4115H, 2);          (*  MOV  2(SP), R5; R5 <- @s *)
        Word2(0C485H, 0);         (*  BIC  R4, 0(R5)           *)
                                  (*  L1:                      *)
        Word1(4130H)              (*  RET                      *)
    END;

    (* _rot (len, adr: INTEGER) *)
    IF rtl[_rot].used THEN
        Label(rtl[_rot].label);
        Word2(4114H, 2);              (*  MOV    2(SP), R4; R4 <- len   *)
        Word2(4115H, 4);              (*  MOV    4(SP), R5; R5 <- adr   *)
        Word1(8314H);                 (*  SUB    #1, R4                 *)
        Word1(5404H);                 (*  ADD    R4, R4                 *)
        Word1(1225H);                 (*  PUSH   @R5                    *)
        Word1(4406H);                 (*  MOV    R4, R6                 *)
                                      (*  L1:                           *)
        Word3(4595H, 2, 0);           (*  MOV    2(R5), 0(R5)           *)
        Word1(5325H);                 (*  ADD    #2, R5                 *)
        Word1(8326H);                 (*  SUB    #2, R6                 *)
        Word1(2000H + 400H - 6);      (*  JNZ    L1                     *)
        Word2(41B5H, 0);              (*  MOV    @SP+, 0(R5)            *)
        Word1(4130H)                  (*  RET                           *)
    END;

    (* _divmod (b, a: INTEGER): INTEGER (* res -> R4, mod -> R5 *) *)
    IF rtl[_divmod].used THEN
        Label(rtl[_divmod].label);
        Word2(4115H, 4);              (*  MOV  4(SP), R5; R5 <- a  *)
        Word1(4304H);                 (*  MOV  #0, R4              *)
                                      (*  L1:                      *)
        Word2(4116H, 2);              (*  MOV  2(SP), R6; R6 <- b  *)
        Word1(9605H);                 (*  CMP  R6, R5              *)
        Word1(3800H + 17);            (*  JL   L3                  *)
        Word1(4327H);                 (*  MOV  #2, R7              *)
        Word1(5606H);                 (*  ADD  R6, R6              *)
                                      (*  L4:                      *)
        Word1(9306H);                 (*  CMP  #0, R6              *)
        Word1(2400H + 6);             (*  JZ   L2                  *)
        Word1(3800H + 5);             (*  JL   L2                  *)
        Word1(9605H);                 (*  CMP  R6, R5              *)
        Word1(3800H + 3);             (*  JL   L2                  *)
        Word1(5606H);                 (*  ADD  R6, R6              *)
        Word1(5707H);                 (*  ADD  R7, R7              *)
        Word1(3C00H + 400H - 8);      (*  JMP  L4                  *)
                                      (*  L2:                      *)
        Word1(0C312H);                (*  BIC  #1, SR              *)
        Word1(1006H);                 (*  RRC  R6                  *)
        Word1(0C312H);                (*  BIC  #1, SR              *)
        Word1(1007H);                 (*  RRC  R7                  *)
        Word1(8605H);                 (*  SUB  R6, R5              *)
        Word1(5704H);                 (*  ADD  R7, R4              *)
        Word1(3C00H + 400H - 21);     (*  JMP  L1                  *)
                                      (*  L3:                      *)
        (*----------- (a < 0) --------------*)
                                      (*  L1:                      *)
        Word1(9305H);                 (*  CMP  #0, R5              *)
        Word1(3400H + 23);            (*  JGE  L3                  *)
        Word2(4116H, 2);              (*  MOV  2(SP), R6; R6 <- b  *)
        Word1(4327H);                 (*  MOV  #2, R7              *)
        Word1(5606H);                 (*  ADD  R6, R6              *)
        Word1(0E335H);                (*  XOR  #-1, R5             *)
        Word1(5315H);                 (*  ADD  #1,  R5             *)
                                      (*  L4:                      *)
        Word1(9306H);                 (*  CMP  #0, R6              *)
        Word1(2400H + 6);             (*  JZ   L2                  *)
        Word1(3800H + 5);             (*  JL   L2                  *)
        Word1(9605H);                 (*  CMP  R6, R5              *)
        Word1(3800H + 3);             (*  JL   L2                  *)
        Word1(5606H);                 (*  ADD  R6, R6              *)
        Word1(5707H);                 (*  ADD  R7, R7              *)
        Word1(3C00H + 400H - 8);      (*  JMP  L4                  *)
                                      (*  L2:                      *)
        Word1(0E335H);                (*  XOR  #-1, R5             *)
        Word1(5315H);                 (*  ADD  #1,  R5             *)
        Word1(0C312H);                (*  BIC  #1, SR              *)
        Word1(1006H);                 (*  RRC  R6                  *)
        Word1(0C312H);                (*  BIC  #1, SR              *)
        Word1(1007H);                 (*  RRC  R7                  *)
        Word1(5605H);                 (*  ADD  R6, R5              *)
        Word1(8704H);                 (*  SUB  R7, R4              *)
        Word1(3C00H + 400H - 25);     (*  JMP  L1                  *)
                                      (*  L3:                      *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _mul (a, b: INTEGER): INTEGER *)
    IF rtl[_mul].used THEN
        Label(rtl[_mul].label);
        Word2(4115H, 2);              (*  MOV  2(SP), R5; R5 <- a  *)
        Word2(4116H, 4);              (*  MOV  4(SP), R6; R6 <- b  *)
        Word1(4304H);                 (*  MOV  #0, R4; res := 0    *)
        Word1(9306H);                 (*  CMP  #0, R6              *)
        Word1(2400H + 7);             (*  JZ   L1                  *)
                                      (*  L2:                      *)
        Word1(0B316H);                (*  BIT  #1, R6              *)
        Word1(2400H + 1);             (*  JZ   L3                  *)
        Word1(5504H);                 (*  ADD  R5, R4              *)
                                      (*  L3:                      *)
        Word1(5505H);                 (*  ADD  R5, R5              *)
        Word1(0C312H);                (*  BIC  #1, SR              *)
        Word1(1006H);                 (*  RRC  R6                  *)
        Word1(2000H + 400H - 7);      (*  JNZ  L2                  *)
                                      (*  L1:                      *)
        Word1(4130H)                  (*  RET                      *)
    END;

    (* _error (modNum, modName, err, line: INTEGER) *)
    IF rtl[_error].used THEN
        Label(rtl[_error].label);
        Word1(5321H);          (*  ADD   #2, SP             *)
        Word1(4134H);          (*  POP   R4; R4 <- modNum   *)
        Word1(4135H);          (*  POP   R5; R5 <- modName  *)
        Word1(4136H);          (*  POP   R6; R6 <- err      *)
        Word1(4137H);          (*  POP   R7; R7 <- line     *)
        Word2(4211H, sp);      (*  MOV   sp(SR), SP                   *)
        Word1(1207H);          (*  PUSH  R7                           *)
        Word1(1206H);          (*  PUSH  R6                           *)
        Word1(1205H);          (*  PUSH  R5                           *)
        Word1(1204H);          (*  PUSH  R4                           *)
        Word2(4214H, sp);      (*  MOV   sp(SR), R4                   *)
        Word2(1294H, trap);    (*  CALL  trap(R4)                     *)
        Word2(04032H, 0F0H)    (*  MOV   CPUOFF+OSCOFF+SCG0+SCG1, SR  *)
    END;

    (* _new (t, size: INTEGER; VAR ptr: INTEGER) *)
    IF rtl[_new].used THEN
        Label(rtl[_new].label);
        Word1(1202H);                  (*  PUSH SR               *)
        Word1(4302H);                  (*  MOV  #0, SR           *)
        Word1(4303H);                  (*  NOP                   *)
        Word1(4104H);                  (*  MOV  SP, R4           *)
        Word2(8034H, StkReserve);      (*  SUB  #StkReserve, R4  *)
        Word1(4005H + 100H * HP);      (*  MOV  HP, R5           *)
        Word2(5115H, 6);               (*  ADD  6(SP), R5        *)
        Word1(9504H);                  (*  CMP  R5, R4           *)
        Word2(4114H, 8);               (*  MOV  8(SP), R4        *)
        Word1(3800H + 12);             (*  JL   L1               *)
        Word3(4190H + HP, 4, 0);       (*  MOV  4(SP), 0(HP)     *)
        Word1(5320H + HP);             (*  ADD  #2, HP           *)
        Word2(4084H + 100H * HP, 0);   (*  MOV  HP, 0(R4)        *)
                                       (*  L3                    *)
        Word2(4380H + HP, 0);          (*  MOV  #0, 0(HP)        *)
        Word1(5320H + HP);             (*  ADD  #2, HP           *)
        Word1(9500H + HP);             (*  CMP  R5, HP           *)
        Word1(3800H + 400H - 5);       (*  JL   L3               *)
        Word1(3C00H + 2);              (*  JMP  L2               *)
                                       (*  L1                    *)
        Word2(4384H, 0);               (*  MOV  #0, 0(R4)        *)
                                       (*  L2                    *)
        Word1(1300H)                   (*  RETI                  *)
    END;

    (* _guardrec (t0, t1: INTEGER): INTEGER *)
    IF rtl[_guardrec].used THEN
        Label(rtl[_guardrec].label);
        Word2(4114H, 2);              (*  MOV  2(SP), R4; R4 <- t0  *)
        Word2(4115H, 4);              (*  MOV  4(SP), R5; R5 <- t1  *)
        Word2(4036H, types);          (*  MOV  #types, R6           *)
                                      (*  L3:                       *)
        Word1(9305H);                 (*  CMP  #0, R5               *)
        Word1(2400H + 8);             (*  JZ   L1                   *)
        Word1(9405H);                 (*  CMP  R4, R5               *)
        Word1(2400H + 10);            (*  JZ   L2                   *)
        Word1(5505H);                 (*  ADD  R5, R5               *)
        Word1(0E335H);                (*  XOR  #-1, R5              *)
        Word1(5315H);                 (*  ADD  #1, R5               *)
        Word1(5605H);                 (*  ADD  R6, R5               *)
        Word1(4525H);                 (*  MOV  @R5, R5              *)
        Word1(3C00H + 400H - 10);     (*  JMP  L3                   *)
                                      (*  L1:                       *)
        Word1(9405H);                 (*  CMP  R4, R5               *)
        Word1(2400H + 2);             (*  JZ   L2                   *)
        Word1(4304H);                 (*  MOV  #0, R4               *)
        Word1(4130H);                 (*  RET                       *)
                                      (*  L2:                       *)
        Word1(4314H);                 (*  MOV  #1, R4               *)
        Word1(4130H)                  (*  RET                       *)
    END;

    (* _is (t, p: INTEGER): INTEGER *)
    IF rtl[_is].used THEN
        Label(rtl[_is].label);
        Word2(4114H, 4);             (*  MOV  4(SP), R4; R4 <- p  *)
        Word2(4115H, 2);             (*  MOV  2(SP), R5; R5 <- t  *)
        Word1(9304H);                (*  TST  R4                  *)
        Word1(2400H + 2);            (*  JZ   L                   *)
        Word2(4414H, -2);            (*  MOV  -2(R4), R4          *)
                                     (*  L:                       *)
        Word1(1204H);                (*  PUSH R4                  *)
        Word1(1205H);                (*  PUSH R5                  *)
        Call(rtl[_guardrec].label);  (*  CALL _guardrec           *)
        Word1(5221H);                (*  ADD  #4, SP              *)
        Word1(4130H)                 (*  RET                      *)
    END;

    (* _guard (t, p: INTEGER): INTEGER *)
    IF rtl[_guard].used THEN
        Label(rtl[_guard].label);
        Word2(4115H, 4);             (*  MOV  4(SP), R5; R5 <- p  *)
        Word1(4314H);                (*  MOV  #1, R4              *)
        Word1(4525H);                (*  MOV  @R5, R5             *)
        Word1(9305H);                (*  TST  R5                  *)
        Word1(2400H + 9);            (*  JZ   L                   *)
        Word2(4515H, -2);            (*  MOV  -2(R5), R5          *)
        Word2(4114H, 2);             (*  MOV  2(SP), R4; R4 <- t  *)
        Word1(1205H);                (*  PUSH R5                  *)
        Word1(1204H);                (*  PUSH R4                  *)
        Call(rtl[_guardrec].label);  (*  CALL _guardrec           *)
        Word1(5221H);                (*  ADD  #4, SP              *)
                                     (*  L:                       *)
        Word1(4130H)                 (*  RET                      *)
    END;

    (* _move (bytes, dest, source: INTEGER) *)
    IF rtl[_move].used THEN
        Label(rtl[_move].label);
        Word2(4116H, 2);              (*  MOV    2(SP), R6; R6 <- bytes   *)
        Word2(4117H, 4);              (*  MOV    4(SP), R7; R7 <- dest    *)
        Word2(4115H, 6);              (*  MOV    6(SP), R5; R5 <- source  *)
        Word1(9306H);                 (*  CMP    #0, R6                   *)
        Word1(3800H + 6);             (*  JL     L1                       *)
        Word1(2400H + 5);             (*  JZ     L1                       *)
                                      (*  L2:                             *)
        Word2(45F7H, 0);              (*  MOV.B  @R5+, 0(R7)              *)
        Word1(5317H);                 (*  ADD    #1, R7                   *)
        Word1(8316H);                 (*  SUB    #1, R6                   *)
        Word1(2000H + 400H - 5);      (*  JNZ    L2                       *)
                                      (*  L1:                             *)
        Word1(4130H)                  (*  RET                             *)
    END;

    (* _arrcpy (base_size, len_dst, dst, len_src, src: INTEGER) *)
    IF rtl[_arrcpy].used THEN
        Label(rtl[_arrcpy].label);
        Word3(9191H, 8, 4);          (*  CMP  8(SP), 4(SP) *)
        Word1(3800H + 18);           (*  JL   L1           *)
        Word2(1211H, 12);            (*  PUSH 12(SP)       *)
        Word2(1211H, 10);            (*  PUSH 10(SP)       *)
        Word2(1211H, 14);            (*  PUSH 14(SP)       *)
        Word2(1211H, 10);            (*  PUSH 10(SP)       *)
        Call(rtl[_mul].label);       (*  CALL _mul         *)
        Word1(5221H);                (*  ADD  #4, SP       *)
        Word1(1204H);                (*  PUSH R4           *)
        Call(rtl[_move].label);      (*  CALL _move        *)
        Word2(5031H, 6);             (*  ADD  #6, SP       *)
        Word1(4314H);                (*  MOV  #1, R4       *)
        Word1(4130H);                (*  RET               *)
                                     (*  L1                *)
        Word1(4304H);                (*  MOV  #0, R4       *)
        Word1(4130H)                 (*  RET               *)
    END;

    (* _length (len, str: INTEGER): INTEGER *)
    IF rtl[_length].used THEN
        Label(rtl[_length].label);
        Word2(4116H, 2);              (*  MOV    2(SP), R6; R6 <- len  *)
        Word2(4117H, 4);              (*  MOV    4(SP), R7; R7 <- str  *)
        Word1(4304H);                 (*  MOV    #0, R4; res := 0      *)
                                      (*  L2:                          *)
        Word1(4775H);                 (*  MOV.B  @R7+, R5              *)
        Word1(9305H);                 (*  CMP    #0, R5                *)
        Word1(2400H + 3);             (*  JZ     L1                    *)
        Word1(5314H);                 (*  ADD    #1, R4                *)
        Word1(8316H);                 (*  SUB    #1, R6                *)
        Word1(2000H + 400H - 6);      (*  JNZ    L2                    *)
                                      (*  L1:                          *)
        Word1(4130H)                  (*  RET                          *)
    END;

    (* _strcmp (op, len2, str2, len1, str1: INTEGER): BOOLEAN *)
    IF rtl[_strcmp].used THEN
        Label(rtl[_strcmp].label);
        Word2(4116H, 4);            (*  MOV    4(SP), R6; R6 <- len2   *)
        Word2(4117H, 8);            (*  MOV    8(SP), R7; R7 <- len1   *)
        Word1(9607H);               (*  CMP    R6, R7                  *)
        Word1(3400H + 1);           (*  JGE    L5                      *)
        Word1(4706H);               (*  MOV    R7, R6                  *)
                                    (*  L5:                            *)
        Word1(1206H);               (*  PUSH   R6                      *)
        Word2(4116H, 12);           (*  MOV    12(SP), R6; R6 <- str1  *)
        Word2(4117H, 8);            (*  MOV    8(SP), R7; R7 <- str2   *)
                                    (*  L3:                            *)
        Word2(9381H, 0);            (*  CMP    #0, 0(SP)               *)
        Word1(2400H + 11);          (*  JZ     L1                      *)
        Word1(4674H);               (*  MOV.B  @R6+, R4                *)
        Word1(4775H);               (*  MOV.B  @R7+, R5                *)
        Word2(8391H, 0);            (*  SUB    #1, 0(SP)               *)
        Word1(9405H);               (*  CMP    R4, R5                  *)
        Word1(2400H + 2);           (*  JZ     L2                      *)
        Word1(8504H);               (*  SUB    R5, R4                  *)
        Word1(3C00H + 5);           (*  JMP    L4                      *)
                                    (*  L2:                            *)
        Word1(9304H);               (*  CMP    #0, R4                  *)
        Word1(2000H + 400H - 13);   (*  JNZ    L3                      *)
        Word1(3C00H + 2);           (*  JMP    L4                      *)
                                    (*  L1:                            *)
        Word2(4034H, 8000H);        (*  MOV    #8000H, R4              *)
                                    (*  L4:                            *)
        Word1(5321H);               (*  ADD    #2, SP                  *)

        Word2(9034H, 8000H);        (*  CMP    #8000H, R4              *)
        Word1(2000H + 18);          (*  JNZ    L6                      *)
        Word2(4116H, 4);            (*  MOV    4(SP), R6; R6 <- len2   *)
        Word2(4117H, 8);            (*  MOV    8(SP), R7; R7 <- len1   *)
        Word1(9607H);               (*  CMP    R6, R7                  *)
        Word1(2400H + 11);          (*  JZ     L7                      *)
        Word1(3800H + 4);           (*  JL     L8                      *)
        Word2(5116H, 10);           (*  ADD    10(SP), R6              *)
        Word1(4664H);               (*  MOV.B  @R6, R4                 *)
        Word1(3C00H + 7);           (*  JMP    L6                      *)
                                    (*  L8:                            *)
        Word2(5117H, 6);            (*  ADD    6(SP), R7               *)
        Word1(4764H);               (*  MOV.B  @R7, R4                 *)
        Word1(0E334H);              (*  XOR    #-1, R4                 *)
        Word1(5314H);               (*  ADD    #1, R4                  *)
        Word1(3C00H + 1);           (*  JMP    L6                      *)
                                    (*  L7:                            *)
        Word1(4304H);               (*  MOV    #0, R4                  *)
                                    (*  L6:                            *)

        Word2(5110H, 2);            (*  ADD  2(SP), PC; PC <- PC + op  *)

        Word1(9304H);               (*  CMP  #0, R4  *)
        Word1(4314H);               (*  MOV  #1, R4  *)
        Word1(2400H + 1);           (*  JZ   L       *)
        Word1(4304H);               (*  MOV  #0, R4  *)
                                    (*  L            *)
        Word1(4130H);               (*  RET          *)
        Word1(4303H);               (*  NOP          *)

        Word1(9304H);               (*  CMP  #0, R4  *)
        Word1(4314H);               (*  MOV  #1, R4  *)
        Word1(2000H + 1);           (*  JNZ  L       *)
        Word1(4304H);               (*  MOV  #0, R4  *)
                                    (*  L            *)
        Word1(4130H);               (*  RET          *)
        Word1(4303H);               (*  NOP          *)

        Word1(9304H);               (*  CMP  #0, R4  *)
        Word1(4314H);               (*  MOV  #1, R4  *)
        Word1(3800H + 1);           (*  JL   L       *)
        Word1(4304H);               (*  MOV  #0, R4  *)
                                    (*  L            *)
        Word1(4130H);               (*  RET          *)
        Word1(4303H);               (*  NOP          *)

        Word1(9304H);               (*  CMP  #0, R4  *)
        Word1(4314H);               (*  MOV  #1, R4  *)
        Word1(3800H + 2);           (*  JL   L       *)
        Word1(2400H + 1);           (*  JZ   L       *)
        Word1(4304H);               (*  MOV  #0, R4  *)
                                    (*  L            *)
        Word1(4130H);               (*  RET          *)

        Word1(9304H);               (*  CMP  #0, R4  *)
        Word1(4304H);               (*  MOV  #0, R4  *)
        Word1(3800H + 2);           (*  JL   L       *)
        Word1(2400H + 1);           (*  JZ   L       *)
        Word1(4314H);               (*  MOV  #1, R4  *)
                                    (*  L            *)
        Word1(4130H);               (*  RET          *)

        Word1(9304H);               (*  CMP  #0, R4  *)
        Word1(4314H);               (*  MOV  #1, R4  *)
        Word1(3400H + 1);           (*  JGE  L       *)
        Word1(4304H);               (*  MOV  #0, R4  *)
                                    (*  L            *)
        Word1(4130H)                (*  RET          *)
    END

END Gen;


PROCEDURE Set* (idx, label: INTEGER);
BEGIN
    rtl[idx].label := label;
    rtl[idx].used  := FALSE
END Set;


PROCEDURE Used* (idx: INTEGER);
BEGIN
    rtl[idx].used := TRUE;
    IF (idx = _guard) OR (idx = _is) THEN
        rtl[_guardrec].used := TRUE
    ELSIF idx = _arrcpy THEN
        rtl[_move].used := TRUE;
        rtl[_mul].used  := TRUE
    END
END Used;


PROCEDURE Init* (pLabel, pWord, pCall: EMITPROC);
BEGIN
    Label := pLabel;
    Word  := pWord;
    Call  := pCall;
    ram   := 200H;
END Init;


END MSP430RTL.