	.module mike_2.c
	.area memory(abs)
	.org 0x3e5e
_IRQ_TOF_vector::
	.word _TOF_isr
	.area data
	.area memory(abs)
	.org 0x3e52
_IRQ_ATD0_vector::
	.word _ATD0_Interrupt
	.area data
	.area memory(abs)
	.org 0x3e50
_IRQ_ATD1_vector::
	.word _ATD1_Interrupt
	.area data
	.area memory(abs)
	.org 0x3e64
_IRQ_OC5_vector::
	.word _OC5_ISR
	.area data
	.area memory(abs)
	.org 0x3e6e
_IRQ_IC0_vector::
	.word _IC0_ISR
	.area data
	.area memory(abs)
	.org 0x3e6c
_IRQ_IC1_vector::
	.word _IC1_ISR
	.area data
	.area text
_line_threshold::
	.word 90
_Intersection_threshold::
	.word 80
_Backup_threshold::
	.word 90
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;              b -> -12,x
;              a -> -10,x
_main::
	pshx
	tfr s,x
	leas -12,sp
; //*****************************************************************************
; // Name:				2007 Line Track code
; // Vers:		 	    0.1
; // Last Revised:		1/27/07 10:16pm
; //*****************************************************************************		  			
; 
; #include<mc9S12dp256.h>
; #include<stdio.h>
; 
; //define 16 bit PWM registers
; #define PWMPERW1 _LP(0xB4) 
; #define PWMPERW3 _LP(0xB6)
; #define PWMPERW5 _LP(0xB8) 	 
; #define PWMPERW7 _LP(0xBA)
; 
; #define PWMDTYW1 _LP(0xBC)
; #define PWMDTYW3 _LP(0xBE)
; #define PWMDTYW5 _LP(0xC0)
; #define PWMDTYW7 _LP(0xC2)
; 
; // Function Prototypes
; void init_pwm(void);
; void init_timer(void);
; void ob_led(int);
; void pause(int);
; void s_pause(int);
; void init_tof(void);
; void init_atd0(void);
; void init_atd1(void);
; void Speaker(void);
; void stop(void);
; void forward(void);
; void reverse(void);
; void left(void);
; void right(void);
; void motorspeed(void);
; void speedcontrol(void);
; void linetrack(int);
; 
; //General 
; #define CLI asm("CLI")
; #define SEI asm("SEI")
; 
; //TOF Interrupt Setup
; void TOF_isr(void); 
; #pragma interrupt_handler TOF_isr
; #pragma abs_address: 0x3e5e
; void (*IRQ_TOF_vector[])()={TOF_isr};
; #pragma end_abs_address
; 
; //ATD0 Interrupt Setup
; void ATD0_Interrupt();
; #pragma interrupt_handler ATD0_Interrupt()
; #pragma abs_address: 0x3E52
; void (*IRQ_ATD0_vector[])()={ATD0_Interrupt};
; #pragma end_abs_address
; 
; //ATD1 Interrupt Setup
; void ATD1_Interrupt();
; #pragma interrupt_handler ATD1_Interrupt()
; #pragma abs_address: 0x3E50
; void (*IRQ_ATD1_vector[])()={ATD1_Interrupt};
; #pragma end_abs_address
; 
; //OC5 Interrupt Setup
; void OC5_ISR();
; #pragma interrupt_handler OC5_ISR()
; #pragma abs_address: 0x3E64
; void (*IRQ_OC5_vector[])()={OC5_ISR};
; #pragma end_abs_address
; 
; //IC0 Interrupt Setup
; void IC0_ISR();
; #pragma interrupt_handler IC0_ISR()
; #pragma abs_address: 0x3E6E
; void (*IRQ_IC0_vector[])()={IC0_ISR};
; #pragma end_abs_address
; 
; //IC1 Interrupt Setup
; void IC1_ISR();
; #pragma interrupt_handler IC1_ISR()
; #pragma abs_address: 0x3E6C
; void (*IRQ_IC1_vector[])()={IC1_ISR};
; #pragma end_abs_address
; 
; //declare global variable
; long int t_counts;
; long int t_tenths;
; long int t_hundredths;
; unsigned int atd0[8];
; unsigned int atd1[8];
; int dela;
; int line;
; const line_threshold = 90;
; unsigned long int IC0_count;
; unsigned long int IC1_count;
; 
; int Intersection;
; const Intersection_threshold = 80;     //original = 60 
; int Backup;
; const Backup_threshold = 90;
; 
; 
; void main(void)
; {
; 
; 
;    int a,b; 
;    init_pwm();
	jsr _init_pwm
;    init_tof();
	jsr _init_tof
;    init_atd0();
	jsr _init_atd0
;    init_atd1();
	jsr _init_atd1
;    init_ic0_1();
	jsr _init_ic0_1
;  
;    pause(1);
	ldd #1
	jsr _pause
;    
;    ATD0CTL5 = 0x30;    //start atd's
	ldab #48
	stab 0x85
;    ATD1CTL5 = 0x30;
	ldab #48
	stab 0x125
;      
;    pause(1);       	   //pause to allow atd to read first time;
	ldd #1
	jsr _pause
	bra L5
L4:
	ldd #4800
	std 0xbe
	ldd #10
	jsr _pause
	ldd #4500
	std 0xbe
	ldd #10
	jsr _pause
	ldd #4800
	std 0xbe
	ldd #10
	jsr _pause
	ldd #4500
	std 0xbe
	ldd #10
	jsr _pause
L5:
;    
;    while(1)
	bra L4
X0:
;    {
;     
;     PWMDTYW3 = 4800;
; 	pause(10);
;     PWMDTYW3 = 4500;
; 	pause(10);
;     PWMDTYW3 = 4800;
; 	pause(10);
;     PWMDTYW3 = 4500;
; 	pause(10);
; 	
; 	
; 	}
;    
;    ob_led(15);
	ldd #15
	jsr _ob_led
;   // while(atd0[3] > 10);		   //wait for switch 2 to be pressed
;   
;     pause(50);
	ldd #50
	jsr _pause
;   
;     ob_led(14);
	ldd #14
	jsr _ob_led
;     pause(50);
	ldd #50
	jsr _pause
	lbra L8
L7:
; 	
; 	/*forward();
; 	
; 	 IC1_count=0;
; 	   while (IC1_count < 2700);	  
; 	   
; 	
; 	 linetrack(0);  			   //linetrack to T
; 	  // ob_led(11); 
; 	 
; 	  forward();
; 	  
; 	  IC1_count=0;
; 	   while (IC1_count < 2000);	  
; 	   stop();  			   //center robot on line
; 	   pause(10); 
; 	
; 	left();	
; 			   //turn till sensor on line
; 	
; 	while(line==0)
; 	 {
; 	    ob_led(line);
; 		
; 	 }
; 	linetrack(0);
; 	
; 	forward();
; 	IC1_count=0; 
; 	while (IC1_count < 2175);  // 860 * number
; 	
; 	 stop();
; 	 pause(20);
;      
; 	 			   //turn till sensor on line
; 	
; 	
; 	 left();		   		//turn till sensor on line heading towards
; 	 IC1_count=0;
; 	 while (IC1_count < 2700);
; 	 pause(5);            //ball request zone
; 	 
; 	/* while(line==0)        
; 	 {
; 	    ob_led(line);
; 	 }  */
; 	 /*stop();
; 	 pause(5);
; 	 
; 	 linetrack(0);	 		   // get to ball request zone
; 
; 	 /*stop();
; 	 pause(5);
; 	 forward();
; 	 
; 	 
; 	 pause(5);
; 	// forward(); */
; 	 
; 	 
; 	 /*
; 	 forward();
; 	 while(Backup != 7);
; 	
; 	 stop();
; 	 pause(5);
; 	 
; 	 IC1_count = 0;
; 	 
; 	 forward();
; 	 
; 	 
; 	 while (IC1_count < 920);
; 	 //linetrack(1);
; 	 
; 	 stop();
; 	 pause(5);
; 	 */
; 	 
; 	 while(1)
; 	 {
; 	 reverse();
	jsr _reverse
L10:
L11:
; 	 while(Backup != 7);
	ldd _Backup
	cpd #7
	bne L10
; 	 
; 	 stop();
	jsr _stop
; 	 pause(5);
	ldd #5
	jsr _pause
; 	 
; 	 left();		   		
	jsr _left
; 	 IC1_count=0;
	ldy #L13
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_IC1_count
	movw -4,x,0,y
	movw -2,x,2,y
L14:
L15:
; 	 while (IC1_count < 17000);   	//180 turn in Request zone
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L17
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	blo L14
; 	 
; 	 stop();
	jsr _stop
; 	 pause(10);
	ldd #10
	jsr _pause
; 	 
; 	 linetrack(0);    				//line track to intersection
	ldd #0
	jsr _linetrack
; 	 stop();
	jsr _stop
; 	 pause(10);
	ldd #10
	jsr _pause
; 	 
; 	 forward();						//move off of intersection
	jsr _forward
; 	 IC1_count=0;
	ldy #L13
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_IC1_count
	movw -4,x,0,y
	movw -2,x,2,y
L18:
L19:
; 	 while (IC1_count < 2700);	
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L21
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	blo L18
; 	 
; 	 linetrack(0);	  	  //linetrack to rebound zone, stop at front line track
	ldd #0
	jsr _linetrack
; 	 stop();
	jsr _stop
; 	 pause(10);
	ldd #10
	jsr _pause
; 	 
; 	 forward();
	jsr _forward
; 	 pause(5);
	ldd #5
	jsr _pause
; 	 
; 	 forward();						//back linetrack into rebound zone
	jsr _forward
L22:
L23:
; 	 while(Backup != 7);
	ldd _Backup
	cpd #7
	bne L22
; 	
; 	 stop();
	jsr _stop
; 	 pause(5);
	ldd #5
	jsr _pause
; 	 
; 	 IC1_count = 0;
	ldy #L13
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_IC1_count
	movw -4,x,0,y
	movw -2,x,2,y
; 	 
; 	 forward();	   	 			//move one approx. one inch into rebound zone
	jsr _forward
L25:
L26:
; 	 
; 	 while (IC1_count < 920);
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L28
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	blo L25
; 	 
; 	 stop();
	jsr _stop
; 	 pause(10);
	ldd #10
	jsr _pause
; 	 
; 	 reverse();	   	 			//move one approx. one inch out of rebound zone
	jsr _reverse
L29:
L30:
; 	 
; 	 while (IC1_count < 3000);
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L32
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	blo L29
; 	 
; 	 stop();
	jsr _stop
; 	 pause(5);
	ldd #5
	jsr _pause
; 	 
; 	 left();		   		
	jsr _left
; 	 IC1_count=0;
	ldy #L13
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_IC1_count
	movw -4,x,0,y
	movw -2,x,2,y
L33:
L34:
; 	 while (IC1_count < 19600);   	//180 turn in Rebound zone
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L36
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	blo L33
; 	 
; 	 stop();
	jsr _stop
; 	 pause(10);
	ldd #10
	jsr _pause
; 	 
; 	 forward();
	jsr _forward
; 	 pause(7);
	ldd #7
	jsr _pause
; 	 
; 	 linetrack(0);    				//line track to intersection
	ldd #0
	jsr _linetrack
; 	 stop();
	jsr _stop
; 	 pause(10);
	ldd #10
	jsr _pause
; 	 
; 	 forward();						//move off of intersection
	jsr _forward
; 	 IC1_count=0;
	ldy #L13
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_IC1_count
	movw -4,x,0,y
	movw -2,x,2,y
L37:
L38:
; 	 while (IC1_count < 2700);	
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L21
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	blo L37
; 	 
; 	 stop();
	jsr _stop
; 	 pause(10);
	ldd #10
	jsr _pause
; 	 
; 	 linetrack(0);	 		   // go to ball request zone
	ldd #0
	jsr _linetrack
; 
; 	 stop();
	jsr _stop
; 	 pause(5);
	ldd #5
	jsr _pause
; 	 
; 	 forward();
	jsr _forward
; 	 pause(5);
	ldd #5
	jsr _pause
; 	
; 	 forward();
	jsr _forward
L40:
L41:
; 	 while(Backup != 7);
	ldd _Backup
	cpd #7
	bne L40
; 	
; 	 stop();
	jsr _stop
; 	 pause(5);
	ldd #5
	jsr _pause
; 	 
; 	 IC1_count = 0;
	ldy #L13
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_IC1_count
	movw -4,x,0,y
	movw -2,x,2,y
; 	 
; 	 forward();
	jsr _forward
L43:
L44:
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L28
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	blo L43
	jsr _stop
	ldd #5
	jsr _pause
L8:
	lbra L7
X1:
; 	 
; 	 
; 	 while (IC1_count < 920);
; 	 stop();
; 	 pause(5);
; 	 
; 	 }  
; 	   
; 	/* reverse();				   // head back to 4 way intersection
; 	 IC1_count = 0;
; 	  
; 	 while (IC1_count < 2850);
; 	
; 	
; 	 linetrack(0);
; 	 
;  	 stop();	  			   // at 4 way
; 	 pause(10);  
; 	 
; 	 reverse();				   // head back to 4 way intersection
; 	 
; 	 IC1_count = 0;
; 	  
; 	 while (IC1_count < 1300);
; 
; 	  linetrack(1);
; 	 
; 	 stop();
; 	 pause(10);  */
; 	
; 	// forward();	   			   //leave start box 
; 	// pause(5); 
;  	
; 	 //linetrack(0);  			   //linetrack to T
; 	
; 	/* forward();	   			   
; 	 IC1_count=0;
; 	 while (IC1_count < 2177); 
; 	 ob_led(12);
; 	 stop();	  
; 	 pause(5);
; 	 ob_led(11);   */ 
; 	
; 	
; 	
;      Speaker();
	jsr _Speaker
; 	// printf ("line = %d %d\n\r",atd1[7],atd1[6]);
; 
; 
; }
L3:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
L47:
	.word 63
	.word 6
	.word 91
	.word 79
	.word 102
	.word 109
	.word 125
	.word 7
	.word 127
	.word 111
	.word 119
	.word 124
	.word 57
	.word 94
	.word 121
	.word 113
