ccdbg-io.c \
ccdbg-manual.c \
ccdbg-memory.c \
+ ccdbg-rom.c \
+ ccdbg-state.c \
cp-usb.c \
cp-usb-async.c
}
void
-ccdbg_flush(void)
+ccdbg_flush(int level)
{
- fflush(stdout);
+ if (ccdbg_level & level)
+ fflush(stdout);
}
ccdbg_clear_debug(int level);
void
-ccdbg_flush(void);
+ccdbg_flush(int level);
#endif /* _CCDBG_DEBUG_H_ */
ccdbg_debug(CC_DEBUG_FLASH, "Upload %d bytes at 0x%04x\n", this_time, ram_addr);
ccdbg_write_memory(dbg, ram_addr, image->data + start, this_time);
-
+#if 0
ccdbg_debug(CC_DEBUG_FLASH, "Verify %d bytes in ram\n", this_time);
ccdbg_read_memory(dbg, ram_addr, verify, this_time);
if (memcmp (image->data + start, verify, this_time) != 0) {
fprintf(stderr, "ram verify failed\n");
return 1;
}
+#endif
flash_addr = image->address + start;
flash_word_addr = flash_addr >> 1;
status = ccdbg_resume(dbg);
for (times = 0; times < 10; times++) {
status = ccdbg_read_status(dbg);
- ccdbg_debug(CC_DEBUG_FLASH, "chip status is 0x%02x\n", status);
+ ccdbg_debug(CC_DEBUG_FLASH, ".");
+ ccdbg_flush(CC_DEBUG_FLASH);
if ((status & CC_STATUS_CPU_HALTED) != 0)
break;
+ usleep(10000);
}
+ ccdbg_debug(CC_DEBUG_FLASH, "\n");
if (times == 10) {
fprintf(stderr, "flash page timed out\n");
return 1;
*/
static uint8_t memory_init[] = {
- 2, MOV_direct_A, 0x7f,
- 3, MOV_direct1_direct2, 0x7e, SFR_DPL0,
- 3, MOV_direct1_direct2, 0x7d, SFR_DPH0,
- 3, MOV_direct1_direct2, 0x7c, SFR_DPL1,
- 3, MOV_direct1_direct2, 0x7b, SFR_DPH1,
3, MOV_DPTR_data16, 0, 0,
-#define HIGH_START 21
-#define LOW_START 22
+#define HIGH_START 2
+#define LOW_START 3
0,
};
0,
};
-static uint8_t memory_fini[] = {
- 2, MOV_A_direct, 0x7f,
- 3, MOV_direct1_direct2, SFR_DPL0, 0x7e,
- 3, MOV_direct1_direct2, SFR_DPH0, 0x7d,
- 3, MOV_direct1_direct2, SFR_DPL1, 0x7c,
- 3, MOV_direct1_direct2, SFR_DPH1, 0x7b,
- 0,
-};
-
uint8_t
ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
{
int i, nl = 0;
+ struct ccstate state;
+
+ 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);
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) {
nl = 0;
}
}
- (void) ccdbg_execute(dbg, memory_fini);
+ ccdbg_state_restore(dbg, &state);
if (nl)
ccdbg_debug(CC_DEBUG_MEMORY, "\n");
return 0;
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;
+ }
+ 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);
*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) {
nl = 0;
}
}
- (void) ccdbg_execute(dbg, memory_fini);
+ ccdbg_state_replace_xmem(dbg, &state, addr, bytes, nbytes);
+ ccdbg_state_restore(dbg, &state);
if (nl)
ccdbg_debug(CC_DEBUG_MEMORY, "\n");
return 0;
return image;
}
-static uint8_t sfr_init[] = {
- 2, MOV_direct_A, 0x7f,
- 0,
-};
-
-static uint8_t sfr_fini[] = {
- 2, MOV_A_direct, 0x7f,
- 0,
-};
-
static uint8_t sfr_read[] = {
2, MOV_A_direct, 0,
#define SFR_READ_ADDR 2
ccdbg_read_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes)
{
int i;
- (void) ccdbg_execute(dbg, sfr_init);
+ 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);
}
- (void) ccdbg_execute(dbg, sfr_fini);
+ ccdbg_state_replace_sfr(dbg, &state, addr, bytes, nbytes);
+ ccdbg_state_restore(dbg, &state);
return 0;
}
--- /dev/null
+/*
+ * Copyright © 2008 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include "ccdbg.h"
+
+uint8_t
+ccdbg_set_rom(struct ccdbg *dbg, struct hex_image *rom)
+{
+ if (dbg->rom)
+ ccdbg_hex_image_free(dbg->rom);
+ dbg->rom = rom;
+ return 0;
+}
+
+uint8_t
+ccdbg_rom_contains(struct ccdbg *dbg, uint16_t addr, int nbytes)
+{
+ struct hex_image *rom = dbg->rom;
+ if (!rom)
+ return 0;
+ if (addr < rom->address || rom->address + rom->length < addr + nbytes)
+ return 0;
+ return 1;
+}
+
+uint8_t
+ccdbg_rom_replace_xmem(struct ccdbg *dbg,
+ uint16_t addr, uint8_t *bytes, int nbytes)
+{
+ struct hex_image *rom = dbg->rom;
+ if (!rom)
+ return 0;
+
+ if (rom->address < addr + nbytes && addr < rom->address + rom->length) {
+ int start, stop;
+
+ start = addr;
+ if (addr < rom->address)
+ start = rom->address;
+ stop = addr + nbytes;
+ if (rom->address + rom->length < stop)
+ stop = rom->address + rom->length;
+ memcpy(bytes + start - addr, rom->data + start - rom->address,
+ stop - start);
+ return 1;
+ }
+ return 0;
+}
--- /dev/null
+/*
+ * Copyright © 2008 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include "ccdbg.h"
+
+static uint8_t save_acc[] = {
+ 1, NOP,
+ 0
+};
+
+static uint8_t save_sfr[] = {
+ 2, MOV_A_direct, 0,
+#define SAVE_SFR_ADDR 2
+ 0,
+};
+
+struct sfr_state {
+ uint8_t address;
+ uint16_t mask;
+ char *name;
+};
+
+static struct sfr_state sfrs[CC_STATE_NSFR] = {
+ { SFR_DPL0, CC_STATE_DP, "dpl0" },
+ { SFR_DPH0, CC_STATE_DP, "dph0" },
+ { SFR_DPL1, CC_STATE_DP, "dpl1" },
+ { SFR_DPH1, CC_STATE_DP, "dph1" },
+ { PSW(0), CC_STATE_PSW, "psw" },
+};
+
+uint8_t
+ccdbg_state_save(struct ccdbg *dbg, struct ccstate *state, unsigned int mask)
+{
+ int i;
+
+ mask |= CC_STATE_ACC;
+ if (mask & CC_STATE_ACC)
+ state->acc = ccdbg_execute(dbg, save_acc);
+ for (i = 0; i < CC_STATE_NSFR; i++) {
+ if (sfrs[i].mask & mask) {
+ save_sfr[SAVE_SFR_ADDR] = sfrs[i].address;
+ state->sfr[i] = ccdbg_execute(dbg, save_sfr);
+ }
+ }
+ state->mask = mask;
+ return 0;
+}
+
+static uint8_t restore_sfr[] = {
+ 3, MOV_direct_data, 0, 0,
+#define RESTORE_SFR_ADDR 2
+#define RESTORE_SFR_DATA 3
+ 0
+};
+
+static uint8_t restore_acc[] = {
+ 2, MOV_A_data, 0,
+#define RESTORE_ACC_DATA 2
+ 0
+};
+
+uint8_t
+ccdbg_state_restore(struct ccdbg *dbg, struct ccstate *state)
+{
+ int i;
+ for (i = CC_STATE_NSFR - 1; i >= 0; i--) {
+ if (sfrs[i].mask & state->mask) {
+ restore_sfr[RESTORE_SFR_ADDR] = sfrs[i].address;
+ restore_sfr[RESTORE_SFR_DATA] = state->sfr[i];
+ ccdbg_execute(dbg, restore_sfr);
+ }
+ }
+ if (state->mask & CC_STATE_ACC) {
+ restore_acc[RESTORE_ACC_DATA] = state->acc;
+ ccdbg_execute(dbg, restore_acc);
+ }
+ state->mask = 0;
+ return 0;
+}
+
+static void
+ccdbg_state_replace(uint16_t sfr_addr, uint8_t sfr, char *name,
+ uint16_t addr, uint8_t *bytes, int nbytes)
+{
+ sfr_addr += 0xdf00;
+
+ if (addr <= sfr_addr && sfr_addr < addr + nbytes) {
+ fprintf(stderr, "replacing %s at 0x%04x - read 0x%02x saved 0x%02x\n",
+ name, sfr_addr, bytes[sfr_addr - addr], sfr);
+ bytes[sfr_addr - addr] = sfr;
+ }
+}
+
+void
+ccdbg_state_replace_xmem(struct ccdbg *dbg, struct ccstate *state,
+ uint16_t addr, uint8_t *bytes, int nbytes)
+{
+ int i;
+ if (state->mask & CC_STATE_ACC)
+ ccdbg_state_replace(ACC(0), state->acc, "acc",
+ addr, bytes, nbytes);
+ for (i = 0; i < CC_STATE_NSFR; i++)
+ if (state->mask & sfrs[i].mask)
+ ccdbg_state_replace(sfrs[i].address, state->sfr[i],
+ sfrs[i].name, addr, bytes, nbytes);
+}
+
+void
+ccdbg_state_replace_sfr(struct ccdbg *dbg, struct ccstate *state,
+ uint8_t addr, uint8_t *bytes, int nbytes)
+{
+ ccdbg_state_replace_xmem(dbg, state, (uint16_t) addr + 0xdf00, bytes, nbytes);
+}
/* 8051 instructions
*/
+#define NOP 0x00
#define MOV_direct_data 0x75
#define LJMP 0x02
#define MOV_Rn_data(n) (0x78 | (n))
/* Bit-addressable accumulator */
#define ACC(bit) (0xE0 | (bit))
+/* Bit-addressable status word */
+#define PSW(bit) (0xD0 | (bit))
+
#define CP_USB_ASYNC
struct ccdbg {
#else
struct cp_usb *cp;
#endif
+ struct hex_image *rom;
};
/* Intel hex file format data
uint8_t data[0];
};
+#define CC_STATE_ACC 0x1
+#define CC_STATE_PSW 0x2
+#define CC_STATE_DP 0x4
+
+#define CC_STATE_NSFR 5
+
+struct ccstate {
+ uint16_t mask;
+ uint8_t acc;
+ uint8_t sfr[CC_STATE_NSFR];
+};
+
#define HEX_RECORD_NORMAL 0x00
#define HEX_RECORD_EOF 0x01
#define HEX_RECORD_EXTENDED_ADDRESS 0x02
uint8_t
ccdbg_write_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes);
+/* ccdbg-rom.c */
+uint8_t
+ccdbg_set_rom(struct ccdbg *dbg, struct hex_image *rom);
+
+uint8_t
+ccdbg_rom_contains(struct ccdbg *dbg, uint16_t addr, int nbytes);
+
+uint8_t
+ccdbg_rom_replace_xmem(struct ccdbg *dbg,
+ uint16_t addrp, uint8_t *bytesp, int nbytes);
+
+/* ccdbg-state.c */
+uint8_t
+ccdbg_state_save(struct ccdbg *dbg, struct ccstate *state, unsigned int mask);
+
+uint8_t
+ccdbg_state_restore(struct ccdbg *dbg, struct ccstate *state);
+
+void
+ccdbg_state_replace_xmem(struct ccdbg *dbg, struct ccstate *state,
+ uint16_t addr, uint8_t *bytes, int nbytes);
+
+void
+ccdbg_state_replace_sfr(struct ccdbg *dbg, struct ccstate *state,
+ uint8_t addr, uint8_t *bytes, int nbytes);
+
#endif /* _CCDBG_H_ */
}
static void
-dump_bytes(uint8_t *memory, int length, uint16_t start)
+dump_bytes(uint8_t *memory, int length, uint16_t start, char *format)
{
int group, i;
for (group = 0; group < length; group += 8) {
- s51_printf("0x%04x ", start + group);
+ s51_printf(format, start + group);
for (i = group; i < length && i < group + 8; i++)
s51_printf("%02x ", memory[i]);
for (; i < group + 8; i++)
return command_error;
length = (int) end - (int) start + 1;
status = ccdbg_read_memory(s51_dbg, start + 0xff00, memory, length);
- dump_bytes(memory, length, start);
+ dump_bytes(memory, length, start, "0x%02x ");
return command_success;
}
return command_error;
length = (int) end - (int) start + 1;
status = ccdbg_read_sfr(s51_dbg, start, memory, length);
- dump_bytes(memory, length, start);
+ dump_bytes(memory, length, start, "0x%02x ");
return command_success;
}
return command_error;
length = (int) end - (int) start + 1;
status = ccdbg_read_memory(s51_dbg, start, memory, length);
- dump_bytes(memory, length, start);
+ dump_bytes(memory, length, start, "0x%04x ");
return command_success;
}
command_file (int argc, char **argv)
{
struct hex_file *hex;
+ struct hex_image *image;
FILE *file;
if (argc != 2)
ccdbg_hex_file_free(hex);
return command_error;
}
- start_address = hex->records[0]->address;
+ image = ccdbg_hex_image_create(hex);
+ ccdbg_hex_file_free(hex);
+ start_address = image->address;
+ ccdbg_set_rom(s51_dbg, image);
return command_success;
}
return command_success;
}
+enum command_result
+command_stop (int argc, char **argv)
+{
+ return command_success;
+}
+
enum command_result
command_reset (int argc, char **argv)
{
return command_success;
}
+static enum command_result
+info_breakpoints(int argc, char **argv)
+{
+ int b;
+ uint16_t address;
+ enum command_result result;
+
+ if (argc == 1) {
+ s51_printf("Num Type Disp Hit Cnt Address What\n");
+ for (b = 0; b < CC_NUM_BREAKPOINTS; b++)
+ if (breakpoints[b].enabled) {
+ s51_printf("%-3d fetch %s 1 1 0x%04x uc::disass() unimplemented\n",
+ b,
+ breakpoints[b].temporary ? "del " : "keep",
+ breakpoints[b].address);
+ }
+ return command_success;
+ }
+
+}
+
+static enum command_result
+info_help(int argc, char **argv);
+
+static struct command_function infos[] = {
+ { "breakpoints", "b", info_breakpoints, "[b]reakpoints",
+ "List current breakpoints\n" },
+ { "help", "?", info_help, "help",
+ "Print this list\n" },
+
+ { NULL, NULL, NULL, NULL, NULL },
+};
+
+static enum command_result
+info_help(int argc, char **argv)
+{
+ return command_function_help(infos, argc, argv);
+}
+
+enum command_result
+command_info(int argc, char **argv)
+{
+ struct command_function *func;
+
+ if (argc < 2)
+ return command_error;
+ func = command_string_to_function(infos, argv[1]);
+ if (!func)
+ return command_syntax;
+ return (*func->func)(argc-1, argv+1);
+}
+
enum command_result
cc_wait(void)
{
#include "s51.h"
-struct command_function {
- char *name;
- char *alias;
- enum command_result (*func)(int argc, char **argv);
- char *usage;
- char *help;
-};
-
static struct command_function functions[] = {
{ "help", "?", command_help, "help", "Print this list\n" },
{ "quit", "q", command_quit, "[q]uit", "Quit\n" },
"Clear break point\n" },
{ "run", "r", command_run, "[r]un [start] [stop]",
"Run with optional start and temp breakpoint addresses\n" },
+ { "go", "g", command_run, "[g]o [start] [stop]",
+ "Run with optional start and temp breakpoint addresses\n" },
{ "next", "n", command_next, "[n]ext",
"Step over one instruction, past any call\n" },
{ "step", "s", command_step, "[s]tep",
"Reset the CPU\n" },
{ "status","status",command_status, "status",
"Display CC1111 debug status\n" },
+ { "info", "i", command_info, "[i]info",
+ "Get information\n" },
+ { "stop", "stop", command_stop, "stop",
+ "Ignored\n" },
+ { NULL, NULL, NULL, NULL, NULL },
};
-#define NUM_FUNCTIONS (sizeof functions / sizeof functions[0])
-
#ifndef FALSE
#define FALSE 0
#define TRUE 1
return TRUE;
}
-static struct command_function *
-command_string_to_function(char *name)
+struct command_function *
+command_string_to_function(struct command_function *functions, char *name)
{
int i;
- for (i = 0; i < NUM_FUNCTIONS; i++)
+ for (i = 0; functions[i].name; i++)
if (!strcmp(name, functions[i].name) ||
!strcmp(name, functions[i].alias))
return &functions[i];
return NULL;
}
+enum command_result
+command_function_help(struct command_function *functions, int argc, char **argv)
+{
+ int i;
+ struct command_function *func;
+
+ if (argc == 1) {
+ for (i = 0; functions[i].name; i++)
+ s51_printf("%-10s%s\n", functions[i].name,
+ functions[i].usage);
+ } else {
+ for (i = 1; i < argc; i++) {
+ func = command_string_to_function(functions, argv[i]);
+ if (!func) {
+ s51_printf("%-10s unknown command\n", argv[i]);
+ return command_syntax;
+ }
+ s51_printf("%-10s %s\n%s", func->name,
+ func->usage, func->help);
+ }
+ }
+ return command_debug;
+}
+
static int
command_split_into_words(char *line, char **argv)
{
enum command_result
command_help(int argc, char **argv)
{
- int i;
- struct command_function *func;
-
- if (argc == 1) {
- for (i = 0; i < NUM_FUNCTIONS; i++)
- s51_printf("%-10s%s\n", functions[i].name,
- functions[i].usage);
- } else {
- for (i = 1; i < argc; i++) {
- func = command_string_to_function(argv[i]);
- if (!func) {
- s51_printf("%-10s unknown command\n", argv[i]);
- return command_syntax;
- }
- s51_printf("%-10s %s\n%s", func->name,
- func->usage, func->help);
- }
- }
- return command_debug;
+ return command_function_help(functions, argc, argv);
}
-static void
+void
command_syntax_error(int argc, char **argv)
{
s51_printf("Syntax error in:");
s51_interrupted = 0;
argc = command_split_into_words(line, argv);
if (argc > 0) {
- func = command_string_to_function(argv[0]);
+ func = command_string_to_function(functions, argv[0]);
if (!func)
command_syntax_error(argc, argv);
else
command_success, command_debug, command_syntax, command_interrupt, command_error,
};
+struct command_function {
+ char *name;
+ char *alias;
+ enum command_result (*func)(int argc, char **argv);
+ char *usage;
+ char *help;
+};
+
+struct command_function *
+command_string_to_function(struct command_function *functions, char *name);
+
+enum command_result
+command_function_help(struct command_function *functions, int argc, char **argv);
+
+void
+command_syntax_error(int argc, char **argv);
+
enum command_result
command_quit (int argc, char **argv);
enum command_result
command_help (int argc, char **argv);
+enum command_result
+command_stop (int argc, char **argv);
+
enum command_result
command_di (int argc, char **argv);
enum command_result
command_status (int argc, char **argv);
+enum command_result
+command_info (int argc, char **argv);
+
enum command_result
cc_wait(void);