X-Git-Url: http://git.squeep.com/?p=dcpu16;a=blobdiff_plain;f=hw_keyboard.c;fp=hw_keyboard.c;h=428971d5a6a6ecfcb51c0e19e88d3ed024c23ab6;hp=90c30b3809590f9882a91da23f67a0af17d476c8;hb=8b552fe61db48874f043bccfb589e5444509444c;hpb=57241bb9e6f6b1acb019efe4f32eb758cf9e93d7 diff --git a/hw_keyboard.c b/hw_keyboard.c index 90c30b3..428971d 100644 --- a/hw_keyboard.c +++ b/hw_keyboard.c @@ -18,23 +18,6 @@ #define VOIDP(__x__) (__x__) #endif -static dcpu16_hw_signal_t keyboard_reset_; -static dcpu16_hw_signal_t keyboard_cycle_; -static dcpu16_hw_signal_t keyboard_hwi_; -static struct dcpu16_hw hw_ = { - .vm = NULL, - .name_ = "Generic Keyboard (compatible)", - .id_l = 0x7406, - .id_h = 0x30cf, - .ver = 0x0001, - .mfg_l = 0x0000, - .mfg_h = 0x0000, - .hwi = keyboard_hwi_, - .cycle = keyboard_cycle_, - .reset = keyboard_reset_, - .data = (struct keyboard_ *)NULL -}; - struct keyboard_ { char *buf; size_t buf_sz; @@ -123,8 +106,8 @@ void keyboard_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr rfbScreen) { #endif /* HAVE_LIBVNCSERVER */ static -void keyboard_reset_(struct dcpu16 *vm, void *data) { - struct keyboard_ *keyboard = (struct keyboard_ *)data; +void keyboard_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) { + struct keyboard_ *keyboard = (struct keyboard_ *)hw->data; (void)vm; @@ -133,15 +116,15 @@ void keyboard_reset_(struct dcpu16 *vm, void *data) { } static -void keyboard_cycle_(struct dcpu16 *vm, void *data) { - struct keyboard_ *keyboard = (struct keyboard_ *)data; +void keyboard_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) { + struct keyboard_ *keyboard = (struct keyboard_ *)hw->data; (void)vm, (void)keyboard; } static -void keyboard_hwi_(struct dcpu16 *vm, void *data) { - struct keyboard_ *keyboard = (struct keyboard_ *)data; +void keyboard_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { + struct keyboard_ *keyboard = (struct keyboard_ *)hw->data; DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A]; DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B]; @@ -171,50 +154,59 @@ void keyboard_hwi_(struct dcpu16 *vm, void *data) { } } -struct dcpu16_hw *keyboard_new(struct dcpu16 *vm) { - struct dcpu16_hw *hw; - char *b; +static +int keyboard_data_init_(struct dcpu16_hw *hw, void *data) { + size_t buf_sz = data ? *(size_t *)data : BUF_SZ; - hw = calloc(1, sizeof *hw); - if (hw == NULL) { - vm->warn_cb_("%s():%s", "calloc", strerror(errno)); - return NULL; - } - memcpy(hw, &hw_, sizeof *hw); - hw->data = calloc(1, sizeof hw->data); + hw->data = calloc(1, sizeof(struct keyboard_)); if (hw->data == NULL) { - vm->warn_cb_("%s():%s", "calloc", strerror(errno)); - free(hw); - return NULL; + hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + return -1; } - b = calloc(BUF_SZ, sizeof *b); - if (b == NULL) { - vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + ((struct keyboard_ *)(hw->data))->buf = malloc(buf_sz * sizeof *((struct keyboard_ *)(hw->data))->buf); + if (((struct keyboard_ *)(hw->data))->buf == NULL) { + hw->vm->warn_cb_("%s():%s", "malloc", strerror(errno)); free(hw->data); - free(hw); - return NULL; + hw->data = NULL; + return -1; } - ((struct keyboard_ *)(hw->data))->buf = b; - ((struct keyboard_ *)(hw->data))->buf_sz = BUF_SZ; - hw->vm = vm; + ((struct keyboard_ *)(hw->data))->buf_sz = buf_sz; - return hw; + return 0; } -void keyboard_del(struct dcpu16_hw **hw) { +static +void keyboard_data_free_(struct dcpu16_hw *hw) { if (hw) { - if (*hw) { - if ((*hw)->data) { - free(((struct keyboard_ *)((*hw)->data))->buf); - ((struct keyboard_ *)((*hw)->data))->buf = NULL; - - free((*hw)->data); - (*hw)->data = NULL; + if (hw->data) { + if (((struct keyboard_ *)(hw->data))->buf) { + free(((struct keyboard_ *)(hw->data))->buf); + ((struct keyboard_ *)(hw->data))->buf = NULL; } - free(*hw); - *hw = NULL; + free(hw->data); + hw->data = NULL; } } } + +static struct dcpu16_hw hw_ = { + .vm = NULL, + .name_ = "Generic Keyboard (compatible)", + .id_l = 0x7406, + .id_h = 0x30cf, + .ver = 0x0001, + .mfg_l = 0x0000, + .mfg_h = 0x0000, + .hwi = keyboard_hwi_, + .cycle = keyboard_cycle_, + .reset = keyboard_reset_, + .data = (struct keyboard_ *)NULL +}; + +struct dcpu16_hw_module dcpu16_hw_module_keyboard = { + .template = &hw_, + .data_init = keyboard_data_init_, + .data_free = keyboard_data_free_, +};