'-----------------------------------------
'==/10
div10         call   #popv
              mov    arg1,st
              call   #mul102
              sar    arg2,#10
              mov    y10,arg2
idiv10a       call   #mul10
              mov    r10,st
              sub    r10,arg1
              sub    r10,#10   nr,wc,wz
      if_b    jmp    #idiv10b
              mov    arg1,r10
              add    arg1,#1
              call   #mul102
              sar    arg2,#10
              add    y10,arg2
              mov    arg2,y10
              jmp    #idiv10a
idiv10b       add    sa,#1
              movd   idiv10c,sa
              nop
idiv10c       mov    stack,r10
              add    sa,#1
              movd   idiv10d,sa
              nop
idiv10d       mov    stack,y10
              jmp    #nextinstr

mul102        shl    arg1,#1
              mov    arg2,arg1
              shl    arg1,#1
              add    arg2,arg1
              shl    arg1,#3
              add    arg2,arg1
              shl    arg1,#1
              add    arg2,arg1
mul102_ret    ret

mul10         shl    arg2,#1
              mov    arg1,arg2
              shl    arg2,#2
              add    arg1,arg2
mul10_ret     ret

y10           long 0
r10           long 0

'--------------------------------------------------
'==bit
ibit          call          #popv
              mov           arg1,st    'adress
              call          #popv
              mov           arg2,st    'ibit
bitloop       sub           arg2,#32     nr,wc
    if_ae     sub           arg2,#32
    if_ae     add           arg1,#1
    if_ae     jmp           #bitloop
              movs          ibit1,arg1
              nop
ibit1         mov           arg0,arg1
              shl           arg0,arg2
              and           arg0,msbmask
              shr           arg0,#31
              add           sa,#1
              movd          ibit2,sa
              nop
ibit2         mov           stack,arg0
              jmp           #nextinstr 
msbmask       long   $80000000          

'--------------------------------------------------
'==set1
set1          call              #popv
              mov               arg1,#1
              shl               arg1,st
              or                outa,arg1
              jmp               #nextinstr

'==set0
set0          call              #popv
              mov               arg1,#1
              shl               arg1,st
              xor               arg1,allones
              and               outa,arg1
              jmp               #nextinstr

'==setout
setout        call              #popv
              mov               arg1,#1
              shl               arg1,st
              or                dira,arg1
              jmp               #nextinstr

'==setin
setin         call              #popv
              mov               arg1,#1
              shl               arg1,st
              xor               arg1,allones
              and               dira,arg1
              jmp               #nextinstr

'--------------------------------------------------
'==sin
sin           call              #popv
              mov               arg1,st
              shr               arg1,#18
              and               arg1,sinmask
              or                arg1,sintable
              rdword            arg2,arg1
              add               sa,#1
              movd              isin,sa
              nop
isin          mov               stack,arg2
              jmp               #nextinstr
sintable      long  $E000
sinmask       long  $1FFF
'-------------------------------------------------
'==initnext
initnext      call              #popv
              mov               xmax,st
              jmp               #nextinstr
xmax          long  0

' ix iy /next/ ->ix ->iy

'==next
next          call              #popv
              mov               arg2,st
              call              #popv
              mov               arg1,st
              add               arg1,#1
              sub               arg1,xmax   nr,wz
        if_z  mov               arg1,#0
        if_z  add               arg2,#1
              add               sa,#1
              movd              inext1,sa
              add               sa,#1
              movd              inext2,sa
inext1        mov               stack,arg2
inext2        mov               stack,arg1              
              jmp               #nextinstr

'-------------------------------------------------
'==init_analog
init_analog   or    dira,clk
              or    dira,ncs
              or    dira,din
              and   dira,zdout
              or    outa,ncs
              jmp   #nextinstr
'-------------------------------------------
'==analog
analog        call      #popv
              mov       ich,st
              call      #convchm
              add       sa,#1
              movd      ianalog,sa
              nop
ianalog       mov       stack,xanal
              jmp       #nextinstr

'    A/D conversion from analog channel ich 
convchm       nop
              mov       xanal,#0
              mov       mode,ich
              shl       mode,#2
              or        mode,#$60
              and       outa,zncs
              and       outa,zclk

              mov       nin,#6                 
