1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Carp; |
7 |
use Data::Dump qw/dump/; |
8 |
|
9 |
use SDL::App; |
10 |
use SDL::Rect; |
11 |
use SDL::Color; |
12 |
|
13 |
use lib './lib'; |
14 |
use Orao; |
15 |
|
16 |
my $debug = 1; |
17 |
my $scale = 3; |
18 |
my $show_mem = 1; |
19 |
my $run_for = 1; |
20 |
my $mem_dump = 'mem.dump'; |
21 |
my $trace = 0; |
22 |
|
23 |
$run_for = 10000; |
24 |
|
25 |
|
26 |
my $app = SDL::App->new( |
27 |
-width => 256 * $scale + ( $show_mem ? 256 : 0 ), |
28 |
-height => 256 * $scale, |
29 |
-depth => 16, |
30 |
); |
31 |
|
32 |
$app->grab_input( 0 ); |
33 |
|
34 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
35 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
36 |
|
37 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
38 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
39 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
40 |
|
41 |
sub p { |
42 |
my ($x,$y,$w) = (@_); |
43 |
|
44 |
warn "p($x,$y,$w)\n" if $trace; |
45 |
|
46 |
my $rect = SDL::Rect->new( |
47 |
-height => $scale, |
48 |
-width => $scale, |
49 |
-x => $x * $scale, |
50 |
-y => $y * $scale, |
51 |
); |
52 |
|
53 |
$app->fill( $rect, $w ? $white : $black ); |
54 |
$app->update( $rect ); |
55 |
} |
56 |
|
57 |
my $stat; |
58 |
|
59 |
my @vram = (0) x 0x2000; |
60 |
|
61 |
sub mem_xy { |
62 |
my $offset = shift; |
63 |
my $x = $offset & 0xff; |
64 |
$x += 256 * $scale; |
65 |
my $y = $offset >> 8; |
66 |
return ($x,$y); |
67 |
} |
68 |
|
69 |
my $orao = new Orao({ |
70 |
vram => sub { |
71 |
my ( $offset, $byte ) = @_; |
72 |
my $x = ( $offset % 32 ) << 3; |
73 |
my $y = $offset >> 5; |
74 |
my $mask = 1; |
75 |
my $old = $vram[$offset]; |
76 |
|
77 |
printf "## vram %04x %02x*%02x %02x -> %02x\n", $offset, $x, $y, $old, $byte if $trace; |
78 |
|
79 |
foreach ( 0 .. 7 ) { |
80 |
p($x + $_, $y, $byte & $mask ); |
81 |
$mask = $mask << 1; |
82 |
} |
83 |
$vram[$offset] = $byte; |
84 |
}, |
85 |
mem_acc => sub { |
86 |
my ( $offset, $what, $value ) = @_; |
87 |
my ( $x, $y ) = mem_xy( $offset ); |
88 |
printf "## mem %04x %02x*%02x %s %s\n", $offset, $x, $y, $what, $value ? $value : '' if $trace; |
89 |
|
90 |
my ( $r,$g,$b ) = ( 0,0,0 ); |
91 |
|
92 |
if ( $what eq 'write' ) { |
93 |
$r = $value; |
94 |
} elsif ( $what eq 'read' ) { |
95 |
$g = $value; |
96 |
} else { |
97 |
$b = $value; |
98 |
} |
99 |
|
100 |
my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b ); |
101 |
$app->pixel( $x, $y, $col ); |
102 |
|
103 |
$stat->{$what}++; |
104 |
if ( $stat->{$what} % 1000 == 0 ) { |
105 |
$app->sync; |
106 |
} |
107 |
}, |
108 |
}); |
109 |
|
110 |
my ($pc, $a, $x, $y, $s, $p) = (0) x 6; |
111 |
#$orao->load_rom('dump/basic.dmp', -2); |
112 |
|
113 |
#$pc = 0xDD11; # BC |
114 |
#$pc = 0xC274; # MC |
115 |
|
116 |
$orao->load_rom('makewav/SCRINV.BIN', 0x1000); |
117 |
$pc = 0x1000; |
118 |
|
119 |
#$orao->load_oraoemu('dump/orao-64k-1.2.dmp'); |
120 |
#$pc = 0xe5b7; |
121 |
|
122 |
# memory dump |
123 |
my $rect = SDL::Rect->new( |
124 |
-height => 256, |
125 |
-width => 256, |
126 |
-x => 256 * $scale, |
127 |
-y => 0, |
128 |
); |
129 |
|
130 |
$app->fill( $rect, $white ); |
131 |
$app->update( $rect ); |
132 |
|
133 |
warn "rendering memory map\n"; |
134 |
|
135 |
my @mmap = ( |
136 |
0x0000, 0x03FF, 'nulti blok', |
137 |
0x0400, 0x5FFF, 'korisnièki RAM (23K)', |
138 |
0x6000, 0x7FFF, 'video RAM', |
139 |
0x8000, 0x9FFF, 'sistemske lokacije', |
140 |
0xA000, 0xAFFF, 'ekstenzija', |
141 |
0xB000, 0xBFFF, 'DOS', |
142 |
0xC000, 0xDFFF, 'BASIC ROM', |
143 |
0xE000, 0xFFFF, 'sistemski ROM', |
144 |
); |
145 |
|
146 |
foreach my $i ( 0 .. $#mmap / 3 ) { |
147 |
my $o = $i * 3; |
148 |
my ( $from, $to, $desc ) = @mmap[$o,$o+1,$o+2]; |
149 |
printf "%04x - %04x - %s\n", $from, $to, $desc; |
150 |
for my $a ( $from .. $to ) { |
151 |
$orao->read_8( $a ); |
152 |
} |
153 |
$app->sync; |
154 |
} |
155 |
|
156 |
sub opcode_cb { |
157 |
my $a = shift || confess "no pc?"; |
158 |
$app->sync; |
159 |
while ( my @v = $orao->prompt( $a ) ) { |
160 |
my $c = shift @v; |
161 |
my $v = shift @v; |
162 |
$v = hex($v) if $v && $v =~ m/^[0-9a-f]+$/; |
163 |
printf "## [%s] %s\n", ($v || 'undef'), join(",",@v) if $debug; |
164 |
@v = map { hex($_) } @v; |
165 |
if ( $c =~ m/^[qx]/i ) { |
166 |
exit; |
167 |
} elsif ( $c eq '?' ) { |
168 |
warn "usage: x|q - exit, e/m 0xdead - dump/edit memory (+/-), j|u 0xbeef - jump, r cycles - run\n"; |
169 |
} elsif ( $c =~ m/^e/i ) { |
170 |
$a = $v; |
171 |
my $to = shift @v; |
172 |
$to = $a + 32 if ( ! $to || $to <= $a ); |
173 |
my $lines = int( ($to - $a - 8) / 8 ); |
174 |
printf "## m %04x %04x lines: %d\n", $a, $to, $lines; |
175 |
while ( $lines ) { |
176 |
print $orao->hexdump( $a ); |
177 |
$a += 8; |
178 |
$lines--; |
179 |
} |
180 |
} elsif ( $c =~ m/^\+/ ) { |
181 |
$a += 8; |
182 |
} elsif ( $c =~ m/^\-/ ) { |
183 |
$a -= 8; |
184 |
} elsif ( $c =~ m/^m/i ) { |
185 |
$a = $v; |
186 |
$orao->poke_code( $a, @v ); |
187 |
printf "poke %d bytes at %04x\n", $#v + 1, $a; |
188 |
} elsif ( $c =~ m/^l/i ) { |
189 |
my $to = shift @ARGV || 0x1000; |
190 |
$a = $to; |
191 |
$orao->load_oraoemu( $v, $a ); |
192 |
} elsif ( $c =~ m/^s/i ) { |
193 |
$orao->save_dump( $v || $mem_dump, @v ); |
194 |
} elsif ( $c =~ m/^r/i ) { |
195 |
$run_for = $v || 1; |
196 |
print "run_for $run_for instructions\n"; |
197 |
last; |
198 |
} elsif ( $c =~ m/^(u|j)/ ) { |
199 |
my $to = $v || $a; |
200 |
printf "set pc to %04x\n", $to; |
201 |
$orao->set_pc( $to ); |
202 |
$run_for = 1; |
203 |
last; |
204 |
} elsif ( $c =~ m/^t/ ) { |
205 |
$trace = not $trace; |
206 |
print "trace ", $trace ? 'on' : 'off', "\n"; |
207 |
} else { |
208 |
warn "# ignore $c\n"; |
209 |
last; |
210 |
} |
211 |
} |
212 |
} |
213 |
|
214 |
sub restart { |
215 |
printf "starting emulation -- pc: %04x a:%d x:%d y:%d s:%d p:%d for %d instructions\n", $pc, $a, $x, $y, $s, $p, $run_for; |
216 |
|
217 |
$orao->set_pc( $pc ); |
218 |
$orao->set_a( $a ); |
219 |
$orao->set_x( $x ); |
220 |
$orao->set_y( $y ); |
221 |
$orao->set_s( $s ); |
222 |
$orao->set_p( $p ); |
223 |
} |
224 |
|
225 |
restart; |
226 |
|
227 |
while ( 1 ) { |
228 |
eval { |
229 |
$orao->run( $run_for , sub { |
230 |
my ($pc, $inst, $a, $x, $y, $s, $p) = @_; |
231 |
$run_for--; |
232 |
|
233 |
$app->pixel( mem_xy( $pc ), $white ); |
234 |
$app->sync if ( $run_for % 500 == 0 ); |
235 |
|
236 |
printf "%04x inst: %x a:%d x:%d y:%d s:%d p:%d [%d]\n", @_, $run_for if ( $trace || $run_for % 1000 == 0 ); |
237 |
opcode_cb( $pc ) if ( $run_for <= 1 ); |
238 |
|
239 |
return 1; |
240 |
} ); |
241 |
}; |
242 |
|
243 |
if ( $@ ) { |
244 |
print "restart after $@\n"; |
245 |
restart(); |
246 |
$run_for = 1; |
247 |
} |
248 |
|
249 |
$run_for ||= 1; |
250 |
} |