2017-01-28 21:19:08 +00:00
|
|
|
//
|
|
|
|
// TIA.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 28/01/2017.
|
|
|
|
// Copyright © 2017 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef TIA_hpp
|
|
|
|
#define TIA_hpp
|
|
|
|
|
|
|
|
#include <cstdint>
|
2017-01-29 02:46:40 +00:00
|
|
|
#include "../CRTMachine.hpp"
|
2017-07-25 01:48:34 +00:00
|
|
|
#include "../../Components/ClockReceiver.hpp"
|
2017-01-28 21:19:08 +00:00
|
|
|
|
|
|
|
namespace Atari2600 {
|
|
|
|
|
2017-07-25 01:48:34 +00:00
|
|
|
class TIA: public ClockReceiver<TIA> {
|
2017-01-28 21:19:08 +00:00
|
|
|
public:
|
2017-01-29 02:46:40 +00:00
|
|
|
TIA();
|
2017-02-13 00:55:02 +00:00
|
|
|
// The supplied hook is for unit testing only; if instantiated with a line_end_function then it will
|
|
|
|
// be called with the latest collision buffer upon the conclusion of each line. What's a collision
|
|
|
|
// buffer? It's an implementation detail. If you're not writing a unit test, leave it alone.
|
|
|
|
TIA(std::function<void(uint8_t *output_buffer)> line_end_function);
|
|
|
|
|
2017-01-29 19:19:26 +00:00
|
|
|
enum class OutputMode {
|
|
|
|
NTSC, PAL
|
|
|
|
};
|
|
|
|
|
2017-02-05 22:51:56 +00:00
|
|
|
/*!
|
2017-07-25 01:48:34 +00:00
|
|
|
Advances the TIA by @c cycles. Any queued setters take effect in the first cycle performed.
|
2017-02-05 22:51:56 +00:00
|
|
|
*/
|
2017-07-25 01:48:34 +00:00
|
|
|
void run_for(const Cycles &cycles);
|
2017-01-29 19:19:26 +00:00
|
|
|
void set_output_mode(OutputMode output_mode);
|
2017-01-28 21:19:08 +00:00
|
|
|
|
2017-01-30 03:16:23 +00:00
|
|
|
void set_sync(bool sync);
|
|
|
|
void set_blank(bool blank);
|
2017-01-29 02:46:40 +00:00
|
|
|
void reset_horizontal_counter(); // Reset is delayed by four cycles.
|
|
|
|
|
2017-02-08 03:14:45 +00:00
|
|
|
/*!
|
|
|
|
@returns the number of cycles between (current TIA time) + from_offset to the current or
|
|
|
|
next horizontal blanking period. Returns numbers in the range [0, 227].
|
|
|
|
*/
|
2017-01-29 02:46:40 +00:00
|
|
|
int get_cycles_until_horizontal_blank(unsigned int from_offset);
|
2017-01-28 21:19:08 +00:00
|
|
|
|
|
|
|
void set_background_colour(uint8_t colour);
|
|
|
|
|
|
|
|
void set_playfield(uint16_t offset, uint8_t value);
|
|
|
|
void set_playfield_control_and_ball_size(uint8_t value);
|
|
|
|
void set_playfield_ball_colour(uint8_t colour);
|
|
|
|
|
|
|
|
void set_player_number_and_size(int player, uint8_t value);
|
|
|
|
void set_player_graphic(int player, uint8_t value);
|
|
|
|
void set_player_reflected(int player, bool reflected);
|
|
|
|
void set_player_delay(int player, bool delay);
|
|
|
|
void set_player_position(int player);
|
|
|
|
void set_player_motion(int player, uint8_t motion);
|
|
|
|
void set_player_missile_colour(int player, uint8_t colour);
|
|
|
|
|
|
|
|
void set_missile_enable(int missile, bool enabled);
|
|
|
|
void set_missile_position(int missile);
|
2017-02-21 12:58:37 +00:00
|
|
|
void set_missile_position_to_player(int missile, bool lock);
|
2017-01-28 21:19:08 +00:00
|
|
|
void set_missile_motion(int missile, uint8_t motion);
|
|
|
|
|
|
|
|
void set_ball_enable(bool enabled);
|
|
|
|
void set_ball_delay(bool delay);
|
|
|
|
void set_ball_position();
|
|
|
|
void set_ball_motion(uint8_t motion);
|
|
|
|
|
|
|
|
void move();
|
|
|
|
void clear_motion();
|
|
|
|
|
|
|
|
uint8_t get_collision_flags(int offset);
|
|
|
|
void clear_collision_flags();
|
2017-01-29 02:46:40 +00:00
|
|
|
|
|
|
|
virtual std::shared_ptr<Outputs::CRT::CRT> get_crt() { return crt_; }
|
|
|
|
|
|
|
|
private:
|
2017-02-22 12:14:30 +00:00
|
|
|
TIA(bool create_crt);
|
2017-01-29 02:46:40 +00:00
|
|
|
std::shared_ptr<Outputs::CRT::CRT> crt_;
|
2017-02-13 00:55:02 +00:00
|
|
|
std::function<void(uint8_t *output_buffer)> line_end_function_;
|
2017-01-29 18:47:36 +00:00
|
|
|
|
2017-01-31 03:42:27 +00:00
|
|
|
// the master counter; counts from 0 to 228 with all visible pixels being in the final 160
|
2017-01-29 18:47:36 +00:00
|
|
|
int horizontal_counter_;
|
2017-01-30 03:16:23 +00:00
|
|
|
|
2017-01-31 03:42:27 +00:00
|
|
|
// contains flags to indicate whether sync or blank are currently active
|
2017-01-30 03:16:23 +00:00
|
|
|
int output_mode_;
|
|
|
|
|
2017-02-05 22:51:56 +00:00
|
|
|
// keeps track of the target pixel buffer for this line and when it was acquired, and a corresponding collision buffer
|
2017-02-22 03:26:20 +00:00
|
|
|
alignas(alignof(uint32_t)) uint8_t collision_buffer_[160];
|
2017-02-05 22:51:56 +00:00
|
|
|
enum class CollisionType : uint8_t {
|
2017-02-06 23:29:00 +00:00
|
|
|
Playfield = (1 << 0),
|
|
|
|
Ball = (1 << 1),
|
|
|
|
Player0 = (1 << 2),
|
|
|
|
Player1 = (1 << 3),
|
|
|
|
Missile0 = (1 << 4),
|
|
|
|
Missile1 = (1 << 5)
|
|
|
|
};
|
|
|
|
|
|
|
|
int collision_flags_;
|
|
|
|
int collision_flags_by_buffer_vaules_[64];
|
|
|
|
|
|
|
|
// colour mapping tables
|
|
|
|
enum class ColourMode {
|
|
|
|
Standard = 0,
|
|
|
|
ScoreLeft,
|
|
|
|
ScoreRight,
|
|
|
|
OnTop
|
|
|
|
};
|
|
|
|
uint8_t colour_mask_by_mode_collision_flags_[4][64]; // maps from [ColourMode][CollisionMark] to colour_pallete_ entry
|
|
|
|
|
|
|
|
enum class ColourIndex {
|
|
|
|
Background = 0,
|
|
|
|
PlayfieldBall,
|
2017-02-07 02:48:41 +00:00
|
|
|
PlayerMissile0,
|
|
|
|
PlayerMissile1
|
2017-02-05 22:51:56 +00:00
|
|
|
};
|
2017-02-06 23:29:00 +00:00
|
|
|
uint8_t colour_palette_[4];
|
2017-01-29 19:19:26 +00:00
|
|
|
|
2017-01-31 03:42:27 +00:00
|
|
|
// playfield state
|
2017-01-31 02:38:58 +00:00
|
|
|
int background_half_mask_;
|
2017-02-06 23:29:00 +00:00
|
|
|
enum class PlayfieldPriority {
|
|
|
|
Standard,
|
|
|
|
Score,
|
|
|
|
OnTop
|
|
|
|
} playfield_priority_;
|
2017-02-01 01:30:32 +00:00
|
|
|
uint32_t background_[2]; // contains two 20-bit bitfields representing the background state;
|
|
|
|
// at index 0 is the left-hand side of the playfield with bit 0 being
|
|
|
|
// the first bit to display, bit 1 the second, etc. Index 1 contains
|
|
|
|
// a mirror image of index 0. If the playfield is being displayed in
|
|
|
|
// mirroring mode, background_[0] will be output on the left and
|
|
|
|
// background_[1] on the right; otherwise background_[0] will be
|
|
|
|
// output twice.
|
2017-01-30 13:08:03 +00:00
|
|
|
|
2017-02-21 12:37:20 +00:00
|
|
|
// objects
|
2017-02-25 22:10:24 +00:00
|
|
|
template<class T> struct Object {
|
2017-02-21 12:37:20 +00:00
|
|
|
// the two programmer-set values
|
|
|
|
int position;
|
|
|
|
int motion;
|
|
|
|
|
|
|
|
// motion_step_ is the current motion counter value; motion_time_ is the next time it will fire
|
|
|
|
int motion_step;
|
|
|
|
int motion_time;
|
|
|
|
|
|
|
|
// indicates whether this object is currently undergoing motion
|
|
|
|
bool is_moving;
|
|
|
|
|
2017-03-26 19:47:04 +00:00
|
|
|
Object() : position(0), motion(0), motion_step(0), motion_time(0), is_moving(false) {};
|
2017-02-21 12:37:20 +00:00
|
|
|
};
|
|
|
|
|
2017-01-31 03:42:27 +00:00
|
|
|
// player state
|
2017-02-25 22:10:24 +00:00
|
|
|
struct Player: public Object<Player> {
|
2017-03-26 19:47:04 +00:00
|
|
|
Player() :
|
|
|
|
adder(4),
|
|
|
|
copy_flags(0),
|
|
|
|
graphic{0, 0},
|
|
|
|
reverse_mask(false),
|
|
|
|
graphic_index(0),
|
|
|
|
pixel_position(32),
|
|
|
|
pixel_counter(0),
|
|
|
|
latched_pixel4_time(-1),
|
|
|
|
copy_index_(0),
|
|
|
|
queue_read_pointer_(0),
|
|
|
|
queue_write_pointer_(0) {}
|
2017-02-25 22:10:24 +00:00
|
|
|
|
2017-02-21 03:22:39 +00:00
|
|
|
int adder;
|
2017-01-31 03:42:27 +00:00
|
|
|
int copy_flags; // a bit field, corresponding to the first few values of NUSIZ
|
2017-02-17 01:52:01 +00:00
|
|
|
uint8_t graphic[2]; // the player graphic; 1 = new, 0 = current
|
2017-01-31 03:42:27 +00:00
|
|
|
int reverse_mask; // 7 for a reflected player, 0 for normal
|
2017-02-17 01:52:01 +00:00
|
|
|
int graphic_index;
|
2017-02-12 19:01:50 +00:00
|
|
|
|
2017-03-05 03:23:50 +00:00
|
|
|
int pixel_position, pixel_counter;
|
2017-02-26 20:12:31 +00:00
|
|
|
int latched_pixel4_time;
|
2017-02-25 22:13:22 +00:00
|
|
|
const bool enqueues = true;
|
2017-02-13 00:55:02 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void skip_pixels(const int count, int from_horizontal_counter) {
|
2017-03-05 03:23:50 +00:00
|
|
|
int old_pixel_counter = pixel_counter;
|
2017-02-21 03:22:39 +00:00
|
|
|
pixel_position = std::min(32, pixel_position + count * adder);
|
2017-03-05 03:23:50 +00:00
|
|
|
pixel_counter += count;
|
2017-03-24 01:59:16 +00:00
|
|
|
if(!copy_index_ && old_pixel_counter < 4 && pixel_counter >= 4) {
|
2017-03-05 03:23:50 +00:00
|
|
|
latched_pixel4_time = from_horizontal_counter + 4 - old_pixel_counter;
|
2017-02-26 20:12:31 +00:00
|
|
|
}
|
2017-02-21 03:22:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void reset_pixels(int copy) {
|
2017-03-05 03:23:50 +00:00
|
|
|
pixel_position = pixel_counter = 0;
|
2017-02-26 20:12:31 +00:00
|
|
|
copy_index_ = copy;
|
2017-02-21 03:22:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void output_pixels(uint8_t *const target, const int count, const uint8_t collision_identity, int from_horizontal_counter) {
|
2017-02-25 22:10:24 +00:00
|
|
|
output_pixels(target, count, collision_identity, pixel_position, adder, reverse_mask);
|
2017-02-26 20:12:31 +00:00
|
|
|
skip_pixels(count, from_horizontal_counter);
|
2017-02-25 22:10:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void dequeue_pixels(uint8_t *const target, const uint8_t collision_identity, const int time_now) {
|
|
|
|
while(queue_read_pointer_ != queue_write_pointer_) {
|
2017-02-25 22:25:10 +00:00
|
|
|
uint8_t *const start_ptr = &target[queue_[queue_read_pointer_].start];
|
2017-03-24 01:59:16 +00:00
|
|
|
if(queue_[queue_read_pointer_].end > time_now) {
|
2017-02-25 22:25:10 +00:00
|
|
|
const int length = time_now - queue_[queue_read_pointer_].start;
|
|
|
|
output_pixels(start_ptr, length, collision_identity, queue_[queue_read_pointer_].pixel_position, queue_[queue_read_pointer_].adder, queue_[queue_read_pointer_].reverse_mask);
|
|
|
|
queue_[queue_read_pointer_].pixel_position += length * queue_[queue_read_pointer_].adder;
|
|
|
|
queue_[queue_read_pointer_].start = time_now;
|
|
|
|
return;
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2017-02-25 22:25:10 +00:00
|
|
|
output_pixels(start_ptr, queue_[queue_read_pointer_].end - queue_[queue_read_pointer_].start, collision_identity, queue_[queue_read_pointer_].pixel_position, queue_[queue_read_pointer_].adder, queue_[queue_read_pointer_].reverse_mask);
|
|
|
|
}
|
|
|
|
queue_read_pointer_ = (queue_read_pointer_ + 1)&3;
|
2017-02-25 22:10:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
void enqueue_pixels(const int start, const int end, int from_horizontal_counter) {
|
2017-02-25 22:25:10 +00:00
|
|
|
queue_[queue_write_pointer_].start = start;
|
|
|
|
queue_[queue_write_pointer_].end = end;
|
|
|
|
queue_[queue_write_pointer_].pixel_position = pixel_position;
|
|
|
|
queue_[queue_write_pointer_].adder = adder;
|
|
|
|
queue_[queue_write_pointer_].reverse_mask = reverse_mask;
|
|
|
|
queue_write_pointer_ = (queue_write_pointer_ + 1)&3;
|
2017-02-26 20:12:31 +00:00
|
|
|
skip_pixels(end - start, from_horizontal_counter);
|
2017-02-25 22:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-26 20:12:31 +00:00
|
|
|
int copy_index_;
|
2017-03-24 01:59:16 +00:00
|
|
|
struct QueuedPixels {
|
2017-02-25 22:10:24 +00:00
|
|
|
int start, end;
|
|
|
|
int pixel_position;
|
|
|
|
int adder;
|
|
|
|
int reverse_mask;
|
2017-03-26 19:47:04 +00:00
|
|
|
QueuedPixels() : start(0), end(0), pixel_position(0), adder(0), reverse_mask(false) {}
|
2017-02-25 22:25:10 +00:00
|
|
|
} queue_[4];
|
|
|
|
int queue_read_pointer_, queue_write_pointer_;
|
2017-02-25 22:10:24 +00:00
|
|
|
|
2017-07-22 01:51:18 +00:00
|
|
|
inline void output_pixels(uint8_t *const target, const int count, const uint8_t collision_identity, int output_pixel_position, int output_adder, int output_reverse_mask) {
|
|
|
|
if(output_pixel_position == 32 || !graphic[graphic_index]) return;
|
2017-02-21 03:22:39 +00:00
|
|
|
int output_cursor = 0;
|
2017-07-22 01:51:18 +00:00
|
|
|
while(output_pixel_position < 32 && output_cursor < count) {
|
|
|
|
int shift = (output_pixel_position >> 2) ^ output_reverse_mask;
|
2017-02-21 03:22:39 +00:00
|
|
|
target[output_cursor] |= ((graphic[graphic_index] >> shift)&1) * collision_identity;
|
|
|
|
output_cursor++;
|
2017-07-22 01:51:18 +00:00
|
|
|
output_pixel_position += output_adder;
|
2017-02-21 03:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 03:42:27 +00:00
|
|
|
} player_[2];
|
|
|
|
|
2017-02-21 12:58:37 +00:00
|
|
|
// common actor for things that appear as a horizontal run of pixels
|
2017-02-25 22:10:24 +00:00
|
|
|
struct HorizontalRun: public Object<HorizontalRun> {
|
2017-02-21 03:22:39 +00:00
|
|
|
int pixel_position;
|
2017-02-21 12:58:37 +00:00
|
|
|
int size;
|
2017-02-25 22:13:22 +00:00
|
|
|
const bool enqueues = false;
|
2017-02-21 03:22:39 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void skip_pixels(const int count, int from_horizontal_counter) {
|
2017-02-21 03:22:39 +00:00
|
|
|
pixel_position = std::max(0, pixel_position - count);
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void reset_pixels(int copy) {
|
2017-02-21 03:22:39 +00:00
|
|
|
pixel_position = size;
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void output_pixels(uint8_t *const target, const int count, const uint8_t collision_identity, int from_horizontal_counter) {
|
2017-02-21 12:58:37 +00:00
|
|
|
int output_cursor = 0;
|
|
|
|
while(pixel_position && output_cursor < count)
|
|
|
|
{
|
|
|
|
target[output_cursor] |= collision_identity;
|
|
|
|
output_cursor++;
|
|
|
|
pixel_position--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 22:13:22 +00:00
|
|
|
void dequeue_pixels(uint8_t *const target, const uint8_t collision_identity, const int time_now) {}
|
2017-02-26 20:12:31 +00:00
|
|
|
void enqueue_pixels(const int start, const int end, int from_horizontal_counter) {}
|
2017-02-25 22:13:22 +00:00
|
|
|
|
2017-02-21 12:58:37 +00:00
|
|
|
HorizontalRun() : pixel_position(0), size(1) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// missile state
|
|
|
|
struct Missile: public HorizontalRun {
|
|
|
|
bool enabled;
|
2017-02-22 01:45:20 +00:00
|
|
|
bool locked_to_player;
|
2017-02-21 12:58:37 +00:00
|
|
|
int copy_flags;
|
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void output_pixels(uint8_t *const target, const int count, const uint8_t collision_identity, int from_horizontal_counter) {
|
2017-02-21 03:22:39 +00:00
|
|
|
if(!pixel_position) return;
|
2017-03-24 01:59:16 +00:00
|
|
|
if(enabled && !locked_to_player) {
|
2017-02-26 20:12:31 +00:00
|
|
|
HorizontalRun::output_pixels(target, count, collision_identity, from_horizontal_counter);
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2017-02-26 20:12:31 +00:00
|
|
|
skip_pixels(count, from_horizontal_counter);
|
2017-02-21 03:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-21 12:58:37 +00:00
|
|
|
|
|
|
|
Missile() : enabled(false), copy_flags(0) {}
|
2017-01-31 03:42:27 +00:00
|
|
|
} missile_[2];
|
2017-02-09 23:37:19 +00:00
|
|
|
|
2017-02-19 13:02:54 +00:00
|
|
|
// ball state
|
2017-02-21 12:58:37 +00:00
|
|
|
struct Ball: public HorizontalRun {
|
2017-02-19 13:02:54 +00:00
|
|
|
bool enabled[2];
|
|
|
|
int enabled_index;
|
2017-02-21 03:22:39 +00:00
|
|
|
const int copy_flags = 0;
|
2017-02-19 13:02:54 +00:00
|
|
|
|
2017-03-24 01:59:16 +00:00
|
|
|
inline void output_pixels(uint8_t *const target, const int count, const uint8_t collision_identity, int from_horizontal_counter) {
|
2017-02-21 03:22:39 +00:00
|
|
|
if(!pixel_position) return;
|
2017-03-24 01:59:16 +00:00
|
|
|
if(enabled[enabled_index]) {
|
2017-02-26 20:12:31 +00:00
|
|
|
HorizontalRun::output_pixels(target, count, collision_identity, from_horizontal_counter);
|
2017-03-24 01:59:16 +00:00
|
|
|
} else {
|
2017-02-26 20:12:31 +00:00
|
|
|
skip_pixels(count, from_horizontal_counter);
|
2017-02-21 03:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:47:04 +00:00
|
|
|
Ball() : enabled{false, false}, enabled_index(0) {}
|
2017-02-19 13:02:54 +00:00
|
|
|
} ball_;
|
|
|
|
|
2017-02-20 23:04:40 +00:00
|
|
|
// motion
|
2017-02-21 12:37:20 +00:00
|
|
|
bool horizontal_blank_extend_;
|
2017-02-22 02:53:09 +00:00
|
|
|
template<class T> void perform_border_motion(T &object, int start, int end);
|
|
|
|
template<class T> void perform_motion_step(T &object);
|
2017-02-10 01:53:42 +00:00
|
|
|
|
|
|
|
// drawing methods and state
|
2017-02-26 20:12:31 +00:00
|
|
|
void draw_missile(Missile &, Player &, const uint8_t collision_identity, int start, int end);
|
2017-02-21 12:37:20 +00:00
|
|
|
template<class T> void draw_object(T &, const uint8_t collision_identity, int start, int end);
|
2017-02-25 22:10:24 +00:00
|
|
|
template<class T> void draw_object_visible(T &, const uint8_t collision_identity, int start, int end, int time_now);
|
2017-02-22 12:14:30 +00:00
|
|
|
inline void draw_playfield(int start, int end);
|
2017-02-21 02:42:59 +00:00
|
|
|
|
2017-02-10 01:53:42 +00:00
|
|
|
inline void output_for_cycles(int number_of_cycles);
|
|
|
|
inline void output_line();
|
|
|
|
|
|
|
|
int pixels_start_location_;
|
|
|
|
uint8_t *pixel_target_;
|
|
|
|
inline void output_pixels(int start, int end);
|
2017-01-28 21:19:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TIA_hpp */
|