From e34a58d6d6d5deaa8b4b37dc369f08a65132970a Mon Sep 17 00:00:00 2001
From: marketideas <quick@marketideas.com>
Date: Thu, 14 Nov 2019 17:04:35 -0800
Subject: [PATCH] merge of HEX

---
 asm.cpp     | 34 +++++++++++---------
 opcodes.cpp |  4 +--
 psuedo.cpp  | 89 +++++++++++++++++++++++++++++++++++++++++++++++------
 psuedo.h    |  5 ++-
 qasm.h      |  1 +
 src/main.s  |  9 ++++--
 6 files changed, 111 insertions(+), 31 deletions(-)

diff --git a/asm.cpp b/asm.cpp
index 1bb0261..9664c39 100644
--- a/asm.cpp
+++ b/asm.cpp
@@ -366,7 +366,9 @@ void CLASS::complete(void)
 	uint64_t n = GetTickCount();
 	if (isDebug())
 	{
-		printf("Processing Time: %lu ms\n", n - starttime);
+		//cout << "Processing Time: " << n - starttime << "ms" << endl;
+		printf("Processing Time: %" PRIu64 " ms\n",n-starttime);
+
 	}
 }
 
@@ -507,8 +509,7 @@ int CLASS::processfile(std::string p, std::string &newfilename)
 		{
 			// is this the first file in the compilation, or a PUT/USE?
 			// if first, change CWD to location of file
-			//LOG_DEBUG << "Changing directory to: " << dir << endl;
-
+			LOG_DEBUG << "Changing directory to: " << dir << endl;
 			chdir(dir.c_str()); // change directory to where the file is
 		}
 
@@ -563,7 +564,6 @@ int CLASS::processfile(std::string p, std::string &newfilename)
 			//fprintf(stderr, "Unable to access file: %s\n", p1.c_str());
 
 			errorct = 1;
-			chdir(currentdir.c_str()); // change directory to where the file is
 			return (ecode);
 		}
 
@@ -579,8 +579,6 @@ int CLASS::processfile(std::string p, std::string &newfilename)
 				{
 					if (*itr == p1)
 					{
-						chdir(currentdir.c_str()); // change directory to where the file is
-
 						return (-9);
 					}
 				}
@@ -649,9 +647,8 @@ int CLASS::processfile(std::string p, std::string &newfilename)
 	}
 	catch (...)
 	{
+
 	}
-	chdir(currentdir.c_str());
-	//printf("\n\nfile read result: %d\n", res);
 	return (res);
 }
 
@@ -938,7 +935,7 @@ void CLASS::showSymbolTable(bool alpha)
 		std::map<std::string, uint32_t> alphamap;
 		std::map<uint32_t, std::string> nummap;
 
-		int columns = 4;
+		int columns = 2;
 		int column = columns;
 
 		for (auto itr = symbols.begin(); itr != symbols.end(); itr++)
