/[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

revision 47 by dpavlin, Tue Jul 31 10:16:36 2007 UTC revision 64 by dpavlin, Tue Jul 31 16:33:41 2007 UTC
# Line 3  package Orao; Line 3  package Orao;
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 M6502;  use M6502;
12    
13  use base qw(Class::Accessor M6502 Screen);  use base qw(Class::Accessor M6502 Screen Prefs);
14  __PACKAGE__->mk_accessors(qw(debug trace run_for mem_dump trace));  __PACKAGE__->mk_accessors(qw(run_for));
15    
16  =head1 NAME  =head1 NAME
17    
# Line 33  Emulator or Orao 8-bit 6502 machine popu Line 33  Emulator or Orao 8-bit 6502 machine popu
33    
34  =head2 init  =head2 init
35    
36  Start emulator  Start emulator, open L<Screen>, load initial ROM images, and render memory
37    
38  =cut  =cut
39    
# Line 46  sub init { Line 46  sub init {
46          warn "Orao calling upstream init\n";          warn "Orao calling upstream init\n";
47          $self->SUPER::init( $self, @_ );          $self->SUPER::init( $self, @_ );
48    
49          warn "staring Orao $Orao::VERSION emulation\n";          warn "Orao $Orao::VERSION emulation starting\n";
50    
51          $self->open_screen;          $self->open_screen;
52          $self->load_rom({          $self->load_rom({
# Line 62  sub init { Line 62  sub init {
62    
63  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
64    
65          my $trace = $self->trace;          my ( $trace, $debug ) = ( $self->trace, $self->debug );
66          $self->trace( 0 );          $self->trace( 0 );
67            $self->debug( 0 );
68    
69          if ( $self->show_mem ) {          if ( $self->show_mem ) {
70    
# Line 104  sub init { Line 105  sub init {
105          }          }
106          $self->sync;          $self->sync;
107          $self->trace( $trace );          $self->trace( $trace );
108            $self->debug( $debug );
109    
110          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );
111    
112          warn "Orao init finished", $self->trace ? ' trace on' : '', "\n";          warn "Orao init finished",
113                    $self->trace ? ' trace' : '',
114                    $self->debug ? ' debug' : '',
115                    "\n";
116    
117  }  }
118    
# Line 130  sub load_rom { Line 135  sub load_rom {
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 ) = @_;
# Line 156  sub _write_chunk { Line 157  sub _write_chunk {
157          }          }
158  }  }
159    
160    =head2 load_oraoemu
161    
162    Load binary files, ROM images and Orao Emulator files
163    
164      $orao->load_oraoemu( '/path/to/file', 0x1000 );
165    
166    Returns true on success.
167    
168    =cut
169    
170  sub load_oraoemu {  sub load_oraoemu {
171          my $self = shift;          my $self = shift;
172          my ( $path, $addr ) = @_;          my ( $path, $addr ) = @_;
173    
174            if ( ! -e $path ) {
175                    warn "ERROR: file $path doesn't exist\n";
176                    return;
177            }
178    
179          my $size = -s $path || confess "no size for $path: $!";          my $size = -s $path || confess "no size for $path: $!";
180    
181          my $buff = read_file( $path );          my $buff = read_file( $path );
# Line 168  sub load_oraoemu { Line 184  sub load_oraoemu {
184                  $addr = 0;                  $addr = 0;
185                  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;
186                  $self->_write_chunk( $addr, substr($buff,2) );                  $self->_write_chunk( $addr, substr($buff,2) );
187                  return;                  return 1;
188          } elsif ( $size == 32800 ) {          } elsif ( $size == 32800 ) {
189                  $addr = 0;                  $addr = 0;
190                  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;
191                  $self->_write_chunk( $addr, substr($buff,0x20) );                  $self->_write_chunk( $addr, substr($buff,0x20) );
192                  return;                  return 1;
193          }          }
194          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;
195          return $self->_write_chunk( $addr, $buff );          $self->_write_chunk( $addr, $buff );
196            return 1;
197    
198          my $chunk;          my $chunk;
199    
# Line 194  sub load_oraoemu { Line 211  sub load_oraoemu {
211    
212          $self->_write_chunk( $addr, $chunk );          $self->_write_chunk( $addr, $chunk );
213    
214            return 1;
215  };  };
216    
217  =head2 save_dump  =head2 save_dump
# Line 236  sub hexdump { Line 254  sub hexdump {
254          );          );
255  }  }
256    
 =head2 prompt  
   
   $orao->prompt( $address, $last_command );  
   
 =cut  
   
 sub prompt {  
         my $self = shift;  
         $self->app->sync;  
         my $a = shift;  
         my $last = shift;  
         print STDERR $self->hexdump( $a ),  
                 $last ? "[$last] " : '',  
                 "> ";  
         my $in = <STDIN>;  
         chomp($in);  
         $in ||= $last;  
         $last = $in;  
         return split(/\s+/, $in) if $in;  
 }  
   
