; CLOCHPIC - Video routines
;
; 7 segment display routines (for compact character tables)


	TITLE	"Video"

	LIST	P=16C84
	NOLIST

; d
#Define	W	0
#Define	F	1

; PIC16C84 defines
	cblock	0x00
		Ind0,RTCC,PCL,Status,FSR,PortA,PortB
	endc
	cblock	0x08
		EEData,EEAdr,PCLatH,IntCon
	endc

; Status bits
	cblock	0x00
		C,DC,Z,PD,TO,RP0,RP1,IRP
	endc

; IntCon bits
	cblock	0x00
		RBIF,INTF,RTIF,RBIE,INTE,RTIE,EEIE,GIE
	endc

; Page 1 registers
Option	EQU	01H
TrisA	EQU	05H
TrisB	EQU	06H
EECon1	EQU	08H
EECon2	EQU	09H

RAMbase	EQU	0CH


; Application specific hardware - PAL level A/D sync, black and white

#define	White	PortA,0
#define	Sync	PortA,1

; Sync macros

; DNOP - Double NOP. Delay of 2 cycles, takes only one instruction
DNOP	MACRO
	LOCAL	Label
Label	GOTO	Label+1
	ENDM

; Delay3W - Delay 3 * W cycles, three instructions
Delay3W	MACRO
	LOCAL	Label
	MOVWF	Delay		; 1  \
Label	DECFSZ	Delay		; n+1 } 3n
	GOTO	Label		;2n-2/
	ENDM

;Status conditional macros
SKIPCC	MACRO
	BTFSC	Status,C
	ENDM
SKIPCS	MACRO
	BTFSS	Status,C
	ENDM
SKIPZ	MACRO
	BTFSS	Status,Z
	ENDM
SKIPNZ	MACRO
	BTFSC	Status,Z
	ENDM

	LIST

	CBLOCK	RAMbase
		Delay,Count
		HSeg1,HSeg2
		SegG,SegF,SegE,SegD,SegC,SegB,SegA
		Seg1,Seg2,Seg3,Seg4,Seg5,Seg6,Seg7,Seg8
		HrU,HrT,MiU,MiT,SeU,SeT,SubSec
		DyU,DyT,MoU,MoT,YrU,YrT
	ENDC

	ORG	0
	GOTO	Main

	ORG	4
	RETURN

; Number segment lookup
; This has a delay of 6 including the CALL

NumTbl	ADDWF	PCL
	RETLW	B'1111110'
	RETLW	B'0110000'
	RETLW	B'1101101'
	RETLW	B'1111001'
	RETLW	B'0110011'
	RETLW	B'1011011'
	RETLW	B'1011111'
	RETLW	B'1110000'
	RETLW	B'1111111'
	RETLW	B'1111011'


Main
	MOVLW	B'11100'
	TRIS	PortA
	MOVLW	B'11111111'
	TRIS	PortB

	BCF	White
	BSF	Sync	;Output black level

	MOVLW	0
	MOVWF	PCLatH

	MOVLW	0	;Initialise clock to
	MOVWF	SeU
	MOVWF	SeT	; 01/01/95
	MOVWF	MiU	; 00-00 00.00
	MOVWF	MiT
	MOVWF	HrU
	MOVWF	HrT
	MOVWF	DyT
	MOVWF	MoT
	MOVWF	SubSec
	MOVLW	1
	MOVWF	DyU
	MOVWF	MoU
	MOVLW	9
	MOVWF	YrT
	MOVLW	5
	MOVWF	YrU

; Frame starts here.
;
; Frame must be exactly 312.5 lines long, each line 64 cycles.
; That ensures frame rate of exactly 50Hz to crystal accuracy.

;5 Long Equalisation pulses

Frame				;Main Loop
	BCF	Sync		; 1		;30us Sync
	DNOP			; 3
	MOVLW	4		; 4
	MOVWF	Count		; 5
Loop1	MOVLW	8		; 6  6
	Delay3W			;30 30
	BSF	Sync		;31 31	;2us Black
	NOP			;32 32
	BCF	Sync		; 1  1	;30us Sync
	NOP			; 2  2
	DECFSZ	Count		; -  -
	GOTO	Loop1		; 5
	MOVLW	8		;    5
	Delay3W			;   29
	NOP			;   30
	BSF	Sync		;   31	;2us Black
	NOP			;   32

