12 |
|
|
13 |
use Carp qw/confess/; |
use Carp qw/confess/; |
14 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
15 |
use M6502 qw'@mem'; |
|
16 |
|
use Exporter 'import'; |
17 |
|
our @EXPORT = qw'$white $black'; |
18 |
|
|
19 |
use base qw(Class::Accessor Prefs); |
use base qw(Class::Accessor Prefs); |
20 |
__PACKAGE__->mk_accessors(qw(app event)); |
__PACKAGE__->mk_accessors(qw(app event)); |
45 |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
46 |
-height => 256 * $self->scale, |
-height => 256 * $self->scale, |
47 |
-depth => 16, |
-depth => 16, |
48 |
|
-flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, |
49 |
); |
); |
50 |
#$app->grab_input( SDL_GRAB_QUERY ); |
#$app->grab_input( SDL_GRAB_QUERY ); |
51 |
$app->grab_input( SDL_GRAB_OFF ); |
$app->grab_input( SDL_GRAB_OFF ); |
58 |
warn "# created SDL::App\n"; |
warn "# created SDL::App\n"; |
59 |
} |
} |
60 |
|
|
61 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
62 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
63 |
|
|
64 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
65 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
66 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
67 |
|
|
|
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
|
|
my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); |
|
|
|
|
|
=head2 p |
|
|
|
|
|
$screen->p( $x, $y, 1 ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub p { |
|
|
my $self = shift; |
|
|
|
|
|
my ($x,$y,$w) = (@_); |
|
|
|
|
|
warn "p($x,$y,$w)\n" if $self->debug; |
|
|
|
|
|
my $scale = $self->scale; |
|
|
my $rect = SDL::Rect->new( |
|
|
-height => $scale, |
|
|
-width => $scale, |
|
|
-x => $x * $scale, |
|
|
-y => $y * $scale, |
|
|
); |
|
|
|
|
|
$app->fill( $rect, $w ? $white : $black ); |
|
|
$app->update( $rect ); |
|
|
} |
|
|
|
|
68 |
=head2 mem_xy |
=head2 mem_xy |
69 |
|
|
70 |
Helper to return x and y coordinates in memory map |
Helper to return x and y coordinates in memory map |
82 |
return ($x,$y); |
return ($x,$y); |
83 |
} |
} |
84 |
|
|
|
=head2 vram |
|
|
|
|
|
Push byte to video memory and draw it |
|
|
|
|
|
$screen->vram( $offset, $byte ); |
|
|
|
|
|
=cut |
|
|
|
|
|
my $_vram_counter; |
|
|
|
|
|
sub vram { |
|
|
|
|
|
return; |
|
|
|
|
|
my ( $self, $offset, $byte ) = @_; |
|
|
my $x = ( $offset % 32 ) << 3; |
|
|
my $y = $offset >> 5; |
|
|
my $mask = 1; |
|
|
my $scale = $self->scale; |
|
|
|
|
|
printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace; |
|
|
|
|
|
foreach ( 0 .. 7 ) { |
|
|
my $on = $byte & $mask; |
|
|
if ( $scale == 1 ) { |
|
|
$app->pixel( $x + $_, $y, $on ? $white : $black ); |
|
|
} else { |
|
|
$self->p($x + $_, $y, $on ); |
|
|
} |
|
|
$mask = $mask << 1; |
|
|
} |
|
|
|
|
|
$app->sync if ( $_vram_counter++ % 10 == 0 ); |
|
|
} |
|
|
|
|
85 |
=head2 mmap_pixel |
=head2 mmap_pixel |
86 |
|
|
87 |
Draw pixel in memory map |
Draw pixel in memory map |
120 |
$app->sync; |
$app->sync; |
121 |
} |
} |
122 |
|
|
123 |
=head2 render |
=head2 render_vram |
124 |
|
|
125 |
Render one frame of video ram |
Render one frame of video ram |
126 |
|
|
127 |
$self->render( @video_memory ); |
$self->render_vram; |
128 |
|
|
129 |
=cut |
=cut |
130 |
|
|
131 |
sub render { |
sub render_vram { |
132 |
my $self = shift; |
my $self = shift; |
133 |
|
|
134 |
return unless $self->booted; |
confess "please implement $self::render_vram"; |
135 |
|
} |
136 |
|
|
|
die "this function isn't supported if scale isn't 1" unless $self->scale == 1; |
|
137 |
|
|
138 |
confess "no data?" unless (@_); |
=head2 render_frame |
|
confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8)); |
|
139 |
|
|
140 |
my $pixels = pack("C*", @_); |
Render one frame of video ram |
141 |
|
|
142 |
|
$self->render_frame( $vram_sdl_surface ); |
143 |
|
|
144 |
|
=cut |
145 |
|
|
146 |
|
sub render_frame { |
147 |
|
my $self = shift; |
148 |
|
|
149 |
|
my $vram = shift; |
150 |
|
confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; |
151 |
|
|
|
my $vram = SDL::Surface->new( |
|
|
-width => 256, |
|
|
-height => 256, |
|
|
-depth => 1, # 1 bit per pixel |
|
|
-pitch => 32, # bytes per line |
|
|
-from => $pixels, |
|
|
); |
|
|
$vram->set_colors( 0, $black, $white, $red ); |
|
152 |
$vram->display_format; |
$vram->display_format; |
153 |
|
|
154 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
my $scale = $self->scale || confess "no scale?"; |
155 |
$vram->blit( $rect, $app, $rect_screen ); |
|
156 |
|
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
157 |
|
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
158 |
|
|
159 |
|
if ( $scale > 1 ) { |
160 |
|
use SDL::Tool::Graphic; |
161 |
|
# last parametar is anti-alias |
162 |
|
my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 ); |
163 |
|
$zoomed->blit( $rect, $app, $rect_screen ); |
164 |
|
} else { |
165 |
|
$vram->blit( $rect, $app, $rect_screen ); |
166 |
|
} |
167 |
|
|
168 |
$app->sync; |
$app->sync; |
169 |
} |
} |
170 |
|
|
171 |
|
|
172 |
=head2 render_mem |
=head2 render_mem |
173 |
|
|
174 |
$self->render_mem( @ram ); |
$self->render_mem( @mem ); |
175 |
|
|
176 |
=cut |
=cut |
177 |
|
|
195 |
|
|
196 |
$vram->display_format; |
$vram->display_format; |
197 |
|
|
198 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
199 |
|
my $rect_mem = SDL::Rect->new( -x => 256 * $self->scale, -y => 0, -width => 256, -height => 256 ); |
200 |
|
|
201 |
$vram->blit( $rect, $app, $rect_mem ); |
$vram->blit( $rect, $app, $rect_mem ); |
202 |
|
|
203 |
$app->sync; |
$app->sync; |
269 |
|
|
270 |
=head2 loop |
=head2 loop |
271 |
|
|
272 |
Implement SDL event loop |
Implement CPU run for C<$run_run> cycles inside SDL event loop |
273 |
|
|
274 |
|
$self->loop( sub { |
275 |
|
my $run_for = shift; |
276 |
|
CPU::exec( $run_for ); |
277 |
|
$self->render_vram; |
278 |
|
} ); |
279 |
|
|
280 |
=cut |
=cut |
281 |
|
|
282 |
sub loop { |
sub loop { |
283 |
my $self = shift; |
my $self = shift; |
284 |
my $event = SDL::Event->new(); |
my $exec = shift; |
285 |
|
|
286 |
|
confess "need coderef as argument" unless ref($exec) eq 'CODE'; |
287 |
|
my $event = SDL::Event->new(); |
288 |
|
|
|
MAIN_LOOP: |
|
289 |
while ( 1 ) { |
while ( 1 ) { |
290 |
$self->key_pressed( 1 ); |
$self->key_pressed( 1 ); |
291 |
M6502::exec($run_for); |
$exec->($run_for); |
|
$self->render( @mem[ 0x6000 .. 0x7fff ] ); |
|
292 |
} |
} |
293 |
} |
} |
294 |
|
|