2017-01-28 21:19:08 +00:00
|
|
|
//
|
|
|
|
// TIA.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 28/01/2017.
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "TIA.hpp"
|
2017-01-29 02:46:40 +00:00
|
|
|
|
|
|
|
using namespace Atari2600;
|
2017-01-29 18:47:36 +00:00
|
|
|
namespace {
|
|
|
|
const int cycles_per_line = 228;
|
2017-02-06 23:29:00 +00:00
|
|
|
const int first_pixel_cycle = 68;
|
2017-01-30 03:16:23 +00:00
|
|
|
|
|
|
|
const int sync_flag = 0x1;
|
|
|
|
const int blank_flag = 0x2;
|
2017-01-31 02:38:58 +00:00
|
|
|
|
|
|
|
uint8_t reverse_table[256];
|
2017-01-29 18:47:36 +00:00
|
|
|
}
|
2017-01-29 02:46:40 +00:00
|
|
|
|
2017-01-29 18:47:36 +00:00
|
|
|
TIA::TIA() :
|
2017-01-29 20:43:57 +00:00
|
|
|
horizontal_counter_(0),
|
2017-02-06 23:29:00 +00:00
|
|
|
pixels_start_location_(0),
|
2017-01-31 02:38:58 +00:00
|
|
|
output_mode_(0),
|
2017-02-07 01:09:12 +00:00
|
|
|
pixel_target_(nullptr),
|
2017-01-31 02:38:58 +00:00
|
|
|
background_{0, 0},
|
|
|
|
background_half_mask_(0)
|
2017-01-29 02:46:40 +00:00
|
|
|
{
|
2017-01-29 19:19:26 +00:00
|
|
|
crt_.reset(new Outputs::CRT::CRT(cycles_per_line * 2 + 1, 1, Outputs::CRT::DisplayType::NTSC60, 1));
|
2017-01-29 02:46:40 +00:00
|
|
|
crt_->set_output_device(Outputs::CRT::Television);
|
2017-01-29 19:19:26 +00:00
|
|
|
set_output_mode(OutputMode::NTSC);
|
2017-01-31 02:38:58 +00:00
|
|
|
|
|
|
|
for(int c = 0; c < 256; c++)
|
|
|
|
{
|
|
|
|
reverse_table[c] = (uint8_t)(
|
|
|
|
((c & 0x01) << 7) | ((c & 0x02) << 5) | ((c & 0x04) << 3) | ((c & 0x08) << 1) |
|
|
|
|
((c & 0x10) >> 1) | ((c & 0x20) >> 3) | ((c & 0x40) >> 5) | ((c & 0x80) >> 7)
|
|
|
|
);
|
|
|
|
}
|
2017-02-06 23:29:00 +00:00
|
|
|
|
|
|
|
for(int c = 0; c < 64; c++)
|
|
|
|
{
|
|
|
|
bool has_playfield = c & (int)(CollisionType::Playfield);
|
|
|
|
bool has_ball = c & (int)(CollisionType::Ball);
|
|
|
|
bool has_player0 = c & (int)(CollisionType::Player0);
|
|
|
|
bool has_player1 = c & (int)(CollisionType::Player1);
|
|
|
|
bool has_missile0 = c & (int)(CollisionType::Missile0);
|
|
|
|
bool has_missile1 = c & (int)(CollisionType::Missile1);
|
|
|
|
|
2017-02-07 02:15:55 +00:00
|
|
|
uint8_t collision_registers[8];
|
|
|
|
collision_registers[0] = ((has_missile0 && has_player1) ? 0x80 : 0x00) | ((has_missile0 && has_player0) ? 0x40 : 0x00);
|
|
|
|
collision_registers[1] = ((has_missile1 && has_player0) ? 0x80 : 0x00) | ((has_missile1 && has_player1) ? 0x40 : 0x00);
|
|
|
|
collision_registers[2] = ((has_playfield && has_player0) ? 0x80 : 0x00) | ((has_ball && has_player0) ? 0x40 : 0x00);
|
|
|
|
collision_registers[3] = ((has_playfield && has_player1) ? 0x80 : 0x00) | ((has_ball && has_player1) ? 0x40 : 0x00);
|
|
|
|
collision_registers[4] = ((has_playfield && has_missile0) ? 0x80 : 0x00) | ((has_ball && has_missile0) ? 0x40 : 0x00);
|
|
|
|
collision_registers[5] = ((has_playfield && has_missile1) ? 0x80 : 0x00) | ((has_ball && has_missile1) ? 0x40 : 0x00);
|
|
|
|
collision_registers[6] = ((has_playfield && has_ball) ? 0x80 : 0x00);
|
|
|
|
collision_registers[7] = ((has_player0 && has_player1) ? 0x80 : 0x00) | ((has_missile0 && has_missile1) ? 0x40 : 0x00);
|
|
|
|
collision_flags_by_buffer_vaules_[c] =
|
|
|
|
(collision_registers[0] >> 6) |
|
|
|
|
(collision_registers[1] >> 4) |
|
|
|
|
(collision_registers[2] >> 2) |
|
|
|
|
(collision_registers[3] >> 0) |
|
|
|
|
(collision_registers[4] << 2) |
|
|
|
|
(collision_registers[5] << 4) |
|
|
|
|
(collision_registers[6] << 6) |
|
|
|
|
(collision_registers[7] << 8);
|
|
|
|
|
2017-02-06 23:42:58 +00:00
|
|
|
// all priority modes show the background if nothing else is present
|
2017-02-06 23:29:00 +00:00
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::Background;
|
|
|
|
|
2017-02-06 23:42:58 +00:00
|
|
|
// test 1 for standard priority: if there is a playfield or ball pixel, plot that colour
|
2017-02-06 23:29:00 +00:00
|
|
|
if(has_playfield || has_ball)
|
|
|
|
{
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] = (uint8_t)ColourIndex::PlayfieldBall;
|
|
|
|
}
|
2017-02-06 23:42:58 +00:00
|
|
|
|
|
|
|
// test 1 for score mode: if there is a ball pixel, plot that colour
|
2017-02-06 23:29:00 +00:00
|
|
|
if(has_ball)
|
|
|
|
{
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] = (uint8_t)ColourIndex::PlayfieldBall;
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:42:58 +00:00
|
|
|
// test 1 for on-top mode, test 2 for everbody else: if there is a player 1 or missile 1 pixel, plot that colour
|
2017-02-06 23:29:00 +00:00
|
|
|
if(has_player1 || has_missile1)
|
|
|
|
{
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::PlayerMissile1;
|
|
|
|
}
|
2017-02-06 23:42:58 +00:00
|
|
|
|
|
|
|
// in the right-hand side of score mode, the playfield has the same priority as player 1
|
2017-02-06 23:29:00 +00:00
|
|
|
if(has_playfield)
|
|
|
|
{
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] = (uint8_t)ColourIndex::PlayerMissile1;
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:42:58 +00:00
|
|
|
// next test for everybody: if there is a player 0 or missile 0 pixel, plot that colour instead
|
2017-02-06 23:29:00 +00:00
|
|
|
if(has_player0 || has_missile0)
|
|
|
|
{
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::Standard][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][c] =
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::PlayerMissile0;
|
|
|
|
}
|
2017-02-06 23:42:58 +00:00
|
|
|
|
|
|
|
// if this is the left-hand side of score mode, the playfield has the same priority as player 0
|
2017-02-06 23:29:00 +00:00
|
|
|
if(has_playfield)
|
|
|
|
{
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][c] = (uint8_t)ColourIndex::PlayerMissile0;
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:42:58 +00:00
|
|
|
// a final test for 'on top' priority mode: if the playfield or ball are visible, prefer that colour to all others
|
2017-02-06 23:29:00 +00:00
|
|
|
if(has_playfield || has_ball)
|
|
|
|
{
|
|
|
|
colour_mask_by_mode_collision_flags_[(int)ColourMode::OnTop][c] = (uint8_t)ColourIndex::PlayfieldBall;
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 19:19:26 +00:00
|
|
|
}
|
2017-01-29 02:46:40 +00:00
|
|
|
|
2017-01-29 19:19:26 +00:00
|
|
|
void TIA::set_output_mode(Atari2600::TIA::OutputMode output_mode)
|
|
|
|
{
|
2017-01-29 02:46:40 +00:00
|
|
|
// this is the NTSC phase offset function; see below for PAL
|
|
|
|
crt_->set_composite_sampling_function(
|
|
|
|
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
|
|
|
|
"{"
|
|
|
|
"uint c = texture(texID, coordinate).r;"
|
|
|
|
"uint y = c & 14u;"
|
|
|
|
"uint iPhase = (c >> 4);"
|
|
|
|
|
|
|
|
"float phaseOffset = 6.283185308 * float(iPhase) / 13.0 + 5.074880441076923;"
|
|
|
|
"return mix(float(y) / 14.0, step(1, iPhase) * cos(phase + phaseOffset), amplitude);"
|
|
|
|
"}");
|
2017-01-29 19:00:01 +00:00
|
|
|
/* speaker_->set_input_rate((float)(get_clock_rate() / 38.0));*/
|
2017-01-29 02:46:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 02:56:01 +00:00
|
|
|
TIA::~TIA()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-29 02:46:40 +00:00
|
|
|
/*void Machine::switch_region()
|
|
|
|
{
|
|
|
|
// the PAL 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;"
|
|
|
|
"uint y = c & 14u;"
|
|
|
|
"uint iPhase = (c >> 4);"
|
|
|
|
|
|
|
|
"uint direction = iPhase & 1u;"
|
|
|
|
"float phaseOffset = float(7u - direction) + (float(direction) - 0.5) * 2.0 * float(iPhase >> 1);"
|
|
|
|
"phaseOffset *= 6.283185308 / 12.0;"
|
|
|
|
"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, true);
|
|
|
|
|
|
|
|
is_pal_region_ = true;
|
|
|
|
speaker_->set_input_rate((float)(get_clock_rate() / 38.0));
|
|
|
|
set_clock_rate(PAL_clock_rate);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// 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
|
2017-01-29 02:56:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
void TIA::run_for_cycles(int number_of_cycles)
|
|
|
|
{
|
2017-01-29 18:47:36 +00:00
|
|
|
// if part way through a line, definitely perform a partial, at most up to the end of the line
|
|
|
|
if(horizontal_counter_)
|
|
|
|
{
|
|
|
|
int cycles = std::min(number_of_cycles, cycles_per_line - horizontal_counter_);
|
|
|
|
output_for_cycles(cycles);
|
|
|
|
number_of_cycles -= cycles;
|
|
|
|
}
|
|
|
|
|
|
|
|
// output full lines for as long as possible
|
2017-01-29 19:00:01 +00:00
|
|
|
while(number_of_cycles >= cycles_per_line)
|
2017-01-29 18:47:36 +00:00
|
|
|
{
|
|
|
|
output_line();
|
|
|
|
number_of_cycles -= cycles_per_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
// partly start a new line if necessary
|
|
|
|
if(number_of_cycles)
|
|
|
|
{
|
|
|
|
output_for_cycles(number_of_cycles);
|
|
|
|
}
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 03:16:23 +00:00
|
|
|
void TIA::set_sync(bool sync)
|
2017-01-29 02:56:01 +00:00
|
|
|
{
|
2017-01-30 12:19:19 +00:00
|
|
|
output_mode_ = (output_mode_ & ~sync_flag) | (sync ? sync_flag : 0);
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 03:16:23 +00:00
|
|
|
void TIA::set_blank(bool blank)
|
2017-01-29 02:56:01 +00:00
|
|
|
{
|
2017-01-30 12:19:19 +00:00
|
|
|
output_mode_ = (output_mode_ & ~blank_flag) | (blank ? blank_flag : 0);
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::reset_horizontal_counter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int TIA::get_cycles_until_horizontal_blank(unsigned int from_offset)
|
|
|
|
{
|
2017-02-08 03:14:45 +00:00
|
|
|
return (cycles_per_line - (horizontal_counter_ + (int)from_offset) % cycles_per_line) % cycles_per_line;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_background_colour(uint8_t colour)
|
|
|
|
{
|
2017-02-06 23:29:00 +00:00
|
|
|
colour_palette_[(int)ColourIndex::Background] = colour;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_playfield(uint16_t offset, uint8_t value)
|
|
|
|
{
|
2017-01-31 02:38:58 +00:00
|
|
|
switch(offset)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
background_[1] = (background_[1] & 0x0ffff) | ((uint32_t)reverse_table[value & 0xf0] << 16);
|
|
|
|
background_[0] = (background_[0] & 0xffff0) | (uint32_t)(value >> 4);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
background_[1] = (background_[1] & 0xf00ff) | ((uint32_t)value << 8);
|
|
|
|
background_[0] = (background_[0] & 0xff00f) | ((uint32_t)reverse_table[value] << 4);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
background_[1] = (background_[1] & 0xfff00) | reverse_table[value];
|
|
|
|
background_[0] = (background_[0] & 0x00fff) | ((uint32_t)value << 12);
|
|
|
|
break;
|
|
|
|
}
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_playfield_control_and_ball_size(uint8_t value)
|
|
|
|
{
|
2017-01-31 02:38:58 +00:00
|
|
|
background_half_mask_ = value & 1;
|
2017-02-06 23:29:00 +00:00
|
|
|
switch(value & 6)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
playfield_priority_ = PlayfieldPriority::Standard;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
playfield_priority_ = PlayfieldPriority::Score;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
playfield_priority_ = PlayfieldPriority::OnTop;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_playfield_ball_colour(uint8_t colour)
|
|
|
|
{
|
2017-02-06 23:29:00 +00:00
|
|
|
colour_palette_[(int)ColourIndex::PlayfieldBall] = colour;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_player_number_and_size(int player, uint8_t value)
|
|
|
|
{
|
2017-01-31 03:42:27 +00:00
|
|
|
switch(value & 7)
|
|
|
|
{
|
|
|
|
case 0: case 1: case 2: case 3: case 4:
|
|
|
|
player_[player].size = 0;
|
|
|
|
player_[player].copy_flags = value & 7;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
player_[player].size = 1;
|
|
|
|
player_[player].copy_flags = 0;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
player_[player].size = 0;
|
|
|
|
player_[player].copy_flags = 7;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
player_[player].size = 2;
|
|
|
|
player_[player].copy_flags = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
missile_[player].size = (value >> 4)&3;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_player_graphic(int player, uint8_t value)
|
|
|
|
{
|
2017-01-31 03:42:27 +00:00
|
|
|
player_[player].graphic = value;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_player_reflected(int player, bool reflected)
|
|
|
|
{
|
2017-01-31 03:42:27 +00:00
|
|
|
player_[player].reverse_mask = reflected ? 7 : 0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_player_delay(int player, bool delay)
|
|
|
|
{
|
2017-01-31 03:42:27 +00:00
|
|
|
// TODO
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_player_position(int player)
|
|
|
|
{
|
2017-02-10 12:23:43 +00:00
|
|
|
position_[(int)MotionIndex::Player0 + player] = 0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_player_motion(int player, uint8_t motion)
|
|
|
|
{
|
2017-02-10 12:23:43 +00:00
|
|
|
motion_[(int)MotionIndex::Player0 + player] = motion >> 4;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_player_missile_colour(int player, uint8_t colour)
|
|
|
|
{
|
2017-02-06 23:29:00 +00:00
|
|
|
colour_palette_[(int)ColourIndex::PlayerMissile0 + player] = colour;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_missile_enable(int missile, bool enabled)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_missile_position(int missile)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_missile_position_to_player(int missile)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_missile_motion(int missile, uint8_t motion)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_ball_enable(bool enabled)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_ball_delay(bool delay)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_ball_position()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_ball_motion(uint8_t motion)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::move()
|
|
|
|
{
|
2017-02-09 23:37:19 +00:00
|
|
|
horizontal_blank_extend_ = true;
|
2017-02-10 12:23:43 +00:00
|
|
|
is_moving_[0] = is_moving_[1] = is_moving_[2] = is_moving_[3] = is_moving_[4] = true;
|
2017-02-11 17:59:13 +00:00
|
|
|
horizontal_move_start_time_ = horizontal_counter_;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::clear_motion()
|
|
|
|
{
|
2017-02-10 12:23:43 +00:00
|
|
|
motion_[0] = motion_[1] = motion_[2] = motion_[3] = motion_[4] = 0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TIA::get_collision_flags(int offset)
|
|
|
|
{
|
2017-02-06 23:29:00 +00:00
|
|
|
return (uint8_t)((collision_flags_ >> (offset << 1)) << 6) & 0xc0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::clear_collision_flags()
|
|
|
|
{
|
2017-02-06 23:29:00 +00:00
|
|
|
collision_flags_ = 0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
2017-01-29 18:47:36 +00:00
|
|
|
|
|
|
|
void TIA::output_for_cycles(int number_of_cycles)
|
|
|
|
{
|
2017-01-29 20:43:57 +00:00
|
|
|
/*
|
|
|
|
Line timing is oriented around 0 being the start of the right-hand side vertical blank;
|
|
|
|
a wsync synchronises the CPU to horizontal_counter_ = 0. All timing below is in terms of the
|
|
|
|
NTSC colour clock.
|
|
|
|
|
|
|
|
Therefore, each line is composed of:
|
2017-02-01 01:30:32 +00:00
|
|
|
|
2017-01-29 20:43:57 +00:00
|
|
|
16 cycles: blank ; -> 16
|
|
|
|
16 cycles: sync ; -> 32
|
|
|
|
16 cycles: colour burst ; -> 48
|
|
|
|
20 cycles: blank ; -> 68
|
|
|
|
8 cycles: blank or pixels, depending on whether the blank extend bit is set
|
|
|
|
152 cycles: pixels
|
|
|
|
*/
|
2017-01-31 03:42:27 +00:00
|
|
|
int output_cursor = horizontal_counter_;
|
2017-01-29 20:43:57 +00:00
|
|
|
horizontal_counter_ += number_of_cycles;
|
2017-01-30 03:16:23 +00:00
|
|
|
|
2017-02-07 02:59:28 +00:00
|
|
|
if(!output_cursor)
|
|
|
|
{
|
|
|
|
memset(collision_buffer_, 0, sizeof(collision_buffer_));
|
2017-02-09 23:37:19 +00:00
|
|
|
horizontal_blank_extend_ = false;
|
2017-02-07 02:59:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 23:37:19 +00:00
|
|
|
// accumulate an OR'd version of the output into the collision buffer
|
2017-02-08 12:30:32 +00:00
|
|
|
draw_playfield(output_cursor, horizontal_counter_);
|
2017-02-10 12:23:43 +00:00
|
|
|
draw_player(player_[0], CollisionType::Player0, (int)MotionIndex::Player0, output_cursor, horizontal_counter_);
|
|
|
|
draw_player(player_[1], CollisionType::Player1, (int)MotionIndex::Player1, output_cursor, horizontal_counter_);
|
2017-02-08 12:30:32 +00:00
|
|
|
|
|
|
|
// convert to television signals
|
|
|
|
|
2017-01-30 12:19:19 +00:00
|
|
|
#define Period(function, target) \
|
2017-01-31 03:42:27 +00:00
|
|
|
if(output_cursor < target) \
|
2017-01-30 12:19:19 +00:00
|
|
|
{ \
|
|
|
|
if(horizontal_counter_ <= target) \
|
|
|
|
{ \
|
2017-01-31 03:42:27 +00:00
|
|
|
crt_->function((unsigned int)((horizontal_counter_ - output_cursor) * 2)); \
|
2017-02-01 01:30:32 +00:00
|
|
|
horizontal_counter_ %= cycles_per_line; \
|
2017-01-30 12:19:19 +00:00
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
2017-01-31 03:42:27 +00:00
|
|
|
crt_->function((unsigned int)((target - output_cursor) * 2)); \
|
|
|
|
output_cursor = target; \
|
2017-01-30 12:19:19 +00:00
|
|
|
} \
|
2017-01-29 20:43:57 +00:00
|
|
|
}
|
2017-01-30 12:19:19 +00:00
|
|
|
|
|
|
|
switch(output_mode_)
|
2017-01-29 20:43:57 +00:00
|
|
|
{
|
2017-01-30 12:19:19 +00:00
|
|
|
default:
|
|
|
|
Period(output_blank, 16)
|
|
|
|
Period(output_sync, 32)
|
|
|
|
Period(output_default_colour_burst, 48)
|
|
|
|
Period(output_blank, 68)
|
|
|
|
break;
|
|
|
|
case sync_flag:
|
|
|
|
case sync_flag | blank_flag:
|
|
|
|
Period(output_sync, 16)
|
|
|
|
Period(output_blank, 32)
|
|
|
|
Period(output_default_colour_burst, 48)
|
|
|
|
Period(output_sync, 228)
|
|
|
|
break;
|
2017-01-29 20:43:57 +00:00
|
|
|
}
|
2017-01-30 12:19:19 +00:00
|
|
|
|
2017-02-07 02:59:28 +00:00
|
|
|
#undef Period
|
|
|
|
|
2017-01-30 12:19:19 +00:00
|
|
|
if(output_mode_ & blank_flag)
|
2017-01-29 20:43:57 +00:00
|
|
|
{
|
2017-02-07 01:09:12 +00:00
|
|
|
if(pixel_target_)
|
|
|
|
{
|
|
|
|
output_pixels(pixels_start_location_, output_cursor);
|
|
|
|
crt_->output_data((unsigned int)(output_cursor - pixels_start_location_) * 2, 2);
|
|
|
|
pixel_target_ = nullptr;
|
|
|
|
pixels_start_location_ = 0;
|
|
|
|
}
|
2017-01-31 03:42:27 +00:00
|
|
|
int duration = std::min(228, horizontal_counter_) - output_cursor;
|
2017-01-30 12:19:19 +00:00
|
|
|
crt_->output_blank((unsigned int)(duration * 2));
|
2017-01-29 20:43:57 +00:00
|
|
|
}
|
2017-01-30 12:19:19 +00:00
|
|
|
else
|
2017-01-29 20:43:57 +00:00
|
|
|
{
|
2017-02-07 01:09:12 +00:00
|
|
|
if(!pixels_start_location_)
|
|
|
|
{
|
|
|
|
pixels_start_location_ = output_cursor;
|
|
|
|
pixel_target_ = crt_->allocate_write_area(160);
|
|
|
|
}
|
2017-02-06 23:29:00 +00:00
|
|
|
|
2017-02-07 01:09:12 +00:00
|
|
|
// convert that into pixels
|
|
|
|
if(pixel_target_) output_pixels(output_cursor, horizontal_counter_);
|
|
|
|
|
2017-02-06 23:29:00 +00:00
|
|
|
// accumulate collision flags
|
|
|
|
while(output_cursor < horizontal_counter_)
|
2017-01-29 20:43:57 +00:00
|
|
|
{
|
2017-02-06 23:42:58 +00:00
|
|
|
collision_flags_ |= collision_flags_by_buffer_vaules_[collision_buffer_[output_cursor - first_pixel_cycle]];
|
2017-02-06 23:29:00 +00:00
|
|
|
output_cursor++;
|
2017-01-29 20:43:57 +00:00
|
|
|
}
|
2017-02-06 23:29:00 +00:00
|
|
|
|
2017-01-29 20:43:57 +00:00
|
|
|
if(horizontal_counter_ == cycles_per_line)
|
|
|
|
{
|
2017-02-07 01:09:12 +00:00
|
|
|
crt_->output_data((unsigned int)(output_cursor - pixels_start_location_) * 2, 2);
|
|
|
|
pixel_target_ = nullptr;
|
|
|
|
pixels_start_location_ = 0;
|
2017-01-29 20:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-30 12:19:19 +00:00
|
|
|
|
2017-01-29 20:43:57 +00:00
|
|
|
horizontal_counter_ %= cycles_per_line;
|
2017-01-29 18:47:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 23:29:00 +00:00
|
|
|
void TIA::output_pixels(int start, int end)
|
|
|
|
{
|
2017-02-07 01:09:12 +00:00
|
|
|
int target_position = start - pixels_start_location_;
|
2017-02-06 23:29:00 +00:00
|
|
|
|
2017-02-09 23:37:19 +00:00
|
|
|
if(start < first_pixel_cycle+8 && horizontal_blank_extend_)
|
|
|
|
{
|
|
|
|
while(start < end && start < first_pixel_cycle+8)
|
|
|
|
{
|
|
|
|
pixel_target_[target_position] = 0;
|
|
|
|
start++;
|
|
|
|
target_position++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:09:12 +00:00
|
|
|
if(playfield_priority_ == PlayfieldPriority::Score)
|
2017-02-06 23:29:00 +00:00
|
|
|
{
|
2017-02-07 01:09:12 +00:00
|
|
|
while(start < end && start < first_pixel_cycle + 80)
|
2017-02-06 23:29:00 +00:00
|
|
|
{
|
2017-02-07 01:09:12 +00:00
|
|
|
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
|
|
|
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreLeft][buffer_value]];
|
|
|
|
start++;
|
|
|
|
target_position++;
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
2017-02-07 01:09:12 +00:00
|
|
|
while(start < end)
|
2017-02-06 23:29:00 +00:00
|
|
|
{
|
2017-02-07 01:09:12 +00:00
|
|
|
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
|
|
|
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[(int)ColourMode::ScoreRight][buffer_value]];
|
|
|
|
start++;
|
|
|
|
target_position++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int table_index = (int)((playfield_priority_ == PlayfieldPriority::Standard) ? ColourMode::Standard : ColourMode::OnTop);
|
|
|
|
while(start < end)
|
|
|
|
{
|
|
|
|
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
|
|
|
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[table_index][buffer_value]];
|
|
|
|
start++;
|
|
|
|
target_position++;
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:47:36 +00:00
|
|
|
void TIA::output_line()
|
|
|
|
{
|
2017-01-30 03:16:23 +00:00
|
|
|
switch(output_mode_)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
// TODO: optimise special case
|
|
|
|
output_for_cycles(cycles_per_line);
|
|
|
|
break;
|
|
|
|
case sync_flag:
|
|
|
|
case sync_flag | blank_flag:
|
|
|
|
crt_->output_sync(32);
|
|
|
|
crt_->output_blank(32);
|
|
|
|
crt_->output_sync(392);
|
2017-02-09 23:37:19 +00:00
|
|
|
horizontal_blank_extend_ = false;
|
2017-01-30 03:16:23 +00:00
|
|
|
break;
|
|
|
|
case blank_flag:
|
|
|
|
crt_->output_blank(32);
|
|
|
|
crt_->output_sync(32);
|
|
|
|
crt_->output_default_colour_burst(32);
|
|
|
|
crt_->output_blank(360);
|
2017-02-09 23:37:19 +00:00
|
|
|
horizontal_blank_extend_ = false;
|
2017-01-30 03:16:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-01-29 18:47:36 +00:00
|
|
|
}
|
2017-02-05 22:51:56 +00:00
|
|
|
|
2017-02-08 12:30:32 +00:00
|
|
|
#pragma mark - Playfield output
|
2017-02-05 22:51:56 +00:00
|
|
|
|
2017-02-06 23:29:00 +00:00
|
|
|
void TIA::draw_playfield(int start, int end)
|
2017-02-05 22:51:56 +00:00
|
|
|
{
|
2017-02-08 12:30:32 +00:00
|
|
|
// don't do anything if this window ends too early
|
|
|
|
if(end < first_pixel_cycle - 4) return;
|
|
|
|
|
|
|
|
// look at what needs to be output four cycles into the future, to model playfield output latency
|
|
|
|
start += 4;
|
|
|
|
end += 4;
|
|
|
|
|
|
|
|
// clip to drawable bounds
|
|
|
|
start = std::max(start, first_pixel_cycle);
|
|
|
|
end = std::min(end, 228);
|
|
|
|
|
|
|
|
// proceed along four-pixel boundaries, plotting four pixels at a time
|
|
|
|
int aligned_position = (start + 3)&~3;
|
|
|
|
while(aligned_position < end)
|
2017-02-05 22:51:56 +00:00
|
|
|
{
|
2017-02-08 12:30:32 +00:00
|
|
|
int offset = (aligned_position - first_pixel_cycle) >> 2;
|
|
|
|
uint32_t value = ((background_[(offset/20)&background_half_mask_] >> (offset%20))&1) * 0x01010101;
|
|
|
|
*(uint32_t *)&collision_buffer_[aligned_position - first_pixel_cycle] |= value;
|
|
|
|
aligned_position += 4;
|
2017-02-05 22:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 01:53:42 +00:00
|
|
|
|
|
|
|
#pragma mark - Player output
|
|
|
|
|
2017-02-10 12:23:43 +00:00
|
|
|
void TIA::draw_player(Player &player, CollisionType collision_identity, const int position_identity, int start, int end)
|
2017-02-10 01:53:42 +00:00
|
|
|
{
|
|
|
|
// don't do anything if this window ends too early
|
2017-02-10 12:23:43 +00:00
|
|
|
int first_pixel = first_pixel_cycle + (horizontal_blank_extend_ ? 8 : 0);
|
|
|
|
if(end < first_pixel) return;
|
|
|
|
if(start < first_pixel) start = first_pixel;
|
2017-02-10 01:53:42 +00:00
|
|
|
|
2017-02-10 12:23:43 +00:00
|
|
|
uint8_t &position = position_[position_identity];
|
2017-02-11 17:59:13 +00:00
|
|
|
uint8_t &motion = motion_[position_identity];
|
|
|
|
bool &is_moving = is_moving_[position_identity];
|
|
|
|
// while(start < end)
|
|
|
|
// {
|
|
|
|
int length = end - start;
|
|
|
|
|
|
|
|
// quick hack!
|
|
|
|
if(is_moving)
|
|
|
|
{
|
|
|
|
position += (motion ^ 8);
|
|
|
|
is_moving_[position_identity] = false;
|
|
|
|
position = (position + 160)%160;
|
|
|
|
}
|
2017-02-10 12:23:43 +00:00
|
|
|
|
2017-02-11 17:59:13 +00:00
|
|
|
// check for initial trigger; player.position is guaranteed to be less than 160 so this is easy
|
|
|
|
if(player.graphic && position + length >= 160)
|
2017-02-10 01:53:42 +00:00
|
|
|
{
|
2017-02-11 17:59:13 +00:00
|
|
|
int trigger_position = 160 - position + 5 + start - first_pixel_cycle;
|
|
|
|
|
|
|
|
int terminus = std::min(160, trigger_position+8);
|
|
|
|
while(trigger_position < terminus)
|
|
|
|
{
|
|
|
|
collision_buffer_[trigger_position] |= (uint8_t)collision_identity;
|
|
|
|
trigger_position++;
|
|
|
|
}
|
2017-02-10 01:53:42 +00:00
|
|
|
}
|
2017-02-11 17:59:13 +00:00
|
|
|
// }
|
2017-02-10 01:53:42 +00:00
|
|
|
|
|
|
|
// update position counter
|
2017-02-10 12:23:43 +00:00
|
|
|
position = (position + end - start) % 160;
|
2017-02-10 01:53:42 +00:00
|
|
|
}
|