                         .module sci-test.c
                         .area text
                       ;  IX -> 0,x
                       ;          dummy -> +3,x
 0000                 _initBoardB::
 0000 BD0000            jsr __enterb
 0003 04                .byte 0x4
                       ; //used 20,000 for PWM period therefore ontime and offtime
                       ; //are changed by a difference of 1000 (5% of 20000)
                       ; 
                       ; #include <hc11.h>
                       ; #include <stdio.h>
                       ; #include <vector.h>
                       ; 
                       ; #define endProg() asm("swi")
                       ; #define enableInt() asm("cli")
                       ; #define disableInt() asm("sei")
                       ; 
                       ; #define fivePercent 1000
                       ; 
                       ; short ontime, offtime;
                       ; char motorStatus, lightStatus;
                       ; 
                       ; void initBoardB(void);
                       ; void OC1handler(void);
                       ; 
                       ; /*******************************************************
                       ; Name: initBoardB
                       ; Purpose: initilize slave board (boardB)
                       ;              - turn LEDs off
                       ;              - turn motor on with PWM duty cycle of 50%
                       ;              - configure SPI
                       ;              - set up OC1
                       ; Inputs: none
                       ; Outputs: none
                       ; ********************************************************/
                       ; void initBoardB()
                       ; {
                       ;     char dummy;
                       ;     disableInt(); //disable interrupts
 0004 0F                    sei
                       
                       ;     
                       ;     PORTA = 0x00;
 0005 7F1000            clr 0x1000
                       ;     PACTL = 0x08; //configure PA3 as output
 0008 C608              ldab #8
 000A F71026            stab 0x1026
                       ;     ontime = 10000;
 000D CC2710            ldd #10000
 0010 FD0004            std _ontime
                       ;     offtime = 10000;
 0013 CC2710            ldd #10000
 0016 FD0002            std _offtime
                       ;     lightStatus = 2;
 0019 C602              ldab #2
 001B F70000            stab _lightStatus
                       ;     
                       ;     //??? SPI Configuration... [master basically does everything]
                       ;     //SPCR = 0x40;
                       ;     dummy = SCSR;
                         ; vol
 001E F6102E            ldab 0x102e
 0021 E703              stab 3,x
                       ;     dummy = SCDR;
                         ; vol
 0023 F6102F            ldab 0x102f
 0026 E703              stab 3,x
                       ;     
                       ;     TOC1vector= &OC1handler;
 0028 CC0063            ldd #_OC1handler
 002B FDFFE8            std 0xffe8
                       ;     TMSK1 = 0x80;
 002E C680              ldab #128
 0030 F71022            stab 0x1022
                       ;     TFLG1 = 0x80;
 0033 C680              ldab #128
 0035 F71023            stab 0x1023
                       ;     TOC1 = TCNT + ontime;
                         ; vol
 0038 FC100E            ldd 0x100e
 003B F30004            addd _ontime
 003E FD1016            std 0x1016
                       ;         
                       ;     printf("init complete..., interrupts started...\n\n");
 0041 CC0238            ldd #L4
 0044 BD0000            jsr _printf
                       ;     printf("init ontime %d, offtime %d\n", ontime, offtime); //debugger
 0047 FC0002            ldd _offtime
 004A 37                pshb
 004B 36                psha
 004C FC0004            ldd _ontime
 004F 37                pshb
 0050 36                psha
 0051 CC021C            ldd #L5
 0054 BD0000            jsr _printf
 0057 1838              puly
 0059 1838              puly
                       ;     enableInt(); //enable interrupts
 005B 0E                    cli
                       
                       ; }
 005C                 L3:
 005C 08                inx
 005D 08                inx
 005E 08                inx
 005F 08                inx
 0060 35                txs
 0061 38                pulx
 0062                   .dbline 0 ; func end
 0062 39                rts
 0063                 _OC1handler::
                       ; 
                       ; 
                       ; /*******************************************************
                       ; Name: OC1handler
                       ; Purpose: - control ontime/offtime for motor (using PWM)
                       ; Inputs: none
                       ; Outputs: none
                       ; ********************************************************/
                       ; /*
                       ; #pragma interrupt_handler OC1handler()
                       ; void OC1handler()
                       ; {
                       ;     if (motorStatus == 1)
                       ;     {
                       ;         motorStatus = 0;
                       ;         
                       ;         if (ontime >= 20000)
                       ;          TOC1 = 20000;
                       ;         else
                       ;          TOC1 = TOC1 + offtime;
                       ;         
                       ;         PORTA |= 0x08;
                       ;     }
                       ;     else if(motorStatus == 0)
                       ;     {
                       ;         motorStatus = 1;
                       ;         
                       ;         if (offtime >= 20000)
                       ;         {
                       ;          TOC1 = 20000;
                       ;          printf("dec...");
                       ;         }
                       ;         else
                       ;          TOC1 = TOC1 + ontime;
                       ;         
                       ;         PORTA ^= 0x08;
                       ;     }
                       ;     
                       ;     TFLG1 = 0x80;
                       ; }
                       ; */
                       ; 
                       ; #pragma interrupt_handler OC1handler()
                       ; void OC1handler()
                       ; {
                       ;     if (ontime >= 20000)
 0063 FC0004            ldd _ontime
 0066 1A834E20          cpd #20000
 006A 2D25              blt L7
                       ;     {
                       ;         ontime = 20000;
 006C CC4E20            ldd #20000
 006F FD0004            std _ontime
                       ;         offtime = 0;
 0072 CC0000            ldd #0
 0075 FD0002            std _offtime
                       ;         TOC1 = TOC1 + ontime;
                         ; vol
 0078 FC1016            ldd 0x1016
 007B F30004            addd _ontime
 007E FD1016            std 0x1016
                       ;         PORTA |= 0x60;
 0081 18CE1000          ldy #0x1000
 0085 181C0060          bset 0,y,#96
                       ;         printf("max PWM...\n");
 0089 CC0210            ldd #L9
 008C BD0000            jsr _printf
                       ;     }
 008F 2063              bra L8
 0091                 L7:
                       ;     else if (offtime >= 20000)
 0091 FC0002            ldd _offtime
 0094 1A834E20          cpd #20000
 0098 2D22              blt L10
                       ;     {
                       ;         offtime = 20000;
 009A CC4E20            ldd #20000
 009D FD0002            std _offtime
                       ;         ontime = 0;
 00A0 CC0000            ldd #0
 00A3 FD0004            std _ontime
                       ;         TOC1 = TOC1 + offtime;
                         ; vol
 00A6 FC1016            ldd 0x1016
 00A9 F30002            addd _offtime
 00AC FD1016            std 0x1016
                       ;         PORTA = 0x10;
 00AF C610              ldab #16
 00B1 F71000            stab 0x1000
                       ;         printf("min PWM...\n");
 00B4 CC0204            ldd #L12
 00B7 BD0000            jsr _printf
                       ;     }   
 00BA 2038              bra L11
 00BC                 L10:
                       ;     else if (motorStatus == 1)
 00BC F60001            ldab _motorStatus
 00BF C101              cmpb #1
 00C1 2616              bne L13
                       ;     {
                       ;         motorStatus = 0;
 00C3 7F0001            clr _motorStatus
                       ;         TOC1 = TOC1 + offtime;
                         ; vol
 00C6 FC1016            ldd 0x1016
 00C9 F30002            addd _offtime
 00CC FD1016            std 0x1016
                       ;         PORTA ^= 0x40;
                         ; vol
 00CF F61000            ldab 0x1000
 00D2 C840              eorb #64
 00D4 F71000            stab 0x1000
                       ;         //printf("status-on \n ontime %d, offtime %d\n", ontime, offtime); //debugger
                       ;         //printf("on");
                       ;     }
 00D7 201B              bra L14
 00D9                 L13:
                       ;     else if(motorStatus == 0)
 00D9 7D0001            tst _motorStatus
 00DC 2616              bne L15
                       ;     {
                       ;         motorStatus = 1;
 00DE C601              ldab #1
 00E0 F70001            stab _motorStatus
                       ;         TOC1 = TOC1 + ontime;
                         ; vol
 00E3 FC1016            ldd 0x1016
 00E6 F30004            addd _ontime
 00E9 FD1016            std 0x1016
                       ;         PORTA ^= 0x40;
                         ; vol
 00EC F61000            ldab 0x1000
 00EF C840              eorb #64
 00F1 F71000            stab 0x1000
                       ;         //printf("status-on \n ontime %d, offtime %d\n", ontime, offtime); //debugger
                       ;         //printf("off");
                       ;     }
 00F4                 L15:
 00F4                 L14:
 00F4                 L11:
 00F4                 L8:
                       ;     
                       ;     TFLG1 = 0x80;
 00F4 C680              ldab #128
 00F6 F71023            stab 0x1023
                       ; }
 00F9                 L6:
 00F9                   .dbline 0 ; func end
 00F9 3B                rti
                       ;  IX -> 0,x
                       ;   masterSignal -> +2,x
                       ;          dummy -> +3,x
 00FA                 _main::
 00FA BD0000            jsr __enterb
 00FD 04                .byte 0x4
                       ; 
                       ; 
                       ; /*******************************************************
                       ; Name: Main
                       ; Purpose: - Initialize system by calling initBoardB() function
                       ;              - Wait for SPI messages and calls other functions
                       ;                to do specific tasks, depending on message recieved.
                       ;              - Turns on correct LED (depending on button pushed
                       ;                from master)
                       ; Inputs: none
                       ; Outputs: none
                       ; ********************************************************/
                       ; main()
                       ; {
                       ;     char dummy, masterSignal;
                       ;     
                       ;     initBoardB();
 00FE BD0000            jsr _initBoardB
 0101 7E01D1            jmp L19
 0104                 L18:
                       ; 
                       ; /*  
                       ;     while(1)
                       ;     {
                       ;         while ((SPSR & 0x80) == 0)
                       ;         {
                       ;             //check for collision error
                       ;             if((SPSR & 0x40) != 0)
                       ;             {
                       ;                 dummy = SPSR; //this clears collision error flag (WCOL) together with next step
                       ;                 dummy = SPDR; //read corrupted data
                       ;             }
                       ;         }
                       ;         
                       ;         //clear SPIF bit in SPSR (SPI transfer complete flag)
                       ;         dummy = SPSR;           
                       ;         masterSignal = SPDR;
                       ; */
                       ;     
                       ;     //=================delete this=======================
                       ;     while(1)
                       ;     {
                       ;         dummy = SCSR;
                         ; vol
 0104 F6102E            ldab 0x102e
 0107 E703              stab 3,x
                       ;         dummy = SCDR;
                         ; vol
 0109 F6102F            ldab 0x102f
 010C E703              stab 3,x
 010E 200A              bra L22
 0110                 L21:
                         ; vol
 0110 F6102E            ldab 0x102e
 0113 E703              stab 3,x
                         ; vol
 0115 F6102F            ldab 0x102f
 0118 E702              stab 2,x
 011A                 L22:
                       ;         
                       ;         while ((SCSR & 0x20) == 0)
 011A 18CE102E          ldy #0x102e
 011E 181F0020ED        brclr 0,y,#32,L21
                       ;         {
                       ;             //clear SCDR
                       ;             dummy = SCSR;       
                       ;             masterSignal = SCDR;
                       ;             //printf("waiting...");
                       ;         }       
                       ;         //===============delete this=====================
                       ;                 
                       ;         if (masterSignal == 0x30)
 0123 E602              ldab 2,x
 0125 C130              cmpb #48
 0127 2652              bne L24
                       ;         {
                       ;             if (offtime < 0)
 0129 FC0002            ldd _offtime
 012C 2C06              bge L26
                       ;                offtime = 0;
 012E CC0000            ldd #0
 0131 FD0002            std _offtime
 0134                 L26:
                       ;             ontime -= fivePercent;
 0134 FC0004            ldd _ontime
 0137 8303E8            subd #1000
 013A FD0004            std _ontime
                       ;             offtime += fivePercent;
 013D FC0002            ldd _offtime
 0140 C303E8            addd #1000
 0143 FD0002            std _offtime
                       ;             
                       ;             PORTA |= 0x10;
 0146 18CE1000          ldy #0x1000
 014A 181C0010          bset 0,y,#16
                       ;             if ((PORTA & 0x20) != 0)
 014E 18CE1000          ldy #0x1000
 0152 181F002008        brclr 0,y,#32,L28
                       ;                 PORTA ^= 0x20;
                         ; vol
 0157 F61000            ldab 0x1000
 015A C820              eorb #32
 015C F71000            stab 0x1000
 015F                 L28:
                       ; 
                       ;             printf("ontime %d, offtime %d\n", ontime, offtime); //debugger
 015F FC0002            ldd _offtime
 0162 37                pshb
 0163 36                psha
 0164 FC0004            ldd _ontime
 0167 37                pshb
 0168 36                psha
 0169 CC01ED            ldd #L30
 016C BD0000            jsr _printf
 016F 1838              puly
 0171 1838              puly
                       ;             printf("red PWM\n");
 0173 CC01E4            ldd #L31
 0176 BD0000            jsr _printf
                       ;         }
 0179 2056              bra L25
 017B                 L24:
                       ;         else if (masterSignal == 0x31)
 017B E602              ldab 2,x
 017D C131              cmpb #49
 017F 2650              bne L32
                       ;         {
                       ;             if (ontime < 0)
 0181 FC0004            ldd _ontime
 0184 2C06              bge L34
                       ;                ontime = 0;
 0186 CC0000            ldd #0
 0189 FD0004            std _ontime
 018C                 L34:
                       ;             ontime += fivePercent;
 018C FC0004            ldd _ontime
 018F C303E8            addd #1000
 0192 FD0004            std _ontime
                       ;             offtime -= fivePercent;
 0195 FC0002            ldd _offtime
 0198 8303E8            subd #1000
 019B FD0002            std _offtime
                       ;             
                       ;             PORTA |= 0x20;
 019E 18CE1000          ldy #0x1000
 01A2 181C0020          bset 0,y,#32
                       ;             if ((PORTA & 0x10) != 0)
 01A6 18CE1000          ldy #0x1000
 01AA 181F001008        brclr 0,y,#16,L36
                       ;                 PORTA ^= 0x10;
                         ; vol
 01AF F61000            ldab 0x1000
 01B2 C810              eorb #16
 01B4 F71000            stab 0x1000
 01B7                 L36:
                       ;             
                       ;             printf("ontime %d, offtime %d\n", ontime, offtime); //debugger          
 01B7 FC0002            ldd _offtime
 01BA 37                pshb
 01BB 36                psha
 01BC FC0004            ldd _ontime
 01BF 37                pshb
 01C0 36                psha
 01C1 CC01ED            ldd #L30
 01C4 BD0000            jsr _printf
 01C7 1838              puly
 01C9 1838              puly
                       ;             printf("inc PWM\n");   
 01CB CC01DB            ldd #L38
 01CE BD0000            jsr _printf
                       ;         }
 01D1                 L32:
 01D1                 L25:
 01D1                 L19:
 01D1 7E0104            jmp L18
 01D4                 X0:
                       ;     }
                       ; }
 01D4                 L17:
 01D4 08                inx
 01D5 08                inx
 01D6 08                inx
 01D7 08                inx
 01D8 35                txs
 01D9 38                pulx
 01DA                   .dbline 0 ; func end
 01DA 39                rts
                         .area bss
 0000                 _lightStatus::
 0000                   .blkb 1
 0001                 _motorStatus::
 0001                   .blkb 1
 0002                 _offtime::
 0002                   .blkb 2
 0004                 _ontime::
 0004                   .blkb 2
                         .area text
 01DB                 L38:
 01DB 696E632050574D0A00     .byte 'i,'n,'c,32,'P,'W,'M,10,0
 01E4                 L31:
 01E4 7265642050574D0A00     .byte 'r,'e,'d,32,'P,'W,'M,10,0
 01ED                 L30:
 01ED 6F6E74696D652025642C206F66667469   .byte 'o,'n,'t,'i,'m,'e,32,37,'d,44,32,'o,'f,'f,'t,'i
 01FD 6D652025640A00    .byte 'm,'e,32,37,'d,10,0
 0204                 L12:
 0204 6D696E2050574D2E2E2E0A00   .byte 'm,'i,'n,32,'P,'W,'M,46,46,46,10,0
 0210                 L9:
 0210 6D61782050574D2E2E2E0A00   .byte 'm,'a,'x,32,'P,'W,'M,46,46,46,10,0
 021C                 L5:
 021C 696E6974206F6E74696D652025642C20   .byte 'i,'n,'i,'t,32,'o,'n,'t,'i,'m,'e,32,37,'d,44,32
 022C 6F666674696D652025640A00   .byte 'o,'f,'f,'t,'i,'m,'e,32,37,'d,10,0
 0238                 L4:
 0238 696E697420636F6D706C6574652E2E2E   .byte 'i,'n,'i,'t,32,'c,'o,'m,'p,'l,'e,'t,'e,46,46,46
 0248 2C20696E746572727570747320737461   .byte 44,32,'i,'n,'t,'e,'r,'r,'u,'p,'t,'s,32,'s,'t,'a
 0258 727465642E2E2E0A0A00   .byte 'r,'t,'e,'d,46,46,46,10,10,0
