Save States - Proof of concept

Here you can discuss the development of patches

Re: Save States - Proof of concept

Postby jal » 2010-10-13 @ 14:28

Manwe wrote:Thanks for your answer, JAL, but I already read the thread


Not well enough, e.g. on page 3 there's mentioning of saving to disk, to which I replied:

jal wrote:As I understand it, ZenJu is perfectly capable of writing the states to disk, but since he does not save the entire state of DOSBox (especially hardware, whose state is usually rather static while a game is running, but definitely not while, say, another game is), saving to disk would not allow restoring the game again (at least not without first starting the game again).


That should've answered your question, right?


JAL
User avatar
jal
Member
 
Posts: 488
Joined: 2004-1-02 @ 15:30
Location: Amersfoort, Netherlands

Re: Save States - Proof of concept

Postby ykhwong » 2010-12-16 @ 18:14

This can save state to disk, specifically to directory /save/1 to 10. It simply works only in current DOSBox instance though. Still experimental.

In save_state.cpp
Code: Select all
#include <fstream>

void SaveState::save(size_t slot) { //throw (Error)
    if (slot >= SLOT_COUNT)  return;

    try {
        for (CompEntry::iterator i = components.begin(); i != components.end(); ++i) {
            std::ostringstream ss;
            i->second.comp.getBytes(ss);
            i->second.rawBytes[slot].set(ss.str());
            char temp[20] = "./save/";
            int realslot = slot+1;
            char realslot_str[20];
            itoa(realslot,realslot_str,10);
            strcat(temp,realslot_str);
            strcat(temp,"/");
            strcat(temp,i->first.c_str());
            std::ofstream outfile (temp, std::ofstream::binary);
            outfile << ss.str();
            //compress all other saved states except position "slot"
            const std::vector<RawBytes>& rb = i->second.rawBytes;
            std::for_each(rb.begin(), rb.begin() + slot, std::mem_fun_ref(&RawBytes::compress));
            std::for_each(rb.begin() + slot + 1, rb.end(), std::mem_fun_ref(&RawBytes::compress));
            outfile.close();
        }
    }
    catch (const std::bad_alloc&)
    {
        throw Error("Save failed! Out of Memory!");
    }
}


void SaveState::load(size_t slot) const { //throw (Error)
//    if (isEmpty(slot)) return;
    for (CompEntry::const_iterator i = components.begin(); i != components.end(); ++i) {
        std::filebuf * fb;
        std::ifstream ss;
        std::ifstream check_file;
        fb = ss.rdbuf();
        char temp[20] = "./save/";
        int realslot = slot+1;
        char realslot_str[20];
        itoa(realslot,realslot_str,10);
        strcat(temp,realslot_str);
        strcat(temp,"/");
        strcat(temp,i->first.c_str());

        check_file.open(temp, std::ifstream::in);
        check_file.close();
        if(check_file.fail()) throw Error("No saved file for this slot. Aborted.");

        fb->open(temp,std::ios::in | std::ios::binary);
        i->second.comp.setBytes(ss);
        if (fb->in_avail() != 0 || ss.eof()) { //basic consistency check
            throw Error("Save state corrupted! Program in inconsistent state!") + " - " + i->first;
        }
        //compress all other saved states except position "slot"
        const std::vector<RawBytes>& rb = i->second.rawBytes;
        std::for_each(rb.begin(), rb.begin() + slot, std::mem_fun_ref(&RawBytes::compress));
        std::for_each(rb.begin() + slot + 1, rb.end(), std::mem_fun_ref(&RawBytes::compress));
        fb->close();
    }
}
ykhwong
Oldbie
 
Posts: 617
Joined: 2004-3-17 @ 15:35

Re: Save States - Proof of concept

Postby Neville » 2010-12-16 @ 18:29

Interesting... are you adding this patch to your unofficial version of DOSBox?
User avatar
Neville
Oldbie
 
Posts: 803
Joined: 2004-8-09 @ 14:01

Re: Save States - Proof of concept

Postby ykhwong » 2010-12-16 @ 18:31

Neville wrote:Interesting... are you adding this patch to your unofficial version of DOSBox?

Yes, you can find save directory in my current release.

Several files are genereated in the location and their names explain what they mean. I can make it a single saved file by flushing buffer when saving but loading states procedure will become more complex.
ykhwong
Oldbie
 
