$	PAGEWIDTH(80)
;	LEADLESS MACHINE CONTROL TEST
;	NOV 18,1981	VER 1.2  WHH

;8100R	INPUTS	D0	ENABLE
;		D1	CONV OPT
;		D2	EXER
;		D3	CAL
;		D4	NORM
;		D5	DROP OPT
;		D6	SORT L/R
;		D7	SORT CL

;8100W	OUTPUTS	D0	CONV A
;		D1	CONV B
;		D2	CONV 3
;		D3	CONV D
;		D4	SOL 1
;		D5	SOL 2
;		D6	SOL 3
;		D7	SOL 4

;8200W	OUTPUTS	D0	DROP A
;		D1	DROP B
;		D2	DROP C
;		D3	DROP D
;		D4	CONT SOL
;		D5	REL SOL

;8300W	OUTPUTS	D0	SORT A
;		D1	SORT B
;		D2	SORT C
;		D3	SORT D


;		DECLARED DATA

CONVST	EQU	33H	;CONV START PHASE
DROPST	EQU	77H	;DROP START PHASE
SORTST	EQU	33H	;SORT START PHASE


;		VARIABLE DATA
MASCTR	EQU	32	;MASTER CLOCK COUNTER
CONVPH	EQU	33	;CONV STEP PHASE
DROPPH	EQU	34	;DROP STEP PHASE
SORT_RENA EQU	35	;SORT RETURN ENA FLAG
SORT_SENA EQU	36	;SORT SORT ENA FLAG
CONV_ENA EQU	37	;CONV ENA FLAG
DROP_ENA EQU	38	;DROP ENA FLAG
SORTCNT EQU	39	;SORTER STEP COUNT
CNVCNT	EQU	40	;CONV STEP COUNT
DRPCNT	EQU	41	;DROP STEP COUNT
REENA	EQU	42	;RE-ENA FLAG
SORTPH	EQU	43	;SORTER STEP PHASE
DRPWT	EQU	44	;DROP STEP DELAY



	ORG	0000H
RSTRT:	AJMP	MAIN

	ORG	0003H
RST1:	RETI

	ORG	000BH
CLCKNT:	AJMP	MASTER_CLOCK

	ORG	0013H
RST2:	RETI

	ORG	001BH
TIMNT:	AJMP	MOTOR_TIMER
	ORG	0023H
RST3:	RETI


MASTER_CLOCK: PUSH	ACC
	PUSH	DPL	;SAVE STATUS
	PUSH	DPH
	PUSH	PSW
	PUSH	P2
	SETB	RS0	;SELECT RB1
	CLR	RS1

	MOV	R0,#MASCTR ;INC MASTER CNTR
	MOV	A,@R0
	INC	A
	MOV	@R0,A
	CJNE	A,#10,MASCL1 ;JUMP IF NOT 100MS

	MOV	DPTR,#8100H ;100MS ROUTINE
	MOV	R0,#CONVPH
	MOV	A,@R0	;TURN INPUT SOL OFF
	ORL	A,#0F0H
	MOVX	@DPTR,A

	MOV	DPTR,#8200H
	MOV	R0,#DROPPH ;TURN TS SOL OFF
	MOV	A,@R0
	ORL	A,#0F0H
	ANL	A,#0DFH	;TURN RELS ON
	MOVX	@DPTR,A
	SJMP	MASCL4

MASCL1:	CJNE	A,#30,MASCL2 ;JUMP IF NOT 300MS
	MOV	R0,#SORT_RENA ;300MS ROUTINE
	MOV	@R0,#0FFH ;SET SORT RET ENA
	ACALL	LSCNT
	SJMP	MASCL4

MASCL2:	CJNE	A,#40,MASCL3 ;JUMP IF NOT 400MS
	MOV	R0,#CONV_ENA ;400MS ROUTINE
	MOV	@R0,#0FFH ;SET CONV ENA
	ACALL	LCNVCT
	MOV	R0,#DROP_ENA ;SET DROP ENA
	MOV	@R0,#0FFH
	ACALL	LDRPCT

	MOV	DPTR,#8200H
	MOV	R0,#DROPPH ;TURN OFF TS SOL
	MOV	A,@R0	;AND RELS SOL
	ORL	A,#0F0H
	MOVX	@DPTR,A
	SJMP	MASCL4

