devices working

This commit is contained in:
2026-07-16 14:05:01 +10:00
parent 42fab467f1
commit fb3efc77c3
7 changed files with 123 additions and 25 deletions

View File

@@ -1,9 +1,7 @@
#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) {
@@ -16,6 +14,17 @@ static inline void updateFlagsRegister(Emulator* emu, int32_t value) {
}
}
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);
}
}
}
void startEmulator(Emulator* emu) {
static void* dispatchTable[] = {
&&MOV,
@@ -32,15 +41,16 @@ 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() updateDevices(emu); \
inst = fetchInst(emu); \
goto *dispatchTable[inst.opcode]
DISPATCH();
@@ -133,8 +143,39 @@ void startEmulator(Emulator* emu) {
emu->pc = ((uint32_t*)emu->mem->data)[++emu->sp];
DISPATCH();
}
SYS: {
// TODO
PORT: {
uint16_t port = emu->regs[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 = emu->regs[inst.operands[1]];
if (device->inPos == DEVICE_BUFFER_LENGTH) {
DISPATCH();
}
device->in[device->inPos] = value;
device->inPos++;
} else {
if (device->outPos == 0) {
emu->regs[inst.operands[1]] = 0;
DISPATCH();
}
emu->regs[inst.operands[1]] = device->out[device->outPos-1];
if (device->outPos > 0)
device->outPos--;
}
DISPATCH();
}
HLT: {
@@ -170,3 +211,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;
}