Skip to content

Commit

Permalink
[Decompilation] [th05] .CDG: Non-color blitting
Browse files Browse the repository at this point in the history
And this is how you make code less undecompilable by improving your
pointless micro-optimizations to use more registers instead of
self-modifying code. Worth it if only to get rid of the branches in
TH04's undecompilable ASM implementation.

Part of P0133, funded by [Anonymous].
  • Loading branch information
nmlgc committed Jan 30, 2021
1 parent 045450c commit c8160b9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile.mak
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bin\th05\res_kso.com: th05\res_kso.cpp
$**
| masters.lib

bin\th05\op.exe: th05\op010.cpp bin\th05\op.obj th05\op011.cpp th05\m_char.cpp bin\th05\frmdelay.obj bin\th04\cdg_load.obj bin\th05\egcrect.obj bin\hfliplut.obj
bin\th05\op.exe: th05\op010.cpp bin\th05\op.obj th05\op011.cpp th05\m_char.cpp th05\cdg_p_nc.cpp bin\th05\frmdelay.obj bin\th04\cdg_load.obj bin\th05\egcrect.obj bin\hfliplut.obj
$(CC) $(CFLAGS) -ml -DGAME=5 -DBINARY='O' -3 -Z -nbin\th05\ -eOP.EXE @&&|
$**
|
Expand Down
18 changes: 2 additions & 16 deletions th04/formats/cdg_put_nocolors.asm
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; Identical to the barely decompilable TH05 version, which doesn't rely on
; self-modifying code for the width.
public CDG_PUT_NOCOLORS_8
cdg_put_nocolors_8 proc far

Expand All @@ -16,41 +18,25 @@ cdg_put_nocolors_8 proc far
sar di, 3
add di, [si+cdg_t.offset_at_bottom_left]
mov ax, [si+cdg_t.vram_dword_w]
if GAME eq 4
mov word ptr cs:@@width+1, ax
jmp short $+2
else
mov bx, ax
endif
shl ax, 2
add ax, (640 / 8)
mov dx, ax
mov ax, [bp+@@top]
if GAME eq 4
mov bx, ax
shl ax, 2
add ax, bx
else
mov cx, ax
shl ax, 2
add ax, cx
endif
add ax, 0A800h
mov es, ax
push ds
mov ds, [si+cdg_t.seg_alpha]
xor si, si
if GAME eq 4
cld
endif
align 2

@@width:
if GAME eq 4
mov cx, 1234h
else
mov cx, bx
endif
rep movsd
sub di, dx
jns short @@width
Expand Down
1 change: 1 addition & 0 deletions th05/cdg_p_nc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "th05/formats/cdg_p_nc.cpp"
61 changes: 61 additions & 0 deletions th05/formats/cdg_p_nc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma codeseg SHARED_

extern "C" {
#include <stddef.h>
#include <dos.h>
#include "platform.h"
#include "pc98.h"
#include "planar.h"
#include "decomp.h"
#include "th04/formats/cdg.h"
}

void pascal cdg_put_nocolors_8(screen_x_t left, vram_y_t top, int slot)
{
#define _DI static_cast<vram_offset_t>(_DI)
#define cdg reinterpret_cast<cdg_t near *>(_SI)
#define stride static_cast<vram_byte_amount_t>(_DX)
#define tmp static_cast<upixel_t>(_AX)
#define top_tmp static_cast<pixel_t>(_CX)
register vram_byte_amount_t cdg_dword_w;

// cdg = cdg_slots[slot];
_SI = slot;
_SI <<= 4; // MODDERS: Should be sizeof(cdg_t);
_SI += FP_OFF(cdg_slots);

// _DI = (vram_offset_shift(left, 0) + cdg->offset_at_bottom_left);
// [top] is calculated into the segment later, not the offset.
_DI = left;
_DI >>= 3;
_DI += cdg->offset_at_bottom_left;

tmp = cdg->vram_dword_w;
cdg_dword_w = tmp;
// One additional full line, since we move from the end of one CDG line to
// the beginning of the previous one.
stride = ((tmp * 4) + ROW_SIZE);

// _ES = (SEG_PLANE_B + (top * (ROW_SIZE / 16)));
tmp = top;
top_tmp = tmp;
_ES = (((tmp * 4) + top_tmp) + SEG_PLANE_B);

__asm { push ds };
_DS = reinterpret_cast<seg_t>(cdg->seg_alpha());
_SI = 0;

do {
_CX = cdg_dword_w;
REP MOVSD;
_DI -= stride;
} while(!FLAGS_SIGN);

__asm { pop ds };

#undef top_tmp
#undef tmp
#undef stride
#undef cdg
#undef _DI
}
2 changes: 1 addition & 1 deletion th05_op.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,7 @@ include th05/hardware/input_held.asm
include th05/hardware/input_wait.asm
include th05/snd/measure.asm
include th05/snd/delaymea.asm
include th04/formats/cdg_put_nocolors.asm
extern CDG_PUT_NOCOLORS_8:proc
extern FRAME_DELAY:proc
extern CDG_LOAD_SINGLE_NOALPHA:proc
extern CDG_LOAD_SINGLE:proc
Expand Down

0 comments on commit c8160b9

Please sign in to comment.