2019-05-04 03:25:42 +00:00
|
|
|
//
|
|
|
|
// Video.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 03/05/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Video.hpp"
|
|
|
|
|
2019-05-05 02:27:58 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2019-05-04 03:25:42 +00:00
|
|
|
using namespace Apple::Macintosh;
|
|
|
|
|
|
|
|
// Re: CRT timings, see the Apple Guide to the Macintosh Hardware Family,
|
|
|
|
// bottom of page 400:
|
|
|
|
//
|
|
|
|
// "For each scan line, 512 pixels are drawn on the screen ...
|
|
|
|
// The horizontal blanking interval takes the time of an additional 192 pixels"
|
|
|
|
//
|
|
|
|
// And, at the top of 401:
|
|
|
|
//
|
|
|
|
// "The visible portion of a full-screen display consists of 342 horizontal scan lines...
|
|
|
|
// During the vertical blanking interval, the turned-off beam ... traces out an additional 28 scan lines,"
|
|
|
|
//
|
2019-08-12 01:41:12 +00:00
|
|
|
Video::Video(DeferredAudio &audio, DriveSpeedAccumulator &drive_speed_accumulator) :
|
2019-06-01 19:03:15 +00:00
|
|
|
audio_(audio),
|
2019-06-01 23:31:32 +00:00
|
|
|
drive_speed_accumulator_(drive_speed_accumulator),
|
2020-01-31 04:29:04 +00:00
|
|
|
crt_(704, 1, 370, 6, Outputs::Display::InputDataType::Luminance1) {
|
2019-05-05 02:27:58 +00:00
|
|
|
|
2019-05-06 03:22:05 +00:00
|
|
|
crt_.set_display_type(Outputs::Display::DisplayType::RGB);
|
2020-09-11 02:10:24 +00:00
|
|
|
|
|
|
|
// UGLY HACK. UGLY, UGLY HACK. UGLY!
|
|
|
|
// The OpenGL scan target fails properly to place visible areas which are not 4:3.
|
|
|
|
// The [newer] Metal scan target has no such issue. So assume that Apple => Metal,
|
|
|
|
// and set a visible area to work around the OpenGL issue if required.
|
|
|
|
// TODO: eliminate UGLY HACK.
|
|
|
|
#ifdef __APPLE__
|
|
|
|
crt_.set_visible_area(Outputs::Display::Rect(0.08f, 10.0f / 368.0f, 0.82f, 344.0f / 368.0f));
|
|
|
|
#else
|
|
|
|
crt_.set_visible_area(Outputs::Display::Rect(0.08f, -0.025f, 0.82f, 0.82f));
|
|
|
|
#endif
|
2019-06-13 22:41:38 +00:00
|
|
|
crt_.set_aspect_ratio(1.73f); // The Mac uses a non-standard scanning area.
|
2019-05-04 03:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Video::set_scan_target(Outputs::Display::ScanTarget *scan_target) {
|
|
|
|
crt_.set_scan_target(scan_target);
|
|
|
|
}
|
2019-05-04 18:23:37 +00:00
|
|
|
|
2020-01-22 03:28:25 +00:00
|
|
|
Outputs::Display::ScanStatus Video::get_scaled_scan_status() const {
|
|
|
|
return crt_.get_scaled_scan_status() / 2.0f;
|
2020-01-21 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 02:27:58 +00:00
|
|
|
void Video::run_for(HalfCycles duration) {
|
2019-07-28 02:23:40 +00:00
|
|
|
// Determine the current video and audio bases. These values don't appear to be latched, they apply immediately.
|
|
|
|
const size_t video_base = (use_alternate_screen_buffer_ ? (0xffff2700 >> 1) : (0xffffa700 >> 1)) & ram_mask_;
|
|
|
|
const size_t audio_base = (use_alternate_audio_buffer_ ? (0xffffa100 >> 1) : (0xfffffd00 >> 1)) & ram_mask_;
|
2019-05-05 02:27:58 +00:00
|
|
|
|
|
|
|
// The number of HalfCycles is literally the number of pixel clocks to move through,
|
|
|
|
// since pixel output occurs at twice the processor clock. So divide by 16 to get
|
|
|
|
// the number of fetches.
|
|
|
|
while(duration > HalfCycles(0)) {
|
|
|
|
const auto pixel_start = frame_position_ % line_length;
|
2019-10-30 02:36:29 +00:00
|
|
|
const int line = int((frame_position_ / line_length).as_integral());
|
2019-07-10 03:27:27 +00:00
|
|
|
|
|
|
|
const auto cycles_left_in_line = std::min(line_length - pixel_start, duration);
|
2019-05-05 02:27:58 +00:00
|
|
|
|
|
|
|
// Line timing, entirely invented as I can find exactly zero words of documentation:
|
|
|
|
//
|
|
|
|
// First 342 lines:
|
|
|
|
//
|
|
|
|
// First 32 words = pixels;
|
|
|
|
// next 5 words = right border;
|
|
|
|
// next 2 words = sync level;
|
|
|
|
// final 5 words = left border.
|
|
|
|
//
|
|
|
|
// Then 12 lines of border, 3 of sync, 11 more of border.
|
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
const int first_word = int(pixel_start.as_integral()) >> 4;
|
|
|
|
const int final_word = int((pixel_start + cycles_left_in_line).as_integral()) >> 4;
|
2019-05-05 02:27:58 +00:00
|
|
|
|
|
|
|
if(first_word != final_word) {
|
|
|
|
if(line < 342) {
|
|
|
|
// If there are any pixels left to output, do so.
|
|
|
|
if(first_word < 32) {
|
|
|
|
const int final_pixel_word = std::min(final_word, 32);
|
|
|
|
|
|
|
|
if(!first_word) {
|
|
|
|
pixel_buffer_ = crt_.begin_data(512);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pixel_buffer_) {
|
|
|
|
for(int c = first_word; c < final_pixel_word; ++c) {
|
2019-07-28 02:23:40 +00:00
|
|
|
uint16_t pixels = ram_[video_base + video_address_] ^ 0xffff;
|
2019-05-05 02:27:58 +00:00
|
|
|
++video_address_;
|
|
|
|
|
2019-05-06 01:55:34 +00:00
|
|
|
pixel_buffer_[15] = pixels & 0x01;
|
|
|
|
pixel_buffer_[14] = pixels & 0x02;
|
|
|
|
pixel_buffer_[13] = pixels & 0x04;
|
|
|
|
pixel_buffer_[12] = pixels & 0x08;
|
|
|
|
pixel_buffer_[11] = pixels & 0x10;
|
|
|
|
pixel_buffer_[10] = pixels & 0x20;
|
|
|
|
pixel_buffer_[9] = pixels & 0x40;
|
|
|
|
pixel_buffer_[8] = pixels & 0x80;
|
2019-05-05 02:27:58 +00:00
|
|
|
|
|
|
|
pixels >>= 8;
|
2019-05-06 01:55:34 +00:00
|
|
|
pixel_buffer_[7] = pixels & 0x01;
|
|
|
|
pixel_buffer_[6] = pixels & 0x02;
|
|
|
|
pixel_buffer_[5] = pixels & 0x04;
|
|
|
|
pixel_buffer_[4] = pixels & 0x08;
|
|
|
|
pixel_buffer_[3] = pixels & 0x10;
|
|
|
|
pixel_buffer_[2] = pixels & 0x20;
|
|
|
|
pixel_buffer_[1] = pixels & 0x40;
|
|
|
|
pixel_buffer_[0] = pixels & 0x80;
|
2019-05-05 02:27:58 +00:00
|
|
|
|
|
|
|
pixel_buffer_ += 16;
|
|
|
|
}
|
2020-09-16 02:20:06 +00:00
|
|
|
} else {
|
|
|
|
video_address_ += size_t(final_pixel_word - first_word);
|
2019-05-05 02:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(final_pixel_word == 32) {
|
|
|
|
crt_.output_data(512);
|
2020-09-11 02:13:19 +00:00
|
|
|
pixel_buffer_ = nullptr;
|
2019-05-05 02:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(first_word < sync_start && final_word >= sync_start) crt_.output_blank((sync_start - 32) * 16);
|
|
|
|
if(first_word < sync_end && final_word >= sync_end) crt_.output_sync((sync_end - sync_start) * 16);
|
|
|
|
if(final_word == 44) crt_.output_blank((44 - sync_end) * 16);
|
2019-07-10 03:27:27 +00:00
|
|
|
} else if(final_word == 44) {
|
|
|
|
if(line >= 353 && line < 356) {
|
|
|
|
/* Output a sync line. */
|
2019-05-05 02:27:58 +00:00
|
|
|
crt_.output_sync(sync_start * 16);
|
|
|
|
crt_.output_blank((sync_end - sync_start) * 16);
|
|
|
|
crt_.output_sync((44 - sync_end) * 16);
|
2019-07-10 03:27:27 +00:00
|
|
|
} else {
|
|
|
|
/* Output a blank line. */
|
2019-05-05 02:27:58 +00:00
|
|
|
crt_.output_blank(sync_start * 16);
|
|
|
|
crt_.output_sync((sync_end - sync_start) * 16);
|
|
|
|
crt_.output_blank((44 - sync_end) * 16);
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 19:03:15 +00:00
|
|
|
|
|
|
|
// Audio and disk fetches occur "just before video data".
|
|
|
|
if(final_word == 44) {
|
2019-07-28 02:23:40 +00:00
|
|
|
const uint16_t audio_word = ram_[audio_address_ + audio_base];
|
2019-06-01 19:03:15 +00:00
|
|
|
++audio_address_;
|
|
|
|
audio_.audio.post_sample(audio_word >> 8);
|
2019-06-01 23:31:32 +00:00
|
|
|
drive_speed_accumulator_.post_sample(audio_word & 0xff);
|
2019-06-01 19:03:15 +00:00
|
|
|
}
|
2019-05-05 02:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
duration -= cycles_left_in_line;
|
|
|
|
frame_position_ = frame_position_ + cycles_left_in_line;
|
|
|
|
if(frame_position_ == frame_length) {
|
|
|
|
frame_position_ = HalfCycles(0);
|
2019-05-06 03:05:24 +00:00
|
|
|
/*
|
|
|
|
Video: $1A700 and the alternate buffer starts at $12700; for a 512K Macintosh, add $60000 to these numbers.
|
|
|
|
*/
|
2019-07-28 02:23:40 +00:00
|
|
|
video_address_ = 0;
|
2019-06-01 19:03:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
"The main sound buffer is at $1FD00 in a 128K Macintosh, and the alternate buffer is at $1A100;
|
|
|
|
for a 512K Macintosh, add $60000 to these values."
|
|
|
|
*/
|
2019-07-28 02:23:40 +00:00
|
|
|
audio_address_ = 0;
|
2019-05-05 02:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 20:54:19 +00:00
|
|
|
bool Video::vsync() {
|
2019-10-30 02:36:29 +00:00
|
|
|
const auto line = (frame_position_ / line_length).as_integral();
|
2019-05-08 20:54:19 +00:00
|
|
|
return line >= 353 && line < 356;
|
|
|
|
}
|
|
|
|
|
2019-07-09 02:28:05 +00:00
|
|
|
HalfCycles Video::get_next_sequence_point() {
|
2019-10-30 02:36:29 +00:00
|
|
|
const auto line = (frame_position_ / line_length).as_integral();
|
2019-07-09 02:28:05 +00:00
|
|
|
if(line >= 353 && line < 356) {
|
|
|
|
// Currently in vsync, so get time until start of line 357,
|
|
|
|
// when vsync will end.
|
2019-07-10 03:27:27 +00:00
|
|
|
return HalfCycles(356) * line_length - frame_position_;
|
2019-07-09 02:28:05 +00:00
|
|
|
} else {
|
|
|
|
// Not currently in vsync, so get time until start of line 353.
|
|
|
|
const auto start_of_vsync = HalfCycles(353) * line_length;
|
|
|
|
if(frame_position_ < start_of_vsync)
|
|
|
|
return start_of_vsync - frame_position_;
|
|
|
|
else
|
|
|
|
return start_of_vsync + HalfCycles(number_of_lines) * line_length - frame_position_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 19:03:15 +00:00
|
|
|
void Video::set_use_alternate_buffers(bool use_alternate_screen_buffer, bool use_alternate_audio_buffer) {
|
2019-05-06 03:05:24 +00:00
|
|
|
use_alternate_screen_buffer_ = use_alternate_screen_buffer;
|
2019-06-01 19:03:15 +00:00
|
|
|
use_alternate_audio_buffer_ = use_alternate_audio_buffer;
|
2019-05-06 03:05:24 +00:00
|
|
|
}
|
2019-06-03 18:50:36 +00:00
|
|
|
|
2019-08-12 01:41:12 +00:00
|
|
|
void Video::set_ram(uint16_t *ram, uint32_t mask) {
|
|
|
|
ram_ = ram;
|
2019-06-03 18:50:36 +00:00
|
|
|
ram_mask_ = mask;
|
|
|
|
}
|