Commit Graph

47 Commits

Author SHA1 Message Date
nmlgc 6d572b33cf [Decompilation] [th01] Player: Main control and rendering function
Yup, what a giant, unstructured mess. Enjoy the 85 conditional
branches.

Completes P0163, funded by Ember2528.
2021-10-20 10:59:36 +02:00
nmlgc 81dd96e4f4 [Research] Discover how `SCOPY@` disables Turbo C++ stack cleanup optimization
Yup, it's a compiler bug, and it removes a small bit of freedom as far
as decompilation order is concerned. In particular, this means that we
can't do TH01's continue and pause menus before having decompiled the
bomb animation.
Would have been nice to pad out the previous push with those, but
instead, I had to spend way too much time figuring *this* out…

Completes P0161, funded by [Anonymous].
2021-10-09 23:28:22 +02:00
nmlgc 44d1389835 [Research] Discover how producing line numbers (`-y`) slightly optimizes jumps
Yes, seriously. Not something we actually need right now, but who
knows!

Part of P0161, funded by [Anonymous].
2021-10-09 23:27:59 +02:00
nmlgc a317cb49fc [Maintenance] Fix another bunch of accumulated typos
Part of P0161, funded by [Anonymous].
2021-10-09 23:26:02 +02:00
nmlgc 36068acb02 [Decompilation] [th01] Stage objects: Card score popup rendering
Part of P0159, funded by Yanga.
2021-09-28 18:05:25 +02:00
nmlgc 9c7981fc59 [Decompilation] [th01] Konngara: Pattern 10/12
The one with lasers fired from the sword across the whole playfield,
either from left to right or from right to left, together with aimed
3-way spreads, every 10 frames.

Part of P0155, funded by Ember2528.
2021-08-22 18:50:20 +02:00
nmlgc 4d24ca53bd [Decompilation] [th04/th05] Bullets: Update function
… (24 + (difficulty * 8) + rank) in TH04, and (42 + (difficulty * 8))
in TH05. Also, TH05 only doesn't have TH04's bullet zap animation
because ZUN didn't consistently use constants…

Completes P0151, funded by Blue Bolt and -Tom-.
2021-07-31 20:19:33 +02:00
nmlgc 18e755d99b [Decompilation] [th04] Bullets: Regular spawn function
Part of P0150, funded by Blue Bolt.
2021-07-31 20:19:32 +02:00
nmlgc 2c52bb5ef3 [Decompilation] [th04] Bullets: Velocity and angle calculation
`switch` statements compiling to binary searches if the range of values
is nasty enough? That's so cool. Apart from a few places in TH02,
this is the only place in PC-98 Touhou to show off that Turbo C++
optimization.

That code's still unexpectedly janky for what you'd expect from the 4th
game in the series, though.

Part of P0150, funded by Blue Bolt.
2021-07-31 16:15:32 +02:00
nmlgc db05bffca3 [Maintenance] Turn motion_t into a template
motion_t is also used for certain animations in MAINE.EXE, so not all
instances refer to entities in playfield space. Explicitly specifying
the latter now allows us to gain…

Part of P0149, funded by Blue Bolt, Ember2528, and -Tom-.
2021-07-31 09:33:50 +02:00
nmlgc 6233a3c3e3 [Research] Assigning near functions from other groups to near function pointers
Yup, P0148 didn't actually solve the issue it was meant to solve, and
I still had to research this workaround.

Part of P0149, funded by Blue Bolt, Ember2528, and -Tom-.
2021-07-31 09:33:49 +02:00
nmlgc aae96aec45 [Decompilation] Find out how to bypass TCC's optimization of 0 immediates
By deferring that immediate 0 to link time. 🤦

