Compare commits
14 Commits
80bca7059f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 74159849c9 | |||
| 8f64b17777 | |||
| f786235064 | |||
| e7350a254f | |||
| be61ecec0f | |||
| ba4121b6c4 | |||
| c5fc6a8493 | |||
| 3bf340d9f2 | |||
| c0da7748d2 | |||
| 14700bdf59 | |||
| afa45ea82d | |||
| 9828ef2811 | |||
| 0504067a51 | |||
| b58b5adf65 |
@@ -9,6 +9,7 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
#include "../instruction.h"
|
#include "../instruction.h"
|
||||||
|
#include "../util.h"
|
||||||
|
|
||||||
// Table of strings to instructions
|
// Table of strings to instructions
|
||||||
struct _stringToInstructionOpcodeTable {char* str; InstructionOpcode opcode;} stringToInstructionOpcodeTable[] = {
|
struct _stringToInstructionOpcodeTable {char* str; InstructionOpcode opcode;} stringToInstructionOpcodeTable[] = {
|
||||||
@@ -41,6 +42,104 @@ InstructionOpcode stringToInstructionOpcode(const char* string) {
|
|||||||
exit(1);
|
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.
|
* Essentially a lexer for the assembly language.
|
||||||
* @param [input] The text to create a program from. NOT the file name
|
* @param [input] The text to create a program from. NOT the file name
|
||||||
@@ -71,79 +170,8 @@ Program createProgramFromText(const char* input) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (processingInstWord) {
|
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||||
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;
|
|
||||||
|
|
||||||
default: {
|
|
||||||
fprintf(stderr, "expecting register identifier, got %s\n", buf);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OP_IMM16: {
|
|
||||||
char* endptr;
|
|
||||||
instruction.immediate = strtol(buf, &endptr, 0);
|
|
||||||
if (endptr == (char*)buf) {
|
|
||||||
fprintf(stderr, "couldn't convert %s to a number\n", buf);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
processingArgNum++;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
bufSize = 0;
|
bufSize = 0;
|
||||||
break;
|
break;
|
||||||
@@ -153,6 +181,9 @@ Program createProgramFromText(const char* input) {
|
|||||||
// Ignore extra new lines
|
// Ignore extra new lines
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (bufSize > 0) {
|
||||||
|
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||||
|
}
|
||||||
addInstructionToProgram(&program, instruction);
|
addInstructionToProgram(&program, instruction);
|
||||||
instruction = (Instruction){};
|
instruction = (Instruction){};
|
||||||
processingInstWord = true;
|
processingInstWord = true;
|
||||||
@@ -196,6 +227,12 @@ Program createProgramFromText(const char* input) {
|
|||||||
|
|
||||||
current++;
|
current++;
|
||||||
if (current >= programLen) {
|
if (current >= programLen) {
|
||||||
|
if (!(processingInstWord && bufSize == 0)) {
|
||||||
|
if (bufSize > 0) {
|
||||||
|
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||||
|
}
|
||||||
|
addInstructionToProgram(&program, instruction);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,6 +248,25 @@ void serializeProgramToFile(Program* program, const char* outputFileName) {
|
|||||||
exit(1);
|
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 = totalWords
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t written = fwrite(&header, sizeof(ROMFileHeader), 1, fp);
|
||||||
|
if (written != 1) {
|
||||||
|
fprintf(stderr, "failed to write file header into file %s\n", outputFileName);
|
||||||
|
fclose(fp);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < program->len; i++) {
|
for (size_t i = 0; i < program->len; i++) {
|
||||||
Instruction* inst = &program->at[i];
|
Instruction* inst = &program->at[i];
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ void addInstructionToProgram(Program* program, Instruction inst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (program->len + 1 >= program->capacity) {
|
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) {
|
if (tmp == NULL) {
|
||||||
fprintf(stderr, "malloc failed while expanding program\n");
|
fprintf(stderr, "malloc failed while expanding program\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@@ -50,7 +50,7 @@ void addLabelToProgram(Program* program, const char* name, uint8_t position) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (program->labels.len + 1 >= program->labels.capacity) {
|
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) {
|
if (tmp == NULL) {
|
||||||
fprintf(stderr, "malloc failed while expanding program labels\n");
|
fprintf(stderr, "malloc failed while expanding program labels\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
110
src/emu.c
110
src/emu.c
@@ -26,6 +26,39 @@ static inline void updateDevices(Emulator* emu) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void pushProgramCounter(Emulator* emu) {
|
||||||
|
((uint32_t*)emu->mem->data)[emu->sp--] = emu->pc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleInterrupt(Emulator* emu) {
|
||||||
|
FlagsRegister flags = (FlagsRegister)emu->regs[REG_F];
|
||||||
|
if (!flags.fields.interrupts) return;
|
||||||
|
|
||||||
|
InterruptHandlerTable interruptTable = *((InterruptHandlerTable*)&emu->mem->data[emu->iht]);
|
||||||
|
|
||||||
|
pushProgramCounter(emu);
|
||||||
|
|
||||||
|
emu->pc = interruptTable.interruptHandlers[emu->irq-1];
|
||||||
|
emu->irq = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t getRegister(Emulator* emu, uint8_t reg) {
|
||||||
|
if (reg == REG_IHT_OFFSET) {
|
||||||
|
return emu->iht;
|
||||||
|
}
|
||||||
|
|
||||||
|
return emu->regs[reg];
|
||||||
|
}
|
||||||
|
|
||||||
|
void setRegister(Emulator* emu, uint8_t reg, uint32_t value) {
|
||||||
|
if (reg == REG_IHT_OFFSET) {
|
||||||
|
emu->iht = value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
emu->regs[reg] = value;
|
||||||
|
}
|
||||||
|
|
||||||
void startEmulator(Emulator* emu) {
|
void startEmulator(Emulator* emu) {
|
||||||
static void* dispatchTable[] = {
|
static void* dispatchTable[] = {
|
||||||
&&MOV,
|
&&MOV,
|
||||||
@@ -49,93 +82,100 @@ void startEmulator(Emulator* emu) {
|
|||||||
emu->halted = false;
|
emu->halted = false;
|
||||||
Instruction inst;
|
Instruction inst;
|
||||||
|
|
||||||
#define DISPATCH() updateDevices(emu); \
|
#define DISPATCH() if (emu->irq != 0) handleInterrupt(emu); \
|
||||||
|
updateDevices(emu); \
|
||||||
|
if (emu->halted) { \
|
||||||
|
return; \
|
||||||
|
} \
|
||||||
inst = fetchInst(emu); \
|
inst = fetchInst(emu); \
|
||||||
|
printf("%s\n", instructionToCStr(inst)); \
|
||||||
goto *dispatchTable[inst.opcode]
|
goto *dispatchTable[inst.opcode]
|
||||||
|
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
|
|
||||||
MOV: {
|
MOV: {
|
||||||
emu->regs[inst.operands[0]] = emu->regs[inst.operands[1]];
|
setRegister(emu, inst.operands[0], getRegister(emu, inst.operands[1]));
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
MVI: {
|
MVI: {
|
||||||
emu->regs[inst.operands[0]] = inst.immediate;
|
setRegister(emu, inst.operands[0], inst.immediate);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
ADD: {
|
ADD: {
|
||||||
int32_t result = emu->regs[inst.operands[0]] + emu->regs[inst.operands[1]];
|
int32_t result = getRegister(emu, inst.operands[0]) + getRegister(emu, inst.operands[1]);
|
||||||
updateFlagsRegister(emu, result);
|
updateFlagsRegister(emu, result);
|
||||||
emu->regs[inst.operands[0]] = result;
|
setRegister(emu, inst.operands[0], result);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
SUB: {
|
SUB: {
|
||||||
int32_t result = emu->regs[inst.operands[0]] - emu->regs[inst.operands[1]];
|
int32_t result = getRegister(emu, inst.operands[0]) - getRegister(emu, inst.operands[1]);
|
||||||
updateFlagsRegister(emu, result);
|
updateFlagsRegister(emu, result);
|
||||||
emu->regs[inst.operands[0]] = result;
|
setRegister(emu, inst.operands[0], result);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
LOAD: {
|
LOAD: {
|
||||||
uint32_t addr = getAddress(
|
uint32_t addr = getAddress(
|
||||||
emu->regs[inst.operands[1]],
|
getRegister(emu, inst.operands[1]),
|
||||||
emu->regs[inst.operands[2]]
|
getRegister(emu, inst.operands[2])
|
||||||
);
|
);
|
||||||
|
|
||||||
emu->regs[inst.operands[0]] = memoryLoadWord(emu->mem, addr);
|
setRegister(emu, inst.operands[0], memoryLoadWord(emu->mem, addr));
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
STORE: {
|
STORE: {
|
||||||
uint32_t addr = getAddress(
|
uint32_t addr = getAddress(
|
||||||
emu->regs[inst.operands[0]],
|
getRegister(emu, inst.operands[1]),
|
||||||
emu->regs[inst.operands[1]]
|
getRegister(emu, inst.operands[2])
|
||||||
);
|
);
|
||||||
memorySaveWord(emu->mem, addr, inst.operands[2]);
|
|
||||||
|
memorySaveWord(emu->mem, addr, getRegister(emu, inst.operands[2]));
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
AND: {
|
AND: {
|
||||||
int32_t result = emu->regs[inst.operands[0]] & emu->regs[inst.operands[1]];
|
int32_t result = getRegister(emu, inst.operands[0]) & getRegister(emu, inst.operands[1]);
|
||||||
updateFlagsRegister(emu, result);
|
updateFlagsRegister(emu, result);
|
||||||
emu->regs[inst.operands[0]] = result;
|
setRegister(emu, inst.operands[0], result);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
OR: {
|
OR: {
|
||||||
int32_t result = emu->regs[inst.operands[0]] | emu->regs[inst.operands[1]];
|
int32_t result = getRegister(emu, inst.operands[0]) | getRegister(emu, inst.operands[1]);
|
||||||
updateFlagsRegister(emu, result);
|
updateFlagsRegister(emu, result);
|
||||||
emu->regs[inst.operands[0]] = result;
|
setRegister(emu, inst.operands[0], result);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
XOR: {
|
XOR: {
|
||||||
int32_t result = emu->regs[inst.operands[0]] ^ emu->regs[inst.operands[1]];
|
int32_t result = getRegister(emu, inst.operands[0]) ^ getRegister(emu, inst.operands[1]);
|
||||||
updateFlagsRegister(emu, result);
|
updateFlagsRegister(emu, result);
|
||||||
emu->regs[inst.operands[0]] = result;
|
setRegister(emu, inst.operands[0], result);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
SHF: {
|
SHF: {
|
||||||
int32_t result;
|
int32_t result;
|
||||||
if (inst.operands[2] == 0)
|
|
||||||
result = emu->regs[inst.operands[0]] << emu->regs[inst.operands[1]];
|
if (inst.operands[0] == 0)
|
||||||
|
result = getRegister(emu, inst.operands[0]) << getRegister(emu, inst.operands[1]);
|
||||||
else
|
else
|
||||||
result = emu->regs[inst.operands[0]] >> emu->regs[inst.operands[1]];
|
result = getRegister(emu, inst.operands[0]) >> getRegister(emu, inst.operands[1]);
|
||||||
|
|
||||||
updateFlagsRegister(emu, result);
|
updateFlagsRegister(emu, result);
|
||||||
emu->regs[inst.operands[0]] = result;
|
setRegister(emu, inst.operands[0], result);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
JMP: {
|
JMP: {
|
||||||
emu->pc = getAddress(inst.operands[0], inst.operands[1]);
|
emu->pc = inst.immediate;
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
JIZ: {
|
JIZ: {
|
||||||
if (emu->regs[REG_F] & FLAG_ZERO)
|
if (((FlagsRegister)emu->regs[REG_F]).fields.zero)
|
||||||
emu->pc = getAddress(inst.operands[0], inst.operands[1]);
|
emu->pc = inst.immediate;
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
CALL: {
|
CALL: {
|
||||||
// save the pc at the current sp and decrement the sp (stack grows downward)
|
// save the pc at the current sp and decrement the sp (stack grows downward)
|
||||||
((uint32_t*)emu->mem->data)[emu->sp--] = emu->pc;
|
pushProgramCounter(emu);
|
||||||
|
|
||||||
// jump to given addr
|
// jump to given addr
|
||||||
emu->pc = getAddress(inst.operands[0], inst.operands[1]);
|
emu->pc = inst.immediate;
|
||||||
|
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
@@ -145,7 +185,7 @@ void startEmulator(Emulator* emu) {
|
|||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
PORT: {
|
PORT: {
|
||||||
uint16_t port = emu->regs[inst.operands[0]];
|
uint16_t port = getRegister(emu, inst.operands[0]);
|
||||||
if (port >= MAX_DEVICES) {
|
if (port >= MAX_DEVICES) {
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
@@ -158,7 +198,7 @@ void startEmulator(Emulator* emu) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (direction == 0) {
|
if (direction == 0) {
|
||||||
uint16_t value = emu->regs[inst.operands[1]];
|
uint16_t value = getRegister(emu, inst.operands[1]);
|
||||||
|
|
||||||
if (device->inPos == DEVICE_BUFFER_LENGTH) {
|
if (device->inPos == DEVICE_BUFFER_LENGTH) {
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
@@ -168,11 +208,11 @@ void startEmulator(Emulator* emu) {
|
|||||||
device->inPos++;
|
device->inPos++;
|
||||||
} else {
|
} else {
|
||||||
if (device->outPos == 0) {
|
if (device->outPos == 0) {
|
||||||
emu->regs[inst.operands[1]] = 0;
|
setRegister(emu, inst.operands[1], 0);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
}
|
}
|
||||||
|
|
||||||
emu->regs[inst.operands[1]] = device->out[device->outPos-1];
|
setRegister(emu, inst.operands[1], device->out[device->outPos-1]);
|
||||||
if (device->outPos > 0)
|
if (device->outPos > 0)
|
||||||
device->outPos--;
|
device->outPos--;
|
||||||
}
|
}
|
||||||
@@ -186,7 +226,7 @@ void startEmulator(Emulator* emu) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Emulator* initEmulator(uint32_t memorySize) {
|
Emulator* initEmulator(uint32_t memorySize) {
|
||||||
Emulator* newEmu = malloc(sizeof(Emulator));
|
Emulator* newEmu = calloc(1, sizeof(Emulator));
|
||||||
if (!newEmu)
|
if (!newEmu)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -196,7 +236,7 @@ Emulator* initEmulator(uint32_t memorySize) {
|
|||||||
// zero out special regs
|
// zero out special regs
|
||||||
newEmu->pc = MEM_PROGRAM_START;
|
newEmu->pc = MEM_PROGRAM_START;
|
||||||
newEmu->sp = 0;
|
newEmu->sp = 0;
|
||||||
newEmu->ihp = 0;
|
newEmu->iht = 0;
|
||||||
|
|
||||||
// init memory
|
// init memory
|
||||||
newEmu->mem = initMemory(memorySize);
|
newEmu->mem = initMemory(memorySize);
|
||||||
@@ -216,4 +256,4 @@ void freeEmulator(Emulator* emu) {
|
|||||||
void installDevice(Emulator* emu, Device* device, uint8_t deviceNumber) {
|
void installDevice(Emulator* emu, Device* device, uint8_t deviceNumber) {
|
||||||
assert(deviceNumber < MAX_DEVICES);
|
assert(deviceNumber < MAX_DEVICES);
|
||||||
emu->devices[deviceNumber] = device;
|
emu->devices[deviceNumber] = device;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,13 +21,17 @@ typedef struct {
|
|||||||
Device* devices[MAX_DEVICES];
|
Device* devices[MAX_DEVICES];
|
||||||
|
|
||||||
uint32_t pc;
|
uint32_t pc;
|
||||||
uint32_t ihp;
|
uint32_t iht;
|
||||||
uint32_t sp;
|
uint32_t sp;
|
||||||
|
|
||||||
bool irq;
|
uint16_t irq;
|
||||||
bool halted;
|
bool halted;
|
||||||
} Emulator;
|
} Emulator;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t interruptHandlers[32];
|
||||||
|
} __attribute__((packed)) InterruptHandlerTable;
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
struct {
|
struct {
|
||||||
uint16_t zero : 1;
|
uint16_t zero : 1;
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ const OperandType INSTRUCTION_OPERAND_TYPES[][3] = {
|
|||||||
{OP_REG, OP_REG, OP_NONE}, // OR
|
{OP_REG, OP_REG, OP_NONE}, // OR
|
||||||
{OP_REG, OP_REG, OP_NONE}, // XOR
|
{OP_REG, OP_REG, OP_NONE}, // XOR
|
||||||
{OP_REG, OP_REG, OP_REG}, // SHF
|
{OP_REG, OP_REG, OP_REG}, // SHF
|
||||||
{OP_REG, OP_REG, OP_NONE}, // JMP
|
{OP_IMM16, OP_NONE, OP_NONE}, // JMP
|
||||||
{OP_REG, OP_REG, OP_NONE}, // JIZ
|
{OP_IMM16, OP_NONE, OP_NONE}, // JIZ
|
||||||
{OP_IMM16, OP_NONE, OP_NONE},// CALL
|
{OP_IMM16, OP_NONE, OP_NONE},// CALL
|
||||||
{OP_NONE, OP_NONE, OP_NONE}, // RET
|
{OP_NONE, OP_NONE, OP_NONE}, // RET
|
||||||
{OP_REG, OP_REG, OP_IMM4}, // PORT
|
{OP_REG, OP_REG, OP_IMM4}, // PORT
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ typedef enum : uint8_t {
|
|||||||
// automatically know the length of the registers array
|
// automatically know the length of the registers array
|
||||||
} Register;
|
} Register;
|
||||||
|
|
||||||
|
#define REG_IHT_OFFSET 8 // offset of interrupt handler table register
|
||||||
|
|
||||||
typedef uint16_t SerializedInst;
|
typedef uint16_t SerializedInst;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
35
src/main.c
35
src/main.c
@@ -2,13 +2,20 @@
|
|||||||
#include "emu.h"
|
#include "emu.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
|
if (argc < 2) {
|
||||||
|
fprintf(stderr, "Usage: %s <ROM to emulate>\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
Emulator* emu = initEmulator(0x00010FFF);
|
Emulator* emu = initEmulator(0x00010FFF);
|
||||||
if (!emu) {
|
if (!emu) {
|
||||||
fprintf(stderr, "emu: error: failed to allocate memory for emulator\n");
|
fprintf(stderr, "emu: error: failed to allocate memory for emulator\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
Instruction program[] = {
|
Instruction program[] = {
|
||||||
{INST_MVI, {REG_A}, 0, true},
|
{INST_MVI, {REG_A}, 0, true},
|
||||||
{INST_MVI, {REG_B}, 123, true},
|
{INST_MVI, {REG_B}, 123, true},
|
||||||
@@ -17,10 +24,34 @@ int main() {
|
|||||||
{INST_HLT, {}, 0, false}
|
{INST_HLT, {}, 0, false}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
RomFile* f = readProgramFromFile("../add.o");
|
||||||
|
memcpy(emu->mem + MEM_PROGRAM_START, f->instructions, sizeof(SerializedInst) * f->header.numInstructions);
|
||||||
memoryLoadProgram(emu->mem, program, sizeof(program)/sizeof(program[0]));
|
memoryLoadProgram(emu->mem, program, sizeof(program)/sizeof(program[0]));
|
||||||
|
*/
|
||||||
|
|
||||||
|
RomFile* rom = readProgramFromFile(argv[1]);
|
||||||
|
if (rom == NULL) {
|
||||||
|
fprintf(stderr, "Failed to read rom from %s\n", argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(emu->mem->data + MEM_PROGRAM_START, rom->instructions, sizeof(SerializedInst) * rom->header.numInstructions);
|
||||||
|
emu->pc = MEM_PROGRAM_START;
|
||||||
|
|
||||||
startEmulator(emu);
|
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);
|
freeEmulator(emu);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,7 +83,8 @@ Instruction decodeInstruction(Memory* m, uint32_t* pc) {
|
|||||||
case OP_IMM16: { // the immediate 16 takes up the space where the next instruction would be
|
case OP_IMM16: { // the immediate 16 takes up the space where the next instruction would be
|
||||||
(*pc) += sizeof(SerializedInst);
|
(*pc) += sizeof(SerializedInst);
|
||||||
|
|
||||||
uint16_t nextDataOver = m->data[*pc];
|
uint16_t nextDataOver;
|
||||||
|
memcpy(&nextDataOver, &m->data[*pc], sizeof(uint16_t));
|
||||||
outputInst.immediate = nextDataOver;
|
outputInst.immediate = nextDataOver;
|
||||||
outputInst.hasImm16 = true;
|
outputInst.hasImm16 = true;
|
||||||
break;
|
break;
|
||||||
@@ -109,4 +110,4 @@ void freeMemory(Memory* m) {
|
|||||||
|
|
||||||
inline uint32_t getAddress(uint16_t low, uint16_t high) {
|
inline uint32_t getAddress(uint16_t low, uint16_t high) {
|
||||||
return (high << 16) | low;
|
return (high << 16) | low;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ RomFile* readProgramFromFile(char* path) {
|
|||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
ROMFileHeader* header = (ROMFileHeader*)buffer;
|
ROMFileHeader* header = (ROMFileHeader*)buffer;
|
||||||
Instruction* instructions = calloc(header->numInstructions, sizeof(SerializedInst));
|
SerializedInst* instructions = calloc(header->numInstructions, sizeof(SerializedInst));
|
||||||
if (!instructions) {
|
if (!instructions) {
|
||||||
free(buffer);
|
free(buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ROMFileHeader header;
|
ROMFileHeader header;
|
||||||
Instruction* instructions;
|
SerializedInst* instructions;
|
||||||
} RomFile;
|
} RomFile;
|
||||||
|
|
||||||
// -- FUNCTIONS -- //
|
// -- FUNCTIONS -- //
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
dingus:
|
|
||||||
|
|
||||||
mvi a 2
|
mvi a 2
|
||||||
mvi b 2
|
mvi b 2
|
||||||
|
|
||||||
add a b
|
add a b
|
||||||
hlt
|
hlt
|
||||||
|
hlt
|
||||||
|
|||||||
Reference in New Issue
Block a user