;            out -> -32,x
;              t -> 2,x
_ob_led::
	pshd
	pshx
	tfr s,x
	leas -34,sp
; 
; 
; // Display value passed to function on on board 7-seg LED of MiniDragon Board
; void ob_led(int t)               
; {
; 
;   int out[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,
	leay -32,x
	xgdy
	ldy #L47
	pshx
	tfr d,x
	ldd #16
X2:
	movw 2,y+,2,x+
	dbne d,X2
	pulx
;     0x39,0x5e,0x79,0x71};
;   
;   DDRH=0xff;
	ldab #255
	stab 0x262
;   
;   PTH=out[t];
	leay -32,x
	sty -34,x
	ldd 2,x
	lsld
	addd -34,x
	xgdy
	ldd 0,y
	stab 0x260
; 
;   
; }
L46:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
_init_pwm::
; 
; // Set up 4-16bit PWM 
; void init_pwm()
; {
;   PWMTST = 0x00; 		   			  // turn off all PWM special Modes
	clr 0xa6
;   PWMCAE = 0x00;				  	  // PWM left aligned output mode 
	clr 0xa4
;   PWMCTL = 0xf0;					  // Use Concatenated Mode (16-bit)
	ldab #240
	stab 0xa5
;   
;   
;   PWMPRCLK = 0x33;					  // E/8 for A and B	   
	ldab #51
	stab 0xa3
;   
;   PWMCLK = 0x00;					  // PW Chan 1, 5 clock A
	clr 0xa2
;   		  							  // PW chan 3, 7 clock B
; 									  
;   PWMPOL= 0xaa;					  	  // High Polarity for all channels
	ldab #170
	stab 0xa1
;   DDRP= 0xaa;						  // Set Port P to output 
	ldab #170
	stab 0x25a
; 
;   PWMPERW1 = 60000;					  //stop (7.5%) all motors
	ldd #0xea60
	std 0xb4
;   PWMDTYW1 = 4500;					  //<- in theory this value should be 4500
	ldd #4500
	std 0xbc
;   
;   PWMPERW3 = 60000;					
	ldd #0xea60
	std 0xb6
;   PWMDTYW3 = 4500;					  //<- in theory this value should be 4500
	ldd #4500
	std 0xbe
;   
;   PWMPERW5 = 60000;					//tyw5 pin 111
	ldd #0xea60
	std 0xb8
;   PWMDTYW5 = 4500;			        // //tyw5 pin 111
	ldd #4500
	std 0xc0
; 
;   PWMPERW7 = 60000;		           	//tyw7 pin 109		 
	ldd #0xea60
	std 0xba
;   PWMDTYW7 = 4500;					//tyw7 pin 109
	ldd #4500
	std 0xc2
;   PWME= 0xaa;						  // Enable ALL PWM Channels
	ldab #170
	stab 0xa0
; 
; }
L48:
	.dbline 0 ; func end
	rts
