X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=hw_lem1802.c;h=502954187eeae0b562327f9ffb72e936f5aa8b6e;hb=a4194ad128ad2b396f3b483da1b48a7a9dedda2f;hp=f17ebc73287a2eff7eaa083f37fb400e6bf3548c;hpb=8b552fe61db48874f043bccfb589e5444509444c;p=dcpu16 diff --git a/hw_lem1802.c b/hw_lem1802.c index f17ebc7..5029541 100644 --- a/hw_lem1802.c +++ b/hw_lem1802.c @@ -18,10 +18,19 @@ #include "chargen-4x8.h" #include "hw_lem1802.h" +/* lem1802 + * + * TODO: + * multiple vnc displays + */ + +#define MSG_(__level__, __vm__, ...) do { ((__vm__) ? ((struct dcpu16 *)(__vm__))->msg_cb_ : dcpu16_msg_)(__level__, __VA_ARGS__); } while (0) +#define MSG_INFO(__vm__, ...) MSG_(DCPU16_MSG_INFO, __vm__, __VA_ARGS__) +#define MSG_ERROR(__vm__, ...) MSG_(DCPU16_MSG_ERROR, __vm__, __VA_ARGS__) #ifdef DEBUG -#define TRACE(...) do { printf("[debug] "); printf(__VA_ARGS__); printf("\n"); } while (0) +#define MSG_DEBUG(__vm__, ...) MSG_(DCPU16_MSG_DEBUG, __vm__, __VA_ARGS__) #else /* DEBUG */ -#define TRACE(...) do {} while (0) +#define MSG_DEBUG(__vm__, ...) do { } while (0) #endif /* DEBUG */ #ifdef WANT_VARIADIC_VOIDP_CAST @@ -32,7 +41,7 @@ #define VOIDFP(__x__) (__x__) #endif -#define LEM1802_POWER_ON_CYCLES 100000 /* this should vary by, let us say, 10% */ +#define LEM1802_POWER_ON_CYCLES 100000 /* 'about one second' */ #define PIX_X 160 /* pixels in display */ #define PIX_Y 128 /* pixels in display */ @@ -66,7 +75,7 @@ struct pixel_ { unsigned char r; unsigned char g; unsigned char b; - unsigned char a; + unsigned char a; /* unused */ }; struct lem1802_ { @@ -105,6 +114,7 @@ long long power_on_cycles_(void) { #if WANT_DELAY_START gettimeofday(&tv, NULL); + /* consider the 'about' in 'about one second' to be +/- 10% */ r += LEM1802_POWER_ON_CYCLES - (LEM1802_POWER_ON_CYCLES / 10); r += tv.tv_usec % (LEM1802_POWER_ON_CYCLES / 5); #endif @@ -133,7 +143,7 @@ static void pixbuf_border_paint_(struct pixel_ *pixbuf, struct pixel_ *border) { size_t x, y, i; - TRACE("%s>> painted border", __func__); + MSG_DEBUG(NULL, "%s>> painting border", __func__); /* top */ for (y = 0; y < PIX_BORDER; y++) { @@ -160,7 +170,7 @@ void font_tile_paint_(struct pixel_ *p, struct pixel_ *fg, struct pixel_ *bg, DC unsigned char *font_bitmap = (unsigned char *)tile; #if 0 - TRACE("%s>> fg:(%u,%u,%u) bg:(%u,%u,%u) font_bitmap:%02x %02x %02x %02x", __func__, + MSG_DEBUG(NULL, "%s>> fg:(%u,%u,%u) bg:(%u,%u,%u) font_bitmap:%02x %02x %02x %02x", __func__, fg->r, fg->g, fg->b, bg->r, bg->g, bg->b, font_bitmap[0], font_bitmap[1], font_bitmap[2], font_bitmap[3]); @@ -189,7 +199,7 @@ void pixbuf_addr_paint_(struct pixel_ *pixbuf, DCPU16_WORD *mem, DCPU16_WORD bas cell_y = (addr - base) / CELL_X; #if 0 - TRACE("%s>> addr:0x%04x col:%u row:%u v:%hu", + MSG_DEBUG(NULL, "%s>> addr:0x%04x col:%u row:%u v:%hu", __func__, addr, cell_x, cell_y, mem[addr]); @@ -221,7 +231,7 @@ void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) { size_t tile; #if 0 - TRACE("%s>> video_base:0x%04x", __func__, display->video_base); + MSG_DEBUG(NULL, "%s>> video_base:0x%04x", __func__, display->video_base); #endif if (display->cycles_until_active_) { @@ -313,7 +323,8 @@ f_done: #ifdef HAVE_LIBVNCSERVER /* create and return a new screen, easiest to do here because we know the screen dimensions */ -rfbScreenInfoPtr lem1802_rfb_new(int argc, char *argv[]) { +static +rfbScreenInfoPtr lem1802_rfb_new_(int argc, char *argv[]) { rfbScreenInfoPtr s; int paddedWidth = PIX_X + ( (PIX_X & 3) ? (4 - (PIX_X & 3)) : 0 ); int height = PIX_Y; @@ -331,21 +342,12 @@ rfbScreenInfoPtr lem1802_rfb_new(int argc, char *argv[]) { s->httpDir = "../classes"; #endif - TRACE("%s>> s:%p", __func__, VOIDP(s)); - TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__, VOIDFP(s->kbdAddEvent), s->frameBuffer); + MSG_DEBUG(NULL, "%s>> s:%p", __func__, VOIDP(s)); + MSG_DEBUG(NULL, "%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__, VOIDFP(s->kbdAddEvent), s->frameBuffer); return s; } -/* set up a new screen to see our pixels */ -void lem1802_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr s) { - struct lem1802_ *display = (struct lem1802_ *)hw->data; - - s->desktopName = "NYA ELEKTRISKA LEM1802"; - s->frameBuffer = (char *)display->pixbuf; - - TRACE("%s>> s:%p\n", __func__, VOIDP(s)); -} /* notify rfb server that pixels may have changed */ static @@ -355,11 +357,13 @@ int pixbuf_render_vnc_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) { (void)pixbuf; + MSG_DEBUG(NULL, "%s>> s:%p", __func__, s); + /* derp */ if (s) rfbMarkRectAsModified(s, 0, 0, x, y); - TRACE("%s>>", __func__); + MSG_DEBUG(NULL, "%s>>", __func__); return retval; } @@ -386,9 +390,7 @@ void lem1802_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) { display->cycle_state_ = 0; -#if DEBUG - vm->trace_cb_("%s>>", __func__); -#endif /* DEBUG */ + MSG_DEBUG(vm, "%s>>", __func__); } static @@ -407,14 +409,14 @@ void lem1802_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) { if (display->blink_tally_ >= display->blink_rate) { display->blink_tally_ = 0; display->blink_state ^= 1; - TRACE("%s>> blink:%u (%u cycles)", __func__, display->blink_state, display->blink_rate); + MSG_DEBUG(vm, "%s>> blink:%u (%u cycles)", __func__, display->blink_state, display->blink_rate); } display->refresh_tally_++; if (display->refresh_tally_ >= display->refresh_rate) { display->refresh_tally_ = 0; if (display->render) - TRACE("%s>> refresh", __func__); + MSG_DEBUG(vm, "%s>> refresh", __func__); lem1802_pixbuf_refresh_full_(display, vm->ram); display->render(display->renderer_data, display->pixbuf, PIX_X, PIX_Y); } @@ -424,7 +426,7 @@ void lem1802_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) { break; case CYCLE_COPY_TO_RAM: - TRACE("%s>> copy_to_ram words:%zu src:%p dst_addr:0x%04x", + MSG_DEBUG(vm, "%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_), @@ -442,7 +444,7 @@ void lem1802_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) { if (display->cycles_until_active_) { display->cycles_until_active_--; if (display->cycles_until_active_ == 0) { - TRACE("%s>> display now active", __func__); + MSG_DEBUG(vm, "%s>> display now active", __func__); } } } @@ -453,7 +455,7 @@ void lem1802_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A]; DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B]; - TRACE("%s>> A:0x%04x B:0x%04x", __func__, reg_a, reg_b); + MSG_DEBUG(vm, "%s>> A:0x%04x B:0x%04x", __func__, reg_a, reg_b); switch (reg_a) { case 0: /* MEM_MAP_SCREEN */ @@ -464,18 +466,22 @@ void lem1802_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { display->video_base = reg_b; if (reg_b == 0) display->cycle_activated = 0; + MSG_DEBUG(vm, "%s>> video_base:0x%04x", __func__, reg_b); break; case 1: /* MEM_MAP_FONT */ display->font_base = reg_b; + MSG_DEBUG(vm, "%s>> font_base:0x%04x", __func__, reg_b); break; case 2: /* MEM_MAP_PALETTE */ display->palette_base = reg_b; + MSG_DEBUG(vm, "%s>> palette_base:0x%04x", __func__, reg_b); break; case 3: /* SET_BORDER_COLOR */ display->border_color = reg_b & 0x000f; + MSG_DEBUG(vm, "%s>> border_color_index:0x%01x", __func__, reg_b & 0x000f); break; case 4: /* MEM_DUMP_FONT */ @@ -483,6 +489,7 @@ void lem1802_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { display->cycle_state_copy_dst_addr_ = reg_b; display->cycle_state_copy_words_ = 256; display->cycle_state_ = CYCLE_COPY_TO_RAM; + MSG_DEBUG(vm, "%s>> copying default font into 0x%04x - 0x%04x", __func__, reg_b, reg_b + 256); dcpu16_cycle_inc(vm, 256); break; @@ -491,6 +498,7 @@ void lem1802_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { display->cycle_state_copy_dst_addr_ = reg_b; display->cycle_state_copy_words_ = 16; display->cycle_state_ = CYCLE_COPY_TO_RAM; + MSG_DEBUG(vm, "%s>> copying default palette into 0x%04x - 0x%04x", __func__, reg_b, reg_b + 16); dcpu16_cycle_inc(vm, 16); break; } @@ -512,22 +520,8 @@ static struct renderer_ { { NULL, NULL, NULL } }; -int lem1802_renderer_set(struct dcpu16_hw *hw, const char *renderer, void *data) { - struct renderer_ *r; - - for (r = lem1802_renderers_; r->renderer; r++) { - if (strcmp(renderer, r->name) == 0) { - ((struct lem1802_ *)(hw->data))->render = r->renderer; - ((struct lem1802_ *)(hw->data))->renderer_data = data; - TRACE("%s>> renderer set to %s", __func__, renderer); - return 0; - } - } - - return -1; -} - -char *lem1802_renderers_iter(void **iterp, char **name, char **args) { +static +char *lem1802_renderers_iter_(void **iterp, char **name, char **args) { struct renderer_ **r = (struct renderer_ **)iterp; if (*r == NULL) @@ -546,18 +540,19 @@ char *lem1802_renderers_iter(void **iterp, char **name, char **args) { return (*r)->name; } +static int lem1802_data_init_(struct dcpu16_hw *hw, void *data) { (void)data; hw->data = calloc(1, sizeof(struct lem1802_)); if (hw->data == NULL) { - hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + MSG_ERROR(hw->vm, "%s():%s", "calloc", strerror(errno)); return -1; } ((struct lem1802_ *)(hw->data))->pixbuf = calloc(PIX_X * PIX_Y, sizeof *((struct lem1802_ *)(hw->data))->pixbuf); if (((struct lem1802_ *)(hw->data))->pixbuf == NULL) { - hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + MSG_ERROR(hw->vm, "%s():%s", "calloc", strerror(errno)); free(hw->data); hw->data = NULL; return -1; @@ -569,26 +564,157 @@ int lem1802_data_init_(struct dcpu16_hw *hw, void *data) { return 0; } +static void lem1802_data_free_(struct dcpu16_hw *hw) { if (hw) { 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->data); hw->data = NULL; } } } +static struct dcpu16_hw_ctl_cmd ctl_[] = { + { "blink_rate", "const unsigned int *", "unsigned int *", "sets or gets cycles per blink toggle" }, + { "refresh_rate", "const unsigned int *", "unsigned int *", "sets or gets cycles per screen refresh" }, +#ifdef HAVE_LIBVNCSERVER + { "new_rfbScreen", "struct { int argc; char **argv;} *", "rfbScreenInfoPtr *", "allocates a new rfb screen" }, + { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this lem1802 instance with an rfb display" }, +#endif /* HAVE_LIBVNCSERVER */ + { "renderers_iter", "void **", "struct {char *name; char *args;} *", "returns the next renderer this module is capable of using" }, + { "renderer", "const char *", "NULL", "sets this lem1802 instance to use renderer" }, + { "renderer_data", "void *", "NULL", "sets renderer-specific data" }, + { NULL, NULL, NULL, NULL } +}; + +static +int lem1802_data_ctl_(struct dcpu16_hw *hw, const char *cmd, void *data_in, void *data_out) { + if (strcmp(cmd, "blink_rate") == 0) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; + const unsigned int *rate_in = (const unsigned int *)data_in; + unsigned int *rate_out = (unsigned int *)data_out; + + if (rate_out) { + *rate_out = display->blink_rate; + } + + if (rate_in) { + display->blink_rate = *rate_in; + } + + MSG_DEBUG(hw->vm, "%s>> %s now:%u was:%u", __func__, "blink_rate", *rate_in, *rate_out); + + return 0; + } -static struct dcpu16_hw hw_ = { + if (strcmp(cmd, "refresh_rate") == 0) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; + const unsigned int *rate_in = (const unsigned int *)data_in; + unsigned int *rate_out = (unsigned int *)data_out; + + if (rate_out) { + *rate_out = display->refresh_rate; + } + + if (rate_in) { + display->refresh_rate = *rate_in; + } + + MSG_DEBUG(hw->vm, "%s>> %s now:%u was:%u", __func__, "refresh_rate", *rate_in, *rate_out); + + return 0; + } + +#ifdef HAVE_LIBVNCSERVER + if (strcmp(cmd, "new_rfbScreen") == 0) { + struct args_ { int argc; char **argv;} *in = (struct args_ *)data_in; + rfbScreenInfoPtr *s_out = (rfbScreenInfoPtr *)data_out; + + if (in == NULL || s_out == NULL) + return -EFAULT; + + *s_out = lem1802_rfb_new_(in->argc, in->argv); + + MSG_DEBUG(hw->vm, "%s>> %s s:%p", __func__, "new_rfbScreen", *s_out); + + return 0; + } + + if (strcmp(cmd, "associate_rfbScreen") == 0) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; + rfbScreenInfoPtr rfbScreen = (rfbScreenInfoPtr)data_in; + (void)data_out; + + if (rfbScreen == NULL) + return -EFAULT; + + rfbScreen->desktopName = "NYA ELEKTRISKA LEM1802"; + rfbScreen->frameBuffer = (char *)display->pixbuf; + + MSG_DEBUG(hw->vm, "%s>> %s rfbScreen:%p", __func__, "associate_rfbScreen", rfbScreen); + + return 0; + } +#endif /* HAVE_LIBVNCSERVER */ + + if (strcmp(cmd, "renderers_iter") == 0) { + void **iterp = (void **)data_in; + struct packed_out_ { + char *name; + char *args; + } *parg = (struct packed_out_ *)data_out; + + if (iterp == NULL || parg == NULL) + return -EFAULT; + + (void)lem1802_renderers_iter_(iterp, &parg->name, &parg->args); + + MSG_DEBUG(hw->vm, "%s>> %s", __func__, "renderers_iter"); + + return 0; + } + + if (strcmp(cmd, "renderer") == 0) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; + char *renderer = (char *)data_in; + (void)data_out; + struct renderer_ *r; + + for (r = lem1802_renderers_; r->renderer; r++) { + if (strcmp(renderer, r->name) == 0) { + display->render = r->renderer; + MSG_DEBUG(hw->vm, "%s>> renderer set to %s", __func__, renderer); + return 0; + } + } + + MSG_ERROR(hw->vm, "unknown renderer '%s'", renderer); + + return -ENOENT; + } + + if (strcmp(cmd, "renderer_data") == 0) { + struct lem1802_ *display = (struct lem1802_ *)hw->data; + (void)data_out; + + display->renderer_data = data_in; + + MSG_DEBUG(hw->vm, "%s>> %s data:%p", __func__, "renderer_data", data_in); + + return 0; + } + + return -EINVAL; +} + + +struct dcpu16_hw_module dcpu16_hw_module_lem1802 = { .name_ = "LEM1802 - Low Energy Monitor", + .id_l = 0xf615, .id_h = 0x7349, .ver = 0x1802, @@ -597,12 +723,10 @@ static struct dcpu16_hw hw_ = { .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_, + .ctl = lem1802_data_ctl_, + .ctl_cmd = ctl_, };