/[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 64 by dpavlin, Tue Jul 31 16:33:41 2007 UTC Orao.pm revision 125 by dpavlin, Sat Aug 4 15:09:44 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;
10    use Screen qw/$white $black/;
11    
12  use base qw(Class::Accessor M6502 Screen Prefs);  use base qw(Class::Accessor VRac M6502 Screen Prefs Tape);
13  __PACKAGE__->mk_accessors(qw(run_for));  #__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.02  Version 0.05
22    
23  =cut  =cut
24    
25  our $VERSION = '0.02';  our $VERSION = '0.05';
26    
27  =head1 SUMMARY  =head1 SUMMARY
28    
# Line 31  Emulator or Orao 8-bit 6502 machine popu Line 30  Emulator or Orao 8-bit 6502 machine popu
30    
31  =cut  =cut
32    
33  =head2 init  =head1 FUNCTIONS
34    
35    =head2 boot
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 $emu = Orao->new({});
40      $emu->boot;
41    
42  =cut  =cut
43    
44  our $orao;  our $emu;
45    
46  select(STDERR); $| = 1;  select(STDERR); $| = 1;
47    
48  sub init {  sub boot {
49          my $self = shift;          my $self = shift;
50          warn "Orao calling upstream init\n";          warn "Orao calling upstream init\n";
51          $self->SUPER::init( $self, @_ );          $self->SUPER::init(
52                    read => sub { $self->read( @_ ) },
53                    write => sub { $self->write( @_ ) },
54            );
55    
56          warn "Orao $Orao::VERSION emulation starting\n";          warn "Orao $Orao::VERSION emulation starting\n";
57    
58            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                  0xC000 => 'rom/BAS12.ROM',                  # should be 0x6000, but oraoemu has 2 byte prefix
66                  0xE000 => 'rom/CRT12.ROM',  #               0x5FFE => '/home/dpavlin/orao/dump/screen.dmp',
67    #               0xC000 => 'rom/Orao/BAS12.ROM',
68    #               0xE000 => 'rom/Orao/CRT12.ROM',
69                    0xC000 => 'rom/Orao/BAS13.ROM',
70                    0xE000 => 'rom/Orao/CRT13.ROM',
71          });          });
72    
73          $PC = 0xDD11;   # BC  #       $PC = 0xDD11;   # BC
74  #       $PC = 0xC274;   # MC  #       $PC = 0xC274;   # MC
75    
76          $orao = $self;          $PC = 0xff89;
77    
78            $emu = $self;
79    
80  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
81    
# Line 66  sub init { Line 83  sub init {
83          $self->trace( 0 );          $self->trace( 0 );
84          $self->debug( 0 );          $self->debug( 0 );
85    
86            warn "rendering video memory\n";
87            $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );
88    
89          if ( $self->show_mem ) {          if ( $self->show_mem ) {
90    
91                  warn "rendering memory map\n";                  warn "rendering memory map\n";
92    
93                    $self->render_mem( @mem );
94    
95                  my @mmap = (                  my @mmap = (
96                          0x0000, 0x03FF, 'nulti blok',                          0x0000, 0x03FF, 'nulti blok',
97                          0x0400, 0x5FFF, 'korisnički RAM (23K)',                          0x0400, 0x5FFF, 'korisnički RAM (23K)',
# Line 81  sub init { Line 103  sub init {
103                          0xE000, 0xFFFF, 'sistemski ROM',                          0xE000, 0xFFFF, 'sistemski ROM',
104                  );                  );
105    
                 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 );  
                                 }  
                         }  
                 }  
   
         } else {  
   
                 warn "rendering video memory\n";  
                 for my $a ( 0x6000 .. 0x7fff ) {  
                         $self->vram( $a - 0x6000, $mem[$a] );  
                 }  
           
106          }          }
107          $self->sync;          $self->sync;
108          $self->trace( $trace );          $self->trace( $trace );
# Line 109  sub init { Line 110  sub init {
110    
111          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );
112    
113          warn "Orao init finished",          warn "Orao boot finished",
114                  $self->trace ? ' trace' : '',                  $self->trace ? ' trace' : '',
115                  $self->debug ? ' debug' : '',                  $self->debug ? ' debug' : '',
116                  "\n";                  "\n";
117    
118            M6502::reset();
119    
120            $self->booted( 1 );
121  }  }
122    
123  =head2 load_rom  =head2 run
124    
125  called to init memory and load initial rom images  Run interactive emulation loop
126    
127    $orao->load_rom;    $emu->run;
128    
129  =cut  =cut
130    
131  sub load_rom {  sub run {
132      my ($self, $loaded_files) = @_;          my $self = shift;
133    
134      #my $time_base = time();          $self->boot if ( ! $self->booted );
135    
136          foreach my $addr ( sort keys %$loaded_files ) {  #       $self->load_tape( '../oraoigre/bdash.tap' );
137                  my $path = $loaded_files->{$addr};  
138                  $self->load_oraoemu( $path, $addr );          $self->loop;
139          }  };
140  }  
141    =head1 Helper functions
142    
143    =cut
144    
145  # write chunk directly into memory, updateing vram if needed  # write chunk directly into memory, updateing vram if needed
146  sub _write_chunk {  sub _write_chunk {
# Line 152  sub _write_chunk { Line 159  sub _write_chunk {
159          $t = $end if ( $end < $t );          $t = $end if ( $end < $t );
160    
161          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);
162          foreach my $a ( $f .. $t ) {          $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );
163                  $self->vram( $a - 0x6000 , $mem[ $a ] );          $self->render_mem( @mem ) if $self->show_mem;
         }  
