#!/usr/bin/perl use strict; use integer; my($infile,$outfile,$length) = @ARGV; unless (defined($outfile)) { die "Usage: $0 infile outfile [length]\n"; } sub nextblock($$$) { my($block,$len,$limit) = @_; my $bytes = $block; if (defined($limit) && $limit - $len < $bytes) { $bytes = $limit - $len; } return $bytes; } open(my $in, '<', $infile) or die "$0: $infile: $!\n"; binmode $in; my $data; my $sum = 0; my $len = 0; my $blocklen; while (($blocklen = nextblock(4096, $len, $length)) && read($in, $data, $blocklen) == $blocklen) { foreach my $w (unpack("V*", $data)) { $sum = ($sum + $w) & 0xffffffff; } $len += $blocklen; } close($in); open(my $out, '>', $outfile) or die "$0: $outfile: $!\n"; my $outfile_c = uc($outfile); $outfile_c =~ s/[\W_]+/_/g; print $out "#ifndef $outfile_c\n"; print $out "#define $outfile_c\n"; printf $out "#define SDRAM_SUM 0x%08x\n", $sum; print $out "#endif\n"; close($out);