Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1792 from reicast/skmp/virtual-serial-port
Browse files Browse the repository at this point in the history
Linux: Virtual Serial Port support
  • Loading branch information
skmp committed Feb 7, 2020
2 parents a589241 + 5edd3db commit 83d61de
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ add_executable(${TNAME}${binSuffix} ${reicast_SRCS} ${deps_SRCS})
target_compile_features(${TNAME}${binSuffix} PRIVATE cxx_std_14)

if(${HOST_OS} EQUAL ${OS_LINUX})
# needed for opentty
target_link_libraries(${TNAME} -lutil)

find_package(ALSA)
if(ALSA_FOUND)
target_compile_definitions(${TNAME} PUBLIC USE_ALSA)
Expand Down
4 changes: 4 additions & 0 deletions libswirl/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@
#define FEAT_HAS_SOFTREND BUILD_COMPILER == COMPILER_VC //GCC wants us to enable sse4 globaly to enable intrins
#endif

#ifndef FEAT_HAS_SERIAL_TTY
#define FEAT_HAS_SERIAL_TTY (HOST_OS == OS_LINUX && !defined(_ANDROID))
#endif

//Depricated build configs
#ifdef HOST_NO_REC
#error Dont use HOST_NO_REC
Expand Down
7 changes: 7 additions & 0 deletions libswirl/gui/gui_settings_advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ void gui_settings_advanced()
ImGui::SameLine();
gui_ShowHelpMarker("Dump the Dreamcast serial console to stdout");
#endif

#if FEAT_HAS_SERIAL_TTY
ImGui::Checkbox("Create Virtual Serial Port", &settings.debug.VirtualSerialPort);
ImGui::SameLine();
gui_ShowHelpMarker("Create a PTY for use with dc-load. Requires restart.");
#endif

ImGui::Checkbox("Dump Textures", &settings.rend.DumpTextures);
ImGui::SameLine();
gui_ShowHelpMarker("Dump all textures into data/texdump/<game id>");
Expand Down
58 changes: 53 additions & 5 deletions libswirl/hw/sh4/modules/scif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,54 @@ SCIF_SCFSR2_type SCIF_SCFSR2;
u8 SCIF_SCFRDR2;
SCIF_SCFDR2_type SCIF_SCFDR2;

#if FEAT_HAS_SERIAL_TTY
#include <fcntl.h>
#include <unistd.h>
extern int pty_master;
#endif

bool SerialHasPendingData() {
bool has_data = false;
#if FEAT_HAS_SERIAL_TTY
if (pty_master != -1) {
fd_set read_fds, write_fds, except_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(pty_master, &read_fds);

timeval interval_timeout = { 0, 0};
has_data = select(pty_master + 1, &read_fds, &write_fds, &except_fds, &interval_timeout) == 1;
}
#endif
return has_data;
}

u8 SerialReadData() {
u8 rd = 0;
#if FEAT_HAS_SERIAL_TTY
if (pty_master != -1){
verify(SerialHasPendingData());
while(read(pty_master, &rd, 1) != 1)
printf("SERIAL: PTY read failed, %d\n", errno);
}
#endif

return rd;
}

void SerialWriteData(u8 data) {
if (settings.debug.SerialConsole) {
putc(data, stdout);
}
#if FEAT_HAS_SERIAL_TTY
if (pty_master != -1) {
while(write(pty_master, &data, 1) != 1)
printf("SERIAL: PTY write failed, %d\n", errno);
}
#endif
}

