X-Git-Url: http://git.squeep.com/?p=dcpu16;a=blobdiff_plain;f=dcpu16.c;fp=dcpu16.c;h=6ab689888d70a28587f07a47560adf1130d254cd;hp=ac052d59887a148f62f5195db9ac8c2c08557063;hb=57241bb9e6f6b1acb019efe4f32eb758cf9e93d7;hpb=9e40f875436d85767042714823f47fdf136ba9b0 diff --git a/dcpu16.c b/dcpu16.c index ac052d5..6ab6898 100644 --- a/dcpu16.c +++ b/dcpu16.c @@ -1358,6 +1358,41 @@ void dcpu16_dump_ram(struct dcpu16 *vm, DCPU16_WORD start, DCPU16_WORD end) { printf("\n"); } +/* instantiate a new 'hardware' device */ +struct dcpu16_hw *dcpu16_hw_new(struct dcpu16 *vm, struct dcpu16_hw_module *mod, void *data) { + struct dcpu16_hw *hw; + + hw = calloc(1, sizeof *hw); + if (hw == NULL) { + vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + return NULL; + } + memcpy(hw, mod->template, sizeof *hw); + hw->vm = vm; + + if (mod->data_init(hw, data)) { + vm->warn_cb_("failed to init hw module data"); + free(hw); + return NULL; + } + hw->data_free = mod->data_free; + + return hw; +} + +/* destroy a 'hardware' device */ +void dcpu16_hw_del(struct dcpu16_hw **hw) { + if (hw) { + if (*hw) { + if ((*hw)->data_free) { + (*hw)->data_free(*hw); + } + free(*hw); + *hw = NULL; + } + } +} + /* dcpu16_hw_add * registers new 'hardware' device with system */