:inloop       nop
              and       mode,#|<6     nr,wz
        if_z  and       outa,zdin
        if_nz or        outa,din
              mov       ctime,cnt
              add       ctime,dtan
              waitcnt   ctime,dtan
              or        outa,clk
              waitcnt   ctime,dtan
              and       outa,zclk
              shl       mode,#1
              djnz      nin,#:inloop

              and       outa,zncs
              or        outa,clk
                                        
              mov       nout,#12
:outloop      nop
              waitcnt   ctime,dtan
              and       outa,zclk
              waitcnt   ctime,dtan
              mov       aout,ina
              shr       aout,outpin
              and       aout,#1
              shl       xanal,#1
              or        xanal,aout
              or        outa,clk
              djnz      nout,#:outloop
              
              or        outa,ncs
              waitcnt   ctime,dtan
convchm_ret   ret                        
                          
outpin  long  1
dout    long  |<1
zdout   long  !|<1
din     long  |<2
zdin    long  !|<2
ncs     long  |<3
zncs    long  !|<3
clk     long  |<0
zclk    long  !|<0
ich     long  0
aout    long  0
xanal   long  0
d       long  0
ctime   long  0
mode    long  0
dtan    long  50
nin     long  0
nout    long  0
'--------------------------------------------
'==paninit
paninit       call      #popv
              mov       pandata,st
              call      #popv
              mov       paamask,#7
              shl       paamask,st
              mov       zpaamask,paamask
              xor       zpaamask,allones
              mov       paa0mask,#1
              shl       paa0mask,st
              mov       zpaa0mask,paa0mask
              xor       zpaa0mask,allones
              call      #popv
              mov       intpin,#1
              shl       intpin,st
              call      #popv
              mov       rdpin,#1
              shl       rdpin,st
              mov       zrdpin,rdpin
              xor       zrdpin,allones
              call      #popv
              mov       cspin,#1
              shl       cspin,st
              mov       zcspin,cspin
              xor       zcspin,allones
              or        dira,cspin
              or        dira,rdpin
              or        dira,paamask
              and       outa,zpaamask
              jmp       #nextinstr

'-----------------------------------------
'==panreset
panreset      call      #popv
              mov       pats,st
              call      #popv
              mov       arg1,st    nr,wz,wc
      if_nz   or        outa,paa0mask
      if_z    and       outa,zpaa0mask
              call      #popv
              mov       paadress,par
              shl       paadress,#2
              add       paadress,st
              mov       maxadress,paadress
              add       maxadress,pandata
              jmp       #nextinstr
'------------------------------------------
'==panalog
panalog       and       outa,zcspin
              and       outa,zrdpin
              mov       pats,pats            nr,wz
              waitpeq   zero,intpin   nr
              mov       arg1,ina
              wrbyte    arg1,paadress
              add       paadress,#1
    if_z      jmp       #paskip
    if_nz     mov       arg2,cnt
    if_nz     add       arg2,pats
    if_nz     waitcnt   arg2,#0
paskip        or        outa,rdpin
              or        outa,cspin
              sub       paadress,maxadress   nr,wc,wz
   if_b       jmp       #panalog
              jmp       #nextinstr
paadress   long  0
pandata    long  0
maxadress  long  0
pats       long  0
paamask    long  0
zpaamask   long  0
paa0mask   long  0
zpaa0mask  long  0
intpin     long  0
rdpin      long  0
zrdpin     long  0
cspin      long  0
zcspin     long  0
'--------------------------------------------
'==pdigit
pdigit        mov       arg1,ina
              shr       arg1,#16
              and       arg1,#63
              wrbyte    arg1,paadress
              add       paadress,#1
              mov       pats,pats    nr,wz
    if_z      jmp       #pdskip
    if_nz     mov       arg2,cnt
    if_nz     add       arg2,pats
    if_nz     waitcnt   arg2,#0
pdskip        sub       paadress,maxadress   nr,wc,wz
    if_b      jmp       #pdigit
              jmp       #nextinstr

'--------------------------------------------
'==pwminit
pwminit  mov       pwmadr,dadr
         add       pwmadr,par
         call      #popv
         mov       pwmmask,#1
         shl       pwmmask,st
         mov       zpwmmask,pwmmask
         xor       zpwmmask,allones
         or        dira,pwmmask
         jmp       #nextinstr

'---------------------------------------------
'==pwm
pwm      rdlong    arg1,pwmadr
         or        outa,pwmmask
         mov       tpwm,cnt
         add       tpwm,arg1
         waitcnt   tpwm,#0
         and       outa,zpwmmask
         mov       tpwm,cnt
         add       tpwm,c1024
         sub       tpwm,arg1
         waitcnt   tpwm,#0
         jmp       #pwm
         jmp       #nextinstr