MASCL3:	CJNE	A,#60,MASCL4 ;JUMP IF NOT 600MS
	CALL	SOLON ;TURN ON INPUT SOL
	MOV	DPTR,#8200
	MOV	R0,#DROPPH ;TURN ON CONT
	MOV	A,@R0
	ORL	A,#0F0H
	ANL	A,#0EFH
	MOVX	@DPTR,A
	MOV	R0,#SORT_SENA ;SET SORT ENA
	MOV	@R0,#0FFH
	ACALL	LSCNT
	MOV	R0,#MASCTR ;CLEAR MASTER COUNTER
	MOV	@R0,#00H

MASCL4:	MOV	TH0,#0D8H ;RESET TIMER
	MOV	TL0,#0EFH
	CLR	TF0
	SETB	TR0
	POP	P2	;RESTORE STATUS
	POP	PSW
	POP	DPH
	POP	DPL
	POP	ACC
	RETI


MOTOR_TIMER: PUSH	ACC ;SAVE STATUS
	PUSH	DPL
	PUSH	DPH
	PUSH	PSW
	PUSH	P2
	CLR	RS0	;SELECT RB2
	SETB	RS1

	MOV	R0,#SORT_SENA
	MOV	A,@R0 ;JUMP IF SORT SENA NOT
	JZ	MOTMR1	;SET
	ACALL	SORT_STEPL ;DO SORT STEP
	MOV	R0,#SORTCNT
	MOV	A,@R0
	DEC	A
	MOV	@R0,A
	JNZ	MOTMR2	;JUMP IF SORT NOT DONE
	MOV	R0,#SORT_SENA
	MOV	@R0,#00H;CLEAR SORT SENA

MOTMR1:	MOV	R0,#SORT_RENA
	MOV	A,@R0	;JUMP IR SORT RET ENA
	JZ	MOTMR2	;NOT SET
	ACALL	SORT_STEPR ;DO STEP
	MOV	R0,#SORTCNT
	MOV	A,@R0
	DEC	A
	MOV	@R0,A
	JNZ	MOTMR2 ;JUMP IF NOT DONE
	MOV	R0,#SORT_RENA
	MOV	@R0,#00H ;CLEAR SORT RET ENA

MOTMR2:	MOV	R0,#CONV_ENA ;JUMP IF CONV ENA
	MOV	A,@R0	;NOT SET
	JZ	MOTMR5
	MOV	R0,#CNVCNT ;JUMP IF CONV CTR NOT
	MOV	A,@R0 ;0
	JNZ	MOTMR3
	MOV	DPTR,#8100H ;JUMP IF NO OPTIC
	MOVX	A,@DPTR
	JNB	ACC.1,MOTMR4
	MOV	R0,#CONV_ENA ;CLEAR CONV ENA
	MOV	@R0,#00H
	SJMP	MOTMR5

MOTMR3:	DEC	A ;DECR STEP COUNTER
	MOV	@R0,A
MOTMR4:	ACALL	CONV_STEP ;TAKE STEP

MOTMR5:	MOV	R0,#DRPWT ;CHECK IF TIME TO
	MOV	A,@R0	;MOVE DROP SHUT
	INC	A
	MOV	@R0,A
	CJNE	A,#2,MOTMR8 ;JUMP IF NOT
	MOV	R0,#DROP_ENA ;JUMP IF DROP NOT ENA
	MOV	A,@R0
	JZ	MOTMR8
	MOV	R0,#DRPCNT ;JUMP IF CNTR NOT
	MOV	A,@R0	;0
	JNZ	MOTMR6
	MOV	DPTR,#8100H ;JUMP IF NO OPTIC
	MOVX	A,@DPTR
	JNB	ACC.5,MOTMR7
	MOV	R0,#DROP_ENA ;CLEAR DROP ENA
	MOV	@R0,#00H
	SJMP	MOTMR8