_stop::
; 
; // Stop
; void stop()
; {
;    PWME = 0x00;
	clr 0xa0
;    PWMDTYW5 = 4500;			    //4400 tyw5 pin 111
	ldd #4500
	std 0xc0
;    PWMDTYW7 = 4500;				//4600  tyw7 pin 109
	ldd #4500
	std 0xc2
;    PWME = 0xaa;
	ldab #170
	stab 0xa0
; }
L49:
	.dbline 0 ; func end
	rts
_reverse::
;    
; // reverse
; void reverse()
; {
;    PWME = 0x00;
	clr 0xa0
;    PWMDTYW5 = 4100;		//half full speed forward 4876//tyw5 pin 111 4100
	ldd #4100
	std 0xc0
;    PWMDTYW7 = 4900;		//half full speed reverse 4125// tyw7 pin 109 4900
	ldd #4900
	std 0xc2
;    PWME = 0xaa;
	ldab #170
	stab 0xa0
; }
L50:
	.dbline 0 ; func end
	rts
_forward::
; // forward
; void forward()
; {
;    PWME = 0x00;
	clr 0xa0
;    PWMDTYW5 = 4900;				//half full speed reverse//tyw5 pin 111 4900
	ldd #4900
	std 0xc0
;    PWMDTYW7 = 4100;				//half full speed forward//tyw7 pin 109 4100
	ldd #4100
	std 0xc2
;    PWME = 0xaa;
	ldab #170
	stab 0xa0
; }
L51:
	.dbline 0 ; func end
	rts
