2015-07-16 23:56:02 +00:00
|
|
|
//
|
|
|
|
// Atari2600.cpp
|
2015-07-26 19:25:11 +00:00
|
|
|
// CLK
|
2015-07-16 23:56:02 +00:00
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Atari2600.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace Atari2600;
|
2016-05-16 12:01:29 +00:00
|
|
|
namespace {
|
|
|
|
static const unsigned int horizontalTimerPeriod = 228;
|
2016-08-14 17:33:20 +00:00
|
|
|
static const double NTSC_clock_rate = 1194720;
|
|
|
|
static const double PAL_clock_rate = 1182298;
|
2016-05-16 12:01:29 +00:00
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
|
2015-12-06 21:53:37 +00:00
|
|
|
Machine::Machine() :
|
2016-12-03 19:07:38 +00:00
|
|
|
horizontal_timer_(0),
|
|
|
|
last_output_state_duration_(0),
|
|
|
|
last_output_state_(OutputState::Sync),
|
|
|
|
rom_(nullptr),
|
|
|
|
tia_input_value_{0xff, 0xff},
|
|
|
|
upcoming_events_pointer_(0),
|
|
|
|
object_counter_pointer_(0),
|
|
|
|
state_by_time_(state_by_extend_time_[0]),
|
|
|
|
cycles_since_speaker_update_(0),
|
|
|
|
is_pal_region_(false)
|
2016-04-24 10:56:08 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
memset(collisions_, 0xff, sizeof(collisions_));
|
2016-05-26 01:43:19 +00:00
|
|
|
setup_reported_collisions();
|
2016-05-28 01:51:27 +00:00
|
|
|
|
|
|
|
for(int vbextend = 0; vbextend < 2; vbextend++)
|
|
|
|
{
|
|
|
|
for(int c = 0; c < 57; c++)
|
|
|
|
{
|
|
|
|
OutputState state;
|
|
|
|
|
|
|
|
// determine which output state will be active in four cycles from now
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 0: case 1: case 2: case 3: state = OutputState::Blank; break;
|
|
|
|
case 4: case 5: case 6: case 7: state = OutputState::Sync; break;
|
|
|
|
case 8: case 9: case 10: case 11: state = OutputState::ColourBurst; break;
|
|
|
|
case 12: case 13: case 14:
|
|
|
|
case 15: case 16: state = OutputState::Blank; break;
|
|
|
|
|
|
|
|
case 17: case 18: state = vbextend ? OutputState::Blank : OutputState::Pixel; break;
|
|
|
|
default: state = OutputState::Pixel; break;
|
|
|
|
}
|
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
state_by_extend_time_[vbextend][c] = state;
|
2016-05-28 01:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-14 17:33:20 +00:00
|
|
|
set_clock_rate(NTSC_clock_rate);
|
2016-04-24 10:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::setup_output(float aspect_ratio)
|
2015-07-16 23:56:02 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
speaker_.reset(new Speaker);
|
2016-12-10 18:42:34 +00:00
|
|
|
crt_.reset(new Outputs::CRT::CRT(228, 1, 263, Outputs::CRT::ColourSpace::YIQ, 228, 1, false, 1));
|
2016-12-03 19:07:38 +00:00
|
|
|
crt_->set_output_device(Outputs::CRT::Television);
|
2016-05-12 01:07:18 +00:00
|
|
|
|
|
|
|
// this is the NTSC phase offset function; see below for PAL
|
2016-12-03 19:07:38 +00:00
|
|
|
crt_->set_composite_sampling_function(
|
2016-05-12 02:11:01 +00:00
|
|
|
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
|
2016-04-26 00:56:15 +00:00
|
|
|
"{"
|
|
|
|
"uint c = texture(texID, coordinate).r;"
|
2016-05-13 02:22:24 +00:00
|
|
|
"uint y = c & 14u;"
|
2016-04-26 00:56:15 +00:00
|
|
|
"uint iPhase = (c >> 4);"
|
2016-05-12 01:07:18 +00:00
|
|
|
|
2016-05-14 20:52:24 +00:00
|
|
|
"float phaseOffset = 6.283185308 * float(iPhase - 1u) / 13.0;"
|
2016-06-08 02:01:14 +00:00
|
|
|
"return mix(float(y) / 14.0, step(1, iPhase) * cos(phase + phaseOffset), amplitude);"
|
2016-02-05 03:28:50 +00:00
|
|
|
"}");
|
2016-12-03 19:07:38 +00:00
|
|
|
speaker_->set_input_rate((float)(get_clock_rate() / 38.0));
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 01:07:18 +00:00
|
|
|
void Machine::switch_region()
|
|
|
|
{
|
|
|
|
// the PAL function
|
2016-12-03 19:07:38 +00:00
|
|
|
crt_->set_composite_sampling_function(
|
2016-05-12 02:11:01 +00:00
|
|
|
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
|
2016-05-12 01:07:18 +00:00
|
|
|
"{"
|
|
|
|
"uint c = texture(texID, coordinate).r;"
|
2016-05-13 02:22:24 +00:00
|
|
|
"uint y = c & 14u;"
|
2016-05-12 01:07:18 +00:00
|
|
|
"uint iPhase = (c >> 4);"
|
|
|
|
|
2016-05-14 20:52:24 +00:00
|
|
|
"uint direction = iPhase & 1u;"
|
|
|
|
"float phaseOffset = float(7u - direction) + (float(direction) - 0.5) * 2.0 * float(iPhase >> 1);"
|
|
|
|
"phaseOffset *= 6.283185308 / 12.0;"
|
2016-06-08 02:01:14 +00:00
|
|
|
"return mix(float(y) / 14.0, step(4, (iPhase + 2u) & 15u) * cos(phase + phaseOffset), amplitude);"
|
2016-05-12 01:07:18 +00:00
|
|
|
"}");
|
2016-06-21 02:18:41 +00:00
|
|
|
|
2016-12-10 18:42:34 +00:00
|
|
|
crt_->set_new_timing(228, 312, Outputs::CRT::ColourSpace::YUV, 228, 1, true);
|
2016-06-21 02:18:41 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
is_pal_region_ = true;
|
|
|
|
speaker_->set_input_rate((float)(get_clock_rate() / 38.0));
|
2016-08-14 17:33:20 +00:00
|
|
|
set_clock_rate(PAL_clock_rate);
|
2016-05-12 01:07:18 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:34:25 +00:00
|
|
|
void Machine::close_output()
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
crt_ = nullptr;
|
2016-04-25 00:34:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:10 +00:00
|
|
|
Machine::~Machine()
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
delete[] rom_;
|
2016-04-25 00:34:25 +00:00
|
|
|
close_output();
|
2015-07-28 01:15:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 14:18:15 +00:00
|
|
|
void Machine::update_timers(int mask)
|
2015-07-16 23:56:02 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
unsigned int upcoming_pointer_plus_4 = (upcoming_events_pointer_ + 4)%number_of_upcoming_events;
|
2016-05-25 01:39:57 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
object_counter_pointer_ = (object_counter_pointer_ + 1)%number_of_recorded_counters;
|
|
|
|
ObjectCounter *oneClockAgo = object_counter_[(object_counter_pointer_ - 1 + number_of_recorded_counters)%number_of_recorded_counters];
|
|
|
|
ObjectCounter *twoClocksAgo = object_counter_[(object_counter_pointer_ - 2 + number_of_recorded_counters)%number_of_recorded_counters];
|
|
|
|
ObjectCounter *now = object_counter_[object_counter_pointer_];
|
2016-05-27 18:33:08 +00:00
|
|
|
|
2016-05-26 01:12:25 +00:00
|
|
|
// grab the background now, for application in four clocks
|
2016-12-03 19:07:38 +00:00
|
|
|
if(mask & (1 << 5) && !(horizontal_timer_&3))
|
2016-05-17 11:09:18 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
unsigned int offset = 4 + horizontal_timer_ - (horizontalTimerPeriod - 160);
|
|
|
|
upcoming_events_[upcoming_pointer_plus_4].updates |= Event::Action::Playfield;
|
|
|
|
upcoming_events_[upcoming_pointer_plus_4].playfield_pixel = playfield_[(offset >> 2)%40];
|
2016-05-17 11:09:18 +00:00
|
|
|
}
|
2016-05-17 01:54:27 +00:00
|
|
|
|
2016-05-21 14:18:15 +00:00
|
|
|
if(mask & (1 << 4))
|
2016-05-16 23:55:56 +00:00
|
|
|
{
|
2016-05-24 11:58:26 +00:00
|
|
|
// the ball becomes visible whenever it hits zero, regardless of whether its status
|
2016-05-27 18:33:08 +00:00
|
|
|
// is the result of a counter rollover or a programmatic reset, and there's a four
|
|
|
|
// clock delay on that triggering the start signal
|
|
|
|
now[4].count = (oneClockAgo[4].count + 1)%160;
|
|
|
|
now[4].pixel = oneClockAgo[4].pixel + 1;
|
2016-05-28 01:51:27 +00:00
|
|
|
if(!now[4].count) now[4].pixel = 0;
|
2016-05-27 18:33:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
now[4] = oneClockAgo[4];
|
2016-05-16 23:55:56 +00:00
|
|
|
}
|
2016-05-17 11:09:18 +00:00
|
|
|
|
2016-05-18 11:51:25 +00:00
|
|
|
// check for player and missle triggers
|
2016-05-18 11:31:05 +00:00
|
|
|
for(int c = 0; c < 4; c++)
|
|
|
|
{
|
2016-05-21 14:18:15 +00:00
|
|
|
if(mask & (1 << c))
|
2016-05-18 11:31:05 +00:00
|
|
|
{
|
2016-05-27 18:33:08 +00:00
|
|
|
// update the count
|
|
|
|
now[c].count = (oneClockAgo[c].count + 1)%160;
|
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
uint8_t repeatMask = player_and_missile_size_[c&1] & 7;
|
2016-05-27 18:33:08 +00:00
|
|
|
ObjectCounter *rollover;
|
|
|
|
ObjectCounter *equality;
|
|
|
|
|
|
|
|
if(c < 2)
|
2016-05-21 14:18:15 +00:00
|
|
|
{
|
2016-05-27 18:33:08 +00:00
|
|
|
// update the pixel
|
|
|
|
now[c].broad_pixel = oneClockAgo[c].broad_pixel + 1;
|
|
|
|
switch(repeatMask)
|
2016-05-21 14:18:15 +00:00
|
|
|
{
|
2016-05-27 18:33:08 +00:00
|
|
|
default: now[c].pixel = oneClockAgo[c].pixel + 1; break;
|
|
|
|
case 5: now[c].pixel = oneClockAgo[c].pixel + (now[c].broad_pixel&1); break;
|
|
|
|
case 7: now[c].pixel = oneClockAgo[c].pixel + (((now[c].broad_pixel | (now[c].broad_pixel >> 1))^1)&1); break;
|
2016-05-21 14:18:15 +00:00
|
|
|
}
|
2016-05-27 18:33:08 +00:00
|
|
|
|
|
|
|
// check for a rollover six clocks ago or equality five clocks ago
|
2016-05-28 01:51:27 +00:00
|
|
|
rollover = twoClocksAgo;
|
|
|
|
equality = oneClockAgo;
|
2016-05-21 14:18:15 +00:00
|
|
|
}
|
2016-05-27 18:33:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// update the pixel
|
|
|
|
now[c].pixel = oneClockAgo[c].pixel + 1;
|
2016-05-21 14:18:15 +00:00
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
// check for a rollover five clocks ago or equality four clocks ago
|
2016-05-28 01:51:27 +00:00
|
|
|
rollover = oneClockAgo;
|
|
|
|
equality = now;
|
2016-05-27 18:33:08 +00:00
|
|
|
}
|
2016-05-25 01:39:57 +00:00
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
if(
|
|
|
|
(rollover[c].count == 159) ||
|
2016-12-03 19:07:38 +00:00
|
|
|
(has_second_copy_[c&1] && equality[c].count == 16) ||
|
|
|
|
(has_third_copy_[c&1] && equality[c].count == 32) ||
|
|
|
|
(has_fourth_copy_[c&1] && equality[c].count == 64)
|
2016-05-27 18:33:08 +00:00
|
|
|
)
|
2016-05-21 14:18:15 +00:00
|
|
|
{
|
2016-05-27 18:33:08 +00:00
|
|
|
now[c].pixel = 0;
|
|
|
|
now[c].broad_pixel = 0;
|
2016-05-21 14:18:15 +00:00
|
|
|
}
|
2016-05-18 11:31:05 +00:00
|
|
|
}
|
2016-05-27 18:33:08 +00:00
|
|
|
else
|
2016-05-21 14:18:15 +00:00
|
|
|
{
|
2016-05-27 18:33:08 +00:00
|
|
|
now[c] = oneClockAgo[c];
|
2016-05-21 14:18:15 +00:00
|
|
|
}
|
2016-05-18 11:31:05 +00:00
|
|
|
}
|
2016-05-25 11:32:25 +00:00
|
|
|
}
|
2016-05-23 01:45:40 +00:00
|
|
|
|
2016-05-25 11:32:25 +00:00
|
|
|
uint8_t Machine::get_output_pixel()
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
ObjectCounter *now = object_counter_[object_counter_pointer_];
|
2016-05-24 11:58:26 +00:00
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
// get the playfield pixel
|
2016-12-03 19:07:38 +00:00
|
|
|
unsigned int offset = horizontal_timer_ - (horizontalTimerPeriod - 160);
|
|
|
|
uint8_t playfieldColour = ((playfield_control_&6) == 2) ? player_colour_[offset / 80] : playfield_colour_;
|
2016-05-25 11:32:25 +00:00
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
// ball pixel
|
2016-05-25 11:32:25 +00:00
|
|
|
uint8_t ballPixel = 0;
|
2016-12-03 19:07:38 +00:00
|
|
|
if(now[4].pixel < ball_size_) {
|
|
|
|
ballPixel = ball_graphics_enable_[ball_graphics_selector_];
|
2016-05-25 01:39:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
// determine the player and missile pixels
|
2016-05-25 11:32:25 +00:00
|
|
|
uint8_t playerPixels[2] = { 0, 0 };
|
|
|
|
uint8_t missilePixels[2] = { 0, 0 };
|
2016-05-24 11:58:26 +00:00
|
|
|
for(int c = 0; c < 2; c++)
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
if(player_graphics_[c] && now[c].pixel < 8) {
|
|
|
|
playerPixels[c] = (player_graphics_[player_graphics_selector_[c]][c] >> (now[c].pixel ^ player_reflection_mask_[c])) & 1;
|
2016-05-24 11:58:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(!missile_graphics_reset_[c] && now[c+2].pixel < missile_size_[c]) {
|
|
|
|
missilePixels[c] = missile_graphics_enable_[c];
|
2016-05-24 11:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-18 11:31:05 +00:00
|
|
|
|
2015-08-12 23:31:57 +00:00
|
|
|
// accumulate collisions
|
2016-12-03 19:07:38 +00:00
|
|
|
int pixel_mask = playerPixels[0] | (playerPixels[1] << 1) | (missilePixels[0] << 2) | (missilePixels[1] << 3) | (ballPixel << 4) | (playfield_output_ << 5);
|
|
|
|
collisions_[0] |= reported_collisions_[pixel_mask][0];
|
|
|
|
collisions_[1] |= reported_collisions_[pixel_mask][1];
|
|
|
|
collisions_[2] |= reported_collisions_[pixel_mask][2];
|
|
|
|
collisions_[3] |= reported_collisions_[pixel_mask][3];
|
|
|
|
collisions_[4] |= reported_collisions_[pixel_mask][4];
|
|
|
|
collisions_[5] |= reported_collisions_[pixel_mask][5];
|
|
|
|
collisions_[6] |= reported_collisions_[pixel_mask][6];
|
|
|
|
collisions_[7] |= reported_collisions_[pixel_mask][7];
|
2015-08-12 23:31:57 +00:00
|
|
|
|
2015-07-31 01:30:00 +00:00
|
|
|
// apply appropriate priority to pick a colour
|
2016-12-03 19:07:38 +00:00
|
|
|
uint8_t playfield_pixel = playfield_output_ | ballPixel;
|
|
|
|
uint8_t outputColour = playfield_pixel ? playfieldColour : background_colour_;
|
2015-07-30 15:52:28 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(!(playfield_control_&0x04) || !playfield_pixel) {
|
|
|
|
if(playerPixels[1] || missilePixels[1]) outputColour = player_colour_[1];
|
|
|
|
if(playerPixels[0] || missilePixels[0]) outputColour = player_colour_[0];
|
2016-05-18 11:51:25 +00:00
|
|
|
}
|
2015-07-30 02:37:37 +00:00
|
|
|
|
2016-05-16 23:04:13 +00:00
|
|
|
// return colour
|
2016-05-18 11:51:25 +00:00
|
|
|
return outputColour;
|
2015-07-19 20:48:14 +00:00
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
|
2016-05-26 01:43:19 +00:00
|
|
|
void Machine::setup_reported_collisions()
|
|
|
|
{
|
2016-05-27 18:33:08 +00:00
|
|
|
for(int c = 0; c < 64; c++)
|
2016-05-26 01:43:19 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
memset(reported_collisions_[c], 0, 8);
|
2016-05-26 01:43:19 +00:00
|
|
|
|
|
|
|
int playerPixels[2] = { c&1, (c >> 1)&1 };
|
|
|
|
int missilePixels[2] = { (c >> 2)&1, (c >> 3)&1 };
|
|
|
|
int ballPixel = (c >> 4)&1;
|
2016-12-03 19:07:38 +00:00
|
|
|
int playfield_pixel = (c >> 5)&1;
|
2016-05-26 01:43:19 +00:00
|
|
|
|
|
|
|
if(playerPixels[0] | playerPixels[1]) {
|
2016-12-03 19:07:38 +00:00
|
|
|
reported_collisions_[c][0] |= ((missilePixels[0] & playerPixels[1]) << 7) | ((missilePixels[0] & playerPixels[0]) << 6);
|
|
|
|
reported_collisions_[c][1] |= ((missilePixels[1] & playerPixels[0]) << 7) | ((missilePixels[1] & playerPixels[1]) << 6);
|
2016-05-26 01:43:19 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
reported_collisions_[c][2] |= ((playfield_pixel & playerPixels[0]) << 7) | ((ballPixel & playerPixels[0]) << 6);
|
|
|
|
reported_collisions_[c][3] |= ((playfield_pixel & playerPixels[1]) << 7) | ((ballPixel & playerPixels[1]) << 6);
|
2016-05-26 01:43:19 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
reported_collisions_[c][7] |= ((playerPixels[0] & playerPixels[1]) << 7);
|
2016-05-26 01:43:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(playfield_pixel | ballPixel) {
|
|
|
|
reported_collisions_[c][4] |= ((playfield_pixel & missilePixels[0]) << 7) | ((ballPixel & missilePixels[0]) << 6);
|
|
|
|
reported_collisions_[c][5] |= ((playfield_pixel & missilePixels[1]) << 7) | ((ballPixel & missilePixels[1]) << 6);
|
2016-05-26 01:43:19 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
reported_collisions_[c][6] |= ((playfield_pixel & ballPixel) << 7);
|
2016-05-26 01:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(missilePixels[0] & missilePixels[1])
|
2016-12-03 19:07:38 +00:00
|
|
|
reported_collisions_[c][7] |= (1 << 6);
|
2016-05-26 01:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 20:08:29 +00:00
|
|
|
void Machine::output_pixels(unsigned int count)
|
2015-07-19 20:48:14 +00:00
|
|
|
{
|
|
|
|
while(count--)
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
if(upcoming_events_[upcoming_events_pointer_].updates)
|
2016-05-23 01:45:40 +00:00
|
|
|
{
|
2016-06-03 01:22:55 +00:00
|
|
|
// apply any queued changes and flush the record
|
2016-12-03 19:07:38 +00:00
|
|
|
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::HMoveSetup)
|
2016-05-23 01:45:40 +00:00
|
|
|
{
|
2016-06-03 01:22:55 +00:00
|
|
|
// schedule an extended left border
|
2016-12-03 19:07:38 +00:00
|
|
|
state_by_time_ = state_by_extend_time_[1];
|
2016-06-03 01:22:55 +00:00
|
|
|
|
|
|
|
// clear any ongoing moves
|
2016-12-03 19:07:38 +00:00
|
|
|
if(hmove_flags_)
|
2016-05-23 01:45:40 +00:00
|
|
|
{
|
2016-06-03 01:22:55 +00:00
|
|
|
for(int c = 0; c < number_of_upcoming_events; c++)
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
upcoming_events_[c].updates &= ~(Event::Action::HMoveCompare | Event::Action::HMoveDecrement);
|
2016-06-03 01:22:55 +00:00
|
|
|
}
|
2016-05-23 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 01:22:55 +00:00
|
|
|
// schedule new moves
|
2016-12-03 19:07:38 +00:00
|
|
|
hmove_flags_ = 0x1f;
|
|
|
|
hmove_counter_ = 15;
|
2016-05-28 01:51:27 +00:00
|
|
|
|
2016-06-03 01:22:55 +00:00
|
|
|
// follow-through into a compare immediately
|
2016-12-03 19:07:38 +00:00
|
|
|
upcoming_events_[upcoming_events_pointer_].updates |= Event::Action::HMoveCompare;
|
2016-06-03 01:22:55 +00:00
|
|
|
}
|
2016-05-23 01:45:40 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::HMoveCompare)
|
2016-05-17 22:35:40 +00:00
|
|
|
{
|
2016-06-03 01:22:55 +00:00
|
|
|
for(int c = 0; c < 5; c++)
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
if(((object_motions_[c] >> 4)^hmove_counter_) == 7)
|
2016-06-03 01:22:55 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
hmove_flags_ &= ~(1 << c);
|
2016-06-03 01:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-03 19:07:38 +00:00
|
|
|
if(hmove_flags_)
|
2016-05-17 22:35:40 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
if(hmove_counter_) hmove_counter_--;
|
|
|
|
upcoming_events_[(upcoming_events_pointer_+4)%number_of_upcoming_events].updates |= Event::Action::HMoveCompare;
|
|
|
|
upcoming_events_[(upcoming_events_pointer_+2)%number_of_upcoming_events].updates |= Event::Action::HMoveDecrement;
|
2016-05-17 22:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 01:22:55 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::HMoveDecrement)
|
2016-05-17 22:35:40 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
update_timers(hmove_flags_);
|
2016-05-18 01:41:32 +00:00
|
|
|
}
|
2016-05-17 22:35:40 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::ResetCounter)
|
2016-06-03 01:22:55 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
object_counter_[object_counter_pointer_][upcoming_events_[upcoming_events_pointer_].counter].count = 0;
|
2016-06-03 01:22:55 +00:00
|
|
|
}
|
2016-05-30 23:56:36 +00:00
|
|
|
|
2016-06-03 01:22:55 +00:00
|
|
|
// zero out current update event
|
2016-12-03 19:07:38 +00:00
|
|
|
upcoming_events_[upcoming_events_pointer_].updates = 0;
|
2016-05-30 23:56:36 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 01:22:55 +00:00
|
|
|
// progress to next event
|
2016-12-03 19:07:38 +00:00
|
|
|
upcoming_events_pointer_ = (upcoming_events_pointer_ + 1)%number_of_upcoming_events;
|
2016-05-25 01:39:57 +00:00
|
|
|
|
2016-05-28 01:51:27 +00:00
|
|
|
// determine which output state is currently active
|
2016-12-03 19:07:38 +00:00
|
|
|
OutputState primary_state = state_by_time_[horizontal_timer_ >> 2];
|
2016-05-28 01:51:27 +00:00
|
|
|
OutputState effective_state = primary_state;
|
|
|
|
|
|
|
|
// update pixel timers
|
|
|
|
if(primary_state == OutputState::Pixel) update_timers(~0);
|
|
|
|
|
|
|
|
// update the background chain
|
2016-12-03 19:07:38 +00:00
|
|
|
if(horizontal_timer_ >= 64 && horizontal_timer_ <= 160+64 && !(horizontal_timer_&3))
|
2016-05-25 11:32:25 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
playfield_output_ = next_playfield_output_;
|
|
|
|
next_playfield_output_ = playfield_[(horizontal_timer_ - 64) >> 2];
|
2016-05-25 11:32:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 23:56:36 +00:00
|
|
|
// if vsync is enabled, output the opposite of the automatic hsync output;
|
|
|
|
// also honour the vertical blank flag
|
2016-12-03 19:07:38 +00:00
|
|
|
if(vsync_enabled_) {
|
2016-05-28 01:51:27 +00:00
|
|
|
effective_state = (effective_state = OutputState::Sync) ? OutputState::Blank : OutputState::Sync;
|
2016-12-03 19:07:38 +00:00
|
|
|
} else if(vblank_enabled_ && effective_state == OutputState::Pixel) {
|
2016-05-28 01:51:27 +00:00
|
|
|
effective_state = OutputState::Blank;
|
2016-05-18 22:49:40 +00:00
|
|
|
}
|
2016-05-17 01:54:27 +00:00
|
|
|
|
|
|
|
// decide what that means needs to be communicated to the CRT
|
2016-12-03 19:07:38 +00:00
|
|
|
last_output_state_duration_++;
|
|
|
|
if(effective_state != last_output_state_) {
|
|
|
|
switch(last_output_state_) {
|
|
|
|
case OutputState::Blank: crt_->output_blank(last_output_state_duration_); break;
|
|
|
|
case OutputState::Sync: crt_->output_sync(last_output_state_duration_); break;
|
|
|
|
case OutputState::ColourBurst: crt_->output_colour_burst(last_output_state_duration_, 96, 0); break;
|
|
|
|
case OutputState::Pixel: crt_->output_data(last_output_state_duration_, 1); break;
|
2015-07-28 02:12:13 +00:00
|
|
|
}
|
2016-12-03 19:07:38 +00:00
|
|
|
last_output_state_duration_ = 0;
|
|
|
|
last_output_state_ = effective_state;
|
2015-07-28 02:12:13 +00:00
|
|
|
|
2016-05-28 01:51:27 +00:00
|
|
|
if(effective_state == OutputState::Pixel) {
|
2016-12-03 19:07:38 +00:00
|
|
|
output_buffer_ = crt_->allocate_write_area(160);
|
2015-08-12 23:31:57 +00:00
|
|
|
} else {
|
2016-12-03 19:07:38 +00:00
|
|
|
output_buffer_ = nullptr;
|
2015-07-28 02:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:54:27 +00:00
|
|
|
// decide on a pixel colour if that's what's happening
|
2016-05-28 01:51:27 +00:00
|
|
|
if(effective_state == OutputState::Pixel)
|
2016-05-16 23:04:13 +00:00
|
|
|
{
|
|
|
|
uint8_t colour = get_output_pixel();
|
2016-12-03 19:07:38 +00:00
|
|
|
if(output_buffer_)
|
2016-05-16 23:04:13 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
*output_buffer_ = colour;
|
|
|
|
output_buffer_++;
|
2016-05-16 23:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 01:54:27 +00:00
|
|
|
|
2016-05-28 01:51:27 +00:00
|
|
|
// advance horizontal timer, perform reset actions if desired
|
2016-12-03 19:07:38 +00:00
|
|
|
horizontal_timer_ = (horizontal_timer_ + 1) % horizontalTimerPeriod;
|
|
|
|
if(!horizontal_timer_)
|
2016-05-16 23:04:13 +00:00
|
|
|
{
|
2016-06-03 01:22:55 +00:00
|
|
|
// switch back to a normal length left border
|
2016-12-03 19:07:38 +00:00
|
|
|
state_by_time_ = state_by_extend_time_[0];
|
2016-05-16 23:55:56 +00:00
|
|
|
set_ready_line(false);
|
2016-05-16 23:04:13 +00:00
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 20:08:29 +00:00
|
|
|
unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value)
|
2015-07-19 20:48:14 +00:00
|
|
|
{
|
|
|
|
uint8_t returnValue = 0xff;
|
2016-05-28 01:51:27 +00:00
|
|
|
unsigned int cycles_run_for = 3;
|
2015-08-10 14:09:40 +00:00
|
|
|
|
2016-05-16 23:55:56 +00:00
|
|
|
// this occurs as a feedback loop — the 2600 requests ready, then performs the cycles_run_for
|
|
|
|
// leap to the end of ready only once ready is signalled — because on a 6502 ready doesn't take
|
|
|
|
// effect until the next read; therefore it isn't safe to assume that signalling ready immediately
|
|
|
|
// skips to the end of the line.
|
2015-08-09 06:42:01 +00:00
|
|
|
if(operation == CPU6502::BusOperation::Ready) {
|
2016-12-03 19:07:38 +00:00
|
|
|
unsigned int distance_to_end_of_ready = horizontalTimerPeriod - horizontal_timer_;
|
2016-05-28 01:51:27 +00:00
|
|
|
cycles_run_for = distance_to_end_of_ready;
|
2015-08-09 06:42:01 +00:00
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
|
2016-05-30 23:56:36 +00:00
|
|
|
output_pixels(cycles_run_for);
|
2016-12-03 19:07:38 +00:00
|
|
|
cycles_since_speaker_update_ += cycles_run_for;
|
2016-05-16 23:55:56 +00:00
|
|
|
|
2015-07-31 20:54:20 +00:00
|
|
|
if(operation != CPU6502::BusOperation::Ready) {
|
2015-08-09 06:42:01 +00:00
|
|
|
|
2015-08-13 14:04:30 +00:00
|
|
|
// check for a paging access
|
2016-12-03 19:07:38 +00:00
|
|
|
if(rom_size_ > 4096 && ((address & 0x1f00) == 0x1f00)) {
|
|
|
|
uint8_t *base_ptr = rom_pages_[0];
|
|
|
|
uint8_t first_paging_register = (uint8_t)(0xf8 - (rom_size_ >> 14)*2);
|
2015-08-13 14:04:30 +00:00
|
|
|
|
|
|
|
const uint8_t paging_register = address&0xff;
|
|
|
|
if(paging_register >= first_paging_register) {
|
|
|
|
const uint16_t selected_page = paging_register - first_paging_register;
|
2016-12-03 19:07:38 +00:00
|
|
|
if(selected_page * 4096 < rom_size_) {
|
|
|
|
base_ptr = &rom_[selected_page * 4096];
|
2015-08-13 12:24:02 +00:00
|
|
|
}
|
2015-08-13 14:04:30 +00:00
|
|
|
}
|
2015-08-13 12:24:02 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(base_ptr != rom_pages_[0]) {
|
|
|
|
rom_pages_[0] = base_ptr;
|
|
|
|
rom_pages_[1] = base_ptr + 1024;
|
|
|
|
rom_pages_[2] = base_ptr + 2048;
|
|
|
|
rom_pages_[3] = base_ptr + 3072;
|
2015-08-13 12:24:02 +00:00
|
|
|
}
|
2015-08-13 14:04:30 +00:00
|
|
|
}
|
2015-08-13 12:24:02 +00:00
|
|
|
|
2015-08-13 14:04:30 +00:00
|
|
|
// check for a ROM read
|
2016-04-25 02:32:24 +00:00
|
|
|
if((address&0x1000) && isReadOperation(operation)) {
|
2016-12-03 19:07:38 +00:00
|
|
|
returnValue &= rom_pages_[(address >> 10)&3][address&1023];
|
2015-07-31 20:54:20 +00:00
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
|
2015-07-31 20:54:20 +00:00
|
|
|
// check for a RAM access
|
2016-04-25 02:32:24 +00:00
|
|
|
if((address&0x1280) == 0x80) {
|
2015-07-31 20:54:20 +00:00
|
|
|
if(isReadOperation(operation)) {
|
2016-12-03 19:07:38 +00:00
|
|
|
returnValue &= mos6532_.get_ram(address);
|
2015-07-31 20:54:20 +00:00
|
|
|
} else {
|
2016-12-03 19:07:38 +00:00
|
|
|
mos6532_.set_ram(address, *value);
|
2015-07-31 20:54:20 +00:00
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 20:54:20 +00:00
|
|
|
// check for a TIA access
|
2016-04-25 02:32:24 +00:00
|
|
|
if(!(address&0x1080)) {
|
2015-07-31 20:54:20 +00:00
|
|
|
if(isReadOperation(operation)) {
|
2015-08-12 23:31:57 +00:00
|
|
|
const uint16_t decodedAddress = address & 0xf;
|
|
|
|
switch(decodedAddress) {
|
|
|
|
case 0x00: // missile 0 / player collisions
|
|
|
|
case 0x01: // missile 1 / player collisions
|
|
|
|
case 0x02: // player 0 / playfield / ball collisions
|
|
|
|
case 0x03: // player 1 / playfield / ball collisions
|
|
|
|
case 0x04: // missile 0 / playfield / ball collisions
|
|
|
|
case 0x05: // missile 1 / playfield / ball collisions
|
|
|
|
case 0x06: // ball / playfield collisions
|
|
|
|
case 0x07: // player / player, missile / missile collisions
|
2016-12-03 19:07:38 +00:00
|
|
|
returnValue &= collisions_[decodedAddress];
|
2015-08-12 23:31:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x08:
|
|
|
|
case 0x09:
|
|
|
|
case 0x0a:
|
|
|
|
case 0x0b:
|
|
|
|
// TODO: pot ports
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0c:
|
|
|
|
case 0x0d:
|
2016-12-03 19:07:38 +00:00
|
|
|
returnValue &= tia_input_value_[decodedAddress - 0x0c];
|
2015-08-12 23:31:57 +00:00
|
|
|
break;
|
2015-07-31 20:54:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-10 15:55:16 +00:00
|
|
|
const uint16_t decodedAddress = address & 0x3f;
|
2015-08-10 15:43:45 +00:00
|
|
|
switch(decodedAddress) {
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x00:
|
2016-12-03 19:07:38 +00:00
|
|
|
vsync_enabled_ = !!(*value & 0x02);
|
2015-08-12 23:31:57 +00:00
|
|
|
break;
|
2016-12-03 19:07:38 +00:00
|
|
|
case 0x01: vblank_enabled_ = !!(*value & 0x02); break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2015-08-10 15:43:45 +00:00
|
|
|
case 0x02:
|
2016-12-03 19:07:38 +00:00
|
|
|
if(horizontal_timer_) set_ready_line(true);
|
2015-08-10 15:43:45 +00:00
|
|
|
break;
|
2015-08-10 14:09:40 +00:00
|
|
|
case 0x03:
|
2016-05-22 20:29:53 +00:00
|
|
|
// Reset is delayed by four cycles.
|
2016-12-03 19:07:38 +00:00
|
|
|
horizontal_timer_ = horizontalTimerPeriod - 4;
|
2016-06-01 23:27:04 +00:00
|
|
|
|
|
|
|
// TODO: audio will now be out of synchronisation — fix
|
2015-08-10 14:09:40 +00:00
|
|
|
break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x04:
|
2016-05-27 18:33:08 +00:00
|
|
|
case 0x05: {
|
|
|
|
int entry = decodedAddress - 0x04;
|
2016-12-03 19:07:38 +00:00
|
|
|
player_and_missile_size_[entry] = *value;
|
|
|
|
missile_size_[entry] = 1 << ((*value >> 4)&3);
|
2016-05-27 18:33:08 +00:00
|
|
|
|
|
|
|
uint8_t repeatMask = (*value)&7;
|
2016-12-03 19:07:38 +00:00
|
|
|
has_second_copy_[entry] = (repeatMask == 1) || (repeatMask == 3);
|
|
|
|
has_third_copy_[entry] = (repeatMask == 2) || (repeatMask == 3) || (repeatMask == 6);
|
|
|
|
has_fourth_copy_[entry] = (repeatMask == 4) || (repeatMask == 6);
|
2016-05-27 18:33:08 +00:00
|
|
|
} break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x06:
|
2016-12-03 19:07:38 +00:00
|
|
|
case 0x07: player_colour_[decodedAddress - 0x06] = *value; break;
|
|
|
|
case 0x08: playfield_colour_ = *value; break;
|
|
|
|
case 0x09: background_colour_ = *value; break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2015-08-13 22:23:04 +00:00
|
|
|
case 0x0a: {
|
2016-12-03 19:07:38 +00:00
|
|
|
uint8_t old_playfield_control = playfield_control_;
|
|
|
|
playfield_control_ = *value;
|
|
|
|
ball_size_ = 1 << ((playfield_control_ >> 4)&3);
|
2015-08-13 22:23:04 +00:00
|
|
|
|
|
|
|
// did the mirroring bit change?
|
2016-12-03 19:07:38 +00:00
|
|
|
if((playfield_control_^old_playfield_control)&1) {
|
|
|
|
if(playfield_control_&1) {
|
|
|
|
for(int c = 0; c < 20; c++) playfield_[c+20] = playfield_[19-c];
|
2015-08-13 22:23:04 +00:00
|
|
|
} else {
|
2016-12-03 19:07:38 +00:00
|
|
|
memcpy(&playfield_[20], playfield_, 20);
|
2015-08-13 22:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x0b:
|
2016-12-03 19:07:38 +00:00
|
|
|
case 0x0c: player_reflection_mask_[decodedAddress - 0x0b] = (*value)&8 ? 0 : 7; break;
|
2015-08-13 20:32:22 +00:00
|
|
|
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x0d:
|
2016-12-03 19:07:38 +00:00
|
|
|
playfield_[0] = ((*value) >> 4)&1;
|
|
|
|
playfield_[1] = ((*value) >> 5)&1;
|
|
|
|
playfield_[2] = ((*value) >> 6)&1;
|
|
|
|
playfield_[3] = (*value) >> 7;
|
2015-08-13 22:23:04 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
if(playfield_control_&1) {
|
|
|
|
for(int c = 0; c < 4; c++) playfield_[39-c] = playfield_[c];
|
2015-08-13 22:23:04 +00:00
|
|
|
} else {
|
2016-12-03 19:07:38 +00:00
|
|
|
memcpy(&playfield_[20], playfield_, 4);
|
2015-08-13 22:23:04 +00:00
|
|
|
}
|
2015-08-13 20:32:22 +00:00
|
|
|
break;
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x0e:
|
2016-12-03 19:07:38 +00:00
|
|
|
playfield_[4] = (*value) >> 7;
|
|
|
|
playfield_[5] = ((*value) >> 6)&1;
|
|
|
|
playfield_[6] = ((*value) >> 5)&1;
|
|
|
|
playfield_[7] = ((*value) >> 4)&1;
|
|
|
|
playfield_[8] = ((*value) >> 3)&1;
|
|
|
|
playfield_[9] = ((*value) >> 2)&1;
|
|
|
|
playfield_[10] = ((*value) >> 1)&1;
|
|
|
|
playfield_[11] = (*value)&1;
|
|
|
|
|
|
|
|
if(playfield_control_&1) {
|
|
|
|
for(int c = 0; c < 8; c++) playfield_[35-c] = playfield_[c+4];
|
2015-08-13 22:23:04 +00:00
|
|
|
} else {
|
2016-12-03 19:07:38 +00:00
|
|
|
memcpy(&playfield_[24], &playfield_[4], 8);
|
2015-08-13 22:23:04 +00:00
|
|
|
}
|
2015-08-13 20:32:22 +00:00
|
|
|
break;
|
|
|
|
case 0x0f:
|
2016-12-03 19:07:38 +00:00
|
|
|
playfield_[19] = (*value) >> 7;
|
|
|
|
playfield_[18] = ((*value) >> 6)&1;
|
|
|
|
playfield_[17] = ((*value) >> 5)&1;
|
|
|
|
playfield_[16] = ((*value) >> 4)&1;
|
|
|
|
playfield_[15] = ((*value) >> 3)&1;
|
|
|
|
playfield_[14] = ((*value) >> 2)&1;
|
|
|
|
playfield_[13] = ((*value) >> 1)&1;
|
|
|
|
playfield_[12] = (*value)&1;
|
|
|
|
|
|
|
|
if(playfield_control_&1) {
|
|
|
|
for(int c = 0; c < 8; c++) playfield_[27-c] = playfield_[c+12];
|
2015-08-13 22:23:04 +00:00
|
|
|
} else {
|
2016-12-03 19:07:38 +00:00
|
|
|
memcpy(&playfield_[32], &playfield_[12], 8);
|
2015-08-13 22:23:04 +00:00
|
|
|
}
|
2015-08-13 20:32:22 +00:00
|
|
|
break;
|
2015-08-12 23:31:57 +00:00
|
|
|
|
|
|
|
case 0x10: case 0x11: case 0x12: case 0x13:
|
2016-05-18 11:31:05 +00:00
|
|
|
case 0x14:
|
2016-12-03 19:07:38 +00:00
|
|
|
upcoming_events_[(upcoming_events_pointer_ + 4)%number_of_upcoming_events].updates |= Event::Action::ResetCounter;
|
|
|
|
upcoming_events_[(upcoming_events_pointer_ + 4)%number_of_upcoming_events].counter = decodedAddress - 0x10;
|
2016-05-18 11:31:05 +00:00
|
|
|
break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2016-06-01 23:27:04 +00:00
|
|
|
case 0x15: case 0x16:
|
|
|
|
update_audio();
|
2016-12-03 19:07:38 +00:00
|
|
|
speaker_->set_control(decodedAddress - 0x15, *value);
|
2016-06-01 23:27:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x17: case 0x18:
|
|
|
|
update_audio();
|
2016-12-03 19:07:38 +00:00
|
|
|
speaker_->set_divider(decodedAddress - 0x17, *value);
|
2016-06-01 23:27:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x19: case 0x1a:
|
|
|
|
update_audio();
|
2016-12-03 19:07:38 +00:00
|
|
|
speaker_->set_volume(decodedAddress - 0x19, *value);
|
2016-06-01 23:27:04 +00:00
|
|
|
break;
|
|
|
|
|
2015-07-31 20:54:20 +00:00
|
|
|
case 0x1c:
|
2016-12-03 19:07:38 +00:00
|
|
|
ball_graphics_enable_[1] = ball_graphics_enable_[0];
|
2015-07-31 20:54:20 +00:00
|
|
|
case 0x1b: {
|
2015-08-12 23:31:57 +00:00
|
|
|
int index = decodedAddress - 0x1b;
|
2016-12-03 19:07:38 +00:00
|
|
|
player_graphics_[0][index] = *value;
|
|
|
|
player_graphics_[1][index^1] = player_graphics_[0][index^1];
|
2015-07-31 20:54:20 +00:00
|
|
|
} break;
|
2016-05-26 01:43:19 +00:00
|
|
|
case 0x1d:
|
2016-06-01 01:23:44 +00:00
|
|
|
case 0x1e:
|
2016-12-03 19:07:38 +00:00
|
|
|
missile_graphics_enable_[decodedAddress - 0x1d] = ((*value) >> 1)&1;
|
2016-06-01 01:23:44 +00:00
|
|
|
// printf("e:%02x <- %c\n", decodedAddress - 0x1d, ((*value)&1) ? 'E' : '-');
|
|
|
|
break;
|
2015-07-31 20:54:20 +00:00
|
|
|
case 0x1f:
|
2016-12-03 19:07:38 +00:00
|
|
|
ball_graphics_enable_[0] = ((*value) >> 1)&1;
|
2015-07-31 20:54:20 +00:00
|
|
|
break;
|
|
|
|
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x20:
|
|
|
|
case 0x21:
|
|
|
|
case 0x22:
|
|
|
|
case 0x23:
|
2015-08-13 17:59:23 +00:00
|
|
|
case 0x24:
|
2016-12-03 19:07:38 +00:00
|
|
|
object_motions_[decodedAddress - 0x20] = *value;
|
2015-08-13 17:59:23 +00:00
|
|
|
break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
case 0x25: player_graphics_selector_[0] = (*value)&1; break;
|
|
|
|
case 0x26: player_graphics_selector_[1] = (*value)&1; break;
|
|
|
|
case 0x27: ball_graphics_selector_ = (*value)&1; break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2015-08-10 15:43:45 +00:00
|
|
|
case 0x28:
|
|
|
|
case 0x29:
|
2016-05-22 21:01:56 +00:00
|
|
|
{
|
2016-05-30 23:56:36 +00:00
|
|
|
// TODO: this should properly mean setting a flag and propagating later, I think?
|
2016-05-22 21:01:56 +00:00
|
|
|
int index = decodedAddress - 0x28;
|
2016-12-03 19:07:38 +00:00
|
|
|
if(!(*value&0x02) && missile_graphics_reset_[index])
|
2016-05-22 21:01:56 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
object_counter_[object_counter_pointer_][index + 2].count = object_counter_[object_counter_pointer_][index].count;
|
2016-05-22 21:01:56 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
uint8_t repeatMask = player_and_missile_size_[index] & 7;
|
2016-05-22 21:01:56 +00:00
|
|
|
int extra_offset;
|
|
|
|
switch(repeatMask)
|
|
|
|
{
|
|
|
|
default: extra_offset = 3; break;
|
|
|
|
case 5: extra_offset = 6; break;
|
|
|
|
case 7: extra_offset = 10; break;
|
|
|
|
}
|
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
object_counter_[object_counter_pointer_][index + 2].count = (object_counter_[object_counter_pointer_][index + 2].count + extra_offset)%160;
|
2016-05-22 21:01:56 +00:00
|
|
|
}
|
2016-12-03 19:07:38 +00:00
|
|
|
missile_graphics_reset_[index] = !!((*value) & 0x02);
|
2016-06-01 01:23:44 +00:00
|
|
|
// printf("r:%02x <- %c\n", decodedAddress - 0x28, ((*value)&2) ? 'R' : '-');
|
2016-05-22 21:01:56 +00:00
|
|
|
}
|
2015-08-10 15:55:16 +00:00
|
|
|
break;
|
2015-07-31 20:54:20 +00:00
|
|
|
|
2016-06-01 01:23:44 +00:00
|
|
|
case 0x2a: {
|
2016-05-19 12:30:10 +00:00
|
|
|
// justification for +5: "we need to wait at least 71 [clocks] before the HMOVE operation is complete";
|
|
|
|
// which will take 16*4 + 2 = 66 cycles from the first compare, implying the first compare must be
|
|
|
|
// in five cycles from now
|
2016-12-03 19:07:38 +00:00
|
|
|
// int start_pause = ((horizontal_timer_ + 3)&3) + 4;
|
|
|
|
upcoming_events_[(upcoming_events_pointer_ + 5)%number_of_upcoming_events].updates |= Event::Action::HMoveSetup;
|
2016-06-01 01:23:44 +00:00
|
|
|
} break;
|
2015-08-10 14:09:40 +00:00
|
|
|
case 0x2b:
|
2016-12-03 19:07:38 +00:00
|
|
|
object_motions_[0] =
|
|
|
|
object_motions_[1] =
|
|
|
|
object_motions_[2] =
|
|
|
|
object_motions_[3] =
|
|
|
|
object_motions_[4] = 0;
|
2015-08-10 15:55:16 +00:00
|
|
|
break;
|
2015-08-12 23:31:57 +00:00
|
|
|
case 0x2c:
|
2016-12-03 19:07:38 +00:00
|
|
|
collisions_[0] = collisions_[1] = collisions_[2] =
|
|
|
|
collisions_[3] = collisions_[4] = collisions_[5] = 0x3f;
|
|
|
|
collisions_[6] = 0x7f;
|
|
|
|
collisions_[7] = 0x3f;
|
2015-08-12 23:31:57 +00:00
|
|
|
break;
|
2015-07-31 20:54:20 +00:00
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 20:54:20 +00:00
|
|
|
// check for a PIA access
|
2016-04-25 02:32:24 +00:00
|
|
|
if((address&0x1280) == 0x280) {
|
2015-07-31 20:54:20 +00:00
|
|
|
if(isReadOperation(operation)) {
|
2016-12-03 19:07:38 +00:00
|
|
|
returnValue &= mos6532_.get_register(address);
|
2015-07-31 20:54:20 +00:00
|
|
|
} else {
|
2016-12-03 19:07:38 +00:00
|
|
|
mos6532_.set_register(address, *value);
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 20:54:20 +00:00
|
|
|
if(isReadOperation(operation)) {
|
|
|
|
*value = returnValue;
|
|
|
|
}
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
mos6532_.run_for_cycles(cycles_run_for / 3);
|
2016-06-03 00:15:48 +00:00
|
|
|
|
2016-05-28 01:51:27 +00:00
|
|
|
return cycles_run_for / 3;
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
void Machine::set_digital_input(Atari2600DigitalInput input, bool state)
|
|
|
|
{
|
|
|
|
switch (input) {
|
2016-12-03 19:07:38 +00:00
|
|
|
case Atari2600DigitalInputJoy1Up: mos6532_.update_port_input(0, 0x10, state); break;
|
|
|
|
case Atari2600DigitalInputJoy1Down: mos6532_.update_port_input(0, 0x20, state); break;
|
|
|
|
case Atari2600DigitalInputJoy1Left: mos6532_.update_port_input(0, 0x40, state); break;
|
|
|
|
case Atari2600DigitalInputJoy1Right: mos6532_.update_port_input(0, 0x80, state); break;
|
2016-06-19 22:57:40 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
case Atari2600DigitalInputJoy2Up: mos6532_.update_port_input(0, 0x01, state); break;
|
|
|
|
case Atari2600DigitalInputJoy2Down: mos6532_.update_port_input(0, 0x02, state); break;
|
|
|
|
case Atari2600DigitalInputJoy2Left: mos6532_.update_port_input(0, 0x04, state); break;
|
|
|
|
case Atari2600DigitalInputJoy2Right: mos6532_.update_port_input(0, 0x08, state); break;
|
2015-08-19 00:58:05 +00:00
|
|
|
|
|
|
|
// TODO: latching
|
2016-12-03 19:07:38 +00:00
|
|
|
case Atari2600DigitalInputJoy1Fire: if(state) tia_input_value_[0] &= ~0x80; else tia_input_value_[0] |= 0x80; break;
|
|
|
|
case Atari2600DigitalInputJoy2Fire: if(state) tia_input_value_[1] &= ~0x80; else tia_input_value_[1] |= 0x80; break;
|
2015-08-19 00:33:24 +00:00
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 23:36:34 +00:00
|
|
|
void Machine::set_switch_is_enabled(Atari2600Switch input, bool state)
|
|
|
|
{
|
|
|
|
switch(input) {
|
2016-12-03 19:07:38 +00:00
|
|
|
case Atari2600SwitchReset: mos6532_.update_port_input(1, 0x01, state); break;
|
|
|
|
case Atari2600SwitchSelect: mos6532_.update_port_input(1, 0x02, state); break;
|
|
|
|
case Atari2600SwitchColour: mos6532_.update_port_input(1, 0x08, state); break;
|
|
|
|
case Atari2600SwitchLeftPlayerDifficulty: mos6532_.update_port_input(1, 0x40, state); break;
|
|
|
|
case Atari2600SwitchRightPlayerDifficulty: mos6532_.update_port_input(1, 0x80, state); break;
|
2016-06-19 23:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 23:34:45 +00:00
|
|
|
void Machine::configure_as_target(const StaticAnalyser::Target &target)
|
2015-07-16 23:56:02 +00:00
|
|
|
{
|
2016-09-15 23:34:45 +00:00
|
|
|
if(!target.cartridges.front()->get_segments().size()) return;
|
|
|
|
Storage::Cartridge::Cartridge::Segment segment = target.cartridges.front()->get_segments().front();
|
|
|
|
size_t length = segment.data.size();
|
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
rom_size_ = 1024;
|
|
|
|
while(rom_size_ < length && rom_size_ < 32768) rom_size_ <<= 1;
|
2015-08-13 12:24:02 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
delete[] rom_;
|
|
|
|
rom_ = new uint8_t[rom_size_];
|
2015-08-17 04:34:01 +00:00
|
|
|
|
|
|
|
size_t offset = 0;
|
2016-12-03 19:07:38 +00:00
|
|
|
const size_t copy_step = std::min(rom_size_, length);
|
|
|
|
while(offset < rom_size_)
|
2015-08-17 04:34:01 +00:00
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
size_t copy_length = std::min(copy_step, rom_size_ - offset);
|
|
|
|
memcpy(&rom_[offset], &segment.data[0], copy_length);
|
2015-08-17 04:34:01 +00:00
|
|
|
offset += copy_length;
|
|
|
|
}
|
2015-08-13 12:24:02 +00:00
|
|
|
|
2016-12-03 19:07:38 +00:00
|
|
|
size_t romMask = rom_size_ - 1;
|
|
|
|
rom_pages_[0] = rom_;
|
|
|
|
rom_pages_[1] = &rom_[1024 & romMask];
|
|
|
|
rom_pages_[2] = &rom_[2048 & romMask];
|
|
|
|
rom_pages_[3] = &rom_[3072 & romMask];
|
2015-07-16 23:56:02 +00:00
|
|
|
}
|
2016-06-01 23:27:04 +00:00
|
|
|
|
|
|
|
#pragma mark - Audio
|
|
|
|
|
|
|
|
void Machine::update_audio()
|
|
|
|
{
|
2016-12-03 19:07:38 +00:00
|
|
|
unsigned int audio_cycles = cycles_since_speaker_update_ / 114;
|
|
|
|
|
|
|
|
speaker_->run_for_cycles(audio_cycles);
|
|
|
|
cycles_since_speaker_update_ %= 114;
|
2016-06-01 23:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::synchronise()
|
|
|
|
{
|
|
|
|
update_audio();
|
|
|
|
}
|
|
|
|
|