cycles now correctly accounted for
[dcpu16] / hw_lem1802.c
index 1be1810fa933720c3da6436b04be742d1633248e..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>
@@ -39,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 */
@@ -75,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;
@@ -90,10 +94,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 +232,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;
     }
@@ -332,7 +364,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__);
 
@@ -359,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 */
@@ -370,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
      */
@@ -393,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
@@ -400,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)
@@ -427,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;
     }
 }