_right::
;    
; // right
; void right()
; {
;    PWME = 0x00;
	clr 0xa0
;    PWMDTYW5 = 5000;				//orignal speed 5250//tyw5 pin 111
	ldd #5000
	std 0xc0
;    PWMDTYW7 = 5000;				//orignal speed 5250//tyw7 pin 109
	ldd #5000
	std 0xc2
;    PWME = 0xaa;
	ldab #170
	stab 0xa0
; }
L52:
	.dbline 0 ; func end
	rts
_left::
; 
; // left
; void left()
; {
;    PWME = 0x00;
	clr 0xa0
;    PWMDTYW5 = 4000;				//orignal speed 3750//tyw5 pin 111
	ldd #4000
	std 0xc0
;    PWMDTYW7 = 4000;				//orignal speed 3750//tyw7 pin 109
	ldd #4000
	std 0xc2
;    PWME = 0xaa;
	ldab #170
	stab 0xa0
; }
L53:
	.dbline 0 ; func end
	rts
_init_tof::
; 
; //Initialize Timer Overflow 
; void init_tof()
; {
;    SEI;
		SEI

;    TSCR1 = 0x80;	  				  //turn on Timer
	ldab #128
	stab 0x46
;    TFLG2 = 0x80; 					  //clear TOF flag
	ldab #128
	stab 0x4f
;    TSCR2 = 0x80;	  			  	  //turn on TOF interrupt
	ldab #128
	stab 0x4d
;    CLI;
		CLI

; }
L54:
	.dbline 0 ; func end
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
_TOF_isr::
	pshx
	tfr s,x
	leas -8,sp