MOTMR6:	DEC	A	;DECR STEP CTR
	MOV	@R0,A
MOTMR7:	ACALL	DROP_STEP ;TAKE STEP

MOTMR8:	MOV	TH1,#0FBH ;RESET TIMER
	MOV	TL1,#2FH
	CLR	TF1
	SETB	TR1
	POP	P2	;RESTORE STATUS
	POP	PSW
	POP	DPH
	POP	DPL
	POP	ACC
	RETI


MAIN:	MOV	SP,#40H	;SET UP STACK
	ACALL	INIT
	ACALL	MOTOR_DIS ;DISABLE MOTORS

MAIN0:	MOV	DPTR,#8100H ;HOLD IF
	MOVX	A,@DPTR ;DISABLED
	JB	ACC.0,MAIN0
	ACALL	REINT	;MOVE MOTORS TO START
	ACALL	TIMST	;START TIMERS

MAIN1:	MOV	DPTR,#8100H
	MOVX	A,@DPTR ;JUMP IF NO DISABLE
	JNB	ACC.0,MAIN2
	MOV	R0,#REENA	;SET REINIT
	MOV	@R0,#0FFH
	ACALL	MOTOR_DIS	;DO DISABLE
	MOV	IE,#00H	;DISABLE INTR
	MOV	TCON,#00H ;CLEAR TIMERS
	SJMP	MAIN1

MAIN2:	MOV	R0,#REENA	;JUMP IF NO
	MOV	A,@R0	;REINIT NEEDED
	JZ	MAIN1
	CLR	A
	MOV	@R0,A	;DO REINIT
	ACALL	REINT
	ACALL	TIMST	;RESTART TIMER
	SJMP	MAIN1


INIT:	MOV	R0,#MASCTR ;CLEAR RAM
	MOV	R2,#20
	CLR	A
INIT1:	MOV	@R0,A
	INC	R0
	DJNZ	R2,INIT1
	RET


TIMST:	MOV	TMOD,#11H ;RESTART TIMERS
	MOV	TH0,#0D8H
	MOV	TL0,#0EFH
	MOV	TH1,#0F8H
	MOV	TL1,#02FH
	MOV	TCON,#50H
	MOV	IE,#8AH	;SET INT ENA
	MOV	IP,#00H	;SET ALL PRIORT = 1
	RET


REINT:	MOV	DPTR,#8100H ;INIT CONV MOTOR
	MOV	R0,#CONVPH
	MOV	A,#CONVST
	MOV	@R0,A
	ORL	A,#0F0H
	MOVX	@DPTR,A

	MOV	DPTR,#8200H ;INIT DROP MOTOR
	MOV	R0,#DROPPH
	MOV	A,#DROPST
	MOV	@R0,A
	ORL	A,#0F0H
	MOVX	@DPTR,A

	MOV	DPTR,#8300H ;INIT SORT MOTOR
	MOV	R0,#SORTPH
	MOV	A,#SORTST
	MOV	@R0,A
	ORL	A,#0F0H
	MOVX	@DPTR,A

	MOV	A,#00H	;WAIT
	CALL	WAIT

REINT1:	MOV	DPTR,#8100H ;DO NEXT MOTOR IF
	MOVX	A,@DPTR	;CONV ON OPTIC
	JB	ACC.1,REINT2
	ACALL	CONV_STEP ;DO STEP
	MOV	A,#0D0H
	ACALL	WAIT
	SJMP	REINT1

REINT2:	MOV	DPTR,#8100H ;DO NEXT MOTOR IF
	MOVX	A,@DPTR ;AT OPTIC
	JB	ACC.5,REINT3
	ACALL	DROP_STEP ;DO STEP
	MOV	A,#80H
	ACALL	WAIT
	SJMP 	REINT2

REINT3:	MOV	DPTR,#8100H ;RET IF SORTER ON
	MOVX	A,@DPTR	;CL
	JB	ACC.7,REINT5
	JNB	ACC.6,REINT4	;DO RIGHT MOVE
	ACALL	SORT_STEPL
	MOV	A,#0D0H	;DO STEP LEFT
	ACALL	WAIT
	SJMP	REINT3

