8 #include <sys/socket.h>
10 #include <sys/fcntl.h>
18 static const char g_fpu[] = "../vhdl/tb_fpu";
20 static bool g_running = false;
21 static int g_fpu_socket[2] = {-1,-1};
22 static pid_t g_fpu_pid = -1;
26 * @returns 0 on success, errno of the failing function on failure
31 // create unix socket pair
33 if (socketpair(AF_UNIX, SOCK_STREAM, 0, g_fpu_socket) != 0)
38 if (g_fpu_pid < 0) // error check
46 // Remap stdio to the socket
47 dup2(g_fpu_socket[0],fileno(stdin));
48 dup2(g_fpu_socket[0],fileno(stdout));
49 dup2(open("/dev/null", O_APPEND), fileno(stderr)); //LALALA I AM NOT LISTENING TO YOUR STUPID ERRORS GHDL
51 // Unbuffer things; buffers are a pain
52 setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL);
54 //fprintf(stderr, "Goodbye!\n");
55 execl(g_fpu, g_fpu,NULL);
56 fprintf(stderr, "Uh oh! %s\n", strerror(errno)); // We will never see this if something goes wrong... oh dear
57 exit(errno); // Child exits here.
62 g_running = true; // We are ready!
72 // Tell the child to stop running the VHDL simulation
73 if (close(g_fpu_socket[1]) != 0)
76 if (kill(g_fpu_pid, SIGKILL) != 0)
83 * Tell the VFPU to execute an instruction, wait for it to finish, return the result
84 * TODO: Generalise for non 32bit Registers
86 Register Exec(const Register & opa, const Register & opb, Opcode op)
90 // Copy floats into 32 bits (casting will alter the representation)
91 unsigned a; memcpy(&a, &opa, 8);
92 unsigned b; memcpy(&b, &opb, 8);
96 int len = sprintf(buffer, "%08x\n%08x\n%03x\n",a, b, op); // This is... truly awful... why am I doing this
97 //fprintf(stderr, "Writing:\n%s", buffer);
100 assert(write(g_fpu_socket[1], buffer, len) == len);
101 //fprintf(stderr, "Wrote!\n");
103 len = read(g_fpu_socket[1], buffer, sizeof(buffer));
108 unsigned result = 0x00000000;
109 for (int i = 0; i < len/2; ++i)
111 unsigned byte2; // cos its two bytes
112 sscanf(buffer+2*i, "%02x", &byte2);
113 result |= (byte2 << 8*(len/2-i-1));
116 //fprintf(stderr, "Buffer: %s\nResult: %08x\n", buffer, result);
119 memcpy(&r, &result, 8); // Amazing.