From 57241bb9e6f6b1acb019efe4f32eb758cf9e93d7 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Thu, 17 May 2012 16:06:59 -0700 Subject: [PATCH] initial api changes to support hw_ devices as more-generic attachable modules generalizing hw handling routines --- dcpu16.c | 35 +++++++++++++++++++++++++++++++++++ dcpu16.h | 10 ++++++++++ hw_spc2000.c | 31 +++++++++++++++++++++++++++++++ hw_spc2000.h | 3 +-- 4 files changed, 77 insertions(+), 2 deletions(-) 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 */ diff --git a/dcpu16.h b/dcpu16.h index 38203ed..ebf2072 100644 --- a/dcpu16.h +++ b/dcpu16.h @@ -68,6 +68,7 @@ struct dcpu16_acct_cb { }; typedef void (dcpu16_hw_signal_t)(struct dcpu16 *, void *); +typedef void (dcpu16_hw_data_free_t)(struct dcpu16_hw *); /* these are used to define hardware attached to the system */ struct dcpu16_hw { struct dcpu16 *vm; /* which system do I belong to */ @@ -82,9 +83,18 @@ struct dcpu16_hw { dcpu16_hw_signal_t *hwi; dcpu16_hw_signal_t *cycle; dcpu16_hw_signal_t *reset; + + dcpu16_hw_data_free_t *data_free; void *data; }; +typedef int (dcpu16_hw_data_init_t)(struct dcpu16_hw *, void *); +struct dcpu16_hw_module { + struct dcpu16_hw *template; + dcpu16_hw_data_init_t *data_init; + dcpu16_hw_data_free_t *data_free; +}; + /* instantiate a new core */ struct dcpu16 *dcpu16_new(void); diff --git a/hw_spc2000.c b/hw_spc2000.c index 49ba350..402a1d7 100644 --- a/hw_spc2000.c +++ b/hw_spc2000.c @@ -9,6 +9,7 @@ static dcpu16_hw_signal_t spc2000_reset_; static dcpu16_hw_signal_t spc2000_cycle_; static dcpu16_hw_signal_t spc2000_hwi_; static struct dcpu16_hw hw_ = { + .vm = NULL, .name_ = "SPC2000 - Suspension Chamber 2000", .id_l = 0x1d9d, .id_h = 0x40e4, @@ -21,11 +22,41 @@ static struct dcpu16_hw hw_ = { .data = (struct spc2000_ *)NULL }; +static dcpu16_hw_data_init_t spc2000_data_init_; +static dcpu16_hw_data_free_t spc2000_data_free_; +struct dcpu16_hw_module dcpu16_hw_module_spc2000 = { + .template = &hw_, + .data_init = spc2000_data_init_, + .data_free = spc2000_data_free_, +}; + struct spc2000_ { DCPU16_WORD skip_unit; long long skip; }; +static +int spc2000_data_init_(struct dcpu16_hw *hw, void *extra) { + (void)extra; + + hw->data = calloc(1, sizeof(struct spc2000_)); + if (hw->data == NULL) { + hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + return -1; + } + return 0; +} + +static +void spc2000_data_free_(struct dcpu16_hw *hw) { + if (hw) { + if (hw->data) { + free(hw->data); + hw->data = NULL; + } + } +} + static void spc2000_reset_(struct dcpu16 *vm, void *data) { struct spc2000_ *spc2000 = (struct spc2000_ *)data; diff --git a/hw_spc2000.h b/hw_spc2000.h index 44cc015..2171ba3 100644 --- a/hw_spc2000.h +++ b/hw_spc2000.h @@ -1,7 +1,6 @@ #ifndef SPC2000_H_IE4EG0MO #define SPC2000_H_IE4EG0MO -struct dcpu16_hw *spc2000_new(struct dcpu16 *); -void spc2000_del(struct dcpu16_hw **); +extern struct dcpu16_hw_module dcpu16_hw_module_spc2000; #endif /* SPC2000_H_IE4EG0MO */ -- 2.43.2