aboutsummaryrefslogtreecommitdiffstats
path: root/src/gb.c
diff options
context:
space:
mode:
authorgbc dev <gbc@localhost>2026-07-15 23:44:28 +0200
committergbc dev <gbc@localhost>2026-07-15 23:44:28 +0200
commit24a015ff047d4b7bb955ba4bc61d5793d7492878 (patch)
treeebaf323a5c530400c81f9ce13db828e9bf6b35e1 /src/gb.c
parentcontrol: video frame capture + deterministic input-movie replay (diff)
downloadsl0pboy-24a015ff047d4b7bb955ba4bc61d5793d7492878.tar.gz
sl0pboy-24a015ff047d4b7bb955ba4bc61d5793d7492878.tar.xz
sl0pboy-24a015ff047d4b7bb955ba4bc61d5793d7492878.zip
serial: headless mode tying serial port to stdio for OS debugging
- --headless: no rendering/TUI; serial TX -> stdout, RX <- stdin (nonblocking) - generalize serial: serial_out_fd/serial_in_fd on the GB; internal-clock transfer now shifts in a real RX byte (from stdin) instead of always 0xFF - paced to native speed by default, --uncapped for turbo; pipes/scripts cleanly - README: document headless serial console
Diffstat (limited to 'src/gb.c')
-rw-r--r--src/gb.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/gb.c b/src/gb.c
index afdcb89..05429fd 100644
--- a/src/gb.c
+++ b/src/gb.c
@@ -4,6 +4,7 @@
#include "cpu.h"
#include <string.h>
#include <stdio.h>
+#include <unistd.h>
void gb_request_interrupt(GB *gb, u8 flag) {
gb->iff |= flag;
@@ -116,9 +117,21 @@ void gb_write(GB *gb, u16 addr, u8 val) {
if (addr == 0xFF02) {
gb->sc = val;
if ((val & 0x81) == 0x81) {
- // transfer with internal clock: log & complete instantly
- if (gb->serial_log) { fputc(gb->sb, stderr); fflush(stderr); }
- gb->sb = 0xFF;
+ // transfer with internal clock (GB is master): shift out gb->sb,
+ // shift in a byte from the peer. Completes instantly.
+ u8 outb = gb->sb;
+ if (gb->serial_log) { fputc(outb, stderr); fflush(stderr); }
+ if (gb->serial_out_fd >= 0) {
+ unsigned char c = outb;
+ (void)!write(gb->serial_out_fd, &c, 1);
+ }
+ // received byte: from serial_in_fd if available, else open-bus 0xFF
+ u8 inb = 0xFF;
+ if (gb->serial_in_fd >= 0) {
+ unsigned char c;
+ if (read(gb->serial_in_fd, &c, 1) == 1) inb = c;
+ }
+ gb->sb = inb;
gb->sc &= ~0x80;
gb_request_interrupt(gb, INT_SERIAL);
}
@@ -204,6 +217,8 @@ void gb_reset(GB *gb) {
c->af = 0x01B0; c->bc = 0x0013; c->de = 0x00D8; c->hl = 0x014D;
}
c->ime = false;
+ gb->serial_out_fd = -1;
+ gb->serial_in_fd = -1;
gb->wram_bank = 1;
gb->joyp_sel = 0x30;
gb->iff = 0xE1;