further changes for v1.7: cpu fixes, support for 'hardware' devices, display to vnc
[dcpu16] / hw_lem1802.c
diff --git a/hw_lem1802.c b/hw_lem1802.c
new file mode 100644 (file)
index 0000000..72af16a
--- /dev/null
@@ -0,0 +1,509 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef HAVE_LIBPNG
+#include <setjmp.h>
+#include <png.h>
+#endif /* HAVE_LIBPNG */
+
+#ifdef HAVE_LIBVNCSERVER
+#include <rfb/rfb.h>
+#include <rfb/keysym.h>
+#endif /* HAVE_LIBVNCSERVER */
+
+#include "dcpu16.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
+};
+
+#define PIX_X 160
+#define PIX_Y 128
+#define PIX_BORDER 16
+#define CELL_X_SZ 4
+#define CELL_Y_SZ 8
+
+#define PALETTE_ENTRIES 16
+static const DCPU16_WORD palette_default_[PALETTE_ENTRIES] = {
+    0x0000, /* black */
+    0x000a, /* blue */
+    0x00a0, /* green */
+    0x00aa, /* cyan */
+    0x0a05, /* red */
+    0x0a0f, /* magenta */
+    0x0aa5, /* yellow */
+    0x0aaf, /* pale blue */
+    0x0555, /* grey */
+    0x055f, /* light blue */
+    0x05f5, /* light green*/
+    0x05ff, /* light cyan */
+    0x0f55, /* light red */
+    0x0f5f, /* light magenta */
+    0x0ff5, /* light yellow */
+    0x0fff, /* white */
+};
+
+struct pixel_ {
+    unsigned char r;
+    unsigned char g;
+    unsigned char b;
+    unsigned char a;
+};
+
+struct lem1802_ {
+    long long cycle_activated; /* for tracking 'turn on delay' */
+
+    DCPU16_WORD video_base;
+    DCPU16_WORD font_base;
+    DCPU16_WORD palette_base;
+    DCPU16_WORD border_color;
+    struct pixel_ *pixbuf;
+
+    unsigned int refresh_rate; /* redraw every n cycles */
+    unsigned int refresh_tally_; /* tick */
+    
+    unsigned int blink_rate; /* toggle every n cycles? still figuring this out.. */
+    unsigned int blink_tally_; /* tick */
+    unsigned int blink_state;
+
+    int (*render)(void *, struct pixel_ *, size_t, size_t);
+    void *renderer_data;
+};
+
+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);
+}
+
+static
+void pixbuf_border_paint_(struct pixel_ *pixbuf, struct pixel_ *border) {
+    size_t x, y, i;
+
+    /* top */
+    for (y = 0; y < PIX_BORDER; y++) {
+        for (x = 0; x < PIX_X; x++) {
+            i = (y * PIX_X) + x;
+            pixbuf[i] = *border;
+            i = ((PIX_Y - y) * PIX_X) + x;
+            pixbuf[i] = *border;
+        }
+    }
+
+    /* sides */
+    for (y = PIX_BORDER; y < (PIX_Y - PIX_BORDER + 1); y++)
+        for (x = 0; x < PIX_BORDER; x++) {
+            i = (y * PIX_X) + x;
+            pixbuf[i] = *border;
+            pixbuf[i + (PIX_X - PIX_BORDER)] = *border;
+        }
+}
+
+static
+void font_tile_paint_(struct pixel_ *p, struct pixel_ *fg, struct pixel_ *bg, DCPU16_WORD *tile) {
+    size_t pix_x, pix_y;
+    unsigned char *font_bitmap = (unsigned char *)tile;
+
+    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)
+                p[((CELL_Y_SZ - pix_y - 1) * PIX_X) + pix_x] = *fg;
+            else
+                p[((CELL_Y_SZ - pix_y - 1) * PIX_X) + pix_x] = *bg;
+        }
+    }
+}
+
+static
+void pixbuf_addr_paint_(struct pixel_ *pixbuf, DCPU16_WORD *mem, 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);
+    struct pixel_ fg, bg;
+    DCPU16_WORD *font_bitmap;
+    int blink;
+
+    cell_x = addr % (PIX_X / CELL_X_SZ);
+    cell_y = addr / (PIX_X / CELL_X_SZ);
+
+    blink = mem[addr] & 0x0080;
+
+    /* tiles take two words each */
+    font_bitmap = tiles + (2 * (mem[addr] & 0x7f));
+
+    pixel_color_(&bg, palette[(mem[addr] >> 8) & 0x0f]);
+    if (blink && blink_state)
+        pixel_color_(&fg, palette[(mem[addr] >> 8) & 0x0f]);
+    else
+        pixel_color_(&fg, palette[(mem[addr] >> 12) & 0x0f]);
+
+    tilestart += (PIX_X * PIX_BORDER); /* skip top border */
+    tilestart += (CELL_Y_SZ * PIX_X) * cell_y; /* skip down to row */
+
+    tilestart += PIX_BORDER; /* skip side border */
+    tilestart += (CELL_X_SZ) * cell_x; /* skip to column */
+
+    font_tile_paint_(tilestart, &fg, &bg, font_bitmap);
+}
+
+static
+void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) {
+    struct pixel_ border;
+    size_t tile;
+
+    if (display->video_base == 0) {
+        /* disconnected, blank display.  static might be fun, too */
+        memset(display->pixbuf, 0, PIX_X * PIX_Y * sizeof *display->pixbuf);
+        return;
+    }
+
+    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++) {
+        pixbuf_addr_paint_(display->pixbuf,
+                           mem,
+                           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->blink_state);
+    }
+}
+
+static
+int pixbuf_render_pnm_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
+    FILE *f = (FILE *)data;
+    size_t i;
+    fprintf(f, "P6\n"
+               "%lu %lu\n"
+               "255\n",
+               x, y);
+    for (i = 0; i < x * y; i++) {
+        fwrite(&pixbuf[i].r, 1, 1, f);
+        fwrite(&pixbuf[i].g, 1, 1, f);
+        fwrite(&pixbuf[i].b, 1, 1, f);
+    }
+    fclose(f);
+
+    return 0;
+}
+
+#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;
+    png_structp png;
+    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_);
+    if (png == NULL) {
+        goto f_done;
+    }
+
+    info = png_create_info_struct(png);
+    if (info == NULL) {
+        png_destroy_write_struct(&png, (png_infopp)NULL);
+        goto f_done;
+    }
+
+    if (setjmp(png_jmpbuf(png))) {
+        png_destroy_write_struct(&png, &info);
+        goto f_done;
+    }
+
+    png_init_io(png, f);
+    png_set_IHDR(png, info, x, y, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+    png_set_invert_alpha(png);
+    png_write_info(png, info);
+    for (i = 0; i < y; i++) {
+        png_write_row(png, (unsigned char *)(pixbuf + (i * x)));
+    }
+    png_write_end(png, info);
+
+    png_destroy_write_struct(&png, &info);
+
+f_done:
+    fclose(f);
+
+    return retval;
+}
+#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;
+    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);
+    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;
+
+#if 0
+    s->rfbScreen->kbdAddEvent = HandleKey; */
+    s->rfbScreen->httpDir = "../classes";
+#endif
+
+    s->rfbScreen->frameBuffer = (char *)pixbuf;
+
+    rfbInitServer(s->rfbScreen);
+
+    rfbRunEventLoop(s->rfbScreen,-1,TRUE);
+
+    return s;
+}
+
+static
+int pixbuf_render_vnc_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
+    int retval = 0;
+    struct vnc_server_ *s = (struct vnc_server_ *)data;
+
+    (void)pixbuf;
+
+    /* derp */
+    rfbMarkRectAsModified(s->rfbScreen,0,0,x,y);
+
+    return retval;
+}
+#endif /* HAVE_LIBVNCSERVER */
+
+
+static
+void lem1802_reset_(struct dcpu16 *vm, void *data) {
+    struct lem1802_ *display = (struct lem1802_ *)data;
+
+    display->cycle_activated = vm->cycle;
+
+    display->video_base = 0;
+    display->font_base = 0;
+    display->palette_base = 0;
+    display->border_color = 0;
+
+    memset(display->pixbuf, 0, PIX_X * PIX_Y * sizeof *display->pixbuf);
+
+    display->refresh_tally_ = 0;
+    display->blink_tally_ = 0;
+    display->blink_state = 0;
+}
+
+static
+void lem1802_cycle_(struct dcpu16 *vm, void *data) {
+    struct lem1802_ *display = (struct lem1802_ *)data;
+
+    (void)vm;
+    /*
+        maybe just step through video memory (if set)
+        one word per clock..?  could just cheat and
+        use accounting callbacks..
+
+        for now just count cycles and issue a full refresh/render
+        every so often
+     */
+
+    display->blink_tally_++;
+    if (display->blink_tally_ >= display->blink_rate)
+        display->blink_state ^= 1;
+
+    display->refresh_tally_++;
+    if (display->refresh_tally_ >= display->refresh_rate) {
+        display->refresh_tally_ = 0;
+        if (display->render)
+            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);
+    }
+}
+
+static
+void lem1802_hwi_(struct dcpu16 *vm, void *data) {
+    struct lem1802_ *display = (struct lem1802_ *)data;
+    DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A];
+    DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B];
+    size_t i;
+
+    switch (reg_a) {
+        case 0: /* MEM_MAP_SCREEN */
+            if (display->cycle_activated == 0 && reg_b) {
+                display->cycle_activated = vm->cycle;
+            }
+            display->video_base = reg_b;
+            break;
+
+        case 1: /* MEM_MAP_FONT */
+            display->font_base = reg_b;
+            break;
+
+        case 2: /* MEM_MAP_PALETTE */
+            display->palette_base = reg_b;
+            break;
+
+        case 3: /* SET_BORDER_COLOR */
+            display->border_color = 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;
+            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;
+            break;
+    }
+}
+
+static struct renderer_ {
+    char *name;
+    char *args;
+    int (*renderer)(void *, struct pixel_ *, size_t, size_t);
+} lem1802_renderers_[] = {
+    { "pnm", "filename", pixbuf_render_pnm_ },
+#ifdef HAVE_LIBPNG
+    { "png", "filename", pixbuf_render_png_ },
+#endif /* HAVE_LIBPNG */
+#ifdef HAVE_LIBVNCSERVER
+    { "vnc", "", pixbuf_render_vnc_ },
+#endif /* HAVE_LIBVNCSERVER */
+    { "none", "", NULL },
+    { 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) {
+    struct renderer_ **r = (struct renderer_ **)iterp;
+
+    if (*r == NULL)
+        *r = lem1802_renderers_;
+    else
+        (*r)++;
+
+    if ((*r)->name == NULL) {
+        *r = NULL;
+        return NULL;
+    }
+
+    *name = (*r)->name;
+    *args = (*r)->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);
+
+    hw->data = calloc(1, sizeof(struct dcpu16_hw));
+    if (hw->data == NULL) {
+        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+        free(hw);
+        return NULL;
+    }
+
+    ((struct lem1802_ *)(hw->data))->pixbuf = calloc(1, 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));
+        free(hw->data);
+        free(hw);
+        return NULL;
+    }
+
+    return hw;
+}
+
+void lem1802_del(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;
+            }
+            free(*hw);
+        }
+        *hw = NULL;
+    }
+}