tolk          jmp  #tolk0
base          long 0
iadr          long 0
aadr          long 0
instr         long 0
type          long 0
adr           long 0
adrmask       long $000FFFFF
sat           long 0
loc           long 0
zero          long 0
allones       long $FFFFFFFF

stack         long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
st            long 0
sa            long 0
arg0          long 0
arg1          long 0
arg2          long 0
gadr          long 0

sstack        long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
              long 0
ssa           long 0

statstack     long 0

pulse         long 0
space         long 330
time          long 0 
dtime         long 0
nbits         long 0
startbit      long 512
 
'---------------------------------------
tolk0    mov     sa,#stack
         mov     ssa,#sstack
         mov     statstack,#0
         mov     iadr,#0

loop     mov     aadr,par
         shr     aadr,#2
         add     aadr,base
         add     aadr,iadr
         shl     aadr,#2
         rdlong  instr,aadr

         call    #cond
         mov     sat,sat   nr,wz,wc
   if_z  jmp     #nextinstr
         mov     type,instr
         shr     type,#27
         mov     adr,instr
         and     adr,adrmask
         mov     loc,#first
         add     loc,type
         jmp     loc

first    jmp     #lpushim
         jmp     #lpush
         jmp     #lpushg
         jmp     #lpop
         jmp     #lpopg
         jmp     #lpushel
         jmp     #lpushelg
         jmp     #lpopel
         jmp     #lpopelg
         jmp     #lpushat
         jmp     #lpopat
         jmp     #lraise
         jmp     #lpopoff
         jmp     #lflip
         jmp     #ldup
         jmp     #ljmp
         jmp     #lcall
         jmp     #lreturn
         jmp     #lasm
         jmp     #lstatus
         jmp     #lrstatus
         jmp     #l21op
         jmp     #l11op
         jmp     #l01op
         jmp     #l10op
         jmp     #l20op

'------------------------------
lpushim  add     sa,#1
         movd    ipushim,sa
         nop
ipushim  mov     stack,adr
         jmp     #nextinstr
'------------------------------
lpush    add     sa,#1
         movd    ipush,sa
         movs    ipush,adr
         nop
ipush    mov     stack,adr                                            
         jmp     #nextinstr
'------------------------------
lpushg   add     sa,#1
         mov     gadr,par
         add     gadr,adr
         rdlong  arg1,gadr
         movd    ipushg,sa
         nop
ipushg   mov     stack,arg1
         jmp     #nextinstr
'-------------------------------
lpop     movd    ipop,adr
         movs    ipop,sa
         nop
ipop     mov     adr,stack
         sub     sa,#1
         jmp     #nextinstr
'-------------------------------
lpopg    mov     gadr,par
         add     gadr,adr
         movs    ipopg,sa
         nop
ipopg    mov     arg1,stack
         wrlong  arg1,gadr
         sub     sa,#1
         jmp     #nextinstr
'-------------------------------
lpushel  mov     arg0,adr
         call    #popv
         add     arg0,st
         add     sa,#1
         movd    ipushel,sa
         movs    ipushel,arg0
         nop
ipushel  mov     stack,arg0
         jmp     #nextinstr
'-------------------------------
lpushelg call    #popv
         mov     arg2,st
         shl     arg2,#2
         mov     gadr,par
         add     gadr,adr
         add     gadr,arg2
         rdlong  arg1,gadr
         add     sa,#1
         movd    ipushelg,sa
         nop
ipushelg mov     stack,arg1
         jmp     #nextinstr
'-------------------------------
lpopel   mov     arg0,adr
         call    #popv      
         add     arg0,st
         call    #popv
         movd    ipopel,arg0
         nop
ipopel   mov     arg0,st
         jmp     #nextinstr
'-------------------------------
lpopelg  mov     gadr,par
         add     gadr,adr
         call    #popv
         mov     arg2,st
         shl     arg2,#2
         add     gadr,arg2
         movs    ipopelg,sa
         nop
