Skip to content

Commit

Permalink
[Separate translation units] [th04/th05] .CDG: Loading and freeing
Browse files Browse the repository at this point in the history
Undecompilable again. The loading functions have these *_noalpha()
variants that simply set a global variable and fall through to the
regular functions, while cdg_free() has its first `PUSH DI` instruction
after the first expression we'd be decompiling. cdg_free_all() *could*
be decompiled… but would also require _FLAGS trickery, and it's simply
not worth starting a translation unit for one such small function.

Part of P0127, funded by [Anonymous].
  • Loading branch information
nmlgc committed Nov 16, 2020
1 parent 00f177e commit 7897bf1
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 67 deletions.
12 changes: 6 additions & 6 deletions Makefile.mak
Expand Up @@ -122,17 +122,17 @@ bin\th04\res_huma.com: th04\res_huma.cpp
$**
| masters.lib

bin\th04\op.exe: bin\th04\op.obj th04\m_char.cpp bin\frmdely2.obj
bin\th04\op.exe: bin\th04\op.obj th04\m_char.cpp bin\th04\cdg_load.obj th04\frmdely2.c
$(CC) $(CFLAGS) -ml -DGAME=4 -DBINARY='O' -3 -Z -d -nbin\th04\ -eOP.EXE @&&|
$**
|

bin\th04\main.exe: bin\th04\main.obj bin\th04\scoreupd.obj th04\main011.cpp bin\hfliplut.obj th04\main032.cpp
bin\th04\main.exe: bin\th04\main.obj bin\th04\scoreupd.obj th04\main011.cpp bin\th04\cdg_load.obj bin\hfliplut.obj th04\main032.cpp
$(CC) $(CFLAGS) -ml -DGAME=4 -DBINARY='M' -3 -Z -nbin\th04\ -eMAIN.EXE @&&|
$**
|

bin\th04\maine.exe: bin\th04\maine.obj th04\maine011.cpp bin\hfliplut.obj
bin\th04\maine.exe: bin\th04\maine.obj th04\maine011.cpp bin\th04\cdg_load.obj bin\hfliplut.obj
$(CC) $(CFLAGS) -ml -DGAME=4 -DBINARY='E' -Z -nbin\th04\ -eMAINE.EXE @&&|
$**
|
Expand All @@ -145,17 +145,17 @@ 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\egcrect.obj bin\hfliplut.obj
bin\th05\op.exe: th05\op010.cpp bin\th05\op.obj th05\op011.cpp th05\m_char.cpp 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 @&&|
$**
|

bin\th05\main.exe: bin\th05\main.obj th05\main010.cpp th05\main011.cpp th05\p_common.cpp th05\p_reimu.cpp th05\p_marisa.cpp th05\p_mima.cpp th05\p_yuuka.cpp bin\th05\player.obj bin\th05\hud_bar.obj bin\th05\scoreupd.obj th05\main012.cpp th05\main013.cpp bin\hfliplut.obj bin\th05\bullet.obj th05\main031.cpp th05\main032.cpp th05\main033.cpp th05\main034.cpp
bin\th05\main.exe: bin\th05\main.obj th05\main010.cpp th05\main011.cpp th05\p_common.cpp th05\p_reimu.cpp th05\p_marisa.cpp th05\p_mima.cpp th05\p_yuuka.cpp bin\th05\player.obj bin\th05\hud_bar.obj bin\th05\scoreupd.obj th05\main012.cpp th05\main013.cpp bin\hfliplut.obj bin\th05\bullet.obj bin\th04\cdg_load.obj th05\main031.cpp th05\main032.cpp th05\main033.cpp th05\main034.cpp
$(CC) $(CFLAGS) -ml -3 -Z -DGAME=5 -DBINARY='M' -nbin\th05\ -eMAIN.EXE @&&|
$**
|

