3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
use Carp; |
use Carp qw/confess/; |
7 |
use lib './lib'; |
use lib './lib'; |
8 |
#use Time::HiRes qw(time); |
#use Time::HiRes qw(time); |
9 |
use File::Slurp; |
use File::Slurp; |
10 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
11 |
|
use List::Util qw/first/; |
12 |
use M6502; |
use M6502; |
13 |
|
|
14 |
use base qw(Class::Accessor M6502 Screen); |
use base qw(Class::Accessor M6502 Screen Prefs); |
15 |
__PACKAGE__->mk_accessors(qw(debug trace run_for mem_dump trace)); |
__PACKAGE__->mk_accessors(qw(run_for)); |
16 |
|
|
17 |
=head1 NAME |
=head1 NAME |
18 |
|
|
32 |
|
|
33 |
=cut |
=cut |
34 |
|
|
35 |
|
my @kbd_ports = ( |
36 |
|
0x87FC,0x87FD,0x87FA,0x87FB,0x87F6,0x87F7, |
37 |
|
0x87EE,0x87EF,0x87DE,0x87DF,0x87BE,0x87BF, |
38 |
|
0x877E,0x877F,0x86FE,0x86FF,0x85FE,0x85FF, |
39 |
|
0x83FE,0x83FF, |
40 |
|
); |
41 |
|
|
42 |
=head2 init |
=head2 init |
43 |
|
|
44 |
Start emulator |
Start emulator, open L<Screen>, load initial ROM images, and render memory |
45 |
|
|
46 |
=cut |
=cut |
47 |
|
|
54 |
warn "Orao calling upstream init\n"; |
warn "Orao calling upstream init\n"; |
55 |
$self->SUPER::init( $self, @_ ); |
$self->SUPER::init( $self, @_ ); |
56 |
|
|
57 |
warn "Orao $Orao::VERSION emulation starting\n", dump( $self ); |
warn "Orao $Orao::VERSION emulation starting\n"; |
58 |
|
|
59 |
$self->open_screen; |
$self->open_screen; |
60 |
$self->load_rom({ |
$self->load_rom({ |
61 |
0x1000 => 'dump/SCRINV.BIN', |
0x1000 => 'dump/SCRINV.BIN', |
62 |
|
# should be 0x6000, but oraoemu has 2 byte prefix |
63 |
|
0x5FFE => 'dump/screen.dmp', |
64 |
0xC000 => 'rom/BAS12.ROM', |
0xC000 => 'rom/BAS12.ROM', |
65 |
0xE000 => 'rom/CRT12.ROM', |
0xE000 => 'rom/CRT12.ROM', |
66 |
}); |
}); |
67 |
|
|
68 |
$PC = 0xDD11; # BC |
# $PC = 0xDD11; # BC |
69 |
# $PC = 0xC274; # MC |
# $PC = 0xC274; # MC |
70 |
|
|
71 |
|
$PC = 0xff89; |
72 |
|
|
73 |
$orao = $self; |
$orao = $self; |
74 |
|
|
75 |
# $self->prompt( 0x1000 ); |
# $self->prompt( 0x1000 ); |
78 |
$self->trace( 0 ); |
$self->trace( 0 ); |
79 |
$self->debug( 0 ); |
$self->debug( 0 ); |
80 |
|
|
81 |
|
$self->render( @mem[ 0x6000 .. 0x7fff ] ); |
82 |
|
|
83 |
if ( $self->show_mem ) { |
if ( $self->show_mem ) { |
84 |
|
|
85 |
warn "rendering memory map\n"; |
warn "rendering memory map\n"; |
86 |
|
|
87 |
|
$self->render_mem( @mem ); |
88 |
|
|
89 |
my @mmap = ( |
my @mmap = ( |
90 |
0x0000, 0x03FF, 'nulti blok', |
0x0000, 0x03FF, 'nulti blok', |
91 |
0x0400, 0x5FFF, 'korisnički RAM (23K)', |
0x0400, 0x5FFF, 'korisnički RAM (23K)', |
97 |
0xE000, 0xFFFF, 'sistemski ROM', |
0xE000, 0xFFFF, 'sistemski ROM', |
98 |
); |
); |
99 |
|
|
|
foreach my $i ( 0 .. $#mmap / 3 ) { |
|
|
my $o = $i * 3; |
|
|
my ( $from, $to, $desc ) = @mmap[$o,$o+1,$o+2]; |
|
|
printf "%04x - %04x - %s\n", $from, $to, $desc; |
|
|
for my $a ( $from .. $to ) { |
|
|
if ( $a >= 0x6000 && $a < 0x8000 ) { |
|
|
my $b = $self->read( $a ); |
|
|
$self->vram( $a - 0x6000, $b ); |
|
|
} else { |
|
|
$self->read( $a ); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
100 |
} else { |
} else { |
101 |
|
|
102 |
warn "rendering video memory\n"; |
warn "rendering video memory\n"; |
103 |
for my $a ( 0x6000 .. 0x7fff ) { |
$self->render( @mem[ 0x6000 .. 0x7fff ] ); |
|
$self->vram( $a - 0x6000, $mem[$a] ); |
|
|
} |
|
104 |
|
|
105 |
} |
} |
106 |
$self->sync; |
$self->sync; |
135 |
} |
} |
136 |
} |
} |
137 |
|
|
138 |
|
# write chunk directly into memory, updateing vram if needed |
|
=head2 load_oraoemu |
|
|
|
|
|
=cut |
|
|
|
|
139 |
sub _write_chunk { |
sub _write_chunk { |
140 |
my $self = shift; |
my $self = shift; |
141 |
my ( $addr, $chunk ) = @_; |
my ( $addr, $chunk ) = @_; |
152 |
$t = $end if ( $end < $t ); |
$t = $end if ( $end < $t ); |
153 |
|
|
154 |
warn sprintf("refresh video ram %04x-%04x\n", $f, $t); |
warn sprintf("refresh video ram %04x-%04x\n", $f, $t); |
155 |
foreach my $a ( $f .. $t ) { |
# foreach my $a ( $f .. $t ) { |
156 |
$self->vram( $a - 0x6000 , $mem[ $a ] ); |
# $self->vram( $a - 0x6000 , $mem[ $a ] ); |
157 |
} |
# } |
158 |
|
$self->render( @mem[ 0x6000 .. 0x7fff ] ); |
159 |
|
$self->render_mem( @mem ) if $self->show_mem; |
160 |
} |
} |
161 |
|
|
162 |
|
=head2 load_oraoemu |
163 |
|
|
164 |
|
Load binary files, ROM images and Orao Emulator files |
165 |
|
|
166 |
|
$orao->load_oraoemu( '/path/to/file', 0x1000 ); |
167 |
|
|
168 |
|
Returns true on success. |
169 |
|
|
170 |
|
=cut |
171 |
|
|
172 |
sub load_oraoemu { |
sub load_oraoemu { |
173 |
my $self = shift; |
my $self = shift; |
174 |
my ( $path, $addr ) = @_; |
my ( $path, $addr ) = @_; |
175 |
|
|
176 |
|
if ( ! -e $path ) { |
177 |
|
warn "ERROR: file $path doesn't exist\n"; |
178 |
|
return; |
179 |
|
} |
180 |
|
|
181 |
my $size = -s $path || confess "no size for $path: $!"; |
my $size = -s $path || confess "no size for $path: $!"; |
182 |
|
|
183 |
my $buff = read_file( $path ); |
my $buff = read_file( $path ); |
186 |
$addr = 0; |
$addr = 0; |
187 |
warn sprintf "loading oraoemu 64k dump %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; |
warn sprintf "loading oraoemu 64k dump %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; |
188 |
$self->_write_chunk( $addr, substr($buff,2) ); |
$self->_write_chunk( $addr, substr($buff,2) ); |
189 |
return; |
return 1; |
190 |
} elsif ( $size == 32800 ) { |
} elsif ( $size == 32800 ) { |
191 |
$addr = 0; |
$addr = 0; |
192 |
warn sprintf "loading oraoemu 1.3 dump %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; |
warn sprintf "loading oraoemu 1.3 dump %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; |
193 |
$self->_write_chunk( $addr, substr($buff,0x20) ); |
$self->_write_chunk( $addr, substr($buff,0x20) ); |
194 |
return; |
return 1; |
195 |
} |
} |
196 |
printf "loading %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; |
printf "loading %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; |
197 |
return $self->_write_chunk( $addr, $buff ); |
$self->_write_chunk( $addr, $buff ); |
198 |
|
return 1; |
199 |
|
|
200 |
my $chunk; |
my $chunk; |
201 |
|
|
213 |
|
|
214 |
$self->_write_chunk( $addr, $chunk ); |
$self->_write_chunk( $addr, $chunk ); |
215 |
|
|
216 |
|
return 1; |
217 |
}; |
}; |
218 |
|
|
219 |
=head2 save_dump |
=head2 save_dump |
250 |
return sprintf(" %04x %s\n", $a, |
return sprintf(" %04x %s\n", $a, |
251 |
join(" ", |
join(" ", |
252 |
map { |
map { |
253 |
sprintf( "%02x", $_ ) |
if ( defined($_) ) { |
254 |
|
sprintf( "%02x", $_ ) |
255 |
|
} else { |
256 |
|
' ' |
257 |
|
} |
258 |
} @mem[ $a .. $a+8 ] |
} @mem[ $a .. $a+8 ] |
259 |
) |
) |
260 |
); |
); |
280 |
my ($addr) = @_; |
my ($addr) = @_; |
281 |
my $byte = $mem[$addr]; |
my $byte = $mem[$addr]; |
282 |
warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace; |
warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace; |
283 |
|
|
284 |
|
# keyboard |
285 |
|
|
286 |
|
if ( first { $addr == $_ } @kbd_ports ) { |
287 |
|
warn sprintf("keyboard port: %04x\n",$addr); |
288 |
|
} elsif ( $addr == 0x87fc ) { |
289 |
|
warn "0x87fc - arrows/back\n"; |
290 |
|
=for pascal |
291 |
|
if VKey=VK_RIGHT then Result:=16; |
292 |
|
if VKey=VK_DOWN then Result:=128; |
293 |
|
if VKey=VK_UP then Result:=192; |
294 |
|
if VKey=VK_LEFT then Result:=224; |
295 |
|
if Ord(KeyPressed)=VK_BACK then Result:=224; |
296 |
|
=cut |
297 |
|
} elsif ( $addr == 0x87fd ) { |
298 |
|
warn "0x87fd - enter\n"; |
299 |
|
=for pascal |
300 |
|
if KeyPressed=Chr(13) then begin |
301 |
|
Mem[$FC]:=13; |
302 |
|
Result:=0; |
303 |
|
end; |
304 |
|
=cut |
305 |
|
} elsif ( $addr == 0x87fa ) { |
306 |
|
warn "0x87fa = F1 - F4\n"; |
307 |
|
=for pascal |
308 |
|
if VKey=VK_F4 then Result:=16; |
309 |
|
if VKey=VK_F3 then Result:=128; |
310 |
|
if VKey=VK_F2 then Result:=192; |
311 |
|
if VKey=VK_F1 then Result:=224; |
312 |
|
=cut |
313 |
|
} elsif ( $addr == 0x87fb ) { |
314 |
|
warn "0x87fb\n"; |
315 |
|
=for pascal |
316 |
|
if KeyPressed=Chr(32) then Result:=32; |
317 |
|
if KeyPressed='"' then Result:=16; |
318 |
|
if KeyPressed='!' then Result:=16; |
319 |
|
if KeyPressed='$' then Result:=16; |
320 |
|
if KeyPressed='%' then Result:=16; |
321 |
|
if KeyPressed='&' then Result:=16; |
322 |
|
if KeyPressed='(' then Result:=16; |
323 |
|
if KeyPressed=')' then Result:=16; |
324 |
|
if KeyPressed='=' then Result:=16; |
325 |
|
if KeyPressed='#' then Result:=16; |
326 |
|
if KeyPressed='+' then Result:=16; |
327 |
|
if KeyPressed='*' then Result:=16; |
328 |
|
if KeyPressed='?' then Result:=16; |
329 |
|
if KeyPressed='<' then Result:=16; |
330 |
|
if KeyPressed='>' then Result:=16; |
331 |
|
if VKey=191 then Result:=16; |
332 |
|
=cut |
333 |
|
} |
334 |
|
|
335 |
$self->mmap_pixel( $addr, 0, $byte, 0 ); |
$self->mmap_pixel( $addr, 0, $byte, 0 ); |
336 |
return $byte; |
return $byte; |
337 |
} |
} |
358 |
} |
} |
359 |
|
|
360 |
if ( $addr > 0xafff ) { |
if ( $addr > 0xafff ) { |
361 |
confess sprintf "write access %04x > afff aborting\n", $self, $addr; |
warn sprintf "write access 0x%04x > 0xafff aborting\n", $addr; |
362 |
|
return; |
363 |
} |
} |
364 |
|
|
365 |
$self->mmap_pixel( $addr, $byte, 0, 0 ); |
$self->mmap_pixel( $addr, $byte, 0, 0 ); |
375 |
|
|
376 |
=head2 prompt |
=head2 prompt |
377 |
|
|
378 |
$orao->prompt( $address, $last_command ); |
my ( $entered_line, @p ) = $orao->prompt( $address, $last_command ); |
379 |
|
|
380 |
=cut |
=cut |
381 |
|
|
393 |
warn "## prompt got: $in\n" if $self->debug; |
warn "## prompt got: $in\n" if $self->debug; |
394 |
$in ||= $last; |
$in ||= $last; |
395 |
$last = $in; |
$last = $in; |
396 |
return split(/\s+/, $in) if $in; |
return ( $in, split(/\s+/, $in) ) if $in; |
397 |
} |
} |
398 |
|
|
399 |
=head2 cli |
=head2 cli |
402 |
|
|
403 |
=cut |
=cut |
404 |
|
|
405 |
|
my $show_R = 0; |
406 |
|
|
407 |
sub cli { |
sub cli { |
408 |
my $self = shift; |
my $self = shift; |
409 |
my $a = $PC || confess "no pc?"; |
my $a = $PC || confess "no pc?"; |
410 |
while ( my @v = $self->prompt( $a, $last ) ) { |
warn $self->dump_R() if $show_R; |
411 |
|
while ( my ($line, @v) = $self->prompt( $a, $last ) ) { |
412 |
my $c = shift @v; |
my $c = shift @v; |
413 |
|
next unless defined($c); |
414 |
my $v = shift @v; |
my $v = shift @v; |
415 |
$v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/; |
$v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/; |
416 |
@v = map { hex($_) } @v; |
@v = map { hex($_) } @v; |
432 |
d\t\tdebug [$d] |
d\t\tdebug [$d] |
433 |
|
|
434 |
__USAGE__ |
__USAGE__ |
435 |
warn sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S); |
warn $self->dump_R; |
436 |
} elsif ( $c =~ m/^e/i ) { |
} elsif ( $c =~ m/^e/i ) { |
437 |
$a = $v if defined($v); |
$a = $v if defined($v); |
438 |
my $to = shift @v; |
my $to = shift @v; |
439 |
$to = $a + 32 if ( ! $to || $to <= $a ); |
$to = $a + 32 if ( ! $to || $to <= $a ); |
440 |
my $lines = int( ($to - $a - 8) / 8 ); |
$to = 0xffff if ( $to > 0xffff ); |
441 |
printf "## m %04x %04x lines: %d\n", $a, $to, $lines; |
my $lines = int( ($to - $a + 8) / 8 ); |
442 |
while ( $lines ) { |
printf "## e %04x %04x (%d bytes) lines: %d\n", $a, $to, ($to-$a), $lines; |
443 |
|
while ( --$lines ) { |
444 |
print $self->hexdump( $a ); |
print $self->hexdump( $a ); |
445 |
$a += 8; |
$a += 8; |
|
$lines--; |
|
446 |
} |
} |
447 |
$last = '+'; |
$last = '+'; |
448 |
|
$show_R = 0; |
449 |
} elsif ( $c =~ m/^\+/ ) { |
} elsif ( $c =~ m/^\+/ ) { |
450 |
$a += 8; |
$a += 8; |
451 |
|
$show_R = 0; |
452 |
} elsif ( $c =~ m/^\-/ ) { |
} elsif ( $c =~ m/^\-/ ) { |
453 |
$a -= 8; |
$a -= 8; |
454 |
|
$show_R = 0; |
455 |
} elsif ( $c =~ m/^m/i ) { |
} elsif ( $c =~ m/^m/i ) { |
456 |
$a = $v; |
$a = $v if defined($v); |
457 |
$self->poke_code( $a, @v ); |
$self->poke_code( $a, @v ); |
458 |
printf "poke %d bytes at %04x\n", $#v + 1, $a; |
printf "poke %d bytes at %04x\n", $#v + 1, $a; |
459 |
$last = '+'; |
$last = '+'; |
460 |
|
$show_R = 0; |
461 |
} elsif ( $c =~ m/^l/i ) { |
} elsif ( $c =~ m/^l/i ) { |
462 |
my $to = shift @v || 0x1000; |
my $to = shift @v || 0x1000; |
463 |
$a = $to; |
$a = $to; |
469 |
} elsif ( $c =~ m/^r/i ) { |
} elsif ( $c =~ m/^r/i ) { |
470 |
$run_for = $v || 1; |
$run_for = $v || 1; |
471 |
print "run_for $run_for instructions\n"; |
print "run_for $run_for instructions\n"; |
472 |
|
$show_R = 1; |
473 |
last; |
last; |
474 |
} elsif ( $c =~ m/^(u|j)/ ) { |
} elsif ( $c =~ m/^(u|j)/ ) { |
475 |
my $to = $v || $a; |
my $to = $v || $a; |
476 |
printf "set pc to %04x\n", $to; |
printf "set pc to %04x\n", $to; |
477 |
$PC = $to; # remember for restart |
$PC = $to; # remember for restart |
478 |
$run_for = 1; |
$run_for = 1; |
479 |
$last = sprintf('m %04x', $to); |
$last = "r $run_for"; |
480 |
|
$show_R = 1; |
481 |
last; |
last; |
482 |
} elsif ( $c =~ m/^t/ ) { |
} elsif ( $c =~ m/^t/ ) { |
483 |
$self->trace( not $self->trace ); |
$self->trace( not $self->trace ); |
484 |
print "trace ", $self->trace ? 'on' : 'off', "\n"; |
print "trace ", $self->trace ? 'on' : 'off', "\n"; |
485 |
|
$last = ''; |
486 |
} elsif ( $c =~ m/^d/ ) { |
} elsif ( $c =~ m/^d/ ) { |
487 |
$self->debug( not $self->debug ); |
$self->debug( not $self->debug ); |
488 |
print "debug ", $self->debug ? 'on' : 'off', "\n"; |
print "debug ", $self->debug ? 'on' : 'off', "\n"; |
489 |
|
$last = ''; |
490 |
} else { |
} else { |
491 |
warn "# ignore $c\n"; |
warn "# ignored $line\n" if ($line); |
492 |
last; |
$last = ''; |
493 |
} |
} |
494 |
} |
} |
495 |
|
|
|
|
|
496 |
} |
} |
497 |
|
|
498 |
=head1 AUTHOR |
=head1 AUTHOR |