	.module firsttest.c
	.area data
_T_counts::
	.blkb 2
	.area idata
	.word 20000
	.area data
_per_on2::
	.blkb 2
	.area idata
	.word 60
	.area data
_per_on3::
	.blkb 2
	.area idata
	.word 70
	.area data
_cntflag::
	.blkb 2
	.area idata
	.word 0
	.area data
	.area text
L4:
	.word 0
	.word 60
	.word 70
	.word 60
L5:
	.word 0
	.word 70
	.word 60
	.word 60
L6:
	.word 0
	.word 60
	.word 70
	.word 60
L7:
	.word 0
	.word 70
	.word 60
	.word 60
;  IX -> 0,x
;  rMEM -> 2,x
;          ?temp -> +4,x
;       leftline -> +6,x
;      rightline -> +8,x
;     anglethree -> +10,x
;       angletwo -> +18,x
;          three -> +26,x
;            two -> +34,x
;          temp2 -> +42,x
;      sensorone -> +44,x
;     sensorzero -> +46,x
;          temp1 -> +48,x
_main::
	jsr __enterb
	.byte 0x32
; /* C Listing 11.4 */
; 
; #include <hc11.h>	/* Include register definitions */
; #include <stdio.h>
; #define motcont *(unsigned char volatile *)(0xB590)
; #define linetrack *(unsigned char volatile *) (0XB5A0)
; 
; 
; /* Declarations */
; unsigned int OC2hi, OC2lo, OC3hi, OC3lo;	/* Duty cycle data */
; unsigned int T_counts=0x4E20, per_on2=60, per_on3=70, cntflag=0;
; 
; 
; /* Prototypes */
; void initoc(void);
; void rtoc2(void);
; void rtoc3(void);
; void cnts2(void);
; void cnts3(void);
; void RTIINTR (void);
; 
; 
; void main(void)
; {
;  	int temp1=0, temp2=0, sensorzero=0, sensorone=0;
	ldd #0
	std 48,x
	ldd #0
	std 42,x
	ldd #0
	std 46,x
	ldd #0
	std 44,x
; 	int rightline = 0, leftline = 0;
	ldd #0
	std 8,x
	ldd #0
	std 6,x
; 	//             0  1 2  3  
; 	int two[4]=  {0,60,70,60};
	ldd 0,x
	addd #34
	ldy #L4
	pshx
	xgdx
	ldd #8
	jsr __asgnblk
	pulx
; 	int three[4]={0,70,60,60};
	ldd 0,x
	addd #26
	ldy #L5
	pshx
	xgdx
	ldd #8
	jsr __asgnblk
	pulx
; 	//                  0  1  2  3
; 	int	angletwo[4]	 ={00,60,70,60};		  
	ldd 0,x
	addd #18
	ldy #L6
	pshx
	xgdx
	ldd #8
	jsr __asgnblk
	pulx
; 	int anglethree[4]={00,70,60,60};
	ldd 0,x
	addd #10
	ldy #L7
	pshx
	xgdx
	ldd #8
	jsr __asgnblk
	pulx
; 	
; 	setbaud(BAUD9600);
	ldd #48
	jsr _setbaud
; 	INTR_OFF();
			sei

; 	/* set up jump vector for buffalo */
; 	asm("	ldaa #$7e");
			ldaa #$7e

; 	asm("	staa $dc");
			staa $dc

; 	asm("	staa $eb");
			staa $eb

; 	asm("	staa $d9");
			staa $d9

; 	asm("	ldd #_rtoc2");
			ldd #_rtoc2

; 	asm("	std $dd");
			std $dd

; 	asm("	ldd #_rtoc3");
			ldd #_rtoc3

; 	asm("	std $da");
			std $da

; 	asm("	ldd #_RTIINTR");
			ldd #_RTIINTR

; 	asm("	std $ec");
			std $ec

; 	
; 	TMSK2=0x40;
	ldab #64
	stab 0x1024
; 	PACTL=PACTL & 0xFC;
	ldy #0x1026
	bclr 0,y,#0x3
; 	PACTL=PACTL | 0x00;
	; vol
	ldab 0x1026
	stab 0x1026
; 	
; 	motcont =0x03;
	ldab #3
	stab 0xb590
; 	cnts2();
	jsr _cnts2
; 	cnts3();
	jsr _cnts3
; 	
; 	initoc();
	jsr _initoc
; 	
; 	cntflag = 0;
	ldd #0
	std _cntflag
	jmp L9
X0:
; 	
; 	while(1)
; 	{	 
L11:
; 		 
; 		 while (cntflag == 1)
; 		 {
; 			temp2 = linetrack & 0x0C;	
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #12
	std 42,x
; 		 	temp1 = linetrack & 0x03;
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #3
	std 48,x
; 			leftline = linetrack & 0xC0;
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #192
	std 6,x
; 			rightline = linetrack & 0x30;
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #48
	std 8,x
;   		 	motcont =0x03;	
	ldab #3
	stab 0xb590
	bra L15
L14:
	clr 0x1022
	clr 0x1000
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #12
	std 42,x
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #3
	std 48,x
L15:
; 		 
; 		 	while ( (temp1 == 0x00)  &&  (temp2 == 0x00) )
	ldd 48,x
	bne L17
	ldd 42,x
	beq L14
L17:
	jmp L19
L18:
; 			{
; 		 	   TMSK1=0x00;
; 			   PORTA=0x00; 
; 			   temp2 = linetrack & 0x0C;	
; 		 	   temp1 = linetrack & 0x03;
; 			}
; 		
; 		    while ( (temp1 == 0x00) && (temp2 != 0x00) )
; 		    {	  
; 			  
; 		 	  TMSK1= 0x00;
	clr 0x1022
; 			  PORTA = 0x00;
	clr 0x1000
; 			  cntflag = 0;
	ldd #0
	std _cntflag
L21:
L22:
; 			  
; 			  while (cntflag <= 244)
	ldd _cntflag
	cpd #244
	bls L21
; 			  {
; 					
; 			  }
; 			  cntflag = 0;
	ldd #0
	std _cntflag
	bra L25
L24:
; 			  
; 			  while (cntflag <= 75)
; 			  {
; 			  
; 		  	   		if (sensorzero == 1)
	ldd 46,x
	cpd #1
	bne L27
; 			  		{
; 			   	 	   motcont = 0x02;
	ldab #2
	stab 0xb590
; 					   per_on2 = 60;
	ldd #60
	std _per_on2
; 				 	   per_on3 = 70;
	ldd #70
	std _per_on3
; 			  		}
L27:
; 			  		if (sensorone == 1)
	ldd 44,x
	cpd #1
	bne L29
; 			  		{
; 					   motcont = 0x01;
	ldab #1
	stab 0xb590
; 			   	 	   per_on2 = 70;
	ldd #70
	std _per_on2
; 				 	   per_on3 = 60;
	ldd #60
	std _per_on3
; 			  		}
L29:
; 			 		TMSK1 = 0x60;
	ldab #96
	stab 0x1022
; 			  		cnts2();
	jsr _cnts2
; 			  		cnts3();
	jsr _cnts3
; 			  		temp1 = linetrack & 0x03;
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #3
	std 48,x
; 					cntflag = 0;
	ldd #0
	std _cntflag
	bra L32
L31:
	clr 0x1022
	clr 0x1000
L32:
; 					while (cntflag <= 183)
	ldd _cntflag
	cpd #183
	bls L31
; 					{
; 					  TMSK1 = 0x00;
; 					  PORTA = 0x00;
; 					}
; 					if (temp1 == 0)
	ldd 48,x
	bne L34
; 					{
; 					 cntflag = 0;
	ldd #0
	std _cntflag
; 					}
L34:
L25:
	ldd _cntflag
	cpd #75
	bhi X2
	jmp L24
X2:
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #3
	std 48,x
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #12
	std 42,x
L19:
	ldd 48,x
	bne L36
	ldd 42,x
	beq X3
	jmp L18
X3:
L36:
	jmp L38
L37:
; 					
; 					
;               }
; 			  temp1 = linetrack & 0x03;
; 			  temp2 = linetrack & 0x0C;
; 	        }
; 		
; 		while ((two[temp1] != 0) && (three[temp1] != 0 ))
; 		{   
; 		  	  sensorzero = 0;
	ldd #0
	std 46,x
; 			  sensorone = 0;
	ldd #0
	std 44,x
; 		  	  if(temp1 == 0x01)
	ldd 48,x
	cpd #1
	bne L40
; 			  {
; 		  	  sensorzero = 1;
	ldd #1
	std 46,x
; 			  }
L40:
; 		  	  if (temp1 == 0x02)
	ldd 48,x
	cpd #2
	bne L42
; 			  {
; 		  	  sensorone = 1;
	ldd #1
	std 44,x
; 			  }
L42:
	ldab #96
	stab 0x1022
	ldab #3
	stab 0xb590
	ldd 0,x
	addd #34
	std 2,x
	ldd 48,x
	lsld
	addd 2,x
	xgdy
	ldd 0,y
	std _per_on2
	ldd 0,x
	addd #26
	std 2,x
	ldd 48,x
	lsld
	addd 2,x
	xgdy
	ldd 0,y
	std _per_on3
	jsr _cnts2
	jsr _cnts3
	ldd #0
	std _cntflag
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #3
	std 48,x
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #12
	std 42,x
L38:
	ldd 48,x
	lsld
	std 4,x
	ldd 0,x
	addd #34
	std 2,x
	ldd 4,x
	addd 2,x
	xgdy
	ldd 0,y
	beq L44
	ldd 0,x
	addd #26
	std 2,x
	ldd 4,x
	addd 2,x
	xgdy
	ldd 0,y
	beq X4
	jmp L37
X4:
L44:
	jmp L46
L45:
; 		  	  TMSK1 = 0x60;
; 			  motcont = 0x03;
; 		  	  per_on2 = two[temp1];
; 		  	  per_on3 = three[temp1];
; 		  	  cnts2();
; 		  	  cnts3();
; 			  cntflag = 0;
; 			  temp1 = linetrack & 0x03;
; 			  temp2 = linetrack & 0x0C;
; 		 }
;        	 while (temp1 == 0x03)
; 		 {
; 		   	  if (temp2 == 0x00)
	ldd 42,x
	bne L48
; 		  	  {
; 		  	  	TMSK1 = 0x60;
	ldab #96
	stab 0x1022
; 				motcont = 0x03;
	ldab #3
	stab 0xb590
; 		  	 	per_on2 = 30;
	ldd #30
	std _per_on2
; 		  	 	per_on3 = 30;
	ldd #30
	std _per_on3
; 		  	 	cnts2();
	jsr _cnts2
; 		  	 	cnts3();
	jsr _cnts3
; 				cntflag = 0;
	ldd #0
	std _cntflag
; 				
; 		  	  }
L48:
	ldab #3
	stab 0xb590
	ldd 0,x
	addd #18
	std 2,x
	ldd 42,x
	lsld
	addd 2,x
	xgdy
	ldd 0,y
	std _per_on2
	ldd 0,x
	addd #10
	std 2,x
	ldd 42,x
	lsld
	addd 2,x
	xgdy
	ldd 0,y
	std _per_on3
	jsr _cnts2
	jsr _cnts3
	; vol
	ldab 0xb5a0
	clra
	anda #0
	andb #3
	std 48,x
L46:
	ldd 48,x
	cpd #3
	bne X5
	jmp L45
X5:
	ldd #0
	std _cntflag
L12:
	ldd _cntflag
	cpd #1
	bne X6
	jmp L11
X6:
L9:
	bra L12
X1:
; 		  	 motcont = 0x03;
; 		  	 per_on2 = angletwo[temp2];
; 		  	 per_on3 = anglethree[temp2];
; 		  	 cnts2();
; 		 	 cnts3();
; 			 temp1 = linetrack & 0x03; 
; 		}
; 		  
; 
; 	    cntflag = 0;
; 	  }
; 		
;     }
; 
; 	
; }
L3:
	xgdx
	addd #50
	xgdx
	txs
	pulx
	.dbline 0 ; func end
	rts
