Add comprehensive tests for ProcessKernel and Usecode VM functionality
- Implemented tests for ProcessKernel in entity_dispatch_entry_tests.cpp, covering process creation, termination, and scanning of runnable processes. - Enhanced fixed_map_tests.cpp with tests for item allocation and release, including recycling of items and process ownership. - Expanded flex_archive_tests.cpp to validate the interaction between usecode events and item processes, ensuring correct behavior of spawned tasks and rendering. - Introduced usecode_vm_tests.cpp to thoroughly test the UsecodeInterpreterState and UsecodeProcessTask functionalities, including stack operations and opcode services.
This commit is contained in:
parent
e757bb33d0
commit
08a907f322
14 changed files with 1784 additions and 34 deletions
|
|
@ -56,6 +56,11 @@ struct AnimPrimitiveProcessEntry {
|
|||
u8 extra[0x29];
|
||||
} CR_PACKED;
|
||||
|
||||
struct EntityVmContextProcessEntry {
|
||||
EntityDispatchEntryBase base;
|
||||
u8 extra[0xf2];
|
||||
} CR_PACKED;
|
||||
|
||||
struct KeyEvent {
|
||||
u8 character;
|
||||
u8 scanCode;
|
||||
|
|
@ -80,6 +85,7 @@ static_assert(sizeof(KeyboardProcessEntry) == 0x36, "KeyboardProcessEntry size m
|
|||
static_assert(sizeof(KeyDaemonProcessEntry) == 0x40, "KeyDaemonProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(ControllerProcessEntry) == 0x40, "ControllerProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(AnimPrimitiveProcessEntry) == 0x5b, "AnimPrimitiveProcessEntry size must match allocation evidence");
|
||||
static_assert(sizeof(EntityVmContextProcessEntry) == 0x124, "EntityVmContext process size must match allocation evidence");
|
||||
static_assert(sizeof(KeyEvent) == 0x0c, "KeyEvent size must match keyboard event copy");
|
||||
|
||||
struct NpcBehaviorServices {
|
||||
|
|
@ -115,6 +121,7 @@ public:
|
|||
ControllerProcessEntry* createControllerProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
AnimPrimitiveProcessEntry* createAnimPrimitiveProcess(u8 activeGroup, u16 itemNumber,
|
||||
u16 animationNumber, u8 direction, const AnimationClip& clip, u16 (&groupCounts)[32]);
|
||||
EntityVmContextProcessEntry* createUsecodeProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
bool startAnimPrimitiveProcess(u16 processId, LiveFixedMapItems& items,
|
||||
const AnimationClip& clip, u8 frameSkip = 0);
|
||||
std::optional<bool> advanceGroundedAnimPrimitive(u16 processId, LiveFixedMapItems& items,
|
||||
|
|
@ -141,6 +148,7 @@ public:
|
|||
void clearTerminatedProcesses();
|
||||
void setProcessSourceType(EntityDispatchEntryBase& entry, u16 itemNumber);
|
||||
void setProcessEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType);
|
||||
bool setProcessInfo(u16 processId, u16 eventType, u16 sourceItem);
|
||||
void scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context, u32 timerTick = 0);
|
||||
u32 runTimedSchedulerPass(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context,
|
||||
void (*afterScan)(void*) = nullptr, void* afterContext = nullptr);
|
||||
|
|
@ -166,6 +174,7 @@ private:
|
|||
std::vector<std::unique_ptr<StandProcessEntry>> ownedStandProcesses_;
|
||||
std::vector<std::unique_ptr<GravityProcessEntry>> ownedGravityProcesses_;
|
||||
std::vector<std::unique_ptr<AnimPrimitiveProcessEntry>> ownedAnimPrimitiveProcesses_;
|
||||
std::vector<std::unique_ptr<EntityVmContextProcessEntry>> ownedUsecodeProcesses_;
|
||||
std::unique_ptr<SystemTimerProcessEntry> systemTimerProcess_;
|
||||
std::unique_ptr<KeyboardProcessEntry> keyboardProcess_;
|
||||
std::unique_ptr<KeyDaemonProcessEntry> keyDaemonProcess_;
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ struct StagedContainedFixedMapItem {
|
|||
u16 itemNumber = 0;
|
||||
u16 parentItemNumber = 0;
|
||||
std::size_t recordIndex = 0;
|
||||
FixedMapEntry data;
|
||||
};
|
||||
|
||||
struct ItemFindArea {
|
||||
|
|
@ -119,13 +120,19 @@ public:
|
|||
const NpcAnimationState* findNpcAnimation(u16 itemNumber) const;
|
||||
NpcAnimationState* editNpcAnimation(u16 itemNumber);
|
||||
u16 containedParent(u16 itemNumber) const;
|
||||
std::vector<u16> containedChildren(u16 parentItemNumber) const;
|
||||
std::optional<StagedFixedMapItem> nearestItem(s32 worldX, s32 worldY, s32 maxDistance) const;
|
||||
std::optional<std::vector<u16>> sweepCandidateIds(const WorldRect& area, u16 excludedItemNumber) const;
|
||||
std::optional<u16> allocateItem(StagedFixedMapItem item);
|
||||
bool insertItem(StagedFixedMapItem item);
|
||||
bool moveItemIntoContainer(u16 itemNumber, u16 containerItemNumber,
|
||||
const std::vector<ShapeTypeInfo>& shapes);
|
||||
bool moveContainedItemToWorld(u16 itemNumber, u16 x, u16 y, u8 z);
|
||||
bool releaseItem(u16 itemNumber, const ProcessKernel& kernel);
|
||||
std::optional<std::vector<u16>> hatchGlobEgg(u16 eggItemNumber, const GlobObject& glob,
|
||||
const std::vector<ShapeTypeInfo>& shapes);
|
||||
bool isGlobHatched(u16 eggItemNumber) const;
|
||||
bool setItemFrame(u16 itemNumber, u8 frame);
|
||||
bool moveItem(u16 itemNumber, u16 x, u16 y, u8 z);
|
||||
std::optional<std::vector<u16>> grabItem(u16 itemNumber, const std::vector<ShapeTypeInfo>& shapes,
|
||||
ProcessKernel& kernel, u16 (&groupCounts)[32], u32 timerTick) const;
|
||||
|
|
@ -134,12 +141,20 @@ public:
|
|||
u16 (&groupCounts)[32], u32 timerTick);
|
||||
|
||||
private:
|
||||
bool unlinkContainedItem(u16 itemNumber);
|
||||
void refreshContainerQuality(u16 parentItemNumber);
|
||||
bool insertContainedItemAtHead(u16 parentItemNumber, u16 itemNumber);
|
||||
std::vector<StagedFixedMapItem> items_;
|
||||
std::optional<StagedFixedMapItem> avatar_;
|
||||
std::vector<NpcAnimationState> npcAnimations_;
|
||||
std::vector<bool> allocatedIds_;
|
||||
std::vector<bool> hatchedGlobIds_;
|
||||
std::vector<u16> parentById_;
|
||||
std::vector<std::optional<FixedMapEntry>> containedItemData_;
|
||||
std::vector<u16> containedNextById_;
|
||||
std::vector<u16> containedHeadById_;
|
||||
std::vector<u16> containedTailById_;
|
||||
std::vector<u16> freeItemIds_;
|
||||
u16 nextFreeItemNumber_ = 0x100;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
|
|
@ -9,22 +11,115 @@
|
|||
|
||||
namespace remorse {
|
||||
|
||||
constexpr std::size_t usecodeEventBytecodeOffset = 14;
|
||||
constexpr std::size_t usecodeFrameBaseOffset = 0x40;
|
||||
constexpr std::size_t usecodeInterpreterStackLimit = 199;
|
||||
constexpr std::size_t usecodeContextProcessSize = 0x124;
|
||||
constexpr std::size_t usecodeContextSlotIndexOffset = 0x32;
|
||||
constexpr std::size_t usecodeContextValueAddOffset = 0x34;
|
||||
constexpr std::size_t usecodeContextOwnerSourceOffset = 0x11b;
|
||||
constexpr std::size_t usecodeContextParameterOffset = 0x11f;
|
||||
constexpr std::size_t usecodeContextModeFlagsOffset = 0x121;
|
||||
constexpr std::size_t usecodeContextActiveFlagOffset = 0x122;
|
||||
|
||||
struct UsecodeCallFrame {
|
||||
u16 offset = 0;
|
||||
u16 segment = 0;
|
||||
};
|
||||
|
||||
enum class UsecodeStepResult {
|
||||
Advanced,
|
||||
Returned,
|
||||
SetInfo,
|
||||
Spawned,
|
||||
Yielded,
|
||||
UnsupportedOpcode,
|
||||
InvalidInstruction,
|
||||
StackFault,
|
||||
ServiceRejected
|
||||
};
|
||||
|
||||
struct UsecodeOpcodeServices {
|
||||
std::function<std::optional<u16>(u16 offset, u16 segment)> readFarWord;
|
||||
std::function<bool(u16 eventType, u16 sourceItem)> setInfo;
|
||||
std::function<u16()> processCount;
|
||||
std::function<std::optional<u16>(u16 classId, u16 eventSlot, u8 extraArgumentBytes,
|
||||
u8 itemArgumentSize, u16 itemArgument, const u8* extraArguments)> spawn;
|
||||
std::function<std::optional<u16>(u16 ordinal, u8 argumentBytes, const u8* arguments)> callIntrinsic;
|
||||
std::function<u16()> currentProcessId;
|
||||
};
|
||||
|
||||
class UsecodeInterpreterState {
|
||||
public:
|
||||
static std::optional<UsecodeInterpreterState> initialize(
|
||||
const std::vector<u8>& itemParameter);
|
||||
const std::vector<u8>& itemParameter, const std::vector<u8>& contextParameter = {},
|
||||
u16 modeFlags = 0);
|
||||
bool pushCodeAddress(u16 offset, u16 segment);
|
||||
bool pushWord(u16 value);
|
||||
bool pushDword(u32 value);
|
||||
std::optional<u16> popWord();
|
||||
std::optional<u32> popDword();
|
||||
UsecodeStepResult step(const std::vector<u8>& code, std::size_t& programCounter,
|
||||
std::vector<u8>& frame, const UsecodeOpcodeServices& services);
|
||||
std::optional<std::array<u16, 2>> lastSetInfoOperands;
|
||||
|
||||
std::array<u8, 0xcc> valueStack{};
|
||||
std::size_t stackTop = 0xcc;
|
||||
std::size_t stackHighWatermark = 0xcc;
|
||||
std::array<UsecodeCallFrame, 32> callFrames{};
|
||||
u16 callFrameCount = 0;
|
||||
u16 stackLimit = 199;
|
||||
u16 stackLimit = usecodeInterpreterStackLimit;
|
||||
u16 temporaryWord = 0;
|
||||
std::optional<u16> contextParameterOffset;
|
||||
bool processExcluded = false;
|
||||
u16 currentProcessId = 0;
|
||||
u16 impliedProcessId = 0;
|
||||
};
|
||||
|
||||
struct UsecodeProcessTask {
|
||||
static std::optional<UsecodeProcessTask> create(u16 processId, u16 classId,
|
||||
u16 eventSlot, u16 sourceItem, std::vector<u8> code,
|
||||
u8 itemArgumentSize = 2, std::vector<u8> extraArguments = {},
|
||||
std::vector<u8> contextParameter = {});
|
||||
UsecodeStepResult step(const UsecodeOpcodeServices& services);
|
||||
|
||||
u16 processId = 0;
|
||||
u16 classId = 0;
|
||||
u16 eventSlot = 0;
|
||||
u16 sourceItem = 0;
|
||||
std::vector<u8> code;
|
||||
std::vector<u8> frame;
|
||||
std::vector<u8> itemMemory;
|
||||
UsecodeInterpreterState interpreter;
|
||||
std::size_t programCounter = usecodeEventBytecodeOffset;
|
||||
bool halted = false;
|
||||
bool timerDelayProcess = false;
|
||||
u32 wakeTick = 0;
|
||||
|
||||
std::optional<u16> readMappedItemWord(FarPtr16 address) const;
|
||||
};
|
||||
|
||||
struct UsecodeContextFactoryFields {
|
||||
FarPtr16 ownerSource{};
|
||||
u16 modeFlags = 0;
|
||||
u16 slotIndex = 0;
|
||||
u16 valueAddOffset = 0;
|
||||
u16 parameterOffset = 0;
|
||||
};
|
||||
|
||||
bool writeUsecodeContextFactoryFields(u8* processBytes, std::size_t processSize,
|
||||
const UsecodeContextFactoryFields& fields);
|
||||
|
||||
class UsecodeTaskRegistry {
|
||||
public:
|
||||
bool add(UsecodeProcessTask task);
|
||||
UsecodeProcessTask* find(u16 processId);
|
||||
const UsecodeProcessTask* find(u16 processId) const;
|
||||
bool remove(u16 processId);
|
||||
std::size_t size() const noexcept { return tasks_.size(); }
|
||||
|
||||
private:
|
||||
std::map<u16, UsecodeProcessTask> tasks_;
|
||||
};
|
||||
|
||||
} // namespace remorse
|
||||
Loading…
Add table
Add a link
Reference in a new issue