/[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 109 by dpavlin, Fri Aug 3 10:29:33 2007 UTC Orao.pm revision 145 by dpavlin, Sun Aug 5 13:27:27 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 Session);
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.06
22    
23  =cut  =cut
24    
25  our $VERSION = '0.04';  our $VERSION = '0.06';
26    
27  =head1 SUMMARY  =head1 SUMMARY
28    
# Line 33  Emulator or Orao 8-bit 6502 machine popu Line 32  Emulator or Orao 8-bit 6502 machine popu
32    
33  =head1 FUNCTIONS  =head1 FUNCTIONS
34    
35  =head2 boot  =head2 run
   
 Start emulator, open L<Screen>, load initial ROM images, and render memory  
36    
37    my $orao = Orao->new({});  Start emulator, open L<Screen>, load initial ROM images, and start emulator loop
   $orao->boot;  
38    
39  =cut  =cut
40    
41  our $orao;  our $emu;
   
 select(STDERR); $| = 1;  
42    
43  sub boot {  sub run {
44          my $self = shift;          my $self = shift;
45    
46          warn "Orao calling upstream init\n";          warn "Orao calling upstream init\n";
47          $self->SUPER::init(          $self->SUPER::init(
48                  read => sub { $self->read( @_ ) },                  read => sub { $self->read( @_ ) },
# Line 58  sub boot { Line 53  sub boot {
53    
54          warn "emulating ", $#mem, " bytes of memory\n";          warn "emulating ", $#mem, " bytes of memory\n";
55    
56    #       $self->scale( 2 );
57    #       $self->show_mem( 1 );
58    
59          $self->open_screen;          $self->open_screen;
60          $self->load_rom({          $self->load_rom({
61                  0x1000 => 'dump/SCRINV.BIN',  #               0x1000 => 'dump/SCRINV.BIN',
62                  # should be 0x6000, but oraoemu has 2 byte prefix                  # should be 0x6000, but oraoemu has 2 byte prefix
63                  0x5FFE => 'dump/screen.dmp',  #               0x5FFE => '/home/dpavlin/orao/dump/screen.dmp',
64  #               0xC000 => 'rom/BAS12.ROM',  #               0xC000 => 'rom/Orao/BAS12.ROM',
65  #               0xE000 => 'rom/CRT12.ROM',  #               0xE000 => 'rom/Orao/CRT12.ROM',
66                  0xC000 => 'rom/BAS13.ROM',                  0xC000 => 'rom/Orao/BAS13.ROM',
67                  0xE000 => 'rom/CRT13.ROM',                  0xE000 => 'rom/Orao/CRT13.ROM',
68          });          });
69    
70  #       $PC = 0xDD11;   # BC  #       $PC = 0xDD11;   # BC
# Line 74  sub boot { Line 72  sub boot {
72    
73          $PC = 0xff89;          $PC = 0xff89;
74    
75          $orao = $self;          $emu = $self;
76    
77  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
78    
# Line 82  sub boot { Line 80  sub boot {
80          $self->trace( 0 );          $self->trace( 0 );
81          $self->debug( 0 );          $self->debug( 0 );
82    
83          warn "rendering video memory\n";          warn "rendering memory\n";
84          $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );          $self->render_mem( @mem );
85    
86          if ( $self->show_mem ) {          if ( $self->show_mem ) {
87    
                 warn "rendering memory map\n";  
   
                 $self->render_mem( @mem );  
   
88                  my @mmap = (                  my @mmap = (
89                          0x0000, 0x03FF, 'nulti blok',                          0x0000, 0x03FF, 'nulti blok',
90                          0x0400, 0x5FFF, 'korisnički RAM (23K)',                          0x0400, 0x5FFF, 'korisnički RAM (23K)',
# Line 102  sub boot { Line 96  sub boot {
96                          0xE000, 0xFFFF, 'sistemski ROM',                          0xE000, 0xFFFF, 'sistemski ROM',
97                  );                  );
98    
99                    print "Orao memory map:";
100    
101                    while ( @mmap ) {
102                            my ( $from, $to, $desc ) = splice(@mmap, 0, 3);
103                            printf("%04x-%04x %s\n", $from, $to, $desc);
104                    }
105    
106          }          }
107          $self->sync;  
108          $self->trace( $trace );          $self->trace( $trace );
109          $self->debug( $debug );          $self->debug( $debug );
110    
         #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );  
   
111          warn "Orao boot finished",          warn "Orao boot finished",
112                  $self->trace ? ' trace' : '',                  $self->trace ? ' trace' : '',
113                  $self->debug ? ' debug' : '',                  $self->debug ? ' debug' : '',
# Line 116  sub boot { Line 115  sub boot {
115    
116          M6502::reset();          M6502::reset();
117    
118          $self->booted( 1 );  #       $self->load_tape( '../oraoigre/bdash.tap' );
 }  
119    
120  =head2 run          $self->loop( sub {
121                    my $run_for = shift;
122  Run interactive emulation loop                  warn sprintf("about to exec from PC %04x for %d cycles\n", $PC, $run_for) if $self->trace;
123                    M6502::exec( $run_for );
124    $orao->run;                  $self->render_vram;
125            });
 =cut  
   
 sub run {  
         my $self = shift;  
   
         $self->boot if ( ! $self->booted );  
         $self->loop;  
126  };  };
127    
128    
129  =head1 Helper functions  =head1 Helper functions
130    
131  =head2 load_rom  =head2 write_chunk
132    
133  called to init memory and load initial rom images  Write chunk directly into memory, updateing vram if needed
134    
135    $orao->load_rom;    $emu->write_chunk( 0x1000, $chunk_data );
136    
137  =cut  =cut
138    
139  sub load_rom {  sub write_chunk {
     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 {  
140          my $self = shift;          my $self = shift;
141          my ( $addr, $chunk ) = @_;          my ( $addr, $chunk ) = @_;
142          $self->write_chunk( $addr, $chunk );          $self->SUPER::write_chunk( $addr, $chunk );
143          my $end = $addr + length($chunk);          my $end = $addr + length($chunk);
144          my ( $f, $t ) = ( 0x6000, 0x7fff );          my ( $f, $t ) = ( 0x6000, 0x7fff );
145    
# Line 172  sub _write_chunk { Line 152  sub _write_chunk {
152          $t = $end if ( $end < $t );          $t = $end if ( $end < $t );
153    
154          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);
155          $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );          $self->render_vram;
156          $self->render_mem( @mem ) if $self->show_mem;          $self->render_mem( @mem );
157  }  }
158    
159  =head2 load_image  =head2 load_image
160    
161  Load binary files, ROM images and Orao Emulator files  Load binary files, ROM images and Orao Emulator files
162    
163    $orao->load_image( '/path/to/file', 0x1000 );    $emu->load_image( '/path/to/file', 0x1000 );
164    
165  Returns true on success.  Returns true on success.
166    
# Line 202  sub load_image { Line 182  sub load_image {
182          if ( $size == 65538 ) {          if ( $size == 65538 ) {
183                  $addr = 0;                  $addr = 0;
184                  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;
185                  $self->_write_chunk( $addr, substr($buff,2) );                  $self->write_chunk( $addr, substr($buff,2) );
186                  return 1;                  return 1;
187          } elsif ( $size == 32800 ) {          } elsif ( $size == 32800 ) {
188                  $addr = 0;                  $addr = 0;
189                  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;
190                  $self->_write_chunk( $addr, substr($buff,0x20) );                  $self->write_chunk( $addr, substr($buff,0x20) );
191                  return 1;                  return 1;
192          }          }
         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 );  