; Now 5 short equalisation pulses, 4 on interlace

	BCF	Sync		; 1	;2us Sync
	NOP			; 2
	BSF	Sync		; 3	;30us Black
	MOVLW	4		; 4
	BTFSC	SubSec,0	;	; 3 on interlace (SubSec odd)
	MOVLW	3		; 6
	MOVWF	Count		; 7
Loop2	MOVLW	8		; 8  8  8  8
	Delay3W			;32 32 32 32
	BCF	Sync		; 1  1  1  1	;2us Sync
	NOP			; 2  2  2  2
	BSF	Sync		; 3  3  3  3	;30us Black
	NOP			; 4  4  4  4
	DECFSZ	Count		; -  -  -  -
	GOTO	Loop2		; 7  7  7
	NOP			;          7
	MOVLW	8		;          8
	Delay3W			;         32

; Done that (phew!)

; Time now for 304 visible lines

; Now some more black lines
	BCF	Sync		; 1		;5us Sync
	MOVLW	D'30'		; 2
	CALL	BlkLns		;64

;Message 'SoFt-ball'
	BCF	Sync		; 1
	CALL	Delay4		; 5
	BSF	Sync		; 6
	MOVLW	B'1001110'	; 7
	MOVWF	Seg1		; 8
	MOVLW	B'0001110'	; 9
	MOVWF	Seg2		;10
	MOVLW	B'1111110'	;11
	MOVWF	Seg3		;12
	MOVLW	B'1001110'	;13
	MOVWF	Seg4		;14
	MOVLW	B'0110111'	;15
	MOVWF	Seg5		;16
	MOVLW	B'1100111'	;17
	MOVWF	Seg6		;18
	MOVLW	B'0110000'	;19
	MOVWF	Seg7		;20
	MOVLW	B'1001110'	;21
	MOVWF	Seg8		;22
	MOVLW	D'13'		;23
	Delay3W			;62
	DNOP			;64

;Display segments (this takes 60 lines)
	BCF	Sync		; 1
	CALL	DisSeg		;64

; 16 black lines
	BCF	Sync		; 1		;5us Sync
	MOVLW	D'16'		; 2
	CALL	BlkLns		;64

; Translate date to segments
	BCF	Sync		; 1
	CALL	Delay4		; 5
	BSF	Sync		; 6
	MOVF	DyT,W		; 7
	CALL	NumTbl		;13
	MOVWF	Seg1		;14
	MOVF	DyU,W		;15
	CALL	NumTbl		;21
	MOVWF	Seg2		;22
	MOVLW	B'0100101'	;23
	MOVWF	Seg3		;24
	MOVF	MoT,W		;25
	CALL	NumTbl		;31
	MOVWF	Seg4		;32
	MOVF	MoU,W		;33
	CALL	NumTbl		;39
	MOVWF	Seg5		;40
	MOVLW	B'0100101'	;41
	MOVWF	Seg6		;42
	MOVF	YrT,W		;43
	CALL	NumTbl		;49
	MOVWF	Seg7		;50
	MOVF	YrU,W		;51
	CALL	NumTbl		;57
	MOVWF	Seg8		;58
	CALL	Delay6		;64

;Display segments (this takes 60 lines)
	BCF	Sync		; 1
	CALL	DisSeg		;64

; 16 black lines
	BCF	Sync		; 1		;5us Sync
	MOVLW	D'16'		; 2
	CALL	BlkLns		;64

; Translate time to segments
	BCF	Sync		;1
	CALL	Delay4		;5
	BSF	Sync		;6
	MOVF	HrT,W		;7
	CALL	NumTbl		;13
	MOVWF	Seg1		;14
	MOVF	HrU,W		;15
	CALL	NumTbl		;21
	MOVWF	Seg2		;22
	MOVLW	B'0000001'	;23
	MOVWF	Seg3		;24
	MOVF	MiT,W		;25
	CALL	NumTbl		;31
	MOVWF	Seg4		;32
	MOVF	MiU,W		;33
	CALL	NumTbl		;39
	MOVWF	Seg5		;40
	MOVLW	B'0000000'	;41
	MOVWF	Seg6		;42
	MOVF	SeT,W		;43
	CALL	NumTbl		;49
	MOVWF	Seg7		;50
	MOVF	SeU,W		;51
	CALL	NumTbl		;57
	MOVWF	Seg8		;58
	CALL	Delay6		;64

