                device pic16c71,hs_osc,wdt_off,pwrt_off,protect_off
posthresh   =   160
negthresh   =   98
center      =   128
; A frame is 600 cycles.  A short frame is 375 cycles (already 225
; cycles after the 'good' value).  A frame is every four of the
; framecounts..
; So, a fullframe is (255-150)=55, which is 150*4 cycles = 600 cycles=150us
; And, a shortframe is (255-75)=180, and the framecount is down to 3, so...
fullframe   =   180      ; (255-180)= 75 (*4 (framect)) *2cyclesperRTCC =600 cycles = 150us
                        ; A short frame is a frame such that
                        ; (2*2*(255-fullframe)+(2*(255-shortframe)) = 525
shortframe  =   180    ; That is, 300+ 2*(255-shortframe) = 375; 175
adconversion = 31       ; this is in adwait loop which gets multiplied by 3
serial_k    =   50      ; 50 for 19,200 baud @16MHz
preamble    =   200     ; *** Change this

tcycles     =   25      ; 600 cycle Manchester bit (150us)
waitbitk    =   104     ; 1.5 bit cells
bitcell     =   66      ; delay for 1 bit cell

SERIN       =   rb.3
TX          =   rb.4
PTX         =   rb.5

            
            org     9h
curbyte     ds      1       ; mloop & main
onect       ds      1       ; mloop & main
txbitcount  ds      1;
;
real        ds      1       ; demod_byte
imag        ds      1       ; demod_byte
real2       ds      1       ; demod_byte
imag2       ds      1       ; demod_byte
byte1       ds      1    
; Memory 11-20
byte2       ds      1
;
quad        ds      1       ; demod_byte
imag_r      ds      1       ; demod_byte
real_r      ds      1       ; demod_byte
tempa       ds      1
tempb       ds      1
quad_a      ds      1
mybyte      ds      1
bytect      ds      1       ; demod_byte
; Memory 21-30
delay_cntr  ds      1       ; sout
bit_cntr    ds      1       ; sout
sine1       ds      1
sine2       ds      1
sine3       ds      1
cosine1     ds      1
cosine2     ds      1
cosine3     ds      1
xmt_byte    ds      1       ; sout
flags       ds      1;
count       ds      1;
framect     ds      1;
txbyte      ds      1;
txcount     ds      1;
tdelay_cntr ds      1;
bitv        =       flags.0
baudsync    =       flags.1
havesync    =       flags.2
skipcollect =       flags.3
inmsg       =       flags.4
waitb       =       flags.5


            org     0
            jmp     5
            org 4h            
doint       jmp     rcv
            org 5
            mov	    INTCON, #10101000b	;RTCC, INT IRQ enabled
            setb    rp0
            mov     OPTION, #00000000b	;RTCC=inst,RB pull-up, /16   
            clrb    rp0


            mov     RTCC, #fullframe
            setb    waitb
            clrb    inmsg
            mov     framect, #4
main        nop
            jmp     main
rcv         mov     RTCC, #fullframe
            djnz    framect, intend
            mov     framect, #4
            jb      waitb, mloop
            jb      inmsg, msg_demod
intend      clrb    rtif
            reti



mloop       clrb    baudsync
            call    collect
            call    adjgain
            mov     mybyte, byte1
            call    sout
            mov     mybyte, byte2
            call    sout
            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



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

                    