From 0efe4b312cc5465dc1b7f657e14a0352f0c1bc88 Mon Sep 17 00:00:00 2001
From: Thomas Harte <thomas.harte@gmail.com>
Date: Thu, 21 Jan 2016 22:16:52 -0500
Subject: [PATCH] Disabled my various bits of rate interchange debugging;
 improved test for when to call update_display due to a RAM write.

---
 Machines/Electron/Electron.cpp                | 21 +++++++++----
 .../Mac/Clock Signal/Wrappers/CSElectron.mm   |  8 ++---
 Outputs/Speaker.hpp                           | 30 +++++++++----------
 3 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/Machines/Electron/Electron.cpp b/Machines/Electron/Electron.cpp
index 5bf777fa8..2048133a1 100644
--- a/Machines/Electron/Electron.cpp
+++ b/Machines/Electron/Electron.cpp
@@ -17,6 +17,8 @@ static const unsigned int cycles_per_frame = 312*cycles_per_line;
 static const unsigned int crt_cycles_multiplier = 8;
 static const unsigned int crt_cycles_per_line = crt_cycles_multiplier * cycles_per_line;
 
+const int first_graphics_line = 28;
+
 Machine::Machine() :
 	_interruptControl(0),
 	_frameCycles(0),
@@ -52,20 +54,28 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin
 		}
 		else
 		{
-			// TODO: range check on address; a lot of the time the machine will be running code outside of
-			// the screen area, meaning that no update is required.
-			update_display();
+			// If we're still before the display will start to be painted, or the address is
+			// less than both the current line address and 0x3000, (the minimum screen mode
+			// base address) then there's no way this write can affect the current frame. Sp
+			// no need to flush the display. Otherwise, output up until now so that any
+			// write doesn't have retroactive effect on the video output.
+			if(!(
+				(_frameCycles < first_graphics_line * cycles_per_line) ||
+				(address < _startLineAddress && address < 0x3000)
+			))
+				update_display();
 
 			_ram[address] = *value;
 		}
 
-		// TODO: RAM timing for Modes 0–3
+		// for the entire frame, RAM is accessible only on odd cycles; in modes below 4
+		// it's also accessible only outside of the pixel regions
 		cycles += (_frameCycles&1)^1;
 		if(_screenMode < 4)
 		{
 			const int current_line = _frameCycles >> 7;
 			const int line_position = _frameCycles & 127;
-			if(current_line >= 28 && current_line < 28+256 && line_position >= 24 && line_position < 104)
+			if(current_line >= first_graphics_line && current_line < first_graphics_line+256 && line_position >= 24 && line_position < 104)
 				cycles = (unsigned int)(104 - line_position);
 		}
 	}
@@ -410,7 +420,6 @@ inline void Machine::update_display()
 {
 	const int lines_of_hsync = 3;
 	const int end_of_hsync = lines_of_hsync * cycles_per_line;
-	const int first_graphics_line = 28;
 
 	if(_frameCycles >= end_of_hsync)
 	{
diff --git a/OSBindings/Mac/Clock Signal/Wrappers/CSElectron.mm b/OSBindings/Mac/Clock Signal/Wrappers/CSElectron.mm
index e04ce5154..54ce70b87 100644
--- a/OSBindings/Mac/Clock Signal/Wrappers/CSElectron.mm	
+++ b/OSBindings/Mac/Clock Signal/Wrappers/CSElectron.mm	
@@ -15,12 +15,12 @@
 @implementation CSElectron {
 	Electron::Machine _electron;
 
-	NSTimeInterval _periodicStart;
-	int _numberOfCycles;
+//	NSTimeInterval _periodicStart;
+//	int _numberOfCycles;
 }
 
 - (void)doRunForNumberOfCycles:(int)numberOfCycles {
-	_numberOfCycles += numberOfCycles;
+/*	_numberOfCycles += numberOfCycles;
 	NSTimeInterval timeNow = [NSDate timeIntervalSinceReferenceDate];
 	NSTimeInterval difference = timeNow - _periodicStart;
 	if(difference > 1.0)
@@ -28,7 +28,7 @@
 		NSLog(@"cycles: %0.0f", (double)_numberOfCycles / difference);
 		_periodicStart = timeNow;
 		_numberOfCycles = 0;
-	}
+	}*/
 	_electron.run_for_cycles(numberOfCycles);
 }
 
diff --git a/Outputs/Speaker.hpp b/Outputs/Speaker.hpp
index 00faf36b3..7a9d11b8e 100644
--- a/Outputs/Speaker.hpp
+++ b/Outputs/Speaker.hpp
@@ -77,16 +77,16 @@ template <class T> class Filter: public Speaker {
 		{
 			if(_coefficients_are_dirty) update_filter_coefficients();
 
-			_periodic_cycles += input_cycles;
-			time_t time_now = time(nullptr);
-			if(time_now > _periodic_start)
-			{
-				printf("input audio samples: %d\n", _periodic_cycles);
-				printf("output audio samples: %d\n", _periodic_output);
-				_periodic_cycles = 0;
-				_periodic_output = 0;
-				_periodic_start = time_now;
-			}
+//			_periodic_cycles += input_cycles;
+//			time_t time_now = time(nullptr);
+//			if(time_now > _periodic_start)
+//			{
+//				printf("input audio samples: %d\n", _periodic_cycles);
+//				printf("output audio samples: %d\n", _periodic_output);
+//				_periodic_cycles = 0;
+//				_periodic_output = 0;
+//				_periodic_start = time_now;
+//			}
 
 			// point sample for now, as a temporary measure
 			input_cycles += _input_cycles_carry;
@@ -111,17 +111,17 @@ template <class T> class Filter: public Speaker {
 				if(steps > 1)
 					static_cast<T *>(this)->skip_samples((unsigned int)(steps-1));
 				input_cycles -= steps;
-				_periodic_output ++;
+//				_periodic_output ++;
 			}
 			_input_cycles_carry = input_cycles;
 		}
 
-		Filter() : _periodic_cycles(0), _periodic_start(0) {}
+		Filter() {} // _periodic_cycles(0), _periodic_start(0)
 
 	private:
-				time_t _periodic_start;
-				int _periodic_cycles;
-				int _periodic_output;
+//				time_t _periodic_start;
+//				int _periodic_cycles;
+//				int _periodic_output;
 		SignalProcessing::Stepper *_stepper;
 		int _input_cycles_carry;