*********************************************************************
* Stepper motor control, and solenoid action, rev6
* OC2 Pulse Width Modulation ...  Jeremy Mayer 10/24/00 *
* Rev4 Implementation: put all stepper motion into subroutines called ADV and CLEAR.
* CTR2 was reset to 31, which causes about a 25 mm total pin offset.
* Table of solenoid patterns was added.
* Newly added: Sensor Reading on Input Port
* Newly added: Data Manipulation, and re-display
* REV 8: Convert 16 indexes to 32 (both input and output)
* REV 9: Added toggle switch (PA0), continue switch (PA1), LED (PA5)
*********************************************************************
***************************************************
* PULSE WIDTH MODULATE PORT A BIT 6
*
* USE OUTPUT COMPARE 2 TO CONTROL DUTY CYCLE
*
***************************************************
*
***************************************************
** SYMBOL DEFINITIONS
***************************************************
* 68HCII REGISTERS
***************************************************
STACK	EQU	$8FFF
BASE	EQU	$1000	;BEGINNING OF REGISTERS
PORTA	EQU	$00
TOC2	EQU	$18	;OUTPUT COMPARE 2 REGISTER
TCTL1	EQU	$20	;TIMER CONTROL REGISTER
TMSK1	EQU	$22	;TIMER MASK1 REGISTER
TFLG1	EQU	$23	;TIMER FLAG1 REGISTER
TMSK2	EQU	$24	;TIMER MASK2 REGISTER
COPRST 	EQU     $103A
OUTP2  	EQU $4000
********SCI stuff*****************************
BAUD	EQU $102B  ; Baud-rate control register to set the baud rate 
SCCR1 	EQU $102C  ; Serial Communication Control Register-1
SCCR2	EQU $102D  ; Serial Communication Control Register-2
SCSR	EQU $102E  ; Serial Communication Status Register
SCDR	EQU $102F  ; Serial Communication Data Register
EOS	EQU $04    ; User-defined End Of String (EOS) character
CR 	EQU $0D    ; Carriage Return Character
LF	EQU $0A    ; Line Feed Character
ESC	EQU $1B
* MASKS
BIT7	EQU	%10000000
BIT6	EQU	%01000000
BIT10	EQU	%00000011
BIT1	EQU	%00000010
BIT0	EQU	%00000001
INV6	EQU	%10111111
*
INIT	EQU $FFA9	;location of JMP to .INIT
B_O 	EQU $FFBE	;location of JMP to .OUT1BS
***************************************************
** DATA SECTION
***************************************************
	ORG	$00DC
	JMP	OC2_ISR	;OC2 INTERRUPT VECTOR (IN BUFFALO)

	ORG	$20
PWMLO	FDB	$300	;LOW OUTPUT TIME
PWMHI	FDB	$300	;HIGH OUTPUT TIME
CTR	RMB	2
CTR2  	RMB	2
STOP	RMB	1
CTR3	FCB	$00
BITCOM	FCB	$00
LASTBYT	RMB	1
TEMP	RMB	1

	ORG	$8000	;Data Blocks of 16 solenoid patterns

TABLE	RMB	34	;34 bytes needed for solenoid patterns

***************************************************
** MAIN PROGRAM
***************************************************
	ORG	$9000
	jmp	MAIN

*--------------------------------------------------
* INITIALIZATION
*--------------------------------------------------
* INITIALIZE STACK & Registers
MAIN	LDS	#STACK
	LDX	#BASE
        
* Set the Pre-Scaler Frequency for TCNT (during first 64 cycles)
	BCLR	TMSK2,X BIT10	;PR1:PRO=00 FOR 32.768 msec (default)

	LDAA #$FF		;close all solenoids
	STAA OUTP2

	JSR InitSCI

MAIN2	ldaa #$00
	staa CTR3
	staa BITCOM
	staa LASTBYT
* INITIALIZE OUTPUT COMPARE OC2 
* (Bit7:6=OM2:OL2  Hi=11, Lo=10, Toggle=01, Disconnect=00)
	BSET	TCTL1,X BIT7	;OM2:OL2=10
	BCLR	TCTL1,X BIT6	;  FOR SET TO LOW

	LDX #BASE

	LDAA PORTA,X		; set plate to go down
	ANDA #%11101111
	STAA PORTA,X

	JSR WAIT

*--------------------------------------------------
* Get all Pins in upmost postion and open all solenoids
*--------------------------------------------------

