; TEMPOCHG.SRC
;
; THIS SUBROUTINE IS USED TO CALCULATE THE SPEED VALUE TO BE USED WHEN
; A TEMPO CHANGE META EVENT IS FOUND.
; INPUT:
;        RESOLUTION (2 BYTES) = LAST 2 BYTES OF MIDI FILE HEADER
;        NEWTEMPO   (3 BYTES) = VALUES FROM THE FF 51 03 META EVENT
; OUTPUT:
;        NEWSPEED   (1 BYTE)  = NEW SPEED VALUE.  YOUR PROGRAM MAY WANT TO
;                               ADJUST THIS BASED ON USER'S PREFERENCES.
;
TEMPOCHG JSR TMPOSTRT
         JMP LOOKUP
DIVISION JMP DIVIDE
;
; PUBLIC VARIABLES
RESHI    DFB $00
RESLO    DFB $00
TMPOHI   DFB $00
TMPOMED  DFB $00
TMPOLO   DFB $00
NEWSPEED DFB $00
;
; PRIVATE VARIABLES
DIVIDEND DFB $00,$00,$00,$00
DIVISOR  DFB $00,$00
QUOTIENT DFB $00,$00,$00,$00
REMAINDR DFB $00,$00
TEMPDIV  DFB $00,$00,$00
TEMPDSR  DFB $00,$00
;
TMPOSTRT LDA #$00
         STA DIVIDEND
         LDA TMPOHI
         STA DIVIDEND+1
         LDA TMPOMED
         STA DIVIDEND+2
         LDA TMPOLO
         STA DIVIDEND+3
         LDA RESHI
         STA DIVISOR
         LDA RESLO
         STA DIVISOR+1
;
DIVIDE   CLD
         LDA #$00
         STA QUOTIENT
         STA QUOTIENT+1
         STA QUOTIENT+2
         STA QUOTIENT+3
         STA REMAINDR
         STA REMAINDR+1
         STA TEMPDIV
         STA TEMPDIV+1
         STA TEMPDIV+2
         LDA DIVISOR
         STA TEMPDSR
         LDA DIVISOR+1
         STA TEMPDSR+1
         LDA TEMPDSR
         BNE NODIV0
         LDA TEMPDSR+1
         BNE NODIV0
         BRK
;
NODIV0   LDY #32             ;DIVIDEND WILL BE SHIFTED 32 TIMES
DIVLOOP  ASL DIVIDEND+3
         ROL DIVIDEND+2
         ROL DIVIDEND+1
         ROL DIVIDEND        ;MOVE 1 BIT TO TEMPORARY DIVIDEND
         ROL TEMPDIV+2
         ROL TEMPDIV+1
         ROL TEMPDIV         ;NOW CHECK IF TEMPDSR < TEMPDIV
         LDA TEMPDIV
         BNE NOTLESS
         LDA TEMPDIV+1
         CMP TEMPDSR
         BCC LESS
         BNE NOTLESS
         LDA TEMPDIV+2
         CMP TEMPDSR+1
         BCS NOTLESS
LESS     CLC                 ;STORE A 0 IN THE QUOTIENT
         ROL QUOTIENT+3
         ROL QUOTIENT+2
         ROL QUOTIENT+1
         ROL QUOTIENT
         CLV
         BVC NEXTBIT
NOTLESS  SEC                 ;SUBTRACT TEMPDSR FROM TEMPDIV
         LDA TEMPDIV+2
         SBC TEMPDSR+1
         STA TEMPDIV+2
         LDA TEMPDIV+1
         SBC TEMPDSR
         STA TEMPDIV+1
         LDA TEMPDIV
         SBC #$00
         STA TEMPDIV
         SEC                 ;AND STORE A 1 IN THE QUOTIENT
         ROL QUOTIENT+3
         ROL QUOTIENT+2
         ROL QUOTIENT+1
         ROL QUOTIENT
NEXTBIT  DEY
         BNE DIVLOOP         ;WHEN DONE, QUOTIENT HAS RESULT, TDIV HAS RMDR
         LDA TEMPDIV+1
         STA REMAINDR
         LDA TEMPDIV+2
         STA REMAINDR+1
         RTS
;
LOOKUP   LDY #2            ;LOOKUP QUOTIENT IN TEMPO TABLE-STARTING WITH 2
;                          ;BECAUSE IF LESS THAN VALUE OF 2 SHOULD USE 1
         LDA QUOTIENT
         BEQ Q3BYT
QBIG     LDY #127
         CLV
         BVC STORSPED
Q3BYT    LDA QUOTIENT+1
         BNE QBIG
;                            ;WE HAVE A 2 BYTE QUOTIENT TO LOOK UP
Q2LOOP   LDA QUOTIENT+2
         CMP TEMPOHI,Y
         BCC FOUNDSP
         BNE NEXTSPED
         LDA QUOTIENT+3
         CMP TEMPOLO,Y
         BCC FOUNDSP
NEXTSPED INY
         CPY #128
         BCC Q2LOOP
FOUNDSP  DEY
STORSPED STY NEWSPEED
         RTS
;
; THE FOLLOWING DATA WAS CREATED BY BASIC PROGRAM "TEMPOCALC"
; IT REPRESENTS THE EVALUATION OF FUNCTION .4889*(26+27*A+5*A*A) FROM 0-127
;
TEMPOHI  DFB 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2
         DFB 3,3,4,4,4,5,5,6,6,7,7,8,8,9,10,10
         DFB 11,12,12,13,14,15,15,16,17,18,19,19,20,21,22,23
         DFB 24,25,26,27,28,29,30,31,32,34,35,36,37,38,39,41
         DFB 42,43,45,46,47,49,50,51,53,54,56,57,59,60,62,63
         DFB 65,66,68,70,71,73,75,76,78,80,82,83,85,87,89,91
         DFB 93,94,96,98,100,102,104,106,108,110,112,114,116,119,121,123
         DFB 125,127,130,132,134,136,139,141,143,146,148,150,153,155,158,160
TEMPOLO  DFB 12,28,48,74,104,139,179,224,18,73,133,197,11,85,164,248
         DFB 81,175,18,121,230,87,206,73,201,78,216,103,250,147,48,211
         DFB 122,38,215,141,71,7,204,149,99,55,15,236,206,180,160,145
         DFB 134,128,127,132,141,154,173,197,225,3,41,84,132,185,243,50
         DFB 118,190,12,94,181,17,114,216,67,179,39,161,31,162,42,183
         DFB 73,224,123,28,193,108,27,207,136,70,9,208,157,110,69,32
         DFB 0,229,207,190,177,170,167,170,177,189,206,228,254,30,67,108
         DFB 154,206,6,67,133,203,23,104,189,23,119,219,68,178,36,156
;
         CHN MFF0DRVR.SC6
