Review nevs

This commit is contained in:
Alexander Batalov 2022-07-06 14:59:54 +03:00
parent f66b8df39b
commit a19dcfad93
3 changed files with 97 additions and 85 deletions

View File

@ -478,8 +478,8 @@ static void opLoadPaletteTable(Program* program)
static void opAddNamedEvent(Program* program) static void opAddNamedEvent(Program* program)
{ {
int proc = programStackPopInteger(program); int proc = programStackPopInteger(program);
const char* v1 = programStackPopString(program); const char* name = programStackPopString(program);
_nevs_addevent(v1, program, proc, 0); _nevs_addevent(name, program, proc, NEVS_TYPE_EVENT);
} }
// addnamedhandler // addnamedhandler
@ -487,8 +487,8 @@ static void opAddNamedEvent(Program* program)
static void opAddNamedHandler(Program* program) static void opAddNamedHandler(Program* program)
{ {
int proc = programStackPopInteger(program); int proc = programStackPopInteger(program);
const char* v1 = programStackPopString(program); const char* name = programStackPopString(program);
_nevs_addevent(v1, program, proc, 1); _nevs_addevent(name, program, proc, NEVS_TYPE_HANDLER);
} }
// clearnamed // clearnamed

View File

@ -1,31 +1,51 @@
#include "nevs.h" #include "nevs.h"
#include "debug.h" #include "debug.h"
#include "interpreter_lib.h"
#include "memory_manager.h" #include "memory_manager.h"
#include "platform_compat.h" #include "platform_compat.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define NEVS_COUNT 40
typedef struct Nevs {
bool used;
char name[32];
Program* program;
int proc;
int type;
int hits;
bool busy;
void (*field_38)();
} Nevs;
static Nevs* _nevs_alloc();
static void _nevs_reset(Nevs* nevs);
static void _nevs_removeprogramreferences(Program* program);
static Nevs* _nevs_find(const char* name);
// 0x6391C8 // 0x6391C8
Nevs* _nevs; static Nevs* gNevs;
// 0x6391CC // 0x6391CC
int _anyhits; static int gNevsHits;
// nevs_alloc // nevs_alloc
// 0x488340 // 0x488340
Nevs* _nevs_alloc() static Nevs* _nevs_alloc()
{ {
if (_nevs == NULL) { if (gNevs == NULL) {
debugPrint("nevs_alloc(): nevs_initonce() not called!"); debugPrint("nevs_alloc(): nevs_initonce() not called!");
exit(99); exit(99);
} }
for (int i = 0; i < NEVS_COUNT; i++) { for (int index = 0; index < NEVS_COUNT; index++) {
Nevs* nevs = &(_nevs[i]); Nevs* nevs = &(gNevs[index]);
if (nevs->field_0 == 0) { if (!nevs->used) {
memset(nevs, 0, sizeof(Nevs)); // NOTE: Uninline.
_nevs_reset(nevs);
return nevs; return nevs;
} }
} }
@ -33,23 +53,33 @@ Nevs* _nevs_alloc()
return NULL; return NULL;
} }
// NOTE: Inlined.
//
// 0x488394
static void _nevs_reset(Nevs* nevs)
{
nevs->used = false;
memset(nevs, 0, sizeof(*nevs));
}
// 0x4883AC // 0x4883AC
void _nevs_close() void _nevs_close()
{ {
if (_nevs != NULL) { if (gNevs != NULL) {
internal_free_safe(_nevs, __FILE__, __LINE__); // "..\\int\\NEVS.C", 97 internal_free_safe(gNevs, __FILE__, __LINE__); // "..\\int\\NEVS.C", 97
_nevs = NULL; gNevs = NULL;
} }
} }
// 0x4883D4 // 0x4883D4
void _nevs_removeprogramreferences(Program* program) static void _nevs_removeprogramreferences(Program* program)
{ {
if (_nevs != NULL) { if (gNevs != NULL) {
for (int i = 0; i < NEVS_COUNT; i++) { for (int i = 0; i < NEVS_COUNT; i++) {
Nevs* nevs = &(_nevs[i]); Nevs* nevs = &(gNevs[i]);
if (nevs->field_0 != 0 && nevs->program == program) { if (nevs->used && nevs->program == program) {
memset(nevs, 0, sizeof(*nevs)); // NOTE: Uninline.
_nevs_reset(nevs);
} }
} }
} }
@ -59,12 +89,11 @@ void _nevs_removeprogramreferences(Program* program)
// 0x488418 // 0x488418
void _nevs_initonce() void _nevs_initonce()
{ {
// TODO: Incomplete. _interpretRegisterProgramDeleteCallback(_nevs_removeprogramreferences);
// _interpretRegisterProgramDeleteCallback(_nevs_removeprogramreferences);
if (_nevs == NULL) { if (gNevs == NULL) {
_nevs = (Nevs*)internal_calloc_safe(sizeof(Nevs), NEVS_COUNT, __FILE__, __LINE__); // "..\\int\\NEVS.C", 131 gNevs = (Nevs*)internal_calloc_safe(sizeof(Nevs), NEVS_COUNT, __FILE__, __LINE__); // "..\\int\\NEVS.C", 131
if (_nevs == NULL) { if (gNevs == NULL) {
debugPrint("nevs_initonce(): out of memory"); debugPrint("nevs_initonce(): out of memory");
exit(99); exit(99);
} }
@ -73,16 +102,16 @@ void _nevs_initonce()
// nevs_find // nevs_find
// 0x48846C // 0x48846C
Nevs* _nevs_find(const char* a1) static Nevs* _nevs_find(const char* name)
{ {
if (!_nevs) { if (gNevs == NULL) {
debugPrint("nevs_find(): nevs_initonce() not called!"); debugPrint("nevs_find(): nevs_initonce() not called!");
exit(99); exit(99);
} }
for (int index = 0; index < NEVS_COUNT; index++) { for (int index = 0; index < NEVS_COUNT; index++) {
Nevs* nevs = &(_nevs[index]); Nevs* nevs = &(gNevs[index]);
if (nevs->field_0 != 0 && compat_stricmp(nevs->field_4, a1) == 0) { if (nevs->used && compat_stricmp(nevs->name, name) == 0) {
return nevs; return nevs;
} }
} }
@ -91,11 +120,9 @@ Nevs* _nevs_find(const char* a1)
} }
// 0x4884C8 // 0x4884C8
int _nevs_addevent(const char* a1, Program* program, int proc, int a4) int _nevs_addevent(const char* name, Program* program, int proc, int type)
{ {
Nevs* nevs; Nevs* nevs = _nevs_find(name);
nevs = _nevs_find(a1);
if (nevs == NULL) { if (nevs == NULL) {
nevs = _nevs_alloc(); nevs = _nevs_alloc();
} }
@ -104,11 +131,11 @@ int _nevs_addevent(const char* a1, Program* program, int proc, int a4)
return 1; return 1;
} }
nevs->field_0 = 1; nevs->used = true;
strcpy(nevs->field_4, a1); strcpy(nevs->name, name);
nevs->program = program; nevs->program = program;
nevs->proc = proc; nevs->proc = proc;
nevs->field_2C = a4; nevs->type = type;
nevs->field_38 = NULL; nevs->field_38 = NULL;
return 0; return 0;
@ -121,8 +148,9 @@ int _nevs_clearevent(const char* a1)
debugPrint("nevs_clearevent( '%s');\n", a1); debugPrint("nevs_clearevent( '%s');\n", a1);
Nevs* nevs = _nevs_find(a1); Nevs* nevs = _nevs_find(a1);
if (nevs) { if (nevs != NULL) {
memset(nevs, 0, sizeof(Nevs)); // NOTE: Uninline.
_nevs_reset(nevs);
return 0; return 0;
} }
@ -131,20 +159,22 @@ int _nevs_clearevent(const char* a1)
// nevs_signal // nevs_signal
// 0x48862C // 0x48862C
int _nevs_signal(const char* a1) int _nevs_signal(const char* name)
{ {
debugPrint("nevs_signal( '%s');\n", a1); debugPrint("nevs_signal( '%s');\n", name);
Nevs* nevs = _nevs_find(a1); Nevs* nevs = _nevs_find(name);
if (nevs == NULL) { if (nevs == NULL) {
return 1; return 1;
} }
debugPrint("nep: %p, used = %u, prog = %p, proc = %d", nevs, nevs->field_0, nevs->program, nevs->proc); debugPrint("nep: %p, used = %u, prog = %p, proc = %d", nevs, nevs->used, nevs->program, nevs->proc);
if (nevs->field_0 && (nevs->program && nevs->proc || nevs->field_38) && !nevs->field_34) { if (nevs->used
nevs->field_30++; && ((nevs->program != NULL && nevs->proc != 0) || nevs->field_38 != NULL)
_anyhits++; && !nevs->busy) {
nevs->hits++;
gNevsHits++;
return 0; return 0;
} }
@ -155,28 +185,24 @@ int _nevs_signal(const char* a1)
// 0x4886AC // 0x4886AC
void _nevs_update() void _nevs_update()
{ {
int v1; if (gNevsHits == 0) {
int v2;
if (_anyhits == 0) {
return; return;
} }
debugPrint("nevs_update(): we have anyhits = %u\n", _anyhits); debugPrint("nevs_update(): we have anyhits = %u\n", gNevsHits);
_anyhits = 0; gNevsHits = 0;
for (int index = 0; index < NEVS_COUNT; index++) { for (int index = 0; index < NEVS_COUNT; index++) {
Nevs* nevs = &(_nevs[index]); Nevs* nevs = &(gNevs[index]);
if (nevs->field_0 && (nevs->program && nevs->proc || nevs->field_38) && !nevs->field_34) { if (nevs->used
v1 = nevs->field_30; && ((nevs->program != NULL && nevs->proc != 0) || nevs->field_38 != NULL)
if (nevs->field_34 < v1) { && !nevs->busy) {
v2 = v1 - 1; if (nevs->hits > 0) {
nevs->field_34 = 1; nevs->busy = true;
nevs->field_30--; nevs->hits -= 1;
gNevsHits += nevs->hits;
_anyhits += v2;
if (nevs->field_38 == NULL) { if (nevs->field_38 == NULL) {
_executeProc(nevs->program, nevs->proc); _executeProc(nevs->program, nevs->proc);
@ -184,10 +210,11 @@ void _nevs_update()
nevs->field_38(); nevs->field_38();
} }
nevs->field_34 = 0; nevs->busy = false;
if (nevs->field_2C == 0) { if (nevs->type == NEVS_TYPE_EVENT) {
memset(nevs, 0, sizeof(Nevs)); // NOTE: Uninline.
_nevs_reset(nevs);
} }
} }
} }

View File

@ -3,31 +3,16 @@
#include "interpreter.h" #include "interpreter.h"
#define NEVS_COUNT (40) typedef enum NevsType {
NEVS_TYPE_EVENT = 0,
NEVS_TYPE_HANDLER = 1,
} NevsType;
typedef struct Nevs {
int field_0;
char field_4[32];
Program* program;
int proc;
int field_2C;
int field_30;
int field_34;
void (*field_38)();
} Nevs;
extern Nevs* _nevs;
extern int _anyhits;
Nevs* _nevs_alloc();
void _nevs_close(); void _nevs_close();
void _nevs_removeprogramreferences(Program* program);
void _nevs_initonce(); void _nevs_initonce();
Nevs* _nevs_find(const char* a1); int _nevs_addevent(const char* name, Program* program, int proc, int type);
int _nevs_addevent(const char* a1, Program* program, int proc, int a4); int _nevs_clearevent(const char* name);
int _nevs_clearevent(const char* a1); int _nevs_signal(const char* name);
int _nevs_signal(const char* a1);
void _nevs_update(); void _nevs_update();
#endif /* NEVS_H */ #endif /* NEVS_H */