Files
tdkpin/original/reconstructed/tests/test_bitmap_loader.c
T
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

175 lines
5.0 KiB
C

#include "../tdkpin_palette.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
static uint8_t g_dgroup[0x5000];
static uint8_t g_resource[2048];
static uint8_t g_bitmap_info[sizeof(BITMAPINFO256)];
static unsigned g_copy_calls;
static unsigned g_step;
static unsigned g_allocations;
static Win16FarPtr g_stack_header;
static Win16FarPtr dgroup_at(uint16_t offset)
{
return win16_make_far_pointer(0x5000, offset);
}
void borland_far_copy(
Win16FarPtr destination, Win16FarPtr source, uint16_t count)
{
assert(count == sizeof(BITMAPINFOHEADER16));
for (uint16_t index = 0; index < count; index++) {
win16_write_u8(
win16_far_add_offset(destination, index),
win16_read_u8(win16_far_add_offset(source, index)));
}
g_copy_calls++;
}
Win16FarPtr borland_get_mem(uint16_t bytes)
{
assert(bytes == sizeof(BITMAPINFO256));
g_allocations++;
return win16_make_far_pointer(0x7000, 0);
}
HRSRC16 FindResource16(
HINSTANCE16 instance, Win16FarPtr name, Win16FarPtr type)
{
assert(++g_step == 1 && instance == 0x4444);
assert(name == win16_make_far_pointer(0, 900));
assert(type == dgroup_at(0x045e));
return 0x1111;
}
HGLOBAL16 LoadResource16(HINSTANCE16 instance, HRSRC16 resource)
{
assert(++g_step == 2 && instance == 0x4444 && resource == 0x1111);
return 0x2222;
}
Win16FarPtr LockResource16(HGLOBAL16 resource)
{
assert(++g_step == 3 && resource == 0x2222);
return win16_make_far_pointer(0x6000, 0);
}
HDC16 CreateDC16(
Win16FarPtr driver,
Win16FarPtr device,
Win16FarPtr output,
Win16FarPtr initialization_data)
{
assert(++g_step == 4);
assert(driver == dgroup_at(0x0462));
assert(device == 0 && output == 0 && initialization_data == 0);
return 0x3333;
}
HPALETTE16 SelectPalette16(
HDC16 dc, HPALETTE16 palette, BOOL16 force_background)
{
assert(dc == 0x3333 && force_background == 0);
if (++g_step == 5) {
assert(palette == 0x4444);
return 0x5555;
}
assert(g_step == 9 && palette == 0x5555);
return 0x4444;
}
BOOL16 UnrealizeObject16(HGDIOBJ16 object)
{
assert(++g_step == 6 && object == 0x4444);
return 1;
}
UINT16 RealizePalette16(HDC16 dc)
{
assert(++g_step == 7 && dc == 0x3333);
return 256;
}
HBITMAP16 CreateDIBitmap16(
HDC16 dc,
Win16FarPtr header,
uint32_t initialization,
Win16FarPtr bits,
Win16FarPtr bitmap_info,
UINT16 color_usage)
{
assert(++g_step == 8 && dc == 0x3333);
assert(initialization == 4 && color_usage == 0);
assert(bits == win16_make_far_pointer(0x6000, 40));
assert(bitmap_info == win16_make_far_pointer(0x7000, 0));
assert(win16_read_u16(
win16_far_add_offset(header, offsetof(BITMAPINFOHEADER16, bit_count))) == 8);
g_stack_header = header;
for (uint16_t index = 0; index < 40; index++) {
assert(win16_read_u8(win16_far_add_offset(bitmap_info, index)) ==
g_resource[index]);
}
for (uint16_t index = 0; index < 256; index++) {
uint16_t color = (uint16_t)(40 + index * 4);
assert(win16_read_u8(win16_far_add_offset(bitmap_info, color)) ==
g_dgroup[0x4452 + index * 3]);
assert(win16_read_u8(win16_far_add_offset(bitmap_info, (uint16_t)(color + 1))) ==
g_dgroup[0x4451 + index * 3]);
assert(win16_read_u8(win16_far_add_offset(bitmap_info, (uint16_t)(color + 2))) ==
g_dgroup[0x4450 + index * 3]);
assert(win16_read_u8(win16_far_add_offset(bitmap_info, (uint16_t)(color + 3))) == 0);
}
return 0x6666;
}
BOOL16 DeleteDC16(HDC16 dc)
{
assert(++g_step == 10 && dc == 0x3333);
return 1;
}
BOOL16 GlobalUnlock16(HGLOBAL16 resource)
{
assert(++g_step == 11 && resource == 0x2222);
return 1;
}
BOOL16 FreeResource16(HGLOBAL16 resource)
{
assert(++g_step == 12 && resource == 0x2222);
return 1;
}
int main(void)
{
uint8_t stack_marker;
uintptr_t stack_base = (uintptr_t)&stack_marker - 0x8000;
win16_reset_segment_bindings();
win16_bind_segment(0x5000, g_dgroup, sizeof(g_dgroup), true);
win16_bind_segment(0x6000, g_resource, sizeof(g_resource), true);
win16_bind_segment(0x7000, g_bitmap_info, sizeof(g_bitmap_info), true);
win16_bind_segment(0x8000, (void *)stack_base, 0x10000, true);
win16_set_dgroup_selector(0x5000);
memcpy(&g_dgroup[0x045e], "DAT", 4);
memcpy(&g_dgroup[0x0462], "Display", 8);
win16_write_u16(dgroup_at(0x444e), 0, 0x4444);
for (uint16_t index = 0; index < 768; index++) {
g_dgroup[0x4450 + index] = (uint8_t)(index * 13 + 9);
}
memset(g_resource, 0, sizeof(g_resource));
g_resource[0] = 40;
g_resource[14] = 8;
for (uint16_t index = 40; index < sizeof(g_resource); index++) {
g_resource[index] = (uint8_t)(index * 7 + 3);
}
HBITMAP16 bitmap = tdkpin_load_bitmap_resource(
win16_make_far_pointer(0, 900), 0x4444);
assert(bitmap == 0x6666 && g_step == 12);
assert(g_copy_calls == 2 && g_allocations == 1 && g_stack_header != 0);
return 0;
}