diff --git a/scripts/src/bus.lua b/scripts/src/bus.lua index 643bf0e9101..064f8503156 100644 --- a/scripts/src/bus.lua +++ b/scripts/src/bus.lua @@ -1531,6 +1531,8 @@ if (BUSES["RS232"]~=null) then MAME_DIR .. "src/devices/bus/rs232/printer.h", MAME_DIR .. "src/devices/bus/rs232/rs232.c", MAME_DIR .. "src/devices/bus/rs232/rs232.h", + MAME_DIR .. "src/devices/bus/rs232/pty.c", + MAME_DIR .. "src/devices/bus/rs232/pty.h", MAME_DIR .. "src/devices/bus/rs232/ser_mouse.c", MAME_DIR .. "src/devices/bus/rs232/ser_mouse.h", MAME_DIR .. "src/devices/bus/rs232/terminal.c", diff --git a/scripts/src/emu.lua b/scripts/src/emu.lua index 547c57e2791..eccb515ad02 100644 --- a/scripts/src/emu.lua +++ b/scripts/src/emu.lua @@ -79,6 +79,8 @@ files { MAME_DIR .. "src/emu/dinvram.h", MAME_DIR .. "src/emu/dioutput.c", MAME_DIR .. "src/emu/dioutput.h", + MAME_DIR .. "src/emu/dipty.c", + MAME_DIR .. "src/emu/dipty.h", MAME_DIR .. "src/emu/dirtc.c", MAME_DIR .. "src/emu/dirtc.h", MAME_DIR .. "src/emu/diserial.c", diff --git a/src/devices/bus/rs232/rs232.c b/src/devices/bus/rs232/rs232.c index acc1af5dea3..cb2d1771a6e 100644 --- a/src/devices/bus/rs232/rs232.c +++ b/src/devices/bus/rs232/rs232.c @@ -103,6 +103,7 @@ device_rs232_port_interface::~device_rs232_port_interface() #include "null_modem.h" #include "printer.h" #include "terminal.h" +#include "pty.h" SLOT_INTERFACE_START( default_rs232_devices ) SLOT_INTERFACE("keyboard", SERIAL_KEYBOARD) @@ -110,4 +111,5 @@ SLOT_INTERFACE_START( default_rs232_devices ) SLOT_INTERFACE("null_modem", NULL_MODEM) SLOT_INTERFACE("printer", SERIAL_PRINTER) SLOT_INTERFACE("terminal", SERIAL_TERMINAL) + SLOT_INTERFACE("pty", PSEUDO_TERMINAL) SLOT_INTERFACE_END diff --git a/src/emu/bus/rs232/pty.c b/src/emu/bus/rs232/pty.c new file mode 100644 index 00000000000..d1ed6d7d24a --- /dev/null +++ b/src/emu/bus/rs232/pty.c @@ -0,0 +1,148 @@ +// license:BSD-3-Clause +// copyright-holders:F. Ulivi +// +#include +#include "pty.h" + +#define FU_TEST + +static const int TIMER_POLL = 1; + +pseudo_terminal_device::pseudo_terminal_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : + device_t(mconfig, PSEUDO_TERMINAL, "Pseudo terminal", tag, owner, clock, "pseudo_terminal", __FILE__), + device_serial_interface(mconfig, *this), + device_rs232_port_interface(mconfig, *this), + device_pty_interface(mconfig, *this), + m_rs232_txbaud(*this, "RS232_TXBAUD"), + m_rs232_rxbaud(*this, "RS232_RXBAUD"), + m_rs232_startbits(*this, "RS232_STARTBITS"), + m_rs232_databits(*this, "RS232_DATABITS"), + m_rs232_parity(*this, "RS232_PARITY"), + m_rs232_stopbits(*this, "RS232_STOPBITS"), + m_input_count(0), + m_input_index(0) +{ +} + +WRITE_LINE_MEMBER(pseudo_terminal_device::update_serial) +{ + int startbits = convert_startbits(m_rs232_startbits->read()); + int databits = convert_databits(m_rs232_databits->read()); + parity_t parity = convert_parity(m_rs232_parity->read()); + stop_bits_t stopbits = convert_stopbits(m_rs232_stopbits->read()); + + set_data_frame(startbits, databits, parity, stopbits); + + int txbaud = convert_baud(m_rs232_txbaud->read()); + set_tra_rate(txbaud); + + int rxbaud = convert_baud(m_rs232_rxbaud->read()); + set_rcv_rate(rxbaud); + + output_rxd(1); + + // TODO: make this configurable + output_dcd(0); + output_dsr(0); + output_cts(0); +} + +static INPUT_PORTS_START(pseudo_terminal) + MCFG_RS232_BAUD("RS232_TXBAUD", RS232_BAUD_9600, "TX Baud", pseudo_terminal_device, update_serial) + MCFG_RS232_BAUD("RS232_RXBAUD", RS232_BAUD_9600, "RX Baud", pseudo_terminal_device, update_serial) + MCFG_RS232_STARTBITS("RS232_STARTBITS", RS232_STARTBITS_1, "Start Bits", pseudo_terminal_device, update_serial) + MCFG_RS232_DATABITS("RS232_DATABITS", RS232_DATABITS_8, "Data Bits", pseudo_terminal_device, update_serial) + MCFG_RS232_PARITY("RS232_PARITY", RS232_PARITY_NONE, "Parity", pseudo_terminal_device, update_serial) + MCFG_RS232_STOPBITS("RS232_STOPBITS", RS232_STOPBITS_1, "Stop Bits", pseudo_terminal_device, update_serial) +INPUT_PORTS_END + +ioport_constructor pseudo_terminal_device::device_input_ports() const +{ + return INPUT_PORTS_NAME(pseudo_terminal); +} + +void pseudo_terminal_device::device_start() +{ + m_timer_poll = timer_alloc(TIMER_POLL); + + if (open()) { +#ifdef FU_TEST + printf("slave PTY = %s\n" , slave_name()); +#endif + } else { +#ifdef FU_TEST + puts("Opening PTY failed"); +#endif + } +} + +void pseudo_terminal_device::device_reset() +{ + update_serial(0); + queue(); +} + +void pseudo_terminal_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) +{ + switch (id) + { + case TIMER_POLL: + queue(); + break; + + default: + device_serial_interface::device_timer(timer, id, param, ptr); + } +} + +void pseudo_terminal_device::tra_callback() +{ + output_rxd(transmit_register_get_data_bit()); +} + +void pseudo_terminal_device::tra_complete() +{ + queue(); +} + +void pseudo_terminal_device::rcv_complete() +{ + receive_register_extract(); + write(get_received_char()); +} + +void pseudo_terminal_device::queue(void) +{ + if (is_transmit_register_empty()) + { + if (m_input_index == m_input_count) + { + m_input_index = 0; + int tmp = read(m_input_buffer , sizeof(m_input_buffer)); + if (tmp > 0) { + m_input_count = tmp; + } else { + m_input_count = 0; + } +#ifdef FU_TEST + if (m_input_count) { + printf("read %u\n" , m_input_count); + } +#endif + } + + if (m_input_count != 0) + { + transmit_register_setup(m_input_buffer[ m_input_index++ ]); + + m_timer_poll->adjust(attotime::never); + } + else + { + int txbaud = convert_baud(m_rs232_txbaud->read()); + m_timer_poll->adjust(attotime::from_hz(txbaud)); + } + } +} + +const device_type PSEUDO_TERMINAL = &device_creator; diff --git a/src/emu/bus/rs232/pty.h b/src/emu/bus/rs232/pty.h new file mode 100644 index 00000000000..75a1c0c2f1a --- /dev/null +++ b/src/emu/bus/rs232/pty.h @@ -0,0 +1,51 @@ +// license:BSD-3-Clause +// copyright-holders:F. Ulivi +// +#ifndef _RS232_PTY_H_ +#define _RS232_PTY_H_ + +#include "rs232.h" + +class pseudo_terminal_device : public device_t, + public device_serial_interface, + public device_rs232_port_interface, + public device_pty_interface +{ +public: + pseudo_terminal_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); + + virtual DECLARE_WRITE_LINE_MEMBER( input_txd ) { + device_serial_interface::rx_w(state); + } + + DECLARE_WRITE_LINE_MEMBER(update_serial); + +protected: + virtual ioport_constructor device_input_ports() const; + virtual void device_start(); + virtual void device_reset(); + virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr); + + virtual void tra_callback(); + virtual void tra_complete(); + virtual void rcv_complete(); + +private: + required_ioport m_rs232_txbaud; + required_ioport m_rs232_rxbaud; + required_ioport m_rs232_startbits; + required_ioport m_rs232_databits; + required_ioport m_rs232_parity; + required_ioport m_rs232_stopbits; + + UINT8 m_input_buffer[ 1024 ]; + UINT32 m_input_count; + UINT32 m_input_index; + emu_timer *m_timer_poll; + + void queue(void); +}; + +extern const device_type PSEUDO_TERMINAL; + +#endif /* _RS232_PTY_H_ */ diff --git a/src/emu/dipty.c b/src/emu/dipty.c new file mode 100644 index 00000000000..197d736b0ad --- /dev/null +++ b/src/emu/dipty.c @@ -0,0 +1,80 @@ +// license:BSD-3-Clause +// copyright-holders:F.Ulivi +/*************************************************************************** + + dipty.h + + Device PTY interface + +***************************************************************************/ + +#include "emu.h" +#include "osdcore.h" + +device_pty_interface::device_pty_interface(const machine_config &mconfig, device_t &device) + : device_interface(device, "pty"), + m_pty_master(NULL), + m_slave_name(), + m_opened(false) +{ +} + +device_pty_interface::~device_pty_interface() +{ +} + +bool device_pty_interface::open(void) +{ + if (!m_opened) { + char buffer[ 128 ]; + + if (osd_openpty(&m_pty_master , buffer , sizeof(buffer)) == FILERR_NONE) { + m_opened = true; + m_slave_name.assign(buffer); + } else { + m_opened = false; + m_pty_master = NULL; + } + } + + return m_opened; +} + +void device_pty_interface::close(void) +{ + if (m_opened) { + osd_close(m_pty_master); + m_opened = false; + } +} + +ssize_t device_pty_interface::read(UINT8 *rx_chars , size_t count) +{ + UINT32 actual_bytes; + + if (m_opened && osd_read(m_pty_master, rx_chars, 0, count, &actual_bytes) == FILERR_NONE) { + return actual_bytes; + } else { + return -1; + } +} + +void device_pty_interface::write(UINT8 tx_char) +{ + UINT32 actual_bytes; + + if (m_opened) { + osd_write(m_pty_master, &tx_char, 0, 1, &actual_bytes); + } +} + +bool device_pty_interface::is_slave_connected(void) const +{ + // TODO: really check for slave status + return m_opened; +} + +const char *device_pty_interface::slave_name(void) const +{ + return m_slave_name.c_str(); +} diff --git a/src/emu/dipty.h b/src/emu/dipty.h new file mode 100644 index 00000000000..cbb4a9cb725 --- /dev/null +++ b/src/emu/dipty.h @@ -0,0 +1,46 @@ +// license:BSD-3-Clause +// copyright-holders:F.Ulivi +/*************************************************************************** + + dipty.h + + Device PTY interface + +***************************************************************************/ + +#pragma once + +#ifndef __EMU_H__ +#error Dont include this file directly; include emu.h instead. +#endif + +#ifndef __DIPTY_H__ +#define __DIPTY_H__ + +class device_pty_interface : public device_interface +{ +public: + // construction/destruction + device_pty_interface(const machine_config &mconfig, device_t &device); + virtual ~device_pty_interface(); + + bool open(void); + void close(void); + + ssize_t read(UINT8 *rx_chars , size_t count); + void write(UINT8 tx_char); + + bool is_slave_connected(void) const; + + const char *slave_name(void) const; + +protected: + osd_file *m_pty_master; + std::string m_slave_name; + bool m_opened; +}; + +// iterator +typedef device_interface_iterator pty_interface_iterator; + +#endif /* __DIPTY_H__ */ diff --git a/src/emu/emu.h b/src/emu/emu.h index 93015100645..969ee94f50e 100644 --- a/src/emu/emu.h +++ b/src/emu/emu.h @@ -78,6 +78,7 @@ typedef device_t * (*machine_config_constructor)(machine_config &config, device_ #include "schedule.h" #include "timer.h" #include "dinetwork.h" +#include "dipty.h" // machine and driver configuration #include "mconfig.h" diff --git a/src/osd/osdcore.h b/src/osd/osdcore.h index c7ae389db03..982cfbf2234 100644 --- a/src/osd/osdcore.h +++ b/src/osd/osdcore.h @@ -165,6 +165,25 @@ file_error osd_read(osd_file *file, void *buffer, UINT64 offset, UINT32 length, -----------------------------------------------------------------------------*/ file_error osd_write(osd_file *file, const void *buffer, UINT64 offset, UINT32 length, UINT32 *actual); +/*----------------------------------------------------------------------------- + osd_openpty: create a new PTY pair + + Parameters: + + file - pointer to an osd_file * to receive the handle of the master + side of the newly-created PTY; this is only valid if the function + returns FILERR_NONE + + name - pointer to memory where slave filename will be stored + + name_len - space allocated for name + + Return value: + + a file_error describing any error that occurred while creating the + PTY, or FILERR_NONE if no error occurred +-----------------------------------------------------------------------------*/ +file_error osd_openpty(osd_file **file, char *name, size_t name_len); /*----------------------------------------------------------------------------- osd_truncate: change the size of an open file diff --git a/src/osd/sdl/sdlfile.c b/src/osd/sdl/sdlfile.c index 74779252641..a93d5623412 100644 --- a/src/osd/sdl/sdlfile.c +++ b/src/osd/sdl/sdlfile.c @@ -359,6 +359,25 @@ file_error osd_write(osd_file *file, const void *buffer, UINT64 offset, UINT32 c } } +//============================================================ +// osd_openpty +//============================================================ + +file_error osd_openpty(osd_file **file, char *name, size_t name_len) +{ + file_error res; + UINT64 filesize; + + if ((res = osd_open(sdlfile_ptty_identifier , 0 , file , &filesize)) != FILERR_NONE) { + return res; + } + + if ((res = sdl_slave_name_ptty(*file , name , name_len)) != FILERR_NONE) { + osd_close(*file); + } + + return res; +} //============================================================ // osd_truncate diff --git a/src/osd/sdl/sdlfile.h b/src/osd/sdl/sdlfile.h index d5f72bfc2ca..4bd1b2a6292 100644 --- a/src/osd/sdl/sdlfile.h +++ b/src/osd/sdl/sdlfile.h @@ -46,5 +46,6 @@ file_error sdl_open_ptty(const char *path, UINT32 openflags, osd_file **file, UI file_error sdl_read_ptty(osd_file *file, void *buffer, UINT64 offset, UINT32 count, UINT32 *actual); file_error sdl_write_ptty(osd_file *file, const void *buffer, UINT64 offset, UINT32 count, UINT32 *actual); file_error sdl_close_ptty(osd_file *file); +file_error sdl_slave_name_ptty(osd_file *file , char *name , size_t name_len); file_error error_to_file_error(UINT32 error); diff --git a/src/osd/sdl/sdlptty_unix.c b/src/osd/sdl/sdlptty_unix.c index 804b0ddae3e..13cd3eb1034 100644 --- a/src/osd/sdl/sdlptty_unix.c +++ b/src/osd/sdl/sdlptty_unix.c @@ -28,6 +28,9 @@ #elif defined(SDLMAME_HAIKU) # include #endif +#if defined(SDLMAME_LINUX) +#include +#endif #include "sdlfile.h" @@ -39,68 +42,87 @@ const char *sdlfile_ptty_identifier = "/dev/pts"; file_error sdl_open_ptty(const char *path, UINT32 openflags, osd_file **file, UINT64 *filesize) { - int master; - int aslave; - char name[100]; + int master; + int aslave; + struct termios tios; + int oldflags; - if (openpty(&master, &aslave, name, NULL, NULL) >= 0) - { - printf("Slave of device %s is %s\n", path, name ); - fcntl(master, F_SETFL, O_NONBLOCK); - (*file)->handle = master; - *filesize = 0; - } - else - { - return FILERR_ACCESS_DENIED; - } + memset(&tios , 0 , sizeof(tios)); + cfmakeraw(&tios); - return FILERR_NONE; + if (openpty(&master, &aslave, NULL, &tios, NULL) >= 0) + { + oldflags = fcntl(master, F_GETFL, 0); + if (oldflags == -1) { + close(master); + return FILERR_FAILURE; + } + + fcntl(master, F_SETFL, oldflags | O_NONBLOCK); + close(aslave); + (*file)->handle = master; + *filesize = 0; + } + else + { + return FILERR_ACCESS_DENIED; + } + + return FILERR_NONE; } file_error sdl_read_ptty(osd_file *file, void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { - ssize_t result; + ssize_t result; - result = read(file->handle, buffer, count); + result = read(file->handle, buffer, count); - if (result < 0) - { - return error_to_file_error(errno); - } + if (result < 0) + { + return error_to_file_error(errno); + } - if (actual != NULL ) - { - *actual = result; - } + if (actual != NULL ) + { + *actual = result; + } - return FILERR_NONE; + return FILERR_NONE; } file_error sdl_write_ptty(osd_file *file, const void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { - ssize_t result; - result = write(file->handle, buffer, count); + ssize_t result; + result = write(file->handle, buffer, count); - if (result < 0) - { - return error_to_file_error(errno); - } + if (result < 0) + { + return error_to_file_error(errno); + } - if (actual != NULL ) - { - *actual = result; - } + if (actual != NULL ) + { + *actual = result; + } - return FILERR_NONE; + return FILERR_NONE; } file_error sdl_close_ptty(osd_file *file) { - close(file->handle); - osd_free(file); + close(file->handle); + osd_free(file); - return FILERR_NONE; + return FILERR_NONE; +} + +file_error sdl_slave_name_ptty(osd_file *file , char *name , size_t name_len) +{ + if (ptsname_r(file->handle , name , name_len) < 0) { + return FILERR_INVALID_ACCESS; + } + + return FILERR_NONE; } #else @@ -110,21 +132,27 @@ const char *sdlfile_ptty_identifier = ""; file_error sdl_open_ptty(const char *path, UINT32 openflags, osd_file **file, UINT64 *filesize) { - return FILERR_ACCESS_DENIED; + return FILERR_ACCESS_DENIED; } file_error sdl_read_ptty(osd_file *file, void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { - return FILERR_ACCESS_DENIED; + return FILERR_ACCESS_DENIED; } file_error sdl_write_ptty(osd_file *file, const void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { - return FILERR_ACCESS_DENIED; + return FILERR_ACCESS_DENIED; } file_error sdl_close_ptty(osd_file *file) { - return FILERR_ACCESS_DENIED; + return FILERR_ACCESS_DENIED; } + +file_error sdl_slave_name_ptty(osd_file *file) +{ + return FILERR_ACCESS_DENIED; +} + #endif