2 * Tool for USB serial communication with Krikzz's FlashKit-MD
3 * Copyright (c) 2017 notaz
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <sys/types.h>
34 #include <arpa/inet.h> // hton
39 #define min(x, y) ((x) < (y) ? (x) : (y))
40 #define max(x, y) ((x) > (y) ? (x) : (y))
51 #define PAR_MODE8 (1 << 4) /* but still drives noth LWR and UWR */
52 #define PAR_DEV_ID (1 << 5)
53 #define PAR_SINGE (1 << 6)
54 #define PAR_INC (1 << 7)
56 static struct flash_info {
70 static int setup(int fd)
75 memset(&tty, 0, sizeof(tty));
77 ret = tcgetattr(fd, &tty);
84 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
85 | INLCR | IGNCR | ICRNL | IXON);
86 tty.c_oflag &= ~OPOST;
87 tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
88 tty.c_cflag &= ~(CSIZE | PARENB);
92 //tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
94 ret = tcsetattr(fd, TCSANOW, &tty);
103 static int write_serial(int fd, const void *data, size_t size)
107 ret = write(fd, data, size);
109 fprintf(stderr, "write %d/%zd: ", ret, size);
117 static int read_serial(int fd, void *data, size_t size)
123 ret = read(fd, (char *)data + got, size - got);
125 fprintf(stderr, "read %d %zd/%zd: ",
136 /* addr arg is always byte address */
137 static void set_addr8(int fd, uint32_t addr)
140 CMD_ADDR, addr >> 16,
144 write_serial(fd, cmd, sizeof(cmd));
147 static void set_addr16(int fd, uint32_t addr)
149 set_addr8(fd, addr >> 1);
152 static uint16_t read_bus8(int fd, uint32_t addr)
155 CMD_ADDR, addr >> 16,
158 CMD_RD | PAR_SINGE | PAR_MODE8
162 write_serial(fd, cmd, sizeof(cmd));
163 read_serial(fd, &r, sizeof(r));
167 static uint16_t read_bus16(int fd, uint32_t addr)
170 CMD_ADDR, addr >> 17,
177 write_serial(fd, cmd, sizeof(cmd));
178 read_serial(fd, &r, sizeof(r));
182 static void write_bus8(int fd, uint32_t addr, uint16_t d)
185 CMD_ADDR, addr >> 16,
188 CMD_WR | PAR_SINGE | PAR_MODE8,
192 write_serial(fd, cmd, sizeof(cmd));
195 static void write_bus16(int fd, uint32_t addr, uint16_t d)
198 CMD_ADDR, addr >> 17,
205 write_serial(fd, cmd, sizeof(cmd));
208 static void read_block8(int fd, void *dst, uint32_t size)
210 // PAR_MODE8 does not work, so read as 16bit and throw away MSB
211 uint8_t tmp[0x10000], *d8 = dst;
219 assert(size <= 0x10000);
220 write_serial(fd, cmd, sizeof(cmd));
221 read_serial(fd, dst, size);
222 read_serial(fd, tmp, size);
224 for (i = 0; i < size / 2; i++)
225 d8[i] = d8[i * 2 + 1];
227 for (i = 0; i < size / 2; i++)
228 d8[i] = tmp[i * 2 + 1];
231 static void read_block16(int fd, void *dst, uint32_t size)
239 assert(size <= 0x10000);
240 write_serial(fd, cmd, sizeof(cmd));
241 read_serial(fd, dst, size);
244 static void flash_seq_write8(int fd, uint32_t addr, const uint8_t *d)
248 CMD_ADDR, info.prog_addr >> 16,
249 CMD_ADDR, info.prog_addr >> 8,
250 CMD_ADDR, info.prog_addr >> 0,
251 CMD_WR | PAR_SINGE | PAR_MODE8, 0xaa,
252 CMD_ADDR, info.prog_addr >> 17,
253 CMD_ADDR, info.prog_addr >> 9,
254 CMD_ADDR, info.prog_addr >> 1,
255 CMD_WR | PAR_SINGE | PAR_MODE8, 0x55,
257 CMD_ADDR, info.prog_addr >> 16,
258 CMD_ADDR, info.prog_addr >> 8,
259 CMD_ADDR, info.prog_addr >> 0,
260 CMD_WR | PAR_SINGE | PAR_MODE8, 0xa0,
262 CMD_ADDR, addr >> 16,
265 CMD_WR | PAR_SINGE | PAR_MODE8, *d,
269 assert(info.prog_addr);
270 write_serial(fd, cmd, sizeof(cmd));
273 static void flash_seq_write16(int fd, uint32_t addr, const uint8_t *d)
277 CMD_ADDR, info.prog_addr >> 17,
278 CMD_ADDR, info.prog_addr >> 9,
279 CMD_ADDR, info.prog_addr >> 1,
280 CMD_WR | PAR_SINGE | PAR_MODE8, 0xaa,
281 CMD_ADDR, info.prog_addr >> 18,
282 CMD_ADDR, info.prog_addr >> 10,
283 CMD_ADDR, info.prog_addr >> 2,
284 CMD_WR | PAR_SINGE | PAR_MODE8, 0x55,
286 CMD_ADDR, info.prog_addr >> 17,
287 CMD_ADDR, info.prog_addr >> 9,
288 CMD_ADDR, info.prog_addr >> 1,
289 CMD_WR | PAR_SINGE | PAR_MODE8, 0xa0,
291 CMD_ADDR, addr >> 17,
294 CMD_WR | PAR_SINGE, d[0], d[1],
298 assert(info.prog_addr);
299 write_serial(fd, cmd, sizeof(cmd));
304 static uint32_t lorom_rom_addr(uint32_t a)
306 return ((a & 0x7f8000) << 1) | 0x8000 | (a & 0x7fff);
309 static void set_addr8l(int fd, uint32_t a)
311 set_addr8(fd, lorom_rom_addr(a));
314 static uint16_t read_bus8l(int fd, uint32_t a)
316 return read_bus8(fd, lorom_rom_addr(a));
319 static void write_bus8l(int fd, uint32_t a, uint16_t d)
321 write_bus8(fd, lorom_rom_addr(a), d);
324 static void flash_seq_write8l(int fd, uint32_t a, const uint8_t *d)
326 a = lorom_rom_addr(a);
332 CMD_WR | PAR_SINGE | PAR_MODE8, 0xaa,
336 CMD_WR | PAR_SINGE | PAR_MODE8, 0x55,
341 CMD_WR | PAR_SINGE | PAR_MODE8, 0xa0,
346 CMD_WR | PAR_SINGE | PAR_MODE8, *d,
350 write_serial(fd, cmd, sizeof(cmd));
353 // -- 8bit+LoROM+adapter --
355 static uint32_t do_flipflops(int fd, uint32_t a)
357 static uint32_t abits_now = ~0u; // A23, A22, A21
358 uint32_t abits = (a >> 21) & 7;
360 if (abits != abits_now) {
361 // printf("flipflops: %x->%x\n", abits_now, abits);
362 write_bus16(fd, 0xa13000, abits);
368 static void set_addr8la(int fd, uint32_t a)
370 set_addr8(fd, do_flipflops(fd, lorom_rom_addr(a)));
373 static uint16_t read_bus8la(int fd, uint32_t a)
375 return read_bus8(fd, do_flipflops(fd, lorom_rom_addr(a)));
378 static void write_bus8la(int fd, uint32_t a, uint16_t d)
380 write_bus8(fd, do_flipflops(fd, lorom_rom_addr(a)), d);
383 static void flash_seq_write8la(int fd, uint32_t a, const uint8_t *d)
385 // we should clear flipflops for the flash commands, but this
386 // doesn't seem to be necessary as the flash chip seems to
387 // ignore the upper bits when looking for commands, and this
388 // extra clearing would slow things down
389 a = do_flipflops(fd, lorom_rom_addr(a));
395 CMD_WR | PAR_SINGE | PAR_MODE8, 0xaa,
399 CMD_WR | PAR_SINGE | PAR_MODE8, 0x55,
404 CMD_WR | PAR_SINGE | PAR_MODE8, 0xa0,
409 CMD_WR | PAR_SINGE | PAR_MODE8, *d,
413 write_serial(fd, cmd, sizeof(cmd));
416 // -- 8bit+LoROM+adapter+sram --
418 static uint32_t lorom_sram_addr(uint32_t a)
423 static void set_addr8las(int fd, uint32_t a)
425 set_addr8(fd, do_flipflops(fd, lorom_sram_addr(a)));
428 static uint16_t read_bus8las(int fd, uint32_t a)
430 return read_bus8(fd, do_flipflops(fd, lorom_sram_addr(a)));
433 static void write_bus8las(int fd, uint32_t a, uint16_t d)
435 write_bus8(fd, do_flipflops(fd, lorom_sram_addr(a)), d);
438 static void flash_seq_write8las(int fd, uint32_t a, const uint8_t *d)
442 // -- 8bit+adapter --
444 static void set_addr8a(int fd, uint32_t a)
446 set_addr8(fd, do_flipflops(fd, a));
449 static uint16_t read_bus8a(int fd, uint32_t a)
451 return read_bus8(fd, do_flipflops(fd, a));
454 static void write_bus8a(int fd, uint32_t a, uint16_t d)
456 write_bus8(fd, do_flipflops(fd, a), d);
459 static void flash_seq_write8a(int fd, uint32_t a, const uint8_t *d)
461 // no flipflop clearing, see flash_seq_write8la
462 a = do_flipflops(fd, a);
468 CMD_WR | PAR_SINGE | PAR_MODE8, 0xaa,
472 CMD_WR | PAR_SINGE | PAR_MODE8, 0x55,
477 CMD_WR | PAR_SINGE | PAR_MODE8, 0xa0,
482 CMD_WR | PAR_SINGE | PAR_MODE8, *d,
486 write_serial(fd, cmd, sizeof(cmd));
489 // -- 8bit+adapter+nocart --
491 static void set_addr8an(int fd, uint32_t a)
493 set_addr8(fd, do_flipflops(fd, a) | 0x200000);
496 static uint16_t read_bus8an(int fd, uint32_t a)
498 return read_bus8(fd, do_flipflops(fd, a) | 0x200000);
501 static void write_bus8an(int fd, uint32_t a, uint16_t d)
503 write_bus8(fd, do_flipflops(fd, a) | 0x200000, d);
506 static void flash_seq_write8an(int fd, uint32_t a, const uint8_t *d)
512 #define N2 "8bit+LoROM"
513 #define N3 "8bit+LoROM+adapter"
514 #define N4 "8bit+LoROM+adapter+sram"
515 #define N5 "8bit+adapter (use '-a 0x400000' for HiROM)"
516 #define N6 "8bit+adapter+nocart"
517 static const struct iof
521 void (*set_addr)(int fd, uint32_t addr);
522 uint16_t (*read_bus)(int fd, uint32_t addr);
523 void (*write_bus)(int fd, uint32_t addr, uint16_t d);
524 void (*read_block)(int fd, void *dst, uint32_t size);
525 void (*flash_seq_write)(int fd, uint32_t addr, const uint8_t *d);
529 { N0, 0, set_addr16, read_bus16, write_bus16, read_block16, flash_seq_write16 },
530 { N1, 0, set_addr8, read_bus8, write_bus8, read_block8, flash_seq_write8 },
531 { N2, 1, set_addr8l, read_bus8l, write_bus8l, read_block8, flash_seq_write8l },
532 { N3, 1, set_addr8la, read_bus8la, write_bus8la, read_block8, flash_seq_write8la },
533 { N4, 0, set_addr8las, read_bus8las, write_bus8las, read_block8, flash_seq_write8las },
534 { N5, 0, set_addr8a, read_bus8a, write_bus8a, read_block8, flash_seq_write8a },
535 { N6, 0, set_addr8an, read_bus8an, write_bus8an, read_block8, flash_seq_write8an },
538 static const struct iof *io = &io_ops[0];
540 static uint16_t flash_seq_r(int fd, uint8_t cmd, uint32_t addr)
543 assert(info.prog_addr);
544 io->write_bus(fd, info.prog_addr >> 0, 0xaa);
545 io->write_bus(fd, info.prog_addr >> 1, 0x55);
547 io->write_bus(fd, info.prog_addr >> 0, cmd);
548 return io->read_bus(fd, addr);
551 static void flash_seq_erase_d(int fd, uint32_t addr, uint8_t d)
553 // printf("erase %06x\n", addr);
554 assert(info.prog_addr);
555 io->write_bus(fd, info.prog_addr >> 0, 0xaa);
556 io->write_bus(fd, info.prog_addr >> 1, 0x55);
557 io->write_bus(fd, info.prog_addr >> 0, 0x80);
559 io->write_bus(fd, info.prog_addr >> 0, 0xaa);
560 io->write_bus(fd, info.prog_addr >> 1, 0x55);
561 io->write_bus(fd, addr, d);
564 static void flash_seq_erase(int fd, uint32_t addr)
566 flash_seq_erase_d(fd, addr, 0x30);
569 static void flash_seq_erase_full(int fd)
571 flash_seq_erase_d(fd, info.prog_addr, 0x10);
574 // status wait + dummy read to cause a wait?
575 static uint16_t ry_read(int fd)
577 uint8_t cmd[2] = { CMD_RY, CMD_RD | PAR_SINGE };
580 write_serial(fd, cmd, sizeof(cmd));
581 read_serial(fd, &rv, sizeof(rv));
585 static void set_delay(int fd, uint8_t delay)
587 uint8_t cmd[2] = { CMD_DELAY, delay };
589 write_serial(fd, cmd, sizeof(cmd));
592 static void read_info(int fd)
594 static const uint16_t qry[3] = { 'Q', 'R', 'Y' };
595 uint16_t resp_cfi[3], sst_mid = ~0;
600 // see if this chip understands CFI (common flash interface)
601 io->write_bus(fd, 0xaa, 0x98);
602 resp_cfi[0] = io->read_bus(fd, 0x20);
603 resp_cfi[1] = io->read_bus(fd, 0x22);
604 resp_cfi[2] = io->read_bus(fd, 0x24);
605 if (memcmp(resp_cfi, qry, sizeof(resp_cfi)) == 0)
607 info.size = 1u << io->read_bus(fd, 0x4e);
608 info.region_cnt = io->read_bus(fd, 0x58);
609 assert(0 < info.region_cnt && info.region_cnt <= 4);
610 for (i = 0, a = 0x5a; i < info.region_cnt; i++, a += 8) {
611 info.region[i].block_count = io->read_bus(fd, a + 0) + 1;
612 info.region[i].block_count += io->read_bus(fd, a + 2) << 8;
613 info.region[i].block_size = io->read_bus(fd, a + 4) << 8;
614 info.region[i].block_size |= io->read_bus(fd, a + 6) << 16;
615 info.region[i].start = total;
616 info.region[i].size =
617 info.region[i].block_size * info.region[i].block_count;
618 assert(info.region[i].size);
619 total += info.region[i].size;
621 if (info.size != total)
622 fprintf(stderr, "warning: total is %u, bad CFI?\n", total);
624 info.prog_addr = 0xaaa;
625 io->write_bus(fd, 0, 0xf0);
626 info.mid = flash_seq_r(fd, 0x90, 0); // autoselect
627 info.did = io->read_bus(fd, 2);
631 // try the SST protocol
632 info.prog_addr = 0x5555;
633 sst_mid = flash_seq_r(fd, 0x90, 0);
637 info.did = io->read_bus(fd, 0x01);
642 info.size = 128 * 1024 << (info.did - 0xb5);
645 fprintf(stderr, "unrecognized SST device %02x\n", info.did);
649 info.region[0].block_count = info.size / 4096;
650 info.region[0].block_size = 4096;
651 info.region[0].start = 0;
652 info.region[0].size = info.size;
658 io->write_bus(fd, 0, 0xf0); // flash reset
660 if (info.prog_addr == 0) {
661 fprintf(stderr, "unable to identify the flash chip :(\n");
662 fprintf(stderr, "CFI response: %02x %02x %02x\n",
663 resp_cfi[0], resp_cfi[1], resp_cfi[2]);
664 fprintf(stderr, "SST MID: %02x\n", sst_mid);
668 printf("Flash info:\n");
669 printf("Manufacturer ID: %04x\n", info.mid);
670 printf("Device ID: %04x\n", info.did);
671 printf("size: %u\n", info.size);
672 printf("Erase Block Regions: %u\n", info.region_cnt);
673 for (i = 0; i < info.region_cnt; i++)
674 printf(" %5u x %u\n", info.region[i].block_size,
675 info.region[i].block_count);
678 static uint32_t get_block_addr(uint32_t addr, uint32_t blk_offset)
680 uint32_t i, base, faddr;
682 assert(info.region_cnt);
685 // get a flash address to allow mapper hardware
686 faddr = addr & (info.size - 1);
687 base = addr & ~(info.size - 1);
689 for (i = 0; i < info.region_cnt; i++) {
690 if (info.region[i].start <= faddr
691 && faddr < info.region[i].start + info.region[i].size)
693 uint32_t blk = (faddr - info.region[i].start)
694 / info.region[i].block_size
696 return base + info.region[i].start
697 + blk * info.region[i].block_size;
701 fprintf(stderr, "\naddress out of range: 0x%x\n", addr);
705 static void print_progress(uint32_t done, uint32_t total)
709 printf("\r%06x/%06x |", done, total);
711 step = (total + 19) / 20;
712 for (i = step; i <= total; i += step)
713 fputc(done >= i ? '=' : '-', stdout);
714 printf("| %3d%%", done * 100 / total);
720 static FILE *open_prep_read(const char *fname, long *size)
722 FILE *f = fopen(fname, "rb");
724 fprintf(stderr, "fopen %s: ", fname);
729 fseek(f, 0, SEEK_END);
731 fseek(f, 0, SEEK_SET);
734 fprintf(stderr, "size of %s is %ld\n", fname, *size);
740 static const char *portname =
742 "/dev/cu.usbserial-AL0254JM";
747 static void usage(const char *argv0)
753 " -d <ttydevice> (default %s)\n"
754 " -r <file> [size] dump the cart (default 4MB)\n"
755 " -w <file> [size] program the flash (def. file size)\n"
756 " -s <file> [size] simple write (SRAM, etc, def. file size)\n"
757 " -e <size> erase (rounds to block size); can specify 'full'\n"
758 " -a <start_address> read/write start address (default 0)\n"
759 " -m <n> use an address mapper n, one of:\n"
761 for (i = 1; i < sizeof(io_ops) / sizeof(io_ops[0]); i++)
763 " %zd: %s\n", i, io_ops[i].name);
764 printf( " -v verify written data\n"
765 " -i get info about the flash chip\n");
769 static void invarg(int argc, char *argv[], int arg)
772 fprintf(stderr, "invalid arg %d: \"%s\"\n", arg, argv[arg]);
774 fprintf(stderr, "missing required argument %d\n", arg);
778 static void *getarg(int argc, char *argv[], int arg)
781 invarg(argc, argv, arg);
785 static long getarg_l(int argc, char *argv[], int arg)
791 invarg(argc, argv, arg);
792 r = strtol(argv[arg], &endp, 0);
793 if (endp == NULL || *endp != 0)
794 invarg(argc, argv, arg);
798 // 32K to easily handle SNES LoROM
799 static uint8_t g_block[0x8000];
800 static uint8_t g_block2[sizeof(g_block)];
802 int main(int argc, char *argv[])
804 const char *fname_w = NULL;
805 const char *fname_r = NULL;
806 const char *fname_ws = NULL;
812 long len, address_in = 0;
820 uint8_t id[2] = { 0, 0 };
826 if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
829 for (arg = 1; arg < argc; arg++) {
830 if (!strcmp(argv[arg], "-d")) {
831 portname = getarg(argc, argv, ++arg);
834 if (!strcmp(argv[arg], "-r")) {
835 fname_r = getarg(argc, argv, ++arg);
836 if (arg + 1 < argc && argv[arg + 1][0] != '-') {
837 size_r = getarg_l(argc, argv, ++arg);
839 invarg(argc, argv, arg);
843 if (!strcmp(argv[arg], "-w")) {
844 fname_w = getarg(argc, argv, ++arg);
845 if (arg + 1 < argc && argv[arg + 1][0] != '-') {
846 size_w = getarg_l(argc, argv, ++arg);
848 invarg(argc, argv, arg);
852 if (!strcmp(argv[arg], "-s")) {
853 fname_ws = getarg(argc, argv, ++arg);
854 if (arg + 1 < argc && argv[arg + 1][0] != '-') {
855 size_ws = getarg_l(argc, argv, ++arg);
857 invarg(argc, argv, arg);
861 if (!strcmp(argv[arg], "-a")) {
862 address_in = getarg_l(argc, argv, ++arg);
864 invarg(argc, argv, arg);
867 if (!strcmp(argv[arg], "-e")) {
869 if (!strcmp(getarg(argc, argv, arg), "full"))
872 size_e = getarg_l(argc, argv, arg);
874 invarg(argc, argv, arg);
878 if (!strcmp(argv[arg], "-m")) {
879 long v = getarg_l(argc, argv, ++arg);
880 if ((size_t)v >= sizeof(io_ops) / sizeof(io_ops[0]))
881 invarg(argc, argv, arg);
887 if (!strcmp(argv[arg], "-v")) {
891 if (!strcmp(argv[arg], "-i")) {
895 invarg(argc, argv, arg);
898 if (fname_r && size_r == 0)
902 f_w = open_prep_read(fname_w, &size_w);
903 if (size_e == 0 && io->addrs_remapped)
905 if (size_e != -1 && size_e < size_w)
911 fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
913 fprintf(stderr, "open %s: ", portname);
919 f_ws = open_prep_read(fname_ws, &size_ws);
923 cmd = CMD_RD | PAR_SINGE | PAR_DEV_ID;
924 write_serial(fd, &cmd, sizeof(cmd));
925 read_serial(fd, id, sizeof(id));
926 if (id[0] != id[1] || id[0] == 0) {
927 fprintf(stderr, "unexpected id: %02x %02x\n", id[0], id[1]);
930 printf("flashkit id: %02x\n", id[0]);
934 if (do_info || size_e || f_w)
935 io->write_bus(fd, 0, 0xf0); // flash reset
937 if (do_info || size_e)
941 printf("performing full erase..."); fflush(stdout);
942 flash_seq_erase_full(fd);
945 fprintf(stderr, "\nerase error: %04x\n", rv);
951 // set_delay(fd, 0); // ?
952 a_blk = get_block_addr(address_in, 0);
953 end = address_in + size_e;
955 printf("erasing %ld bytes:\n", size_e);
956 print_progress(0, size_e);
957 for (a = address_in; a < end; ) {
958 flash_seq_erase(fd, a_blk);
961 fprintf(stderr, "\nerase error: %lx %04x\n",
966 a_blk = get_block_addr(a_blk, 1);
968 print_progress(a - address_in, size_e);
974 printf("flashing %ld bytes:\n", size_w);
975 for (a = 0; a < size_w; a += write_step) {
979 len = min(size_w - a, write_step);
980 r = fread(b, 1, len, f_w);
985 io->flash_seq_write(fd, address_in + a, b);
988 print_progress(a, size_w);
990 print_progress(a, size_w);
992 if (write_step == 2 && rv != ((b[0] << 8) | b[1]))
993 fprintf(stderr, "warning: last bytes: %04x %02x%02x\n",
999 printf("writing %ld bytes:\n", size_ws);
1000 for (a = 0; a < size_ws; a += write_step) {
1001 uint16_t b = 0xffff;
1004 len = min(size_ws - a, write_step);
1005 r = fread(&b, 1, len, f_ws);
1010 if (write_step == 2)
1012 io->write_bus(fd, address_in + a, b);
1015 print_progress(a, size_ws);
1017 print_progress(a, size_ws);
1020 if (fname_r || size_v) {
1021 long blks, blks_v, done, verify_diff = 0;
1023 blks = (size_r + sizeof(g_block) - 1) / sizeof(g_block);
1024 blks_v = (size_v + sizeof(g_block) - 1) / sizeof(g_block);
1025 blks = max(blks, blks_v);
1027 f_r = fopen(fname_r, "wb");
1029 fprintf(stderr, "fopen %s: ", fname_r);
1035 printf("reading %ld bytes:\n", max(size_r, size_v));
1036 print_progress(0, blks * sizeof(g_block));
1038 for (done = 0; done < size_r || done < size_v; ) {
1039 a = address_in + done;
1040 if (io->addrs_remapped || (a >> 21) != a_blk) {
1042 io->set_addr(fd, a);
1044 io->read_block(fd, g_block, sizeof(g_block));
1045 if (f_r && done < size_r) {
1046 len = min(size_r - done, sizeof(g_block));
1047 if (fwrite(g_block, 1, len, f_r) != len) {
1052 if (done < size_v) {
1053 len = min(size_v - done, sizeof(g_block));
1054 if (fread(g_block2, 1, len, f_w) != len) {
1058 verify_diff |= memcmp(g_block, g_block2, len);
1060 done += sizeof(g_block);
1061 print_progress(done, blks * sizeof(g_block));
1064 fprintf(stderr, "verify FAILED\n");