#!/usr/bin/perl #------------------------------------------------------------------------------ # Xilinx .MIF to .COE Converter Module # filename: mif_hex2dec.pl # # by Jeremy Webb # # Rev 1.0, April 14, 2006 # # This utility is intended to convert a Xilinx Memory Initialization # File (.MIF) to a Xilinx Coefficient File (.COE) file. This script # basically converts base 2 hexadecimal values to base 10 integers. # This script defaults to 18-bit resolution for the hexadecimal-to-decimal # conversion. # # Revision History: # 1.0 04/14/2006 Initial release # # Please report bugs, errors, etc. #------------------------------------------------------------------------------ # Retrieve command line argument use strict; use Toolbox::Simple qw(dec2hex); # Retrieve command line argument my $file = $ARGV[0]; # MIF File my $resin = $ARGV[1]; # Defaults to 18: # Error Checking: if ($file eq "") { print_usage(); exit 1; } elsif ($file eq "HELP") { print_help(); exit 1; } elsif ($file eq "help") { print_help(); exit 1; } # Initialize Variables: my $res; # Hexadecimal Bit Resolution. if ($resin eq "") { $res = 18; } else { $res = $resin; } # Determine number of hex digits in MIF file. my $base2pwr = 2**$res; my $pwrhex = dec2hex($base2pwr); my @pwrchars = split("",$pwrhex); my $pwrcharcnt = scalar(@pwrchars); my $fullhex = $pwrcharcnt * 4; my $diffhex = $fullhex - $res; # Subtract 1 from the base2 power: 2^18 - 1; my $basemin1 = $base2pwr - 1; my $base2pwrb = 2**($res - 1); my $basemin2 = $base2pwrb - 1; my $basemin2hex = dec2hex($basemin2); print "$pwrhex\n"; print "$basemin2hex\n"; # Open file for reading: open inF, "< $file" or die "Can't open $file : $!"; my (@data) = ; # Strip the .mif from the file name and use for the module name: $file =~ s/\.mif$//; # Remove the "\n" newline characters from the data: foreach my $i (@data) { chomp($i); } # Open File for writing decimal values: my $decfile1 = join("_",$file,"dec"); my $decfile = join(".",$decfile1,"txt"); # check to make sure that the file doesn't exist. die "Oops! A file called '$decfile' already exists.\n" if -e $decfile; open(my $outF, ">", $decfile); # Open File for writing COE File: my $coefile = join(".",$file,"coe"); # check to make sure that the file doesn't exist. die "Oops! A file called '$coefile' already exists.\n" if -e $coefile; open(my $coedata, ">", $coefile); # Open File for writing MIF File: my $matfile = join(".",$file,"m"); # check to make sure that the file doesn't exist. die "Oops! A file called '$matfile' already exists.\n" if -e $matfile; open(my $matdata, ">", $matfile); # COE File Header: print($coedata "radix = 10;\n"); print($coedata "coefdata=\n"); # Create variables for "FOR" loop: my @signedata; my $lines = scalar(@data); my $line = 0; for ($line = 0; $line < $lines; $line++) { my $hexval = $data[$line]; my $newhex = join "", "0x", $hexval; my $unsignedec = hex $newhex; my $signedec; if ($unsignedec > hex($basemin2hex)) { $signedec = $unsignedec - hex($pwrhex); } else { $signedec = $unsignedec;} my $newsign = $signedec / $basemin1; my $index = $line+1; print($matdata "out($index) = $newsign;\n"); if ($line < $lines-1) { print($coedata "$signedec,\n"); } else { print($coedata "$signedec;"); } push(@signedata,$signedec); print($outF "$signedec\n"); } # Close write file: close($outF); close($matdata); close($coedata); exit; #------------------------------------------------------------------------------ # Generic Error and Exit routine #------------------------------------------------------------------------------ sub dienice { my($errmsg) = @_; print"$errmsg\n"; exit; } sub print_usage { print("\nUsage: mif_hex2dec.pl [FILE] [RES]\n"); print("\n"); print("Options:\n"); print("\tFILE\tWhere FILE is the Xilinx Memory Initialization File (*.mif) containing base-2 numbers.\n"); print("\tRES\tHexadecimal Resolution in bits. For example, 18. DEFAULT: 18\n"); print("\n"); print("\tHELP\tType: mif_hex2dec.pl HELP, to display information about this script.\n"); print("\n"); return; } sub print_help { print("\nMIF_HEX2DEC.PL HELP \n"); print("\n"); print("This utility is intended to convert a Xilinx Memory Initialization \n"); print("File (.MIF) to a Xilinx Coefficient File (.COE) file. This script \n"); print("basically converts base 2 hexadecimal values to base 10 integers. \n"); print("This script defaults to 18-bit resolution for the hexadecimal-to-decimal \n"); print("conversion. \n"); print("\n"); return; }