164  }  }
165    
166  =head2 load_oraoemu  =head2 load_image
167    
168  Load binary files, ROM images and Orao Emulator files  Load binary files, ROM images and Orao Emulator files
169    
170    $orao->load_oraoemu( '/path/to/file', 0x1000 );    $emu->load_image( '/path/to/file', 0x1000 );
171    
172  Returns true on success.  Returns true on success.
173    
174  =cut  =cut
175    
176  sub load_oraoemu {  sub load_image {
177          my $self = shift;          my $self = shift;
178          my ( $path, $addr ) = @_;          my ( $path, $addr ) = @_;
179    
# Line 214  sub load_oraoemu { Line 220  sub load_oraoemu {
220          return 1;          return 1;
221  };  };
222    
 =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 {  
                                 sprintf( "%02x", $_ )  
                         } @mem[ $a .. $a+8 ]  
                 )  
         );  
 }  
223    
224  =head1 Memory management  =head1 Memory management
225    
# Line 269  Read from memory Line 236  Read from memory
236    
237  =cut  =cut
238    
239    my $keyboard_none = 255;
240    
241    my $keyboard = {
242            0x87FC => {
243                    'right'         => 16,
244                    'down'          => 128,
245                    'up'            => 192,
246                    'left'          => 224,
247                    'backspace' => 224,
248            },
249            0x87FD => sub {
250                    my ( $self, $key ) = @_;
251                    if ( $key eq 'return' ) {
252                            M6502::_write( 0xfc, 13 );
253                            warn "return\n";
254                            return 0;
255                    } elsif ( $key =~ m/ ctrl/ || $self->key_down( 'left ctrl' ) || $self->key_down( 'right ctrl' ) ) {
256                            warn "ctrl\n";
257                            return 16;
258                    }
259                    return $keyboard_none;
260            },
261            0x87FA => {
262                    'f4' => 16,
263                    'f3' => 128,
264                    'f2' => 192,
265                    'f1' => 224,
266            },
267            0x87FB => sub {
268                    my ( $self, $key ) = @_;
269                    if ( $key eq 'space' ) {
270                            return 32;
271                    } elsif ( $self->key_down( 'left shift' ) || $self->key_down( 'right shift' ) ) {
272                            warn "shift\n";
273                            return 16;
274    #               } elsif ( $self->tape ) {
275    #                       warn "has tape!";
276    #                       return 0;
277                    }
278                    return $keyboard_none;
279            },
280            0x87F6 => {
281                    '6' => 16,
282                    't' => 128,
283                    'y' => 192,     # hr: z
284                    'r' => 224,
285            },
286            0x87F7 => {
287                    '5' => 32,
288                    '4' => 16,
289            },
290            0x87EE => {
291                    '7' => 16,
292                    'u' => 128,
293                    'i' => 192,
294                    'o' => 224,
295            },
296            0x87EF => {
297                    '8' => 32,
298                    '9' => 16,
299            },
300            0x87DE => {
301                    '1' => 16,
302                    'w' => 128,
303                    'q' => 192,
304                    'e' => 224,
305            },
306            0x87DF => {
307                    '2' => 32,
308                    '3' => 16,
309            },
310            0x87BE => {
311                    'm' => 16,
312                    'k' => 128,
313                    'j' => 192,
314                    'l' => 224,
315            },
316            0x87BF => {
317                    ',' => 32,      # <
318                    '.' => 16,      # >
319            },
320            0x877E => {
321                    'z' => 16,      # hr:y
322                    's' => 128,
323                    'a' => 192,
324                    'd' => 224,
325            },
326            0x877F => {
327                    'x' => 32,
328                    'c' => 16,
329            },
330            0x86FE => {
331                    'n' => 16,
332                    'g' => 128,
333                    'h' => 192,
334                    'f' => 224,
335            },
336            0x86FF => {
337                    'b' => 32,
338                    'v' => 16,
339            },
340            0x85FE => {
341                    '<' => 16,              # :
342                    '\\' => 128,    # ¾
343                    '\'' => 192,    # ę
344                    ';' => 224,             # č
345            },
346            0x85FF => {
347                    '/' => 32,
348                    'f11' => 16,    # ^
349            },
350            0x83FE => {
351                    'f12' => 16,    # ;
352                    '[' => 128,             # ¹
353                    ']' => 192,             # š
354                    'p' => 224,
355            },
356            0x83FF => {
357                    '-' => 32,
358                    '0' => 16,
359            },
360    };
361    
362  sub read {  sub read {
363          my $self = shift;          my $self = shift;
364          my ($addr) = @_;          my ($addr) = @_;
365          my $byte = $mem[$addr];          my $byte = $mem[$addr];
366            confess sprintf("can't find memory at address %04x",$addr) unless defined($byte);
367          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;
368    
369            # keyboard
370    
371            if ( defined( $keyboard->{$addr} ) ) {
372                    warn sprintf("keyboard port: %04x\n",$addr) if $self->trace;
373                    my $key = $self->key_pressed;
374                    if ( defined($key) ) {
375                            my $ret = $keyboard_none;
376                            my $r = $keyboard->{$addr} || confess "no definition for keyboard port found";
377                            if ( ref($r) eq 'CODE' ) {
378                                    $ret = $r->($self, $key);
379                            } elsif ( defined($r->{$key}) ) {
380                                    $ret = $r->{$key};
381                                    if ( ref($ret) eq 'CODE' ) {
382                                            $ret = $ret->($self);
383                                    }
384                            } else {
385                                    warn sprintf("keyboard port: %04x unknown key: '%s'\n", $addr, $key) if $debug;
386                            }
387                            warn sprintf("keyboard port: %04x key:%s code:%d\n",$addr,$key,$ret) if ( $ret != $keyboard_none );
388                            return $ret;
389                    }
390                    return $keyboard_none;
391            }
392    
393            if ( $addr == 0x87ff ) {
394                    return $self->read_tape;
395            }
396    
397          $self->mmap_pixel( $addr, 0, $byte, 0 );          $self->mmap_pixel( $addr, 0, $byte, 0 );
398          return $byte;          return $byte;
399  }  }
# Line 291  sub write { Line 411  sub write {
411          my ($addr,$byte) = @_;          my ($addr,$byte) = @_;
412          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;
413    
         if ( $addr >= 0x6000 && $addr < 0x8000 ) {  
                 $self->vram( $addr - 0x6000 , $byte );  
         }  
   
414          if ( $addr == 0x8800 ) {          if ( $addr == 0x8800 ) {
415                  warn sprintf "sound ignored: %x\n", $byte;                  warn sprintf "sound ignored: %x\n", $byte;
416          }          }
# Line 309  sub write { Line 425  sub write {
425          return;          return;
426  }  }
427    
428  =head1 Command Line  =head2 render_vram
   
 Command-line debugging intrerface is implemented for communication with  
 emulated device  
429    
430  =head2 prompt  Render one frame of video ram
431    
432    my ( $entered_line, @p ) = $orao->prompt( $address, $last_command );    $self->render_vram( @video_memory );
433    
434  =cut  =cut
435    
436  my $last = 'r 1';  my @flip;
437    
438  sub prompt {  foreach my $i ( 0 .. 255 ) {
439          my $self = shift;          my $t = 0;
440          $self->app->sync;          $i & 0b00000001 and $t = $t | 0b10000000;
441          my $a = shift;          $i & 0b00000010 and $t = $t | 0b01000000;
442          print STDERR $self->hexdump( $a ),          $i & 0b00000100 and $t = $t | 0b00100000;
443                  $last ? "[$last] " : '',          $i & 0b00001000 and $t = $t | 0b00010000;
444                  "> ";          $i & 0b00010000 and $t = $t | 0b00001000;
445          my $in = <STDIN>;          $i & 0b00100000 and $t = $t | 0b00000100;
446          chomp($in);          $i & 0b01000000 and $t = $t | 0b00000010;
447          warn "## prompt got: $in\n" if $self->debug;          $i & 0b10000000 and $t = $t | 0b00000001;
448          $in ||= $last;          #warn "$i = $t\n";
449          $last = $in;          $flip[$i] = $t;
         return ( $in, split(/\s+/, $in) ) if $in;  
450  }  }
451    
 =head2 cli  
