;----------------------------------------------------------------------------- ; cc.asm ; Copyright 1994, 1995 Eric Smith & Richard Ottosen ; ; Closed Caption Decoder using PIC 16C61 running at 16 MHz ; requires LM1881 sync separator or equivalent ; ; cc.asm is free software; you can redistribute it and/or modify it under the ; terms of the GNU General Public License version 2 as published by the Free ; Software Foundation. Note that I am not granting permission to redistribute ; or modify dtmf.asm under the terms of any later version of the General Public ; License. ; ; This program is distributed in the hope that it will be useful (or at least ; amusing), but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General ; Public License for more details. ; ; You should have received a copy of the GNU General Public License along with ; this program (in the file "COPYING"); if not, write to the Free Software ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ; ; $Header: /usr/home/kolwynia/eric/pic/cc/RCS/cc.asm,v 1.15 1995/08/21 01:05:15 eric Exp eric $ ;----------------------------------------------------------------------------- device pic16c61,hs_osc,wdt_off,protect_off f equ 1 ; for destination argument w equ 0 ; for destination argument ind equ 0 ; used for indirects thru fsr rtcc equ 1 ; real time clock/counter pc equ 2 ; program counter status equ 3 ; status register: cf equ 0 ; bit 0 = carry bit flag dcf equ 1 ; bit 1 = digit carry bit flag zf equ 2 ; bit 2 = zero bit flag pdf equ 3 ; bit 3 = power down bit flag tof equ 4 ; bit 4 = time out bit flag rs0 equ 5 ; bit 5 = bank select for pic16c84 ; bits 6,7 do not apply to pic16c84 fsr equ 4 ; file select register (index register) ; ascii values bel equ 07h ; bell bs equ 08h ; back space tab equ 09h ; horizontal tab lf equ 0ah ; line feed ff equ 0ch ; form feed cr equ 0dh ; carriage return xon equ 11h ; transmit on xoff equ 13h ; transmit off eof equ 1ah ; end of file esc equ 1bh ; escape sp equ 20h ; space porta equ 5 ; i/o port a: data equ 0 ; bit 0 = data input Peak equ 1 ; bit 1 = Active low reset for peak detect ; (Occurs during line 21) Restore equ 2 ; bit 2 = Back porch DC restore pulse csync equ 3 ; bit 3 = composite sync input from sync ; stripper (active low) ; Bit 4 = input (tied to ground) ;*** this does not work with ; bit 4 is tied to bit 3 in order to allow ;*** PIC16C71 having PA4 bug ; RTCC to count sync pulses portb equ 6 ; i/o port b: Pump equ 0 ; bit 0 = Charge pump drive for RS232 -4 Volts txd equ 1 ; bit 2 = rs232 serial data output led1 equ 2 ; bit 2 = led debug: led2 equ 3 ; bit 3 = led debug: field signal led3 equ 4 ; bit 4 = Active caption LED pzt equ 4 ; bit 4 = pzt speaker output button0 equ 5 ; bit 5 = next mode button input button1 equ 6 ; bit 6 = next mode button input button2 equ 7 ; bit 7 = next mode button input ; (active low) ;----------------------------------------------------------------------------- ; ram ;----------------------------------------------------------------------------- rambase equ 0ch ; start of ram org rambase count ds 1 ; general purpose counter dlycnt ds 1 ; counter for delays (used in several places) mscnt ds 1 ; number of milliseconds using delay routine cycles ds 1 ; number of cycles in a beep period ds 1 ; period of a click temp ds 1 ; very temporary storage temp2 ds 1 serreg ds 1 ; serial output character buffer bitcnt ds 1 ; serial output bit count speed ds 1 ; serial speed mflags ds 1 ; misc. flags lazycr equ 0 ; bit 0 = lazy cr pending lastbit equ 1 ; bit 1 = last bit (used in getsbit) field equ 2 ; bit 2 = odd field sidx8 ds 1 sidx1 ds 1 trcnt ds 1 sample ds 14 ; 112 samples at 2 MHz. ; Closed caption is just under 104 samples long. ; Extra 8 samples allow some slop for delay to ; start of run-in. odata ds 2 ;----------------------------------------------------------------------------- ; reset and interrupt vectors ;----------------------------------------------------------------------------- org 0 goto reset nop nop nop goto reset ; interrupts not used ;----------------------------------------------------------------------------- ; utilities ;----------------------------------------------------------------------------- ; delay for w *1ms at 4mhz osc. freq. delay movwf mscnt ; count milliseconds dly10 movlw 250 movwf dlycnt dly20 decf dlycnt ; 1 cycle btfss status,zf ; 1 goto dly20 ; 2 =4 cycles decfsz mscnt ; last msec? goto dly10 return ; make a "bell" sound beep movlw 200 movwf cycles movlw 254 ; a nice tone??? movwf period ; (fall into "click") ; make a "click" sound "cycles" number of times. ; frequency = 1 /(1 /(osc frequency /4) *5 cycles *period *2) click bsf portb,pzt ; click high movf period,w ; time for one half of cycle movwf dlycnt ; into delay counter clk10 goto clk15 ; (burn 2 cycles) 2 cycles clk15 decfsz dlycnt ; 1 goto clk10 ; 2= 5 cycles bcf portb,pzt ; click low movf period,w ; time for one half of cycle movwf dlycnt ; into delay counter clk20 goto clk25 ; (burn 2 cycles) 2 cycles clk25 decfsz dlycnt ; 1 goto clk20 ; 2= 5 cycles decfsz cycles ; another cycle of click? goto click ; branch if so return ;----------------------------------------------------------------------------- ; output a byte in binary ;----------------------------------------------------------------------------- outbb: movwf temp movlw 8 movwf temp2 outbb1: movlw '0' btfsc temp,7 addlw 1 call xmit rlf temp decfsz temp2 goto outbb1 return ;----------------------------------------------------------------------------- ; output a byte in hexadecimal ;----------------------------------------------------------------------------- outhb: movwf temp swapf temp,w call outhd movf temp,w ; fall into outhd ;----------------------------------------------------------------------------- ; output a hex digit ;----------------------------------------------------------------------------- outhd: andlw 0fh addlw 0f6h btfsc status,cf addlw 07h addlw 3ah ; fall into xmit ;----------------------------------------------------------------------------- ; serial output ;----------------------------------------------------------------------------- ;Transmit character in W-Reg as 8 bits, no parity, 1 stop. ; This routine has the start, stop and data bits non-inverted because an ; inverting RS-232 buffer is used. xmit BCF PortB,Pump ; Pump some charge into negative 4 Volt supply movwf serreg ; save character movlw 10 ; put # of data bits + start bit + # stop bits movwf bitcnt ; into counter bsf status,cf ; set up the stop bit bcf portb,txd ; send start bit xmt10 movf speed,w ; delay 1 bit time movwf dlycnt BSF PortB,Pump ; Pump some charge into negative 4 Volt supply xd1 nop ; 1 cycle decfsz dlycnt ; 1 goto xd1 ; 2 =4 cycles BCF PortB,Pump ; Pump some charge into negative 4 Volt supply ;nop ; make sure overhead is an even multiple of ;nop ; xd1 loop time. 16 cycles in this case. ; (no adjustment needed with charge pumping). decf bitcnt ; count the data bits btfsc status,zf goto xmt30 ; and exit when done rrf serreg ; get data bit into carry btfsc status,cf ; if carry is set bsf portb,txd ; then xmit a zero btfss status,cf ; if carry is clear bcf portb,txd ; then transmit a one goto xmt10 xmt30 BSF PortB,Pump ; Pump some charge into negative 4 Volt supply return ; end of "xmit" ;----------------------------------------------------------------------------- ; initialize the hardware ;----------------------------------------------------------------------------- inithw: clrwdt ; reset watchdog timer movlw 08h ; external edge to timer option ; high to low edge for timer ; prescaler assigned to watchdog ; prescaler divide by 128 for now RestOn EQU 0FBh ; PortA with the DC Restore clamp on PeakOn EQU 0FDh ; PortA with the Peak detect reset on ClampsOff EQU 0FFh ; PortA with both Peak reset and Restore clamp ; off and not clamping. MOVLW ClampsOff ; PortA bits 0 and 3 are inputs tris porta BCF PortA,Restore ; Use both these pins as open collector outputs BCF PortA,Peak MOVLW 0E0h ; PortB bits 0 through 4 are outputs tris portb clrf portb ; 0 volts on speaker movlw 208-4 ; 4800 bps w/ 16.000 MHz crystal movwf speed return ;----------------------------------------------------------------------------- ; get data samples for the full scan line ;----------------------------------------------------------------------------- getsamp: rrf porta,w ; get first bit of sample+0 rlf sample+0 rrf porta,w ; get second bit of sample+0 rlf sample+0 rrf porta,w ; get third bit of sample+0 rlf sample+0 rrf porta,w ; get fourth bit of sample+0 rlf sample+0 rrf porta,w ; get fifth bit of sample+0 rlf sample+0 rrf porta,w ; get sixth bit of sample+0 rlf sample+0 rrf porta,w ; get seventh bit of sample+0 rlf sample+0 rrf porta,w ; get eighth bit of sample+0 rlf sample+0 rrf porta,w ; get first bit of sample+1 rlf sample+1 rrf porta,w ; get second bit of sample+1 rlf sample+1 rrf porta,w ; get third bit of sample+1 rlf sample+1 rrf porta,w ; get fourth bit of sample+1 rlf sample+1 rrf porta,w ; get fifth bit of sample+1 rlf sample+1 rrf porta,w ; get sixth bit of sample+1 rlf sample+1 rrf porta,w ; get seventh bit of sample+1 rlf sample+1 rrf porta,w ; get eighth bit of sample+1 rlf sample+1 rrf porta,w ; get first bit of sample+2 rlf sample+2 rrf porta,w ; get second bit of sample+2 rlf sample+2 rrf porta,w ; get third bit of sample+2 rlf sample+2 rrf porta,w ; get fourth bit of sample+2 rlf sample+2 rrf porta,w ; get fifth bit of sample+2 rlf sample+2 rrf porta,w ; get sixth bit of sample+2 rlf sample+2 rrf porta,w ; get seventh bit of sample+2 rlf sample+2 rrf porta,w ; get eighth bit of sample+2 rlf sample+2 rrf porta,w ; get first bit of sample+3 rlf sample+3 rrf porta,w ; get second bit of sample+3 rlf sample+3 rrf porta,w ; get third bit of sample+3 rlf sample+3 rrf porta,w ; get fourth bit of sample+3 rlf sample+3 rrf porta,w ; get fifth bit of sample+3 rlf sample+3 rrf porta,w ; get sixth bit of sample+3 rlf sample+3 rrf porta,w ; get seventh bit of sample+3 rlf sample+3 rrf porta,w ; get eighth bit of sample+3 rlf sample+3 rrf porta,w ; get first bit of sample+4 rlf sample+4 rrf porta,w ; get second bit of sample+4 rlf sample+4 rrf porta,w ; get third bit of sample+4 rlf sample+4 rrf porta,w ; get fourth bit of sample+4 rlf sample+4 rrf porta,w ; get fifth bit of sample+4 rlf sample+4 rrf porta,w ; get sixth bit of sample+4 rlf sample+4 rrf porta,w ; get seventh bit of sample+4 rlf sample+4 rrf porta,w ; get eighth bit of sample+4 rlf sample+4 rrf porta,w ; get first bit of sample+5 rlf sample+5 rrf porta,w ; get second bit of sample+5 rlf sample+5 rrf porta,w ; get third bit of sample+5 rlf sample+5 rrf porta,w ; get fourth bit of sample+5 rlf sample+5 rrf porta,w ; get fifth bit of sample+5 rlf sample+5 rrf porta,w ; get sixth bit of sample+5 rlf sample+5 rrf porta,w ; get seventh bit of sample+5 rlf sample+5 rrf porta,w ; get eighth bit of sample+5 rlf sample+5 rrf porta,w ; get first bit of sample+6 rlf sample+6 rrf porta,w ; get second bit of sample+6 rlf sample+6 rrf porta,w ; get third bit of sample+6 rlf sample+6 rrf porta,w ; get fourth bit of sample+6 rlf sample+6 rrf porta,w ; get fifth bit of sample+6 rlf sample+6 rrf porta,w ; get sixth bit of sample+6 rlf sample+6 rrf porta,w ; get seventh bit of sample+6 rlf sample+6 rrf porta,w ; get eighth bit of sample+6 rlf sample+6 rrf porta,w ; get first bit of sample+7 rlf sample+7 rrf porta,w ; get second bit of sample+7 rlf sample+7 rrf porta,w ; get third bit of sample+7 rlf sample+7 rrf porta,w ; get fourth bit of sample+7 rlf sample+7 rrf porta,w ; get fifth bit of sample+7 rlf sample+7 rrf porta,w ; get sixth bit of sample+7 rlf sample+7 rrf porta,w ; get seventh bit of sample+7 rlf sample+7 rrf porta,w ; get eighth bit of sample+7 rlf sample+7 rrf porta,w ; get first bit of sample+8 rlf sample+8 rrf porta,w ; get second bit of sample+8 rlf sample+8 rrf porta,w ; get third bit of sample+8 rlf sample+8 rrf porta,w ; get fourth bit of sample+8 rlf sample+8 rrf porta,w ; get fifth bit of sample+8 rlf sample+8 rrf porta,w ; get sixth bit of sample+8 rlf sample+8 rrf porta,w ; get seventh bit of sample+8 rlf sample+8 rrf porta,w ; get eighth bit of sample+8 rlf sample+8 rrf porta,w ; get first bit of sample+9 rlf sample+9 rrf porta,w ; get second bit of sample+9 rlf sample+9 rrf porta,w ; get third bit of sample+9 rlf sample+9 rrf porta,w ; get fourth bit of sample+9 rlf sample+9 rrf porta,w ; get fifth bit of sample+9 rlf sample+9 rrf porta,w ; get sixth bit of sample+9 rlf sample+9 rrf porta,w ; get seventh bit of sample+9 rlf sample+9 rrf porta,w ; get eighth bit of sample+9 rlf sample+9 rrf porta,w ; get first bit of sample+10 rlf sample+10 rrf porta,w ; get second bit of sample+10 rlf sample+10 rrf porta,w ; get third bit of sample+10 rlf sample+10 rrf porta,w ; get fourth bit of sample+10 rlf sample+10 rrf porta,w ; get fifth bit of sample+10 rlf sample+10 rrf porta,w ; get sixth bit of sample+10 rlf sample+10 rrf porta,w ; get seventh bit of sample+10 rlf sample+10 rrf porta,w ; get eighth bit of sample+10 rlf sample+10 rrf porta,w ; get first bit of sample+11 rlf sample+11 rrf porta,w ; get second bit of sample+11 rlf sample+11 rrf porta,w ; get third bit of sample+11 rlf sample+11 rrf porta,w ; get fourth bit of sample+11 rlf sample+11 rrf porta,w ; get fifth bit of sample+11 rlf sample+11 rrf porta,w ; get sixth bit of sample+11 rlf sample+11 rrf porta,w ; get seventh bit of sample+11 rlf sample+11 rrf porta,w ; get eighth bit of sample+11 rlf sample+11 rrf porta,w ; get first bit of sample+12 rlf sample+12 rrf porta,w ; get second bit of sample+12 rlf sample+12 rrf porta,w ; get third bit of sample+12 rlf sample+12 rrf porta,w ; get fourth bit of sample+12 rlf sample+12 rrf porta,w ; get fifth bit of sample+12 rlf sample+12 rrf porta,w ; get sixth bit of sample+12 rlf sample+12 rrf porta,w ; get seventh bit of sample+12 rlf sample+12 rrf porta,w ; get eighth bit of sample+12 rlf sample+12 rrf porta,w ; get first bit of sample+13 rlf sample+13 rrf porta,w ; get second bit of sample+13 rlf sample+13 rrf porta,w ; get third bit of sample+13 rlf sample+13 rrf porta,w ; get fourth bit of sample+13 rlf sample+13 rrf porta,w ; get fifth bit of sample+13 rlf sample+13 rrf porta,w ; get sixth bit of sample+13 rlf sample+13 rrf porta,w ; get seventh bit of sample+13 rlf sample+13 rrf porta,w ; get eighth bit of sample+13 rlf sample+13 return ;----------------------------------------------------------------------------- ; get a single sample bit from the sample buffer ; return it in the C flag ; return with Z flag set if out of bits ;----------------------------------------------------------------------------- getsbit: bcf status,cf ; advance bit position rrf sidx1 btfss status,cf goto getsb2 movlw 80h movwf sidx1 incf sidx8 movf sidx8,w ; test for end of buffer xorlw sample+14 btfss status,zf goto getsb2 bsf status,zf ; out of bits return getsb2: movf sidx8,w movwf fsr movf ind,w andwf sidx1,w ; polarity of raw sample data is inverted, so zero flag ends up true btfsc status,zf goto getsb1 btfsc mflags,lastbit ; got a zero bit, increment trcnt if lastbit incf trcnt ; was a one bcf mflags,lastbit bcf status,cf bcf status,zf return getsb1: btfss mflags,lastbit ; got a one bit, increment trcnt if lastbit incf trcnt ; was a zero bsf mflags,lastbit bsf status,cf bcf status,zf return ;----------------------------------------------------------------------------- ; check parity ;----------------------------------------------------------------------------- parity: movwf temp movlw 8 movwf bitcnt clrw par1: xorwf temp,w rrf temp decfsz bitcnt goto par1 andlw 01h return ;----------------------------------------------------------------------------- ; output one character as ascii, or as in hexadecimal as [xx] if unprintable ;----------------------------------------------------------------------------- aout: andlw 07fh ; strip parity (sigh) movwf temp movf temp ; don't output nulls btfsc status,zf return addlw 0e0h ; values from 00 to 1f show hex btfss status,cf goto aouth movf temp,w ; 7f show hex xorlw 07fh btfsc status,zf goto aouth movf temp,w goto xmit aouth: movlw '[' call xmit movf temp,w call outhb movlw ']' goto xmit ;----------------------------------------------------------------------------- ; process the raw samples ;----------------------------------------------------------------------------- process: BCF PortB,Pump ; Pump some charge into negative 4 Volt supply bsf portb,led3 ; assume the worst clrf odata clrf odata+1 movlw sample-1 movwf sidx8 movlw 01h movwf sidx1 clrf trcnt ; transition counter bcf mflags,lastbit ; Find start of run-in WalkUp: call getsbit btfsc status,cf ; First sample must be a 0 if closed caption return movlw 6 ; Limit how far we look movwf bitcnt WU10 call getsbit btfsc status,cf goto pone ; Found the first one bit of the run-in decfsz bitcnt goto WU10 return ; Run-in not found... no closed caption ; look for the gap between the leadin and the start bit pone: movlw 6 ; count down zero bits movwf bitcnt pzero: call getsbit btfsc status,zf goto errno0 ; ran out of bits btfsc status,cf goto pone ; darn, it's a one, start counting all over decfsz bitcnt goto pzero BSF PortB,Pump ; Pump some charge into negative 4 Volt supply ; now find a one bit fone: call getsbit btfsc status,zf goto errno1 ; ran out of bits btfss status,cf goto fone ; darn, it's a zero, look some more BCF PortB,Pump ; Pump some charge into negative 4 Volt supply ; $$$ DEBUG - output transition count ; movf trcnt,w ; call outhb ; movlw ' ' ; goto xmit ; test the transition counter to make sure leadin was present tmin equ 10-1 ;****debug to allow for slow comparator (LM393) ;***tmin equ 14 tmax equ 16-1 movf trcnt,w addlw 256-tmin addlw 255+tmin-tmax btfsc status,cf return bcf portb,led3 ; indicate valid leadin and start bit found call getsbit ; skip the second and third samples btfsc status,zf ; of the start bit goto toofewbits call getsbit btfsc status,zf goto toofewbits movlw 16 movwf bitcnt pl: call getsbit ; skip three samples and get fourth btfsc status,zf goto toofewbits call getsbit btfsc status,zf goto toofewbits call getsbit btfsc status,zf goto toofewbits call getsbit btfsc status,zf goto toofewbits rrf odata+1 rrf odata decfsz bitcnt goto pl movf odata,w ; check parity of first byte call parity btfsc status,zf goto perror movf odata,w call parity btfsc status,zf goto perror movf odata,w ; strip parity from first byte andlw 7fh movwf odata movf odata+1,w ; strip parity from second byte andlw 7fh movwf odata+1 movf odata,w ; if the first byte is >= 20h, handle addlw 0e0h ; it normally btfsc status,cf goto p8 movf odata,w ; if the first byte isn't 14h, throw it xorlw 014h ; away btfss status,zf return movf odata+1,w ; if the second byte isn't 2dh, throw it xorlw 02dh ; away btfss status,zf return bsf mflags,lazycr ; remember to do a later return p8: btfss mflags,lazycr goto p9 bcf mflags,lazycr movlw cr call xmit movlw lf call xmit p9: movf odata,w call aout movf odata+1,w call aout return toofewbits: movlw 'B' goto error errno0: movlw '0' goto error errno1: movlw '1' goto error perror: movlw 'P' error: movwf temp movlw '[' call xmit movf temp,w call xmit movlw ']' call xmit return ;----------------------------------------------------------------------------- ; program entry point ;----------------------------------------------------------------------------- reset: call inithw ; initialize hardware call beep ; indicate special mode clrf mflags ; clear the misc. flags ;----------------------------------------------------------------------------- ; main loop ;----------------------------------------------------------------------------- main: call waiteq ; wait for a field movlw 22 ; wait for line 20 movwf temp wait21: ;Sync up to do DC restore wn1 btfss porta,csync ; if we're already in a sync pulse goto wn1 ; we have to wait for the next one BSF PortB,Pump ; Pump some charge into negative 4 Volt supply ws1: btfsc porta,csync ; wait for start of sync pulse goto ws1 BCF PortB,Pump ; Pump some charge into negative 4 Volt supply movlw 5 ; delay to end of hsync pulse hd1: addlw 0ffh btfss status,zf goto hd1 BCF PortA,Restore ; Make sure output is still low MOVLW RestOn ; Clamp video for DC restore tris porta ; Pulse 2uS in center of blanking NOP NOP NOP NOP NOP BSF PortB,Pump ; Pump some charge into negative 4 Volt supply MOVLW ClampsOff ; Turn the DC restore clamp off tris porta decfsz temp goto wait21 btfss mflags,field ; if it's not an odd field, try again goto main BCF PortA,Peak ; Make sure output is still low MOVLW PeakOn ; Reset peak detector tris porta ; Re-sync for accurate start of line 21 ws2: btfsc porta,csync ; wait for start of sync pulse goto ws2 BCF PortB,Pump ; Pump some charge into negative 4 Volt supply movlw 7 ; delay 7 uS to get within 7 samples of hd2 addlw 0ffh ; begining of start of run-in btfss status,zf goto hd2 NOP NOP NOP MOVLW ClampsOff ; Release peak detect clamp tris porta BSF portb,led1 ;***debug call getsamp BCF portb,led1 ;***debug call process BSF PortB,Pump ; Pump some charge into negative 4 Volt supply goto main ;----------------------------------------------------------------------------- ; sync separator that mimics the lm1881 logic (at 16mhz oscillator frequency). ;----------------------------------------------------------------------------- ; wait for the equilizing pulses waiteq movlw 10 ; if sync is active for more than 12us or so movwf temp ; then it is a serrated vertical pulse we00 btfss porta,csync ; wait for sync to be inactive goto we00 BSF PortB,Pump ; Pump some charge into negative 4 Volt supply we02 btfsc porta,csync ; wait for sync to be active goto we02 BCF PortB,Pump ; Pump some charge into negative 4 Volt supply we05 btfsc porta,csync ; is sync inactive yet? goto we10 ; yes, it must really be an hsync or equal. decfsz temp ; has it been too long? goto we05 ; no, keep watching it goto waiteq ; must be a serrated vertical pulse, start over we10 BSF PortB,Pump ; Pump some charge into negative 4 Volt supply movlw 38 ; time for more than half of a line movwf temp ; (about 47us /250ns = 188 cycles) we20 btfss porta,csync ; sync active? 2 goto we30 ; yes, found an equalizing pulse 0 decfsz temp ; no, out of time? 1 goto we20 ; no, keep watching for it 2 =5 goto we02 ; it's more than half a line, try again we30 btfss porta,csync ; wait for sync to be inactive goto we30 ; we've now found either the second equalizing pulse of an odd field, or the ; first equalizing pulse of an even field, but we don't yet know which. ; fall into code to separate the vertical sync ;----------------------------------------------------------------------------- ; separate the vertical sync and determine which field we're in ;----------------------------------------------------------------------------- clrf count movlw 12 ; time for half of a wide equalizing space movwf temp ; (about 16us / 250ns = 64 cycles) sv10 btfsc porta,csync ; wait for sync to be active goto sv10 BCF PortB,Pump ; Pump some charge into negative 4 Volt supply ; now measure the width of the pulse incf count sv20 btfsc porta,csync ; sync still active? 2 cycle goto sv10 ; no, it was an equalizing pulse 0 decfsz temp ; yes, has it been wide enough? 1 goto sv20 ; no, keep watching it 2 =5 ; now we've gotten a wide (vertical) sync pulse sv30 btfss porta,csync ; wait for sync to go inactive goto sv30 BSF PortB,Pump ; Pump some charge into negative 4 Volt supply ; the count of equalizing pulses preceding the first vetical sync pulse ; determines whether it is an even or odd field bcf mflags,field btfsc count,0 ; copy "temp" lsb as odd/even field bsf mflags,field ;**** debug btfsc count,0 ;Field signal bsf portb,led2 btfss count,0 bcf portb,led2 return ; return with sync inactive end