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
|
|
|
*/
|
|
|
|
|
|
|
|
// Flag comparisons
|
|
|
|
// ----------------
|
|
|
|
// When used inside a conditional expression like
|
|
|
|
// if(FLAGS_*) { goto some_label; | return; }
|
2020-09-05 17:03:48 +00:00
|
|
|
// these assemble into the single given instruction. Apply the ! operator to
|
|
|
|
// get the N versions.
|
2020-11-08 20:04:36 +00:00
|
|
|
#define FLAGS_CARRY (_FLAGS & 0x01) /* JC / JAE / JB */
|
2020-09-05 16:51:10 +00:00
|
|
|
#define FLAGS_ZERO (_FLAGS & 0x40) /* JZ */
|
2020-11-06 13:37:10 +00:00
|
|
|
#define FLAGS_SIGN (_FLAGS & 0x80) /* JS */
|
2020-09-05 16:51:10 +00:00
|
|
|
// ----------------
|
2020-09-11 23:51:37 +00:00
|
|
|
|
2020-11-01 19:09:18 +00:00
|
|
|
// Alternate version that doesn't spill the port number to DX
|
2022-02-12 22:36:00 +00:00
|
|
|
#define outportb2(port, val) _asm { \
|
2020-11-01 19:09:18 +00:00
|
|
|
mov al, val; \
|
|
|
|
out port, al; \
|
|
|
|
}
|
|
|
|
|
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)];
|
|
|
|
};
|
|
|
|
|
2020-11-08 20:04:36 +00:00
|
|
|
// poke() versions that actually inline with pseudoregisters
|
|
|
|
// ---------------------------------------------------------
|
2020-11-06 13:37:10 +00:00
|
|
|
#define pokew(sgm, off, val) { *(uint16_t far *)(MK_FP(sgm, off)) = val; }
|
2020-11-08 20:04:36 +00:00
|
|
|
|
|
|
|
// Turbo C++ 4.0 generates wrong segment prefix opcodes for the _FS and _GS
|
|
|
|
// pseudoregisters - 0x46 (INC SI) and 0x4E (DEC SI) rather than the correct
|
|
|
|
// 0x64 and 0x65, respectively. These prefixes are also not supported in
|
|
|
|
// inline assembly, which is limited to pre-386 anyway. Compiling via assembly
|
|
|
|
// (`#pragma inline`) would work and generate the correct instructions here,
|
|
|
|
// but that would incur yet another dependency on a 16-bit TASM, for something
|
|
|
|
// honestly quite insignificant.
|
|
|
|
//
|
|
|
|
// So, can we somehow work around this issue while retaining the readability
|
|
|
|
// of the usage code and pretending that this bug doesn't exist? Comparisons
|
|
|
|
// with segment registers unfortunately don't inline, so something like
|
|
|
|
// if(sgm == _FS)
|
|
|
|
// wouldn't work, even inside a macro that replaces [sgm] with _FS. But since
|
|
|
|
// __emit__() *does* inline, we can use function templates! The default
|
|
|
|
// versions provide the regularly intended C code for all other registers,
|
|
|
|
// while explicit specializations for _FS and _GS __emit__() the correct
|
|
|
|
// instruction opcodes for all offset registers needed. Then, we only need to
|
|
|
|
// somehow move the pseudoregisters up into the type system... which can
|
|
|
|
// simply be done by turning them into class names via preprocessor token
|
|
|
|
// pasting. Sure, this limits this approach to raw registers with no immediate
|
|
|
|
// offsets, but let's hope we won't ever need those...
|
|
|
|
//
|
|
|
|
// Also, hey, no need for the MK_FP() macro if we directly return the correct
|
|
|
|
// types.
|
2021-01-26 15:33:06 +00:00
|
|
|
|
2021-03-07 12:07:29 +00:00
|
|
|
#if defined(__TURBOC__) && defined(__MSDOS__)
|
|
|
|
// Declared in <dos.h> in these compilers.
|
|
|
|
void __emit__(uint8_t __byte, ...);
|
|
|
|
#endif
|
2020-11-09 14:34:53 +00:00
|
|
|
|
2021-03-07 12:07:29 +00:00
|
|
|
struct Decomp_ES { void __seg* value() { return (void __seg *)(_ES); } };
|
|
|
|
struct Decomp_FS { void __seg* value() { return (void __seg *)(_FS); } };
|
|
|
|
struct Decomp_GS { void __seg* value() { return (void __seg *)(_GS); } };
|
|
|
|
struct Decomp_DI { void __near* value() { return (void __near *)(_DI); } };
|
2020-11-08 20:04:36 +00:00
|
|
|
|
2021-03-07 12:07:29 +00:00
|
|
|
// Removing [val] from the parameter lists of the template functions below
|
|
|
|
// perfects the inlining.
|
|
|
|
#define poked(sgm, off, val) \
|
|
|
|
_EAX = val; \
|
|
|
|
poked_eax((Decomp##sgm *)NULL, (Decomp##off *)NULL, (uint8_t)(0x89));
|
2020-11-08 20:04:36 +00:00
|
|
|
|
2021-03-07 12:07:29 +00:00
|
|
|
#define poke_or_d(sgm, off, val) \
|
|
|
|
_EAX = val; \
|
|
|
|
poked_eax((Decomp##sgm *)NULL, (Decomp##off *)NULL, (uint8_t)(0x09));
|
2020-11-08 20:04:36 +00:00
|
|
|
|
2021-03-07 12:07:29 +00:00
|
|
|
template <class Segment, class Offset> inline void poked_eax(
|
|
|
|
Segment *sgm, Offset *off, uint8_t op
|
|
|
|
) {
|
|
|
|
if(op == 0x89) {
|
|
|
|
*(uint32_t far *)(sgm->value() + off->value()) = _EAX;
|
|
|
|
} else if(op == 0x09) {
|
|
|
|
*(uint32_t far *)(sgm->value() + off->value()) |= _EAX;
|
2020-11-08 20:04:36 +00:00
|
|
|
}
|
2021-03-07 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void poked_eax(Decomp_FS *sgm, Decomp_DI *off, uint8_t op) {
|
|
|
|
__emit__(0x66, 0x64, op, 0x05); // [op] FS:[DI], EAX
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void poked_eax(Decomp_GS *sgm, Decomp_DI *off, uint8_t op) {
|
|
|
|
__emit__(0x66, 0x65, op, 0x05); // [op] GS:[DI], EAX
|
|
|
|
}
|
2021-05-30 18:09:31 +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;
|
|
|
|
return reinterpret_cast<pixel_t>(&address_0);
|
|
|
|
}
|
|
|
|
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.)
|
|
|
|
#define inhibit_Z3(type, x) \
|
|
|
|
*reinterpret_cast<type near *>(reinterpret_cast<uint16_t>(&x))
|
|
|
|
|
2021-05-30 18:09:31 +00:00
|
|
|
#else
|
|
|
|
#define keep_0(x) x
|
2022-01-11 18:26:28 +00:00
|
|
|
#define inhibit_Z3(type, x) x
|
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);
|