From a28bcebdb6106a2b1eb19342dbb9a5145b973f4d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johann=20B=C3=B6hm?= <johann.boehm@univie.ac.at>
Date: Fri, 24 May 2024 07:32:55 +0200
Subject: [PATCH] Bugfixes

---
 src/.uviemon_history | 295 +++++++++++++++++++++++++++++++++++++++++++
 src/ftdi_device.cpp  |   6 +-
 src/uviemon_cli.cpp  | 201 ++++++++++++++++++++---------
 src/uviemon_cli.hpp  |   6 +-
 4 files changed, 442 insertions(+), 66 deletions(-)

diff --git a/src/.uviemon_history b/src/.uviemon_history
index ae47117..0ed4677 100644
--- a/src/.uviemon_history
+++ b/src/.uviemon_history
@@ -124,3 +124,298 @@ inst
 inst
 inst
 inst
+inst
+reg
+reg
+reg
+reg
+reg
+reg
+reg
+reg
+reg
+reg
+reg i0
+reg i0
+reg
+reg o6
+reg i0
+reg i0
+reg 
+reg 06
+reg o6
+reg
+reg w0
+reg w1
+reg w2
+reg w3
+reg
+reg psr
+reg tbr
+reg wim
+reg y
+reg pc
+reg npy
+reg npc
+reg wim
+reg fsr
+reg wim
+reg
+reg
+reg f0
+reg d0
+fsr
+reg fsr
+reg
+reg f0
+ref f1
+reg f1
+reg d0
+reg
+reg f12
+mem 0xe0301000 128
+reg f0
+reg
+reg f0
+ref f1
+reg f1
+reg fß
+reg f0
+reg f1
+reg f0
+reg f0
+mem 0xe0301000
+reg f0
+reg
+reg f0
+reg f0
+reg f0
+reg f0
+reg d0
+reg d0
+reg d0
+reg d0
+reg d1
+reg d0
+reg d1
+reg 0
+d
+d
+reg d0
+reg d1
+reg d2
+reg d10
+reg d10
+reg d0
+reg d10
+reg d0
+reg d1
+reg d2
+reg d3
+reg f0
+reg
+reg g0
+reg g0 1
+reg g0
+reg
+reg g0 
+reg g0 1
+reg g0
+reg g0
+reg g0 1
+reg
+reg d0 1
+help
+reg
+wmem 0xe0300200 1
+reg
+reg g0 1
+reg
+wmem 0xe0300200 1
+reg
+reg
+reg i0 2
+reg
+reg
+reg g1 2
+reg
+reg g2 2
+reg
+reg
+mem 0xe0300000
+mem 0xe0300000 64
+mem 0xe0300000 62
+reg
+reg fsr
+reg psr
+reg
+reg tbr
+reg tbr
+reg
+reg y
+reg y 1
+reg y
+reg
+reg wim
+reg pc
+reg npc
+reg fsr
+reg sp
+reg fp
+reg sp
+reg fp
+reg
+reg
+reg i0
+reg i0 1
+reg
+reg
+reg w0
+reg w0i0
+reg w0i0 2
+reg
+reg
+reg f0
+reg f0
+reg f0
+reg f1
+reg f2
+reg f0
+reg f0 1
+reg d0
+reg d1
+reg f0
+reg d0
+reg f0
+reg f0 0.25
+reg f0
+reg f0
+reg f0 0.25
+reg f0
+reg d0
+reg d0
+reg d0
+reg d0 0.5
+reg d0 0.5
+reg d0 0,5
+reg d0
+reg d0 0.25
+reg d0
+reg d0 0.25
+reg d0
+cpu
+cpu
+cpu
+cpu
+cpu
+cpu enable 1
+cpu
+cpu disable 1
+cpu
+cpu active 1
+cpu
+cpu
+cpu enable 1
+cpu
+cpu disable 1
+cpu
+cpu enable 1
+cpu
+cpu active 1
+cpu 
+cpu
+cpu active 1
+cpu
+cpu active 1
+cpu
+cpu active 0
+cpu
+inst
+inst
+cpu
+reg
+inst
+inst 20
+mem 0xe00000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+mem 0xe0000000
+memh 0xe0000000
+memb 0xe0000000
+memh 0xe0000000
+mem 0xe0000000
+help
+mem 0x40000000
+memh 0x40000000
+memb 0x40000000
+help
+memb 0x40000000
+mem 0x40000000
+wash 4 0x40000000 0xDECAFBAD
+mem 0x40000000
+help
+reg
+cpu
+cpu active 1
+cpu
+cpu enable 2
+cpu
+scan
+help
+scan
+scan
+scan
+scan
+load ../../test_1
+mem 0x400028d0
+mem 0x400028d0 64
+run
+load ../../test_2
+run
+load ../../test_1
+mem 0x400028d0
+run
+mem 0x400028d0
+reset 0
+load ../../test_1
+mem 0x400028d0
+run
+mem 0x400028d0
+load ../../test_2
+run
+load ../../test_2
+run
+load ../../test_1
+mem 0x400028d0
+run
+mem 0x400028d0
+help
+load ../../test_1
+mem 0x400028d0 64
+run
+mem 0x400028d0 64
+load ../../test_2
+run
+reg
+cpu
diff --git a/src/ftdi_device.cpp b/src/ftdi_device.cpp
index 76a70d8..e914a07 100644
--- a/src/ftdi_device.cpp
+++ b/src/ftdi_device.cpp
@@ -54,7 +54,7 @@ FT_STATUS ftdi_open_device(DWORD device_index, int cpu_type)
 		unsigned long minorVer = (driver_version >> 8) & 0xFF;
 		unsigned long buildVer = driver_version & 0xFF;
 
