import { JAL_ENCODE , JAL_DECODE , DecodeOpcode } from "./OpcodeBullshit" ; import { Heap } from 'modloader64_api/heap' ; import { IModLoaderAPI } from 'modloader64_api/IModLoaderAPI' ; export function get_upper ( input ) { return Math . abs ( ( input >> 16 ) & 0x0000FFFF ) ; } export function get_lower ( input ) { return Math . abs ( input & 0x0000FFFF ) ; } export function get_aligned ( input , alignment ) { return input + ( alignment - input % alignment ) ; } enum CODECAVE_TYPE { CAVETO = 0 , CAVETOANDJAL , CAVEMAKE , CAVEMAKEANDJAL , CAVEMAKEBEGJAL , COUNT } export function do_codecave ( address = 0 , parambuf = [ ] , type , ModLoader , heap ) { let address_buf = ModLoader . emulator . rdramReadBuffer ( address , 4 ) ; let inst = DecodeOpcode ( address_buf ) ; let input = 0 ; let to = 0 ; let newproc = 0 ; let newprocp = 0 ; if ( inst . code !== "JAL" ) ModLoader . logger . warn ( "Caving a non JAL instruction!" ) ; if ( type <= CODECAVE_TYPE . CAVETOANDJAL ) to = parambuf [ 0 ] ; if ( type === CODECAVE_TYPE . CAVETOANDJAL || type === CODECAVE_TYPE . CAVEMAKEANDJAL || type === CODECAVE_TYPE . CAVEMAKEBEGJAL ) input = parambuf [ 1 ] ; if ( type >= 2 ) { newproc = get_aligned ( heap . malloc ( 0x200 ) , 4 ) ; newprocp = newproc ; } if ( to ) to = JAL_ENCODE ( to ) ; if ( input ) input = JAL_ENCODE ( input ) ; if ( newproc ) to = JAL_ENCODE ( to ) ; if ( to ) { ModLoader . emulator . rdramWrite32 ( address , to ) ; ModLoader . emulator . rdramWrite32 ( address + 4 , 0 ) ; } if ( input && type !== CODECAVE_TYPE . CAVEMAKEBEGJAL ) { ModLoader . emulator . rdramWrite32 ( address + 8 , input ) ; ModLoader . emulator . rdramWrite32 ( address + 0xC , 0 ) ; } else if ( input ) { ModLoader . emulator . rdramWrite32 ( newprocp += 4 , input ) ; ModLoader . emulator . rdramWrite32 ( newprocp += 4 , 0 ) ; } if ( newproc ) { ModLoader . emulator . rdramWrite32 ( newprocp += 4 , 0x03E00008 ) ; ModLoader . emulator . rdramWrite32 ( newprocp += 4 , 0 ) ; } return 0 ; }