1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Carp; |
7 |
use Data::Dump qw/dump/; |
8 |
|
9 |
use SDL::App; |
10 |
use SDL::Rect; |
11 |
use SDL::Color; |
12 |
|
13 |
use lib './lib'; |
14 |
use Orao; |
15 |
|
16 |
my $debug = 0; |
17 |
my $scale = 1; |
18 |
my $show_mem = 1; |
19 |
my $run_for = 1; |
20 |
my $mem_dump = 'mem.dump'; |
21 |
my $trace = 0; |
22 |
|
23 |
|
24 |
my $app = SDL::App->new( |
25 |
-width => 256 * $scale + ( $show_mem ? 256 : 0 ), |
26 |
-height => 256 * $scale, |
27 |
-depth => 16, |
28 |
); |
29 |
|
30 |
$app->grab_input( 0 ); |
31 |
|
32 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
33 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
34 |
|
35 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
36 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
37 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
38 |
|
39 |
sub p { |
40 |
my ($x,$y,$w) = (@_); |
41 |
|
42 |
warn "p($x,$y,$w)\n" if $debug; |
43 |
|
44 |
my $rect = SDL::Rect->new( |
45 |
-height => $scale, |
46 |
-width => $scale, |
47 |
-x => $x * $scale, |
48 |
-y => $y * $scale, |
49 |
); |
50 |
|
51 |
$app->fill( $rect, $w ? $white : $black ); |
52 |
$app->update( $rect ); |
53 |
} |
54 |
|
55 |
my $stat; |
56 |
|
57 |
sub mem_xy { |
58 |
my $offset = shift; |
59 |
my $x = $offset & 0xff; |
60 |
$x += 256 * $scale; |
61 |
my $y = $offset >> 8; |
62 |
return ($x,$y); |
63 |
} |
64 |
|
65 |
=head2 vram |
66 |
|
67 |
$orao->vram( $offset, $byte ); |
68 |
|
69 |
=cut |
70 |
|
71 |
sub vram { |
72 |
my ( $offset, $byte ) = @_; |
73 |
my $x = ( $offset % 32 ) << 3; |
74 |
my $y = $offset >> 5; |
75 |
my $mask = 1; |
76 |
|
77 |
printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $trace; |
78 |
|
79 |
foreach ( 0 .. 7 ) { |
80 |
p($x + $_, $y, $byte & $mask ); |
81 |
$mask = $mask << 1; |
82 |
} |
83 |
} |
84 |
|
85 |
my $orao = new Orao({ |
86 |
mmu => sub { |
87 |
my ( $offset, $what, $value ) = @_; |
88 |
my ( $x, $y ) = mem_xy( $offset ); |
89 |
printf "## mem %04x %s = %02x %02d*%02d\n", $offset, $what, $value, $x, $y if $trace; |
90 |
|
91 |
if ( $offset >= 0x6000 && $offset < 0x8000 ) { |
92 |
vram( $offset - 0x6000 , $value ); |
93 |
} |
94 |
|
95 |
my ( $r,$g,$b ) = ( 0,0,0 ); |
96 |
|
97 |
if ( $what eq 'write' ) { |
98 |
$r = $value; |
99 |
if ( $offset > 0xafff ) { |
100 |
printf "access to %04x above affff aborting\n", $offset; |
101 |
return -1; |
102 |
} |
103 |
if ( $offset == 0x8800 ) { |
104 |
printf "sound ignored: %x\n", $value; |
105 |
} |
106 |
} elsif ( $what eq 'read' ) { |
107 |
$g = $value; |
108 |
} else { |
109 |
$b = $value; |
110 |
} |
111 |
|
112 |
my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b ); |
113 |
$app->pixel( $x, $y, $col ); |
114 |
|
115 |
$stat->{$what}++; |
116 |
if ( $stat->{$what} % 1000 == 0 ) { |
117 |
$app->sync; |
118 |
} |
119 |
}, |
120 |
}); |
121 |
|
122 |
my ($pc, $a, $x, $y, $s, $p) = (0) x 6; |
123 |
#$orao->load_rom('dump/basic.dmp', -2); |
124 |
|
125 |
#$pc = 0xDD11; # BC |
126 |
#$pc = 0xC274; # MC |
127 |
|
128 |
$orao->load_rom('makewav/SCRINV.BIN', 0x1000); |
129 |
$pc = 0x1000; |
130 |
|
131 |
#$orao->load_oraoemu('dump/orao-64k-1.2.dmp'); |
132 |
#$pc = 0xe5b7; |
133 |
|
134 |
# memory dump |
135 |
my $rect = SDL::Rect->new( |
136 |
-height => 256, |
137 |
-width => 256, |
138 |
-x => 256 * $scale, |
139 |
-y => 0, |
140 |
); |
141 |
|
142 |
$app->fill( $rect, $white ); |
143 |
$app->update( $rect ); |
144 |
|
145 |
warn "rendering memory map\n"; |
146 |
|
147 |
my @mmap = ( |
148 |
0x0000, 0x03FF, 'nulti blok', |
149 |
0x0400, 0x5FFF, 'korisnièki RAM (23K)', |
150 |
0x6000, 0x7FFF, 'video RAM', |
151 |
0x8000, 0x9FFF, 'sistemske lokacije', |
152 |
0xA000, 0xAFFF, 'ekstenzija', |
153 |
0xB000, 0xBFFF, 'DOS', |
154 |
0xC000, 0xDFFF, 'BASIC ROM', |
155 |
0xE000, 0xFFFF, 'sistemski ROM', |
156 |
); |
157 |
|
158 |
foreach my $i ( 0 .. $#mmap / 3 ) { |
159 |
my $o = $i * 3; |
160 |
my ( $from, $to, $desc ) = @mmap[$o,$o+1,$o+2]; |
161 |
printf "%04x - %04x - %s\n", $from, $to, $desc; |
162 |
for my $a ( $from .. $to ) { |
163 |
$orao->read_8( $a ); |
164 |
} |
165 |
$app->sync; |
166 |
} |
167 |
|
168 |
my $last; |
169 |
|
170 |
sub opcode_cb { |
171 |
my $a = shift || confess "no pc?"; |
172 |
$app->sync; |
173 |
while ( my @v = $orao->prompt( $a, $last ) ) { |
174 |
my $c = shift @v; |
175 |
my $v = shift @v; |
176 |
$v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/; |
177 |
printf "## [%s] %s\n", ($v || 'undef'), join(",",@v) if $debug; |
178 |
@v = map { hex($_) } @v; |
179 |
if ( $c =~ m/^[qx]/i ) { |
180 |
exit; |
181 |
} elsif ( $c eq '?' ) { |
182 |
warn <<__USAGE__; |
183 |
uage: |
184 |
x|q\t\texit |
185 |
e 6000 6010\tdump memory, +/- to walk forward/backward |
186 |
m 1000 ff 00\tput ff 00 on 1000 |
187 |
j|u 1000\t\tjump (change pc) |
188 |
r 42\t\trun 42 instruction opcodes |
189 |
__USAGE__ |
190 |
} elsif ( $c =~ m/^e/i ) { |
191 |
$a ||= $v; |
192 |
my $to = shift @v; |
193 |
$to = $a + 32 if ( ! $to || $to <= $a ); |
194 |
my $lines = int( ($to - $a - 8) / 8 ); |
195 |
printf "## m %04x %04x lines: %d\n", $a, $to, $lines; |
196 |
while ( $lines ) { |
197 |
print $orao->hexdump( $a ); |
198 |
$a += 8; |
199 |
$lines--; |
200 |
} |
201 |
$last = '+'; |
202 |
} elsif ( $c =~ m/^\+/ ) { |
203 |
$a += 8; |
204 |
} elsif ( $c =~ m/^\-/ ) { |
205 |
$a -= 8; |
206 |
} elsif ( $c =~ m/^m/i ) { |
207 |
$a = $v; |
208 |
$orao->poke_code( $a, @v ); |
209 |
printf "poke %d bytes at %04x\n", $#v + 1, $a; |
210 |
} elsif ( $c =~ m/^l/i ) { |
211 |
my $to = shift @v || 0x1000; |
212 |
$a = $to; |
213 |
$orao->load_oraoemu( $v, $a ); |
214 |
} elsif ( $c =~ m/^s/i ) { |
215 |
$orao->save_dump( $v || $mem_dump, @v ); |
216 |
} elsif ( $c =~ m/^r/i ) { |
217 |
$run_for = $v || 1; |
218 |
print "run_for $run_for instructions\n"; |
219 |
last; |
220 |
} elsif ( $c =~ m/^(u|j)/ ) { |
221 |
my $to = $v || $a; |
222 |
printf "set pc to %04x\n", $to; |
223 |
$orao->set_pc( $to ); |
224 |
$pc = $to; # remember for restart |
225 |
$run_for = 1; |
226 |
$last = 'r 1'; |
227 |
last; |
228 |
} elsif ( $c =~ m/^t/ ) { |
229 |
$trace = not $trace; |
230 |
print "trace ", $trace ? 'on' : 'off', "\n"; |
231 |
} else { |
232 |
warn "# ignore $c\n"; |
233 |
} |
234 |
} |
235 |
} |
236 |
|
237 |
sub restart { |
238 |
printf "starting emulation -- pc: %04x a:%d x:%d y:%d s:%d p:%d for %d instructions\n", $pc, $a, $x, $y, $s, $p, $run_for; |
239 |
|
240 |
$orao->set_pc( $pc ); |
241 |
$orao->set_a( $a ); |
242 |
$orao->set_x( $x ); |
243 |
$orao->set_y( $y ); |
244 |
$orao->set_s( $s ); |
245 |
$orao->set_p( $p ); |
246 |
} |
247 |
|
248 |
restart; |
249 |
|
250 |
while ( 1 ) { |
251 |
eval { |
252 |
$orao->run( $run_for , sub { |
253 |
my ($pc, $inst, $a, $x, $y, $s, $p) = @_; |
254 |
$run_for--; |
255 |
|
256 |
$app->pixel( mem_xy( $pc ), $white ); |
257 |
$app->sync if ( $run_for % 500 == 0 ); |
258 |
|
259 |
printf " PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x [%d]\n", $pc, $a,$p,$x,$y,$s, $run_for if ( $trace || $run_for % 1000 == 0 ); |
260 |
opcode_cb( $pc ) if ( $run_for <= 1 ); |
261 |
|
262 |
return 1; |
263 |
} ); |
264 |
}; |
265 |
|
266 |
if ( $@ ) { |
267 |
print "restart after $@\n"; |
268 |
restart(); |
269 |
$run_for = 1; |
270 |
} |
271 |
|
272 |
$run_for ||= 1; |
273 |
} |