193    
194            printf "loading %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size;
195            $self->write_chunk( $addr, $buff );
196          return 1;          return 1;
197  };  };
198    
 =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 ]  
                 )  
         );  
 }  
199    
200  =head1 Memory management  =head1 Memory management
201    
# Line 327  my $keyboard = { Line 247  my $keyboard = {
247                  } elsif ( $self->key_down( 'left shift' ) || $self->key_down( 'right shift' ) ) {                  } elsif ( $self->key_down( 'left shift' ) || $self->key_down( 'right shift' ) ) {
248                          warn "shift\n";                          warn "shift\n";
249                          return 16;                          return 16;
250    #               } elsif ( $self->tape ) {
251    #                       warn "has tape!";
252    #                       return 0;
253                  }                  }
254                  return $keyboard_none;                  return $keyboard_none;
255          },          },
# Line 415  my $keyboard = { Line 338  my $keyboard = {
338  sub read {  sub read {
339          my $self = shift;          my $self = shift;
340          my ($addr) = @_;          my ($addr) = @_;
341            return if ( $addr > 0xffff );
342          my $byte = $mem[$addr];          my $byte = $mem[$addr];
343          confess sprintf("can't find memory at address %04x",$addr) unless defined($byte);          confess sprintf("can't find memory at address %04x",$addr) unless defined($byte);
344          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;
# Line 447  sub read { Line 371  sub read {
371                  return $self->read_tape;                  return $self->read_tape;
372          }          }
373    
374          $self->mmap_pixel( $addr, 0, $byte, 0 );          $self->mmap_pixel( $addr, 0, $byte, 0 ) if $self->show_mem;
375          return $byte;          return $byte;
376  }  }
377    
# Line 465  sub write { Line 389  sub write {
389          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;
390    
391          if ( $addr == 0x8800 ) {          if ( $addr == 0x8800 ) {
392                    $self->write_tape( $byte );
393                  warn sprintf "sound ignored: %x\n", $byte;                  warn sprintf "sound ignored: %x\n", $byte;
394          }          }
395    
# Line 472  sub write { Line 397  sub write {
397                  confess sprintf "write access 0x%04x > 0xafff aborting\n", $addr;                  confess sprintf "write access 0x%04x > 0xafff aborting\n", $addr;
398          }          }
399    
400          $self->mmap_pixel( $addr, $byte, 0, 0 );          $self->mmap_pixel( $addr, $byte, 0, 0 ) if $self->show_mem;
   
