| 1 | /* |
| 2 | * PDB, the PicoDrive debugger |
| 3 | * (C) notaz, 2010 |
| 4 | * |
| 5 | * This work is licensed under the terms of MAME license. |
| 6 | * See COPYING file in the top-level directory. |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <netdb.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "debug_net.h" |
| 17 | |
| 18 | static const char * const regnames[] = { |
| 19 | "PC", |
| 20 | "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", |
| 21 | "R8", "R9", "R10", "R11", "R12", "R13", "R14", "SP", |
| 22 | "PC2", "PC3", "PR", "SR", "GBR", "VBR", "MACH", "MACL", |
| 23 | "MEM0", "MEM1", // mem i/o csums |
| 24 | }; |
| 25 | |
| 26 | int main(int argc, char *argv[]) |
| 27 | { |
| 28 | unsigned int pc_trace[5][4], pc_trace_p[5] = { 0, }; |
| 29 | struct addrinfo *ai, *ais, hints; |
| 30 | int sock = -1, sock1, sock2; |
| 31 | struct sockaddr_in6 sa; |
| 32 | packet_t packet1, packet2; |
| 33 | int i, ret, cnt, cpuid; |
| 34 | int check_len_override = 0; |
| 35 | socklen_t sal; |
| 36 | |
| 37 | if (argv[1] != NULL) |
| 38 | check_len_override = atoi(argv[1]); |
| 39 | |
| 40 | memset(&hints, 0, sizeof(hints)); |
| 41 | hints.ai_flags = AI_CANONNAME; |
| 42 | hints.ai_family = PF_UNSPEC; |
| 43 | hints.ai_socktype = SOCK_STREAM; |
| 44 | |
| 45 | ret = getaddrinfo("::", "1234", &hints, &ais); |
| 46 | if (ret != 0) |
| 47 | return -1; |
| 48 | |
| 49 | for (ai = ais; ai != NULL; ai = ai->ai_next) { |
| 50 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 51 | if (sock == -1) |
| 52 | continue; |
| 53 | |
| 54 | ret = bind(sock, ai->ai_addr, ai->ai_addrlen); |
| 55 | if (ret != 0) { |
| 56 | close(sock); |
| 57 | sock = -1; |
| 58 | continue; |
| 59 | } |
| 60 | break; |
| 61 | } |
| 62 | freeaddrinfo(ais); |
| 63 | |
| 64 | if (sock == -1) { |
| 65 | perror("failed to bind"); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | ret = listen(sock, SOMAXCONN); |
| 70 | if (ret != 0) { |
| 71 | perror("failed to listen"); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | sal = sizeof(sa); |
| 76 | sock1 = accept(sock, (struct sockaddr *)&sa, &sal); |
| 77 | if (sock1 == -1) { |
| 78 | perror("failed to accept"); |
| 79 | return -1; |
| 80 | } |
| 81 | printf("client1 connected\n"); |
| 82 | |
| 83 | sal = sizeof(sa); |
| 84 | sock2 = accept(sock, (struct sockaddr *)&sa, &sal); |
| 85 | if (sock2 == -1) { |
| 86 | perror("failed to accept"); |
| 87 | return -1; |
| 88 | } |
| 89 | printf("client2 connected\n"); |
| 90 | |
| 91 | for (cnt = 0; ; cnt++) |
| 92 | { |
| 93 | int len; |
| 94 | #define tmp_size (4+4 + 24*4 + 2*4) |
| 95 | ret = recv(sock1, &packet1, tmp_size, MSG_WAITALL); |
| 96 | if (ret != tmp_size) { |
| 97 | if (ret < 0) |
| 98 | perror("recv1"); |
| 99 | else |
| 100 | printf("recv1 %d\n", ret); |
| 101 | return -1; |
| 102 | } |
| 103 | ret = recv(sock2, &packet2, tmp_size, MSG_WAITALL); |
| 104 | if (ret != tmp_size) { |
| 105 | if (ret < 0) |
| 106 | perror("recv2"); |
| 107 | else |
| 108 | printf("recv2 %d\n", ret); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | cpuid = packet1.header.cpuid; |
| 113 | len = sizeof(packet1.header) + packet1.header.len; |
| 114 | if (check_len_override > 0) |
| 115 | len = check_len_override; |
| 116 | |
| 117 | if (memcmp(&packet1, &packet2, len) == 0) { |
| 118 | pc_trace[cpuid][pc_trace_p[cpuid]++ & 3] = packet1.regs[0]; |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | if (packet1.header.cpuid != packet2.header.cpuid) |
| 123 | printf("%d: CPU %d %d\n", cnt, packet1.header.cpuid & 0xff, packet2.header.cpuid & 0xff); |
| 124 | else if (*(int *)&packet1.header != *(int *)&packet2.header) |
| 125 | printf("%d: header\n", cnt); |
| 126 | |
| 127 | // check regs (and stuff) |
| 128 | for (i = 0; i < 1+24+2; i++) |
| 129 | if (packet1.regs[i] != packet2.regs[i]) |
| 130 | printf("%d: %3s: %08x %08x\n", cnt, regnames[i], packet1.regs[i], packet2.regs[i]); |
| 131 | |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | printf("--\nCPU %d\n", cpuid); |
| 136 | for (cpuid = 0; cpuid < 2; cpuid++) { |
| 137 | printf("trace%d: ", cpuid); |
| 138 | for (i = 0; i < 4; i++) |
| 139 | printf(" %08x", pc_trace[cpuid][pc_trace_p[cpuid]++ & 3]); |
| 140 | |
| 141 | if (packet1.header.cpuid == cpuid) |
| 142 | printf(" %08x", packet1.regs[0]); |
| 143 | else if (packet2.header.cpuid == cpuid) |
| 144 | printf(" %08x", packet2.regs[0]); |
| 145 | printf("\n"); |
| 146 | } |
| 147 | |
| 148 | for (i = 0; i < 24+1; i++) |
| 149 | printf("%3s: %08x %08x\n", regnames[i], packet1.regs[i], packet2.regs[i]); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |