Skip to content

Commit cf45410

Browse files
committed
add proc-bitfield, start experimenting with instruction decoding
1 parent e6512de commit cf45410

File tree

7 files changed

+87
-41
lines changed

7 files changed

+87
-41
lines changed

src/cpu/dynarec/dynarec.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ int missing_block_handler(u32 physical_address, n64_dynarec_block_t* block, n64_
3636
#endif
3737

3838
mark_metric(METRIC_BLOCK_COMPILATION);
39-
static_assert(sizeof(JitBlock) == sizeof(n64_dynarec_block_t), "sizeof(Rust JitBlock) == sizeof(n64_dynarec_block_t)");
40-
v3_compile_new_block((JitBlock*)block, (uint8_t*)code_mask, BLOCKCACHE_INNER_SIZE, N64CPU.pc, physical_address);
41-
42-
43-
printf("Code mask at 0: %d\n", code_mask[0]);
44-
printf("Code mask at 1: %d\n", code_mask[1]);
45-
printf("Host size: %zu\nGuest size: %zu\n", block->host_size, block->guest_size);
39+
v3_compile_new_block(block, code_mask, N64CPU.pc, physical_address);
4640
v2_compile_new_block(block, code_mask, N64CPU.pc, physical_address);
4741
if (block->run == NULL) {
4842
logfatal("Failed to compile block!");

src/cpu/dynarec/v2/v2_compiler.c

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "v2_compiler.h"
22

3+
#include "jit_rs.h"
34
#include <log.h>
45
#include <mem/n64bus.h>
56
#include <disassemble.h>
@@ -190,6 +191,15 @@ int idle_loop_replacement() {
190191
return ticks_to_skip;
191192
}
192193

194+
void v3_compile_new_block(
195+
n64_dynarec_block_t* block,
196+
bool* code_mask,
197+
u64 virtual_address,
198+
u32 physical_address) {
199+
fill_temp_code(virtual_address, physical_address, code_mask);
200+
rs_jit_compile_new_block((uint32_t*)temp_code, temp_code_len, virtual_address, physical_address);
201+
}
202+
193203
void v2_compile_new_block(
194204
n64_dynarec_block_t* block,
195205
bool* code_mask,

src/cpu/dynarec/v2/v2_compiler.h

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ void v2_compile_new_block(n64_dynarec_block_t *block, bool *code_mask, u64 virtu
2727
void v2_compiler_init();
2828
void v2_set_idle_loop_detection_enabled(bool enabled);
2929

30+
void v3_compile_new_block(n64_dynarec_block_t *block, bool *code_mask, u64 virtual_address, u32 physical_address);
31+
3032
#endif // N64_V2_COMPILER_H

src/jit/Cargo.lock

+41-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jit/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ crate-type = ["staticlib"]
88

99
[dependencies]
1010
dynasm = "2.0.0"
11+
proc-bitfield = "0.5.0"

src/jit/src/lib.rs

+4-32
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,6 @@
1-
use std::ffi::c_void;
2-
3-
#[repr(C)]
4-
pub struct JitBlock {
5-
// int (*run)(r4300i_t* cpu);
6-
pub run: *mut c_void,
7-
// size_t guest_size;
8-
pub guest_size: usize,
9-
// size_t host_size;
10-
pub host_size: usize,
11-
// n64_block_sysconfig_t sysconfig;
12-
pub sysconfig : u64,
13-
// u64 virtual_address;
14-
pub virtual_address : u64,
15-
// struct n64_dynarec_block* next; // for other sysconfigs
16-
pub next: *mut JitBlock
17-
}
18-
19-
fn compile_new_block(_block: &mut JitBlock, _code_mask: &mut [u8], virtual_address: u64, physical_address: u32) {
20-
println!("Compiling a new block at virtual address 0x{virtual_address:016X} and physical address 0x{physical_address:08X}");
21-
// block.run = std::ptr::null_mut();
22-
// block.host_size = 1;
23-
// block.guest_size = 2;
24-
// block.sysconfig = 3;
25-
// block.virtual_address = 4;
26-
// block.next = std::ptr::null_mut();
27-
}
28-
1+
mod mips_parser;
292
#[no_mangle]
30-
pub unsafe extern fn v3_compile_new_block(block: *mut JitBlock, code_mask: *mut u8, code_mask_size: usize, virtual_address: u64, physical_address: u32) {
31-
let safe_block = block.as_mut().unwrap_unchecked();
32-
let safe_code_mask = std::slice::from_raw_parts_mut(code_mask, code_mask_size);
33-
compile_new_block(safe_block, safe_code_mask, virtual_address, physical_address);
3+
pub unsafe extern fn rs_jit_compile_new_block(instructions: *mut u32, num_instructions: usize, virtual_address: u64, physical_address: u32) {
4+
let safe_code = std::slice::from_raw_parts(instructions, num_instructions);
5+
mips_parser::parse(safe_code, virtual_address, physical_address);
346
}

src/jit/src/mips_parser.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
proc_bitfield::bitfield! {
2+
#[derive(Clone, Copy, PartialEq, Eq)]
3+
pub struct MipsInstruction(pub u32): Debug, FromStorage, IntoStorage, DerefStorage {
4+
pub raw : u32 @ ..,
5+
6+
pub imm: u16 @ 0..=15,
7+
pub s_imm: i16 @ 0..=15,
8+
9+
pub rt: u8 @ 16 .. 20,
10+
pub rs: u8 @ 21 .. 25,
11+
pub op: u8 @ 26 .. 31,
12+
}
13+
}
14+
15+
pub fn parse(code: &[u32], virtual_address: u64, physical_address: u32) {
16+
let instructions = code.iter().map(|word| MipsInstruction(*word));
17+
18+
let code_len = code.len();
19+
println!("Compiling {code_len} instructions at virtual address 0x{virtual_address:016X} and physical address 0x{physical_address:08X}");
20+
21+
let mut addr = virtual_address;
22+
for instr in instructions {
23+
let x = instr.raw();
24+
let opcode = instr.op();
25+
println!("{addr:016X}\t{x:08X} (opcode {opcode:X})");
26+
addr += 4;
27+
}
28+
}

0 commit comments

Comments
 (0)