From d8693d924e35a0f468d91dc60ce6fbffd2dd38e8 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: Fri, 8 Mar 2024 11:26:27 +0100
Subject: [PATCH] Added CPU autodetection

---
 src/.uviemon_history |  5 +++++
 src/address_map.h    | 22 ++++++++++++++++++++++
 src/adress_map.c     |  1 +
 src/ftdi_device.cpp  | 20 ++++++++++++++++++++
 src/uviemon.cpp      |  3 ++-
 5 files changed, 50 insertions(+), 1 deletion(-)
 create mode 100644 src/adress_map.c

diff --git a/src/.uviemon_history b/src/.uviemon_history
index 36dc45d..8a570aa 100644
--- a/src/.uviemon_history
+++ b/src/.uviemon_history
@@ -71,3 +71,8 @@ run
 load ../../test_2
 run
 exit
+mem 0xFFFFF000
+quit
+exit
+exit
+exit
diff --git a/src/address_map.h b/src/address_map.h
index 278c934..1332ced 100644
--- a/src/address_map.h
+++ b/src/address_map.h
@@ -18,4 +18,26 @@ const uint32_t ADDRESSES[SUPPORTED_CPUS][DEFINED_ADDRESSES] = {
 	{ 0x40000000, 0xFF900000, 0xE0000000, 0xFF904010 }
 };
 
+
+#define AHB_PNP 0xFFFFF000
+
+#define DEV_GAISLER_LEON3        0x003
+#define DEV_GAISLER_LEON3DSU     0x004
+#define DEV_GAISLER_LEON4        0x048
+#define DEV_GAISLER_LEON4DSU     0x049
+#define DEV_GAISLER_LEON3FT      0x053
+#define KNOWN_CPUS               5
+
+const uint32_t CPU_MAP[KNOWN_CPUS][2] = {
+	{ DEV_GAISLER_LEON3, 0 },
+	{ DEV_GAISLER_LEON3DSU, 0 },
+	{ DEV_GAISLER_LEON4, 1 },
+	{ DEV_GAISLER_LEON4DSU, 1 },
+	{ DEV_GAISLER_LEON3FT, 0 }
+};
+
+extern const char *CPU_NAMES[];
+
+#define amba_pnp_device(id)  (((id) >> 12) & 0xfff)
+
 #endif
diff --git a/src/adress_map.c b/src/adress_map.c
new file mode 100644
index 0000000..2974339
--- /dev/null
+++ b/src/adress_map.c
@@ -0,0 +1 @@
+const char* CPU_NAMES[] = { "LEON 3", "LEON 4" };
diff --git a/src/ftdi_device.cpp b/src/ftdi_device.cpp
index 8df90bf..90df503 100644
--- a/src/ftdi_device.cpp
+++ b/src/ftdi_device.cpp
@@ -71,6 +71,26 @@ FT_STATUS open_device(DWORD device_index, int cpu_type)
 		return ftStatus;
 	}
 
+	// Try to autodetect CPU type
+	if (device.cpu_type == -1) {
+		printf("Autodetecting CPU...");
+		DWORD cpu = amba_pnp_device(ioread32(AHB_PNP));
+
+		for(int i = 0; i < KNOWN_CPUS; i++) {
+			if (cpu == CPU_MAP[i][0]) {
+				device.cpu_type = CPU_MAP[i][1];
+				printf("%s CPU found!\n", CPU_NAMES[device.cpu_type]);
+				break;
+			}
+		}
+
+		if (device.cpu_type == -1) {
+			printf("Unknown cpu type: %03x ... Try to explicitly indicate a cpu with -cpu_type\n", cpu);
+			exit(-1);
+		}
+		
+	}
+
 	// Move this to another step?
 	init_core_1(); // Initialize core 1 (this will run all the programs)
 	//_initCore2();  // Initialize core 2 (this will be idle)
diff --git a/src/uviemon.cpp b/src/uviemon.cpp
index 2c08847..4680bce 100644
--- a/src/uviemon.cpp
+++ b/src/uviemon.cpp
@@ -87,6 +87,7 @@ void showHelp()
 	printf("\t -help: \t This list of all available commands\n");
 	printf("\t -info: \t Version numbers and driver info\n");
 	printf("\t -list: \t List all available FTDI devices\n");
+	printf("\t -cpu_tye <num>: \t 0 for LEON 3 and 1 for LEON4 autodetection used of omitted \n");
 	printf("\t -jtag <num>: \t Open console with jtag device\n\n");
 }
 
@@ -110,7 +111,7 @@ int main(int argc, char *argv[])
 	}
 
 	int i = 1;
-	int cpu_type = 0;
+	int cpu_type = -1;
 	int device_index = 0;
 
 	while(i < argc) {
-- 
GitLab