summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/save.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/emu/save.cpp')
-rw-r--r--src/emu/save.cpp410
1 files changed, 220 insertions, 190 deletions
diff --git a/src/emu/save.cpp b/src/emu/save.cpp
index eff71cd6870..eb1fd704571 100644
--- a/src/emu/save.cpp
+++ b/src/emu/save.cpp
@@ -79,7 +79,12 @@ void save_manager::allow_registration(bool allowed)
// allow/deny registration
m_reg_allowed = allowed;
if (!allowed)
+ {
dump_registry();
+
+ // everything is registered by now, evaluate the savestate size
+ m_rewind->clamp_capacity();
+ }
}
@@ -567,279 +572,304 @@ rewinder::rewinder(save_manager &save)
: m_save(save)
, m_enabled(save.machine().options().rewind())
, m_capacity(save.machine().options().rewind_capacity())
+ , m_current_index(REWIND_INDEX_NONE)
+ , m_first_invalid_index(REWIND_INDEX_NONE)
+ , m_first_time_warning(true)
+ , m_first_time_note(true)
{
}
//-------------------------------------------------
-// check_size - shrink the state list if it is
-// about to hit the capacity
+// clamp_capacity - safety checks for commandline
+// override
//-------------------------------------------------
-void rewinder::check_size()
+void rewinder::clamp_capacity()
{
- // safety check that shouldn't be allowed to trigger
- if (m_state_list.size() > m_capacity)
- {
- // drop all states beyond capacity
- uint32_t count = m_state_list.size() - m_capacity;
- m_state_list.erase(m_state_list.begin(), m_state_list.begin() + count);
- }
+ if (!m_enabled)
+ return;
- // check if we're about to hit capacity
- if (m_state_list.size() == m_capacity)
- {
- // check the last state
- ram_state *last = m_state_list.back().get();
+ const size_t total = m_capacity * 1024 * 1024;
+ const size_t single = ram_state::get_size(m_save);
- // if we're not on top of the list, no need to move states around
- if (!last->m_valid)
- return;
+ // can't set below zero, but allow commandline to override options' upper limit
+ if (total < 0)
+ m_capacity = 0;
- // we can now get the first state and invalidate it
- std::unique_ptr<ram_state> first(std::move(m_state_list.front()));
- first->m_valid = false;
-
- // move it to the end for future use
- m_state_list.push_back(std::move(first));
- m_state_list.erase(m_state_list.begin());
+ // if capacity is below savestate size, can't save anything
+ if (total < single)
+ {
+ m_enabled = false;
+ m_save.machine().logerror("Rewind has been disabled, because rewind capacity is smaller than savestate size.\n");
+ m_save.machine().logerror("Rewind buffer size: %d bytes. Savestate size: %d bytes.\n", total, single);
+ m_save.machine().popmessage("Rewind has been disabled. See error.log for details");
}
}
//-------------------------------------------------
-// report_error - report rewind success or
-// error type
+// invalidate - mark all the future states as
+// invalid to prevent loading them, as the
+// current input might have changed
//-------------------------------------------------
-void rewinder::report_error(save_error error, rewind_operation operation, int index)
+void rewinder::invalidate()
{
- const char *const opname = (operation == rewind_operation::LOAD) ? "load" : "save";
+ if (!m_enabled)
+ return;
- switch (error)
+ // is there anything to invalidate?
+ if (!current_index_is_last())
{
- // internal saveload failures
- case STATERR_ILLEGAL_REGISTRATIONS:
- m_save.machine().popmessage("Error: Unable to %s state due to illegal registrations. See error.log for details.", opname);
- break;
+ // all states starting from the current one will be invalid
+ m_first_invalid_index = m_current_index;
- case STATERR_INVALID_HEADER:
- m_save.machine().popmessage("Error: Unable to %s state due to an invalid header. Make sure the save state is correct for this machine.", opname);
- break;
+ // actually invalidate
+ for (auto it = m_state_list.begin() + m_first_invalid_index; it < m_state_list.end(); ++it)
+ it->get()->m_valid = false;
+ }
+}
- case STATERR_READ_ERROR:
- m_save.machine().popmessage("Error: Unable to %s state due to a read error.", opname);
- break;
- case STATERR_WRITE_ERROR:
- m_save.machine().popmessage("Error: Unable to %s state due to a write error.", opname);
- break;
+//-------------------------------------------------
+// capture - record a single state, returns true
+// on success
+//-------------------------------------------------
- // external saveload failures
- case STATERR_NOT_FOUND:
- if (operation == rewind_operation::LOAD)
- m_save.machine().popmessage("No rewind state to load.");
- break;
+bool rewinder::capture()
+{
+ if (!m_enabled)
+ {
+ report_error(STATERR_DISABLED, rewind_operation::SAVE);
+ return false;
+ }
- // success
- case STATERR_NONE:
+ if (current_index_is_last())
+ {
+ // we need to create a new state
+ std::unique_ptr<ram_state> state = std::make_unique<ram_state>(m_save);
+ const save_error error = state->save();
+
+ // validate the state
+ if (error == STATERR_NONE)
+ // it's safe to append
+ m_state_list.push_back(std::move(state));
+ else
{
- const char *const warning = (m_save.machine().system().flags & MACHINE_SUPPORTS_SAVE) ? ""
- : "\nWarning: Save states are not officially supported for this machine.";
+ // internal error, complain and evacuate
+ report_error(error, rewind_operation::SAVE);
+ return false;
+ }
+ }
+ else
+ {
+ // invalidate the future states
+ invalidate();
- // figure out the qantity of valid states
- int invalid = get_first_invalid_index();
+ // update the existing state
+ ram_state *state = m_state_list.at(m_current_index).get();
+ const save_error error = state->save();
+
+ // validate the state
+ if (error != STATERR_NONE)
+ {
+ // internal error, complain and evacuate
+ report_error(error, rewind_operation::SAVE);
+ return false;
+ }
+ }
- // all states are valid
- if (invalid == REWIND_INDEX_NONE)
- invalid = m_state_list.size();
+ // make sure we will fit in
+ if (!check_size())
+ // the list keeps growing
+ m_current_index++;
- if (operation == rewind_operation::SAVE)
- m_save.machine().popmessage("Rewind state %i captured.\nRewind state count: %i.%s", invalid - 1, invalid, warning);
- else
- m_save.machine().popmessage("Rewind state %i loaded.\nRewind state count: %i.%s", index, invalid, warning);
- }
- break;
+ // update first invalid index
+ if (current_index_is_last())
+ m_first_invalid_index = REWIND_INDEX_NONE;
+ else
+ m_first_invalid_index = m_current_index + 1;
- // something that shouldn't be allowed to happen
- default:
- m_save.machine().popmessage("Error: Unknown error during state %s.", opname);
- break;
- }
+ // success
+ report_error(STATERR_NONE, rewind_operation::SAVE);
+ return true;
}
//-------------------------------------------------
-// get_current_index - get the index of the
-// state to assume as current
+// step - single step back in time, returns true
+// on success
//-------------------------------------------------
-int rewinder::get_current_index()
+bool rewinder::step()
{
- // nowhere to search
- if (m_state_list.empty())
- return REWIND_INDEX_NONE;
+ if (!m_enabled)
+ {
+ report_error(STATERR_DISABLED, rewind_operation::LOAD);
+ return false;
+ }
- // fetch the current machine time
- attotime curtime = m_save.machine().time();
+ // do we have states to load?
+ if (m_current_index <= REWIND_INDEX_FIRST || m_first_invalid_index == REWIND_INDEX_FIRST)
+ {
+ // no valid states, complain and evacuate
+ report_error(STATERR_NOT_FOUND, rewind_operation::LOAD);
+ return false;
+ }
- // find the state at the current time, or at least the first one after
- for (auto it = m_state_list.begin(); it < m_state_list.end(); ++it)
- if (it->get()->m_time >= curtime)
- return it - m_state_list.begin();
+ // prepare to load the last valid index if we're too far ahead
+ if (m_first_invalid_index > REWIND_INDEX_NONE && m_current_index > m_first_invalid_index)
+ m_current_index = m_first_invalid_index;
- // all states are older
- return REWIND_INDEX_NONE;
-}
+ // step back and obtain the state pointer
+ ram_state *state = m_state_list.at(--m_current_index).get();
+ // try to load and report the result
+ const save_error error = state->load();
+ report_error(error, rewind_operation::LOAD);
-//-------------------------------------------------
-// get_first_invalid_index - get the index of the
-// first invalid state
-//-------------------------------------------------
-
-int rewinder::get_first_invalid_index()
-{
- for (auto it = m_state_list.begin(); it < m_state_list.end(); ++it)
- if (!it->get()->m_valid)
- return it - m_state_list.begin();
+ if (error == save_error::STATERR_NONE)
+ return true;
- // all states are valid
- return REWIND_INDEX_NONE;
+ return false;
}
//-------------------------------------------------
-// invalidate - mark all the future states as
-// invalid
+// check_size - shrink the state list if it is
+// about to hit the capacity. returns true if
+// the list got shrank
//-------------------------------------------------
-int rewinder::invalidate()
+bool rewinder::check_size()
{
- // fetch the current state index
- int index = get_current_index();
+ if (!m_enabled)
+ false;
- // more invalid states may be farther back, account for them too
- int invalid = get_first_invalid_index();
+ // state sizes in bytes
+ const size_t singlesize = ram_state::get_size(m_save);
+ size_t totalsize = m_state_list.size() * singlesize;
- // roll back if we can
- if (invalid != REWIND_INDEX_NONE && (index == REWIND_INDEX_NONE || invalid < index))
- index = invalid;
+ // convert our limit from megabytes
+ const size_t capsize = m_capacity * 1024 * 1024;
- if (index != REWIND_INDEX_NONE)
+ // safety check that shouldn't be allowed to trigger
+ if (totalsize > capsize)
{
- // if it's the last state in the list, skip further invalidation
- if (++index >= m_state_list.size())
- return index;
+ // states to remove
+ const u32 count = (totalsize - capsize) / singlesize;
- // invalidate all the future states, as the current input might have changed
- for (auto it = m_state_list.begin() + index; it < m_state_list.end(); ++it)
- it->get()->m_valid = false;
+ // drop everything that's beyond capacity
+ m_state_list.erase(m_state_list.begin(), m_state_list.begin() + count);
}
- // index of the first invalid state
- return index;
-}
+ // update before new check
+ totalsize = m_state_list.size() * singlesize;
+ // check if capacity will be hit by the newly captured state
+ if (totalsize + singlesize >= capsize)
+ {
+ // check if we have spare states ahead
+ if (!current_index_is_last())
+ // no need to move states around
+ return false;
-//-------------------------------------------------
-// capture - record a single state
-//-------------------------------------------------
-
-void rewinder::capture()
-{
- // fetch the current state index and invalidate the future states
- int index = invalidate();
+ // we can now get the first state and invalidate it
+ std::unique_ptr<ram_state> first(std::move(m_state_list.front()));
+ first->m_valid = false;
- if (index == REWIND_INDEX_NONE)
- {
- // no current state, create one
- std::unique_ptr<ram_state> state = std::make_unique<ram_state>(m_save);
- save_error error = state->save();
+ // move it to the end for future use
+ m_state_list.push_back(std::move(first));
+ m_state_list.erase(m_state_list.begin());
- // validate the state
- if (error == STATERR_NONE)
+ if (m_first_time_note)
{
- // it's safe to append
- m_state_list.push_back(std::move(state));
- }
- else
- {
- // internal error, complain and evacuate
- report_error(error, rewind_operation::SAVE);
- return;
- }
- }
- else
- {
- // update the existing state
- ram_state *state = m_state_list.at(--index).get();
- save_error error = state->save();
-
- // validate the state
- if (error != STATERR_NONE)
- {
- // internal error, complain and evacuate
- report_error(error, rewind_operation::SAVE);
- return;
+ m_save.machine().logerror("Rewind note: Capacity has been reached. Old savestates will be erased.\n");
+ m_save.machine().logerror("Capacity: %d bytes. Savestate size: %d bytes. Savestate count: %d.\n",
+ totalsize, singlesize, m_state_list.size());
+ m_first_time_note = false;
}
- }
- // make sure we still fit in
- check_size();
+ return true;
+ }
- // success
- report_error(STATERR_NONE, rewind_operation::SAVE);
+ return false;
}
//-------------------------------------------------
-// step - single step back in time
+// report_error - report rewind results
//-------------------------------------------------
-void rewinder::step()
+void rewinder::report_error(save_error error, rewind_operation operation)
{
- // check presence of states
- if (m_state_list.empty())
+ const char *const opname = (operation == rewind_operation::LOAD) ? "load" : "save";
+ switch (error)
{
- // no states, complain and evacuate
- report_error(STATERR_NOT_FOUND, rewind_operation::LOAD);
- return;
- }
+ // internal saveload failures
+ case STATERR_ILLEGAL_REGISTRATIONS:
+ m_save.machine().logerror("Rewind error: Unable to %s state due to illegal registrations.", opname);
+ m_save.machine().popmessage("Rewind error occured. See error.log for details.");
+ break;
- // fetch the current state index
- int index = get_current_index();
+ case STATERR_INVALID_HEADER:
+ m_save.machine().logerror("Rewind error: Unable to %s state due to an invalid header. "
+ "Make sure the save state is correct for this machine.\n", opname);
+ m_save.machine().popmessage("Rewind error occured. See error.log for details.");
+ break;
- // if there is room, retreat
- if (index != REWIND_INDEX_FIRST)
- {
- // we may be on top of the list, when all states are older
- if (index == REWIND_INDEX_NONE)
+ case STATERR_READ_ERROR:
+ m_save.machine().logerror("Rewind error: Unable to %s state due to a read error.\n", opname);
+ m_save.machine().popmessage("Rewind error occured. See error.log for details.");
+ break;
+
+ case STATERR_WRITE_ERROR:
+ m_save.machine().logerror("Rewind error: Unable to %s state due to a write error.\n", opname);
+ m_save.machine().popmessage("Rewind error occured. See error.log for details.");
+ break;
+
+ // external saveload failures
+ case STATERR_NOT_FOUND:
+ if (operation == rewind_operation::LOAD)
+ {
+ m_save.machine().logerror("Rewind error: No rewind state to load.\n");
+ m_save.machine().popmessage("Rewind error occured. See error.log for details.");
+ }
+ break;
+
+ case STATERR_DISABLED:
+ if (operation == rewind_operation::LOAD)
{
- // use the last consecutively valid state, to ensure rewinder integrity
- index = get_first_invalid_index();
+ m_save.machine().logerror("Rewind error: Rewind is disabled.\n");
+ m_save.machine().popmessage("Rewind error occured. See error.log for details.");
+ }
+ break;
- if (index == REWIND_INDEX_NONE)
- // all states are valid
- index = m_state_list.size();
- else if (index == REWIND_INDEX_FIRST)
+ // success
+ case STATERR_NONE:
+ {
+ const u64 supported = m_save.machine().system().flags & MACHINE_SUPPORTS_SAVE;
+ const char *const warning = supported || !m_first_time_warning ? "" :
+ "Rewind warning: Save states are not officially supported for this machine.\n";
+ const char *const opnamed = (operation == rewind_operation::LOAD) ? "loaded" : "captured";
+
+ // for rewinding outside of debugger, give some indication that rewind has worked, as screen doesn't update
+ m_save.machine().popmessage("Rewind state %i %s.\n%s", m_current_index + 1, opnamed, warning);
+ if (m_first_time_warning && operation == rewind_operation::LOAD && !supported)
{
- // no valid states, complain and evacuate
- report_error(STATERR_NOT_FOUND, rewind_operation::LOAD);
- return;
+ m_save.machine().logerror(warning);
+ m_first_time_warning = false;
}
}
+ break;
- // obtain the state pointer
- ram_state *state = m_state_list.at(--index).get();
-
- // try to load and report the result
- report_error(state->load(), rewind_operation::LOAD, index);
- return;
+ // something that shouldn't be allowed to happen
+ default:
+ m_save.machine().logerror("Error: Unknown error during state %s.\n", opname);
+ m_save.machine().popmessage("Rewind error occured. See error.log for details.");
+ break;
}
-
- // no valid states, complain
- report_error(STATERR_NOT_FOUND, rewind_operation::LOAD);
}