/[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 132 by dpavlin, Sat Aug 4 21:04:05 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);  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  Start emulator, open L<Screen>, load initial ROM images, and render memory  =head2 run
36    
37  =cut  Start emulator, open L<Screen>, load initial ROM images, and start emulator loop
38    
39  our $orao;  =cut
40    
41  select(STDERR); $| = 1;  our $emu;
42    
43  sub init {  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, @_ );          $self->SUPER::init(
48                    read => sub { $self->read( @_ ) },
49                    write => sub { $self->write( @_ ) },
50            );
51    
52          warn "Orao $Orao::VERSION emulation starting\n";          warn "Orao $Orao::VERSION emulation starting\n";
53    
54            warn "emulating ", $#mem, " bytes of memory\n";
55    
56    #       $self->scale( 2 );
57    
58          $self->open_screen;          $self->open_screen;
59          $self->load_rom({          $self->load_rom({
60                  0x1000 => 'dump/SCRINV.BIN',  #               0x1000 => 'dump/SCRINV.BIN',
61                  0xC000 => 'rom/BAS12.ROM',                  # should be 0x6000, but oraoemu has 2 byte prefix
62                  0xE000 => 'rom/CRT12.ROM',  #               0x5FFE => '/home/dpavlin/orao/dump/screen.dmp',
63    #               0xC000 => 'rom/Orao/BAS12.ROM',
64    #               0xE000 => 'rom/Orao/CRT12.ROM',
65                    0xC000 => 'rom/Orao/BAS13.ROM',
66                    0xE000 => 'rom/Orao/CRT13.ROM',
67          });          });
68    
69          $PC = 0xDD11;   # BC  #       $PC = 0xDD11;   # BC
70  #       $PC = 0xC274;   # MC  #       $PC = 0xC274;   # MC
71    
72          $orao = $self;          $PC = 0xff89;
73    
74            $emu = $self;
75    
76  #       $self->prompt( 0x1000 );  #       $self->prompt( 0x1000 );
77    
# Line 66  sub init { Line 79  sub init {
79          $self->trace( 0 );          $self->trace( 0 );
80          $self->debug( 0 );          $self->debug( 0 );
81    
82            warn "rendering video memory\n";
83            $self->render_vram;
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 );  
                                 }  
                         }  
                 }  
   
         } else {  
   
                 warn "rendering video memory\n";  
                 for my $a ( 0x6000 .. 0x7fff ) {  
                         $self->vram( $a - 0x6000, $mem[$a] );  
                 }  
           
102          }          }
103          $self->sync;          $self->sync;
104          $self->trace( $trace );          $self->trace( $trace );
# Line 109  sub init { Line 106  sub init {
106    
107          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );          #( $A, $P, $X, $Y, $S, $IPeriod ) = ( 1, 2, 3, 4, 5, 6 );
108    
109          warn "Orao init finished",          warn "Orao boot finished",
110                  $self->trace ? ' trace' : '',                  $self->trace ? ' trace' : '',
111                  $self->debug ? ' debug' : '',                  $self->debug ? ' debug' : '',
112                  "\n";                  "\n";
113    
114  }          M6502::reset();
115    
116  =head2 load_rom  #       $self->load_tape( '../oraoigre/bdash.tap' );
117    
118  called to init memory and load initial rom images          $self->loop( sub {
119                    my $run_for = shift;
120                    warn sprintf("about to exec from PC %04x for %d cycles\n", $PC, $run_for) if $self->trace;
121                    M6502::exec( $run_for );
122                    $self->render_vram;
123            });
124    };
125    
   $orao->load_rom;  
126    
127  =cut  =head1 Helper functions
128    
129  sub load_rom {  =head2 write_chunk
     my ($self, $loaded_files) = @_;  
130    
131      #my $time_base = time();  Write chunk directly into memory, updateing vram if needed
132    
133          foreach my $addr ( sort keys %$loaded_files ) {    $emu->write_chunk( 0x1000, $chunk_data );
                 my $path = $loaded_files->{$addr};  
                 $self->load_oraoemu( $path, $addr );  
         }  
 }  
