From 8a4e9539ddc2ea6fdc48288ad2b3ac06d7b6261c Mon Sep 17 00:00:00 2001 From: Rose Apollo <55228370+AUnicornWithNoLife@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:29:32 +0000 Subject: [PATCH] part 1 --- .vscode/settings.json | 3 +- asm/asm_const.h | 1 - asm/assembler.c | 154 +++++++++++++++++++++++++++++++++++++++++- asm/main.c | 2 - build.sh | 2 +- 5 files changed, 155 insertions(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d48e3bc..0bc5109 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,7 @@ "files.associations": { "ram.h": "c", "stdint.h": "c", - "carg-parse.h": "c" + "carg-parse.h": "c", + "cpu_human.h": "c" } } \ No newline at end of file diff --git a/asm/asm_const.h b/asm/asm_const.h index 7033d6b..60203ab 100644 --- a/asm/asm_const.h +++ b/asm/asm_const.h @@ -2,7 +2,6 @@ #define ASM_ASM_CONST_H #include "../emu/cpu_const.h" -#include "../emu/cpu_human.h" #include typedef struct diff --git a/asm/assembler.c b/asm/assembler.c index d4a0b4b..abbc68b 100644 --- a/asm/assembler.c +++ b/asm/assembler.c @@ -5,10 +5,160 @@ #include #include #include +#include +#include -AsmInstructionArray InterpretAssembly(char *data) +// i apologise for the next three functions, they are written very werirdly, +// and are not as readable as they coudld be, sorry +// the rough flow for each is just to loop along a string until you +// encounter a specific char, or a non space / tab char +// +// again i apologise for the unreadability, they use way to many +// goto's, but should be pretty efficient. + +char *FindNextChar(char *data, char toFind, bool careAboutNull) +{ +begin: + switch (*data) + { + case '\0': + if (careAboutNull) + { + fprintf(stderr, "FOUND NULL CHAR BEFORE CHAR TO FIND\n"); + exit(-1); + } + + goto final; + default: + if (*data == toFind) + { + final: + return data; + } + + data++; + goto begin; + } +} + +char *FindNextNonSpaceChar(char *data, bool careAboutNull) +{ +begin: + switch(*data) + { + case '\0': + if (careAboutNull) + { + fprintf(stderr, "FOUND NULL CHAR BEFORE NON SPACE / TAB CHAR\n"); + exit(-1); + } + + goto final; + case ' ': + case '\t': + data++; + goto begin; + default: + final: + return data; + } +} + +char *FindNextSpaceChar(char *data, bool careAboutNull) +{ +begin: + switch(*data) + { + case '\0': + if (careAboutNull) + { + fprintf(stderr, "FOUND NULL CHAR BEFORE NON SPACE / TAB CHAR\n"); + exit(-1); + } + case ' ': + case '\t': + return data; + default: + data++; + goto begin; + } +} + +AsmInstructionArray *InterpretAssembly(char *data) { char *d = data; - + AsmInstructionArray *asmIA = malloc(sizeof(AsmInstructionArray)); + uint32_t cSize = 0, mSize = 32; + asmIA->instruction = malloc(sizeof(AsmInstruction) * 32); + while (true) + { +loopStart: + if (cSize == mSize) + { + mSize += 32; + asmIA->instruction = realloc(asmIA->instruction, mSize); + } + + AsmInstruction current = *(asmIA->instruction + (cSize * sizeof(AsmInstruction))); + + d = FindNextNonSpaceChar(d, false); + + if (*d == '\0') + goto final; + if (*d == '\n') + { + d++; + goto loopStart; + } + + if (*(FindNextSpaceChar(d, true) - 1) == ':') + { + memcpy(¤t.label, d, (FindNextSpaceChar(d, true) - 1) - d); + + d = FindNextNonSpaceChar(d, true); + } + else + { + current.label[0] = '\0'; + } + + memcpy(¤t.instruction, d, 3); + + d = FindNextNonSpaceChar(d, false); + + if (*d == '\0') + goto final; + if (*d == '\n') + { + d++; + goto loopEnd; + } + + char *comma = FindNextChar(d, ',', true), + *nline = FindNextChar(d, '\n', true); + + bool a2 = comma < nline; + char *eoA1 = a2 ? comma : nline; + memcpy(¤t.arg1, d, eoA1 - d); + + if (!a2) + goto loopEnd; + + d = FindNextNonSpaceChar(eoA1 + 1, true); + memcpy(¤t.arg2, d, nline - d); + d = nline + 1; + +loopEnd: + cSize++; + } + +final: + // TODO i think its mostly done, just need to make sure i free any rand temp + // shit used here + + asmIA->instruction = realloc(asmIA->instruction, cSize); + asmIA->length = cSize; + + return asmIA; } \ No newline at end of file diff --git a/asm/main.c b/asm/main.c index cab41a5..1fee886 100644 --- a/asm/main.c +++ b/asm/main.c @@ -1,5 +1,3 @@ -#include "../emu/cpu_const.h" -#include "../emu/cpu_human.h" #include "../lib/carg-parse/carg-parse.h" #include "./asm_const.h" #include diff --git a/build.sh b/build.sh index e9566fd..d396534 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ mkdir ./bin clang -O2 -o ./bin/r16emu ./emu/main.c ./emu/cpu/instruction.c ./emu/cpu/ram.c ./emu/cpu/register.c ./emu/cpu/stack.c ./lib/carg-parse/carg-parse.c echo "built emulator" -clang -O2 -o ./bin/r16asm ./asm/main.c ./lib/carg-parse/carg-parse.c +clang -O2 -o ./bin/r16asm ./asm/main.c ./asm/assembler.c ./lib/carg-parse/carg-parse.c echo "built assemblor" \ No newline at end of file