Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions iodevices/altair-88-sio.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,35 @@ void altair_sio0_data_out(BYTE data)
sio0_stat |= 0b10000000;
}


/*
* Helper function to check and accept incoming socket connection.
* Called from both sio3_status_in() and sio3_data_in() to ensure
* the connection is established regardless of which port is accessed
* first. Fixes a bug where reading data port 7 directly (without
* polling status port 6 first) would always return stale data because
* accept() was only called from the status path.
*/
static void sio3_check_connection(void)
{
struct pollfd p[1];
if (ucons[0].ssc == 0) {
p[0].fd = ucons[0].ss;
p[0].events = POLLIN;
p[0].revents = 0;
poll(p, 1, 0);
if (p[0].revents) {
if ((ucons[0].ssc = accept(ucons[0].ss, NULL, NULL)) == -1) {
LOGW(TAG, "can't accept server socket");
ucons[0].ssc = 0;
}
}
}
}




/*
* read status register
*
Expand All @@ -214,20 +243,7 @@ BYTE altair_sio3_status_in(void)
struct pollfd p[1];

/* if socket not connected check for a new connection */
if (ucons[0].ssc == 0) {
p[0].fd = ucons[0].ss;
p[0].events = POLLIN;
p[0].revents = 0;
poll(p, 1, 0);
/* accept a new connection */
if (p[0].revents) {
if ((ucons[0].ssc = accept(ucons[0].ss, NULL,
NULL)) == -1) {
LOGW(TAG, "can't accept server socket");
ucons[0].ssc = 0;
}
}
}
sio3_check_connection();

sio3_t2 = get_clock_us();
if (sio3_baud_rate > 0 &&
Expand Down Expand Up @@ -268,6 +284,7 @@ BYTE altair_sio3_data_in(void)
static BYTE last;
struct pollfd p[1];

sio3_check_connection();
/* if not connected return last */
if (ucons[0].ssc == 0)
return last;
Expand Down