;Lab3 Part A
;Andrew Fowler
;Damian Szmulewicz Section: 3276
;Controls LED based on inputs


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



		.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
WDCR		.set		0x7029
GPADIR 		.set		0x6F8A
GPASET		.set		0x6FC2
GPADAT 		.set 		0x6FC0
GPATOGGLE 	.set 		0x6FC6
;*****************************************************************

		.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
		MOV		AL, #0x0000		;Sets MUX to all input/output
		MOV		AR1, #GPAMUX1
		MOV		*AR1,AL
		MOV		AL, #0x00FF		;Sets GPIO to input and output (1=out)
		MOV		AR1, #GPADIR
		MOV		*AR1,AL

;*****************************************************************
; Part E main program 
	
MainE	
		
TestCond MOV		AR0, #GPADAT	; Sets AR0 to the read the input register
		 MOV		AL, *AR0		; Moves contents of GPADAT to accumulator
		 LSR		AL, 15			; Shifts the 15th bit all the way to the end to 
		 							; see if the number is 1 or 0
		 B			StartL,NEQ		; If not 0 (aka 1), go to Night rider
		 B			loopc,UNC		; else, go to input thing
		 
		
		
			
		
		
;**************************************************************
;Main night rider
StartL0	B		TestCond,UNC		; CHecks to see if its Night Rider time or Input time
		MOV		AL, #0
		MOV		AR1, #GPADAT
		MOV		*AR1, AL
StartL	MOV		AR2, #0x80			;Sets AR4 to be the light on the left
		MOV		AR5, #0x01			;Sets AR5 to be the light on the right
		MOV		AR0, #0
		B		QuarterLoop,UNC		;Branches to turn the light on
loopM	MOV		@AL,AR2				;Shifts the bit right so the next light will turn on
		LSR		AL,1
		MOV		@AR2, AL
		MOV		@AH,AR5				;Shifts bit left one so the lefter light will turn on
		LSL		AH,1
		MOV		@AR5, AH
		INC		AR0					;Increment Coutner
		MOV		@AH,AR0
		SUB		AH,#7				;Subtract then compare the coutner with 7
		B		StartL0, EQ			;If the coutner has reached 7 go back to start and reset
;		B		loopM, UNC			;made in QuarterLoop routine
		B		QuarterLoop,UNC		;Branches to turn the light on
		
		
;***************************************************************
;Quarter Second Loop

; A "^" signifies the above comment is pertinent to this line

QuarterLoop
		MOV		AR6, #0			; See part D for comments about timing
		MOV		AL, #0
loop25	MOV		AR3, #2
		MOV		AH,	#0	
		INC		AL
		SUB		@AR3,AL
		B		loop125, NEQ
		
		MOV		@AL, AR2		;Sets the pin at AR4 true
		MOV		AR1, #GPATOGGLE	; ^
		MOV		*AR1, AL		; ^
		MOV		@AL, AR5		;Sets the pin at AR5 true
		MOV		AR1, #GPATOGGLE	; ^
		MOV		*AR1, AL		; ^
		MOV		AL, #0			;Resets AL
		INC		AR6				;Increments counters and compares
		MOV		@AH,AR6			; ^
		SUB		AH,#2			; ^
		B		loopM, EQ		; go to main loop and turn on next lights
		B		loop25, UNC		; else go to this loop again to turn off lights
		

loop125	MOV		AR4, #57344
		INC		AH
		SUB		@AR4,AH
		B		loop125, NEQ
		B		loop25,UNC

;***************************************************************
;Read output

loopc	MOV		AL, *AR0		;Mvoes contents of GPADAT to accumulator
		LSR		AL, 8
		MOV		AR1, #GPADAT
		MOV		*AR1, AL
		B		TestCond, UNC
;		B		loopc, UNC
		
;
;*******************************************************************		
END1	B		END1,UNC		; infinite loop to just keep us from trying to execute
								; un-initialized (no program) memory. 

