Added a disassembler which is still very buggy. Also fixed some bugs with the opcode mask getting function.

This commit is contained in:
2022-08-26 21:41:02 +00:00
parent e4a191a17e
commit 0785f21805
8 changed files with 153 additions and 38 deletions
+110
View File
@@ -0,0 +1,110 @@
#include "../includes/disass.h"
const unsigned char* Image;
const unsigned char COPYMASKREG = 0x20;
const unsigned char COPYMASKADD = 0xA0;
const unsigned char ADDMASK = 0x40;
const unsigned char SUBMASK = 0x60;
const unsigned char CMPMASK = 0x80;
const unsigned char REGMASK = 0x00;//0x18; //0001 1000
const unsigned char CONSTMASK = 0x08; //0000 1000
const unsigned char ADDRESSMASK = 0x10; //0001 0000
int IsRegisterPattern(unsigned char pattern, unsigned char lexeme[2]);
void Disassemble(unsigned char image[HIGHMEMORY]) {
Image = image;
int position = 0;
unsigned char lexeme[3] = { 0 };
unsigned char lexem2[8] = { 0 };
unsigned char instruction = image[position];
while(instruction != 0) {
//1110 0000
unsigned char masked = instruction & 0xE0;
if (masked) {
IsRegisterPattern(instruction & 0x07, lexeme);
unsigned char parameter = instruction & 0x18;
if (!parameter) {
position++;
IsRegisterPattern(Image[position], lexem2);
} else if (parameter == CONSTMASK) {
lexem2[0] = 4 >> Image[position] | 0x30;
lexem2[1] = Image[position] | 0x30;
position++;
}
else if (parameter == ADDRESSMASK) {
//snprintf(lexem2, sizeof lexem2, "%#04X", Image[position]);
lexem2[0] = 'X';
}
//If the top bits are set
if (masked & COPYMASKREG) {
printf("copy %s, %d\n", lexeme, Image[position]);
}
else if (masked & COPYMASKADD) {
printf("CMP1\n");
}
else if (masked & ADDMASK) {
printf("CMP2\n");
}
else if (masked & SUBMASK) {
printf("CMP3\n");
}
else if (masked & CMPMASK) {
printf("CMP\n");
}
else {
fprintf(stderr, "[Error] Unknown opcode %#02X\n", masked);
exit(1);
}
}
instruction = Image[position++];
memset(lexem2, '\0', sizeof lexem2);
memset(lexeme, '\0', sizeof lexeme);
}
}
int IsRegisterPattern(unsigned char pattern, unsigned char lexeme[2]) {
if (pattern > 8) return 0;
//The registers' bit patterns are zero indexed, so 'r1' is '000'
//but 'r8' is '111' and so forth.
pattern++;
lexeme[0] = 'r';
lexeme[1] = pattern | 0x30;
return 1;
}
/*
//REG XXX0 0XXX 1010 1111
//Constant XXX0 1XXX
//Address XXX1 0XXX
NOP - 0000 0000 NOP
JZ - 0000 0001 JZ Address
INT - 0000 0010 INT Constant
YLD - 0000 0011 YLD
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
- 0110 1XXX SUB REG, Constant
CMP - 1000 0XXX CMP REG, REG
- 1000 1XXX CMP REG, Constant
- 1001 0XXX CMP REG, Address */