;;  Filename: AC_4ch_dimmer_20140210.asm
;;  Copyright (c) 2006,2014 Phil Short
;;
;;  This program is provided free for you to use in any way that you wish
;;  subject to the laws and regulations where you are using it.  Due diligence
;;  is strongly suggested before using this code.
;;
;;  The Author makes no warranty of any kind, express or implied, with regard
;;  to this program or the documentation contained in this document.  The
;;  Author shall not be liable in any event for incidental or consequential
;;  damages in connection with, or arising out of, the furnishing, performance
;;  or use of these programs.
;;
;;  Documentation may be found in the doityourselfchristmas.com file library.
;;
;;  7 October 2006 pjs Took renard as the basis for a new 4-channel version
;;    using a PIC12F629.
;;  December 2006 pjs - working version
;;  January 2007 pjs - increased minimum length of output pulses (to meet
;;    20 uS opto requirements).  This has a new (and complex) state machine
;;    for determining when to turn the triac drive pins on and off, but uses
;;    less memory and simpler logic for setting up the tables used for the
;;    the triac driver state machine.
;;
;;  February 2014 pjs removed receiver logic to make this standalone firmware.
;;    Also, changed pinout for use on renard hardware. Fixed the SAR logic,
;;    and added sample chase code.
;;  February 2014 pjs modified for more flexible pin assignments
;;
;;  Hardware mappings
;;  Pin 7 (GP0) - triac driver 1 (output)
;;  Pin 6 (GP1) - triac driver 2 (output)
;;  Pin 5 (GP2) - triac driver 3 (output)
;;  Pin 4 (GP3) - zero-crossing (pulse centered around zero-crossing)
;;  Pin 3 (GP4) - triac driver 0 (output)
;;  Pin 2 (GP5) - reserved for possible future use.
;;
;;  Timer 0 is used to generate an interrupt detecting the leading edge of the
;;    serial data start bit, and to time the bits after that.
;;  Timer 1 is used for timing the triac outputs.

 LIST R=DEC

 #IFDEF __12F629
   #INCLUDE "p12f629.inc"
 #ENDIF
 #IFDEF __12F675
   #INCLUDE "p12f675.inc"
 #ENDIF

 __CONFIG _CPD_OFF & _CP_OFF & _MCLRE_OFF & _BODEN_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

;; Fundamental declarations
 #define CLOCK 4000000
 #define BAUDRATE 19200

;; These defines should be changed if the pinout is modified.
	
 #define CHANNEL1_BIT 4
 #define CHANNEL2_BIT 0
 #define CHANNEL3_BIT 1
 #define CHANNEL4_BIT 2
;; Derived declarations

 #define BIT_TIMER ((CLOCK / 4) / BAUDRATE)

 #define ZC GPIO,3
 #define TEST_STROBE GPIO,0
		
 CBLOCK 0x39
	
;; local variables used by half_cycle_setup (non-persistent)

   slot_id_test, src_mask, new_mask, dst_offset, slot_id_working_max

	
;; Current dimmer values

   channel0, channel1, channel2, channel3

;; variables used for outputting to the triacs

   val0, ctr0
   val1, ctr1
   val2, ctr2
   val3, ctr3

 ENDC
 CBLOCK 0x20
   _w, _status
 ENDC
;;
;; Start of executable code
;;
	
 org 0
	nop
	goto initialize
	
;; this is where the interrupt routine would go if there was one.
 org 4
;; ----------------------------------------------------------------------------
;;
;; start of Foreground code.
;;

;;
;; Setup the ctr[3:0] and val[3:0] variables
;;
;; IMPORTANT NOTE:  Ensure that that the jump table further down in the code
;;   is totally within page 0 of the code (between 0x00 and 0x0FF).
;;
half_cycle_setup:

	movlw	(1<<CHANNEL1_BIT) | (1<<CHANNEL2_BIT) | (1<<CHANNEL3_BIT) | (1<<CHANNEL4_BIT)
	movwf	GPIO		; turn off (set) the outputs (redundant ???)
;;	bcf	TEST_STROBE
	movwf	src_mask	; mask of unprocessed entries.
	
	movlw   28		; start at end of table, work backwards
	movwf	dst_offset

	clrf	slot_id_working_max
	
;; Find smallest value that hasn't already been incorporated into the table
;; (i.e. the one that occurs last in the half-wave cycle).

