aboutsummaryrefslogtreecommitdiff
path: root/mkrelocxex.pl
blob: a871cd52052a3b293e71fa2d480398765c69e332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl -w

# mkrelocxex prototype in perl (will rewrite in C).
# this version only supports init addresses, not run addresses.

use bytes;

die "usage: $0 <lo> <hi> <out>\n" unless @ARGV == 3;

open $lo,  "<", $ARGV[0] or die "$ARGV[0]: $!\n";
open $hi,  "<", $ARGV[1] or die "$ARGV[1]: $!\n";
open $out, ">", $ARGV[2] or die "$ARGV[2]: $!\n";

sub read_word {
	my $fh = shift;
	my ($a, $b);

	read($fh, $a, 1) || return undef;
	read($fh, $b, 1) || return undef;

	return ord($a) | (ord($b) << 8);
}

sub read_header {
	my ($start, $end);
	my ($a, $b);
	my $fh = shift;
	$start = read_word($fh) || return undef;
	if($start == 0xffff) {
		$start = read_word($fh) || return undef;
	}
	$end = read_word($fh) || return undef;
	return ($start, $end);
}

sub read_seg {
	my $fh = shift;
	my ($start, $end) = @_;
	my @bytes;
	for($start..($end)) {
		my $b;
		read($fh, $b, 1) || die "early EOF, WTF?\n";
		push @bytes, ord($b);
	}
	return @bytes;
}

sub print_table {
	my ($name, $t) = @_;
	print "\n$name byte table:\n";
	my $i = 0;
	for(@$t) {
		printf "\$%04x ", $_;
		print "\n" if $i && ($i & 10 == 0);
	}
	print "\n";
}

($start, $end) = read_header($lo);
($hi_start, $hi_end) = read_header($hi);

printf("lo start/end: \$%04x/\$%04x\n", $start, $end);
printf("hi start/end: \$%04x/\$%04x\n", $hi_start, $hi_end);

if(($start % 0x100) || ($hi_start % 0x100)) {
	die "starting address not on a page boundary\n";
}

if($start != ($hi_start - 0x100)) {
	die "starting addresses not one page apart\n";
}

if(($hi_start != ($start + 0x0100)) || ($hi_end != ($end + 0x0100))) {
	die "mismatched segment lengths\n";
}

@bytes = read_seg($lo, $start, $end);
@hi_bytes = read_seg($hi, $hi_start, $hi_end);

for($i = 0; $i < @bytes; $i++) {
	my ($a, $b) = ($bytes[$i], $hi_bytes[$i]);
	next if $a == $b;
	if($b == ($a + 1)) {
		push @hi_table, ($i + $start);
	} else {
		die "invalid difference (not 0 or 1)\n";
	}
}

push(@hi_table, 0);

print_table("hi", \@hi_table);
print "table size: " . @hi_table . " bytes\n\n";

($istart, $iend) = read_header($lo);

#warn "istart $istart  iend $iend\n";
if($istart == 0x2e2 && $iend == 0x2e3) {
	$init = read_word($lo);
}

# OK, make the output file now...
print $out chr(0xff);
print $out chr(0xff);
#warn $start;
print $out chr($start & 0xff);
print $out chr($start >> 8);
print $out chr($end & 0xff);
print $out chr($end >> 8);
print $out chr($_) for @bytes;

open $r, "<", "reloc.xex" || die $!;
(undef) = read_word($r);
$rstart = read_word($r);
$rend   = read_word($r);
$rlen   = $rend - $rstart + 1;
read $r, $rcode, $rlen;
close $r;

# 8-byte address table
$rcode .= chr($start & 0xff);
$rcode .= chr($start >> 8);
$rcode .= chr($end & 0xff);
$rcode .= chr($end >> 8);
$rcode .= chr(0);
$rcode .= chr(0);
$rcode .= chr($init & 0xff);
$rcode .= chr($init >> 8);

for(@hi_table) {
	$rcode .= chr($_ & 0xff);
	$rcode .= chr($_ >> 8);
}

$rend = $rstart + length($rcode) - 1;

#warn "$rstart $rend " . length($rcode);

# don't really need a ffff header, makes it easier to read hexdumps.
print $out chr(0xff);
print $out chr(0xff);

# segment start/end
print $out chr($rstart & 0xff);
print $out chr($rstart >> 8);
print $out chr($rend & 0xff);
print $out chr($rend >> 8);

# segment contents (code + tables)
print $out $rcode;

# init address
print $out chr(0xe2);
print $out chr(0x02);
print $out chr(0xe3);
print $out chr(0x02);
print $out chr($rstart & 0xff);
print $out chr($rstart >> 8);

close $out;