Added a disassembler which is still very buggy. Also fixed some bugs with the opcode mask getting function.
This commit is contained in:
+21
-31
@@ -4,12 +4,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef HIGHMEMORY
|
||||
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
|
||||
#endif
|
||||
|
||||
//#define HEAPTOP HIGHMEMORY
|
||||
|
||||
typedef struct {
|
||||
Token* token;
|
||||
int address;
|
||||
@@ -24,7 +18,6 @@ struct intr {
|
||||
Symbol* CreateSymbol(Token* token, int address);
|
||||
const List* TokensList;
|
||||
int CurrentToken = 0;
|
||||
//int HeapTop = HIGHMEMORY;
|
||||
|
||||
void AddSymbol(Token*, int);
|
||||
void PrintSymbols(void);
|
||||
@@ -38,10 +31,10 @@ int ExpectTokenClass(int, ...);
|
||||
const Symbol* GetSymbol(char*);
|
||||
List* SymbolsTable;
|
||||
unsigned int ProgramCounter = 0;
|
||||
char Memory[HIGHMEMORY];
|
||||
unsigned char Memory[HIGHMEMORY];
|
||||
|
||||
void ParseTokens(List* tokens) {
|
||||
if (!tokens) return;
|
||||
unsigned char* ParseTokens(List* tokens) {
|
||||
if (!tokens) return NULL;
|
||||
|
||||
memset(Memory, 0, sizeof(Memory));
|
||||
|
||||
@@ -72,15 +65,17 @@ void ParseTokens(List* tokens) {
|
||||
AdvanceParser();
|
||||
}
|
||||
|
||||
PrintSymbols();
|
||||
if (SymbolsTable->size > 0) PrintSymbols();
|
||||
|
||||
DestroyList(SymbolsTable);
|
||||
for(int i = 0; i < 32; i++) {
|
||||
if (i != 0 && i % 3 == 0) printf("\n");
|
||||
printf("%02X ", Memory[i] & 0xFF);
|
||||
}
|
||||
printf("\n");
|
||||
// for(int i = 0; i < 33; 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);
|
||||
return Memory;
|
||||
}
|
||||
|
||||
void HandleAssemblerDirective(void) {
|
||||
@@ -132,9 +127,6 @@ void HandleAssemblerDirective(void) {
|
||||
}
|
||||
}
|
||||
|
||||
//REG XXX0 0XXX 1010 1111
|
||||
//Constant XXX0 1XXX
|
||||
//Address XXX1 0XXX
|
||||
void HandleOperation(void) {
|
||||
const Instruction* inst = GetOpcodeDetails(PeekToken()->type);
|
||||
|
||||
@@ -142,14 +134,12 @@ void HandleOperation(void) {
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
printf("[%#05X] %s ", ProgramCounter, inst->lexeme);
|
||||
|
||||
Memory[ProgramCounter] = GetInstructionMask(inst->op, inst->parameter_one);
|
||||
|
||||
ProgramCounter++;
|
||||
|
||||
if (inst->parameter_one == None) {
|
||||
printf("\n");
|
||||
//printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -158,8 +148,8 @@ void HandleOperation(void) {
|
||||
|
||||
if (inst->parameter_one & Reg) {
|
||||
//Encode the register number here.
|
||||
Memory[ProgramCounter - 1] |= (PeekToken()->type - 29) & 0x07;
|
||||
printf("%s ", PeekToken()->lexeme);
|
||||
Memory[ProgramCounter - 1] |= (PeekToken()->type - R1) & 0x07;
|
||||
//printf("%s ", PeekToken()->lexeme);
|
||||
}
|
||||
else if (inst->parameter_one & Address || inst->parameter_one & Constant) {
|
||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||
@@ -175,11 +165,11 @@ void HandleOperation(void) {
|
||||
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) {
|
||||
Memory[ProgramCounter - 1] |= 0x08; //0b00001000;
|
||||
printf("%s ", PeekToken()->lexeme);
|
||||
//printf("%s ", PeekToken()->lexeme);
|
||||
}
|
||||
|
||||
ProgramCounter += 2;
|
||||
@@ -189,7 +179,7 @@ void HandleOperation(void) {
|
||||
}
|
||||
|
||||
if (inst->parameter_two == None) {
|
||||
printf("\n");
|
||||
//printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -213,11 +203,11 @@ void HandleOperation(void) {
|
||||
}
|
||||
|
||||
if (inst->op == CMP) 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) {
|
||||
Memory[ProgramCounter - 1] |= 0x08; //0b00001000;
|
||||
@@ -226,7 +216,7 @@ void HandleOperation(void) {
|
||||
Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||
Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||
|
||||
printf("%s ", PeekToken()->lexeme);
|
||||
//printf("%s ", PeekToken()->lexeme);
|
||||
}
|
||||
|
||||
ProgramCounter += 2;
|
||||
@@ -238,7 +228,7 @@ void HandleOperation(void) {
|
||||
printf("Inst %s (class: %x)\n", inst->lexeme, inst->parameter_two);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
//printf("\n");
|
||||
}
|
||||
|
||||
int ExpectTokenClass(int count, ...) {
|
||||
|
||||
Reference in New Issue
Block a user