Files
CLK/Components/6847/6847.cpp
T

169 lines
8.0 KiB
C++

//
// 6847.cpp
// Clock Signal
//
// Created by Thomas Harte on 04/05/2026.
// Copyright © 2026 Thomas Harte. All rights reserved.
//
#include "6847.hpp"
#include <cstdint>
namespace {
static constexpr uint8_t font[64][12] = {
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x02, 0x1a, 0x2a, 0x2a, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x08, 0x14, 0x22, 0x22, 0x3e, 0x22, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3c, 0x12, 0x12, 0x1c, 0x12, 0x12, 0x3c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x20, 0x20, 0x20, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3c, 0x12, 0x12, 0x12, 0x12, 0x12, 0x3c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3e, 0x20, 0x20, 0x3c, 0x20, 0x20, 0x3e, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3e, 0x20, 0x20, 0x3c, 0x20, 0x20, 0x20, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1e, 0x20, 0x20, 0x26, 0x22, 0x22, 0x1e, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x3e, 0x22, 0x22, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x08, 0x08, 0x08, 0x08, 0x08, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x22, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x24, 0x28, 0x30, 0x28, 0x24, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3e, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x36, 0x2a, 0x2a, 0x22, 0x22, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x32, 0x2a, 0x26, 0x22, 0x22, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3e, 0x22, 0x22, 0x22, 0x22, 0x22, 0x3e, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3c, 0x22, 0x22, 0x3c, 0x20, 0x20, 0x20, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x2a, 0x24, 0x1a, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3c, 0x22, 0x22, 0x3c, 0x28, 0x24, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x10, 0x08, 0x04, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x2a, 0x2a, 0x36, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x22, 0x14, 0x08, 0x14, 0x22, 0x22, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x22, 0x22, 0x14, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3e, 0x02, 0x04, 0x08, 0x10, 0x20, 0x3e, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x20, 0x20, 0x10, 0x08, 0x04, 0x02, 0x02, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x0e, 0x02, 0x02, 0x02, 0x02, 0x02, 0x0e, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x08, 0x1c, 0x2a, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x3e, 0x10, 0x08, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x14, 0x14, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x14, 0x14, 0x36, 0x00, 0x36, 0x14, 0x14, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x08, 0x1e, 0x20, 0x1c, 0x02, 0x3c, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x32, 0x32, 0x04, 0x08, 0x10, 0x26, 0x26, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x10, 0x28, 0x28, 0x10, 0x2a, 0x24, 0x1a, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x08, 0x10, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x08, 0x04, 0x02, 0x02, 0x02, 0x04, 0x08, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x08, 0x1c, 0x3e, 0x1c, 0x08, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x3e, 0x08, 0x08, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x10, 0x20, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x02, 0x02, 0x04, 0x08, 0x10, 0x20, 0x20, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x18, 0x24, 0x24, 0x24, 0x24, 0x24, 0x18, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x08, 0x18, 0x08, 0x08, 0x08, 0x08, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x02, 0x1c, 0x20, 0x20, 0x3e, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x02, 0x0c, 0x02, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x04, 0x0c, 0x14, 0x3e, 0x04, 0x04, 0x04, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3e, 0x20, 0x3c, 0x02, 0x02, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x20, 0x20, 0x3c, 0x22, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x3e, 0x02, 0x04, 0x08, 0x10, 0x20, 0x20, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x1c, 0x22, 0x22, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x1c, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x04, 0x08, 0x10, 0x20, 0x10, 0x08, 0x04, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x10, 0x08, 0x04, 0x02, 0x04, 0x08, 0x10, 0x00, 0x00, },
{ 0x00, 0x00, 0x00, 0x18, 0x24, 0x04, 0x08, 0x08, 0x00, 0x08, 0x00, 0x00, },
};
static constexpr int CRTMultiplier = 14;
}
using namespace Motorola::MC6847;
// TODO: the real 6847 is in-phase, but I don't currently know whether it's 228 colour cycles per line or 227.
// So the below isn't quite accurate. This is something to figure out.
MC6847Base::MC6847Base(const Outputs::Display::Type display_type) :
crt_(
910,
1,
display_type,
Outputs::Display::InputDataType::Red4Green4Blue4 // TODO.
) {
crt_.set_display_type(Outputs::Display::DisplayType::CompositeColour);
}
void MC6847Base::pixel_line(const int line, const int line_begin, const int line_end) {
Numeric::clamp<0, LineLayout::EndOfSync>(line_begin, line_end, [&](int, const int end) {
if(end == LineLayout::EndOfSync) {
crt_.output_sync(LineLayout::EndOfSync * CRTMultiplier);
}
});
Numeric::clamp<LineLayout::EndOfSync, LineLayout::EndOfLeftBorder>(line_begin, line_end, [&](int, const int end) {
if(end == LineLayout::EndOfLeftBorder) {
crt_.output_blank((LineLayout::EndOfLeftBorder - LineLayout::EndOfSync) * CRTMultiplier);
}
});
Numeric::clamp<LineLayout::EndOfLeftBorder, LineLayout::EndOfPixels>(
line_begin,
line_end,
[&](const int begin, const int end) {
if(begin == LineLayout::EndOfLeftBorder) {
pixels_ = reinterpret_cast<uint16_t *>(crt_.begin_data(256));
}
// TODO: obey graphics mode here, use proper colours, more.
if(pixels_) {
const int row = address_.row();
for(int c = line_begin; c < line_end; c++) {
const int column = c - LineLayout::EndOfLeftBorder;
const int pixels = font[line_.data[column] & 63][row];
pixels_[0] = (pixels & 0x80) ? 0xffff : 0x0000;
pixels_[1] = (pixels & 0x40) ? 0xffff : 0x0000;
pixels_[2] = (pixels & 0x20) ? 0xffff : 0x0000;
pixels_[3] = (pixels & 0x10) ? 0xffff : 0x0000;
pixels_[4] = (pixels & 0x08) ? 0xffff : 0x0000;
pixels_[5] = (pixels & 0x04) ? 0xffff : 0x0000;
pixels_[6] = (pixels & 0x02) ? 0xffff : 0x0000;
pixels_[7] = (pixels & 0x01) ? 0xffff : 0x0000;
pixels_ += 8;
}
}
if(end == LineLayout::EndOfPixels) {
crt_.output_data(32 * CRTMultiplier, 256);
address_.apply_hsync();
printf("\n");
}
}
);
Numeric::clamp<LineLayout::EndOfPixels, LineLayout::EndOfLine>(line_begin, line_end, [&](int, const int end) {
if(end == LineLayout::EndOfLine) {
crt_.output_blank((LineLayout::EndOfLine - LineLayout::EndOfPixels) * CRTMultiplier);
}
});
}
void MC6847Base::border_line(const int, const int end) {
if(end == LineLayout::EndOfLine) {
crt_.output_sync(LineLayout::EndOfSync * CRTMultiplier);
crt_.output_blank((LineLayout::EndOfLine - LineLayout::EndOfSync) * CRTMultiplier);
}
}
void MC6847Base::porch_line(const int begin, const int end) {
border_line(begin, end);
}
void MC6847Base::sync_line(const int, const int end) {
if(end == LineLayout::EndOfLine) {
crt_.output_sync((LineLayout::EndOfLine) * CRTMultiplier);
}
}
void MC6847Base::reset() {
address_.apply_vertical_preload();
}