;Display segments (this takes 60 lines)
	BCF	Sync
	CALL	DisSeg

;Increment time
	BCF	Sync		;1
	INCF	SubSec		;2	Increment 1/50th sec
	MOVLW	-D'50'		;3
	ADDWF	SubSec,W	;4	Carry now set if second has expired
	MOVLW	0		;5
	BSF	Sync		;6
	SKIPCC		;	Zero SubSec if =50
	MOVWF	SubSec		;8
	SKIPCC
	INCF	SeU		;10	And increment Second Units
	MOVLW	-D'10'		;11
	ADDWF	SeU,W		;12	Carry if needed Second Units->Tens
	MOVLW	0		;13
	SKIPCC			;14
	MOVWF	SeU		;15
	SKIPCC			;16
	INCF	SeT		;17
	MOVLW	-D'6'		;18
	ADDWF	SeT,W		;19
	MOVLW	0		;20
	SKIPCC			;21
	MOVWF	SeT		;22
	SKIPCC			;23
	INCF	MiU		;24
	MOVLW	-D'10'		;25
	ADDWF	MiU,W		;26
	MOVLW	0		;27
	SKIPCC			;28
	MOVWF	MiU		;29
	SKIPCC			;30
	INCF	MiT		;31
	MOVLW	-D'6'		;32
	ADDWF	MiT,W		;33
	MOVLW	0		;34
	SKIPCC			;35
	MOVWF	MiT		;36
	SKIPCC			;37
	INCF	HrU		;38
	MOVLW	-D'10'		;39
	ADDWF	HrU,W		;40
	MOVLW	0		;41
	SKIPCC			;42
	MOVWF	HrU		;43
	SKIPCC			;44
	INCF	HrT		;45
	MOVF	HrU,W		;46 ; Now check for Hours=24, and zero if so
	BTFSC	HrT,0
	ADDLW	D'10'		;48
	BTFSC	HrT,1
	ADDLW	D'20'		;50
	ADDLW	-D'24'		;51
	MOVLW	0		;52
	SKIPCC
	MOVWF	HrU		;54
	SKIPCC
	MOVWF	HrT		;56
	SKIPCC
	INCF	DyU		;58
	CALL	Delay6		;64


; Some black lines
	BCF	Sync		; 1		;5us Sync
	MOVLW	D'59'		; 2
	CALL	BlkLns		;64


; insert half line here on interlace

; Sync


; Now 5 short equalisation pulses
; prefixed by half video line on interlace

; Slight bodge of CCIR/PAL - the half line segment is actually a short eq pulse
	BCF	Sync		; 1		;2us Sync
	NOP			; 2
	BSF	Sync		; 3		;30us Black
	MOVLW	4		; 4
	BTFSS	SubSec,0	; -
	MOVLW	5		; 6
	MOVWF	Count		; 7
Loop6	MOVLW	8		; 8  8  8  8
	Delay3W			;32 32 32 32
	BCF	Sync		; 1  1  1  1	;2us Sync
	NOP			; 2  2  2  2
	BSF	Sync		; 3  3  3  3	;30us Black
	NOP			; 4  4  4  4
	DECFSZ	Count		; -  -  -  -
	GOTO	Loop6		; 7  7  7
	MOVLW	7		;          7
	Delay3W			;         28
	DNOP			;         30
	GOTO	Frame		;         32

; Delay routines
Delay7	NOP
Delay6	NOP
Delay5	NOP
Delay4	RETURN

; Some black lines
BlkLns	ADDLW	-1
	BSF	Sync		; 1		;59us black
	MOVWF	Count
	DNOP			; 1
Loop5	MOVLW	D'17'		; 1  1  1  1
	MOVWF	Delay		; 1  1  1  1
LoopD9	DECFSZ	Delay		;18 18 18 18
	GOTO	LoopD9		;32 32 32 32
	NOP			; 1  1  1  1
	DNOP			; 2  2  2  2
	BCF	Sync		; 1  1  1  1	;5us Sync
	CALL	Delay4		; 4  4  4  4
	BSF	Sync		; 1  1  1  1	;59us Black
	DECFSZ	Count		; 1  1  1  2
	GOTO	Loop5		; 2  2  2
	NOP			;          1
	MOVLW	D'17'		;          1
	Delay3W			;         51
	NOP			;          1
	RETURN			;          2


