From 95e00dd9580e06a516978f78f6731b16e682f8c4 Mon Sep 17 00:00:00 2001
From: Thomas Harte <thomas.harte@gmail.com>
Date: Fri, 27 Jan 2023 22:20:36 -0500
Subject: [PATCH] Take slightly wrong-headed steps towards proper command
 timing.

---
 Components/9918/Implementation/9918.cpp     | 11 ++++++-
 Components/9918/Implementation/9918Base.hpp | 32 +++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/Components/9918/Implementation/9918.cpp b/Components/9918/Implementation/9918.cpp
index 3a0c9815e..5da9d04a0 100644
--- a/Components/9918/Implementation/9918.cpp
+++ b/Components/9918/Implementation/9918.cpp
@@ -159,7 +159,7 @@ void TMS9918<personality>::run_for(const HalfCycles cycles) {
 #endif
 
 		if(fetch_cycles_pool) {
-			// Determine how much writing to do.
+			// Determine how much writing to do; at the absolute most go to the end of this line.
 			const int fetch_cycles = std::min(
 				Timing<personality>::CyclesPerLine - this->fetch_pointer_.column,
 				fetch_cycles_pool
@@ -171,6 +171,15 @@ void TMS9918<personality>::run_for(const HalfCycles cycles) {
 			this->minimum_access_column_ = this->fetch_pointer_.column + this->cycles_until_access_;
 			this->cycles_until_access_ -= fetch_cycles;
 
+			// ... and to any pending Yamaha commands.
+			if constexpr (is_yamaha_vdp(personality)) {
+				if(Storage<personality>::command_) {
+					Storage<personality>::minimum_command_column_ =
+						this->fetch_pointer_.column + Storage<personality>::command_->cycles;
+					Storage<personality>::command_->cycles -= fetch_cycles;
+				}
+			}
+
 
 			// ---------------------------------------
 			// Latch scrolling position, if necessary.
diff --git a/Components/9918/Implementation/9918Base.hpp b/Components/9918/Implementation/9918Base.hpp
index 5032ebb64..d0f8a164b 100644
--- a/Components/9918/Implementation/9918Base.hpp
+++ b/Components/9918/Implementation/9918Base.hpp
@@ -187,6 +187,27 @@ template <Personality personality> struct Storage<personality, std::enable_if_t<
 	CommandContext command_context_;
 	std::unique_ptr<Command> command_ = nullptr;
 
+	enum class CommandStep {
+		None,
+		ReadPixel,
+		WritePixel,
+	};
+	CommandStep next_command_step_ = CommandStep::None;
+	int minimum_command_column_ = 0;
+
+	void update_command_step() {
+		if(!command_) {
+			next_command_step_ = CommandStep::None;
+			return;
+		}
+
+		switch(command_->access) {
+			case Command::AccessType::PlotPoint:
+				next_command_step_ = CommandStep::ReadPixel;
+			break;
+		}
+	}
+
 	Storage() noexcept {
 		// Perform sanity checks on the event lists.
 #ifndef NDEBUG
@@ -552,6 +573,17 @@ template <Personality personality> struct Base: public Storage<personality> {
 		// Don't do anything if the required time for the access to become executable
 		// has yet to pass.
 		if(queued_access_ == MemoryAccess::None || access_column < minimum_access_column_) {
+			if constexpr (is_yamaha_vdp(personality)) {
+//				if(
+//					Storage<personality>::next_command_step_ == Storage<personality>::CommandStep::None ||
+//					access_column < Storage<personality>::minimum_access_column_
+//				) {
+//					return;
+//				}
+
+				// TODO: command-engine action.
+			}
+
 			return;
 		}