2020-09-05 16:51:10 +00:00
|
|
|
/* ReC98
|
|
|
|
* -----
|
2021-09-06 18:16:39 +00:00
|
|
|
* Declarations to help decompiling the seemingly impossible
|
2020-09-05 16:51:10 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-09 01:24:33 +00:00
|
|
|
#define DECOMP_HPP
|
|
|
|
|
2020-09-11 23:51:37 +00:00
|
|
|
// Alternate version that sets the value first
|
2022-02-12 22:36:00 +00:00
|
|
|
#define outport2(port, val) _asm { \
|
2020-09-11 23:51:37 +00:00
|
|
|
mov ax, val; \
|
|
|
|
mov dx, port; \
|
|
|
|
out dx, ax; \
|
|
|
|
}
|
2020-11-06 13:37:10 +00:00
|
|
|
|
2021-09-06 18:16:39 +00:00
|
|
|
// Should just be unwrapped wherever it appears. Code that directly uses T
|
|
|
|
// would be much cleaner.
|
|
|
|
template <class T> union StupidBytewiseWrapperAround {
|
|
|
|
T t;
|
|
|
|
int8_t byte[sizeof(T)];
|
|
|
|
uint8_t ubyte[sizeof(T)];
|
|
|
|
};
|
|
|
|
|
2022-04-04 04:12:44 +00:00
|
|
|
// Does exactly the same as Turbo C++'s __memcpy__() intrinsic, just with
|
|
|
|
// stupidly reordered instructions. Can be replaced with regular copy
|
|
|
|
// assignment wherever it appears.
|
2023-06-16 06:48:18 +00:00
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define prepare_di_si(dst, dst_offset, src, src_offset) { \
|
|
|
|
_DI = dst; \
|
|
|
|
_DI += dst_offset; \
|
|
|
|
_SI = src; \
|
|
|
|
_SI += src_offset; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define prepare_si_di(dst, dst_offset, src, src_offset) { \
|
|
|
|
_SI = src; \
|
|
|
|
_SI += src_offset; \
|
|
|
|
_DI = dst; \
|
|
|
|
_DI += dst_offset; \
|
|
|
|
}
|
|
|
|
|
2022-04-04 04:12:44 +00:00
|
|
|
#if (GAME == 5)
|
2023-06-16 06:48:18 +00:00
|
|
|
#define copy_near_struct_member( \
|
|
|
|
dst, dst_offset, src, src_offset, size, prepare_func \
|
|
|
|
) { \
|
|
|
|
_CX = (size / sizeof(uint16_t)); \
|
2022-04-04 04:12:44 +00:00
|
|
|
asm { push ds; pop es; } \
|
2023-06-16 06:48:18 +00:00
|
|
|
prepare_func(FP_OFF(&dst), dst_offset, FP_OFF(&src), src_offset); \
|
2022-04-04 04:12:44 +00:00
|
|
|
asm { rep movsw; } \
|
|
|
|
}
|
|
|
|
#else
|
2023-06-16 06:48:18 +00:00
|
|
|
#define copy_near_struct_member( \
|
|
|
|
dst, dst_offset, src, src_offset, size, prepare_func \
|
|
|
|
) { \
|
2022-04-04 04:12:44 +00:00
|
|
|
asm { push ds; pop es; } \
|
2023-06-16 06:48:18 +00:00
|
|
|
prepare_func(FP_OFF(&dst), dst_offset, FP_OFF(&src), src_offset); \
|
|
|
|
_CX = (size / sizeof(uint16_t)); \
|
|
|
|
asm { rep movsw; } \
|
2022-04-04 04:12:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
2023-06-16 06:48:18 +00:00
|
|
|
// ----------------------------------------------------------------------
|
2022-04-04 04:12:44 +00:00
|
|
|
|
2022-01-11 18:26:28 +00:00
|
|
|
// Circumventing compiler optimizations
|
|
|
|
// ------------------------------------
|
|
|
|
// If you don't want to recreate the code layout of the original PC-98
|
|
|
|
// binaries, these can be safely deleted. They just make the code worse.
|
|
|
|
|
2021-05-30 18:09:31 +00:00
|
|
|
#if defined(__TURBOC__) && defined(__MSDOS__)
|
|
|
|
// Use this function wherever the original code used a immediate 0 literal
|
|
|
|
// that Turbo C++ would optimize away, e.g. in register assignments
|
|
|
|
// (_AX = 0 → XOR AX, AX) or comparisons (_AX == 0 → OR AX, AX). This way,
|
|
|
|
// the compiler is forced to leave space for any potential offset, with the
|
|
|
|
// literal 0 then being spelled out by the linker.
|
|
|
|
template <class T> inline T keep_0(T x) {
|
|
|
|
if(x == 0) {
|
|
|
|
extern void *near address_0;
|
2022-08-15 10:53:06 +00:00
|
|
|
return reinterpret_cast<T>(&address_0);
|
2021-05-30 18:09:31 +00:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
2022-01-11 18:26:28 +00:00
|
|
|
|
|
|
|
// Bypasses the -Z -3 function parameter optimization, where [x] would be
|
|
|
|
// combined with any potential subsequent 16-bit parameter adjacent in
|
|
|
|
// memory to form a 32-bit PUSH.
|
|
|
|
// (Interestingly, using a template function inlines either too well or
|
|
|
|
// too badly. Only this macro guarantees the intended 16-bit PUSH to be
|
|
|
|
// consistently emitted.)
|
2022-06-24 21:25:08 +00:00
|
|
|
#define inhibit_Z3(x) \
|
|
|
|
*reinterpret_cast<int16_t near *>(reinterpret_cast<uint16_t>(&x))
|
2022-01-11 18:26:28 +00:00
|
|
|
|
2022-07-09 19:40:53 +00:00
|
|
|
// Calling an empty inlined function prevents certain jump optimizations.
|
|
|
|
inline void optimization_barrier(void) {
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:09:31 +00:00
|
|
|
#else
|
|
|
|
#define keep_0(x) x
|
2022-06-24 21:25:08 +00:00
|
|
|
#define inhibit_Z3(x) x
|
2022-07-09 19:40:53 +00:00
|
|
|
#define optimization_barrier()
|
2021-05-30 18:09:31 +00:00
|
|
|
#endif
|
2022-01-11 18:26:28 +00:00
|
|
|
// ------------------------------------
|
2020-11-08 20:04:36 +00:00
|
|
|
|
|
|
|
// 32-bit ASM instructions not supported by Turbo C++ 4.0J's built-in
|
|
|
|
// assembler. Makes no sense to compile with `#pragma inline` (and thus,
|
|
|
|
// require a 16-bit TASM) just for those.
|
|
|
|
#define MOVSD __emit__(0x66, 0xA5);
|
|
|
|
#define REP __emit__(0xF3);
|