_initoc::
; 
; void initoc(void)
; /* Initialize OC2 */
; {
; 	TOC2 = TCNT;
	; vol
	ldd 0x100e
	std 0x1018
; 	TOC3 = TCNT;
	; vol
	ldd 0x100e
	std 0x101a
; 	TCTL1 = 0x00;
	clr 0x1020
; 	TFLG1 = 0x60;
	ldab #96
	stab 0x1023
; 	TMSK1 = 0x60;
	ldab #96
	stab 0x1022
; 	INTR_ON();
			cli

; }
L50:
	.dbline 0 ; func end
	rts
_rtoc2::
; 
; void rtoc2(void)
; /* Interrupt Service Routine for OC2 PWM */
; {   
; 	if ((PORTA & 0X40) == 0){ 
	ldy #0x1000
	brclr 0,y,#64,X7
	bra L52
X7:
; 		TOC2 = TOC2 + OC2hi;
	; vol
	ldd 0x1018
	addd _OC2hi
	std 0x1018
; 		PORTA = PORTA | 0x40;
	ldy #0x1000
	bset 0,y,#64
; 		}
	bra L53
L52:
; 	else{
; 		TOC2 = TOC2 + OC2lo;
	; vol
	ldd 0x1018
	addd _OC2lo
	std 0x1018
; 		PORTA = PORTA & 0xBF;
	ldy #0x1000
	bclr 0,y,#0x40
; 		}
L53:
; 	//TCTL1 = TCTL1 ^ 0x40;	/* invert OL2 to toggle next OC2 edge */
; 	TFLG1 = 0x40;	/* Clear flag OC2F */
	ldab #64
	stab 0x1023
; 	asm("	rti");	/* This is an interrupt service routine */
			rti

; }
L51:
	.dbline 0 ; func end
	rts