Part of P0146, funded by -Tom- and Ember2528.
2021-06-09 23:12:04 +02:00
nmlgc 794c6ab55c [Maintenance] Fix another bunch of accumulated typos
Part of P0139, funded by [Anonymous].
2021-05-11 18:47:49 +02:00
nmlgc 0f75a77dee [Research] Discover a workaround to word-align code segments from Turbo C++
Turns out that this is one of the effects of the -WX option ("Create
DPMI application")… along with generally messing up code generation.
Nothing we can't work around though, luckily! Finally getting to cross
that off the list of reasons that prevent decompilation.

Part of P0137, funded by [Anonymous].
2021-04-03 20:19:41 +02:00
nmlgc 152ecaa496 [Separate translation units] [th05] Music Room piano (undecompilable functions)
Reasons:
• piano_fm_part_put_raw(): SI register referenced and not saved on
  the stack
• piano_current_note_from(): Would be decompilable… into a mess.
  Not worth adding a separate translation unit just for it.
• piano_part_keys_put_raw(): DI register saved before the SI register
• piano_pressed_key_put(): DI register referenced and not immediately
  saved on the stack
• piano_label_put_raw(): SI and DI registered referenced and not saved
  on the stack
• grcg_setcolor_direct_seg1_raw(): Let's procrastinate this one until
  we have to reference all of these instances in C land.

And we could have even emitted that PIANO_KEY_PRESSED_TOP pixel data
into the code segment, by using `#pragma option -z` to give identical
names to both the code and the data segment. At least we can decompile
the first two functions here.

Part of P0135, funded by [Anonymous].
2021-03-19 23:23:06 +01:00
nmlgc 84d4914a3b [Separate translation units] [th02] snd_mmd_resident() (undecompilable)
Reason: Wants to be word-aligned, and the previous version in OP.EXE,
game_exit(), is not, despite having an even length :(
Oh well, at least I'm confident enough about it by now to document it.
And out of all decompilations to be thrown away, this is a pretty
dispensable one.

Part of P0133, funded by [Anonymous].
2021-01-31 15:21:11 +01:00
nmlgc 21d82fab04 [Maintenance] Declare the input bitflags in a consistent way
Turns out the inlining behavior of `const` variables at global scope
that we've been relying on lately is actually exclusive to C++ mode…
once again!

Part of P0133, funded by [Anonymous].
2021-01-30 19:13:11 +01:00
nmlgc 8b0165738a [Decompilation] [th03] .MRS: Byte-aligned, opaque blitting
Containing not one, but two decompilation innovations, one of which
works around a compiler bug using C++ template functions…

Completes P0126, funded by [Anonymous] and Blue Bolt.
2020-11-16 20:01:32 +01:00
nmlgc 967bb8b633 [Decompilation] [th03] Input mode and delay functions
Nice to see that Borland C++ optimizes bit-tests to cover just the high
or low byte of a word if possible, and that these don't have to be
two-byte structures after all.

Completes P0114, funded by Lmocinemod.
2020-09-07 21:18:40 +02:00
nmlgc 2ef3db3052 [Maintenance] Remove the `operator =` overloads for Subpixels
At least we've now documented their negative effects.

Part of P0110, funded by [Anonymous] and Blue Bolt.
2020-08-19 20:10:08 +02:00
nmlgc 52def53fd3 [Decompilation] [th01] Boss entities: Clamped movement + unblitting + blitting
A function which does so many individual things that I can't even
summarize it in a comment without spelling out each line of code. And
then, this entire clamped movement system is only used for Kikuri's
soul sprites. The usage code for every other entity just parties on
the [cur_left] and [cur_top] members, and passes a delta of (0, 0) to
this function. 🤷

Part of P0108, funded by Yanga.
2020-08-12 18:02:18 +02:00
nmlgc de688ddc20 [Decompilation] [th01] Boss entities: Byte-aligned unblitting + blitting
And we're back to not using functions, and needing our own VRAM access
macros…

Part of P0107, funded by Yanga.
2020-08-12 17:51:43 +02:00
nmlgc 816ba8342c [Decompilation] [th01] Background + foreground VRAM blit functions
Yes, functions! Those would have been nice earlier!
Also documented another overly specific Borland C++ code generation
quirk with regard to assigning expression results to elements of 16-bit
arrays…

Part of P0107, funded by Yanga.
2020-08-12 17:51:42 +02:00
nmlgc 72538610ba [Decompilation] [th01] .GRC: Load function
Still no idea what those 7 unknown bytes in the .GRC and .BOS headers
could be.

Part of P0105, funded by Yanga.
2020-08-12 16:19:39 +02:00
nmlgc edb70162e0 [Decompilation] [th01] Pellets: Manager class constructor
Note how Turbo C++ auto-generates that call to `operator new`, which
you don't see in the decompilation anymore. So yeah, as soon as you add
a constructor, Turbo C++ enforces heap allocation for any instance of
that class, even function-local ones that would otherwise be
stack-allocated.

That's where the bad reputation of C++ comes from, I guess?

Part of P0102, funded by Yanga.
2020-07-12 16:23:12 +02:00
nmlgc 43c97ccaa1 [Maintenance] Decide on __asm as the keyword for inline assembly
Which works in both Borland C++, Open Watcom, and Visual C++.

Not that we're about to port any of the games to these compilers, just
something I noticed while evaluating 32-bit compilers for ReC98's own
32-bit pipeline tools. Modders might want to look into that though,
since 100% position independence also makes it easier to change
compilers.
2020-06-21 22:18:00 +02:00
nmlgc 442a92d32b [Decompilation] [th01] Player shots: Spawn function
Continuing to learn new things about Turbo C++'s code generation!

Part of P0098, funded by Yanga.
2020-06-13 21:15:31 +02:00
nmlgc 51de73bcc9 [Decompilation] [th01] Orb physics
"Physics". Not only did ZUN restrict the X velocity to the 5 discrete
states of -8, -4, 0, 4, and 8 (because hey, unaligned blitting is slow
anyway?), but gravity is also only applied every 5 frames.

We're still missing quite a bit of usage code, but these are the core
functions. One of which turned out to be undecompilable, due to… a
rigorously defined instruction order when performing arithmetic between
`double`s and `float`s?! Still, spelling out all this stuff in ASM
seems much better than somehow splitting the data segment, just so that
we can immediately use literals there.

Part of P0097, funded by Ember2528.
2020-06-13 21:15:27 +02:00
nmlgc 026fff63a5 [Decompilation] [th01] Ending picture loading and display
Aww, how far we've come with inlining and helpful macros.

Part of P0094, funded by Yanga.
2020-05-25 15:18:44 +02:00
nmlgc 753e1b670a [Decompilation] Change graph_move_byterect_interpage() page parameters to int
Turbo C++ doesn't do the 32-bit PUSH LARGE optimization for 8-bit
parameters?

Part of P0094, funded by Yanga.
2020-05-25 15:15:41 +02:00
nmlgc 57be510056 [Decompilation] [th01] High score menu: Initial table rendering
With ternary operator expressions straight out of Jigoku.
(TL note: Jigoku means hell.)
Nice to see that Turbo C++ apparently has no nesting limit on function
call inlining in general, though!

Part of P0092, funded by Yanga.
2020-05-25 15:03:55 +02:00
nmlgc 07dab293ad [Decompilation] [th01] Input declarations shared between REIIDEN and FUUIN
Of which we can express precisely *nothing* as an inline function,
because Turbo C++ always emits a useless `JMP SHORT $+2` at the end of
such an inlined function if it contains nested `if` statements. This is
also what forced some of the functions in 90252cc to be expressed as
macros. By now, this is clear enough to be documented separately.

And to warrant this separate commit.

Completes P0090, funded by Yanga.
2020-05-12 15:05:49 +02:00
nmlgc 00050d0e5e [Decompilation] [th01] VRAM text typing
Yeah, that 8×16 text RAM grid is so restricting.

Part of P0082, funded by Ember2528.
2020-03-13 19:09:12 +01:00
nmlgc ebd214bbb2 [Decompilation] [th05] RES_KSO.COM
And of course, TH05 ruins the consistency once again. Sure, the added
file error handling is nice, but we also have changes in the playful
messages (lol), and now need a third distinct optimization barrier
(🤦)… But as it turns out, inlined calls to empty functions work as
well. They also seem closer to what ZUN might have actually written
there, given that their function body could have been removed by the
preprocessor, similar to the logging functions in the Windows Touhou
games. (With the difference that the latter infamously *aren't*
inlined…)

Part of P0077, funded by Splashman and -Tom-.
2020-02-23 17:53:17 +01:00
nmlgc b8ca607c38 [Decompilation] [th02] Get zun_res1.c right
4½ years after aa56a7c, it turns out that the correct decompilation
involves… no-ops generated by assigning variables that just happen to
be in registers to themselves?! Which does get optimized out, but only
after TCC folded identical tail code in all branches of a function,
thus effectively functioning as an optimization barrier.

The initial attempt used register pseudovariables, but this definitely
is the best possible way this could work – portable, and doesn't
unnecessarily shred the code into tiny inlined functions pieces. The
mindblowing thing here is that ZUN could have actually written this to
have additional, albeit unnecessary, lines to place breakpoints on. But
that means he must have chosen those two local variables in SI and DI
completely by chance… 🤯

The best thing though? ~#pragma inline is gone~

Part of P0076, funded by [Anonymous] and -Tom-.
2020-02-23 17:12:35 +01:00
nmlgc a1f36ffa04 [Reverse-engineering] [th03] Demo initialization
Something small that came out of the resident structure review.
Including more research to suggest that we probably can't ever use C++
constructors for anything in ReC98.

Part of P0076, funded by [Anonymous] and -Tom-.
2020-02-23 16:58:30 +01:00
nmlgc ff777a099e [Decompilation] [th03] Add a type for the paletted playchar IDs
Much better than the previous incorrect comment, since the char_id()
method now easily generates the correct sequence of conversion
instructions, seen all throughout the ASM. Even within array
subscripts!

Part of P0076, funded by [Anonymous] and -Tom-.
2020-02-23 16:56:14 +01:00
nmlgc a6d292a62c [Decompilation] [th01] graph_putsa_fx
TH01's (original) version also replicates the PC-98 text RAM's reverse
and underline attributes. Which was removed in later games,
interestingly and inconsistently enough.

Part of P0068, funded by Yanga.
2020-01-14 22:04:12 +01:00
nmlgc 9f7dde8953 [Decompilation] [th01] Inter-page rectangle moves
Semi-unused, that is, the one use of this function doesn't actually
move the rectangle to a different position. Ironically, the non-moving
back-to-front function immediately above *is* unused…

Also, too bad that stack order is the only reason we can't use structs
to combine all plane variables into a single object.

Part of P0067, funded by Splashman.
2020-01-14 21:50:23 +01:00
nmlgc e55a48b700 [Decompilation] [th01] master.lib resident palette function reimplementations
Which store colors as GRB, as suggested by the structure's ID string.
Even master.lib's own functions add an additional XCHG AH, AL
instruction to get colors into and out of this format. MASTER.MAN
suggests that it's some sort of standard on PC-98. It does match the
order of ths hardware's palette register ports, after all.
(0AAh = green, 0ACh = red, 0AEh = blue)

Now we also know why __seg* wasn't used more commonly, as lamented in
c8e8e98. Turbo C++ simply doesn't support a lot of arithmetic on
segment pointers.

And then that undecompilable far call to a function within the same
segment, but inside a different translation unit…
Also, thanks again to Egor for the SCOPY@ hack that debuted in 0460072.
Would have probably struggled with this a lot more without that.

And *then* you realize that TH01 effectively doesn't even use the
resident palette. 😐

And yes, we're procrastinating the whole issue of potentially using
a single translation unit for all three binaries by using a common
segment name, because it *really* isn't that easy.

Completes P0066, funded by Keyblade Wiedling Neko and Splashman.
2020-01-05 20:23:27 +01:00
nmlgc 5f4f5d87dc [Decompilation] [th03] Shot update and render functions
Meh, can't overload arithmetic operators that take a Subpixel without
generating a needless load and store, even with -Z. But heck, slightly
uglifying subpixel/subpixel arithmetic is exactly the right trade-off.

Completes P0061, funded by Touhou Patch Center.
2019-12-05 21:41:31 +01:00
nmlgc da27eb4c31 [Decompilation] [th05] Yuuka's shot control functions
Ooh, shot position being determined by RNG at lower shot levels? That's
some RNG manipulation TAS potential right there! Maybe.

Part of P0037, funded by zorg.
2019-10-14 23:54:29 +02:00
nmlgc db4de240e9 [Decompilation] Prepare the C side for the shot type control functions
That should make this convoluted copypasta a bit easier to read. And
sure, I could have done something about the loop as well, but
SHOT_FUNC_INIT already hides enough control flow behind a macro…

Part of P0037, funded by zorg.
2019-10-14 23:42:20 +02:00
nmlgc 82b0e1db24 [Reverse-engineering] [th05] Player shot cycle bitflags
A TH05 innovation that actually makes the game code easier to read?!
Although it was quite hard to actually reverse-engineer it, with the
confusing flag ordering pointing to some deeper meaning behind the
flags, which really doesn't exist.

Completes P0036, funded by zorg.
2019-10-14 23:42:09 +02:00
nmlgc a533b5d3ea [Reverse-engineering] [th04/th05] Player sprite area invalidation
And once again, the TH05 version is un-decompilable. :/ It was pretty
close this time, though, as the entire block between PUSH DI and POP DI
kind of resembles a separate inlined function, in accordance with Turbo
C++'s  automatic backup of the DI register, as researched in 7f971a0.

Except that it contains a loop, and Turbo C++ refuses to inline any
function with `do`, `while`, `for`, or `goto`. If it didn't, it would
have totally worked.

Also, yes, C++ class methods are treated identically in this regard.

Oh well. Shot type control functions next, finally!

Completes P0035, funded by zorg.
2019-09-24 22:04:26 +02:00
nmlgc 7f971a0d1c [Research] Find out why we can't decompile TH05's hud_bar_put()
Part of P0033, funded by zorg.
2019-09-21 14:01:51 +02:00
nmlgc 8dfc2cd535 [Research] Find out why ≥TH03 checks input twice per frame, with a 0.6ms delay
Thanks to @joncampbell123 for the tip!

Funded by zorg.
2018-09-13 18:32:24 +02:00