2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 ccdbg_chip_erase(struct ccdbg *dbg)
24 return ccdbg_cmd_write_read8(dbg, CC_CHIP_ERASE, NULL, 0);
28 ccdbg_wr_config(struct ccdbg *dbg, uint8_t config)
30 return ccdbg_cmd_write_read8(dbg, CC_WR_CONFIG, &config, 1);
34 ccdbg_rd_config(struct ccdbg *dbg)
36 return ccdbg_cmd_write_read8(dbg, CC_RD_CONFIG, NULL, 0);
40 ccdbg_get_pc(struct ccdbg *dbg)
44 pc1 = ccdbg_cmd_write_read16(dbg, CC_GET_PC, NULL, 0);
45 pc2 = ccdbg_cmd_write_read16(dbg, CC_GET_PC, NULL, 0);
47 fprintf (stderr, "Invalid pc %04x != %04x\n",
53 ccdbg_read_status(struct ccdbg *dbg)
55 return ccdbg_cmd_write_read8(dbg, CC_READ_STATUS, NULL, 0);
59 ccdbg_set_hw_brkpnt(struct ccdbg *dbg, uint8_t number, uint8_t enable, uint16_t addr)
63 data[0] = (number << 3) | (enable << 2);
64 data[1] = (addr >> 8);
66 return ccdbg_cmd_write_read8(dbg, CC_SET_HW_BRKPNT, data, 3);
70 ccdbg_halt(struct ccdbg *dbg)
72 return ccdbg_cmd_write_read8(dbg, CC_HALT, NULL, 0);
76 ccdbg_resume(struct ccdbg *dbg)
78 return ccdbg_cmd_write_read8(dbg, CC_RESUME, NULL, 0);
82 ccdbg_debug_instr(struct ccdbg *dbg, uint8_t *instr, int nbytes)
84 return ccdbg_cmd_write_read8(dbg, CC_DEBUG_INSTR(nbytes), instr, nbytes);
88 ccdbg_debug_instr_discard(struct ccdbg *dbg, uint8_t *instr, int nbytes)
90 static uint8_t discard;
91 ccdbg_cmd_write_queue8(dbg, CC_DEBUG_INSTR(nbytes),
92 instr, nbytes, &discard);
96 ccdbg_debug_instr_queue(struct ccdbg *dbg, uint8_t *instr, int nbytes,
99 return ccdbg_cmd_write_queue8(dbg, CC_DEBUG_INSTR(nbytes),
100 instr, nbytes, reply);
104 ccdbg_step_instr(struct ccdbg *dbg)
106 return ccdbg_cmd_write_read8(dbg, CC_STEP_INSTR, NULL, 0);
110 ccdbg_step_replace(struct ccdbg *dbg, uint8_t *instr, int nbytes)
112 return ccdbg_cmd_write_read8(dbg, CC_STEP_REPLACE(nbytes), instr, nbytes);
116 ccdbg_get_chip_id(struct ccdbg *dbg)
118 return ccdbg_cmd_write_read16(dbg, CC_GET_CHIP_ID, NULL, 0);
122 * Execute a sequence of instructions
125 ccdbg_execute(struct ccdbg *dbg, uint8_t *inst)
128 while(inst[0] != 0) {
129 uint8_t len = inst[0];
131 ccdbg_debug(CC_DEBUG_INSTRUCTIONS, "\t%02x", inst[1]);
132 for (i = 0; i < len - 1; i++)
133 ccdbg_debug(CC_DEBUG_INSTRUCTIONS, " %02x", inst[i+2]);
134 ccdbg_debug_instr_queue(dbg, inst+1, len, &status);
136 ccdbg_debug(CC_DEBUG_INSTRUCTIONS, " ");
137 ccdbg_debug(CC_DEBUG_INSTRUCTIONS, " -> %02x\n", status);
144 static uint8_t jump_mem[] = {
152 ccdbg_set_pc(struct ccdbg *dbg, uint16_t pc)
154 jump_mem[PC_HIGH] = pc >> 8;
155 jump_mem[PC_LOW] = pc & 0xff;
156 return ccdbg_execute(dbg, jump_mem);
160 ccdbg_execute_hex_image(struct ccdbg *dbg, struct hex_image *image)
165 if (image->address < 0xf000) {
166 fprintf(stderr, "Cannot execute program starting at 0x%04x\n", image->address);
169 ccdbg_write_hex_image(dbg, image, 0);
170 ccdbg_set_pc(dbg, image->address);
171 pc = ccdbg_get_pc(dbg);
172 ccdbg_debug(CC_DEBUG_EXECUTE, "pc starts at 0x%04x\n", pc);
173 status = ccdbg_resume(dbg);
174 ccdbg_debug(CC_DEBUG_EXECUTE, "resume status: 0x%02x\n", status);