From 5aa885bb00a76a6be066a164187c8c2a065d6fab Mon Sep 17 00:00:00 2001
From: mrb0nk500 <b0nk@b0nk.xyz>
Date: Wed, 2 Feb 2022 14:53:54 -0400
Subject: sux.h: Added a `tick()` function.

This function will increment the cycle count by one
everytime it's called.

Later on, this'll help simplify I/O emulation, as I've
considered implementing read, write, and tick callbacks
for I/O.
---
 sux.h | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/sux.h b/sux.h
index efda8f0..18087a0 100644
--- a/sux.h
+++ b/sux.h
@@ -357,11 +357,25 @@ static void *memcopy(void *restrict dst, const void *restrict src, unsigned int
 	return dst;
 }
 
-
-static /*inline*/ uint64_t read_value(struct sux *cpu, uint64_t reg, uint64_t address, uint8_t size, uint8_t inc_clk, uint8_t check_io) {
+static void tick(struct sux *cpu, uint8_t inc_clk) {
 	#if getclk
 	cpu->clk += inc_clk;
 	#endif
+	#if (IO || debug) && !branch
+	#if keypoll
+	pthread_mutex_lock(&mutex);
+	#endif
+	get_key(scr, 0, cpu->clk);
+	#if keypoll
+	pthread_mutex_unlock(&mutex);
+	#endif
+	#endif
+}
+
+static /*inline*/ uint64_t read_value(struct sux *cpu, uint64_t reg, uint64_t address, uint8_t size, uint8_t inc_clk, uint8_t check_io) {
+	if (inc_clk) {
+		tick(cpu, inc_clk);
+	}
 	size = (size > 7) ? 7 : size;
 	uint64_t mask = (-(uint64_t)1 >> ((7 - size) * 8));
 	if (address < mem_size) {
@@ -415,9 +429,9 @@ static /*inline*/ void write_value(struct sux *cpu, uint64_t value, uint64_t add
 		#endif
 		#endif
 	}
-	#if getclk
-	cpu->clk += inc_clk;
-	#endif
+	if (inc_clk) {
+		tick(cpu, inc_clk);
+	}
 }
 
 static /*inline*/ uint64_t offset_addr(struct sux *cpu, uint64_t offset, uint8_t size, uint8_t inc_clk, uint8_t prefix) {
-- 
cgit v1.2.3-13-gbd6f