2016-12-11 02:07:52 +00:00
|
|
|
//
|
|
|
|
// Video.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/12/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-12-11 02:07:52 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Video.hpp"
|
|
|
|
|
2017-11-11 01:37:18 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2016-12-11 02:07:52 +00:00
|
|
|
using namespace Electron;
|
|
|
|
|
2016-12-11 23:34:49 +00:00
|
|
|
#define graphics_line(v) ((((v) >> 7) - first_graphics_line + field_divider_line) % field_divider_line)
|
|
|
|
#define graphics_column(v) ((((v) & 127) - first_graphics_cycle + 128) & 127)
|
|
|
|
|
2016-12-11 02:07:52 +00:00
|
|
|
namespace {
|
2019-12-22 18:42:24 +00:00
|
|
|
constexpr int cycles_per_line = 128;
|
|
|
|
constexpr int lines_per_frame = 625;
|
|
|
|
constexpr int cycles_per_frame = lines_per_frame * cycles_per_line;
|
|
|
|
constexpr int crt_cycles_multiplier = 8;
|
|
|
|
constexpr int crt_cycles_per_line = crt_cycles_multiplier * cycles_per_line;
|
|
|
|
|
|
|
|
constexpr int field_divider_line = 312; // i.e. the line, simultaneous with which, the first field's sync ends. So if
|
|
|
|
// the first line with pixels in field 1 is the 20th in the frame, the first line
|
|
|
|
// with pixels in field 2 will be 20+field_divider_line
|
|
|
|
constexpr int first_graphics_line = 31;
|
|
|
|
constexpr int first_graphics_cycle = 33;
|
|
|
|
|
|
|
|
constexpr int display_end_interrupt_line = 256;
|
|
|
|
|
|
|
|
constexpr int real_time_clock_interrupt_1 = 16704;
|
|
|
|
constexpr int real_time_clock_interrupt_2 = 56704;
|
|
|
|
constexpr int display_end_interrupt_1 = (first_graphics_line + display_end_interrupt_line)*cycles_per_line;
|
|
|
|
constexpr int display_end_interrupt_2 = (first_graphics_line + field_divider_line + display_end_interrupt_line)*cycles_per_line;
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Lifecycle
|
2016-12-16 00:49:25 +00:00
|
|
|
|
2018-11-15 02:52:57 +00:00
|
|
|
VideoOutput::VideoOutput(uint8_t *memory) :
|
|
|
|
ram_(memory),
|
|
|
|
crt_(crt_cycles_per_line,
|
|
|
|
1,
|
|
|
|
Outputs::Display::Type::PAL50,
|
|
|
|
Outputs::Display::InputDataType::Red1Green1Blue1) {
|
2016-12-11 02:07:52 +00:00
|
|
|
memset(palette_, 0xf, sizeof(palette_));
|
2016-12-15 23:07:46 +00:00
|
|
|
setup_screen_map();
|
2016-12-23 03:46:02 +00:00
|
|
|
setup_base_address();
|
2016-12-15 23:07:46 +00:00
|
|
|
|
2016-12-11 02:07:52 +00:00
|
|
|
// TODO: as implied below, I've introduced a clock's latency into the graphics pipeline somehow. Investigate.
|
2018-11-15 02:52:57 +00:00
|
|
|
crt_.set_visible_area(crt_.get_rect_for_area(first_graphics_line - 1, 256, (first_graphics_cycle+1) * crt_cycles_multiplier, 80 * crt_cycles_multiplier, 4.0f / 3.0f));
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 02:52:57 +00:00
|
|
|
void VideoOutput::set_scan_target(Outputs::Display::ScanTarget *scan_target) {
|
|
|
|
crt_.set_scan_target(scan_target);
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 03:28:25 +00:00
|
|
|
Outputs::Display::ScanStatus VideoOutput::get_scaled_scan_status() const {
|
|
|
|
return crt_.get_scaled_scan_status() / float(crt_cycles_multiplier);
|
2020-01-21 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 04:44:21 +00:00
|
|
|
void VideoOutput::set_display_type(Outputs::Display::DisplayType display_type) {
|
|
|
|
crt_.set_display_type(display_type);
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Display update methods
|
2016-12-16 00:49:25 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void VideoOutput::start_pixel_line() {
|
2016-12-11 02:07:52 +00:00
|
|
|
current_pixel_line_ = (current_pixel_line_+1)&255;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!current_pixel_line_) {
|
2016-12-11 02:07:52 +00:00
|
|
|
start_line_address_ = start_screen_address_;
|
|
|
|
current_character_row_ = 0;
|
|
|
|
is_blank_line_ = false;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-11 02:07:52 +00:00
|
|
|
bool mode_has_blank_lines = (screen_mode_ == 6) || (screen_mode_ == 3);
|
|
|
|
is_blank_line_ = (mode_has_blank_lines && ((current_character_row_ > 7 && current_character_row_ < 10) || (current_pixel_line_ > 249)));
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!is_blank_line_) {
|
2016-12-11 02:07:52 +00:00
|
|
|
start_line_address_++;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(current_character_row_ > 7) {
|
2016-12-11 02:07:52 +00:00
|
|
|
start_line_address_ += ((screen_mode_ < 4) ? 80 : 40) * 8 - 8;
|
|
|
|
current_character_row_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current_screen_address_ = start_line_address_;
|
|
|
|
current_pixel_column_ = 0;
|
|
|
|
initial_output_target_ = current_output_target_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void VideoOutput::end_pixel_line() {
|
2018-11-26 03:04:04 +00:00
|
|
|
const int data_length = int(current_output_target_ - initial_output_target_);
|
2018-11-27 03:36:22 +00:00
|
|
|
if(data_length) {
|
|
|
|
crt_.output_data(data_length * current_output_divider_, size_t(data_length));
|
|
|
|
}
|
2016-12-11 02:07:52 +00:00
|
|
|
current_character_row_++;
|
|
|
|
}
|
|
|
|
|
2018-11-03 23:58:44 +00:00
|
|
|
void VideoOutput::output_pixels(int number_of_cycles) {
|
2016-12-11 02:07:52 +00:00
|
|
|
if(!number_of_cycles) return;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_blank_line_) {
|
2018-11-15 02:52:57 +00:00
|
|
|
crt_.output_blank(number_of_cycles * crt_cycles_multiplier);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2018-11-03 23:58:44 +00:00
|
|
|
int divider = 1;
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(screen_mode_) {
|
2018-09-13 00:25:30 +00:00
|
|
|
case 0: case 3: divider = 1; break;
|
|
|
|
case 1: case 4: case 6: divider = 2; break;
|
|
|
|
case 2: case 5: divider = 4; break;
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!initial_output_target_ || divider != current_output_divider_) {
|
2018-11-26 03:04:04 +00:00
|
|
|
const int data_length = int(current_output_target_ - initial_output_target_);
|
2018-11-27 03:36:22 +00:00
|
|
|
if(data_length) {
|
|
|
|
crt_.output_data(data_length * current_output_divider_, size_t(data_length));
|
|
|
|
}
|
2016-12-11 02:07:52 +00:00
|
|
|
current_output_divider_ = divider;
|
2018-11-15 02:52:57 +00:00
|
|
|
initial_output_target_ = current_output_target_ = crt_.begin_data(size_t(640 / current_output_divider_), size_t(8 / divider));
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define get_pixel() \
|
2017-03-26 18:34:47 +00:00
|
|
|
if(current_screen_address_&32768) {\
|
2016-12-11 02:07:52 +00:00
|
|
|
current_screen_address_ = (screen_mode_base_address_ + current_screen_address_)&32767;\
|
|
|
|
}\
|
|
|
|
last_pixel_byte_ = ram_[current_screen_address_];\
|
|
|
|
current_screen_address_ = current_screen_address_+8
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(screen_mode_) {
|
2016-12-11 02:07:52 +00:00
|
|
|
case 0: case 3:
|
2017-03-26 18:34:47 +00:00
|
|
|
if(initial_output_target_) {
|
|
|
|
while(number_of_cycles--) {
|
2016-12-11 02:07:52 +00:00
|
|
|
get_pixel();
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint64_t *>(current_output_target_) = palette_tables_.eighty1bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 8;
|
2016-12-11 02:07:52 +00:00
|
|
|
current_pixel_column_++;
|
|
|
|
}
|
2018-11-27 03:36:22 +00:00
|
|
|
} else current_output_target_ += 8*number_of_cycles;
|
2016-12-11 02:07:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2017-03-26 18:34:47 +00:00
|
|
|
if(initial_output_target_) {
|
|
|
|
while(number_of_cycles--) {
|
2016-12-11 02:07:52 +00:00
|
|
|
get_pixel();
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint32_t *>(current_output_target_) = palette_tables_.eighty2bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 4;
|
2016-12-11 02:07:52 +00:00
|
|
|
current_pixel_column_++;
|
|
|
|
}
|
2018-11-27 03:36:22 +00:00
|
|
|
} else current_output_target_ += 4*number_of_cycles;
|
2016-12-11 02:07:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2017-03-26 18:34:47 +00:00
|
|
|
if(initial_output_target_) {
|
|
|
|
while(number_of_cycles--) {
|
2016-12-11 02:07:52 +00:00
|
|
|
get_pixel();
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.eighty4bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 2;
|
2016-12-11 02:07:52 +00:00
|
|
|
current_pixel_column_++;
|
|
|
|
}
|
2018-11-27 03:36:22 +00:00
|
|
|
} else current_output_target_ += 2*number_of_cycles;
|
2016-12-11 02:07:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: case 6:
|
2017-03-26 18:34:47 +00:00
|
|
|
if(initial_output_target_) {
|
|
|
|
if(current_pixel_column_&1) {
|
2016-12-11 02:07:52 +00:00
|
|
|
last_pixel_byte_ <<= 4;
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint32_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 4;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
|
|
number_of_cycles--;
|
|
|
|
current_pixel_column_++;
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
while(number_of_cycles > 1) {
|
2016-12-11 02:07:52 +00:00
|
|
|
get_pixel();
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint32_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 4;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
|
|
last_pixel_byte_ <<= 4;
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint32_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 4;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
|
|
number_of_cycles -= 2;
|
|
|
|
current_pixel_column_+=2;
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
if(number_of_cycles) {
|
2016-12-11 02:07:52 +00:00
|
|
|
get_pixel();
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint32_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 4;
|
2016-12-11 02:07:52 +00:00
|
|
|
current_pixel_column_++;
|
|
|
|
}
|
2018-11-27 03:36:22 +00:00
|
|
|
} else current_output_target_ += 4 * number_of_cycles;
|
2016-12-11 02:07:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2017-03-26 18:34:47 +00:00
|
|
|
if(initial_output_target_) {
|
|
|
|
if(current_pixel_column_&1) {
|
2016-12-11 02:07:52 +00:00
|
|
|
last_pixel_byte_ <<= 2;
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 2;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
|
|
number_of_cycles--;
|
|
|
|
current_pixel_column_++;
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
while(number_of_cycles > 1) {
|
2016-12-11 02:07:52 +00:00
|
|
|
get_pixel();
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 2;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
|
|
last_pixel_byte_ <<= 2;
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 2;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
|
|
number_of_cycles -= 2;
|
|
|
|
current_pixel_column_+=2;
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
if(number_of_cycles) {
|
2016-12-11 02:07:52 +00:00
|
|
|
get_pixel();
|
2018-09-13 00:25:30 +00:00
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
current_output_target_ += 2;
|
2016-12-11 02:07:52 +00:00
|
|
|
current_pixel_column_++;
|
|
|
|
}
|
2018-11-27 03:36:22 +00:00
|
|
|
} else current_output_target_ += 2*number_of_cycles;
|
2016-12-11 02:07:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef get_pixel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void VideoOutput::run_for(const Cycles cycles) {
|
2019-10-30 02:36:29 +00:00
|
|
|
int number_of_cycles = int(cycles.as_integral());
|
2016-12-15 23:09:49 +00:00
|
|
|
output_position_ = (output_position_ + number_of_cycles) % cycles_per_frame;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(number_of_cycles) {
|
2016-12-15 23:07:46 +00:00
|
|
|
int draw_action_length = screen_map_[screen_map_pointer_].length;
|
|
|
|
int time_left_in_action = std::min(number_of_cycles, draw_action_length - cycles_into_draw_action_);
|
2018-11-03 23:58:44 +00:00
|
|
|
if(screen_map_[screen_map_pointer_].type == DrawAction::Pixels) output_pixels(time_left_in_action);
|
2016-12-11 02:07:52 +00:00
|
|
|
|
2016-12-15 23:07:46 +00:00
|
|
|
number_of_cycles -= time_left_in_action;
|
|
|
|
cycles_into_draw_action_ += time_left_in_action;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(cycles_into_draw_action_ == draw_action_length) {
|
|
|
|
switch(screen_map_[screen_map_pointer_].type) {
|
2018-11-15 02:52:57 +00:00
|
|
|
case DrawAction::Sync: crt_.output_sync(draw_action_length * crt_cycles_multiplier); break;
|
|
|
|
case DrawAction::ColourBurst: crt_.output_default_colour_burst(draw_action_length * crt_cycles_multiplier); break;
|
|
|
|
case DrawAction::Blank: crt_.output_blank(draw_action_length * crt_cycles_multiplier); break;
|
2018-11-03 23:58:44 +00:00
|
|
|
case DrawAction::Pixels: end_pixel_line(); break;
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
2016-12-15 23:07:46 +00:00
|
|
|
screen_map_pointer_ = (screen_map_pointer_ + 1) % screen_map_.size();
|
|
|
|
cycles_into_draw_action_ = 0;
|
|
|
|
if(screen_map_[screen_map_pointer_].type == DrawAction::Pixels) start_pixel_line();
|
2016-12-12 13:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 02:07:52 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Register hub
|
2016-12-16 00:49:25 +00:00
|
|
|
|
2020-01-05 18:40:02 +00:00
|
|
|
void VideoOutput::write(int address, uint8_t value) {
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(address & 0xf) {
|
2016-12-11 02:07:52 +00:00
|
|
|
case 0x02:
|
2017-10-22 01:50:53 +00:00
|
|
|
start_screen_address_ = (start_screen_address_ & 0xfe00) | static_cast<uint16_t>((value & 0xe0) << 1);
|
2016-12-11 02:07:52 +00:00
|
|
|
if(!start_screen_address_) start_screen_address_ |= 0x8000;
|
|
|
|
break;
|
|
|
|
case 0x03:
|
2017-10-22 01:50:53 +00:00
|
|
|
start_screen_address_ = (start_screen_address_ & 0x01ff) | static_cast<uint16_t>((value & 0x3f) << 9);
|
2016-12-11 02:07:52 +00:00
|
|
|
if(!start_screen_address_) start_screen_address_ |= 0x8000;
|
|
|
|
break;
|
2017-03-26 18:34:47 +00:00
|
|
|
case 0x07: {
|
2016-12-11 02:07:52 +00:00
|
|
|
// update screen mode
|
|
|
|
uint8_t new_screen_mode = (value >> 3)&7;
|
|
|
|
if(new_screen_mode == 7) new_screen_mode = 4;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(new_screen_mode != screen_mode_) {
|
2016-12-11 02:07:52 +00:00
|
|
|
screen_mode_ = new_screen_mode;
|
2016-12-23 03:46:02 +00:00
|
|
|
setup_base_address();
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x08: case 0x09: case 0x0a: case 0x0b:
|
2017-03-26 18:34:47 +00:00
|
|
|
case 0x0c: case 0x0d: case 0x0e: case 0x0f: {
|
2019-12-22 05:22:17 +00:00
|
|
|
constexpr int registers[4][4] = {
|
2016-12-11 02:07:52 +00:00
|
|
|
{10, 8, 2, 0},
|
|
|
|
{14, 12, 6, 4},
|
|
|
|
{15, 13, 7, 5},
|
|
|
|
{11, 9, 3, 1},
|
|
|
|
};
|
|
|
|
const int index = (address >> 1)&3;
|
|
|
|
const uint8_t colour = ~value;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(address&1) {
|
2016-12-11 02:07:52 +00:00
|
|
|
palette_[registers[index][0]] = (palette_[registers[index][0]]&3) | ((colour >> 1)&4);
|
|
|
|
palette_[registers[index][1]] = (palette_[registers[index][1]]&3) | ((colour >> 0)&4);
|
|
|
|
palette_[registers[index][2]] = (palette_[registers[index][2]]&3) | ((colour << 1)&4);
|
|
|
|
palette_[registers[index][3]] = (palette_[registers[index][3]]&3) | ((colour << 2)&4);
|
|
|
|
|
|
|
|
palette_[registers[index][2]] = (palette_[registers[index][2]]&5) | ((colour >> 4)&2);
|
|
|
|
palette_[registers[index][3]] = (palette_[registers[index][3]]&5) | ((colour >> 3)&2);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-11 02:07:52 +00:00
|
|
|
palette_[registers[index][0]] = (palette_[registers[index][0]]&6) | ((colour >> 7)&1);
|
|
|
|
palette_[registers[index][1]] = (palette_[registers[index][1]]&6) | ((colour >> 6)&1);
|
|
|
|
palette_[registers[index][2]] = (palette_[registers[index][2]]&6) | ((colour >> 5)&1);
|
|
|
|
palette_[registers[index][3]] = (palette_[registers[index][3]]&6) | ((colour >> 4)&1);
|
|
|
|
|
|
|
|
palette_[registers[index][0]] = (palette_[registers[index][0]]&5) | ((colour >> 2)&2);
|
|
|
|
palette_[registers[index][1]] = (palette_[registers[index][1]]&5) | ((colour >> 1)&2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// regenerate all palette tables for now
|
2017-03-26 18:34:47 +00:00
|
|
|
for(int byte = 0; byte < 256; byte++) {
|
2017-10-22 02:30:15 +00:00
|
|
|
uint8_t *target = reinterpret_cast<uint8_t *>(&palette_tables_.forty1bpp[byte]);
|
2018-09-13 00:25:30 +00:00
|
|
|
target[0] = palette_[(byte&0x80) >> 4];
|
|
|
|
target[1] = palette_[(byte&0x40) >> 3];
|
|
|
|
target[2] = palette_[(byte&0x20) >> 2];
|
|
|
|
target[3] = palette_[(byte&0x10) >> 1];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
2017-10-22 02:30:15 +00:00
|
|
|
target = reinterpret_cast<uint8_t *>(&palette_tables_.eighty2bpp[byte]);
|
2018-09-13 00:25:30 +00:00
|
|
|
target[0] = palette_[((byte&0x80) >> 4) | ((byte&0x08) >> 2)];
|
|
|
|
target[1] = palette_[((byte&0x40) >> 3) | ((byte&0x04) >> 1)];
|
|
|
|
target[2] = palette_[((byte&0x20) >> 2) | ((byte&0x02) >> 0)];
|
|
|
|
target[3] = palette_[((byte&0x10) >> 1) | ((byte&0x01) << 1)];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
2017-10-22 02:30:15 +00:00
|
|
|
target = reinterpret_cast<uint8_t *>(&palette_tables_.eighty1bpp[byte]);
|
2018-09-13 00:25:30 +00:00
|
|
|
target[0] = palette_[(byte&0x80) >> 4];
|
|
|
|
target[1] = palette_[(byte&0x40) >> 3];
|
|
|
|
target[2] = palette_[(byte&0x20) >> 2];
|
|
|
|
target[3] = palette_[(byte&0x10) >> 1];
|
|
|
|
target[4] = palette_[(byte&0x08) >> 0];
|
|
|
|
target[5] = palette_[(byte&0x04) << 1];
|
|
|
|
target[6] = palette_[(byte&0x02) << 2];
|
|
|
|
target[7] = palette_[(byte&0x01) << 3];
|
|
|
|
|
|
|
|
target = reinterpret_cast<uint8_t *>(&palette_tables_.forty2bpp[byte]);
|
|
|
|
target[0] = palette_[((byte&0x80) >> 4) | ((byte&0x08) >> 2)];
|
|
|
|
target[1] = palette_[((byte&0x40) >> 3) | ((byte&0x04) >> 1)];
|
|
|
|
|
|
|
|
target = reinterpret_cast<uint8_t *>(&palette_tables_.eighty4bpp[byte]);
|
|
|
|
target[0] = palette_[((byte&0x80) >> 4) | ((byte&0x20) >> 3) | ((byte&0x08) >> 2) | ((byte&0x02) >> 1)];
|
|
|
|
target[1] = palette_[((byte&0x40) >> 3) | ((byte&0x10) >> 2) | ((byte&0x04) >> 1) | ((byte&0x01) >> 0)];
|
2016-12-11 02:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 21:17:51 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void VideoOutput::setup_base_address() {
|
|
|
|
switch(screen_mode_) {
|
2016-12-23 03:46:02 +00:00
|
|
|
case 0: case 1: case 2: screen_mode_base_address_ = 0x3000; break;
|
|
|
|
case 3: screen_mode_base_address_ = 0x4000; break;
|
|
|
|
case 4: case 5: screen_mode_base_address_ = 0x5800; break;
|
|
|
|
case 6: screen_mode_base_address_ = 0x6000; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Interrupts
|
2016-12-11 21:17:51 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
VideoOutput::Interrupt VideoOutput::get_next_interrupt() {
|
2016-12-11 23:34:49 +00:00
|
|
|
VideoOutput::Interrupt interrupt;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(output_position_ < real_time_clock_interrupt_1) {
|
2016-12-11 23:34:49 +00:00
|
|
|
interrupt.cycles = real_time_clock_interrupt_1 - output_position_;
|
|
|
|
interrupt.interrupt = RealTimeClock;
|
|
|
|
return interrupt;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(output_position_ < display_end_interrupt_1) {
|
2016-12-11 23:34:49 +00:00
|
|
|
interrupt.cycles = display_end_interrupt_1 - output_position_;
|
|
|
|
interrupt.interrupt = DisplayEnd;
|
|
|
|
return interrupt;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(output_position_ < real_time_clock_interrupt_2) {
|
2016-12-11 23:34:49 +00:00
|
|
|
interrupt.cycles = real_time_clock_interrupt_2 - output_position_;
|
|
|
|
interrupt.interrupt = RealTimeClock;
|
|
|
|
return interrupt;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(output_position_ < display_end_interrupt_2) {
|
2016-12-11 23:34:49 +00:00
|
|
|
interrupt.cycles = display_end_interrupt_2 - output_position_;
|
|
|
|
interrupt.interrupt = DisplayEnd;
|
|
|
|
return interrupt;
|
|
|
|
}
|
|
|
|
|
|
|
|
interrupt.cycles = real_time_clock_interrupt_1 + cycles_per_frame - output_position_;
|
|
|
|
interrupt.interrupt = RealTimeClock;
|
|
|
|
return interrupt;
|
2016-12-11 21:17:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - RAM timing and access information
|
2016-12-11 23:34:49 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
unsigned int VideoOutput::get_cycles_until_next_ram_availability(int from_time) {
|
2016-12-11 23:34:49 +00:00
|
|
|
unsigned int result = 0;
|
2017-11-23 21:06:05 +00:00
|
|
|
int position = (output_position_ + from_time) % cycles_per_frame;
|
2016-12-12 13:01:10 +00:00
|
|
|
|
2017-11-23 21:06:05 +00:00
|
|
|
// Apply the standard cost of aligning to the available 1Mhz of RAM bandwidth.
|
2016-12-12 13:01:10 +00:00
|
|
|
result += 1 + (position&1);
|
2017-11-23 21:06:05 +00:00
|
|
|
|
2018-05-13 19:43:03 +00:00
|
|
|
// In Modes 0-3 there is also a complete block on any access while pixels are being fetched.
|
2017-03-26 18:34:47 +00:00
|
|
|
if(screen_mode_ < 4) {
|
2016-12-15 23:52:16 +00:00
|
|
|
const int current_column = graphics_column(position + (position&1));
|
|
|
|
int current_line = graphics_line(position);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(current_column < 80 && current_line < 256) {
|
2017-11-23 21:06:05 +00:00
|
|
|
// Mode 3 is a further special case: in 'every ten line block', the final two aren't painted,
|
|
|
|
// so the CPU is allowed access. But the offset of the ten-line blocks depends on when the
|
|
|
|
// user switched into Mode 3, so that needs to be calculated relative to current output.
|
2017-03-26 18:34:47 +00:00
|
|
|
if(screen_mode_ == 3) {
|
2017-11-23 21:06:05 +00:00
|
|
|
// Get the line the display was on.
|
2016-12-15 23:52:16 +00:00
|
|
|
int output_position_line = graphics_line(output_position_);
|
2017-11-23 21:06:05 +00:00
|
|
|
|
|
|
|
int implied_row;
|
|
|
|
if(current_line >= output_position_line) {
|
|
|
|
// Get the number of lines since then if still in the same frame.
|
|
|
|
int lines_since_output_position = current_line - output_position_line;
|
2018-07-28 14:52:34 +00:00
|
|
|
|
2017-11-23 21:06:05 +00:00
|
|
|
// Therefore get the character row at the proposed time, modulo 10.
|
|
|
|
implied_row = (current_character_row_ + lines_since_output_position) % 10;
|
|
|
|
} else {
|
|
|
|
// If the frame has rolled over, the implied row is just related to the current line.
|
|
|
|
implied_row = current_line % 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode 3 ends after 250 lines, not the usual 256.
|
|
|
|
if(implied_row < 8 && current_line < 250) result += static_cast<unsigned int>(80 - current_column);
|
2016-12-15 23:52:16 +00:00
|
|
|
}
|
2017-10-21 23:49:04 +00:00
|
|
|
else result += static_cast<unsigned int>(80 - current_column);
|
2016-12-15 23:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 23:34:49 +00:00
|
|
|
return result;
|
2016-12-11 21:17:51 +00:00
|
|
|
}
|
2016-12-15 23:07:46 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
VideoOutput::Range VideoOutput::get_memory_access_range() {
|
2016-12-16 00:43:04 +00:00
|
|
|
// This can't be more specific than this without applying a lot more thought because of mixed modes:
|
|
|
|
// suppose a program runs half the screen in an 80-column mode then switches to 40 columns. Then the
|
|
|
|
// real end address will be at 128*80 + 128*40 after the original base, subject to wrapping that depends
|
|
|
|
// on where the overflow occurred. Assuming accesses may run from the lowest possible position through to
|
|
|
|
// the end of RAM is good enough for 95% of use cases however.
|
2016-12-16 00:20:14 +00:00
|
|
|
VideoOutput::Range range;
|
|
|
|
range.low_address = std::min(start_screen_address_, screen_mode_base_address_);
|
|
|
|
range.high_address = 0x8000;
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - The screen map
|
2016-12-15 23:07:46 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void VideoOutput::setup_screen_map() {
|
2016-12-15 23:07:46 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Odd field: Even field:
|
|
|
|
|
|
|
|
|--S--| -S-|
|
|
|
|
|--S--| |--S--|
|
|
|
|
|-S-B-| = 3 |--S--| = 2.5
|
|
|
|
|--B--| |--B--|
|
|
|
|
|--P--| |--P--|
|
|
|
|
|--B--| = 312 |--B--| = 312.5
|
|
|
|
|-B-
|
|
|
|
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
for(int c = 0; c < 2; c++) {
|
|
|
|
if(c&1) {
|
2016-12-15 23:07:46 +00:00
|
|
|
screen_map_.emplace_back(DrawAction::Sync, (cycles_per_line * 5) >> 1);
|
|
|
|
screen_map_.emplace_back(DrawAction::Blank, cycles_per_line >> 1);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-15 23:07:46 +00:00
|
|
|
screen_map_.emplace_back(DrawAction::Blank, cycles_per_line >> 1);
|
|
|
|
screen_map_.emplace_back(DrawAction::Sync, (cycles_per_line * 5) >> 1);
|
|
|
|
}
|
2017-07-22 01:52:08 +00:00
|
|
|
for(int l = 0; l < first_graphics_line - 3; l++) emplace_blank_line();
|
|
|
|
for(int l = 0; l < 256; l++) emplace_pixel_line();
|
|
|
|
for(int l = 256 + first_graphics_line; l < 312; l++) emplace_blank_line();
|
2016-12-15 23:07:46 +00:00
|
|
|
if(c&1) emplace_blank_line();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void VideoOutput::emplace_blank_line() {
|
2016-12-15 23:07:46 +00:00
|
|
|
screen_map_.emplace_back(DrawAction::Sync, 9);
|
|
|
|
screen_map_.emplace_back(DrawAction::ColourBurst, 24 - 9);
|
|
|
|
screen_map_.emplace_back(DrawAction::Blank, 128 - 24);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void VideoOutput::emplace_pixel_line() {
|
2016-12-15 23:07:46 +00:00
|
|
|
// output format is:
|
|
|
|
// 9 cycles: sync
|
|
|
|
// ... to 24 cycles: colour burst
|
|
|
|
// ... to first_graphics_cycle: blank
|
|
|
|
// ... for 80 cycles: pixels
|
|
|
|
// ... until end of line: blank
|
|
|
|
screen_map_.emplace_back(DrawAction::Sync, 9);
|
|
|
|
screen_map_.emplace_back(DrawAction::ColourBurst, 24 - 9);
|
|
|
|
screen_map_.emplace_back(DrawAction::Blank, first_graphics_cycle - 24);
|
|
|
|
screen_map_.emplace_back(DrawAction::Pixels, 80);
|
|
|
|
screen_map_.emplace_back(DrawAction::Blank, 48 - first_graphics_cycle);
|
|
|
|
}
|