How Emulators 'Rewind' Games | MVG

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] from the early days of the underground where it was frowned upon and even contested in court the importance of emulation cannot be understated back then few people realized its true value and purpose especially when it comes to preservation but fast forward to 2019 emulation is everywhere the NES and Super NES games on the switch for example the mini consoles that have entered the market over the last couple of years two games on your cell phone throwback games and then hardware devices like the Neo Geo and Capcom arcade sticks all of these are examples and use emulation to provide the gaming experience so why do we even use emulation well there are many different reasons first is for preservation as mentioned some games are out of print too expensive or just so rare that the only recourse is to download an emulator and a copy of the ROM game publishers have been at the forefront of emulation for many years like digital clips and em2 others are starting to come around to this like Square Enix with releases like the collection of mana if we consider the SNK Neo Geo releases that are available on the Nintendo switch each shop acquiring a collection on all original titles that make up this collection on real Neo Geo MVS or worse AES Hardware would cost you a small fortune then there's a convenience factor playing your favorite NES Super NES or Genesis roms on your phone is a very compelling reason there are other things to consider as well if you stream or create content it's a far easier way to show off gameplay rather than connecting up real Hardware and if you don't possess good quality RGB cables or if the system is an RGB modded in the first place then the quality isn't going to look great then there is the hacking mindset any time a new system is hacked the mystery and intrigue is there what can the hardware do can I run Super NES Nintendo 64 PlayStation or even Dreamcast how far can we push the hardware emulation is also important for research right now there is great strides being made to PlayStation 3 Xbox 360 Wii U and even Nintendo switch emulation we don't like to talk about these systems because they are still relatively new and we start to enter a great area of legal issues but they are here and will undoubtedly become important works as time goes on in some cases on the ps3 and xbox360 some titles have been delisted from the individual digital stores and the only way to play these games is now via emulation so emulation is a topic that obviously is very important to me it's something that I've been involved with in the past as well as being very interested in some of the cool techniques that emulation provides that was just not available to original hardware that we're starting to see come up in both commercial products as well as emulation that's been around over the last five years we touched on the convenience of emulators but there are two features that are very useful in emulation that I want to cover both of which have many use cases the first is save States save States is essentially taking a snapshot of the exact state of the emulated machine and stored into a file which is persistent then when you load a save state you are loading the contents of the file and applying the state of the emulated machine back essentially restoring the exact position of the game save states are handy to use especially when playing through a game that doesn't have a battery save or you just beat an extremely hard level or maybe before a tough boss fight to learn its pattern without having to start over the game completely the second is rewind this is when you can go back in time a few seconds in the game maybe you were playing a platformer game and fell down a hole and died holding down rewind will take you back a few seconds to the exact point where you want to continue from even modern racing games like Forza have a rewind feature if you took a corner way too fast and went headfirst into the barrier no worries just hold down the trigger and rewind the game a few seconds and retry and perfect that corner I find both of these concepts interesting and we're going to take a technical look at them so if you've ever wondered how they work a sit back and we're going to demonstrate now for this I'm going to use Game Boy emulation as the target Gameboy is a relatively simple system to emulate with the following specs the CPU is a custom 8-bit sharp process of the LR 35 902 clocked at four point one nine megahertz there's eight kilobytes of video ram eight kilobytes of internal ram and the sound is comprised of two pulse wave generators one noise channel and one PCM 4-bit wave channel the graphics display is 160 by 144 pixels wide the Gameboy CPU is composed of eight different registers registers store data values that the CPU can manipulate when it executes various instructions the Gameboys CPU is an 8-bit CPU meaning that each of its registers can hold eight bits or one byte of data the CPU has eight different registers labeled a b c d e f h and l the cpu has the ability to store 16-bit values as well simply by combining registers together for example AF b c d e and HL are the 16-bit registers that can also be loaded and read from the cpu also contains a program counter and a stack pointer a program counter is a register in the cpu that contains the address of the instruction that's being executed at the current time the most basic way to emulate a system is to reset the program counter to its default value and then execute each hexadecimal value found in a piece of code in this case that code would be a Gameboy ROM with the ROM file we would decode what the instruction is and then interpret how to handle it let's consider the following example this is our gameboy CPU and it has the following values stored in its register let's assume that the hexadecimal value for t1 was read from the ROM and that was the next instruction to be executed by the emulator if we look up 41 in our gameboy CPU instruction set we see that the instruction LD be C this is a load operation which takes the value in register B and stores it in register C so after this instruction has completed then our registers would look something like this once this instruction is complete the program counter is incremented and then the next instruction is read all this is done in a while loop to simulate the execution of the CPU and the machine we're trying to emulate this is about as simple as things will get however an emulation can be more complex than this of course but you get the idea and perhaps we will look at more emulation techniques in a future series on the channel but for now let's go back to save states in order to correctly save the state of the target machine you need to persist the exact state of all CPU registers and more the Gameboy contains registers for video joystick IO the LCD position scrolling registers the color palettes sound timer registers as well as V Ram tiles sprites and background it sounds like a lot to consider and it is but if you cover these areas it should provide with what you need so let's go ahead and put this to the test so as you can see this is our main emulation loop it fetches decodes and interprets the next instruction checks for any interrupts then performs any graphics updates sound and timing updates this is all wrapped up in a loop until the V blank interrupts is triggered and then we break out of this loop and render the current frame buffer now above this loop is where we check for keyboard presses so we're going to add a key press for f1 to save our state and f2 to load it and by using a simple C++ iostream function to write the state of our CPU graphics and other registers and memory to a file this should be enough to handle this for us so let's now add the load state code and then try now with Super Mario Land [Music] as you can see we are able to retrieve the emulation State back to where we saved it this is a basic safe state that almost every emulator out there will use now things can be done in different ways of course but you certainly get the idea so let's move on to rewind let's consider how maybe that will work what if we could somehow store a collection of save States but not in files but in memory in a list or collection and iterate through them in reverse order to simulate pages in a book being flipped from most recent to least recent well we can and this is one way that it can be done and I do stress one way because there's multiple ways you can do this one method is to use a C++ stack which is known as a LIFO queue or last in first out in other words if we consider the analogy of stacking books on top of each other you can only remove the topmost book and when you do it leaves the next recently added book and we can use this same exact approach for rewinding and emulator so let's take the same savestate code we had previously and add additional code to it to add those safe States to a rewind buffer which is essentially a block of RAM that's large enough to store 360 save States or six seconds of rewind at 60 frames per second and then let's add code when we press the key R on the keyboard that rewinds the emulation state by finding the save state on the top of the queue setting its values back to the emulator then popping that saves tale off the stack or removing the book off the top of the pile and then when you hold down R it will then find the next recent frame and perform the same thing over and over again popping frames from the stack and rewinding the game once you release the R key then the game starts gathering frames again in its everyone buffer as you can see I can rewind back in time as much as I want to if I die on the spot here no worries I can just press the R key and then I can go back a little and then try again rewind has only become recently introduced in emulation in the past six years or so in general terms it uses up system memory and resources to manage the buffer but there are also some tricks that you can use to optimize for example only storing the Delta values between two frames rather than capturing the entire machine state every single time emulation is a great tool and with cool tricks like save states and rewind these are just two features that emulation can use to provide a great user experience these were simply not possible with original hardware but they are a welcome addition to allow many older games to become more accessible to more users that may have never experienced the original games so there you have it guys there are the two techniques that I thought would be interesting to let you guys know how they work save States and rewind support both of those two techniques are very important in emulation obviously save states have been around for pretty much the start of emulation from the very first emulators I think save States was something that was pretty trivial to to code but rewind has really only kind of come in to emulation over the last six or seven years as we've got more resources more memory to use more buffer space we can start utilizing things like rewind and have a collage buffer that we can then use to to iterate save state frames and go back X number of seconds in order to rewind and emulated ROM as far back is as we need to in order to you know beat the next section of the game or whatever but guys I want to hear your thoughts in the comments below did you like this episode if so I'll consider making a series on emulation techniques in the future something that I may do in 2020 I want to hear your thoughts in the comments below did you like this let me know now before I go guys I want to wish everyone a safe and happy holiday and a happy new year it's been a fantastic year on the channel and we've got some really cool stuff coming up for 2020 but I just wanted to take a moment to wish everyone a safe and happy holiday and to say thank you for supporting the channel this year it's been absolutely tremendous and we're just going to continue to grow into 2020 and I'm very much looking forward to it well guys we're going to leave it here if you like this video you know what to do leave me a thumbs up and as always don't forget to Like and subscribe and I'll catch you guys in the next video bye for now [Music]
Info
Channel: Modern Vintage Gamer
Views: 448,832
Rating: undefined out of 5
Keywords: emulation, emulators, savestates, rewind, game boy, mvg, modern vintage gamer, emulator, nintendo, retro game, handheld, code, c++, std::stack, development, coding, how stuff works, cpu, microprocessor, registers, ram, buffers
Id: HBnIM2PsC1A
Channel Id: undefined
Length: 13min 32sec (812 seconds)
Published: Mon Dec 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.