--- M6502/Orao.pm 2007/08/02 13:58:26 96 +++ Orao.pm 2007/08/05 15:16:10 150 @@ -4,15 +4,13 @@ use strict; use Carp qw/confess/; -use lib './lib'; -#use Time::HiRes qw(time); use File::Slurp; use Data::Dump qw/dump/; -use List::Util qw/first/; -use M6502; +use M6502; # import @mem $PC and friends +use Screen qw/$white $black/; -use base qw(Class::Accessor M6502 Screen Prefs); -__PACKAGE__->mk_accessors(qw(booted)); +use base qw(Class::Accessor VRac M6502 Screen Prefs Tape Session); +#__PACKAGE__->mk_accessors(qw()); =head1 NAME @@ -20,42 +18,31 @@ =head1 VERSION -Version 0.04 +Version 0.06 =cut -our $VERSION = '0.04'; +our $VERSION = '0.06'; =head1 SUMMARY -Emulator or Orao 8-bit 6502 machine popular in Croatia +Emulator for Orao 8-bit 6502 machine popular in Croatia (especially schools) =cut -my @kbd_ports = ( - 0x87FC,0x87FD,0x87FA,0x87FB,0x87F6,0x87F7, - 0x87EE,0x87EF,0x87DE,0x87DF,0x87BE,0x87BF, - 0x877E,0x877F,0x86FE,0x86FF,0x85FE,0x85FF, - 0x83FE,0x83FF, -); - =head1 FUNCTIONS -=head2 boot - -Start emulator, open L, load initial ROM images, and render memory +=head2 run - my $orao = Orao->new({}); - $orao->boot; +Start emulator, open L, load initial ROM images, and start emulator loop =cut -our $orao; +our $emu; -select(STDERR); $| = 1; - -sub boot { +sub run { my $self = shift; + warn "Orao calling upstream init\n"; $self->SUPER::init( read => sub { $self->read( @_ ) }, @@ -66,13 +53,19 @@ warn "emulating ", $#mem, " bytes of memory\n"; +# $self->scale( 2 ); +# $self->show_mem( 1 ); + $self->load_session( 'session.pl' ); + $self->open_screen; $self->load_rom({ - 0x1000 => 'dump/SCRINV.BIN', +# 0x1000 => 'dump/SCRINV.BIN', # should be 0x6000, but oraoemu has 2 byte prefix - 0x5FFE => 'dump/screen.dmp', - 0xC000 => 'rom/BAS12.ROM', - 0xE000 => 'rom/CRT12.ROM', +# 0x5FFE => '/home/dpavlin/orao/dump/screen.dmp', +# 0xC000 => 'rom/Orao/BAS12.ROM', +# 0xE000 => 'rom/Orao/CRT12.ROM', + 0xC000 => 'rom/Orao/BAS13.ROM', + 0xE000 => 'rom/Orao/CRT13.ROM', }); # $PC = 0xDD11; # BC @@ -80,7 +73,7 @@ $PC = 0xff89; - $orao = $self; + $emu = $self; # $self->prompt( 0x1000 ); @@ -88,14 +81,11 @@ $self->trace( 0 ); $self->debug( 0 ); - $self->render( @mem[ 0x6000 .. 0x7fff ] ); + warn "rendering memory\n"; + $self->render_mem( @mem ); 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)', @@ -107,18 +97,18 @@ 0xE000, 0xFFFF, 'sistemski ROM', ); - } else { + print "Orao memory map:"; + + while ( @mmap ) { + my ( $from, $to, $desc ) = splice(@mmap, 0, 3); + printf("%04x-%04x %s\n", $from, $to, $desc); + } - warn "rendering video memory\n"; - $self->render( @mem[ 0x6000 .. 0x7fff ] ); - } - $self->sync; + $self->trace( $trace ); $self->debug( $debug ); - #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 ); - warn "Orao boot finished", $self->trace ? ' trace' : '', $self->debug ? ' debug' : '', @@ -126,50 +116,31 @@ M6502::reset(); - $self->booted( 1 ); -} - -=head2 run - -Run interactive emulation loop - - $orao->run; - -=cut - -sub run { - my $self = shift; +# $self->load_tape( '../oraoigre/bdash.tap' ); - $self->boot if ( ! $self->booted ); - $self->loop; + $self->loop( sub { + my $run_for = shift; + warn sprintf("about to exec from PC %04x for %d cycles\n", $PC, $run_for) if $self->trace; + M6502::exec( $run_for ); + $self->render_vram; + }); }; + =head1 Helper functions -=head2 load_rom +=head2 write_chunk -called to init memory and load initial rom images +Write chunk directly into memory, updateing vram if needed - $orao->load_rom; + $emu->write_chunk( 0x1000, $chunk_data ); =cut -sub load_rom { - my ($self, $loaded_files) = @_; - - #my $time_base = time(); - - foreach my $addr ( sort keys %$loaded_files ) { - my $path = $loaded_files->{$addr}; - $self->load_image( $path, $addr ); - } -} - -# write chunk directly into memory, updateing vram if needed -sub _write_chunk { +sub write_chunk { my $self = shift; my ( $addr, $chunk ) = @_; - $self->write_chunk( $addr, $chunk ); + $self->SUPER::write_chunk( $addr, $chunk ); my $end = $addr + length($chunk); my ( $f, $t ) = ( 0x6000, 0x7fff ); @@ -182,18 +153,15 @@ $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( @mem[ 0x6000 .. 0x7fff ] ); - $self->render_mem( @mem ) if $self->show_mem; + $self->render_vram; + $self->render_mem( @mem ); } =head2 load_image Load binary files, ROM images and Orao Emulator files - $orao->load_image( '/path/to/file', 0x1000 ); + $emu->load_image( '/path/to/file', 0x1000 ); Returns true on success. @@ -215,80 +183,20 @@ 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) ); + $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->write_chunk( $addr, substr($buff,0x20) ); return 1; } - printf "loading %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; - $self->_write_chunk( $addr, $buff ); - return 1; - - my $chunk; - - my $pos = 0; - - while ( my $long = substr($buff,$pos,4) ) { - my @b = split(//, $long, 4); - $chunk .= - ( $b[3] || '' ) . - ( $b[2] || '' ) . - ( $b[1] || '' ) . - ( $b[0] || '' ); - $pos += 4; - } - - $self->_write_chunk( $addr, $chunk ); + printf "loading %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size; + $self->write_chunk( $addr, $buff ); return 1; }; -=head2 save_dump - - $orao->save_dump( 'filename', $from, $to ); - -=cut - -sub save_dump { - my $self = shift; - - my ( $path, $from, $to ) = @_; - - $from ||= 0; - $to ||= 0xffff; - - open(my $fh, '>', $path) || die "can't open $path: $!"; - print $fh $self->read_chunk( $from, $to ); - close($fh); - - my $size = -s $path; - warn sprintf "saved %s %d %x bytes\n", $path, $size, $size; -} - -=head2 hexdump - - $orao->hexdump( $address ); - -=cut - -sub hexdump { - my $self = shift; - my $a = shift; - return sprintf(" %04x %s\n", $a, - join(" ", - map { - if ( defined($_) ) { - sprintf( "%02x", $_ ) - } else { - ' ' - } - } @mem[ $a .. $a+8 ] - ) - ); -} =head1 Memory management @@ -305,65 +213,166 @@ =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) = @_; + return if ( $addr > 0xffff ); 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 ( first { $addr == $_ } @kbd_ports ) { - warn sprintf("keyboard port: %04x\n",$addr); - } elsif ( $addr == 0x87fc ) { - warn "0x87fc - arrows/back\n"; -=for pascal - if VKey=VK_RIGHT then Result:=16; - if VKey=VK_DOWN then Result:=128; - if VKey=VK_UP then Result:=192; - if VKey=VK_LEFT then Result:=224; - if Ord(KeyPressed)=VK_BACK then Result:=224; -=cut - } elsif ( $addr == 0x87fd ) { - warn "0x87fd - enter\n"; -=for pascal - if KeyPressed=Chr(13) then begin - Mem[$FC]:=13; - Result:=0; - end; -=cut - } elsif ( $addr == 0x87fa ) { - warn "0x87fa = F1 - F4\n"; -=for pascal - if VKey=VK_F4 then Result:=16; - if VKey=VK_F3 then Result:=128; - if VKey=VK_F2 then Result:=192; - if VKey=VK_F1 then Result:=224; -=cut - } elsif ( $addr == 0x87fb ) { - warn "0x87fb\n"; -=for pascal - if KeyPressed=Chr(32) then Result:=32; - if KeyPressed='"' then Result:=16; - if KeyPressed='!' then Result:=16; - if KeyPressed='$' then Result:=16; - if KeyPressed='%' then Result:=16; - if KeyPressed='&' then Result:=16; - if KeyPressed='(' then Result:=16; - if KeyPressed=')' then Result:=16; - if KeyPressed='=' then Result:=16; - if KeyPressed='#' then Result:=16; - if KeyPressed='+' then Result:=16; - if KeyPressed='*' then Result:=16; - if KeyPressed='?' then Result:=16; - if KeyPressed='<' then Result:=16; - if KeyPressed='>' then Result:=16; - if VKey=191 then Result:=16; -=cut + 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 ); + $self->mmap_pixel( $addr, 0, $byte, 0 ) if $self->show_mem; return $byte; } @@ -380,11 +389,8 @@ 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 ) { + $self->write_tape( $byte ); warn sprintf "sound ignored: %x\n", $byte; } @@ -392,147 +398,78 @@ confess sprintf "write access 0x%04x > 0xafff aborting\n", $addr; } - $self->mmap_pixel( $addr, $byte, 0, 0 ); - + $self->mmap_pixel( $addr, $byte, 0, 0 ) if $self->show_mem; $mem[$addr] = $byte; return; } -=head1 Command Line +=head1 Architecture specific -Command-line debugging intrerface is implemented for communication with -emulated device +=head2 render_vram -=head2 prompt +Render one frame of video ram - my ( $entered_line, @p ) = $orao->prompt( $address, $last_command ); + $self->render_vram; =cut -my $last = 'r 1'; +my @flip; + +foreach my $i ( 0 .. 255 ) { + my $t = 0; + $i & 0b00000001 and $t = $t | 0b10000000; + $i & 0b00000010 and $t = $t | 0b01000000; + $i & 0b00000100 and $t = $t | 0b00100000; + $i & 0b00001000 and $t = $t | 0b00010000; + $i & 0b00010000 and $t = $t | 0b00001000; + $i & 0b00100000 and $t = $t | 0b00000100; + $i & 0b01000000 and $t = $t | 0b00000010; + $i & 0b10000000 and $t = $t | 0b00000001; + #warn "$i = $t\n"; + $flip[$i] = $t; +} -sub prompt { + +sub render_vram { 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; + + my $pixels = pack("C*", map { $flip[$_] } @mem[ 0x6000 .. 0x7fff ]); + + my $vram = SDL::Surface->new( + -width => 256, + -height => 256, + -depth => 1, # 1 bit per pixel + -pitch => 32, # bytes per line + -from => $pixels, + ); + $vram->set_colors( 0, $black, $white ); + + $self->render_frame( $vram ); } -=head2 cli +=head2 cpu_PC - $orao->cli(); +Helper metod to set or get PC for current architecture =cut -my $show_R = 0; +sub cpu_PC { + my ( $self, $addr ) = @_; + if ( defined($addr) ) { + $PC = $addr; + warn sprintf("running from PC %04x\n", $PC); + }; + return $PC; +} -sub cli { - my $self = shift; - my $a = $PC || confess "no pc?"; - 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; - 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 $self->dump_R; - } 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 ) { - print $self->hexdump( $a ); - $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 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_image( $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"; - $show_R = 1; - 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"; - $show_R = 1; - 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 SEE ALSO - return $run_for; -} +L, L, L, L =head1 AUTHOR Dobrica Pavlinusic, C<< >> -=head1 BUGS - =head1 ACKNOWLEDGEMENTS See also L which is source of all