/*
 *	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_call.h"
#include "state.h"
#include "interpreter.h"
#include "messages.h"
#include "portme.h"
#include "story.h"
#include "variable.h"
#include "memory.h"


void opcodes_call(void) {
	uint8_t  args    = __state.operandCount;
	uint8_t  storeId = ZPEEK(__state.pc++);
	uint32_t routine;
	uint8_t  numLocals;
	int8_t   i;
	uint16_t src;
	uint8_t  dst;

	if ((args == 0) || (__state.operands[0] == 0)) {
		// Legal no-op; store 0 to return value and bounce.
		variableStore(storeId, 0);
	} else {
		routine   = memoryUnpackAddress(__state.operands[0], MEMORY_ROUTINE);
		numLocals = ZPEEK(routine++);
#ifdef DEBUGGINGx
		printf("op0=%u routine=%X, numLocals=%d\n", __state.operands[0], (unsigned int)routine, numLocals);
#endif
		if (numLocals > 15) portDie(MSG_OP_CALL_TOO_MANY_LOCALS, numLocals);

		if (__state.sp + 5 + numLocals >= sizeof(__state.stack)) portDie(MSG_OP_CALL_STACK_OVERFLOW);

		// Save where we should store the call's result.
		__state.stack[__state.sp++] = storeId;

		// Next instruction to run upon return.
		__state.stack[__state.sp++] = __state.pc & 0xffff;
		__state.stack[__state.sp++] = ((__state.pc >> 16) & 0xffff);

		// Current base pointer.
		__state.stack[__state.sp++] = __state.bp;

		// Number of locals.
		__state.stack[__state.sp++] = numLocals;

		// New base pointer.
		__state.bp = __state.sp;

		if (storyVersion() <= 4) {
			for (i=0; i<numLocals; i++, routine += 2) __state.stack[__state.sp++] = ZPEEKW(routine);
		} else {
			for (i=0; i<numLocals; i++)  __state.stack[__state.sp++] = 0;
		}

		// Remove return address from argument count.
		args--;
		// It's legal to have more arguments than locals.  Toss extras.
		if (args > numLocals) args = numLocals;

		// Copy arguments into local variables on stack.
		src = 1;
		dst = __state.bp;
		for (i=0; i<args; i++) {
			__state.stack[dst++] = __state.operands[src++];
		}

		// Move PC to called routine.
		__state.pc = routine;
	}
}


void opcodes_ret(void) {
	interpreterDoReturn(__state.operands[0]);
}

void opcodes_rfalse(void) {
	interpreterDoReturn(0);
}


void opcodes_rtrue(void) {
	interpreterDoReturn(1);
}


void opcodes_ret_popped(void) {
	// Top of stack.
	interpreterDoReturn(variableLoad(0));
}