; 
; //Timer Overflow Counter (ISR)
; void TOF_isr()  
; {
;    t_counts++;
	ldy #_t_counts
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L56
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __ladd
	ldy #_t_counts
	movw -4,x,0,y
	movw -2,x,2,y
;    if ((t_counts%38)==0)
	ldy #_t_counts
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L59
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lmod
	ldy #L60
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	bne L57
;    {
; 	  t_tenths++;
	ldy #_t_tenths
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L56
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __ladd
	ldy #_t_tenths
	movw -4,x,0,y
	movw -2,x,2,y
;    }
L57:
;    if ((t_counts%4)==0)
	ldy #_t_counts
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L63
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lmod
	ldy #L60
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	bne L61
;    {
;       t_hundredths++;
	ldy #_t_hundredths
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L56
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __ladd
	ldy #_t_hundredths
	movw -4,x,0,y
	movw -2,x,2,y
;    }
L61:
;    
;    TFLG2 = 0x80;
	ldab #128
	stab 0x4f
; }
L55:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rti
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;       end_time -> -12,x
;     pause_time -> 2,x
_pause::
	pshd
	pshx
	tfr s,x
	leas -12,sp
; 
; //pause for pause_time*0.1 sec
; void pause(int pause_time)
; {
;    long int end_time;
;    end_time = t_tenths + pause_time;
	ldy #_t_tenths
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldd 2,x
	jsr __d2lreg2
	jsr __ladd
	leay -12,x
	movw -4,x,0,y
	movw -2,x,2,y
L65:
L66:
; //   printf ("%ld = %ld + %d\n\r",end_time,t_tenths,pause_time);
;    while(end_time>t_tenths)
	leay -12,x
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_t_tenths
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	bgt L65
;    {
;    }
; }
L64:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;       end_time -> -12,x
;     pause_time -> 2,x
_s_pause::
	pshd
	pshx
	tfr s,x
	leas -12,sp
; 
; //pause for pause_time*0.01 sec
; void s_pause(int pause_time)
; {
;    long int end_time;
;    end_time = t_hundredths + pause_time;
	ldy #_t_hundredths
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldd 2,x
	jsr __d2lreg2
	jsr __ladd
	leay -12,x
	movw -4,x,0,y
	movw -2,x,2,y
L69:
L70:
; //   printf ("%ld = %ld + %d\n\r",end_time,t_tenths,pause_time);
;    while(end_time>t_hundredths)
	leay -12,x
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #_t_hundredths
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __lcmp
	bgt L69
;    {
;    }
; }
L68:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
_init_atd0::
; 
; // ******************* Initialize ATD *******************
; void init_atd0()
; {
;    SEI;
		SEI

;    ATD0CTL2 = 0xC2;
	ldab #194
	stab 0x82
;    ATD0CTL3 = 0x40;
	ldab #64
	stab 0x83
;    ATD0CTL4 = 0xE7;
	ldab #231
	stab 0x84
;    CLI;
		CLI

; }
L72:
	.dbline 0 ; func end
	rts
_ATD0_Interrupt::
; 
; /********************************************************************/
; /* This code is primarily taken from example 7.3.5 in the text      */
; /* and then converted to act as an ISR                              */
; /********************************************************************/
;   	  
; void ATD0_Interrupt()
; {
; /*print results to PC screen for testing             */
;    atd0[0]= ATD0DR0H;	
	; vol
	ldab 0x90
	clra
	std _atd0
;    atd0[1]= ATD0DR1H;
	; vol
	ldab 0x92
	clra
	std _atd0+2
;    atd0[2]= ATD0DR2H;
	; vol
	ldab 0x94
	clra
	std _atd0+4
;    atd0[3]= ATD0DR3H;
	; vol
	ldab 0x96
	clra
	std _atd0+6
;    atd0[4]= ATD0DR4H;
	; vol
	ldab 0x98
	clra
	std _atd0+8
;    atd0[5]= ATD0DR5H;
	; vol
	ldab 0x9a
	clra
	std _atd0+10
;    atd0[6]= ATD0DR6H;
	; vol
	ldab 0x9c
	clra
	std _atd0+12
;    atd0[7]= ATD0DR7H;		
	; vol
	ldab 0x9e
	clra
	std _atd0+14
; 
; }
L73:
	.dbline 0 ; func end
	rti