_rtoc3::
; 
; void rtoc3(void)
; {	
; 	if ((PORTA & 0X20) == 0) {
	ldy #0x1000
	brclr 0,y,#32,X8
	bra L55
X8:
; 		TOC3 = TOC3 + OC3hi;
	; vol
	ldd 0x101a
	addd _OC3hi
	std 0x101a
; 		PORTA = PORTA | 0x20;
	ldy #0x1000
	bset 0,y,#32
; 	}	
	bra L56
L55:
; 	else{
; 		TOC3 = TOC3 + OC3lo;
	; vol
	ldd 0x101a
	addd _OC3lo
	std 0x101a
; 		PORTA = PORTA & 0xDF;
	ldy #0x1000
	bclr 0,y,#0x20
; 	}
L56:
; 	//TCTL1 = TCTL1 ^ 0x10;	/* invert OL2 to toggle next OC2 edge */
; 	TFLG1 = 0x20;	/* Clear flag OC2F */
	ldab #32
	stab 0x1023
; 	asm("	rti");	/* This is an interrupt service routine */
			rti

; }
L54:
	.dbline 0 ; func end
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;  IX -> 0,x
;          ?temp -> +2,x
;          ?temp -> +6,x
_cnts2::
	jsr __enterb
	.byte 0x88
