Repository navigation
Expand file tree
/
Copy pathps2.rs
More file actions
156 lines (149 loc) · 6.44 KB
/
Copy pathps2.rs
File metadata and controls
156 lines (149 loc) · 6.44 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! PlayStation 2 hardware-address translation.
use wasm_encoder::{BlockType, Function, Instruction, ValType};
use crate::{
abi::AbiImportId,
stdlib::{StateProviderMemoryRange, StdlibTypeId},
};
use super::super::GcLayout;
use super::super::imports::Abi;
use super::super::memory_plan::AbiReadScratch;
const BACKEND_POINTER_32: i32 = 2;
const BACKEND_POINTER_64: i32 = 3;
const BACKEND_RETROARCH: i32 = 4;
pub(super) fn compile_translate_address(
abi: &Abi,
gc: &GcLayout,
abi_read: AbiReadScratch,
readable: StateProviderMemoryRange,
) -> Function {
let mut function = Function::new([(1, ValType::I64), (1, ValType::I32)]);
let emulator = 1;
let address = 2;
let size = 3;
let base = 4;
let backend = 5;
let emulator_type = gc.standard_index(StdlibTypeId::PS2Emulator);
function
.instruction(&Instruction::LocalGet(emulator))
.instruction(&Instruction::RefIsNull)
.instruction(&Instruction::If(BlockType::Empty))
.instruction(&Instruction::I64Const(0))
.instruction(&Instruction::Return)
.instruction(&Instruction::End)
.instruction(&Instruction::LocalGet(address))
.instruction(&Instruction::I64ExtendI32U)
.instruction(&Instruction::I64Const(readable.start as i64))
.instruction(&Instruction::I64LtU)
.instruction(&Instruction::If(BlockType::Empty))
.instruction(&Instruction::I64Const(0))
.instruction(&Instruction::Return)
.instruction(&Instruction::End)
.instruction(&Instruction::LocalGet(address))
.instruction(&Instruction::I64ExtendI32U)
.instruction(&Instruction::I64Const(readable.end as i64))
.instruction(&Instruction::I64GeU)
.instruction(&Instruction::If(BlockType::Empty))
.instruction(&Instruction::I64Const(0))
.instruction(&Instruction::Return)
.instruction(&Instruction::End)
.instruction(&Instruction::LocalGet(address))
.instruction(&Instruction::I64ExtendI32U)
.instruction(&Instruction::LocalGet(size))
.instruction(&Instruction::I64ExtendI32U)
.instruction(&Instruction::I64Add)
.instruction(&Instruction::I64Const(readable.end as i64))
.instruction(&Instruction::I64GtU)
.instruction(&Instruction::If(BlockType::Empty))
.instruction(&Instruction::I64Const(0))
.instruction(&Instruction::Return)
.instruction(&Instruction::End)
.instruction(&Instruction::LocalGet(emulator))
.instruction(&Instruction::StructGet {
struct_type_index: emulator_type,
field_index: gc.standard_field_index(crate::stdlib::StdlibFieldId::PS2EmulatorBackend),
})
.instruction(&Instruction::LocalSet(backend))
.instruction(&Instruction::LocalGet(emulator))
.instruction(&Instruction::StructGet {
struct_type_index: emulator_type,
field_index: gc.standard_field_index(crate::stdlib::StdlibFieldId::PS2EmulatorBase),
})
.instruction(&Instruction::LocalSet(base))
.instruction(&Instruction::LocalGet(backend))
.instruction(&Instruction::I32Const(BACKEND_POINTER_32))
.instruction(&Instruction::I32Eq)
.instruction(&Instruction::If(BlockType::Empty));
emit_process_read(&mut function, abi, abi_read, base, 4);
function
.instruction(&Instruction::I32Const(abi_read.start()))
.instruction(&Instruction::I32Load(super::super::memarg()))
.instruction(&Instruction::I64ExtendI32U)
.instruction(&Instruction::LocalSet(base))
.instruction(&Instruction::Else)
.instruction(&Instruction::LocalGet(backend))
.instruction(&Instruction::I32Const(BACKEND_POINTER_64))
.instruction(&Instruction::I32Eq)
.instruction(&Instruction::If(BlockType::Empty));
emit_process_read(&mut function, abi, abi_read, base, 8);
function
.instruction(&Instruction::I32Const(abi_read.start()))
.instruction(&Instruction::I64Load(super::super::memarg()))
.instruction(&Instruction::LocalSet(base))
.instruction(&Instruction::Else)
.instruction(&Instruction::LocalGet(backend))
.instruction(&Instruction::I32Const(BACKEND_RETROARCH))
.instruction(&Instruction::I32Eq)
.instruction(&Instruction::If(BlockType::Empty))
.instruction(&Instruction::LocalGet(emulator))
.instruction(&Instruction::StructGet {
struct_type_index: emulator_type,
field_index: gc
.standard_field_index(crate::stdlib::StdlibFieldId::PS2EmulatorAuxiliary),
})
.instruction(&Instruction::LocalSet(base));
emit_process_read(&mut function, abi, abi_read, base, 1);
function
.instruction(&Instruction::LocalGet(emulator))
.instruction(&Instruction::StructGet {
struct_type_index: emulator_type,
field_index: gc.standard_field_index(crate::stdlib::StdlibFieldId::PS2EmulatorBase),
})
.instruction(&Instruction::LocalSet(base))
.instruction(&Instruction::Else)
.instruction(&Instruction::I64Const(0))
.instruction(&Instruction::Return)
.instruction(&Instruction::End)
.instruction(&Instruction::End)
.instruction(&Instruction::End)
.instruction(&Instruction::LocalGet(base))
.instruction(&Instruction::I64Eqz)
.instruction(&Instruction::If(BlockType::Empty))
.instruction(&Instruction::I64Const(0))
.instruction(&Instruction::Return)
.instruction(&Instruction::End)
.instruction(&Instruction::LocalGet(base))
.instruction(&Instruction::LocalGet(address))
.instruction(&Instruction::I64ExtendI32U)
.instruction(&Instruction::I64Add)
.instruction(&Instruction::End);
function
}
fn emit_process_read(
function: &mut Function,
abi: &Abi,
abi_read: AbiReadScratch,
address: u32,
size: u32,
) {
function
.instruction(&Instruction::LocalGet(0))
.instruction(&Instruction::LocalGet(address))
.instruction(&Instruction::I32Const(abi_read.destination(size)))
.instruction(&Instruction::I32Const(size as i32))
.instruction(&Instruction::Call(abi.function(AbiImportId::ProcessRead)))
.instruction(&Instruction::I32Eqz)
.instruction(&Instruction::If(BlockType::Empty))
.instruction(&Instruction::I64Const(0))
.instruction(&Instruction::Return)
.instruction(&Instruction::End);
}