--- M6502/Orao.pm 2007/07/31 16:06:27 59 +++ M6502/Orao.pm 2007/08/03 12:21:47 110 @@ -10,8 +10,8 @@ use Data::Dump qw/dump/; use M6502; -use base qw(Class::Accessor M6502 Screen Prefs); -__PACKAGE__->mk_accessors(qw(run_for)); +use base qw(Class::Accessor M6502 Screen Prefs Tape); +__PACKAGE__->mk_accessors(qw(booted)); =head1 NAME @@ -19,11 +19,11 @@ =head1 VERSION -Version 0.02 +Version 0.04 =cut -our $VERSION = '0.02'; +our $VERSION = '0.04'; =head1 SUMMARY @@ -31,33 +31,49 @@ =cut -=head2 init +=head1 FUNCTIONS + +=head2 boot Start emulator, open L, load initial ROM images, and render memory + my $orao = Orao->new({}); + $orao->boot; + =cut our $orao; select(STDERR); $| = 1; -sub init { +sub boot { my $self = shift; warn "Orao calling upstream init\n"; - $self->SUPER::init( $self, @_ ); + $self->SUPER::init( + read => sub { $self->read( @_ ) }, + write => sub { $self->write( @_ ) }, + ); warn "Orao $Orao::VERSION emulation starting\n"; + warn "emulating ", $#mem, " bytes of memory\n"; + $self->open_screen; $self->load_rom({ 0x1000 => 'dump/SCRINV.BIN', - 0xC000 => 'rom/BAS12.ROM', - 0xE000 => 'rom/CRT12.ROM', + # should be 0x6000, but oraoemu has 2 byte prefix + 0x5FFE => 'dump/screen.dmp', +# 0xC000 => 'rom/BAS12.ROM', +# 0xE000 => 'rom/CRT12.ROM', + 0xC000 => 'rom/BAS13.ROM', + 0xE000 => 'rom/CRT13.ROM', }); - $PC = 0xDD11; # BC +# $PC = 0xDD11; # BC # $PC = 0xC274; # MC + $PC = 0xff89; + $orao = $self; # $self->prompt( 0x1000 ); @@ -66,10 +82,15 @@ $self->trace( 0 ); $self->debug( 0 ); + warn "rendering video memory\n"; + $self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); + if ( $self->show_mem ) { warn "rendering memory map\n"; + $self->render_mem( @mem ); + my @mmap = ( 0x0000, 0x03FF, 'nulti blok', 0x0400, 0x5FFF, 'korisnički RAM (23K)', @@ -81,27 +102,6 @@ 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 ); @@ -109,13 +109,36 @@ #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 ); - warn "Orao init finished", + warn "Orao boot finished", $self->trace ? ' trace' : '', $self->debug ? ' debug' : '', "\n"; + M6502::reset(); + + $self->booted( 1 ); } +=head2 run + +Run interactive emulation loop + + $orao->run; + +=cut + +sub run { + my $self = shift; + + $self->boot if ( ! $self->booted ); + + $self->load_tape( '../oraoigre/bdash.tap' ); + + $self->loop; +}; + +=head1 Helper functions + =head2 load_rom called to init memory and load initial rom images @@ -131,15 +154,11 @@ foreach my $addr ( sort keys %$loaded_files ) { my $path = $loaded_files->{$addr}; - $self->load_oraoemu( $path, $addr ); + $self->load_image( $path, $addr ); } } - -=head2 load_oraoemu - -=cut - +# write chunk directly into memory, updateing vram if needed sub _write_chunk { my $self = shift; my ( $addr, $chunk ) = @_; @@ -156,15 +175,29 @@ $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 ] ); - } + $self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); + $self->render_mem( @mem ) if $self->show_mem; } -sub load_oraoemu { +=head2 load_image + +Load binary files, ROM images and Orao Emulator files + + $orao->load_image( '/path/to/file', 0x1000 ); + +Returns true on success. + +=cut + +sub load_image { 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 ); @@ -173,15 +206,16 @@ $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; + 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) ); - return; + 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; @@ -199,6 +233,7 @@ $self->_write_chunk( $addr, $chunk ); + return 1; }; =head2 save_dump @@ -235,7 +270,11 @@ return sprintf(" %04x %s\n", $a, join(" ", map { - sprintf( "%02x", $_ ) + if ( defined($_) ) { + sprintf( "%02x", $_ ) + } else { + ' ' + } } @mem[ $a .. $a+8 ] ) ); @@ -256,11 +295,164 @@ =cut +my $keyboard_none = 255; + +my $keyboard = { + 0x87FC => { + 'right' => 16, + 'down' => 128, + 'up' => 192, + 'left' => 224, + 'backspace' => 224, + }, + 0x87FD => sub { + my ( $self, $key ) = @_; + if ( $key eq 'return' ) { + M6502::_write( 0xfc, 13 ); + warn "return\n"; + return 0; + } elsif ( $key =~ m/ ctrl/ || $self->key_down( 'left ctrl' ) || $self->key_down( 'right ctrl' ) ) { + warn "ctrl\n"; + return 16; + } + return $keyboard_none; + }, + 0x87FA => { + 'f4' => 16, + 'f3' => 128, + 'f2' => 192, + 'f1' => 224, + }, + 0x87FB => sub { + my ( $self, $key ) = @_; + if ( $key eq 'space' ) { + return 32; + } elsif ( $self->key_down( 'left shift' ) || $self->key_down( 'right shift' ) ) { + warn "shift\n"; + return 16; +# } elsif ( $self->tape ) { +# warn "has tape!"; +# return 0; + } + return $keyboard_none; + }, + 0x87F6 => { + '6' => 16, + 't' => 128, + 'y' => 192, # hr: z + 'r' => 224, + }, + 0x87F7 => { + '5' => 32, + '4' => 16, + }, + 0x87EE => { + '7' => 16, + 'u' => 128, + 'i' => 192, + 'o' => 224, + }, + 0x87EF => { + '8' => 32, + '9' => 16, + }, + 0x87DE => { + '1' => 16, + 'w' => 128, + 'q' => 192, + 'e' => 224, + }, + 0x87DF => { + '2' => 32, + '3' => 16, + }, + 0x87BE => { + 'm' => 16, + 'k' => 128, + 'j' => 192, + 'l' => 224, + }, + 0x87BF => { + ',' => 32, # < + '.' => 16, # > + }, + 0x877E => { + 'z' => 16, # hr:y + 's' => 128, + 'a' => 192, + 'd' => 224, + }, + 0x877F => { + 'x' => 32, + 'c' => 16, + }, + 0x86FE => { + 'n' => 16, + 'g' => 128, + 'h' => 192, + 'f' => 224, + }, + 0x86FF => { + 'b' => 32, + 'v' => 16, + }, + 0x85FE => { + '<' => 16, # : + '\\' => 128, # ¾ + '\'' => 192, # ę + ';' => 224, # č + }, + 0x85FF => { + '/' => 32, + 'f11' => 16, # ^ + }, + 0x83FE => { + 'f12' => 16, # ; + '[' => 128, # ¹ + ']' => 192, # š + 'p' => 224, + }, + 0x83FF => { + '-' => 32, + '0' => 16, + }, +}; + sub read { my $self = shift; my ($addr) = @_; my $byte = $mem[$addr]; + confess sprintf("can't find memory at address %04x",$addr) unless defined($byte); warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace; + + # keyboard + + if ( defined( $keyboard->{$addr} ) ) { + warn sprintf("keyboard port: %04x\n",$addr) if $self->trace; + my $key = $self->key_pressed; + if ( defined($key) ) { + my $ret = $keyboard_none; + my $r = $keyboard->{$addr} || confess "no definition for keyboard port found"; + if ( ref($r) eq 'CODE' ) { + $ret = $r->($self, $key); + } elsif ( defined($r->{$key}) ) { + $ret = $r->{$key}; + if ( ref($ret) eq 'CODE' ) { + $ret = $ret->($self); + } + } else { + warn sprintf("keyboard port: %04x unknown key: '%s'\n", $addr, $key) if $debug; + } + warn sprintf("keyboard port: %04x key:%s code:%d\n",$addr,$key,$ret) if ( $ret != $keyboard_none ); + return $ret; + } + return $keyboard_none; + } + + if ( $addr == 0x87ff ) { + return $self->read_tape; + } + $self->mmap_pixel( $addr, 0, $byte, 0 ); return $byte; } @@ -278,10 +470,6 @@ 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 == 0x8800 ) { warn sprintf "sound ignored: %x\n", $byte; } @@ -303,7 +491,7 @@ =head2 prompt - $orao->prompt( $address, $last_command ); + my ( $entered_line, @p ) = $orao->prompt( $address, $last_command ); =cut @@ -313,7 +501,7 @@ my $self = shift; $self->app->sync; my $a = shift; - print STDERR $self->hexdump( $a ), + print $self->hexdump( $a ), $last ? "[$last] " : '', "> "; my $in = ; @@ -321,7 +509,7 @@ warn "## prompt got: $in\n" if $self->debug; $in ||= $last; $last = $in; - return split(/\s+/, $in) if $in; + return ( $in, split(/\s+/, $in) ) if $in; } =head2 cli @@ -330,11 +518,16 @@ =cut +my $show_R = 0; + sub cli { my $self = shift; my $a = $PC || confess "no pc?"; - while ( my @v = $self->prompt( $a, $last ) ) { + my $run_for = 0; + warn $self->dump_R() if $show_R; + 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; @@ -356,11 +549,13 @@ 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); + warn $self->dump_R; + $last = ''; } elsif ( $c =~ m/^e/i ) { $a = $v if defined($v); my $to = shift @v; $to = $a + 32 if ( ! $to || $to <= $a ); + $to = 0xffff if ( $to > 0xffff ); my $lines = int( ($to - $a + 8) / 8 ); printf "## e %04x %04x (%d bytes) lines: %d\n", $a, $to, ($to-$a), $lines; while ( --$lines ) { @@ -368,47 +563,70 @@ $a += 8; } $last = '+'; + $show_R = 0; } elsif ( $c =~ m/^\+/ ) { $a += 8; + $show_R = 0; } elsif ( $c =~ m/^\-/ ) { $a -= 8; + $show_R = 0; } elsif ( $c =~ m/^m/i ) { - $a = $v; + $a = $v if defined($v); $self->poke_code( $a, @v ); printf "poke %d bytes at %04x\n", $#v + 1, $a; $last = '+'; + $show_R = 0; } elsif ( $c =~ m/^l/i ) { my $to = shift @v || 0x1000; $a = $to; - $self->load_oraoemu( $v, $a ); + $self->load_image( $v, $a ); $last = ''; } elsif ( $c =~ m/^s/i ) { $self->save_dump( $v || 'mem.dump', @v ); $last = ''; - } elsif ( $c =~ m/^r/i ) { + } elsif ( $c =~ m/^re/i ) { # reset + M6502::reset(); + $last = 'r 1'; + } elsif ( $c =~ m/^r/i ) { # run $run_for = $v || 1; print "run_for $run_for instructions\n"; + $show_R = 1; last; - } elsif ( $c =~ m/^(u|j)/ ) { + } elsif ( $c =~ m/^(u|j)/i ) { my $to = $v || $a; printf "set pc to %04x\n", $to; $PC = $to; # remember for restart $run_for = 1; - $last = sprintf('m %04x', $to); + $last = "r $run_for"; + $show_R = 1; last; - } elsif ( $c =~ m/^t/ ) { + } elsif ( $c =~ m/^tape/ ) { + if ( $c =~ m/rate/ ) { + $self->tape_rate( $v ); + warn "will read table with rate $v\n"; + } elsif ( ! $v ) { + warn "ERROR: please specify tape name!\n"; + } elsif ( ! -e $v ) { + warn "ERROR: tape $v: $!\n"; + } else { + $self->load_tape( $v ); + } + $last = ''; + } elsif ( $c =~ m/^t/i ) { $self->trace( not $self->trace ); print "trace ", $self->trace ? 'on' : 'off', "\n"; - } elsif ( $c =~ m/^d/ ) { + $last = ''; + } elsif ( $c =~ m/^d/i ) { $self->debug( not $self->debug ); print "debug ", $self->debug ? 'on' : 'off', "\n"; + $last = ''; } else { - warn "# ignore $c\n"; - last; + warn "# ignored $line\n" if ($line); + $last = ''; } } - + return $run_for; } =head1 AUTHOR