Discuss Scratch

Barthdry
Scratcher
500+ posts

How does real emulator are made in scratch

I have no idea how Game Boy emulators are made or any other emulator in scratch and they work almost flawlessly.
Greg8128
Scratcher
500+ posts

How does real emulator are made in scratch

The decide being emulated (for example the GameBoy) has a small set of instructions from which all programs are made. So basically, for each instruction, you need to write some code that runs that instruction correctly. There are some other details, though. Some instructions are a bit tricky to implement in a fast way; I remember that the GameBoy Color emulator “gbc.sb2” had to use a list to store outputs for binary operations (binary AND, XOR, OR) because Scratch doesn't have builtin support for those operations, and computing them by iterating over every binary digit would take too long. Another good technique for increasing performance is, in the code which looks up the code for the instruction being run, to try to ‘balance’ each if-else statement so that the ‘if’ and ‘else’ branches have a roughly equal chance of being run:\


Bad:
if <> then 

else
if <> then

else
if <> then

else

end
end
end
Good:
if <> then 
if <> then

else

end
else
if <> then

else

end
end

Another thing to note is that some games on some devices rely on wasted instructions for timing, so if your emulator doesn't wait for the next frame after emulating a certain amount of CPU cycles then the game won't work correctly. That's as much as I know.

Last edited by Greg8128 (May 24, 2021 10:05:44)


My best projects:

Barthdry
Scratcher
500+ posts

How does real emulator are made in scratch

But is there a way to explain to me how these thing work and it loads hex and many things like that even i saw one project which need the list to be loaded to start emulating.
oxiti8
Scratcher
1000+ posts

How does real emulator are made in scratch

Barthdry wrote:

But is there a way to explain to me how these thing work and it loads hex and many things like that even i saw one project which need the list to be loaded to start emulating.
Hex and lists are all just different ways of converting the rom to a format that can be read by scratch. One emulator uses base64 even.
Lets say you have a rom converted to plain hexadecimal, no newlines or spaces. Say, 0E00F000F265.
First, you need a way to get that data into the program. for a string like this, the easiest way for the user to import it would be for the project to bring up a dialog box, have the user paste or type that hex-converted data, and store the answer..

ask [paste your hex converted rom!] and wait
if <not <(answer) = []>> then
set [ v] to (answer)


else
say [You didn't paste anything!] for (2) secs
end
Now, different emulators do different things to the string to make it more readable. Some affix 0x to the beginning of every two bytes.

Powered by DjangoBB