;Lab4
;Andrew Fowler
;Damian Szmulewicz Section: 3276
; Keypad operation


;*****************************************************************



		.global		_c_int00

data_sect	.set		0xa000			;constant that is actually the starting addr of .data section
bss_sect	.set		0xb000			;constant that is actually the starting addr of .bss section
GPAMUX1 	.set 		0x6F86
GPAMUX2 	.set 		0x6F87
WDCR		.set		0x7029
GPADIR 		.set		0x6F8A
GPASET		.set		0x6FC2
GPADAT 		.set 		0x6FC0
;*****************************************************************

		.data				

;char    .byte    	'4' 	;define string, ASCII characters placed in memory as WORDS!

	    .bss		results,256	;reserves 256 words at label 'results' in the .bss section	
		
;		.global		char		;.global directive lets you to see the assigned addresses in map file.	
		
;****************** Part A ***********************
		.text					; Program section, see the command linker file, program code 
								; should be placed in the text section which starts at 0x9000
						
_c_int00:						; This label tells the linker where the entry (starting) point for 
		EALLOW 						; Allow access to protected registers
		SETC 	OBJMODE 			; Allow 32 bit mov instructions
								; WDCR is watchdog timer control register
								; In constants section need to set WDCR=0x7029
		MOV 	AL,#1101000b 	; Disable the watchdog timer.
								; Must have 101 in bits 5, 4, 3 for
								; any writes to WDCR. 1 in bit 6
								; disables watchdog
		MOV 	AR1,#WDCR
		MOV		*AR1, AL 		; Disables watchdog timer

; Subroutine to scan and read what value is displayed on keypad
; Order of pins: 5 6 7 8 and read 1 2 3 4 - confirmed as lab page shows.
; 
; Since there is no defined input/output that we have to use lets use the ones that
; are not wire wrapped already, so pins 16 on we know are not wire wrapped. 
; 
; So here is the plan:
; 	Pins 1 = 8
;	pin 2 = 9
; 	pin 3 = 10 
;	pin 4 = 11 - outputs
;	pin 5 = 12
;	pin 6 = 13
;	pin 7 = 14
; 	Pins 8 = 15 - inputs
;	LED outputs = pins 0-3 outputs
; 
; Steps to do this: 
;	!!!If the key is read to be true DELAY 200ms!!!
;	Set 6/7/8 to 1 5 to 0 and read input 1
;		Display 1 in binary on LEDS
; 	Set 5/7/8 to 1 6 to 0 and read input 1
; 		Display 4 in binary on LEDS
;	Set 5/6/8 to 1 7 to 0 and read input 1
;		Display 7 in binary on LEDS
;	Set 5/6/7 to 1 8 to 0 and read input 1
;		Display (*) or 0xE oon LEDS
;	Change input 1 to input 2 and repeat
; REMEMBER 3xNOPs between column scans
; ASCII tables at http://www.asciitable.com/
; Could run it through a loop and just replace 1 if we can figure out
; how to change the values of the diplayed things by adding, or just 
; copy pasting around and replacing pin 1 with pin 2, 3, 4.

Keypad	LC		DELAY2
		MOV		AL, #0x0000		;Sets all pins to input/output so we can use them
		MOV		AR1, #GPAMUX1
		MOV		*AR1,AL
		MOV		AL, #0x0F0F		;Sets GPIO to input and output (1=out) so we want 0000|1111|0000|1111
		MOV		AR1, #GPADIR
		MOV		*AR1,AL
		
		; Write the correct things to pins 5-8
		; Read input at 1
		; Check input to see if its 1 or 0
		;		If 1 display LED and DELAY
		;		If Zero, skip and keep going
		
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0x700		; Set 2/3/4 to 1, 1 to 0 =  0111|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
		LC		DELAY2
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x1000		; Checks to see if bit 1 is zero or 1
		nop
		nop
		nop
		 B		skipB,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x00A		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
; So the progress here is, the first key is done, now the code just has to be copy pasted and changed a little bit so
; it can be resued for all the possible pins
skipB	
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0x700		; Set 2/3/4 to 1, 1 to 0 =  0111|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x2000		; Checks to see if bit 1 is zero or 1
		 B		skipC,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x00B		;Want to make the GPIO pin 0000|0000|1011
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
		
skipC
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0x700		; Set 2/3/4 to 1, 1 to 0 =  0111|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x4000		; Checks to see if bit 1 is zero or 1
		 B		skipD,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x00C		;Want to make the GPIO pin 0000|0000|1100
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
skipD
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0x700		; Set 2/3/4 to 1, 1 to 0 =  0111|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x8000		; Checks to see if bit 1 is zero or 1
		 B		skip3,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x00D		;Want to make the GPIO pin 0000|0000|1101
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC	
		
