2c6d7038c1415528f8c5b36ebbaf0f006f52158d
[ipdf/code.git] / src / vfpu.cpp
1 #include "vfpu.h"
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <sys/stat.h>
10 #include <sys/fcntl.h>
11 #include <stdbool.h>
12 #include <signal.h>
13 #include <string.h>
14
15 #include <sstream>
16 #include <iomanip>
17
18 #include "common.h"
19
20 using namespace std;
21
22 namespace VFPU
23 {
24
25 static const char g_fpu[] = "vfpu";
26
27 static bool g_running = false;
28 static int g_fpu_socket[2] = {-1,-1};
29 static pid_t g_fpu_pid = -1;
30
31 /**
32  * Starts the VFPU
33  * @returns 0 on success, errno of the failing function on failure
34  */
35 int Start()
36 {
37         assert(!g_running);
38         // create unix socket pair
39         
40         if (socketpair(AF_UNIX, SOCK_STREAM, 0, g_fpu_socket) != 0)
41                 return errno;
42         
43
44         g_fpu_pid = fork();
45         if (g_fpu_pid < 0) // error check
46                 return errno;
47
48
49         // Child branch
50         if (g_fpu_pid == 0)
51         {
52                 
53                 // Remap stdio to the socket
54                 dup2(g_fpu_socket[0],fileno(stdin));
55                 dup2(g_fpu_socket[0],fileno(stdout));
56                 
57                 // Unbuffer things; buffers are a pain
58                 setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL);
59
60                 Debug("Child about to suppress stderr and exec vfpu");
61                 dup2(open("/dev/null", O_APPEND), fileno(stderr)); //LALALA I AM NOT LISTENING TO YOUR STUPID ERRORS GHDL
62                 execl(g_fpu, g_fpu,NULL);
63                 int err = errno; // Because errno will be set again by the next system call
64                 Fatal("Uh oh! %s\n", strerror(err)); // We will never see this if something goes wrong... oh dear
65                 exit(err); // Child exits here.
66         }
67
68         // Parent branch
69         usleep(100);
70         g_running = true; // We are ready!
71         return 0;
72 }
73
74 /**
75  * Halt the VFPU
76  */
77 int Halt()
78 {
79         assert(g_running);
80         // Tell the child to stop running the VHDL simulation
81         if (close(g_fpu_socket[1]) != 0)
82                 return errno;
83         usleep(1000);
84         if (kill(g_fpu_pid, SIGKILL) != 0)
85                 return errno;
86         g_running = false;
87         return 0;
88 }
89
90 float Exec(float opa, float opb, Opcode op, Rmode rmode)
91 {
92         unsigned a; memcpy(&a, &opa, sizeof(float));
93         unsigned b; memcpy(&b, &opb, sizeof(float));
94         
95         unsigned r = (unsigned)(Exec(Register(a), Register(b), op, rmode).to_ulong());
96         float result; memcpy(&result, &r, sizeof(float));
97         return result;
98 }
99
100 /**
101  * Tell the VFPU to execute an instruction, wait for it to finish, return the result
102  * TODO: Make this not mix C++ and C so badly?
103  * TODO: It will still only work for magic 32 bit FPU
104  */
105 Register Exec(const Register & a, const Register &  b, Opcode op, Rmode rmode)
106 {
107         assert(g_running);
108                 
109         stringstream s;
110         s << hex << setw(8) << setfill('0') << a.to_ullong() << "\n" << b.to_ullong() << "\n" << setw(3) << op <<"\n" << setw(2) << rmode << "\n\n";
111         string str(s.str());
112         //Debug("Writing: %s", str.c_str());
113
114         // So we used C++ streams to make our C string...
115         assert(write(g_fpu_socket[1], str.c_str(), str.size()) == (int)str.size());
116
117         char buffer[BUFSIZ]; 
118         int len = read(g_fpu_socket[1], buffer, sizeof(buffer));
119         //assert(len == 9);
120         buffer[--len] = '\0'; // Get rid of newline
121         //Debug("Read: %s", buffer);
122         
123         Register result(0);
124         for (int i = 0; i < len/2; ++i)
125         {
126                 unsigned byte; // It is ONE byte (2 nibbles == 2 hex digits)
127                 sscanf(buffer+2*i, "%02x", &byte);
128                 result |= (byte << 8*(len/2-i-1));
129         }
130         
131         stringstream s2;
132         s2 << hex << result.to_ullong();
133         //Debug("Result is: %s", s2.str().c_str());
134         return result;
135 }
136
137 }
138
139

UCC git Repository :: git.ucc.asn.au