- Enhanced isometric_controls_tests.cpp to include assertions for controller fire attempt suppression under various conditions. - Added tests for the Usecode interpreter in usecode_vm_tests.cpp, verifying script execution and stack manipulation. - Introduced a new PowerShell script, clean-old-build-artifacts.ps1, to facilitate the cleanup of old build artifacts in specified directories.
874 lines
No EOL
35 KiB
C++
874 lines
No EOL
35 KiB
C++
#include "remorse/usecode_vm.h"
|
|
#include "remorse/usecode_archive.h"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace remorse {
|
|
namespace {
|
|
|
|
void writeWord(u8* bytes, std::size_t offset, u16 value)
|
|
{
|
|
bytes[offset] = static_cast<u8>(value);
|
|
bytes[offset + 1] = static_cast<u8>(value >> 8);
|
|
}
|
|
|
|
void writeFarPointer(u8* bytes, std::size_t offset, FarPtr16 pointer)
|
|
{
|
|
writeWord(bytes, offset, pointer.offset);
|
|
writeWord(bytes, offset + sizeof(u16), pointer.segment);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::optional<UsecodeInterpreterState> UsecodeInterpreterState::initialize(
|
|
const std::vector<u8>& itemParameter, const std::vector<u8>& contextParameter,
|
|
u16 modeFlags, bool contextParameterPresent)
|
|
{
|
|
const bool saveStackPointer = (!contextParameter.empty() || contextParameterPresent) && modeFlags != 0;
|
|
const std::size_t savedPointerBytes = saveStackPointer ? sizeof(FarPtr16) : 0;
|
|
if (itemParameter.size() > usecodeInterpreterStackLimit ||
|
|
contextParameter.size() > usecodeInterpreterStackLimit - itemParameter.size() ||
|
|
savedPointerBytes > usecodeInterpreterStackLimit - itemParameter.size() - contextParameter.size()) {
|
|
return std::nullopt;
|
|
}
|
|
UsecodeInterpreterState state;
|
|
state.stackHighWatermark = state.valueStack.size();
|
|
const auto pushBytes = [&state](const std::vector<u8>& bytes) {
|
|
state.stackTop -= bytes.size();
|
|
std::copy(bytes.begin(), bytes.end(), state.valueStack.begin() + state.stackTop);
|
|
};
|
|
pushBytes(itemParameter);
|
|
const std::size_t stackTopBeforeContextParameter = state.stackTop;
|
|
pushBytes(contextParameter);
|
|
if (saveStackPointer) {
|
|
state.stackTop -= savedPointerBytes;
|
|
state.contextParameterOffset = static_cast<u16>(state.stackTop - sizeof(u16) * 2);
|
|
state.contextParameterStackTop = static_cast<u16>(stackTopBeforeContextParameter);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
bool UsecodeInterpreterState::pushCodeAddress(u16 offset, u16 segment)
|
|
{
|
|
if (callFrameCount >= 0x1f) {
|
|
return false;
|
|
}
|
|
callFrames[callFrameCount++] = UsecodeCallFrame{offset, segment};
|
|
return true;
|
|
}
|
|
|
|
bool UsecodeInterpreterState::pushWord(u16 value)
|
|
{
|
|
if (stackTop < sizeof(value)) {
|
|
return false;
|
|
}
|
|
stackTop -= sizeof(value);
|
|
valueStack[stackTop] = static_cast<u8>(value);
|
|
valueStack[stackTop + 1] = static_cast<u8>(value >> 8);
|
|
return true;
|
|
}
|
|
|
|
bool UsecodeInterpreterState::pushByte(u8 value)
|
|
{
|
|
if (stackTop == 0) {
|
|
return false;
|
|
}
|
|
valueStack[--stackTop] = value;
|
|
return true;
|
|
}
|
|
|
|
bool UsecodeInterpreterState::pushDword(u32 value)
|
|
{
|
|
if (stackTop < sizeof(value)) {
|
|
return false;
|
|
}
|
|
stackTop -= sizeof(value);
|
|
for (std::size_t index = 0; index < sizeof(value); ++index) {
|
|
valueStack[stackTop + index] = static_cast<u8>(value >> (index * 8));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::optional<u16> UsecodeInterpreterState::popWord()
|
|
{
|
|
if (stackTop > stackHighWatermark || stackHighWatermark > valueStack.size() ||
|
|
stackHighWatermark - stackTop < sizeof(u16)) {
|
|
return std::nullopt;
|
|
}
|
|
const u16 value = static_cast<u16>(valueStack[stackTop] |
|
|
(static_cast<u16>(valueStack[stackTop + 1]) << 8));
|
|
stackTop += sizeof(u16);
|
|
return value;
|
|
}
|
|
|
|
std::optional<u32> UsecodeInterpreterState::popDword()
|
|
{
|
|
if (stackTop > stackHighWatermark || stackHighWatermark > valueStack.size() ||
|
|
stackHighWatermark - stackTop < sizeof(u32)) {
|
|
return std::nullopt;
|
|
}
|
|
u32 value = 0;
|
|
for (std::size_t index = 0; index < sizeof(value); ++index) {
|
|
value |= static_cast<u32>(valueStack[stackTop + index]) << (index * 8);
|
|
}
|
|
stackTop += sizeof(value);
|
|
return value;
|
|
}
|
|
|
|
UsecodeStepResult UsecodeInterpreterState::step(const std::vector<u8>& code,
|
|
std::size_t& programCounter, std::vector<u8>& frame, std::size_t frameBaseOffset,
|
|
const UsecodeOpcodeServices& services)
|
|
{
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const u8 opcode = code[programCounter++];
|
|
if (opcode == 0x00 || opcode == 0x01 || opcode == 0x3e || opcode == 0x3f) {
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const auto rawOffset = static_cast<s8>(code[programCounter]);
|
|
const auto localIndex = static_cast<std::ptrdiff_t>(frameBaseOffset) + rawOffset;
|
|
const bool byteOperation = opcode == 0x00 || opcode == 0x3e;
|
|
const std::size_t valueSize = byteOperation ? sizeof(u8) : sizeof(u16);
|
|
if (localIndex < 0 || static_cast<std::size_t>(localIndex) > frame.size() ||
|
|
frame.size() - static_cast<std::size_t>(localIndex) < valueSize) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const std::size_t localOffset = static_cast<std::size_t>(localIndex);
|
|
if (opcode == 0x3e) {
|
|
++programCounter;
|
|
return pushWord(frame[localOffset])
|
|
? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x3f) {
|
|
++programCounter;
|
|
const u16 value = static_cast<u16>(frame[localOffset] |
|
|
(static_cast<u16>(frame[localOffset + 1]) << 8));
|
|
return pushWord(value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (stackTop > valueStack.size() - sizeof(u16)) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
++programCounter;
|
|
if (opcode == 0x00) {
|
|
frame[localOffset] = valueStack[stackTop];
|
|
} else {
|
|
frame[localOffset] = valueStack[stackTop];
|
|
frame[localOffset + 1] = valueStack[stackTop + 1];
|
|
}
|
|
return popWord() ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x0a) {
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const auto value = static_cast<s16>(static_cast<s8>(code[programCounter++]));
|
|
return pushWord(static_cast<u16>(value))
|
|
? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x0c) {
|
|
if (code.size() - programCounter < sizeof(u32)) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
u32 value = 0;
|
|
for (std::size_t index = 0; index < sizeof(value); ++index) {
|
|
value |= static_cast<u32>(code[programCounter + index]) << (index * 8);
|
|
}
|
|
programCounter += sizeof(value);
|
|
return pushDword(value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x0f) {
|
|
const std::size_t instructionStart = programCounter - 1;
|
|
if (code.size() - programCounter < 3) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const u8 argumentBytes = code[programCounter];
|
|
const u16 ordinal = static_cast<u16>(code[programCounter + 1] |
|
|
(static_cast<u16>(code[programCounter + 2]) << 8));
|
|
if (!services.callIntrinsic) {
|
|
programCounter = instructionStart;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
if (argumentBytes > valueStack.size() - stackTop) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
const auto result = services.callIntrinsic(ordinal, argumentBytes,
|
|
valueStack.data() + stackTop);
|
|
if (!result) {
|
|
programCounter = instructionStart;
|
|
return UsecodeStepResult::ServiceRejected;
|
|
}
|
|
programCounter += 3;
|
|
temporaryWord = *result;
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
if (opcode == 0x14 || opcode == 0x1c || opcode == 0x1e || opcode == 0x24 || opcode == 0x2a ||
|
|
opcode == 0x2c || opcode == 0x36) {
|
|
const auto right = popWord();
|
|
const auto left = right ? popWord() : std::nullopt;
|
|
if (!left || !right) {
|
|
if (right) pushWord(*right);
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
u16 result = 0;
|
|
if (opcode == 0x14) result = static_cast<u16>(*left + *right);
|
|
else if (opcode == 0x1c) result = static_cast<u16>(*left - *right);
|
|
else if (opcode == 0x1e) result = static_cast<u16>(
|
|
static_cast<s16>(*left) * static_cast<s16>(*right));
|
|
else if (opcode == 0x24) result = static_cast<u16>(*left == *right);
|
|
else if (opcode == 0x2a) result = static_cast<u16>(*left <= *right);
|
|
else if (opcode == 0x2c) result = static_cast<u16>(static_cast<s16>(*left) > static_cast<s16>(*right));
|
|
else result = static_cast<u16>(*left != *right);
|
|
return pushWord(result) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x51 || opcode == 0x52) {
|
|
if (code.size() - programCounter < sizeof(u16)) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const s16 relativeOffset = static_cast<s16>(code[programCounter] |
|
|
(static_cast<u16>(code[programCounter + 1]) << 8));
|
|
programCounter += sizeof(u16);
|
|
bool shouldJump = opcode == 0x52;
|
|
if (opcode == 0x51) {
|
|
const auto condition = popWord();
|
|
if (!condition) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
shouldJump = *condition == 0;
|
|
}
|
|
if (shouldJump) {
|
|
const auto target = static_cast<std::ptrdiff_t>(programCounter) + relativeOffset;
|
|
if (target < 0 || static_cast<std::size_t>(target) >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
programCounter = static_cast<std::size_t>(target);
|
|
}
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
if (opcode == 0x53) {
|
|
return UsecodeStepResult::Yielded;
|
|
}
|
|
if (opcode == 0x54) {
|
|
if (code.size() - programCounter < 2) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
programCounter += 2;
|
|
if (!popWord()) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
if (temporaryWord >= 3) {
|
|
impliedProcessId = temporaryWord;
|
|
}
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
if (opcode == 0x59) {
|
|
if (!services.currentProcessId) {
|
|
--programCounter;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
currentProcessId = services.currentProcessId();
|
|
return pushWord(currentProcessId)
|
|
? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x5d) {
|
|
return pushWord(static_cast<u8>(temporaryWord))
|
|
? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x6e) {
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const auto adjustment = static_cast<s8>(code[programCounter++]);
|
|
const auto newTop = static_cast<std::ptrdiff_t>(stackTop) - adjustment;
|
|
if (newTop < 0 || newTop > static_cast<std::ptrdiff_t>(valueStack.size())) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
stackTop = static_cast<std::size_t>(newTop);
|
|
stackHighWatermark = std::max(stackHighWatermark, stackTop);
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
if (opcode == 0x74) {
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const u8 value = code[programCounter++];
|
|
return pushByte(value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x12) {
|
|
const auto value = popWord();
|
|
if (!value) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
temporaryWord = *value;
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
if (opcode == 0x0b) {
|
|
if (code.size() - programCounter < 2) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const u16 value = static_cast<u16>(code[programCounter] |
|
|
(static_cast<u16>(code[programCounter + 1]) << 8));
|
|
programCounter += 2;
|
|
return pushWord(value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x40) {
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const std::size_t operandOffset = code[programCounter++];
|
|
const auto localIndex = static_cast<std::ptrdiff_t>(frameBaseOffset) + operandOffset;
|
|
if (localIndex < 0 || static_cast<std::size_t>(localIndex) > frame.size() ||
|
|
frame.size() - static_cast<std::size_t>(localIndex) < sizeof(u32)) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const std::size_t localOffset = static_cast<std::size_t>(localIndex);
|
|
const u32 value = static_cast<u32>(frame[localOffset]) |
|
|
(static_cast<u32>(frame[localOffset + 1]) << 8) |
|
|
(static_cast<u32>(frame[localOffset + 2]) << 16) |
|
|
(static_cast<u32>(frame[localOffset + 3]) << 24);
|
|
return pushDword(value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x4b) {
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
if (!services.currentProcessId) {
|
|
--programCounter;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
const auto displacement = static_cast<s8>(code[programCounter++]);
|
|
const auto localIndex = static_cast<std::ptrdiff_t>(frameBaseOffset) + displacement;
|
|
if (localIndex < 0 || static_cast<std::size_t>(localIndex) >= frame.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const u32 address = (static_cast<u32>(services.currentProcessId()) << 16) |
|
|
static_cast<u16>(localIndex);
|
|
return pushDword(address) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x4c) {
|
|
if (programCounter >= code.size()) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const std::size_t valueSize = code[programCounter++];
|
|
if (valueSize != sizeof(u16)) {
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
const auto address = popDword();
|
|
if (!address) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
if (!services.readFarWord) {
|
|
pushDword(*address);
|
|
return UsecodeStepResult::ServiceRejected;
|
|
}
|
|
const auto value = services.readFarWord(static_cast<u16>(*address),
|
|
static_cast<u16>(*address >> 16));
|
|
if (!value) {
|
|
pushDword(*address);
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
return pushWord(*value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x77) {
|
|
if (!services.setInfo) {
|
|
--programCounter;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
const auto first = popWord();
|
|
const auto second = first ? popWord() : std::nullopt;
|
|
if (!first || !second) {
|
|
if (first) pushWord(*first);
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
lastSetInfoOperands = std::array<u16, 2>{*first, *second};
|
|
if (!services.setInfo(*second, *first)) {
|
|
pushWord(*second);
|
|
pushWord(*first);
|
|
return UsecodeStepResult::ServiceRejected;
|
|
}
|
|
return UsecodeStepResult::SetInfo;
|
|
}
|
|
if (opcode == 0x50) {
|
|
return UsecodeStepResult::Returned;
|
|
}
|
|
if (opcode == 0x57) {
|
|
const std::size_t instructionStart = programCounter - 1;
|
|
if (code.size() - programCounter < 6) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const u8 extraArgumentBytes = code[programCounter];
|
|
const u8 itemArgumentSize = code[programCounter + 1];
|
|
const u16 classId = static_cast<u16>(code[programCounter + 2] |
|
|
(static_cast<u16>(code[programCounter + 3]) << 8));
|
|
const u16 eventSlot = static_cast<u16>(code[programCounter + 4] |
|
|
(static_cast<u16>(code[programCounter + 5]) << 8));
|
|
if ((itemArgumentSize != 0 && itemArgumentSize != sizeof(u16)) ||
|
|
stackTop > valueStack.size() - sizeof(u32) ||
|
|
extraArgumentBytes > valueStack.size() - stackTop - sizeof(u32)) {
|
|
--programCounter;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
if (!services.spawn || (itemArgumentSize != 0 && !services.readFarWord)) {
|
|
--programCounter;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
const auto itemPointer = popDword();
|
|
if (!itemPointer) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
u16 itemArgument = 0;
|
|
if (itemArgumentSize == sizeof(u16)) {
|
|
const auto value = services.readFarWord(static_cast<u16>(*itemPointer),
|
|
static_cast<u16>(*itemPointer >> 16));
|
|
if (!value) {
|
|
pushDword(*itemPointer);
|
|
programCounter = instructionStart;
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
itemArgument = *value;
|
|
}
|
|
const u8* extraArguments = valueStack.data() + stackTop;
|
|
programCounter += 6;
|
|
const auto processId = services.spawn(classId, eventSlot, extraArgumentBytes,
|
|
itemArgumentSize, itemArgument, extraArguments);
|
|
if (!processId) {
|
|
pushDword(*itemPointer);
|
|
programCounter = instructionStart;
|
|
return UsecodeStepResult::ServiceRejected;
|
|
}
|
|
temporaryWord = *processId;
|
|
return UsecodeStepResult::Spawned;
|
|
}
|
|
if (opcode == 0x5b) {
|
|
if (code.size() - programCounter < 2) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
programCounter += 2;
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
if (opcode == 0x5c) {
|
|
constexpr std::size_t symbolInfoBytes = 11;
|
|
if (code.size() - programCounter < symbolInfoBytes) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
programCounter += symbolInfoBytes;
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
if (opcode == 0x5e) {
|
|
return pushWord(temporaryWord) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
|
}
|
|
if (opcode == 0x78) {
|
|
if (!services.processCount) {
|
|
--programCounter;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
processExcluded = services.processCount() > 1;
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
--programCounter;
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
|
|
std::optional<UsecodeProcessTask> UsecodeProcessTask::create(u16 processId, u16 classId,
|
|
u16 eventSlot, u16 sourceItem, std::vector<u8> code, u8 itemArgumentSize,
|
|
std::vector<u8> extraArguments, std::vector<u8> contextParameter)
|
|
{
|
|
if (processId < 3 || code.size() <= usecodeEventBytecodeOffset ||
|
|
(itemArgumentSize != 0 && itemArgumentSize != sizeof(u16))) {
|
|
return std::nullopt;
|
|
}
|
|
const std::vector<u8> itemParameter = itemArgumentSize == 0 ? std::vector<u8>{}
|
|
: std::vector<u8>{static_cast<u8>(sourceItem), static_cast<u8>(sourceItem >> 8)};
|
|
auto interpreter = UsecodeInterpreterState::initialize(itemParameter, contextParameter,
|
|
itemArgumentSize);
|
|
if (!interpreter) {
|
|
return std::nullopt;
|
|
}
|
|
UsecodeProcessTask task;
|
|
task.processId = processId;
|
|
task.classId = classId;
|
|
task.eventSlot = eventSlot;
|
|
task.sourceItem = sourceItem;
|
|
task.code = std::move(code);
|
|
task.frame.resize(0x80);
|
|
task.itemMemory.resize(0x102);
|
|
task.itemMemory[0x100] = static_cast<u8>(sourceItem);
|
|
task.itemMemory[0x101] = static_cast<u8>(sourceItem >> 8);
|
|
std::size_t argumentOffset = usecodeFrameBaseOffset + 6;
|
|
if (itemArgumentSize == sizeof(u16)) {
|
|
task.frame[argumentOffset] = 0x00;
|
|
task.frame[argumentOffset + 1] = 0x01;
|
|
argumentOffset += sizeof(u32);
|
|
}
|
|
if (extraArguments.size() > task.frame.size() - argumentOffset) {
|
|
return std::nullopt;
|
|
}
|
|
std::copy(extraArguments.begin(), extraArguments.end(), task.frame.begin() + argumentOffset);
|
|
task.interpreter = std::move(*interpreter);
|
|
return task;
|
|
}
|
|
|
|
std::optional<u16> UsecodeProcessTask::readMappedItemWord(FarPtr16 address) const
|
|
{
|
|
if (address.segment != 0 || address.offset > itemMemory.size() ||
|
|
itemMemory.size() - address.offset < sizeof(u16)) {
|
|
return std::nullopt;
|
|
}
|
|
return static_cast<u16>(itemMemory[address.offset] |
|
|
(static_cast<u16>(itemMemory[address.offset + 1]) << 8));
|
|
}
|
|
|
|
std::optional<u16> UsecodeProcessTask::readMappedFarWord(FarPtr16 address) const
|
|
{
|
|
if (address.segment == processId) {
|
|
if (address.offset > frame.size() || frame.size() - address.offset < sizeof(u16)) {
|
|
return std::nullopt;
|
|
}
|
|
return static_cast<u16>(frame[address.offset] |
|
|
(static_cast<u16>(frame[address.offset + 1]) << 8));
|
|
}
|
|
return readMappedItemWord(address);
|
|
}
|
|
|
|
bool UsecodeProcessTask::writeMappedFarWord(FarPtr16 address, u16 value)
|
|
{
|
|
std::vector<u8>* memory = nullptr;
|
|
if (address.segment == processId) {
|
|
memory = &frame;
|
|
} else if (address.segment == 0) {
|
|
memory = &itemMemory;
|
|
}
|
|
if (memory == nullptr || address.offset > memory->size() ||
|
|
memory->size() - address.offset < sizeof(u16)) {
|
|
return false;
|
|
}
|
|
(*memory)[address.offset] = static_cast<u8>(value);
|
|
(*memory)[address.offset + 1] = static_cast<u8>(value >> 8);
|
|
return true;
|
|
}
|
|
|
|
bool writeUsecodeContextFactoryFields(u8* processBytes, std::size_t processSize,
|
|
const UsecodeContextFactoryFields& fields)
|
|
{
|
|
if (processBytes == nullptr || processSize < usecodeContextProcessSize) {
|
|
return false;
|
|
}
|
|
const auto writeWord = [processBytes](std::size_t offset, u16 value) {
|
|
processBytes[offset] = static_cast<u8>(value);
|
|
processBytes[offset + 1] = static_cast<u8>(value >> 8);
|
|
};
|
|
writeWord(usecodeContextSlotIndexOffset, fields.slotIndex);
|
|
writeWord(usecodeContextValueAddOffset, fields.valueAddOffset);
|
|
writeWord(usecodeContextOwnerSourceOffset, fields.ownerSource.offset);
|
|
writeWord(usecodeContextOwnerSourceOffset + sizeof(u16), fields.ownerSource.segment);
|
|
writeWord(usecodeContextParameterOffset, fields.parameterOffset);
|
|
processBytes[usecodeContextModeFlagsOffset] = static_cast<u8>(fields.modeFlags);
|
|
processBytes[usecodeContextActiveFlagOffset] = 0;
|
|
return true;
|
|
}
|
|
|
|
std::optional<UsecodeContextInstance> createUsecodeContext(UsecodeRuntimeMemory& memory,
|
|
const UsecodeContextCreateRequest& request)
|
|
{
|
|
const bool hasOwnerSource = request.ownerSource.offset != 0 || request.ownerSource.segment != 0;
|
|
if (!memory.contains(request.codeSource, 1) || !memory.contains(request.ownerRows, 9) ||
|
|
(hasOwnerSource && !memory.contains(request.ownerSource, 1))) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::vector<u8> itemParameter;
|
|
if (request.modeFlags != 0 && request.itemParameter) {
|
|
if (request.itemParameter->size() != request.modeFlags) {
|
|
return std::nullopt;
|
|
}
|
|
itemParameter = *request.itemParameter;
|
|
}
|
|
const std::vector<u8> contextParameter = request.contextParameter.value_or(std::vector<u8>{});
|
|
auto interpreter = UsecodeInterpreterState::initialize(itemParameter, contextParameter,
|
|
request.modeFlags, request.contextParameter.has_value());
|
|
if (!interpreter || !interpreter->pushCodeAddress(request.slotIndex, request.valueAddOffset)) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const auto processAddress = memory.allocate(usecodeContextProcessSize);
|
|
if (!processAddress) {
|
|
return std::nullopt;
|
|
}
|
|
const auto callStackAddress = memory.allocate(usecodeInterpreterCallStackSize);
|
|
if (!callStackAddress) {
|
|
memory.release(*processAddress);
|
|
return std::nullopt;
|
|
}
|
|
|
|
UsecodeContextInstance context;
|
|
context.processAddress = *processAddress;
|
|
context.callStackAddress = *callStackAddress;
|
|
writeFarPointer(context.processBytes.data(), usecodeContextInterpreterOffset, *callStackAddress);
|
|
std::copy(interpreter->valueStack.begin() + interpreter->stackTop, interpreter->valueStack.end(),
|
|
context.processBytes.begin() + usecodeContextInterpreterOffset + interpreter->stackTop);
|
|
|
|
const auto stackTopOffset = static_cast<u32>(processAddress->offset) +
|
|
usecodeContextInterpreterOffset + static_cast<u32>(interpreter->stackTop);
|
|
const auto stackBaseOffset = static_cast<u32>(processAddress->offset) + usecodeContextStackPointerOffset;
|
|
if (stackTopOffset > 0xffff || stackBaseOffset > 0xffff) {
|
|
memory.release(*callStackAddress);
|
|
memory.release(*processAddress);
|
|
return std::nullopt;
|
|
}
|
|
writeFarPointer(context.processBytes.data(), usecodeContextStackPointerOffset,
|
|
FarPtr16{static_cast<u16>(stackTopOffset), processAddress->segment});
|
|
writeFarPointer(context.processBytes.data(), usecodeContextStackBaseOffset,
|
|
FarPtr16{static_cast<u16>(stackBaseOffset), processAddress->segment});
|
|
writeWord(context.processBytes.data(), usecodeContextStackLimitOffset, interpreter->stackLimit);
|
|
writeFarPointer(context.processBytes.data(), usecodeContextCodeSourceOffset, request.codeSource);
|
|
writeFarPointer(context.processBytes.data(), usecodeContextSelfPointerOffset, *processAddress);
|
|
writeFarPointer(context.processBytes.data(), usecodeContextOwnerRowsOffset, request.ownerRows);
|
|
|
|
const UsecodeContextFactoryFields fields{request.ownerSource, request.modeFlags,
|
|
request.slotIndex, request.valueAddOffset, interpreter->contextParameterOffset.value_or(0)};
|
|
if (!writeUsecodeContextFactoryFields(context.processBytes.data(), context.processBytes.size(), fields)) {
|
|
memory.release(*callStackAddress);
|
|
memory.release(*processAddress);
|
|
return std::nullopt;
|
|
}
|
|
if (interpreter->contextParameterStackTop) {
|
|
const std::size_t savedPointerOffset = usecodeContextInterpreterOffset + interpreter->stackTop;
|
|
const u32 savedStackOffset = static_cast<u32>(processAddress->offset) +
|
|
usecodeContextInterpreterOffset + *interpreter->contextParameterStackTop;
|
|
if (savedPointerOffset > context.processBytes.size() - sizeof(FarPtr16) || savedStackOffset > 0xffff) {
|
|
memory.release(*callStackAddress);
|
|
memory.release(*processAddress);
|
|
return std::nullopt;
|
|
}
|
|
writeFarPointer(context.processBytes.data(), savedPointerOffset,
|
|
FarPtr16{static_cast<u16>(savedStackOffset), processAddress->segment});
|
|
}
|
|
|
|
std::vector<u8> callStackBytes(usecodeInterpreterCallStackSize, 0);
|
|
for (u16 index = 0; index < interpreter->callFrameCount; ++index) {
|
|
const std::size_t frameOffset = static_cast<std::size_t>(index) * 2 * sizeof(u16);
|
|
writeWord(callStackBytes.data(), frameOffset, interpreter->callFrames[index].offset);
|
|
writeWord(callStackBytes.data(), frameOffset + sizeof(u16), interpreter->callFrames[index].segment);
|
|
}
|
|
writeWord(callStackBytes.data(), 0x80, interpreter->callFrameCount);
|
|
|
|
const std::vector<u8> processBytes(context.processBytes.begin(), context.processBytes.end());
|
|
if (!memory.write(*processAddress, processBytes) || !memory.write(*callStackAddress, callStackBytes)) {
|
|
memory.release(*callStackAddress);
|
|
memory.release(*processAddress);
|
|
return std::nullopt;
|
|
}
|
|
context.interpreter = std::move(*interpreter);
|
|
return context;
|
|
}
|
|
|
|
bool destroyUsecodeContext(UsecodeRuntimeMemory& memory, const UsecodeContextInstance& context) noexcept
|
|
{
|
|
const bool callStackReleased = memory.release(context.callStackAddress);
|
|
const bool processReleased = memory.release(context.processAddress);
|
|
return callStackReleased && processReleased;
|
|
}
|
|
|
|
std::optional<UsecodeEventContext> createUsecodeEventContext(UsecodeRuntimeMemory& memory,
|
|
const std::vector<u8>& ownerRows, u16 classId, u16 eventSlot, u16 sourceItem,
|
|
const std::vector<u8>& code, u8 itemArgumentSize)
|
|
{
|
|
constexpr std::size_t ownerRowSize = 13;
|
|
const std::size_t ownerRowOffset = static_cast<std::size_t>(classId) * ownerRowSize;
|
|
if (code.size() <= usecodeEventBytecodeOffset ||
|
|
(itemArgumentSize != 0 && itemArgumentSize != sizeof(u16)) ||
|
|
ownerRowOffset > ownerRows.size() || ownerRows.size() - ownerRowOffset < ownerRowSize) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
UsecodeEventContext eventContext;
|
|
const auto allocateRegion = [&memory, &eventContext](const std::vector<u8>& bytes)
|
|
-> std::optional<FarPtr16> {
|
|
auto region = memory.allocate(bytes.size());
|
|
if (!region) {
|
|
return std::nullopt;
|
|
}
|
|
if (!memory.write(*region, bytes)) {
|
|
memory.release(*region);
|
|
return std::nullopt;
|
|
}
|
|
eventContext.ownedRegions.push_back(*region);
|
|
return region;
|
|
};
|
|
|
|
const auto codeRegion = allocateRegion(code);
|
|
const std::vector<u8> ownerRow(ownerRows.begin() + ownerRowOffset,
|
|
ownerRows.begin() + ownerRowOffset + ownerRowSize);
|
|
const auto ownerRowRegion = codeRegion ? allocateRegion(ownerRow) : std::nullopt;
|
|
const auto outputRegion = ownerRowRegion ? memory.allocate(sizeof(u16)) : std::nullopt;
|
|
if (!codeRegion || !ownerRowRegion || !outputRegion) {
|
|
if (outputRegion) {
|
|
memory.release(*outputRegion);
|
|
}
|
|
for (const auto region : eventContext.ownedRegions) {
|
|
memory.release(region);
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
eventContext.processIdOutput = *outputRegion;
|
|
eventContext.ownedRegions.push_back(*outputRegion);
|
|
|
|
std::vector<u8> contextParameter{
|
|
static_cast<u8>(outputRegion->offset),
|
|
static_cast<u8>(outputRegion->offset >> 8),
|
|
static_cast<u8>(outputRegion->segment),
|
|
static_cast<u8>(outputRegion->segment >> 8)
|
|
};
|
|
UsecodeContextCreateRequest request;
|
|
request.codeSource = *codeRegion;
|
|
request.ownerRows = FarPtr16{4, ownerRowRegion->segment};
|
|
if (itemArgumentSize == sizeof(u16)) {
|
|
request.itemParameter = std::vector<u8>{static_cast<u8>(sourceItem),
|
|
static_cast<u8>(sourceItem >> 8)};
|
|
}
|
|
request.contextParameter = std::move(contextParameter);
|
|
request.modeFlags = itemArgumentSize;
|
|
request.slotIndex = classId;
|
|
request.valueAddOffset = eventSlot;
|
|
auto context = createUsecodeContext(memory, request);
|
|
if (!context) {
|
|
for (const auto region : eventContext.ownedRegions) {
|
|
memory.release(region);
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
eventContext.context = std::move(*context);
|
|
return eventContext;
|
|
}
|
|
|
|
bool destroyUsecodeEventContext(UsecodeRuntimeMemory& memory,
|
|
const UsecodeEventContext& eventContext) noexcept
|
|
{
|
|
bool released = destroyUsecodeContext(memory, eventContext.context);
|
|
for (const auto region : eventContext.ownedRegions) {
|
|
released = memory.release(region) && released;
|
|
}
|
|
return released;
|
|
}
|
|
|
|
UsecodeStepResult UsecodeProcessTask::step(const UsecodeOpcodeServices& services)
|
|
{
|
|
if (halted) {
|
|
return UsecodeStepResult::Returned;
|
|
}
|
|
if (programCounter < code.size() && code[programCounter] == 0x11) {
|
|
if (code.size() - programCounter < 5) {
|
|
return UsecodeStepResult::InvalidInstruction;
|
|
}
|
|
const u16 calleeClassId = static_cast<u16>(code[programCounter + 1] |
|
|
(static_cast<u16>(code[programCounter + 2]) << 8));
|
|
const u16 calleeEventSlot = static_cast<u16>(code[programCounter + 3] |
|
|
(static_cast<u16>(code[programCounter + 4]) << 8));
|
|
if (!services.callEvent) {
|
|
return UsecodeStepResult::UnsupportedOpcode;
|
|
}
|
|
if (routineFrames.size() >= 31) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
auto calleeBody = services.callEvent(calleeClassId, calleeEventSlot);
|
|
if (!calleeBody || calleeBody->bytes.size() <= usecodeEventBytecodeOffset) {
|
|
return UsecodeStepResult::ServiceRejected;
|
|
}
|
|
constexpr std::size_t callFrameBytes = 3 * sizeof(u16);
|
|
if (frameBaseOffset < callFrameBytes) {
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
routineFrames.push_back({classId, eventSlot, std::move(code), frame, frameBaseOffset,
|
|
programCounter + 5});
|
|
if (!interpreter.pushCodeAddress(calleeClassId, calleeEventSlot)) {
|
|
code = std::move(routineFrames.back().code);
|
|
frame = std::move(routineFrames.back().frame);
|
|
routineFrames.pop_back();
|
|
return UsecodeStepResult::StackFault;
|
|
}
|
|
const std::size_t calleeArgumentOffset = frameBaseOffset;
|
|
classId = calleeClassId;
|
|
eventSlot = calleeEventSlot;
|
|
frameBaseOffset -= callFrameBytes;
|
|
std::fill(frame.begin(), frame.end(), u8{0});
|
|
const std::size_t copiedArgumentBytes = std::min(frame.size() - calleeArgumentOffset,
|
|
interpreter.valueStack.size() - interpreter.stackTop);
|
|
std::copy_n(interpreter.valueStack.begin() + interpreter.stackTop,
|
|
copiedArgumentBytes, frame.begin() + calleeArgumentOffset);
|
|
code = std::move(calleeBody->bytes);
|
|
programCounter = usecodeEventBytecodeOffset;
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
const auto result = interpreter.step(code, programCounter, frame, frameBaseOffset, services);
|
|
if (result == UsecodeStepResult::Returned && !routineFrames.empty()) {
|
|
UsecodeRoutineFrame caller = std::move(routineFrames.back());
|
|
routineFrames.pop_back();
|
|
--interpreter.callFrameCount;
|
|
classId = caller.classId;
|
|
eventSlot = caller.eventSlot;
|
|
frameBaseOffset = caller.frameBaseOffset;
|
|
frame = std::move(caller.frame);
|
|
code = std::move(caller.code);
|
|
programCounter = caller.returnProgramCounter;
|
|
return UsecodeStepResult::Advanced;
|
|
}
|
|
halted = result == UsecodeStepResult::Returned;
|
|
return result;
|
|
}
|
|
|
|
bool UsecodeTaskRegistry::add(UsecodeProcessTask task)
|
|
{
|
|
if (task.processId < 3) {
|
|
return false;
|
|
}
|
|
return tasks_.emplace(task.processId, std::move(task)).second;
|
|
}
|
|
|
|
bool UsecodeTaskRegistry::addWithContext(UsecodeProcessTask task, UsecodeEventContext context)
|
|
{
|
|
const u16 processId = task.processId;
|
|
if (processId < 3 || tasks_.find(processId) != tasks_.end() || contexts_.find(processId) != contexts_.end()) {
|
|
destroyUsecodeEventContext(runtimeMemory_, context);
|
|
return false;
|
|
}
|
|
task.interpreter = context.context.interpreter;
|
|
tasks_.emplace(processId, std::move(task));
|
|
contexts_.emplace(processId, std::move(context));
|
|
return true;
|
|
}
|
|
|
|
UsecodeProcessTask* UsecodeTaskRegistry::find(u16 processId)
|
|
{
|
|
const auto found = tasks_.find(processId);
|
|
return found == tasks_.end() ? nullptr : &found->second;
|
|
}
|
|
|
|
const UsecodeProcessTask* UsecodeTaskRegistry::find(u16 processId) const
|
|
{
|
|
const auto found = tasks_.find(processId);
|
|
return found == tasks_.end() ? nullptr : &found->second;
|
|
}
|
|
|
|
const UsecodeEventContext* UsecodeTaskRegistry::context(u16 processId) const noexcept
|
|
{
|
|
const auto found = contexts_.find(processId);
|
|
return found == contexts_.end() ? nullptr : &found->second;
|
|
}
|
|
|
|
bool UsecodeTaskRegistry::remove(u16 processId)
|
|
{
|
|
const auto task = tasks_.find(processId);
|
|
if (task == tasks_.end()) {
|
|
return false;
|
|
}
|
|
const auto context = contexts_.find(processId);
|
|
if (context != contexts_.end()) {
|
|
destroyUsecodeEventContext(runtimeMemory_, context->second);
|
|
contexts_.erase(context);
|
|
}
|
|
tasks_.erase(task);
|
|
return true;
|
|
}
|
|
|
|
} // namespace remorse
|