_init_atd1::
; 
; // ******************* Initialize ATD *******************
; void init_atd1()
; {
;    SEI;
		SEI

;    ATD1CTL2 = 0xC2;
	ldab #194
	stab 0x122
;    ATD1CTL3 = 0x40;
	ldab #64
	stab 0x123
;    ATD1CTL4 = 0xE7;
	ldab #231
	stab 0x124
;    CLI;
		CLI

; }
L81:
	.dbline 0 ; func end
	rts
_ATD1_Interrupt::
; 
; /********************************************************************/
; /* This code is primarily taken from example 7.3.5 in the text      */
; /* and then converted to act as an ISR                              */
; /********************************************************************/
;   	  
; void ATD1_Interrupt()
; {
; /*print results to PC screen for testing             */
;    atd1[0]= ATD1DR0H;	
	; vol
	ldab 0x130
	clra
	std _atd1
;    atd1[1]= ATD1DR1H;
	; vol
	ldab 0x132
	clra
	std _atd1+2
;    atd1[2]= ATD1DR2H;
	; vol
	ldab 0x134
	clra
	std _atd1+4
;    atd1[3]= ATD1DR3H;
	; vol
	ldab 0x136
	clra
	std _atd1+6
;    atd1[4]= ATD1DR4H;
	; vol
	ldab 0x138
	clra
	std _atd1+8
;    atd1[5]= ATD1DR5H;
	; vol
	ldab 0x13a
	clra
	std _atd1+10
;    atd1[6]= ATD1DR6H;
	; vol
	ldab 0x13c
	clra
	std _atd1+12
;    atd1[7]= ATD1DR7H;		
	; vol
	ldab 0x13e
	clra
	std _atd1+14
;  
; //Checking Line Track
;   line = 0;
	ldd #0
	std _line
;   
;   if (atd1[0] < line_threshold)
	ldd _atd1
	cpd _line_threshold
	bhs L90
;   {
;     line += 0x1;
	ldd _line
	addd #1
	std _line
;   }
L90:
;   if (atd1[1] < line_threshold)
	ldd _atd1+2
	cpd _line_threshold
	bhs L92
;   {
;     line += 0x2;
	ldd _line
	addd #2
	std _line
;    }
L92:
;   if (atd1[2] < line_threshold)
	ldd _atd1+4
	cpd _line_threshold
	bhs L95
;    {
;     line += 0x4;
	ldd _line
	addd #4
	std _line
;    }
L95:
; //Checking Intersection 
;   Intersection = 0;
	ldd #0
	std _Intersection
;   
;   if (atd1[6] < Intersection_threshold)
	ldd _atd1+12
	cpd _Intersection_threshold
	bhs L98
;   {
;     Intersection += 0x1;
	ldd _Intersection
	addd #1
	std _Intersection
;   }
L98:
;   if (atd1[7] < Intersection_threshold)
	ldd _atd1+14
	cpd _Intersection_threshold
	bhs L101
;   {
;     Intersection += 0x2;
	ldd _Intersection
	addd #2
	std _Intersection
;    }
L101:
; //Checking Backup 
;   Backup = 0;
	ldd #0
	std _Backup
;   
;   if (atd1[3] < Backup_threshold)
	ldd _atd1+6
	cpd _Backup_threshold
	bhs L104
;   {
;     Backup += 0x1;
	ldd _Backup
	addd #1
	std _Backup
;   }
L104:
;   if (atd1[4] < Backup_threshold)
	ldd _atd1+8
	cpd _Backup_threshold
	bhs L107
;   {
;     Backup += 0x2;
	ldd _Backup
	addd #2
	std _Backup
;    }
L107:
;   if (atd1[5] < Backup_threshold)
	ldd _atd1+10
	cpd _Backup_threshold
	bhs L110
;    {
;     Backup += 0x4;
	ldd _Backup
	addd #4
	std _Backup
;    }
L110:
;    
;   // printf("%x %x %x %x\n\r", atd1[0], atd1[1], atd1[2], line); 	
; }
L82:
	.dbline 0 ; func end
	rti
L114:
	.word 47801
	.word 42589
	.word 37936
	.word 35817
	.word 31888
	.word 28409
	.word 25303
	.word 23900
L115:
	.word 5
	.word 5
	.word 5
	.word 5
	.word 5
	.word 5
	.word 5
	.word 5
;              i -> -36,x
;            dur -> -34,x
;          score -> -18,x
;              j -> -2,x
_Speaker::
	pshx
	tfr s,x
	leas -38,sp