; void cnts2 (void)
; {
; 	OC2hi=T_counts * (float)per_on2/100;
	ldd _per_on2
	lsrd
	jsr __d2lreg
	jsr __long2fp
	jsr __fpmov
	ldy #L61
	jsr __tofp1
	jsr __fpmul
	ldd _per_on2
	anda #0
	andb #1
	jsr __d2lreg
	jsr __pshfp1 ; spill
	jsr __long2fp
	jsr __fpmov
	jsr __pulfp1 ; reload
	jsr __fpadd
	jsr __fpmov
	ldd _T_counts
	lsrd
	jsr __d2lreg
	jsr __long2fp
	jsr __pshfp2 ; spill
	jsr __fpmov
	ldy #L61
	jsr __tofp1
	jsr __fpmul
	ldd _T_counts
	anda #0
	andb #1
	jsr __d2lreg
	jsr __pshfp1 ; spill
	jsr __long2fp
	jsr __fpmov
	jsr __pulfp1 ; reload
	jsr __fpadd
	jsr __pulfp2 ; reload
	jsr __fpmul
	ldy #L62
	jsr __tofp2
	jsr __fpdiv
	ldd 0,x
	addd #2
	xgdy
	jsr __fromfp1
	ldd 0,x
	addd #2
	xgdy
	jsr __tofp1
	ldy #L63
	jsr __tofp2
	jsr __fpcmp
	blt L59
	ldd 0,x
	addd #2
	xgdy
	jsr __tofp1
	ldy #L63
	jsr __tofp2
	jsr __fpsub
	jsr __fp2int
	addd #0x8000
	std 6,x
	bra L60
L59:
	ldd 0,x
	addd #2
	xgdy
	jsr __tofp1
	jsr __fp2int
	std 6,x
L60:
	ldd 6,x
	std _OC2hi
; 	OC2lo=T_counts - OC2hi;
	ldd _T_counts
	subd _OC2hi
	std _OC2lo
; }
L57:
	xgdx
	addd #8
	xgdx
	txs
	pulx
	.dbline 0 ; func end
	rts
