merge
[dcpu16] / hw_lem1802.c
index 72af16ac769cb6525560402ad4e4a29371fb4e28..f8004a38571670eb9e13443e80dc1a00d36f4743 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/time.h>
 
 #ifdef HAVE_LIBPNG
 #include <setjmp.h>
 #include "chargen-4x8.h"
 #include "hw_lem1802.h"
 
-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
-};
+/* 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 MSG_DEBUG(__vm__, ...) MSG_(DCPU16_MSG_DEBUG, __vm__, __VA_ARGS__)
+#else /* DEBUG  */
+#define MSG_DEBUG(__vm__, ...) do { } while (0)
+#endif /* DEBUG */
+
+#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 /* 'about one second' */
 
-#define PIX_X 160
-#define PIX_Y 128
-#define PIX_BORDER 16
+#define PIX_X 160       /* pixels in display */
+#define PIX_Y 128       /* pixels in display */
+#define PIX_BORDER 16   /* border pixels from edge to first tile */
 #define CELL_X_SZ 4
 #define CELL_Y_SZ 8
+#define CELL_X ((PIX_X - (2 * PIX_BORDER)) / CELL_X_SZ) /* tiles in display */
+#define CELL_Y ((PIX_Y - (2 * PIX_BORDER)) / CELL_Y_SZ) /* tiles in display */
 
 #define PALETTE_ENTRIES 16
 static const DCPU16_WORD palette_default_[PALETTE_ENTRIES] = {
@@ -56,18 +68,19 @@ static const DCPU16_WORD palette_default_[PALETTE_ENTRIES] = {
     0x0f55, /* light red */
     0x0f5f, /* light magenta */
     0x0ff5, /* light yellow */
-    0x0fff, /* white */
+    0x0fff  /* white */
 };
 
 struct pixel_ {
     unsigned char r;
     unsigned char g;
     unsigned char b;
-    unsigned char a;
+    unsigned char a; /* unused */
 };
 
 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;
@@ -82,22 +95,56 @@ 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);
+    /* 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
+
+    return r;
+}
+
 static inline
 void pixel_color_(struct pixel_ *pix, DCPU16_WORD color) {
-    pix->r = (color & 0x000f) | ((color & 0x000f) << 8);
-    pix->g = ((color & 0x00f0) >> 8) | (color & 0x00f0);
-    pix->b = ((color & 0x0f00) >> 8) | ((color & 0x0f00) >> 16);
-    pix->a = ((color & 0xf000) >> 16) | ((color & 0xf000) >> 24);
+    unsigned char x;
+
+    x = (color >> 0) & 0x000f;
+    pix->r = x | (x << 4);
+
+    x = (color >> 4) & 0x000f;
+    pix->g = x | (x << 4);
+
+    x = (color >> 8) & 0x000f;
+    pix->b = x | (x << 4);
+
+    x = (color >> 12) & 0x000f;
+    pix->a = x | (x << 4);
 }
 
 static
 void pixbuf_border_paint_(struct pixel_ *pixbuf, struct pixel_ *border) {
     size_t x, y, i;
 
+    MSG_DEBUG(NULL, "%s>> painting border", __func__);
+
     /* top */
     for (y = 0; y < PIX_BORDER; y++) {
         for (x = 0; x < PIX_X; x++) {
@@ -122,6 +169,13 @@ void font_tile_paint_(struct pixel_ *p, struct pixel_ *fg, struct pixel_ *bg, DC
     size_t pix_x, pix_y;
     unsigned char *font_bitmap = (unsigned char *)tile;
 
+#if 0
+    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]);
+#endif
+
     for (pix_x = 0; pix_x < CELL_X_SZ; pix_x++) {
         for (pix_y = 0; pix_y < CELL_Y_SZ; pix_y++) {
             if ((font_bitmap[pix_x] >> pix_y) & 0x01)
@@ -133,7 +187,7 @@ void font_tile_paint_(struct pixel_ *p, struct pixel_ *fg, struct pixel_ *bg, DC
 }
 
 static
-void pixbuf_addr_paint_(struct pixel_ *pixbuf, DCPU16_WORD *mem, DCPU16_WORD addr, DCPU16_WORD *palette, DCPU16_WORD *tiles, unsigned int blink_state) {
+void pixbuf_addr_paint_(struct pixel_ *pixbuf, DCPU16_WORD *mem, DCPU16_WORD base, DCPU16_WORD addr, DCPU16_WORD *palette, DCPU16_WORD *tiles, unsigned int blink_state) {
     struct pixel_ *tilestart = pixbuf; /* start of display */
     unsigned int cell_x = addr % (PIX_X / CELL_X_SZ),
                  cell_y = addr / (PIX_X / CELL_X_SZ);
@@ -141,8 +195,15 @@ void pixbuf_addr_paint_(struct pixel_ *pixbuf, DCPU16_WORD *mem, DCPU16_WORD add
     DCPU16_WORD *font_bitmap;
     int blink;
 
-    cell_x = addr % (PIX_X / CELL_X_SZ);
-    cell_y = addr / (PIX_X / CELL_X_SZ);
+    cell_x = (addr - base) % CELL_X;
+    cell_y = (addr - base) / CELL_X;
+
+#if 0
+    MSG_DEBUG(NULL, "%s>> addr:0x%04x col:%u row:%u v:%hu",
+          __func__,
+          addr,
+          cell_x, cell_y, mem[addr]);
+#endif
 
     blink = mem[addr] & 0x0080;
 
@@ -169,8 +230,18 @@ void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) {
     struct pixel_ border;
     size_t tile;
 
+#if 0
+    MSG_DEBUG(NULL, "%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;
     }
@@ -178,12 +249,13 @@ void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) {
     pixel_color_(&border, display->border_color);
     pixbuf_border_paint_(display->pixbuf, &border);
 
-    for (tile = 0; tile < (PIX_X / CELL_X_SZ) * (PIX_Y / CELL_Y_SZ); tile++) {
+    for (tile = 0; tile < CELL_X * CELL_Y; tile++) {
         pixbuf_addr_paint_(display->pixbuf,
                            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);
     }
 }
@@ -208,14 +280,6 @@ int pixbuf_render_pnm_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
 
 #ifdef HAVE_LIBPNG
 static
-void pixbuf_render_png_user_warning_(png_structp png, png_const_charp msg) {
-    (void)png, (void)msg;
-}
-static
-void pixbuf_render_png_user_error_(png_structp png, png_const_charp msg) {
-    (void)png, (void)msg;
-}
-static
 int pixbuf_render_png_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
     FILE *f = (FILE *)data;
     int retval = 0;
@@ -223,7 +287,7 @@ int pixbuf_render_png_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
     png_infop info;
     size_t i;
 
-    png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, pixbuf_render_png_user_error_, pixbuf_render_png_user_warning_);
+    png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
     if (png == NULL) {
         goto f_done;
     }
@@ -258,56 +322,48 @@ f_done:
 #endif /* HAVE_LIBPNG */
 
 #ifdef HAVE_LIBVNCSERVER
-struct vnc_server_ {
-    rfbScreenInfoPtr rfbScreen;
-};
-
-/* create and return a new struct vnc_server_, for use as a vnc renderer's generic data */
-void *lem1802_vnc_init_data(int argc, char *argv[], struct dcpu16_hw *hw) {
-    struct vnc_server_ *s;
-    struct pixel_ *pixbuf = ((struct lem1802_ *)(hw->data))->pixbuf;
+/* create and return a new screen, easiest to do here because we know the screen dimensions */
+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;
     int bitsPerSample = 8;
     int samplesPerPixel = 3;
     int bytesPerPixel = 4;
 
-    s = calloc(1, sizeof *s);
+    s = rfbGetScreen(&argc, argv, paddedWidth, height, bitsPerSample, samplesPerPixel, bytesPerPixel);
     if (s == NULL)
         return NULL;
 
-    s->rfbScreen = rfbGetScreen(&argc, argv, paddedWidth, height, bitsPerSample, samplesPerPixel, bytesPerPixel);
-    if (s->rfbScreen == NULL) {
-        free(s);
-        return NULL;
-    }
-
-    s->rfbScreen->desktopName = "lem1802";
-    s->rfbScreen->alwaysShared = TRUE;
+    s->alwaysShared = TRUE;
 
 #if 0
-    s->rfbScreen->kbdAddEvent = HandleKey; */
-    s->rfbScreen->httpDir = "../classes";
+    s->httpDir = "../classes";
 #endif
 
-    s->rfbScreen->frameBuffer = (char *)pixbuf;
-
-    rfbInitServer(s->rfbScreen);
-
-    rfbRunEventLoop(s->rfbScreen,-1,TRUE);
+    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;
 }
 
+
+/* notify rfb server that pixels may have changed */
 static
 int pixbuf_render_vnc_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
+    rfbScreenInfoPtr s = (rfbScreenInfoPtr)data;
     int retval = 0;
-    struct vnc_server_ *s = (struct vnc_server_ *)data;
 
     (void)pixbuf;
 
+    MSG_DEBUG(NULL, "%s>> s:%p", __func__, s);
+
     /* derp */
-    rfbMarkRectAsModified(s->rfbScreen,0,0,x,y);
+    if (s)
+        rfbMarkRectAsModified(s, 0, 0, x, y);
+
+    MSG_DEBUG(NULL, "%s>>", __func__);
 
     return retval;
 }
@@ -315,11 +371,12 @@ 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;
 
-    display->cycle_activated = vm->cycle;
+    (void)vm;
 
+    display->cycle_activated = 0;
     display->video_base = 0;
     display->font_base = 0;
     display->palette_base = 0;
@@ -330,84 +387,119 @@ void lem1802_reset_(struct dcpu16 *vm, void *data) {
     display->refresh_tally_ = 0;
     display->blink_tally_ = 0;
     display->blink_state = 0;
+
+    display->cycle_state_ = 0;
+
+    MSG_DEBUG(vm, "%s>>", __func__);
 }
 
 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
      */
 
     display->blink_tally_++;
-    if (display->blink_tally_ >= display->blink_rate)
+    if (display->blink_tally_ >= display->blink_rate) {
+        display->blink_tally_ = 0;
         display->blink_state ^= 1;
+        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)
+            MSG_DEBUG(vm, "%s>> refresh", __func__);
+            lem1802_pixbuf_refresh_full_(display, vm->ram);
             display->render(display->renderer_data, display->pixbuf, PIX_X, PIX_Y);
     }
 
-    if (display->render) {
-        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:
+            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_),
+                  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) {
+            MSG_DEBUG(vm, "%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;
+
+    MSG_DEBUG(vm, "%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)
+                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 */
-            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;
+            MSG_DEBUG(vm, "%s>> copying default font into 0x%04x - 0x%04x", __func__, reg_b, reg_b + 256);
+            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;
+            MSG_DEBUG(vm, "%s>> copying default palette into 0x%04x - 0x%04x", __func__, reg_b, reg_b + 16);
+            dcpu16_cycle_inc(vm, 16);
             break;
     }
 }
@@ -428,21 +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;
-            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)
@@ -461,49 +540,193 @@ 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;
-
-    hw = calloc(1, sizeof *hw);
-    if (hw == NULL) {
-        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
-        return NULL;
-    }
-
-    memcpy(hw, &hw_, sizeof *hw);
+static
+int lem1802_data_init_(struct dcpu16_hw *hw, void *data) {
+    (void)data;
 
-    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;
+        MSG_ERROR(hw->vm, "%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));
+        MSG_ERROR(hw->vm, "%s():%s", "calloc", strerror(errno));
         free(hw->data);
-        free(hw);
-        return NULL;
+        hw->data = NULL;
+        return -1;
     }
 
-    return hw;
+    ((struct lem1802_ *)(hw->data))->refresh_rate = 1666;
+    ((struct lem1802_ *)(hw->data))->blink_rate = 75000;
+
+    return 0;
 }
 
-void lem1802_del(struct dcpu16_hw **hw) {
+static
+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) {
+            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;
+    }
+
+    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(NULL, "%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;
             }
-            free(*hw);
         }
-        *hw = NULL;
+
+        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,
+    .mfg_l  = 0x8b36,
+    .mfg_h  = 0x1c6c,
+    .hwi    = lem1802_hwi_,
+    .cycle  = lem1802_cycle_,
+    .reset  = lem1802_reset_,
+
+    .data_init = lem1802_data_init_,
+    .data_free = lem1802_data_free_,
+    .ctl = lem1802_data_ctl_,
+    .ctl_cmd = ctl_,
+};
+