;**********************************************************************
                ;               	 ALARM SYSTEM PROGRAM
                ;**********************************************************************
                ;
                ; Source		: Alarmsys.asm
                ; Date		: July 24, 2000
                ;----------------------------------------------------------------------
                ;
                ; Written By	: Mike Genovy
                ; Original Code   : May 19, 1983
                ; Last Modified	: August 17, 2000
                ;
                ;**********************************************************************
                ; Notes:
                ; ------
                ;
                ;	The original code was hand assembled, and tested on the Synertek
                ; SYM 6502 computer. Then the eprom for the single board computer was
                ; programmed after all addresses had been corrected to the S.B.C. 
                ; addresses. 
                ;	I have created this source code file from the original code written
                ; in 1983 and assembled it using the Cross 16 assembler from Universal
                ; Cross assemblers. This assembler produces an INTEL Hex format output 
                ; file. 
                ;-------------------------------------------------------------------------
                ; 
                ; Alarm Program Operation:
                ; ------------------------
                ;
                ; Power up or a Manual Reset:
                ;	- Read 8 switch inputs 
                ;	- If any are open at this time they will not be scanned during
                ;	  normal operation.
                ;
                ; Run Mode:
                ;	- Scan switch inputs ( the switches to be scanned are determined
                ;	  at pwer up, or after a manual reset ) 
                ;	- If any switch inputs go open the alarm will be turned on after
                ;	  a 15 second delay. The Alarm LED will also be turned on at this
                ;	  time.
                ;	- The Alarm relay will remain on for 15 minutes. After 15 minutes
                ;	  the alarm relay will be turned off, but the alarm LED will be
                ;	  left on.
                ;
                ;
                ;
                ; Disable Mode:
                ;
                ;
                ;
                ;
                ;
                ; Test Mode:
                ;
                ;
                ;
                ;
                ;
                ;
                ;
                ;
                ;
                ; 6522 Port Assignments
                ; ---------------------
                ;
                ; PA0 - Alarm switch input ( Low in normal position )
                ; PA1	- Alarm switch input
                ; PA2	- Alarm switch input
                ; PA3	- Alarm switch input
                ; PA4	- Alarm switch input
                ; PA5	- Alarm switch input
                ; PA6	- Disable switch input
                ; PA7	- Tamper switch input ( This bit is inverted in the code )
                ;
                ; CA1
                ; CA2	- Pulse Output 1
                ;
                ; PB0	- Alarm relay output
                ; PB1	- Alarm LED  output
                ; PB2	- Run LED output		- These LED's are on the front of
                ; PB3	- Disable LED output	- the alarm panel, and indicate
                ; PB4	- Test LED output		- the current mode of operation.
                ; PB5	- Run input		- These 3 inputs are from a 3 position 
                ; PB6	- Disable input	- rotary switch, referred to as the
                ; PB7	- Test input	- Control Switch in this code.
                ;
                ; CB1
                ; CB2	- Pulse output 2
                ;
                ;	This alarm program has 3 main time delay loops, and 2 delay loops
                ; associated with alarm switch debouncing. 
                ;
                ;	The Main program delay loops are:
                ;	---------------------------------
                ;
                ;	WAIT1 - Delay loop before starting to scan switches when put in Run 
                ;		  mode
                ;
                ;	WAIT2 - Delay loop before turning on the alarm relay and alarm led
                ;
                ;	WAIT3 - Delay loop before turning off the alarm relay
                ;
                ;	The Control Switch subroutine ( RDCSW: ) delay loop is:
                ;	-------------------------------------------------------
                ;
                ;	WAITA - This is a switch debounce delay loop
                ;
                ;	The Read Port A subroutine ( RDPTA ) delay loop is:
                ;	---------------------------------------------------
                ;
                ;	WAITB- This is a switch debounce delay loop
                ;
                ;	The present length of these delays is:
                ;	======================================
                ;
                ;	WAIT1	-	60 seconds ---> now 2 seconds
                ;	WAIT2	-	60 seconds ---> now 2 seconds
                ;	WAIT3	-	15 minutes
                ;	WAITA	- 	0.5 seconds
                ;	WAITB	-	0.5 seconds
                ;
                ;======================================================================
                ; Define the type of processor and the EPROM HEX file format
                ;======================================================================
                ;
 0000           		CPU  "6502.TBL"	; Processor table
 0000           		HOF  "INT8"		; HEX format
                ;
                ;======================================================================
                ; Define starting addresses of the 6502 S.B.C. EPROM program & tables
                ;======================================================================
                ;
 B800 =         MAIN:		EQU	0B800H	;Cartridge starts at B800 hex
                
 BF00 =         INITVIA:	EQU	0BF00H	;array used to init 6522 via
                
 BF30 =         INITPAGE0:	EQU	0BF30H	;array used to init Page 0 locations
                
 BFFA =         VECTOR:	EQU	0BFFAH	;Vectors
                
 A800 =         VIABASE:	EQU	0A800H	;6522 base address
                
 0002 =         PAGE0:	EQU	0002H		;Page 0 storage
                ;
                ;======================================================================
                ; Define delay values of the 6502 S.B.C. 
                ;======================================================================
                ;
                ; WAIT1VAL - Delay before starting to scan switches when put in Run mode
                ; WAIT2VAL - Delay before turning on the alarm relay and alarm led
                ; WAIT3VAL - Delay before turning off the alarm relay
                ; WAITAVAL - Read Control Switch ( RDCSW ) switch debounce delay
                ; WAITBVAL - Read Port A ( RDPTA ) switch debounce delay
                
                ;	WAIT1VAL:	EQU	03CH	;60 second
                
                ;	WAIT2VAL:	EQU	03CH	;60 second
                
 0002 =         	WAIT1VAL:	EQU	02H	; 2 seconds
                
 0002 =         	WAIT2VAL:	EQU	02H	; 2 seconds
                
 000F =         	WAIT3VAL:	EQU	00FH	;15 minutes
                
 000A =         	WAITAVAL:	EQU	0AH	;10 x 50 msec
                	
 000A =         	WAITBVAL:	EQU	0AH	;10 x 50 msec
                ;
                ;======================================================================
                ; Define Page 0 memory allocation
                ;======================================================================
                ;
 0002           	ORG     PAGE0
                
 0002           ARYADR:	DFS	2 * 1	; 6522 VIA initialization array address pointer
 0004           PRTADR:	DFS	2 * 1	; 6522 port address pointer
 0006           RAMADR:	DFS	2 * 1	; Page 0 variable table address pointer
 0008           EPMADR:	DFS	2 * 1	; Eprom table address pointer
 000A           VIA:		DFS	2 * 1	; 6522 VIA chip base address
 000C           MASK:		DFS	1    	; Port A mask value
 000D           COUNT:	DFS	1    	; 50 msec counter for clock routine
 000E           SEC:		DFS	1    	; Seconds counter for wait loops
 000F           CSW:		DFS	1    	; Control switch value
 0010           ALMSW:	DFS	1    	; Port A alarm switch value
 0011           MIN:		DFS	1    	; Minutes counter for wait loops
 0012           SECONDS:	DFS	1    	; Seconds counter for clock routine
 0013           MINUTES:	DFS	1    	; Minutes counter fro clock routine
 0014           CNT:		DFS	1    	; 50 msec counter for RDCSW and RDPTA routine
 0015           HOURS:	DFS	1    	; Hours counter for clock routine
 0016           HR:		DFS	1    	; Hours counter for wait loops
 0017           TEMP:		DFS	1    	; Temporary storage of Port A input value
                ;
                ;======================================================================
                ; 6522 VIA Addresses
                ;======================================================================
                ;
                ;	A800H		ORB/IRB	Output / Input register B
                ;	A801H		ORA/IRA	Output / Input register A
                ;	A802H		DDR B		Data Direction Register A
                ;	A803H		DDR A		Data Direction Register B
                ;	A804H		T1C-L		Timer # 1 Low order latch/Low order counter
                ;	A805H		T1C-H		Timer # 1 High order counter
                ;	A806H		T1L-L		Timer # 1 Low order latches
                ;	A807H		T1L-H		Timer # 1 High order latches
                ;	A808H		T2C-L		Timer # 2 Low order latch/Low order counter
                ;	A809H		T2C-H		Timer # 2 High order counter
                ;	A80AH		SR		Shift register
                ;	A80BH		ACR		Auxillary Control Register
                ;	A80CH		PCR		Peripheral Control Register
                ;	A80DH		IFR		Interrupt Flag Register
                ;	A80EH		IER		Interrupt Enable Register
                ;	A80FH		ORA/IRA	Output Register A ( no effect on Handshaking )
                ;
                ;---------------------------------------------------------------------------
                ;
 A800           	ORG     VIABASE
                
 A800           ORB:	DFS	1		;Output Register B
 A800 =         IRB:	EQU	ORB   	;Input Register B
 A801           ORA:	DFS	1		;Output Register A
 A801 =         IRA:	EQU	ORA		;Input Register A
 A802           DDRB:	DFS	1		;Data Direction Register B
 A803           DDRA:	DFS	1		;Data Direction Register A
 A804           T1CL:	DFS	1		;read:  T1 counter, low-orde
                				;write: T1 latches, low-order
 A805           T1CH:	DFS	1		;T1 counter, high-order
 A806           T1LL:	DFS	1		;T1 latches, low-order
 A807           T1LH:	DFS	1		;T1 latches, high-order
 A808           T2CL:	DFS	1		;read:  T2 counter, low-order
                                		;write: T2 latches, low-order
 A809           T2CH:	DFS	1		;T2 counter, high-order
 A80A           SR:	DFS	1		;Shift Register
 A80B           ACR:	DFS	1		;Auxiliary Control Register
 A80C           PCR:	DFS	1		;Peripheral Control Register
 A80D           IFR:	DFS	1		;Interrupt Flag Register
 A80E           IER:	DFS	1		;Interrupt Enable Register
                ;
                ;======================================================================
                ; 6522 VIA Initialization Array
                ;======================================================================
                ;
 BF00           		ORG	INITVIA
                
 BF00 08        ARYENT_AR:	DFB	008H		; # of entries in 6522 VIA initialization
                					; array
 BF01 03A8      DDRA_AR:	DWL	0A803H	; Data Direction Register A address
 BF03 00        		DFB	000H		; Set Port A to all outputs
 BF04 00A8      IORB_AR:	DWL	0A800H	; Input/Output Register B address
 BF06 00        		DFB	000H		; Initial output to Port B is 00
 BF07 02A8      DDRB_AR:	DWL	0A802H	; Data Direction Register B address
 BF09 1F        		DFB	01FH		; PB0 to PB4 = Outputs PB5 to PB7 = Inputs
 BF0A 0CA8      PCR_AR:	DWL	0A80CH	; Peripheral Control Register address
 BF0C AA        		DFB	0AAH		; CA2/CB2 set for pulse output mode
 BF0D 0BA8      ACR_AR:	DWL	0A80BH	; Auxillary Control Register address
 BF0F 40        		DFB	040H		; Timer # 1 set for Free Run Mode
 BF10 0EA8      IER_AR1:	DWL	0A80EH	; Interrupt Enable Register Address
 BF12 3F        		DFB	03FH		; Disable all other interrupts
 BF13 0DA8      IFR_AR:	DWL	0A80DH	; Interrupt Flag Register address
 BF15 C0        		DFB	0C0H		; Clear Timer # 1 Interrupt flag
 BF16 0EA8      IER_AR2:	DWL	0A80EH	; Interrupt Enable Register address
 BF18 C0        		DFB	0C0H		; Enable Timer # 1 interrupts
                ;
                ;======================================================================
                ; Page 0 Initial Values
                ;======================================================================
                ;
 BF30           		ORG	INITPAGE0
                
 BF30 0D        RAMENT_AR:	DFB	00DH		; # of entries in RAM Initialization table
 BF31 0A00      STADR_AR:	DWL	{VIA}		; Base address of RAM Variable Table
 BF33 00A8      VIA_AR:	DWL	0A800H	; VIA Chip base address
 BF35 FF        MASK_AR:	DFB	0FFH		; Initial Port A mask value
                					; ( All switches OK )
 BF36 14        COUNT_AR:	DFB	014H		; Initial 50 msec count value for clock
                					; routine
 BF37 00        SEC_AR:	DFB	00H		; Initial seconds counter value for wait loops
 BF38 C0        CSW_AR:	DFB	0C0H		; Initial Control Switch value ( Run mode )
 BF39 00        ALMSW_AR:	DFB	00H		; Initial Port A alarm data ( No alarms )
 BF3A 00        MIN_AR:	DFB	00H		; Initial minutes counter value for wait loops
 BF3B 00        SECONDS_AR:	DFB	00H		; Initial seconds for clock routine
 BF3C 00        MINUTES_AR:	DFB	00H		; Initial minutes for clock routine
 BF3D 00        CNT_AR:	DFB	00H		; Initial 50 msec counter value for RDCSW 
                					; and RDPTA
 BF3E 00        HOURS_AR:	DFB	00H		; Initial hours for clock routine
 BF3F 00        HR_AR:	DFB	00H		; Initial hours counter value for wait loops
                ;
                ;======================================================================
                ; Define address in EPROM where INTERRUPT Vectors stored
                ;======================================================================
                ;
 BFFA           		ORG  VECTOR		;Must start at 0BFFAH for the Vectors 
                					;to be equivalent to 0FFFAH !!!
                
 BFFA 00B8      NMI:		DWL	0B800H	; Non-Maskable Interrupt Vector
                					; ( Same as a Reset )
 BFFC 00B8      RESETVEC:	DWL	0B800H	; Reset Vector
                					; ( " Power on reset " vector )
 BFFE 10BA      BREAK:	DWL	{CLKINT}	; IRQ and Break Vector
                					; ( Clock routine )
                ;
                ;======================================================================
                ; Define starting address EPROM HEX file
                ;======================================================================
                ;
 B800           		ORG  MAIN		;
                ;
                ;**********************************************************************
                ;                         MAIN ROUTINE
                ;**********************************************************************
                ;
                ;----------------------------------------------------------------------
                ; Stack Initialization
                ;----------------------------------------------------------------------
                ;
 B800           RESET:
 B800 A2FF      	LDX	#0FFH		;
 B802 9A        	TXS			; Initialize the stack pointer
                ;
                ;----------------------------------------------------------------------
                ; 6522 Initialization routine
                ;----------------------------------------------------------------------
                ;
                ; Set up the VIA array address pointer on Page 0 first
                ;
 B803 A9BF      	LDA	#HIGH{INITVIA+1}	; Initialize starting address of VIA array
 B805 8503      	STA	ARYADR+1		; located on Page 0 locations 0003/0002
 B807 A901      	LDA	#LOW{INITVIA+1}	;
 B809 8502      	STA	ARYADR		;
                
 B80B AD00BF    	LDA	ARYENT_AR 	; Get the number of entries in the VIA array
 B80E AA        	TAX			; Set X register to number of VIA array entries
                				; There are a total of 8 entries in the VIA array.
                				; These 8 values are copied from EPROM to Page 0
                ;
                ; Now copy values from the VIA array in EPROM to Page 0 locations
                ;
 B80F           LOOP:
 B80F A000      	LDY	#00H		; Set up the VIA array index value
 B811 B102      	LDA	(ARYADR),Y	; Get Low Byte of Port A DDR-A address and store in 
 B813 8504      	STA	PRTADR	; PRTADR ( Location 0004 )
 B815 C8        	INY			; Increment array index
 B816 B102      	LDA	(ARYADR),Y	; Get High Byte of Port A DDR-A address and store in 
 B818 8505      	STA	PRTADR+1	; PRTADR + 1 ( Location 0005 )
 B81A C8        	INY			; Increment array index
 B81B B102      	LDA	(ARYADR),Y	; Get Initial Port A value of 00H
 B81D A000      	LDY	#00H		; Set array index to 0
 B81F 9104      	STA	(PRTADR),Y	; Output to Port A DDR-A. This sets all of Port A
                				; to inputs
 B821 A502      	LDA	ARYADR	; Bump up the VIA array address value pointer
 B823 18        	CLC			; Clear carry
 B824 6903      	ADC	#03H		; Add 3 to Low Byte of Array address
 B826 8502      	STA	ARYADR	;
 B828 9002      	BCC	DECR		;
 B82A E603      	INC	ARYADR+1	; Increment High Byte if a Carry exists
 B82C           DECR:
 B82C CA        	DEX			; Decrement # of VIA array entries - If 0 then we 
 B82D D0E0      	BNE	LOOP		; are done setting up Page 0 VIA locations
                ;
                ;----------------------------------------------------------------------
                ; Initialize RAM Variable Table
                ;----------------------------------------------------------------------
                ;
 B82F AD30BF    	LDA	RAMENT_AR	; Get # of RAM table entries from Array in Eprom
 B832 AA        	TAX			; Set X register to # of RAM table entries
                				; There are a total of 0DH ( 13 decimal ) entries
                				; These values are copied from EPROM to Page 0
 B833 AD31BF    	LDA	STADR_AR	; Initialize RAM variable table starting address
 B836 8506      	STA	RAMADR	; located on Page 0 
 B838 AD32BF    	LDA	STADR_AR+1	;
 B83B 8507      	STA	RAMADR+1	;
                ;
                ; Set up the EPROM array address pointer on Page 0 first
                ;
 B83D A933      	LDA	#LOW{VIA_AR}	;
 B83F 8508      	STA	EPMADR		; Initialize EPROM table starting address
 B841 A9BF      	LDA	#HIGH{VIA_AR+1}	; located on Page 0 locations 0008/0009
 B843 8509      	STA	EPMADR+1		;
 B845 A000      	LDY	#00H			; Set index to 0
                ;
                ; Now copy values from the VIA array in EPROM to Page 0 locations
                ;
 B847           GETDTA:
 B847 B108      	LDA	(EPMADR),Y	; Get EPROM data
 B849 9106      	STA	(RAMADR),Y	; Store in RAM variable table
 B84B C8        	INY			; Increment index pointer
 B84C CA        	DEX			; Decrement # of array entries, If 0 then done
 B84D D0F8      	BNE	GETDTA	;
                ;
                ; Some minor housekeeping
                ;
 B84F D8        	CLD			; Clear Decimal mode flag
 B850 58        	CLI			; Clear Interrupt disable bit
                ;
                ;----------------------------------------------------------------------
                ; Start Timer # 1 in Free Run mode with 50 msec count. This will be used
                ; for the Real Time Clock routine to calculate various delays
                ;----------------------------------------------------------------------
                ;
 B851 A004      	LDY	#04H		; Load Index
 B853 A950      	LDA	#50H		; 
 B855 910A      	STA	(VIA),Y	; Load Timer # 1 Low Latch with 50
 B857 C8        	INY			;
 B858 A9C3      	LDA	#0C3H		;
 B85A 910A      	STA	(VIA),Y	; Load Timer # 1 High Latch with 0C3H
                				; ( Timer # 1 is now started )
                ;
                ;----------------------------------------------------------------------
                ; Initial PORT A read and Control Switch read
                ;----------------------------------------------------------------------
                ;
 B85C 20D3B9    	JSR	INITRD	; Read Port A alarm switches. This is the Initial 
                				; read of Port A alarm switches. The initial value
                				; from Port A is used as a mask for normal scanning.
                				; So....if a switch is open when Run is first 
                				; entered then that switch will be ignored in any
                				; further scanning of inputs.
 B85F           LOOP1:
 B85F 2094B9    	JSR RDCSW		; Read the Control switch position 
 B862 A50F      	LDA	CSW		; Get Control switch position
 B864 C9C0      	CMP	#0C0H		; and see if its RUN position ?
 B866 F017      	BEQ	RUN		;
 B868 C9A0      	CMP	#0A0H		; DISABLE position ?
 B86A F00D      	BEQ	DISABLE1	;
 B86C C960      	CMP	#060H		;
 B86E F00C      	BEQ	TEST1		; TEST position ?
 B870 A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B872 A91C      	LDA	#01CH		;
 B874 910A      	STA	(VIA),Y	; Turn on RUN, DISABLE, and TEST Leds to indicate
                				; no valid Control switch position read.
 B876 4C5FB8    	JMP	LOOP1		; Continue scanning Control switch
 B879           DISABLE1:
 B879 4C48B9    	JMP	DISABLE	;
 B87C           TEST1:
 B87C 4C6EB9    	JMP	TEST		;
                ;
                ;----------------------------------------------------------------------
                ; Control Switch in RUN Position
                ;----------------------------------------------------------------------
                ;
 B87F           RUN:
 B87F A000      	LDY	#00H		; Load index to point to DDR-A
 B881 A904      	LDA	#04H		;
 B883 910A      	STA	(VIA),Y	; Turn on the RUN Led
 B885 A900      	LDA	#00H		; Set Seconds counter to 0
 B887 850E      	STA	SEC		;
 B889           LOOP2:
 B889 2094B9    	JSR	RDCSW		; Read the Control switch position 
 B88C A50F      	LDA	CSW		; Get Control switch position
 B88E C9C0      	CMP	#0C0H		; and see if its RUN position ? 
 B890 F017      	BEQ	WAIT1		;
 B892 C9A0      	CMP	#0A0H		;
 B894 F00D      	BEQ	DISABLE2	; DISABLE position ?
 B896 C960      	CMP	#060H		;
 B898 F00C      	BEQ	TEST2		; TEST position ?
 B89A A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B89C A91C      	LDA	#01CH		;
 B89E 910A      	STA	(VIA),Y	; Turn on RUN, DISABLE, and TEST Leds to indicate
                				; no valid Control switch position read.
 B8A0 4C89B8    	JMP	LOOP2		; Continue scanning Control switch
 B8A3           DISABLE2:
 B8A3 4C48B9    	JMP	DISABLE	;
 B8A6           TEST2:
 B8A6 4C6EB9    	JMP	TEST		;
                ;
                ;----------------------------------------------------------------------
                ; This wait loop gives a 60 second delay prior to starting the normal
                ; scan. This is the EXIT delay, and allows one to turn the Alarm on,
                ; then exit the building without activating the alarm.
                ; To change this delay change the value of WAIT1VAL
                ;----------------------------------------------------------------------
                ;
 B8A9           WAIT1:
 B8A9 A902      	LDA	#WAIT1VAL	;
 B8AB C50E      	CMP	SEC		; See if 60 seconds has elapsed
 B8AD F003      	BEQ	SCAN		; Yes, so start normal scan
 B8AF 4C89B8    	JMP	LOOP2		; No, so continue scanning Control switch
                ;	JMP	DISABLE	;
                ;	JMP	TEST		;
                ;
                ;----------------------------------------------------------------------
                ; Normal Alarm Switch Scanning
                ;----------------------------------------------------------------------
                ;
 B8B2           SCAN:
 B8B2 20DFB9    	JSR	RDPTA		; Read Port A alarm switches. 
 B8B5 A510      	LDA	ALMSW		; Get the alarm switch data
 B8B7 250C      	AND	MASK		; and mask with the Initial Port A input data
 B8B9 D020      	BNE	TAMPER	; If not 0 then check the TAMPER bit ( Bit 7 )
 B8BB           LOOP3:
 B8BB 2094B9    	JSR	RDCSW		; Read the Control switch position 
 B8BE A50F      	LDA	CSW		; Get Control switch position
 B8C0 C9C0      	CMP	#0C0H		; and see if its RUN position ?
 B8C2 F0EE      	BEQ	SCAN		;
 B8C4 C9A0      	CMP	#0A0H		; DISABLE position ?
 B8C6 F00D      	BEQ	DISABLE3	;
 B8C8 C960      	CMP	#060H		; TEST position ?
 B8CA F00C      	BEQ	TEST3		;
 B8CC A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B8CE A91C      	LDA	#01CH		; 
 B8D0 910A      	STA	(VIA),Y	; Turn on RUN, DISABLE, and TEST Leds to indicate
                				; no valid Control switch position read.
 B8D2 4CBBB8    	JMP	LOOP3		; Continue scanning Control switch
 B8D5           DISABLE3:
 B8D5 4C48B9    	JMP	DISABLE	;
 B8D8           TEST3:
 B8D8 4C6EB9    	JMP	TEST		;
                ;
                ;----------------------------------------------------------------------
                ; Check TAMPER switch input first !
                ;----------------------------------------------------------------------
                ;
 B8DB           TAMPER:
 B8DB 18        	CLC			; Clear carry flag	
 B8DC 2A        	ROL	A		; Bit 7 is the Tamper input
 B8DD B02D      	BCS	ALARM		; If Bit 7 = 1 the Tamper is active
 B8DF A900      	LDA	#00H		; 
 B8E1 850E      	STA	SEC		; Set seconds counter to 0
 B8E3           LOOP4:
 B8E3 2094B9    	JSR	RDCSW		; Read the Control switch position
 B8E6 A50F      	LDA	CSW		; Get Control switch position
 B8E8 C9C0      	CMP	#0C0H		; and see if its RUN position ?
 B8EA F017      	BEQ	WAIT2		;
 B8EC C9A0      	CMP	#0A0H		;
 B8EE F00D      	BEQ	DISABLE4	; DISABLE position ?
 B8F0 C960      	CMP	#060H		;
 B8F2 F00C      	BEQ	TEST4		; TEST position ?
 B8F4 A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B8F6 A91C      	LDA	#01CH		; 
 B8F8 910A      	STA	(VIA),Y	; Turn on RUN, DISABLE, and TEST Leds to indicate
                				; no valid Control switch position read.
 B8FA 4CE3B8    	JMP	LOOP4		; Continue scanning Control switch
 B8FD           DISABLE4:
 B8FD 4C48B9    	JMP	DISABLE	;
 B900           TEST4:
 B900 4C6EB9    	JMP	TEST		;
                ;
                ;----------------------------------------------------------------------
                ; This wait loop gives a 60 second delay prior to turning on the Alarm
                ; Led and Alarm relay. 
                ; To change this delay change the value of WAIT2VAL
                ;----------------------------------------------------------------------
                ;
 B903           WAIT2:
 B903 A902      	LDA	#WAIT2VAL	;
 B905 C50E      	CMP	SEC		; See if 60 seconds has elapsed
 B907 F003      	BEQ	ALARM		; Yes, so turn on Alarm relay
 B909 4CE3B8    	JMP	LOOP4		; No, so continue scanning Control switch
                ;
                ;----------------------------------------------------------------------
                ; Alarm Led and Alarm Relay activated
                ;----------------------------------------------------------------------
                ;
 B90C           ALARM:
 B90C A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B90E A907      	LDA	#07H		;
 B910 910A      	STA	(VIA),Y	; Turn on Alarm Led and Alarm relay
 B912 A900      	LDA	#00H		; 
 B914 8511      	STA	MIN		; Set Minutes counter to 0
 B916           LOOP5:
 B916 2094B9    	JSR	RDCSW		; Read the Control switch position
 B919 A50F      	LDA	CSW		; Get Control switch position
 B91B C9C0      	CMP	#0C0H		; and see if its RUN position ?
 B91D F017      	BEQ	WAIT3		;
 B91F C9A0      	CMP	#0A0H		;
 B921 F00D      	BEQ	DISABLE5	; DISABLE position ?
 B923 C960      	CMP	#060H		;
 B925 F00C      	BEQ	TEST5		; TEST position ?
 B927 A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B929 A91C      	LDA	#01CH		;
 B92B 910A      	STA	(VIA),Y	; Turn on RUN, DISABLE, and TEST Leds to indicate
                				; no valid Control switch position read.
 B92D 4C16B9    	JMP	LOOP5		; Continue scanning Control switch
 B930           DISABLE5:
 B930 4C48B9    	JMP	DISABLE	;
 B933           TEST5:
 B933 4C6EB9    	JMP	TEST		;
                ;
                ;----------------------------------------------------------------------
                ; This wait loop gives a 15 minute delay prior to turning off the Alarm
                ; Led and Alarm relay. 
                ; To change this delay change the value of WAIT3VAL
                ;----------------------------------------------------------------------
                ;
 B936           WAIT3:
 B936 A90F      	LDA	#WAIT3VAL	;
 B938 C511      	CMP	MIN		; See if 15 minutes has elapsed
 B93A F003      	BEQ	OFF		; Yes, so turn off Alarm relay
 B93C 4C16B9    	JMP	LOOP5		; No, so continue scanning Control switch
                ;
                ;----------------------------------------------------------------------
                ; Alarm Led and Alarm Relay de-activated
                ;----------------------------------------------------------------------
                ;
 B93F           OFF:
 B93F A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B941 A906      	LDA	#006H		;
 B943 910A      	STA	(VIA),Y	; Turn off Alarm Led and Alarm relay
                ;
 B945 4CB2B8    	JMP	SCAN		; Go back and start scan again
                ;
                ;----------------------------------------------------------------------
                ; Disable Routine
                ;----------------------------------------------------------------------
                ;
 B948           DISABLE:
 B948 A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B94A A908      	LDA	#08H		;
 B94C 910A      	STA	(VIA),Y	; Turn off Alarm Led, Alarm relay, and turn on 
                				; Disable Led
 B94E           LOOP6:
 B94E 2094B9    	JSR	RDCSW		; Read the Control switch position
 B951 A50F      	LDA	CSW		; Get Control switch position
 B953 C9C0      	CMP	#0C0H		; and see if its RUN position ?
 B955 F011      	BEQ	RESET1	;
 B957 C9A0      	CMP	#0A0H		;
 B959 F0F3      	BEQ	LOOP6		; DISABLE position ?
 B95B C960      	CMP	#060H		;
 B95D F00F      	BEQ	TEST		; TEST position ?
 B95F A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B961 A91C      	LDA	#01CH		;
 B963 910A      	STA	(VIA),Y	; Turn on RUN, DISABLE, and TEST Leds to indicate
                				; no valid Control switch position read.
 B965 4C4EB9    	JMP	LOOP6		; Continue scanning Control switch
 B968           RESET1:
 B968 4C00B8    	JMP	RESET		;
 B96B 4C6EB9    	JMP	TEST		;
                ;
                ;----------------------------------------------------------------------
                ; Test Routine
                ;----------------------------------------------------------------------
                ;
 B96E           TEST:
 B96E A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B970 A913      	LDA	#13H		;
 B972 910A      	STA	(VIA),Y	; Turn on Alarm Led, Alarm relay, and turn on 
                				; Test Led
 B974           LOOP7:
 B974 2094B9    	JSR	RDCSW		; Read the Control switch position
 B977 A50F      	LDA	CSW		; Get Control switch position
 B979 C9C0      	CMP	#0C0H		; and see if its RUN position ?
 B97B F011      	BEQ	RESET2	;
 B97D C9A0      	CMP	#0A0H		;
 B97F F0C7      	BEQ	DISABLE	; DISABLE position ?
 B981 C960      	CMP	#060H		;
 B983 F0EF      	BEQ	LOOP7		; TEST position ?
 B985 A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B987 A91C      	LDA	#01CH		;
 B989 910A      	STA	(VIA),Y	; Turn on RUN, DISABLE, and TEST Leds to indicate
                				; no valid Control switch position read.
 B98B 4C74B9    	JMP	LOOP7		; Continue scanning Control switch
 B98E           RESET2:
 B98E 4C00B8    	JMP	RESET		;
 B991 4C48B9    	JMP	DISABLE	;
                ;
                ;======================================================================
                ; 			Read Control Switch Subroutine
                ;======================================================================
                ;
 B994           RDCSW:
 B994 A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B996 B10A      	LDA	(VIA),Y	; Read Port B 
 B998 29E0      	AND	#0E0H		; Mask out bits 0 to 4
 B99A 850F      	STA	CSW		; Store the Control Switch value
 B99C A900      	LDA	#00H		; 
 B99E 8514      	STA	CNT		; Set the 50 msec counter to zero
 B9A0 A90A      	LDA	#WAITAVAL	;
 B9A2           WAITA:
 B9A2 C514      	CMP	CNT		; Wait for 0.5 seconds ( 10 x 50 msec )
 B9A4 F003      	BEQ	READ		;
 B9A6 4CA2B9    	JMP	WAITA		;
 B9A9           READ:
 B9A9 A000      	LDY	#00H		; Load index to point to ORB ( Output Register B )
 B9AB B10A      	LDA	(VIA),Y	; Read Port B again
 B9AD 29E0      	AND	#0E0H		; Mask out bits 0 to 4
 B9AF C50F      	CMP	CSW		; Compare new value to old value
 B9B1 F003      	BEQ	EXIT1		; If same continue, else read it again
 B9B3 4C94B9    	JMP	RDCSW		;
                ;
                ; Control Switch is the same position
                ;
 B9B6           EXIT1:
 B9B6 A50F      	LDA	CSW		; Get Control Switch value
 B9B8 C9C0      	CMP	#0C0H		; and check if its Run position
 B9BA F007      	BEQ	RUNPOS	;
                ;
                ; Watchdog output - Pulse the CB2 line
                ;
 B9BC           EXIT:
 B9BC A000      	LDY	#00H		; Not in Run position
 B9BE B10A      	LDA	(VIA),Y	; Read Port B
 B9C0 910A      	STA	(VIA),Y	; Write to Port B
                ;
 B9C2 60        	RTS			; Return from subroutine
                ;
                ; Check Disable switch
                ;
 B9C3           RUNPOS:
 B9C3 20DFB9    	JSR	RDPTA		; Read Port A data inputs
 B9C6 A510      	LDA	ALMSW		; Get Port A input value
 B9C8 2940      	AND	#40H		; Only need Bit 6 ( Disable switch )
 B9CA D004      	BNE	END		;
 B9CC A9A0      	LDA	#0A0H		; Disable switch closed, so set Control switch
 B9CE 850F      	STA	CSW		; value to Disable position
 B9D0           END:
 B9D0 4CBCB9    	JMP	EXIT		;
                ;
                ;======================================================================
                ;			Initial Port A Read Subroutine
                ;======================================================================
                ;
 B9D3           INITRD:
 B9D3 20DFB9    	JSR	RDPTA		; Read Port A alarm switches
 B9D6 A510      	LDA	ALMSW		; Get switch data
 B9D8 49FF      	EOR	#0FFH		; Complement the data
 B9DA 29BF      	AND	#0BFH		; Mask out Bit 6 which is the Disable switch
 B9DC 850C      	STA	MASK		; Save this as Port A mask value
 B9DE 60        	RTS			;
                ;
                ;======================================================================
                ;			Read Port A Subroutine
                ;======================================================================
                ;
 B9DF           RDPTA:
 B9DF 20FCB9    	JSR	RDDTA		; Read Port A alarm switches 
 B9E2 8510      	STA	ALMSW		; Save switch data
 B9E4 A900      	LDA	#00H		;
 B9E6 8514      	STA	CNT		; Set the 50 msec counter to zero
 B9E8 A90A      	LDA	#WAITBVAL	;
 B9EA           WAITB:
 B9EA C514      	CMP	CNT		; Wait for 0.5 seconds ( 10 x 50 msec )
 B9EC F003      	BEQ	READA		;
 B9EE 4CEAB9    	JMP	WAITB		;
 B9F1           READA:
 B9F1 20FCB9    	JSR	RDDTA		; Read Port A alarm switches again
 B9F4 C510      	CMP	ALMSW		; Compare new data to old data
 B9F6 F003      	BEQ	EXITA		; If same exit, else
 B9F8 4CDFB9    	JMP	RDPTA		; go back and read switches again
 B9FB           EXITA:
 B9FB 60        	RTS			; Return
                ;
                ;======================================================================
                ;			New Read Port A Subroutine
                ;======================================================================
                ;
 B9FC           RDDTA:
 B9FC A001      	LDY	#01H		;
 B9FE B10A      	LDA	(VIA),Y	; Read Port A inputs
 BA00 8517      	STA	TEMP		; Save Port A value
 BA02 18        	CLC			; Clear carry
 BA03 2A        	ROL	A		; Shift Bit 7 to carry
 BA04 B005      	BCS	OK		; Check if bit 7 = 1
 BA06 A517      	LDA	TEMP		; Get Port A value	Reverse Sense of Bit 7
 BA08 0980      	ORA	#80H		; and set Bit 7
 BA0A 60        	RTS			; Return
 BA0B           OK:
 BA0B A517      	LDA	TEMP		; Get Port A value
 BA0D 297F      	AND	#7FH		; and clear Bit 7		Reverse Sense of Bit 7
 BA0F 60        	RTS			; Return
                ;
                ;======================================================================
                ;			Clock Interrupt Subroutine
                ;======================================================================
                ;
 BA10           CLKINT:
 BA10 08        	PHP			; Push processor status on stack
 BA11 48        	PHA			; Push A on stack
 BA12 8A        	TXA			; Transfer X to A
 BA13 48        	PHA			; Push X on stack
 BA14 98        	TYA			; Transfer Y to A
 BA15 48        	PHA			; Push Y on stack
 BA16 A004      	LDY	#04H		; Load index
 BA18 B10A      	LDA	(VIA),Y	; Read Timer#1 low order counter to clear int flag
 BA1A E614      	INC	CNT		; Increment 50 msec counter
 BA1C C60D      	DEC	COUNT		; Decrement 50 msec count value
 BA1E D02A      	BNE	EXIT2		; If 0 then ( 20 x 50 msec ) = 1.0 sec has elapsed
 BA20 E60E      	INC	SEC		; Increment seconds counter
 BA22 A914      	LDA	#14H		; 
 BA24 850D      	STA	COUNT		; Reset 50 msec count value
 BA26 A901      	LDA	#01H		; 
 BA28 18        	CLC			;
 BA29 6512      	ADC	SECONDS	; Add 1 to seconds
 BA2B 8512      	STA	SECONDS	;
 BA2D C93C      	CMP	#3CH		; Has 60 seconds elapsed ?
 BA2F D019      	BNE	EXIT2		;
 BA31 E611      	INC	MIN		; Increment minutes counter
 BA33 A900      	LDA	#00H		;
 BA35 8512      	STA	SECONDS	; Reset seconds count to zero
 BA37 A901      	LDA	#01H		;
 BA39 18        	CLC			;
 BA3A 6513      	ADC	MINUTES	; Add 1 to minutes
 BA3C 8513      	STA	MINUTES	;
 BA3E C93C      	CMP	#3CH		; Has 60 minutes elapsed ?
 BA40 D008      	BNE	EXIT2		;
 BA42 E616      	INC	HR		; Increment hours counter
 BA44 A900      	LDA	#00H		;
 BA46 8513      	STA	MINUTES	; Reset minutes counter to zero
 BA48 E615      	INC	HOURS		; Increment hours
 BA4A           EXIT2:
 BA4A 68        	PLA			; Pull A from stack
 BA4B A8        	TAY			; Restore Y register
 BA4C 68        	PLA			;
 BA4D AA        	TAX			; Restore X register
 BA4E 68        	PLA			; Restore A register
 BA4F 28        	PLP			; Restore processor status
 BA50 40        	RTI			; Return from Interrupt
                ;
                ;
                ;
 0000           	END
.A80B  ACR                BF0D  ACR_AR             B90C  ALARM              
0010  ALMSW              BF39  ALMSW_AR           0002  ARYADR             
BF00  ARYENT_AR          BFFE  BREAK              BA10  CLKINT             
0014  CNT                BF3D  CNT_AR             000D  COUNT              
BF36  COUNT_AR           000F  CSW                BF38  CSW_AR             
A803  DDRA               BF01  DDRA_AR            A802  DDRB               
BF07  DDRB_AR            B82C  DECR               B948  DISABLE            
B879  DISABLE1           B8A3  DISABLE2           B8D5  DISABLE3           
B8FD  DISABLE4           B930  DISABLE5           B9D0  END                
0008  EPMADR             B9BC  EXIT               B9B6  EXIT1              
BA4A  EXIT2              B9FB  EXITA              B847  GETDTA             
0015  HOURS              BF3E  HOURS_AR           0016  HR                 
BF3F  HR_AR              A80E  IER                BF10  IER_AR1            
BF16  IER_AR2            A80D  IFR                BF13  IFR_AR             
BF30  INITPAGE0          B9D3  INITRD             BF00  INITVIA            
BF04  IORB_AR            A801  IRA                A800  IRB                
B80F  LOOP               B85F  LOOP1              B889  LOOP2              
B8BB  LOOP3              B8E3  LOOP4              B916  LOOP5              
B94E  LOOP6              B974  LOOP7              B800  MAIN               
000C  MASK               BF35  MASK_AR            0011  MIN                
0013  MINUTES            BF3C  MINUTES_AR         BF3A  MIN_AR             
BFFA  NMI                B93F  OFF                BA0B  OK                 
A801  ORA                A800  ORB                0002  PAGE0              
A80C  PCR                BF0A  PCR_AR             0004  PRTADR             
0006  RAMADR             BF30  RAMENT_AR          B994  RDCSW              
B9FC  RDDTA              B9DF  RDPTA              B9A9  READ               
B9F1  READA              B800  RESET              B968  RESET1             
B98E  RESET2             BFFC  RESETVEC           B87F  RUN                
B9C3  RUNPOS             B8B2  SCAN               000E  SEC                
0012  SECONDS            BF3B  SECONDS_AR         BF37  SEC_AR             
A80A  SR                 BF31  STADR_AR           A805  T1CH               
A804  T1CL               A807  T1LH               A806  T1LL               
A809  T2CH               A808  T2CL               B8DB  TAMPER             
0017  TEMP               B96E  TEST               B87C  TEST1              
B8A6  TEST2              B8D8  TEST3              B900  TEST4              
B933  TEST5              BFFA  VECTOR             000A  VIA                
A800  VIABASE            BF33  VIA_AR             B8A9  WAIT1              
0002  WAIT1VAL           B903  WAIT2              0002  WAIT2VAL           
B936  WAIT3              000F  WAIT3VAL           B9A2  WAITA              
000A  WAITAVAL           B9EA  WAITB              000A  WAITBVAL