Compare commits
2
Commits
ae1e73e00f
...
5f437b6869
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f437b6869 | ||
|
|
e3b4293314 |
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
|
|
||||||
#define OPCODECOUNT 12
|
#define OPCODECOUNT 13
|
||||||
#define REGISTERCOUNT 8
|
#define REGISTERCOUNT 8
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,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, JMP, IN, OUT,
|
CMP, NOP, JMP, CALL, IN, OUT,
|
||||||
//Registers
|
//Registers
|
||||||
R1, R2, R3, R4, R5, R6, R7, R8
|
R1, R2, R3, R4, R5, R6, R7, R8
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|||||||
+6
-1
@@ -1,12 +1,17 @@
|
|||||||
;.org 0x100
|
;.org 0x100
|
||||||
.db msg "Hello, world!", 0
|
.db msg "Hello, world!", 0
|
||||||
;.db more_stuff "AA", 0
|
.db more_stuff "AA", 0
|
||||||
copy r7, msg ; //
|
copy r7, msg ; //
|
||||||
cmp r8, 45 ;1000 1111
|
cmp r8, 45 ;1000 1111
|
||||||
add r4, 30
|
add r4, 30
|
||||||
sub r1, 69
|
sub r1, 69
|
||||||
int 20
|
int 20
|
||||||
jz 45
|
jz 45
|
||||||
|
jmp msg
|
||||||
|
in 10
|
||||||
|
call more_stuff
|
||||||
|
ret
|
||||||
|
yld
|
||||||
|
|
||||||
;copy r1, 45 ;//B0 = 10110000
|
;copy r1, 45 ;//B0 = 10110000
|
||||||
;copy 2, 45
|
;copy 2, 45
|
||||||
|
|||||||
+18
-7
@@ -20,17 +20,12 @@ void Disassemble(unsigned char image[HIGHMEMORY]) {
|
|||||||
int position = image[0] + image[1];
|
int position = image[0] + image[1];
|
||||||
int end = image[2] + image[3];
|
int end = image[2] + image[3];
|
||||||
|
|
||||||
printf("Start: %d and End: %d\n", position, end);
|
|
||||||
|
|
||||||
char* parameter1 = calloc(32, sizeof(char));
|
char* parameter1 = calloc(32, sizeof(char));
|
||||||
char* parameter2 = calloc(32, sizeof(char));
|
char* parameter2 = calloc(32, sizeof(char));
|
||||||
|
|
||||||
unsigned char instruction = image[position];
|
unsigned char instruction = image[position];
|
||||||
|
|
||||||
while(end >= position) {//(instruction != 0) {
|
while(end > position) {
|
||||||
// printf("%d\n", position);
|
|
||||||
// printf("%#04X\n", instruction);
|
|
||||||
|
|
||||||
unsigned char masked = instruction & 0xE0;
|
unsigned char masked = instruction & 0xE0;
|
||||||
|
|
||||||
if (masked) {
|
if (masked) {
|
||||||
@@ -82,7 +77,7 @@ void Disassemble(unsigned char image[HIGHMEMORY]) {
|
|||||||
position += 3;
|
position += 3;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
printf("INT %d\n", Image[position + 1] + Image[position + 2]);
|
printf("INT %d\n", parameter);
|
||||||
position += 3;
|
position += 3;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
@@ -93,6 +88,22 @@ void Disassemble(unsigned char image[HIGHMEMORY]) {
|
|||||||
printf("RET\n");
|
printf("RET\n");
|
||||||
position++;
|
position++;
|
||||||
break;
|
break;
|
||||||
|
case 5:
|
||||||
|
printf("CALL [%#04X]\n", parameter);
|
||||||
|
position += 3;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
printf("JMP [%#04X]\n", parameter);
|
||||||
|
position += 3;
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
printf("IN %d\n", parameter);
|
||||||
|
position += 3;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
printf("OUT %d\n", parameter);
|
||||||
|
position += 3;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
position++;
|
position++;
|
||||||
break;
|
break;
|
||||||
|
|||||||
+8
-1
@@ -14,7 +14,8 @@ Instruction instructions[OPCODECOUNT] = {
|
|||||||
{ "in", IN, Constant | Reg, None},
|
{ "in", IN, Constant | Reg, None},
|
||||||
{ "out", OUT, None, None},
|
{ "out", OUT, None, None},
|
||||||
{ "nop", NOP, None, None},
|
{ "nop", NOP, None, None},
|
||||||
{ "jz", JZ, Address, None}
|
{ "jmp", JMP, Address, None},
|
||||||
|
{ "call", CALL, Address, None}
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned char GetInstructionMask(TokenType type, TokenClass parameterOneType) {
|
unsigned char GetInstructionMask(TokenType type, TokenClass parameterOneType) {
|
||||||
@@ -33,6 +34,12 @@ unsigned char GetInstructionMask(TokenType type, TokenClass parameterOneType) {
|
|||||||
return 0x01;//0b00000001;
|
return 0x01;//0b00000001;
|
||||||
case INT:
|
case INT:
|
||||||
return 0x02; //0b00000010;
|
return 0x02; //0b00000010;
|
||||||
|
case YLD:
|
||||||
|
return 0x03;
|
||||||
|
case RET:
|
||||||
|
return 0x04;
|
||||||
|
case CALL:
|
||||||
|
return 0x05;
|
||||||
case JMP:
|
case JMP:
|
||||||
return 0x06;//0b00000110;
|
return 0x06;//0b00000110;
|
||||||
case IN:
|
case IN:
|
||||||
|
|||||||
+104
-17
@@ -83,13 +83,13 @@ unsigned char* ParseTokens(List* tokens) {
|
|||||||
Heap[2] = 2 >> totalSize & 0xFF;
|
Heap[2] = 2 >> totalSize & 0xFF;
|
||||||
Heap[3] = totalSize & 0xFF;
|
Heap[3] = totalSize & 0xFF;
|
||||||
|
|
||||||
for(int i = 0; i < 33; i++) {
|
// for(int i = 0; i < 33; i++) {
|
||||||
if (i != 0 && i % 3 == 0) printf("\n");
|
// if (i != 0 && i % 3 == 0) printf("\n");
|
||||||
printf("%02X ", Heap[i] & 0xFF);
|
// printf("%02X ", Heap[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);
|
||||||
|
|
||||||
return Heap;
|
return Heap;
|
||||||
}
|
}
|
||||||
@@ -112,7 +112,7 @@ void HandleAssemblerDirective(void) {
|
|||||||
|
|
||||||
switch(identifier->type) {
|
switch(identifier->type) {
|
||||||
case STRING:
|
case STRING:
|
||||||
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, identifier->lexeme, HeapTop);
|
//printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, identifier->lexeme, HeapTop);
|
||||||
|
|
||||||
for(int i = 0; i < strlen(identifier->lexeme); i++) {
|
for(int i = 0; i < strlen(identifier->lexeme); i++) {
|
||||||
//Memory[ProgramCounter + i] = PeekToken()->lexeme[i];
|
//Memory[ProgramCounter + i] = PeekToken()->lexeme[i];
|
||||||
@@ -188,8 +188,6 @@ void HandleRegisterBasedOpcode(unsigned char instruction, unsigned char mask) {
|
|||||||
Memory[ProgramCounter] = 2 >> symbol->address & 0xFF;
|
Memory[ProgramCounter] = 2 >> symbol->address & 0xFF;
|
||||||
Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
||||||
|
|
||||||
ProgramCounter += 2;
|
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
}
|
}
|
||||||
else if (token->type == NUMBER) {
|
else if (token->type == NUMBER) {
|
||||||
@@ -200,13 +198,13 @@ void HandleRegisterBasedOpcode(unsigned char instruction, unsigned char mask) {
|
|||||||
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||||
Memory[ProgramCounter + 1] = *value & 0xFF;
|
Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||||
|
|
||||||
ProgramCounter += 2;
|
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "[Error] Expected operand, got %s\n", token->lexeme);
|
fprintf(stderr, "[Error] Expected operand, got %s\n", token->lexeme);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ProgramCounter += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleOperation(void) {
|
void HandleOperation(void) {
|
||||||
@@ -217,7 +215,6 @@ void HandleOperation(void) {
|
|||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
//Memory[ProgramCounter] = GetInstructionMask(inst->op, inst->parameter_one);
|
|
||||||
unsigned char mask = GetInstructionMask(inst->op, inst->parameter_one);
|
unsigned char mask = GetInstructionMask(inst->op, inst->parameter_one);
|
||||||
|
|
||||||
if (mask & registerBasedOpcodeMask) {
|
if (mask & registerBasedOpcodeMask) {
|
||||||
@@ -251,8 +248,6 @@ void HandleOperation(void) {
|
|||||||
|
|
||||||
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||||
Memory[ProgramCounter + 1] = *value & 0xFF;
|
Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||||
|
|
||||||
ProgramCounter += 2;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "[Error] Line: %d: Expected address after jump if zero (JZ) instruction.\n", ((Token*) TokensList->content[CurrentToken - 1])->line);
|
fprintf(stderr, "[Error] Line: %d: Expected address after jump if zero (JZ) instruction.\n", ((Token*) TokensList->content[CurrentToken - 1])->line);
|
||||||
@@ -272,15 +267,107 @@ void HandleOperation(void) {
|
|||||||
|
|
||||||
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||||
Memory[ProgramCounter + 1] = *value & 0xFF;
|
Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||||
|
|
||||||
ProgramCounter += 2;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "[Error] Line %d: Expected Interrupt vector.\n", PeekToken()->line);
|
fprintf(stderr, "[Error] Line %d: Expected Interrupt vector.\n", PeekToken()->line);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ProgramCounter += 2;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
case 3: //YLD
|
||||||
|
Memory[ProgramCounter] = 0x03;
|
||||||
|
|
||||||
|
ProgramCounter++;
|
||||||
|
break;
|
||||||
|
case 4: //RET
|
||||||
|
Memory[ProgramCounter] = 0x04;
|
||||||
|
|
||||||
|
ProgramCounter++;
|
||||||
|
break;
|
||||||
|
case 5: //CALL
|
||||||
|
Memory[ProgramCounter] = 0x05;
|
||||||
|
|
||||||
|
ProgramCounter++;
|
||||||
|
|
||||||
|
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||||
|
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||||
|
|
||||||
|
if (!symbol) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: %s is undefined.\n", PeekToken()->line, PeekToken()->lexeme);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Memory[ProgramCounter] = 2 >> symbol->address & 0xFF;
|
||||||
|
Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Expected address or label.\n", PeekToken()->line);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProgramCounter += 2;
|
||||||
|
break;
|
||||||
|
case 6: //JMP
|
||||||
|
Memory[ProgramCounter] = 0x06;
|
||||||
|
|
||||||
|
ProgramCounter++;
|
||||||
|
|
||||||
|
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||||
|
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||||
|
|
||||||
|
if (!symbol) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: %s is undefined.\n", PeekToken()->line, PeekToken()->lexeme);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Memory[ProgramCounter] = 2 >> symbol->address & 0xFF;
|
||||||
|
Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Expected address or label.\n", PeekToken()->line);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProgramCounter += 2;
|
||||||
|
break;
|
||||||
|
case 7: //IN
|
||||||
|
Memory[ProgramCounter] = 0x07;
|
||||||
|
|
||||||
|
ProgramCounter++;
|
||||||
|
|
||||||
|
if (PeekToken()->type == NUMBER) {
|
||||||
|
int* value = PeekToken()->value;
|
||||||
|
|
||||||
|
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||||
|
Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Expected port number.\n", PeekToken()->line);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProgramCounter += 2;
|
||||||
|
break;
|
||||||
|
case 8: //OUT
|
||||||
|
Memory[ProgramCounter] = 0x08;
|
||||||
|
|
||||||
|
ProgramCounter++;
|
||||||
|
|
||||||
|
if (PeekToken()->type == NUMBER) {
|
||||||
|
int* value = PeekToken()->value;
|
||||||
|
|
||||||
|
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||||
|
Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Expected port number.\n", PeekToken()->line);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProgramCounter += 2;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user