cycles now correctly accounted for
[dcpu16] / hw_lem1802.c
index 92e5149c64d5be7b42489b4f9a371222949b1b5f..e7e618393fc4e313607a24b7f9022a012656b1ae 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>
@@ -17,7 +18,6 @@
 #include "chargen-4x8.h"
 #include "hw_lem1802.h"
 
-#undef DEBUG
 #ifdef DEBUG
 #define TRACE(...) do { printf("[debug] "); printf(__VA_ARGS__); printf("\n"); } while (0)
 #else /* DEBUG  */
@@ -40,6 +40,8 @@ static struct dcpu16_hw hw_ = {
     .data   = (struct lem1802_ *)NULL
 };
 
+#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 */
 #define PIX_BORDER 16   /* border pixels from edge to first tile */
@@ -65,7 +67,7 @@ static const DCPU16_WORD palette_default_[PALETTE_ENTRIES] = {
     0x0f55, /* light red */
     0x0f5f, /* light magenta */
     0x0ff5, /* light yellow */
-    0x0fff, /* white */
+    0x0fff  /* white */
 };
 
 struct pixel_ {
@@ -76,7 +78,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;
@@ -91,16 +94,47 @@ 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) {
-    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
@@ -133,10 +167,12 @@ 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
     TRACE("%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++) {
@@ -160,10 +196,12 @@ void pixbuf_addr_paint_(struct pixel_ *pixbuf, DCPU16_WORD *mem, DCPU16_WORD bas
     cell_x = (addr - base) % CELL_X;
     cell_y = (addr - base) / CELL_X;
 
+#if 0
     TRACE("%s>> addr:0x%04x col:%u row:%u v:%hu",
           __func__,
           addr,
           cell_x, cell_y, mem[addr]);
+#endif
 
     blink = mem[addr] & 0x0080;
 
@@ -190,10 +228,18 @@ void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) {
     struct pixel_ border;
     size_t tile;
 
+#if 0
     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;
     }
@@ -232,14 +278,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;
@@ -247,7 +285,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;
     }
@@ -282,58 +320,52 @@ 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 */
+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 = "NYA ELEKTRISKA 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;
+    TRACE("%s>> s:%p", __func__, s);
+    TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__, s->kbdAddEvent, s->frameBuffer);
 
-    rfbInitServer(s->rfbScreen);
+    return s;
+}
 
-    rfbRunEventLoop(s->rfbScreen,-1,TRUE);
+/* 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;
 
-    TRACE("%s>> initialized new vnc server", __func__);
+    s->desktopName = "NYA ELEKTRISKA LEM1802";
+    s->frameBuffer = (char *)display->pixbuf;
 
-    return s;
+    TRACE("%s>> s:%p\n", __func__, 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;
 
     /* derp */
-    rfbMarkRectAsModified(s->rfbScreen,0,0,x,y);
+    if (s)
+        rfbMarkRectAsModified(s, 0, 0, x, y);
 
     TRACE("%s>>", __func__);
 
@@ -360,6 +392,8 @@ 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 */
@@ -371,10 +405,8 @@ void lem1802_cycle_(struct dcpu16 *vm, void *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
      */
@@ -394,6 +426,33 @@ 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_,
+                  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
@@ -401,14 +460,14 @@ 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;
 
     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)
@@ -428,23 +487,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;
     }
 }
@@ -527,7 +582,9 @@ struct dcpu16_hw *lem1802_new(struct dcpu16 *vm) {
     }
 
     ((struct lem1802_ *)(hw->data))->refresh_rate = 1666;
-    ((struct lem1802_ *)(hw->data))->blink_rate = 100000;
+    ((struct lem1802_ *)(hw->data))->blink_rate = 75000;
+
+    hw->vm = vm;
 
     return hw;
 }