HERE		
	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV
	
	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV

	JSR ADV
	JSR ADV			;#28

	JSR ADV			
	JSR ADV		

	JSR ADV
	JSR ADV

	LDAB #00		;index 32
	stab OUTP2

	JSR ADV
	JSR ADV		;34th ADV

*********** stepper motor now at down position

	LDAA #$FF
	STAA OUTP2

	JSR WAIT

	JSR RECORD

	ldx #$8000

	JSR MANIP

	ldx #$8000

******REV4 Code inserted here**********************

*--------------------------------------------------
* Step through 16 increments
*--------------------------------------------------

HERE		
	LDX #BASE
	LDAA PORTA,X		; set plate to go down
	ANDA #%11101111
	STAA PORTA,X

	ldx #$8000

	LDAB 0,X		;index 0
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 1
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 2
	stab OUTP2

	JSR ADV
 	INX
		
	LDAB 0,X		;index 3
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 4
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 5
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 6
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 7
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 8
	stab OUTP2

	JSR ADV
	INX	

	LDAB 0,X		;index 9
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 10
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 11
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 12
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 13
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 14
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 15
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 16
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 17
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 18
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 19
	stab OUTP2

	JSR ADV
 	INX
		
	LDAB 0,X		;index 20
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 21
	stab OUTP2

	JSR ADV
	INX
	
	LDAB 0,X		;index 22
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 23
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 24
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 25
	stab OUTP2

	JSR ADV
	INX	

	LDAB 0,X		;index 26
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 27
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 28
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 29
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 30
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 31
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 32
	stab OUTP2

	JSR ADV
	INX

	LDAB 0,X		;index 33
	stab OUTP2

	JSR ADV
	

*********** stepper motor now at down position

	LDAA #$FF
	STAA OUTP2

	JSR WAIT

	JSR CLEAR

	jmp MAIN2

***************************************************
** SUBROUTINE WAIT - wait for toggle switch press
***************************************************

WAIT	ldx #BASE

	LDAA PORTA,X		;turn on waiting light
	ANDA #%11011111
	STAA PORTA,X

contin	LDAA PORTA,X		;poll untill continue switch is pushed
	ANDA #%00000010
	BNE contin

	LDAA PORTA,X		;turn waiting light off
	ORA #%00100000
	STAA PORTA,X

RTS

***************************************************
** SUBROUTINE MANIP - data manipulation
***************************************************

MANIP	ldx #$8000

	
B0	ldaa 0,x
	staa TEMP

	ldab BITCOM
	andb #%00000001			;? is BITCOM bit0 == 0?
	bne SETNX0
	bra G0

SETNX0	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%00000001
	staa 0,x
	bra B1

G0
	ldaa LASTBYT
	anda #%00000001			;? is LASTBYT bit0 == 1? brnch if 0
	beq SETNX0

	ldaa 0,x
	anda #%00000001			;? is bit0 == 0? brnch if 1
	bne SETNX0

*	;this is a valid solenoid throw spot
*	;bit0 is already clear

	ldab BITCOM
	orb #%00000001
	stab BITCOM			;set BITCOM bit0

****************************************************************

B1	
	ldab BITCOM
	andb #%00000010			;? is BITCOM bit1 == 0?
	bne SETNX1
	bra G1

SETNX1	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%00000010
	staa 0,x
	bra B2

G1
	ldaa LASTBYT
	anda #%00000010			;? is LASTBYT bit1 == 1?
	beq SETNX1

	ldaa 0,x
	anda #%00000010			;? is bit1 == 0?
	bne SETNX1

*	;this is a valid solenoid throw spot
*	;bit0 is already clear

	ldab BITCOM
	orb #%00000010
	stab BITCOM			;set BITCOM bit1

****************************************************************

B2	
	ldab BITCOM
	andb #%00000100			;? is BITCOM bit2 == 0?
	bne SETNX2
	bra G2

SETNX2	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%00000100
	staa 0,x
	bra B3

G2

	ldaa LASTBYT
	anda #%00000100			;? is LASTBYT bit2 == 1?
	beq SETNX2

	ldaa 0,x
	anda #%00000100			;? is bit2 == 0?
	bne SETNX2

*	;this is a valid solenoid throw spot
*	;bit2 is already clear

	ldab BITCOM
	orb #%00000100
	stab BITCOM			;set BITCOM bit2

****************************************************************

B3	
	ldab BITCOM
	andb #%00001000			;? is BITCOM bit3 == 0?
	bne SETNX3
	bra G3

