2017-07-31 23:56:59 +00:00
|
|
|
//
|
|
|
|
// CRTC6845.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 31/07/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-07-31 23:56:59 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef CRTC6845_hpp
|
|
|
|
#define CRTC6845_hpp
|
|
|
|
|
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
|
|
|
|
|
|
|
#include <cstdint>
|
2017-08-01 00:14:46 +00:00
|
|
|
#include <cstdio>
|
2017-07-31 23:56:59 +00:00
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace Motorola::CRTC {
|
2017-07-31 23:56:59 +00:00
|
|
|
|
2017-08-01 00:14:46 +00:00
|
|
|
struct BusState {
|
2017-10-18 02:10:28 +00:00
|
|
|
bool display_enable = false;
|
|
|
|
bool hsync = false;
|
|
|
|
bool vsync = false;
|
|
|
|
bool cursor = false;
|
|
|
|
uint16_t refresh_address = 0;
|
|
|
|
uint16_t row_address = 0;
|
2023-12-05 03:02:38 +00:00
|
|
|
|
|
|
|
// Not strictly part of the bus state; provided because the partition between 6845 and bus handler
|
|
|
|
// doesn't quite hold up in some emulated systems where the two are integrated and share more state.
|
|
|
|
int field_count = 0;
|
2017-08-01 00:14:46 +00:00
|
|
|
};
|
|
|
|
|
2017-08-01 20:15:19 +00:00
|
|
|
class BusHandler {
|
|
|
|
public:
|
2017-08-26 17:56:23 +00:00
|
|
|
/*!
|
|
|
|
Performs the first phase of a 6845 bus cycle; this is the phase in which it is intended that
|
|
|
|
systems using the 6845 respect the bus state and produce pixels, sync or whatever they require.
|
|
|
|
*/
|
|
|
|
void perform_bus_cycle_phase1(const BusState &) {}
|
|
|
|
|
|
|
|
/*!
|
2018-05-13 19:34:31 +00:00
|
|
|
Performs the second phase of a 6845 bus cycle. Some bus state, including sync, is updated
|
2017-08-26 17:56:23 +00:00
|
|
|
directly after phase 1 and hence is visible to an observer during phase 2. Handlers may therefore
|
|
|
|
implement @c perform_bus_cycle_phase2 to be notified of the availability of that state without
|
|
|
|
having to wait until the next cycle has begun.
|
|
|
|
*/
|
|
|
|
void perform_bus_cycle_phase2(const BusState &) {}
|
2017-08-01 20:15:19 +00:00
|
|
|
};
|
|
|
|
|
2017-08-10 16:28:57 +00:00
|
|
|
enum Personality {
|
2017-08-27 22:10:07 +00:00
|
|
|
HD6845S, // Type 0 in CPC parlance. Zero-width HSYNC available, no status, programmable VSYNC length.
|
2017-08-30 02:16:40 +00:00
|
|
|
// Considered exactly identical to the UM6845, so this enum covers both.
|
2017-08-27 22:10:07 +00:00
|
|
|
UM6845R, // Type 1 in CPC parlance. Status register, fixed-length VSYNC.
|
|
|
|
MC6845, // Type 2. No status register, fixed-length VSYNC, no zero-length HSYNC.
|
|
|
|
AMS40226 // Type 3. Status is get register, fixed-length VSYNC, no zero-length HSYNC.
|
2017-08-10 16:28:57 +00:00
|
|
|
};
|
|
|
|
|
2023-12-04 14:45:32 +00:00
|
|
|
// https://www.pcjs.org/blog/2018/03/20/ advises that "the behavior of bits 5 and 6 [of register 10, the cursor start
|
|
|
|
// register is really card specific".
|
|
|
|
//
|
|
|
|
// This enum captures those specifics.
|
|
|
|
enum CursorType {
|
|
|
|
/// No cursor signal is generated.
|
|
|
|
None,
|
|
|
|
/// MDA style: 00 => symmetric blinking; 01 or 10 => no blinking; 11 => short on, long off.
|
|
|
|
MDA,
|
|
|
|
/// EGA style: ignore the bits completely.
|
|
|
|
EGA,
|
|
|
|
};
|
|
|
|
|
2017-08-27 22:10:07 +00:00
|
|
|
// TODO UM6845R and R12/R13; see http://www.cpcwiki.eu/index.php/CRTC#CRTC_Differences
|
|
|
|
|
2023-12-04 14:52:46 +00:00
|
|
|
template <class T, CursorType cursor_type> class CRTC6845 {
|
2017-07-31 23:56:59 +00:00
|
|
|
public:
|
2017-08-10 16:28:57 +00:00
|
|
|
|
2017-08-26 16:59:59 +00:00
|
|
|
CRTC6845(Personality p, T &bus_handler) noexcept :
|
2017-08-27 22:10:07 +00:00
|
|
|
personality_(p), bus_handler_(bus_handler), status_(0) {}
|
2017-08-01 00:14:46 +00:00
|
|
|
|
2017-08-23 01:54:48 +00:00
|
|
|
void select_register(uint8_t r) {
|
|
|
|
selected_register_ = r;
|
|
|
|
}
|
|
|
|
|
2017-08-27 22:13:55 +00:00
|
|
|
uint8_t get_status() {
|
2017-08-27 22:10:07 +00:00
|
|
|
switch(personality_) {
|
|
|
|
case UM6845R: return status_ | (bus_state_.vsync ? 0x20 : 0x00);
|
|
|
|
case AMS40226: return get_register();
|
|
|
|
default: return 0xff;
|
|
|
|
}
|
2017-08-23 01:54:48 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
2017-08-27 22:13:55 +00:00
|
|
|
uint8_t get_register() {
|
2017-08-27 22:10:07 +00:00
|
|
|
if(selected_register_ == 31) status_ &= ~0x80;
|
|
|
|
if(selected_register_ == 16 || selected_register_ == 17) status_ &= ~0x40;
|
|
|
|
|
|
|
|
if(personality_ == UM6845R && selected_register_ == 31) return dummy_register_;
|
2017-08-23 01:54:48 +00:00
|
|
|
if(selected_register_ < 12 || selected_register_ > 17) return 0xff;
|
|
|
|
return registers_[selected_register_];
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_register(uint8_t value) {
|
2023-12-04 20:54:56 +00:00
|
|
|
static constexpr uint8_t masks[] = {
|
2017-08-23 01:54:48 +00:00
|
|
|
0xff, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0x7f, 0x7f,
|
|
|
|
0xff, 0x1f, 0x7f, 0x1f, 0x3f, 0xff, 0x3f, 0xff
|
|
|
|
};
|
|
|
|
|
2017-08-30 02:16:40 +00:00
|
|
|
// Per CPC documentation, skew doesn't work on a "type 1 or 2", i.e. an MC6845 or a UM6845R.
|
|
|
|
if(selected_register_ == 8 && personality_ != UM6845R && personality_ != MC6845) {
|
|
|
|
switch((value >> 4)&3) {
|
|
|
|
default: display_skew_mask_ = 1; break;
|
|
|
|
case 1: display_skew_mask_ = 2; break;
|
|
|
|
case 2: display_skew_mask_ = 4; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-26 21:54:54 +00:00
|
|
|
if(selected_register_ < 16) {
|
2017-08-23 01:54:48 +00:00
|
|
|
registers_[selected_register_] = value & masks[selected_register_];
|
2017-08-26 21:54:54 +00:00
|
|
|
}
|
2017-08-27 22:10:07 +00:00
|
|
|
if(selected_register_ == 31 && personality_ == UM6845R) {
|
|
|
|
dummy_register_ = value;
|
|
|
|
}
|
2017-08-23 01:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void trigger_light_pen() {
|
|
|
|
registers_[17] = bus_state_.refresh_address & 0xff;
|
|
|
|
registers_[16] = bus_state_.refresh_address >> 8;
|
2017-08-27 22:10:07 +00:00
|
|
|
status_ |= 0x40;
|
2017-08-23 01:54:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 23:56:59 +00:00
|
|
|
void run_for(Cycles cycles) {
|
2019-10-30 02:36:29 +00:00
|
|
|
auto cyles_remaining = cycles.as_integral();
|
2017-08-01 00:14:46 +00:00
|
|
|
while(cyles_remaining--) {
|
2023-12-03 22:57:19 +00:00
|
|
|
// Check for end of visible characters.
|
2017-08-01 02:01:54 +00:00
|
|
|
if(character_counter_ == registers_[1]) {
|
2017-08-23 01:54:48 +00:00
|
|
|
// TODO: consider skew in character_is_visible_. Or maybe defer until perform_bus_cycle?
|
2017-08-01 19:29:37 +00:00
|
|
|
character_is_visible_ = false;
|
2017-08-23 01:54:48 +00:00
|
|
|
end_of_line_address_ = bus_state_.refresh_address;
|
2017-08-01 11:36:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 17:56:23 +00:00
|
|
|
perform_bus_cycle_phase1();
|
|
|
|
bus_state_.refresh_address = (bus_state_.refresh_address + 1) & 0x3fff;
|
2017-08-10 15:12:53 +00:00
|
|
|
|
2023-12-03 22:57:19 +00:00
|
|
|
bus_state_.cursor = is_cursor_line_ &&
|
2023-12-04 14:52:46 +00:00
|
|
|
bus_state_.refresh_address == (registers_[15] | (registers_[14] << 8));
|
2023-12-03 22:57:19 +00:00
|
|
|
|
|
|
|
// Check for end-of-line.
|
2017-08-23 01:54:48 +00:00
|
|
|
if(character_counter_ == registers_[0]) {
|
2017-08-10 15:12:53 +00:00
|
|
|
character_counter_ = 0;
|
2017-08-23 01:54:48 +00:00
|
|
|
do_end_of_line();
|
|
|
|
character_is_visible_ = true;
|
|
|
|
} else {
|
2023-12-03 22:57:19 +00:00
|
|
|
// Increment counter.
|
2017-08-23 01:54:48 +00:00
|
|
|
character_counter_++;
|
2017-08-01 02:01:54 +00:00
|
|
|
}
|
2017-08-26 17:56:23 +00:00
|
|
|
|
2023-12-03 22:57:19 +00:00
|
|
|
// Check for start of horizontal sync.
|
2017-08-26 21:22:48 +00:00
|
|
|
if(character_counter_ == registers_[2]) {
|
|
|
|
hsync_counter_ = 0;
|
|
|
|
bus_state_.hsync = true;
|
|
|
|
}
|
|
|
|
|
2023-12-03 22:57:19 +00:00
|
|
|
// Check for end of horizontal sync; note that a sync time of zero will result in an immediate
|
2017-08-27 22:11:40 +00:00
|
|
|
// cancellation of the plan to perform sync if this is an HD6845S or UM6845R; otherwise zero
|
|
|
|
// will end up counting as 16 as it won't be checked until after overflow.
|
2017-08-26 21:22:48 +00:00
|
|
|
if(bus_state_.hsync) {
|
2017-08-27 22:10:07 +00:00
|
|
|
switch(personality_) {
|
|
|
|
case HD6845S:
|
|
|
|
case UM6845R:
|
|
|
|
bus_state_.hsync = hsync_counter_ != (registers_[3] & 15);
|
|
|
|
hsync_counter_ = (hsync_counter_ + 1) & 15;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hsync_counter_ = (hsync_counter_ + 1) & 15;
|
|
|
|
bus_state_.hsync = hsync_counter_ != (registers_[3] & 15);
|
|
|
|
break;
|
|
|
|
}
|
2017-08-26 21:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 17:56:23 +00:00
|
|
|
perform_bus_cycle_phase2();
|
2017-08-01 00:14:46 +00:00
|
|
|
}
|
2017-07-31 23:56:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 16:59:59 +00:00
|
|
|
const BusState &get_bus_state() const {
|
|
|
|
return bus_state_;
|
|
|
|
}
|
|
|
|
|
2017-08-23 01:54:48 +00:00
|
|
|
private:
|
2017-08-26 17:56:23 +00:00
|
|
|
inline void perform_bus_cycle_phase1() {
|
2017-08-30 02:16:40 +00:00
|
|
|
// Skew theory of operation: keep a history of the last three states, and apply whichever is selected.
|
2020-05-10 03:00:39 +00:00
|
|
|
character_is_visible_shifter_ = (character_is_visible_shifter_ << 1) | unsigned(character_is_visible_);
|
|
|
|
bus_state_.display_enable = (int(character_is_visible_shifter_) & display_skew_mask_) && line_is_visible_;
|
2017-08-26 17:56:23 +00:00
|
|
|
bus_handler_.perform_bus_cycle_phase1(bus_state_);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void perform_bus_cycle_phase2() {
|
|
|
|
bus_handler_.perform_bus_cycle_phase2(bus_state_);
|
2017-07-31 23:56:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:54:48 +00:00
|
|
|
inline void do_end_of_line() {
|
2023-12-04 14:52:46 +00:00
|
|
|
if constexpr (cursor_type != CursorType::None) {
|
|
|
|
// Check for cursor disable.
|
|
|
|
// TODO: this is handled differently on the EGA, should I ever implement that.
|
|
|
|
is_cursor_line_ &= bus_state_.row_address != (registers_[11] & 0x1f);
|
|
|
|
}
|
2023-12-03 22:57:19 +00:00
|
|
|
|
|
|
|
// Check for end of vertical sync.
|
2017-08-26 01:26:01 +00:00
|
|
|
if(bus_state_.vsync) {
|
2017-08-26 16:31:19 +00:00
|
|
|
vsync_counter_ = (vsync_counter_ + 1) & 15;
|
2023-12-03 22:57:19 +00:00
|
|
|
// On the UM6845R and AMS40226, honour the programmed vertical sync time; on the other CRTCs
|
2017-08-27 22:11:40 +00:00
|
|
|
// always use a vertical sync count of 16.
|
2017-08-27 22:10:07 +00:00
|
|
|
switch(personality_) {
|
|
|
|
case HD6845S:
|
|
|
|
case AMS40226:
|
|
|
|
bus_state_.vsync = vsync_counter_ != (registers_[3] >> 4);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bus_state_.vsync = vsync_counter_ != 0;
|
|
|
|
break;
|
2017-08-23 01:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-31 23:56:59 +00:00
|
|
|
|
2017-08-23 01:54:48 +00:00
|
|
|
if(is_in_adjustment_period_) {
|
|
|
|
line_counter_++;
|
|
|
|
if(line_counter_ == registers_[5]) {
|
|
|
|
is_in_adjustment_period_ = false;
|
|
|
|
do_end_of_frame();
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-03 22:57:19 +00:00
|
|
|
// Advance vertical counter.
|
2017-08-23 01:54:48 +00:00
|
|
|
if(bus_state_.row_address == registers_[9]) {
|
|
|
|
bus_state_.row_address = 0;
|
|
|
|
line_address_ = end_of_line_address_;
|
|
|
|
|
2023-12-03 22:57:19 +00:00
|
|
|
// Check for entry into the overflow area.
|
2017-08-23 01:54:48 +00:00
|
|
|
if(line_counter_ == registers_[4]) {
|
|
|
|
if(registers_[5]) {
|
|
|
|
line_counter_ = 0;
|
|
|
|
is_in_adjustment_period_ = true;
|
|
|
|
} else {
|
|
|
|
do_end_of_frame();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
line_counter_ = (line_counter_ + 1) & 0x7f;
|
2017-08-27 02:58:16 +00:00
|
|
|
|
2023-12-03 22:57:19 +00:00
|
|
|
// Check for start of vertical sync.
|
2017-08-27 02:58:16 +00:00
|
|
|
if(line_counter_ == registers_[7]) {
|
|
|
|
bus_state_.vsync = true;
|
|
|
|
vsync_counter_ = 0;
|
|
|
|
}
|
|
|
|
|
2023-12-03 22:57:19 +00:00
|
|
|
// Check for end of visible lines.
|
2017-08-27 02:58:16 +00:00
|
|
|
if(line_counter_ == registers_[6]) {
|
|
|
|
line_is_visible_ = false;
|
|
|
|
}
|
2017-08-23 01:54:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bus_state_.row_address = (bus_state_.row_address + 1) & 0x1f;
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 23:56:59 +00:00
|
|
|
|
2017-08-23 01:54:48 +00:00
|
|
|
bus_state_.refresh_address = line_address_;
|
|
|
|
character_counter_ = 0;
|
|
|
|
character_is_visible_ = (registers_[1] != 0);
|
2023-12-03 22:57:19 +00:00
|
|
|
|
2023-12-04 14:52:46 +00:00
|
|
|
if constexpr (cursor_type != CursorType::None) {
|
|
|
|
// Check for cursor enable.
|
|
|
|
is_cursor_line_ |= bus_state_.row_address == (registers_[10] & 0x1f);
|
|
|
|
|
|
|
|
switch(cursor_type) {
|
2023-12-05 03:02:38 +00:00
|
|
|
// MDA-style blinking.
|
|
|
|
// https://retrocomputing.stackexchange.com/questions/27803/what-are-the-blinking-rates-of-the-caret-and-of-blinking-text-on-pc-graphics-car
|
|
|
|
// gives an 8/8 pattern for regular blinking though mode 11 is then just a guess.
|
2023-12-04 14:52:46 +00:00
|
|
|
case CursorType::MDA:
|
|
|
|
switch(registers_[10] >> 5) {
|
2023-12-05 03:02:38 +00:00
|
|
|
case 0b11: is_cursor_line_ &= (bus_state_.field_count & 8) < 3; break;
|
|
|
|
case 0b00: is_cursor_line_ &= bool(bus_state_.field_count & 8); break;
|
|
|
|
case 0b01: is_cursor_line_ = false; break;
|
|
|
|
case 0b10: is_cursor_line_ = true; break;
|
2023-12-04 14:52:46 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 19:00:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:54:48 +00:00
|
|
|
inline void do_end_of_frame() {
|
|
|
|
line_counter_ = 0;
|
|
|
|
line_is_visible_ = true;
|
2020-05-10 03:00:39 +00:00
|
|
|
line_address_ = uint16_t((registers_[12] << 8) | registers_[13]);
|
2017-08-23 01:54:48 +00:00
|
|
|
bus_state_.refresh_address = line_address_;
|
2023-12-05 03:02:38 +00:00
|
|
|
++bus_state_.field_count;
|
2017-08-14 16:42:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:28:57 +00:00
|
|
|
Personality personality_;
|
2017-08-01 00:14:46 +00:00
|
|
|
T &bus_handler_;
|
|
|
|
BusState bus_state_;
|
|
|
|
|
2017-10-18 02:10:28 +00:00
|
|
|
uint8_t registers_[18] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
uint8_t dummy_register_ = 0;
|
|
|
|
int selected_register_ = 0;
|
2017-08-01 02:01:54 +00:00
|
|
|
|
2017-10-18 02:10:28 +00:00
|
|
|
uint8_t character_counter_ = 0;
|
|
|
|
uint8_t line_counter_ = 0;
|
2017-08-01 02:01:54 +00:00
|
|
|
|
2017-10-18 02:10:28 +00:00
|
|
|
bool character_is_visible_ = false, line_is_visible_ = false;
|
2017-08-01 02:01:54 +00:00
|
|
|
|
2017-10-18 02:10:28 +00:00
|
|
|
int hsync_counter_ = 0;
|
|
|
|
int vsync_counter_ = 0;
|
|
|
|
bool is_in_adjustment_period_ = false;
|
2017-08-23 01:54:48 +00:00
|
|
|
|
2017-10-18 02:10:28 +00:00
|
|
|
uint16_t line_address_ = 0;
|
|
|
|
uint16_t end_of_line_address_ = 0;
|
|
|
|
uint8_t status_ = 0;
|
2017-08-30 02:16:40 +00:00
|
|
|
|
|
|
|
int display_skew_mask_ = 1;
|
2017-10-18 02:10:28 +00:00
|
|
|
unsigned int character_is_visible_shifter_ = 0;
|
2023-12-03 22:57:19 +00:00
|
|
|
|
|
|
|
bool is_cursor_line_ = false;
|
2017-07-31 23:56:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CRTC6845_hpp */
|