X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hexed%2Fpc_transfer.c;h=bcfb652a0a06947fb8ab1ca00dbccb59fb259130;hb=65d9165cf790c51d46b68866f47b042105083f43;hp=569acfc9129c22d55406e74b645b2a21505f08fb;hpb=93c5aa8a4e294c0736b3ddd02b57b3b4f353e539;p=megadrive.git diff --git a/hexed/pc_transfer.c b/hexed/pc_transfer.c index 569acfc..bcfb652 100644 --- a/hexed/pc_transfer.c +++ b/hexed/pc_transfer.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "transfer.h" @@ -190,7 +191,10 @@ static void usage(const char *argv0) fprintf(stderr, "usage:\n%s [args]\n" "\tsend [size]\n" "\trecv \n" - "\tjump \n", argv0); + "\tjump \n" + "\tio {r{8,16,32} ,w{8,16,32} }*\n" + "\tloadstate \n" + "\trecvvram \n", argv0); exit(1); } @@ -208,6 +212,16 @@ static unsigned int atoi_or_die(const char *a) return i; } +static void checked_gzread(gzFile f, void *data, size_t size) +{ + unsigned int ret; + ret = gzread(f, data, size); + if (ret != size) { + fprintf(stderr, "gzread returned %d/%zu\n", ret, size); + exit(1); + } +} + int main(int argc, char *argv[]) { unsigned int addr = 0, size = 0; @@ -322,6 +336,89 @@ int main(int argc, char *argv[]) count++; } } + else if (strcmp(argv[1], "loadstate") == 0) + { + unsigned char chunk; + char header[12]; + gzFile f; + int len; + + if (argc != 3) + usage(argv[0]); + + f = gzopen(argv[2], "rb"); + if (f == NULL) { + perror("gzopen"); + return 1; + } + + checked_gzread(f, header, sizeof(header)); + if (strncmp(header, "PicoSEXT", 8) != 0) { + fprintf(stderr, "bad header\n"); + return 1; + } + + while (!gzeof(file)) + { + ret = gzread(f, &chunk, 1); + if (ret == 0) + break; + checked_gzread(f, &len, 4); + //printf("%2d %x\n", chunk, len); + switch (chunk) { + case 3: // VRAM + checked_gzread(f, data, len); + size += len; + break; + case 5: // CRAM + checked_gzread(f, data + 0x10000, len); + size += len; + break; + case 6: // VSRAM + checked_gzread(f, data + 0x10080, len); + size += len; + break; + case 8: // video + checked_gzread(f, data + 0x10100, len); + data[size+0] &= ~1; // no display disable + data[size+1] |= 0x40; // no blanking + size += 0x20; + break; + default: + if (chunk > 64+8) { + fprintf(stderr, "bad chunk: %d\n", chunk); + return 1; + } + gzseek(f, len, SEEK_CUR); + break; + } + } + gzclose(f); + if (size != 0x10120) { + fprintf(stderr, "bad final size: %x\n", size); + return 1; + } + // unbyteswap *RAMs (stored byteswapped) + for (i = 0; i < 0x10100; i += 2) { + int tmp = data[i]; + data[i] = data[i + 1]; + data[i + 1] = tmp; + } + } + else if (strcmp(argv[1], "recvvram") == 0) + { + if (argc != 3) + usage(argv[0]); + + file = fopen(argv[2], "wb"); + if (file == NULL) { + fprintf(stderr, "can't open file: %s\n", argv[2]); + return 1; + } + + size = 0x10000; + memset(data, 0, size); + } else usage(argv[0]); @@ -335,18 +432,29 @@ int main(int argc, char *argv[]) printf("regs: %02x %02x %02x\n", inb(PORT_DATA), inb(PORT_STATUS), inb(PORT_CONTROL)); + + /* wait for start condition */ + if (!(inb(PORT_STATUS) & 0x40)) + printf("waiting for TH high..\n"); + while (!(inb(PORT_STATUS) & 0x40)) + usleep(10000); + outb(0xe8, PORT_CONTROL); /* TR low - request for transfer */ + /* wait for request ack */ if (inb(PORT_STATUS) & 0x40) printf("waiting for TH low..\n"); - while (inb(PORT_STATUS) & 0x40) - usleep(100000); + for (i = 10000; inb(PORT_STATUS) & 0x40; i += 100) { + if (i > 100000) + i = 100000; + usleep(i); + } outb(0xe0, PORT_CONTROL); if (strcmp(argv[1], "send") == 0) { - send_cmd(CMD_MD_SEND); + send_cmd(CMD_PC_SEND); send_byte((addr >> 16) & 0xff); send_byte((addr >> 8) & 0xff); send_byte((addr >> 0) & 0xff); @@ -367,7 +475,7 @@ int main(int argc, char *argv[]) } else if (strcmp(argv[1], "recv") == 0) { - send_cmd(CMD_MD_RECV); + send_cmd(CMD_PC_RECV); send_byte((addr >> 16) & 0xff); send_byte((addr >> 8) & 0xff); send_byte((addr >> 0) & 0xff); @@ -448,12 +556,46 @@ int main(int argc, char *argv[]) } } } + else if (strcmp(argv[1], "loadstate") == 0) + { + send_cmd(CMD_LOADSTATE); + + for (i = 0; i < size; i++) + { + if ((i & 0x1f) == 0) { + printf("\b\b\b\b\b\b\b\b\b\b\b\b\b"); + printf("%06x/%06x", i, size); + fflush(stdout); + } + + send_byte(data[i]); + } + } + else if (strcmp(argv[1], "recvvram") == 0) + { + send_cmd(CMD_VRAM_RECV); + output_to_input(); + + for (i = 0; i < size; i++) + { + if ((i & 0xff) == 0) { + printf("\b\b\b\b\b\b\b\b\b\b\b\b\b"); + printf("%06x/%06x", i, size); + fflush(stdout); + } + + data[i] = recv_byte(); + } + + fwrite(data, 1, size, file); + } - if (file != NULL) { + if (size != 0) { printf("\b\b\b\b\b\b\b\b\b\b\b\b\b"); printf("%06x/%06x\n", i, size); - fclose(file); } + if (file != NULL) + fclose(file); /* switch TL back to high, disable outputs */ outb(0xe0, PORT_CONTROL);