/[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 110 by dpavlin, Fri Aug 3 12:21:47 2007 UTC Orao.pm revision 126 by dpavlin, Sat Aug 4 15:43:28 2007 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5    
6  use Carp qw/confess/;  use Carp qw/confess/;
 use lib './lib';  
 #use Time::HiRes qw(time);  
7  use File::Slurp;  use File::Slurp;
8  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
9  use M6502;  use M6502; # import @mem $PC and friends
10    use Screen qw/$white $black/;
11    
12  use base qw(Class::Accessor M6502 Screen Prefs Tape);  use base qw(Class::Accessor VRac M6502 Screen Prefs Tape);
13  __PACKAGE__->mk_accessors(qw(booted));  #__PACKAGE__->mk_accessors(qw());
14    
15  =head1 NAME  =head1 NAME
16    
# Line 19  Orao - Orao emulator Line 18  Orao - Orao emulator
18    
19  =head1 VERSION  =head1 VERSION
20    
21  Version 0.04  Version 0.05
22    
23  =cut  =cut
24    
25  our $VERSION = '0.04';  our $VERSION = '0.05';
26    
27  =head1 SUMMARY  =head1 SUMMARY
28    
# Line 37  Emulator or Orao 8-bit 6502 machine popu Line 36  Emulator or Orao 8-bit 6502 machine popu
36    
37  Start emulator, open L<Screen>, load initial ROM images, and render memory  Start emulator, open L<Screen>, load initial ROM images, and render memory
38    
39    my $orao = Orao->new({});    my $emu = Orao->new({});
40    $orao->boot;    $emu->boot;
41    
42  =cut  =cut
43    
44  our $orao;  our $emu;
45    
46  select(STDERR); $| = 1;  select(STDERR); $| = 1;
47    
# Line 58  sub boot { Line 57  sub boot {
57    
58          warn "emulating ", $#mem, " bytes of memory\n";          warn "emulating ", $#mem, " bytes of memory\n";
59    
60    #       $self->scale( 2 );
61    
62          $self->open_screen;          $self->open_screen;
63          $self->load_rom({          $self->load_rom({
64                  0x1000 => 'dump/SCRINV.BIN',  #               0x1000 => 'dump/SCRINV.BIN',
65                  # should be 0x6000, but oraoemu has 2 byte prefix                  # should be 0x6000, but oraoemu has 2 byte prefix
66                  0x5FFE => 'dump/screen.dmp',  #               0x5FFE => '/home/dpavlin/orao/dump/screen.dmp',
67  #               0xC000 => 'rom/BAS12.ROM',  #               0xC000 => 'rom/Orao/BAS12.ROM',
68  #               0xE000 => 'rom/CRT12.ROM',  #               0xE000 => 'rom/Orao/CRT12.ROM',
69                  0xC000 => 'rom/BAS13.ROM',                  0xC000 => 'rom/Orao/BAS13.ROM',
70                  0xE000 => 'rom/CRT13.ROM',                  0xE000 => 'rom/Orao/CRT13.ROM',
71          });          });
72    
73  #       $PC = 0xDD11;   # BC  #       $PC = 0xDD11;   # BC
# Line 74  sub boot { Line 75  sub boot {
75    
76          $PC = 0xff89;          $PC = 0xff89;
77    
78          $orao = $self;          $emu = $self;
79    
80  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
81    
# Line 123  sub boot { Line 124  sub boot {
124    
125  Run interactive emulation loop  Run interactive emulation loop
126    
127    $orao->run;    $emu->run;
128    
129  =cut  =cut
130    
# Line 132  sub run { Line 133  sub run {
133    
134          $self->boot if ( ! $self->booted );          $self->boot if ( ! $self->booted );
135    
136          $self->load_tape( '../oraoigre/bdash.tap' );  #       $self->load_tape( '../oraoigre/bdash.tap' );
137    
138          $self->loop;          $self->loop( sub {
139                    M6502::exec( $_[0] );
140                    $self->render_vram;
141            });
142  };  };
143    
144  =head1 Helper functions  =head1 Helper functions
145    
 =head2 load_rom  
   
 called to init memory and load initial rom images  
   
   $orao->load_rom;  
   
