Fixed exe2memfile.pl bug

This commit is contained in:
David Harris 2021-12-27 00:44:18 +00:00
parent 69243f41ad
commit 67bbb03897

View File

@ -65,51 +65,65 @@ for(my $i=0; $i<=$#ARGV; $i++) {
if (/Disassembly of section .data:/) { $mode = 1;} if (/Disassembly of section .data:/) { $mode = 1;}
} elsif ($mode == 1) { # Parse data segment } elsif ($mode == 1) { # Parse data segment
# if (/^\s*(\S\S\S\S\S\S\S\S):\s+(.*)/) { # changed to \t 30 Oct 2021 dmh to fix parsing issue in d_fmadd_b17 # if (/^\s*(\S\S\S\S\S\S\S\S):\s+(.*)/) { # changed to \t 30 Oct 2021 dmh to fix parsing issue in d_fmadd_b17
if (/^\s*(\S\S\S\S\S\S\S\S):\t+(.*)/) { if (/^\s*(\S\S\S\S\S\S\S\S):\s+(.*)/) {
$address = &fixadr($1); $address = &fixadr($1);
# print "addresss $address maxaddress $maxaddress\n"; # print "addresss $address maxaddress $maxaddress\n";
if ($address > $maxaddress) { $maxaddress = $address; } if ($address > $maxaddress) { $maxaddress = $address; }
#print "test $address $1 $2\n";
my $lineorig = $2;
my $line = $2; my $line = $2;
# strip off leading 0x
$line =~ s/^0x//;
# merge chunks with spaces # merge chunks with spaces
$line =~ s/(\S)\s(\S)/$1$2/g; $line =~ s/(\S)\s(\S)/$1$2/g;
my $linemerge = $line;
# strip off comments # strip off comments
$line =~ /^(\S*)/; $line =~ /^(\S*)/;
$payload = $1; $payload = $1;
# if ($address >= 17520 && $address <= 17552) { # was 12304
# print "Address: $address\n orig: $lineorig \n merge: $linemerge \n line: $line \n payload: $payload\n";
# }
&emitData($address, $payload); &emitData($address, $payload);
} }
if (/Disassembly of section .riscv.attributes:/) { $mode = 2; } if (/Disassembly of section .riscv.attributes:/) { $mode = 2; }
} }
} }
close(FILE); close(FILE);
# print("maxaddress: $maxaddress\n");
$maxaddress += 32; # pad some zeros at the end $maxaddress += 32; # pad some zeros at the end
# print("maxaddress: $maxaddress\n");
# print to memory file # print to memory file
if ($fname =~ /rv32/) { if ($fname =~ /rv32/) {
open(MEMFILE, ">$memfile") || die("Can't write $memfile"); open(MEMFILE, ">$memfile") || die("Can't write $memfile");
for (my $i=0; $i<= $maxaddress; $i = $i + 4) { for (my $i=0; $i<= $maxaddress; $i = $i + 4) {
for ($j=3; $j>=0; $j--) { for ($j=3; $j>=0; $j--) {
if (defined($memfilebytes[$i+$j])) { if (defined($memfilebytes[$i+$j])) {
print MEMFILE "$memfilebytes[$i+$j]"; print MEMFILE "$memfilebytes[$i+$j]";
} else { } else {
print MEMFILE "00"; print MEMFILE "00";
}
} }
print MEMFILE "\n"; }
} print MEMFILE "\n";
close(MEMFILE); }
close(MEMFILE);
} else { } else {
open(MEMFILE, ">$memfile") || die("Can't write $memfile"); open(MEMFILE, ">$memfile") || die("Can't write $memfile");
for (my $i=0; $i<= $maxaddress; $i = $i + 8) { for (my $i=0; $i<= $maxaddress; $i = $i + 8) {
for ($j=7; $j>=0; $j--) { for ($j=7; $j>=0; $j--) {
if (defined($memfilebytes[$i+$j])) { my $loc = $i+$j;
print MEMFILE "$memfilebytes[$i+$j]"; # if ($loc >= 17520 && $loc <= 17552) {
} else { # print "loc: $loc val $memfilebytes[$loc]\n";
print MEMFILE "00"; # }
} if (defined($memfilebytes[$loc])) {
print MEMFILE "$memfilebytes[$loc]";
} else {
print MEMFILE "00";
}
}
print MEMFILE "\n";
} }
print MEMFILE "\n"; close(MEMFILE);
}
close(MEMFILE);
} }
} }
} }
@ -121,7 +135,9 @@ sub emitData {
my $address = shift; my $address = shift;
my $payload = shift; my $payload = shift;
# print("Emitting data. address = $address payload = $payload\n"); # if ($address > 17520 && $address < 17552) { # was 12304
# print("Emitting data. address = $address payload = $payload\n");
# }
my $len = length($payload); my $len = length($payload);
if ($len <= 8) { if ($len <= 8) {
@ -130,6 +146,9 @@ sub emitData {
my $adr = $address+$i; my $adr = $address+$i;
my $b = substr($payload, $len-2-2*$i, 2); my $b = substr($payload, $len-2-2*$i, 2);
$memfilebytes[$adr] = $b; $memfilebytes[$adr] = $b;
# if ($address >= 17520 && $address <= 17552) {
# print(" Wrote $b to $adr\n");
# }
# print(" $adr $b\n"); # print(" $adr $b\n");
} }
} elsif ($len == 12) { } elsif ($len == 12) {