/[VRac]/Orao.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /Orao.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

M6502/Orao.pm revision 94 by dpavlin, Thu Aug 2 13:04:29 2007 UTC Orao.pm revision 124 by dpavlin, Sat Aug 4 14:13:28 2007 UTC
# Line 8  use lib './lib'; Line 8  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/;
 use List::Util qw/first/;  
11  use M6502;  use M6502;
12    
13  use base qw(Class::Accessor M6502 Screen Prefs);  use base qw(Class::Accessor VRac M6502 Screen Prefs Tape);
14  __PACKAGE__->mk_accessors(qw(run_for));  #__PACKAGE__->mk_accessors(qw());
15    
16  =head1 NAME  =head1 NAME
17    
# Line 20  Orao - Orao emulator Line 19  Orao - Orao emulator
19    
20  =head1 VERSION  =head1 VERSION
21    
22  Version 0.03  Version 0.04
23    
24  =cut  =cut
25    
26  our $VERSION = '0.03';  our $VERSION = '0.04';
27    
28  =head1 SUMMARY  =head1 SUMMARY
29    
# Line 32  Emulator or Orao 8-bit 6502 machine popu Line 31  Emulator or Orao 8-bit 6502 machine popu
31    
32  =cut  =cut
33    
34  my @kbd_ports = (  =head1 FUNCTIONS
     0x87FC,0x87FD,0x87FA,0x87FB,0x87F6,0x87F7,  
     0x87EE,0x87EF,0x87DE,0x87DF,0x87BE,0x87BF,  
     0x877E,0x877F,0x86FE,0x86FF,0x85FE,0x85FF,  
     0x83FE,0x83FF,  
 );  
35    
36  =head2 boot  =head2 boot
37    
38  Start emulator, open L<Screen>, load initial ROM images, and render memory  Start emulator, open L<Screen>, load initial ROM images, and render memory
39    
40    my $orao = Orao->new({});    my $emu = Orao->new({});
41    $orao->boot;    $emu->boot;
42    
43  =cut  =cut
44    
45  our $orao;  our $emu;
46    
47  select(STDERR); $| = 1;  select(STDERR); $| = 1;
48    
# Line 66  sub boot { Line 60  sub boot {
60    
61          $self->open_screen;          $self->open_screen;
62          $self->load_rom({          $self->load_rom({
63                  0x1000 => 'dump/SCRINV.BIN',  #               0x1000 => 'dump/SCRINV.BIN',
64                  # should be 0x6000, but oraoemu has 2 byte prefix                  # should be 0x6000, but oraoemu has 2 byte prefix
65                  0x5FFE => 'dump/screen.dmp',  #               0x5FFE => 'dump/screen.dmp',
66                  0xC000 => 'rom/BAS12.ROM',  #               0xC000 => 'rom/Orao/BAS12.ROM',
67                  0xE000 => 'rom/CRT12.ROM',  #               0xE000 => 'rom/Orao/CRT12.ROM',
68                    0xC000 => 'rom/Orao/BAS13.ROM',
69                    0xE000 => 'rom/Orao/CRT13.ROM',
70          });          });
71    
72  #       $PC = 0xDD11;   # BC  #       $PC = 0xDD11;   # BC
# Line 78  sub boot { Line 74  sub boot {
74    
75          $PC = 0xff89;          $PC = 0xff89;
76    
77          $orao = $self;          $emu = $self;
78    
79  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
80    
# Line 86  sub boot { Line 82  sub boot {
82          $self->trace( 0 );          $self->trace( 0 );
83          $self->debug( 0 );          $self->debug( 0 );
84    
85          $self->render( @mem[ 0x6000 .. 0x7fff ] );          warn "rendering video memory\n";
86            $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );
87    
88          if ( $self->show_mem ) {          if ( $self->show_mem ) {
89    
# Line 105  sub boot { Line 102  sub boot {
102                          0xE000, 0xFFFF, 'sistemski ROM',                          0xE000, 0xFFFF, 'sistemski ROM',
103                  );                  );
104    
         } else {  
   
                 warn "rendering video memory\n";  
                 $self->render( @mem[ 0x6000 .. 0x7fff ] );  
           
105          }          }
106          $self->sync;          $self->sync;
107          $self->trace( $trace );          $self->trace( $trace );
# Line 124  sub boot { Line 116  sub boot {
116    
117          M6502::reset();          M6502::reset();
118    
119            $self->booted( 1 );
120  }  }
121    
122  =head2 load_rom  =head2 run
123    
124  called to init memory and load initial rom images  Run interactive emulation loop
125    
126    $orao->load_rom;    $emu->run;
127    
128  =cut  =cut
129    
130  sub load_rom {  sub run {
131      my ($self, $loaded_files) = @_;          my $self = shift;
132    
133      #my $time_base = time();          $self->boot if ( ! $self->booted );
134    
135          foreach my $addr ( sort keys %$loaded_files ) {  #       $self->load_tape( '../oraoigre/bdash.tap' );
136                  my $path = $loaded_files->{$addr};  
137                  $self->load_image( $path, $addr );          $self->loop;
138          }  };
139  }  
140    =head1 Helper functions
141    
142    =cut
143    
144  # write chunk directly into memory, updateing vram if needed  # write chunk directly into memory, updateing vram if needed
145  sub _write_chunk {  sub _write_chunk {
# Line 162  sub _write_chunk { Line 158  sub _write_chunk {
158          $t = $end if ( $end < $t );          $t = $end if ( $end < $t );
159    
160          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);
161  #       foreach my $a ( $f .. $t ) {          $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );
 #               $self->vram( $a - 0x6000 , $mem[ $a ] );  
 #       }  
         $self->render( @mem[ 0x6000 .. 0x7fff ] );  
162          $self->render_mem( @mem ) if $self->show_mem;          $self->render_mem( @mem ) if $self->show_mem;
163  }  }
164    
# Line 173  sub _write_chunk { Line 166  sub _write_chunk {
166    
167  Load binary files, ROM images and Orao Emulator files  Load binary files, ROM images and Orao Emulator files
168    
169    $orao->load_image( '/path/to/file', 0x1000 );    $emu->load_image( '/path/to/file', 0x1000 );
170    
171  Returns true on success.  Returns true on success.
172    
# Line 226  sub load_image { Line 219  sub load_image {
219          return 1;          return 1;
220  };  };
221    
 =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 ]  
                 )  
         );  
 }  
