;-----------------------------------------------------
qFactor
;       This routine adjusts for the Q-factor setting.
;       On entry, wreg contains a stimulus level: 0=T, 255=C.
;       On exit, wreg contains the adjusted stimulus level.
;       MC_Q is known to be between 0 and 6, based on POTVAL22.
;        Q=6 --> loudest  Q=3 --> no adjustment  Q=0 --> softest
;       If we define:    stimout = stimin + qadj,
;        then we can use a symmetry property to require only 3 LUTs.
;            qadj(stimin,Q)=-qadj(255-stimin,6-Q)
;
;shared variable: MC_Q
;local variables: QSTIMSAVE
;                 QSYM
;                 QSTIM
;                 QVALUE
;                 QADJ

        movwf QSTIMSAVE         ; save value
        movlw 3
        cpfseq MC_Q
        goto qf0
        movfw QSTIMSAVE         ; MC_Q=3. No adjustment necessary.
        goto qfdone
qf0
                                ; save some values
        clrf QSYM,F             ; 0 flag says we're not using symmetry
        movfw QSTIMSAVE
        movwf QSTIM             ; stimulus level
        movfw MC_Q
        movwf QVALUE            ; MC_Q 4-6

        cpfsgt MC_Q
        goto qf1
        goto qf2
qf1
                        ; MC_Q is 0-2. Use the symmetry property.
        bsf QSYM,0              ; non-0 flag says we're using symmetry
        QSTIM=255-QSTIMSAVE     ; adjust stimulus level
        QVALUE=6-MC_Q        ;

qf2                     ; QVALUE is 4-6. Make adjustment
        movlw 4
        cpfseq QVALUE
        goto qf3
                        ; QVALUE is 4. Use qlut4
        movfw QSTIM
        call qlut4
        movwf QADJ
        goto qf5
qf3
        movlw 5
        cpfseq QVALUE
        goto qf4
                        ; QVALUE is 5. Use qlut5
        movfw QSTIM
        call qlut5
        movwf QADJ
        goto qf5
qf4
                        ; QVALUE is 6. Use qlut6
        movfw QSTIM
        call qlut6
        movwf QADJ

qf5                     ; QADJ has been calc'd
        tstfsz QSYM     ; test whether we're using symmetry
        goto qfsym
        goto qfnosym
qfsym
        wreg=QSTIMSAVE-QADJ
        goto qfdone
qfnosym
        wreg=QSTIMSAVE+QADJ

qfdone

        return          ; from qFactor
;-----------------------------------------------------