SETNX3	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%00001000
	staa 0,x
	bra B4

G3

	ldaa LASTBYT
	anda #%00001000			;? is LASTBYT bit3 == 1?
	beq SETNX3

	ldaa 0,x
	anda #%00001000			;? is bit3 == 0?
	bne SETNX3

*	;this is a valid solenoid throw spot
*	;bit2 is already clear

	ldab BITCOM
	orb #%00001000
	stab BITCOM			;set BITCOM bit3

****************************************************************

B4	
	ldab BITCOM
	andb #%00010000			;? is BITCOM bit4 == 0?
	bne SETNX4
	bra G4

SETNX4	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%00010000
	staa 0,x
	bra B5

G4

	ldaa LASTBYT
	anda #%00010000			;? is LASTBYT bit4 == 1?
	beq SETNX4

	ldaa 0,x
	anda #%00010000			;? is bit4 == 0?
	bne SETNX4

*	;this is a valid solenoid throw spot
*	;bit4 is already clear

	ldab BITCOM
	orb #%00010000
	stab BITCOM			;set BITCOM bit4

****************************************************************

B5	
	ldab BITCOM
	andb #%00100000			;? is BITCOM bit5 == 0?
	bne SETNX5
	bra G5

SETNX5	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%00100000
	staa 0,x
	bra B6

G5

	ldaa LASTBYT
	anda #%00100000			;? is LASTBYT bit5 == 1?
	beq SETNX5

	ldaa 0,x
	anda #%00100000			;? is bit5 == 0?
	bne SETNX5

*	;this is a valid solenoid throw spot
*	;bit4 is already clear

	ldab BITCOM
	orb #%00100000
	stab BITCOM			;set BITCOM bit5


*****************************************************************

B6	
	ldab BITCOM
	andb #%01000000			;? is BITCOM bit6 == 0?
	bne SETNX6
	bra G6

SETNX6	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%01000000
	staa 0,x
	bra B7

G6

	ldaa LASTBYT
	anda #%01000000			;? is LASTBYT bit6 == 1?
	beq SETNX6

	ldaa 0,x
	anda #%01000000			;? is bit6 == 0?
	bne SETNX6

*	;this is a valid solenoid throw spot
*	;bit6 is already clear

	ldab BITCOM
	orb #%01000000
	stab BITCOM			;set BITCOM bit6

*****************************************************************

B7	
	ldab BITCOM
	andb #%10000000			;? is BITCOM bit7 == 0?
	bne SETNX7
	bra G7

SETNX7	ldaa 0,x			;this pin is complete keep storing 1's
	ora #%10000000
	staa 0,x
	bra B8

G7

	ldaa LASTBYT
	anda #%10000000			;? is LASTBYT bit7 == 1?
	beq SETNX7

	ldaa 0,x
	anda #%10000000			;? is bit7 == 0?
	bne SETNX7

*	;this is a valid solenoid throw spot
*	;bit6 is already clear

	ldab BITCOM
	orb #%10000000
	stab BITCOM			;set BITCOM bit7


*******************************************************************

B8	
	LDAA TEMP
	STAA LASTBYT		;before inx store current byte as lastbyte
	INX
	INC CTR3
	LDAA CTR3
	CMPA #34
	BNE AGAIN		;increment x and loop through all 34 time steps
	BRA DONE
AGAIN	JMP B0
DONE

	RTS

***************************************************
** SUBROUTINE ADVance - move stepper down 256 counts
***************************************************
	
ADV	PSHX

	LDX #BASE
	
	LDAA #$ff
	STAA CTR

	ldaa #0
	STAA STOP

* ENABLE OC2 INTERRUPT
	BSET TMSK1,X BIT6
* TURN ON INTERRUPT SYSTEM
	CLI

WAIT41	LDAB STOP
	CMPB #1
	BNE WAIT41

* TURN OFF INTERRUPT SYSTEM
	SEI

	PULX
	RTS

