Got the start of encoding opcodes.

This commit is contained in:
2022-08-26 18:27:41 +00:00
parent 0d060248cb
commit eb5e2c506e
5 changed files with 66 additions and 19 deletions
+1
View File
@@ -25,5 +25,6 @@ extern Register registers[REGISTERCOUNT];
int IsOpcode(const char*, TokenType*); int IsOpcode(const char*, TokenType*);
int IsRegister(const char*, TokenType*); int IsRegister(const char*, TokenType*);
const Instruction* GetOpcodeDetails(TokenType); const Instruction* GetOpcodeDetails(TokenType);
unsigned char GetInstructionMask(TokenType type, TokenClass parameterOneType);
#endif #endif
+1 -1
View File
@@ -26,7 +26,7 @@ typedef enum {
DB, ORG, DB, ORG,
//Opcodes //Opcodes
COPY, ADD, SUB, JZ, INT, YLD, RET, COPY, ADD, SUB, JZ, INT, YLD, RET,
CMP, NOP, IN, OUT, CMP, NOP, JMP, IN, OUT,
//Registers //Registers
R1, R2, R3, R4, R5, R6, R7, R8 R1, R2, R3, R4, R5, R6, R7, R8
} TokenType; } TokenType;
+3 -3
View File
@@ -1,9 +1,9 @@
.org 0x100 .org 0x100
.db msg "Hello, world!", 0 .db msg "Hello, world!", 0
.db more_stuff "And yet another string!", 0 .db more_stuff "AAAA", 0
copy r1, msg copy r1, more_stuff ;//B0 = 10110000
copy r2, 0 copy more_stuff, 45
loop: loop:
cmp r1, 0 cmp r1, 0
jz loop jz loop
+29 -8
View File
@@ -16,6 +16,33 @@ Instruction instructions[OPCODECOUNT] = {
{ "nop", NOP, None, None} { "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 NOP - 0000 0000 NOP
JZ - 0000 0001 JZ Address JZ - 0000 0001 JZ Address
@@ -24,14 +51,14 @@ Instruction instructions[OPCODECOUNT] = {
RET - 0000 0100 RET RET - 0000 0100 RET
CALL - 0000 0101 CALL Address CALL - 0000 0101 CALL Address
JMP - 0000 0110 JMP Address JMP - 0000 0110 JMP Address
IN - 0000 0111 IN Constant
OUT - 0000 1000 OUT Constant
COPY - 0010 0XXX COPY REG, REG COPY - 0010 0XXX COPY REG, REG
- 0010 1XXX COPY REG, Constant - 0010 1XXX COPY REG, Constant
- 0011 0XXX COPY REG, Address - 0011 0XXX COPY REG, Address
- 1010 0XXX COPY Address, REG - 1010 0XXX COPY Address, REG
- 1010 1XXX COPY Address, Constant - 1010 1XXX COPY Address, Constant
- 1011 0XXX COPY Address, Address - 1011 0XXX COPY Address, Address
ADD - 0100 0XXX ADD REG, REG ADD - 0100 0XXX ADD REG, REG
- 0100 1XXX ADD REG, Constant - 0100 1XXX ADD REG, Constant
SUB - 0110 0XXX SUB REG, REG SUB - 0110 0XXX SUB REG, REG
@@ -39,17 +66,11 @@ Instruction instructions[OPCODECOUNT] = {
CMP - 1000 0XXX CMP REG, REG CMP - 1000 0XXX CMP REG, REG
- 1000 1XXX CMP REG, Constant - 1000 1XXX CMP REG, Constant
- 1001 0XXX CMP REG, Address - 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 //COPY X01X XXXX
//ADD 010X XXXX //ADD 010X XXXX
//SUB 011X XXXX //SUB 011X XXXX
//CMP 100X XXXX //CMP 100X XXXX
//IN 110X XXXX
//REG XXX0 0XXX //REG XXX0 0XXX
//Constant XXX0 1XXX //Constant XXX0 1XXX
//Address XXX1 0XXX //Address XXX1 0XXX
+32 -7
View File
@@ -74,11 +74,11 @@ void ParseTokens(List* tokens) {
PrintSymbols(); PrintSymbols();
DestroyList(SymbolsTable); DestroyList(SymbolsTable);
// for(int i = 0; i < 256; i++) { for(int i = 0; i < 32; i++) {
// if (i != 0 && i % 80 == 0) printf("\n"); if (i != 0 && i % 3 == 0) printf("\n");
// printf("%02X ", Memory[i] & 0xFF); printf("%02X ", Memory[i] & 0xFF);
// } }
// printf("\n"); printf("\n");
// printf("Program Counter: %d\n", ProgramCounter); // printf("Program Counter: %d\n", ProgramCounter);
//printf("Heap Top: %#06X\n", HeapTop); //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) { void HandleOperation(void) {
const Instruction* inst = GetOpcodeDetails(PeekToken()->type); const Instruction* inst = GetOpcodeDetails(PeekToken()->type);
@@ -141,6 +144,8 @@ void HandleOperation(void) {
printf("[%#05X] %s ", ProgramCounter, inst->lexeme); printf("[%#05X] %s ", ProgramCounter, inst->lexeme);
Memory[ProgramCounter] |= GetInstructionMask(inst->op, inst->parameter_one);
ProgramCounter++; ProgramCounter++;
if (inst->parameter_one == None) { if (inst->parameter_one == None) {
@@ -153,6 +158,7 @@ void HandleOperation(void) {
if (inst->parameter_one & Reg) { if (inst->parameter_one & Reg) {
//Encode the register number here. //Encode the register number here.
Memory[ProgramCounter - 1] |= PeekToken()->type - 29;
printf("%s ", PeekToken()->lexeme); printf("%s ", PeekToken()->lexeme);
} }
else if (inst->parameter_one & Address || inst->parameter_one & Constant) { else if (inst->parameter_one & Address || inst->parameter_one & Constant) {
@@ -164,9 +170,16 @@ void HandleOperation(void) {
exit(1); 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); 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; ProgramCounter += 2;
} }
@@ -198,9 +211,21 @@ void HandleOperation(void) {
exit(1); 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); 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; ProgramCounter += 2;
} }