/
Mupen.ts
108 lines (92 loc) · 3.08 KB
/
Mupen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { MonkeyPatch, IMonkeyPatch } from './IMonkeyPatch';
import {IMupen} from '../modloader/consoles/mupen/IMupen';
import bitwise from 'bitwise';
import { UInt8 } from 'bitwise/types';
export class MonkeyPatch_rdramWriteBit8 extends MonkeyPatch
implements IMonkeyPatch {
mupen: IMupen;
constructor(mupen: IMupen) {
super();
this.mupen = mupen;
}
patch(): void {
this.replacement = (addr: number, bitoffset: number, bit: boolean) => {
let data = this.mupen.M64p.Memory.rdramRead8(addr);
let bits = bitwise.byte.read(data as UInt8);
bits[bitoffset] = bit ? 1 : 0;
data = bitwise.byte.write(bits);
this.mupen.M64p.Memory.rdramWrite8(addr, data);
};
this.original = this.mupen.M64p.Memory.rdramWriteBit8;
(this.mupen.M64p.Memory as any)['rdramWriteBit8'] = this.replacement;
}
unpatch(): void {
(this.mupen.M64p.Memory as any)['rdramWriteBit8'] = this.original;
}
}
export class MonkeyPatch_rdramReadBit8 extends MonkeyPatch
implements IMonkeyPatch {
mupen: IMupen;
constructor(mupen: IMupen) {
super();
this.mupen = mupen;
}
patch(): void {
this.replacement = (addr: number, bitoffset: number): boolean => {
let data = this.mupen.M64p.Memory.rdramRead8(addr);
let bits = bitwise.byte.read(data as UInt8);
return bits[bitoffset] === 1;
};
this.original = this.mupen.M64p.Memory.rdramReadBit8;
(this.mupen.M64p.Memory as any)['rdramReadBit8'] = this.replacement;
}
unpatch(): void {
(this.mupen.M64p.Memory as any)['rdramReadBit8'] = this.original;
}
}
export class MonkeyPatch_rdramWriteBits8 extends MonkeyPatch
implements IMonkeyPatch {
mupen: IMupen;
constructor(mupen: IMupen) {
super();
this.mupen = mupen;
}
patch(): void {
this.replacement = (addr: number, buf: Buffer) => {
let arr = [];
for (let i = 0; i < buf.byteLength; i++) {
arr.push(buf[i]);
}
let b = bitwise.byte.write(arr as any);
this.mupen.M64p.Memory.rdramWrite8(addr, b);
};
this.original = this.mupen.M64p.Memory.rdramWriteBit8;
(this.mupen.M64p.Memory as any)['rdramWriteBits8'] = this.replacement;
}
unpatch(): void {
(this.mupen.M64p.Memory as any)['rdramWriteBits8'] = this.original;
}
}
export class MonkeyPatch_rdramReadBits8 extends MonkeyPatch
implements IMonkeyPatch {
mupen: IMupen;
constructor(mupen: IMupen) {
super();
this.mupen = mupen;
}
patch(): void {
this.replacement = (addr: number) => {
let b = bitwise.byte.read(this.mupen.M64p.Memory.rdramRead8(addr) as any);
let buf: Buffer = Buffer.alloc(0x8);
for (let i = 0; i < b.length; i++) {
buf.writeUInt8(b[i], i);
}
return buf;
};
this.original = this.mupen.M64p.Memory.rdramReadBits8;
(this.mupen.M64p.Memory as any)['rdramReadBits8'] = this.replacement;
}
unpatch(): void {
(this.mupen.M64p.Memory as any)['rdramReadBits8'] = this.original;
}
}