-		printf("Device driver version: %lu.%lu.%lu", majorVer, minorVer, buildVer);
+		printf("Device driver version: %lu.%lu.%lu\n", majorVer, minorVer, buildVer);
 	} else {
 		fprintf(stderr, "Cannot get driver version for device %d\n", device_index);
 		return ftStatus;
@@ -986,12 +986,12 @@ void scan_instruction_codes(BYTE bitLengthIR)
 		if (length != 0)
 		{
 			//cout << "- DR length for address 0x" << hex << uppercase << (unsigned int)i << ": " << dec << length << " bit" << endl;
-			printf("- DR length for address %#010x: %d bit", (unsigned int) i, length);
+			printf("- DR length for address %#010x: %d bit\n", (unsigned int) i, length);
 			num_instructions ++;
 		}
 	}
 
-	printf("Scan complete! Found %d instructions.", num_instructions);
+	printf("Scan complete! Found %d instructions.\n", num_instructions);
 }
 
 BYTE scan_DR_length(BYTE opcode)
diff --git a/src/uviemon_cli.cpp b/src/uviemon_cli.cpp
index 18f3d71..ea12409 100644
--- a/src/uviemon_cli.cpp
+++ b/src/uviemon_cli.cpp
@@ -13,14 +13,15 @@
 
 #include "address_map.h"
 #include "uviemon_reg.h"
+//#include "uviemon_opcode.h"
 
 #include <iostream> // cout & cerr
 #include <iomanip>	// Used for setfill and setw (cout formatting)
 #include <fstream>	// For loading a file in load()
 #include <cmath>	// For std::ceil in load()
-
 #include <stdlib.h>
 
+
 #ifndef PACKAGE
   #define PACKAGE
   #ifndef PACKAGE_VERSION
@@ -86,7 +87,9 @@ static void print_value_error_msg(const char * const value);
 //static int parse_register_number(const char * const reg, uint32_t * const reg_num, uint32_t highest_register);
 static const char * const get_tt_error_desc(uint32_t error_code);
 
