From d5b05bc1b6b9fe6885991c3b49c1f3b1df594660 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Thu, 15 Apr 2021 09:10:48 -0700 Subject: Add a quick and dirty script for computing PLL parameters. --- pll.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 pll.rb (limited to 'pll.rb') diff --git a/pll.rb b/pll.rb new file mode 100755 index 0000000..49464d4 --- /dev/null +++ b/pll.rb @@ -0,0 +1,23 @@ +#!/usr/bin/ruby -w + +TARGET_FREQ = ARGV.shift.to_f +NATIVE_FREQ = 50.0 + +CLOCK_WIDTH = 5 + +best = nil +best_mult = nil +best_div = nil +1.upto(2**CLOCK_WIDTH) do | mult | + 1.upto(2**CLOCK_WIDTH) do | div | + new = NATIVE_FREQ * mult / div + 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)) + best = new + best_mult = mult + best_div = div + end + end +end + +error = (best - TARGET_FREQ).abs / TARGET_FREQ +$stdout.write("Closest I can get is #{best}: *#{best_mult} /#{best_div} (#{(error * 100).round}% error)\n") -- cgit v1.2.3