ipopelg  mov     arg1,stack
         wrlong  arg1,gadr
         sub     sa,#1
         jmp     #nextinstr
'-------------------------------
lpushat  call    #popv
         mov     arg0,st
         call    #popv
         add     arg0,st
         add     sa,#1
         movd    ipushat,sa
         movs    ipushat,arg0
         nop
ipushat  mov     stack,arg0
         jmp     #nextinstr
'--------------------------------
lpopat   call    #popv
         mov     arg0,st
         call    #popv
         add     arg0,st
         call    #popv
         movd    ipopat,arg0
         nop
ipopat   mov     arg1,st
         jmp     #nextinstr
'-------------------------------
lraise   add     sa,#1
         jmp     #nextinstr
'-------------------------------
lpopoff  sub     sa,#1
         jmp     #nextinstr
'-------------------------------
lflip    call    #popv
         mov     arg1,st
         call    #popv
         add     sa,#1
         movd    iflip1,sa
         add     sa,#1
         movd    iflip2,sa
iflip1   mov     stack,arg1
iflip2   mov     stack,st
         jmp     #nextinstr
'--------------------------------
ldup     movs    idup,sa
         add     sa,#1
         movd    idup,sa
         nop
idup     mov     stack,stack
         jmp     #nextinstr
'--------------------------------
ljmp     mov     iadr,adr
         jmp     #loop
'--------------------------------
lcall    add     ssa,#1
         movd    ispush,ssa
         nop
ispush   mov     sstack,iadr
         mov     iadr,adr
         jmp     #loop
'--------------------------------
lreturn  movs    ispop,ssa
         nop
ispop    mov     iadr,sstack
         add     iadr,#1
         sub     ssa,#1
         jmp     #loop
'--------------------------------
lasm     mov     loc,#iasm0
         add     loc,adr
         jmp     loc
iasm0    jmp     #0
         jmp     #0
         jmp     #0
         jmp     #0
         jmp     #0
         jmp     #0
         jmp     #0
         jmp     #0
'--------------------------------
lstatus  shl  statstack,#2
         movs istatus,sa
         nop
istatus  mov  st,st        nr,wz,wc
  if_z   or   statstack,#1
  if_a   or   statstack,#2
         sub  sa,#1
         jmp  #nextinstr
'--------------------------------
lrstatus shr  statstack,#2
         jmp  #nextinstr
'-----------------------------------------------------
'  0 +, 2 -, 4 or, 6 and, 8 xor, 10 >>n, 12 >>, 14 << 16 *, 25 receive
'-----------------------------------------------------
l21op      call    #popv
           mov     arg1,st
           call    #popv
           mov     loc,#l210
           add     loc,adr
           jmp     loc
'---------------
l210       add     st,arg1    '+
           jmp     #com21
'----------------
           sub     st,arg1    '-
           jmp     #com21
'----------------
           or      st,arg1    'or
           jmp     #com21
'----------------
           and     st,arg1    'and
           jmp     #com21
'----------------
           xor     st,arg1    'xor
           jmp     #com21
'----------------
           shr     st,arg1    'sar
           jmp     #com21
'-----------------
           sar     st,arg1    'shr
           jmp     #com21
'-----------------
           shl     st,arg1    'shl
           jmp     #com21
'-----------------
           mov     arg2,st  'mult
           mov     st,#0
           mov     nbits,#32
multloop   and     arg1,#1    nr,wz,wc
    if_nz  add     st,arg2
           shl     arg2,#1
           shr     arg1,#1
           djnz    nbits,#multloop
           jmp     #com21
'-----------------
           mov     pulse,arg1  'receive
           mov     arg1,st
           mov     arg0,#1
           shl     arg0,arg1
           mov     st,#0
recwait0   mov     arg2,ina
           and     arg2,arg0  wz
     if_z  jmp     #recwait0
recwait1   mov     arg2,ina
           and     arg2,arg0  wz
     if_nz jmp     #recwait1
           mov     nbits,#9
           mov     dtime,pulse
           sar     dtime,#1
           mov     time,cnt
           add     time,dtime