pwmadr   long 0
pwmmask  long 0
zpwmmask long 0
tpwm     long 0
c1024    long 1024

'-------------------------------------------------
'==send
send          call      #popv
              mov       sendpulse,st
              call      #popv
              mov       sendpin,st
              call      #popv
              mov       xsend,st

              mov       sendmask,#1
              shl       sendmask,sendpin
              mov       sendzmask,sendmask
              xor       sendzmask,allones

              and       xsend,#255
              shl       xsend,#1
              or        xsend,startbit
              mov       nsend,#10
              mov       sendtime,cnt
              add       sendtime,sendpulse
sendloop      and       xsend,#1       nr,wz
       if_z   and       outa,sendzmask
       if_nz  or        outa,sendmask
              waitcnt   sendtime,sendpulse
              shr       xsend,#1
              djnz      nsend,#sendloop
              add       sendtime,sendspace
              waitcnt   sendtime,#0
              jmp       #nextinstr

sendpulse     long      0
sendspace     long      330
sendpin       long      0
sendmask      long      0
sendzmask     long      0
sendtime      long      0
nsend         long      0
xsend         long      0
startbit      long      512
'-------------------------------------------------
'==send32
send32        call      #popv
              mov       sendpin32,st
              call      #popv
              mov       xsend32,st

              mov       sendmask32,#1
              shl       sendmask32,sendpin32
              mov       sendzmask32,sendmask32
              xor       sendzmask32,allones
              mov       nsend32,#32

              mov       sendtime32,cnt
              add       sendtime32,sendpulse32
              and       outa,sendzmask32
              waitcnt   sendtime32,sendpulse32

send32loop    and       xsend32,#1       nr,wz
       if_z   and       outa,sendzmask32
       if_nz  or        outa,sendmask32
              waitcnt   sendtime32,sendpulse32
              shr       xsend32,#1
              djnz      nsend32,#send32loop
              or        outa,sendmask32
              add       sendtime32,sendspace32
              waitcnt   sendtime32,#0
              jmp       #nextinstr

sendpulse32   long      160
sendspace32   long      300
sendpin32     long      0
sendmask32    long      0
sendzmask32   long      0
sendtime32    long      0
nsend32       long      0
xsend32       long      0

'------------------------------------------
'==receive32
receive32     call      #popv
              mov       recpin32,st
              mov       recmask32,#1
              shl       recmask32,recpin32
              mov       recdata32,#0
recwait032    mov       pins32,ina
              and       pins32,recmask32  wz
        if_z  jmp       #recwait032
recwait132    mov       pins32,ina
              and       pins32,recmask32  wz
        if_nz jmp       #recwait132
        
              mov       recn32,#33
              mov       recdt32,recpulse32
              sar       recdt32,#1
              mov       rectime32,cnt
              add       rectime32,recdt32
recloop32     waitcnt   rectime32,recpulse32
              mov       pins32,ina
              and       pins32,recmask32
              shr       pins32,recpin32
              shl       pins32,#31            
              shr       recdata32,#1
              or        recdata32,pins32
              djnz      recn32,#recloop32
              add       sa,#1
              movd      irec32,sa
              nop
irec32        mov       stack,recdata32
              jmp       #nextinstr

pins32        long 0
recpulse32    long 160
recpin32      long 0
recmask32     long 0
recdata32     long 0
recdt32       long 0
rectime32     long 0 
recn32        long 0
'--------------------------------------------
'==spiinit
spiinit       call      #popv
              mov       spicmask,#1
              shl       spicmask,st
              mov       zspicmask,spicmask
              xor       zspicmask,allones
              call      #popv
              mov       spidomask,#1
              shl       spidomask,st
              mov       zspidomask,spidomask
              xor       zspidomask,allones
              call      #popv
              mov       spidimask,#1
              shl       spidimask,st
              jmp       #nextinstr

spicmask      long   0
zspicmask     long   0
spidomask     long   0
zspidomask    long   0
spidimask     long   0
nspi          long   0
tspi          long   0
dtspi         long   200
'---------------------------------------------
'==spisend
spisend       call   #popv
              mov    arg1,st
              mov    nspi,#8
ispis         and    outa,zspicmask
              and    arg1,#128     nr,wz
   if_z       and    outa,zspidomask
   if_nz      or     outa,spidomask
