                        .module robot.c
                        .area memory(abs)
                        .org 0x3e5e
 3E5E           _IRQ_TOF_vector::
 3E5E 022E              .word _TOF_isr
                        .area data
                        .area memory(abs)
                        .org 0x3e52
 3E52           _IRQ_ATD0_vector::
 3E52 0394              .word _ATD0_Interrupt
                        .area data
                        .area memory(abs)
                        .org 0x3e50
 3E50           _IRQ_ATD1_vector::
 3E50 03E1              .word _ATD1_Interrupt
                        .area data
                        .area memory(abs)
                        .org 0x3e64
 3E64           _IRQ_OC5_vector::
 3E64 050F              .word _OC5_ISR
                        .area data
                        .area text
 0000           _line_threshhold::
 0000 005A              .word 90
 0002           ;  lreg1 -> -4,x
 0002           ;  lreg2 -> -8,x
 0002           ;          ?temp -> -18,x
 0002           ;           time -> -16,x
 0002           ;              t -> -12,x
 0002           ;              i -> -10,x
 0002           _main::
 0002 34                pshx
 0003 B775              tfr s,x
 0005 1BF1E6            leas -26,sp
 0008           ; //*****************************************************************************
 0008           ; // Name:                              2007 Line Track code
 0008           ; // Vers:                          0.1
 0008           ; // Last Revised:              1/27/07 10:16pm
 0008           ; //*****************************************************************************                                       
 0008           ; 
 0008           ; #include<mc9S12dp256.h>
 0008           ; #include<stdio.h>
 0008           ; 
 0008           ; //define 16 bit PWM registers
 0008           ; #define PWMPERW1 _LP(0xB4) 
 0008           ; #define PWMPERW3 _LP(0xB6)
 0008           ; #define PWMPERW5 _LP(0xB8)
 0008           ; #define PWMPERW7 _LP(0xBA)
 0008           ; 
 0008           ; #define PWMDTYW1 _LP(0xBC)
 0008           ; #define PWMDTYW3 _LP(0xBE)
 0008           ; #define PWMDTYW5 _LP(0xC0)
 0008           ; #define PWMDTYW7 _LP(0xC2)
 0008           ; 
 0008           ; // Function Prototypes
 0008           ; void init_pwm(void);
 0008           ; void init_timer(void);
 0008           ; void ob_led(int);
 0008           ; void pause(int);
 0008           ; void s_pause(int);
 0008           ; void init_tof(void);
 0008           ; void init_atd0(void);
 0008           ; void Speaker(void);
 0008           ; void stop(void);
 0008           ; void forward(void);
 0008           ; void reverse(void);
 0008           ; void left(void);
 0008           ; void right(void);
 0008           ; 
 0008           ; //General 
 0008           ; #define CLI asm("CLI")
 0008           ; #define SEI asm("SEI")
 0008           ; 
 0008           ; //TOF Interrupt Setup
 0008           ; void TOF_isr(void); 
 0008           ; #pragma interrupt_handler TOF_isr
 0008           ; #pragma abs_address: 0x3e5e
 0008           ; void (*IRQ_TOF_vector[])()={TOF_isr};
 0008           ; #pragma end_abs_address
 0008           ; 
 0008           ; //ATD0 Interrupt Setup
 0008           ; void ATD0_Interrupt();
 0008           ; #pragma interrupt_handler ATD0_Interrupt()
 0008           ; #pragma abs_address: 0x3E52
 0008           ; void (*IRQ_ATD0_vector[])()={ATD0_Interrupt};
 0008           ; #pragma end_abs_address
 0008           ; 
 0008           ; //ATD1 Interrupt Setup
 0008           ; void ATD1_Interrupt();
 0008           ; #pragma interrupt_handler ATD1_Interrupt()
 0008           ; #pragma abs_address: 0x3E50
 0008           ; void (*IRQ_ATD1_vector[])()={ATD1_Interrupt};
 0008           ; #pragma end_abs_address
 0008           ; 
 0008           ; //OC5 Interrupt Setup
 0008           ; void OC5_ISR();
 0008           ; #pragma interrupt_handler OC5_ISR()
 0008           ; #pragma abs_address: 0x3E64
 0008           ; void (*IRQ_OC5_vector[])()={OC5_ISR};
 0008           ; #pragma end_abs_address
 0008           ; 
 0008           ; //declare global variable
 0008           ; long int t_counts;
 0008           ; long int t_tenths;
 0008           ; long int t_hundredths;
 0008           ; unsigned int atd0[8];
 0008           ; int atd1[8];
 0008           ; int dela;
 0008           ; int line;
 0008           ; const line_threshhold = 90;
 0008           ; void main(void)
 0008           ; {
 0008           ; 
 0008           ;    int t,i;
 0008           ;    long int time;
 0008           ;    
 0008           ;    init_pwm();
 0008 16015E            jsr _init_pwm
 000B           ;    init_tof();
 000B 16021A            jsr _init_tof
 000E           ;    init_atd0();
 000E 160380            jsr _init_atd0
 0011           ;    init_atd1();
 0011 1603CD            jsr _init_atd1
 0014           ;    pause(1);
 0014 CC0001            ldd #1
 0017 1602FA            jsr _pause
 001A           ;    ATD0CTL5 = 0x30;
 001A C630              ldab #48
 001C 7B0085            stab 0x85
 001F           ;    ATD1CTL5 = 0x30;
 001F C630              ldab #48
 0021 7B0125            stab 0x125
 0024 182000D3          lbra L5
 0028           L4:
 0028           ; 
 0028           ;    while(1)
 0028           ;    {
 0028           ;       t++;
 0028 EC14              ldd -12,x
 002A C30001            addd #1
 002D 6C14              std -12,x
 002F           ;         if (t>15)
 002F EC14              ldd -12,x
 0031 8C000F            cpd #15
 0034 2F05              ble L7
 0036           ;            t=0;
 0036 CC0000            ldd #0
 0039 6C14              std -12,x
 003B           L7:
 003B           ;       		 
 003B           ;       ob_led(t);
 003B EC14              ldd -12,x
 003D 160123            jsr _ob_led
 0040           ;         pause(10);
 0040 CC000A            ldd #10
 0043 1602FA            jsr _pause
 0046           ;         for (i=0;i<=7;i++)
 0046 CC0000            ldd #0
 0049 6C16              std -10,x
 004B           L9:
 004B EC16              ldd -10,x
 004D 59                lsld
 004E 6CE1EE            std -18,x
 0051 C30004            addd #_atd1
 0054 B7C6              xgdy
 0056 ED40              ldy 0,y
 0058 6D86              sty 6,sp
 005A 18021684          movw -10,x,4,sp
 005E ECE1EE            ldd -18,x
 0061 C30014            addd #_atd0
 0064 B7C6              xgdy
 0066 ED40              ldy 0,y
 0068 6D82              sty 2,sp
 006A 18021680          movw -10,x,0,sp
 006E CC055E            ldd #L13
 0071 160000            jsr _printf
 0074           L10:
 0074 EC16              ldd -10,x
 0076 C30001            addd #1
 0079 6C16              std -10,x
 007B EC16              ldd -10,x
 007D 8C0007            cpd #7
 0080 2FC9              ble L9
 0082           ;         {
 0082           ;            printf ("atd0[%u] = %u   atd1[%u] = %u\n\r",i,atd0[i],i,atd1[i]);
 0082           ;         }
 0082           ;         printf("%ld %ld\n\r",t_tenths,t_hundredths);
 0082 CD0024            ldy #_t_hundredths
 0085 1802401C          movw 0,y,-4,x
 0089 1802421E          movw 2,y,-2,x
 008D 1984              leay 4,sp
 008F 18021C40          movw -4,x,0,y
 0093 18021E42          movw -2,x,2,y
 0097 CD0028            ldy #_t_tenths
 009A 1802401C          movw 0,y,-4,x
 009E 1802421E          movw 2,y,-2,x
 00A2 1980              leay 0,sp
 00A4 18021C40          movw -4,x,0,y
 00A8 18021E42          movw -2,x,2,y
 00AC CC0554            ldd #L14
 00AF 160000            jsr _printf
 00B2           ;         
 00B2           ;       switch(line)
 00B2 FC0000            ldd _line
 00B5 8C0001            cpd #1
 00B8 2712              beq L17
 00BA FC0000            ldd _line
 00BD 8C0002            cpd #2
 00C0 2715              beq L19
 00C2 FC0000            ldd _line
 00C5 8C0004            cpd #4
 00C8 2718              beq L21
 00CA 2021              bra L15
 00CC           X0:
 00CC           ; 	{
 00CC           L17:
 00CC           ; 	   case 1:
 00CC           ; 	      left(); 
 00CC 160205            jsr _left
 00CF           ;                 printf ("left\n\r");
 00CF CC054D            ldd #L18
 00D2 160000            jsr _printf
 00D5           ;             break;
 00D5 2019              bra L16
 00D7           L19:
 00D7           ; 	   case 2:	
 00D7           ;           forward();
 00D7 1601C6            jsr _forward
 00DA           ;                 printf ("forward\n\r");
 00DA CC0543            ldd #L20
 00DD 160000            jsr _printf
 00E0           ;                 break;
 00E0 200E              bra L16
 00E2           L21:
 00E2           ;        case 4:		  
 00E2           ; 	      right();
 00E2 1601F0            jsr _right
 00E5           ;                 printf ("right\n\r");
 00E5 CC053B            ldd #L22
 00E8 160000            jsr _printf
 00EB           ;                 break;
 00EB 2003              bra L16
 00ED           L15:
 00ED           ; 	    default:
 00ED           ; 		  forward();
 00ED 1601C6            jsr _forward
 00F0           ;       }   
 00F0           L16:
 00F0 1801800000        movw _line,0,sp
 00F5 CC052E            ldd #L23
 00F8 160000            jsr _printf
 00FB           L5:
 00FB 1820FF29          lbra L4
 00FF           X1:
 00FF           ; //	  Speaker();
 00FF           ; 	  printf ("line = %d \n\r",line);
 00FF           ;     
 00FF           ;    }
 00FF           ; }
 00FF           L3:
 00FF B757              tfr x,s
 0101 30                pulx
 0102                   .dbline 0 ; func end
 0102 3D                rts
 0103           L25:
 0103 003F              .word 63
 0105 0006              .word 6
 0107 005B              .word 91
 0109 004F              .word 79
 010B 0066              .word 102
 010D 006D              .word 109
 010F 007D              .word 125
 0111 0007              .word 7
 0113 007F              .word 127
 0115 006F              .word 111
 0117 0077              .word 119
 0119 007C              .word 124
 011B 0039              .word 57
 011D 005E              .word 94
 011F 0079              .word 121
 0121 0071              .word 113
 0123           ;            out -> -32,x
 0123           ;              t -> 2,x
 0123           _ob_led::
 0123 3B                pshd
 0124 34                pshx
 0125 B775              tfr s,x
 0127 1BF1DE            leas -34,sp
 012A           ; 
 012A           ; 
 012A           ; // Display value passed to function on on board 7-seg LED of MiniDragon Board
 012A           ; void ob_led(int t)               
 012A           ; {
 012A           ; 
 012A           ;   int out[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,
 012A 19E1E0            leay -32,x
 012D B7C6              xgdy
 012F CD0103            ldy #L25
 0132 34                pshx
 0133 B745              tfr d,x
 0135 CC0010            ldd #16
 0138           X2:
 0138 18027131          movw 2,y+,2,x+
 013C 0434F9            dbne d,X2
 013F 30                pulx
 0140           ;     0x39,0x5e,0x79,0x71};
 0140           ;   
 0140           ;   DDRH=0xff;
 0140 C6FF              ldab #255
 0142 7B0262            stab 0x262
 0145           ;   
 0145           ;   PTH=out[t];
 0145 19E1E0            leay -32,x
 0148 6DE1DE            sty -34,x
 014B EC02              ldd 2,x
 014D 59                lsld
 014E E3E1DE            addd -34,x
 0151 B7C6              xgdy
 0153 EC40              ldd 0,y
 0155 7B0260            stab 0x260
 0158           ; 
 0158           ;   
 0158           ; }
 0158           L24:
 0158 B757              tfr x,s
 015A 30                pulx
 015B 1B82              leas 2,sp
 015D                   .dbline 0 ; func end
 015D 3D                rts
 015E           _init_pwm::
 015E           ; 
 015E           ; // Set up 4-16bit PWM 
 015E           ; void init_pwm()
 015E           ; {
 015E           ;   PWMTST = 0x00; 		   			  // turn off all PWM special Modes
 015E 7900A6            clr 0xa6
 0161           ;   PWMCAE = 0x00;                                        // PWM left aligned output mode 
 0161 7900A4            clr 0xa4
 0164           ;   PWMCTL = 0xf0;                                        // Use Concatenated Mode (16-bit)
 0164 C6F0              ldab #240
 0166 7B00A5            stab 0xa5
 0169           ;   
 0169           ;   
 0169           ;   PWMPRCLK = 0x33;                                      // E/8 for A and B       
 0169 C633              ldab #51
 016B 7B00A3            stab 0xa3
 016E           ;   
 016E           ;   PWMCLK = 0x00;                                        // PW Chan 1, 5 clock A
 016E 7900A2            clr 0xa2
 0171           ;                                                                         // PW chan 3, 7 clock B
 0171           ;                                                                         
 0171           ;   PWMPOL= 0xaa;                                                 // High Polarity for all channels
 0171 C6AA              ldab #170
 0173 7B00A1            stab 0xa1
 0176           ;   DDRP= 0xaa;                                           // Set Port P to output 
 0176 C6AA              ldab #170
 0178 7B025A            stab 0x25a
 017B           ; 
 017B           ;   PWMPERW1 = 60000;                                     //stop (7.5%) all motors
 017B CCEA60            ldd #0xea60
 017E 7C00B4            std 0xb4
 0181           ;   PWMDTYW1 = 4500;
 0181 CC1194            ldd #4500
 0184 7C00BC            std 0xbc
 0187           ;   
 0187           ;   PWMPERW3 = 60000;                                   
 0187 CCEA60            ldd #0xea60
 018A 7C00B6            std 0xb6
 018D           ;   PWMDTYW3 = 4500;
 018D CC1194            ldd #4500
 0190 7C00BE            std 0xbe
 0193           ;   
 0193           ;   PWMPERW5 = 60000;                                   
 0193 CCEA60            ldd #0xea60
 0196 7C00B8            std 0xb8
 0199           ;   PWMDTYW5 = 4500;
 0199 CC1194            ldd #4500
 019C 7C00C0            std 0xc0
 019F           ; 
 019F           ;   PWMPERW7 = 60000;                                    
 019F CCEA60            ldd #0xea60
 01A2 7C00BA            std 0xba
 01A5           ;   PWMDTYW7 = 4500;
 01A5 CC1194            ldd #4500
 01A8 7C00C2            std 0xc2
 01AB           ;   PWME= 0xaa;                                           // Enable ALL PWM Channels
 01AB C6AA              ldab #170
 01AD 7B00A0            stab 0xa0
 01B0           ; 
 01B0           ; }
 01B0           L26:
 01B0                   .dbline 0 ; func end
 01B0 3D                rts
 01B1           _stop::
 01B1           ; 
 01B1           ; // Stop
 01B1           ; void stop(void)
 01B1           ; {
 01B1           ;    PWME = 0x00;
 01B1 7900A0            clr 0xa0
 01B4           ;    PWMDTYW1 = 4500;
 01B4 CC1194            ldd #4500
 01B7 7C00BC            std 0xbc
 01BA           ;    PWMDTYW3 = 4500;
 01BA CC1194            ldd #4500
 01BD 7C00BE            std 0xbe
 01C0           ;    PWME = 0xaa;
 01C0 C6AA              ldab #170
 01C2 7B00A0            stab 0xa0
 01C5           ; }
 01C5           L27:
 01C5                   .dbline 0 ; func end
 01C5 3D                rts
 01C6           _forward::
 01C6           ;    
 01C6           ; // forward
 01C6           ; void forward(void)
 01C6           ; {
 01C6           ;    PWME = 0x00;
 01C6 7900A0            clr 0xa0
 01C9           ;    PWMDTYW1 = 3000;
 01C9 CC0BB8            ldd #3000
 01CC 7C00BC            std 0xbc
 01CF           ;    PWMDTYW3 = 6000;
 01CF CC1770            ldd #6000
 01D2 7C00BE            std 0xbe
 01D5           ;    PWME = 0xaa;
 01D5 C6AA              ldab #170
 01D7 7B00A0            stab 0xa0
 01DA           ; }
 01DA           L28:
 01DA                   .dbline 0 ; func end
 01DA 3D                rts
 01DB           _reverse::
 01DB           ; // reverse
 01DB           ; void reverse(void)
 01DB           ; {
 01DB           ;    PWME = 0x00;
 01DB 7900A0            clr 0xa0
 01DE           ;    PWMDTYW1 = 6000;
 01DE CC1770            ldd #6000
 01E1 7C00BC            std 0xbc
 01E4           ;    PWMDTYW3 = 3000;
 01E4 CC0BB8            ldd #3000
 01E7 7C00BE            std 0xbe
 01EA           ;    PWME = 0xaa;
 01EA C6AA              ldab #170
 01EC 7B00A0            stab 0xa0
 01EF           ; }
 01EF           L29:
 01EF                   .dbline 0 ; func end
 01EF 3D                rts
 01F0           _right::
 01F0           ;    
 01F0           ; // right
 01F0           ; void right(void)
 01F0           ; {
 01F0           ;    PWME = 0x00;
 01F0 7900A0            clr 0xa0
 01F3           ;    PWMDTYW1 = 6000;
 01F3 CC1770            ldd #6000
 01F6 7C00BC            std 0xbc
 01F9           ;    PWMDTYW3 = 6000;
 01F9 CC1770            ldd #6000
 01FC 7C00BE            std 0xbe
 01FF           ;    PWME = 0xaa;
 01FF C6AA              ldab #170
 0201 7B00A0            stab 0xa0
 0204           ; }
 0204           L30:
 0204                   .dbline 0 ; func end
 0204 3D                rts
 0205           _left::
 0205           ; 
 0205           ; // left
 0205           ; void left(void)
 0205           ; {
 0205           ;    PWME = 0x00;
 0205 7900A0            clr 0xa0
 0208           ;    PWMDTY1 = 3000;
 0208 CC0BB8            ldd #3000
 020B 7B00BD            stab 0xbd
 020E           ;    PWMDTY3 = 3000;
 020E CC0BB8            ldd #3000
 0211 7B00BF            stab 0xbf
 0214           ;    PWME = 0xaa;
 0214 C6AA              ldab #170
 0216 7B00A0            stab 0xa0
 0219           ; }
 0219           L31:
 0219                   .dbline 0 ; func end
 0219 3D                rts
 021A           _init_tof::
 021A           ; 
 021A           ; //Initialize Timer Overflow 
 021A           ; void init_tof()
 021A           ; {
 021A           ;    SEI;
 021A 1410                      SEI
 021C           
 021C           ;    TSCR1 = 0x80;                                        //turn on Timer
 021C C680              ldab #128
 021E 7B0046            stab 0x46
 0221           ;    TFLG2 = 0x80;                                        //clear TOF flag
 0221 C680              ldab #128
 0223 7B004F            stab 0x4f
 0226           ;    TSCR2 = 0x80;                                        //turn on TOF interrupt
 0226 C680              ldab #128
 0228 7B004D            stab 0x4d
 022B           ;    CLI;
 022B 10EF                      CLI
 022D           
 022D           ; }
 022D           L32:
 022D                   .dbline 0 ; func end
 022D 3D                rts
 022E           ;  lreg1 -> -4,x
 022E           ;  lreg2 -> -8,x
 022E           _TOF_isr::
 022E 34                pshx
 022F B775              tfr s,x
 0231 1B98              leas -8,sp
 0233           ; 
 0233           ; //Timer Overflow Counter (ISR)
 0233           ; void TOF_isr(void)  
 0233           ; {
 0233           ;    t_counts++;
 0233 CD002C            ldy #_t_counts
 0236 1802401C          movw 0,y,-4,x
 023A 1802421E          movw 2,y,-2,x
 023E CD052A            ldy #L34
 0241 18024018          movw 0,y,-8,x
 0245 1802421A          movw 2,y,-6,x
 0249 160000            jsr __ladd
 024C CD002C            ldy #_t_counts
 024F 18021C40          movw -4,x,0,y
 0253 18021E42          movw -2,x,2,y
 0257           ;    if ((t_counts%38)==0)
 0257 CD002C            ldy #_t_counts
 025A 1802401C          movw 0,y,-4,x
 025E 1802421E          movw 2,y,-2,x
 0262 CD0526            ldy #L37
 0265 18024018          movw 0,y,-8,x
 0269 1802421A          movw 2,y,-6,x
 026D 160000            jsr __lmod
 0270 CD0522            ldy #L38
 0273 18024018          movw 0,y,-8,x
 0277 1802421A          movw 2,y,-6,x
 027B 160000            jsr __lcmp
 027E 2624              bne L35
 0280           ;    {
 0280           ;         t_tenths++;
 0280 CD0028            ldy #_t_tenths
 0283 1802401C          movw 0,y,-4,x
 0287 1802421E          movw 2,y,-2,x
 028B CD052A            ldy #L34
 028E 18024018          movw 0,y,-8,x
 0292 1802421A          movw 2,y,-6,x
 0296 160000            jsr __ladd
 0299 CD0028            ldy #_t_tenths
 029C 18021C40          movw -4,x,0,y
 02A0 18021E42          movw -2,x,2,y
 02A4           ;    }
 02A4           L35:
 02A4           ;    if ((t_counts%4)==0)
 02A4 CD002C            ldy #_t_counts
 02A7 1802401C          movw 0,y,-4,x
 02AB 1802421E          movw 2,y,-2,x
 02AF CD051E            ldy #L41
 02B2 18024018          movw 0,y,-8,x
 02B6 1802421A          movw 2,y,-6,x
 02BA 160000            jsr __lmod
 02BD CD0522            ldy #L38
 02C0 18024018          movw 0,y,-8,x
 02C4 1802421A          movw 2,y,-6,x
 02C8 160000            jsr __lcmp
 02CB 2624              bne L39
 02CD           ;    {
 02CD           ;       t_hundredths++;
 02CD CD0024            ldy #_t_hundredths
 02D0 1802401C          movw 0,y,-4,x
 02D4 1802421E          movw 2,y,-2,x
 02D8 CD052A            ldy #L34
 02DB 18024018          movw 0,y,-8,x
 02DF 1802421A          movw 2,y,-6,x
 02E3 160000            jsr __ladd
 02E6 CD0024            ldy #_t_hundredths
 02E9 18021C40          movw -4,x,0,y
 02ED 18021E42          movw -2,x,2,y
 02F1           ;    }
 02F1           L39:
 02F1           ;    
 02F1           ;    TFLG2 = 0x80;
 02F1 C680              ldab #128
 02F3 7B004F            stab 0x4f
 02F6           ; }
 02F6           L33:
 02F6 B757              tfr x,s
 02F8 30                pulx
 02F9                   .dbline 0 ; func end
 02F9 0B                rti
 02FA           ;  lreg1 -> -4,x
 02FA           ;  lreg2 -> -8,x
 02FA           ;       end_time -> -12,x
 02FA           ;     pause_time -> 2,x
 02FA           _pause::
 02FA 3B                pshd
 02FB 34                pshx
 02FC B775              tfr s,x
 02FE 1B94              leas -12,sp
 0300           ; 
 0300           ; //pause for pause_time*0.1 sec
 0300           ; void pause(int pause_time)
 0300           ; {
 0300           ;    long int end_time;
 0300           ;    end_time = t_tenths + pause_time;
 0300 CD0028            ldy #_t_tenths
 0303 1802401C          movw 0,y,-4,x
 0307 1802421E          movw 2,y,-2,x
 030B EC02              ldd 2,x
 030D 160000            jsr __d2lreg2
 0310 160000            jsr __ladd
 0313 1914              leay -12,x
 0315 18021C40          movw -4,x,0,y
 0319 18021E42          movw -2,x,2,y
 031D           L43:
 031D           L44:
 031D           ; //   printf ("%ld = %ld + %d\n\r",end_time,t_tenths,pause_time);
 031D           ;    while(end_time>t_tenths)
 031D 1914              leay -12,x
 031F 1802401C          movw 0,y,-4,x
 0323 1802421E          movw 2,y,-2,x
 0327 CD0028            ldy #_t_tenths
 032A 18024018          movw 0,y,-8,x
 032E 1802421A          movw 2,y,-6,x
 0332 160000            jsr __lcmp
 0335 2EE6              bgt L43
 0337           ;    {
 0337           ;    }
 0337           ; }
 0337           L42:
 0337 B757              tfr x,s
 0339 30                pulx
 033A 1B82              leas 2,sp
 033C                   .dbline 0 ; func end
 033C 3D                rts
 033D           ;  lreg1 -> -4,x
 033D           ;  lreg2 -> -8,x
 033D           ;       end_time -> -12,x
 033D           ;     pause_time -> 2,x
 033D           _s_pause::
 033D 3B                pshd
 033E 34                pshx
 033F B775              tfr s,x
 0341 1B94              leas -12,sp
 0343           ; 
 0343           ; //pause for pause_time*0.01 sec
 0343           ; void s_pause(int pause_time)
 0343           ; {
 0343           ;    long int end_time;
 0343           ;    end_time = t_hundredths + pause_time;
 0343 CD0024            ldy #_t_hundredths
 0346 1802401C          movw 0,y,-4,x
 034A 1802421E          movw 2,y,-2,x
 034E EC02              ldd 2,x
 0350 160000            jsr __d2lreg2
 0353 160000            jsr __ladd
 0356 1914              leay -12,x
 0358 18021C40          movw -4,x,0,y
 035C 18021E42          movw -2,x,2,y
 0360           L47:
 0360           L48:
 0360           ; //   printf ("%ld = %ld + %d\n\r",end_time,t_tenths,pause_time);
 0360           ;    while(end_time>t_hundredths)
 0360 1914              leay -12,x
 0362 1802401C          movw 0,y,-4,x
 0366 1802421E          movw 2,y,-2,x
 036A CD0024            ldy #_t_hundredths
 036D 18024018          movw 0,y,-8,x
 0371 1802421A          movw 2,y,-6,x
 0375 160000            jsr __lcmp
 0378 2EE6              bgt L47
 037A           ;    {
 037A           ;    }
 037A           ; }
 037A           L46:
 037A B757              tfr x,s
 037C 30                pulx
 037D 1B82              leas 2,sp
 037F                   .dbline 0 ; func end
 037F 3D                rts
 0380           _init_atd0::
 0380           ; 
 0380           ; // ******************* Initialize ATD *******************
 0380           ; void init_atd0()
 0380           ; {
 0380           ;    SEI;
 0380 1410                      SEI
 0382           
 0382           ;    ATD0CTL2 = 0xC2;
 0382 C6C2              ldab #194
 0384 7B0082            stab 0x82
 0387           ;    ATD0CTL3 = 0x40;
 0387 C640              ldab #64
 0389 7B0083            stab 0x83
 038C           ;    ATD0CTL4 = 0xE7;
 038C C6E7              ldab #231
 038E 7B0084            stab 0x84
 0391           ;    CLI;
 0391 10EF                      CLI
 0393           
 0393           ; }
 0393           L50:
 0393                   .dbline 0 ; func end
 0393 3D                rts
 0394           _ATD0_Interrupt::
 0394           ; 
 0394           ; /********************************************************************/
 0394           ; /* This code is primarily taken from example 7.3.5 in the text      */
 0394           ; /* and then converted to act as an ISR                              */
 0394           ; /********************************************************************/
 0394           ;   	  
 0394           ; void ATD0_Interrupt(void)
 0394           ; {
 0394           ; /*print results to PC screen for testing             */
 0394           ;    atd0[0]= ATD0DR0H;	
 0394           	; vol
 0394 F60090            ldab 0x90
 0397 87                clra
 0398 7C0014            std _atd0
 039B           ;    atd0[1]= ATD0DR1H;
 039B                   ; vol
 039B F60092            ldab 0x92
 039E 87                clra
 039F 7C0016            std _atd0+2
 03A2           ;    atd0[2]= ATD0DR2H;
 03A2                   ; vol
 03A2 F60094            ldab 0x94
 03A5 87                clra
 03A6 7C0018            std _atd0+4
 03A9           ;    atd0[3]= ATD0DR3H;
 03A9                   ; vol
 03A9 F60096            ldab 0x96
 03AC 87                clra
 03AD 7C001A            std _atd0+6
 03B0           ;    atd0[4]= ATD0DR4H;
 03B0                   ; vol
 03B0 F60098            ldab 0x98
 03B3 87                clra
 03B4 7C001C            std _atd0+8
 03B7           ;    atd0[5]= ATD0DR5H;
 03B7                   ; vol
 03B7 F6009A            ldab 0x9a
 03BA 87                clra
 03BB 7C001E            std _atd0+10
 03BE           ;    atd0[6]= ATD0DR6H;
 03BE                   ; vol
 03BE F6009C            ldab 0x9c
 03C1 87                clra
 03C2 7C0020            std _atd0+12
 03C5           ;    atd0[7]= ATD0DR7H;         
 03C5                   ; vol
 03C5 F6009E            ldab 0x9e
 03C8 87                clra
 03C9 7C0022            std _atd0+14
 03CC           ; // printf("%x %x %x %x\n\r", ATD0DR0L, ATD0DR1L, ATD0DR2L, ATD0DR3L); 
 03CC           ; }
 03CC           L51:
 03CC                   .dbline 0 ; func end
 03CC 0B                rti
 03CD           _init_atd1::
 03CD           ; 
 03CD           ; // ******************* Initialize ATD *******************
 03CD           ; void init_atd1()
 03CD           ; {
 03CD           ;    SEI;
 03CD 1410                      SEI
 03CF           
 03CF           ;    ATD1CTL2 = 0xC2;
 03CF C6C2              ldab #194
 03D1 7B0122            stab 0x122
 03D4           ;    ATD1CTL3 = 0x40;
 03D4 C640              ldab #64
 03D6 7B0123            stab 0x123
 03D9           ;    ATD1CTL4 = 0xE7;
 03D9 C6E7              ldab #231
 03DB 7B0124            stab 0x124
 03DE           ;    CLI;
 03DE 10EF                      CLI
 03E0           
 03E0           ; }
 03E0           L59:
 03E0                   .dbline 0 ; func end
 03E0 3D                rts
 03E1           _ATD1_Interrupt::
 03E1           ; 
 03E1           ; /********************************************************************/
 03E1           ; /* This code is primarily taken from example 7.3.5 in the text      */
 03E1           ; /* and then converted to act as an ISR                              */
 03E1           ; /********************************************************************/
 03E1           ;   	  
 03E1           ; void ATD1_Interrupt(void)
 03E1           ; {
 03E1           ; /*print results to PC screen for testing             */
 03E1           ;    atd1[0]= ATD1DR0H;	
 03E1           	; vol
 03E1 F60130            ldab 0x130
 03E4 87                clra
 03E5 7C0004            std _atd1
 03E8           ;    atd1[1]= ATD1DR1H;
 03E8                   ; vol
 03E8 F60132            ldab 0x132
 03EB 87                clra
 03EC 7C0006            std _atd1+2
 03EF           ;    atd1[2]= ATD1DR2H;
 03EF                   ; vol
 03EF F60134            ldab 0x134
 03F2 87                clra
 03F3 7C0008            std _atd1+4
 03F6           ;    atd1[3]= ATD1DR3H;
 03F6                   ; vol
 03F6 F60136            ldab 0x136
 03F9 87                clra
 03FA 7C000A            std _atd1+6
 03FD           ;    atd1[4]= ATD1DR4H;
 03FD                   ; vol
 03FD F60138            ldab 0x138
 0400 87                clra
 0401 7C000C            std _atd1+8
 0404           ;    atd1[5]= ATD1DR5H;
 0404                   ; vol
 0404 F6013A            ldab 0x13a
 0407 87                clra
 0408 7C000E            std _atd1+10
 040B           ;    atd1[6]= ATD1DR6H;
 040B                   ; vol
 040B F6013C            ldab 0x13c
 040E 87                clra
 040F 7C0010            std _atd1+12
 0412           ;    atd1[7]= ATD1DR7H;         
 0412                   ; vol
 0412 F6013E            ldab 0x13e
 0415 87                clra
 0416 7C0012            std _atd1+14
 0419           ; // printf("%x %x %x %x\n\r", ATD0DR0L, ATD0DR1L, ATD0DR2L, ATD0DR3L); 
 0419           ; //Checking Line Track
 0419           ;   line = 0;
 0419 CC0000            ldd #0
 041C 7C0000            std _line
 041F           ;   
 041F           ;   if (atd1[0] < line_threshhold)
 041F FC0004            ldd _atd1
 0422 BC0000            cpd _line_threshhold
 0425 2C09              bge L68
 0427           ;   {
 0427           ;     line += 0x1;
 0427 FC0000            ldd _line
 042A C30001            addd #1
 042D 7C0000            std _line
 0430           ;   }
 0430           L68:
 0430           ;   if (atd1[1] < line_threshhold)
 0430 FC0006            ldd _atd1+2
 0433 BC0000            cpd _line_threshhold
 0436 2C09              bge L70
 0438           ;   {
 0438           ;     line += 0x2;
 0438 FC0000            ldd _line
 043B C30002            addd #2
 043E 7C0000            std _line
 0441           ;    }
 0441           L70:
 0441           ;   if (atd1[2] < line_threshhold)
 0441 FC0008            ldd _atd1+4
 0444 BC0000            cpd _line_threshhold
 0447 2C09              bge L73
 0449           ;    {
 0449           ;     line += 0x4;
 0449 FC0000            ldd _line
 044C C30004            addd #4
 044F 7C0000            std _line
 0452           ;    }
 0452           L73:
 0452           ;    	
 0452           ; }
 0452           L60:
 0452                   .dbline 0 ; func end
 0452 0B                rti
 0453           L77:
 0453 BAB9              .word 47801
 0455 A65D              .word 42589
 0457 9430              .word 37936
 0459 8BE9              .word 35817
 045B 7C90              .word 31888
 045D 6EF9              .word 28409
 045F 62D7              .word 25303
 0461 5D5C              .word 23900
 0463           L78:
 0463 0005              .word 5
 0465 0005              .word 5
 0467 0005              .word 5
 0469 0005              .word 5
 046B 0005              .word 5
 046D 0005              .word 5
 046F 0005              .word 5
 0471 0005              .word 5
 0473           ;              i -> -36,x
 0473           ;            dur -> -34,x
 0473           ;          score -> -18,x
 0473           ;              j -> -2,x
 0473           _Speaker::
 0473 34                pshx
 0474 B775              tfr s,x
 0476 1BF1DA            leas -38,sp
 0479           ; 
 0479           ; //*****************************************************************************
 0479           ; #define  C5  47801
 0479           ; #define  C5S 45126
 0479           ; #define  D5    42589
 0479           ; #define        D5S 40193
 0479           ; #define        E5  37936
 0479           ; #define  F5    35817
 0479           ; #define  F5S 33784
 0479           ; #define        G5      31888
 0479           ; #define  G5S 30084
 0479           ; #define  A5    28409
 0479           ; #define  A5S 26824
 0479           ; #define  B5    25303
 0479           ; #define  C6    23900
 0479           ; #define  C6S 22542
 0479           ; #define  D6  21276
 0479           ; #define  D6S 20096
 0479           ; #define  E6  18825
 0479           ; #define  F6  17895
 0479           ; #define  F6S 16892
 0479           ; #define  G6    15944
 0479           ; #define  G6S 15051
 0479           ; #define  A6  14204
 0479           ; #define  A6S 13404
 0479           ; #define  B6  12658
 0479           ; 
 0479           ; 
 0479           ; #define  notes  8
 0479           ; #define  toggle 0x04
 0479           ; 
 0479           ; void Speaker (void)
 0479           ; {
 0479           ;    int j;
 0479           ;    int i;
 0479           ;    unsigned int score[notes] = {C5,D5,E5,F5,G5,A5,B5,C6};
 0479 19E1EE            leay -18,x
 047C B7C6              xgdy
 047E CD0453            ldy #L77
 0481 34                pshx
 0482 B745              tfr d,x
 0484 CC0008            ldd #8
 0487           X3:
 0487 18027131          movw 2,y+,2,x+
 048B 0434F9            dbne d,X3
 048E 30                pulx
 048F           ;    unsigned int dur[notes] = {5,5,5,5,5,5,5,5};
 048F 19E1DE            leay -34,x
 0492 B7C6              xgdy
 0494 CD0463            ldy #L78
 0497 34                pshx
 0498 B745              tfr d,x
 049A CC0008            ldd #8
 049D           X4:
 049D 18027131          movw 2,y+,2,x+
 04A1 0434F9            dbne d,X4
 04A4 30                pulx
 04A5           ; 
 04A5           ; //   TSCR1 = 0x90;   //Enable TCNT, fast timer flag clear
 04A5           ; //   TSCR2 = 0x83;   //Set TCNT prescaler to 8
 04A5           ;    TFLG1 = 0x20;   //Clear all TxF flags
 04A5 C620              ldab #32
 04A7 7B004E            stab 0x4e
 04AA           ;    TIE  |= 0x20;   //Enable TC5 interrupt
 04AA 1C004C20          bset 0x4c,#32
 04AE           ;    TIOS |= 0x20;   //Enable OC5 function
 04AE 1C004020          bset 0x40,#32
 04B2           ;    TCTL1 = toggle; //Select toggle option as OC5 pin action
 04B2 C604              ldab #4
 04B4 7B0048            stab 0x48
 04B7           ;    j = 0;
 04B7 CC0000            ldd #0
 04BA 6C1E              std -2,x
 04BC           ;    dela = score[0];
 04BC ECE1EE            ldd -18,x
 04BF 7C0002            std _dela
 04C2           ;    TC5 = TCNT + dela;      //Play the first note
 04C2                   ; vol
 04C2 FC0044            ldd 0x44
 04C5 F30002            addd _dela
 04C8 7C005A            std 0x5a
 04CB 202D              bra L80
 04CD           L79:
 04CD 19E1EE            leay -18,x
 04D0 6DE1DA            sty -38,x
 04D3 EC1E              ldd -2,x
 04D5 59                lsld
 04D6 E3E1DA            addd -38,x
 04D9 B7C6              xgdy
 04DB EC40              ldd 0,y
 04DD 7C0002            std _dela
 04E0 19E1DE            leay -34,x
 04E3 6DE1DA            sty -38,x
 04E6 EC1E              ldd -2,x
 04E8 59                lsld
 04E9 E3E1DA            addd -38,x
 04EC B7C6              xgdy
 04EE EC40              ldd 0,y
 04F0 16033D            jsr _s_pause
 04F3 EC1E              ldd -2,x
 04F5 C30001            addd #1
 04F8 6C1E              std -2,x
 04FA           L80:
 04FA           ;    while(j < notes)
 04FA EC1E              ldd -2,x
 04FC 8C0008            cpd #8
 04FF 2DCC              blt L79
 0501           ;    {
 0501           ;       dela = score[j];     //Play the jth note
 0501           ;         s_pause(dur[j]);
 0501           ;         j++;
 0501           ;    }
 0501           ; 
 0501           ;    TIE  &= ~0x20;   //Disable TC5 interrupt
 0501 1D004C20          bclr 0x4c,#0x20
 0505           ; //   TSCR1 = 0x00;   //Turn off the Timer System
 0505           ;    PTT = 0x00;     //Blank Port T
 0505 790240            clr 0x240
 0508           ;    TCTL1 = 0;
 0508 790048            clr 0x48
 050B           ; }
 050B           L76:
 050B B757              tfr x,s
 050D 30                pulx
 050E                   .dbline 0 ; func end
 050E 3D                rts
 050F           _OC5_ISR::
 050F           ; 
 050F           ; //******************************** OC5_ISR *************************************
 050F           ; 
 050F           ; #pragma interrupt_handler OC5_ISR()
 050F           ; void OC5_ISR ()
 050F           ; {
 050F           ;    TC5 += dela;
 050F           	; vol
 050F FC005A            ldd 0x5a
 0512 F30002            addd _dela
 0515 7C005A            std 0x5a
 0518           ;    TFLG1 = 0x20;
 0518 C620              ldab #32
 051A 7B004E            stab 0x4e
 051D           ; }
 051D           L82:
 051D                   .dbline 0 ; func end
 051D 0B                rti
                        .area bss
 0000           _line::
 0000                   .blkb 2
 0002           _dela::
 0002                   .blkb 2
 0004           _atd1::
 0004                   .blkb 16
 0014           _atd0::
 0014                   .blkb 16
 0024           _t_hundredths::
 0024                   .blkb 4
 0028           _t_tenths::
 0028                   .blkb 4
 002C           _t_counts::
 002C                   .blkb 4
                        .area text
 051E           L41:
 051E 00000004          .word 0,4
 0522           L38:
 0522 00000000          .word 0,0
 0526           L37:
 0526 00000026          .word 0,38
 052A           L34:
 052A 00000001          .word 0,1
 052E           L23:
 052E 6C696E65203D202564200A0D00        .byte 'l,'i,'n,'e,32,61,32,37,'d,32,10,13,0
 053B           L22:
 053B 72696768740A0D00  .byte 'r,'i,'g,'h,'t,10,13,0
 0543           L20:
 0543 666F72776172640A0D00      .byte 'f,'o,'r,'w,'a,'r,'d,10,13,0
 054D           L18:
 054D 6C6566740A0D00    .byte 'l,'e,'f,'t,10,13,0
 0554           L14:
 0554 256C6420256C640A0D00      .byte 37,'l,'d,32,37,'l,'d,10,13,0
 055E           L13:
 055E 617464305B25755D203D202575202020  .byte 'a,'t,'d,48,91,37,'u,93,32,61,32,37,'u,32,32,32
 056E 617464315B25755D203D2025750A0D00  .byte 'a,'t,'d,49,91,37,'u,93,32,61,32,37,'u,10,13,0
