2019-10-05 01:34:15 +00:00
|
|
|
//
|
|
|
|
// Video.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/10/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-10-09 01:18:08 +00:00
|
|
|
#ifndef Atari_ST_Video_hpp
|
|
|
|
#define Atari_ST_Video_hpp
|
2019-10-05 01:34:15 +00:00
|
|
|
|
2019-11-09 20:31:41 +00:00
|
|
|
#include "../../../Outputs/CRT/CRT.hpp"
|
|
|
|
#include "../../../ClockReceiver/ClockReceiver.hpp"
|
2019-10-05 01:34:15 +00:00
|
|
|
|
2019-11-18 04:28:00 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-10-05 01:34:15 +00:00
|
|
|
namespace Atari {
|
|
|
|
namespace ST {
|
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
/*!
|
|
|
|
Models a combination of the parts of the GLUE, MMU and Shifter that in net
|
|
|
|
form the video subsystem of the Atari ST. So not accurate to a real chip, but
|
|
|
|
(hopefully) to a subsystem.
|
|
|
|
*/
|
2019-10-05 01:34:15 +00:00
|
|
|
class Video {
|
|
|
|
public:
|
|
|
|
Video();
|
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
/*!
|
|
|
|
Sets the memory pool that provides video, and its size.
|
|
|
|
*/
|
|
|
|
void set_ram(uint16_t *, size_t size);
|
|
|
|
|
2019-10-05 01:34:15 +00:00
|
|
|
/*!
|
|
|
|
Sets the target device for video data.
|
|
|
|
*/
|
|
|
|
void set_scan_target(Outputs::Display::ScanTarget *scan_target);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Produces the next @c duration period of pixels.
|
|
|
|
*/
|
|
|
|
void run_for(HalfCycles duration);
|
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
|
2019-10-09 01:18:08 +00:00
|
|
|
/*!
|
|
|
|
@returns the number of cycles until there is next a change in the hsync,
|
|
|
|
vsync or display_enable outputs.
|
|
|
|
*/
|
|
|
|
HalfCycles get_next_sequence_point();
|
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
/*!
|
|
|
|
@returns @c true if the horizontal sync output is currently active; @c false otherwise.
|
|
|
|
|
|
|
|
@discussion On an Atari ST, this generates a VPA-style interrupt, which is often erroneously
|
|
|
|
documented as being triggered by horizontal blank.
|
|
|
|
*/
|
2019-11-09 02:25:28 +00:00
|
|
|
bool hsync();
|
2019-11-18 02:28:51 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns @c true if the vertical sync output is currently active; @c false otherwise.
|
|
|
|
|
|
|
|
@discussion On an Atari ST, this generates a VPA-style interrupt, which is often erroneously
|
|
|
|
documented as being triggered by vertical blank.
|
|
|
|
*/
|
2019-10-09 01:18:08 +00:00
|
|
|
bool vsync();
|
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
/*!
|
|
|
|
@returns @c true if the display enabled output is currently active; @c false otherwise.
|
2019-10-11 02:46:58 +00:00
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
@discussion On an Atari ST this is fed to the MFP. The documentation that I've been able to
|
|
|
|
find implies a total 28-cycle delay between the real delay enabled signal changing and its effect
|
|
|
|
on the 68000 interrupt input via the MFP. As I have yet to determine how much delay is caused
|
|
|
|
by the MFP a full 28-cycle delay is applied by this class. This should be dialled down when the
|
|
|
|
MFP's responsibility is clarified.
|
|
|
|
*/
|
|
|
|
bool display_enabled();
|
|
|
|
|
|
|
|
/// @returns the effect of reading from @c address; only the low 6 bits are decoded.
|
2019-11-02 03:01:06 +00:00
|
|
|
uint16_t read(int address);
|
2019-11-18 02:28:51 +00:00
|
|
|
|
|
|
|
/// Writes @c value to @c address, of which only the low 6 bits are decoded.
|
2019-10-11 03:29:46 +00:00
|
|
|
void write(int address, uint16_t value);
|
2019-10-10 03:01:11 +00:00
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
/// Used internally to track state.
|
|
|
|
enum class FieldFrequency {
|
|
|
|
Fifty = 0, Sixty = 1, SeventyTwo = 2
|
|
|
|
};
|
|
|
|
|
2019-10-05 01:34:15 +00:00
|
|
|
private:
|
|
|
|
Outputs::CRT::CRT crt_;
|
2019-10-09 01:18:08 +00:00
|
|
|
|
2019-11-08 04:11:06 +00:00
|
|
|
uint16_t raw_palette_[16];
|
2019-10-10 03:01:11 +00:00
|
|
|
uint16_t palette_[16];
|
2019-10-11 02:46:58 +00:00
|
|
|
int base_address_ = 0;
|
|
|
|
int current_address_ = 0;
|
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
uint16_t *ram_ = nullptr;
|
2019-10-11 02:46:58 +00:00
|
|
|
uint16_t line_buffer_[256];
|
2019-10-10 03:01:11 +00:00
|
|
|
|
2019-11-09 03:18:47 +00:00
|
|
|
int x_ = 0, y_ = 0, next_y_ = 0;
|
2019-10-28 02:39:00 +00:00
|
|
|
|
2019-11-02 03:01:06 +00:00
|
|
|
uint16_t video_mode_ = 0;
|
2019-11-06 04:02:25 +00:00
|
|
|
uint16_t sync_mode_ = 0;
|
|
|
|
|
|
|
|
FieldFrequency field_frequency_ = FieldFrequency::Fifty;
|
|
|
|
enum class OutputBpp {
|
|
|
|
One, Two, Four
|
2019-11-18 02:28:51 +00:00
|
|
|
} output_bpp_ = OutputBpp::Four;
|
2019-11-06 04:02:25 +00:00
|
|
|
void update_output_mode();
|
|
|
|
|
2019-11-09 03:18:47 +00:00
|
|
|
struct HorizontalState {
|
2019-11-06 04:02:25 +00:00
|
|
|
bool enable = false;
|
|
|
|
bool blank = false;
|
|
|
|
bool sync = false;
|
2019-11-09 03:18:47 +00:00
|
|
|
} horizontal_;
|
|
|
|
struct VerticalState {
|
|
|
|
bool enable = false;
|
|
|
|
bool blank = false;
|
|
|
|
|
|
|
|
enum class SyncSchedule {
|
|
|
|
/// No sync events this line.
|
|
|
|
None,
|
|
|
|
/// Sync should begin during this horizontal line.
|
|
|
|
Begin,
|
|
|
|
/// Sync should end during this horizontal line.
|
|
|
|
End,
|
|
|
|
} sync_schedule = SyncSchedule::None;
|
|
|
|
bool sync = false;
|
|
|
|
} vertical_, next_vertical_;
|
2019-11-08 03:53:26 +00:00
|
|
|
int line_length_ = 1024;
|
2019-11-06 04:02:25 +00:00
|
|
|
|
|
|
|
int data_latch_position_ = 0;
|
2019-11-18 02:28:51 +00:00
|
|
|
uint16_t data_latch_[4] = {0, 0, 0, 0};
|
2019-11-06 04:02:25 +00:00
|
|
|
void latch_word();
|
|
|
|
|
2019-11-11 02:39:40 +00:00
|
|
|
class Shifter {
|
|
|
|
public:
|
|
|
|
Shifter(Outputs::CRT::CRT &crt, uint16_t *palette) : crt_(crt), palette_(palette) {}
|
|
|
|
void output_blank(int duration);
|
|
|
|
void output_sync(int duration);
|
|
|
|
void output_border(int duration, OutputBpp bpp);
|
|
|
|
void output_pixels(int duration, OutputBpp bpp);
|
|
|
|
|
|
|
|
void load(uint64_t value);
|
|
|
|
|
|
|
|
private:
|
|
|
|
int duration_ = 0;
|
|
|
|
enum class OutputMode {
|
|
|
|
Sync, Blank, Border, Pixels
|
|
|
|
} output_mode_ = OutputMode::Sync;
|
2019-11-18 02:28:51 +00:00
|
|
|
uint16_t border_colour_ = 0;
|
|
|
|
OutputBpp bpp_ = OutputBpp::Four;
|
2019-11-11 02:39:40 +00:00
|
|
|
union {
|
|
|
|
uint64_t output_shifter_;
|
|
|
|
uint32_t shifter_halves_[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
void flush_output(OutputMode next_mode);
|
|
|
|
|
2019-11-18 02:28:51 +00:00
|
|
|
uint16_t *pixel_buffer_ = nullptr;
|
2019-11-11 02:39:40 +00:00
|
|
|
size_t pixel_pointer_ = 0;
|
|
|
|
|
|
|
|
Outputs::CRT::CRT &crt_;
|
|
|
|
uint16_t *palette_ = nullptr;
|
|
|
|
} shifter_;
|
2019-11-18 04:28:00 +00:00
|
|
|
|
|
|
|
/// Contains copies of the various observeable fields, after the relevant propagation delay.
|
|
|
|
struct PublicState {
|
|
|
|
bool display_enable = false;
|
|
|
|
} public_state_;
|
|
|
|
|
|
|
|
struct Event {
|
|
|
|
int delay;
|
|
|
|
enum class Type {
|
|
|
|
SetDisplayEnable, ResetDisplayEnable
|
|
|
|
} type;
|
|
|
|
|
|
|
|
Event(Type type, int delay) : delay(delay), type(type) {}
|
|
|
|
|
|
|
|
void apply(PublicState &state) {
|
|
|
|
apply(type, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void apply(Type type, PublicState &state) {
|
|
|
|
switch(type) {
|
|
|
|
default:
|
|
|
|
case Type::SetDisplayEnable: state.display_enable = true; break;
|
|
|
|
case Type::ResetDisplayEnable: state.display_enable = false; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Event> pending_events_;
|
|
|
|
void add_event(int delay, Event::Type type) {
|
|
|
|
// Apply immediately if there's no delay (or a negative delay).
|
|
|
|
if(delay <= 0) {
|
|
|
|
Event::apply(type, public_state_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise enqueue, having subtracted the delay for any preceding events,
|
|
|
|
// and subtracting from the subsequent, if any.
|
|
|
|
auto insertion_point = pending_events_.begin();
|
|
|
|
while(insertion_point != pending_events_.end() && insertion_point->delay > delay) {
|
|
|
|
delay -= insertion_point->delay;
|
|
|
|
++insertion_point;
|
|
|
|
}
|
|
|
|
if(insertion_point != pending_events_.end()) {
|
|
|
|
insertion_point->delay -= delay;
|
|
|
|
}
|
|
|
|
pending_events_.emplace(insertion_point, type, delay);
|
|
|
|
}
|
2019-10-05 01:34:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:18:08 +00:00
|
|
|
#endif /* Atari_ST_Video_hpp */
|