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 {
|
2016-12-12 13:01:10 +00:00
|
|
|
|
static const int cycles_per_line = 128;
|
|
|
|
|
static const int lines_per_frame = 625;
|
2016-12-11 21:17:51 +00:00
|
|
|
|
static const int cycles_per_frame = lines_per_frame * cycles_per_line;
|
2016-12-12 13:01:10 +00:00
|
|
|
|
static const int crt_cycles_multiplier = 8;
|
|
|
|
|
static const int crt_cycles_per_line = crt_cycles_multiplier * cycles_per_line;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
2016-12-12 13:01:10 +00:00
|
|
|
|
static const int field_divider_line = 312; // i.e. the line, simultaneous with which, the first field's sync ends. So if
|
2016-12-16 00:49:25 +00:00
|
|
|
|
// 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
|
2016-12-12 13:01:10 +00:00
|
|
|
|
static const int first_graphics_line = 31;
|
|
|
|
|
static const int first_graphics_cycle = 33;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
2016-12-11 23:34:49 +00:00
|
|
|
|
static const int display_end_interrupt_line = 256;
|
2016-12-11 02:07:52 +00:00
|
|
|
|
|
2016-12-11 23:34:49 +00:00
|
|
|
|
static const int real_time_clock_interrupt_1 = 16704;
|
|
|
|
|
static const int real_time_clock_interrupt_2 = 56704;
|
|
|
|
|
static const int display_end_interrupt_1 = (first_graphics_line + display_end_interrupt_line)*cycles_per_line;
|
|
|
|
|
static const int display_end_interrupt_2 = (first_graphics_line + field_divider_line + display_end_interrupt_line)*cycles_per_line;
|
2017-10-23 22:35:37 +00:00
|
|
|
|
|
|
|
|
|
struct FourBPPBookender: public Outputs::CRT::TextureBuilder::Bookender {
|
|
|
|
|
void add_bookends(uint8_t *const left_value, uint8_t *const right_value, uint8_t *left_bookend, uint8_t *right_bookend) {
|
|
|
|
|
*left_bookend = static_cast<uint8_t>(((*left_value) & 0x0f) | (((*left_value) & 0x0f) << 4));
|
|
|
|
|
*right_bookend = static_cast<uint8_t>(((*right_value) & 0xf0) | (((*right_value) & 0xf0) >> 4));
|
|
|
|
|
}
|
|
|
|
|
};
|
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
|
|
|
|
|
2017-09-01 02:29:24 +00:00
|
|
|
|
VideoOutput::VideoOutput(uint8_t *memory) : ram_(memory) {
|
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
|
|
|
|
crt_.reset(new Outputs::CRT::CRT(crt_cycles_per_line, 8, Outputs::CRT::DisplayType::PAL50, 1));
|
|
|
|
|
crt_->set_rgb_sampling_function(
|
|
|
|
|
"vec3 rgb_sample(usampler2D sampler, vec2 coordinate, vec2 icoordinate)"
|
|
|
|
|
"{"
|
|
|
|
|
"uint texValue = texture(sampler, coordinate).r;"
|
2018-04-18 23:29:03 +00:00
|
|
|
|
"texValue >>= 4 - (int(icoordinate.x) & 4);"
|
2016-12-11 02:07:52 +00:00
|
|
|
|
"return vec3( uvec3(texValue) & uvec3(4u, 2u, 1u));"
|
|
|
|
|
"}");
|
2018-04-18 23:29:03 +00:00
|
|
|
|
crt_->set_integer_coordinate_multiplier(8.0f);
|
2017-10-23 22:35:37 +00:00
|
|
|
|
std::unique_ptr<Outputs::CRT::TextureBuilder::Bookender> bookender(new FourBPPBookender);
|
|
|
|
|
crt_->set_bookender(std::move(bookender));
|
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.
|
|
|
|
|
crt_->set_visible_area(crt_->get_rect_for_area(first_graphics_line - 3, 256, (first_graphics_cycle+1) * crt_cycles_multiplier, 80 * crt_cycles_multiplier, 4.0f / 3.0f));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
|
// MARK: - CRT getter
|
2016-12-16 00:49:25 +00:00
|
|
|
|
|
2017-12-18 02:26:06 +00:00
|
|
|
|
Outputs::CRT::CRT *VideoOutput::get_crt() {
|
|
|
|
|
return crt_.get();
|
2016-12-11 02:07:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-04-17 00:00:56 +00:00
|
|
|
|
if(current_output_target_) {
|
|
|
|
|
const unsigned int data_length = static_cast<unsigned int>(current_output_target_ - initial_output_target_);
|
|
|
|
|
crt_->output_data(data_length * current_output_divider_, data_length);
|
|
|
|
|
}
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_character_row_++;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
|
void VideoOutput::output_pixels(unsigned 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_) {
|
2016-12-11 02:07:52 +00:00
|
|
|
|
crt_->output_blank(number_of_cycles * crt_cycles_multiplier);
|
2017-03-26 18:34:47 +00:00
|
|
|
|
} else {
|
2017-02-11 18:35:22 +00:00
|
|
|
|
unsigned int divider = 1;
|
2017-03-26 18:34:47 +00:00
|
|
|
|
switch(screen_mode_) {
|
2016-12-11 02:07:52 +00:00
|
|
|
|
case 0: case 3: divider = 2; break;
|
|
|
|
|
case 1: case 4: case 6: divider = 4; break;
|
|
|
|
|
case 2: case 5: divider = 8; break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
|
if(!initial_output_target_ || divider != current_output_divider_) {
|
2018-04-17 00:00:56 +00:00
|
|
|
|
if(current_output_target_) {
|
|
|
|
|
const unsigned int data_length = static_cast<unsigned int>(current_output_target_ - initial_output_target_);
|
|
|
|
|
crt_->output_data(data_length * current_output_divider_, data_length);
|
|
|
|
|
}
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_output_divider_ = divider;
|
2017-10-18 02:09:48 +00:00
|
|
|
|
initial_output_target_ = current_output_target_ = crt_->allocate_write_area(640 / current_output_divider_, 4);
|
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();
|
2017-10-22 02:30:15 +00:00
|
|
|
|
*reinterpret_cast<uint32_t *>(current_output_target_) = palette_tables_.eighty1bpp[last_pixel_byte_];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_output_target_ += 4;
|
|
|
|
|
current_pixel_column_++;
|
|
|
|
|
}
|
|
|
|
|
} else current_output_target_ += 4*number_of_cycles;
|
|
|
|
|
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();
|
2017-10-22 02:30:15 +00:00
|
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.eighty2bpp[last_pixel_byte_];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_output_target_ += 2;
|
|
|
|
|
current_pixel_column_++;
|
|
|
|
|
}
|
|
|
|
|
} else current_output_target_ += 2*number_of_cycles;
|
|
|
|
|
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();
|
|
|
|
|
*current_output_target_ = palette_tables_.eighty4bpp[last_pixel_byte_];
|
|
|
|
|
current_output_target_ += 1;
|
|
|
|
|
current_pixel_column_++;
|
|
|
|
|
}
|
|
|
|
|
} else current_output_target_ += number_of_cycles;
|
|
|
|
|
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;
|
2017-10-22 02:30:15 +00:00
|
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_output_target_ += 2;
|
|
|
|
|
|
|
|
|
|
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();
|
2017-10-22 02:30:15 +00:00
|
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_output_target_ += 2;
|
|
|
|
|
|
|
|
|
|
last_pixel_byte_ <<= 4;
|
2017-10-22 02:30:15 +00:00
|
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_output_target_ += 2;
|
|
|
|
|
|
|
|
|
|
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();
|
2017-10-22 02:30:15 +00:00
|
|
|
|
*reinterpret_cast<uint16_t *>(current_output_target_) = palette_tables_.forty1bpp[last_pixel_byte_];
|
2016-12-11 02:07:52 +00:00
|
|
|
|
current_output_target_ += 2;
|
|
|
|
|
current_pixel_column_++;
|
|
|
|
|
}
|
|
|
|
|
} else current_output_target_ += 2 * number_of_cycles;
|
|
|
|
|
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;
|
|
|
|
|
*current_output_target_ = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
|
current_output_target_ += 1;
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
*current_output_target_ = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
|
current_output_target_ += 1;
|
|
|
|
|
|
|
|
|
|
last_pixel_byte_ <<= 2;
|
|
|
|
|
*current_output_target_ = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
|
current_output_target_ += 1;
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
*current_output_target_ = palette_tables_.forty2bpp[last_pixel_byte_];
|
|
|
|
|
current_output_target_ += 1;
|
|
|
|
|
current_pixel_column_++;
|
|
|
|
|
}
|
|
|
|
|
} else current_output_target_ += number_of_cycles;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef get_pixel
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
|
void VideoOutput::run_for(const Cycles cycles) {
|
2017-07-25 02:36:42 +00:00
|
|
|
|
int number_of_cycles = cycles.as_int();
|
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_);
|
2017-10-21 23:49:04 +00:00
|
|
|
|
if(screen_map_[screen_map_pointer_].type == DrawAction::Pixels) output_pixels(static_cast<unsigned int>(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) {
|
2017-10-21 23:49:04 +00:00
|
|
|
|
case DrawAction::Sync: crt_->output_sync(static_cast<unsigned int>(draw_action_length * crt_cycles_multiplier)); break;
|
|
|
|
|
case DrawAction::ColourBurst: crt_->output_default_colour_burst(static_cast<unsigned int>(draw_action_length * crt_cycles_multiplier)); break;
|
|
|
|
|
case DrawAction::Blank: crt_->output_blank(static_cast<unsigned int>(draw_action_length * crt_cycles_multiplier)); break;
|
|
|
|
|
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
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
|
void VideoOutput::set_register(int address, uint8_t value) {
|
|
|
|
|
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: {
|
2016-12-11 02:07:52 +00:00
|
|
|
|
static const int registers[4][4] = {
|
|
|
|
|
{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-10-22 01:50:53 +00:00
|
|
|
|
#define pack(a, b) static_cast<uint8_t>((a << 4) | (b))
|
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]);
|
2016-12-11 02:07:52 +00:00
|
|
|
|
target[0] = pack(palette_[(byte&0x80) >> 4], palette_[(byte&0x40) >> 3]);
|
|
|
|
|
target[1] = pack(palette_[(byte&0x20) >> 2], palette_[(byte&0x10) >> 1]);
|
|
|
|
|
|
2017-10-22 02:30:15 +00:00
|
|
|
|
target = reinterpret_cast<uint8_t *>(&palette_tables_.eighty2bpp[byte]);
|
2016-12-11 02:07:52 +00:00
|
|
|
|
target[0] = pack(palette_[((byte&0x80) >> 4) | ((byte&0x08) >> 2)], palette_[((byte&0x40) >> 3) | ((byte&0x04) >> 1)]);
|
|
|
|
|
target[1] = pack(palette_[((byte&0x20) >> 2) | ((byte&0x02) >> 0)], palette_[((byte&0x10) >> 1) | ((byte&0x01) << 1)]);
|
|
|
|
|
|
2017-10-22 02:30:15 +00:00
|
|
|
|
target = reinterpret_cast<uint8_t *>(&palette_tables_.eighty1bpp[byte]);
|
2016-12-11 02:07:52 +00:00
|
|
|
|
target[0] = pack(palette_[(byte&0x80) >> 4], palette_[(byte&0x40) >> 3]);
|
|
|
|
|
target[1] = pack(palette_[(byte&0x20) >> 2], palette_[(byte&0x10) >> 1]);
|
|
|
|
|
target[2] = pack(palette_[(byte&0x08) >> 0], palette_[(byte&0x04) << 1]);
|
|
|
|
|
target[3] = pack(palette_[(byte&0x02) << 2], palette_[(byte&0x01) << 3]);
|
|
|
|
|
|
2016-12-15 23:07:46 +00:00
|
|
|
|
palette_tables_.forty2bpp[byte] = pack( palette_[((byte&0x80) >> 4) | ((byte&0x08) >> 2)], palette_[((byte&0x40) >> 3) | ((byte&0x04) >> 1)]);
|
2016-12-11 02:07:52 +00:00
|
|
|
|
palette_tables_.eighty4bpp[byte] = pack( palette_[((byte&0x80) >> 4) | ((byte&0x20) >> 3) | ((byte&0x08) >> 2) | ((byte&0x02) >> 1)],
|
2016-12-15 23:07:46 +00:00
|
|
|
|
palette_[((byte&0x40) >> 3) | ((byte&0x10) >> 2) | ((byte&0x04) >> 1) | ((byte&0x01) >> 0)]);
|
2016-12-11 02:07:52 +00:00
|
|
|
|
}
|
|
|
|
|
#undef pack
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|