2017-01-28 21:19:08 +00:00
|
|
|
//
|
|
|
|
// TIA.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 28/01/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-01-28 21:19:08 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "TIA.hpp"
|
2017-12-19 02:47:30 +00:00
|
|
|
|
2017-02-22 03:04:27 +00:00
|
|
|
#include <cassert>
|
2017-12-19 02:47:30 +00:00
|
|
|
#include <cstring>
|
2017-01-29 02:46:40 +00:00
|
|
|
|
|
|
|
using namespace Atari2600;
|
2017-01-29 18:47:36 +00:00
|
|
|
namespace {
|
2019-12-22 05:22:17 +00:00
|
|
|
constexpr int cycles_per_line = 228;
|
|
|
|
constexpr int first_pixel_cycle = 68;
|
2017-01-30 03:16:23 +00:00
|
|
|
|
2019-12-22 05:22:17 +00:00
|
|
|
constexpr int sync_flag = 0x1;
|
|
|
|
constexpr 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
|
|
|
|
2018-11-30 02:26:05 +00:00
|
|
|
TIA::TIA():
|
2023-05-12 18:14:45 +00:00
|
|
|
crt_(cycles_per_line * 2 - 1, 1, Outputs::Display::Type::NTSC60, Outputs::Display::InputDataType::Luminance8Phase8) {
|
2018-11-30 02:26:05 +00:00
|
|
|
|
|
|
|
set_output_mode(OutputMode::NTSC);
|
2017-01-31 02:38:58 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
for(int c = 0; c < 256; c++) {
|
2020-05-10 03:00:39 +00:00
|
|
|
reverse_table[c] = uint8_t(
|
2017-01-31 02:38:58 +00:00
|
|
|
((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
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
for(int c = 0; c < 64; c++) {
|
2020-05-10 03:00:39 +00:00
|
|
|
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-06 23:29:00 +00:00
|
|
|
|
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
|
2020-05-10 03:00:39 +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:29:00 +00:00
|
|
|
|
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-03-24 01:59:16 +00:00
|
|
|
if(has_playfield || has_ball) {
|
2020-05-10 03:00:39 +00:00
|
|
|
colour_mask_by_mode_collision_flags_[int(ColourMode::Standard)][c] = uint8_t(ColourIndex::PlayfieldBall);
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
2017-02-06 23:42:58 +00:00
|
|
|
|
|
|
|
// test 1 for score mode: if there is a ball pixel, plot that colour
|
2017-03-24 01:59:16 +00:00
|
|
|
if(has_ball) {
|
2020-05-10 03:00:39 +00:00
|
|
|
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:29:00 +00:00
|
|
|
}
|
|
|
|
|
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-03-24 01:59:16 +00:00
|
|
|
if(has_player1 || has_missile1) {
|
2020-05-10 03:00:39 +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::PlayerMissile1);
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
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-03-24 01:59:16 +00:00
|
|
|
if(has_playfield) {
|
2020-05-10 03:00:39 +00:00
|
|
|
colour_mask_by_mode_collision_flags_[int(ColourMode::ScoreRight)][c] = uint8_t(ColourIndex::PlayerMissile1);
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
|
|
|
|
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-03-24 01:59:16 +00:00
|
|
|
if(has_player0 || has_missile0) {
|
2020-05-10 03:00:39 +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::PlayerMissile0);
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
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-03-24 01:59:16 +00:00
|
|
|
if(has_playfield) {
|
2020-05-10 03:00:39 +00:00
|
|
|
colour_mask_by_mode_collision_flags_[int(ColourMode::ScoreLeft)][c] = uint8_t(ColourIndex::PlayerMissile0);
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
|
|
|
|
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-03-24 01:59:16 +00:00
|
|
|
if(has_playfield || has_ball) {
|
2020-05-10 03:00:39 +00:00
|
|
|
colour_mask_by_mode_collision_flags_[int(ColourMode::OnTop)][c] = uint8_t(ColourIndex::PlayfieldBall);
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-29 19:19:26 +00:00
|
|
|
}
|
2017-01-29 02:46:40 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_output_mode(Atari2600::TIA::OutputMode output_mode) {
|
2018-11-04 01:54:25 +00:00
|
|
|
Outputs::Display::Type display_type;
|
2018-11-30 02:26:05 +00:00
|
|
|
tv_standard_ = output_mode;
|
2017-02-20 02:20:37 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
if(output_mode == OutputMode::NTSC) {
|
2018-11-04 01:54:25 +00:00
|
|
|
display_type = Outputs::Display::Type::NTSC60;
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2018-11-04 01:54:25 +00:00
|
|
|
display_type = Outputs::Display::Type::PAL50;
|
2017-02-20 02:20:37 +00:00
|
|
|
}
|
2018-11-30 02:26:05 +00:00
|
|
|
crt_.set_display_type(Outputs::Display::DisplayType::CompositeColour);
|
2018-03-30 22:24:18 +00:00
|
|
|
|
2017-03-15 01:33:38 +00:00
|
|
|
// line number of cycles in a line of video is one less than twice the number of clock cycles per line; the Atari
|
|
|
|
// outputs 228 colour cycles of material per line when an NTSC line 227.5. Since all clock numbers will be doubled
|
|
|
|
// later, cycles_per_line * 2 - 1 is therefore the real length of an NTSC line, even though we're going to supply
|
|
|
|
// cycles_per_line * 2 cycles of information from one sync edge to the next
|
2018-11-30 02:26:05 +00:00
|
|
|
crt_.set_new_display_type(cycles_per_line * 2 - 1, display_type);
|
|
|
|
|
|
|
|
// Update the luminance/phase mappings of the current palette.
|
|
|
|
for(size_t c = 0; c < colour_palette_.size(); ++c) {
|
|
|
|
set_colour_palette_entry(c, colour_palette_[c].original);
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 02:20:37 +00:00
|
|
|
|
2018-11-30 02:26:05 +00:00
|
|
|
void TIA::set_crt_delegate(Outputs::CRT::Delegate *delegate) {
|
|
|
|
crt_.set_delegate(delegate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_scan_target(Outputs::Display::ScanTarget *scan_target) {
|
|
|
|
crt_.set_scan_target(scan_target);
|
2017-01-29 02:46:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 03:28:25 +00:00
|
|
|
Outputs::Display::ScanStatus TIA::get_scaled_scan_status() const {
|
|
|
|
return crt_.get_scaled_scan_status() / 2.0f;
|
2020-01-21 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void TIA::run_for(const Cycles cycles) {
|
2019-10-30 02:36:29 +00:00
|
|
|
int number_of_cycles = int(cycles.as_integral());
|
2017-07-25 01:48:34 +00:00
|
|
|
|
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
|
2017-03-24 01:59:16 +00:00
|
|
|
if(horizontal_counter_) {
|
2017-07-25 01:48:34 +00:00
|
|
|
int output_cycles = std::min(number_of_cycles, cycles_per_line - horizontal_counter_);
|
|
|
|
output_for_cycles(output_cycles);
|
|
|
|
number_of_cycles -= output_cycles;
|
2017-01-29 18:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// output full lines for as long as possible
|
2017-03-24 01:59:16 +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
|
2017-03-24 01:59:16 +00:00
|
|
|
if(number_of_cycles) {
|
2017-01-29 18:47:36 +00:00
|
|
|
output_for_cycles(number_of_cycles);
|
|
|
|
}
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_sync(bool sync) {
|
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-03-24 01:59:16 +00:00
|
|
|
void TIA::set_blank(bool blank) {
|
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
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::reset_horizontal_counter() {
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
int TIA::get_cycles_until_horizontal_blank(const Cycles from_offset) {
|
2019-10-30 02:36:29 +00:00
|
|
|
return (cycles_per_line - (horizontal_counter_ + from_offset.as_integral()) % cycles_per_line) % cycles_per_line;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_background_colour(uint8_t colour) {
|
2018-11-30 02:26:05 +00:00
|
|
|
set_colour_palette_entry(size_t(ColourIndex::Background), colour);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TIA::set_colour_palette_entry(size_t index, uint8_t colour) {
|
|
|
|
const uint8_t luminance = ((colour & 14) * 255) / 14;
|
|
|
|
|
|
|
|
uint8_t phase = colour >> 4;
|
|
|
|
|
|
|
|
if(tv_standard_ == OutputMode::NTSC) {
|
|
|
|
if(!phase) phase = 255;
|
|
|
|
else {
|
|
|
|
phase = -(phase * 127) / 13;
|
|
|
|
phase -= 102;
|
|
|
|
phase &= 127;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(phase < 2 || phase > 13) {
|
|
|
|
phase = 255;
|
|
|
|
} else {
|
|
|
|
const auto direction = phase & 1;
|
|
|
|
|
|
|
|
phase >>= 1;
|
2018-11-30 03:12:20 +00:00
|
|
|
if(direction) phase ^= 0xf;
|
|
|
|
phase = (phase + 6 + direction) & 0xf;
|
2018-11-30 02:26:05 +00:00
|
|
|
|
2018-11-30 03:12:20 +00:00
|
|
|
phase = (phase * 127) / 12;
|
2018-11-30 02:26:05 +00:00
|
|
|
phase &= 127;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
colour_palette_[index].original = colour;
|
|
|
|
uint8_t *target = reinterpret_cast<uint8_t *>(&colour_palette_[index].luminance_phase);
|
|
|
|
target[0] = luminance;
|
|
|
|
target[1] = phase;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_playfield(uint16_t offset, uint8_t value) {
|
2017-02-22 03:26:20 +00:00
|
|
|
assert(offset >= 0 && offset < 3);
|
2017-03-24 01:59:16 +00:00
|
|
|
switch(offset) {
|
2017-01-31 02:38:58 +00:00
|
|
|
case 0:
|
2020-05-10 03:00:39 +00:00
|
|
|
background_[1] = (background_[1] & 0x0ffff) | (uint32_t(reverse_table[value & 0xf0]) << 16);
|
|
|
|
background_[0] = (background_[0] & 0xffff0) | uint32_t(value >> 4);
|
2017-01-31 02:38:58 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2020-05-10 03:00:39 +00:00
|
|
|
background_[1] = (background_[1] & 0xf00ff) | (uint32_t(value) << 8);
|
|
|
|
background_[0] = (background_[0] & 0xff00f) | (uint32_t(reverse_table[value]) << 4);
|
2017-01-31 02:38:58 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
background_[1] = (background_[1] & 0xfff00) | reverse_table[value];
|
2020-05-10 03:00:39 +00:00
|
|
|
background_[0] = (background_[0] & 0x00fff) | (uint32_t(value) << 12);
|
2017-01-31 02:38:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +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-03-24 01:59:16 +00:00
|
|
|
switch(value & 6) {
|
2017-02-06 23:29:00 +00:00
|
|
|
case 0:
|
|
|
|
playfield_priority_ = PlayfieldPriority::Standard;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
playfield_priority_ = PlayfieldPriority::Score;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
playfield_priority_ = PlayfieldPriority::OnTop;
|
|
|
|
break;
|
|
|
|
}
|
2017-02-19 13:02:54 +00:00
|
|
|
|
|
|
|
ball_.size = 1 << ((value >> 4)&3);
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_playfield_ball_colour(uint8_t colour) {
|
2018-11-30 02:26:05 +00:00
|
|
|
set_colour_palette_entry(size_t(ColourIndex::PlayfieldBall), colour);
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_player_number_and_size(int player, uint8_t value) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(player >= 0 && player < 2);
|
2017-02-21 03:22:39 +00:00
|
|
|
int size = 0;
|
2017-03-24 01:59:16 +00:00
|
|
|
switch(value & 7) {
|
2017-01-31 03:42:27 +00:00
|
|
|
case 0: case 1: case 2: case 3: case 4:
|
|
|
|
player_[player].copy_flags = value & 7;
|
|
|
|
break;
|
|
|
|
case 5:
|
2017-02-21 03:22:39 +00:00
|
|
|
size = 1;
|
2017-01-31 03:42:27 +00:00
|
|
|
player_[player].copy_flags = 0;
|
|
|
|
break;
|
|
|
|
case 6:
|
2017-02-12 02:01:58 +00:00
|
|
|
player_[player].copy_flags = 6;
|
2017-01-31 03:42:27 +00:00
|
|
|
break;
|
|
|
|
case 7:
|
2017-02-21 03:22:39 +00:00
|
|
|
size = 2;
|
2017-01-31 03:42:27 +00:00
|
|
|
player_[player].copy_flags = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-21 03:22:39 +00:00
|
|
|
missile_[player].size = 1 << ((value >> 4)&3);
|
|
|
|
missile_[player].copy_flags = player_[player].copy_flags;
|
|
|
|
player_[player].adder = 4 >> size;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_player_graphic(int player, uint8_t value) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(player >= 0 && player < 2);
|
2017-02-12 23:16:50 +00:00
|
|
|
player_[player].graphic[1] = value;
|
2017-02-12 22:35:09 +00:00
|
|
|
player_[player^1].graphic[0] = player_[player^1].graphic[1];
|
2017-02-19 13:02:54 +00:00
|
|
|
if(player) ball_.enabled[0] = ball_.enabled[1];
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_player_reflected(int player, bool reflected) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(player >= 0 && player < 2);
|
2017-01-31 03:42:27 +00:00
|
|
|
player_[player].reverse_mask = reflected ? 7 : 0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_player_delay(int player, bool delay) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(player >= 0 && player < 2);
|
2017-02-17 01:52:01 +00:00
|
|
|
player_[player].graphic_index = delay ? 0 : 1;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_player_position(int player) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(player >= 0 && player < 2);
|
2017-02-19 12:28:24 +00:00
|
|
|
// players have an extra clock of delay before output and don't display upon reset;
|
|
|
|
// both aims are achieved by setting to -1 because: (i) it causes the clock to be
|
|
|
|
// one behind its real hardware value, creating the extra delay; and (ii) the player
|
|
|
|
// code is written to start a draw upon wraparound from 159 to 0, so -1 is the
|
|
|
|
// correct option rather than 159.
|
2017-02-21 12:37:20 +00:00
|
|
|
player_[player].position = -1;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_player_motion(int player, uint8_t motion) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(player >= 0 && player < 2);
|
2017-02-21 12:37:20 +00:00
|
|
|
player_[player].motion = (motion >> 4)&0xf;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_player_missile_colour(int player, uint8_t colour) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(player >= 0 && player < 2);
|
2018-11-30 02:26:05 +00:00
|
|
|
set_colour_palette_entry(size_t(ColourIndex::PlayerMissile0) + size_t(player), colour);
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_missile_enable(int missile, bool enabled) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(missile >= 0 && missile < 2);
|
2017-02-21 03:22:39 +00:00
|
|
|
missile_[missile].enabled = enabled;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_missile_position(int missile) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(missile >= 0 && missile < 2);
|
2017-02-21 12:37:20 +00:00
|
|
|
missile_[missile].position = 0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_missile_position_to_player(int missile, bool lock) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(missile >= 0 && missile < 2);
|
2017-02-22 01:45:20 +00:00
|
|
|
missile_[missile].locked_to_player = lock;
|
2017-02-26 20:12:31 +00:00
|
|
|
player_[missile].latched_pixel4_time = -1;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_missile_motion(int missile, uint8_t motion) {
|
2017-02-22 03:04:27 +00:00
|
|
|
assert(missile >= 0 && missile < 2);
|
2017-02-21 12:37:20 +00:00
|
|
|
missile_[missile].motion = (motion >> 4)&0xf;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_ball_enable(bool enabled) {
|
2017-02-19 13:02:54 +00:00
|
|
|
ball_.enabled[1] = enabled;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_ball_delay(bool delay) {
|
2017-02-19 13:02:54 +00:00
|
|
|
ball_.enabled_index = delay ? 0 : 1;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_ball_position() {
|
2017-02-21 12:37:20 +00:00
|
|
|
ball_.position = 0;
|
2017-02-19 13:02:54 +00:00
|
|
|
|
|
|
|
// setting the ball position also triggers a draw
|
2017-02-26 18:39:25 +00:00
|
|
|
ball_.reset_pixels(0);
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::set_ball_motion(uint8_t motion) {
|
2017-02-21 12:37:20 +00:00
|
|
|
ball_.motion = (motion >> 4) & 0xf;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::move() {
|
2017-02-09 23:37:19 +00:00
|
|
|
horizontal_blank_extend_ = true;
|
2017-02-21 12:37:20 +00:00
|
|
|
player_[0].is_moving = player_[1].is_moving = missile_[0].is_moving = missile_[1].is_moving = ball_.is_moving = true;
|
|
|
|
player_[0].motion_step = player_[1].motion_step = missile_[0].motion_step = missile_[1].motion_step = ball_.motion_step = 15;
|
|
|
|
player_[0].motion_time = player_[1].motion_time = missile_[0].motion_time = missile_[1].motion_time = ball_.motion_time = (horizontal_counter_ + 3) & ~3;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::clear_motion() {
|
2017-02-21 12:37:20 +00:00
|
|
|
player_[0].motion = player_[1].motion = missile_[0].motion = missile_[1].motion = ball_.motion = 0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
uint8_t TIA::get_collision_flags(int offset) {
|
2020-05-10 03:00:39 +00:00
|
|
|
return uint8_t((collision_flags_ >> (offset << 1)) << 6) & 0xc0;
|
2017-01-29 02:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +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
|
|
|
|
2017-03-24 01:59:16 +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-03-01 01:28:54 +00:00
|
|
|
bool is_reset = output_cursor < 224 && horizontal_counter_ >= 224;
|
2017-01-30 03:16:23 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
if(!output_cursor) {
|
2017-12-19 02:47:30 +00:00
|
|
|
std::memset(collision_buffer_, 0, sizeof(collision_buffer_));
|
2017-02-21 12:37:20 +00:00
|
|
|
|
|
|
|
ball_.motion_time %= 228;
|
|
|
|
player_[0].motion_time %= 228;
|
|
|
|
player_[1].motion_time %= 228;
|
|
|
|
missile_[0].motion_time %= 228;
|
|
|
|
missile_[1].motion_time %= 228;
|
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-15 01:56:16 +00:00
|
|
|
int latent_start = output_cursor + 4;
|
|
|
|
int latent_end = horizontal_counter_ + 4;
|
|
|
|
draw_playfield(latent_start, latent_end);
|
2020-05-10 03:00:39 +00:00
|
|
|
draw_object<Player>(player_[0], uint8_t(CollisionType::Player0), output_cursor, horizontal_counter_);
|
|
|
|
draw_object<Player>(player_[1], uint8_t(CollisionType::Player1), output_cursor, horizontal_counter_);
|
|
|
|
draw_missile(missile_[0], player_[0], uint8_t(CollisionType::Missile0), output_cursor, horizontal_counter_);
|
|
|
|
draw_missile(missile_[1], player_[1], uint8_t(CollisionType::Missile1), output_cursor, horizontal_counter_);
|
|
|
|
draw_object<Ball>(ball_, uint8_t(CollisionType::Ball), 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-03-24 01:59:16 +00:00
|
|
|
if(output_cursor < target) { \
|
|
|
|
if(horizontal_counter_ <= target) { \
|
2018-11-30 02:26:05 +00:00
|
|
|
crt_.function((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; \
|
2017-03-24 01:59:16 +00:00
|
|
|
} else { \
|
2018-11-30 02:26:05 +00:00
|
|
|
crt_.function((target - output_cursor) * 2); \
|
2017-01-31 03:42:27 +00:00
|
|
|
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
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
switch(output_mode_) {
|
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-03-24 01:59:16 +00:00
|
|
|
if(output_mode_ & blank_flag) {
|
|
|
|
if(pixel_target_) {
|
2017-02-07 01:09:12 +00:00
|
|
|
output_pixels(pixels_start_location_, output_cursor);
|
2018-11-30 02:26:05 +00:00
|
|
|
const int data_length = int(output_cursor - pixels_start_location_);
|
|
|
|
crt_.output_data(data_length * 2, size_t(data_length));
|
2017-02-07 01:09:12 +00:00
|
|
|
pixel_target_ = nullptr;
|
|
|
|
pixels_start_location_ = 0;
|
|
|
|
}
|
2017-01-31 03:42:27 +00:00
|
|
|
int duration = std::min(228, horizontal_counter_) - output_cursor;
|
2018-11-30 02:26:05 +00:00
|
|
|
crt_.output_blank(duration * 2);
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2018-11-30 02:26:05 +00:00
|
|
|
if(!pixels_start_location_) {
|
2017-02-07 01:09:12 +00:00
|
|
|
pixels_start_location_ = output_cursor;
|
2018-11-30 02:26:05 +00:00
|
|
|
pixel_target_ = reinterpret_cast<uint16_t *>(crt_.begin_data(160));
|
2017-02-07 01:09:12 +00:00
|
|
|
}
|
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
|
2017-03-24 01:59:16 +00:00
|
|
|
while(output_cursor < horizontal_counter_) {
|
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
|
|
|
|
2018-11-30 02:26:05 +00:00
|
|
|
if(horizontal_counter_ == cycles_per_line) {
|
2018-11-03 23:58:44 +00:00
|
|
|
const int data_length = int(output_cursor - pixels_start_location_);
|
2018-11-30 02:26:05 +00:00
|
|
|
crt_.output_data(data_length * 2, size_t(data_length));
|
2017-02-07 01:09:12 +00:00
|
|
|
pixel_target_ = nullptr;
|
|
|
|
pixels_start_location_ = 0;
|
2017-01-29 20:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-30 12:19:19 +00:00
|
|
|
|
2017-03-01 01:28:54 +00:00
|
|
|
if(is_reset) horizontal_blank_extend_ = false;
|
|
|
|
|
2017-01-29 20:43:57 +00:00
|
|
|
horizontal_counter_ %= cycles_per_line;
|
2017-01-29 18:47:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::output_pixels(int start, int end) {
|
2017-02-23 02:54:49 +00:00
|
|
|
start = std::max(start, pixels_start_location_);
|
2017-02-07 01:09:12 +00:00
|
|
|
int target_position = start - pixels_start_location_;
|
2017-02-06 23:29:00 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
if(start < first_pixel_cycle+8 && horizontal_blank_extend_) {
|
|
|
|
while(start < end && start < first_pixel_cycle+8) {
|
2018-11-30 02:26:05 +00:00
|
|
|
pixel_target_[target_position] = 0xff00; // TODO: this assumes little endianness.
|
2017-02-09 23:37:19 +00:00
|
|
|
start++;
|
|
|
|
target_position++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
if(playfield_priority_ == PlayfieldPriority::Score) {
|
|
|
|
while(start < end && start < first_pixel_cycle + 80) {
|
2017-02-07 01:09:12 +00:00
|
|
|
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
2020-05-10 03:00:39 +00:00
|
|
|
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[int(ColourMode::ScoreLeft)][buffer_value]].luminance_phase;
|
2017-02-07 01:09:12 +00:00
|
|
|
start++;
|
|
|
|
target_position++;
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
2017-03-24 01:59:16 +00:00
|
|
|
while(start < end) {
|
2017-02-07 01:09:12 +00:00
|
|
|
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
2020-05-10 03:00:39 +00:00
|
|
|
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[int(ColourMode::ScoreRight)][buffer_value]].luminance_phase;
|
2017-02-07 01:09:12 +00:00
|
|
|
start++;
|
|
|
|
target_position++;
|
|
|
|
}
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2020-05-10 03:00:39 +00:00
|
|
|
int table_index = int((playfield_priority_ == PlayfieldPriority::Standard) ? ColourMode::Standard : ColourMode::OnTop);
|
2017-03-24 01:59:16 +00:00
|
|
|
while(start < end) {
|
2017-02-07 01:09:12 +00:00
|
|
|
uint8_t buffer_value = collision_buffer_[start - first_pixel_cycle];
|
2018-11-30 02:26:05 +00:00
|
|
|
pixel_target_[target_position] = colour_palette_[colour_mask_by_mode_collision_flags_[table_index][buffer_value]].luminance_phase;
|
2017-02-07 01:09:12 +00:00
|
|
|
start++;
|
|
|
|
target_position++;
|
2017-02-06 23:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::output_line() {
|
|
|
|
switch(output_mode_) {
|
2017-01-30 03:16:23 +00:00
|
|
|
default:
|
|
|
|
// TODO: optimise special case
|
|
|
|
output_for_cycles(cycles_per_line);
|
|
|
|
break;
|
|
|
|
case sync_flag:
|
|
|
|
case sync_flag | blank_flag:
|
2018-11-30 02:26:05 +00:00
|
|
|
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:
|
2018-11-30 02:26:05 +00:00
|
|
|
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-11-12 20:59:11 +00:00
|
|
|
// MARK: - Playfield output
|
2017-02-05 22:51:56 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::draw_playfield(int start, int end) {
|
2017-02-08 12:30:32 +00:00
|
|
|
// don't do anything if this window ends too early
|
2017-02-15 01:56:16 +00:00
|
|
|
if(end < first_pixel_cycle) return;
|
2017-02-08 12:30:32 +00:00
|
|
|
|
|
|
|
// 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;
|
2017-03-24 01:59:16 +00:00
|
|
|
while(aligned_position < end) {
|
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;
|
2017-10-22 02:30:15 +00:00
|
|
|
*reinterpret_cast<uint32_t *>(&collision_buffer_[aligned_position - first_pixel_cycle]) |= value;
|
2017-02-08 12:30:32 +00:00
|
|
|
aligned_position += 4;
|
2017-02-05 22:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 01:53:42 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Motion
|
2017-02-12 16:31:17 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
template<class T> void TIA::perform_motion_step(T &object) {
|
|
|
|
if((object.motion_step ^ (object.motion ^ 8)) == 0xf) {
|
2017-02-20 22:58:28 +00:00
|
|
|
object.is_moving = false;
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2017-02-26 18:39:25 +00:00
|
|
|
if(object.position == 159) object.reset_pixels(0);
|
|
|
|
else if(object.position == 15 && object.copy_flags&1) object.reset_pixels(1);
|
|
|
|
else if(object.position == 31 && object.copy_flags&2) object.reset_pixels(2);
|
|
|
|
else if(object.position == 63 && object.copy_flags&4) object.reset_pixels(3);
|
2017-02-26 20:12:31 +00:00
|
|
|
else object.skip_pixels(1, object.motion_time);
|
2017-02-22 02:53:09 +00:00
|
|
|
object.position = (object.position + 1) % 160;
|
2017-02-20 22:58:28 +00:00
|
|
|
object.motion_step --;
|
|
|
|
object.motion_time += 4;
|
2017-02-17 01:28:37 +00:00
|
|
|
}
|
2017-02-12 16:31:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
template<class T> void TIA::perform_border_motion(T &object, int, int end) {
|
2017-02-20 22:58:28 +00:00
|
|
|
while(object.is_moving && object.motion_time < end)
|
2017-02-22 02:53:09 +00:00
|
|
|
perform_motion_step<T>(object);
|
2017-02-12 16:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
template<class T> void TIA::draw_object(T &object, const uint8_t collision_identity, int start, int end) {
|
2017-02-21 02:42:59 +00:00
|
|
|
int first_pixel = first_pixel_cycle - 4 + (horizontal_blank_extend_ ? 8 : 0);
|
|
|
|
|
2017-02-25 22:10:24 +00:00
|
|
|
object.dequeue_pixels(collision_buffer_, collision_identity, end - first_pixel_cycle);
|
|
|
|
|
2017-02-21 02:42:59 +00:00
|
|
|
// movement works across the entire screen, so do work that falls outside of the pixel area
|
2017-03-24 01:59:16 +00:00
|
|
|
if(start < first_pixel) {
|
2017-03-26 22:33:05 +00:00
|
|
|
perform_border_motion<T>(object, start, std::min(end, first_pixel));
|
2017-02-21 02:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// don't continue to do any drawing if this window ends too early
|
|
|
|
if(end < first_pixel) return;
|
|
|
|
if(start < first_pixel) start = first_pixel;
|
|
|
|
if(start >= end) return;
|
|
|
|
|
|
|
|
// perform the visible part of the line, if any
|
2017-03-24 01:59:16 +00:00
|
|
|
if(start < 224) {
|
2017-02-25 22:10:24 +00:00
|
|
|
draw_object_visible<T>(object, collision_identity, start - first_pixel_cycle + 4, std::min(end - first_pixel_cycle + 4, 160), end - first_pixel_cycle);
|
2017-02-21 02:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// move further if required
|
2017-03-24 01:59:16 +00:00
|
|
|
if(object.is_moving && end >= 224 && object.motion_time < end) {
|
2017-02-22 02:53:09 +00:00
|
|
|
perform_motion_step<T>(object);
|
2017-02-21 02:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
template<class T> void TIA::draw_object_visible(T &object, const uint8_t collision_identity, int start, int end, int time_now) {
|
2017-02-21 02:42:59 +00:00
|
|
|
// perform a miniature event loop on (i) triggering draws; (ii) drawing; and (iii) motion
|
|
|
|
int next_motion_time = object.motion_time - first_pixel_cycle + 4;
|
2017-03-24 01:59:16 +00:00
|
|
|
while(start < end) {
|
2017-02-21 02:42:59 +00:00
|
|
|
int next_event_time = end;
|
|
|
|
|
|
|
|
// is the next event a movement tick?
|
2017-03-24 01:59:16 +00:00
|
|
|
if(object.is_moving && next_motion_time < next_event_time) {
|
2017-02-21 02:42:59 +00:00
|
|
|
next_event_time = next_motion_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the next event a graphics trigger?
|
|
|
|
int next_copy = 160;
|
2017-02-26 18:39:25 +00:00
|
|
|
int next_copy_id = 0;
|
2017-03-24 01:59:16 +00:00
|
|
|
if(object.copy_flags) {
|
|
|
|
if(object.position < 16 && object.copy_flags&1) {
|
2017-02-21 02:42:59 +00:00
|
|
|
next_copy = 16;
|
2017-02-26 18:39:25 +00:00
|
|
|
next_copy_id = 1;
|
2017-03-24 01:59:16 +00:00
|
|
|
} else if(object.position < 32 && object.copy_flags&2) {
|
2017-02-21 02:42:59 +00:00
|
|
|
next_copy = 32;
|
2017-02-26 18:39:25 +00:00
|
|
|
next_copy_id = 2;
|
2017-03-24 01:59:16 +00:00
|
|
|
} else if(object.position < 64 && object.copy_flags&4) {
|
2017-02-21 02:42:59 +00:00
|
|
|
next_copy = 64;
|
2017-02-26 18:39:25 +00:00
|
|
|
next_copy_id = 3;
|
2017-02-21 02:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int next_copy_time = start + next_copy - object.position;
|
|
|
|
if(next_copy_time < next_event_time) next_event_time = next_copy_time;
|
|
|
|
|
|
|
|
// the decision is to progress by length
|
|
|
|
const int length = next_event_time - start;
|
|
|
|
|
2017-02-26 20:12:31 +00:00
|
|
|
// enqueue a future intention to draw pixels if spitting them out now would violate accuracy;
|
|
|
|
// otherwise draw them now
|
2017-03-24 01:59:16 +00:00
|
|
|
if(object.enqueues && next_event_time > time_now) {
|
|
|
|
if(start < time_now) {
|
2017-02-26 20:12:31 +00:00
|
|
|
object.output_pixels(&collision_buffer_[start], time_now - start, collision_identity, start + first_pixel_cycle - 4);
|
|
|
|
object.enqueue_pixels(time_now, next_event_time, time_now + first_pixel_cycle - 4);
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2017-02-26 20:12:31 +00:00
|
|
|
object.enqueue_pixels(start, next_event_time, start + first_pixel_cycle - 4);
|
2017-02-25 22:10:24 +00:00
|
|
|
}
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2017-02-26 20:12:31 +00:00
|
|
|
object.output_pixels(&collision_buffer_[start], length, collision_identity, start + first_pixel_cycle - 4);
|
2017-02-25 22:10:24 +00:00
|
|
|
}
|
2017-02-21 02:42:59 +00:00
|
|
|
|
|
|
|
// the next interesting event is after next_event_time cycles, so progress
|
|
|
|
object.position = (object.position + length) % 160;
|
|
|
|
start = next_event_time;
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
// if the event is a motion tick, apply; if it's a draw trigger, trigger a draw
|
|
|
|
if(object.is_moving && start == next_motion_time) {
|
2017-02-21 02:42:59 +00:00
|
|
|
perform_motion_step(object);
|
|
|
|
next_motion_time += 4;
|
2017-03-24 01:59:16 +00:00
|
|
|
} else if(start == next_copy_time) {
|
2017-02-26 18:39:25 +00:00
|
|
|
object.reset_pixels(next_copy_id);
|
2017-02-21 02:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-26 20:12:31 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Missile drawing
|
2017-02-26 20:12:31 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void TIA::draw_missile(Missile &missile, Player &player, const uint8_t collision_identity, int start, int end) {
|
|
|
|
if(!missile.locked_to_player || player.latched_pixel4_time < 0) {
|
2017-02-26 20:12:31 +00:00
|
|
|
draw_object<Missile>(missile, collision_identity, start, end);
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2017-02-26 20:12:31 +00:00
|
|
|
draw_object<Missile>(missile, collision_identity, start, player.latched_pixel4_time);
|
|
|
|
missile.position = 0;
|
|
|
|
draw_object<Missile>(missile, collision_identity, player.latched_pixel4_time, end);
|
|
|
|
player.latched_pixel4_time = -1;
|
|
|
|
}
|
|
|
|
}
|