***********************************************************************
** SUBROUTINE Record - moves stepper up 36 x 256 counts (while inputing data), down 512 counts
***********************************************************************
RECORD
	LDX #BASE

	LDAA PORTA,X		; set plate to go up
	ORA #%00010000
	STAA PORTA,X

	LDX #$8000

	jsr KILLT
	LDAB $2000		;index 32
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 31
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 30
	stab 0,X

	JSR ADV
 	INX
		
	jsr KILLT
	LDAB $2000		;index 29
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 28
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 27
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 26
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 25
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 24
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 23
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 22
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 21
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 20
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 19
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 18
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 17
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 16
	stab 0,X

	JSR ADV

	jsr KILLT
	LDAB $2000		;index 15
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 14
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 13
	stab 0,X

	JSR ADV
 	INX
		
	jsr KILLT
	LDAB $2000		;index 12
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 11
	stab 0,X

	JSR ADV
	INX
	
	jsr KILLT
	LDAB $2000		;index 10
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 9
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 8
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 7
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 6
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 5
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 4
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 3
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 2
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 1
	stab 0,X

	JSR ADV
	INX

	jsr KILLT
	LDAB $2000		;index 0
	stab 0,X

	JSR ADV


	JSR ADV

	JSR ADV			; advance 512 counts
	JSR ADV
	JSR ADV
	JSR ADV			
	JSR ADV
	JSR ADV			;40th ADV
	JSR ADV			;confused here? extra 2 ADV's seems to help (still slightly off)
	JSR ADV
	


	LDX #BASE
	LDAA PORTA,X		; set plate to go down
	ANDA #%11101111
	STAA PORTA,X

DELAY	DEC CTR			;kill 256 E-clocks before direction change
	BNE DELAY

	JSR ADV			; advance 512 counts
	JSR ADV
	JSR ADV
	JSR ADV
	JSR ADV			
	JSR ADV			;total=34

	LDAA PORTA,X		; set plate to go up
	ORA #%00010000
	STAA PORTA,X

DELY	DEC CTR			;kill 256 E-clocks before direction change
	BNE DELY
*	JSR ADV

	RTS
***********************************************************************
** SUBROUTINE FLICK - open and close all sol's for elec contact
***********************************************************************
FLICK	ldaa #$00
	staa $4000
	ldab #60
burn	deca
	bne burn
	decb
	bne burn
	ldaa #$ff
	staa $4000
	RTS
***********************************************************************
** SUBROUTINE KILLT - wait
***********************************************************************
KILLT	ldaa #$00
	ldab #$ff
burn2	deca
	bne burn2
	decb
	bne burn2
	RTS

***********************************************************************
** SUBROUTINE CLEAR - moves stepper up 36 x 256 counts, down 512 counts
***********************************************************************
CLEAR
	LDX #BASE

	LDAA PORTA,X		; set plate to go up
	ORA #%00010000
	STAA PORTA,X

	ldaa #40		; call ADV 38 times, later move back down 5 ADV's, up one
	staa CTR2
MAS	JSR ADV
	DEC CTR2
	BNE MAS
	
	LDAA PORTA,X		; set plate to go down
	ANDA #%11101111
	STAA PORTA,X

DELY2	DEC CTR			;kill 256 E-clocks before direction change
	BNE DELY2

	JSR ADV			; advance 512 counts
	JSR ADV
	JSR ADV			;at zero here
	JSR ADV			
	JSR ADV			;down one more
	JSR ADV

	LDAA PORTA,X		; set plate to go up
	ORA #%00010000
	STAA PORTA,X

DELY3	DEC CTR			;kill 256 E-clocks before direction change
	BNE DELY3
*	JSR ADV
	
	ldx #$2000
	jsr B_O			;should read $00 for all solenoids closed!

	RTS
***************************************************
** INTERRUPT SERVICE ROUTINE
***************************************************
	ORG	$A000

* INTERRUPT FROM OC2?
OC2_ISR	LDX   	#BASE
	BRCLR	TFLG1,X BIT6 RT_OC2	;IGNORE ILLEGAL
*					;   INTERRUPT
	DEC CTR
	BNE CONT

	INC STOP
* Disable OC2 interrupt
	BCLR TMSK1,X BIT6

CONT	
*--------------------------------------------------
* SERVICE OUTPUT COMPARE 2
*--------------------------------------------------
* CLEAR OC2 FLAG
	BCLR	TFLG1,X INV6

* WAS LAST OUTPUT HIGH? (Bit7:6=OM2:OL2  Hi=11,Lo=10)
	BRSET	TCTL1,X BIT6 LASTHI ;BRANCH ON YES

* PROGRAM NEXT OUTPUT TO BE HIGH
*   SET OC2 OUTPUT ACTION
	BSET	TCTL1,X BIT6	;OM2:OL2=11 (bit 7 already 1)

