1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-26 08:49:37 +00:00

Switched the 2600 to postfix and non-camel-case instance variable names.

This commit is contained in:
Thomas Harte 2016-12-03 14:07:38 -05:00
parent b81c058c0a
commit ebb62a2d78
2 changed files with 291 additions and 304 deletions

View File

@ -18,18 +18,18 @@ namespace {
}
Machine::Machine() :
_horizontalTimer(0),
_lastOutputStateDuration(0),
_lastOutputState(OutputState::Sync),
_rom(nullptr),
_tiaInputValue{0xff, 0xff},
_upcomingEventsPointer(0),
_objectCounterPointer(0),
_stateByTime(_stateByExtendTime[0]),
_cycles_since_speaker_update(0),
_is_pal_region(false)
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)
{
memset(_collisions, 0xff, sizeof(_collisions));
memset(collisions_, 0xff, sizeof(collisions_));
setup_reported_collisions();
for(int vbextend = 0; vbextend < 2; vbextend++)
@ -51,7 +51,7 @@ Machine::Machine() :
default: state = OutputState::Pixel; break;
}
_stateByExtendTime[vbextend][c] = state;
state_by_extend_time_[vbextend][c] = state;
}
}
set_clock_rate(NTSC_clock_rate);
@ -59,12 +59,12 @@ Machine::Machine() :
void Machine::setup_output(float aspect_ratio)
{
_speaker.reset(new Speaker);
_crt.reset(new Outputs::CRT::CRT(228, 1, 263, Outputs::CRT::ColourSpace::YIQ, 228, 1, 1));
_crt->set_output_device(Outputs::CRT::Television);
speaker_.reset(new Speaker);
crt_.reset(new Outputs::CRT::CRT(228, 1, 263, Outputs::CRT::ColourSpace::YIQ, 228, 1, 1));
crt_->set_output_device(Outputs::CRT::Television);
// this is the NTSC phase offset function; see below for PAL
_crt->set_composite_sampling_function(
crt_->set_composite_sampling_function(
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
"{"
"uint c = texture(texID, coordinate).r;"
@ -74,13 +74,13 @@ void Machine::setup_output(float aspect_ratio)
"float phaseOffset = 6.283185308 * float(iPhase - 1u) / 13.0;"
"return mix(float(y) / 14.0, step(1, iPhase) * cos(phase + phaseOffset), amplitude);"
"}");
_speaker->set_input_rate((float)(get_clock_rate() / 38.0));
speaker_->set_input_rate((float)(get_clock_rate() / 38.0));
}
void Machine::switch_region()
{
// the PAL function
_crt->set_composite_sampling_function(
crt_->set_composite_sampling_function(
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
"{"
"uint c = texture(texID, coordinate).r;"
@ -93,39 +93,39 @@ void Machine::switch_region()
"return mix(float(y) / 14.0, step(4, (iPhase + 2u) & 15u) * cos(phase + phaseOffset), amplitude);"
"}");
_crt->set_new_timing(228, 312, Outputs::CRT::ColourSpace::YUV, 228, 1);
crt_->set_new_timing(228, 312, Outputs::CRT::ColourSpace::YUV, 228, 1);
_is_pal_region = true;
_speaker->set_input_rate((float)(get_clock_rate() / 38.0));
is_pal_region_ = true;
speaker_->set_input_rate((float)(get_clock_rate() / 38.0));
set_clock_rate(PAL_clock_rate);
}
void Machine::close_output()
{
_crt = nullptr;
crt_ = nullptr;
}
Machine::~Machine()
{
delete[] _rom;
delete[] rom_;
close_output();
}
void Machine::update_timers(int mask)
{
unsigned int upcomingPointerPlus4 = (_upcomingEventsPointer + 4)%number_of_upcoming_events;
unsigned int upcoming_pointer_plus_4 = (upcoming_events_pointer_ + 4)%number_of_upcoming_events;
_objectCounterPointer = (_objectCounterPointer + 1)%number_of_recorded_counters;
ObjectCounter *oneClockAgo = _objectCounter[(_objectCounterPointer - 1 + number_of_recorded_counters)%number_of_recorded_counters];
ObjectCounter *twoClocksAgo = _objectCounter[(_objectCounterPointer - 2 + number_of_recorded_counters)%number_of_recorded_counters];
ObjectCounter *now = _objectCounter[_objectCounterPointer];
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_];
// grab the background now, for application in four clocks
if(mask & (1 << 5) && !(_horizontalTimer&3))
if(mask & (1 << 5) && !(horizontal_timer_&3))
{
unsigned int offset = 4 + _horizontalTimer - (horizontalTimerPeriod - 160);
_upcomingEvents[upcomingPointerPlus4].updates |= Event::Action::Playfield;
_upcomingEvents[upcomingPointerPlus4].playfieldPixel = _playfield[(offset >> 2)%40];
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];
}
if(mask & (1 << 4))
@ -150,7 +150,7 @@ void Machine::update_timers(int mask)
// update the count
now[c].count = (oneClockAgo[c].count + 1)%160;
uint8_t repeatMask = _playerAndMissileSize[c&1] & 7;
uint8_t repeatMask = player_and_missile_size_[c&1] & 7;
ObjectCounter *rollover;
ObjectCounter *equality;
@ -181,9 +181,9 @@ void Machine::update_timers(int mask)
if(
(rollover[c].count == 159) ||
(_hasSecondCopy[c&1] && equality[c].count == 16) ||
(_hasThirdCopy[c&1] && equality[c].count == 32) ||
(_hasFourthCopy[c&1] && equality[c].count == 64)
(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)
)
{
now[c].pixel = 0;
@ -199,16 +199,16 @@ void Machine::update_timers(int mask)
uint8_t Machine::get_output_pixel()
{
ObjectCounter *now = _objectCounter[_objectCounterPointer];
ObjectCounter *now = object_counter_[object_counter_pointer_];
// get the playfield pixel
unsigned int offset = _horizontalTimer - (horizontalTimerPeriod - 160);
uint8_t playfieldColour = ((_playfieldControl&6) == 2) ? _playerColour[offset / 80] : _playfieldColour;
unsigned int offset = horizontal_timer_ - (horizontalTimerPeriod - 160);
uint8_t playfieldColour = ((playfield_control_&6) == 2) ? player_colour_[offset / 80] : playfield_colour_;
// ball pixel
uint8_t ballPixel = 0;
if(now[4].pixel < _ballSize) {
ballPixel = _ballGraphicsEnable[_ballGraphicsSelector];
if(now[4].pixel < ball_size_) {
ballPixel = ball_graphics_enable_[ball_graphics_selector_];
}
// determine the player and missile pixels
@ -216,33 +216,33 @@ uint8_t Machine::get_output_pixel()
uint8_t missilePixels[2] = { 0, 0 };
for(int c = 0; c < 2; c++)
{
if(_playerGraphics[c] && now[c].pixel < 8) {
playerPixels[c] = (_playerGraphics[_playerGraphicsSelector[c]][c] >> (now[c].pixel ^ _playerReflectionMask[c])) & 1;
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;
}
if(!_missileGraphicsReset[c] && now[c+2].pixel < _missileSize[c]) {
missilePixels[c] = _missileGraphicsEnable[c];
if(!missile_graphics_reset_[c] && now[c+2].pixel < missile_size_[c]) {
missilePixels[c] = missile_graphics_enable_[c];
}
}
// accumulate collisions
int pixel_mask = playerPixels[0] | (playerPixels[1] << 1) | (missilePixels[0] << 2) | (missilePixels[1] << 3) | (ballPixel << 4) | (_playfieldOutput << 5);
_collisions[0] |= _reportedCollisions[pixel_mask][0];
_collisions[1] |= _reportedCollisions[pixel_mask][1];
_collisions[2] |= _reportedCollisions[pixel_mask][2];
_collisions[3] |= _reportedCollisions[pixel_mask][3];
_collisions[4] |= _reportedCollisions[pixel_mask][4];
_collisions[5] |= _reportedCollisions[pixel_mask][5];
_collisions[6] |= _reportedCollisions[pixel_mask][6];
_collisions[7] |= _reportedCollisions[pixel_mask][7];
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];
// apply appropriate priority to pick a colour
uint8_t playfieldPixel = _playfieldOutput | ballPixel;
uint8_t outputColour = playfieldPixel ? playfieldColour : _backgroundColour;
uint8_t playfield_pixel = playfield_output_ | ballPixel;
uint8_t outputColour = playfield_pixel ? playfieldColour : background_colour_;
if(!(_playfieldControl&0x04) || !playfieldPixel) {
if(playerPixels[1] || missilePixels[1]) outputColour = _playerColour[1];
if(playerPixels[0] || missilePixels[0]) outputColour = _playerColour[0];
if(!(playfield_control_&0x04) || !playfield_pixel) {
if(playerPixels[1] || missilePixels[1]) outputColour = player_colour_[1];
if(playerPixels[0] || missilePixels[0]) outputColour = player_colour_[0];
}
// return colour
@ -253,32 +253,32 @@ void Machine::setup_reported_collisions()
{
for(int c = 0; c < 64; c++)
{
memset(_reportedCollisions[c], 0, 8);
memset(reported_collisions_[c], 0, 8);
int playerPixels[2] = { c&1, (c >> 1)&1 };
int missilePixels[2] = { (c >> 2)&1, (c >> 3)&1 };
int ballPixel = (c >> 4)&1;
int playfieldPixel = (c >> 5)&1;
int playfield_pixel = (c >> 5)&1;
if(playerPixels[0] | playerPixels[1]) {
_reportedCollisions[c][0] |= ((missilePixels[0] & playerPixels[1]) << 7) | ((missilePixels[0] & playerPixels[0]) << 6);
_reportedCollisions[c][1] |= ((missilePixels[1] & playerPixels[0]) << 7) | ((missilePixels[1] & playerPixels[1]) << 6);
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);
_reportedCollisions[c][2] |= ((playfieldPixel & playerPixels[0]) << 7) | ((ballPixel & playerPixels[0]) << 6);
_reportedCollisions[c][3] |= ((playfieldPixel & playerPixels[1]) << 7) | ((ballPixel & playerPixels[1]) << 6);
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);
_reportedCollisions[c][7] |= ((playerPixels[0] & playerPixels[1]) << 7);
reported_collisions_[c][7] |= ((playerPixels[0] & playerPixels[1]) << 7);
}
if(playfieldPixel | ballPixel) {
_reportedCollisions[c][4] |= ((playfieldPixel & missilePixels[0]) << 7) | ((ballPixel & missilePixels[0]) << 6);
_reportedCollisions[c][5] |= ((playfieldPixel & missilePixels[1]) << 7) | ((ballPixel & missilePixels[1]) << 6);
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);
_reportedCollisions[c][6] |= ((playfieldPixel & ballPixel) << 7);
reported_collisions_[c][6] |= ((playfield_pixel & ballPixel) << 7);
}
if(missilePixels[0] & missilePixels[1])
_reportedCollisions[c][7] |= (1 << 6);
reported_collisions_[c][7] |= (1 << 6);
}
}
@ -286,103 +286,103 @@ void Machine::output_pixels(unsigned int count)
{
while(count--)
{
if(_upcomingEvents[_upcomingEventsPointer].updates)
if(upcoming_events_[upcoming_events_pointer_].updates)
{
// apply any queued changes and flush the record
if(_upcomingEvents[_upcomingEventsPointer].updates & Event::Action::HMoveSetup)
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::HMoveSetup)
{
// schedule an extended left border
_stateByTime = _stateByExtendTime[1];
state_by_time_ = state_by_extend_time_[1];
// clear any ongoing moves
if(_hMoveFlags)
if(hmove_flags_)
{
for(int c = 0; c < number_of_upcoming_events; c++)
{
_upcomingEvents[c].updates &= ~(Event::Action::HMoveCompare | Event::Action::HMoveDecrement);
upcoming_events_[c].updates &= ~(Event::Action::HMoveCompare | Event::Action::HMoveDecrement);
}
}
// schedule new moves
_hMoveFlags = 0x1f;
_hMoveCounter = 15;
hmove_flags_ = 0x1f;
hmove_counter_ = 15;
// follow-through into a compare immediately
_upcomingEvents[_upcomingEventsPointer].updates |= Event::Action::HMoveCompare;
upcoming_events_[upcoming_events_pointer_].updates |= Event::Action::HMoveCompare;
}
if(_upcomingEvents[_upcomingEventsPointer].updates & Event::Action::HMoveCompare)
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::HMoveCompare)
{
for(int c = 0; c < 5; c++)
{
if(((_objectMotion[c] >> 4)^_hMoveCounter) == 7)
if(((object_motions_[c] >> 4)^hmove_counter_) == 7)
{
_hMoveFlags &= ~(1 << c);
hmove_flags_ &= ~(1 << c);
}
}
if(_hMoveFlags)
if(hmove_flags_)
{
if(_hMoveCounter) _hMoveCounter--;
_upcomingEvents[(_upcomingEventsPointer+4)%number_of_upcoming_events].updates |= Event::Action::HMoveCompare;
_upcomingEvents[(_upcomingEventsPointer+2)%number_of_upcoming_events].updates |= Event::Action::HMoveDecrement;
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;
}
}
if(_upcomingEvents[_upcomingEventsPointer].updates & Event::Action::HMoveDecrement)
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::HMoveDecrement)
{
update_timers(_hMoveFlags);
update_timers(hmove_flags_);
}
if(_upcomingEvents[_upcomingEventsPointer].updates & Event::Action::ResetCounter)
if(upcoming_events_[upcoming_events_pointer_].updates & Event::Action::ResetCounter)
{
_objectCounter[_objectCounterPointer][_upcomingEvents[_upcomingEventsPointer].counter].count = 0;
object_counter_[object_counter_pointer_][upcoming_events_[upcoming_events_pointer_].counter].count = 0;
}
// zero out current update event
_upcomingEvents[_upcomingEventsPointer].updates = 0;
upcoming_events_[upcoming_events_pointer_].updates = 0;
}
// progress to next event
_upcomingEventsPointer = (_upcomingEventsPointer + 1)%number_of_upcoming_events;
upcoming_events_pointer_ = (upcoming_events_pointer_ + 1)%number_of_upcoming_events;
// determine which output state is currently active
OutputState primary_state = _stateByTime[_horizontalTimer >> 2];
OutputState primary_state = state_by_time_[horizontal_timer_ >> 2];
OutputState effective_state = primary_state;
// update pixel timers
if(primary_state == OutputState::Pixel) update_timers(~0);
// update the background chain
if(_horizontalTimer >= 64 && _horizontalTimer <= 160+64 && !(_horizontalTimer&3))
if(horizontal_timer_ >= 64 && horizontal_timer_ <= 160+64 && !(horizontal_timer_&3))
{
_playfieldOutput = _nextPlayfieldOutput;
_nextPlayfieldOutput = _playfield[(_horizontalTimer - 64) >> 2];
playfield_output_ = next_playfield_output_;
next_playfield_output_ = playfield_[(horizontal_timer_ - 64) >> 2];
}
// if vsync is enabled, output the opposite of the automatic hsync output;
// also honour the vertical blank flag
if(_vSyncEnabled) {
if(vsync_enabled_) {
effective_state = (effective_state = OutputState::Sync) ? OutputState::Blank : OutputState::Sync;
} else if(_vBlankEnabled && effective_state == OutputState::Pixel) {
} else if(vblank_enabled_ && effective_state == OutputState::Pixel) {
effective_state = OutputState::Blank;
}
// decide what that means needs to be communicated to the CRT
_lastOutputStateDuration++;
if(effective_state != _lastOutputState) {
switch(_lastOutputState) {
case OutputState::Blank: _crt->output_blank(_lastOutputStateDuration); break;
case OutputState::Sync: _crt->output_sync(_lastOutputStateDuration); break;
case OutputState::ColourBurst: _crt->output_colour_burst(_lastOutputStateDuration, 96, 0); break;
case OutputState::Pixel: _crt->output_data(_lastOutputStateDuration, 1); break;
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;
}
_lastOutputStateDuration = 0;
_lastOutputState = effective_state;
last_output_state_duration_ = 0;
last_output_state_ = effective_state;
if(effective_state == OutputState::Pixel) {
_outputBuffer = _crt->allocate_write_area(160);
output_buffer_ = crt_->allocate_write_area(160);
} else {
_outputBuffer = nullptr;
output_buffer_ = nullptr;
}
}
@ -390,19 +390,19 @@ void Machine::output_pixels(unsigned int count)
if(effective_state == OutputState::Pixel)
{
uint8_t colour = get_output_pixel();
if(_outputBuffer)
if(output_buffer_)
{
*_outputBuffer = colour;
_outputBuffer++;
*output_buffer_ = colour;
output_buffer_++;
}
}
// advance horizontal timer, perform reset actions if desired
_horizontalTimer = (_horizontalTimer + 1) % horizontalTimerPeriod;
if(!_horizontalTimer)
horizontal_timer_ = (horizontal_timer_ + 1) % horizontalTimerPeriod;
if(!horizontal_timer_)
{
// switch back to a normal length left border
_stateByTime = _stateByExtendTime[0];
state_by_time_ = state_by_extend_time_[0];
set_ready_line(false);
}
}
@ -418,47 +418,47 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
// effect until the next read; therefore it isn't safe to assume that signalling ready immediately
// skips to the end of the line.
if(operation == CPU6502::BusOperation::Ready) {
unsigned int distance_to_end_of_ready = horizontalTimerPeriod - _horizontalTimer;
unsigned int distance_to_end_of_ready = horizontalTimerPeriod - horizontal_timer_;
cycles_run_for = distance_to_end_of_ready;
}
output_pixels(cycles_run_for);
_cycles_since_speaker_update += cycles_run_for;
cycles_since_speaker_update_ += cycles_run_for;
if(operation != CPU6502::BusOperation::Ready) {
// check for a paging access
if(_rom_size > 4096 && ((address & 0x1f00) == 0x1f00)) {
uint8_t *base_ptr = _romPages[0];
uint8_t first_paging_register = (uint8_t)(0xf8 - (_rom_size >> 14)*2);
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);
const uint8_t paging_register = address&0xff;
if(paging_register >= first_paging_register) {
const uint16_t selected_page = paging_register - first_paging_register;
if(selected_page * 4096 < _rom_size) {
base_ptr = &_rom[selected_page * 4096];
if(selected_page * 4096 < rom_size_) {
base_ptr = &rom_[selected_page * 4096];
}
}
if(base_ptr != _romPages[0]) {
_romPages[0] = base_ptr;
_romPages[1] = base_ptr + 1024;
_romPages[2] = base_ptr + 2048;
_romPages[3] = base_ptr + 3072;
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;
}
}
// check for a ROM read
if((address&0x1000) && isReadOperation(operation)) {
returnValue &= _romPages[(address >> 10)&3][address&1023];
returnValue &= rom_pages_[(address >> 10)&3][address&1023];
}
// check for a RAM access
if((address&0x1280) == 0x80) {
if(isReadOperation(operation)) {
returnValue &= _mos6532.get_ram(address);
returnValue &= mos6532_.get_ram(address);
} else {
_mos6532.set_ram(address, *value);
mos6532_.set_ram(address, *value);
}
}
@ -475,7 +475,7 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
case 0x05: // missile 1 / playfield / ball collisions
case 0x06: // ball / playfield collisions
case 0x07: // player / player, missile / missile collisions
returnValue &= _collisions[decodedAddress];
returnValue &= collisions_[decodedAddress];
break;
case 0x08:
@ -487,23 +487,23 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
case 0x0c:
case 0x0d:
returnValue &= _tiaInputValue[decodedAddress - 0x0c];
returnValue &= tia_input_value_[decodedAddress - 0x0c];
break;
}
} else {
const uint16_t decodedAddress = address & 0x3f;
switch(decodedAddress) {
case 0x00:
_vSyncEnabled = !!(*value & 0x02);
vsync_enabled_ = !!(*value & 0x02);
break;
case 0x01: _vBlankEnabled = !!(*value & 0x02); break;
case 0x01: vblank_enabled_ = !!(*value & 0x02); break;
case 0x02:
if(_horizontalTimer) set_ready_line(true);
if(horizontal_timer_) set_ready_line(true);
break;
case 0x03:
// Reset is delayed by four cycles.
_horizontalTimer = horizontalTimerPeriod - 4;
horizontal_timer_ = horizontalTimerPeriod - 4;
// TODO: audio will now be out of synchronisation — fix
break;
@ -511,117 +511,117 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
case 0x04:
case 0x05: {
int entry = decodedAddress - 0x04;
_playerAndMissileSize[entry] = *value;
_missileSize[entry] = 1 << ((*value >> 4)&3);
player_and_missile_size_[entry] = *value;
missile_size_[entry] = 1 << ((*value >> 4)&3);
uint8_t repeatMask = (*value)&7;
_hasSecondCopy[entry] = (repeatMask == 1) || (repeatMask == 3);
_hasThirdCopy[entry] = (repeatMask == 2) || (repeatMask == 3) || (repeatMask == 6);
_hasFourthCopy[entry] = (repeatMask == 4) || (repeatMask == 6);
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);
} break;
case 0x06:
case 0x07: _playerColour[decodedAddress - 0x06] = *value; break;
case 0x08: _playfieldColour = *value; break;
case 0x09: _backgroundColour = *value; break;
case 0x07: player_colour_[decodedAddress - 0x06] = *value; break;
case 0x08: playfield_colour_ = *value; break;
case 0x09: background_colour_ = *value; break;
case 0x0a: {
uint8_t old_playfield_control = _playfieldControl;
_playfieldControl = *value;
_ballSize = 1 << ((_playfieldControl >> 4)&3);
uint8_t old_playfield_control = playfield_control_;
playfield_control_ = *value;
ball_size_ = 1 << ((playfield_control_ >> 4)&3);
// did the mirroring bit change?
if((_playfieldControl^old_playfield_control)&1) {
if(_playfieldControl&1) {
for(int c = 0; c < 20; c++) _playfield[c+20] = _playfield[19-c];
if((playfield_control_^old_playfield_control)&1) {
if(playfield_control_&1) {
for(int c = 0; c < 20; c++) playfield_[c+20] = playfield_[19-c];
} else {
memcpy(&_playfield[20], _playfield, 20);
memcpy(&playfield_[20], playfield_, 20);
}
}
} break;
case 0x0b:
case 0x0c: _playerReflectionMask[decodedAddress - 0x0b] = (*value)&8 ? 0 : 7; break;
case 0x0c: player_reflection_mask_[decodedAddress - 0x0b] = (*value)&8 ? 0 : 7; break;
case 0x0d:
_playfield[0] = ((*value) >> 4)&1;
_playfield[1] = ((*value) >> 5)&1;
_playfield[2] = ((*value) >> 6)&1;
_playfield[3] = (*value) >> 7;
playfield_[0] = ((*value) >> 4)&1;
playfield_[1] = ((*value) >> 5)&1;
playfield_[2] = ((*value) >> 6)&1;
playfield_[3] = (*value) >> 7;
if(_playfieldControl&1) {
for(int c = 0; c < 4; c++) _playfield[39-c] = _playfield[c];
if(playfield_control_&1) {
for(int c = 0; c < 4; c++) playfield_[39-c] = playfield_[c];
} else {
memcpy(&_playfield[20], _playfield, 4);
memcpy(&playfield_[20], playfield_, 4);
}
break;
case 0x0e:
_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;
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(_playfieldControl&1) {
for(int c = 0; c < 8; c++) _playfield[35-c] = _playfield[c+4];
if(playfield_control_&1) {
for(int c = 0; c < 8; c++) playfield_[35-c] = playfield_[c+4];
} else {
memcpy(&_playfield[24], &_playfield[4], 8);
memcpy(&playfield_[24], &playfield_[4], 8);
}
break;
case 0x0f:
_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;
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(_playfieldControl&1) {
for(int c = 0; c < 8; c++) _playfield[27-c] = _playfield[c+12];
if(playfield_control_&1) {
for(int c = 0; c < 8; c++) playfield_[27-c] = playfield_[c+12];
} else {
memcpy(&_playfield[32], &_playfield[12], 8);
memcpy(&playfield_[32], &playfield_[12], 8);
}
break;
case 0x10: case 0x11: case 0x12: case 0x13:
case 0x14:
_upcomingEvents[(_upcomingEventsPointer + 4)%number_of_upcoming_events].updates |= Event::Action::ResetCounter;
_upcomingEvents[(_upcomingEventsPointer + 4)%number_of_upcoming_events].counter = decodedAddress - 0x10;
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;
break;
case 0x15: case 0x16:
update_audio();
_speaker->set_control(decodedAddress - 0x15, *value);
speaker_->set_control(decodedAddress - 0x15, *value);
break;
case 0x17: case 0x18:
update_audio();
_speaker->set_divider(decodedAddress - 0x17, *value);
speaker_->set_divider(decodedAddress - 0x17, *value);
break;
case 0x19: case 0x1a:
update_audio();
_speaker->set_volume(decodedAddress - 0x19, *value);
speaker_->set_volume(decodedAddress - 0x19, *value);
break;
case 0x1c:
_ballGraphicsEnable[1] = _ballGraphicsEnable[0];
ball_graphics_enable_[1] = ball_graphics_enable_[0];
case 0x1b: {
int index = decodedAddress - 0x1b;
_playerGraphics[0][index] = *value;
_playerGraphics[1][index^1] = _playerGraphics[0][index^1];
player_graphics_[0][index] = *value;
player_graphics_[1][index^1] = player_graphics_[0][index^1];
} break;
case 0x1d:
case 0x1e:
_missileGraphicsEnable[decodedAddress - 0x1d] = ((*value) >> 1)&1;
missile_graphics_enable_[decodedAddress - 0x1d] = ((*value) >> 1)&1;
// printf("e:%02x <- %c\n", decodedAddress - 0x1d, ((*value)&1) ? 'E' : '-');
break;
case 0x1f:
_ballGraphicsEnable[0] = ((*value) >> 1)&1;
ball_graphics_enable_[0] = ((*value) >> 1)&1;
break;
case 0x20:
@ -629,23 +629,23 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
case 0x22:
case 0x23:
case 0x24:
_objectMotion[decodedAddress - 0x20] = *value;
object_motions_[decodedAddress - 0x20] = *value;
break;
case 0x25: _playerGraphicsSelector[0] = (*value)&1; break;
case 0x26: _playerGraphicsSelector[1] = (*value)&1; break;
case 0x27: _ballGraphicsSelector = (*value)&1; break;
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;
case 0x28:
case 0x29:
{
// TODO: this should properly mean setting a flag and propagating later, I think?
int index = decodedAddress - 0x28;
if(!(*value&0x02) && _missileGraphicsReset[index])
if(!(*value&0x02) && missile_graphics_reset_[index])
{
_objectCounter[_objectCounterPointer][index + 2].count = _objectCounter[_objectCounterPointer][index].count;
object_counter_[object_counter_pointer_][index + 2].count = object_counter_[object_counter_pointer_][index].count;
uint8_t repeatMask = _playerAndMissileSize[index] & 7;
uint8_t repeatMask = player_and_missile_size_[index] & 7;
int extra_offset;
switch(repeatMask)
{
@ -654,9 +654,9 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
case 7: extra_offset = 10; break;
}
_objectCounter[_objectCounterPointer][index + 2].count = (_objectCounter[_objectCounterPointer][index + 2].count + extra_offset)%160;
object_counter_[object_counter_pointer_][index + 2].count = (object_counter_[object_counter_pointer_][index + 2].count + extra_offset)%160;
}
_missileGraphicsReset[index] = !!((*value) & 0x02);
missile_graphics_reset_[index] = !!((*value) & 0x02);
// printf("r:%02x <- %c\n", decodedAddress - 0x28, ((*value)&2) ? 'R' : '-');
}
break;
@ -665,21 +665,21 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
// 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
// int start_pause = ((_horizontalTimer + 3)&3) + 4;
_upcomingEvents[(_upcomingEventsPointer + 5)%number_of_upcoming_events].updates |= Event::Action::HMoveSetup;
// int start_pause = ((horizontal_timer_ + 3)&3) + 4;
upcoming_events_[(upcoming_events_pointer_ + 5)%number_of_upcoming_events].updates |= Event::Action::HMoveSetup;
} break;
case 0x2b:
_objectMotion[0] =
_objectMotion[1] =
_objectMotion[2] =
_objectMotion[3] =
_objectMotion[4] = 0;
object_motions_[0] =
object_motions_[1] =
object_motions_[2] =
object_motions_[3] =
object_motions_[4] = 0;
break;
case 0x2c:
_collisions[0] = _collisions[1] = _collisions[2] =
_collisions[3] = _collisions[4] = _collisions[5] = 0x3f;
_collisions[6] = 0x7f;
_collisions[7] = 0x3f;
collisions_[0] = collisions_[1] = collisions_[2] =
collisions_[3] = collisions_[4] = collisions_[5] = 0x3f;
collisions_[6] = 0x7f;
collisions_[7] = 0x3f;
break;
}
}
@ -688,9 +688,9 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
// check for a PIA access
if((address&0x1280) == 0x280) {
if(isReadOperation(operation)) {
returnValue &= _mos6532.get_register(address);
returnValue &= mos6532_.get_register(address);
} else {
_mos6532.set_register(address, *value);
mos6532_.set_register(address, *value);
}
}
@ -699,7 +699,7 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
}
}
_mos6532.run_for_cycles(cycles_run_for / 3);
mos6532_.run_for_cycles(cycles_run_for / 3);
return cycles_run_for / 3;
}
@ -707,19 +707,19 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
void Machine::set_digital_input(Atari2600DigitalInput input, bool state)
{
switch (input) {
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;
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;
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;
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;
// TODO: latching
case Atari2600DigitalInputJoy1Fire: if(state) _tiaInputValue[0] &= ~0x80; else _tiaInputValue[0] |= 0x80; break;
case Atari2600DigitalInputJoy2Fire: if(state) _tiaInputValue[1] &= ~0x80; else _tiaInputValue[1] |= 0x80; break;
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;
default: break;
}
@ -728,11 +728,11 @@ void Machine::set_digital_input(Atari2600DigitalInput input, bool state)
void Machine::set_switch_is_enabled(Atari2600Switch input, bool state)
{
switch(input) {
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;
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;
}
}
@ -742,48 +742,36 @@ void Machine::configure_as_target(const StaticAnalyser::Target &target)
Storage::Cartridge::Cartridge::Segment segment = target.cartridges.front()->get_segments().front();
size_t length = segment.data.size();
_rom_size = 1024;
while(_rom_size < length && _rom_size < 32768) _rom_size <<= 1;
rom_size_ = 1024;
while(rom_size_ < length && rom_size_ < 32768) rom_size_ <<= 1;
delete[] _rom;
_rom = new uint8_t[_rom_size];
delete[] rom_;
rom_ = new uint8_t[rom_size_];
size_t offset = 0;
const size_t copy_step = std::min(_rom_size, length);
while(offset < _rom_size)
const size_t copy_step = std::min(rom_size_, length);
while(offset < rom_size_)
{
size_t copy_length = std::min(copy_step, _rom_size - offset);
memcpy(&_rom[offset], &segment.data[0], copy_length);
size_t copy_length = std::min(copy_step, rom_size_ - offset);
memcpy(&rom_[offset], &segment.data[0], copy_length);
offset += copy_length;
}
size_t romMask = _rom_size - 1;
_romPages[0] = _rom;
_romPages[1] = &_rom[1024 & romMask];
_romPages[2] = &_rom[2048 & romMask];
_romPages[3] = &_rom[3072 & romMask];
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];
}
#pragma mark - Audio
void Machine::update_audio()
{
unsigned int audio_cycles = _cycles_since_speaker_update / 114;
unsigned int audio_cycles = cycles_since_speaker_update_ / 114;
// static unsigned int total_cycles = 0;
// total_cycles += audio_cycles;
// static time_t logged_time = 0;
// time_t time_now = time(nullptr);
// if(time_now - logged_time > 0)
// {
// printf("[s] %ld : %d\n", time_now - logged_time, total_cycles);
// total_cycles = 0;
// logged_time = time_now;
// }
_speaker->run_for_cycles(audio_cycles);
_cycles_since_speaker_update %= 114;
speaker_->run_for_cycles(audio_cycles);
cycles_since_speaker_update_ %= 114;
}
void Machine::synchronise()

