1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-26 08:49:37 +00:00
CLK/Machines/Acorn/Electron/Video.cpp

296 lines
8.9 KiB
C++
Raw Normal View History

//
// Video.cpp
// Clock Signal
//
// Created by Thomas Harte on 10/12/2016.
// Copyright 2016 Thomas Harte. All rights reserved.
//
#include "Video.hpp"
#include <cstring>
using namespace Electron;
// MARK: - Lifecycle
2016-12-16 00:49:25 +00:00
VideoOutput::VideoOutput(const uint8_t *memory) :
ram_(memory),
2024-09-09 00:57:43 +00:00
crt_(h_total,
1,
Outputs::Display::Type::PAL50,
Outputs::Display::InputDataType::Red1Green1Blue1) {
2024-09-07 02:12:19 +00:00
crt_.set_visible_area(crt_.get_rect_for_area(
312 - vsync_end,
256,
2024-09-09 01:06:59 +00:00
h_total - hsync_start,
2024-09-09 00:57:43 +00:00
80 * 8,
2024-09-07 02:12:19 +00:00
4.0f / 3.0f
));
}
void VideoOutput::set_scan_target(Outputs::Display::ScanTarget *scan_target) {
crt_.set_scan_target(scan_target);
}
Outputs::Display::ScanStatus VideoOutput::get_scaled_scan_status() const {
2024-09-07 01:36:05 +00:00
return crt_.get_scaled_scan_status();
}
2018-11-30 04:44:21 +00:00
void VideoOutput::set_display_type(Outputs::Display::DisplayType display_type) {
crt_.set_display_type(display_type);
}
Outputs::Display::DisplayType VideoOutput::get_display_type() const {
return crt_.get_display_type();
}
uint8_t VideoOutput::run_for(const Cycles cycles) {
uint8_t interrupts{};
int number_of_cycles = cycles.as<int>();
while(number_of_cycles--) {
2024-09-09 00:20:03 +00:00
// The below is my attempt at transcription of the equivalent VHDL code in moogway82's
// JamSoftElectronULA — https://github.com/moogway82/JamSoftElectronULA — which is itself
// derived from hoglet67's https://github.com/hoglet67/ElectronFpga and that author's
// reverse-engineering of the Electron ULA. It should therefore be as accurate to the
// original hardware as my comprehension of VHDL and adaptation into sequential code allows.
2024-09-09 01:06:59 +00:00
// In this, the sequential world of C++, all tests below should assume that the position
// named by (h_count_, v_count_) is the one that was active **prior to this cycle**.
//
// So this cycle spans the period from (h_count_, v_count_) to (h_count_, v_count_)+1.
// Test for interrupts.
2024-09-09 00:16:43 +00:00
if(v_count_ == v_rtc && ((!field_ && !h_count_) || (field_ && h_count_ == h_half))) {
interrupts |= static_cast<uint8_t>(Interrupt::RealTimeClock);
}
2024-09-09 00:16:43 +00:00
if(h_count_ == hsync_start && ((v_count_ == v_disp_gph && !mode_text_) or (v_count_ == v_disp_txt && mode_text_))) {
interrupts |= static_cast<uint8_t>(Interrupt::DisplayEnd);
}
// Update syncs.
2024-09-09 00:16:43 +00:00
if(!field_) {
if(!h_count_ && v_count_ == vsync_start) {
vsync_int_ = true;
} else if(h_count_ == h_half && v_count_ == vsync_end) {
vsync_int_ = false;
}
} else {
2024-09-09 00:16:43 +00:00
if(h_count_ == h_half && v_count_ == vsync_start) {
vsync_int_ = true;
} else if(!h_count_ && v_count_ == vsync_end + 1) {
vsync_int_ = false;
}
}
2024-09-09 00:16:43 +00:00
if(h_count_ == hsync_start) {
hsync_int_ = true;
} else if(h_count_ == hsync_end) {
hsync_int_ = false;
}
// Update character row on the trailing edge of hsync.
2024-09-09 00:16:43 +00:00
if(h_count_ == hsync_end) {
2024-09-09 01:06:59 +00:00
if(is_v_end()) {
2024-09-09 00:16:43 +00:00
char_row_ = 0;
} else {
2024-09-09 00:16:43 +00:00
char_row_ = last_line() ? 0 : char_row_ + 1;
}
}
// Disable the top bit of the char_row counter outside of text mode.
2024-09-09 00:16:43 +00:00
if(!mode_text_) {
char_row_ &= 7;
}
// Latch video address at frame start.
2024-09-09 01:06:59 +00:00
if(h_count_ == h_reset_addr && is_v_end()) {
2024-09-09 00:16:43 +00:00
row_addr_ = byte_addr_ = screen_base_;
}
// Copy byte_addr back into row_addr if a new character row has begun.
2024-09-09 00:16:43 +00:00
if(hsync_int_) {
if(last_line()) {
2024-09-09 00:16:43 +00:00
row_addr_ = byte_addr_;
} else {
2024-09-09 00:16:43 +00:00
byte_addr_ = row_addr_;
}
}
// Determine current output item.
OutputStage stage;
2024-09-07 01:01:30 +00:00
int screen_pitch = screen_pitch_;
2024-09-09 00:16:43 +00:00
if(vsync_int_ || hsync_int_) {
stage = OutputStage::Sync;
} else if(in_blank()) {
2024-09-09 01:12:45 +00:00
if(h_count_ >= hburst_start && h_count_ < hburst_end) {
stage = OutputStage::ColourBurst;
} else {
stage = OutputStage::Blank;
}
} else {
stage = OutputStage::Pixels;
2024-09-09 00:16:43 +00:00
screen_pitch = (mode_40_ ? 320 : 640) / static_cast<int>(mode_bpp_);
}
2024-09-07 01:01:30 +00:00
if(stage != output_ || screen_pitch != screen_pitch_) {
switch(output_) {
2024-09-09 01:12:45 +00:00
case OutputStage::Sync: crt_.output_sync(output_length_); break;
case OutputStage::Blank: crt_.output_blank(output_length_); break;
case OutputStage::ColourBurst: crt_.output_default_colour_burst(output_length_); break;
case OutputStage::Pixels:
if(current_output_target_) {
crt_.output_data(
output_length_,
static_cast<size_t>(current_output_target_ - initial_output_target_)
);
} else {
crt_.output_data(output_length_);
}
break;
}
output_length_ = 0;
output_ = stage;
2024-09-07 01:01:30 +00:00
screen_pitch_ = screen_pitch;
if(stage == OutputStage::Pixels) {
2024-09-07 01:01:30 +00:00
initial_output_target_ = current_output_target_ = crt_.begin_data(static_cast<size_t>(screen_pitch_));
}
}
2024-09-09 00:57:43 +00:00
output_length_ += 8;
2024-09-09 00:16:43 +00:00
if(output_ == OutputStage::Pixels && (!mode_40_ || h_count_ & 8) && current_output_target_) {
const uint8_t data = ram_[byte_addr_ | char_row_];
2024-09-09 00:16:43 +00:00
switch(mode_bpp_) {
2024-09-07 01:01:30 +00:00
case Bpp::One:
2024-09-07 01:36:05 +00:00
current_output_target_[0] = palette1bpp_[(data >> 7) & 1];
current_output_target_[1] = palette1bpp_[(data >> 6) & 1];
current_output_target_[2] = palette1bpp_[(data >> 5) & 1];
current_output_target_[3] = palette1bpp_[(data >> 4) & 1];
current_output_target_[4] = palette1bpp_[(data >> 3) & 1];
current_output_target_[5] = palette1bpp_[(data >> 2) & 1];
current_output_target_[6] = palette1bpp_[(data >> 1) & 1];
current_output_target_[7] = palette1bpp_[(data >> 0) & 1];
2024-09-07 01:01:30 +00:00
current_output_target_ += 8;
break;
case Bpp::Two:
2024-09-07 01:36:05 +00:00
current_output_target_[0] = palette2bpp_[((data >> 6) & 2) | ((data >> 3) & 1)];
current_output_target_[1] = palette2bpp_[((data >> 5) & 2) | ((data >> 2) & 1)];
current_output_target_[2] = palette2bpp_[((data >> 4) & 2) | ((data >> 1) & 1)];
current_output_target_[3] = palette2bpp_[((data >> 3) & 2) | ((data >> 0) & 1)];
2024-09-07 01:01:30 +00:00
current_output_target_ += 4;
break;
case Bpp::Four:
2024-09-07 01:36:05 +00:00
current_output_target_[0] = palette4bpp_[((data >> 4) & 8) | ((data >> 3) & 4) | ((data >> 2) & 2) | ((data >> 1) & 1)];
current_output_target_[1] = palette4bpp_[((data >> 3) & 8) | ((data >> 2) & 4) | ((data >> 1) & 2) | ((data >> 0) & 1)];
2024-09-07 01:01:30 +00:00
current_output_target_ += 2;
break;
}
}
// Increment the byte address across the line.
// (slghtly pained logic here because the input clock is still at the pixel rate, not the byte rate)
2024-09-09 00:16:43 +00:00
if(h_count_ < h_active) {
if(
2024-09-09 00:16:43 +00:00
(!mode_40_ && !(h_count_ & 0x7)) ||
(mode_40_ && ((h_count_ & 0xf) == 0x8))
) {
2024-09-09 00:16:43 +00:00
byte_addr_ += 8;
2024-09-09 00:16:43 +00:00
if(!(byte_addr_ & 0b0111'1000'0000'0000)) {
byte_addr_ = mode_base_ | (byte_addr_ & 0x0000'0111'1111'1111);
}
}
}
2024-09-09 01:06:59 +00:00
// Horizontal and vertical counter updates; code below should act
h_count_ += 8;
if(h_count_ == h_total) {
h_count_ = 0;
if(is_v_end()) {
v_count_ = 0;
field_ = !field_;
} else {
++v_count_;
}
}
}
return interrupts;
}
// MARK: - Register hub
2016-12-16 00:49:25 +00:00
void VideoOutput::write(int address, uint8_t value) {
2024-09-07 01:36:05 +00:00
address &= 0xf;
switch(address) {
case 0x02:
2024-09-09 00:16:43 +00:00
screen_base_ =
(screen_base_ & 0b0111'1110'0000'0000) |
((value << 1) & 0b0000'0001'1100'0000);
break;
2024-09-07 00:55:26 +00:00
case 0x03:
2024-09-09 00:16:43 +00:00
screen_base_ =
2024-09-07 00:55:26 +00:00
((value << 9) & 0b0111'1110'0000'0000) |
2024-09-09 00:16:43 +00:00
(screen_base_ & 0b0000'0001'1100'0000);
2024-09-07 00:55:26 +00:00
break;
2017-03-26 18:34:47 +00:00
case 0x07: {
uint8_t mode = (value >> 3)&7;
2024-09-09 00:16:43 +00:00
mode_40_ = mode >= 4;
mode_text_ = mode == 3 || mode == 6;
switch(mode) {
case 0:
case 1:
2024-09-09 00:16:43 +00:00
case 2: mode_base_ = 0x3000; break;
case 3: mode_base_ = 0x4000; break;
case 6: mode_base_ = 0x6000; break;
default: mode_base_ = 0x5800; break;
}
switch(mode) {
2024-09-09 00:16:43 +00:00
default: mode_bpp_ = Bpp::One; break;
case 1:
2024-09-09 00:16:43 +00:00
case 5: mode_bpp_ = Bpp::Two; break;
case 2: mode_bpp_ = Bpp::Four; break;
}
} break;
case 0x08: case 0x09: case 0x0a: case 0x0b:
case 0x0c: case 0x0d: case 0x0e: case 0x0f: {
2024-09-07 01:36:05 +00:00
palette_[address - 8] = ~value;
if(address <= 0x09) {
palette1bpp_[0] = palette_entry<1, 0, 1, 4, 0, 4>();
palette1bpp_[1] = palette_entry<1, 2, 0, 6, 0, 2>();
palette2bpp_[0] = palette_entry<1, 0, 1, 4, 0, 4>();
palette2bpp_[1] = palette_entry<1, 1, 1, 5, 0, 5>();
2024-09-07 01:47:13 +00:00
palette2bpp_[2] = palette_entry<1, 2, 0, 2, 0, 6>();
2024-09-07 01:36:05 +00:00
palette2bpp_[3] = palette_entry<1, 3, 0, 3, 0, 7>();
}
palette4bpp_[0] = palette_entry<1, 0, 1, 4, 0, 4>();
palette4bpp_[2] = palette_entry<1, 1, 1, 5, 0, 5>();
2024-09-07 01:47:13 +00:00
palette4bpp_[8] = palette_entry<1, 2, 0, 2, 0, 6>();
2024-09-07 01:36:05 +00:00
palette4bpp_[10] = palette_entry<1, 3, 0, 3, 0, 7>();
palette4bpp_[4] = palette_entry<3, 0, 3, 4, 2, 4>();
palette4bpp_[6] = palette_entry<3, 1, 3, 5, 2, 5>();
2024-09-07 01:47:13 +00:00
palette4bpp_[12] = palette_entry<3, 2, 2, 2, 2, 6>();
2024-09-07 01:36:05 +00:00
palette4bpp_[14] = palette_entry<3, 3, 2, 3, 2, 7>();
2024-09-07 01:47:13 +00:00
palette4bpp_[5] = palette_entry<5, 0, 5, 4, 4, 4>();
palette4bpp_[7] = palette_entry<5, 1, 5, 5, 4, 5>();
palette4bpp_[13] = palette_entry<5, 2, 4, 2, 4, 6>();
palette4bpp_[15] = palette_entry<5, 3, 4, 3, 4, 7>();
2024-09-07 01:36:05 +00:00
palette4bpp_[1] = palette_entry<7, 0, 7, 4, 6, 4>();
palette4bpp_[3] = palette_entry<7, 1, 7, 5, 6, 5>();
2024-09-07 01:47:13 +00:00
palette4bpp_[9] = palette_entry<7, 2, 6, 2, 6, 6>();
2024-09-07 01:36:05 +00:00
palette4bpp_[11] = palette_entry<7, 3, 6, 3, 6, 7>();
} break;
}
}