Files
MyLittleCPU/src/device.c
2026-07-16 14:05:01 +10:00

38 lines
713 B
C

#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;
}