From eb5e2c506e2656c917499b12a1310f36ce4d5f21 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Fri, 26 Aug 2022 18:27:41 +0000 Subject: [PATCH] Got the start of encoding opcodes. --- includes/opcodes.h | 1 + includes/token.h | 2 +- misc/test.asm | 6 +++--- src/opcodes.c | 37 +++++++++++++++++++++++++++++-------- src/parser.c | 39 ++++++++++++++++++++++++++++++++------- 5 files changed, 66 insertions(+), 19 deletions(-) diff --git a/includes/opcodes.h b/includes/opcodes.h index e8e44d3..611413b 100644 --- a/includes/opcodes.h +++ b/includes/opcodes.h @@ -25,5 +25,6 @@ extern Register registers[REGISTERCOUNT]; int IsOpcode(const char*, TokenType*); int IsRegister(const char*, TokenType*); const Instruction* GetOpcodeDetails(TokenType); +unsigned char GetInstructionMask(TokenType type, TokenClass parameterOneType); #endif \ No newline at end of file diff --git a/includes/token.h b/includes/token.h index df2a229..876ea13 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, NOP, IN, OUT, + CMP, NOP, JMP, IN, OUT, //Registers R1, R2, R3, R4, R5, R6, R7, R8 } TokenType; diff --git a/misc/test.asm b/misc/test.asm index 349113d..e12f7e6 100644 --- a/misc/test.asm +++ b/misc/test.asm @@ -1,9 +1,9 @@ .org 0x100 .db msg "Hello, world!", 0 -.db more_stuff "And yet another string!", 0 +.db more_stuff "AAAA", 0 -copy r1, msg -copy r2, 0 +copy r1, more_stuff ;//B0 = 10110000 +copy more_stuff, 45 loop: cmp r1, 0 jz loop diff --git a/src/opcodes.c b/src/opcodes.c index 490cb4e..aff6387 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -16,6 +16,33 @@ Instruction instructions[OPCODECOUNT] = { { "nop", NOP, None, None} }; +unsigned char GetInstructionMask(TokenType type, TokenClass parameterOneType) { + switch (type) { + case COPY: + if (parameterOneType == Reg) return 0x20; //0b00100000; + + return 0xA0;////0b10100000; + case ADD: + return 0x40;//0b01000000; + case SUB: + return 0x60;//0b01100000; + case CMP: + return 0x80;//0b10000000; + case JZ: + return 0x01;//0b00000001; + case INT: + return 0x02; //0b00000010; + case JMP: + return 0x06;//0b00000110; + case IN: + return 0x07;//0b00000111; + case OUT: + return 0x08;//0b00001000; + default: + return 0x00; + } +} + /* NOP - 0000 0000 NOP JZ - 0000 0001 JZ Address @@ -24,14 +51,14 @@ Instruction instructions[OPCODECOUNT] = { RET - 0000 0100 RET CALL - 0000 0101 CALL Address JMP - 0000 0110 JMP Address - + IN - 0000 0111 IN Constant + OUT - 0000 1000 OUT Constant COPY - 0010 0XXX COPY REG, REG - 0010 1XXX COPY REG, Constant - 0011 0XXX COPY REG, Address - 1010 0XXX COPY Address, REG - 1010 1XXX COPY Address, Constant - 1011 0XXX COPY Address, Address - ADD - 0100 0XXX ADD REG, REG - 0100 1XXX ADD REG, Constant SUB - 0110 0XXX SUB REG, REG @@ -39,17 +66,11 @@ Instruction instructions[OPCODECOUNT] = { CMP - 1000 0XXX CMP REG, REG - 1000 1XXX CMP REG, Constant - 1001 0XXX CMP REG, Address - //Read from port specified by either a register or a constant - IN - 1100 0XXX IN REG - - 1101 0XXX IN Constant - - OUT - 1111 1XXX OUT REG */ //COPY X01X XXXX //ADD 010X XXXX //SUB 011X XXXX //CMP 100X XXXX -//IN 110X XXXX //REG XXX0 0XXX //Constant XXX0 1XXX //Address XXX1 0XXX diff --git a/src/parser.c b/src/parser.c index 0d3fe2f..1d4eead 100644 --- a/src/parser.c +++ b/src/parser.c @@ -74,11 +74,11 @@ void ParseTokens(List* tokens) { PrintSymbols(); DestroyList(SymbolsTable); - // for(int i = 0; i < 256; i++) { - // if (i != 0 && i % 80 == 0) printf("\n"); - // printf("%02X ", Memory[i] & 0xFF); - // } - // printf("\n"); + for(int i = 0; i < 32; i++) { + if (i != 0 && i % 3 == 0) printf("\n"); + printf("%02X ", Memory[i] & 0xFF); + } + printf("\n"); // printf("Program Counter: %d\n", ProgramCounter); //printf("Heap Top: %#06X\n", HeapTop); } @@ -132,6 +132,9 @@ void HandleAssemblerDirective(void) { } } +//REG XXX0 0XXX +//Constant XXX0 1XXX +//Address XXX1 0XXX void HandleOperation(void) { const Instruction* inst = GetOpcodeDetails(PeekToken()->type); @@ -141,6 +144,8 @@ void HandleOperation(void) { printf("[%#05X] %s ", ProgramCounter, inst->lexeme); + Memory[ProgramCounter] |= GetInstructionMask(inst->op, inst->parameter_one); + ProgramCounter++; if (inst->parameter_one == None) { @@ -153,6 +158,7 @@ void HandleOperation(void) { if (inst->parameter_one & Reg) { //Encode the register number here. + Memory[ProgramCounter - 1] |= PeekToken()->type - 29; printf("%s ", PeekToken()->lexeme); } else if (inst->parameter_one & Address || inst->parameter_one & Constant) { @@ -164,9 +170,16 @@ void HandleOperation(void) { exit(1); } + Memory[ProgramCounter - 1] |= 0x10;//0b00010000; + Memory[ProgramCounter] = 2 >> symbol->address & 0xFF; + Memory[ProgramCounter + 1] = symbol->address & 0xFF; + printf("[%s@%#04X] ", PeekToken()->lexeme, symbol->address); } - else if (PeekToken()->type == NUMBER) printf("%s ", PeekToken()->lexeme); + else if (PeekToken()->type == NUMBER) { + Memory[ProgramCounter - 1] |= 0x08; //0b00001000; + printf("%s ", PeekToken()->lexeme); + } ProgramCounter += 2; } @@ -198,9 +211,21 @@ void HandleOperation(void) { exit(1); } + Memory[ProgramCounter - 1] |= 0x10;//0b00010000; + Memory[ProgramCounter] = 2 >> symbol->address & 0xFF; + Memory[ProgramCounter + 1] = symbol->address & 0xFF; + printf("[%s@%#04X] ", PeekToken()->lexeme, symbol->address); } - else if (PeekToken()->type == NUMBER) printf("%s ", PeekToken()->lexeme); + else if (PeekToken()->type == NUMBER) { + Memory[ProgramCounter - 1] |= 0x08; //0b00001000; + int* value = PeekToken()->value; + + Memory[ProgramCounter] = 2 >> *value &0xFF; + Memory[ProgramCounter + 1] = *value &0xFF; + + printf("%s ", PeekToken()->lexeme); + } ProgramCounter += 2; }