Skip to content

Commit

Permalink
rom patching
Browse files Browse the repository at this point in the history
  • Loading branch information
bates64 committed Sep 23, 2022
1 parent dbd033a commit 5d10d2e
Show file tree
Hide file tree
Showing 23 changed files with 28,574 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ indent_size = 2
[{package-lock,package}.json]
indent_style = space
indent_size = 2

[Makefile]
indent_style = tab
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "patches/papermario"]
path = patches/papermario
url = [email protected]:pmret/papermario.git
31 changes: 31 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"version": 4,
"configurations": [
{
"name": "papermario",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"path": [
"${workspaceFolder}/patches/papermario/include",
"${workspaceFolder}/patches/papermario/src"
]
},
"includePath": [
"${workspaceFolder}/patches/papermario/include",
//"${workspaceFolder}/patches/papermario/ver/us/build/include",
"${workspaceFolder}/patches/papermario/src"
//"${workspaceFolder}/patches/papermario/assets/us"
],
"defines": [
"F3DEX_GBI_2",
"_LANGUAGE_C",
"_MIPS_SZLONG=32",
"VERSION=us",
"VERSION_US"
],
"cStandard": "c89",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
]
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.formatOnSave": true,
},
"clang-tidy.blacklist": ["*"]
}
1 change: 1 addition & 0 deletions mamar-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"idb-keyval": "^6.2.0",
"mamar-wasm-bridge": "*",
"mupen64plus-web": "^1.3.2",
"patches": "*",
"pm64-typegen": "*",
"react": "^18.2.0",
"react-aria": "^3.19.0",
Expand Down
46 changes: 27 additions & 19 deletions mamar-web/src/app/emu/PlaybackControls.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
import { ActionButton, Flex, ToggleButton, View } from "@adobe/react-spectrum"
import Play from "@spectrum-icons/workflow/Play"
import { useEffect, useState } from "react"
import * as patches from "patches"
import { Bgm } from "pm64-typegen"
import { useEffect, useMemo, useState } from "react"

import { useBgm } from "../store"
import useMupen from "../util/hooks/useMupen"
import useRomData from "../util/hooks/useRomData"
import Patcher from "../util/Patcher"

import "./PlaybackControls.scss"

function usePatchedRom(bgm: Bgm | undefined): ArrayBuffer | undefined {
const cleanRom = useRomData()

return useMemo(() => {
if (!bgm) {
return undefined
}

const rom = cleanRom.slice(0)
//const patcher = new Patcher(rom)
//patcher.overwriteFunction(0xF4A4, patches.skipIntroLogos)
//patcher.overwriteFunction(0x10, [0x1B99678D, 0xE109577C])

// TODO: encode bgm, write over Toad Town in SBN

return rom
}, [cleanRom, bgm])
}

export default function PlaybackControls() {
const [bgm] = useBgm()
const [isPlaying, setIsPlaying] = useState(false)
const romData = useRomData()
const romData = usePatchedRom(isPlaying ? bgm : undefined)
const mupen = useMupen(romData)

useEffect(() => {
if (!mupen || !bgm) {
mupen?.pause?.()
setIsPlaying(false)
return
}

if (isPlaying) {
mupen.resume()
mupen?.resume?.()
} else {
mupen.pause()
mupen?.pause?.()
}

const onKeydown = (event: KeyboardEvent) => {
Expand All @@ -34,21 +50,13 @@ export default function PlaybackControls() {
}
document.addEventListener("keydown", onKeydown)
return () => document.removeEventListener("keydown", onKeydown)
}, [mupen, bgm, isPlaying])
}, [mupen, isPlaying])

if (!bgm) {
return <View />
}

return <Flex alignItems="center">
<ActionButton
onPress={() => {
mupen?.reloadRom(romData)
}}
>
Reload
</ActionButton>

<ActionButton
onPress={() => {
mupen?.softReset()
Expand Down
13 changes: 13 additions & 0 deletions mamar-web/src/app/util/Patcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default class Patcher {
dv: DataView

constructor(rom: ArrayBuffer) {
this.dv = new DataView(rom)
}

overwriteFunction(romAddr: number, dataU32: number[]) {
for (let i = 0; i < dataU32.length; i++) {
this.dv.setUint32(romAddr + i * 4, dataU32[i], false)
}
}
}
13 changes: 9 additions & 4 deletions mamar-web/src/app/util/hooks/useMupen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ export default function useMupen(romData: ArrayBuffer | undefined): EmulatorCont
break
console.log("reloading ROM")
state.current = State.RELOADING
mupen.reloadRom(romData).finally(() => {
console.log("rom reload complete")
state.current = State.READY
})

// Emulator must be running to reload rom
mupen.resume()
.then(() => new Promise(r => setTimeout(r, 100)))
.then(() => mupen.reloadRom(romData))
.finally(() => {
console.log("rom reload complete")
state.current = State.READY
})
break
case State.RELOADING:
console.warn("ignoring ROM reload, already reloading")
Expand Down
12 changes: 12 additions & 0 deletions n64crc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CFLAGS ?= -O2

all: build/n64crc.mjs build/n64crc

build/n64crc: n64crc.c
gcc $(CFLAGS) -o $@ $<

build/n64crc.mjs: n64crc.c
emcc $(CFLAGS) -DEMSCRIPTEN -sEXPORTED_RUNTIME_METHODS=ccall,cwrap -o $@ $<

clean:
rm -f build/n64crc.mjs build/n64crc
Loading

0 comments on commit 5d10d2e

Please sign in to comment.