8 #include <sys/socket.h>
10 #include <sys/fcntl.h>
23 static const char g_fpu[] = "../vhdl/tb_fpu";
25 static bool g_running = false;
26 static int g_fpu_socket[2] = {-1,-1};
27 static pid_t g_fpu_pid = -1;
31 * @returns 0 on success, errno of the failing function on failure
36 // create unix socket pair
38 if (socketpair(AF_UNIX, SOCK_STREAM, 0, g_fpu_socket) != 0)
43 if (g_fpu_pid < 0) // error check
51 // Remap stdio to the socket
52 dup2(g_fpu_socket[0],fileno(stdin));
53 dup2(g_fpu_socket[0],fileno(stdout));
54 dup2(open("/dev/null", O_APPEND), fileno(stderr)); //LALALA I AM NOT LISTENING TO YOUR STUPID ERRORS GHDL
56 // Unbuffer things; buffers are a pain
57 setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL);
59 //fprintf(stderr, "Goodbye!\n");
60 execl(g_fpu, g_fpu,NULL);
61 fprintf(stderr, "Uh oh! %s\n", strerror(errno)); // We will never see this if something goes wrong... oh dear
62 exit(errno); // Child exits here.
67 g_running = true; // We are ready!
77 // Tell the child to stop running the VHDL simulation
78 if (close(g_fpu_socket[1]) != 0)
81 if (kill(g_fpu_pid, SIGKILL) != 0)
87 float Exec(float opa, float opb, Opcode op)
89 unsigned a; memcpy(&a, &opa, sizeof(float));
90 unsigned b; memcpy(&b, &opb, sizeof(float));
92 unsigned r = (unsigned)(Exec(Register(a), Register(b), op).to_ulong());
93 float result; memcpy(&result, &r, sizeof(float));
98 * Tell the VFPU to execute an instruction, wait for it to finish, return the result
99 * TODO: Make this not mix C++ and C so badly?
101 Register Exec(const Register & a, const Register & b, Opcode op)
106 s << hex << a.to_ullong() << "\n" << b.to_ullong() << "\n" << setw(3) << setfill('0') << op << "\n";
108 //fprintf(stderr, "Writing:\n%s\n", str.c_str());
110 // So we used C++ streams to make our C string...
111 assert(write(g_fpu_socket[1], str.c_str(), str.size()) == (int)str.size());
112 //fprintf(stderr, "Wrote!\n");
115 int len = read(g_fpu_socket[1], buffer, sizeof(buffer));
117 buffer[--len] = '\0'; // Get rid of newline
118 //fprintf(stderr, "Read!\n");
121 for (int i = 0; i < len/2; ++i)
123 unsigned byte; // It is ONE byte (2 nibbles == 2 hex digits)
124 sscanf(buffer+2*i, "%02x", &byte);
125 result |= (byte << 8*(len/2-i-1));