; Main loop.  Pseudocode follows.
; #define PREAMBLE 0xd4
; #define MSGEND 0xaa
; 
; while(1){
;     if(!skip_collect){ 
;         (real, imag) = collect();
;     }
;     else { 
;         skip_collect = 0;
;         next;
;     }
;     adjust_gain(real, imag);
;     (real2, imag2) = collect();
;     bitv = demodulate(real, imag, real2, imag2);
;     onecount+=bitv;
;     if(bitv == 0) onecount = 0;
;     if(onecount > 4){
;         skip_collect = 1;
;     }
;     shift(curbyte, bitv);
;     if(curbyte == PREAMBLE){
;       msg_demod();
;     }
; }
; 
; #include "collect.src"
; #include "adjgain.src"
; #include "msg_demod.src"

; TODO:
;       Set bit directions
;       Set ports
                
                device pic16c71,hs_osc,wdt_off,pwrt_off,protect_off

        INCLUDE 'mheader.src'
RA3         =   ra.3
RB5         =   rb.5

               org     0h
               jmp      5
               org      4h
               jmp      doint
               org      5
               
            clr     PCLATH
            clrb    rp0
            mov     ADCON0, #10000001b
; Have to enable serial interrupts!!!
            mov	    INTCON, #00100000b	;RTCC, INT IRQ enabled
            call    dally
            setb    rp0
            mov     TRISA, #0011b
            mov     TRISB, #00000001b
            mov     OPTION, #00000011b  ;RTCC=inst,RB pull-up, /4   
            mov     ADCON1, #00000010b
            clrb    rp0
            mov     RTCC, #fullframe
            setb    waitb
            clrb    inmsg
            clr     ra
            clr     rb
            setb    TXSWTCH
            clrb    PTX
            clr     FSR
            setb    GIE

start       mov     RTCC, #fullframe
            setb    waitb
            clrb    inmsg
            mov     framect, #4        
main        nop
            jmp     main

    INCLUDE 'collect.src'
    INCLUDE 'adjgain.src'
    INCLUDE 'msgdemod.src'
    INCLUDE 'txmit.src'


; This is our RTCC overflow interrupt
doint       jb      intf, txint
            jb      rtif, rcvint
            
rcvint      mov     RTCC, #fullframe
            setb    TXSWTCH
            djnz    framect, intend
            mov     framect, #4
            jb      waitb, mloop
            jb      inmsg, msg_demod
intend      clrb    rtif
            reti

; Here's where we try to sync up...
mloop      jb      skipcollect, :didskip
            call    collect
            mov     real, byte1
            mov     imag, byte2
            call    adjgain
            call    collect
            mov     real2, byte1
            mov     imag2, byte2
            call    pskdemod
            jnb     bitv, :gotzero
            add     onect, #1
            cjae    onect, #3, :ones
            jmp     :next
:gotzero    mov     onect, #0
:next       movb    c, bitv
            rr      curbyte
            cje     curbyte, #preamble, :msginit
            reti
            
:didskip    clrb    skipcollect
            reti
            
:ones       setb    skipcollect
            reti

:msginit    setb    inmsg
            clrb    waitb
            reti
            