; 
; //*****************************************************************************
; #define  C5  47801
; #define  C5S 45126
; #define  D5	 42589
; #define	 D5S 40193
; #define	 E5  37936
; #define  F5	 35817
; #define  F5S 33784
; #define	 G5	 31888
; #define  G5S 30084
; #define  A5	 28409
; #define  A5S 26824
; #define  B5	 25303
; #define  C6	 23900
; #define  C6S 22542
; #define  D6  21276
; #define  D6S 20096
; #define  E6  18825
; #define  F6  17895
; #define  F6S 16892
; #define  G6	 15944
; #define  G6S 15051
; #define  A6  14204
; #define  A6S 13404
; #define  B6  12658
; 
; 
; #define  notes  8
; #define  toggle 0x04
; 
; void Speaker ()
; {
;    int j;
;    int i;
;    unsigned int score[notes] = {C5,D5,E5,F5,G5,A5,B5,C6};
	leay -18,x
	xgdy
	ldy #L114
	pshx
	tfr d,x
	ldd #8
X3:
	movw 2,y+,2,x+
	dbne d,X3
	pulx
;    unsigned int dur[notes] = {5,5,5,5,5,5,5,5};
	leay -34,x
	xgdy
	ldy #L115
	pshx
	tfr d,x
	ldd #8
X4:
	movw 2,y+,2,x+
	dbne d,X4
	pulx
; 
; //   TSCR1 = 0x90;   //Enable TCNT, fast timer flag clear
; //   TSCR2 = 0x83;   //Set TCNT prescaler to 8
;    TFLG1 = 0x20;   //Clear all TxF flags
	ldab #32
	stab 0x4e
;    TIE  |= 0x20;   //Enable TC5 interrupt
	bset 0x4c,#32
;    TIOS |= 0x20;   //Enable OC5 function
	bset 0x40,#32
;    TCTL1 = toggle; //Select toggle option as OC5 pin action
	ldab #4
	stab 0x48
;    j = 0;
	ldd #0
	std -2,x
;    dela = score[0];
	ldd -18,x
	std _dela
;    TC5 = TCNT + dela;      //Play the first note
	; vol
	ldd 0x44
	addd _dela
	std 0x5a
	bra L117
L116:
	leay -18,x
	sty -38,x
	ldd -2,x
	lsld
	addd -38,x
	xgdy
	ldd 0,y
	std _dela
	leay -34,x
	sty -38,x
	ldd -2,x
	lsld
	addd -38,x
	xgdy
	ldd 0,y
	jsr _s_pause
	ldd -2,x
	addd #1
	std -2,x
L117:
;    while(j < notes)
	ldd -2,x
	cpd #8
	blt L116
;    {
;       dela = score[j];     //Play the jth note
; 	  s_pause(dur[j]);
; 	  j++;
;    }
; 
;    TIE  &= ~0x20;   //Disable TC5 interrupt
	bclr 0x4c,#0x20
; //   TSCR1 = 0x00;   //Turn off the Timer System
;    PTT = 0x00;     //Blank Port T
	clr 0x240
;    TCTL1 = 0;
	clr 0x48
; }
L113:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rts
_OC5_ISR::
; 
; //******************************** OC5_ISR *************************************
; 
; #pragma interrupt_handler OC5_ISR()
; void OC5_ISR ()
; {
;    TC5 += dela;
	; vol
	ldd 0x5a
	addd _dela
	std 0x5a
;    TFLG1 = 0x20;
	ldab #32
	stab 0x4e
; }
L119:
	.dbline 0 ; func end
	rti
;        fr_line -> -2,x
;             fr -> 2,x
_linetrack::
	pshd
	pshx
	tfr s,x
	leas -2,sp