401          $mem[$addr] = $byte;          $mem[$addr] = $byte;
402          return;          return;
403  }  }
404    
405  =head1 Command Line  =head1 Architecture specific
406    
407  Command-line debugging intrerface is implemented for communication with  =head2 render_vram
 emulated device  
408    
409  =head2 prompt  Render one frame of video ram
410    
411    my ( $entered_line, @p ) = $orao->prompt( $address, $last_command );    $self->render_vram;
412    
413  =cut  =cut
414    
415  my $last = 'r 1';  my @flip;
416    
417    foreach my $i ( 0 .. 255 ) {
418            my $t = 0;
419            $i & 0b00000001 and $t = $t | 0b10000000;
420            $i & 0b00000010 and $t = $t | 0b01000000;
421            $i & 0b00000100 and $t = $t | 0b00100000;
422            $i & 0b00001000 and $t = $t | 0b00010000;
423            $i & 0b00010000 and $t = $t | 0b00001000;
424            $i & 0b00100000 and $t = $t | 0b00000100;
425            $i & 0b01000000 and $t = $t | 0b00000010;
426            $i & 0b10000000 and $t = $t | 0b00000001;
427            #warn "$i = $t\n";
428            $flip[$i] = $t;
429    }
430    
431    
432  sub prompt {  sub render_vram {
433          my $self = shift;          my $self = shift;
434          $self->app->sync;  
435          my $a = shift;          my $pixels = pack("C*", map { $flip[$_] } @mem[ 0x6000 .. 0x7fff ]);
436          print $self->hexdump( $a ),  
437                  $last ? "[$last] " : '',          my $vram = SDL::Surface->new(
438                  "> ";                  -width => 256,
439          my $in = <STDIN>;                  -height => 256,
440          chomp($in);                  -depth => 1,    # 1 bit per pixel
441          warn "## prompt got: $in\n" if $self->debug;                  -pitch => 32,   # bytes per line
442          $in ||= $last;                  -from => $pixels,
443          $last = $in;          );
444          return ( $in, split(/\s+/, $in) ) if $in;          $vram->set_colors( 0, $black, $white );
445    
446            $self->render_frame( $vram );
447  }  }
448    
449  =head2 cli  =head2 cpu_PC
450    
451    $orao->cli();  Helper metod to set or get PC for current architecture
452    
453  =cut  =cut
454    
455  my $show_R = 0;  sub cpu_PC {
456            my ( $self, $addr ) = @_;
457            if ( defined($addr) ) {
458                    $PC = $addr;
459                    warn sprintf("running from PC %04x\n", $PC);
460            };
461            return $PC;
462    }
463    
464  sub cli {  =head1 SEE ALSO
         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;  
                         $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/^r/i ) {  
                         $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 ( ! $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 = '';  
                 }  
         }  
465    
466          return $run_for;  L<VRac>, L<M6502>, L<Screen>, L<Tape>
 }  
467    
468  =head1 AUTHOR  =head1 AUTHOR
469    

Legend:
Removed from v.109  
changed lines
  Added in v.145

  ViewVC Help
Powered by ViewVC 1.1.26