222    
223  =head1 Memory management  =head1 Memory management
224    
# Line 285  Read from memory Line 235  Read from memory
235    
236  =cut  =cut
237    
238    my $keyboard_none = 255;
239    
240    my $keyboard = {
241            0x87FC => {
242                    'right'         => 16,
243                    'down'          => 128,
244                    'up'            => 192,
245                    'left'          => 224,
246                    'backspace' => 224,
247            },
248            0x87FD => sub {
249                    my ( $self, $key ) = @_;
250                    if ( $key eq 'return' ) {
251                            M6502::_write( 0xfc, 13 );
252                            warn "return\n";
253                            return 0;
254                    } elsif ( $key =~ m/ ctrl/ || $self->key_down( 'left ctrl' ) || $self->key_down( 'right ctrl' ) ) {
255                            warn "ctrl\n";
256                            return 16;
257                    }
258                    return $keyboard_none;
259            },
260            0x87FA => {
261                    'f4' => 16,
262                    'f3' => 128,
263                    'f2' => 192,
264                    'f1' => 224,
265            },
266            0x87FB => sub {
267                    my ( $self, $key ) = @_;
268                    if ( $key eq 'space' ) {
269                            return 32;
270                    } elsif ( $self->key_down( 'left shift' ) || $self->key_down( 'right shift' ) ) {
271                            warn "shift\n";
272                            return 16;
273    #               } elsif ( $self->tape ) {
274    #                       warn "has tape!";
275    #                       return 0;
276                    }
277                    return $keyboard_none;
278            },
279            0x87F6 => {
280                    '6' => 16,
281                    't' => 128,
282                    'y' => 192,     # hr: z
283                    'r' => 224,
284            },
285            0x87F7 => {
286                    '5' => 32,
287                    '4' => 16,
288            },
289            0x87EE => {
290                    '7' => 16,
291                    'u' => 128,
292                    'i' => 192,
293                    'o' => 224,
294            },
295            0x87EF => {
296                    '8' => 32,
297                    '9' => 16,
298            },
299            0x87DE => {
300                    '1' => 16,
301                    'w' => 128,
302                    'q' => 192,
303                    'e' => 224,
304            },
305            0x87DF => {
306                    '2' => 32,
307                    '3' => 16,
308            },
309            0x87BE => {
310                    'm' => 16,
311                    'k' => 128,
312                    'j' => 192,
313                    'l' => 224,
314            },
315            0x87BF => {
316                    ',' => 32,      # <
317                    '.' => 16,      # >
318            },
319            0x877E => {
320                    'z' => 16,      # hr:y
321                    's' => 128,
322                    'a' => 192,
323                    'd' => 224,
324            },
325            0x877F => {
326                    'x' => 32,
327                    'c' => 16,
328            },
329            0x86FE => {
330                    'n' => 16,
331                    'g' => 128,
332                    'h' => 192,
333                    'f' => 224,
334            },
335            0x86FF => {
336                    'b' => 32,
337                    'v' => 16,
338            },
339            0x85FE => {
340                    '<' => 16,              # :
341                    '\\' => 128,    # ¾
342                    '\'' => 192,    # æ
343                    ';' => 224,             # è
344            },
345            0x85FF => {
346                    '/' => 32,
347                    'f11' => 16,    # ^
348            },
349            0x83FE => {
350                    'f12' => 16,    # ;
351                    '[' => 128,             # ¹
352                    ']' => 192,             # ð
353                    'p' => 224,
354            },
355            0x83FF => {
356                    '-' => 32,
357                    '0' => 16,
358            },
359    };
360    
361  sub read {  sub read {
362          my $self = shift;          my $self = shift;
363          my ($addr) = @_;          my ($addr) = @_;
# Line 294  sub read { Line 367  sub read {
367    
368          # keyboard          # keyboard
369    
370          if ( first { $addr == $_ } @kbd_ports ) {          if ( defined( $keyboard->{$addr} ) ) {
371                  warn sprintf("keyboard port: %04x\n",$addr);                  warn sprintf("keyboard port: %04x\n",$addr) if $self->trace;
372          } elsif ( $addr == 0x87fc ) {                  my $key = $self->key_pressed;
373                  warn "0x87fc - arrows/back\n";                  if ( defined($key) ) {
374  =for pascal                          my $ret = $keyboard_none;
375                  if VKey=VK_RIGHT then Result:=16;                          my $r = $keyboard->{$addr} || confess "no definition for keyboard port found";
376                  if VKey=VK_DOWN then Result:=128;                          if ( ref($r) eq 'CODE' ) {
377                  if VKey=VK_UP then Result:=192;                                  $ret = $r->($self, $key);
378                  if VKey=VK_LEFT then Result:=224;                          } elsif ( defined($r->{$key}) ) {
379                  if Ord(KeyPressed)=VK_BACK then Result:=224;                                  $ret = $r->{$key};
380  =cut                                  if ( ref($ret) eq 'CODE' ) {
381          } elsif ( $addr == 0x87fd ) {                                          $ret = $ret->($self);
382                  warn "0x87fd - enter\n";                                  }
383  =for pascal                          } else {
384      if KeyPressed=Chr(13) then begin                                  warn sprintf("keyboard port: %04x unknown key: '%s'\n", $addr, $key) if $debug;
385        Mem[$FC]:=13;                          }
386        Result:=0;                          warn sprintf("keyboard port: %04x key:%s code:%d\n",$addr,$key,$ret) if ( $ret != $keyboard_none );
387      end;                          return $ret;
388  =cut                  }
389          } elsif ( $addr == 0x87fa ) {                  return $keyboard_none;
390                  warn "0x87fa = F1 - F4\n";          }
391  =for pascal  
392      if VKey=VK_F4 then Result:=16;          if ( $addr == 0x87ff ) {
393      if VKey=VK_F3 then Result:=128;                  return $self->read_tape;
     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  
394          }          }
395    
396          $self->mmap_pixel( $addr, 0, $byte, 0 );          $self->mmap_pixel( $addr, 0, $byte, 0 );
# Line 360  sub write { Line 410  sub write {
410          my ($addr,$byte) = @_;          my ($addr,$byte) = @_;
411          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;
412    
         if ( $addr >= 0x6000 && $addr < 0x8000 ) {  
                 $self->vram( $addr - 0x6000 , $byte );  
         }  
   
413          if ( $addr == 0x8800 ) {          if ( $addr == 0x8800 ) {
414                  warn sprintf "sound ignored: %x\n", $byte;                  warn sprintf "sound ignored: %x\n", $byte;
415          }          }
# Line 378  sub write { Line 424  sub write {
424          return;          return;
425  }  }
426    
 =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 = <STDIN>;  
         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  
   
 my $show_R = 0;  
   
 sub cli {  
         my $self = shift;  
         my $a = $PC || confess "no pc?";  
         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 = '';  
                 }  
         }  
   
 }  
   
427  =head1 AUTHOR  =head1 AUTHOR
428    
429  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

Legend:
Removed from v.94  
changed lines
  Added in v.124

  ViewVC Help
Powered by ViewVC 1.1.26