;  lreg1 -> -4,x
;  lreg2 -> -8,x
;  IX -> 0,x
;          ?temp -> +2,x
;          ?temp -> +6,x
_cnts3::
	jsr __enterb
	.byte 0x88
; 
; void cnts3 (void)
; {
;   	OC3hi=T_counts * (float)per_on3/100;
	ldd _per_on3
	lsrd
	jsr __d2lreg
	jsr __long2fp
	jsr __fpmov
	ldy #L61
	jsr __tofp1
	jsr __fpmul
	ldd _per_on3
	anda #0
	andb #1
	jsr __d2lreg
	jsr __pshfp1 ; spill
	jsr __long2fp
	jsr __fpmov
	jsr __pulfp1 ; reload
	jsr __fpadd
	jsr __fpmov
	ldd _T_counts
	lsrd
	jsr __d2lreg
	jsr __long2fp
	jsr __pshfp2 ; spill
	jsr __fpmov
	ldy #L61
	jsr __tofp1
	jsr __fpmul
	ldd _T_counts
	anda #0
	andb #1
	jsr __d2lreg
	jsr __pshfp1 ; spill
	jsr __long2fp
	jsr __fpmov
	jsr __pulfp1 ; reload
	jsr __fpadd
	jsr __pulfp2 ; reload
	jsr __fpmul
	ldy #L62
	jsr __tofp2
	jsr __fpdiv
	ldd 0,x
	addd #2
	xgdy
	jsr __fromfp1
	ldd 0,x
	addd #2
	xgdy
	jsr __tofp1
	ldy #L63
	jsr __tofp2
	jsr __fpcmp
	blt L66
	ldd 0,x
	addd #2
	xgdy
	jsr __tofp1
	ldy #L63
	jsr __tofp2
	jsr __fpsub
	jsr __fp2int
	addd #0x8000
	std 6,x
	bra L67
L66:
	ldd 0,x
	addd #2
	xgdy
	jsr __tofp1
	jsr __fp2int
	std 6,x
L67:
	ldd 6,x
	std _OC3hi
; 	OC3lo=T_counts - OC3hi;
	ldd _T_counts
	subd _OC3hi
	std _OC3lo
; }	
L64:
	xgdx
	addd #8
	xgdx
	txs
	pulx
	.dbline 0 ; func end
	rts
_RTIINTR::
; 
; void RTIINTR (void)
; {
;   TFLG2 = 0x40;
	ldab #64
	stab 0x1025
;   
;   cntflag ++;
	ldd _cntflag
	addd #1
	std _cntflag
;   
;   asm("	  rti");
			  rti

; }
L68:
	.dbline 0 ; func end
	rts
	.area bss
_OC3lo::
	.blkb 2
_OC3hi::
	.blkb 2
_OC2lo::
	.blkb 2
_OC2hi::
	.blkb 2
	.area text
L63:
	.word 0x4700,0x0
L62:
	.word 0x42c8,0x0
L61:
	.word 0x4000,0x0