-#define TT_ERROR_COUNT 32
+static void hex_to_string_8(const uint8_t * const data, char *output, size_t size);
+static void hex_to_string_16(const uint16_t * const data, char *output, size_t size);
+static void hex_to_string_32(const uint32_t * const data, char *output, size_t size);
 
 struct tt_error tt_errors[] = {
 	{0x0, "[reset]: Power-on reset"},
@@ -125,7 +128,73 @@ struct tt_error tt_errors[] = {
 };
 
 
-string _hexToString(DWORD *data, size_t size)
+static void hex_to_string_8(const uint8_t * const data, char *output, size_t size)
+{
+	char value;
+	size_t i;
+	
+	for(i = 0; i < size; i++) {
+		value = (char)data[i];
+
+		if (value >= 32 && value <= 126)
+			output[i] = value;
+		else
+			output[i] = '.';
+	}
+
+	output[i] = '\0';
+}
+
+static void hex_to_string_16(const uint16_t * const data, char *output, size_t size)
+{
+	char value[2];
+	size_t i;
+	
+	for(i = 0; i < size; i++) {
+		value[0] = (char)((data[i] >> 8) & 0xFF);
+		value[1] = (char)(data[i] & 0xFF);
+
+		for(uint32_t j = 0; j < 2; j++) {
+			if (value[j] >= 32 && value[j] <= 126)
+				output[i * 2 + j] = value[j];
+			else
+				output[i * 2 + j] = '.';
+		}
+		
+		
+	}
+
+	output[i * 2] = '\0';
+}
+
+
+static void hex_to_string_32(const uint32_t * const data, char *output, size_t size)
+{
+	char value[4];
+	size_t i;
+	
+	for(i = 0; i < size; i++) {
+		value[0] = (char)((data[i] >> 24) & 0xFF);
+		value[1] = (char)((data[i] >> 16) & 0xFF);
+		value[2] = (char)((data[i] >> 8) & 0xFF);
+		value[3] = (char)(data[i] & 0xFF);
+
+		for(uint32_t j = 0; j < 4; j++) {
+			if (value[j] >= 32 && value[j] <= 126)
+				output[i * 4 + j] = value[j];
+			else
+				output[i * 4 + j] = '.';
+		}
+		
+		
+	}
+
+	output[i * 4] = '\0';
+}
+
+
+
+/*string _hexToString(DWORD *data, size_t size)
 {
 	BYTE byteArray[size * sizeof(data[0])];
 
@@ -133,7 +202,7 @@ string _hexToString(DWORD *data, size_t size)
 	{
 		byteArray[i * 4] = (BYTE)((data[i] >> 24) & 0xFF);
 		byteArray[i * 4 + 1] = (BYTE)((data[i] >> 16) & 0xFF);
-		byteArray[i * 4 + 2] = (BYTE)((data[i] >> 8) & 0xFF);
+ 		byteArray[i * 4 + 2] = (BYTE)((data[i] >> 8) & 0xFF);
 		byteArray[i * 4 + 3] = (BYTE)(data[i] & 0xFF);
 	}
 
@@ -204,7 +273,7 @@ string _hexToString(BYTE *data, size_t size)
 	}
 
 	return s;
-}
+}*/
 
 /*void cleanup()
 {
@@ -290,9 +359,9 @@ void help(const char *command, int param_count, char params[MAX_PARAMETERS][MAX_
 
 	printf("  bdump:\t Read <length#2> BYTEs of data from memory starting at an <address#1>, saving the data to a <filePath#1>\n\n");
 
-	printf("  inst:\t Prints the last <instruction_cnt#1> instruction to stdout\n\n");
-
-	printf("  reg:\t Prints or sets registers\n\n");
+	printf("  cpu:\t\t Prints cpu status or enables/disables/activates a specific cpu\n");
+	printf("  inst:\t\t Prints the last <instruction_cnt#1> instruction to stdout\n");
+	printf("  reg:\t\t Prints or sets registers\n\n");
 
 	printf("  load: \t Write a file with <filePath#1> to the device memory\n");
 	printf("  verify: \t Verify a file written to the device memory with <filePath#1>\n");
@@ -338,7 +407,7 @@ void run(const char *command, int param_count, char params[MAX_PARAMETERS][MAX_P
 
 static const char * const get_tt_error_desc(uint32_t error_code)
 {
-	for (int i = 0; i < TT_ERROR_COUNT; i++) {
+	for (uint32_t i = 0; i < sizeof(tt_errors) / sizeof(tt_errors[0]); i++) {
 		if (tt_errors[i].error_code == error_code)
 			return tt_errors[i].error_desc;
 	}
@@ -832,128 +901,140 @@ void mem(DWORD startAddr, DWORD length)
 
 	DWORD chars[showWidth];
 	WORD arrayIndex = 0;
+	char string_output[4 * showWidth + 1];
 
 	ioread32(startAddr, arr, length, length > 256); // Use sequential reads
-
-	for (DWORD i = 0; i < length; i++)
-	{
-		if (i % showWidth == 0)
-		{
-			if (i > 0)
-			{
-				cout << _hexToString(chars, arrayIndex);
+	
+	for (DWORD i = 0; i < length; i++) {
+		if (i % showWidth == 0) {
+			if (i > 0) {
+				hex_to_string_32((uint32_t*)chars, string_output, arrayIndex);	
+				printf("%s\n", string_output);
+				//cout << _hexToString(chars, arrayIndex);
 
 				arrayIndex = 0;
 
-				cout << endl;
+				//cout << endl;
 			}
 
-			cout << hex << nouppercase << "0x" << setfill('0') << setw(8) << startAddr << "  " << flush;
+			printf("%#08x  ", startAddr);
+			//cout << hex << nouppercase << "0x" << setfill('0') << setw(8) << startAddr << "  " << flush;
 			startAddr += 16;
 		}
 
-		cout << setfill('0') << setw(8) << hex << nouppercase << arr[i] << "  " << flush;
+		printf("%08x  ", arr[i]);
+		//cout << setfill('0') << setw(8) << hex << nouppercase << arr[i] << "  " << flush;
 
-		chars[arrayIndex] = arr[i];
-		arrayIndex++;
+		chars[arrayIndex++] = arr[i];
 	}
 
-	if (arrayIndex < showWidth)
-	{
+	hex_to_string_32((uint32_t*)chars, string_output, (arrayIndex < showWidth) ? arrayIndex : showWidth);
+	printf("%s\n", string_output);
+	
+	/*if (arrayIndex < showWidth) {
+		hex_to_string_32((uint32_t*)chars, string_output, arrayIndex);
 		cout << _hexToString(chars, arrayIndex) << endl;
-	}
-	else
-	{
+	} else {
+		hex_to_string_32((uint32_t*)chars, string_output, showWidth);
 		cout << _hexToString(chars, showWidth) << endl;
-	}
+	}*/
+
+	
 }
 
 void memh(DWORD startAddr, DWORD length)
 {
 	const DWORD maxAddr = startAddr + 2 * length;
 	const BYTE arrSize = 8;
+	char string_output[17];
 
 	WORD index = 0;
 	WORD arrayIndex = 0;
 	WORD arr[arrSize];
 
 	// Loop through all the addresses and read out individual DWORDs
-	for (DWORD addr = startAddr; addr < maxAddr; addr += 2)
-	{
-		if (index % arrSize == 0)
-		{
-			if (index > 0)
-			{
-				cout << _hexToString(arr, arrSize);
+	for (DWORD addr = startAddr; addr < maxAddr; addr += 2) {
+		if (index % arrSize == 0) {
+			if (index > 0) {
+				hex_to_string_16((uint16_t*)arr, string_output, arrSize);
+				printf("%s\n", string_output);
+				
+				//cout << _hexToString(arr, arrSize);
 				arrayIndex = 0;
 
-				cout << endl;
+				//cout << endl;
 			}
 
-			cout << hex << nouppercase << "0x" << setfill('0') << setw(8) << addr << "  " << flush;
+			printf("%#08x  ", addr);
+			//cout << hex << nouppercase << "0x" << setfill('0') << setw(8) << addr << "  " << flush;
 		}
 
 		WORD data = ioread16(addr);
-		arr[arrayIndex] = data;
-		arrayIndex++;
+		arr[arrayIndex++] = data;
 
-		cout << setfill('0') << setw(4) << hex << nouppercase << data << "  " << flush;
+		printf("%04x ", data);
+		//cout << setfill('0') << setw(4) << hex << nouppercase << data << "  " << flush;
 
 		index++;
 	}
 
-	if (index < arrSize)
-	{
+	hex_to_string_16((uint16_t*)arr, string_output, (index < arrSize) ? arrayIndex : arrSize);
+	printf("%s\n", string_output);
+
+	/*if (index < arrSize) {
 		cout << _hexToString(arr, arrayIndex) << endl;
-	}
-	else
-	{
+	} else {
 		cout << _hexToString(arr, arrSize) << endl;
-	}
+	}*/
+	
 }
 
 void memb(DWORD startAddr, DWORD length)
 {
 	const DWORD maxAddr = startAddr + 1 * length;
 	const BYTE arrSize = 16;
+	char string_output[17];
 
 	WORD index = 0;
 	WORD arrayIndex = 0;
 	BYTE arr[arrSize];
 
 	// Loop through all the addresses and read out individual DWORDs
-	for (DWORD addr = startAddr; addr < maxAddr; addr++)
-	{
-		if (index % arrSize == 0)
-		{
-			if (index > 0)
-			{
-				cout << _hexToString(arr, arrSize);
+	for (DWORD addr = startAddr; addr < maxAddr; addr++) {
+		if (index % arrSize == 0) {
+			if (index > 0) {
+				hex_to_string_8((uint8_t*) arr, string_output, arrSize);
+				printf("%s\n", string_output);
+				//cout << _hexToString(arr, arrSize);
 				arrayIndex = 0;
 
-				cout << endl;
+				//cout << endl;
 			}
 
-			cout << hex << nouppercase << "0x" << setfill('0') << setw(8) << addr << "  " << flush;
+			printf("%#08x  ", addr);
+			//cout << hex << nouppercase << "0x" << setfill('0') << setw(8) << addr << "  " << flush;
 		}
 
 		WORD data = ioread8(addr);
-		arr[arrayIndex] = data;
-		arrayIndex++;
+		arr[arrayIndex++] = data;
 
-		cout << setfill('0') << setw(2) << hex << nouppercase << data << "  " << flush;
+		printf("%02x ", data);
+		//cout << setfill('0') << setw(2) << hex << nouppercase << data << "  " << flush;
 
 		index++;
 	}
 
-	if (index < arrSize)
+	hex_to_string_8((uint8_t*)arr, string_output, (index < arrSize) ? arrayIndex : arrSize);
+	printf("%s\n", string_output);
+
+	/*if (index < arrSize)
 	{
 		cout << _hexToString(arr, arrayIndex) << endl;
 	}
 	else
 	{
 		cout << _hexToString(arr, arrSize) << endl;
-	}
+	}*/
 }
 
 void bdump(DWORD startAddr, DWORD length, string path)
diff --git a/src/uviemon_cli.hpp b/src/uviemon_cli.hpp
index 5c6cfb3..3a977c1 100644
--- a/src/uviemon_cli.hpp
+++ b/src/uviemon_cli.hpp
@@ -31,11 +31,11 @@ typedef struct {
 } command;
 
 
-//extern std::unordered_map<int, std::string> tt_errors;
-
+/*
 std::string _hexToString(DWORD *data, size_t size);
 std::string _hexToString(WORD *data, size_t size);
 std::string _hexToString(BYTE *data, size_t size);
+*/
 
 int parse_input(char *input);
 void print_help_text();
@@ -97,7 +97,7 @@ struct opcode {
 	};
 }__attribute__((packed));
 
-
+ 
 struct tt_error {
 	uint32_t error_code;
 	const char * const error_desc;
-- 
GitLab