]> hydra-www.ietfng.org Git - acmetensortoys-chiptunes/commitdiff
Initial perl script for computing timing constants
authorNathaniel Wesley Filardo <nwf@pf.priv.oc.ietfng.org>
Thu, 18 Feb 2010 04:46:24 +0000 (23:46 -0500)
committerNathaniel Wesley Filardo <nwf@pf.priv.oc.ietfng.org>
Thu, 18 Feb 2010 04:46:24 +0000 (23:46 -0500)
progenv/gentimes.pl [new file with mode: 0644]

diff --git a/progenv/gentimes.pl b/progenv/gentimes.pl
new file mode 100644 (file)
index 0000000..14f911f
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Getopt::Long;
+use Math::Trig 'pi2';
+
+sub genfreqtab ($$$) {
+       my ($sr,$lo,$hi) = @_;
+       my $res = [ ];
+       for my $i ($lo..$hi) {
+               push @$res, 2**16/$sr*(440*2**(($i-49)/12));
+       }       
+       $res
+}
+
+sub gensinetab ($$) {
+       my ($ndiv, $max) = @_;
+       my $res = [ ];
+       for my $i (0..$ndiv-1) {
+               push @$res, int(sin(pi2*$i/$ndiv) * $max);
+       }
+       $res
+}
+
+my $FCPU = 20000000;
+my $SAMPLERATE = 16000;
+my $SINEWSIZE = 64;
+
+print "static const s8 sinetable[] = {\n";
+print join ", ", map { int } @{gensinetab($SINEWSIZE,127)} ;
+print "};\n\n";
+
+# The frequency table ranges from C1 (note 4) to B7 (note 87)
+# 
+print "static const u16 freqtable[] = {\n";
+foreach (@{genfreqtab($SAMPLERATE,4,87)}) { printf "%4x, ", int($_); }
+print "};\n\n";
+
+# Generate timer0 prescaler and limit for the mode we use.
+# t0denoms are from AVR spec.
+my @t0denoms = ( undef, 1, 8, 64, 256, 1024, undef, undef );
+my $ix = 1;
+while ($ix < 5 and $FCPU/$SAMPLERATE/$t0denoms[$ix] > 255) { $ix++; }
+die if $ix == 5;
+print "static const int T0DIV = ", $ix, ";\n";
+print "static const int T0MAX = ",
+               int($FCPU/$SAMPLERATE/$t0denoms[$ix]), ";\n\n";