GBC serial link

pepposole

New Member
OP
Newbie
Joined
Nov 8, 2016
Messages
4
Trophies
0
Age
44
XP
53
Country
Italy
Hi guys!
I'm studying console emulation and i tried to build my own Classic Gameboy emulator.

Most of the functionality are done and working but i'm stucked with serial link emulation.

The goal is emulate the cable link through the wifi/LAN but i really can't cope with very strict timings of the games.

Most of the time the game go in timeout state because the Wifi network (or the LAN) is not fast enough to deliver data packets from one PC to another.

I know this is possible because it exists an emulator called My Old Boy that work flawlessly in Serial Link emulation mode through my net.

How do you think it works? It create a sort of empty packets in order to avoid timeouts? Or maybe it got a very fast way to send data through the network?

Thank you
Davide
 

mammastuffing

Well-Known Member
Member
Joined
Aug 7, 2015
Messages
172
Trophies
0
Age
34
XP
1,582
Country
How have you currently implemented the link protocol?

I've never written an emulator but I'm thinking you would have to adjust the clock speed in the link communication to match the speed of the network.

Also, I would send the data over UDP for faster communication.
 
Last edited by mammastuffing,

migles

All my gbatemp friends are now mods, except for me
Member
Joined
Sep 19, 2013
Messages
8,033
Trophies
0
Location
Earth-chan
XP
5,299
Country
China
i guess one other way is making the emulation pause while waiting for the packet to receive\sent... and resuming when the computer has the packet...
this can work very well on pokemon games since they only send and receive info when the "please wait" message appears, but for real time games i don't have idea...
 
  • Like
Reactions: Wolfvak

FAST6191

Techromancer
Editorial Team
Joined
Nov 21, 2005
Messages
36,798
Trophies
3
XP
28,284
Country
United Kingdom
Option 2. Modern computers are powerful enough that you can emulate two GB/GBCs at the same time. Emulate them on a host PC and stream the footage (maybe also do something fun with savestates and make it a bit more transparent to the person joining) back to the client. If you are having latency and reliability issues inside the memory bus of a modern computer then you have gone very wrong somewhere. Still might not be ideal for twitchy/speed/reaction games but streaming low resolution video/VNC is a considerably more solved problem.

After this you might explore the dark and murky world of UDP but don't as it will still not get you much further. Generally you are not going to get the latency down, you are going to suffer the odd dropped packet. It is old but damned if it is not still relevant http://www.gamasutra.com/view/feature/131781/the_internet_sucks_or_what_i_.php

I've never written an emulator but I'm thinking you would have to adjust the clock speed in the link communication to match the speed of the network.

The trouble tends to come in the serial cable protocols being rather latency sensitive (as you read this if a packet takes 1000ms to make it to your computer then nobody would care really, for a game that is an age) and rather dropped packet sensitive (there not being many dropped packets in direct 1:1 cables less than 1m long and running on machines that are clocked barely into the MHz). As it is just games then nobody also makes robust code either -- oh dear tetris link failed is not as bad as oh dear bank link failed. For normal consoles that you would have been on the same couch for then you just stream the screen and take in button inputs when you are doing net play.
 

mammastuffing

Well-Known Member
Member
Joined
Aug 7, 2015
Messages
172
Trophies
0
Age
34
XP
1,582
Country
Option 2. Modern computers are powerful enough that you can emulate two GB/GBCs at the same time. Emulate them on a host PC and stream the footage (maybe also do something fun with savestates and make it a bit more transparent to the person joining) back to the client. If you are having latency and reliability issues inside the memory bus of a modern computer then you have gone very wrong somewhere. Still might not be ideal for twitchy/speed/reaction games but streaming low resolution video/VNC is a considerably more solved problem.

