

EG_INCREMENT_TBL = [ # From MAME ymfm.cpp
    0x00000000, 0x00000000, 0x10101010, 0x10101010,  # 0-3    (0x00-0x03)
    0x10101010, 0x10101010, 0x11101110, 0x11101110,  # 4-7    (0x04-0x07)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 8-11   (0x08-0x0B)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 12-15  (0x0C-0x0F)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 16-19  (0x10-0x13)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 20-23  (0x14-0x17)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 24-27  (0x18-0x1B)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 28-31  (0x1C-0x1F)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 32-35  (0x20-0x23)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 36-39  (0x24-0x27)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 40-43  (0x28-0x2B)
    0x10101010, 0x10111010, 0x11101110, 0x11111110,  # 44-47  (0x2C-0x2F)
    0x11111111, 0x21112111, 0x21212121, 0x22212221,  # 48-51  (0x30-0x33)
    0x22222222, 0x42224222, 0x42424242, 0x44424442,  # 52-55  (0x34-0x37)
    0x44444444, 0x84448444, 0x84848484, 0x88848884,  # 56-59  (0x38-0x3B)
    0x88888888, 0x88888888, 0x88888888, 0x88888888   # 60-63  (0x3C-0x3F)
].freeze

EG_INCREMENT_AVG = EG_INCREMENT_TBL.map{|e| (0..7).map{|i|(e>>i*4)&15}.sum / 8.0}.freeze

EG_RATEDIV = (0..63).map{|r| [1,2048 >> (r>>2)].max}.freeze

EG_EFFECTIVE_RATE = EG_INCREMENT_AVG.zip(EG_RATEDIV).map{|(i,d)|i/d}.freeze


EG_CLOCKDIV = ((24*3)/1.0)/3.0
EG_MOREBITS = 1<<(26-10)
EG_FACTOR = EG_MOREBITS*EG_CLOCKDIV

EG_CON = EG_EFFECTIVE_RATE.map.with_index{|r,i|"EG_RATE_#{i} = round(#{r}*EG_FACTOR)"}.join(?\n)

EG_MTBL = (0..63).map{|i|"byte EG_RATE_#{i}>>(((encod EG_RATE_#{i})-7)#>0)"}.join(?\n)
EG_STBL = (0..63).map{|i|"byte ((encod EG_RATE_#{i})-7)#>0"}.join(?\n)

File.write "eg_rates.txt",(EG_CON+(?\n*5)+EG_MTBL+(?\n*5)+EG_STBL)
File.write "eg_rates_int.txt",EG_EFFECTIVE_RATE.map.with_index{|r,i|"long #{(r*EG_FACTOR).round} ' #{i}"}.join(?\n)