Replace the partial mechanics transcriptions with a separate, readable C11 reconstruction of the complete Win16 image while preserving the original raw Ghidra export as immutable evidence. Cover all ordinary and overlapping entry points, Borland runtime behavior, Win16 imports, segmented data, callbacks, resources, indirect control flow, physics, rendering, persistence, and startup/shutdown lifecycles. Add deterministic extraction and audit tooling plus address-linked ledgers for functions, imports, DGROUP ranges and objects, relocations, resources, and callbacks. The final gate records zero raw, partial, restored, unknown, blocked, or unclassified required units. Keep the semantic-fidelity boundary explicit: the portable C is not claimed to reproduce a byte-identical Borland NE build. Add strict focused harnesses for every reconstructed C unit, exact resource round-trip checks, and a 16-bit Borland Real48 reference probe. No Rust source or Cargo metadata is changed in this phase. Test Plan: - `bash original/tools/test_reconstructed_c.sh` -- passed - `bash original/tools/probe_real48_reference.sh` -- passed bit-for-bit - `python3 original/tools/audit_reconstruction.py --require-complete` -- passed - `git diff --cached --check` -- passed - `git diff HEAD -- '*.rs' Cargo.toml Cargo.lock` -- empty
46 lines
1.4 KiB
C
46 lines
1.4 KiB
C
/* Borland 16-bit stack-overflow guard and TDKPIN startup entry. */
|
|
|
|
#include "tdkpin_stack.h"
|
|
|
|
enum {
|
|
BORLAND_STACK_SAFETY_BYTES = 0x0400,
|
|
BORLAND_STACK_LOWER_LIMIT_OFFSET = 0x000a,
|
|
BORLAND_STACK_LOW_WATER_OFFSET = 0x000e,
|
|
};
|
|
|
|
/*
|
|
* 1020:03cb — check AX bytes plus the Borland 0x400-byte safety reserve.
|
|
* All comparisons are unsigned 16-bit operations against the live SS layout.
|
|
* Runtime Error 202 covers addition overflow, collision with SP, and crossing
|
|
* the absolute lower limit. A successful deeper use updates SS:000e.
|
|
*/
|
|
uint16_t borland_check_stack(uint16_t requested_bytes)
|
|
{
|
|
uint16_t required =
|
|
(uint16_t)(requested_bytes + BORLAND_STACK_SAFETY_BYTES);
|
|
if (required < requested_bytes) {
|
|
borland_runtime_error(202);
|
|
}
|
|
|
|
uint16_t stack_pointer = win16_current_stack_pointer();
|
|
if (required >= stack_pointer) {
|
|
borland_runtime_error(202);
|
|
}
|
|
|
|
uint16_t remaining = (uint16_t)(stack_pointer - required);
|
|
if (remaining <
|
|
win16_read_stack_u16(BORLAND_STACK_LOWER_LIMIT_OFFSET)) {
|
|
borland_runtime_error(202);
|
|
}
|
|
if (remaining < win16_read_stack_u16(BORLAND_STACK_LOW_WATER_OFFSET)) {
|
|
win16_write_stack_u16(BORLAND_STACK_LOW_WATER_OFFSET, remaining);
|
|
}
|
|
return remaining;
|
|
}
|
|
|
|
/* 1008:298a — startup unit entry: execute the stack guard with AX=0. */
|
|
void tdkpin_initialize_stack_guard(void)
|
|
{
|
|
borland_check_stack(0);
|
|
}
|