/[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 64 by dpavlin, Tue Jul 31 16:33:41 2007 UTC revision 105 by dpavlin, Thu Aug 2 21:55:06 2007 UTC
# Line 11  use Data::Dump qw/dump/; Line 11  use Data::Dump qw/dump/;
11  use M6502;  use M6502;
12    
13  use base qw(Class::Accessor M6502 Screen Prefs);  use base qw(Class::Accessor M6502 Screen Prefs);
14  __PACKAGE__->mk_accessors(qw(run_for));  __PACKAGE__->mk_accessors(qw(booted));
15    
16  =head1 NAME  =head1 NAME
17    
# Line 19  Orao - Orao emulator Line 19  Orao - Orao emulator
19    
20  =head1 VERSION  =head1 VERSION
21    
22  Version 0.02  Version 0.04
23    
24  =cut  =cut
25    
26  our $VERSION = '0.02';  our $VERSION = '0.04';
27    
28  =head1 SUMMARY  =head1 SUMMARY
29    
# Line 31  Emulator or Orao 8-bit 6502 machine popu Line 31  Emulator or Orao 8-bit 6502 machine popu
31    
32  =cut  =cut
33    
34  =head2 init  =head1 FUNCTIONS
35    
36    =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({});
41      $orao->boot;
42    
43  =cut  =cut
44    
45  our $orao;  our $orao;
46    
47  select(STDERR); $| = 1;  select(STDERR); $| = 1;
48    
49  sub init {  sub boot {
50          my $self = shift;          my $self = shift;
51          warn "Orao calling upstream init\n";          warn "Orao calling upstream init\n";
52          $self->SUPER::init( $self, @_ );          $self->SUPER::init(
53                    read => sub { $self->read( @_ ) },
54                    write => sub { $self->write( @_ ) },
55            );
56    
57          warn "Orao $Orao::VERSION emulation starting\n";          warn "Orao $Orao::VERSION emulation starting\n";
58    
59            warn "emulating ", $#mem, " bytes of memory\n";
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
65                    0x5FFE => 'dump/screen.dmp',
66                  0xC000 => 'rom/BAS12.ROM',                  0xC000 => 'rom/BAS12.ROM',
67                  0xE000 => 'rom/CRT12.ROM',                  0xE000 => 'rom/CRT12.ROM',
68          });          });
69    
70          $PC = 0xDD11;   # BC  #       $PC = 0xDD11;   # BC
71  #       $PC = 0xC274;   # MC  #       $PC = 0xC274;   # MC
72    
73            $PC = 0xff89;
74    
75          $orao = $self;          $orao = $self;
76    
77  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
# Line 66  sub init { Line 80  sub init {
80          $self->trace( 0 );          $self->trace( 0 );
81          $self->debug( 0 );          $self->debug( 0 );
82    
83            $self->render( @mem[ 0x6000 .. 0x7fff ] );
84    
85          if ( $self->show_mem ) {          if ( $self->show_mem ) {
86    
87                  warn "rendering memory map\n";                  warn "rendering memory map\n";
88    
89                    $self->render_mem( @mem );
90    
91                  my @mmap = (                  my @mmap = (
92                          0x0000, 0x03FF, 'nulti blok',                          0x0000, 0x03FF, 'nulti blok',
93                          0x0400, 0x5FFF, 'korisnički RAM (23K)',                          0x0400, 0x5FFF, 'korisnički RAM (23K)',
# Line 81  sub init { Line 99  sub init {
99                          0xE000, 0xFFFF, 'sistemski ROM',                          0xE000, 0xFFFF, 'sistemski ROM',
100                  );                  );
101    
                 foreach my $i ( 0 .. $#mmap / 3 ) {  
                         my $o = $i * 3;  
                         my ( $from, $to, $desc ) = @mmap[$o,$o+1,$o+2];  
                         printf "%04x - %04x - %s\n", $from, $to, $desc;  
                         for my $a ( $from .. $to ) {  
                                 if ( $a >= 0x6000 && $a < 0x8000 ) {  
                                         my $b = $self->read( $a );  
                                         $self->vram( $a - 0x6000, $b );  
                                 } else {  
                                         $self->read( $a );  
                                 }  
                         }  
                 }  
   
102          } else {          } else {
103    
104                  warn "rendering video memory\n";                  warn "rendering video memory\n";
105                  for my $a ( 0x6000 .. 0x7fff ) {                  $self->render( @mem[ 0x6000 .. 0x7fff ] );
                         $self->vram( $a - 0x6000, $mem[$a] );  
                 }  
106                    
107          }          }
108          $self->sync;          $self->sync;
# Line 109  sub init { Line 111  sub init {
111    
112          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );
113    
114          warn "Orao init finished",          warn "Orao boot finished",
115                  $self->trace ? ' trace' : '',                  $self->trace ? ' trace' : '',
116                  $self->debug ? ' debug' : '',                  $self->debug ? ' debug' : '',
117                  "\n";                  "\n";
118    
119            M6502::reset();
120    
121            $self->booted( 1 );
122  }  }
123    
124    =head2 run
125    
126    Run interactive emulation loop
127    
128      $orao->run;
129    
130    =cut
131    
132    sub run {
133            my $self = shift;
134    
135            $self->show_mem( 1 );
136    
137            $self->boot if ( ! $self->booted );
138            $self->loop;
139    };
140    
141    =head1 Helper functions
142    
143  =head2 load_rom  =head2 load_rom
144    
145  called to init memory and load initial rom images  called to init memory and load initial rom images
# Line 131  sub load_rom { Line 155  sub load_rom {
155    
156          foreach my $addr ( sort keys %$loaded_files ) {          foreach my $addr ( sort keys %$loaded_files ) {
157                  my $path = $loaded_files->{$addr};                  my $path = $loaded_files->{$addr};
158                  $self->load_oraoemu( $path, $addr );                  $self->load_image( $path, $addr );
159          }          }
160  }  }
161    
# Line 152  sub _write_chunk { Line 176  sub _write_chunk {
176          $t = $end if ( $end < $t );          $t = $end if ( $end < $t );
177    
178          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);
179          foreach my $a ( $f .. $t ) {  #       foreach my $a ( $f .. $t ) {
180                  $self->vram( $a - 0x6000 , $mem[ $a ] );  #               $self->vram( $a - 0x6000 , $mem[ $a ] );
181          }  #       }
182            $self->render( @mem[ 0x6000 .. 0x7fff ] );
183            $self->render_mem( @mem ) if $self->show_mem;
184  }  }
185    
186  =head2 load_oraoemu  =head2 load_image
187    
188  Load binary files, ROM images and Orao Emulator files  Load binary files, ROM images and Orao Emulator files
189    
190    $orao->load_oraoemu( '/path/to/file', 0x1000 );    $orao->load_image( '/path/to/file', 0x1000 );
191    
192  Returns true on success.  Returns true on success.
193    
194  =cut  =cut
195    
196  sub load_oraoemu {  sub load_image {
197          my $self = shift;          my $self = shift;
198          my ( $path, $addr ) = @_;          my ( $path, $addr ) = @_;
199    
# Line 248  sub hexdump { Line 274  sub hexdump {
274          return sprintf(" %04x %s\n", $a,          return sprintf(" %04x %s\n", $a,
275                  join(" ",                  join(" ",
276                          map {                          map {
277                                  sprintf( "%02x", $_ )                                  if ( defined($_) ) {
278                                            sprintf( "%02x", $_ )
279                                    } else {
280                                            '  '
281                                    }
282                          } @mem[ $a .. $a+8 ]                          } @mem[ $a .. $a+8 ]
283                  )                  )
284          );          );
# Line 269  Read from memory Line 299  Read from memory
299    
300  =cut  =cut
301    
302    my $keyboard_none = 255;
303    
304    my $keyboard = {
305            0x87FC => {
306                    'right'         => 16,
307                    'down'          => 128,
308                    'up'            => 192,
309                    'left'          => 224,
310                    'backspace' => 224,
311            },
312            0x87FD => sub {
313                    my ( $self, $key ) = @_;
314                    if ( $key eq 'return' ) {
315                            M6502::_write( 0xfc, 13 );
316                            warn "return\n";
317                            return 0;
318                    } elsif ( $key =~ m/ ctrl/ || $self->key_down( 'left ctrl' ) || $self->key_down( 'right ctrl' ) ) {
319                            warn "ctrl\n";
320                            return 16;
321                    }
322                    return $keyboard_none;
323            },
324            0x87FA => {
325                    'f4' => 16,
326                    'f3' => 128,
327                    'f2' => 192,
328                    'f1' => 224,
329            },
330            0x87FB => sub {
331                    my ( $self, $key ) = @_;
332                    if ( $key eq 'space' ) {
333                            return 32;
334                    } elsif ( $self->key_down( 'left shift' ) || $self->key_down( 'right shift' ) ) {
335                            warn "shift\n";
336                            return 16;
337                    }
338                    return $keyboard_none;
339            },
340            0x87F6 => {
341                    '6' => 16,
342                    't' => 128,
343                    'y' => 192,     # hr: z
344                    'r' => 224,
345            },
346            0x87F7 => {
347                    '5' => 32,
348                    '4' => 16,
349            },
350            0x87EE => {
351                    '7' => 16,
352                    'u' => 128,
353                    'i' => 192,
354                    'o' => 224,
355            },
356            0x87EF => {
357                    '8' => 32,
358                    '9' => 16,
359            },
360            0x87DE => {
361                    '1' => 16,
362                    'w' => 128,
363                    'q' => 192,
364                    'e' => 224,
365            },
366            0x87DF => {
367                    '2' => 32,
368                    '3' => 16,
369            },
370            0x87BE => {
371                    'm' => 16,
372                    'k' => 128,
373                    'j' => 192,
374                    'l' => 224,
375            },
376            0x87BF => {
377                    ',' => 32,      # <
378                    '.' => 16,      # >
379            },
380            0x877E => {
381                    'z' => 16,      # hr:y
382                    's' => 128,
383                    'a' => 192,
384                    'd' => 224,
385            },
386            0x877F => {
387                    'x' => 32,
388                    'c' => 16,
389            },
390            0x86FE => {
391                    'n' => 16,
392                    'g' => 128,
393                    'h' => 192,
394                    'f' => 224,
395            },
396            0x86FF => {
397                    'b' => 32,
398                    'v' => 16,
399            },
400            0x85FE => {
401                    '<' => 16,              # :
402                    '\\' => 128,    # ¾
403                    '\'' => 192,    # ę
404                    ';' => 224,             # č
405            },
406            0x85FF => {
407                    '/' => 32,
408                    'f11' => 16,    # ^
409            },
410            0x83FE => {
411                    'f12' => 16,    # ;
412                    '[' => 128,             # ¹
413                    ']' => 192,             # š
414                    'p' => 224,
415            },
416            0x83FF => {
417                    '-' => 32,
418                    '0' => 16,
419            },
420    };
421    
422  sub read {  sub read {
423          my $self = shift;          my $self = shift;
424          my ($addr) = @_;          my ($addr) = @_;
425          my $byte = $mem[$addr];          my $byte = $mem[$addr];
426            confess sprintf("can't find memory at address %04x",$addr) unless defined($byte);
427          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;
428    
429            # keyboard
430    
431            if ( defined( $keyboard->{$addr} ) ) {
432                    warn sprintf("keyboard port: %04x\n",$addr) if $self->trace;
433                    my $key = $self->key_pressed;
434                    if ( defined($key) ) {
435                            my $ret = $keyboard_none;
436                            my $r = $keyboard->{$addr} || confess "no definition for keyboard port found";
437                            if ( ref($r) eq 'CODE' ) {
438                                    $ret = $r->($self, $key);
439                            } elsif ( defined($r->{$key}) ) {
440                                    $ret = $r->{$key};
441                                    if ( ref($ret) eq 'CODE' ) {
442                                            $ret = $ret->($self);
443                                    }
444                            } else {
445                                    warn sprintf("keyboard port: %04x unknown key: '%s'\n", $addr, $key) if $debug;
446                            }
447                            warn sprintf("keyboard port: %04x key:%s code:%d\n",$addr,$key,$ret) if ( $ret != $keyboard_none );
448                            return $ret;
449                    }
450                    return $keyboard_none;
451            }
452    
453          $self->mmap_pixel( $addr, 0, $byte, 0 );          $self->mmap_pixel( $addr, 0, $byte, 0 );
454          return $byte;          return $byte;
455  }  }
# Line 326  sub prompt { Line 502  sub prompt {
502          my $self = shift;          my $self = shift;
503          $self->app->sync;          $self->app->sync;
504          my $a = shift;          my $a = shift;
505          print STDERR $self->hexdump( $a ),          print $self->hexdump( $a ),
506                  $last ? "[$last] " : '',                  $last ? "[$last] " : '',
507                  "> ";                  "> ";
508          my $in = <STDIN>;          my $in = <STDIN>;
# Line 343  sub prompt { Line 519  sub prompt {
519    
520  =cut  =cut
521    
522    my $show_R = 0;
523    
524  sub cli {  sub cli {
525          my $self = shift;          my $self = shift;
526          my $a = $PC || confess "no pc?";          my $a = $PC || confess "no pc?";
527            my $run_for = 0;
528            warn $self->dump_R() if $show_R;
529          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {
530                  my $c = shift @v;                  my $c = shift @v;
531                  next unless defined($c);                  next unless defined($c);
# Line 370  t\t\ttrace [$t] Line 550  t\t\ttrace [$t]
550  d\t\tdebug [$d]  d\t\tdebug [$d]
551    
552  __USAGE__  __USAGE__
553                          warn sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S);                          warn $self->dump_R;
554                            $last = '';
555                  } elsif ( $c =~ m/^e/i ) {                  } elsif ( $c =~ m/^e/i ) {
556                          $a = $v if defined($v);                          $a = $v if defined($v);
557                          my $to = shift @v;                          my $to = shift @v;
558                          $to = $a + 32 if ( ! $to || $to <= $a );                          $to = $a + 32 if ( ! $to || $to <= $a );
559                            $to = 0xffff if ( $to > 0xffff );
560                          my $lines = int( ($to - $a + 8) / 8 );                          my $lines = int( ($to - $a + 8) / 8 );
561                          printf "## e %04x %04x (%d bytes) lines: %d\n", $a, $to, ($to-$a), $lines;                          printf "## e %04x %04x (%d bytes) lines: %d\n", $a, $to, ($to-$a), $lines;
562                          while ( --$lines ) {                          while ( --$lines ) {
# Line 382  __USAGE__ Line 564  __USAGE__
564                                  $a += 8;                                  $a += 8;
565                          }                          }
566                          $last = '+';                          $last = '+';
567                            $show_R = 0;
568                  } elsif ( $c =~ m/^\+/ ) {                  } elsif ( $c =~ m/^\+/ ) {
569                          $a += 8;                          $a += 8;
570                            $show_R = 0;
571                  } elsif ( $c =~ m/^\-/ ) {                  } elsif ( $c =~ m/^\-/ ) {
572                          $a -= 8;                          $a -= 8;
573                            $show_R = 0;
574                  } elsif ( $c =~ m/^m/i ) {                  } elsif ( $c =~ m/^m/i ) {
575                          $a = $v;                          $a = $v if defined($v);
576                          $self->poke_code( $a, @v );                          $self->poke_code( $a, @v );
577                          printf "poke %d bytes at %04x\n", $#v + 1, $a;                          printf "poke %d bytes at %04x\n", $#v + 1, $a;
578                          $last = '+';                          $last = '+';
579                            $show_R = 0;
580                  } elsif ( $c =~ m/^l/i ) {                  } elsif ( $c =~ m/^l/i ) {
581                          my $to = shift @v || 0x1000;                          my $to = shift @v || 0x1000;
582                          $a = $to;                          $a = $to;
583                          $self->load_oraoemu( $v, $a );                          $self->load_image( $v, $a );
584                          $last = '';                          $last = '';
585                  } elsif ( $c =~ m/^s/i ) {                  } elsif ( $c =~ m/^s/i ) {
586                          $self->save_dump( $v || 'mem.dump', @v );                          $self->save_dump( $v || 'mem.dump', @v );
# Line 402  __USAGE__ Line 588  __USAGE__
588                  } elsif ( $c =~ m/^r/i ) {                  } elsif ( $c =~ m/^r/i ) {
589                          $run_for = $v || 1;                          $run_for = $v || 1;
590                          print "run_for $run_for instructions\n";                          print "run_for $run_for instructions\n";
591                            $show_R = 1;
592                          last;                          last;
593                  } elsif ( $c =~ m/^(u|j)/ ) {                  } elsif ( $c =~ m/^(u|j)/ ) {
594                          my $to = $v || $a;                          my $to = $v || $a;
# Line 409  __USAGE__ Line 596  __USAGE__
596                          $PC = $to;      # remember for restart                          $PC = $to;      # remember for restart
597                          $run_for = 1;                          $run_for = 1;
598                          $last = "r $run_for";                          $last = "r $run_for";
599                            $show_R = 1;
600                          last;                          last;
601                  } elsif ( $c =~ m/^t/ ) {                  } elsif ( $c =~ m/^t/ ) {
602                          $self->trace( not $self->trace );                          $self->trace( not $self->trace );
# Line 424  __USAGE__ Line 612  __USAGE__
612                  }                  }
613          }          }
614    
615            return $run_for;
616  }  }
617    
618  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26