; This is the collect algorithm.  This does baud synchronization and
; actually gets an A/D value which is returned to be demodulated.
; The pseudocode follows.
;collect {
;    if(baudclock_sync){
;        do_wait();
;        byte1 = do_a_to_d(channel1);
;        byte2 = do_a_to_d(channel2);
;    }
;    else {
;        havebyte = 0;
;        nbyte = do_a_to_d(channel1);
;        push_ring(sinering, nbyte);
;        nbyte = do_a_to_d(channel2);
;        push_ring(cosring, nbyte);
;        nbyte = do_a_to_d(channel1);
;        push_ring(sinering, nbyte);
;        nbyte = do_a_to_d(channel2);
;        push_ring(cosring, nbyte);
;        while(!havebyte) {
;            nbyte = do_a_to_d(channel1);
;            push_ring(sinering, nbyte);
;            nbyte = do_a_to_d(channel2);
;            push_ring(cosring, nbyte);
;            if(drastic_change(cosring[last], cosring[last-1])){
;                byte2 = cosring[first];
;                byte1 = sinering[first];
;                set_baudclock();
;                baudclock_sync = 1;
;                havebyte = 1;
;            }
;            else if (drastic_change(sinering[last], sinering[last-1])){
;                byte2 = cosring[first];
;                byte1 = sinering[first];
;                set_baudclock();
;                baudclock_sync = 1;
;                havebyte = 1;
;            }
;        }
;    }
;    return (byte1, byte2);
;}
;

collect     setb    TXSWTCH
;            setb    RB5
;            jnb     baudsync, :acquire
;            clrb    RB5
; This is what we do if we already have a baudsync
            clrb    adcon0.3            ; Use the first quadraturechannel
;            setb    RB5    
;            mov     count, #waittime
;:waithere   djnz    count, :waithere
            setb    go_done             ; Start A/D conversion
;            clrb    RB5
            call    adwait
            mov     byte1, adres
            setb    adcon0.3            ; Switch which quadrature channel is being listened on
            setb    go_done             ; Start A/D conversion
            call    adwait
            mov     byte2, adres
;            setb    RB5
            nop
;            clrb    RB5
            nop
            nop
            nop
;            setb    RB5
            nop
;            clrb    RB5
            ret

:acquire    clrb    havesync
            clr     sine1
            clr     sine2
            clr     sine3
            clr     cosine1
            clr     cosine2
            clr     cosine3

            clrb    RB5
            nop
            setb    RB5
            clrb    RB5
            nop
            nop
            nop
            nop
            setb    RB5
            call    do_sample
            call    do_sample
:samploop   setb    RB5
            nop
            clrb    RB5
            nop
            setb    RB5
            nop
            nop
            clrb    RB5
            call    do_sample
            cjae    sine2, #posthresh, :foo_a
            cjb     sine2, #negthresh, :foo_b
:tryagain   cjae    cosine2, #posthresh, :foo_c
            cjb     cosine2, #negthresh, :foo_d
            jmp     :samploop
:foo_a      cjb     sine3, #negthresh, :bingo
            jmp     :tryagain
:foo_b      cjae    sine3, #posthresh, :bingo
            jmp     :tryagain
:foo_c      cjb     cosine3, #negthresh, :bingo
            jmp     :samploop
:foo_d      cjae    cosine3, #posthresh, :bingo
            jmp     :samploop
; *** We should do something here now that we have a 'drastic' change.
:bingo      mov     RTCC, #shortframe
            mov     byte1, sine1
            mov     byte2, cosine1
            setb    baudsync
            ret

do_sample   clrb    adcon0.3
            setb    go_done
            call    adwait
            mov     byte1, adres
            setb    adcon0.3
            setb    go_done
            call    adwait
            mov     byte2, adres
            call    sinshift
            call    cosshift
            mov     sine3, byte1
            mov     cosine3, byte2
            ret

sinshift    mov     sine2, sine1
            mov     sine3, sine2
            ret
            
cosshift    mov     cosine2, cosine1
            mov     cosine3, cosine2
            ret
            
adwait      mov     count, #adconversion
:adloop     djnz    count, :adloop
            ret


