2010-08-09 13:28:56 +00:00
|
|
|
#ifndef NALL_SERIAL_HPP
|
|
|
|
#define NALL_SERIAL_HPP
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
#include <nall/intrinsics.hpp>
|
|
|
|
#include <nall/stdint.hpp>
|
|
|
|
#include <nall/string.hpp>
|
|
|
|
|
Update to v094r29 release.
byuu says:
Note: for Windows users, please go to nall/intrinsics.hpp line 60 and
correct the typo from "DISPLAY_WINDOW" to "DISPLAY_WINDOWS" before
compiling, otherwise things won't work at all.
This will be a really major WIP for the core SNES emulation, so please
test as thoroughly as possible.
I rewrote the 65816 CPU core's dispatcher from a jump table to a switch
table. This was so that I could pass class variables as parameters to
opcodes without crazy theatrics.
With that, I killed the regs.r[N] stuff, the flag_t operator|=, &=, ^=
stuff, and all of the template versions of opcodes.
I also removed some stupid pointless flag tests in xcn and pflag that
would always be true.
I sure hope that AWJ is happy with this; because this change was so that
my flag assignments and branch tests won't need to build regs.P into
a full 8-bit variable anymore.
It does of course incur a slight performance hit when you pass in
variables by-value to functions, but it should help with binary size
(and thus cache) by reducing a lot of extra functions. (I know I could
have used template parameters for some things even with a switch table,
but chose not to for the aforementioned reasons.)
Overall, it's about a ~1% speedup from the previous build. The CPU core
instructions were never a bottleneck, but I did want to fix the P flag
building stuff because that really was a dumb mistake v_v'
2015-06-22 13:31:49 +00:00
|
|
|
#if !defined(API_POSIX)
|
|
|
|
#error "nall/serial: unsupported system"
|
2013-05-02 11:25:45 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace nall {
|
2012-03-10 12:47:19 +00:00
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
struct serial {
|
2015-07-14 09:32:43 +00:00
|
|
|
serial() {
|
|
|
|
port = -1;
|
|
|
|
port_open = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~serial() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto readable() -> bool {
|
2013-05-02 11:25:45 +00:00
|
|
|
if(port_open == false) return false;
|
|
|
|
fd_set fdset;
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(port, &fdset);
|
|
|
|
timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
int result = select(FD_SETSIZE, &fdset, nullptr, nullptr, &timeout);
|
|
|
|
if(result < 1) return false;
|
|
|
|
return FD_ISSET(port, &fdset);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-1 on error, otherwise return bytes read
|
2015-07-14 09:32:43 +00:00
|
|
|
auto read(uint8_t* data, unsigned length) -> int {
|
2013-05-02 11:25:45 +00:00
|
|
|
if(port_open == false) return -1;
|
|
|
|
return ::read(port, (void*)data, length);
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto writable() -> bool {
|
2013-05-02 11:25:45 +00:00
|
|
|
if(port_open == false) return false;
|
|
|
|
fd_set fdset;
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(port, &fdset);
|
|
|
|
timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
int result = select(FD_SETSIZE, nullptr, &fdset, nullptr, &timeout);
|
|
|
|
if(result < 1) return false;
|
|
|
|
return FD_ISSET(port, &fdset);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-1 on error, otherwise return bytes written
|
2015-07-14 09:32:43 +00:00
|
|
|
auto write(const uint8_t* data, unsigned length) -> int {
|
2013-05-02 11:25:45 +00:00
|
|
|
if(port_open == false) return -1;
|
|
|
|
return ::write(port, (void*)data, length);
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto open(const string& portname, unsigned rate, bool flowcontrol) -> bool {
|
2013-05-02 11:25:45 +00:00
|
|
|
close();
|
|
|
|
|
|
|
|
port = ::open(portname, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
|
|
|
|
if(port == -1) return false;
|
|
|
|
|
|
|
|
if(ioctl(port, TIOCEXCL) == -1) { close(); return false; }
|
|
|
|
if(fcntl(port, F_SETFL, 0) == -1) { close(); return false; }
|
|
|
|
if(tcgetattr(port, &original_attr) == -1) { close(); return false; }
|
|
|
|
|
|
|
|
termios attr = original_attr;
|
|
|
|
cfmakeraw(&attr);
|
|
|
|
cfsetspeed(&attr, rate);
|
|
|
|
|
|
|
|
attr.c_lflag &=~ (ECHO | ECHONL | ISIG | ICANON | IEXTEN);
|
|
|
|
attr.c_iflag &=~ (BRKINT | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY);
|
|
|
|
attr.c_iflag |= (IGNBRK | IGNPAR);
|
|
|
|
attr.c_oflag &=~ (OPOST);
|
|
|
|
attr.c_cflag &=~ (CSIZE | CSTOPB | PARENB | CLOCAL);
|
|
|
|
attr.c_cflag |= (CS8 | CREAD);
|
|
|
|
if(flowcontrol == false) {
|
|
|
|
attr.c_cflag &= ~CRTSCTS;
|
|
|
|
} else {
|
|
|
|
attr.c_cflag |= CRTSCTS;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
attr.c_cc[VTIME] = attr.c_cc[VMIN] = 0;
|
|
|
|
|
|
|
|
if(tcsetattr(port, TCSANOW, &attr) == -1) { close(); return false; }
|
|
|
|
return port_open = true;
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto close() -> void {
|
2013-05-02 11:25:45 +00:00
|
|
|
if(port != -1) {
|
|
|
|
tcdrain(port);
|
|
|
|
if(port_open == true) {
|
|
|
|
tcsetattr(port, TCSANOW, &original_attr);
|
|
|
|
port_open = false;
|
2010-08-23 08:02:38 +00:00
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
::close(port);
|
|
|
|
port = -1;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
private:
|
|
|
|
int port;
|
|
|
|
bool port_open;
|
|
|
|
termios original_attr;
|
|
|
|
};
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|