; //******************************************************************************
; /*measure motor speed testing             */  
; // void motorspeed();
; //{
; //   printf("%d %d\n\r",a,b);	
; 	  
;   //PWME = 0x00;
;   // PWMDTYW1 = a;					 //measure motor speed//
;   // PWMDTYW3 = b;					  //measure motor speed//
;   // PWME = 0xaa;					   //measure motor speed//
;    
;   // pause(50);						    //measure motor speed//
;   // a = a+100;
;   // b = b-100;
; 
; //}
; //******************************************************************************
; /*speedcontrol stepup testing						*/
;  /*
;  void speedcontrol()
;  
;  {
;  	ob_led(8);
; forward();	  	 // set the speed control test//
; pause(20);		 // set the speed control test//
; 	
; stop();			 // set the speed control test//
; pause(20);		 // set the speed control test//
; 	 ob_led(9);	 // set the speed control test//  
; reverse();		 // set the speed control test//
; pause(20);		 // set the speed control test//
;    
; stop();			 // set the speed control test//
; pause(20);		 // set the speed control test//
; 	
;   } 
; */
; 
; //******************************************************************************
; /*linetrack  testing						*/
; void linetrack(int fr)
; {
	bra L122
L121:
;    int fr_line;
;    while(((Intersection!=0x03)&&(fr==0))||((Backup!=0x07)&&(fr==1)))
;    {
;       if (fr==0)
	ldd 2,x
	bne L124
;       {
;          fr_line=line;
	movw _line,-2,x
;       }
	bra L125
L124:
;       else
;       {
;          fr_line=Backup;
	movw _Backup,-2,x
;       }
L125:
; 
;       ob_led(line);     
	ldd _line
	jsr _ob_led
; 
;       switch(fr_line)
	ldd -2,x
	cpd #1
	beq L128
	ldd -2,x
	cpd #2
	beq L129
	ldd -2,x
	cpd #4
	beq L132
	bra L126
X5:
;       {
L128:
; 	  case 1:
;          left();
	jsr _left
; //		   printf ("left\n\r");
; 	     break;
	bra L127
L129:
; 	  case 2:	
;          if (fr==0)
	ldd 2,x
	bne L130
; 		 {
; 		    forward();
	jsr _forward
;          }
	bra L127
L130:
; 		 else
; 		 {
; 		    reverse();
	jsr _reverse
;          }
; 
; 		 break;
	bra L127
L132:
;       case 4:		  
; 		 right();
	jsr _right
; //		   printf ("right\n\r");
; 		 break;
L126:
L127:
; /*      default:
;          if (fr==0)
; 		 {
; 		    forward();
;          }
; 		 else
; 		 {
; 		    reverse();
;          }
; */
;       }
; 	  
; 	  pause(1);
	ldd #1
	jsr _pause
;          if (fr==0)
	ldd 2,x
	bne L133
; 		 {
; 		    forward();
	jsr _forward
; 	        s_pause(5);				//original number 10
	ldd #5
	jsr _s_pause
; 	     }
	bra L134
L133:
; 		 else
; 		 {
; 		    reverse();
	jsr _reverse
; 		    s_pause(8);				//original number 25
	ldd #8
	jsr _s_pause
;          }
L134:
L122:
	ldd _Intersection
	cpd #3
	beq L135
	ldd 2,x
	beq L121
L135:
	ldd _Backup
	cpd #7
	beq L136
	ldd 2,x
	cpd #1
	lbeq L121
L136:
;    }
; }
L120:
	tfr x,s
	pulx
	leas 2,sp
	.dbline 0 ; func end
	rts
_init_ic0_1::
; 
; //******************************************************************************
; // IC0-1 Init
; 
; void init_ic0_1(void)
; {
; //   TSCR2 = 0x81;	   //might interfere with SOUND!!!!!!!!!!!!!!!! but in for testing
;    TFLG1 = 0x03;   //Clear flags
	ldab #3
	stab 0x4e
;    TIE  |= 0x03;   //Enable IC0+1 interrupt
	bset 0x4c,#3
;    TIOS &= 0xfc;   //make 0+1 input capture
	bclr 0x40,#0x3
;    TCTL4 = 0x05;   //capture 0+1 on rising edge
	ldab #5
	stab 0x4b
; }
L137:
	.dbline 0 ; func end
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
_IC0_ISR::
	pshx
	tfr s,x
	leas -8,sp
; 
; void IC0_ISR (void)
; {
;    TFLG1 = 0x01; 	// clear int flag 
	ldab #1
	stab 0x4e
;    IC0_count++;
	ldy #_IC0_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L139
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __ladd
	ldy #_IC0_count
	movw -4,x,0,y
	movw -2,x,2,y
; }
L138:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rti
;  lreg1 -> -4,x
;  lreg2 -> -8,x
_IC1_ISR::
	pshx
	tfr s,x
	leas -8,sp
; 
; 
; void IC1_ISR (void)
; {
;    TFLG1 = 0x02; 	// clear int flag
	ldab #2
	stab 0x4e
;    IC1_count++;
	ldy #_IC1_count
	movw 0,y,-4,x
	movw 2,y,-2,x
	ldy #L139
	movw 0,y,-8,x
	movw 2,y,-6,x
	jsr __ladd
	ldy #_IC1_count
	movw -4,x,0,y
	movw -2,x,2,y
; }
L140:
	tfr x,s
	pulx
	.dbline 0 ; func end
	rti
	.area bss
_Backup::
	.blkb 2
_Intersection::
	.blkb 2
_IC1_count::
	.blkb 4
_IC0_count::
	.blkb 4
_line::
	.blkb 2
_dela::
	.blkb 2
_atd1::
	.blkb 16
_atd0::
	.blkb 16
_t_hundredths::
	.blkb 4
_t_tenths::
	.blkb 4
_t_counts::
	.blkb 4
	.area text
L139:
	.word 0,1
L63:
	.word 0,4
L60:
	.word 0,0
L59:
	.word 0,38
L56:
	.word 0,1
L36:
	.word 0,19600
L32:
	.word 0,3000
L28:
	.word 0,920
L21:
	.word 0,2700
L17:
	.word 0,17000
L13:
	.word 0,0