I like this approach, although streaming the footage seems a bit too much. I'd probably go for a GGPO like solution in that case, where the input is sent instead for each frame and also use prediction for it. Meaning that you send your input for every frame and for every frame you haven't received input, you assume it was the same as the previous frame. When you receive the real input you might realise that your prediction was wrong. If that is the case, you roll back the game, put the correct input in, and then fast forward the game with all the inputs received up to the current time. I'd recommend that you look at the GGPO implementation if you find it interesting - it's a good solution for twitch games.

After this you might explore the dark and murky world of UDP but don't as it will still not get you much further. Generally you are not going to get the latency down, you are going to suffer the odd dropped packet. It is old but damned if it is not still relevant http://www.gamasutra.com/view/feature/131781/the_internet_sucks_or_what_i_.php

The problem with TCP is that if you actually do suffer packet loss you will have to wait at least one round trip before being able to resend the data. So with a delay of 1000 ms one way, it would take at least 3 seconds before the receiver gets the data if a packet is dropped. With UDP you can write your own protocol. A simple approach would be to buffer every byte and send them all with every packet. With every packet you send you also acknowledge the last one you received. That way you can clear the buffer of every byte that has been acknowledged to been received.
 

pepposole

New Member
OP
Newbie
Joined
Nov 8, 2016
Messages
4
Trophies
0
Age
44
XP
53
Country
Italy
Great ideas!
In the meantime i did some progress but i'm still far from the finish line.

Now i can emulate it, with no problem, two instances of the emulator (connected by serial link) on the same machine.

That's what i do.

Every 8192 CPU cycles, emulator one send serial link state (data, clock and transfer flag) to the other emulator.
Emulator-two do the same and wait for the emulator-one packet (if it's not already received).

If transfer flags are set on both sides and clocks are different, then a serial data interrupt is raised in the same moment (at the same cycles) on both emulators.

It works perfectly but this solution needs to exchange circa 1000 packets per second (since the clock of GB Color could reach 8Mhz).
When the packets are sent through wifi, everything is getting really slow since the avg ping of my network is 3ms.

If i try to exchange serial state every 16384 cycles, latency-sensitive games like Mario Tennis just drop connection.


I also tried to dump traffic generated by two instances of My Old Boy connected through wifi.

This is the PCAP.... and apparently it can keep connection perfectly running just exchanging TCP data even with 40ms+++ latency.


http://www.dbtecno.it/upload/emu.pcap


I'm running out of ideas =(
 
Last edited by pepposole,

pepposole

New Member
OP
Newbie
Joined
Nov 8, 2016
Messages
4
Trophies
0
Age
44
XP
53
Country
Italy
OMG i got a vision thanks to Fast6191 suggestion...

2 instances of the emulator running on machine A and 2 instances running on machine B.

serial link is kept up between the instances on the same machine (getting rid of latency problems)

* machine A *

instance 1 takes input from keyboard
instance 2 takes input through the network from machine B

instance 1 video output is shown
instance 2 got no video output nor sound output

* machine B *

same as machine A

if two machines are CPU-cycles-synced, and input are exchanged in the same moment (and not necessarily VERY fast), machine A and machine B should show the exacly identical video output on screen....

what do you think?
 

FAST6191

Techromancer
Editorial Team
Joined
Nov 21, 2005
Messages
36,798
Trophies
3
XP
28,284
Country
United Kingdom
If you are playing a two humans game of chess, or an otherwise unrelated to each other game (two players playing their own levels that can not trouble each other but will compare high scores at the end or something) then sure.

Trouble would likely arise though if you have random events (say pokemon, and believe me if you are writing an emulator doing link emulation for the general public you will sick of pokemon before all is said and done, has a move with 50% accuracy, virtual coin flip says machine A wins coin flip, B fails coin flip)) and computer AI maybe seeing different results happen, and I am not sure you can rely on any randomness generation being that broken/an expanded version of https://xkcd.com/221/ . At that point the emulators become desynced and you have traded a latency issue for a predictive input issue, something which is a nightmare for a full modern PC game you have coded yourself, let alone a 20 something year old closed source game that would never have dreamed of anything like this running in an emulator.
Worse still what if someone makes a cheat?