REINT4:	ACALL	SORT_STEPR ;DO STEP RIGHT
	MOV	A,#0D0H
	ACALL	WAIT
	SJMP	REINT3

REINT5:	MOV	A,#40H	;WAIT FOR MOTORS
	ACALL	WAIT
	RET


CONV_STEP:MOV	DPTR,#8100H
	MOV	R0,#CONVPH
	MOV	A,@R0	;FORM NEW STEP
	RL	A
	MOV	@R0,A	;SAVE
	ORL	A,#0F0H ;WRITE TO MOTOR ONLY
	MOVX	@DPTR,A
	RET


DROP_STEP: MOV	DPTR,#8200H
	MOV	R0,#DROPPH ;FORM NEW STEP
	MOV	A,@R0
	RL	A
	MOV	@R0,A
	ORL	A,#0F0H	;WRITE TO MOTOR ONLY
	MOVX	@DPTR,A
	RET


SORT_STEPL: MOV	DPTR,#8300H
	MOV	R0,#SORTPH ;FORM NEW STEP
	MOV	A,@R0
	RL	A
	MOV	@R0,A
	ORL	A,#0F0H ;WRITE TO MOTOR ONLY
	MOVX	@DPTR,A
	RET


SORT_STEPR: MOV	DPTR,#8300H
	MOV	R0,#SORTPH ;FORM NEW STEP
	MOV	A,@R0
	RR	A
	MOV	@R0,A
	ORL	A,#0F0H	;WRITE TO MOTOR ONLY
	MOVX	@DPTR,A
	RET


MOTOR_DIS: MOV	DPTR,#8100H ;DISABLE ALL
	MOV	A,#0FFH	;DRIVERS
	MOVX	@DPTR,A
	MOV	DPTR,#8200H
	MOVX	@DPTR,A
	MOV	DPTR,#8300H
	MOVX	@DPTR,A
	RET


WAIT:	MOV	TH0,A	;WRITE DELAY VALUE TO
	MOV	TL0,#00H ;TIMER
	MOV	TCON,#10H
WAIT1:	JNB	TCON.5,WAIT1
	RET

SOLON:	MOV	DPTR,#8100H
	MOV	R0,#CONVPH
	MOV	A,@R0
	MOV	R2,A
	MOVX	A,@DPTR
	JNB	ACC.2,SOLON1
	JNB	ACC.3,SOLON2
	JNB	ACC.4,SOLON3
	MOV	A,R2	;ENERG SOL 4
	ANL	A,#7FH
	ORL	A,#70H
	SJMP	SOLON4
SOLON1:	MOV	A,R2	;ENERG SOL 1
	ANL	A,#0EFH
	ORL	A,#0E0H
	SJMP	SOLON4	;ENERG SOL 2
SOLON2:	MOV	A,R2
	ANL	A,#0DFH
	ORL	A,#0D0H
	SJMP	SOLON4
SOLON3:	MOV	A,R2	;ENERG SOL 3
	ANL	A,#0BFH
	ORL	A,#0B0H

SOLON4:	MOVX	@DPTR,A
	RET


LSCNT:	MOV	R0,#SORTCNT ;CALC NEW SORT CNT
	MOV	DPTR,#8100H
	MOVX	A,@DPTR
	JNB	ACC.2,LSCNT1
	JNB	ACC.3,LSCNT2
	JNB	ACC.4,LSCNT3
	MOV	@R0,#42	;SET SORT TO TRK 1
	RET

LSCNT1:	MOV	@R0,#30 ;SET SORT TO TRK 4
	RET
LSCNT2:	MOV	@R0,#18 ;SET SORT TO TRK 3
	RET
LSCNT3:	MOV	@R0,#6 ;SET SORT TO TRK 2
	RET


LCNVCT:	MOV	R0,#CNVCNT ;SET CONV CTR
	MOV	@R0,#4
	RET


LDRPCT:	MOV	R0,#DRPCNT ;SET DROP CTR
	MOV	@R0,#2
	RET

	END
