Пример #1
0
/**
 * Tell the VFPU to execute an instruction, wait for it to finish, return the result
 * TODO: Make this not mix C++ and C so badly?
 * TODO: It will still only work for magic 32 bit FPU
 */
Register Exec(const Register & a, const Register &  b, Opcode op, Rmode rmode)
{
	assert(g_running);
	stringstream s;
	s << hex << setw(8) << setfill('0') << a.to_ulong() << "\n" << b.to_ulong() << "\n" << setw(1) << op <<"\n" << setw(1) << rmode << "\n";
	string str(s.str());
	//Debug("Writing: %s", str.c_str());

	// So we used C++ streams to make our C string...
	assert(write(g_fpu_socket[1], str.c_str(), str.size()) == (int)str.size());

	char buffer[BUFSIZ]; 
	int len = read(g_fpu_socket[1], buffer, sizeof(buffer));
	//assert(len == 9);
	buffer[--len] = '\0'; // Get rid of newline
	//Debug("Read: %s", buffer);
	
	Register result(0);
	for (int i = 0; i < len/2; ++i)
	{
		unsigned byte; // It is ONE byte (2 nibbles == 2 hex digits)
		sscanf(buffer+2*i, "%02x", &byte);
		result |= (byte << 8*(len/2-i-1));
	}
	
	stringstream s2;
	//TODO: Make it compile on non C++11
	s2 << hex << result.to_ulong();
	//Debug("Result is: %s", s2.str().c_str());
	return result;
}