You could have some redundancy and sync savestates between them but leave one as a dummy machine until it drops or something, you might also get somewhere if you wanted to sync them with a dominant savestate (I am sure we have all had an online game make us move somewhere else when the lag drops down to a manageable level, same idea) or pause the emulation until one player comes back in.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • K3Nv2 @ K3Nv2:
    Alot of manufactures do care about older consoles they just want to whine about piracy
    +2
  • S @ salazarcosplay:
    @BigOnYa I had 4.89 hfw on super slim that was great, but when I got a new hard disk I forgot where the guide was and could only find a guide for 4.90 and its resources
  • S @ salazarcosplay:
    @BigOnYa I think another reason to want to update is if the hfw is at the level of the fw
  • S @ salazarcosplay:
    you can sync trophies
  • BigOnYa @ BigOnYa:
    Yea that's what I'm sitting on now- 4.9, and it seems fine, have had no issues at all
  • S @ salazarcosplay:
    I don't know if people play online or such
  • K3Nv2 @ K3Nv2:
    My ps3 short circuited during a deep clean still salty about it after downloading 2tbs worth but SteamDeck okay with emulation still just can't run mgs4 worth shit
  • BigOnYa @ BigOnYa:
    Yea forgot bout trophies. They just silly to me. Just like the xbox achievements. Hey, to each they own tho.
  • K3Nv2 @ K3Nv2:
    It keeps players in touch with the game like a check list of things to do after they beat it
  • S @ salazarcosplay:
    @BigOnYa they ruined the gaming experience for me to be honest
  • S @ salazarcosplay:
    @BigOnYa Im not crazy about getting all of them, i feel like I have something to show for for the time put in
  • S @ salazarcosplay:
    @BigOnYa If you want to do rgh or 360 mod
  • S @ salazarcosplay:
    does it matter if you update your 360 or not before trying is it advisable or not
  • BigOnYa @ BigOnYa:
    Yea I don't pay attention to them really. Or do I try to 100% a game. I just play till story ends/ or I get the girl!
  • K3Nv2 @ K3Nv2:
    Bigonya uses his wiener to mod 360s
    +1
  • Xdqwerty @ Xdqwerty:
    Going to the water park, see ya
  • BigOnYa @ BigOnYa:
    You should update the 360 to newest dash before RGHing it yes. But not a big deal if you don't, you can install new dash/avatar updates after. It's just easier to do it auto online before, instead manual offline after.
  • BigOnYa @ BigOnYa:
    Have fun @Xdqwerty. If you see a chocolate candy bar floating in the water, don't eat it!
  • AncientBoi @ AncientBoi:
    :O:ohnoes: Y didn't U Tell ME that ALSO? @BigOnYa :ohnoes: 🤢🤮
    +1
  • BigOnYa @ BigOnYa:
    Does it taste like... chicken?
    +1
  • S @ salazarcosplay:
    @BigOnYa I wanted to ask you about your experience with seeing south park. Most of the people a bit younger like my younger brother and cousins that are a few younger than me that saw kids found south park funny because of the curse words, kids at school, that seemed like liking the show on a very basic level.

    I could not quite have a in depth discussion of the show.

    How was it for you? As an adult. What did you find the most interesting part about it. Did you relate to the parents of the kids and their situations. Was it satires, the commentary on society. The references on celebrities' and pop culture.
  • BigOnYa @ BigOnYa:
    I remember seeing the very first episode back in the day, and have watched every episode since. I used to set my VCR to record them even, shows how long ago.
  • BigOnYa @ BigOnYa:
    I just like any comedies really, and cartoons have always been a favorite of mine. Family guy, American Dad, Futurama, Cleveland Show, Simpsons - I like them all.
  • BigOnYa @ BigOnYa:
    South Park is great cause they always touch on relavent issues going on today, and make something funny out of it.
    +1
    BigOnYa @ BigOnYa: South Park is great cause they always touch on relavent issues going on today, and make... +1