#!/usr/bin/python
# FC curve generator. (c) Antti S. Lankila 2008.
# This can exactly reproduce at least three chips, to which I have access.

import math

# My R4
dac = 0.0004
base_freq = 175
cf_offset = 0
scaler = 390

# my R3
#dac = 0.0004
#base_freq = 220
#cf_offset = 180
#scaler = 355

# Stock ReSID r2/r3
#dac = 0.0004
#base_freq = 220
#cf_offset = -285
#scaler = 355

def approximate_dac(x):
    # count the 1 bits
    bits = 0;
    for _ in range(0, 12):
        if x & (1 << _):
            bits += _ ** 2

    # give a bit of extra boost for each 1 bit
    val = x * (1 + dac * bits);
    return val

def get_point(x):
    x = approximate_dac(x)
    freq = base_freq + 10 ** ((x - cf_offset) / scaler)

    # this estimates the saturation of what I believe to be due to MOSFET
    # clearly this could be better stated, but let this "recursive" definition
    # stand for now.
    for _ in range(2000, 18000, 1000):
        if freq > _:
            freq -= (freq-_) / 3.5

    return freq

def main():
    for _ in range(0, 2048, 1):
        print "%d %f" % (_, get_point(_))

if __name__ == '__main__':
    main()

