From f6a32460714820bdd04876d481cc23fb13889abe Mon Sep 17 00:00:00 2001 From: nmlgc Date: Sun, 1 Nov 2020 20:09:18 +0100 Subject: [PATCH] [Reverse-engineering] Enabling and disabling the EGC Which involves temporarily enabling a GDC mode change bit. Completes P0125, funded by [Anonymous]. --- Makefile.mak | 2 +- decomp.h | 6 ++++++ libs/sprite16/sprite16.asm | 21 +++------------------ th01/hardware/egc.h | 22 ++++++++++++++++++++++ th01/hardware/egc.inc | 25 +++++++++++++++++++------ th01/zunsoft.c | 20 +++++++++++--------- th02_main.asm | 24 ++++++------------------ th04/hardware/egccopyr.asm | 7 +------ 8 files changed, 69 insertions(+), 58 deletions(-) diff --git a/Makefile.mak b/Makefile.mak index 971d5409..6abc2af8 100644 --- a/Makefile.mak +++ b/Makefile.mak @@ -32,7 +32,7 @@ bin\Pipeline\grzview.com: Pipeline\grzview.cpp th01\formats\grz.cpp $(CC) $(CFLAGS) -Z -mt -lt -nbin\Pipeline\ $** masters.lib bin\th01\zunsoft.com: th01\zunsoft.c - $(CC) $(CFLAGS) -mt -lt -nbin\th01\ $** masters.lib + $(CC) $(CFLAGS) -mt -lt -DGAME=1 -nbin\th01\ $** masters.lib # Segment 2 (game-independent code) # --------------------------------- diff --git a/decomp.h b/decomp.h index c99f0eac..c57ba361 100644 --- a/decomp.h +++ b/decomp.h @@ -12,6 +12,12 @@ #define FLAGS_ZERO (_FLAGS & 0x40) /* JZ */ // ---------------- +// Alternate version that doesn't spill the port number to DX +#define outportb2(port, val) __asm { \ + mov al, val; \ + out port, al; \ +} + // Alternate version that sets the value first #define OUTW2(port, val) __asm { \ mov ax, val; \ diff --git a/libs/sprite16/sprite16.asm b/libs/sprite16/sprite16.asm index 62f935f0..8a52d384 100644 --- a/libs/sprite16/sprite16.asm +++ b/libs/sprite16/sprite16.asm @@ -21,6 +21,7 @@ ; And yes, we can't move this to an include file for some reason. include libs/master.lib/macros.inc +include th01/hardware/egc.inc ; =========================================================================== @@ -1928,15 +1929,7 @@ sub_BD0 proc near push ax push dx cli - mov al, 7 - out 6Ah, al ; PC-98 GDC (6a): - ; - mov al, 5 - out 6Ah, al ; PC-98 GDC (6a): - ; - mov al, 6 - out 6Ah, al ; PC-98 GDC (6a): - ; + graph_egc 1 GRCG_SETMODE_VIA_MOV al, GC_TDW outw EGC_ACTIVEPLANEREG, 0FFF0h outw EGC_READPLANEREG, 0FFh @@ -1963,15 +1956,7 @@ sub_C10 proc near outw EGC_ACTIVEPLANEREG, 0FFF0h outw EGC_READPLANEREG, 0FFh outw EGC_MASKREG, 0FFFFh - mov al, 7 - out 6Ah, al ; PC-98 GDC (6a): - ; - mov al, 4 - out 6Ah, al ; PC-98 GDC (6a): - ; - mov al, 6 - out 6Ah, al ; PC-98 GDC (6a): - ; + graph_egc 0 GRCG_OFF_VIA_XOR al sti pop dx diff --git a/th01/hardware/egc.h b/th01/hardware/egc.h index e459bfc9..ce14a0e7 100644 --- a/th01/hardware/egc.h +++ b/th01/hardware/egc.h @@ -1,6 +1,28 @@ #include "defconv.h" #include "decomp.h" +/// Enabling and disabling +/// ---------------------- + +#define graph_mode_change(enable_or_disable) \ + outportb2(0x6A, (0x06 + enable_or_disable)) + +// Requires graphics mode changing to be enabled via graph_mode_change(true). +#define graph_mode_egc(enable_or_disable) \ + outportb2(0x6A, (0x04 + enable_or_disable)) + +#define graph_egc(enable_or_disable) \ + graph_mode_change(true); \ + graph_mode_egc(enable_or_disable); \ + graph_mode_change(false); + +#define graph_egc_on() \ + graph_egc(true); + +#define graph_egc_off() \ + graph_egc(false); +/// ---------------------- + // Requires the EGC to have been activated before. #define egc_setup_copy() \ OUTW2(EGC_ACTIVEPLANEREG, 0xFFF0); \ diff --git a/th01/hardware/egc.inc b/th01/hardware/egc.inc index dd40ef4d..503a5ca7 100644 --- a/th01/hardware/egc.inc +++ b/th01/hardware/egc.inc @@ -1,6 +1,22 @@ ; ReC98 ; Inlined EGC code +graph_mode_change macro enable_or_disable:req + mov al, (6 + enable_or_disable) + out 6Ah, al +endm + +graph_mode_egc macro enable_or_disable:req + mov al, (4 + enable_or_disable) + out 6Ah, al +endm + +graph_egc macro enable_or_disable:req + graph_mode_change 1 + graph_mode_egc enable_or_disable + graph_mode_change 0 +endm + EGC_SETUP_COPY macro outw2 EGC_ACTIVEPLANEREG, 0FFF0h egc_selectpat @@ -26,12 +42,9 @@ endm EGC_START_COPY_INLINED macro GRCG_OFF_VIA_MOV al - mov al, 7 - out 6Ah, al - mov al, 5 - out 6Ah, al + graph_mode_change 1 + graph_mode_egc 1 GRCG_SETMODE_VIA_MOV al, GC_TDW - mov al, 6 - out 6Ah, al + graph_mode_change 0 EGC_SETUP_COPY endm diff --git a/th01/zunsoft.c b/th01/zunsoft.c index ce7a5233..4fdfd7a1 100644 --- a/th01/zunsoft.c +++ b/th01/zunsoft.c @@ -6,6 +6,7 @@ #include #include "platform.h" #include "pc98.h" +#include "th01/hardware/egc.h" #define CIRCLE_COUNT 4 #define STAR_COUNT 50 @@ -209,16 +210,17 @@ void main(void) mov es, ax test byte ptr es:0x45C, 0x40 jz hw_setup_done - mov al, 0x7 - out 0x6a, al - mov al, 0x20 - out 0x6a, al - mov al, 0x6 - out 0x6a, al - // Activate all graphics hardware in 16-color mode - and byte ptr es:0x54d, 0x7f -hw_setup_done: } + graph_mode_change(true); __asm { + mov al, 0x20 + out 0x6a, al + } + graph_mode_change(false); + + // Activate all graphics hardware in 16-color mode + __asm { and byte ptr es:0x54d, 0x7f } + +hw_setup_done: zunsoft_init(); objects_setup(); while(1) { diff --git a/th02_main.asm b/th02_main.asm index 906a733b..8f4fc71d 100644 --- a/th02_main.asm +++ b/th02_main.asm @@ -2741,16 +2741,10 @@ loc_BD26: call farfp_1F490 outw2 EGC_ACTIVEPLANEREG, 0FFF0h outw2 EGC_MASKREG, 0FFFFh - mov al, 7 - out 6Ah, al ; PC-98 GDC (6a): - ; - mov al, 4 - out 6Ah, al ; PC-98 GDC (6a): - ; + graph_mode_change 1 + graph_mode_egc 0 GRCG_OFF_VIA_MOV al - mov al, 6 - out 6Ah, al ; PC-98 GDC (6a): - ; + graph_mode_change 0 mov ax, word_2034A cmp ax, word_20616 jnz short loc_BD62 @@ -2843,16 +2837,10 @@ loc_BE3B: mov byte_1E501, al outw2 EGC_ACTIVEPLANEREG, 0FFF0h outw2 EGC_MASKREG, 0FFFFh - mov al, 7 - out 6Ah, al ; PC-98 GDC (6a): - ; - mov al, 4 - out 6Ah, al ; PC-98 GDC (6a): - ; + graph_mode_change 1 + graph_mode_egc 0 GRCG_OFF_VIA_MOV al - mov al, 6 - out 6Ah, al ; PC-98 GDC (6a): - ; + graph_mode_change 0 cmp byte_1E501, 0 jz short loc_BE77 inc _scroll_line diff --git a/th04/hardware/egccopyr.asm b/th04/hardware/egccopyr.asm index 6a260177..c2503c16 100644 --- a/th04/hardware/egccopyr.asm +++ b/th04/hardware/egccopyr.asm @@ -94,12 +94,7 @@ egc_start_copy proc near popf pop es assume es:nothing - mov al, 7 - out 6Ah, al - mov al, 5 - out 6Ah, al - mov al, 6 - out 6Ah, al + graph_egc 1 outw EGC_ACTIVEPLANEREG, 0FFF0h outw EGC_READPLANEREG, 0FFh outw EGC_MASKREG, 0FFFFh