fixed bug with instruction decoding in dcpu, fixed bugs in display
[dcpu16] / hw_lem1802.c
index 72af16ac769cb6525560402ad4e4a29371fb4e28..ae0c9dd3ae9b21eede8c12c01c8bd778dd8756cc 100644 (file)
 #include "chargen-4x8.h"
 #include "hw_lem1802.h"
 
+#ifdef DEBUG
+#define TRACE(...) do { printf("[debug] "); printf(__VA_ARGS__); printf("\n"); } while (0)
+#else /* DEBUG  */
+#define TRACE(...) do {} while (0)
+#endif /* DEBUG */
+
 static dcpu16_hw_signal_t lem1802_reset_;
 static dcpu16_hw_signal_t lem1802_cycle_;
 static dcpu16_hw_signal_t lem1802_hwi_;
@@ -33,11 +39,13 @@ static struct dcpu16_hw hw_ = {
     .data   = (struct lem1802_ *)NULL
 };
 
-#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] = {
@@ -98,6 +106,8 @@ static
 void pixbuf_border_paint_(struct pixel_ *pixbuf, struct pixel_ *border) {
     size_t x, y, i;
 
+    TRACE("%s>> painted border", __func__);
+
     /* top */
     for (y = 0; y < PIX_BORDER; y++) {
         for (x = 0; x < PIX_X; x++) {
@@ -122,6 +132,11 @@ 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;
 
+    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]);
+
     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 +148,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 +156,13 @@ 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;
+
+    TRACE("%s>> addr:0x%04x col:%u row:%u v:%hu",
+          __func__,
+          addr,
+          cell_x, cell_y, mem[addr]);
 
     blink = mem[addr] & 0x0080;
 
@@ -169,6 +189,8 @@ void lem1802_pixbuf_refresh_full_(struct lem1802_ *display, DCPU16_WORD *mem) {
     struct pixel_ border;
     size_t tile;
 
+    TRACE("%s>> video_base:0x%04x", __func__, display->video_base);
+
     if (display->video_base == 0) {
         /* disconnected, blank display.  static might be fun, too */
         memset(display->pixbuf, 0, PIX_X * PIX_Y * sizeof *display->pixbuf);
@@ -178,9 +200,10 @@ 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,
@@ -282,7 +305,7 @@ void *lem1802_vnc_init_data(int argc, char *argv[], struct dcpu16_hw *hw) {
         return NULL;
     }
 
-    s->rfbScreen->desktopName = "lem1802";
+    s->rfbScreen->desktopName = "NYA ELEKTRISKA LEM1802";
     s->rfbScreen->alwaysShared = TRUE;
 
 #if 0
@@ -296,6 +319,8 @@ void *lem1802_vnc_init_data(int argc, char *argv[], struct dcpu16_hw *hw) {
 
     rfbRunEventLoop(s->rfbScreen,-1,TRUE);
 
+    TRACE("%s>> initialized new vnc server", __func__);
+
     return s;
 }
 
@@ -309,6 +334,8 @@ int pixbuf_render_vnc_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
     /* derp */
     rfbMarkRectAsModified(s->rfbScreen,0,0,x,y);
 
+    TRACE("%s>>", __func__);
+
     return retval;
 }
 #endif /* HAVE_LIBVNCSERVER */
@@ -318,8 +345,9 @@ static
 void lem1802_reset_(struct dcpu16 *vm, void *data) {
     struct lem1802_ *display = (struct lem1802_ *)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,6 +358,10 @@ void lem1802_reset_(struct dcpu16 *vm, void *data) {
     display->refresh_tally_ = 0;
     display->blink_tally_ = 0;
     display->blink_state = 0;
+
+#if DEBUG
+    vm->trace_cb_("%s>>", __func__);
+#endif /* DEBUG */
 }
 
 static
@@ -347,20 +379,20 @@ void lem1802_cycle_(struct dcpu16 *vm, void *data) {
      */
 
     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;
+        TRACE("%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__);
+            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);
-    }
 }
 
 static
@@ -370,12 +402,16 @@ void lem1802_hwi_(struct dcpu16 *vm, void *data) {
     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->video_base = reg_b;
+            if (reg_b == 0)
+                display->cycle_activated = 0;
             break;
 
         case 1: /* MEM_MAP_FONT */
@@ -435,6 +471,7 @@ int lem1802_renderer_set(struct dcpu16_hw *hw, const char *renderer, void *data)
         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;
         }
     }
@@ -488,6 +525,9 @@ struct dcpu16_hw *lem1802_new(struct dcpu16 *vm) {
         return NULL;
     }
 
+    ((struct lem1802_ *)(hw->data))->refresh_rate = 1666;
+    ((struct lem1802_ *)(hw->data))->blink_rate = 75000;
+
     return hw;
 }