diff options
Diffstat (limited to 'src/gb.c')
| -rw-r--r-- | src/gb.c | 21 |
1 files changed, 18 insertions, 3 deletions
@@ -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; |