;Display segments
;
; Call immediately after BCF Sync, takes 60 line periods, returns control at
; time 64us
;
; On entry segment are in Seg1-8, order B'ABCDEFG'
;
DisSeg	MOVLW	SegG		; 4	;Line 1 black
	MOVWF	FSR		; 5
	BSF	Sync		; 6
	MOVLW	0		; 7
	MOVWF	SegA		; 8
	MOVWF	SegB		; 9
	MOVWF	SegC		;10
	MOVWF	SegD		;11
	MOVWF	SegE		;12
	MOVWF	SegF		;13
	MOVWF	SegG		;14
	MOVLW	6		;15
	Delay3W			;33
	DNOP			;35
	CALL	ConSeg		;64
	BCF	Sync		; 1	;Line 2 black
	CALL	Delay4		; 5
	BSF	Sync		; 6
	CALL	ConSeg		;35
	CALL	ConSeg		;64
	BCF	Sync		; 1	;Line 3 black
	CALL	Delay4		; 5
	BSF	Sync		; 6
	CALL	ConSeg		;35
	CALL	ConSeg		;64
	BCF	Sync		; 1	;Line 4 black
	CALL	Delay4		; 5
	BSF	Sync		; 6
	CALL	ConSeg		;35
	CALL	ConSeg		;64
	BCF	Sync		; 1	;Line 5  Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 6  Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 7  Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 8  Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 9  Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 10 Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 11 Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 12 Segment A
	CALL	RSegA		;64
	BCF	Sync		; 1	;Line 13 Segment BF
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 14 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 15 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 16 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 17 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 18 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 19 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 20 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 21 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 22 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 23 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 24 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 25 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 26 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 27 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 28 Segment B,F
	CALL	RSegFB		;64
	BCF	Sync		; 1	;Line 29 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 30 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 31 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 32 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 33 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 34 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 35 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 36 Segment G
	CALL	RSegG		;64
	BCF	Sync		; 1	;Line 37 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 38 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 39 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 40 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 41 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 42 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 43 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 44 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 45 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 46 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 47 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 48 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 49 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 50 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 51 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 52 Segment C,E
	CALL	RSegEC		;64
	BCF	Sync		; 1	;Line 53 Segment D
	CALL	RSegD		;64
	BCF	Sync		; 1	;Line 54 Segment D
	CALL	RSegD		;64
	BCF	Sync		; 1	;Line 55 Segment D
	CALL	RSegD		;64
	BCF	Sync		; 1	;Line 56 Segment D
	CALL	RSegD		;64
	BCF	Sync		; 1	;Line 57 Segment D
	CALL	RSegD		;64
	BCF	Sync		; 1	;Line 58 Segment D
	CALL	RSegD		;64
	BCF	Sync		; 1	;Line 59 Segment D
	CALL	RSegD		;64
	BCF	Sync		; 1	;Line 60 Segment D
	GOTO	RSegD		;64

; Line drawing routines - call at time 1, returns at time 64
RSegA	MOVF	SegA,W		; 4
	MOVWF	HSeg1		; 5
	BSF	Sync		; 6
	CALL	Delay5		;11
	GOTO	SegLine1	;64

RSegG	MOVF	SegG,W		; 4
	MOVWF	HSeg1		; 5
	BSF	Sync		; 6
	CALL	Delay5		;11
	GOTO	SegLine1	;64

RSegD	MOVF	SegD,W		; 4
	MOVWF	HSeg1		; 5
	BSF	Sync		; 6
	CALL	Delay5		;11
	GOTO	SegLine1	;64

RSegFB	MOVF	SegF,W		; 4
	MOVWF	HSeg1		; 5
	BSF	Sync		; 6
	MOVF	SegB,W		; 7
	MOVWF	HSeg2		; 8
	DNOP			;11
	NOP
	GOTO	SegLine2	;64

RSegEC	MOVF	SegE,W		; 4
	MOVWF	HSeg1		; 5
	BSF	Sync		; 6
	MOVF	SegC,W		; 7
	MOVWF	HSeg2		; 8
	DNOP			;11
	NOP
	GOTO	SegLine2	;64