bin\th05\maine.exe: bin\th05\maine.obj th05\maine011.cpp th05\regist.cpp th05\staff.cpp bin\th05\egcrect.obj bin\hfliplut.obj
bin\th05\maine.exe: bin\th05\maine.obj th05\maine011.cpp th05\regist.cpp th05\staff.cpp bin\th04\cdg_load.obj bin\th05\egcrect.obj bin\hfliplut.obj
$(CC) $(CFLAGS) -ml -DGAME=5 -DBINARY='E' -Z -nbin\th05\ -eMAINE.EXE @&&|
$**
|
Expand Down
1 change: 1 addition & 0 deletions Tupfile
Expand Up @@ -86,6 +86,7 @@ BMP2ARR = bin\\Pipeline\\bmp2arr.exe
: th04_zuninit.asm |> !as |> bin\\th04\\zuninit.obj
: th04_memchk.asm |> !as |> bin\\th04\\memchk.obj
: th04\\scoreupd.asm |> !as4 |>
: th04\\cdg_load.asm |> !as4 |>
: th04_op.asm |> !as |> bin\\th04\\op.obj
: th04_main.asm | \
th02/sprites/pellet.asp \
Expand Down
1 change: 1 addition & 0 deletions Tupfile.bat
Expand Up @@ -36,6 +36,7 @@ tasm32 /m /mx /kh32768 /t th03_mainl.asm bin\th03\mainl.obj
tasm32 /m /mx /kh32768 /t th04_zuninit.asm bin\th04\zuninit.obj
tasm32 /m /mx /kh32768 /t th04_memchk.asm bin\th04\memchk.obj
tasm32 /m /mx /kh32768 /t /dGAME=4 th04\scoreupd.asm bin\th04\scoreupd.obj
tasm32 /m /mx /kh32768 /t /dGAME=4 th04\cdg_load.asm bin\th04\cdg_load.obj
tasm32 /m /mx /kh32768 /t th04_op.asm bin\th04\op.obj
tasm32 /m /mx /kh32768 /t th04_main.asm bin\th04\main.obj
tasm32 /m /mx /kh32768 /t th04_maine.asm bin\th04\maine.obj
Expand Down
1 change: 1 addition & 0 deletions th03/formats/cdg[data].asm
@@ -1,3 +1,4 @@
; Won't load the alpha plane of the next CDG file if nonzero.
public cdg_noalpha
cdg_noalpha db ?
align 2
1 change: 1 addition & 0 deletions th04/cdg_load.asm
@@ -0,0 +1 @@
include th04/formats/cdg_load.asm
1 change: 1 addition & 0 deletions th04/formats/cdg[bss].asm
@@ -1,3 +1,4 @@
include th03/formats/cdg[bss].asm
public cdg_images_to_load
cdg_images_to_load db ?
evendata
111 changes: 60 additions & 51 deletions th04/formats/cdg_load.asm
@@ -1,7 +1,30 @@
.386
.model large SHARED_
locals

include th03/formats/cdg.inc

extrn FILE_ROPEN:proc
extrn FILE_READ:proc
extrn FILE_SEEK:proc
extrn FILE_CLOSE:proc
extrn HMEM_ALLOCBYTE:proc
extrn HMEM_FREE:proc
extrn _cdg_slots:cdg_t:CDG_SLOT_COUNT
extrn cdg_noalpha:byte
extrn cdg_images_to_load:byte

g_SHARED group SHARED, SHARED_
SHARED segment word public 'CODE' use16
SHARED ends

SHARED_ segment word public 'CODE' use16
assume cs:g_SHARED

public CDG_LOAD_SINGLE_NOALPHA
public CDG_LOAD_SINGLE

cdg_load_single_noalpha label proc
cdg_load_single_noalpha label proc
mov cdg_noalpha, 1
align 2

Expand All @@ -17,15 +40,12 @@ cdg_load_single proc far
push di
mov di, [bp+@@slot]
push di
nopcall cdg_free
shl di, 4
nop ; This was definitely compiled from C...
call cdg_free
shl di, 4 ; *= size cdg_t
add di, offset _cdg_slots
pushd [bp+@@fn]
call file_ropen
push ds
push di
push size cdg_t
call file_read
call file_ropen pascal, large [bp+@@fn]
call file_read pascal, ds, di, size cdg_t
mov ax, [di+cdg_t.CDG_plane_size]
mov dx, ax
cmp [di+cdg_t.plane_layout], CDG_COLORS
Expand All @@ -38,22 +58,20 @@ cdg_load_single proc far
@@read:
mul [bp+@@n]
movzx eax, ax
push eax
push 1
call file_seek
call file_seek pascal, eax, 1
call cdg_read_single
call file_close
mov cdg_noalpha, 0
pop di
pop si
pop bp
retf 8
cdg_load_single endp
cdg_load_single endp
align 2

