	title	"KW11L"
;++
;
; KW11L 60 Hz clock emulation for BA11-VA (etc.) case.
;
; By John Wilson, D Bit <wilson@dbit.com>.
;
; 02/26/1999	JMBW	Created (PIC16F84 version).
; 02/12/2020	JMBW	Adapted for PIC12F508 w/simultaneous 50/60 Hz output.
;
; This file is hereby released into the public domain.
;
; The "circuit" (it's too simple to deserve that word) is as follows:
;
; U1 = PIC12F508
; C1 = .1 uF bypass cap
; C2/C3 = 30 pF ceramic caps
; Y1 = 32.768 kHz watch crystal
; Q1/Q2 = 2N7002 MOSFETs
;
; I'll name the pins on C1/C2/C3/Y1 as pins 1/2 just for clarity, but it
; doesn't matter which is which.
;
; U1 pin 1 and U1 pin 4 and C1 pin 1 go to +5
; U1 pin 8 and C1 pin 2 go to GND
; U1 pin 2 and C2 pin 1 go to Y1 pin 1
; U1 pin 3 and C3 pin 1 go to Y2 pin 2
; C2 pin 2 and C3 pin 2 go to GND
; U1 pin 7 *or* U1 pin 6 goes to BEVNT L
; All others NC
;
; Here's a nasty ASCII art attempt at this:
;
;			       U1 PIC12F508		+--- 60 Hz
;				+---- ----+	  	|
;  +5 --+------------------Vdd--| 1  U  8 |--Vss--+ +---E^ Q1
;	|   Y1 +--|H|--+--OSC1--| 2	7 |--GP0--(-+	|
;    C1 =      +-------(--OSC2--| 3	6 |--GP1--(-+  ///
;	|      |       |     +--| 4	5 |	  | |
;      ///  C2 =    C3 =     |	+---------+	  | |   +--- 50 Hz
;	       |       |     |			  | |   |
;	      ///     ///    +--/MCLR-- +5	  | +---E^ Q2
;						  |     |
;						  |	|
;						 ///   ///
;
; Off-board connections:
;  +5		BV1
;  GND		BT1
;  BEVNT L	BR1 (connect to either "60 Hz" or "50 Hz", depending on what
;		country your PDP-11 OS thinks it's in)
;
; The easiest way to handle these is to wire-wrap them directly to the
; backplane pins, then tape/cable-tie the board in place so that it won't short
; against anything.  Maybe wrap it in tape, or a small zip-loc bag.
;
;--
	list	p=p12f508
	include <p12f508.inc>
	__config _LP_OSC&_WDT_OFF&_MCLRE_OFF ;low-power oscillator (watch xtal)
;
clk	equ	d'32768'	;input clock frequency in Hz (watch xtal)
;
cyc	equ	clk/4		;4 clocks per instruction cycle
divis	equ	d'3000'		;divisor for counter math
				;(dividend is thirds of a msec * CYC)
;
OUT60=	h'01'			;60 Hz output = GPIO0
OUT50=	h'02'			;50 Hz output = GPIO1
;
	cblock	h'07'		;variable space
	 frac			;fraction of cycle to delay
	endc
;
; Macro to delay a certain amount of time
;
; msecs = number of whole milliseconds
; thirds = number of thirds of milliseconds (it's always a multiple of 1/3)
; ncyc = # cycles in other code until next DELAY Macro
;
delay	macro	msecs,thirds,ncyc
newtot	set	((((msecs*3)+thirds)*cyc)/divis) ;# cycles since top of loop
rem	set	(((msecs*3)+thirds)*cyc)%divis ;remainder (0::DIVIS-1)
cycles	set	newtot-total-ncyc ;how much to delay now, minus code outside
total	set	newtot		;where we'll be when it's all over
	while	cycles>=2	;GOTOs take two cycles (only way this fits)
	 goto	$+1		;[2] delay two cycles
cycles	set	cycles-2
	endw
 if cycles
	nop			;[1] plus one more if odd
 endif
	endm
;
	radix	dec
;
; Entry point (wraps to 000 from 1FF at reset)
;
;	code	0		;needed in MPASMX but unwanted in GPASM
	org	0
	movwf	OSCCAL		;save RC osc calibration value (we don't use)
				;(loaded at reset by MOVLW xx at 1FF)
	clrf	GPIO		;clear output latch
	movlw	h'FC'		;bits 0/1 are outputs
	tris	GPIO
	movlw	-(divis/d'100')	;init fraction counter
	movwf	frac
;
; Main loop -- toggle GPIO0 output at 60 Hz and GPIO1 output at 50 Hz.
;
; We're playing back two waveforms at once, which sync up every 100 msec (after
; six square pulses on GPIO0 and five on GPIO1).  Schedule for each 100 msec:
;
;		-60 Hz-	-50 Hz-
; 0 ms		on #1	on #1
; 8.33 ms	off
; 10 ms			off
; 16.66 ms	on #2
; 20 ms			on #2
; 25 ms		off
; 30 ms			off
; 33.33 ms	on #3
; 40 ms			on #3
; 41.66 ms	off
; 50 ms		on #4	off
; 58.33 ms	off
; 60 ms			on #4
; 66.66 ms	on #5
; 70 ms			off
; 75 ms		off
; 80 ms			on #5
; 83.33 ms	on #6
; 90 ms			off
; 91.66 ms	off
; 100 ms => start over at 0
;
; Use "CLRW/MOVWF GPIO" instead of "CLRF GPIO" when writing zeros to both at
; once to make sure changes always happen on the second of two cycles, to
; prevent needless jitter.
;
total	set	0		;total cycles delayed so far in 100 msec period
rem	set	0		;remainder so far (partial cycle) out of 3000
;
loop	; time		OUT60	OUT50
	; 0 ms		on #1	on #1
	movlw	OUT60+OUT50	;[1] set both
	movwf	GPIO		;[1]
				;2 cycles
	delay	8,1,2		;8 1/3 ms
	; 8.33 ms	off	+
	movlw	OUT50		;[1] clear OUT60, keep OUT50
	movwf	GPIO		;[1]
	delay	10,0,2		;10 ms
	; 10 ms		-	off
	clrw			;[1] clear OUT50
	movwf	GPIO		;[1]
	delay	16,2,2		;16 2/3 ms
	; 16.66 ms	on #2	-
	movlw	OUT60		;[1] set OUT60
	movwf	GPIO		;[1]
	delay	20,0,2		;20 ms
	; 20 ms		+	on #2
	movlw	OUT60+OUT50	;[1] set OUT50, keep OUT60
	movwf	GPIO		;[1]
	delay	25,0,2		;25 ms
	; 25 ms		off	+
	movlw	OUT50		;[1] clear OUT60, keep OUT50
	movwf	GPIO		;[1]
	delay	30,0,2		;30 ms
	; 30 ms		-	off
	clrw			;[1] clear both
	movwf	GPIO		;[1]
	delay	33,1,2		;33 1/3 ms
	; 33.33 ms	on #3	-
	movlw	OUT60		;[1] set OUT60
	movwf	GPIO		;[1]
	delay	40,0,2		;40 ms
	; 40 ms		+	on #3
	movlw	OUT60+OUT50	;[1] set OUT50, keep OUT60
	movwf	GPIO		;[1]
	delay	41,2,2		;41 2/3 ms
	; 41.66 ms	off	+
	movlw	OUT50		;[1] clear OUT60, keep OUT50
	movwf	GPIO		;[1]
	delay	50,0,2		;50 ms
	; 50 ms		on #4	off
	movlw	OUT60		;[1] set OUT60, clear OUT50
	movwf	GPIO		;[1]
	delay	58,1,2		;58 1/2 ms
	; 58.33 ms	off	-
	clrw			;[1] clear OUT60
	movwf	GPIO		;[1]
	delay	60,0,2		;60 ms
	; 60 ms		-	on #4
	movlw	OUT50		;[1] set OUT50
	movwf	GPIO		;[1]
	delay	66,2,2		;66 2/3 ms
	; 66.66 ms	on #5	+
	movlw	OUT60+OUT50	;[1] set OUT60, keep OUT50
	movwf	GPIO		;[1]
	delay	70,0,2		;70 ms
	; 70 ms		+	off
	movlw	OUT60		;[1] clear OUT50
	movwf	GPIO		;[1]
	delay	75,0,2		;75 ms
	; 75 ms		off	-
	clrw			;[1] clear OUT60
	movwf	GPIO		;[1]
	delay	80,0,2		;80 ms
	; 80 ms		-	on #5
	movlw	OUT50		;[1] set OUT50
	movwf	GPIO		;[1]
	delay	83,1,2		;83 1/3 ms
	; 83.33 ms	on #6	+
	movlw	OUT60+OUT50	;[1] set OUT60, keep OUT50
	movwf	GPIO		;[1]
	delay	90,0,2		;90 ms
	; 90 ms		+	off
	movlw	OUT60		;[1] clear OUT50, keep OUT60
	movwf	GPIO		;[1]
	delay	91,2,2		;91 2/3 ms
	; 91.66 ms	off	-
	clrw			;[1] clear OUT60
	movwf	GPIO		;[1]
	delay	100,0,(5+2+2)	;100 ms, allowing for:
				;5 leap-tick code (so, 6 cycles if needed)
				;2 GOTO LOOP
				;2 at top of loop before first DELAY
	; fix fraction -- we owe a NOP if remainder overflows
	; if it overflows, this code (before SKIP) takes 6 cycles
	; if not, it takes 5
	movlw	rem/d'100'	;[1] get remainder from all those DELAY macros
	addwf	frac,F		;[1] see if it adds up to a leap tick
	btfss	STATUS,C	;[1/2] did it wrap through 0?
	 goto	skip		;[2] no
	movlw	divis/d'100'	;[1] divisor
	subwf	frac,F		;[1] rewind to account for extra delay
skip	goto	loop		;[2] back to top
;
	end
