Compare commits
4 Commits
be61ecec0f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 74159849c9 | |||
| 8f64b17777 | |||
| f786235064 | |||
| e7350a254f |
@@ -42,6 +42,104 @@ InstructionOpcode stringToInstructionOpcode(const char* string) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void processBufferedToken(
|
||||
char* buf,
|
||||
size_t bufSize,
|
||||
Instruction* instruction,
|
||||
bool* processingInstWord,
|
||||
uint8_t* processingArgNum
|
||||
) {
|
||||
if (*processingInstWord) {
|
||||
instruction->opcode = stringToInstructionOpcode(buf);
|
||||
*processingInstWord = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (*processingArgNum >= 3) {
|
||||
fprintf(stderr, "expecting new line after instruction args, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
OperandType type = INSTRUCTION_OPERAND_TYPES[instruction->opcode][*processingArgNum];
|
||||
switch (type) {
|
||||
case OP_NONE: {
|
||||
fprintf(stderr, "expecting new line after instruction args, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
case OP_REG: {
|
||||
if (bufSize > 1) {
|
||||
fprintf(stderr, "expecting register identifier, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
switch (buf[0]) {
|
||||
case 'a':
|
||||
case 'A':
|
||||
instruction->operands[*processingArgNum] = REG_A;
|
||||
break;
|
||||
case 'b':
|
||||
case 'B':
|
||||
instruction->operands[*processingArgNum] = REG_B;
|
||||
break;
|
||||
case 'c':
|
||||
case 'C':
|
||||
instruction->operands[*processingArgNum] = REG_C;
|
||||
break;
|
||||
case 'x':
|
||||
case 'X':
|
||||
instruction->operands[*processingArgNum] = REG_X;
|
||||
break;
|
||||
case 'y':
|
||||
case 'Y':
|
||||
instruction->operands[*processingArgNum] = REG_Y;
|
||||
break;
|
||||
case 'z':
|
||||
case 'Z':
|
||||
instruction->operands[*processingArgNum] = REG_Z;
|
||||
break;
|
||||
case 'r':
|
||||
case 'R':
|
||||
instruction->operands[*processingArgNum] = REG_R;
|
||||
break;
|
||||
case 'f':
|
||||
case 'F':
|
||||
instruction->operands[*processingArgNum] = REG_F;
|
||||
break;
|
||||
|
||||
default: {
|
||||
fprintf(stderr, "expecting register identifier, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_IMM4: {
|
||||
char* endptr;
|
||||
long value = strtol(buf, &endptr, 0);
|
||||
if (endptr == (char*)buf) {
|
||||
fprintf(stderr, "couldn't convert %s to a number\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
if (value < 0 || value > 0xF) {
|
||||
fprintf(stderr, "%s is out of range for a 4-bit immediate\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
instruction->operands[*processingArgNum] = (Register)value;
|
||||
break;
|
||||
}
|
||||
case OP_IMM16: {
|
||||
char* endptr;
|
||||
instruction->immediate = strtol(buf, &endptr, 0);
|
||||
instruction->hasImm16 = true;
|
||||
if (endptr == (char*)buf) {
|
||||
fprintf(stderr, "couldn't convert %s to a number\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
(*processingArgNum)++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Essentially a lexer for the assembly language.
|
||||
* @param [input] The text to create a program from. NOT the file name
|
||||
@@ -72,80 +170,8 @@ Program createProgramFromText(const char* input) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (processingInstWord) {
|
||||
instruction.opcode = stringToInstructionOpcode(buf);
|
||||
processingInstWord = false;
|
||||
} else {
|
||||
if (processingArgNum >= 3) {
|
||||
fprintf(stderr, "expecting new line after instruction args, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
OperandType type = INSTRUCTION_OPERAND_TYPES[instruction.opcode][processingArgNum];
|
||||
switch (type) {
|
||||
case OP_NONE: {
|
||||
fprintf(stderr, "expecting new line after instruction args, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
case OP_REG: {
|
||||
if (bufSize > 1) {
|
||||
fprintf(stderr, "expecting register identifier, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
switch (buf[0]) {
|
||||
case 'a':
|
||||
case 'A':
|
||||
instruction.operands[processingArgNum] = REG_A;
|
||||
break;
|
||||
case 'b':
|
||||
case 'B':
|
||||
instruction.operands[processingArgNum] = REG_B;
|
||||
break;
|
||||
case 'c':
|
||||
case 'C':
|
||||
instruction.operands[processingArgNum] = REG_C;
|
||||
break;
|
||||
case 'x':
|
||||
case 'X':
|
||||
instruction.operands[processingArgNum] = REG_X;
|
||||
break;
|
||||
case 'y':
|
||||
case 'Y':
|
||||
instruction.operands[processingArgNum] = REG_Y;
|
||||
break;
|
||||
case 'z':
|
||||
case 'Z':
|
||||
instruction.operands[processingArgNum] = REG_Z;
|
||||
break;
|
||||
case 'r':
|
||||
case 'R':
|
||||
instruction.operands[processingArgNum] = REG_R;
|
||||
break;
|
||||
case 'f':
|
||||
case 'F':
|
||||
instruction.operands[processingArgNum] = REG_F;
|
||||
break;
|
||||
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||
|
||||
default: {
|
||||
fprintf(stderr, "expecting register identifier, got %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_IMM16: {
|
||||
char* endptr;
|
||||
instruction.immediate = strtol(buf, &endptr, 0);
|
||||
instruction.hasImm16 = true;
|
||||
if (endptr == (char*)buf) {
|
||||
fprintf(stderr, "couldn't convert %s to a number\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
processingArgNum++;
|
||||
}
|
||||
|
||||
buf[0] = '\0';
|
||||
bufSize = 0;
|
||||
break;
|
||||
@@ -155,6 +181,9 @@ Program createProgramFromText(const char* input) {
|
||||
// Ignore extra new lines
|
||||
break;
|
||||
}
|
||||
if (bufSize > 0) {
|
||||
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||
}
|
||||
addInstructionToProgram(&program, instruction);
|
||||
instruction = (Instruction){};
|
||||
processingInstWord = true;
|
||||
@@ -198,6 +227,12 @@ Program createProgramFromText(const char* input) {
|
||||
|
||||
current++;
|
||||
if (current >= programLen) {
|
||||
if (!(processingInstWord && bufSize == 0)) {
|
||||
if (bufSize > 0) {
|
||||
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||
}
|
||||
addInstructionToProgram(&program, instruction);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -213,11 +248,16 @@ void serializeProgramToFile(Program* program, const char* outputFileName) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
size_t totalWords = 0;
|
||||
for (size_t i = 0; i < program->len; i++) {
|
||||
totalWords += program->at[i].hasImm16 ? 2 : 1;
|
||||
}
|
||||
|
||||
// Write magic header
|
||||
ROMFileHeader header = {
|
||||
.magic = "mlc\0",
|
||||
.version = 1,
|
||||
.numInstructions = program->len
|
||||
.numInstructions = totalWords
|
||||
};
|
||||
|
||||
size_t written = fwrite(&header, sizeof(ROMFileHeader), 1, fp);
|
||||
|
||||
@@ -30,7 +30,7 @@ void addInstructionToProgram(Program* program, Instruction inst) {
|
||||
}
|
||||
|
||||
if (program->len + 1 >= program->capacity) {
|
||||
Instruction* tmp = malloc(sizeof(Instruction) * program->capacity * 2);
|
||||
Instruction* tmp = realloc(program->at, sizeof(Instruction) * program->capacity * 2);
|
||||
if (tmp == NULL) {
|
||||
fprintf(stderr, "malloc failed while expanding program\n");
|
||||
exit(1);
|
||||
@@ -50,7 +50,7 @@ void addLabelToProgram(Program* program, const char* name, uint8_t position) {
|
||||
}
|
||||
|
||||
if (program->labels.len + 1 >= program->labels.capacity) {
|
||||
Label* tmp = malloc(sizeof(Label) * program->labels.capacity * 2);
|
||||
Label* tmp = realloc(program->labels.at, sizeof(Label) * program->labels.capacity * 2);
|
||||
if (tmp == NULL) {
|
||||
fprintf(stderr, "malloc failed while expanding program labels\n");
|
||||
exit(1);
|
||||
|
||||
@@ -226,7 +226,7 @@ void startEmulator(Emulator* emu) {
|
||||
}
|
||||
|
||||
Emulator* initEmulator(uint32_t memorySize) {
|
||||
Emulator* newEmu = malloc(sizeof(Emulator));
|
||||
Emulator* newEmu = calloc(1, sizeof(Emulator));
|
||||
if (!newEmu)
|
||||
return NULL;
|
||||
|
||||
@@ -256,4 +256,4 @@ void freeEmulator(Emulator* emu) {
|
||||
void installDevice(Emulator* emu, Device* device, uint8_t deviceNumber) {
|
||||
assert(deviceNumber < MAX_DEVICES);
|
||||
emu->devices[deviceNumber] = device;
|
||||
}
|
||||
}
|
||||
|
||||
13
src/main.c
13
src/main.c
@@ -36,8 +36,21 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
memcpy(emu->mem->data + MEM_PROGRAM_START, rom->instructions, sizeof(SerializedInst) * rom->header.numInstructions);
|
||||
emu->pc = MEM_PROGRAM_START;
|
||||
|
||||
startEmulator(emu);
|
||||
|
||||
printf("Emulation completed!\n");
|
||||
printf("Registers:\n");
|
||||
printf(" A: %d\n", emu->regs[REG_A]);
|
||||
printf(" B: %d\n", emu->regs[REG_B]);
|
||||
printf(" C: %d\n", emu->regs[REG_C]);
|
||||
printf(" X: %d\n", emu->regs[REG_X]);
|
||||
printf(" Y: %d\n", emu->regs[REG_Y]);
|
||||
printf(" Z: %d\n", emu->regs[REG_Z]);
|
||||
printf(" R: %d\n", emu->regs[REG_R]);
|
||||
printf(" F: %d\n", emu->regs[REG_F]);
|
||||
|
||||
freeEmulator(emu);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user