Created a basic binary format to help guide the disassembler.
This commit is contained in:
+54
-14
@@ -18,6 +18,8 @@ struct intr {
|
||||
Symbol* CreateSymbol(Token* token, int address);
|
||||
const List* TokensList;
|
||||
int CurrentToken = 0;
|
||||
//int HeapStart = 4; // zero indexed, used for declared variables.
|
||||
//int BinaryEnd = 4; // Zero indexed (binary starts with a 4 byte header)
|
||||
|
||||
void AddSymbol(Token*, int);
|
||||
void PrintSymbols(void);
|
||||
@@ -30,13 +32,18 @@ int Expect(int, ...);
|
||||
int ExpectTokenClass(int, ...);
|
||||
const Symbol* GetSymbol(char*);
|
||||
List* SymbolsTable;
|
||||
void WriteMemory(unsigned char value, int location);
|
||||
void WriteMemoryORMask(unsigned char mask, int location);
|
||||
unsigned int HeapTop = 4;
|
||||
unsigned int ProgramCounter = 0;
|
||||
unsigned char Heap[HIGHMEMORY];
|
||||
unsigned char Memory[HIGHMEMORY];
|
||||
|
||||
unsigned char* ParseTokens(List* tokens) {
|
||||
if (!tokens) return NULL;
|
||||
|
||||
memset(Memory, 0, sizeof(Memory));
|
||||
memset(Heap, 0, sizeof(Heap));
|
||||
|
||||
TokensList = tokens;
|
||||
|
||||
@@ -68,14 +75,23 @@ unsigned char* ParseTokens(List* tokens) {
|
||||
if (SymbolsTable->size > 0) PrintSymbols();
|
||||
|
||||
DestroyList(SymbolsTable);
|
||||
|
||||
memcpy(&Heap[HeapTop], Memory, ProgramCounter);
|
||||
int totalSize = HeapTop + ProgramCounter;
|
||||
Heap[0] = 2 >> HeapTop & 0xFF;
|
||||
Heap[1] = HeapTop & 0xFF;
|
||||
Heap[2] = 2 >> totalSize & 0xFF;
|
||||
Heap[3] = totalSize & 0xFF;
|
||||
|
||||
for(int i = 0; i < 33; i++) {
|
||||
if (i != 0 && i % 3 == 0) printf("\n");
|
||||
printf("%02X ", Memory[i] & 0xFF);
|
||||
printf("%02X ", Heap[i] & 0xFF);
|
||||
}
|
||||
printf("\n");
|
||||
// printf("Program Counter: %d\n", ProgramCounter);
|
||||
//printf("Heap Top: %#06X\n", HeapTop);
|
||||
return Memory;
|
||||
printf("Program Counter: %d\n", ProgramCounter);
|
||||
printf("Heap Top: %#06X\n", HeapTop);
|
||||
|
||||
return Heap;
|
||||
}
|
||||
|
||||
void HandleAssemblerDirective(void) {
|
||||
@@ -86,21 +102,25 @@ void HandleAssemblerDirective(void) {
|
||||
Token* identifier = PeekToken();
|
||||
|
||||
if (!Expect(1, IDENTIFIER)) {
|
||||
fprintf(stderr, "Expected identifier on line %d\n", PeekToken()->line);
|
||||
fprintf(stderr, "Expected identifier on line %d\n", identifier->line);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
AddSymbol(identifier, ProgramCounter);
|
||||
|
||||
switch(PeekToken()->type) {
|
||||
case STRING:
|
||||
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, PeekToken()->lexeme, ProgramCounter);
|
||||
identifier = PeekToken();
|
||||
|
||||
for(int i = 0; i < strlen(PeekToken()->lexeme); i++) {
|
||||
Memory[ProgramCounter + i] = PeekToken()->lexeme[i];
|
||||
switch(identifier->type) {
|
||||
case STRING:
|
||||
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, identifier->lexeme, ProgramCounter);
|
||||
|
||||
for(int i = 0; i < strlen(identifier->lexeme); i++) {
|
||||
//Memory[ProgramCounter + i] = PeekToken()->lexeme[i];
|
||||
Heap[HeapTop + i] = identifier->lexeme[i];
|
||||
}
|
||||
|
||||
ProgramCounter += strlen(PeekToken()->lexeme);
|
||||
//ProgramCounter += strlen(PeekToken()->lexeme);
|
||||
HeapTop += strlen(identifier->lexeme);
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
@@ -113,8 +133,10 @@ void HandleAssemblerDirective(void) {
|
||||
}
|
||||
else {
|
||||
//Add the NULL byte.
|
||||
Memory[ProgramCounter] = '\0';
|
||||
ProgramCounter++;
|
||||
//Memory[ProgramCounter] = '\0';
|
||||
Heap[HeapTop] = '\0';
|
||||
//ProgramCounter++;
|
||||
HeapTop++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -153,7 +175,7 @@ void HandleRegisterBasedOpcode(unsigned char instruction, unsigned char mask) {
|
||||
|
||||
token = PeekToken();
|
||||
|
||||
if (token->token_class == IDENTIFIER || token->token_class == LABEL) {
|
||||
if (token->type == IDENTIFIER || token->type == LABEL) {
|
||||
const Symbol* symbol = GetSymbol(token->lexeme);
|
||||
|
||||
if (!symbol) {
|
||||
@@ -363,4 +385,22 @@ int ParserAtEnd(void) {
|
||||
|
||||
Token* PeekToken(void) {
|
||||
return TokensList->content[CurrentToken];
|
||||
}
|
||||
|
||||
void WriteMemory(unsigned char value, int location) {
|
||||
if (location > HIGHMEMORY) {
|
||||
fprintf(stderr, "[Error] Exceeded memmory size\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Memory[location] = value;
|
||||
}
|
||||
|
||||
void WriteMemoryORMask(unsigned char mask, int location) {
|
||||
if (location > HIGHMEMORY) {
|
||||
fprintf(stderr, "[Error] Exceeded memmory size\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Memory[location] |= mask;
|
||||
}
|
||||
Reference in New Issue
Block a user