Posts: 617
Joined: 2004-3-17 @ 15:35

Re: Save States - Proof of concept

Postby Xelasarg » 2010-12-17 @ 20:32

Nice one, ykhwong!

This works surprisingly well. Maybe it'll never be stable enough to be integrated into an official release, but it's a fine addition to your experimental builds.
"What's a paladin?!
User avatar
Xelasarg
Member
 
Posts: 240
Joined: 2003-9-19 @ 09:10
Location: In the middle of something

Re: Save States - Proof of concept

Postby ykhwong » 2010-12-31 @ 21:18

Tiny updates for saving to disk. All credits go to ZenJu.
- States can be saved to or loaded from disk.
- Added a verification code to check if states are properly saved to disk.
- Creates a save directory if it does not exist.
- Saves a name of program running in DOSBox.
- Does not save CPU paging part and does not register POD of memory function as they disturb loading states in another instance.

Saved state files are reusable even after DOSBox exits.

Special key review:
Left ALT + F5 -> Save state / Left ALT + F9 -> Load state
Left Alt + F6 -> Previous slot / Left ALT + F7 -> Next slot
You do not have the required permissions to view the files attached to this post.
ykhwong
Oldbie
 
Posts: 617
Joined: 2004-3-17 @ 15:35

Re: Save States - Proof of concept

Postby Bisqwit » 2011-1-23 @ 12:43

For reference, here is a link to the savestate patch that I created a few years back. viewtopic.php?t=15047

My savestates could be saved on the disk, and loaded from there, even across dosbox close & open, and it had movie recording support, but movies tended to desync if loaded in a computer with different speed specifications.
It addressed the re-entrancy problem by disabling save&load function while re-entrant (during DOS input wait INT 21h for example), and it addressed the file I/O consistency by implementing an emulation layer over the I/O which virtualizes the whole I/O layer, preserving a delta over the state where files where when dosbox was started (no actual files are thus modified). I don't remember how I solved the dilemma with callback function pointers.

Just providing it here to allow possibly some insight that might help develop this currently studied candidate further.
Image
Bisqwit
Newbie
 
Posts: 13
Joined: 2007-4-02 @ 07:48
Location: Kerava, Finland

Re: Save States - Proof of concept

Postby wd » 2011-1-28 @ 00:35

At around that time i committed several changes to avoid machine recursions, but the
main ones remain (paging and shells, the latter being the more relevant since it can block
saving and loading completely).

There are some bits and pieces that i wasn't too happy about back then, like having some
layers over existing code rather than some more integrated solution, but that may or may
not be relevant.
wd
DOSBox Author
 
Posts: 10819
Joined: 2003-12-03 @ 21:23

Re: Save States - Proof of concept

Postby al3xmeister » 2011-2-27 @ 13:26

I've tried to install dosbox but with the save state proof it displays the "sdl_net not installed" error. any suggestions?
al3xmeister
Newbie
 
Posts: 9
Joined: 2010-7-12 @ 12:56

Re: Save States - Proof of concept

Postby ripa » 2011-2-27 @ 16:52

Do you have SDL_net.dll in the same directory as dosbox.exe? It should work just fine if you install regular dosbox, then put the new exe into the directory. You can rename the exe so you can have vanilla dosbox and save-state dosbox installed in the same directory.
ripa
Member
 
Posts: 426
Joined: 2005-4-18 @ 00:53
Location: Finland

Re: Save States - Proof of concept

Postby al3xmeister » 2011-2-27 @ 18:18

I've done it, it works, thanks
al3xmeister
Newbie
 
Posts: 9
Joined: 2010-7-12 @ 12:56

Re: Save States - Proof of concept

Postby jez » 2012-4-07 @ 14:22

