From 11c8d71babb0210d070dd6ab12a255a5fa3159a5 Mon Sep 17 00:00:00 2001
From: mrb0nk500 <b0nk@b0nk.xyz>
Date: Wed, 6 May 2020 14:10:46 -0400
Subject: Removed some duplicate code in the assembler, and made the processor
 status register, a union now.

---
 lexer.c  | 115 +++++++++++++++++++--------------------------------------------
 opcode.h |   2 +-
 sux.c    |  12 +++----
 sux.h    |   8 ++---
 4 files changed, 46 insertions(+), 91 deletions(-)

diff --git a/lexer.c b/lexer.c
index a7d50ec..5a79325 100644
--- a/lexer.c
+++ b/lexer.c
@@ -428,6 +428,7 @@ uint64_t lex(char *str, struct line *l, uint64_t address, uint8_t dbg) {
 	uint8_t k = 0;
 	uint8_t rs = 0;
 	uint8_t isop = 0;
+	uint8_t base = 0;
 	int num = 0;
 	int isch = 0;
 	int16_t ln = -1;
@@ -471,6 +472,7 @@ uint64_t lex(char *str, struct line *l, uint64_t address, uint8_t dbg) {
 	l[line].aop = 0;
 	l[line].addr = address;
 	while (str[i] != '\0' && str[i] != '\n') {
+		base = 0;
 		space = 0;
 		tab = 0;
 		while (isspace(str[i+j])) {
@@ -569,75 +571,38 @@ uint64_t lex(char *str, struct line *l, uint64_t address, uint8_t dbg) {
 				l[line].am = IMM;
 				lex_type = TOK_IMM;
 				break;
-			case '$':
+				if (str[i] == '$') {
+			case '$':	base = 16;
+				} else if (str[i] == '%') {
+			case '%':	base = 2;
+				}
 				i++;
 				while (isxdigit(str[i]) && (str[i] != '\0' && str[i] != '\n')) {
 					lexeme[j++] = str[i++];
 				}
 				lexeme[j] = '\0';
 				switch (lex_type) {
-					case TOK_SYM:
-						l[line].op = strtoull(lexeme, NULL, 16);
-						mksymbol(sym, l[line].op, 1, 0, 0, dbg);
-						l[line].sym = get_symid(sym, address, line, dbg);
-						isfixup += (l[line].sym == 0xFFFF);
-						if (dbg) {
-							printf("lex(): isfixup: %u\n", isfixup);
-						}
-						l[line].opbase = BASE_HEX;
-						break;
-					case TOK_PLUS:
-					case TOK_MINUS:
-						l[line].aop = strtoull(lexeme, NULL, 16);
-						l[line].aopbase = BASE_HEX;
-						break;
 					default:
 						if (l[line].cm != 0xFF) {
-							l[line].aop = strtoull(lexeme, NULL, 16);
-							l[line].aopbase = BASE_HEX;
+					case TOK_PLUS :
+					case TOK_MINUS:	l[line].aop = strtoull(lexeme, NULL, base);
+							l[line].aopbase = (base & 16) ? TOK_HEX : TOK_BIN;
 						} else {
-							l[line].op = strtoull(lexeme, NULL, 16);
-							l[line].opbase = BASE_HEX;
+					case TOK_SYM:	l[line].op = strtoull(lexeme, NULL, base);
+							l[line].opbase = (base & 16) ? TOK_HEX : TOK_BIN;
 						}
-						break;
-
-				}
-				lex_type = TOK_HEX;
-				break;
-			case '%':
-				i++;
-				while (isdigit(str[i]) && (str[i] != '\0' && str[i] != '\n')) {
-					lexeme[j++] = str[i++];
-				}
-				lexeme[j] = '\0';
-				switch (lex_type) {
-					case TOK_SYM:
-						l[line].op = strtoull(lexeme, NULL, 2);
-						mksymbol(sym, l[line].op, 1, 0, 0, dbg);
-						l[line].sym = get_symid(sym, address, line, dbg);
-						isfixup += (l[line].sym == 0xFFFF);
-						if (dbg) {
-							printf("lex(): isfixup: %u\n", isfixup);
-						}
-						l[line].opbase = BASE_BIN;
-						break;
-					case TOK_PLUS:
-					case TOK_MINUS:
-						l[line].aop = strtoull(lexeme, NULL, 2);
-						l[line].aopbase = BASE_BIN;
-						break;
-					default:
-						if (l[lineidx].cm != 0xFF) {
-							l[line].aop = strtoull(lexeme, NULL, 2);
-							l[line].aopbase = BASE_BIN;
-						} else {
-							l[line].op = strtoull(lexeme, NULL, 2);
-							l[line].opbase = BASE_BIN;
+						if (lex_type == TOK_SYM) {
+							mksymbol(sym, l[line].op, 1, 0, 0, dbg);
+							l[line].sym = get_symid(sym, address, line, dbg);
+							isfixup += (l[line].sym == 0xFFFF);
+							if (dbg) {
+								printf("lex(): isfixup: %u\n", isfixup);
+							}
 						}
 						break;
-
 				}
-				lex_type = TOK_BIN;
+
+				lex_type = (base & 16) ? TOK_HEX : TOK_BIN;
 				break;
 			case '+':
 				lexeme[j] = '+';
@@ -825,34 +790,24 @@ uint64_t lex(char *str, struct line *l, uint64_t address, uint8_t dbg) {
 						if (lexeme[k] == '\0') {
 							if (num) {
 								switch (lex_type) {
-									case TOK_SYM:
-										l[line].op = strtoull(lexeme, NULL, 10);
-										mksymbol(sym, l[line].op, 1, 0, 0, dbg);
-										if (isfixup) {
-											isfixup = reslv_fixups(l, dbg);
-										}
-										l[line].sym = get_symid(sym, address, line, dbg);
-										isfixup += l[line].sym == 0xFFFF;
-										if (dbg) {
-											printf("lex(): isfixup: %u\n", isfixup);
-										}
-										l[line].opbase = BASE_DEC;
-										break;
-									case TOK_PLUS:
-									case TOK_MINUS:
-										l[line].aop = strtoull(lexeme, NULL, 10);
-										l[line].aopbase = BASE_DEC;
-										break;
 									default:
-										if (l[lineidx].cm != 0xFF) {
-											l[line].aop = strtoull(lexeme, NULL, 10);
-											l[line].aopbase = BASE_DEC;
+										if (l[line].cm != 0xFF) {
+									case TOK_PLUS :
+									case TOK_MINUS:	l[line].aop = strtoull(lexeme, NULL, 10);
+											l[line].aopbase = TOK_DEC;
 										} else {
-											l[line].op = strtoull(lexeme, NULL, 10);
-											l[line].opbase = BASE_DEC;
+									case TOK_SYM:	l[line].op = strtoull(lexeme, NULL, 10);
+											l[line].opbase = TOK_DEC;
+										}
+										if (lex_type == TOK_SYM) {
+											mksymbol(sym, l[line].op, 1, 0, 0, dbg);
+											l[line].sym = get_symid(sym, address, line, dbg);
+											isfixup += (l[line].sym == 0xFFFF);
+											if (dbg) {
+												printf("lex(): isfixup: %u\n", isfixup);
+											}
 										}
 										break;
-
 								}
 								lex_type = TOK_DEC;
 							} else if (isch && lex_type != TOK_HEX && lex_type != TOK_BIN) {
diff --git a/opcode.h b/opcode.h
index b9d1817..cd7a59b 100644
--- a/opcode.h
+++ b/opcode.h
@@ -211,7 +211,7 @@ union reg {
 };
 
 struct sux {
-	uint64_t ps; /* The processor status register. */
+	union reg ps; /* The processor status register. */
 	uint64_t a[8], b[8], y[8], x[8]; /* Registers A, B, X, and Y. */
 	uint64_t pc[8]; /* Program counter. */
 	uint16_t sp[8]; /* Stack pointer. */
diff --git a/sux.c b/sux.c
index 1299432..3c84425 100644
--- a/sux.c
+++ b/sux.c
@@ -102,10 +102,10 @@ void *run(void *args) {
 			, cpu->y[thread]);
 		wprintw(scr,
 			", sp: $%04X"
-			", ps: $%02"PRIX64
+			", ps: $%02X"
 			", inst: "
 			, cpu->sp[thread]
-			, cpu->ps);
+			, cpu->ps.u8[thread]);
 		#if keypoll
 		pthread_mutex_unlock(&mutex);
 		#endif
@@ -148,7 +148,7 @@ void *run(void *args) {
 	#endif
 		switch(opcode) {
 			case CPS: /* Clear Processor Status. */
-				cpu->ps = 0;
+				cpu->ps.u64 = 0;
 				break;
 			case AAB: /* Add Accumulator with carry by B register. */
 				value.u64 = cpu->b[thread]; /* Falls Through. */
@@ -461,8 +461,8 @@ void *run(void *args) {
 			case NOP: /* No OPeration. */
 				break;
 			case RTI: /* ReTurn from Interrupt routine. */
-				cpu->sp[thread] +=1;
-				cpu->ps	= addr[(cpu->stk_st[thread] << 16)+(cpu->sp[thread])] << (thread << 3); /* Falls through. */
+				cpu->sp[thread]   += 1;
+				cpu->ps.u8[thread] = addr[(cpu->stk_st[thread] << 16)+(cpu->sp[thread])]; /* Falls through. */
 			case RTS: /* ReTurn from Subroutine. */
 			case RTL: /* ReTurn from subroutine Long. */
 				cpu->sp[thread] += 8;
@@ -500,7 +500,7 @@ void *run(void *args) {
 				addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]-5] = value.u8[2];
 				addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]-6] = value.u8[1];
 				addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]-7] = value.u8[0];
-				addr[(cpu->stk_st[thread] << 16)+(cpu->sp[thread]-8)] = (uint64_t)cpu->ps >> (thread << 3);
+				addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]-8] = cpu->ps.u8[thread];
 				cpu->sp[thread] -= 9;
 				setflag(1, I);
 				if (opcode == BRK) {
diff --git a/sux.h b/sux.h
index aa0355e..b36c4b1 100644
--- a/sux.h
+++ b/sux.h
@@ -22,8 +22,8 @@ uint8_t subdbg;
 
 WINDOW *scr;
 
-#define setflag(flag, bit) ((flag)) ? (cpu->ps |= (bit << (thread << 3))) : (cpu->ps &= ~(bit << (thread << 3)))
-#define getflag(bit) (cpu->ps & (bit << (thread << 3)))
+#define setflag(flag, bit) ((flag)) ? (cpu->ps.u8[thread] |= bit) : (cpu->ps.u8[thread] &= ~bit)
+#define getflag(bit) (cpu->ps.u8[thread] & bit)
 
 extern pthread_mutex_t mutex;
 extern pthread_mutex_t main_mutex;
@@ -231,7 +231,7 @@ inline void push(struct sux *cpu, uint64_t value, uint8_t opcode, uint8_t thread
 		case PHB: r.reg = cpu->b[thread]; break;
 		case PHX: r.reg = cpu->x[thread]; break;
 		case PHY: r.reg = cpu->y[thread]; break;
-		case PHP: r.reg = cpu->ps;	break;
+		case PHP: r.reg = cpu->ps.u64;    break;
 	}
 	/* Unroll Loop by implementing Duff's Device. */
 	switch (tmp) {
@@ -271,7 +271,7 @@ inline void pull(struct sux *cpu, uint64_t value, uint8_t opcode, uint8_t thread
 		case PLB: cpu->b[thread] = r.reg;	break;
 		case PLX: cpu->x[thread] = r.reg;	break;
 		case PLY: cpu->y[thread] = r.reg;	break;
-		case PLP: cpu->ps = r.reg; 		break;
+		case PLP: cpu->ps.u64    = r.reg;	break;
 	}
 }
 
-- 
cgit v1.2.3-13-gbd6f