                        .module cl11_4RTI.c
                        .area data
 0000           _T_counts::
 0000                   .blkb 2
                        .area idata
 0000 4E20              .word 20000
                        .area data
 0002           _per_on2::
 0002                   .blkb 2
                        .area idata
 0002 000A              .word 10
                        .area data
 0004           _per_on3::
 0004                   .blkb 2
                        .area idata
 0004 000A              .word 10
                        .area data
 0006           _helper::
 0006                   .blkb 2
                        .area idata
 0006 0000              .word 0
                        .area data
 0008           _cntflag::
 0008                   .blkb 2
                        .area idata
 0008 0000              .word 0
                        .area data
                        .area text
                ;  IX -> 0,x
                ;        cnttime -> +2,x
 0000           _main::
 0000 BD0000            jsr __enterb
 0003 04                .byte 0x4
 0004           ; /* C Listing 11.4 */
 0004           ; 
 0004           ; #include <hc11.h>     /* Include register definitions */
 0004           ; #include <stdio.h>
 0004           ; #define motcont *(unsigned char volatile *)(0xB590)
 0004           ; 
 0004           ; 
 0004           ; /* Declarations */
 0004           ; unsigned int OC2hi, OC2lo, OC3hi, OC3lo;      /* Duty cycle data */
 0004           ; unsigned int T_counts=0x4e20, per_on2=10, per_on3=10, helper=0,cntflag=0;
 0004           ; 
 0004           ; 
 0004           ; /* Prototypes */
 0004           ; void initoc(void);
 0004           ; void rtoc2(void);
 0004           ; void rtoc3(void);
 0004           ; void cnts2(void);
 0004           ; void cnts3(void);
 0004           ; void RTIINTR (void);
 0004           ; 
 0004           ; 
 0004           ; void main(void)
 0004           ; {
 0004           ;       int cnttime;
 0004           ;       setbaud(BAUD9600);
 0004 CC0030            ldd #48
 0007 BD0000            jsr _setbaud
 000A           ;       INTR_OFF();
 000A 0F                                sei
 000B           
 000B           ;       /* set up jump vector for buffalo */
 000B           ;       asm("   ldaa #$7e");
 000B 867E                              ldaa #$7e
 000D           
 000D           ;       asm("   staa $dc");
 000D 97DC                              staa $dc
 000F           
 000F           ;       asm("   staa $eb");
 000F 97EB                              staa $eb
 0011           
 0011           ;       asm("   staa $d9");
 0011 97D9                              staa $d9
 0013           
 0013           ;       asm("   ldd #_rtoc2");
 0013 CC00D4                            ldd #_rtoc2
 0016           
 0016           ;       asm("   std $dd");
 0016 DDDD                              std $dd
 0018           
 0018           ;       asm("   ldd #_rtoc3");
 0018 CC010A                            ldd #_rtoc3
 001B           
 001B           ;       asm("   std $da");
 001B DDDA                              std $da
 001D           
 001D           ;       asm("   ldd #_RTIINTR");
 001D CC02FA                            ldd #_RTIINTR
 0020           
 0020           ;       asm("   std $ec");
 0020 DDEC                              std $ec
 0022           
 0022           ;       OC2hi = 0x3e8;
 0022 CC03E8            ldd #1000
 0025 FD0006            std _OC2hi
 0028           ;       OC2lo = 0x3e8;
 0028 CC03E8            ldd #1000
 002B FD0004            std _OC2lo
 002E           ;       OC3hi = 0x3e8;
 002E CC03E8            ldd #1000
 0031 FD0002            std _OC3hi
 0034           ;       OC3lo = 0x3e8;
 0034 CC03E8            ldd #1000
 0037 FD0000            std _OC3lo
 003A           ;       TMSK2=0x40;
 003A C640              ldab #64
 003C F71024            stab 0x1024
 003F           ;       PACTL=PACTL|0x03;
 003F 18CE1026          ldy #0x1026
 0043 181C0003          bset 0,y,#3
 0047           ;       
 0047           ;       initoc();
 0047 BD00B9            jsr _initoc
 004A           ;       
 004A           ;       cnts2();
 004A BD0140            jsr _cnts2
 004D           ;       cnts3();
 004D BD021D            jsr _cnts3
 0050 205E              bra L5
 0052           L4:
 0052           ; 	
 0052           ; 	while(1)
 0052           ; 	{	 
 0052           ; 		 
 0052           ; 	 	 if (helper == 1){
 0052 FC0006            ldd _helper
 0055 1A830001          cpd #1
 0059 2617              bne L7
 005B           ;                       per_on2=per_on3=75;
 005B CC004B            ldd #75
 005E FD0004            std _per_on3
 0061 CC004B            ldd #75
 0064 FD0002            std _per_on2
 0067           ;                       cnts2();
 0067 BD0140            jsr _cnts2
 006A           ;                       cnts3();
 006A BD021D            jsr _cnts3
 006D           ;                       
 006D           ;                       motcont = 0x00;
 006D 7FB590            clr 0xb590
 0070           ;                }       
 0070 203E              bra L8
 0072           L7:
 0072           ; 		 else if (helper == 5){
 0072 FC0006            ldd _helper
 0075 1A830005          cpd #5
 0079 2619              bne L9
 007B           ;                       per_on2=per_on3=40;
 007B CC0028            ldd #40
 007E FD0004            std _per_on3
 0081 CC0028            ldd #40
 0084 FD0002            std _per_on2
 0087           ;                       cnts2();
 0087 BD0140            jsr _cnts2
 008A           ;                       cnts3();
 008A BD021D            jsr _cnts3
 008D           ;                       //printf("%x   %x   %x    %x\n",OC2hi,OC2lo,OC3hi,OC3lo); 
 008D           ;                       motcont = 0x03;
 008D C603              ldab #3
 008F F7B590            stab 0xb590
 0092           ;                }       
 0092 201C              bra L10
 0094           L9:
 0094           ; 		else if  (helper == 10){ 
 0094 FC0006            ldd _helper
 0097 1A83000A          cpd #10
 009B 2613              bne L11
 009D           ;                        TMSK1=0x00;
 009D 7F1022            clr 0x1022
 00A0           ;                PORTA=0x00;
 00A0 7F1000            clr 0x1000
 00A3           ;                        printf("%d\n",helper);
 00A3 FC0006            ldd _helper
 00A6 37                pshb
 00A7 36                psha
 00A8 CC033D            ldd #L13
 00AB BD0000            jsr _printf
 00AE 1838              puly
 00B0           ;                        }       
 00B0           L11:
 00B0           L10:
 00B0           L8:
 00B0           L5:
 00B0 20A0              bra L4
 00B2           X0:
 00B2           ;     }
 00B2           ; 
 00B2           ; 	
 00B2           ; }
 00B2           L3:
 00B2 08                inx
 00B3 08                inx
 00B4 08                inx
 00B5 08                inx
 00B6 35                txs
 00B7 38                pulx
 00B8                   .dbline 0 ; func end
 00B8 39                rts
 00B9           _initoc::
 00B9           ; 
 00B9           ; void initoc(void)
 00B9           ; /* Initialize OC2 */
 00B9           ; {
 00B9           ; 	TOC2 = TCNT;
 00B9           	; vol
 00B9 FC100E            ldd 0x100e
 00BC FD1018            std 0x1018
 00BF           ;       TOC3 = TCNT;
 00BF                   ; vol
 00BF FC100E            ldd 0x100e
 00C2 FD101A            std 0x101a
 00C5           ;       TCTL1 = 0x00;
 00C5 7F1020            clr 0x1020
 00C8           ;       TFLG1 = 0x60;
 00C8 C660              ldab #96
 00CA F71023            stab 0x1023
 00CD           ;       TMSK1 = 0x60;
 00CD C660              ldab #96
 00CF F71022            stab 0x1022
 00D2           ;       INTR_ON();
 00D2 0E                                cli
 00D3           
 00D3           ; }
 00D3           L14:
 00D3                   .dbline 0 ; func end
 00D3 39                rts
 00D4           _rtoc2::
 00D4           ; 
 00D4           ; void rtoc2(void)
 00D4           ; /* Interrupt Service Routine for OC2 PWM */
 00D4           ; {  
 00D4           ; 	if ((PORTA & 0X40) == 0){ 
 00D4 18CE1000          ldy #0x1000
 00D8 181F004002        brclr 0,y,#64,X1
 00DD 2013              bra L16
 00DF           X1:
 00DF           ; 		TOC2 = TOC2 + OC2lo;
 00DF           	; vol
 00DF FC1018            ldd 0x1018
 00E2 F30004            addd _OC2lo
 00E5 FD1018            std 0x1018
 00E8           ;               PORTA = PORTA | 0x40;
 00E8 18CE1000          ldy #0x1000
 00EC 181C0040          bset 0,y,#64
 00F0           ;               }
 00F0 2009              bra L17
 00F2           L16:
 00F2           ; 	else
 00F2           ; 		TOC2 = TOC2 + OC2hi;
 00F2           	; vol
 00F2 FC1018            ldd 0x1018
 00F5 F30006            addd _OC2hi
 00F8 FD1018            std 0x1018
 00FB           L17:
 00FB           ; 		PORTA = PORTA |  0xBF;
 00FB 18CE1000          ldy #0x1000
 00FF 181C00BF          bset 0,y,#191
 0103           ;       //TCTL1 = TCTL1 ^ 0x40; /* invert OL2 to toggle next OC2 edge */
 0103           ;       TFLG1 = 0x40;   /* Clear flag OC2F */
 0103 C640              ldab #64
 0105 F71023            stab 0x1023
 0108           ;       asm("   rti");  /* This is an interrupt service routine */
 0108 3B                                rti
 0109           
 0109           ; }
 0109           L15:
 0109                   .dbline 0 ; func end
 0109 39                rts
 010A           _rtoc3::
 010A           ; 
 010A           ; void rtoc3(void)
 010A           ; {	
 010A           ; 	if ((PORTA & 0X20) == 0) {
 010A 18CE1000          ldy #0x1000
 010E 181F002002        brclr 0,y,#32,X2
 0113 2013              bra L19
 0115           X2:
 0115           ; 		TOC3 = TOC3 + OC3lo;
 0115           	; vol
 0115 FC101A            ldd 0x101a
 0118 F30000            addd _OC3lo
 011B FD101A            std 0x101a
 011E           ;               PORTA = PORTA | 0x20;
 011E 18CE1000          ldy #0x1000
 0122 181C0020          bset 0,y,#32
 0126           ;       }       
 0126 2009              bra L20
 0128           L19:
 0128           ; 	else
 0128           ; 		TOC3 = TOC3 + OC3hi;
 0128           	; vol
 0128 FC101A            ldd 0x101a
 012B F30002            addd _OC3hi
 012E FD101A            std 0x101a
 0131           L20:
 0131           ; 		PORTA = PORTA | 0xDF;
 0131 18CE1000          ldy #0x1000
 0135 181C00DF          bset 0,y,#223
 0139           ;       //TCTL1 = TCTL1 ^ 0x10; /* invert OL2 to toggle next OC2 edge */
 0139           ;       TFLG1 = 0x20;   /* Clear flag OC2F */
 0139 C620              ldab #32
 013B F71023            stab 0x1023
 013E           ;       asm("   rti");  /* This is an interrupt service routine */
 013E 3B                                rti
 013F           
 013F           ; }
 013F           L18:
 013F                   .dbline 0 ; func end
 013F 39                rts
 0140           ;  lreg1 -> -4,x
 0140           ;  lreg2 -> -8,x
 0140           ;  IX -> 0,x
 0140           ;          ?temp -> +2,x
 0140           ;          ?temp -> +6,x
 0140           _cnts2::
 0140 BD0000            jsr __enterb
 0143 88                .byte 0x88
 0144           ; void cnts2 (void)
 0144           ; {
 0144           ;       OC2hi=T_counts * (float)per_on2/100;
 0144 FC0002            ldd _per_on2
 0147 04                lsrd
 0148 BD0000            jsr __d2lreg
 014B BD0000            jsr __long2fp
 014E BD0000            jsr __fpmov
 0151 18CE0339          ldy #L25
 0155 BD0000            jsr __tofp1
 0158 BD0000            jsr __fpmul
 015B FC0002            ldd _per_on2
 015E 8400              anda #0
 0160 C401              andb #1
 0162 BD0000            jsr __d2lreg
 0165 BD0000            jsr __pshfp1 ; spill
 0168 BD0000            jsr __long2fp
 016B BD0000            jsr __fpmov
 016E BD0000            jsr __pulfp1 ; reload
 0171 BD0000            jsr __fpadd
 0174 BD0000            jsr __fpmov
 0177 FC0000            ldd _T_counts
 017A 04                lsrd
 017B BD0000            jsr __d2lreg
 017E BD0000            jsr __long2fp
 0181 BD0000            jsr __pshfp2 ; spill
 0184 BD0000            jsr __fpmov
 0187 18CE0339          ldy #L25
 018B BD0000            jsr __tofp1
 018E BD0000            jsr __fpmul
 0191 FC0000            ldd _T_counts
 0194 8400              anda #0
 0196 C401              andb #1
 0198 BD0000            jsr __d2lreg
 019B BD0000            jsr __pshfp1 ; spill
 019E BD0000            jsr __long2fp
 01A1 BD0000            jsr __fpmov
 01A4 BD0000            jsr __pulfp1 ; reload
 01A7 BD0000            jsr __fpadd
 01AA BD0000            jsr __pulfp2 ; reload
 01AD BD0000            jsr __fpmul
 01B0 18CE0335          ldy #L26
 01B4 BD0000            jsr __tofp2
 01B7 BD0000            jsr __fpdiv
 01BA EC00              ldd 0,x
 01BC C30002            addd #2
 01BF 188F              xgdy
 01C1 BD0000            jsr __fromfp1
 01C4 EC00              ldd 0,x
 01C6 C30002            addd #2
 01C9 188F              xgdy
 01CB BD0000            jsr __tofp1
 01CE 18CE0331          ldy #L27
 01D2 BD0000            jsr __tofp2
 01D5 BD0000            jsr __fpcmp
 01D8 2D1E              blt L23
 01DA EC00              ldd 0,x
 01DC C30002            addd #2
 01DF 188F              xgdy
 01E1 BD0000            jsr __tofp1
 01E4 18CE0331          ldy #L27
 01E8 BD0000            jsr __tofp2
 01EB BD0000            jsr __fpsub
 01EE BD0000            jsr __fp2int
 01F1 C38000            addd #0x8000
 01F4 ED06              std 6,x
 01F6 200F              bra L24
 01F8           L23:
 01F8 EC00              ldd 0,x
 01FA C30002            addd #2
 01FD 188F              xgdy
 01FF BD0000            jsr __tofp1
 0202 BD0000            jsr __fp2int
 0205 ED06              std 6,x
 0207           L24:
 0207 EC06              ldd 6,x
 0209 FD0006            std _OC2hi
 020C           ;       OC2lo=T_counts - OC2hi;
 020C FC0000            ldd _T_counts
 020F B30006            subd _OC2hi
 0212 FD0004            std _OC2lo
 0215           ; }
 0215           L21:
 0215 8F                xgdx
 0216 C30008            addd #8
 0219 8F                xgdx
 021A 35                txs
 021B 38                pulx
 021C                   .dbline 0 ; func end
 021C 39                rts
 021D           ;  lreg1 -> -4,x
 021D           ;  lreg2 -> -8,x
 021D           ;  IX -> 0,x
 021D           ;          ?temp -> +2,x
 021D           ;          ?temp -> +6,x
 021D           _cnts3::
 021D BD0000            jsr __enterb
 0220 88                .byte 0x88
 0221           ; 
 0221           ; void cnts3 (void)
 0221           ; {
 0221           ;       OC3hi=T_counts * (float)per_on3/100;
 0221 FC0004            ldd _per_on3
 0224 04                lsrd
 0225 BD0000            jsr __d2lreg
 0228 BD0000            jsr __long2fp
 022B BD0000            jsr __fpmov
 022E 18CE0339          ldy #L25
 0232 BD0000            jsr __tofp1
 0235 BD0000            jsr __fpmul
 0238 FC0004            ldd _per_on3
 023B 8400              anda #0
 023D C401              andb #1
 023F BD0000            jsr __d2lreg
 0242 BD0000            jsr __pshfp1 ; spill
 0245 BD0000            jsr __long2fp
 0248 BD0000            jsr __fpmov
 024B BD0000            jsr __pulfp1 ; reload
 024E BD0000            jsr __fpadd
 0251 BD0000            jsr __fpmov
 0254 FC0000            ldd _T_counts
 0257 04                lsrd
 0258 BD0000            jsr __d2lreg
 025B BD0000            jsr __long2fp
 025E BD0000            jsr __pshfp2 ; spill
 0261 BD0000            jsr __fpmov
 0264 18CE0339          ldy #L25
 0268 BD0000            jsr __tofp1
 026B BD0000            jsr __fpmul
 026E FC0000            ldd _T_counts
 0271 8400              anda #0
 0273 C401              andb #1
 0275 BD0000            jsr __d2lreg
 0278 BD0000            jsr __pshfp1 ; spill
 027B BD0000            jsr __long2fp
 027E BD0000            jsr __fpmov
 0281 BD0000            jsr __pulfp1 ; reload
 0284 BD0000            jsr __fpadd
 0287 BD0000            jsr __pulfp2 ; reload
 028A BD0000            jsr __fpmul
 028D 18CE0335          ldy #L26
 0291 BD0000            jsr __tofp2
 0294 BD0000            jsr __fpdiv
 0297 EC00              ldd 0,x
 0299 C30002            addd #2
 029C 188F              xgdy
 029E BD0000            jsr __fromfp1
 02A1 EC00              ldd 0,x
 02A3 C30002            addd #2
 02A6 188F              xgdy
 02A8 BD0000            jsr __tofp1
 02AB 18CE0331          ldy #L27
 02AF BD0000            jsr __tofp2
 02B2 BD0000            jsr __fpcmp
 02B5 2D1E              blt L30
 02B7 EC00              ldd 0,x
 02B9 C30002            addd #2
 02BC 188F              xgdy
 02BE BD0000            jsr __tofp1
 02C1 18CE0331          ldy #L27
 02C5 BD0000            jsr __tofp2
 02C8 BD0000            jsr __fpsub
 02CB BD0000            jsr __fp2int
 02CE C38000            addd #0x8000
 02D1 ED06              std 6,x
 02D3 200F              bra L31
 02D5           L30:
 02D5 EC00              ldd 0,x
 02D7 C30002            addd #2
 02DA 188F              xgdy
 02DC BD0000            jsr __tofp1
 02DF BD0000            jsr __fp2int
 02E2 ED06              std 6,x
 02E4           L31:
 02E4 EC06              ldd 6,x
 02E6 FD0002            std _OC3hi
 02E9           ;       OC3lo=T_counts - OC3hi;
 02E9 FC0000            ldd _T_counts
 02EC B30002            subd _OC3hi
 02EF FD0000            std _OC3lo
 02F2           ; }     
 02F2           L28:
 02F2 8F                xgdx
 02F3 C30008            addd #8
 02F6 8F                xgdx
 02F7 35                txs
 02F8 38                pulx
 02F9                   .dbline 0 ; func end
 02F9 39                rts
 02FA           _RTIINTR::
 02FA           ; 
 02FA           ; void RTIINTR (void)
 02FA           ; {	
 02FA           ; 	TFLG2=0x40;
 02FA C640              ldab #64
 02FC F71025            stab 0x1025
 02FF           ;       
 02FF           ;       cntflag++;
 02FF FC0008            ldd _cntflag
 0302 C30001            addd #1
 0305 FD0008            std _cntflag
 0308           ;                  if (cntflag == 31){
 0308 FC0008            ldd _cntflag
 030B 1A83001F          cpd #31
 030F 261E              bne L33
 0311           ;                  cntflag=0;
 0311 CC0000            ldd #0
 0314 FD0008            std _cntflag
 0317           ;                  helper++;
 0317 FC0006            ldd _helper
 031A C30001            addd #1
 031D FD0006            std _helper
 0320           ;                  if (helper == 15)
 0320 FC0006            ldd _helper
 0323 1A83000F          cpd #15
 0327 2606              bne L35
 0329           ;                     helper=0;
 0329 CC0000            ldd #0
 032C FD0006            std _helper
 032F           L35:
 032F           ; 		   }
 032F           L33:
 032F           ; 	asm("   rti");
 032F 3B                           rti
 0330           
 0330           ; } 
 0330           L32:
 0330                   .dbline 0 ; func end
 0330 39                rts
                        .area bss
 0000           _OC3lo::
 0000                   .blkb 2
 0002           _OC3hi::
 0002                   .blkb 2
 0004           _OC2lo::
 0004                   .blkb 2
 0006           _OC2hi::
 0006                   .blkb 2
                        .area text
 0331           L27:
 0331 47000000          .word 0x4700,0x0
 0335           L26:
 0335 42C80000          .word 0x42c8,0x0
 0339           L25:
 0339 40000000          .word 0x4000,0x0
 033D           L13:
 033D 25640A00          .byte 37,'d,10,0
