                         .module cl11_4.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 0055              .word 85
                         .area data
 0004                 _per_on3::
 0004                   .blkb 2
                         .area idata
 0004 0055              .word 85
                         .area data
                         .area text
                       ;  IX -> 0,x
                       ;              x -> +2,x
 0000                 _main::
 0000 BD0000            jsr __enterb
 0003 04                .byte 0x4
                       ; /* C Listing 11.4 */
                       ; 
                       ; #include <hc11.h>   /* Include register definitions */
                       ; #include <stdio.h>
                       ; /* Declarations */
                       ; unsigned int OC2hi, OC2lo, OC3hi, OC3lo;    /* Duty cycle data */
                       ; unsigned int T_counts=0x4e20, per_on2=85, per_on3=85;
                       ; 
                       ; 
                       ; /* Prototypes */
                       ; void initoc(void);
                       ; void rtoc2(void);
                       ; void rtoc3(void);
                       ; void cnts2(void);
                       ; void cnts3(void);
                       ; 
                       ; void main(void)
                       ; {
                       ;     int x=0;
 0004 CC0000            ldd #0
 0007 ED02              std 2,x
                       ;     setbaud(BAUD9600);
 0009 CC0030            ldd #48
 000C BD0000            jsr _setbaud
                       ;     INTR_OFF();
 000F 0F                        sei
                       
                       ;     /* set up jump vector for buffalo */
                       ;     asm("   ldaa #$7e");
 0010 867E                      ldaa #$7e
                       
                       ;     asm("   staa $dc");
 0012 97DC                      staa $dc
                       
                       ;     asm("   ldd #_rtoc2");
 0014 CC009C                    ldd #_rtoc2
                       
                       ;     asm("   std $dd");
 0017 DDDD                      std $dd
                       
                       ;     asm("   ldaa #$7e");
 0019 867E                      ldaa #$7e
                       
                       ;     asm("   staa $d9");
 001B 97D9                      staa $d9
                       
                       ;     asm("   ldd #_rtoc3");
 001D CC00CA                    ldd #_rtoc3
                       
                       ;     asm("   std $da");
 0020 DDDA                      std $da
                       
                       ;     OC2hi = 0x3e8;
 0022 CC03E8            ldd #1000
 0025 FD0006            std _OC2hi
                       ;     OC2lo = 0x3e8;
 0028 CC03E8            ldd #1000
 002B FD0004            std _OC2lo
                       ;     OC3hi = 0x3e8;
 002E CC03E8            ldd #1000
 0031 FD0002            std _OC3hi
                       ;     OC3lo = 0x3e8;
 0034 CC03E8            ldd #1000
 0037 FD0000            std _OC3lo
                       ;     initoc();
 003A BD007F            jsr _initoc
 003D 202B              bra L5
 003F                 L4:
 003F BD00F8            jsr _cnts2
 0042 BD01D5            jsr _cnts3
 0045 FC0000            ldd _OC3lo
 0048 37                pshb
 0049 36                psha
 004A FC0002            ldd _OC3hi
 004D 37                pshb
 004E 36                psha
 004F FC0004            ldd _OC2lo
 0052 37                pshb
 0053 36                psha
 0054 FC0006            ldd _OC2hi
 0057 37                pshb
 0058 36                psha
 0059 CC02BE            ldd #L7
 005C BD0000            jsr _printf
 005F BD0000            jsr __movspb
 0062 08                .byte 8
 0063 EC02              ldd 2,x
 0065 C30005            addd #5
 0068 ED02              std 2,x
 006A                 L5:
                       ;     
                       ;     while(x!=1000)
 006A EC02              ldd 2,x
 006C 1A8303E8          cpd #1000
 0070 26CD              bne L4
                       ;     {   
                       ;         
                       ;          cnts2();
                       ;          cnts3();
                       ;          printf("%x   %x   %x    %x\n",OC2hi,OC2lo,OC3hi,OC3lo); 
                       ;         x=x+5;
                       ;     }       /* Loop forever */
                       ; TMSK1=0x00;
 0072 7F1022            clr 0x1022
                       ; PORTA=0x00;
 0075 7F1000            clr 0x1000
                       ;     
                       ; }
 0078                 L3:
 0078 08                inx
 0079 08                inx
 007A 08                inx
 007B 08                inx
 007C 35                txs
 007D 38                pulx
 007E                   .dbline 0 ; func end
 007E 39                rts
 007F                 _initoc::
                       ; 
                       ; void initoc(void)
                       ; /* Initialize OC2 */
                       ; {
                       ;     TOC2 = TCNT;
                         ; vol
 007F FC100E            ldd 0x100e
 0082 FD1018            std 0x1018
                       ;     TOC3 = TCNT;
                         ; vol
 0085 FC100E            ldd 0x100e
 0088 FD101A            std 0x101a
                       ;     TCTL1 = 0xF0;
 008B C6F0              ldab #240
 008D F71020            stab 0x1020
                       ;     TFLG1 = 0x60;
 0090 C660              ldab #96
 0092 F71023            stab 0x1023
                       ;     TMSK1 = 0x60;
 0095 C660              ldab #96
 0097 F71022            stab 0x1022
                       ;     INTR_ON();
 009A 0E                        cli
                       
                       ; }
 009B                 L8:
 009B                   .dbline 0 ; func end
 009B 39                rts
 009C                 _rtoc2::
                       ; 
                       ; void rtoc2(void)
                       ; /* Interrupt Service Routine for OC2 PWM */
                       ; {  
                       ;     if ((PORTA & 0X40) == 0) 
 009C 18CE1000          ldy #0x1000
 00A0 181F004002        brclr 0,y,#64,X0
 00A5 200B              bra L10
 00A7                 X0:
                       ;         TOC2 = TOC2 + OC2lo;
                         ; vol
 00A7 FC1018            ldd 0x1018
 00AA F30004            addd _OC2lo
 00AD FD1018            std 0x1018
 00B0 2009              bra L11
 00B2                 L10:
                       ;     else
                       ;         TOC2 = TOC2 + OC2hi;
                         ; vol
 00B2 FC1018            ldd 0x1018
 00B5 F30006            addd _OC2hi
 00B8 FD1018            std 0x1018
 00BB                 L11:
                       ;     TCTL1 = TCTL1 ^ 0x40;   /* invert OL2 to toggle next OC2 edge */
                         ; vol
 00BB F61020            ldab 0x1020
 00BE C840              eorb #64
 00C0 F71020            stab 0x1020
                       ;     TFLG1 = 0x40;   /* Clear flag OC2F */
 00C3 C640              ldab #64
 00C5 F71023            stab 0x1023
                       ;     asm("   rti");  /* This is an interrupt service routine */
 00C8 3B                        rti
                       
                       ; }
 00C9                 L9:
 00C9                   .dbline 0 ; func end
 00C9 39                rts
 00CA                 _rtoc3::
                       ; 
                       ; void rtoc3(void)
                       ; {   
                       ;     if ((PORTA & 0X20) == 0) 
 00CA 18CE1000          ldy #0x1000
 00CE 181F002002        brclr 0,y,#32,X1
 00D3 200B              bra L13
 00D5                 X1:
                       ;         TOC3 = TOC3 + OC3lo;
                         ; vol
 00D5 FC101A            ldd 0x101a
 00D8 F30000            addd _OC3lo
 00DB FD101A            std 0x101a
 00DE 2009              bra L14
 00E0                 L13:
                       ;     else
                       ;         TOC3 = TOC3 + OC3hi;
                         ; vol
 00E0 FC101A            ldd 0x101a
 00E3 F30002            addd _OC3hi
 00E6 FD101A            std 0x101a
 00E9                 L14:
                       ;     TCTL1 = TCTL1 ^ 0x10;   /* invert OL2 to toggle next OC2 edge */
                         ; vol
 00E9 F61020            ldab 0x1020
 00EC C810              eorb #16
 00EE F71020            stab 0x1020
                       ;     TFLG1 = 0x20;   /* Clear flag OC2F */
 00F1 C620              ldab #32
 00F3 F71023            stab 0x1023
                       ;     asm("   rti");  /* This is an interrupt service routine */
 00F6 3B                        rti
                       
                       ; }
 00F7                 L12:
 00F7                   .dbline 0 ; func end
 00F7 39                rts
                       ;  lreg1 -> -4,x
                       ;  lreg2 -> -8,x
                       ;  IX -> 0,x
                       ;          ?temp -> +2,x
                       ;          ?temp -> +6,x
 00F8                 _cnts2::
 00F8 BD0000            jsr __enterb
 00FB 88                .byte 0x88
                       ; void cnts2 (void)
                       ; {
                       ;     OC2hi=T_counts * (float)per_on2/100;
 00FC FC0002            ldd _per_on2
 00FF 04                lsrd
 0100 BD0000            jsr __d2lreg
 0103 BD0000            jsr __long2fp
 0106 BD0000            jsr __fpmov
 0109 18CE02BA          ldy #L19
 010D BD0000            jsr __tofp1
 0110 BD0000            jsr __fpmul
 0113 FC0002            ldd _per_on2
 0116 8400              anda #0
 0118 C401              andb #1
 011A BD0000            jsr __d2lreg
 011D BD0000            jsr __pshfp1 ; spill
 0120 BD0000            jsr __long2fp
 0123 BD0000            jsr __fpmov
 0126 BD0000            jsr __pulfp1 ; reload
 0129 BD0000            jsr __fpadd
 012C BD0000            jsr __fpmov
 012F FC0000            ldd _T_counts
 0132 04                lsrd
 0133 BD0000            jsr __d2lreg
 0136 BD0000            jsr __long2fp
 0139 BD0000            jsr __pshfp2 ; spill
 013C BD0000            jsr __fpmov
 013F 18CE02BA          ldy #L19
 0143 BD0000            jsr __tofp1
 0146 BD0000            jsr __fpmul
 0149 FC0000            ldd _T_counts
 014C 8400              anda #0
 014E C401              andb #1
 0150 BD0000            jsr __d2lreg
 0153 BD0000            jsr __pshfp1 ; spill
 0156 BD0000            jsr __long2fp
 0159 BD0000            jsr __fpmov
 015C BD0000            jsr __pulfp1 ; reload
 015F BD0000            jsr __fpadd
 0162 BD0000            jsr __pulfp2 ; reload
 0165 BD0000            jsr __fpmul
 0168 18CE02B6          ldy #L20
 016C BD0000            jsr __tofp2
 016F BD0000            jsr __fpdiv
 0172 EC00              ldd 0,x
 0174 C30002            addd #2
 0177 188F              xgdy
 0179 BD0000            jsr __fromfp1
 017C EC00              ldd 0,x
 017E C30002            addd #2
 0181 188F              xgdy
 0183 BD0000            jsr __tofp1
 0186 18CE02B2          ldy #L21
 018A BD0000            jsr __tofp2
 018D BD0000            jsr __fpcmp
 0190 2D1E              blt L17
 0192 EC00              ldd 0,x
 0194 C30002            addd #2
 0197 188F              xgdy
 0199 BD0000            jsr __tofp1
 019C 18CE02B2          ldy #L21
 01A0 BD0000            jsr __tofp2
 01A3 BD0000            jsr __fpsub
 01A6 BD0000            jsr __fp2int
 01A9 C38000            addd #0x8000
 01AC ED06              std 6,x
 01AE 200F              bra L18
 01B0                 L17:
 01B0 EC00              ldd 0,x
 01B2 C30002            addd #2
 01B5 188F              xgdy
 01B7 BD0000            jsr __tofp1
 01BA BD0000            jsr __fp2int
 01BD ED06              std 6,x
 01BF                 L18:
 01BF EC06              ldd 6,x
 01C1 FD0006            std _OC2hi
                       ;     OC2lo=T_counts - OC2hi;
 01C4 FC0000            ldd _T_counts
 01C7 B30006            subd _OC2hi
 01CA FD0004            std _OC2lo
                       ; }
 01CD                 L15:
 01CD 8F                xgdx
 01CE C30008            addd #8
 01D1 8F                xgdx
 01D2 35                txs
 01D3 38                pulx
 01D4                   .dbline 0 ; func end
 01D4 39                rts
                       ;  lreg1 -> -4,x
                       ;  lreg2 -> -8,x
                       ;  IX -> 0,x
                       ;          ?temp -> +2,x
                       ;          ?temp -> +6,x
 01D5                 _cnts3::
 01D5 BD0000            jsr __enterb
 01D8 88                .byte 0x88
                       ; 
                       ; void cnts3 (void)
                       ; {
                       ;     OC3hi=T_counts * (float)per_on3/100;
 01D9 FC0004            ldd _per_on3
 01DC 04                lsrd
 01DD BD0000            jsr __d2lreg
 01E0 BD0000            jsr __long2fp
 01E3 BD0000            jsr __fpmov
 01E6 18CE02BA          ldy #L19
 01EA BD0000            jsr __tofp1
 01ED BD0000            jsr __fpmul
 01F0 FC0004            ldd _per_on3
 01F3 8400              anda #0
 01F5 C401              andb #1
 01F7 BD0000            jsr __d2lreg
 01FA BD0000            jsr __pshfp1 ; spill
 01FD BD0000            jsr __long2fp
 0200 BD0000            jsr __fpmov
 0203 BD0000            jsr __pulfp1 ; reload
 0206 BD0000            jsr __fpadd
 0209 BD0000            jsr __fpmov
 020C FC0000            ldd _T_counts
 020F 04                lsrd
 0210 BD0000            jsr __d2lreg
 0213 BD0000            jsr __long2fp
 0216 BD0000            jsr __pshfp2 ; spill
 0219 BD0000            jsr __fpmov
 021C 18CE02BA          ldy #L19
 0220 BD0000            jsr __tofp1
 0223 BD0000            jsr __fpmul
 0226 FC0000            ldd _T_counts
 0229 8400              anda #0
 022B C401              andb #1
 022D BD0000            jsr __d2lreg
 0230 BD0000            jsr __pshfp1 ; spill
 0233 BD0000            jsr __long2fp
 0236 BD0000            jsr __fpmov
 0239 BD0000            jsr __pulfp1 ; reload
 023C BD0000            jsr __fpadd
 023F BD0000            jsr __pulfp2 ; reload
 0242 BD0000            jsr __fpmul
 0245 18CE02B6          ldy #L20
 0249 BD0000            jsr __tofp2
 024C BD0000            jsr __fpdiv
 024F EC00              ldd 0,x
 0251 C30002            addd #2
 0254 188F              xgdy
 0256 BD0000            jsr __fromfp1
 0259 EC00              ldd 0,x
 025B C30002            addd #2
 025E 188F              xgdy
 0260 BD0000            jsr __tofp1
 0263 18CE02B2          ldy #L21
 0267 BD0000            jsr __tofp2
 026A BD0000            jsr __fpcmp
 026D 2D1E              blt L24
 026F EC00              ldd 0,x
 0271 C30002            addd #2
 0274 188F              xgdy
 0276 BD0000            jsr __tofp1
 0279 18CE02B2          ldy #L21
 027D BD0000            jsr __tofp2
 0280 BD0000            jsr __fpsub
 0283 BD0000            jsr __fp2int
 0286 C38000            addd #0x8000
 0289 ED06              std 6,x
 028B 200F              bra L25
 028D                 L24:
 028D EC00              ldd 0,x
 028F C30002            addd #2
 0292 188F              xgdy
 0294 BD0000            jsr __tofp1
 0297 BD0000            jsr __fp2int
 029A ED06              std 6,x
 029C                 L25:
 029C EC06              ldd 6,x
 029E FD0002            std _OC3hi
                       ;     OC3lo=T_counts - OC3hi;
 02A1 FC0000            ldd _T_counts
 02A4 B30002            subd _OC3hi
 02A7 FD0000            std _OC3lo
                       ; }
 02AA                 L22:
 02AA 8F                xgdx
 02AB C30008            addd #8
 02AE 8F                xgdx
 02AF 35                txs
 02B0 38                pulx
 02B1                   .dbline 0 ; func end
 02B1 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
 02B2                 L21:
 02B2 47000000          .word 0x4700,0x0
 02B6                 L20:
 02B6 42C80000          .word 0x42c8,0x0
 02BA                 L19:
 02BA 40000000          .word 0x4000,0x0
 02BE                 L7:
 02BE 25782020202578202020257820202020   .byte 37,'x,32,32,32,37,'x,32,32,32,37,'x,32,32,32,32
 02CE 25780A00          .byte 37,'x,10,0
