
*GENERAL PORTS
PORTPX  EQU $6000		I/O port px
PORTPY  EQU $8000		O port py	
PORTA	EQU	$1000		port A
PORTD	EQU	$1008		port D
DDRD	EQU	$1009		port D data direction register
PACNT	EQU	$1026		pulse accumulator control register
KBDD	EQU $A000		keyboard adress

*PORTA SETUP
SPACNT	EQU	#$80		set the pa7 bit of the port A to behave as output


*SPI REGISTERS
SPCR	EQU	$1028		SPI control register
SPSR	EQU	$1029		SPI status register
SPDR	EQU	$102A		SPI data register

*SPI SETUP	 VALUES
SDDRD	EQU	#$18		data for DDRD in SPI master mode
SSPCR	EQU	#$50		DATA FOR SPCR

*DEVICE SELECT CONSTANTS
SELAD	EQU	#$EF		store to port A to select AD
SELDA	EQU	#$F7		store to port A to select DA
SELK0	EQU	#$7F		store to port A to select line 0 of the keyboard
SELK1	EQU	#$BF		store to port A to select line 1 of the keyboard
SELK2	EQU	#$3F		store to port A to select line 2 of the keyboard
DSELA	EQU	#$FF		store to port A to deselect all devices connected to the port A

*EXTERNAL RAM
XRAM	EQU	$2000		external RAM


*STACK
STACK	EQU	#$3FFF	stack pointer

		ORG	$E000	

*MAIN
START	LDS	  #STACK	set the stack pointer
		JSR  INIT
		LDAD #$0FFF	2.048V to the output of the DAC
		JSR  DAC
		LDAD #$0800	1.024V to the output of the DAC
		JSR  DAC
		LDAD #$0000	0V to the output of the DAC
		JSR  DAC
		LDAA #$8F
		STAA ADCON
		JSR  ADC
		JSR  KBD
		LDAD  #$0FFE

		STAA SPDR
SIG		JSR     DAC
		INCB
		BNE    SKIP
		CMPA #$0F
		BNE   INCA
              LDAD #$0000
		BRA    SKIP 
INCA	INCA
SKIP		BRA	   SIG
 	
LOOP	BRA  LOOP
		



INIT	LDAA #SPACNT		set pa7 to be an output line
		STAA PACNT 

		LDAA #DSELA		deselect all devices on port A
		STAA PORTA 
		
		LDAA #SDDRD		set  the data direction register for SPI operation
		STAA DDRD

		LDAA #SSPCR		set  the SPI control register ... master=1, cpol=cpha=0
		STAA SPCR

		LDAA #$00
		STAA SPDR
		RTS


DAC		PSHA
		PSHA 
LOOP0	LDAA SPSR		vait while SPI is busy 
		BPL  LOOP0
		PULA
		STAA SPDR		send the upper byte to DAC
LOOP1	LDAA SPSR		vait while SPI is busy 
		BPL  LOOP1
		STAB SPDR		send the lower byte to DAC

		LDAA #SELDA	start the conversion
		STAA PORTA		
		
		LDAA #DSELA		deselect DAC
		STAA PORTA
		PULA		
		RTS				

ADC		LDAA #SELAD		select the AD converter
		STAA PORTA

		LDAA ADCON			load configuration byte for the AD
LOOP2	LDAB SPSR			wait while SPI is busy
		BPL  LOOP2
		STAA SPDR			send the configuration byte to the AD

LOOP3	LDAB SPSR			wait while SPI is busy
		BPL  LOOP3
		LDAB #$00			send dummy byte to the AD 
		STAB SPDR

LOOP4	LDAB SPSR			wait while SPI is busy
		BPL  LOOP4
		LDAA SPDR			read the upper 8-bits of the conversion
		STAB SPDR			send dummy byte to the AD
LOOP5	LDAB SPSR			wait while SPI is busy
		BPL LOOP5
		LDAB SPDR			read the lower 8(4) bits of the conversion
		LSRD				shift right for three times 
		LSRD
		LSRD				the 12-bit word is stored in:  regA3-regA0 and regB7-regB0 
*							where regA3 is the MSB and regB0 the LSB 

		PSHA
		LDAA #DSELA		deselect the AD converter
		STAA PORTA
		PULA
		RTS

KBD		LDAA #SELK0		select line 0
		STAA PORTA
		LDAA KBDD
		STAA KBD0		store line 0 variable 

		LDAA #SELK1		select line 1
		STAA PORTA
		LDAA KBDD
		STAA KBD1		store line 1 variable 

		LDAA #SELK2		select line 2
		STAA PORTA
		LDAA KBDD
		STAA KBD2		store line 0 variable 

		LDAA #DSELA		deselect keyboard
		STAA PORTA
		RTS




	

* VARIABLES
	ORG XRAM
ADCON	RMB  1 	
KBD0	RMB 1
KBD1	RMB 1
KBD2	RMB 1

* Reset Vector:
	ORG	$FFFE
	FDB	START

	END
