2 * Copyright © 2013 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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 #include "ao-stmload.h"
30 ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
34 cc_usb_printf(cc, "R %x\n", address);
35 // printf ("read %08x\n", address);
36 for (byte = 0; byte < 0x100; byte++) {
37 block[byte] = cc_usb_getchar(cc);
38 // printf (" %02x", block[byte]);
39 // if ((byte & 0xf) == 0xf)
45 ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
49 cc_usb_printf(cc, "W %x\n", address);
50 // printf ("write %08x\n", address);
51 for (byte = 0; byte < 0x100; byte++) {
52 cc_usb_printf(cc, "%c", block[byte]);
53 // printf (" %02x", block[byte]);
54 // if ((byte & 0xf) == 0xf)
60 ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
62 struct hex_image *image;
65 uint32_t base = address & ~0xff;
66 uint32_t bound = (address + length + 0xff) & ~0xff;
68 image = calloc(sizeof (struct hex_image) + (bound - base), 1);
69 image->address = base;
70 image->length = bound - base;
71 pages = image->length / 0x100;
72 for (page = 0; page < pages; page++)
73 ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
78 ao_self_write(struct cc_usb *cc, struct hex_image *image)
82 uint32_t base, bound, length, address;
86 base = image->address & ~0xff;
87 bound = (image->address + image->length + 0xff) & ~0xff;
90 length = bound - base;
92 pages = length / 0x100;
93 printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
94 for (page = 0; page < pages; page++) {
96 address = base + page * 0x100;
98 if (address < image->address || address + 0x100 > image->address + image->length) {
99 ao_self_block_read(cc, address, block);
102 stop = address + 0x100;
103 if (start < image->address)
104 start = image->address;
105 if (stop > image->address + image->length)
106 stop = image->address + image->length;
107 memcpy(block + start - address, image->data + start - image->address, stop - start);
108 ao_self_block_write(cc, address, block);
109 ao_self_block_read(cc, address, check);
110 if (memcmp(block, check, 0x100) != 0) {
111 fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
114 putchar('.'); fflush(stdout);