private int convertVoltage() {
            if (ADMUX_reg.isSingleEnded()) {
                // single ended conversions don't require amplification
                ADCInput dev = connectedDevices[ADMUX_reg.getSingleIndex()];
                float vin = dev != null ? dev.getVoltage() : 0;
                // TODO: select correct voltage reference from ADMUX register.
                float vref = voltageRef;
                if ( vin >= vref ) return 0x3ff;
                return 0x3ff & (int)(vin * 1024 / vref);
            } else {
                // use the differential gain amplifier.
                ADCInput pos = connectedDevices[ADMUX_reg.getPosIndex()];
                ADCInput neg = connectedDevices[ADMUX_reg.getNegIndex()];
                float vpos = pos != null ? pos.getVoltage() : 0;
                float vneg = neg != null ? neg.getVoltage() : 0;
                // TODO: select correct voltage reference from ADMUX register.
                float vref = voltageRef;
                float val = ((vpos - vneg) * ADMUX_reg.getGain() * 512 / vref);
                if ( val < -512 ) return 0x3ff;
                if ( val > 511 ) return 0x1ff;
                return 0x3ff & (int)val;
            }
        }