;*******************************************************************************************************


; NEXT COLUMN --- 3-#



;********************************************************************************************************
		
		
		
		
		
skip3	nop
		nop
		nop
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xB00		; Set 2/3/4 to 1, 1 to 0 =  1011|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
		LC		DELAY2
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x1000		; Checks to see if bit 1 is zero or 1
		 B		skip6,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x003		;Want to make the GPIO pin 0000|0000|0011
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC

skip6	
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xB00		; Set 2/3/4 to 1, 1 to 0 =  1011|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x2000		; Checks to see if bit 1 is zero or 1
		 B		skip9,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x006		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
		
skip9
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xB00		; Set 2/3/4 to 1, 1 to 0 =  1011|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x4000		; Checks to see if bit 1 is zero or 1
		 B		skip15,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x009		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
skip15
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xB00		; Set 2/3/4 to 1, 1 to 0 =  1011|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x8000		; Checks to see if bit 1 is zero or 1
		 B		skip2,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x00F		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC	


		
;*******************************************************************************************************


; NEXT COLUMN --- 2-0



;********************************************************************************************************
		
		
		
		
		
skip2
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xD00		; Set 2/3/4 to 1, 1 to 0 =  1101|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
		LC		DELAY2
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x1000		; Checks to see if bit 1 is zero or 1
		 B		skip5,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x002		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
; So the progress here is, the first key is done, now the code just has to be copy pasted and changed a little bit so
; it can be resued for all the possible pins
skip5	
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xD00		; Set 2/3/4 to 1, 1 to 0 =  1011|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x2000		; Checks to see if bit 1 is zero or 1
		 B		skip8,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x005		;Want to make the GPIO pin display 5
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
		
skip8
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xD00		; Set 2/3/4 to 1, 1 to 0 =  1101|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x4000		; Checks to see if bit 1 is zero or 1
		 B		skip0,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x008		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
skip0
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xD00		; Set 2/3/4 to 1, 1 to 0 =  1101|0000|0000
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x8000		; Checks to see if bit 1 is zero or 1
		 B		skip1,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x000		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC	

;*******************************************************************************************************


; NEXT COLUMN --- 1-*



;********************************************************************************************************
		
		
		
		
		
skip1	nop
		nop
		nop
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xE00		; 
		OR		AL, AH			;
		MOV		*AR1, AL		;
		LC		DELAY2
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x1000		; Checks to see if bit 1 is zero or 1
		 B		skip4,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x001		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
skip4	
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xE00		; 
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x2000		; Checks to see if bit 1 is zero or 1
		 B		skip7,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x004		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
		
skip7
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xE00		; 
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x4000		; Checks to see if bit 1 is zero or 1
		 B		skip16,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x007		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC
skip16
		MOV		AR1, #GPADAT	; Makes AR1 the location of GPADAT
		MOV		AL, *AR1		; MOves the data at GPADAT to AH so we can do operations with it
		AND		AL, #0xF		; Preserves LED configuration that is being displayed right now also clears the other pins
								;   that the next step will use
		MOV		AH, #0xE00		; 
		OR		AL, AH			;
		MOV		*AR1, AL		;
;Read input
		MOV		AR1, #GPADAT	; Sets AR0 to the read the input register
		MOV		AL, *AR1		; Moves contents of GPADAT to accumulator
		AND		AL, #0x8000		; Checks to see if bit 1 is zero or 1
		 B		Keypad,NEQ		; If the pin is not 0 it means no key is presed so we need to skip LED setting
;Set LED part
		MOV		AL, #0x00E		;Want to make the GPIO pin 0000|0000|1010
		MOV		AR1, #GPADAT
		MOV		*AR1, AL		; And the LED should turn on right here
		LC		DELAY			; Just calls the delay subroutine
		B		Keypad, UNC	

;Unatainable lines for expansion
		nop
		nop
		nop
	
DELAY	nop
		nop
		nop		
		MOV		AL, #0
loop10	MOV		AR3, #5
		MOV		AH,	#0	
		INC		AL
		SUB		@AR3,AL
		B		loop110, NEQ
		B		Keypad, UNC
loop110	MOV		AR4, #40000
		INC		AH
		SUB		@AR4,AH
		B		loop110, NEQ
		B		loop10,UNC

DELAY2	nop
		nop
		nop		
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		LRET












		
END1	B		END1,UNC		; infinite loop to just keep us from trying to execute
								; un-initialized (no program) memory. 