452    
453    $orao->cli();  sub render_vram {
454            my $self = shift;
455    
456  =cut          confess "no data?" unless (@_);
457            confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8));
458    
459  sub cli {          return unless $self->booted;
460          my $self = shift;  
461          my $a = $PC || confess "no pc?";          my $pixels = pack("C*", map { $flip[$_] } @_);
462          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {  
463                  my $c = shift @v;          my $vram = SDL::Surface->new(
464                  next unless defined($c);                  -width => 256,
465                  my $v = shift @v;                  -height => 256,
466                  $v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/;                  -depth => 1,    # 1 bit per pixel
467                  @v = map { hex($_) } @v;                  -pitch => 32,   # bytes per line
468                  printf "## a: %04x parsed cli: c:%s v:%s %s\n", $a, $c, ($v || 'undef'), join(",",@v) if $self->debug;                  -from => $pixels,
469                  if ( $c =~ m/^[qx]/i ) {          );
470                          exit;          $vram->set_colors( 0, $black, $white );
                 } 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 sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S);  
                 } elsif ( $c =~ m/^e/i ) {  
                         $a = $v if defined($v);  
                         my $to = shift @v;  
                         $to = $a + 32 if ( ! $to || $to <= $a );  
                         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 = '+';  
                 } elsif ( $c =~ m/^\+/ ) {  
                         $a += 8;  
                 } elsif ( $c =~ m/^\-/ ) {  
                         $a -= 8;  
                 } elsif ( $c =~ m/^m/i ) {  
                         $a = $v;  
                         $self->poke_code( $a, @v );  
                         printf "poke %d bytes at %04x\n", $#v + 1, $a;  
                         $last = '+';  
                 } elsif ( $c =~ m/^l/i ) {  
                         my $to = shift @v || 0x1000;  
                         $a = $to;  
                         $self->load_oraoemu( $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";  
                         last;  
                 } elsif ( $c =~ m/^(u|j)/ ) {  
                         my $to = $v || $a;  
                         printf "set pc to %04x\n", $to;  
                         $PC = $to;      # remember for restart  
                         $run_for = 1;  
                         $last = "r $run_for";  
                         last;  
                 } elsif ( $c =~ m/^t/ ) {  
                         $self->trace( not $self->trace );  
                         print "trace ", $self->trace ? 'on' : 'off', "\n";  
                         $last = '';  
                 } elsif ( $c =~ m/^d/ ) {  
                         $self->debug( not $self->debug );  
                         print "debug ", $self->debug ? 'on' : 'off', "\n";  
                         $last = '';  
                 } else {  
                         warn "# ignored $line\n" if ($line);  
                         $last = '';  
                 }  
         }  
471    
472            $self->render_frame( $vram );
473  }  }
474    
475  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26