146  =cut  =cut
147    
 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 );  
         }  
 }  
   
148  # write chunk directly into memory, updateing vram if needed  # write chunk directly into memory, updateing vram if needed
149  sub _write_chunk {  sub _write_chunk {
150          my $self = shift;          my $self = shift;
# Line 183  sub _write_chunk { Line 170  sub _write_chunk {
170    
171  Load binary files, ROM images and Orao Emulator files  Load binary files, ROM images and Orao Emulator files
172    
173    $orao->load_image( '/path/to/file', 0x1000 );    $emu->load_image( '/path/to/file', 0x1000 );
174    
175  Returns true on success.  Returns true on success.
176    
# Line 236  sub load_image { Line 223  sub load_image {
223          return 1;          return 1;
224  };  };
225    
 =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 ]  
                 )  
         );  
 }  
226    
227  =head1 Memory management  =head1 Memory management
228    
# Line 484  sub write { Line 428  sub write {
428          return;          return;
429  }  }
430    
431  =head1 Command Line  =head2 render_vram
   
 Command-line debugging intrerface is implemented for communication with  
 emulated device  
432    
433  =head2 prompt  Render one frame of video ram
434    
435    my ( $entered_line, @p ) = $orao->prompt( $address, $last_command );    $self->render_vram;
436    
437  =cut  =cut
438    
439  my $last = 'r 1';  my @flip;
440    
441  sub prompt {  foreach my $i ( 0 .. 255 ) {
442          my $self = shift;          my $t = 0;
443          $self->app->sync;          $i & 0b00000001 and $t = $t | 0b10000000;
444          my $a = shift;          $i & 0b00000010 and $t = $t | 0b01000000;
445          print $self->hexdump( $a ),          $i & 0b00000100 and $t = $t | 0b00100000;
446                  $last ? "[$last] " : '',          $i & 0b00001000 and $t = $t | 0b00010000;
447                  "> ";          $i & 0b00010000 and $t = $t | 0b00001000;
448          my $in = <STDIN>;          $i & 0b00100000 and $t = $t | 0b00000100;
449          chomp($in);          $i & 0b01000000 and $t = $t | 0b00000010;
450          warn "## prompt got: $in\n" if $self->debug;          $i & 0b10000000 and $t = $t | 0b00000001;
451          $in ||= $last;          #warn "$i = $t\n";
452          $last = $in;          $flip[$i] = $t;
         return ( $in, split(/\s+/, $in) ) if $in;  
453  }  }
454    
 =head2 cli  
455    
456    $orao->cli();  sub render_vram {
457            my $self = shift;
458    
459  =cut          return unless $self->booted;
460    
461  my $show_R = 0;          my $pixels = pack("C*", map { $flip[$_] } @mem[ 0x6000 .. 0x7fff ]);
462    
463  sub cli {          my $vram = SDL::Surface->new(
464          my $self = shift;                  -width => 256,
465          my $a = $PC || confess "no pc?";                  -height => 256,
466          my $run_for = 0;                  -depth => 1,    # 1 bit per pixel
467          warn $self->dump_R() if $show_R;                  -pitch => 32,   # bytes per line
468          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {                  -from => $pixels,
469                  my $c = shift @v;          );
470                  next unless defined($c);          $vram->set_colors( 0, $black, $white );
                 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;  
                         $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 ) {  
                                 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/^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)/i ) {  
                         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/^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";  
                         $last = '';  
                 } elsif ( $c =~ m/^d/i ) {  
                         $self->debug( not $self->debug );  
                         print "debug ", $self->debug ? 'on' : 'off', "\n";  
                         $last = '';  
                 } else {  
                         warn "# ignored $line\n" if ($line);  
                         $last = '';  
                 }  
         }  
471    
472          return $run_for;          $self->render_frame( $vram );
473  }  }
474    
475  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.110  
changed lines
  Added in v.126

  ViewVC Help
Powered by ViewVC 1.1.26