X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=hw_lem1802.c;h=f17ebc73287a2eff7eaa083f37fb400e6bf3548c;hb=8b552fe61db48874f043bccfb589e5444509444c;hp=1be1810fa933720c3da6436b04be742d1633248e;hpb=a9cc197dc14d237e6bcea74591b81688de81ad66;p=dcpu16 diff --git a/hw_lem1802.c b/hw_lem1802.c index 1be1810..f17ebc7 100644 --- a/hw_lem1802.c +++ b/hw_lem1802.c @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef HAVE_LIBPNG #include @@ -23,21 +24,15 @@ #define TRACE(...) do {} while (0) #endif /* DEBUG */ -static dcpu16_hw_signal_t lem1802_reset_; -static dcpu16_hw_signal_t lem1802_cycle_; -static dcpu16_hw_signal_t lem1802_hwi_; -static struct dcpu16_hw hw_ = { - .name_ = "LEM1802 - Low Energy Monitor", - .id_l = 0xf615, - .id_h = 0x7349, - .ver = 0x1802, - .mfg_l = 0x8b36, - .mfg_h = 0x1c6c, - .hwi = lem1802_hwi_, - .cycle = lem1802_cycle_, - .reset = lem1802_reset_, - .data = (struct lem1802_ *)NULL -}; +#ifdef WANT_VARIADIC_VOIDP_CAST +#define VOIDP(__x__) ((void *)(__x__)) +#define VOIDFP(__x__) ((void *)(size_t)(__x__)) +#else +#define VOIDP(__x__) (__x__) +#define VOIDFP(__x__) (__x__) +#endif + +#define LEM1802_POWER_ON_CYCLES 100000 /* this should vary by, let us say, 10% */ #define PIX_X 160 /* pixels in display */ #define PIX_Y 128 /* pixels in display */ @@ -75,7 +70,8 @@ struct pixel_ { }; struct lem1802_ { - long long cycle_activated; /* for tracking 'turn on delay' */ + long long cycle_activated; /* running since */ + long long cycles_until_active_; /* for tracking power-up delay */ DCPU16_WORD video_base; DCPU16_WORD font_base; @@ -90,10 +86,32 @@ struct lem1802_ { unsigned int blink_tally_; /* tick */ unsigned int blink_state; + enum cycle_state_ { + CYCLE_IDLE, + CYCLE_COPY_TO_RAM, + } cycle_state_; + const DCPU16_WORD *cycle_state_copy_src_ptr_; + DCPU16_WORD cycle_state_copy_dst_addr_; + size_t cycle_state_copy_words_; + int (*render)(void *, struct pixel_ *, size_t, size_t); void *renderer_data; }; +static +long long power_on_cycles_(void) { + struct tv; + long long r = 0; + +#if WANT_DELAY_START + gettimeofday(&tv, NULL); + r += LEM1802_POWER_ON_CYCLES - (LEM1802_POWER_ON_CYCLES / 10); + r += tv.tv_usec % (LEM1802_POWER_ON_CYCLES / 5); +#endif + + return r; +} + static inline void pixel_color_(struct pixel_ *pix, DCPU16_WORD color) { unsigned char x; @@ -206,8 +224,14 @@ void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) { TRACE("%s>> video_base:0x%04x", __func__, display->video_base); #endif + if (display->cycles_until_active_) { + /* show cute power-up sequence.. */ + memset(display->pixbuf, 0, PIX_X * PIX_Y * sizeof *display->pixbuf); + return; + } + if (display->video_base == 0) { - /* disconnected, blank display. static might be fun, too */ + /* disconnected, blank display */ memset(display->pixbuf, 0, PIX_X * PIX_Y * sizeof *display->pixbuf); return; } @@ -220,8 +244,8 @@ void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) { mem, display->video_base, display->video_base + tile, - display->palette_base ? mem + display->palette_base : palette_default_, - display->font_base ? mem + display->font_base : (unsigned short *)chargen_4x8_glyphs, + display->palette_base ? mem + display->palette_base : (DCPU16_WORD *)palette_default_, + display->font_base ? mem + display->font_base : (DCPU16_WORD *)chargen_4x8_glyphs, display->blink_state); } } @@ -307,8 +331,8 @@ rfbScreenInfoPtr lem1802_rfb_new(int argc, char *argv[]) { s->httpDir = "../classes"; #endif - TRACE("%s>> s:%p", __func__, s); - TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__, s->kbdAddEvent, s->frameBuffer); + TRACE("%s>> s:%p", __func__, VOIDP(s)); + TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__, VOIDFP(s->kbdAddEvent), s->frameBuffer); return s; } @@ -320,7 +344,7 @@ void lem1802_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr s) { s->desktopName = "NYA ELEKTRISKA LEM1802"; s->frameBuffer = (char *)display->pixbuf; - TRACE("%s>> s:%p\n", __func__, s); + TRACE("%s>> s:%p\n", __func__, VOIDP(s)); } /* notify rfb server that pixels may have changed */ @@ -332,7 +356,8 @@ int pixbuf_render_vnc_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) { (void)pixbuf; /* derp */ - rfbMarkRectAsModified(s, 0, 0, x, y); + if (s) + rfbMarkRectAsModified(s, 0, 0, x, y); TRACE("%s>>", __func__); @@ -342,8 +367,8 @@ int pixbuf_render_vnc_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) { static -void lem1802_reset_(struct dcpu16 *vm, void *data) { - struct lem1802_ *display = (struct lem1802_ *)data; +void lem1802_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; (void)vm; @@ -359,21 +384,21 @@ void lem1802_reset_(struct dcpu16 *vm, void *data) { display->blink_tally_ = 0; display->blink_state = 0; + display->cycle_state_ = 0; + #if DEBUG vm->trace_cb_("%s>>", __func__); #endif /* DEBUG */ } static -void lem1802_cycle_(struct dcpu16 *vm, void *data) { - struct lem1802_ *display = (struct lem1802_ *)data; +void lem1802_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; (void)vm; /* - maybe just step through video memory (if set) - one word per clock..? could just cheat and - use accounting callbacks.. - + for more cycle-accuracy, could step through video memory, if set, + one word per clock.. for now just count cycles and issue a full refresh/render every so often */ @@ -393,21 +418,48 @@ void lem1802_cycle_(struct dcpu16 *vm, void *data) { lem1802_pixbuf_refresh_full_(display, vm->ram); display->render(display->renderer_data, display->pixbuf, PIX_X, PIX_Y); } + + switch (display->cycle_state_) { + case CYCLE_IDLE: + break; + + case CYCLE_COPY_TO_RAM: + TRACE("%s>> copy_to_ram words:%zu src:%p dst_addr:0x%04x", + __func__, + display->cycle_state_copy_words_, + VOIDP(display->cycle_state_copy_src_ptr_), + display->cycle_state_copy_dst_addr_); + vm->ram[display->cycle_state_copy_dst_addr_] = *display->cycle_state_copy_src_ptr_; + display->cycle_state_copy_dst_addr_++; + display->cycle_state_copy_src_ptr_++; + display->cycle_state_copy_words_--; + if (display->cycle_state_copy_words_ == 0) { + display->cycle_state_ = CYCLE_IDLE; + } + break; + } + + if (display->cycles_until_active_) { + display->cycles_until_active_--; + if (display->cycles_until_active_ == 0) { + TRACE("%s>> display now active", __func__); + } + } } static -void lem1802_hwi_(struct dcpu16 *vm, void *data) { - struct lem1802_ *display = (struct lem1802_ *)data; +void lem1802_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A]; DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B]; - size_t i; TRACE("%s>> A:0x%04x B:0x%04x", __func__, reg_a, reg_b); switch (reg_a) { case 0: /* MEM_MAP_SCREEN */ if (display->cycle_activated == 0 && reg_b) { - display->cycle_activated = vm->cycle; + display->cycle_activated = vm->cycle_; + display->cycles_until_active_ = power_on_cycles_(); } display->video_base = reg_b; if (reg_b == 0) @@ -427,23 +479,19 @@ void lem1802_hwi_(struct dcpu16 *vm, void *data) { break; case 4: /* MEM_DUMP_FONT */ - for (i = 0; i < 128 ; i++) { - vm->ram[reg_b] = chargen_4x8_glyphs[reg_b][0] << 8; - vm->ram[reg_b] |= chargen_4x8_glyphs[reg_b][1]; - reg_b += 1; - vm->ram[reg_b] = chargen_4x8_glyphs[reg_b][2] << 8; - vm->ram[reg_b] |= chargen_4x8_glyphs[reg_b][3]; - reg_b += 1; - } - vm->cycle += 256; + display->cycle_state_copy_src_ptr_ = (DCPU16_WORD *)chargen_4x8_glyphs; + display->cycle_state_copy_dst_addr_ = reg_b; + display->cycle_state_copy_words_ = 256; + display->cycle_state_ = CYCLE_COPY_TO_RAM; + dcpu16_cycle_inc(vm, 256); break; case 5: /* MEM_DUMP_PALETTE */ - for (i = 0; i < 16; i++) { - vm->ram[reg_b] = palette_default_[i]; - reg_b += 1; - } - vm->cycle += 16; + display->cycle_state_copy_src_ptr_ = palette_default_; + display->cycle_state_copy_dst_addr_ = reg_b; + display->cycle_state_copy_words_ = 16; + display->cycle_state_ = CYCLE_COPY_TO_RAM; + dcpu16_cycle_inc(vm, 16); break; } } @@ -498,54 +546,63 @@ char *lem1802_renderers_iter(void **iterp, char **name, char **args) { return (*r)->name; } -/* instantitate a new display */ -struct dcpu16_hw *lem1802_new(struct dcpu16 *vm) { - struct dcpu16_hw *hw; +int lem1802_data_init_(struct dcpu16_hw *hw, void *data) { + (void)data; - 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(struct dcpu16_hw)); + hw->data = calloc(1, sizeof(struct lem1802_)); 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; } - ((struct lem1802_ *)(hw->data))->pixbuf = calloc(1, PIX_X * PIX_Y * sizeof *((struct lem1802_ *)(hw->data))->pixbuf); + ((struct lem1802_ *)(hw->data))->pixbuf = calloc(PIX_X * PIX_Y, sizeof *((struct lem1802_ *)(hw->data))->pixbuf); if (((struct lem1802_ *)(hw->data))->pixbuf == NULL) { - vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno)); free(hw->data); - free(hw); - return NULL; + hw->data = NULL; + return -1; } ((struct lem1802_ *)(hw->data))->refresh_rate = 1666; ((struct lem1802_ *)(hw->data))->blink_rate = 75000; - hw->vm = vm; - - return hw; + return 0; } -void lem1802_del(struct dcpu16_hw **hw) { +void lem1802_data_free_(struct dcpu16_hw *hw) { if (hw) { - if (*hw) { - if ((*hw)->data) { - if (((struct lem1802_ *)(*hw)->data)->pixbuf) { - free(((struct lem1802_ *)(*hw)->data)->pixbuf); - ((struct lem1802_ *)(*hw)->data)->pixbuf = NULL; - } - free((*hw)->data); - (*hw)->data = NULL; + if (hw->data) { + /* FIXME: free renderer data */ + hw->vm->warn_cb_("FIXME"); + + if (((struct lem1802_ *)(hw->data))->pixbuf) { + free(((struct lem1802_ *)(hw->data))->pixbuf); + ((struct lem1802_ *)(hw->data))->pixbuf = NULL; } - free(*hw); + + free(hw->data); + hw->data = NULL; } - *hw = NULL; } } + + +static struct dcpu16_hw hw_ = { + .name_ = "LEM1802 - Low Energy Monitor", + .id_l = 0xf615, + .id_h = 0x7349, + .ver = 0x1802, + .mfg_l = 0x8b36, + .mfg_h = 0x1c6c, + .hwi = lem1802_hwi_, + .cycle = lem1802_cycle_, + .reset = lem1802_reset_, + .data = (struct lem1802_ *)NULL +}; + +struct dcpu16_hw_module dcpu16_hw_module_lem1802 = { + .template = &hw_, + .data_init = lem1802_data_init_, + .data_free = lem1802_data_free_, +}; +