diff --git a/includes/opcodes.h b/includes/opcodes.h index c7da0f9..e8e44d3 100644 --- a/includes/opcodes.h +++ b/includes/opcodes.h @@ -4,7 +4,7 @@ #include #include "token.h" -#define OPCODECOUNT 10 +#define OPCODECOUNT 11 #define REGISTERCOUNT 8 typedef struct { diff --git a/includes/token.h b/includes/token.h index 6c0e261..df2a229 100644 --- a/includes/token.h +++ b/includes/token.h @@ -26,7 +26,7 @@ typedef enum { DB, ORG, //Opcodes COPY, ADD, SUB, JZ, INT, YLD, RET, - CMP, IN, OUT, + CMP, NOP, IN, OUT, //Registers R1, R2, R3, R4, R5, R6, R7, R8 } TokenType; @@ -34,9 +34,10 @@ typedef enum { typedef enum { None = 0, Reg = 1, - Immediate16 = 2, - Opcode = 4, - Directive = 8 + Constant = 2, + Address = 4, + Opcode = 8, + Directive = 16 } TokenClass; typedef struct { diff --git a/src/opcodes.c b/src/opcodes.c index 701f9e6..d2ba250 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -3,18 +3,45 @@ #include Instruction instructions[OPCODECOUNT] = { - { "copy", COPY, Reg, Reg | Immediate16 }, - { "add", ADD, Reg, Reg | Immediate16 }, - { "sub", SUB, Reg, Reg | Immediate16 }, - { "jz", JZ, Immediate16, None }, - { "int", INT, Immediate16, None }, + { "copy", COPY, Reg | Address, Reg | Constant | Address }, + { "add", ADD, Reg, Reg | Constant }, + { "sub", SUB, Reg, Reg | Constant }, + { "jz", JZ, Address, None }, + { "int", INT, Constant, None }, { "yld", YLD, None, None }, { "ret", RET, None, None }, - { "cmp", CMP, Reg | Immediate16, Reg | Immediate16 }, - { "in", IN, None, None}, - { "out", OUT, None, None} + { "cmp", CMP, Reg, Reg | Constant }, + { "in", IN, Constant | Reg, None}, + { "out", OUT, None, None}, + { "nop", NOP, None, None} }; +/* + NOP - 0000 0000 + JZ - 0000 0001 JZ Address + INT - 0000 0010 INT Constant + YLD - 0000 0011 YLD + RET - 0000 0100 RET + CALL - 0000 0101 CALL Address + //IN reads from a port number specified by either a register or a constant. + IN - 0000 0110 IN Constant + - 0000 0111 IN REG + COPY - 0000 1XXX COPY REG, REG + - 0001 0XXX COPY REG, Constant + - 0010 0XXX COPY REG, Address + - 0010 1XXX COPY Address, REG + - 0011 0XXX COPY Address, Constant + - 0011 1XXX COPY Address, Address + ADD - 0100 0XXX ADD REG, REG + - 0100 1XXX ADD REG, Constant + SUB - 0101 0XXX SUB REG, REG + - 0101 1XXX SUB REG, Constant + CMP - 0110 0XXX CMP REG, REG + - 0110 1XXX CMP REG, Constant + - 0111 0XXX CMP REG, Address + OUT - 0111 1XXX OUT REG +*/ + Register registers[REGISTERCOUNT] = { { "r1", R1 }, { "r2", R2 }, diff --git a/src/parser.c b/src/parser.c index 562968f..0d3fe2f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -38,10 +38,13 @@ int ExpectTokenClass(int, ...); const Symbol* GetSymbol(char*); List* SymbolsTable; unsigned int ProgramCounter = 0; +char Memory[HIGHMEMORY]; void ParseTokens(List* tokens) { if (!tokens) return; + memset(Memory, 0, sizeof(Memory)); + TokensList = tokens; SymbolsTable = CreateList(); @@ -56,7 +59,7 @@ void ParseTokens(List* tokens) { case Opcode: HandleOperation(); break; - case Immediate16: + case Address: if (t->type == LABEL) { printf("%s: \n", t->lexeme); AddSymbol(t, ProgramCounter); @@ -71,7 +74,12 @@ void ParseTokens(List* tokens) { PrintSymbols(); DestroyList(SymbolsTable); - //printf("Program Counter: %d\n", ProgramCounter); + // for(int i = 0; i < 256; i++) { + // if (i != 0 && i % 80 == 0) printf("\n"); + // printf("%02X ", Memory[i] & 0xFF); + // } + // printf("\n"); + // printf("Program Counter: %d\n", ProgramCounter); //printf("Heap Top: %#06X\n", HeapTop); } @@ -93,6 +101,10 @@ void HandleAssemblerDirective(void) { case STRING: printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, PeekToken()->lexeme, ProgramCounter); + for(int i = 0; i < strlen(PeekToken()->lexeme); i++) { + Memory[ProgramCounter + i] = PeekToken()->lexeme[i]; + } + ProgramCounter += strlen(PeekToken()->lexeme); AdvanceParser(); @@ -106,6 +118,7 @@ void HandleAssemblerDirective(void) { } else { //Add the NULL byte. + Memory[ProgramCounter] = '\0'; ProgramCounter++; return; } @@ -142,7 +155,7 @@ void HandleOperation(void) { //Encode the register number here. printf("%s ", PeekToken()->lexeme); } - else if (inst->parameter_one & Immediate16) { + else if (inst->parameter_one & Address || inst->parameter_one & Constant) { if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) { const Symbol* symbol = GetSymbol(PeekToken()->lexeme); @@ -176,7 +189,7 @@ void HandleOperation(void) { if (inst->parameter_two && PeekToken()->token_class > 0) { //printf("Matched 2 '%s'\n", PeekToken()->lexeme); - if (inst->parameter_two & Immediate16) { + if (inst->parameter_two & Address || inst->parameter_two & Constant) { if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) { const Symbol* symbol = GetSymbol(PeekToken()->lexeme); diff --git a/src/token.c b/src/token.c index 5c1d1fb..c511b6e 100644 --- a/src/token.c +++ b/src/token.c @@ -28,8 +28,9 @@ TokenClass GetTokenClass(TokenType type) { case STRING: case IDENTIFIER: case LABEL: + return Address; case NUMBER: - return Immediate16; + return Constant; default: return None; }