Skip to content

Commit

Permalink
[Decompilation] KAJA driver ISR magic check
Browse files Browse the repository at this point in the history
Down to one inline ASM instruction for these.

Part of P0139, funded by [Anonymous].
  • Loading branch information
nmlgc committed May 11, 2021
1 parent 8e16481 commit acd2985
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
13 changes: 13 additions & 0 deletions libs/kaja/kaja.h
Expand Up @@ -23,6 +23,19 @@ typedef enum {
#define PMD 0x60
#define MMD 0x61

// Memory structure of a KAJA interrupt service routine, pointed to by its
// address stored in the x86 interrupt vector. Used for identifying whether
// PMD or MMD are installed at a certain IRQ#, by checking [magic].
typedef struct {
uint8_t jmp[2]; // JMP to the code
uint8_t magic[3]; // "PMD" or "MMD"
} kaja_isr_t;

#define kaja_isr_magic_matches(isr, magic1, magic2, magic3) \
(((kaja_isr_t *)(isr))->magic[0] == magic1) && \
(((kaja_isr_t *)(isr))->magic[1] == magic2) && \
(((kaja_isr_t *)(isr))->magic[2] == magic3)

// MMDFUNC.DOC and https://gist.github.com/devinacker/bdc58cfdba6a1ee80449
// both imply that this is a hardcoded property of the MMD format
#define MMD_TICKS_PER_QUARTER_NOTE 48
Expand Down
2 changes: 1 addition & 1 deletion th02/op_01.cpp
Expand Up @@ -5,11 +5,11 @@

extern "C" {
#include <process.h>
#include "libs/kaja/kaja.h"
#include "th02/th02.h"
#include "x86real.h"
#include "th02/resident.hpp"
#include "master.hpp"
#include "libs/kaja/kaja.h"
#include "th02/hardware/frmdelay.h"
#include "th02/hardware/grp_rect.h"
#include "th02/hardware/input.hpp"
Expand Down
2 changes: 1 addition & 1 deletion th02/op_06.cpp
Expand Up @@ -4,10 +4,10 @@
*/

extern "C" {
#include "libs/kaja/kaja.h"
#include "th02/th02.h"
#include "x86real.h"
#include "master.hpp"
#include "libs/kaja/kaja.h"
#include "th01/math/polar.hpp"
#include "th02/math/vector.hpp"
#include "th02/hardware/frmdelay.h"
Expand Down
26 changes: 11 additions & 15 deletions th02/snd/mmd_res.c
@@ -1,27 +1,23 @@
#pragma option -WX -zCSHARED -k-

#include "platform.h"
#include "x86real.h"
#include "libs/kaja/kaja.h"
#include "th02/snd/snd.h"

bool16 snd_mmd_resident(void)
{
_ES = 0;
__asm les bx, dword ptr es:[MMD * 4];
__asm cmp byte ptr es:[bx+2], 'M';
__asm jnz nope;
__asm cmp byte ptr es:[bx+3], 'M';
__asm jnz nope;
__asm cmp byte ptr es:[bx+4], 'D';
__asm jnz nope;
snd_interrupt_if_midi = MMD;
snd_midi_active = true;
snd_midi_possible = true;
// Enforced by the -WX code generation. Just replace these two lines with
// `return true`.
_AX = true;
__asm retf;
nope:
__asm { les bx, dword ptr es:[MMD * 4]; }
if(kaja_isr_magic_matches(MK_FP(_ES, _BX), 'M', 'M', 'D')) {
snd_interrupt_if_midi = MMD;
snd_midi_active = true;
snd_midi_possible = true;
// Enforced by the -WX code generation. Just replace these two lines
// with `return true`.
_AX = true;
__asm retf;
}
snd_midi_possible = false;
return false;
}
Expand Down
15 changes: 5 additions & 10 deletions th02/snd/pmd_res.c
@@ -1,6 +1,7 @@
#pragma option -zCSHARED -k-

#include "platform.h"
#include "x86real.h"
#include "libs/kaja/kaja.h"
#include "th02/snd/snd.h"

Expand All @@ -12,16 +13,10 @@ bool16 snd_pmd_resident(void)
snd_midi_possible = false;

_ES = 0;
__asm les bx, dword ptr es:[PMD * 4];
__asm cmp byte ptr es:[bx+2], 'P';
__asm jnz nope;
__asm cmp byte ptr es:[bx+3], 'M';
__asm jnz nope;
__asm cmp byte ptr es:[bx+4], 'D';
__asm jnz nope;
return true;
nope:
__asm { les bx, dword ptr es:[PMD * 4] };
if(kaja_isr_magic_matches(MK_FP(_ES, _BX), 'P', 'M', 'D')) {
return true;
}
return false;
}

#pragma codestring "\x90"

0 comments on commit acd2985

Please sign in to comment.