View File

@ -46,26 +46,26 @@ class Machine:
// to satisfy CRTMachine::Machine
virtual void setup_output(float aspect_ratio);
virtual void close_output();
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt() { return _crt; }
virtual std::shared_ptr<Outputs::Speaker> get_speaker() { return _speaker; }
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt() { return crt_; }
virtual std::shared_ptr<Outputs::Speaker> get_speaker() { return speaker_; }
virtual void run_for_cycles(int number_of_cycles) { CPU6502::Processor<Machine>::run_for_cycles(number_of_cycles); }
// TODO: different rate for PAL
private:
uint8_t *_rom, *_romPages[4];
size_t _rom_size;
uint8_t *rom_, *rom_pages_[4];
size_t rom_size_;
// the RIOT
PIA _mos6532;
PIA mos6532_;
// playfield registers
uint8_t _playfieldControl;
uint8_t _playfieldColour;
uint8_t _backgroundColour;
uint8_t _playfield[41];
uint8_t playfield_control_;
uint8_t playfield_colour_;
uint8_t background_colour_;
uint8_t playfield_[41];
// ... and derivatives
int _ballSize, _missileSize[2];
int ball_size_, missile_size_[2];
// delayed clock events
enum OutputState {
@ -87,12 +87,12 @@ class Machine:
int updates;
OutputState state;
uint8_t playfieldPixel;
uint8_t playfield_pixel;
int counter;
Event() : updates(0), playfieldPixel(0) {}
} _upcomingEvents[number_of_upcoming_events];
unsigned int _upcomingEventsPointer;
Event() : updates(0), playfield_pixel(0) {}
} upcoming_events_[number_of_upcoming_events];
unsigned int upcoming_events_pointer_;
// object counters
struct ObjectCounter {
@ -101,74 +101,73 @@ class Machine:
int broad_pixel; // for sprite objects, a count of cycles since the last counter reset; otherwise unused
ObjectCounter() : count(0), pixel(0), broad_pixel(0) {}
} _objectCounter[number_of_recorded_counters][5];
unsigned int _objectCounterPointer;
} object_counter_[number_of_recorded_counters][5];
unsigned int object_counter_pointer_;
// the latched playfield output
uint8_t _playfieldOutput, _nextPlayfieldOutput;
uint8_t playfield_output_, next_playfield_output_;
// player registers
uint8_t _playerColour[2];
uint8_t _playerReflectionMask[2];
uint8_t _playerGraphics[2][2];
uint8_t _playerGraphicsSelector[2];
bool _playerStart[2];
uint8_t player_colour_[2];
uint8_t player_reflection_mask_[2];
uint8_t player_graphics_[2][2];
uint8_t player_graphics_selector_[2];
// object flags
bool _hasSecondCopy[2];
bool _hasThirdCopy[2];
bool _hasFourthCopy[2];
uint8_t _objectMotion[5]; // the value stored to this counter's motion register
bool has_second_copy_[2];
bool has_third_copy_[2];
bool has_fourth_copy_[2];
uint8_t object_motions_[5]; // the value stored to this counter's motion register
// player + missile registers
uint8_t _playerAndMissileSize[2];
uint8_t player_and_missile_size_[2];
// missile registers
uint8_t _missileGraphicsEnable[2];
bool _missileGraphicsReset[2];
uint8_t missile_graphics_enable_[2];
bool missile_graphics_reset_[2];
// ball registers
uint8_t _ballGraphicsEnable[2];
uint8_t _ballGraphicsSelector;
uint8_t ball_graphics_enable_[2];
uint8_t ball_graphics_selector_;
// graphics output
unsigned int _horizontalTimer;
bool _vSyncEnabled, _vBlankEnabled;
unsigned int horizontal_timer_;
bool vsync_enabled_, vblank_enabled_;
// horizontal motion control
uint8_t _hMoveCounter;
uint8_t _hMoveFlags;
uint8_t hmove_counter_;
uint8_t hmove_flags_;
// joystick state
uint8_t _tiaInputValue[2];
uint8_t tia_input_value_[2];
// collisions
uint8_t _collisions[8];
uint8_t collisions_[8];
void output_pixels(unsigned int count);
uint8_t get_output_pixel();
void update_timers(int mask);
// outputs
std::shared_ptr<Outputs::CRT::CRT> _crt;
std::shared_ptr<Speaker> _speaker;
std::shared_ptr<Outputs::CRT::CRT> crt_;
std::shared_ptr<Speaker> speaker_;
// current mode
bool _is_pal_region;
bool is_pal_region_;
// speaker backlog accumlation counter
unsigned int _cycles_since_speaker_update;
unsigned int cycles_since_speaker_update_;
void update_audio();
// latched output state
unsigned int _lastOutputStateDuration;
OutputState _stateByExtendTime[2][57];
OutputState *_stateByTime;
OutputState _lastOutputState;
uint8_t *_outputBuffer;
unsigned int last_output_state_duration_;
OutputState state_by_extend_time_[2][57];
OutputState *state_by_time_;
OutputState last_output_state_;
uint8_t *output_buffer_;
// lookup table for collision reporting
uint8_t _reportedCollisions[64][8];
uint8_t reported_collisions_[64][8];
void setup_reported_collisions();
};