insert_entry_loop:		; this loop is executed 0-3 times.
	clrf	new_mask
	movlw	0xFF		; sentinel value
	movwf	slot_id_test
	
	btfss	src_mask,CHANNEL1_BIT
	goto	test_slot1

	movf	channel0,w
	movwf	slot_id_test

	bsf	new_mask,CHANNEL1_BIT
	
test_slot1:
	btfss	src_mask,CHANNEL2_BIT
	goto	test_slot2

	movf	channel1,w
	subwf	slot_id_test,w	; w = slot_id_test - channel1


	btfss	STATUS,C
	goto	test_slot2	; C clear -> channel1 > slot_id_test -> 'goto'

	btfss	STATUS,Z
	clrf	new_mask

	subwf	slot_id_test,f
	bsf	new_mask,CHANNEL2_BIT

test_slot2:
	btfss	src_mask,CHANNEL3_BIT
	goto	test_slot3

	movf	channel2,w
	subwf	slot_id_test,w	; w = slot_id_test - channel2

	btfss	STATUS,C
	goto	test_slot3	; C clear -> channel2 > slot_id_test -> 'goto'

	btfss	STATUS,Z
	clrf	new_mask

	subwf	slot_id_test,f	; now slot_id_test = channel2
	bsf	new_mask,CHANNEL3_BIT

test_slot3:
	btfss	src_mask,CHANNEL4_BIT
	goto	test_slot_end

	movf	channel3,w
	subwf	slot_id_test,w	; w = slot_id_test - channel3

	btfss	STATUS,C
	goto	test_slot_end	; C clear -> slot_id_test > channel3 -> 'goto'

	btfss	STATUS,Z
	clrf	new_mask

	subwf	slot_id_test,f
	bsf	new_mask,CHANNEL4_BIT

;; Here, slot_id_test holds the value for the smallest remaining slot(s),
;;   and new_mask is a bitmap of slots which have that value.

test_slot_end:

	movf	slot_id_test,w
	subwf	slot_id_working_max,w	; w = slot_id_working_max-slot_id_test
	subwf	slot_id_working_max,f	; slot_id_working_max = slot_id_test
	
	movwf	slot_id_test	; slot_id_test now holds the delta, unused
				; first time through.

	movlw	0xF9
	addwf	dst_offset,f
	
	movf	dst_offset,w	;
	addwf	PCL,f
;; offset 0
	comf	slot_id_working_max,w
	addlw	1
	movwf	ctr0
	subwf	ctr1,f
	comf	new_mask,w
	movwf	val0
	goto	insert_entry_finish_test
;; offset 7
	comf	slot_id_working_max,w
	addlw	1
	movwf	ctr1
	subwf	ctr2,f
	comf	new_mask,w
	movwf	val1
	goto	insert_entry_finish_test
;; offset 14
	comf	slot_id_working_max,w
	addlw	1
	movwf	ctr2
	subwf	ctr3,f
	comf	new_mask,w
	movwf	val2
	goto	insert_entry_finish_test
;; offset 21
	comf	slot_id_working_max,w
	addlw	1
	movwf	ctr3
	comf	new_mask,w
	movwf	val3

insert_entry_finish_test:	; bail out if the setup is complete (all values
				; are in the same timeslot).

	andwf	src_mask,f
	btfsc	STATUS,Z
	return
		
	goto	insert_entry_loop

;;
;; This routine runs the timers and drives the triacs.
;;
;; IMPORTANT NOTE:  Ensure that that the jump table further down in the code
;;   (first four labels after 'addwf PCL') is totally within page 1 of the
;;   code (between 0x100 and 0x1FF).
;;
half_cycle_work:

 	bcf	T1CON,TMR1ON	; stop the timer...
	clrf	TMR1L
 	bsf	T1CON,TMR1ON	; start the timer...

	movf	dst_offset,w
	addwf	PCL,f