; Reads a single CDG image from the master.lib file, which previously has been
; positioned at the beginning of the image data, into the slot in DI.
cdg_read_single proc near
cdg_read_single proc near
mov al, [di+cdg_t.plane_layout]
or al, al ; AL == CDG_COLORS?
jz short @@colors
Expand All @@ -63,28 +81,21 @@ cdg_read_single proc near
jnz short @@skip_alpha

@@alpha:
push [di+cdg_t.CDG_plane_size]
call hmem_allocbyte
call hmem_allocbyte pascal, [di+cdg_t.CDG_plane_size]
mov [di+cdg_t.seg_alpha], ax
push ax
push 0
push [di+cdg_t.CDG_plane_size]
call file_read
call file_read pascal, ax, 0, [di+cdg_t.CDG_plane_size]
jmp short @@colors

@@skip_alpha:
movzx eax, [di+cdg_t.CDG_plane_size]
push eax
push 1
call file_seek
call file_seek pascal, eax, 1

@@colors:
cmp [di+cdg_t.plane_layout], CDG_ALPHA
jz short @@ret
mov ax, [di+cdg_t.CDG_plane_size]
shl ax, 2
push ax
call hmem_allocbyte
call hmem_allocbyte pascal, ax
mov [di+cdg_t.seg_colors], ax
push ax
push 0
Expand All @@ -95,17 +106,17 @@ cdg_read_single proc near

@@ret:
retn
cdg_read_single endp
cdg_read_single endp


public CDG_LOAD_ALL_NOALPHA
public CDG_LOAD_ALL

cdg_load_all_noalpha label proc
cdg_load_all_noalpha label proc
mov cdg_noalpha, 1
align 2

cdg_load_all proc far
cdg_load_all proc far

@@fn = dword ptr 6
@@slot_first = word ptr 10
Expand All @@ -114,27 +125,21 @@ cdg_load_all proc far
mov bp, sp
push si
push di
pushd [bp+@@fn]
call file_ropen
call file_ropen pascal, large [bp+@@fn]
mov di, [bp+@@slot_first]
shl di, 4
shl di, 4 ; *= size cdg_t
add di, offset _cdg_slots
push ds
push di
push size cdg_t
call file_read
call file_read pascal, ds, di, size cdg_t
mov si, di
mov bp, [bp+@@slot_first]
mov al, cdg_t.image_count[si]
mov cdg_images_to_load, al
push ds
pop es
assume es:_DATA

@@loop:
push bp
call cdg_free
mov cx, 3
call cdg_free pascal, bp
mov cx, (cdg_t.seg_alpha / dword)
rep movsd
sub si, cdg_t.seg_alpha
sub di, cdg_t.seg_alpha
Expand All @@ -149,39 +154,40 @@ cdg_load_all proc far
pop si
pop bp
retf 6
cdg_load_all endp
cdg_load_all endp


public CDG_FREE
cdg_free proc far
cdg_free proc far

@@slot = word ptr 4

mov bx, sp
push di
mov di, ss:[bx+4]
shl di, 4
mov di, ss:[bx+@@slot]
shl di, 4 ; *= size cdg_t
add di, offset _cdg_slots.seg_alpha
cmp word ptr [di], 0
jz short @@colors
push word ptr [di]
call hmem_free
call hmem_free pascal, word ptr [di]
mov word ptr [di], 0

@@colors:
add di, 2
add di, word ; = seg_colors
cmp word ptr [di], 0
jz short @@ret
push word ptr [di]
call hmem_free
call hmem_free pascal, word ptr [di]
mov word ptr [di], 0

@@ret:
pop di
retf 2
cdg_free endp
cdg_free endp
align 2


public CDG_FREE_ALL
cdg_free_all proc far
cdg_free_all proc far
push si
mov si, CDG_SLOT_COUNT - 1

Expand All @@ -191,4 +197,7 @@ cdg_free_all proc far
jge short @@loop
pop si
retf
cdg_free_all endp
cdg_free_all endp
SHARED_ ends

end
12 changes: 12 additions & 0 deletions th04/frmdely2.c
@@ -0,0 +1,12 @@
// Temporary workaround copy of TH02's version, until we can put it into the
// regular SHARED segment for TH04's OP.EXE as well.
#pragma codeseg SHARED_