recloop    waitcnt time,pulse
           mov     arg2,ina
           and     arg2,arg0
           shr     arg2,arg1
           shl     arg2,#7            
           shr     st,#1
           or      st,arg2
           djnz    nbits,#recloop
'-----------
com21      add     sa,#1
           movd    i21op,sa
           nop
i21op      mov     stack,st
           jmp     #nextinstr


'-------------------------------------
'   0 abs,  2 compl,  5 neg, 7 G,  11 Z,  15 inpin
'-------------------------------------
l11op      call    #popv
           mov     loc,#l110
           add     loc,adr
           jmp     loc
l110       abs     arg1,st            '||
           jmp     #com11
'--------------   
           mov     arg1,st
           xor     arg1,allones       'compl
           jmp     #com11
'--------------
           neg     arg1,st            'neg
           jmp     #com11
'--------------
           mov     st,st   nr,wz,wc   'G
     if_a  mov     arg1,#1
     if_be mov     arg1,#0
           jmp     #com11
'--------------
           mov     st,st   nr,wz,wc   'Z
     if_z  mov     arg1,#1
     if_nz mov     arg1,#0  
           jmp     #com11
'--------------
           mov     arg1,ina           'inpin
           shr     arg1,st
           and     arg1,#1
'--------------
com11      add     sa,#1
           movd    i11,sa
           nop
i11        mov     stack,arg1
           jmp     #nextinstr
'---------------------------------------
'   0 inall, 2 now
'---------------------------------------
l01op      mov      loc,#l010
           add      loc,adr
           jmp      loc
l010       mov      arg1,ina     'inall
           jmp      #com01
'--------------
           mov      arg1,cnt     'now
com01      add      sa,#1
           movd     i01,sa
           nop
i01        mov      stack,arg1
           jmp      #nextinstr
'-----------------------------------------
'   0 wait, 4 setdir
'-----------------------------------------
l10op      call     #popv
           mov      loc,#l100
           add      loc,adr
           jmp      loc
l100       min      st,#120      'wait
           add      st,cnt
           waitcnt  st,#0
           jmp      #nextinstr
'--------------
           or       dira,st
           jmp      #nextinstr
'-----------------------------------------
'   0 outpins
'-----------------------------------------
l20op      call     #popv
           mov      arg1,st      
           call     #popv
           and      st,arg1
           or       outa,st      'outpins
           xor      arg1,allones
           or       st,arg1
           and      outa,st
           jmp      #nextinstr
           
'----------------------------------------
'    0 send
'----------------------------------------
'l30op      call      #popv
'           mov       pulse,st
'           call      #popv
'           mov       arg0,st
'           call      #popv
'
'           mov       arg1,#1
'           shl       arg1,arg0
'           mov       arg2,arg1
'           xor       arg2,allones
'
'           and       st,#255
'           shl       st,#1
'           or        st,startbit
'           mov       nbits,#10
'           mov       time,cnt
'           add       time,pulse
'sendloop   and       st,#1       nr,wz
'    if_z   and       outa,arg2
'    if_nz  or        outa,arg1
'           waitcnt   time,pulse
'           shr       st,#1
'           djnz      nbits,#sendloop
'           add       time,space
'           waitcnt   time,#0
'           jmp       #nextinstr

nextinstr  add  iadr,#1
           jmp  #loop

'----------------------------------------
popv       movs  ipopv,sa
           nop
ipopv      mov   st,stack
           sub   sa,#1
popv_ret   ret            
'---------------------------------

cndtable   long  $B145AF
cond       mov   arg1,instr
           shr   arg1,#22
           and   arg1,#28
'if_z       mov   sat,#1
'if_z       jmp   cond_ret
           mov   arg2,statstack
           and   arg2,#3
           or    arg2,arg1
           mov   sat,#1
           shl   sat,arg2
           and   sat,cndtable
cond_ret   ret
