summaryrefslogtreecommitdiff
path: root/pll.rb
diff options
context:
space:
mode:
Diffstat (limited to 'pll.rb')
-rwxr-xr-xpll.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/pll.rb b/pll.rb
new file mode 100755
index 0000000..49464d4
--- /dev/null
+++ b/pll.rb
@@ -0,0 +1,23 @@
1#!/usr/bin/ruby -w
2
3TARGET_FREQ = ARGV.shift.to_f
4NATIVE_FREQ = 50.0
5
6CLOCK_WIDTH = 5
7
8best = nil
9best_mult = nil
10best_div = nil
111.upto(2**CLOCK_WIDTH) do | mult |
12 1.upto(2**CLOCK_WIDTH) do | div |
13 new = NATIVE_FREQ * mult / div
14 if not best or (new - TARGET_FREQ).abs < (best - TARGET_FREQ).abs or ((new - TARGET_FREQ).abs == (best - TARGET_FREQ).abs and (mult + div) < (best_mult + best_div))
15 best = new
16 best_mult = mult
17 best_div = div
18 end
19 end
20end
21
22error = (best - TARGET_FREQ).abs / TARGET_FREQ
23$stdout.write("Closest I can get is #{best}: *#{best_mult} /#{best_div} (#{(error * 100).round}% error)\n")