struct Sh4ModScif_impl : Sh4ModScif
{
SuperH4Mmr* sh4mmr;
Expand Down Expand Up @@ -55,15 +103,14 @@ struct Sh4ModScif_impl : Sh4ModScif

void SerialWrite(u32 addr, u32 data)
{
if (settings.debug.SerialConsole) {
putc(data, stdout);
}
SerialWriteData(data);
}

//SCIF_SCFSR2 read
u32 ReadSerialStatus(u32 addr)
{
if (false /*PendingSerialData()*/)

if (SerialHasPendingData())
{
return 0x60 | 2;
}
Expand Down Expand Up @@ -92,7 +139,8 @@ struct Sh4ModScif_impl : Sh4ModScif
//SCIF_SCFRDR2
u32 ReadSerialData(u32 addr)
{
s32 rd = 0;//ReadSerial();
u8 rd = SerialReadData();

return (u8)rd;
}

Expand Down
6 changes: 5 additions & 1 deletion libswirl/libswirl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ void InitSettings()
settings.pvr.ForceGLES2 = false;

settings.debug.SerialConsole = false;
settings.debug.VirtualSerialPort = false;

settings.bios.UseReios = 0;
settings.reios.ElfFile = "";
Expand Down Expand Up @@ -408,7 +409,8 @@ void LoadSettings(bool game_specific)
settings.pvr.ForceGLES2 = cfgLoadBool(config_section, "pvr.ForceGLES2", settings.pvr.ForceGLES2);

settings.debug.SerialConsole = cfgLoadBool(config_section, "Debug.SerialConsoleEnabled", settings.debug.SerialConsole);

settings.debug.VirtualSerialPort = cfgLoadBool(config_section, "Debug.VirtualSerialPort", settings.debug.VirtualSerialPort);

settings.bios.UseReios = cfgLoadBool(config_section, "bios.UseReios", settings.bios.UseReios);
settings.reios.ElfFile = cfgLoadStr(game_specific ? cfgGetGameId() : "reios", "ElfFile", settings.reios.ElfFile.c_str());

Expand Down Expand Up @@ -560,6 +562,8 @@ void SaveSettings()
cfgSaveBool("config", "pvr.ForceGLES2", settings.pvr.ForceGLES2);

cfgSaveBool("config", "Debug.SerialConsoleEnabled", settings.debug.SerialConsole);
cfgSaveBool("config", "Debug.VirtualSerialPort", settings.debug.VirtualSerialPort);

cfgSaveInt("input", "MouseSensitivity", settings.input.MouseSensitivity);
cfgSaveInt("input", "VirtualGamepadVibration", settings.input.VirtualGamepadVibration);
for (int i = 0; i < MAPLE_PORTS; i++)
Expand Down
22 changes: 21 additions & 1 deletion libswirl/linux-dist/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "hw/sh4/dyna/blockmanager.h"
#include "hw/maple/maple_cfg.h"
#include <unistd.h>
#include <pty.h>

#include "libswirl.h"
#include "hw/pvr/Renderer_if.h"
Expand Down Expand Up @@ -411,6 +412,10 @@ void os_gl_term()
#endif
}

#if FEAT_HAS_SERIAL_TTY
int pty_master;
#endif

int main(int argc, wchar* argv[])
{

Expand Down Expand Up @@ -451,7 +456,22 @@ int main(int argc, wchar* argv[])
if (reicast_init(argc, argv))
die("Reicast initialization failed\n");


#if FEAT_HAS_SERIAL_TTY
if (settings.debug.VirtualSerialPort) {
int slave;
char slave_name[2048];
pty_master = -1;
if (openpty(&pty_master, &slave, slave_name, nullptr, nullptr) >= 0)
{
printf("Creating virtual serial port at %s\n", slave_name);

// not for us to use, we use master
// do not close to avoid EIO though
// close(slave);
}
}
#endif

#if FEAT_HAS_NIXPROF
install_prof_handler(1);
#endif
Expand Down
1 change: 1 addition & 0 deletions libswirl/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ struct settings_t

struct {
bool SerialConsole;
bool VirtualSerialPort;
} debug;

struct {
Expand Down
2 changes: 1 addition & 1 deletion reicast/linux/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ MFLAGS :=
ASFLAGS :=
LDFLAGS := -g -Wl,-Map,$(notdir $@).map,--gc-sections -Wl,-O3 -Wl,--sort-common -fopenmp #defaults
INCS :=
LIBS := -lm -lpthread #defaults
LIBS := -lm -lpthread -lutil #defaults

CFLAGS := -g -O3 -D RELEASE -c -D USES_HOMEDIR -fopenmp # defaults
CFLAGS += -frename-registers -fno-strict-aliasing
Expand Down

0 comments on commit 83d61de

Please sign in to comment.