Files
ddidderr 8b99e9607c feat(reconstruction): complete binary-backed C recovery
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
2026-08-23 16:41:17 +02:00

414 lines
14 KiB
C

#include "../tdkpin_dialog.h"
#include <assert.h>
static Win16FarPtr g_dialog;
static Win16FarPtr g_child;
static Win16FarPtr g_close_proc;
static Win16FarPtr g_closed_window;
static HWND16 g_end_window;
static INT16 g_end_result;
static unsigned g_for_each_calls;
static Win16FarPtr g_virtual_proc;
static uint16_t g_virtual_argument;
static Win16FarPtr g_disposed_string;
static unsigned g_destruct_sequence;
static HWND16 g_text_window;
static INT16 g_text_control;
static Win16FarPtr g_text_buffer;
static INT16 g_text_maximum;
static unsigned g_prepare_calls;
static Win16FarPtr g_caption;
static INT16 g_set_ids[2];
static Win16FarPtr g_set_texts[2];
static unsigned g_set_text_calls;
static INT16 g_send_id;
static UINT16 g_send_message;
static WPARAM16 g_send_wparam;
static LPARAM16 g_send_lparam;
Win16FarPtr g_win16_create_dialog_param;
Win16FarPtr g_win16_dialog_box_param;
uint8_t g_dialog_alternate_template;
Win16FarPtr g_object_windows_application;
HINSTANCE16 g_win16_instance;
static bool g_constructor_enters = true;
static unsigned g_constructor_entry_calls;
static Win16FarPtr g_construct_parent;
static unsigned g_clear_flag_calls;
static unsigned g_set_flag_calls;
static Win16FarPtr g_duplicate_result;
static Win16FarPtr g_duplicate_source;
static HWND16 g_create_result;
static INT16 g_modal_result;
static unsigned g_create_calls;
static unsigned g_modal_calls;
static Win16FarPtr g_expected_template;
static HWND16 g_expected_parent_window;
static Win16FarPtr g_expected_dialog_proc;
static LPARAM16 g_expected_init_parameter;
void object_windows_for_each_child(
Win16FarPtr container, Win16FarPtr action)
{
assert(container == g_dialog);
assert(action == win16_make_far_pointer(0x6000, 0x2bb1));
g_for_each_calls++;
object_windows_dialog_mark_child(0xffff, g_child);
}
void object_windows_set_flag_4_if_windowed(
uint16_t ignored, Win16FarPtr object)
{
(void)ignored;
if (win16_read_u16(win16_far_add_offset(object, 4)) != 0) {
Win16FarPtr flags = win16_far_add_offset(object, 0x16);
win16_write_u8(flags, (uint8_t)(win16_read_u8(flags) | 4));
}
}
BOOL16 EndDialog16(HWND16 dialog, INT16 result)
{
g_end_window = dialog;
g_end_result = result;
return 1;
}
void object_windows_close_if_allowed(Win16FarPtr window)
{
g_closed_window = window;
}
void win16_call_object_u16_method(
Win16FarPtr procedure, Win16FarPtr object, uint16_t argument)
{
assert(object == g_dialog);
g_virtual_proc = procedure;
g_virtual_argument = argument;
}
void borland_far_strdispose(Win16FarPtr string)
{
assert(++g_destruct_sequence == 1);
g_disposed_string = string;
}
void object_windows_object_destruct(Win16FarPtr object, uint16_t vmt)
{
unsigned expected = g_disposed_string == 0 ? 1u : 2u;
assert(++g_destruct_sequence == expected);
assert(object == g_dialog && vmt == 0);
}
void borland_finish_object_destructor(
BorlandObjectCallFrame *frame, uint16_t vmt_field_offset)
{
unsigned expected = g_disposed_string == 0 ? 2u : 3u;
assert(++g_destruct_sequence == expected);
assert(frame->object == g_dialog && frame->vmt_argument == 0x0200);
assert(vmt_field_offset == 0);
}
UINT16 GetDlgItemTextFar16(
HWND16 dialog,
INT16 control_id,
Win16FarPtr buffer,
INT16 maximum)
{
g_text_window = dialog;
g_text_control = control_id;
g_text_buffer = buffer;
g_text_maximum = maximum;
return 7;
}
void object_windows_prepare_window(Win16FarPtr window)
{
assert(window == g_dialog);
g_prepare_calls++;
}
BOOL16 SetWindowTextFar16(HWND16 window, Win16FarPtr text)
{
assert(window == 0x77);
g_caption = text;
return 0;
}
BOOL16 SetDlgItemText16(
HWND16 dialog, INT16 control_id, Win16FarPtr text)
{
assert(dialog == 0x77 && g_set_text_calls < 2);
g_set_ids[g_set_text_calls] = control_id;
g_set_texts[g_set_text_calls] = text;
g_set_text_calls++;
return 0;
}
LRESULT16 SendDlgItemMessage16(
HWND16 dialog,
INT16 control_id,
UINT16 message,
WPARAM16 wparam,
LPARAM16 lparam)
{
assert(dialog == 0x77);
g_send_id = control_id;
g_send_message = message;
g_send_wparam = wparam;
g_send_lparam = lparam;
return -1;
}
bool borland_enter_object_constructor(
BorlandObjectCallFrame *frame, uint16_t vmt_field_offset)
{
assert(vmt_field_offset == 0);
g_constructor_entry_calls++;
if (g_constructor_enters && frame->vmt_argument != 0) {
win16_write_u16(frame->object, 0, frame->vmt_argument);
}
return g_constructor_enters;
}
Win16FarPtr object_windows_object_construct(
Win16FarPtr object, uint16_t vmt, Win16FarPtr parent)
{
assert(object == g_dialog && vmt == 0);
g_construct_parent = parent;
return object;
}
void object_windows_clear_flag_4(Win16FarPtr object)
{
assert(object == g_dialog);
g_clear_flag_calls++;
}
void object_windows_set_flag_1(Win16FarPtr object)
{
assert(object == g_dialog);
g_set_flag_calls++;
}
Win16FarPtr borland_far_strdup(Win16FarPtr source)
{
g_duplicate_source = source;
return g_duplicate_result;
}
HWND16 win16_call_create_dialog_param(
Win16FarPtr procedure,
HINSTANCE16 instance,
Win16FarPtr template_name,
HWND16 parent,
Win16FarPtr dialog_proc,
LPARAM16 init_parameter)
{
assert(procedure == g_win16_create_dialog_param);
assert(instance == g_win16_instance);
assert(template_name == g_expected_template);
assert(parent == g_expected_parent_window);
assert(dialog_proc == g_expected_dialog_proc);
assert(init_parameter == g_expected_init_parameter);
g_create_calls++;
return g_create_result;
}
INT16 win16_call_dialog_box_param(
Win16FarPtr procedure,
HINSTANCE16 instance,
Win16FarPtr template_name,
HWND16 parent,
Win16FarPtr dialog_proc,
LPARAM16 init_parameter)
{
assert(procedure == g_win16_dialog_box_param);
assert(instance == g_win16_instance);
assert(template_name == g_expected_template);
assert(parent == g_expected_parent_window);
assert(dialog_proc == g_expected_dialog_proc);
assert(init_parameter == g_expected_init_parameter);
g_modal_calls++;
win16_write_u16(g_object_windows_application, 0x0e, 0xdead);
win16_write_u16(g_object_windows_application, 0x10, 0xbeef);
return g_modal_result;
}
int main(void)
{
static uint8_t dgroup[0x1000];
static uint8_t code[0x3000];
static uint8_t dialog_bytes[80];
static uint8_t child_bytes[80];
win16_reset_segment_bindings();
win16_bind_segment(0x5000, dgroup, sizeof(dgroup), true);
win16_set_dgroup_selector(0x5000);
win16_bind_ne_segment(2, 0x6000, code, sizeof(code), false);
win16_bind_segment(0x7000, dialog_bytes, sizeof(dialog_bytes), true);
win16_bind_segment(0x7100, child_bytes, sizeof(child_bytes), true);
g_dialog = win16_make_far_pointer(0x7000, 2);
g_child = win16_make_far_pointer(0x7100, 2);
win16_write_u16(g_dialog, 0, 0x0100);
win16_write_u16(g_dialog, 4, 0x77);
win16_write_u16(g_child, 4, 0x88);
g_close_proc = win16_make_far_pointer(0x6200, 0x1234);
win16_write_u16(win16_dgroup_pointer(0x0150), 0,
win16_far_offset(g_close_proc));
win16_write_u16(win16_dgroup_pointer(0x0152), 0,
win16_far_selector(g_close_proc));
object_windows_end_modal_dialog(g_dialog, 5);
assert(g_for_each_calls == 0 && g_end_window == 0);
win16_write_u8(win16_far_add_offset(g_dialog, 0x25), 1);
object_windows_end_modal_dialog(g_dialog, 5);
assert(g_for_each_calls == 1);
assert(g_end_window == 0x77 && g_end_result == 5);
assert((win16_read_u8(win16_far_add_offset(g_child, 0x16)) & 4) != 0);
ObjectWindowsMessage message = {0};
win16_write_u8(win16_far_add_offset(g_dialog, 0x25), 0);
object_windows_dialog_close(g_dialog, &message);
assert(g_closed_window == g_dialog);
win16_write_u8(win16_far_add_offset(g_dialog, 0x25), 1);
object_windows_dialog_close(g_dialog, &message);
assert(g_virtual_proc == g_close_proc && g_virtual_argument == 2);
Win16FarPtr string = win16_make_far_pointer(0x7300, 0x1111);
win16_write_u16(g_dialog, 0x1d, win16_far_offset(string));
win16_write_u16(g_dialog, 0x1f, win16_far_selector(string));
g_destruct_sequence = 0;
g_disposed_string = 0;
object_windows_dialog_destruct(g_dialog, 0x0200);
assert(g_destruct_sequence == 3 && g_disposed_string == string);
win16_write_u16(g_dialog, 0x1f, 0);
g_destruct_sequence = 0;
g_disposed_string = 0;
object_windows_dialog_destruct(g_dialog, 0x0200);
assert(g_destruct_sequence == 2 && g_disposed_string == 0);
Win16FarPtr text_buffer = win16_make_far_pointer(0x7400, 0x2222);
win16_write_u16(g_dialog, 0x2e, win16_far_offset(text_buffer));
win16_write_u16(g_dialog, 0x30, win16_far_selector(text_buffer));
win16_write_u16(g_dialog, 0x32, 0x1234);
assert(object_windows_dialog_read_text(g_dialog));
assert(g_text_window == 0x77 && g_text_control == 0x1234);
assert(g_text_buffer == text_buffer && g_text_maximum == 0x65);
Win16FarPtr caption = win16_make_far_pointer(0x7500, 0x0100);
Win16FarPtr label = win16_make_far_pointer(0x7500, 0x0200);
win16_write_u16(g_dialog, 0x26, win16_far_offset(caption));
win16_write_u16(g_dialog, 0x28, win16_far_selector(caption));
win16_write_u16(g_dialog, 0x2a, win16_far_offset(label));
win16_write_u16(g_dialog, 0x2c, win16_far_selector(label));
win16_write_u16(g_dialog, 0x32, 0);
g_prepare_calls = 0;
g_set_text_calls = 0;
object_windows_dialog_setup_text_fields(g_dialog);
assert(g_prepare_calls == 1 && g_caption == caption);
assert(g_set_text_calls == 2);
assert(g_set_ids[0] == 100 && g_set_texts[0] == label);
assert(g_set_ids[1] == 101 && g_set_texts[1] == text_buffer);
assert(g_send_id == 101 && g_send_message == 0x0415);
assert(g_send_wparam == 0xffff && g_send_lparam == 0);
Win16FarPtr parent = g_child;
Win16FarPtr template_name = win16_make_far_pointer(0x7500, 0x0300);
g_duplicate_result = win16_make_far_pointer(0x7600, 0x0400);
g_constructor_entry_calls = 0;
g_clear_flag_calls = 0;
g_duplicate_source = 0;
assert(object_windows_dialog_construct(
g_dialog, 0x0120, template_name, parent) == g_dialog);
assert(g_constructor_entry_calls == 1 && g_construct_parent == parent);
assert(g_duplicate_source == template_name);
assert(win16_read_far_pointer(g_dialog, 0x1d) == g_duplicate_result);
assert(win16_read_far_pointer(g_dialog, 0x21) == 0);
assert(win16_read_u8(win16_far_add_offset(g_dialog, 0x25)) == 0);
assert(g_clear_flag_calls == 1);
Win16FarPtr integer_template = win16_make_far_pointer(0, 0x7f02);
g_duplicate_source = 0;
(void)object_windows_dialog_construct(
g_dialog, 0, integer_template, 0);
assert(g_duplicate_source == 0);
assert(win16_read_far_pointer(g_dialog, 0x1d) == integer_template);
g_constructor_enters = false;
assert(object_windows_dialog_construct(
g_dialog, 0x0120, template_name, parent) == g_dialog);
g_constructor_enters = true;
g_dialog_alternate_template = 1;
g_constructor_entry_calls = 0;
Win16FarPtr input_text = win16_make_far_pointer(0x7500, 0x0500);
(void)object_windows_text_dialog_construct(
g_dialog, 0x0130, 0x1234, input_text, label, caption, parent);
assert(g_constructor_entry_calls == 2);
assert(win16_read_far_pointer(g_dialog, 0x1d) ==
win16_make_far_pointer(0, 0x7f05));
assert(win16_read_far_pointer(g_dialog, 0x26) == caption);
assert(win16_read_far_pointer(g_dialog, 0x2a) == label);
assert(win16_read_far_pointer(g_dialog, 0x2e) == input_text);
assert(win16_read_u16(win16_far_add_offset(g_dialog, 0x32)) == 0x1234);
g_win16_instance = 0x4444;
g_win16_create_dialog_param =
win16_make_far_pointer(0x6000, 0x1000);
g_win16_dialog_box_param =
win16_make_far_pointer(0x6000, 0x2000);
g_expected_template = win16_make_far_pointer(0, 0x7f05);
g_expected_dialog_proc = win16_make_far_pointer(0x6000, 0x3000);
g_expected_init_parameter = (LPARAM16)win16_make_far_pointer(
0x7000, 0x0040);
win16_write_u16(g_dialog, 0x12, win16_far_offset(g_expected_dialog_proc));
win16_write_u16(g_dialog, 0x14, win16_far_selector(g_expected_dialog_proc));
win16_write_u16(g_dialog, 0x21, (uint16_t)g_expected_init_parameter);
win16_write_u16(
g_dialog, 0x23, (uint16_t)((uint32_t)g_expected_init_parameter >> 16));
win16_write_u16(g_dialog, 6, win16_far_offset(parent));
win16_write_u16(g_dialog, 8, win16_far_selector(parent));
g_expected_parent_window = 0x88;
win16_write_u16(g_dialog, 2, 0);
g_create_result = 0x12fe;
g_create_calls = 0;
assert(object_windows_create_modeless_dialog(g_dialog) == 0x1201);
assert(g_create_calls == 1);
assert(win16_read_u16(win16_far_add_offset(g_dialog, 4)) == 0x12fe);
assert(win16_read_u8(win16_far_add_offset(g_dialog, 0x25)) == 0);
win16_write_u16(g_dialog, 2, 0);
g_create_result = 0;
assert(object_windows_create_modeless_dialog(g_dialog) == 0);
assert(win16_read_u16(win16_far_add_offset(g_dialog, 2)) == 0xffff);
win16_set_indeterminate_u16(0xabcd);
assert(object_windows_create_modeless_dialog(g_dialog) == 0xab00);
static uint8_t application_bytes[32];
win16_bind_segment(
0x7700, application_bytes, sizeof(application_bytes), true);
g_object_windows_application = win16_make_far_pointer(0x7700, 0);
Win16FarPtr previous_active = win16_make_far_pointer(0x7800, 0x1111);
win16_write_u16(
g_object_windows_application, 0x0e, win16_far_offset(previous_active));
win16_write_u16(
g_object_windows_application, 0x10, win16_far_selector(previous_active));
win16_write_u16(g_dialog, 2, 0);
g_modal_result = 5;
g_modal_calls = 0;
assert(object_windows_execute_modal_dialog(g_dialog) == 5);
assert(g_modal_calls == 1);
assert(win16_read_far_pointer(g_object_windows_application, 0x0e) ==
previous_active);
assert(win16_read_u16(win16_far_add_offset(g_dialog, 4)) == 0);
assert(win16_read_u8(win16_far_add_offset(g_dialog, 0x25)) == 1);
win16_write_u16(g_dialog, 2, 0);
g_modal_result = -1;
assert(object_windows_execute_modal_dialog(g_dialog) == -1);
assert(win16_read_u16(win16_far_add_offset(g_dialog, 2)) == 0xffff);
assert(object_windows_execute_modal_dialog(g_dialog) == -1);
return 0;
}