@@ -954,7 +951,7 @@ void CLASS::showSymbolTable(bool alpha)
 
 			for (auto itr = alphamap.begin(); itr != alphamap.end(); ++itr)
 			{
-				printf("%-16s 0x%08X ", itr->first.c_str(), itr->second);
+				printf("%-16s 0x%08X       ", itr->first.c_str(), itr->second);
 				if ( !--column )
 				{
 					printf("\n");
@@ -967,7 +964,7 @@ void CLASS::showSymbolTable(bool alpha)
 			printf("\n\nSymbol table sorted numerically:\n\n");
 			for (auto itr = nummap.begin(); itr != nummap.end(); ++itr)
 			{
-				printf("0x%08X %-16s ", itr->first, itr->second.c_str());
+				printf("0x%08X       %-16s ", itr->first, itr->second.c_str());
 				if ( !--column )
 				{
 					printf("\n");
@@ -1201,6 +1198,11 @@ void CLASS::complete(void)
 	{
 		if (errorct == 0)
 		{
+			std::string currentdir = Poco::Path::current();
+
+			savepath = processFilename(savepath, currentdir, 0);
+			printf("saving to file: %s\n", savepath.c_str());
+
 			std::ofstream f(savepath);
 
 			uint32_t lineno = 0;
@@ -1249,7 +1251,9 @@ int CLASS::evaluate(MerlinLine &line, std::string expr, int64_t &value)
 			if (isDebug() > 2)
 			{
 				int c = SetColor(CL_RED);
-				printf("eval Error=%d %08lX |%s|\n", res, result, eval.badsymbol.c_str());
+				cout << "eval Error=" << res << "0x" << std::hex << result << std::dec << eval.badsymbol << endl;
+
+				//printf("eval Error=%d %08lX |%s|\n", res, result, eval.badsymbol.c_str());
 				SetColor(c);
 			}
 		}
@@ -1259,7 +1263,8 @@ int CLASS::evaluate(MerlinLine &line, std::string expr, int64_t &value)
 			value = result;
 			if ((listing) && (pass > 0) && (isDebug() > 2))
 			{
-				printf("EV1=%08lX '%c'\n", v1, line.expr_shift);
+				cout << "EV1=0x" << std::hex << v1 << " '" << std::dec << line.expr_shift << ";" << endl;
+				//printf("EV1=%08lX '%c'\n", v1, line.expr_shift);
 			}
 			if (v1 >= 0x10000)
 			{
@@ -1278,7 +1283,8 @@ int CLASS::evaluate(MerlinLine &line, std::string expr, int64_t &value)
 	}
 	if (isDebug() >= 3)
 	{
-		printf("Eval Result: %08lX (status=%d)\n", value, res);
+		cout << "Eval Result: 0x" << std::hex << value << std::dec << "(status=" << res << ")" << endl;
+		//printf("Eval Result: %08lX (status=%d)\n", value, res);
 	}
 	return (res);
 }
diff --git a/opcodes.cpp b/opcodes.cpp
index 8f80503..71af0fd 100644
--- a/opcodes.cpp
+++ b/opcodes.cpp
@@ -700,7 +700,7 @@ void CLASS::insertOpcodes(void)
 	pushopcode("EXT", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("ENT", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("ORG", P_ORG, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
-	pushopcode("DSK", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
+	pushopcode("DSK", P_SAV, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("SAV", P_SAV, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("DS",  P_DS, OP_PSUEDO,  OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("REL", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
@@ -736,7 +736,7 @@ void CLASS::insertOpcodes(void)
 	pushopcode("DB",  0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("ADR", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("ADRL", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
-	pushopcode("HEX", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
+	pushopcode("HEX", P_HEX, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("DS",  0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("DO",  0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
 	pushopcode("ELSE", 0x00, OP_PSUEDO, OPHANDLER(&CLASS::doPSEUDO));
diff --git a/psuedo.cpp b/psuedo.cpp
index 4691a0e..1c08ccf 100644
--- a/psuedo.cpp
+++ b/psuedo.cpp
@@ -17,7 +17,7 @@ int CLASS::doDS(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
 {
 	int res = 0;
 	int32_t v = line.expr_value;
-	if (line.eval_result!=0)
+	if (line.eval_result != 0)
 	{
 		line.setError(errForwardRef);
 	}
@@ -29,13 +29,13 @@ int CLASS::doDS(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
 	{
 		res = v;
 
-		if (a.pass>0)
+		if (a.pass > 0)
 		{
-			for (int i=0;i<v;i++)
+			for (int i = 0; i < v; i++)
 			{
 				line.outbytes.push_back(0x00);
 			}
-			line.outbytect=v;
+			line.outbytect = v;
 		}
 
 	}
@@ -87,6 +87,74 @@ int CLASS::doLST(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
 	return (0);
 }
 
+int CLASS::doHEX(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
+{
+	int res = 0;
+	std::vector<std::string> values;
+	values.clear();
+
+	std::string os = Poco::toUpper(Poco::trim(line.operand));
+	std::string vs = "0123456789ABCDEF";
+	std::string hex = "";
+
+	for ( uint32_t i = 0; i < os.length(); ++i )
+	{
+		char c = os[i];
+
+		// Check for a comma if needed, and continue to next char if found
+		if ( hex.length() == 0 && c == ',' )
+		{
+			continue;
+		}
+
+		if ( vs.find(c) == std::string::npos )
+		{
+			line.setError(errBadOperand);
+			return -1;
+		}
+
+		// Got a good char, append to hex string and see if we've got a byte
+		hex.append(1, c);
+		if ( hex.length() == 2 )
+		{
+			// Got 2 chars (1 byte), so store in values array
+			values.push_back(hex);
+			hex.clear();
+		}
+	}
+
+	// We can't have an odd character dangling around!
+	if ( hex.size() != 0 )
+	{
+		line.setError(errOverflow);
+		return -1;
+	}
+
+	int byteCnt = (int)values.size();
+	a.PC.currentpc += byteCnt;
+
+	if (a.pass > 0)
+	{
+		for ( uint32_t i = 0; i < values.size(); ++i )
+		{
+			std::string s = "$";
+			s.append(values[i]);
+			int64_t v;
+			if ( 0 == a.evaluate(line, s, v) )
+			{
+				line.outbytes.push_back((uint8_t)v);
+			}
+		}
+		line.outbytect = byteCnt;
+	}
+	else
+	{
+		line.pass0bytect = byteCnt;
+	}
+	return res;
+}
+
+
 int CLASS::ProcessOpcode(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
 {
 	int res = 0;
@@ -110,24 +178,27 @@ int CLASS::ProcessOpcode(T65816Asm &a, MerlinLine &line, TSymbol &opinfo)
 			res = doDUM(a, line, opinfo);
 			break;
 		case P_ORG:
-			if (line.operand.length()>0)
+			if (line.operand.length() > 0)
 			{
-				a.PC.orgsave=a.PC.currentpc;
+				a.PC.orgsave = a.PC.currentpc;
 				a.PC.currentpc = line.expr_value;
-				line.startpc=line.expr_value;
+				line.startpc = line.expr_value;
 			}
 			else
 			{
 				a.PC.currentpc = a.PC.orgsave;
-				line.startpc=a.PC.orgsave;
+				line.startpc = a.PC.orgsave;
 			}
 			break;
 		case P_SAV:
-			a.savepath = line.operand;
+			a.savepath = a.processFilename(line.operand, Poco::Path::current(), 0);
 			break;
 		case P_LST:
 			res = doLST(a, line, opinfo);
 			break;
+		case P_HEX:
+			res = doHEX(a, line, opinfo);
+			break;
 	}
 	return (res);
 }
diff --git a/psuedo.h b/psuedo.h
index bab2c51..7ee4512 100644
--- a/psuedo.h
+++ b/psuedo.h
@@ -13,6 +13,7 @@ enum
 	P_DS,
 	P_PUT,
 	P_USE,
+	P_HEX,
 
 	P_MAX
 };
@@ -26,9 +27,7 @@ public:
 	int doLST(T65816Asm &a, MerlinLine &line, TSymbol &opinfo);
 	int doDUM(T65816Asm &a, MerlinLine &line, TSymbol &opinfo);
 	int doDS(T65816Asm &a, MerlinLine &line, TSymbol &opinfo);
-
-
-
+	int doHEX(T65816Asm &a, MerlinLine &line, TSymbol &opinfo);
 };
 
 #undef CLASS
\ No newline at end of file
diff --git a/qasm.h b/qasm.h
index 79264aa..9bb3445 100644
--- a/qasm.h
+++ b/qasm.h
@@ -1,4 +1,5 @@
 #pragma once
+#include "inttypes.h"
 #include "palPoco.h"
 #include "pallogger.h"
 #include "eventtask.h"
diff --git a/src/main.s b/src/main.s
index 68b4b83..2640247 100644
--- a/src/main.s
+++ b/src/main.s
@@ -311,9 +311,6 @@ myQuit
 
         asll $1234
 
-
-        lst off
-
         lda	<$fff0+24       ;zp
         lda	>$fff0+24       ;ABS (lo word)
         lda ^$fff0+24		;ABS (hi word)
@@ -363,6 +360,12 @@ L00BC   bit	L00BC
         ldx	L00BC,y
         stx	L00BC,y
 
+* Data Storage Tests
+
+        hex 11,22,33,44,55,66,77,88,99
+        hex 112233445566778899
+        hex aabb,cc,ddee,ff
+
 //]XCODEEND       ; Keep this at the end and put your code above this
     ;lst off