| 1 |
4 |
cutullus |
#!/usr/bin/perl
|
| 2 |
|
|
############################################################
|
| 3 |
|
|
# openfire_util
|
| 4 |
|
|
#
|
| 5 |
|
|
# Provides compilation functions for OpenFire.
|
| 6 |
|
|
#
|
| 7 |
|
|
# Stephen Douglas Craven
|
| 8 |
|
|
# 1/17/2006
|
| 9 |
|
|
# Configurable Computing Lab
|
| 10 |
|
|
# Virginia Tech
|
| 11 |
|
|
############################################################
|
| 12 |
|
|
|
| 13 |
|
|
# Globals
|
| 14 |
|
|
use vars qw/ %opt /;
|
| 15 |
|
|
|
| 16 |
|
|
# Command line options processing
|
| 17 |
|
|
sub init()
|
| 18 |
|
|
{
|
| 19 |
|
|
use Getopt::Std;
|
| 20 |
|
|
my $opt_string = 'hcmd:f:a:p:n:';
|
| 21 |
|
|
getopts( "$opt_string", \%opt ) or usage();
|
| 22 |
|
|
usage() if ($opt{h} or !$opt{f} or ($opt{d} and !$opt{a}) or $opt{C});
|
| 23 |
|
|
}
|
| 24 |
|
|
|
| 25 |
|
|
# Message about this program and how to use it
|
| 26 |
|
|
sub usage()
|
| 27 |
|
|
{
|
| 28 |
|
|
print STDERR << "EOF";
|
| 29 |
|
|
|
| 30 |
|
|
This program compiles a MicroBlze C program and converts the resulting
|
| 31 |
|
|
ELF file into a memory image (.ROM file) for use by openfire_sim and
|
| 32 |
|
|
verilog simlulators.
|
| 33 |
|
|
|
| 34 |
|
|
NOTE: XMD must be installed and in the user's path.
|
| 35 |
|
|
|
| 36 |
|
|
usage: $0 [-h] -f file <-d data_file address> -p <XPS project directory>
|
| 37 |
|
|
|
| 38 |
|
|
-h : this (help) message
|
| 39 |
|
|
-d data : optional data file -- MUST be paired with -a flag
|
| 40 |
|
|
-a address : optional hex address (0x####) for data file
|
| 41 |
|
|
-f file : MicroBlaze C file
|
| 42 |
|
|
-c : create a rom file (.rom) for openfire_sim and verilog sim
|
| 43 |
|
|
-p directory : XPS project directory: make -f system.make libs must be already run!
|
| 44 |
|
|
-m : use hardware multiplier
|
| 45 |
|
|
-n master_name : name of master MicroBlaze (default: microblaze_0)
|
| 46 |
|
|
|
| 47 |
|
|
example: $0 -f file.c -d data.dat -a 0x1234
|
| 48 |
|
|
|
| 49 |
|
|
EOF
|
| 50 |
|
|
exit;
|
| 51 |
|
|
}
|
| 52 |
|
|
|
| 53 |
|
|
init();
|
| 54 |
|
|
|
| 55 |
|
|
# Fill in default values
|
| 56 |
|
|
if($opt{n}) {
|
| 57 |
|
|
$microblaze_name = $opt{n};
|
| 58 |
|
|
} else {
|
| 59 |
|
|
$microblaze_name = microblaze_0;
|
| 60 |
|
|
}
|
| 61 |
|
|
|
| 62 |
|
|
# Parse path to file to get filename
|
| 63 |
|
|
$_ = $opt{f};
|
| 64 |
|
|
@path = split(/\//);
|
| 65 |
|
|
@htap = reverse(@path);
|
| 66 |
|
|
$_ = $htap[0];
|
| 67 |
|
|
($filename, $extension) = split(/\./);
|
| 68 |
|
|
# Make sure its a C file first
|
| 69 |
|
|
usage() if ($extension ne "c");
|
| 70 |
|
|
|
| 71 |
|
|
# If file doesn't exist, raise error
|
| 72 |
|
|
unless (-e $opt{f}) {
|
| 73 |
|
|
print STDERR $opt{f}." NOT Found!\n";
|
| 74 |
|
|
exit;
|
| 75 |
|
|
}
|
| 76 |
|
|
|
| 77 |
|
|
# Create GCC command to compile C program
|
| 78 |
|
|
# Requires that libraries are previously compiled
|
| 79 |
|
|
$gcc_command = "mb-gcc ".$opt{f}." -O2 -o of_executable.elf \\\n";
|
| 80 |
|
|
$gcc_command = $gcc_command." -I".$opt{p}."/".$microblaze_name."/include/ -I".$opt{p}."/code/ \\\n";
|
| 81 |
|
|
# If we have a HW multiplier, use it
|
| 82 |
|
|
if($opt{m}) {
|
| 83 |
|
|
$gcc_command = $gcc_command." -mno-xl-soft-mul \\\n";
|
| 84 |
|
|
}
|
| 85 |
|
|
$gcc_command = $gcc_command." -L".$opt{p}."/".$microblaze_name."/lib/ -xl-mode-executable";
|
| 86 |
|
|
|
| 87 |
|
|
print $gcc_command."\n";
|
| 88 |
|
|
# Compile code
|
| 89 |
|
|
system($gcc_command);
|
| 90 |
|
|
|
| 91 |
|
|
# Make sure compilation succeeded
|
| 92 |
|
|
unless (-e "of_executable.elf") {
|
| 93 |
|
|
print STDERR "of_executable.elf NOT Found! Compilation Failed!\n";
|
| 94 |
|
|
exit;
|
| 95 |
|
|
}
|
| 96 |
|
|
|
| 97 |
|
|
# Stop here, unless user wants a ROM file as well for simulation
|
| 98 |
|
|
unless($opt{c}) {exit(0);}
|
| 99 |
|
|
|
| 100 |
|
|
####################################################################################
|
| 101 |
|
|
# A ROM file is a simple file showing the hex value of each memory location on
|
| 102 |
|
|
# a separate line. This is useful for Verilog simulators, which can read
|
| 103 |
|
|
# this format with a $readmemh. The openfire_sim C simulator also reads
|
| 104 |
|
|
# this format.
|
| 105 |
|
|
#
|
| 106 |
|
|
# Sample ROM file:
|
| 107 |
|
|
# A123B456
|
| 108 |
|
|
# C321D654
|
| 109 |
|
|
# and so on
|
| 110 |
|
|
####################################################################################
|
| 111 |
|
|
|
| 112 |
|
|
# If the user wants to add data to the ROM file, examine the file to determine size
|
| 113 |
|
|
if ($opt{d}) {
|
| 114 |
|
|
if (-e $opt{d})
|
| 115 |
|
|
{
|
| 116 |
|
|
( $dev, $ino, $mode, $nlink,
|
| 117 |
|
|
$uid, $gid, $rdev, $size,
|
| 118 |
|
|
$atime, $mtime, $ctime,
|
| 119 |
|
|
$blksize, $blocks ) = stat($opt{d});
|
| 120 |
|
|
} else {
|
| 121 |
|
|
print STDERR "File ".$opt{d}." NOT Found!\n";
|
| 122 |
|
|
exit;
|
| 123 |
|
|
}
|
| 124 |
|
|
}
|
| 125 |
|
|
|
| 126 |
|
|
# We need the maximum memory size, so convert provided address from hex to decimal
|
| 127 |
|
|
# and add the size of the data file (found above) to that
|
| 128 |
|
|
if ($opt{a}) {
|
| 129 |
|
|
$max_mem = oct($opt{a}) + $size + 10;
|
| 130 |
|
|
} else { # no address / data file provided; base size of ROM file on executable
|
| 131 |
|
|
( $dev, $ino, $mode, $nlink,
|
| 132 |
|
|
$uid, $gid, $rdev, $size,
|
| 133 |
|
|
$atime, $mtime, $ctime,
|
| 134 |
|
|
$blksize, $blocks ) = stat("of_executable.elf");
|
| 135 |
|
|
$max_mem = $size/2; # guestimate on program size based on ELF size
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
####################################################################################
|
| 139 |
|
|
# The ROM file is created by loading the ELF file into the XMD MicroBlaze simulator.
|
| 140 |
|
|
# The memory of the simulator is then dumped into a text file. This is sort of
|
| 141 |
|
|
# a hack, but it is much easier than parsing the ELF file myself -- I am certain
|
| 142 |
|
|
# not to make mistakes about where text and data sections go.
|
| 143 |
|
|
####################################################################################
|
| 144 |
|
|
# Create a TCL script for XMD
|
| 145 |
|
|
open(tcl_file, ">tmp666.tcl");
|
| 146 |
|
|
print tcl_file "xconnect mb sim\n";
|
| 147 |
|
|
print tcl_file "xdownload 0 of_executable.elf\n";
|
| 148 |
|
|
print tcl_file "xdownload 0 -data ".$opt{d}." ".$opt{a}."\n" if $opt{d};
|
| 149 |
|
|
print tcl_file "set hope [xrmem 0 0 ".$max_mem."]\n";
|
| 150 |
|
|
print tcl_file "puts \$hope\n";
|
| 151 |
|
|
close(tcl_file);
|
| 152 |
|
|
|
| 153 |
|
|
# Execute the TCL script, capturing the output
|
| 154 |
|
|
$xmd_cmd = "xmd -tcl tmp666.tcl";
|
| 155 |
|
|
$xmd_output = `$xmd_cmd`;
|
| 156 |
|
|
|
| 157 |
|
|
# Parse the output to just capture the memory data
|
| 158 |
|
|
##################################################################
|
| 159 |
|
|
# WARNING! This is likely to change with different XMD versions! #
|
| 160 |
|
|
##################################################################
|
| 161 |
|
|
$flag = 0;
|
| 162 |
|
|
foreach $_ (split(/\n/, $xmd_output)){
|
| 163 |
|
|
if ($flag){
|
| 164 |
|
|
@output = split(/ /, $_);
|
| 165 |
|
|
}
|
| 166 |
|
|
if ( $_ =~/Setting PC/) {$flag = 1;}
|
| 167 |
|
|
}
|
| 168 |
|
|
|
| 169 |
|
|
# Open a file to write results
|
| 170 |
|
|
binmode rom_file;
|
| 171 |
|
|
open(rom_file, ">$filename.rom");
|
| 172 |
|
|
|
| 173 |
|
|
# Print memory contents in hex
|
| 174 |
|
|
$count = 0;
|
| 175 |
|
|
foreach $number (@output) {
|
| 176 |
|
|
if ($number =~/\d/) {
|
| 177 |
|
|
$hex[$count] = sprintf("%X", $number);
|
| 178 |
|
|
if($number < 16) {
|
| 179 |
|
|
$hex[$count] = "0".$hex[$count];
|
| 180 |
|
|
}
|
| 181 |
|
|
$count++;
|
| 182 |
|
|
}
|
| 183 |
|
|
if ($count == 4) {
|
| 184 |
|
|
$count = 0;
|
| 185 |
|
|
printf rom_file $hex[0].$hex[1].$hex[2].$hex[3]."\n";
|
| 186 |
|
|
}
|
| 187 |
|
|
}
|
| 188 |
|
|
close(rom_file);
|
| 189 |
|
|
|
| 190 |
|
|
# Remove the TCL script we created
|
| 191 |
|
|
system("rm tmp666.tcl");
|
| 192 |
|
|
exit;
|
| 193 |
|
|
|
| 194 |
|
|
# Stuff below is old for making COE coregen files
|
| 195 |
|
|
# Needs rework
|
| 196 |
|
|
if($opt{c} | $opt{C}) {
|
| 197 |
|
|
open(coe_file, ">$filename.coe");
|
| 198 |
|
|
open(rom_file, "$filename.rom");
|
| 199 |
|
|
print coe_file "memory_initialization_radix=16;\n";
|
| 200 |
|
|
$reset = 1;
|
| 201 |
|
|
while(<rom_file>){
|
| 202 |
|
|
if ($reset > 0) {
|
| 203 |
|
|
chomp($_);
|
| 204 |
|
|
print coe_file "memory_initialization_vector= $_";
|
| 205 |
|
|
}
|
| 206 |
|
|
else {
|
| 207 |
|
|
chomp($_);
|
| 208 |
|
|
print coe_file ", ".$_;
|
| 209 |
|
|
}
|
| 210 |
|
|
$reset = 0;
|
| 211 |
|
|
}
|
| 212 |
|
|
print coe_file ";\n";
|
| 213 |
|
|
close(coe_file);
|
| 214 |
|
|
close(rom_file);
|
| 215 |
|
|
}
|
| 216 |
|
|
|