From: Nathaniel Wesley Filardo Date: Thu, 18 Feb 2010 04:46:24 +0000 (-0500) Subject: Initial perl script for computing timing constants X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=7d716da1767b4fd5008b33b9a6d6c1b7added1c4;p=acmetensortoys-chiptunes Initial perl script for computing timing constants --- diff --git a/progenv/gentimes.pl b/progenv/gentimes.pl new file mode 100644 index 0000000..14f911f --- /dev/null +++ b/progenv/gentimes.pl @@ -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";