*   SET HIGH OUTPUT COMPARE TIME
	LDD	TOC2,X
	ADDD	PWMLO
	STD	TOC2,X
	BRA	RT_OC2

* PROGRAM NEXT OUTPUT TO BE LOW
*   SET OC2 OUTPUT ACTION
LASTHI	BCLR	TCTL1,X BIT6	;OM2:OL2=10

*   SET LOW OUTPUT COMPARE TIME
	LDD	TOC2,X
	ADDD	PWMHI
	STD	TOC2,X

* RETURN FROM INTERRUPT
RT_OC2	RTI

************************************************************************
*                       SUBROUTINE -  OutChar
* Description: Outputs the character in register A to the screen after 
*              checking if the Transmitter Data Register is Empty
* Input         : Data to be transmitted in register A.
* Output        : Transmit the data.
* Destroys      : None.
* Calls         : None.
************************************************************************
*
OutChar  PSHB				; Save contents of B register
Loop1   LDAB SCSR			; Check status reg (load it into B reg)

	ANDB #%1000000		; Check if transmit buffer is empty

	BEQ Loop1			; Wait until empty

	STAA SCDR			; A register ==> SCI data

	PULB				; Restore B register
	RTS				; Return from subtoutine
*
************************************************************************
*                       SUBROUTINE -  OutStr
* Description: Outputs the string terminated by EOS. The starting location 
*              of the string is pointed by X register. Calls the OutChar
*              subroutine to display a character on the screen and
*              exit once EOS has been reached. 
* Input         : Starting location of the string to be transmitted 
*               : (passed in X register)
* Output        : Prints the string.
* Destroys      : Contents of X register.
* Calls         : OutChar.
************************************************************************
*
OutStr:	PSHA
OutStr1:
	LDAA	0,X			; Get a character (put in A register)

	CMPA	#EOS			; Check if it's EOS
	BEQ	Done			; Branch to Done if it's EOS

        BSR     OutChar			; Print the character by calling OutChar
        INX
	BRA     OutStr1
Done:	PULA
        RTS				; Return from subtoutine
*
************************************************************************
*                SUBROUTINE  -  InChar
* Description: Receives the typed character into register A.
* Input         : None
* Output        : Register A = input from SCI
* Destroys      : Contents of Register A
* Calls         : None.
************************************************************************
*
InChar                              

	LDAA SCSR		; Check status reg.
	ANDA #%00100000		;     (load it into A reg)
*                      		; Check if receive buffer full
	BEQ InChar		; Wait until data present
	LDAA SCDR		; SCI data ==> A register
	
	
	RTS			; Return from subroutine
*
************************************************************************
*                SUBROUTINE  -  ADDOUT
* Description: Receives the hex address in x, write ASCI equiv to screen
* Input         : address in X
* Output        : ASCI address to screen
* Destroys      : 
* Calls         : OutChar

** USE BUFFALO UTILITY SUBROUTINE .OUT2BS

************************************************************************
*                       SUBROUTINE - InitSCI         
* Description: This subroutine initializes the BAUD rate to 9600 and
*              sets up the SCI port for 1 start bit, 8 data bits and
*              1 stop bit.  It also enables the transmitter and receiver.
*              Effected registers are BAUD, SCCR1, and SCCR2.
* Input         : None.
* Output        : Initializes SCI.
* Destroys      : None.
* Calls         : None.
************************************************************************
*
InitSCI  PSHA				; Save contents of A register 
	LDAA #$30
	STAA BAUD			; Set BAUD rate to 9600
	LDAA #$00			; Set SCI Mode to 1 start bit,  
	STAA SCCR1			;     8 data bits, and 1 stop bit.
	LDAA #%00001100			; Enable SCI Transmitter 
	STAA SCCR2			;     and Receiver

	PULA			; Restore A register
	RTS			; Return from subtoutine
*
************************************************************************
*                SUBROUTINE  -  InChar
* Description: Receives the typed character into register A.
* Input         : None
* Output        : Register A = input from SCI
* Destroys      : Contents of Register A
* Calls         : None.
************************************************************************
*
InChar                                 

	LDAA SCSR			 ; Check status reg.
	ANDA #%00100000                  ;     (load it into A reg)
*                      			 ; Check if receive buffer full
	BEQ InChar                       ; Wait until data present
	LDAA SCDR                        ; SCI data ==> A register
	RTS                              ; Return from subroutine

************************************************************************
*                       END OF PROGRAM
************************************************************************