Any updates on whether something like this might make it into the main build anytime soon? There are some games with dodgy save/load mechanisms that can cause corruption; a solid DOSBox save state mechanism would be awesome as a save/load replacement for these games (yeah yeah, the quality of some DOS programmers' code sucked...)
== Jez ==
User avatar
jez
Member
 
Posts: 159
Joined: 2002-9-24 @ 20:40

Re: Save States - Proof of concept

Postby Geektastic » 2012-4-14 @ 22:29

ask wd. Really though, no probably not anytime soon. ZenJu was only here for about a month, and his last post was almost two years ago. He may have gotten dismayed that wd didn't want save states in the mainline code (at that time). Kind of like Bisqwit's position, in that he was quite motivated to code this at one time, but that post above was his last post here, too. Still, ZenJu's implementation is certainly usable for most games and goes beyond a "proof of concept"! If you require save-to-disk saved states, wkhwong patched the code for this, linked above. So I guess I should ask: What software did you try that didn't work with beta9? If you haven't tried, please do. Do not assume because it's "old" it doesn't work anymore - it does.
User avatar
Geektastic
Newbie
 
Posts: 1
Joined: 2012-4-14 @ 21:56

Re: Save States - Proof of concept

Postby tikalat » 2012-4-18 @ 02:51

Thank you for the experimental save states code.

I think they are really practical for me (even if a temporary solution at this stage).


I'm (very slowly) going through some of the ykhwong save state version. To make it a little more useful for what I play.



This one adds palette states. Example is (sometimes) loading up states from the Gobliiins series (random incorrect colors).

Patch seems to fix this up atm.

Code: Select all
ykhwong svn-daum 2012-02-20


gui/render.cpp

(add to end of file)
#include "../save_state.h"

//save state support
namespace
{
class SerializeRender : public SerializeGlobalPOD
{
public:
    SerializeRender() : SerializeGlobalPOD("Render")
    {
         registerPOD(render.pal);
    }
} dummy;
}
You do not have the required permissions to view the files attached to this post.
tikalat
Member
 
Posts: 287
Joined: 2012-4-13 @ 16:39

Re: Save States - Proof of concept

Postby tikalat » 2012-4-18 @ 17:11

Another small improvement. This time for mouse cursor.

Sometimes Lemmings 2 can act goofy with the mouse, allowing restricted cursor movement on loadstate.

This will resize the 'mouse window' on savestate.

Code: Select all
ykhwong svn-daum 2012-02-20

(ints/mouse.cpp)

(add to end of file)
#include "../save_state.h"


//save state support
namespace
{
class SerializeMouse : public SerializeGlobalPOD
{
public:
    SerializeMouse() : SerializeGlobalPOD("Mouse")
    {
         registerPOD(mouse.min_x);
         registerPOD(mouse.min_y);
         registerPOD(mouse.max_x);
         registerPOD(mouse.max_y);
         registerPOD(mouse.clipx);
         registerPOD(mouse.clipy);
    }
} dummy;
}
You do not have the required permissions to view the files attached to this post.
tikalat
Member
 
Posts: 287
Joined: 2012-4-13 @ 16:39

Re: Save States - Proof of concept

Postby Jorpho » 2012-4-18 @ 17:25

jez wrote:There are some games with dodgy save/load mechanisms that can cause corruption
Can you be more specific?
User avatar
Jorpho
l33t
 
Posts: 4167
Joined: 2003-2-14 @ 19:50
Location: Canada

Re: Save States - Proof of concept

Postby Dominus » 2012-4-18 @ 17:28

Isn't tikalat giving enough examples?;)
User avatar
Dominus
DOSBox Moderator
 
Posts: 5591
Joined: 2002-10-03 @ 09:54
Location: Vienna

Re: Save States - Proof of concept

Postby Jorpho » 2012-4-18 @ 18:01

Tikalat is giving examples of games that have problems with the DOSBox savestate code. I understood Jez as referring to games whose own internal code has problems loading and saving games using their own mechanisms.
User avatar
Jorpho
l33t
 
Posts: 4167
Joined: 2003-2-14 @ 19:50
Location: Canada

Re: Save States - Proof of concept

Postby Dominus » 2012-4-18 @ 18:32

Sorry, you are right ;)
I can't name any now but I know I wished for a savestate feature in a couple of games.
But I can't think of one where the savegames were prone to corruption.
User avatar
Dominus
DOSBox Moderator
 
Posts: 5591
Joined: 2002-10-03 @ 09:54
Location: Vienna

Re: Save States - Proof of concept

Postby ykhwong » 2012-4-19 @ 02:42

Hi, tikalat. Thanks for the patches. I think one of the important things to investigate is palette corruption that still needs to be fixed.
ykhwong
Oldbie
 
Posts: 617
Joined: 2004-3-17 @ 15:35

PreviousNext

Return to DOSBox Patches

Who is online

Users browsing this forum: No registered users and 2 guests