'              call   #spiwait
              or     outa,spicmask
              shl    arg1,#1
'              call   #spiwait
              djnz   nspi,#ispis
              and    outa,zspicmask
              jmp    #nextinstr

'spiwait       mov      tspi,cnt
'              add      tspi,dtspi
'              waitcnt  tspi,#0
'spiwait_ret   ret
'---------------------------------              
'==spirec
spirec        mov    nspi,#8
              mov    arg2,#0
ispir         or     outa,spicmask
'              call   #spiwait
  '            and    outa,zspicmask
'              call   #spiwait
              mov    arg1,ina
              and    arg1,spidimask  wz
   if_nz      or     arg2,#1
              and    outa,zspicmask
              shl    arg2,#1
              djnz   nspi,#ispir
              shr    arg2,#1
              add    sa,#1
              movd   ispirec,sa
              nop
ispirec       mov    stack,arg2
              jmp    #nextinstr
'-----------------------------------
'==iicinit
iicinit       call #popv
              mov  clkmask,#1
              shl  clkmask,st
              mov  zclkmask,clkmask
              xor  zclkmask,allones
              call #popv
              mov  sdamask,#1
              shl  sdamask,st
              mov  zsdamask,sdamask
              xor  zsdamask,allones
              and  outa,zclkmask      '  Z
              or   dira,clkmask       '  0
              and  outa,zsdamask      '0
              and  dira,zsdamask      'Z
              jmp  #nextinstr

iicwait       mov  tiic,cnt
              add  tiic,dtiic
              waitcnt  tiic,#0
iicwait_ret   ret

iicwaitl      mov  tiic,cnt
              add  tiic,dtiicl
              waitcnt   tiic,#0
iicwaitl_ret  ret

clkmask       long 0
zclkmask      long 0
sdamask       long 0
zsdamask      long 0
tiic          long 0
dtiic         long 400
dtiicl        long 4000
'--------------------------------------
'==iicstart
iicstart      or   outa,clkmask       '  1
              call #iicwait
              or   dira,sdamask       '0
              call #iicwait
              and  outa,zclkmask      '  0
              and  dira,zsdamask      'Z
              jmp  #nextinstr
'--------------------------------------
'==iicstop
iicstop       or   dira,sdamask       '0
              or   outa,clkmask       '  1
              call #iicwait
              and  dira,zsdamask      'Z
              call #iicwait
              and  outa,zclkmask      '  0
              jmp  #nextinstr
'--------------------------------------
'==iicack
iicack        or   dira,sdamask       '0
              call #iicwait
              or   outa,clkmask       '  1
              call #iicwait
              and  outa,zclkmask      '  0
              and  dira,zsdamask      'Z
              jmp  #nextinstr
'--------------------------------------
'==iicnoack
iicnoack      call #iicwait
              or   outa,clkmask       '  1
              call #iicwait
              and  outa,zclkmask      '  0
              jmp  #nextinstr
'--------------------------------------
'==iicwack
iicwack       call #iicwait
              or   outa,clkmask       '  1
              call #iicwait
              and  outa,zclkmask      '  0
              jmp  #nextinstr
'--------------------------------------
'==iicwrite
iicwrite      call #popv
              mov  arg1,st
              mov  arg2,#8
iicwrite1     and  arg1,#128  nr,wz
      if_z    or   dira,sdamask       '0
      if_nz   and  dira,zsdamask      'Z
              call #iicwait
              or   outa,clkmask       '  1
              call #iicwait
              and  outa,zclkmask      '  0
              shl  arg1,#1
              djnz arg2,#iicwrite1
              and  dira,zsdamask      'Z
              jmp  #nextinstr
'---------------------------------------------
'==iicread
iicread       mov   arg1,#0
              mov   arg2,#8
iicread1      or    outa,clkmask      '  1
              call  #iicwait
              mov   arg0,ina
              and   arg0,sdamask  nr,wz,wz
              shl   arg1,#1
      if_nz   or    arg1,#1
              and   outa,zclkmask     '  0
              call  #iicwait
              djnz  arg2,#iicread1
              add   sa,#1
              movd  iicread2,sa
              nop              
iicread2      mov   stack,arg1
              jmp   #nextinstr
'-----------------------------------------------
'==stop
stop          cogid    arg1,#1
              cogstop  arg1,#3
              jmp      #nextinstr      

