2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2010-08-09 13:28:56 +00:00
|
|
|
|
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() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto readable() -> bool {
|
2016-05-02 09:57:04 +00:00
|
|
|
if(!opened) return false;
|
2013-05-02 11:25:45 +00:00
|
|
|
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-11-16 08:38:05 +00:00
|
|
|
auto read(uint8_t* data, uint length) -> int {
|
2016-05-02 09:57:04 +00:00
|
|
|
if(!opened) return -1;
|
2013-05-02 11:25:45 +00:00
|
|
|
return ::read(port, (void*)data, length);
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto writable() -> bool {
|
2016-05-02 09:57:04 +00:00
|
|
|
if(!opened) return false;
|
2013-05-02 11:25:45 +00:00
|
|
|
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-11-16 08:38:05 +00:00
|
|
|
auto write(const uint8_t* data, uint length) -> int {
|
2016-05-02 09:57:04 +00:00
|
|
|
if(!opened) return -1;
|
2013-05-02 11:25:45 +00:00
|
|
|
return ::write(port, (void*)data, length);
|
|
|
|
}
|
|
|
|
|
2016-05-02 09:57:04 +00:00
|
|
|
//rate==0: use flow control (synchronous mode)
|
|
|
|
//rate!=0: baud-rate (asynchronous mode)
|
|
|
|
auto open(string device, uint rate = 0) -> bool {
|
2013-05-02 11:25:45 +00:00
|
|
|
close();
|
|
|
|
|
2016-05-02 09:57:04 +00:00
|
|
|
if(!device) device = "/dev/ttyU0"; //note: default device name is for FreeBSD 10+
|
|
|
|
port = ::open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
|
2013-05-02 11:25:45 +00:00
|
|
|
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);
|
2016-05-02 09:57:04 +00:00
|
|
|
cfsetspeed(&attr, rate ? rate : 57600); //rate value has no effect in synchronous mode
|
2013-05-02 11:25:45 +00:00
|
|
|
|
|
|
|
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);
|
2016-05-02 09:57:04 +00:00
|
|
|
if(rate) {
|
2013-05-02 11:25:45 +00:00
|
|
|
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; }
|
2016-05-02 09:57:04 +00:00
|
|
|
return opened = true;
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto close() -> void {
|
2013-05-02 11:25:45 +00:00
|
|
|
if(port != -1) {
|
|
|
|
tcdrain(port);
|
2016-05-02 09:57:04 +00:00
|
|
|
if(opened) {
|
2013-05-02 11:25:45 +00:00
|
|
|
tcsetattr(port, TCSANOW, &original_attr);
|
2016-05-02 09:57:04 +00:00
|
|
|
opened = 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:
|
2016-05-02 09:57:04 +00:00
|
|
|
int port = -1;
|
|
|
|
bool opened = false;
|
2013-05-02 11:25:45 +00:00
|
|
|
termios original_attr;
|
|
|
|
};
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
}
|