--- M6502/Orao.pm 2007/07/30 21:00:36 33 +++ M6502/Orao.pm 2007/07/31 16:33:41 64 @@ -3,14 +3,15 @@ use warnings; use strict; -use Carp; +use Carp qw/confess/; use lib './lib'; #use Time::HiRes qw(time); use File::Slurp; use Data::Dump qw/dump/; +use M6502; -use base qw(Class::Accessor M6502 Screen); -__PACKAGE__->mk_accessors(qw(debug trace run_for mem_dump trace)); +use base qw(Class::Accessor M6502 Screen Prefs); +__PACKAGE__->mk_accessors(qw(run_for)); =head1 NAME @@ -32,20 +33,20 @@ =head2 init -Start emulator +Start emulator, open L, load initial ROM images, and render memory =cut our $orao; -our $PC = 0x1000; +select(STDERR); $| = 1; sub init { my $self = shift; - warn "call upstream init\n"; + warn "Orao calling upstream init\n"; $self->SUPER::init( $self, @_ ); - warn "staring Orao $Orao::VERSION emulation\n"; + warn "Orao $Orao::VERSION emulation starting\n"; $self->open_screen; $self->load_rom({ @@ -54,32 +55,64 @@ 0xE000 => 'rom/CRT12.ROM', }); + $PC = 0xDD11; # BC +# $PC = 0xC274; # MC + $orao = $self; # $self->prompt( 0x1000 ); - warn "rendering memory map\n"; - - my @mmap = ( - 0x0000, 0x03FF, 'nulti blok', - 0x0400, 0x5FFF, 'korisnički RAM (23K)', - 0x6000, 0x7FFF, 'video RAM', - 0x8000, 0x9FFF, 'sistemske lokacije', - 0xA000, 0xAFFF, 'ekstenzija', - 0xB000, 0xBFFF, 'DOS', - 0xC000, 0xDFFF, 'BASIC ROM', - 0xE000, 0xFFFF, 'sistemski ROM', - ); - - 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 ) { -# $orao->read( $a ); -# } -# $self->sync; + my ( $trace, $debug ) = ( $self->trace, $self->debug ); + $self->trace( 0 ); + $self->debug( 0 ); + + if ( $self->show_mem ) { + + warn "rendering memory map\n"; + + my @mmap = ( + 0x0000, 0x03FF, 'nulti blok', + 0x0400, 0x5FFF, 'korisnički RAM (23K)', + 0x6000, 0x7FFF, 'video RAM', + 0x8000, 0x9FFF, 'sistemske lokacije', + 0xA000, 0xAFFF, 'ekstenzija', + 0xB000, 0xBFFF, 'DOS', + 0xC000, 0xDFFF, 'BASIC ROM', + 0xE000, 0xFFFF, 'sistemski ROM', + ); + + 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 ); + } + } + } + + } else { + + warn "rendering video memory\n"; + for my $a ( 0x6000 .. 0x7fff ) { + $self->vram( $a - 0x6000, $mem[$a] ); + } + } + $self->sync; + $self->trace( $trace ); + $self->debug( $debug ); + + #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 ); + + warn "Orao init finished", + $self->trace ? ' trace' : '', + $self->debug ? ' debug' : '', + "\n"; } @@ -102,15 +135,47 @@ } } +# write chunk directly into memory, updateing vram if needed +sub _write_chunk { + my $self = shift; + my ( $addr, $chunk ) = @_; + $self->write_chunk( $addr, $chunk ); + my $end = $addr + length($chunk); + my ( $f, $t ) = ( 0x6000, 0x7fff ); + + if ( $end < $f || $addr >= $t ) { + warn "skip vram update\n"; + return; + }; + + $f = $addr if ( $addr > $f ); + $t = $end if ( $end < $t ); + + warn sprintf("refresh video ram %04x-%04x\n", $f, $t); + foreach my $a ( $f .. $t ) { + $self->vram( $a - 0x6000 , $mem[ $a ] ); + } +} =head2 load_oraoemu +Load binary files, ROM images and Orao Emulator files + + $orao->load_oraoemu( '/path/to/file', 0x1000 ); + +Returns true on success. + =cut sub load_oraoemu { my $self = shift; my ( $path, $addr ) = @_; + if ( ! -e $path ) { + warn "ERROR: file $path doesn't exist\n"; + return; + } + my $size = -s $path || confess "no size for $path: $!"; my $buff = read_file( $path ); @@ -118,17 +183,17 @@ if ( $size == 65538 ) { $addr = 0; warn sprintf "loading oraoemu 64k dump %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; - $self->write_chunk( $addr, substr($buff,2) ); - return; + $self->_write_chunk( $addr, substr($buff,2) ); + return 1; } elsif ( $size == 32800 ) { $addr = 0; warn sprintf "loading oraoemu 1.3 dump %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; - #$self->write_chunk( $addr, substr($buff,0x20) ); - $self->poke_code( $addr, map { ord($_) } split(//,substr($buff,0x20)) ); - return; + $self->_write_chunk( $addr, substr($buff,0x20) ); + return 1; } printf "loading %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; - return $self->write_chunk( $addr, $buff ); + $self->_write_chunk( $addr, $buff ); + return 1; my $chunk; @@ -144,8 +209,9 @@ $pos += 4; } - $self->write_chunk( $addr, $chunk ); + $self->_write_chunk( $addr, $chunk ); + return 1; }; =head2 save_dump @@ -183,31 +249,11 @@ join(" ", map { sprintf( "%02x", $_ ) - } $self->ram( $a, $a+8 ) + } @mem[ $a .. $a+8 ] ) ); } -=head2 prompt - - $orao->prompt( $address, $last_command ); - -=cut - -sub prompt { - my $self = shift; - my $a = shift; - my $last = shift; - print STDERR $self->hexdump( $a ), - $last ? "[$last] " : '', - "> "; - my $in = ; - chomp($in); - $in ||= $last; - $last = $in; - return split(/\s+/, $in) if $in; -} - =head1 Memory management Orao implements all I/O using mmap addresses. This was main reason why @@ -215,8 +261,6 @@ =cut -my @mem = (0xff) x 0x100000; # 64Mb - =head2 read Read from memory @@ -229,7 +273,7 @@ my $self = shift; my ($addr) = @_; my $byte = $mem[$addr]; - warn "# Orao::read(",dump(@_),") = ",dump( $byte ),"\n" if $self->debug; + warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace; $self->mmap_pixel( $addr, 0, $byte, 0 ); return $byte; } @@ -244,26 +288,143 @@ sub write { my $self = shift; - warn "# Orao::write(",dump(@_),")\n" if $self->debug; my ($addr,$byte) = @_; + warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace; if ( $addr >= 0x6000 && $addr < 0x8000 ) { $self->vram( $addr - 0x6000 , $byte ); } - if ( $addr > 0xafff ) { - warn sprintf "access to %04x above affff aborting\n", $addr; - return -1; - } if ( $addr == 0x8800 ) { warn sprintf "sound ignored: %x\n", $byte; } + if ( $addr > 0xafff ) { + confess sprintf "write access 0x%04x > 0xafff aborting\n", $addr; + } + $self->mmap_pixel( $addr, $byte, 0, 0 ); $mem[$addr] = $byte; + return; +} + +=head1 Command Line + +Command-line debugging intrerface is implemented for communication with +emulated device + +=head2 prompt + + my ( $entered_line, @p ) = $orao->prompt( $address, $last_command ); + +=cut + +my $last = 'r 1'; + +sub prompt { + my $self = shift; + $self->app->sync; + my $a = shift; + print STDERR $self->hexdump( $a ), + $last ? "[$last] " : '', + "> "; + my $in = ; + chomp($in); + warn "## prompt got: $in\n" if $self->debug; + $in ||= $last; + $last = $in; + return ( $in, split(/\s+/, $in) ) if $in; } +=head2 cli + + $orao->cli(); + +=cut + +sub cli { + my $self = shift; + my $a = $PC || confess "no pc?"; + while ( my ($line, @v) = $self->prompt( $a, $last ) ) { + my $c = shift @v; + next unless defined($c); + my $v = shift @v; + $v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/; + @v = map { hex($_) } @v; + printf "## a: %04x parsed cli: c:%s v:%s %s\n", $a, $c, ($v || 'undef'), join(",",@v) if $self->debug; + if ( $c =~ m/^[qx]/i ) { + exit; + } elsif ( $c eq '?' ) { + my $t = $self->trace ? 'on' : 'off' ; + my $d = $self->debug ? 'on' : 'off' ; + warn <<__USAGE__; +Usage: + +x|q\t\texit +e 6000 6010\tdump memory, +/- to walk forward/backward +m 1000 ff 00\tput ff 00 on 1000 +j|u 1000\t\tjump (change pc) +r 42\t\trun 42 instruction opcodes +t\t\ttrace [$t] +d\t\tdebug [$d] + +__USAGE__ + warn sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S); + } elsif ( $c =~ m/^e/i ) { + $a = $v if defined($v); + my $to = shift @v; + $to = $a + 32 if ( ! $to || $to <= $a ); + my $lines = int( ($to - $a + 8) / 8 ); + printf "## e %04x %04x (%d bytes) lines: %d\n", $a, $to, ($to-$a), $lines; + while ( --$lines ) { + print $self->hexdump( $a ); + $a += 8; + } + $last = '+'; + } elsif ( $c =~ m/^\+/ ) { + $a += 8; + } elsif ( $c =~ m/^\-/ ) { + $a -= 8; + } elsif ( $c =~ m/^m/i ) { + $a = $v; + $self->poke_code( $a, @v ); + printf "poke %d bytes at %04x\n", $#v + 1, $a; + $last = '+'; + } elsif ( $c =~ m/^l/i ) { + my $to = shift @v || 0x1000; + $a = $to; + $self->load_oraoemu( $v, $a ); + $last = ''; + } elsif ( $c =~ m/^s/i ) { + $self->save_dump( $v || 'mem.dump', @v ); + $last = ''; + } elsif ( $c =~ m/^r/i ) { + $run_for = $v || 1; + print "run_for $run_for instructions\n"; + last; + } elsif ( $c =~ m/^(u|j)/ ) { + my $to = $v || $a; + printf "set pc to %04x\n", $to; + $PC = $to; # remember for restart + $run_for = 1; + $last = "r $run_for"; + last; + } elsif ( $c =~ m/^t/ ) { + $self->trace( not $self->trace ); + print "trace ", $self->trace ? 'on' : 'off', "\n"; + $last = ''; + } elsif ( $c =~ m/^d/ ) { + $self->debug( not $self->debug ); + print "debug ", $self->debug ? 'on' : 'off', "\n"; + $last = ''; + } else { + warn "# ignored $line\n" if ($line); + $last = ''; + } + } + +} =head1 AUTHOR