134    
135  # write chunk directly into memory, updateing vram if needed  =cut
136  sub _write_chunk {  
137    sub write_chunk {
138          my $self = shift;          my $self = shift;
139          my ( $addr, $chunk ) = @_;          my ( $addr, $chunk ) = @_;
140          $self->write_chunk( $addr, $chunk );          $self->SUPER::write_chunk( $addr, $chunk );
141          my $end = $addr + length($chunk);          my $end = $addr + length($chunk);
142          my ( $f, $t ) = ( 0x6000, 0x7fff );          my ( $f, $t ) = ( 0x6000, 0x7fff );
143    
# Line 152  sub _write_chunk { Line 150  sub _write_chunk {
150          $t = $end if ( $end < $t );          $t = $end if ( $end < $t );
151    
152          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);          warn sprintf("refresh video ram %04x-%04x\n", $f, $t);
153          foreach my $a ( $f .. $t ) {          $self->render_vram;
154                  $self->vram( $a - 0x6000 , $mem[ $a ] );          $self->render_mem( @mem ) if $self->show_mem;
         }  
155  }  }
156    
157  =head2 load_oraoemu  =head2 load_image
158    
159  Load binary files, ROM images and Orao Emulator files  Load binary files, ROM images and Orao Emulator files
160    
161    $orao->load_oraoemu( '/path/to/file', 0x1000 );    $emu->load_image( '/path/to/file', 0x1000 );
162    
163  Returns true on success.  Returns true on success.
164    
165  =cut  =cut
166    
167  sub load_oraoemu {  sub load_image {
168          my $self = shift;          my $self = shift;
169          my ( $path, $addr ) = @_;          my ( $path, $addr ) = @_;
170    
# Line 183  sub load_oraoemu { Line 180  sub load_oraoemu {
180          if ( $size == 65538 ) {          if ( $size == 65538 ) {
181                  $addr = 0;                  $addr = 0;
182                  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;
183                  $self->_write_chunk( $addr, substr($buff,2) );                  $self->write_chunk( $addr, substr($buff,2) );
184                  return 1;                  return 1;
185          } elsif ( $size == 32800 ) {          } elsif ( $size == 32800 ) {
186                  $addr = 0;                  $addr = 0;
187                  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;
188                  $self->_write_chunk( $addr, substr($buff,0x20) );                  $self->write_chunk( $addr, substr($buff,0x20) );
189                  return 1;                  return 1;
190          }          }
         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 );  
191    
192            printf "loading %s at %04x - %04x %02x\n", $path, $addr, $addr+$size-1, $size;
193            $self->write_chunk( $addr, $buff );
194          return 1;          return 1;
195  };  };
196    
 =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 ]  
                 )  
         );  
 }  
