/[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 99 by dpavlin, Thu Aug 2 16:21:17 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/;
11    use List::Util qw/first/;
12  use M6502;  use M6502;
13    
14  use base qw(Class::Accessor M6502 Screen Prefs);  use base qw(Class::Accessor M6502 Screen Prefs);
15  __PACKAGE__->mk_accessors(qw(run_for));  __PACKAGE__->mk_accessors(qw(booted));
16    
17  =head1 NAME  =head1 NAME
18    
# Line 19  Orao - Orao emulator Line 20  Orao - Orao emulator
20    
21  =head1 VERSION  =head1 VERSION
22    
23  Version 0.02  Version 0.04
24    
25  =cut  =cut
26    
27  our $VERSION = '0.02';  our $VERSION = '0.04';
28    
29  =head1 SUMMARY  =head1 SUMMARY
30    
# Line 31  Emulator or Orao 8-bit 6502 machine popu Line 32  Emulator or Orao 8-bit 6502 machine popu
32    
33  =cut  =cut
34    
35  =head2 init  my @kbd_ports = (
36        0x87FC,0x87FD,0x87FA,0x87FB,0x87F6,0x87F7,
37        0x87EE,0x87EF,0x87DE,0x87DF,0x87BE,0x87BF,
38        0x877E,0x877F,0x86FE,0x86FF,0x85FE,0x85FF,
39        0x83FE,0x83FF,
40    );
41    
42    =head1 FUNCTIONS
43    
44    =head2 boot
45    
46  Start emulator, open L<Screen>, load initial ROM images, and render memory  Start emulator, open L<Screen>, load initial ROM images, and render memory
47    
48      my $orao = Orao->new({});
49      $orao->boot;
50    
51  =cut  =cut
52    
53  our $orao;  our $orao;
54    
55  select(STDERR); $| = 1;  select(STDERR); $| = 1;
56    
57  sub init {  sub boot {
58          my $self = shift;          my $self = shift;
59          warn "Orao calling upstream init\n";          warn "Orao calling upstream init\n";
60          $self->SUPER::init( $self, @_ );          $self->SUPER::init(
61                    read => sub { $self->read( @_ ) },
62                    write => sub { $self->write( @_ ) },
63            );
64    
65          warn "Orao $Orao::VERSION emulation starting\n";          warn "Orao $Orao::VERSION emulation starting\n";
66    
67            warn "emulating ", $#mem, " bytes of memory\n";
68    
69          $self->open_screen;          $self->open_screen;
70          $self->load_rom({          $self->load_rom({
71                  0x1000 => 'dump/SCRINV.BIN',                  0x1000 => 'dump/SCRINV.BIN',
72                    # should be 0x6000, but oraoemu has 2 byte prefix
73                    0x5FFE => 'dump/screen.dmp',
74                  0xC000 => 'rom/BAS12.ROM',                  0xC000 => 'rom/BAS12.ROM',
75                  0xE000 => 'rom/CRT12.ROM',                  0xE000 => 'rom/CRT12.ROM',
76          });          });
77    
78          $PC = 0xDD11;   # BC  #       $PC = 0xDD11;   # BC
79  #       $PC = 0xC274;   # MC  #       $PC = 0xC274;   # MC
80    
81            $PC = 0xff89;
82    
83          $orao = $self;          $orao = $self;
84    
85  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
# Line 66  sub init { Line 88  sub init {
88          $self->trace( 0 );          $self->trace( 0 );
89          $self->debug( 0 );          $self->debug( 0 );
90    
91            $self->render( @mem[ 0x6000 .. 0x7fff ] );
92    
93          if ( $self->show_mem ) {          if ( $self->show_mem ) {
94    
95                  warn "rendering memory map\n";                  warn "rendering memory map\n";
96    
97                    $self->render_mem( @mem );
98    
99                  my @mmap = (                  my @mmap = (
100                          0x0000, 0x03FF, 'nulti blok',                          0x0000, 0x03FF, 'nulti blok',
101                          0x0400, 0x5FFF, 'korisnički RAM (23K)',                          0x0400, 0x5FFF, 'korisnički RAM (23K)',
# Line 81  sub init { Line 107  sub init {
107                          0xE000, 0xFFFF, 'sistemski ROM',                          0xE000, 0xFFFF, 'sistemski ROM',
108                  );                  );
109    
                 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 );  
                                 }  
                         }  
                 }  
   
110          } else {          } else {
111    
112                  warn "rendering video memory\n";                  warn "rendering video memory\n";
113                  for my $a ( 0x6000 .. 0x7fff ) {                  $self->render( @mem[ 0x6000 .. 0x7fff ] );
                         $self->vram( $a - 0x6000, $mem[$a] );  
                 }  
114                    
115          }          }
116          $self->sync;          $self->sync;
# Line 109  sub init { Line 119  sub init {
119    
120          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );
121    
122          warn "Orao init finished",          warn "Orao boot finished",
123                  $self->trace ? ' trace' : '',                  $self->trace ? ' trace' : '',
124                  $self->debug ? ' debug' : '',                  $self->debug ? ' debug' : '',
125                  "\n";                  "\n";
126    
127            M6502::reset();
128    
129            $self->booted( 1 );
130  }  }
131    
132    =head2 run
133    
134    Run interactive emulation loop
135    
136      $orao->run;
137    
138    =cut
139    
140    sub run {
141            my $self = shift;
142    
143            $self->show_mem( 1 );
144    
145            $self->boot if ( ! $self->booted );
146            $self->loop;
147    };
148    
149    =head1 Helper functions
150    
151  =head2 load_rom  =head2 load_rom
152    
153  called to init memory and load initial rom images  called to init memory and load initial rom images
# Line 131  sub load_rom { Line 163  sub load_rom {
163    
164          foreach my $addr ( sort keys %$loaded_files ) {          foreach my $addr ( sort keys %$loaded_files ) {
165                  my $path = $loaded_files->{$addr};                  my $path = $loaded_files->{$addr};
166                  $self->load_oraoemu( $path, $addr );                  $self->load_image( $path, $addr );
167          }          }
168  }  }
169    
# Line 152  sub _write_chunk { Line 184  sub _write_chunk {
184          $t = $end if ( $end < $t );          $t = $end if ( $end < $t );
185    
186          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);
187          foreach my $a ( $f .. $t ) {  #       foreach my $a ( $f .. $t ) {
188                  $self->vram( $a - 0x6000 , $mem[ $a ] );  #               $self->vram( $a - 0x6000 , $mem[ $a ] );
189          }  #       }
190            $self->render( @mem[ 0x6000 .. 0x7fff ] );
191            $self->render_mem( @mem ) if $self->show_mem;
192  }  }
193    
194  =head2 load_oraoemu  =head2 load_image
195    
196  Load binary files, ROM images and Orao Emulator files  Load binary files, ROM images and Orao Emulator files
197    
198    $orao->load_oraoemu( '/path/to/file', 0x1000 );    $orao->load_image( '/path/to/file', 0x1000 );
199    
200  Returns true on success.  Returns true on success.
201    
202  =cut  =cut
203    
204  sub load_oraoemu {  sub load_image {
205          my $self = shift;          my $self = shift;
206          my ( $path, $addr ) = @_;          my ( $path, $addr ) = @_;
207    
# Line 248  sub hexdump { Line 282  sub hexdump {
282          return sprintf(" %04x %s\n", $a,          return sprintf(" %04x %s\n", $a,
283                  join(" ",                  join(" ",
284                          map {                          map {
285                                  sprintf( "%02x", $_ )                                  if ( defined($_) ) {
286                                            sprintf( "%02x", $_ )
287                                    } else {
288                                            '  '
289                                    }
290                          } @mem[ $a .. $a+8 ]                          } @mem[ $a .. $a+8 ]
291                  )                  )
292          );          );
# Line 269  Read from memory Line 307  Read from memory
307    
308  =cut  =cut
309    
310    my $keyboard = {
311            0x87FC => {
312                    'right'         => 16,
313                    'down'          => 128,
314                    'up'            => 192,
315                    'left'          => 224,
316                    'backspace' => 224,
317            },
318            0x87FD => {
319                    'return' => sub {
320                            M6502::write( 0xfc, 13 );
321                            return 0;
322                    },
323                    'left ctrl'  => 16,
324                    'right ctrl' => 16,
325            },
326            0x87FA => {
327                    'f4' => 16,
328                    'f3' => 128,
329                    'f2' => 192,
330                    'f1' => 224,
331            },
332            0x87FB => {
333                    'space' => 32,
334                    'left shift' => 16,
335                    'right shift' => 16,
336            },
337            0x87F6 => {
338                    '6' => 16,
339                    't' => 128,
340                    'z' => 192,
341                    'r' => 224,
342            },
343            0x87F7 => {
344                    '5' => 32,
345                    '4' => 16,
346            },
347            0x87EE => {
348                    '7' => 16,
349                    'u' => 128,
350                    'i' => 192,
351                    'o' => 224,
352            },
353            0x87EF => {
354                    '8' => 32,
355                    '9' => 16,
356            },
357            0x87DE => {
358                    '1' => 16,
359                    'w' => 128,
360                    'q' => 192,
361                    'e' => 224,
362            },
363            0x87DF => {
364                    '2' => 32,
365                    '3' => 16,
366            },
367            0x87BE => {
368                    'm' => 16,
369                    'k' => 128,
370                    'j' => 192,
371                    'l' => 224,
372            },
373            0x87BF => {
374                    ',' => 32,
375                    '.' => 16,
376            },
377            0x877E => {
378                    'y' => 16,
379                    's' => 128,
380                    'a' => 192,
381                    'd' => 224,
382            },
383            0x877F => {
384                    'x' => 32,
385                    'c' => 16,
386            },
387            0x86FE => {
388                    'n' => 16,
389                    'g' => 128,
390                    'h' => 192,
391                    'f' => 224,
392            },
393            0x86FF => {
394                    'b' => 32,
395                    'c' => 16,
396            },
397            0x85FE => {
398                    ':' => 16,
399                    '\\' => 128,
400                    '\'' => 192,
401                    ';' => 224,
402                    '8' => 16,      # FIXME?
403            },
404            0x85FF => {
405                    '/' => 32,
406                    '6' => 16,      # FIXME?
407            },
408            0x83FE => {
409                    ';' => 16,
410                    '[' => 128,
411                    ']' => 192,
412                    'p' => 224,
413                    '=' => 16,      # FIXME?
414            },
415            0x83FF => {
416                    '-' => 32,
417                    '0' => 16,
418            },
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 ( first { $addr == $_ } @kbd_ports ) {
432                    warn sprintf("keyboard port: %04x\n",$addr) if $self->trace;
433                    my $key = $self->key_pressed;
434                    if ( defined($key) ) {
435                            my $r = $keyboard->{$addr} || confess "no definition for keyboard port found";
436                            if ( my $ret = $r->{$key} ) {
437                                    if ( ref($ret) eq 'CODE' ) {
438                                            $ret = $ret->();
439                                            warn "executed $key and got: $ret\n";
440                                    } else {
441                                            warn sprintf("keyboard port: %04x key: '%s' code: %02x\n", $addr, $key, $ret);
442                                    }
443                                    $mem[$addr] = $ret;
444                                    return $ret;
445                            } else {
446                                    warn sprintf("keyboard port: %04x unknown key: '%s'\n", $addr, $key) if $debug;
447                            }
448                            warn sprintf("keyboard port: %04x %s\n",$addr,dump( $r )) if $self->trace;
449                    }
450            }
451    
452          $self->mmap_pixel( $addr, 0, $byte, 0 );          $self->mmap_pixel( $addr, 0, $byte, 0 );
453          return $byte;          return $byte;
454  }  }
# Line 326  sub prompt { Line 501  sub prompt {
501          my $self = shift;          my $self = shift;
502          $self->app->sync;          $self->app->sync;
503          my $a = shift;          my $a = shift;
504          print STDERR $self->hexdump( $a ),          print $self->hexdump( $a ),
505                  $last ? "[$last] " : '',                  $last ? "[$last] " : '',
506                  "> ";                  "> ";
507          my $in = <STDIN>;          my $in = <STDIN>;
# Line 343  sub prompt { Line 518  sub prompt {
518    
519  =cut  =cut
520    
521    my $show_R = 0;
522    
523  sub cli {  sub cli {
524          my $self = shift;          my $self = shift;
525          my $a = $PC || confess "no pc?";          my $a = $PC || confess "no pc?";
526            my $run_for = 0;
527            warn $self->dump_R() if $show_R;
528          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {
529                  my $c = shift @v;                  my $c = shift @v;
530                  next unless defined($c);                  next unless defined($c);
# Line 370  t\t\ttrace [$t] Line 549  t\t\ttrace [$t]
549  d\t\tdebug [$d]  d\t\tdebug [$d]
550    
551  __USAGE__  __USAGE__
552                          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;
553                            $last = '';
554                  } elsif ( $c =~ m/^e/i ) {                  } elsif ( $c =~ m/^e/i ) {
555                          $a = $v if defined($v);                          $a = $v if defined($v);
556                          my $to = shift @v;                          my $to = shift @v;
557                          $to = $a + 32 if ( ! $to || $to <= $a );                          $to = $a + 32 if ( ! $to || $to <= $a );
558                            $to = 0xffff if ( $to > 0xffff );
559                          my $lines = int( ($to - $a + 8) / 8 );                          my $lines = int( ($to - $a + 8) / 8 );
560                          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;
561                          while ( --$lines ) {                          while ( --$lines ) {
# Line 382  __USAGE__ Line 563  __USAGE__
563                                  $a += 8;                                  $a += 8;
564                          }                          }
565                          $last = '+';                          $last = '+';
566                            $show_R = 0;
567                  } elsif ( $c =~ m/^\+/ ) {                  } elsif ( $c =~ m/^\+/ ) {
568                          $a += 8;                          $a += 8;
569                            $show_R = 0;
570                  } elsif ( $c =~ m/^\-/ ) {                  } elsif ( $c =~ m/^\-/ ) {
571                          $a -= 8;                          $a -= 8;
572                            $show_R = 0;
573                  } elsif ( $c =~ m/^m/i ) {                  } elsif ( $c =~ m/^m/i ) {
574                          $a = $v;                          $a = $v if defined($v);
575                          $self->poke_code( $a, @v );                          $self->poke_code( $a, @v );
576                          printf "poke %d bytes at %04x\n", $#v + 1, $a;                          printf "poke %d bytes at %04x\n", $#v + 1, $a;
577                          $last = '+';                          $last = '+';
578                            $show_R = 0;
579                  } elsif ( $c =~ m/^l/i ) {                  } elsif ( $c =~ m/^l/i ) {
580                          my $to = shift @v || 0x1000;                          my $to = shift @v || 0x1000;
581                          $a = $to;                          $a = $to;
582                          $self->load_oraoemu( $v, $a );                          $self->load_image( $v, $a );
583                          $last = '';                          $last = '';
584                  } elsif ( $c =~ m/^s/i ) {                  } elsif ( $c =~ m/^s/i ) {
585                          $self->save_dump( $v || 'mem.dump', @v );                          $self->save_dump( $v || 'mem.dump', @v );
# Line 402  __USAGE__ Line 587  __USAGE__
587                  } elsif ( $c =~ m/^r/i ) {                  } elsif ( $c =~ m/^r/i ) {
588                          $run_for = $v || 1;                          $run_for = $v || 1;
589                          print "run_for $run_for instructions\n";                          print "run_for $run_for instructions\n";
590                            $show_R = 1;
591                          last;                          last;
592                  } elsif ( $c =~ m/^(u|j)/ ) {                  } elsif ( $c =~ m/^(u|j)/ ) {
593                          my $to = $v || $a;                          my $to = $v || $a;
# Line 409  __USAGE__ Line 595  __USAGE__
595                          $PC = $to;      # remember for restart                          $PC = $to;      # remember for restart
596                          $run_for = 1;                          $run_for = 1;
597                          $last = "r $run_for";                          $last = "r $run_for";
598                            $show_R = 1;
599                          last;                          last;
600                  } elsif ( $c =~ m/^t/ ) {                  } elsif ( $c =~ m/^t/ ) {
601                          $self->trace( not $self->trace );                          $self->trace( not $self->trace );
# Line 424  __USAGE__ Line 611  __USAGE__
611                  }                  }
612          }          }
613    
614            return $run_for;
615  }  }
616    
617  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26