257  =head1 Memory management  =head1 Memory management
258    
259  Orao implements all I/O using mmap addresses. This was main reason why  Orao implements all I/O using mmap addresses. This was main reason why
# Line 298  sub write { Line 295  sub write {
295                  $self->vram( $addr - 0x6000 , $byte );                  $self->vram( $addr - 0x6000 , $byte );
296          }          }
297    
         if ( $addr > 0xafff ) {  
                 warn sprintf "access to %04x above affff aborting\n", $addr;  
                 return -1;  
         }  
298          if ( $addr == 0x8800 ) {          if ( $addr == 0x8800 ) {
299                  warn sprintf "sound ignored: %x\n", $byte;                  warn sprintf "sound ignored: %x\n", $byte;
300          }          }
301    
302            if ( $addr > 0xafff ) {
303                    confess sprintf "write access 0x%04x > 0xafff aborting\n", $addr;
304            }
305    
306          $self->mmap_pixel( $addr, $byte, 0, 0 );          $self->mmap_pixel( $addr, $byte, 0, 0 );
307    
308          $mem[$addr] = $byte;          $mem[$addr] = $byte;
# Line 317  sub write { Line 314  sub write {
314  Command-line debugging intrerface is implemented for communication with  Command-line debugging intrerface is implemented for communication with
315  emulated device  emulated device
316    
317  =head2 cli  =head2 prompt
318    
319    $orao->cli();    my ( $entered_line, @p ) = $orao->prompt( $address, $last_command );
320    
321  =cut  =cut
322    
323  my $last = 'r 1';  my $last = 'r 1';
324    
325    sub prompt {
326            my $self = shift;
327            $self->app->sync;
328            my $a = shift;
329            print STDERR $self->hexdump( $a ),
330                    $last ? "[$last] " : '',
331                    "> ";
332            my $in = <STDIN>;
333            chomp($in);
334            warn "## prompt got: $in\n" if $self->debug;
335            $in ||= $last;
336            $last = $in;
337            return ( $in, split(/\s+/, $in) ) if $in;
338    }
339    
340    =head2 cli
341    
342      $orao->cli();
343    
344    =cut
345    
346  sub cli {  sub cli {
347          my $self = shift;          my $self = shift;
348          my $a = $PC || confess "no pc?";          my $a = $PC || confess "no pc?";
349          while ( my @v = $self->prompt( $a, $last ) ) {          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {
350                  my $c = shift @v;                  my $c = shift @v;
351                    next unless defined($c);
352                  my $v = shift @v;                  my $v = shift @v;
353                  $v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/;                  $v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/;
                 printf "## [%s] %s\n", ($v || 'undef'), join(",",@v) if $self->debug;  
354                  @v = map { hex($_) } @v;                  @v = map { hex($_) } @v;
355                    printf "## a: %04x parsed cli: c:%s v:%s %s\n", $a, $c, ($v || 'undef'), join(",",@v) if $self->debug;
356                  if ( $c =~ m/^[qx]/i ) {                  if ( $c =~ m/^[qx]/i ) {
357                          exit;                          exit;
358                  } elsif ( $c eq '?' ) {                  } elsif ( $c eq '?' ) {
359                            my $t = $self->trace ? 'on' : 'off' ;
360                            my $d = $self->debug ? 'on' : 'off' ;
361                          warn <<__USAGE__;                          warn <<__USAGE__;
362  uage:  Usage:
363    
364  x|q\t\texit  x|q\t\texit
365  e 6000 6010\tdump memory, +/- to walk forward/backward  e 6000 6010\tdump memory, +/- to walk forward/backward
366  m 1000 ff 00\tput ff 00 on 1000  m 1000 ff 00\tput ff 00 on 1000
367  j|u 1000\t\tjump (change pc)  j|u 1000\t\tjump (change pc)
368  r 42\t\trun 42 instruction opcodes  r 42\t\trun 42 instruction opcodes
369    t\t\ttrace [$t]
370    d\t\tdebug [$d]
371    
372  __USAGE__  __USAGE__
373                            warn sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S);
374                  } elsif ( $c =~ m/^e/i ) {                  } elsif ( $c =~ m/^e/i ) {
375                          $a ||= $v;                          $a = $v if defined($v);
376                          my $to = shift @v;                          my $to = shift @v;
377                          $to = $a + 32 if ( ! $to || $to <= $a );                          $to = $a + 32 if ( ! $to || $to <= $a );
378                          my $lines = int( ($to - $a - 8) / 8 );                          my $lines = int( ($to - $a + 8) / 8 );
379                          printf "## m %04x %04x lines: %d\n", $a, $to, $lines;                          printf "## e %04x %04x (%d bytes) lines: %d\n", $a, $to, ($to-$a), $lines;
380                          while ( $lines ) {                          while ( --$lines ) {
381                                  print $self->hexdump( $a );                                  print $self->hexdump( $a );
382                                  $a += 8;                                  $a += 8;
                                 $lines--;  
383                          }                          }
384                          $last = '+';                          $last = '+';
385                  } elsif ( $c =~ m/^\+/ ) {                  } elsif ( $c =~ m/^\+/ ) {
# Line 365  __USAGE__ Line 390  __USAGE__
390                          $a = $v;                          $a = $v;
391                          $self->poke_code( $a, @v );                          $self->poke_code( $a, @v );
392                          printf "poke %d bytes at %04x\n", $#v + 1, $a;                          printf "poke %d bytes at %04x\n", $#v + 1, $a;
393                            $last = '+';
394                  } elsif ( $c =~ m/^l/i ) {                  } elsif ( $c =~ m/^l/i ) {
395                          my $to = shift @v || 0x1000;                          my $to = shift @v || 0x1000;
396                          $a = $to;                          $a = $to;
397                          $self->load_oraoemu( $v, $a );                          $self->load_oraoemu( $v, $a );
398                            $last = '';
399                  } elsif ( $c =~ m/^s/i ) {                  } elsif ( $c =~ m/^s/i ) {
400                          $self->save_dump( $v || 'mem.dump', @v );                          $self->save_dump( $v || 'mem.dump', @v );
401                            $last = '';
402                  } elsif ( $c =~ m/^r/i ) {                  } elsif ( $c =~ m/^r/i ) {
403                          $run_for = $v || 1;                          $run_for = $v || 1;
404                          print "run_for $run_for instructions\n";                          print "run_for $run_for instructions\n";
# Line 380  __USAGE__ Line 408  __USAGE__
408                          printf "set pc to %04x\n", $to;                          printf "set pc to %04x\n", $to;
409                          $PC = $to;      # remember for restart                          $PC = $to;      # remember for restart
410                          $run_for = 1;                          $run_for = 1;
411                            $last = "r $run_for";
412                          last;                          last;
413                  } elsif ( $c =~ m/^t/ ) {                  } elsif ( $c =~ m/^t/ ) {
414                          $self->trace( not $self->trace );                          $self->trace( not $self->trace );
415                          print "trace ", $self->trace ? 'on' : 'off', "\n";                          print "trace ", $self->trace ? 'on' : 'off', "\n";
416                            $last = '';
417                    } elsif ( $c =~ m/^d/ ) {
418                            $self->debug( not $self->debug );
419                            print "debug ", $self->debug ? 'on' : 'off', "\n";
420                            $last = '';
421                  } else {                  } else {
422                          warn "# ignore $c\n";                          warn "# ignored $line\n" if ($line);
423                          last;                          $last = '';
424                  }                  }
425          }          }
426    
   
427  }  }
428    
429  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.47  
changed lines
  Added in v.64

  ViewVC Help
Powered by ViewVC 1.1.26