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 * Read and write arbitrary memory through the debug port
25 static uint8_t memory_init[] = {
26 3, MOV_DPTR_data16, 0, 0,
33 static uint8_t write8[] = {
41 static uint8_t read8[] = {
48 ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
51 memory_init[HIGH_START] = addr >> 8;
52 memory_init[LOW_START] = addr;
53 (void) ccdbg_execute(dbg, memory_init);
54 for (i = 0; i < nbytes; i++) {
55 write8[DATA_BYTE] = *bytes++;
56 ccdbg_execute(dbg, write8);
57 if ((i & 0xf) == 0xf) {
58 ccdbg_debug(CC_DEBUG_MEMORY, ".");
62 if ((i & 0xff) == 0xff) {
63 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
68 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
73 ccdbg_read_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
76 memory_init[HIGH_START] = addr >> 8;
77 memory_init[LOW_START] = addr;
78 (void) ccdbg_execute(dbg, memory_init);
79 for (i = 0; i < nbytes; i++) {
80 *bytes++ = ccdbg_execute(dbg, read8);
81 if ((i & 0xf) == 0xf) {
82 ccdbg_debug(CC_DEBUG_MEMORY, ".");
86 if ((i & 0xff) == 0xff) {
87 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
92 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
97 ccdbg_write_uint8(struct ccdbg *dbg, uint16_t addr, uint8_t byte)
99 return ccdbg_write_memory(dbg, addr, &byte, 1);
103 ccdbg_write_hex_image(struct ccdbg *dbg, struct hex_image *image, uint16_t offset)
105 ccdbg_write_memory(dbg, image->address + offset, image->data, image->length);
110 ccdbg_read_hex_image(struct ccdbg *dbg, uint16_t address, uint16_t length)
112 struct hex_image *image;
114 image = calloc(sizeof(struct hex_image) + length, 1);
115 image->address = address;
116 image->length = length;
117 memset(image->data, 0xff, length);
118 ccdbg_read_memory(dbg, address, image->data, length);