/*
 *	Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *	SOFTWARE.
 */


#include "oc_save.h"
#include "interpreter.h"
#include "state.h"
#include "portme.h"
#include "story.h"
#include "opcodes.h"
#include "window.h"
#include "memory.h"
#include "ui.h"


void opcodes_save(void) {
	void     *out;
	bool      ok  = false;
	uint32_t  i;
	byte      b;

	out = portFOpen(uiSaveGet(), "wb");
	if (out) {
		ok = true;
		// Write out PC.
		ok &= (portFWrite(&__state.pc, sizeof(__state.pc), 1, out) == 1);
		// Write out SP.
		ok &= (portFWrite(&__state.sp, sizeof(__state.sp), 1, out) == 1);
		// Write out BP.
		ok &= (portFWrite(&__state.bp, sizeof(__state.bp), 1, out) == 1);
		// Write out dynamic game RAM.
		for (i=0; i<storyStaticMemoryBaseAddress(); i++) {
			b = ZPEEK(i);
			ok &= (portFWrite(&b, 1, 1, out) == 1);
		}
		// Write out stack.
		ok &= (portFWrite(__state.stack, sizeof(__state.stack[0]), __state.sp, out) == __state.sp);
		portFClose(out);
	}

	interpreterDoBranch(ok ? 1 : 0);
}


void opcodes_restart(void) {
	uint16_t checksum = __state.checksum;
	uint32_t length   = uiStorySizeGet();

	stateReset();
	portStoryLoad(uiStoryGet(), &length);  // If they change the file size on the second load, they have more problems than us not updating it.
	__state.checksum = checksum;
	opcodesSetup();
	storySetup();
}


void opcodes_restore(void) {
	void     *in = 0;
	bool      ok = false;
	uint32_t  i;
	byte      b;

	in = portFOpen(uiSaveGet(), "rb");
	if (in) {
		ok = true;
		// Read in PC.
		ok &= (portFRead(&__state.pc, sizeof(__state.pc), 1, in) == 1);
		// Read in SP.
		ok &= (portFRead(&__state.sp, sizeof(__state.sp), 1, in) == 1);
		// Read in BP.
		ok &= (portFRead(&__state.bp, sizeof(__state.bp), 1, in) == 1);
		// Read in dynamic game RAM.
		for (i=0; i<storyStaticMemoryBaseAddress(); i++) {
			ok &= (portFRead(&b, 1, 1, in) == 1);
			ZPOKE(i, b);
		}
		// Read in stack.
		ok &= (portFRead(__state.stack, sizeof(__state.stack[0]), __state.sp, in) == __state.sp);
		portFClose(in);
	}

	if (ok) {
		// Collapse upper window.
		__state.statusWindowLineCount = 0;
		windowSplit(0);
	}

	interpreterDoBranch(ok ? 1 : 0);
}