;; Initial entry points
;; The following table is best understood as a great big state machine.  Each
;;   state is a group of 7 or 8 lines, beginning with the label work_nVab,
;;   where n is a number between 0 and 5 that indicates which ctr/val pair that
;;   we are counting down towards, V is either H or L, indicating the value of
;;   bit 5 of TMR1L that we are waiting for, and the last two characters
;;   represent how many levels of output have been set that must be turned off.
;; The table has been re-arranged for two purposes...first, to put the possible
;;   initial states at the beginning of the table, and second to make sure that
;;   each state is only 8 clocks long (i.e. re-arranging so that the code falls
;;   through, rather than taking jumps, when making a jump would slow things
;;   down too much.
work_0H:
	btfss	TMR1L,5
	goto	work_0H
	
	decfsz	ctr0,f
	goto	work_0L

	comf	val0,w
	xorwf	GPIO,f

	goto	work_1L01

work_1H00:
	btfss	TMR1L,5
	goto	work_1H00
	
	decfsz	ctr1,f
	goto	work_1L00

	comf	val1,w
	xorwf	GPIO,f

	goto	work_2L01

work_2H00:
	btfss	TMR1L,5
	goto	work_2H00
	
	decfsz	ctr2,f
	goto	work_2L00

	comf	val2,w
	xorwf	GPIO,f

	goto	work_3L01
		
work_3H00:
	btfss	TMR1L,5
	goto	work_3H00
	
	decfsz	ctr3,f
	goto	work_3L00

	comf	val3,w
	xorwf	GPIO,f

	goto	work_4L0

;; ------------------------ tier zero -----------------------

work_0L:
	btfsc	TMR1L,5
	goto	work_0L
	
	decfsz	ctr0,f
	goto	work_0H

	comf	val0,w
	xorwf	GPIO,f

	goto	work_1H01

;; ------------------------ tier one -----------------------
	
	
work_1H01:
	btfss	TMR1L,5
	goto	work_1H01
	
	decfsz	ctr1,f
	goto	work_1L10

	comf	val1,w
	xorwf	GPIO,f

	goto	work_2L11

work_1L01:
	btfsc	TMR1L,5
	goto	work_1L01

	decfsz	ctr1,f
	goto	work_1H10

	comf	val1,w
	xorwf	GPIO,f

	goto	work_2H11

work_1H10:
	btfss	TMR1L,5
	goto	work_1H10
	
	comf	val0,w
	xorwf	GPIO,f
	
	decfsz	ctr1,f
	goto	work_1L00

	comf	val1,w
	xorwf	GPIO,f

;;	goto	work_2L01

work_2L01:			; out-of-place tier 2 (placed to equalize time)
	btfsc	TMR1L,5
	goto	work_2L01

	decfsz	ctr2,f
	goto	work_2H10

	comf	val2,w
	xorwf	GPIO,f

	goto	work_3H11

work_1L10:
	btfsc	TMR1L,5
	goto	work_1L10
	
	comf	val0,w
	xorwf	GPIO,f
	
	decfsz	ctr1,f
	goto	work_1H00

	comf	val1,w
	xorwf	GPIO,f

;;	goto	work_2H01

work_2H01:			; out-of-place tier 2 (placed to equalize time)
	btfss	TMR1L,5
	goto	work_2H01
	
	decfsz	ctr2,f
	goto	work_2L10

	comf	val2,w
	xorwf	GPIO,f

	goto	work_3L11

work_1L00:
	btfsc	TMR1L,5
	goto	work_1L00
	
	decfsz	ctr1,f
	goto	work_1H00

	comf	val1,w
	xorwf	GPIO,f

	goto	work_2H01

;; ------------------------ tier two -----------------------
	
work_2H11:
	btfss	TMR1L,5
	goto	work_2H11

	comf	val0,w
	xorwf	GPIO,f
	
	decfsz	ctr2,f
	goto	work_2L10

	comf	val2,w
	xorwf	GPIO,f

;;	goto	work_3L11

work_3L11:			; out-of-place tier 3 (placed to equalize time)
	btfsc	TMR1L,5
	goto	work_3L11

	comf	val1,w
	xorwf	GPIO,f
	
	decfsz	ctr3,f
	goto	work_3H10

	comf	val3,w
	xorwf	GPIO,f

;;	goto	work_4H1

work_4H1:			; out-of-place tier 4 (placed to equalize time)
	btfss	TMR1L,5
	goto	work_4H1

	comf	val2,w
	xorwf	GPIO,f
	
	goto	work_5L


work_2L11:
	btfsc	TMR1L,5
	goto	work_2L11

	comf	val0,w
	xorwf	GPIO,f
	
	decfsz	ctr2,f
	goto	work_2H10

	comf	val2,w
	xorwf	GPIO,f

;;	goto	work_3H11

work_3H11:			; out-of-place tier 3 (placed to equalize time)
	btfss	TMR1L,5
	goto	work_3H11

	comf	val1,w
	xorwf	GPIO,f
	
	decfsz	ctr3,f
	goto	work_3L10

	comf	val3,w
	xorwf	GPIO,f

;;	goto	work_4L1

work_4L1:			; out-of-place tier 4 (placed to equalize time)
	btfsc	TMR1L,5
	goto	work_4L1

	comf	val2,w
	xorwf	GPIO,f
	
	goto	work_5H

work_2H10:
	btfss	TMR1L,5
	goto	work_2H10
	
	comf	val1,w
	xorwf	GPIO,f
	
	decfsz	ctr2,f
	goto	work_2L00

	comf	val2,w
	xorwf	GPIO,f

;;	goto	work_3L01

work_3L01:			; out-of-place tier 3 (placed to equalize time)
	btfsc	TMR1L,5
	goto	work_3L01

	decfsz	ctr3,f
	goto	work_3H10

	comf	val3,w
	xorwf	GPIO,f

	goto	work_4H1

work_2L10:
	btfsc	TMR1L,5
	goto	work_2L10
	
	comf	val1,w
	xorwf	GPIO,f
	
	decfsz	ctr2,f
	goto	work_2H00

	comf	val2,w
	xorwf	GPIO,f

;;	goto	work_3H01

work_3H01:			; out-of-place tier 3 (placed to equalize time)
	btfss	TMR1L,5
	goto	work_3H01
	
	decfsz	ctr3,f
	goto	work_3L10

	comf	val3,w
	xorwf	GPIO,f

	goto	work_4L1

work_2L00:
	btfsc	TMR1L,5
	goto	work_2L00
	
	decfsz	ctr2,f
	goto	work_2H00

	comf	val2,w
	xorwf	GPIO,f

	goto	work_3H01

;; ----------------------- tier three ----------------------
work_3H10:
	btfss	TMR1L,5
	goto	work_3H10
	
	comf	val2,w
	xorwf	GPIO,f
	
	decfsz	ctr3,f
	goto	work_3L00

	comf	val3,w
	xorwf	GPIO,f

;;	goto	work_4L0

work_4L0:			; out-of-place tier 4 (placed to equalize time)
	btfsc	TMR1L,5
	goto	work_4L0

	goto	work_5H


work_3L10:
	btfsc	TMR1L,5
	goto	work_3L10
	
	comf	val2,w
	xorwf	GPIO,f
	
	decfsz	ctr3,f
	goto	work_3H00

	comf	val3,w
	xorwf	GPIO,f

;;	goto	work_4H0

work_4H0:			; out-of-place tier 4 (placed to equalize time)
	btfss	TMR1L,5
	goto	work_4H0

	goto	work_5L

work_3L00:
	btfsc	TMR1L,5
	goto	work_3L00
	
	decfsz	ctr3,f
	goto	work_3H00

	comf	val3,w
	xorwf	GPIO,f

	goto	work_4H0

;; -------------- all tier four stuff moved ---------------
;; ----------------------- tier five ----------------------
work_5H:
	btfss	TMR1L,5
	goto	work_5H

	comf	val3,w
	xorwf	GPIO,f

	return	

work_5L:
	btfsc	TMR1L,5
	goto	work_5L

	comf	val3,w
	xorwf	GPIO,f

	return	

;; ---------------------------------------------------------------------------------
initialize:
;;
;; initialize some registers
;;
	movlw	0
	movwf	PCLATH		; normal value of PCLATH is 0
	
	clrf	channel0
	clrf	channel1
	clrf	channel2
	clrf	channel3

	bsf	channel0,0
	
;; configure the I/O ports

	movlw	7
	movwf	CMCON		; disable the comparators

  #IFDEF __12F675
	bsf	STATUS,RP0	; probably not redundant
	clrf	ANSEL ^ 0x80
	bcf	STATUS,RP0
	
	clrf	ADCON0		; probably redundant
  #ENDIF
	movlw	0xFF		; turn off the triacs (although this won't take
	movwf	GPIO		; effect until the TRISIO reg is initialized)

	bsf	STATUS,RP0

	call	0x3FF
	movwf	OSCCAL ^ 0x80

	movlw	~((1<<CHANNEL1_BIT) | (1<<CHANNEL2_BIT) | (1<<CHANNEL3_BIT) | (1<<CHANNEL4_BIT))
	movwf	TRISIO ^ 0x80	; h/w ignores bit 3 of this register

;; internal pullups don't really matter (only one pin is input, and that pin
;;   has a strong external pullup resistor).  But they are set up anyway.
	bsf	OPTION_REG ^ 0x80,7	; global weak-pullup enable (off)