#include "master.hpp"
#include "th02/hardware/frmdelay.h"

void pascal far frame_delay_2(int frames)
{
vsync_reset1();
while(vsync_Count1 < frames) {}
}
5 changes: 4 additions & 1 deletion th04_main.asm
Expand Up @@ -13135,7 +13135,10 @@ include th04/formats/cdg_put_noalpha.asm
include th04/formats/cdg_put_plane_roll.asm
include th04/hardware/input_sense.asm
include th04/snd/se.asm
include th04/formats/cdg_load.asm
extern CDG_LOAD_SINGLE_NOALPHA:proc
extern CDG_LOAD_ALL:proc
extern CDG_FREE:proc
extern CDG_FREE_ALL:proc
SHARED_ ends

; ===========================================================================
Expand Down
5 changes: 4 additions & 1 deletion th04_maine.asm
Expand Up @@ -4617,7 +4617,10 @@ include th04/hardware/input_sense.asm
include th04/snd/se.asm
include th04/bgimage.asm
include th04/bgimage_put_rect.asm
include th04/formats/cdg_load.asm
extern CDG_LOAD_SINGLE_NOALPHA:proc
extern CDG_LOAD_SINGLE:proc
extern CDG_FREE:proc
extern CDG_FREE_ALL:proc
SHARED_ ends

.data
Expand Down
11 changes: 9 additions & 2 deletions th04_op.asm
Expand Up @@ -31,6 +31,8 @@ include th04/music/music.inc
extern _memcpy:proc
extern _strlen:proc

g_SHARED group SHARED, SHARED_

; ===========================================================================

; Segment type: Pure code
Expand Down Expand Up @@ -2667,10 +2669,15 @@ include th04/snd/se.asm
include th04/hardware/egcrect.asm
include th04/bgimage.asm
include th04/bgimage_put_rect.asm
include th04/formats/cdg_load.asm
extern FRAME_DELAY_2:proc
SHARED ends

SHARED_ segment word public 'CODE' use16
extern CDG_LOAD_ALL_NOALPHA:proc
extern CDG_LOAD_ALL:proc
extern CDG_FREE_ALL:proc
extern FRAME_DELAY_2:proc
SHARED_ ends

.data

include th03/formats/cfg[data].asm
Expand Down
8 changes: 6 additions & 2 deletions th05_main.asm
Expand Up @@ -10568,8 +10568,12 @@ include th04/hardware/input_sense.asm
include th05/hardware/input_held.asm
include th05/hardware/input_wait.asm
include th05/hardware/frame_delay.asm
db 0
include th04/formats/cdg_load.asm
extern CDG_LOAD_ALL_NOALPHA:proc
extern CDG_LOAD_ALL:proc
extern CDG_LOAD_SINGLE_NOALPHA:proc
extern CDG_LOAD_SINGLE:proc
extern CDG_FREE:proc
extern CDG_FREE_ALL:proc
SHARED_ ends

; ===========================================================================
Expand Down
4 changes: 2 additions & 2 deletions th05_maine.asm
Expand Up @@ -7552,8 +7552,8 @@ include th05/hardware/input_wait.asm
include th05/snd/measure.asm
include th05/snd/delaymea.asm
include th05/hardware/frame_delay.asm
db 0
include th04/formats/cdg_load.asm
extern CDG_LOAD_ALL_NOALPHA:proc
extern CDG_FREE_ALL:proc
extern EGC_COPY_RECT_1_TO_0_16:proc
SHARED_ ends

Expand Down
6 changes: 4 additions & 2 deletions th05_op.asm
Expand Up @@ -2560,8 +2560,10 @@ include th05/snd/measure.asm
include th05/snd/delaymea.asm
include th04/formats/cdg_put_nocolors.asm
include th05/hardware/frame_delay.asm
db 0
include th04/formats/cdg_load.asm
extern CDG_LOAD_SINGLE_NOALPHA:proc
extern CDG_LOAD_SINGLE:proc
extern CDG_LOAD_ALL:proc
extern CDG_FREE_ALL:proc
extern EGC_COPY_RECT_1_TO_0_16:proc
SHARED_ ends

Expand Down

0 comments on commit 7897bf1

Please sign in to comment.