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
141 lines
4.5 KiB
C
141 lines
4.5 KiB
C
#include "../tdkpin_palette.h"
|
|
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
static uint8_t g_dgroup[0x5000];
|
|
static uint8_t g_allocation[sizeof(LOGPALETTE256)];
|
|
static uint8_t g_resource_bytes[768];
|
|
static uint8_t g_bitmap_info[sizeof(BITMAPINFO256)];
|
|
static unsigned g_range_checks;
|
|
static unsigned g_get_mem_calls;
|
|
static unsigned g_free_mem_calls;
|
|
static unsigned g_create_palette_calls;
|
|
static unsigned g_find_calls;
|
|
static unsigned g_load_calls;
|
|
static unsigned g_lock_calls;
|
|
static unsigned g_unlock_calls;
|
|
|
|
static Win16FarPtr dgroup_at(uint16_t offset)
|
|
{
|
|
return win16_make_far_pointer(0x5000, offset);
|
|
}
|
|
|
|
Win16FarPtr borland_get_mem(uint16_t bytes)
|
|
{
|
|
assert(bytes == sizeof(LOGPALETTE256));
|
|
g_get_mem_calls++;
|
|
return win16_make_far_pointer(0x6000, 0);
|
|
}
|
|
|
|
void borland_free_mem(Win16FarPtr block, uint16_t bytes)
|
|
{
|
|
assert(block == win16_make_far_pointer(0x6000, 0));
|
|
assert(bytes == sizeof(LOGPALETTE256));
|
|
g_free_mem_calls++;
|
|
}
|
|
|
|
void borland_check_int32_range(
|
|
int32_t value, const BorlandInt32Range *range)
|
|
{
|
|
assert(value >= range->minimum && value <= range->maximum);
|
|
if (range->maximum == 0xffff) {
|
|
assert(value == sizeof(LOGPALETTE256));
|
|
} else {
|
|
assert(range->minimum == 0 && range->maximum == 0x02ff);
|
|
}
|
|
g_range_checks++;
|
|
}
|
|
|
|
HPALETTE16 CreatePalette16(Win16FarPtr logical_palette)
|
|
{
|
|
assert(logical_palette == win16_make_far_pointer(0x6000, 0));
|
|
assert(win16_read_u16(logical_palette) == 0x0300);
|
|
assert(win16_read_u16(win16_far_add_offset(logical_palette, 2)) == 256);
|
|
for (uint16_t index = 0; index < 256; index++) {
|
|
Win16FarPtr entry = win16_far_add_offset(
|
|
logical_palette, (uint16_t)(4 + index * 4));
|
|
assert(win16_read_u8(entry) == g_dgroup[0x4450 + index * 3]);
|
|
assert(win16_read_u8(win16_far_add_offset(entry, 1)) ==
|
|
g_dgroup[0x4451 + index * 3]);
|
|
assert(win16_read_u8(win16_far_add_offset(entry, 2)) ==
|
|
g_dgroup[0x4452 + index * 3]);
|
|
assert(win16_read_u8(win16_far_add_offset(entry, 3)) == 0);
|
|
}
|
|
g_create_palette_calls++;
|
|
return 0x2468;
|
|
}
|
|
|
|
HRSRC16 FindResource16(
|
|
HINSTANCE16 instance, Win16FarPtr name, Win16FarPtr type)
|
|
{
|
|
assert(instance == 0x4444);
|
|
assert(name == win16_make_far_pointer(0, 999));
|
|
assert(type == dgroup_at(0x045a));
|
|
g_find_calls++;
|
|
return 0x1111;
|
|
}
|
|
|
|
HGLOBAL16 LoadResource16(HINSTANCE16 instance, HRSRC16 resource)
|
|
{
|
|
assert(instance == 0x4444 && resource == 0x1111);
|
|
g_load_calls++;
|
|
return 0x2222;
|
|
}
|
|
|
|
Win16FarPtr LockResource16(HGLOBAL16 resource)
|
|
{
|
|
assert(resource == 0x2222);
|
|
g_lock_calls++;
|
|
return win16_make_far_pointer(0x7000, 0);
|
|
}
|
|
|
|
BOOL16 GlobalUnlock16(HGLOBAL16 resource)
|
|
{
|
|
assert(resource == 0x2222);
|
|
g_unlock_calls++;
|
|
return 1;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
win16_reset_segment_bindings();
|
|
win16_bind_segment(0x5000, g_dgroup, sizeof(g_dgroup), true);
|
|
win16_bind_segment(0x6000, g_allocation, sizeof(g_allocation), true);
|
|
win16_bind_segment(0x7000, g_resource_bytes, sizeof(g_resource_bytes), false);
|
|
win16_bind_segment(0x8000, g_bitmap_info, sizeof(g_bitmap_info), true);
|
|
win16_set_dgroup_selector(0x5000);
|
|
memcpy(&g_dgroup[0x045a], "pal", 4);
|
|
for (uint16_t index = 0; index < 768; index++) {
|
|
g_dgroup[0x4450 + index] = (uint8_t)(index * 37 + 11);
|
|
g_resource_bytes[index] = (uint8_t)(index * 19 + 7);
|
|
}
|
|
|
|
tdkpin_create_palette_from_rgb_table();
|
|
assert(g_get_mem_calls == 1 && g_create_palette_calls == 1);
|
|
assert(g_range_checks == 1 && g_free_mem_calls == 1);
|
|
assert(win16_read_u16(dgroup_at(0x444e)) == 0x2468);
|
|
|
|
g_range_checks = 0;
|
|
tdkpin_load_palette_resource(
|
|
dgroup_at(0x4450), win16_make_far_pointer(0, 999), 0x4444);
|
|
assert(g_find_calls == 1 && g_load_calls == 1 && g_lock_calls == 1);
|
|
assert(g_unlock_calls == 1 && g_range_checks == 1536);
|
|
assert(memcmp(&g_dgroup[0x4450], g_resource_bytes, 768) == 0);
|
|
|
|
Win16FarPtr bitmap_info = win16_make_far_pointer(0x8000, 0);
|
|
win16_write_u16(
|
|
bitmap_info, offsetof(BITMAPINFOHEADER16, bit_count), 8);
|
|
tdkpin_fill_bitmap_info_palette(bitmap_info);
|
|
for (uint16_t index = 0; index < 256; index++) {
|
|
size_t output = offsetof(BITMAPINFO256, colors) + index * 4;
|
|
assert(g_bitmap_info[output] == g_dgroup[0x4452 + index * 3]);
|
|
assert(g_bitmap_info[output + 1] == g_dgroup[0x4451 + index * 3]);
|
|
assert(g_bitmap_info[output + 2] == g_dgroup[0x4450 + index * 3]);
|
|
assert(g_bitmap_info[output + 3] == 0);
|
|
}
|
|
return 0;
|
|
}
|