;ConSeg - Convert Seg1-8 to SegA-G
;
;On entry FSR=SegA, Seg1-8=B'GFEDCBA' segments
; Call 7 times
;
; Takes 29 cycles including CALL

ConSeg	BTFSC	Seg1,0		;16	;Map Seg1-8 LSB to Ind0
	BSF	Ind0,0		;
	BTFSC	Seg2,0		;
	BSF	Ind0,1		;
	BTFSC	Seg3,0		;
	BSF	Ind0,2		;
	BTFSC	Seg4,0		;
	BSF	Ind0,3		;
	BTFSC	Seg5,0		;
	BSF	Ind0,4		;
	BTFSC	Seg6,0		;
	BSF	Ind0,5		;
	BTFSC	Seg7,0		;
	BSF	Ind0,6		;
	BTFSC	Seg8,0		;
	BSF	Ind0,7		;
	RRF	Seg1		;8	;Rotate right Seg1-8
	RRF	Seg2
	RRF	Seg3
	RRF	Seg4
	RRF	Seg5
	RRF	Seg6
	RRF	Seg7
	RRF	Seg8
	INCF	FSR		;1	;Increment FSR to next segment map
	RETURN			;2

; SegLine1
;
; On entry, HSeg1 contains bitmap of bars in line
; Execute CALL SegLine 10us into line, next instruction starts at 62
SegLine1
	BTFSC	HSeg1,0		;13
	BSF	White		;14
	DNOP			;16
	NOP			;17
	BCF	White		;18
	BTFSC	HSeg1,1		;19
	BSF	White		;20
	DNOP			;22
	NOP			;23
	BCF	White		;24
	BTFSC	HSeg1,2		;25
	BSF	White		;26
	DNOP			;28
	NOP			;29
	BCF	White		;30
	BTFSC	HSeg1,3		;31
	BSF	White		;32
	DNOP			;34
	NOP			;35
	BCF	White		;36
	BTFSC	HSeg1,4		;37
	BSF	White		;38
	DNOP			;40
	NOP			;41
	BCF	White		;42
	BTFSC	HSeg1,5		;43
	BSF	White		;44
	DNOP			;46
	NOP			;47
	BCF	White		;48
	BTFSC	HSeg1,6		;49
	BSF	White		;50
	DNOP			;52
	NOP			;53
	BCF	White		;54
	BTFSC	HSeg1,7		;55
	BSF	White		;56
	DNOP			;58
	NOP			;59
	BCF	White		;60
	NOP
	RETURN			;62

SegLine2
	BTFSC	HSeg1,0		;13
	BSF	White		;14
	BCF	White		;15
	BTFSC	HSeg2,0		;16
	BSF	White		;17
	BCF	White		;18
	BTFSC	HSeg1,1		;19
	BSF	White		;20
	BCF	White		;21
	BTFSC	HSeg2,1		;22
	BSF	White		;23
	BCF	White		;24
	BTFSC	HSeg1,2		;25
	BSF	White		;26
	BCF	White		;27
	BTFSC	HSeg2,2		;28
	BSF	White		;29
	BCF	White		;30
	BTFSC	HSeg1,3		;31
	BSF	White		;32
	BCF	White		;33
	BTFSC	HSeg2,3		;34
	BSF	White		;35
	BCF	White		;36
	BTFSC	HSeg1,4		;37
	BSF	White		;38
	BCF	White		;39
	BTFSC	HSeg2,4		;40
	BSF	White		;41
	BCF	White		;42
	BTFSC	HSeg1,5		;43
	BSF	White		;44
	BCF	White		;45
	BTFSC	HSeg2,5		;46
	BSF	White		;47
	BCF	White		;48
	BTFSC	HSeg1,6		;49
	BSF	White		;50
	BCF	White		;51
	BTFSC	HSeg2,6		;52
	BSF	White		;53
	BCF	White		;54
	BTFSC	HSeg1,7		;55
	BSF	White		;56
	BCF	White		;57
	BTFSC	HSeg2,7		;58
	BSF	White		;59
	BCF	White		;60
	NOP
	RETURN			;62


	ORG	2007
	DW	B'11001'

	END
