Compare commits
27 Commits
92f2cddb99
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 74159849c9 | |||
| 8f64b17777 | |||
| f786235064 | |||
| e7350a254f | |||
| be61ecec0f | |||
| ba4121b6c4 | |||
| c5fc6a8493 | |||
| 3bf340d9f2 | |||
| c0da7748d2 | |||
| 14700bdf59 | |||
| afa45ea82d | |||
| 9828ef2811 | |||
| 0504067a51 | |||
| b58b5adf65 | |||
| 80bca7059f | |||
| 82f9644e15 | |||
| bb0709f6ce | |||
| 8e1f83ab31 | |||
| fb3efc77c3 | |||
| 68b7667725 | |||
| 42fab467f1 | |||
| a2d90f620a | |||
| c270a48141 | |||
| 74992e673a | |||
| 6a91f64fce | |||
| d5a0df1299 | |||
| b27c75b5e9 |
@@ -5,11 +5,15 @@ sources = files(
|
||||
'src/emu.c',
|
||||
'src/memory.c',
|
||||
'src/instruction.c',
|
||||
'src/util.c'
|
||||
'src/util.c',
|
||||
'src/device.c'
|
||||
)
|
||||
|
||||
asmSources = files(
|
||||
'src/assembler/main.c'
|
||||
'src/assembler/main.c',
|
||||
'src/assembler/assemble.c',
|
||||
'src/assembler/program.c',
|
||||
'src/instruction.c'
|
||||
)
|
||||
|
||||
args = ['-Wall', '-Wextra']
|
||||
|
||||
314
src/assembler/assemble.c
Normal file
314
src/assembler/assemble.c
Normal file
@@ -0,0 +1,314 @@
|
||||
#include "assemble.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "../instruction.h"
|
||||
#include "../util.h"
|
||||
|
||||
// Table of strings to instructions
|
||||
struct _stringToInstructionOpcodeTable {char* str; InstructionOpcode opcode;} stringToInstructionOpcodeTable[] = {
|
||||
{"mov", INST_MOV},
|
||||
{"mvi", INST_MVI},
|
||||
{"add", INST_ADD},
|
||||
{"sub", INST_SUB},
|
||||
{"load", INST_LOAD},
|
||||
{"store", INST_STORE},
|
||||
{"and", INST_AND},
|
||||
{"or", INST_OR},
|
||||
{"xor", INST_XOR},
|
||||
{"shf", INST_SHF},
|
||||
{"jmp", INST_JMP},
|
||||
{"jiz", INST_JIZ},
|
||||
{"call", INST_CALL},
|
||||
{"ret", INST_RET},
|
||||
{"port", INST_PORT},
|
||||
{"hlt", INST_HLT},
|
||||
};
|
||||
|
||||
InstructionOpcode stringToInstructionOpcode(const char* string) {
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
if (strcmp(stringToInstructionOpcodeTable[i].str, string) == 0) {
|
||||
return stringToInstructionOpcodeTable[i].opcode;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "Invalid instruction string %s\n", 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
|
||||
* @return A new program, with all instructions from the input
|
||||
*/
|
||||
Program createProgramFromText(const char* input) {
|
||||
Program program = newProgram();
|
||||
|
||||
char buf[32] = {'\0'};
|
||||
size_t bufSize = 0;
|
||||
size_t programLen = strlen(input);
|
||||
size_t current = 0;
|
||||
|
||||
uint8_t currentInstruction = 0;
|
||||
|
||||
bool processingInstWord = true;
|
||||
uint8_t processingArgNum = 0;
|
||||
|
||||
Instruction instruction = {};
|
||||
|
||||
for (;;) {
|
||||
// scan through until we hit a space, colon or new line
|
||||
char c = input[current];
|
||||
switch (c) {
|
||||
case ' ': {
|
||||
if (bufSize == 0) {
|
||||
// Ignore extra spaces
|
||||
break;
|
||||
}
|
||||
|
||||
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||
|
||||
buf[0] = '\0';
|
||||
bufSize = 0;
|
||||
break;
|
||||
}
|
||||
case '\n': {
|
||||
if (processingInstWord && bufSize == 0) {
|
||||
// Ignore extra new lines
|
||||
break;
|
||||
}
|
||||
if (bufSize > 0) {
|
||||
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||
}
|
||||
addInstructionToProgram(&program, instruction);
|
||||
instruction = (Instruction){};
|
||||
processingInstWord = true;
|
||||
processingArgNum = 0;
|
||||
buf[0] = '\0';
|
||||
bufSize = 0;
|
||||
currentInstruction++;
|
||||
break;
|
||||
}
|
||||
case ':': {
|
||||
if (!processingInstWord) {
|
||||
fprintf(stderr, "label must be first word on a new line\n");
|
||||
exit(1);
|
||||
}
|
||||
addLabelToProgram(&program, buf, currentInstruction);
|
||||
if (input[current + 1] != '\n') {
|
||||
fprintf(stderr, "expecting new line after label\n");
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '\t': {
|
||||
// Ignore tabs
|
||||
break;
|
||||
}
|
||||
case '\r': {
|
||||
// Ignore Windows specific formatting
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
if (bufSize + 2 >= sizeof(buf)) {
|
||||
fprintf(stderr, "text exceeds buffer size\n");
|
||||
exit(1);
|
||||
}
|
||||
buf[bufSize] = c;
|
||||
buf[bufSize + 1] = '\0';
|
||||
bufSize++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
current++;
|
||||
if (current >= programLen) {
|
||||
if (!(processingInstWord && bufSize == 0)) {
|
||||
if (bufSize > 0) {
|
||||
processBufferedToken(buf, bufSize, &instruction, &processingInstWord, &processingArgNum);
|
||||
}
|
||||
addInstructionToProgram(&program, instruction);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
void serializeProgramToFile(Program* program, const char* outputFileName) {
|
||||
|
||||
FILE* fp = fopen(outputFileName, "wb");
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "Couldn't open file %s for writing\n", 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 = 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++) {
|
||||
Instruction* inst = &program->at[i];
|
||||
|
||||
uint16_t packed = 0;
|
||||
|
||||
packed |= (SerializedInst)(inst->opcode & 0b0000000000001111) ;
|
||||
packed |= (SerializedInst)(inst->operands[0] & 0b0000000000001111) << 4;
|
||||
packed |= (SerializedInst)(inst->operands[1] & 0b0000000000001111) << 8;
|
||||
packed |= (SerializedInst)(inst->operands[2] & 0b0000000000001111) << 12;
|
||||
|
||||
size_t written = fwrite(&packed, sizeof(uint16_t), 1, fp);
|
||||
if (written != 1) {
|
||||
fprintf(stderr, "failed to write %" PRIx16 " into file %s\n", packed, outputFileName);
|
||||
fclose(fp);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (inst->hasImm16) {
|
||||
size_t written = fwrite(&inst->immediate, sizeof(uint16_t), 1, fp);
|
||||
if (written != 1) {
|
||||
fprintf(stderr, "failed to write %" PRIx16 " into file %s\n", packed, outputFileName);
|
||||
fclose(fp);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
}
|
||||
|
||||
void assemble(const char* input, const char* outputFileName) {
|
||||
clock_t start = clock();
|
||||
Program program = createProgramFromText(input);
|
||||
clock_t parsed = clock();
|
||||
serializeProgramToFile(&program, outputFileName);
|
||||
clock_t serialized = clock();
|
||||
|
||||
printf("Finished assembling %s, took %lg seconds (%lg secs parsing, %lg secs serializing)\n",
|
||||
outputFileName,
|
||||
(serialized - start) / (long double) CLOCKS_PER_SEC,
|
||||
(parsed - start) / (long double) CLOCKS_PER_SEC,
|
||||
(serialized - parsed) / (long double) CLOCKS_PER_SEC
|
||||
);
|
||||
}
|
||||
7
src/assembler/assemble.h
Normal file
7
src/assembler/assemble.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "program.h"
|
||||
|
||||
Program createProgramFromText(const char* input);
|
||||
|
||||
void assemble(const char* input, const char* outputFileName);
|
||||
60
src/assembler/main.c
Normal file
60
src/assembler/main.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "assemble.h"
|
||||
|
||||
char* getFileContents(const char* filename) {
|
||||
// https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c
|
||||
FILE* fp;
|
||||
long lSize;
|
||||
char* file;
|
||||
|
||||
fp = fopen(filename, "rb");
|
||||
if (!fp) {
|
||||
perror(filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
lSize = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
file = calloc(1, lSize + 1);
|
||||
if (!file) {
|
||||
fclose(fp);
|
||||
fprintf(stderr, "memory allocation fail when reading file %s\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (1!=fread(file, lSize, 1, fp)) {
|
||||
fclose(fp);
|
||||
free(file);
|
||||
fputs("couldn't read entire file", stderr);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// we done
|
||||
fclose(fp);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s\n <input file> [output file]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
char* outputName = "out.bin";
|
||||
if (argc >= 3) {
|
||||
outputName = argv[2];
|
||||
}
|
||||
|
||||
char* asmContents = getFileContents(argv[1]);
|
||||
if (asmContents == NULL) {
|
||||
printf("couldn't read file %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO: implement
|
||||
assemble(asmContents, outputName);
|
||||
}
|
||||
95
src/assembler/program.c
Normal file
95
src/assembler/program.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "program.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
Program newProgram(void) {
|
||||
Program program = {
|
||||
.at = malloc(sizeof(Instruction) * 16),
|
||||
.len = 0,
|
||||
.capacity = 16,
|
||||
.labels = {
|
||||
.at = malloc(sizeof(Instruction) * 16),
|
||||
.len = 0,
|
||||
.capacity = 16,
|
||||
}
|
||||
};
|
||||
|
||||
if (program.at == NULL || program.labels.at == NULL) {
|
||||
fprintf(stderr, "malloc failed while creating new program\n");
|
||||
exit(1);
|
||||
}
|
||||
return program;
|
||||
}
|
||||
|
||||
void addInstructionToProgram(Program* program, Instruction inst) {
|
||||
if (program->at == NULL) {
|
||||
fprintf(stderr, "unexpected NULL value when adding instruction to program\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (program->len + 1 >= program->capacity) {
|
||||
Instruction* tmp = realloc(program->at, sizeof(Instruction) * program->capacity * 2);
|
||||
if (tmp == NULL) {
|
||||
fprintf(stderr, "malloc failed while expanding program\n");
|
||||
exit(1);
|
||||
}
|
||||
program->at = tmp;
|
||||
program->capacity *= 2;
|
||||
}
|
||||
|
||||
program->at[program->len] = inst;
|
||||
program->len++;
|
||||
}
|
||||
|
||||
void addLabelToProgram(Program* program, const char* name, uint8_t position) {
|
||||
if (program->labels.at == NULL) {
|
||||
fprintf(stderr, "unexpected NULL value when adding label to program\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (program->labels.len + 1 >= program->labels.capacity) {
|
||||
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);
|
||||
}
|
||||
program->labels.at = tmp;
|
||||
program->labels.capacity *= 2;
|
||||
}
|
||||
|
||||
char* nameCopy = malloc(strlen(name) + 1);
|
||||
if (nameCopy == NULL) {
|
||||
fprintf(stderr, "malloc failed while expanding program labels\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
strcpy(nameCopy, name);
|
||||
|
||||
program->labels.at[program->labels.len] = (Label) {
|
||||
.name = nameCopy,
|
||||
.position = position
|
||||
};
|
||||
program->labels.len++;
|
||||
}
|
||||
|
||||
void freeProgram(Program* program) {
|
||||
if (program->at != NULL) {
|
||||
free(program->at);
|
||||
}
|
||||
program->at = NULL;
|
||||
program->capacity = 0;
|
||||
program->len = 0;
|
||||
|
||||
if (program->labels.at != NULL) {
|
||||
for (size_t i = 0; i < program->labels.len; i++) {
|
||||
free(program->labels.at[i].name);
|
||||
}
|
||||
free(program->labels.at);
|
||||
}
|
||||
program->labels.at = NULL;
|
||||
program->labels.capacity = 0;
|
||||
program->labels.len = 0;
|
||||
}
|
||||
|
||||
26
src/assembler/program.h
Normal file
26
src/assembler/program.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "../instruction.h"
|
||||
|
||||
typedef struct {
|
||||
char* name;
|
||||
uint8_t position;
|
||||
} Label;
|
||||
|
||||
typedef struct {
|
||||
Instruction* at;
|
||||
size_t len;
|
||||
size_t capacity;
|
||||
|
||||
struct {
|
||||
Label* at;
|
||||
size_t len;
|
||||
size_t capacity;
|
||||
} labels;
|
||||
} Program;
|
||||
|
||||
Program newProgram(void);
|
||||
void addInstructionToProgram(Program* program, Instruction inst);
|
||||
void addLabelToProgram(Program* program, const char* name, uint8_t position);
|
||||
void freeProgram(Program* program);
|
||||
|
||||
38
src/device.c
Normal file
38
src/device.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "device.h"
|
||||
|
||||
Device* createDevice(
|
||||
const char* name,
|
||||
void (*update)(Device* self)
|
||||
) {
|
||||
Device* new = malloc(sizeof(Device));
|
||||
if (!new)
|
||||
return NULL;
|
||||
|
||||
size_t nameLen = strlen(name) + 1;
|
||||
nameLen = nameLen < 32 ? nameLen : 32;
|
||||
|
||||
memcpy(new->name, name, nameLen);
|
||||
new->name[nameLen] = 0;
|
||||
|
||||
new->update = update;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
uint16_t deviceRead(Device* self) {
|
||||
if (self->inPos == 0)
|
||||
return 0;
|
||||
|
||||
return self->in[--self->inPos];
|
||||
}
|
||||
|
||||
void deviceSend(Device* self, uint16_t value) {
|
||||
if (self->outPos == DEVICE_BUFFER_LENGTH)
|
||||
return;
|
||||
|
||||
self->out[self->outPos++] = value;
|
||||
}
|
||||
|
||||
bool deviceCanRead(Device* self) {
|
||||
return self->inPos > 0;
|
||||
}
|
||||
30
src/device.h
Normal file
30
src/device.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define DEVICE_BUFFER_LENGTH 128
|
||||
|
||||
typedef struct Device Device;
|
||||
struct Device {
|
||||
char name[32];
|
||||
|
||||
uint16_t in[DEVICE_BUFFER_LENGTH];
|
||||
uint8_t inPos;
|
||||
|
||||
uint16_t out[DEVICE_BUFFER_LENGTH];
|
||||
uint8_t outPos;
|
||||
|
||||
void (*update)(Device* self);
|
||||
};
|
||||
|
||||
Device* createDevice(
|
||||
const char* name,
|
||||
void (*update)(Device* self)
|
||||
);
|
||||
|
||||
uint16_t deviceRead(Device* self);
|
||||
void deviceSend(Device* self, uint16_t value);
|
||||
bool deviceCanRead(Device* self);
|
||||
173
src/emu.c
173
src/emu.c
@@ -1,21 +1,64 @@
|
||||
#include "emu.h"
|
||||
|
||||
static inline Instruction fetchInst(Emulator* emu) {
|
||||
Instruction inst = decodeInstruction(emu->mem, &emu->pc);
|
||||
printf("%s\n", instructionToCStr(inst));
|
||||
return inst;
|
||||
return decodeInstruction(emu->mem, &emu->pc);
|
||||
}
|
||||
|
||||
static inline void updateFlagsRegister(Emulator* emu, int32_t value) {
|
||||
if (value == 0) {
|
||||
emu->regs[REG_F] = FLAG_ZERO;
|
||||
} else if (value < 0) {
|
||||
emu->regs[REG_F] = FLAG_NEGATIVE;
|
||||
} else if (value > UINT16_MAX) {
|
||||
emu->regs[REG_F] = FLAG_OVERFLOW;
|
||||
|
||||
FlagsRegister flags = (FlagsRegister)emu->regs[REG_F];
|
||||
|
||||
flags.fields.zero = value == 0;
|
||||
flags.fields.negative = value < 0;
|
||||
flags.fields.overflow = value > UINT16_MAX;
|
||||
|
||||
emu->regs[REG_F] = flags.raw;
|
||||
}
|
||||
|
||||
static inline void updateDevices(Emulator* emu) {
|
||||
for (size_t i = 0; i < MAX_DEVICES; i++) {
|
||||
Device* device = emu->devices[i];
|
||||
if (!device) continue;
|
||||
|
||||
if (device->update) {
|
||||
device->update(device);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
static void* dispatchTable[] = {
|
||||
&&MOV,
|
||||
@@ -32,99 +75,107 @@ void startEmulator(Emulator* emu) {
|
||||
&&JIZ,
|
||||
&&CALL,
|
||||
&&RET,
|
||||
&&SYS,
|
||||
&&PORT,
|
||||
&&HLT
|
||||
};
|
||||
|
||||
emu->halted = false;
|
||||
Instruction inst;
|
||||
|
||||
#define DISPATCH() inst = fetchInst(emu); \
|
||||
goto *dispatchTable[inst.opcode];
|
||||
#define DISPATCH() if (emu->irq != 0) handleInterrupt(emu); \
|
||||
updateDevices(emu); \
|
||||
if (emu->halted) { \
|
||||
return; \
|
||||
} \
|
||||
inst = fetchInst(emu); \
|
||||
printf("%s\n", instructionToCStr(inst)); \
|
||||
goto *dispatchTable[inst.opcode]
|
||||
|
||||
DISPATCH();
|
||||
|
||||
MOV: {
|
||||
emu->regs[inst.operands[0]] = emu->regs[inst.operands[1]];
|
||||
setRegister(emu, inst.operands[0], getRegister(emu, inst.operands[1]));
|
||||
DISPATCH();
|
||||
}
|
||||
MVI: {
|
||||
emu->regs[inst.operands[0]] = inst.immediate;
|
||||
setRegister(emu, inst.operands[0], inst.immediate);
|
||||
DISPATCH();
|
||||
}
|
||||
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);
|
||||
emu->regs[inst.operands[0]] = result;
|
||||
setRegister(emu, inst.operands[0], result);
|
||||
DISPATCH();
|
||||
}
|
||||
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);
|
||||
emu->regs[inst.operands[0]] = result;
|
||||
setRegister(emu, inst.operands[0], result);
|
||||
DISPATCH();
|
||||
}
|
||||
LOAD: {
|
||||
uint32_t addr = getAddress(
|
||||
emu->regs[inst.operands[1]],
|
||||
emu->regs[inst.operands[2]]
|
||||
getRegister(emu, inst.operands[1]),
|
||||
getRegister(emu, inst.operands[2])
|
||||
);
|
||||
|
||||
emu->regs[inst.operands[0]] = memoryLoadWord(emu->mem, addr);
|
||||
setRegister(emu, inst.operands[0], memoryLoadWord(emu->mem, addr));
|
||||
DISPATCH();
|
||||
}
|
||||
STORE: {
|
||||
uint32_t addr = getAddress(
|
||||
emu->regs[inst.operands[0]],
|
||||
emu->regs[inst.operands[1]]
|
||||
getRegister(emu, 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();
|
||||
}
|
||||
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);
|
||||
emu->regs[inst.operands[0]] = result;
|
||||
setRegister(emu, inst.operands[0], result);
|
||||
DISPATCH();
|
||||
}
|
||||
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);
|
||||
emu->regs[inst.operands[0]] = result;
|
||||
setRegister(emu, inst.operands[0], result);
|
||||
DISPATCH();
|
||||
}
|
||||
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);
|
||||
emu->regs[inst.operands[0]] = result;
|
||||
setRegister(emu, inst.operands[0], result);
|
||||
DISPATCH();
|
||||
}
|
||||
SHF: {
|
||||
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
|
||||
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);
|
||||
emu->regs[inst.operands[0]] = result;
|
||||
setRegister(emu, inst.operands[0], result);
|
||||
DISPATCH();
|
||||
}
|
||||
JMP: {
|
||||
emu->pc = getAddress(inst.operands[0], inst.operands[1]);
|
||||
emu->pc = inst.immediate;
|
||||
DISPATCH();
|
||||
}
|
||||
JIZ: {
|
||||
if (emu->regs[REG_F] & FLAG_ZERO)
|
||||
emu->pc = getAddress(inst.operands[0], inst.operands[1]);
|
||||
if (((FlagsRegister)emu->regs[REG_F]).fields.zero)
|
||||
emu->pc = inst.immediate;
|
||||
DISPATCH();
|
||||
}
|
||||
CALL: {
|
||||
// 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
|
||||
emu->pc = getAddress(inst.operands[0], inst.operands[1]);
|
||||
emu->pc = inst.immediate;
|
||||
|
||||
DISPATCH();
|
||||
}
|
||||
@@ -133,8 +184,39 @@ void startEmulator(Emulator* emu) {
|
||||
emu->pc = ((uint32_t*)emu->mem->data)[++emu->sp];
|
||||
DISPATCH();
|
||||
}
|
||||
SYS: {
|
||||
// TODO
|
||||
PORT: {
|
||||
uint16_t port = getRegister(emu, inst.operands[0]);
|
||||
if (port >= MAX_DEVICES) {
|
||||
DISPATCH();
|
||||
}
|
||||
|
||||
uint8_t direction = inst.operands[2];
|
||||
|
||||
Device* device = emu->devices[port];
|
||||
if (!device) {
|
||||
DISPATCH();
|
||||
}
|
||||
|
||||
if (direction == 0) {
|
||||
uint16_t value = getRegister(emu, inst.operands[1]);
|
||||
|
||||
if (device->inPos == DEVICE_BUFFER_LENGTH) {
|
||||
DISPATCH();
|
||||
}
|
||||
|
||||
device->in[device->inPos] = value;
|
||||
device->inPos++;
|
||||
} else {
|
||||
if (device->outPos == 0) {
|
||||
setRegister(emu, inst.operands[1], 0);
|
||||
DISPATCH();
|
||||
}
|
||||
|
||||
setRegister(emu, inst.operands[1], device->out[device->outPos-1]);
|
||||
if (device->outPos > 0)
|
||||
device->outPos--;
|
||||
}
|
||||
|
||||
DISPATCH();
|
||||
}
|
||||
HLT: {
|
||||
@@ -144,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;
|
||||
|
||||
@@ -154,7 +236,7 @@ Emulator* initEmulator(uint32_t memorySize) {
|
||||
// zero out special regs
|
||||
newEmu->pc = MEM_PROGRAM_START;
|
||||
newEmu->sp = 0;
|
||||
newEmu->ihp = 0;
|
||||
newEmu->iht = 0;
|
||||
|
||||
// init memory
|
||||
newEmu->mem = initMemory(memorySize);
|
||||
@@ -170,3 +252,8 @@ void freeEmulator(Emulator* emu) {
|
||||
freeMemory(emu->mem);
|
||||
free(emu);
|
||||
}
|
||||
|
||||
void installDevice(Emulator* emu, Device* device, uint8_t deviceNumber) {
|
||||
assert(deviceNumber < MAX_DEVICES);
|
||||
emu->devices[deviceNumber] = device;
|
||||
}
|
||||
|
||||
26
src/emu.h
26
src/emu.h
@@ -4,25 +4,47 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include "memory.h"
|
||||
#include "device.h"
|
||||
|
||||
#define FLAG_ZERO (1 << 0)
|
||||
#define FLAG_NEGATIVE (1 << 1)
|
||||
#define FLAG_OVERFLOW (1 << 2)
|
||||
|
||||
#define MAX_DEVICES 64
|
||||
|
||||
// -- TYPES -- //
|
||||
typedef struct {
|
||||
uint16_t regs[REG_MAX];
|
||||
Memory* mem;
|
||||
Device* devices[MAX_DEVICES];
|
||||
|
||||
uint32_t pc;
|
||||
uint32_t ihp;
|
||||
uint32_t iht;
|
||||
uint32_t sp;
|
||||
|
||||
uint16_t irq;
|
||||
bool halted;
|
||||
} Emulator;
|
||||
|
||||
typedef struct {
|
||||
uint32_t interruptHandlers[32];
|
||||
} __attribute__((packed)) InterruptHandlerTable;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint16_t zero : 1;
|
||||
uint16_t overflow : 1;
|
||||
uint16_t negative : 1;
|
||||
uint16_t interrupts : 1;
|
||||
} fields;
|
||||
|
||||
uint16_t raw;
|
||||
} FlagsRegister;
|
||||
|
||||
// -- FUNCTIONS -- //
|
||||
Emulator* initEmulator(uint32_t memorySize);
|
||||
void freeEmulator(Emulator* emu);
|
||||
void startEmulator(Emulator* emu);
|
||||
void startEmulator(Emulator* emu);
|
||||
void installDevice(Emulator* emu, Device* device, uint8_t deviceNumber);
|
||||
@@ -1,22 +1,22 @@
|
||||
#include "instruction.h"
|
||||
|
||||
const OperandType INSTRUCTION_OPERAND_TYPES[][3] = {
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_IMM16, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_REG},
|
||||
{OP_REG, OP_REG, OP_REG},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_REG},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_NONE},
|
||||
{OP_NONE, OP_NONE, OP_NONE},
|
||||
{OP_REG, OP_REG, OP_REG},
|
||||
{OP_NONE, OP_NONE, OP_NONE}
|
||||
{OP_REG, OP_REG, OP_NONE}, // MOV
|
||||
{OP_REG, OP_IMM16, OP_NONE}, // MVI
|
||||
{OP_REG, OP_REG, OP_NONE}, // ADD
|
||||
{OP_REG, OP_REG, OP_NONE}, // SUB
|
||||
{OP_REG, OP_REG, OP_REG}, // LOAD
|
||||
{OP_REG, OP_REG, OP_REG}, // STORE
|
||||
{OP_REG, OP_REG, OP_NONE}, // AND
|
||||
{OP_REG, OP_REG, OP_NONE}, // OR
|
||||
{OP_REG, OP_REG, OP_NONE}, // XOR
|
||||
{OP_REG, OP_REG, OP_REG}, // SHF
|
||||
{OP_IMM16, OP_NONE, OP_NONE}, // JMP
|
||||
{OP_IMM16, OP_NONE, OP_NONE}, // JIZ
|
||||
{OP_IMM16, OP_NONE, OP_NONE},// CALL
|
||||
{OP_NONE, OP_NONE, OP_NONE}, // RET
|
||||
{OP_REG, OP_REG, OP_IMM4}, // PORT
|
||||
{OP_NONE, OP_NONE, OP_NONE} // HLT
|
||||
};
|
||||
|
||||
char* opcodeToCStr(InstructionOpcode opcode) {
|
||||
@@ -35,7 +35,7 @@ char* opcodeToCStr(InstructionOpcode opcode) {
|
||||
case INST_JIZ: return "JIZ";
|
||||
case INST_CALL: return "CALL";
|
||||
case INST_RET: return "RET";
|
||||
case INST_SYS: return "SYS";
|
||||
case INST_PORT: return "PORT";
|
||||
case INST_HLT: return "HLT";
|
||||
default: return "FIXME";
|
||||
}
|
||||
@@ -81,6 +81,14 @@ char* instructionToCStr(Instruction inst) {
|
||||
registerToCStr(inst.operands[opIdx])
|
||||
);
|
||||
break;
|
||||
case OP_IMM4:
|
||||
written += snprintf(
|
||||
buffer + written,
|
||||
remaining,
|
||||
"%d",
|
||||
inst.operands[opIdx]
|
||||
);
|
||||
break;
|
||||
case OP_IMM16:
|
||||
written += snprintf(
|
||||
buffer + written,
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
|
||||
// -- TYPES -- //
|
||||
typedef enum : uint8_t {
|
||||
INST_MOV, INST_MVI, INST_ADD, INST_SUB, INST_LOAD, INST_STORE, INST_AND, INST_OR,
|
||||
INST_XOR, INST_SHF, INST_JMP, INST_JIZ, INST_CALL, INST_RET, INST_SYS, INST_HLT
|
||||
INST_MOV, INST_MVI, INST_ADD, INST_SUB, INST_LOAD, INST_STORE, INST_AND, INST_OR,
|
||||
INST_XOR, INST_SHF, INST_JMP, INST_JIZ, INST_CALL, INST_RET, INST_PORT, INST_HLT
|
||||
} InstructionOpcode;
|
||||
|
||||
typedef enum {
|
||||
typedef enum : uint8_t {
|
||||
// General purpose registers
|
||||
REG_A, REG_B, REG_C,
|
||||
|
||||
@@ -28,17 +28,19 @@ typedef enum {
|
||||
// automatically know the length of the registers array
|
||||
} Register;
|
||||
|
||||
#define REG_IHT_OFFSET 8 // offset of interrupt handler table register
|
||||
|
||||
typedef uint16_t SerializedInst;
|
||||
|
||||
typedef struct {
|
||||
InstructionOpcode opcode;
|
||||
uint8_t operands[3];
|
||||
Register operands[3];
|
||||
uint16_t immediate; // unused for almost all instructions
|
||||
bool hasImm16;
|
||||
} Instruction;
|
||||
|
||||
typedef enum {
|
||||
OP_NONE, OP_REG, OP_IMM16
|
||||
OP_NONE, OP_REG, OP_IMM4, OP_IMM16
|
||||
} OperandType;
|
||||
|
||||
// -- CONSTANTS -- //
|
||||
|
||||
55
src/main.c
55
src/main.c
@@ -2,27 +2,56 @@
|
||||
#include "emu.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);
|
||||
if (!emu) {
|
||||
fprintf(stderr, "emu: error: failed to allocate memory for emulator\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
RomFile* romFile = readProgramFromFile("../test.bin");
|
||||
|
||||
/*Instruction program[] = {
|
||||
{INST_MVI, {REG_A, 0, 0}, 5, true},
|
||||
{INST_MVI, {REG_B, 0, 0}, 3, true},
|
||||
{INST_ADD, {REG_A, REG_B, 0}, 0, false},
|
||||
/*
|
||||
Instruction program[] = {
|
||||
{INST_MVI, {REG_A}, 0, true},
|
||||
{INST_MVI, {REG_B}, 123, true},
|
||||
{INST_PORT, {REG_A, REG_B, 0}, 0, false},
|
||||
{INST_PORT, {REG_A, REG_B, 1}, 0, false},
|
||||
{INST_HLT, {}, 0, false}
|
||||
};*/
|
||||
printf("%d\n", romFile->header.numInstructions);
|
||||
memoryLoadProgram(emu->mem, romFile->instructions, romFile->header.numInstructions);
|
||||
|
||||
};
|
||||
|
||||
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]));
|
||||
*/
|
||||
|
||||
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);
|
||||
|
||||
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]);
|
||||
|
||||
//startEmulator(emu);
|
||||
freeEmulator(emu);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ Instruction decodeInstruction(Memory* m, uint32_t* pc) {
|
||||
uint8_t offset = 0;
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
switch (opTypes[i]) {
|
||||
case OP_IMM4:
|
||||
case OP_REG: {
|
||||
// get the data at the current offset, and grab the 4 bits that are there with a bitwise
|
||||
// "and" operator
|
||||
@@ -82,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
|
||||
(*pc) += sizeof(SerializedInst);
|
||||
|
||||
uint16_t nextDataOver = m->data[*pc];
|
||||
uint16_t nextDataOver;
|
||||
memcpy(&nextDataOver, &m->data[*pc], sizeof(uint16_t));
|
||||
outputInst.immediate = nextDataOver;
|
||||
outputInst.hasImm16 = true;
|
||||
break;
|
||||
@@ -108,4 +110,4 @@ void freeMemory(Memory* m) {
|
||||
|
||||
inline uint32_t getAddress(uint16_t low, uint16_t high) {
|
||||
return (high << 16) | low;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ RomFile* readProgramFromFile(char* path) {
|
||||
fclose(f);
|
||||
|
||||
ROMFileHeader* header = (ROMFileHeader*)buffer;
|
||||
Instruction* instructions = calloc(header->numInstructions, sizeof(SerializedInst));
|
||||
SerializedInst* instructions = calloc(header->numInstructions, sizeof(SerializedInst));
|
||||
if (!instructions) {
|
||||
free(buffer);
|
||||
return NULL;
|
||||
|
||||
@@ -13,7 +13,7 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
ROMFileHeader header;
|
||||
Instruction* instructions;
|
||||
SerializedInst* instructions;
|
||||
} RomFile;
|
||||
|
||||
// -- FUNCTIONS -- //
|
||||
|
||||
@@ -3,3 +3,4 @@ mvi b 2
|
||||
|
||||
add a b
|
||||
hlt
|
||||
hlt
|
||||
|
||||
3
tests/port.asm
Normal file
3
tests/port.asm
Normal file
@@ -0,0 +1,3 @@
|
||||
mvi a 1
|
||||
port a 123 out
|
||||
hlt
|
||||
Reference in New Issue
Block a user