197    
198  =head1 Memory management  =head1 Memory management
199    
# Line 269  Read from memory Line 210  Read from memory
210    
211  =cut  =cut
212    
213    my $keyboard_none = 255;
214    
215    my $keyboard = {
216            0x87FC => {
217                    'right'         => 16,
218                    'down'          => 128,
219                    'up'            => 192,
220                    'left'          => 224,
221                    'backspace' => 224,
222            },
223            0x87FD => sub {
224                    my ( $self, $key ) = @_;
225                    if ( $key eq 'return' ) {
226                            M6502::_write( 0xfc, 13 );
227                            warn "return\n";
228                            return 0;
229                    } elsif ( $key =~ m/ ctrl/ || $self->key_down( 'left ctrl' ) || $self->key_down( 'right ctrl' ) ) {
230                            warn "ctrl\n";
231                            return 16;
232                    }
233                    return $keyboard_none;
234            },
235            0x87FA => {
236                    'f4' => 16,
237                    'f3' => 128,
238                    'f2' => 192,
239                    'f1' => 224,
240            },
241            0x87FB => sub {
242                    my ( $self, $key ) = @_;
243                    if ( $key eq 'space' ) {
244                            return 32;
245                    } elsif ( $self->key_down( 'left shift' ) || $self->key_down( 'right shift' ) ) {
246                            warn "shift\n";
247                            return 16;
248    #               } elsif ( $self->tape ) {
249    #                       warn "has tape!";
250    #                       return 0;
251                    }
252                    return $keyboard_none;
253            },
254            0x87F6 => {
255                    '6' => 16,
256                    't' => 128,
257                    'y' => 192,     # hr: z
258                    'r' => 224,
259            },
260            0x87F7 => {
261                    '5' => 32,
262                    '4' => 16,
263            },
264            0x87EE => {
265                    '7' => 16,
266                    'u' => 128,
267                    'i' => 192,
268                    'o' => 224,
269            },
270            0x87EF => {
271                    '8' => 32,
272                    '9' => 16,
273            },
274            0x87DE => {
275                    '1' => 16,
276                    'w' => 128,
277                    'q' => 192,
278                    'e' => 224,
279            },
280            0x87DF => {
281                    '2' => 32,
282                    '3' => 16,
283            },
284            0x87BE => {
285                    'm' => 16,
286                    'k' => 128,
287                    'j' => 192,
288                    'l' => 224,
289            },
290            0x87BF => {
291                    ',' => 32,      # <
292                    '.' => 16,      # >
293            },
294            0x877E => {
295                    'z' => 16,      # hr:y
296                    's' => 128,
297                    'a' => 192,
298                    'd' => 224,
299            },
300            0x877F => {
301                    'x' => 32,
302                    'c' => 16,
303            },
304            0x86FE => {
305                    'n' => 16,
306                    'g' => 128,
307                    'h' => 192,
308                    'f' => 224,
309            },
310            0x86FF => {
311                    'b' => 32,
312                    'v' => 16,
313            },
314            0x85FE => {
315                    '<' => 16,              # :
316                    '\\' => 128,    # ¾
317                    '\'' => 192,    # ę
318                    ';' => 224,             # č
319            },
320            0x85FF => {
321                    '/' => 32,
322                    'f11' => 16,    # ^
323            },
324            0x83FE => {
325                    'f12' => 16,    # ;
326                    '[' => 128,             # ¹
327                    ']' => 192,             # š
328                    'p' => 224,
329            },
330            0x83FF => {
331                    '-' => 32,
332                    '0' => 16,
333            },
334    };
335    
336  sub read {  sub read {
337          my $self = shift;          my $self = shift;
338          my ($addr) = @_;          my ($addr) = @_;
339            return if ( $addr > 0xffff );
340          my $byte = $mem[$addr];          my $byte = $mem[$addr];
341            confess sprintf("can't find memory at address %04x",$addr) unless defined($byte);
342          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::read(%04x) = %02x\n", $addr, $byte) if $self->trace;
343    
344            # keyboard
345    
346            if ( defined( $keyboard->{$addr} ) ) {
347                    warn sprintf("keyboard port: %04x\n",$addr) if $self->trace;
348                    my $key = $self->key_pressed;
349                    if ( defined($key) ) {
350                            my $ret = $keyboard_none;
351                            my $r = $keyboard->{$addr} || confess "no definition for keyboard port found";
352                            if ( ref($r) eq 'CODE' ) {
353                                    $ret = $r->($self, $key);
354                            } elsif ( defined($r->{$key}) ) {
355                                    $ret = $r->{$key};
356                                    if ( ref($ret) eq 'CODE' ) {
357                                            $ret = $ret->($self);
358                                    }
359                            } else {
360                                    warn sprintf("keyboard port: %04x unknown key: '%s'\n", $addr, $key) if $debug;
361                            }
362                            warn sprintf("keyboard port: %04x key:%s code:%d\n",$addr,$key,$ret) if ( $ret != $keyboard_none );
363                            return $ret;
364                    }
365                    return $keyboard_none;
366            }
367    
368            if ( $addr == 0x87ff ) {
369                    return $self->read_tape;
370            }
371    
372          $self->mmap_pixel( $addr, 0, $byte, 0 );          $self->mmap_pixel( $addr, 0, $byte, 0 );
373          return $byte;          return $byte;
374  }  }
# Line 291  sub write { Line 386  sub write {
386          my ($addr,$byte) = @_;          my ($addr,$byte) = @_;
387          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;          warn sprintf("# Orao::write(%04x,%02x)\n", $addr, $byte) if $self->trace;
388    
         if ( $addr >= 0x6000 && $addr < 0x8000 ) {  
                 $self->vram( $addr - 0x6000 , $byte );  
         }  
   
