Merge ccdbg and altos sources into one giant repository
[fw/altos] / lib / ccdbg-memory.c
index 3406a1b17a1d48a1dafec3e8449fdceccc484281..554ac637c6911491e61005ba76ca7932d8d06686 100644 (file)
@@ -48,6 +48,11 @@ uint8_t
 ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
 {
        int i, nl = 0;
+       struct ccstate state;
+
+       if (dbg->usb)
+               return cc_usb_write_memory(dbg->usb, addr, bytes, nbytes);
+       ccdbg_state_save(dbg, &state, CC_STATE_ACC | CC_STATE_PSW | CC_STATE_DP);
        memory_init[HIGH_START] = addr >> 8;
        memory_init[LOW_START] = addr;
        (void) ccdbg_execute(dbg, memory_init);
@@ -56,7 +61,7 @@ ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
                ccdbg_execute(dbg, write8);
                if ((i & 0xf) == 0xf) {
                        ccdbg_debug(CC_DEBUG_MEMORY, ".");
-                       ccdbg_flush();
+                       ccdbg_flush(CC_DEBUG_MEMORY);
                        nl = 1;
                }
                if ((i & 0xff) == 0xff) {
@@ -64,6 +69,7 @@ ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
                        nl = 0;
                }
        }
+       ccdbg_state_restore(dbg, &state);
        if (nl)
                ccdbg_debug(CC_DEBUG_MEMORY, "\n");
        return 0;
@@ -73,6 +79,15 @@ uint8_t
 ccdbg_read_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
 {
        int i, nl = 0;
+       struct ccstate state;
+
+       if (ccdbg_rom_contains(dbg, addr, nbytes)) {
+               ccdbg_rom_replace_xmem(dbg, addr, bytes, nbytes);
+               return 0;
+       }
+       if (dbg->usb)
+               return cc_usb_read_memory(dbg->usb, addr, bytes, nbytes);
+       ccdbg_state_save(dbg, &state, CC_STATE_ACC | CC_STATE_PSW | CC_STATE_DP);
        memory_init[HIGH_START] = addr >> 8;
        memory_init[LOW_START] = addr;
        (void) ccdbg_execute(dbg, memory_init);
@@ -80,7 +95,7 @@ ccdbg_read_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
                *bytes++ = ccdbg_execute(dbg, read8);
                if ((i & 0xf) == 0xf) {
                        ccdbg_debug(CC_DEBUG_MEMORY, ".");
-                       ccdbg_flush();
+                       ccdbg_flush(CC_DEBUG_MEMORY);
                        nl = 1;
                }
                if ((i & 0xff) == 0xff) {
@@ -88,6 +103,8 @@ ccdbg_read_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
                        nl = 0;
                }
        }
+       ccdbg_state_replace_xmem(dbg, &state, addr, bytes, nbytes);
+       ccdbg_state_restore(dbg, &state);
        if (nl)
                ccdbg_debug(CC_DEBUG_MEMORY, "\n");
        return 0;
@@ -110,7 +127,7 @@ struct hex_image *
 ccdbg_read_hex_image(struct ccdbg *dbg, uint16_t address, uint16_t length)
 {
        struct hex_image *image;
-       
+
        image = calloc(sizeof(struct hex_image) + length, 1);
        image->address = address;
        image->length = length;
@@ -118,3 +135,45 @@ ccdbg_read_hex_image(struct ccdbg *dbg, uint16_t address, uint16_t length)
        ccdbg_read_memory(dbg, address, image->data, length);
        return image;
 }
+
+static uint8_t sfr_read[] = {
+       2,      MOV_A_direct,           0,
+#define SFR_READ_ADDR  2
+       0,
+};
+
+static uint8_t sfr_write[] = {
+       3,      MOV_direct_data,        0,      0,
+#define SFR_WRITE_ADDR 2
+#define SFR_WRITE_DATA 3
+       0,
+};
+
+uint8_t
+ccdbg_read_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes)
+{
+       int     i;
+       struct ccstate state;
+
+       ccdbg_state_save(dbg, &state, CC_STATE_ACC);
+       for (i = 0; i < nbytes; i++) {
+               sfr_read[SFR_READ_ADDR] = addr + i;
+               *bytes++ = ccdbg_execute(dbg, sfr_read);
+       }
+       ccdbg_state_replace_sfr(dbg, &state, addr, bytes, nbytes);
+       ccdbg_state_restore(dbg, &state);
+       return 0;
+}
+
+uint8_t
+ccdbg_write_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes)
+{
+       int     i;
+
+       for (i = 0; i < nbytes; i++) {
+               sfr_write[SFR_WRITE_ADDR] = addr + i;
+               sfr_write[SFR_WRITE_DATA] = *bytes++;
+               ccdbg_execute(dbg, sfr_write);
+       }
+       return 0;
+}