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
62 lines
1.5 KiB
C
62 lines
1.5 KiB
C
/* Borland Win16 random-number support reconstructed from Code5. */
|
|
|
|
#include "tdkpin_random.h"
|
|
|
|
enum { BORLAND_RANDOM_MULTIPLIER = 0x08088405u };
|
|
|
|
uint32_t borland_next_random(void)
|
|
{
|
|
g_borland_random_seed =
|
|
g_borland_random_seed * BORLAND_RANDOM_MULTIPLIER + 1u;
|
|
return g_borland_random_seed;
|
|
}
|
|
|
|
uint16_t borland_random_below(uint16_t upper_bound)
|
|
{
|
|
uint32_t random = borland_next_random();
|
|
return (uint16_t)(((uint64_t)random * upper_bound) >> 32);
|
|
}
|
|
|
|
BorlandReal48 borland_random_real48_registers(void)
|
|
{
|
|
uint32_t random = borland_next_random();
|
|
if (random == 0) {
|
|
return (BorlandReal48){{0, 0, 0, 0, 0, 0}};
|
|
}
|
|
uint8_t exponent = 0x80;
|
|
while ((random & 0x80000000u) == 0) {
|
|
random <<= 1;
|
|
exponent--;
|
|
}
|
|
random &= 0x7fffffffu;
|
|
return (BorlandReal48){{
|
|
exponent,
|
|
0,
|
|
(uint8_t)random,
|
|
(uint8_t)(random >> 8),
|
|
(uint8_t)(random >> 16),
|
|
(uint8_t)(random >> 24),
|
|
}};
|
|
}
|
|
|
|
int8_t borland_random_exponent(void)
|
|
{
|
|
return (int8_t)borland_random_real48_registers().bytes[0];
|
|
}
|
|
|
|
/*
|
|
* 1020:14de — x87 Random: reinterpret the signed FILD of RandSeed as unsigned
|
|
* by adding exactly 2^31, then FSCALE by -32. Binary64 represents every
|
|
* uint32_t/2^32 result exactly, so this host spelling loses no target bits.
|
|
*/
|
|
double borland_random_unit_interval(void)
|
|
{
|
|
uint32_t random = borland_next_random();
|
|
return (double)random / 4294967296.0;
|
|
}
|
|
|
|
void borland_seed_random_from_dos_time(void)
|
|
{
|
|
g_borland_random_seed = win16_dos_time_words();
|
|
}
|