389          if ( $addr == 0x8800 ) {          if ( $addr == 0x8800 ) {
390                  warn sprintf "sound ignored: %x\n", $byte;                  warn sprintf "sound ignored: %x\n", $byte;
391          }          }
# Line 309  sub write { Line 400  sub write {
400          return;          return;
401  }  }
402    
403  =head1 Command Line  =head2 render_vram
404    
405  Command-line debugging intrerface is implemented for communication with  Render one frame of video ram
 emulated device  
406    
407  =head2 prompt    $self->render_vram;
   
   my ( $entered_line, @p ) = $orao->prompt( $address, $last_command );  
408    
409  =cut  =cut
410    
411  my $last = 'r 1';  my @flip;
412    
413  sub prompt {  foreach my $i ( 0 .. 255 ) {
414          my $self = shift;          my $t = 0;
415          $self->app->sync;          $i & 0b00000001 and $t = $t | 0b10000000;
416          my $a = shift;          $i & 0b00000010 and $t = $t | 0b01000000;
417          print STDERR $self->hexdump( $a ),          $i & 0b00000100 and $t = $t | 0b00100000;
418                  $last ? "[$last] " : '',          $i & 0b00001000 and $t = $t | 0b00010000;
419                  "> ";          $i & 0b00010000 and $t = $t | 0b00001000;
420          my $in = <STDIN>;          $i & 0b00100000 and $t = $t | 0b00000100;
421          chomp($in);          $i & 0b01000000 and $t = $t | 0b00000010;
422          warn "## prompt got: $in\n" if $self->debug;          $i & 0b10000000 and $t = $t | 0b00000001;
423          $in ||= $last;          #warn "$i = $t\n";
424          $last = $in;          $flip[$i] = $t;
         return ( $in, split(/\s+/, $in) ) if $in;  
425  }  }
426    
 =head2 cli  
427    
428    $orao->cli();  sub render_vram {
429            my $self = shift;
430    
431  =cut          my $pixels = pack("C*", map { $flip[$_] } @mem[ 0x6000 .. 0x7fff ]);
432    
433  sub cli {          my $vram = SDL::Surface->new(
434          my $self = shift;                  -width => 256,
435          my $a = $PC || confess "no pc?";                  -height => 256,
436          while ( my ($line, @v) = $self->prompt( $a, $last ) ) {                  -depth => 1,    # 1 bit per pixel
437                  my $c = shift @v;                  -pitch => 32,   # bytes per line
438                  next unless defined($c);                  -from => $pixels,
439                  my $v = shift @v;          );
440                  $v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/;          $vram->set_colors( 0, $black, $white );
441                  @v = map { hex($_) } @v;  
442                  printf "## a: %04x parsed cli: c:%s v:%s %s\n", $a, $c, ($v || 'undef'), join(",",@v) if $self->debug;          $self->render_frame( $vram );
443                  if ( $c =~ m/^[qx]/i ) {  }
444                          exit;  
445                  } elsif ( $c eq '?' ) {  =head2 cpu_PC
                         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 = '';  
                 }  
         }  
446    
447    =cut
448    
449    sub cpu_PC {
450            my ( $self, $addr ) = @_;
451            if ( defined($addr) ) {
452                    $PC = $addr;
453                    warn sprintf("running from PC %04x\n", $PC);
454            };
455            return $PC;
456  }  }
457    
458  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26