From 8dd7c6ef232bd6b3290409f83abcac3779ebec74 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Tue, 29 May 2018 22:30:45 -0400 Subject: [PATCH 1/9] Eliminates 'reversed_c' as I no longer believe low-resolution colour numbers are reversed. Also gets explicit about phase. --- Machines/AppleII/Video.cpp | 12 +++++++++--- Machines/AppleII/Video.hpp | 9 ++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Machines/AppleII/Video.cpp b/Machines/AppleII/Video.cpp index 9e9d98bc3..80342d7c1 100644 --- a/Machines/AppleII/Video.cpp +++ b/Machines/AppleII/Video.cpp @@ -36,6 +36,7 @@ VideoBase::VideoBase() : // Show only the centre 75% of the TV frame. crt_->set_video_signal(Outputs::CRT::VideoSignal::Composite); crt_->set_visible_area(Outputs::CRT::Rect(0.115f, 0.117f, 0.77f, 0.77f)); + crt_->set_immediate_default_phase(0.0f); } Outputs::CRT::CRT *VideoBase::get_crt() { @@ -46,6 +47,12 @@ uint16_t VideoBase::scaled_byte[256]; uint16_t VideoBase::low_resolution_patterns[2][16]; void VideoBase::setup_tables() { + // Rules of Apple II high resolution video: + // + // Bit 0 appears on screen first. Then bit 1. Etc to bit 6. + // + // If bit 7 is set, the whole serialisation is delayed for half a pixel, holding + // whichever level was previously being output. for(int c = 0; c < 128; ++c) { const uint16_t value = ((c & 0x01) ? 0x0003 : 0x0000) | @@ -70,17 +77,16 @@ void VideoBase::setup_tables() { for(int c = 0; c < 16; ++c) { // Produce the whole 28-bit pattern that would cover two columns. - const int reversed_c = ((c&0x1) ? 0x8 : 0x0) | ((c&0x2) ? 0x4 : 0x0) | ((c&0x4) ? 0x2 : 0x0) | ((c&0x8) ? 0x1 : 0x0); int pattern = 0; for(int l = 0; l < 7; ++l) { pattern <<= 4; - pattern |= reversed_c; + pattern |= c; } // Pack that 28-bit pattern into the appropriate look-up tables. uint8_t *const left_entry = reinterpret_cast(&low_resolution_patterns[0][c]); uint8_t *const right_entry = reinterpret_cast(&low_resolution_patterns[1][c]); - left_entry[0] = static_cast(pattern);; + left_entry[0] = static_cast(pattern); left_entry[1] = static_cast(pattern >> 7); right_entry[0] = static_cast(pattern >> 14); right_entry[1] = static_cast(pattern >> 21); diff --git a/Machines/AppleII/Video.hpp b/Machines/AppleII/Video.hpp index 1e23d1f30..d9f904a3b 100644 --- a/Machines/AppleII/Video.hpp +++ b/Machines/AppleII/Video.hpp @@ -144,15 +144,15 @@ template class Video: public VideoBase { const uint8_t graphic = bus_handler_.perform_read(static_cast(graphics_address + c)); pixel_pointer_[c] = scaled_byte[graphic]; if(graphic & 0x80) { - reinterpret_cast(&pixel_pointer_[c])[0] |= graphics_carry_; + reinterpret_cast(&pixel_pointer_[c])[0] |= (graphics_carry_&1); } - graphics_carry_ = (graphic >> 6) & 1; + graphics_carry_ = graphic >> 6; } break; } if(ending_column >= 40) { - crt_->output_data(280, 80); + output_data(80); } } else { if(ending_column >= 40) { @@ -261,6 +261,9 @@ template class Video: public VideoBase { const int flash_length = 8406; BusHandler &bus_handler_; + void output_data(unsigned int length) { + crt_->output_data((length*7)/2, length); + } }; } From 35e84ff1a8fedab88e9a52865b7b7a2d8ef6196a Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 31 May 2018 21:40:46 -0400 Subject: [PATCH 2/9] Corrects NTSC quadrature phase. --- Components/6560/6560.hpp | 10 ++++---- Machines/Atari2600/TIA.cpp | 6 ++--- Outputs/CRT/CRT.cpp | 6 +++-- Outputs/CRT/CRT.hpp | 4 ++-- .../Internals/Shaders/IntermediateShader.cpp | 24 +++++++++---------- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Components/6560/6560.hpp b/Components/6560/6560.hpp index 4bd44dbb4..fbcacb390 100644 --- a/Components/6560/6560.hpp +++ b/Components/6560/6560.hpp @@ -69,7 +69,7 @@ template class MOS6560 { speaker_(audio_generator_) { crt_->set_svideo_sampling_function( - "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase)" + "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)" "{" "vec2 yc = texture(texID, coordinate).rg / vec2(255.0);" @@ -125,10 +125,10 @@ template class MOS6560 { 19, 86, 123, 59, }; const uint8_t ntsc_chrominances[16] = { - 255, 255, 7, 71, - 25, 86, 48, 112, - 0, 119, 7, 71, - 25, 86, 48, 112, + 255, 255, 121, 57, + 103, 42, 80, 16, + 0, 9, 121, 57, + 103, 42, 80, 16, }; const uint8_t *chrominances; Outputs::CRT::DisplayType display_type; diff --git a/Machines/Atari2600/TIA.cpp b/Machines/Atari2600/TIA.cpp index 29f294fb0..af37c49f9 100644 --- a/Machines/Atari2600/TIA.cpp +++ b/Machines/Atari2600/TIA.cpp @@ -124,19 +124,19 @@ void TIA::set_output_mode(Atari2600::TIA::OutputMode output_mode) { if(output_mode == OutputMode::NTSC) { crt_->set_svideo_sampling_function( - "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase)" + "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)" "{" "uint c = texture(texID, coordinate).r;" "uint y = c & 14u;" "uint iPhase = (c >> 4);" "float phaseOffset = 6.283185308 * float(iPhase) / 13.0 + 5.074880441076923;" - "return vec2(float(y) / 14.0, step(1, iPhase) * cos(phase + phaseOffset));" + "return vec2(float(y) / 14.0, step(1, iPhase) * cos(phase - phaseOffset));" "}"); display_type = Outputs::CRT::DisplayType::NTSC60; } else { crt_->set_svideo_sampling_function( - "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase)" + "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)" "{" "uint c = texture(texID, coordinate).r;" "uint y = c & 14u;" diff --git a/Outputs/CRT/CRT.cpp b/Outputs/CRT/CRT.cpp index 86a74b69b..7f0f4f20b 100644 --- a/Outputs/CRT/CRT.cpp +++ b/Outputs/CRT/CRT.cpp @@ -170,7 +170,9 @@ void CRT::advance_cycles(unsigned int number_of_cycles, bool hsync_requested, bo // outside of the locked region source_output_position_x1() = static_cast(horizontal_flywheel_->get_current_output_position()); source_phase() = colour_burst_phase_; - source_amplitude() = colour_burst_amplitude_; + + // TODO: determine what the PAL phase-shift machines actually do re: the swinging burst. + source_amplitude() = phase_alternates_ ? 128 - colour_burst_amplitude_ : 128 + colour_burst_amplitude_; } // decrement the number of cycles left to run for and increment the @@ -368,7 +370,7 @@ void CRT::output_colour_burst(unsigned int number_of_cycles, uint8_t phase, uint scan.type = Scan::Type::ColourBurst; scan.number_of_cycles = number_of_cycles; scan.phase = phase; - scan.amplitude = amplitude; + scan.amplitude = amplitude >> 1; output_scan(&scan); } diff --git a/Outputs/CRT/CRT.hpp b/Outputs/CRT/CRT.hpp index 335a2710e..c27fa86e5 100644 --- a/Outputs/CRT/CRT.hpp +++ b/Outputs/CRT/CRT.hpp @@ -332,10 +332,10 @@ class CRT { output mode will be applied. @param shader A GLSL fragment including a function with the signature - `vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase)` + `vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)` that evaluates to the s-video signal level, luminance as the first component and chrominance as the second, as a function of a source buffer, sampling location and colour - carrier phase. + carrier phase; amplitude is supplied for its sign. */ inline void set_svideo_sampling_function(const std::string &shader) { enqueue_openGL_function([shader, this] { diff --git a/Outputs/CRT/Internals/Shaders/IntermediateShader.cpp b/Outputs/CRT/Internals/Shaders/IntermediateShader.cpp index 36c614659..630d09ce1 100644 --- a/Outputs/CRT/Internals/Shaders/IntermediateShader.cpp +++ b/Outputs/CRT/Internals/Shaders/IntermediateShader.cpp @@ -96,10 +96,10 @@ std::unique_ptr IntermediateShader::make_shader(const std::s // setup phaseAndAmplitudeVarying.x as colour burst subcarrier phase, in radians; // setup phaseAndAmplitudeVarying.y as colour burst amplitude; - // setup phaseAndAmplitudeVarying.z as 1 / (colour burst amplitude), or 0.0 if amplitude is 0.0; + // setup phaseAndAmplitudeVarying.z as 1 / abs(colour burst amplitude), or 0.0 if amplitude is 0.0; "phaseAndAmplitudeVarying.x = (extendedOutputPosition.x + (phaseTimeAndAmplitude.x / 64.0)) * 0.5 * 3.141592654;" - "phaseAndAmplitudeVarying.y = phaseTimeAndAmplitude.y / 255.0;" - "phaseAndAmplitudeVarying.z = (phaseAndAmplitudeVarying.y > 0.0) ? 1.0 / phaseAndAmplitudeVarying.y : 0.0;" + "phaseAndAmplitudeVarying.y = (phaseTimeAndAmplitude.y - 128) / 127.0;" + "phaseAndAmplitudeVarying.z = (abs(phaseAndAmplitudeVarying.y) > 0.05) ? 1.0 / abs(phaseAndAmplitudeVarying.y) : 0.0;" // determine output position by scaling the output position according to the texture size "vec2 eyePosition = 2.0*(extendedOutputPosition / outputTextureSize) - vec2(1.0);" @@ -134,8 +134,8 @@ std::unique_ptr IntermediateShader::make_composite_source_sh svideo_shader << "float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)" "{" - "vec2 svideoColour = svideo_sample(texID, coordinate, iCoordinate, phase);" - "return mix(svideoColour.x, svideoColour.y, amplitude);" + "vec2 svideoColour = svideo_sample(texID, coordinate, iCoordinate, phase, amplitude);" + "return mix(svideoColour.x, svideoColour.y, abs(amplitude));" "}"; } else { fragment_shader << @@ -145,7 +145,7 @@ std::unique_ptr IntermediateShader::make_composite_source_sh "{" "vec3 rgbColour = clamp(rgb_sample(texID, coordinate, iCoordinate), vec3(0.0), vec3(1.0));" "vec3 lumaChromaColour = rgbToLumaChroma * rgbColour;" - "vec2 quadrature = vec2(cos(phase), -sin(phase)) * amplitude;" + "vec2 quadrature = vec2(cos(phase), sin(phase)) * vec2(abs(amplitude), amplitude);" "return dot(lumaChromaColour, vec3(1.0 - amplitude, quadrature));" "}"; } @@ -178,11 +178,11 @@ std::unique_ptr IntermediateShader::make_svideo_source_shade fragment_shader << rgb_shader << "uniform mat3 rgbToLumaChroma;" - "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase)" + "vec2 svideo_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)" "{" "vec3 rgbColour = clamp(rgb_sample(texID, coordinate, iCoordinate), vec3(0.0), vec3(1.0));" "vec3 lumaChromaColour = rgbToLumaChroma * rgbColour;" - "vec2 quadrature = vec2(cos(phase), -sin(phase));" + "vec2 quadrature = vec2(cos(phase), sin(phase)) * vec2(1.0, sign(amplitude));" "return vec2(lumaChromaColour.x, 0.5 + dot(quadrature, lumaChromaColour.yz) * 0.5);" "}"; } @@ -190,8 +190,8 @@ std::unique_ptr IntermediateShader::make_svideo_source_shade fragment_shader << "void main(void)" "{" - "vec2 sample = svideo_sample(texID, inputPositionsVarying[5], iInputPositionVarying, phaseAndAmplitudeVarying.x);" - "vec2 quadrature = vec2(cos(phaseAndAmplitudeVarying.x), -sin(phaseAndAmplitudeVarying.x)) * 0.5 * phaseAndAmplitudeVarying.z;" + "vec2 sample = svideo_sample(texID, inputPositionsVarying[5], iInputPositionVarying, phaseAndAmplitudeVarying.x, phaseAndAmplitudeVarying.y);" + "vec2 quadrature = vec2(cos(phaseAndAmplitudeVarying.x), sin(phaseAndAmplitudeVarying.x)) * vec2(1.0, sign(phaseAndAmplitudeVarying.y)) * 0.5 * phaseAndAmplitudeVarying.z;" "fragColour = vec3(sample.x, vec2(0.5) + (sample.y * quadrature));" "}"; @@ -244,11 +244,11 @@ std::unique_ptr IntermediateShader::make_chroma_luma_separat // define chroma to be whatever was here, minus luma "float chrominance = 0.5 * (samples.z - luminance) * phaseAndAmplitudeVarying.z;" - "luminance /= (1.0 - phaseAndAmplitudeVarying.y);" + "luminance /= (1.0 - abs(phaseAndAmplitudeVarying.y));" // split choma colours here, as the most direct place, writing out // RGB = (luma, chroma.x, chroma.y) - "vec2 quadrature = vec2(cos(phaseAndAmplitudeVarying.x), -sin(phaseAndAmplitudeVarying.x));" + "vec2 quadrature = vec2(cos(phaseAndAmplitudeVarying.x), sin(phaseAndAmplitudeVarying.x)) * vec2(1.0, sign(phaseAndAmplitudeVarying.y));" "fragColour = vec3(luminance, vec2(0.5) + (chrominance * quadrature));" "}",false, false); } From 0833412df98204ae1e466e50af06810560b1f6e4 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 1 Jun 2018 19:45:37 -0400 Subject: [PATCH 3/9] Corrects port for ZON-X reads. --- Machines/ZX8081/ZX8081.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Machines/ZX8081/ZX8081.cpp b/Machines/ZX8081/ZX8081.cpp index 5d5828256..ed52a1e98 100644 --- a/Machines/ZX8081/ZX8081.cpp +++ b/Machines/ZX8081/ZX8081.cpp @@ -157,7 +157,7 @@ template class ConcreteMachine: // The below emulates the ZonX AY expansion device. if(is_zx81) { - if((address&0xef) == 0x0f) { + if((address&0xef) == 0xcf) { value &= ay_read_data(); } } From 1a9cea050e2bc7efaf9fbcde4d60098ae8bb4fba Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 1 Jun 2018 19:48:42 -0400 Subject: [PATCH 4/9] Minor: ensure AY registers *read* as 0 from reset, as well as being 0. --- Components/AY38910/AY38910.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Components/AY38910/AY38910.hpp b/Components/AY38910/AY38910.hpp index 737a0c8ea..e9784a87e 100644 --- a/Components/AY38910/AY38910.hpp +++ b/Components/AY38910/AY38910.hpp @@ -92,7 +92,7 @@ class AY38910: public ::Outputs::Speaker::SampleSource { Concurrency::DeferringAsyncTaskQueue &task_queue_; int selected_register_ = 0; - uint8_t registers_[16]; + uint8_t registers_[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; uint8_t output_registers_[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; uint8_t port_inputs_[2]; From 69dc3cc4d8ca7b9654a533c32ceb89b4aa5d1ffd Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 1 Jun 2018 22:52:29 -0400 Subject: [PATCH 5/9] Switches to using the same varying for byte and subpixel selection. --- Machines/AppleII/Video.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Machines/AppleII/Video.cpp b/Machines/AppleII/Video.cpp index 80342d7c1..3080357ed 100644 --- a/Machines/AppleII/Video.cpp +++ b/Machines/AppleII/Video.cpp @@ -27,7 +27,7 @@ VideoBase::VideoBase() : crt_->set_composite_sampling_function( "float composite_sample(usampler2D sampler, vec2 coordinate, vec2 icoordinate, float phase, float amplitude)" "{" - "uint texValue = texture(sampler, coordinate).r;" + "uint texValue = texture(sampler, vec2(icoordinate.x / (7*textureSize(sampler, 0).x), coordinate.y)).r;" "texValue >>= int(icoordinate.x) % 7;" "return float(texValue & 1u);" "}"); From 80b281d9f1317b6b8087f4ff054251e7f6529e25 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 2 Jun 2018 18:25:00 -0400 Subject: [PATCH 6/9] Switches back to whole bytes per pixel, owing to persistent precision problems at 1bpp. Also fixes the inaccurately-named `cycles_since_update_`. --- Machines/ZX8081/Video.cpp | 44 +++++++++++++++++++-------------------- Machines/ZX8081/Video.hpp | 2 +- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Machines/ZX8081/Video.cpp b/Machines/ZX8081/Video.cpp index a6b6cf6b2..95e691301 100644 --- a/Machines/ZX8081/Video.cpp +++ b/Machines/ZX8081/Video.cpp @@ -16,25 +16,20 @@ namespace { The number of bytes of PCM data to allocate at once; if/when more are required, the class will simply allocate another batch. */ -const std::size_t StandardAllocationSize = 40; - -/// The amount of time a byte takes to output. -const std::size_t HalfCyclesPerByte = 8; +const std::size_t StandardAllocationSize = 320; } Video::Video() : crt_(new Outputs::CRT::CRT(207 * 2, 1, Outputs::CRT::DisplayType::PAL50, 1)) { - // Set a composite sampling function that assumes 1bpp input. + // Set a composite sampling function that assumes two-level input; either a byte is 0, which is black, + // or it is non-zero, which is white. crt_->set_composite_sampling_function( "float composite_sample(usampler2D sampler, vec2 coordinate, vec2 icoordinate, float phase, float amplitude)" "{" - "uint texValue = texture(sampler, coordinate).r;" - "texValue <<= int(icoordinate.x) & 7;" - "return float(texValue & 128u);" + "return texture(sampler, coordinate).r;" "}"); - crt_->set_integer_coordinate_multiplier(8.0f); // Show only the centre 80% of the TV frame. crt_->set_video_signal(Outputs::CRT::VideoSignal::Composite); @@ -43,7 +38,7 @@ Video::Video() : void Video::run_for(const HalfCycles half_cycles) { // Just keep a running total of the amount of time that remains owed to the CRT. - cycles_since_update_ += static_cast(half_cycles.as_int()); + time_since_update_ += half_cycles; } void Video::flush() { @@ -53,29 +48,29 @@ void Video::flush() { void Video::flush(bool next_sync) { if(sync_) { // If in sync, that takes priority. Output the proper amount of sync. - crt_->output_sync(cycles_since_update_); + crt_->output_sync(static_cast(time_since_update_.as_int())); } else { // If not presently in sync, then... if(line_data_) { // If there is output data queued, output it either if it's being interrupted by // sync, or if we're past its end anyway. Otherwise let it be. - unsigned int data_length = static_cast(line_data_pointer_ - line_data_) * HalfCyclesPerByte; - if(data_length < cycles_since_update_ || next_sync) { - unsigned int output_length = std::min(data_length, cycles_since_update_); - crt_->output_data(output_length, output_length / HalfCyclesPerByte); + int data_length = static_cast(line_data_pointer_ - line_data_); + if(data_length < time_since_update_.as_int() || next_sync) { + auto output_length = std::min(data_length, time_since_update_.as_int()); + crt_->output_data(static_cast(output_length), static_cast(output_length)); line_data_pointer_ = line_data_ = nullptr; - cycles_since_update_ -= output_length; + time_since_update_ -= HalfCycles(output_length); } else return; } // Any pending pixels being dealt with, pad with the white level. uint8_t *colour_pointer = static_cast(crt_->allocate_write_area(1)); if(colour_pointer) *colour_pointer = 0xff; - crt_->output_level(cycles_since_update_); + crt_->output_level(static_cast(time_since_update_.as_int())); } - cycles_since_update_ = 0; + time_since_update_ = 0; } void Video::set_sync(bool sync) { @@ -101,14 +96,19 @@ void Video::output_byte(uint8_t byte) { if(line_data_) { // If the buffer is full, output it now and obtain a new one if(line_data_pointer_ - line_data_ == StandardAllocationSize) { - crt_->output_data(StandardAllocationSize * HalfCyclesPerByte, StandardAllocationSize); - cycles_since_update_ -= StandardAllocationSize * HalfCyclesPerByte; + crt_->output_data(StandardAllocationSize, StandardAllocationSize); + time_since_update_ -= StandardAllocationSize; line_data_pointer_ = line_data_ = crt_->allocate_write_area(StandardAllocationSize); if(!line_data_) return; } - line_data_pointer_[0] = byte; - line_data_pointer_ ++; + // Convert to one-byte-per-pixel where any non-zero value will act as white. + uint8_t mask = 0x80; + for(int c = 0; c < 8; c++) { + line_data_pointer_[c] = byte & mask; + mask >>= 1; + } + line_data_pointer_ += 8; } } diff --git a/Machines/ZX8081/Video.hpp b/Machines/ZX8081/Video.hpp index d0e4a3af1..dac52b361 100644 --- a/Machines/ZX8081/Video.hpp +++ b/Machines/ZX8081/Video.hpp @@ -45,7 +45,7 @@ class Video { bool sync_ = false; uint8_t *line_data_ = nullptr; uint8_t *line_data_pointer_ = nullptr; - unsigned int cycles_since_update_ = 0; + HalfCycles time_since_update_ = 0; std::unique_ptr crt_; void flush(bool next_sync); From d84b8700a38a2141a1e88975be45afaa9caa66cf Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 2 Jun 2018 22:03:45 -0400 Subject: [PATCH 7/9] Switches the Apple II to one byte per pixel. Just trying to get it right for now; optimisation to come. --- Machines/AppleII/Video.cpp | 70 ++----------------------- Machines/AppleII/Video.hpp | 103 +++++++++++++++++++++++++------------ 2 files changed, 75 insertions(+), 98 deletions(-) diff --git a/Machines/AppleII/Video.cpp b/Machines/AppleII/Video.cpp index 3080357ed..13070ff57 100644 --- a/Machines/AppleII/Video.cpp +++ b/Machines/AppleII/Video.cpp @@ -10,28 +10,16 @@ using namespace AppleII::Video; -namespace { - -struct ScaledByteFiller { - ScaledByteFiller() { - VideoBase::setup_tables(); - } -} throwaway; - -} - VideoBase::VideoBase() : - crt_(new Outputs::CRT::CRT(455, 1, Outputs::CRT::DisplayType::NTSC60, 1)) { + crt_(new Outputs::CRT::CRT(910, 1, Outputs::CRT::DisplayType::NTSC60, 1)) { - // Set a composite sampling function that assumes 1bpp input, and uses just 7 bits per byte. + // Set a composite sampling function that assumes one byte per pixel input, and + // accepts any non-zero value as being fully on, zero being fully off. crt_->set_composite_sampling_function( "float composite_sample(usampler2D sampler, vec2 coordinate, vec2 icoordinate, float phase, float amplitude)" "{" - "uint texValue = texture(sampler, vec2(icoordinate.x / (7*textureSize(sampler, 0).x), coordinate.y)).r;" - "texValue >>= int(icoordinate.x) % 7;" - "return float(texValue & 1u);" + "return texture(sampler, coordinate).r;" "}"); - crt_->set_integer_coordinate_multiplier(7.0f); // Show only the centre 75% of the TV frame. crt_->set_video_signal(Outputs::CRT::VideoSignal::Composite); @@ -43,56 +31,6 @@ Outputs::CRT::CRT *VideoBase::get_crt() { return crt_.get(); } -uint16_t VideoBase::scaled_byte[256]; -uint16_t VideoBase::low_resolution_patterns[2][16]; - -void VideoBase::setup_tables() { - // Rules of Apple II high resolution video: - // - // Bit 0 appears on screen first. Then bit 1. Etc to bit 6. - // - // If bit 7 is set, the whole serialisation is delayed for half a pixel, holding - // whichever level was previously being output. - for(int c = 0; c < 128; ++c) { - const uint16_t value = - ((c & 0x01) ? 0x0003 : 0x0000) | - ((c & 0x02) ? 0x000c : 0x0000) | - ((c & 0x04) ? 0x0030 : 0x0000) | - ((c & 0x08) ? 0x0140 : 0x0000) | - ((c & 0x10) ? 0x0600 : 0x0000) | - ((c & 0x20) ? 0x1800 : 0x0000) | - ((c & 0x40) ? 0x6000 : 0x0000); - - uint8_t *const table_entry = reinterpret_cast(&scaled_byte[c]); - table_entry[0] = static_cast(value & 0xff); - table_entry[1] = static_cast(value >> 8); - } - for(int c = 128; c < 256; ++c) { - uint8_t *const source_table_entry = reinterpret_cast(&scaled_byte[c & 0x7f]); - uint8_t *const destination_table_entry = reinterpret_cast(&scaled_byte[c]); - - destination_table_entry[0] = static_cast(source_table_entry[0] << 1); - destination_table_entry[1] = static_cast((source_table_entry[1] << 1) | (source_table_entry[0] >> 6)); - } - - for(int c = 0; c < 16; ++c) { - // Produce the whole 28-bit pattern that would cover two columns. - int pattern = 0; - for(int l = 0; l < 7; ++l) { - pattern <<= 4; - pattern |= c; - } - - // Pack that 28-bit pattern into the appropriate look-up tables. - uint8_t *const left_entry = reinterpret_cast(&low_resolution_patterns[0][c]); - uint8_t *const right_entry = reinterpret_cast(&low_resolution_patterns[1][c]); - left_entry[0] = static_cast(pattern); - left_entry[1] = static_cast(pattern >> 7); - right_entry[0] = static_cast(pattern >> 14); - right_entry[1] = static_cast(pattern >> 21); - } -} - void VideoBase::set_graphics_mode() { use_graphics_mode_ = true; } diff --git a/Machines/AppleII/Video.hpp b/Machines/AppleII/Video.hpp index d9f904a3b..fd7f91d98 100644 --- a/Machines/AppleII/Video.hpp +++ b/Machines/AppleII/Video.hpp @@ -27,7 +27,6 @@ class BusHandler { class VideoBase { public: VideoBase(); - static void setup_tables(); /// @returns The CRT this video feed is feeding. Outputs::CRT::CRT *get_crt(); @@ -46,9 +45,12 @@ class VideoBase { protected: std::unique_ptr crt_; + uint8_t *pixel_pointer_ = nullptr; + int pixel_pointer_column_ = 0; + bool pixels_are_high_density_ = false; + int video_page_ = 0; int row_ = 0, column_ = 0, flash_ = 0; - uint16_t *pixel_pointer_ = nullptr; std::vector character_rom_; enum class GraphicsMode { @@ -58,10 +60,7 @@ class VideoBase { } graphics_mode_ = GraphicsMode::LowRes; bool use_graphics_mode_ = false; bool mixed_mode_ = false; - uint16_t graphics_carry_ = 0; - - static uint16_t scaled_byte[256]; - static uint16_t low_resolution_patterns[2][16]; + uint8_t graphics_carry_ = 0; }; template class Video: public VideoBase { @@ -91,7 +90,7 @@ template class Video: public VideoBase { const int cycles_this_line = std::min(65 - column_, int_cycles); if(row_ >= first_sync_line && row_ < first_sync_line + 3) { - crt_->output_sync(static_cast(cycles_this_line) * 7); + crt_->output_sync(static_cast(cycles_this_line) * 14); } else { const int ending_column = column_ + cycles_this_line; const GraphicsMode line_mode = use_graphics_mode_ ? graphics_mode_ : GraphicsMode::Text; @@ -101,8 +100,13 @@ template class Video: public VideoBase { // of line 192. if(column_ < 40) { if(row_ < 192) { - if(!column_) { - pixel_pointer_ = reinterpret_cast(crt_->allocate_write_area(80, 2)); + GraphicsMode pixel_mode = (!mixed_mode_ || row_ < 160) ? line_mode : GraphicsMode::Text; + bool requires_high_density = pixel_mode != GraphicsMode::Text; + if(!column_ || requires_high_density != pixels_are_high_density_) { + if(column_) output_data_to_column(column_); + pixel_pointer_ = crt_->allocate_write_area(561); + pixel_pointer_column_ = column_; + pixels_are_high_density_ = requires_high_density; graphics_carry_ = 0; } @@ -111,10 +115,7 @@ template class Video: public VideoBase { const int pixel_row = row_ & 7; const uint16_t row_address = static_cast((character_row >> 3) * 40 + ((character_row&7) << 7)); const uint16_t text_address = static_cast(((video_page_+1) * 0x400) + row_address); - const uint16_t graphics_address = static_cast(((video_page_+1) * 0x2000) + row_address + ((pixel_row&7) << 10)); - const int row_shift = (row_&4); - GraphicsMode pixel_mode = (!mixed_mode_ || row_ < 160) ? line_mode : GraphicsMode::Text; switch(pixel_mode) { case GraphicsMode::Text: { const uint8_t inverses[] = { @@ -128,35 +129,71 @@ template class Video: public VideoBase { const std::size_t character_address = static_cast(((character & 0x3f) << 3) + pixel_row); const uint8_t character_pattern = character_rom_[character_address] ^ inverses[character >> 6]; - pixel_pointer_[c] = scaled_byte[character_pattern & 0x7f]; + + int mask = 0x01; + for(int p = 0; p < 7; ++p) { + pixel_pointer_[p] = character_pattern & mask; + mask <<= 1; + } + graphics_carry_ = character_pattern & 0x40; + pixel_pointer_ += 7; } } break; - case GraphicsMode::LowRes: + case GraphicsMode::LowRes: { + const int row_shift = (row_&4); for(int c = column_; c < pixel_end; ++c) { - const uint8_t character = bus_handler_.perform_read(static_cast(text_address + c)); - pixel_pointer_[c] = low_resolution_patterns[c&1][(character >> row_shift)&0xf]; - } - break; + const uint8_t nibble = (bus_handler_.perform_read(static_cast(text_address + c)) >> row_shift) & 0x0f; - case GraphicsMode::HighRes: + if(c&1) { + pixel_pointer_[0] = pixel_pointer_[4] = pixel_pointer_[8] = pixel_pointer_[12] = nibble & 4; + pixel_pointer_[1] = pixel_pointer_[5] = pixel_pointer_[9] = pixel_pointer_[13] = nibble & 8; + pixel_pointer_[2] = pixel_pointer_[6] = pixel_pointer_[10] = nibble & 1; + pixel_pointer_[3] = pixel_pointer_[7] = pixel_pointer_[11] = nibble & 2; + graphics_carry_ = nibble & 8; + } else { + pixel_pointer_[0] = pixel_pointer_[4] = pixel_pointer_[8] = pixel_pointer_[12] = nibble & 1; + pixel_pointer_[1] = pixel_pointer_[5] = pixel_pointer_[9] = pixel_pointer_[13] = nibble & 2; + pixel_pointer_[2] = pixel_pointer_[6] = pixel_pointer_[10] = nibble & 4; + pixel_pointer_[3] = pixel_pointer_[7] = pixel_pointer_[11] = nibble & 8; + graphics_carry_ = nibble & 2; + } + pixel_pointer_ += 14; + } + } break; + + case GraphicsMode::HighRes: { + const uint16_t graphics_address = static_cast(((video_page_+1) * 0x2000) + row_address + ((pixel_row&7) << 10)); for(int c = column_; c < pixel_end; ++c) { const uint8_t graphic = bus_handler_.perform_read(static_cast(graphics_address + c)); - pixel_pointer_[c] = scaled_byte[graphic]; + if(graphic & 0x80) { - reinterpret_cast(&pixel_pointer_[c])[0] |= (graphics_carry_&1); + pixel_pointer_[0] = graphics_carry_; + pixel_pointer_++; } - graphics_carry_ = graphic >> 6; + int mask = 0x01; + for(int p = 0; p < 12; p += 2) { + pixel_pointer_[p] = graphic & mask; + pixel_pointer_[p+1] = graphic & mask; + mask <<= 1; + } + pixel_pointer_[12] = graphic & 0x40; + pixel_pointer_ += 13; + if(!(graphic & 0x80)) { + pixel_pointer_[0] = graphic & 0x40; + pixel_pointer_++; + } + graphics_carry_ = graphic & 0x40; } - break; + } break; } if(ending_column >= 40) { - output_data(80); + output_data_to_column(40); } } else { if(ending_column >= 40) { - crt_->output_blank(280); + crt_->output_blank(560); } } } @@ -169,13 +206,13 @@ template class Video: public VideoBase { const int first_blank_start = std::max(40, column_); const int first_blank_end = std::min(first_sync_column, ending_column); if(first_blank_end > first_blank_start) { - crt_->output_blank(static_cast(first_blank_end - first_blank_start) * 7); + crt_->output_blank(static_cast(first_blank_end - first_blank_start) * 14); } const int sync_start = std::max(first_sync_column, column_); const int sync_end = std::min(first_sync_column + 4, ending_column); if(sync_end > sync_start) { - crt_->output_sync(static_cast(sync_end - sync_start) * 7); + crt_->output_sync(static_cast(sync_end - sync_start) * 14); } int second_blank_start; @@ -183,7 +220,7 @@ template class Video: public VideoBase { const int colour_burst_start = std::max(first_sync_column + 4, column_); const int colour_burst_end = std::min(first_sync_column + 7, ending_column); if(colour_burst_end > colour_burst_start) { - crt_->output_default_colour_burst(static_cast(colour_burst_end - colour_burst_start) * 7); + crt_->output_default_colour_burst(static_cast(colour_burst_end - colour_burst_start) * 14); } second_blank_start = std::max(first_sync_column + 7, column_); @@ -192,7 +229,7 @@ template class Video: public VideoBase { } if(ending_column > second_blank_start) { - crt_->output_blank(static_cast(ending_column - second_blank_start) * 7); + crt_->output_blank(static_cast(ending_column - second_blank_start) * 14); } } @@ -204,7 +241,7 @@ template class Video: public VideoBase { // Add an extra half a colour cycle of blank; this isn't counted in the run_for // count explicitly but is promised. - crt_->output_blank(1); + crt_->output_blank(2); } } } @@ -261,8 +298,10 @@ template class Video: public VideoBase { const int flash_length = 8406; BusHandler &bus_handler_; - void output_data(unsigned int length) { - crt_->output_data((length*7)/2, length); + void output_data_to_column(int column) { + int length = column - pixel_pointer_column_; + crt_->output_data(static_cast(length*14), static_cast(length * (pixels_are_high_density_ ? 14 : 7))); + pixel_pointer_ = nullptr; } }; From d380595ad49d88d6272b7f8bd5be00619ca680ac Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 3 Jun 2018 07:27:03 -0400 Subject: [PATCH 8/9] Unrolls the loops for slightly fewer conditionals. --- Machines/AppleII/Video.cpp | 15 ------------ Machines/AppleII/Video.hpp | 47 +++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/Machines/AppleII/Video.cpp b/Machines/AppleII/Video.cpp index 13070ff57..b190e72a0 100644 --- a/Machines/AppleII/Video.cpp +++ b/Machines/AppleII/Video.cpp @@ -57,19 +57,4 @@ void VideoBase::set_high_resolution() { void VideoBase::set_character_rom(const std::vector &character_rom) { character_rom_ = character_rom; - - // Bytes in the character ROM are stored in reverse bit order. Reverse them - // ahead of time so as to be able to use the same scaling table as for - // high-resolution graphics. - for(auto &byte : character_rom_) { - byte = - ((byte & 0x40) ? 0x01 : 0x00) | - ((byte & 0x20) ? 0x02 : 0x00) | - ((byte & 0x10) ? 0x04 : 0x00) | - ((byte & 0x08) ? 0x08 : 0x00) | - ((byte & 0x04) ? 0x10 : 0x00) | - ((byte & 0x02) ? 0x20 : 0x00) | - ((byte & 0x01) ? 0x40 : 0x00) | - (byte & 0x80); - } } diff --git a/Machines/AppleII/Video.hpp b/Machines/AppleII/Video.hpp index fd7f91d98..e5d2f97df 100644 --- a/Machines/AppleII/Video.hpp +++ b/Machines/AppleII/Video.hpp @@ -130,11 +130,14 @@ template class Video: public VideoBase { const uint8_t character_pattern = character_rom_[character_address] ^ inverses[character >> 6]; - int mask = 0x01; - for(int p = 0; p < 7; ++p) { - pixel_pointer_[p] = character_pattern & mask; - mask <<= 1; - } + // The character ROM is output MSB to LSB rather than LSB to MSB. + pixel_pointer_[0] = character_pattern & 0x40; + pixel_pointer_[1] = character_pattern & 0x20; + pixel_pointer_[2] = character_pattern & 0x10; + pixel_pointer_[3] = character_pattern & 0x08; + pixel_pointer_[4] = character_pattern & 0x04; + pixel_pointer_[5] = character_pattern & 0x02; + pixel_pointer_[6] = character_pattern & 0x01; graphics_carry_ = character_pattern & 0x40; pixel_pointer_ += 7; } @@ -142,9 +145,12 @@ template class Video: public VideoBase { case GraphicsMode::LowRes: { const int row_shift = (row_&4); + // TODO: decompose into two loops, possibly. for(int c = column_; c < pixel_end; ++c) { const uint8_t nibble = (bus_handler_.perform_read(static_cast(text_address + c)) >> row_shift) & 0x0f; + // Low-resolution graphics mode shifts the colour code on a loop, but has to account for whether this + // 14-sample output window is starting at the beginning of a colour cycle or halfway through. if(c&1) { pixel_pointer_[0] = pixel_pointer_[4] = pixel_pointer_[8] = pixel_pointer_[12] = nibble & 4; pixel_pointer_[1] = pixel_pointer_[5] = pixel_pointer_[9] = pixel_pointer_[13] = nibble & 8; @@ -167,23 +173,28 @@ template class Video: public VideoBase { for(int c = column_; c < pixel_end; ++c) { const uint8_t graphic = bus_handler_.perform_read(static_cast(graphics_address + c)); + // High resolution graphics shift out LSB to MSB, optionally with a delay of half a pixel. + // If there is a delay, the previous output level is held to bridge the gap. if(graphic & 0x80) { pixel_pointer_[0] = graphics_carry_; - pixel_pointer_++; - } - int mask = 0x01; - for(int p = 0; p < 12; p += 2) { - pixel_pointer_[p] = graphic & mask; - pixel_pointer_[p+1] = graphic & mask; - mask <<= 1; - } - pixel_pointer_[12] = graphic & 0x40; - pixel_pointer_ += 13; - if(!(graphic & 0x80)) { - pixel_pointer_[0] = graphic & 0x40; - pixel_pointer_++; + pixel_pointer_[1] = pixel_pointer_[2] = graphic & 0x01; + pixel_pointer_[3] = pixel_pointer_[4] = graphic & 0x02; + pixel_pointer_[5] = pixel_pointer_[6] = graphic & 0x04; + pixel_pointer_[7] = pixel_pointer_[8] = graphic & 0x08; + pixel_pointer_[9] = pixel_pointer_[10] = graphic & 0x10; + pixel_pointer_[11] = pixel_pointer_[12] = graphic & 0x20; + pixel_pointer_[13] = graphic & 0x40; + } else { + pixel_pointer_[0] = pixel_pointer_[1] = graphic & 0x01; + pixel_pointer_[2] = pixel_pointer_[3] = graphic & 0x02; + pixel_pointer_[4] = pixel_pointer_[5] = graphic & 0x04; + pixel_pointer_[6] = pixel_pointer_[7] = graphic & 0x08; + pixel_pointer_[8] = pixel_pointer_[9] = graphic & 0x10; + pixel_pointer_[10] = pixel_pointer_[11] = graphic & 0x20; + pixel_pointer_[12] = pixel_pointer_[13] = graphic & 0x40; } graphics_carry_ = graphic & 0x40; + pixel_pointer_ += 14; } } break; } From 076fa55651bc1390f5524ac6ef66fa7ca827a996 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 3 Jun 2018 08:11:17 -0400 Subject: [PATCH 9/9] Corrects: flux set is no-flux incoming. This restores good sleeping behaviour. --- Components/DiskII/DiskII.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Components/DiskII/DiskII.cpp b/Components/DiskII/DiskII.cpp index ef2093fd1..758ca4883 100644 --- a/Components/DiskII/DiskII.cpp +++ b/Components/DiskII/DiskII.cpp @@ -124,9 +124,9 @@ void DiskII::decide_clocking_preference() { // If in read mode, clocking is either: // // just-in-time, if drives are running or the shift register has any 1s in it or a flux event hasn't yet passed; or - // none, given that drives are not running, the shift register has already emptied and there's no flux about to fire. + // none, given that drives are not running, the shift register has already emptied and there's no flux about to be received. if(!(inputs_ & ~input_flux)) { - clocking_preference_ = (!motor_is_enabled_ && !shift_register_ && !(inputs_&input_flux)) ? ClockingHint::Preference::None : ClockingHint::Preference::JustInTime; + clocking_preference_ = (!motor_is_enabled_ && !shift_register_ && (inputs_&input_flux)) ? ClockingHint::Preference::None : ClockingHint::Preference::JustInTime; } // If in writing mode, clocking is real time.