Add new value range check when writing register in CLI version
authorHugo Villeneuve <hugo@hugovil.com>
Thu, 6 Feb 2014 03:02:43 +0000 (22:02 -0500)
committerHugo Villeneuve <hugo@hugovil.com>
Thu, 13 Feb 2014 05:25:17 +0000 (00:25 -0500)
The GUI version also had this check, so the functionality has been merged.

src/common/sfr.c
src/common/sfr.h
src/gtk/regwin.c

index 165ba2e..8d74dcf 100644 (file)
@@ -317,9 +317,20 @@ regwin_read(int row)
        return val;
 }
 
-void
+int
 regwin_write(struct regwin_infos_t *p, int new)
 {
+       int max_value;
+
+       max_value = (1 << (4 * p->w)) - 1; /* 16^w - 1 */
+
+       /* Check that the new value is not too large for the register type. */
+       if (new > max_value) {
+               /* Display message for CLI version */
+               printf("Value out of range\n");
+               return -1;
+       }
+
        if (p->write_func == NULL) {
                /*
                 * Write register value using generic 8 or 16 bits write
@@ -330,6 +341,8 @@ regwin_write(struct regwin_infos_t *p, int new)
                /* Write register value using custom function pointer. */
                p->write_func(p->param, new);
        }
+
+       return 0;
 }
 
 struct regwin_infos_t *
@@ -342,7 +355,7 @@ sfr_get_infos(const char *regname)
                        return &regwin_infos[row];
        }
 
-       return NULL; /* Programming error. */
+       return NULL; /* Register not found. */
 }
 
 struct regwin_infos_t *
index fe552dd..4d4d511 100644 (file)
@@ -34,7 +34,7 @@ struct regwin_infos_t {
 int
 regwin_read(int row);
 
-void
+int
 regwin_write(struct regwin_infos_t *p, int new);
 
 struct regwin_infos_t *
index 8b470a9..a2b0f6e 100644 (file)
@@ -80,6 +80,7 @@ regwin_cell_edited(GtkCellRendererText *cell, gchar *path_string,
        int old;
        int new;
        char *str;
+       int rc;
        struct regwin_infos_t *regwin_infos;
 
        (void) cell; /* Remove compiler warning about unused variables. */
@@ -96,47 +97,30 @@ regwin_cell_edited(GtkCellRendererText *cell, gchar *path_string,
 
        log_info("Register: %s", str);
        regwin_infos = sfr_get_infos(str);
+       log_info("  width:     %d bits", 4 * regwin_infos->w);
 
        /* Read current (old) value. */
        gtk_tree_model_get(model, &iter, COL_VAL, &str, -1);
 
        old = asciihex2int(str);
-
-       if (regwin_infos->w == 2)
-               log_info("  old value: $%02X", old);
-       else if (regwin_infos->w == 4)
-               log_info("  old value: $%04X", old);
+       log_info("  old value: $%04X", old);
 
        new = asciihex2int(new_str);
-
-       if (regwin_infos->w == 2) {
-               if ((new < 0) || (new > 0xFF)) {
-                       log_info("  new value: out of range");
-                       new = old; /* Put back old value... */
-               } else {
-                       log_info("  new value: $%02X", new);
-               }
-       } else if (regwin_infos->w == 4) {
-               if ((new < 0) || (new > 0xFFFF)) {
-                       log_info("  new value: out of range");
-                       new = old; /* Put back old value... */
-               } else {
-                       log_info("  new value: $%04X", new);
-               }
+       log_info("  new value: $%04X", new);
+
+       /* Store new value in emulator register (if in range). */
+       rc = regwin_write(regwin_infos, new);
+       if (rc == 0) {
+               /* Store new value in gtk model. */
+               int2asciihex(new, str, regwin_infos->w);
+               gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_VAL, str, -1);
+
+               /*
+                * Make sure to update all windows.
+                * For example, R0-R7 values depends on internal memory values.
+                */
+               emugtk_UpdateDisplay();
        }
-
-       /* Store new value in emulator register. */
-       regwin_write(regwin_infos, new);
-
-       /* Store new value in gtk model. */
-       int2asciihex(new, str, regwin_infos->w);
-        gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_VAL, str, -1);
-
-       /*
-        * Make sure to update all windows.
-        * For example, R0-R7 values depends on internal memory values.
-        */
-       emugtk_UpdateDisplay();
 };
 
 static void