;;
;; PIC h/w timer 1 (16 bit timer) is reset on the low-going edge of the AC 
;; sense signal.  This timer is sensed by polling, rather than by interrupting.
;;
	clrf	T1CON
	bsf	T1CON,TMR1ON

;;
;; Use SAR to adjust OSC upwards so that there are 8192 clocks while ZC is low
;; We know that it has to be adjusted upwards (as opposed to downwards) because
;;   the nominal count of clocks while ZC is low is 8333 - 760 uS (as observed on
;;   a Renard-16), which is less than 8192.
	
	movlw	0x40		; start with bit 6 due to upwards dir.

SAR_stage:
	movwf	_w		; no interrupts, so use _w for tmp var
	
	bsf	STATUS,RP0
	addwf	OSCCAL^0x80,w	; test new value of OSCCAL

	btfsc	STATUS,C	; go to next stage if OSCCAL would
	goto	SAR_shift_right	; overflow
	movwf	OSCCAL^0x80
	
	bcf	STATUS,RP0

	movlw	120		; 120 cycles of ZC for clock to
	movwf	_status		; stabilize
	
SAR_zc_wait1:	
	btfss	ZC		; wait for ZC to go high (~500 us before cross)
	goto	SAR_zc_wait1

	call	delay_4instructions
	call	delay_4instructions
	call	delay_4instructions
	call	delay_4instructions
	
