From f4924e98567056ac3c1e9caef67783a7992d07b0 Mon Sep 17 00:00:00 2001
From: notaz <notasas@gmail.com>
Date: Sun, 26 Sep 2021 20:44:32 +0300
Subject: [PATCH] add adapter handling for HiROM

---
 flashkit.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 3 deletions(-)

diff --git a/flashkit.c b/flashkit.c
index 843decb..fb64293 100644
--- a/flashkit.c
+++ b/flashkit.c
@@ -439,11 +439,81 @@ static void flash_seq_write8las(int fd, uint32_t a, const uint8_t *d)
 {
 }
 
+// -- 8bit+adapter --
+
+static void set_addr8a(int fd, uint32_t a)
+{
+	set_addr8(fd, do_flipflops(fd, a));
+}
+
+static uint16_t read_bus8a(int fd, uint32_t a)
+{
+	return read_bus8(fd, do_flipflops(fd, a));
+}
+
+static void write_bus8a(int fd, uint32_t a, uint16_t d)
+{
+	write_bus8(fd, do_flipflops(fd, a), d);
+}
+
+static void flash_seq_write8a(int fd, uint32_t a, const uint8_t *d)
+{
+	// no flipflop clearing, see flash_seq_write8la
+	a = do_flipflops(fd, a);
+	uint8_t cmd[] = {
+		// unlock
+		CMD_ADDR, 0,
+		CMD_ADDR, 0x8a,
+		CMD_ADDR, 0xaa,
+		CMD_WR | PAR_SINGE | PAR_MODE8, 0xaa,
+		CMD_ADDR, 0,
+		CMD_ADDR, 0x85,
+		CMD_ADDR, 0x55,
+		CMD_WR | PAR_SINGE | PAR_MODE8, 0x55,
+		// program setup
+		CMD_ADDR, 0,
+		CMD_ADDR, 0x8a,
+		CMD_ADDR, 0xaa,
+		CMD_WR | PAR_SINGE | PAR_MODE8, 0xa0,
+		// program data
+		CMD_ADDR, a >> 16,
+		CMD_ADDR, a >> 8,
+		CMD_ADDR, a >> 0,
+		CMD_WR | PAR_SINGE | PAR_MODE8, *d,
+		CMD_RY
+	};
+
+	write_serial(fd, cmd, sizeof(cmd));
+}
+
+// -- 8bit+adapter+nocart --
+
+static void set_addr8an(int fd, uint32_t a)
+{
+	set_addr8(fd, do_flipflops(fd, a) | 0x200000);
+}
+
+static uint16_t read_bus8an(int fd, uint32_t a)
+{
+	return read_bus8(fd, do_flipflops(fd, a) | 0x200000);
+}
+
+static void write_bus8an(int fd, uint32_t a, uint16_t d)
+{
+	write_bus8(fd, do_flipflops(fd, a) | 0x200000, d);
+}
+
+static void flash_seq_write8an(int fd, uint32_t a, const uint8_t *d)
+{
+}
+
 #define N0 ""
 #define N1 "8bit"
 #define N2 "8bit+LoROM"
 #define N3 "8bit+LoROM+adapter"
 #define N4 "8bit+LoROM+adapter+sram"
+#define N5 "8bit+adapter (use '-a 0x400000' for HiROM)"
+#define N6 "8bit+adapter+nocart"
 static const struct iof
 {
 	const char *name;
@@ -461,6 +531,8 @@ io_ops[] =
 	{ N2, 1, set_addr8l,   read_bus8l,   write_bus8l,   read_block8,  flash_seq_write8l   },
 	{ N3, 1, set_addr8la,  read_bus8la,  write_bus8la,  read_block8,  flash_seq_write8la  },
 	{ N4, 0, set_addr8las, read_bus8las, write_bus8las, read_block8,  flash_seq_write8las },
+	{ N5, 0, set_addr8a,   read_bus8a,   write_bus8a,   read_block8,  flash_seq_write8a   },
+	{ N6, 0, set_addr8an,  read_bus8an,  write_bus8an,  read_block8,  flash_seq_write8an  },
 };
 
 static const struct iof *io = &io_ops[0];
@@ -962,10 +1034,13 @@ int main(int argc, char *argv[])
 
 		printf("reading %ld bytes:\n", max(size_r, size_v));
 		print_progress(0, blks * sizeof(g_block));
-		io->set_addr(fd, address_in);
+		a_blk = -1;
 		for (done = 0; done < size_r || done < size_v; ) {
-			if (io->addrs_remapped)
-				io->set_addr(fd, address_in + done);
+			a = address_in + done;
+			if (io->addrs_remapped || (a >> 21) != a_blk) {
+				a_blk = a >> 21;
+				io->set_addr(fd, a);
+			}
 			io->read_block(fd, g_block, sizeof(g_block));
 			if (f_r && done < size_r) {
 				len = min(size_r - done, sizeof(g_block));
-- 
2.39.5