SAR_zc_wait2:	
	btfsc	ZC
	goto	SAR_zc_wait2

	clrf	TMR1L		; reset the timer
	clrf	TMR1H

	call	delay_4instructions
	call	delay_4instructions
	call	delay_4instructions
	call	delay_4instructions
	
	decfsz	_status,f
	goto	SAR_zc_wait1
				; just after trailing edge of ZC
SAR_zc_wait3:			; wait until ZC goes high again
	btfss	ZC
	goto	SAR_zc_wait3

	movf	TMR1H,w
	andlw	0xE0
	btfsc	STATUS,Z
	goto	SAR_shift_right	; commit the OSCCAL change (clock not too fast)

SAR_backup:			; 
	bsf	STATUS,RP0
	movf	_w,w
	subwf	OSCCAL^0x80,f	; clock too fast - reverse the OSCCAL change
	
SAR_shift_right:
	bcf	STATUS,RP0
	bcf	STATUS,C	; clear the carry flag.
	rrf	_w,w
	btfss	_w,2
	goto	SAR_stage

	bsf	STATUS,RP0
	movlw	0x4		; extra good-luck term (for temp, pwr variance)
	addwf	OSCCAL ^ 0x80,w
	btfss	STATUS,C
	movwf	OSCCAL^0x80
	bcf	STATUS,RP0

	goto	SAR_test_bypass
	
SAR_test_loop1:
	bsf	TEST_STROBE
	btfsc	ZC
	goto	SAR_test_loop1

	clrf	TMR1L
	clrf	TMR1H
SAR_test_loop2:	
	bcf	TEST_STROBE
	btfsc	TMR1H,4
	bsf	TEST_STROBE
	btfss	ZC
	goto	SAR_test_loop2
	goto	SAR_test_loop1

SAR_test_bypass:	

;; wait for first high-to-low transition on ZC

startup_wait1:			; wait until ZC is high
	btfsc	ZC
	goto	startup_wait1
startup_wait2:			; wait for falling edge of ZC
	btfss	ZC
	goto	startup_wait2

mainloop:
;;	bcf	TEST_STROBE
	call	app_logic
	call	half_cycle_setup
;;	bsf	TEST_STROBE
mainloop_wait:			; wait for ZC to go low
	btfsc	ZC
	goto	mainloop_wait
	
	call	half_cycle_work

	goto	startup_wait2

;;
;; This is the routine that controls the overall appearance of the lights.
;;   This is where the chase logic, for example, would go.
;;
app_logic:
	movf	channel0,f
	btfsc	STATUS,Z
	goto	channel0_z

	incfsz	channel0,f
	return
	incf	channel1,f
	return

channel0_z:	
	movf	channel1,f
	btfsc	STATUS,Z
	goto	channel1_z

	incfsz	channel1,f
	return
	incf	channel2,f
	return

channel1_z:	
	movf	channel2,f
	btfsc	STATUS,Z
	goto	channel2_z

	incfsz	channel2,f
	return
	incf	channel3,f
	return

channel2_z:	
	movf	channel3,f
	btfsc	STATUS,Z
	goto	channel3_z

	incfsz	channel3,f
	return
	incf	channel0,f
	return
channel3_z:
	incf	channel0,f
	return

delay_4instructions:
	return
	
;;
;; EEPROM data - first (and only) defined byte is the board address
;;
  ORG H'2100'
  ORG 0x2100
  	de 00h
    		
  END
