Compare commits

...

11 Commits

Author SHA1 Message Date
Logan McNaughton 913ec771bb new speed limiter code (#107)
* new speed limiter code

* add version
2025-01-07 09:11:59 +01:00
Logan McNaughton c3dee292a2 build update (#106) 2025-01-06 19:11:27 +01:00
Logan McNaughton 508ae5bc17 build updates (#105)
* build updates

* more

* more

* more

* more
2025-01-06 19:08:47 +01:00
Logan McNaughton 376fc85660 Fix typo (#104) 2025-01-06 18:15:52 +01:00
Logan McNaughton 31bad0416d Code cleanup (#103) 2025-01-06 10:08:58 +01:00
Logan McNaughton e103118407 Use github action to set up rust (#102) 2025-01-01 16:46:54 +01:00
Logan McNaughton 12e37b0b29 code formatting (#100) 2024-12-31 22:21:55 +01:00
Logan McNaughton 415c3eca40 Clippy fixes (#99) 2024-12-31 22:15:39 +01:00
Logan McNaughton 2c60f6bf81 Bumpto 0.1.13 (#98) 2024-12-29 12:15:01 +01:00
Logan McNaughton 5409b0f5d4 use spin sleep for more accurate VI timing (#97) 2024-12-29 12:06:59 +01:00
Logan McNaughton f9de4110ce Bump to 0.1.12 (#96) 2024-12-23 12:09:18 +01:00
25 changed files with 260 additions and 175 deletions
+23 -19
View File
@@ -12,6 +12,8 @@ env:
jobs:
build-linux:
runs-on: ubuntu-latest
env:
TARGET: x86_64-unknown-linux-gnu
steps:
- uses: actions/checkout@v4
with:
@@ -19,53 +21,55 @@ jobs:
- name: Install sdl2 headers
run: |
sudo apt -y install libsdl2-dev
- name: Update rust
run: |
rustup update
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
rustflags: ""
- name: Build
run: |
cargo build --release -v
cargo build --release -v --target=${{ env.TARGET }}
- name: Upload file
uses: actions/upload-artifact@v4
with:
name: gopher64-linux
path: target/release/gopher64
path: target/${{ env.TARGET }}/release/gopher64
build-windows:
runs-on: windows-latest
env:
TARGET: x86_64-pc-windows-msvc
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Update rust
run: |
rustup update
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
rustflags: ""
- name: Build
run: |
cargo build --release -v
cargo build --release -v --target=${{ env.TARGET }}
- name: Upload file
uses: actions/upload-artifact@v4
with:
name: gopher64-windows
path: target/release/gopher64.exe
path: target/${{ env.TARGET }}/release/gopher64.exe
build-macos:
runs-on: macos-13
runs-on: macos-latest
env:
TARGET: x86_64-apple-darwin
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Install sdl2 headers
run: |
brew install sdl2
- name: Update rust
run: |
rustup update
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: ${{ env.TARGET }}
rustflags: ""
- name: Build
run: |
cargo build --release -v
cargo build --release -v --target=${{ env.TARGET }}
- name: Upload file
uses: actions/upload-artifact@v4
with:
name: gopher64-mac
path: target/release/gopher64
path: target/${{ env.TARGET }}/release/gopher64
+5 -7
View File
@@ -16,13 +16,11 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Install sdl2 headers
run: |
sudo apt -y install libsdl2-dev
- name: Update rust
run: |
rustup update
rustup component add clippy
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: clippy, rustfmt
- name: Rustfmt Check
uses: actions-rust-lang/rustfmt@v1
- name: Lint
run: |
cargo clippy -- -Dwarnings
-5
View File
@@ -1,5 +0,0 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
]
}
+2 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "gopher64"
version = "0.1.11"
version = "0.1.13"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -19,6 +19,7 @@ sha2 = "0.10"
rusttype = "0.9"
futures = "0.3"
sdl2-sys = "0.37"
spin_sleep = "1.3"
clap = { version = "4.4", features = ["derive"] }
[dependencies.sdl2]
+4 -3
View File
@@ -1,7 +1,10 @@
fn main() {
println!("cargo::rerun-if-changed=parallel-rdp");
let mut build = cc::Build::new();
build
.cpp(true)
.warnings(false)
.std("c++17")
.file("parallel-rdp/parallel-rdp-standalone/parallel-rdp/command_ring.cpp")
.file("parallel-rdp/parallel-rdp-standalone/parallel-rdp/rdp_device.cpp")
@@ -68,9 +71,7 @@ fn main() {
{
build.flag("-march=x86-64-v3");
}
build
.flag("-Wno-missing-field-initializers")
.flag("-Wno-unused-parameter");
}
build.compile("parallel-rdp");
}
+52 -47
View File
@@ -51,16 +51,16 @@ enum vi_registers
};
static bool fullscreen;
static void *rdram;
static SDL_Window *window;
static RDP::CommandProcessor *processor;
static SDL_WSIPlatform *wsi_platform;
static WSI *wsi;
static uint32_t cmd_data[0x00040000 >> 2];
static int cmd_cur;
static int cmd_ptr;
static uint8_t emu_running;
static bool emu_running;
static uint64_t rdp_sync_signal;
static uint32_t vi_registers[14];
static GFX_INFO gfx_info;
static uint64_t last_frame_counter;
static uint64_t frame_counter;
@@ -132,18 +132,16 @@ static const unsigned cmd_len_lut[64] = {
1,
};
enum
void rdp_init(void *_window, GFX_INFO _gfx_info, bool _fullscreen)
{
MB_RDRAM_DRAM_ALIGNMENT_REQUIREMENT = 64 * 1024
};
window = (SDL_Window *)_window;
SDL_SetEventFilter(sdl_event_filter, nullptr);
void vk_init(void *mem_base, uint32_t rdram_size, uint8_t _fullscreen)
{
fullscreen = _fullscreen != 0;
rdram = mem_base;
gfx_info = _gfx_info;
fullscreen = _fullscreen;
bool window_vsync = 0;
wsi = new WSI;
SDL_WSIPlatform *wsi_platform = new SDL_WSIPlatform;
wsi_platform = new SDL_WSIPlatform;
wsi_platform->set_window(window);
wsi->set_platform(wsi_platform);
wsi->set_present_mode(window_vsync ? PresentMode::SyncToVBlank : PresentMode::UnlockedMaybeTear);
@@ -151,47 +149,61 @@ void vk_init(void *mem_base, uint32_t rdram_size, uint8_t _fullscreen)
Context::SystemHandles handles = {};
if (!::Vulkan::Context::init_loader(nullptr))
{
vk_close();
rdp_close();
}
if (!wsi->init_simple(1, handles))
{
vk_close();
rdp_close();
}
RDP::CommandProcessorFlags flags = 0;
processor = new RDP::CommandProcessor(wsi->get_device(), rdram, 0, rdram_size, rdram_size / 2, flags);
processor = new RDP::CommandProcessor(wsi->get_device(), gfx_info.RDRAM, 0, gfx_info.RDRAM_SIZE, gfx_info.RDRAM_SIZE / 2, flags);
if (!processor->device_is_supported())
{
delete processor;
delete wsi;
processor = nullptr;
vk_close();
rdp_close();
}
wsi->begin_frame();
emu_running = 1;
emu_running = true;
last_frame_counter = 0;
frame_counter = 0;
}
void vk_close()
void rdp_close()
{
delete processor;
delete wsi;
wsi->end_frame();
if (processor)
{
delete processor;
processor = nullptr;
}
if (wsi)
{
delete wsi;
wsi = nullptr;
}
if (wsi_platform)
{
delete wsi_platform;
wsi_platform = nullptr;
}
}
int sdl_event_filter(void *userdata, SDL_Event *event)
{
if (event->type == SDL_WINDOWEVENT)
{
SDL_WSIPlatform *platform = (SDL_WSIPlatform *)&wsi->get_platform();
switch (event->window.event)
{
case SDL_WINDOWEVENT_CLOSE:
emu_running = 0;
emu_running = false;
break;
case SDL_WINDOWEVENT_RESIZED:
platform->do_resize();
wsi_platform->do_resize();
break;
default:
break;
@@ -202,7 +214,7 @@ int sdl_event_filter(void *userdata, SDL_Event *event)
switch (event->key.keysym.scancode)
{
case SDL_SCANCODE_ESCAPE:
emu_running = 0;
emu_running = false;
break;
default:
break;
@@ -212,12 +224,6 @@ int sdl_event_filter(void *userdata, SDL_Event *event)
return 0;
}
void set_sdl_window(void *_window)
{
window = (SDL_Window *)_window;
SDL_SetEventFilter(sdl_event_filter, nullptr);
}
static void calculate_viewport(float *x, float *y, float *width, float *height)
{
bool window_widescreen = false;
@@ -301,10 +307,9 @@ static void render_frame(Vulkan::Device &device)
void rdp_set_vi_register(uint32_t reg, uint32_t value)
{
processor->set_vi_register(RDP::VIRegister(reg), value);
vi_registers[reg] = value;
}
uint8_t rdp_update_screen()
bool rdp_update_screen()
{
auto &device = wsi->get_device();
render_frame(device);
@@ -353,11 +358,11 @@ static uint32_t viCalculateVerticalHeight(uint32_t vstart, uint32_t yscale)
return (delta * scale) / 0x800;
}
uint64_t rdp_process_commands(uint32_t *dpc_regs, uint8_t *SP_DMEM)
uint64_t rdp_process_commands()
{
uint64_t interrupt_timer = 0;
const uint32_t DP_CURRENT = dpc_regs[DPC_CURRENT_REG] & 0x00FFFFF8;
const uint32_t DP_END = dpc_regs[DPC_END_REG] & 0x00FFFFF8;
const uint32_t DP_CURRENT = *gfx_info.DPC_CURRENT_REG & 0x00FFFFF8;
const uint32_t DP_END = *gfx_info.DPC_END_REG & 0x00FFFFF8;
int length = DP_END - DP_CURRENT;
if (length <= 0)
@@ -367,16 +372,16 @@ uint64_t rdp_process_commands(uint32_t *dpc_regs, uint8_t *SP_DMEM)
if ((cmd_ptr + length) & ~(0x0003FFFF >> 3))
return interrupt_timer;
dpc_regs[DPC_STATUS_REG] |= DP_STATUS_PIPE_BUSY | DP_STATUS_START_GCLK;
*gfx_info.DPC_STATUS_REG |= DP_STATUS_PIPE_BUSY | DP_STATUS_START_GCLK;
uint32_t offset = DP_CURRENT;
if (dpc_regs[DPC_STATUS_REG] & DP_STATUS_XBUS_DMA)
if (*gfx_info.DPC_STATUS_REG & DP_STATUS_XBUS_DMA)
{
do
{
offset &= 0xFF8;
cmd_data[2 * cmd_ptr + 0] = SDL_SwapBE32(*reinterpret_cast<const uint32_t *>(SP_DMEM + offset));
cmd_data[2 * cmd_ptr + 1] = SDL_SwapBE32(*reinterpret_cast<const uint32_t *>(SP_DMEM + offset + 4));
cmd_data[2 * cmd_ptr + 0] = SDL_SwapBE32(*reinterpret_cast<const uint32_t *>(gfx_info.DMEM + offset));
cmd_data[2 * cmd_ptr + 1] = SDL_SwapBE32(*reinterpret_cast<const uint32_t *>(gfx_info.DMEM + offset + 4));
offset += sizeof(uint64_t);
cmd_ptr++;
} while (--length > 0);
@@ -392,8 +397,8 @@ uint64_t rdp_process_commands(uint32_t *dpc_regs, uint8_t *SP_DMEM)
do
{
offset &= 0xFFFFF8;
cmd_data[2 * cmd_ptr + 0] = *reinterpret_cast<const uint32_t *>((uint8_t *)rdram + offset);
cmd_data[2 * cmd_ptr + 1] = *reinterpret_cast<const uint32_t *>((uint8_t *)rdram + offset + 4);
cmd_data[2 * cmd_ptr + 0] = *reinterpret_cast<const uint32_t *>(gfx_info.RDRAM + offset);
cmd_data[2 * cmd_ptr + 1] = *reinterpret_cast<const uint32_t *>(gfx_info.RDRAM + offset + 4);
offset += sizeof(uint64_t);
cmd_ptr++;
} while (--length > 0);
@@ -408,7 +413,7 @@ uint64_t rdp_process_commands(uint32_t *dpc_regs, uint8_t *SP_DMEM)
if (cmd_ptr - cmd_cur - cmd_length < 0)
{
dpc_regs[DPC_START_REG] = dpc_regs[DPC_CURRENT_REG] = dpc_regs[DPC_END_REG];
*gfx_info.DPC_START_REG = *gfx_info.DPC_CURRENT_REG = *gfx_info.DPC_END_REG;
return interrupt_timer;
}
@@ -427,19 +432,19 @@ uint64_t rdp_process_commands(uint32_t *dpc_regs, uint8_t *SP_DMEM)
rdp_sync_signal = 0;
}
uint32_t width = viCalculateHorizonalWidth(vi_registers[VI_H_START_REG], vi_registers[VI_X_SCALE_REG], vi_registers[VI_WIDTH_REG]);
uint32_t width = viCalculateHorizonalWidth(*gfx_info.VI_H_START_REG, *gfx_info.VI_X_SCALE_REG, *gfx_info.VI_WIDTH_REG);
if (width == 0)
{
width = 320;
}
uint32_t height = viCalculateVerticalHeight(vi_registers[VI_V_START_REG], vi_registers[VI_Y_SCALE_REG]);
uint32_t height = viCalculateVerticalHeight(*gfx_info.VI_V_START_REG, *gfx_info.VI_Y_SCALE_REG);
if (height == 0)
{
height = 240;
}
interrupt_timer = width * height * 4;
dpc_regs[DPC_STATUS_REG] &= ~(DP_STATUS_PIPE_BUSY | DP_STATUS_START_GCLK);
*gfx_info.DPC_STATUS_REG &= ~(DP_STATUS_PIPE_BUSY | DP_STATUS_START_GCLK);
}
cmd_cur += cmd_length;
@@ -447,13 +452,13 @@ uint64_t rdp_process_commands(uint32_t *dpc_regs, uint8_t *SP_DMEM)
cmd_ptr = 0;
cmd_cur = 0;
dpc_regs[DPC_CURRENT_REG] = dpc_regs[DPC_END_REG];
dpc_regs[DPC_STATUS_REG] |= DP_STATUS_CBUF_READY;
*gfx_info.DPC_CURRENT_REG = *gfx_info.DPC_END_REG;
*gfx_info.DPC_STATUS_REG |= DP_STATUS_CBUF_READY;
return interrupt_timer;
}
void full_sync()
void rdp_full_sync()
{
if (rdp_sync_signal)
{
+25 -7
View File
@@ -1,17 +1,35 @@
#pragma once
#ifdef __cplusplus
#include <cstdint>
extern "C"
{
#endif
void vk_init(void *mem_base, uint32_t rdram_size, uint8_t fullscreen);
void vk_close();
void set_sdl_window(void *_window);
void rdp_set_vi_register(uint32_t reg, uint32_t value);
uint8_t rdp_update_screen();
uint64_t rdp_process_commands(uint32_t *dpc_regs, uint8_t *SP_DMEM);
void full_sync();
typedef struct
{
uint8_t *RDRAM;
uint8_t *DMEM;
uint32_t RDRAM_SIZE;
uint32_t *DPC_CURRENT_REG;
uint32_t *DPC_START_REG;
uint32_t *DPC_END_REG;
uint32_t *DPC_STATUS_REG;
uint32_t *VI_H_START_REG;
uint32_t *VI_V_START_REG;
uint32_t *VI_X_SCALE_REG;
uint32_t *VI_Y_SCALE_REG;
uint32_t *VI_WIDTH_REG;
} GFX_INFO;
void rdp_init(void *_window, GFX_INFO _gfx_info, bool fullscreen);
void rdp_close();
void rdp_set_vi_register(uint32_t reg, uint32_t value);
bool rdp_update_screen();
uint64_t rdp_process_commands();
void rdp_full_sync();
int sdl_event_filter(void *userdata, SDL_Event *event);
#ifdef __cplusplus
}
+16 -9
View File
@@ -40,10 +40,10 @@ pub fn run_game(file_path: &std::path::Path, device: &mut Device, fullscreen: bo
cart_rom::init(device, rom_contents); // cart needs to come before rdram
// rdram pointer is shared with parallel-rdp
let (rdram_ptr, rdram_size) = rdram::init(device);
rdram::init(device);
ui::audio::init(&mut device.ui, 33600);
ui::video::init(&mut device.ui, rdram_ptr, rdram_size, fullscreen);
ui::video::init(device, fullscreen);
ui::input::init(&mut device.ui);
mi::init(device);
@@ -59,6 +59,9 @@ pub fn run_game(file_path: &std::path::Path, device: &mut Device, fullscreen: bo
cart_rom::load_rom_save(device);
cpu::run(device);
ui::video::close();
ui::storage::write_saves(&device.ui);
}
fn swap_rom(contents: Vec<u8>) -> Vec<u8> {
@@ -89,7 +92,7 @@ fn swap_rom(contents: Vec<u8>) -> Vec<u8> {
fn get_rom_contents(file_path: &std::path::Path) -> Vec<u8> {
let mut contents = vec![];
if file_path.extension().unwrap().to_ascii_lowercase() == "zip" {
if file_path.extension().unwrap().eq_ignore_ascii_case("zip") {
let zip_file = fs::File::open(file_path).unwrap();
let mut archive = zip::ZipArchive::new(zip_file).unwrap();
for i in 0..archive.len() {
@@ -106,7 +109,7 @@ fn get_rom_contents(file_path: &std::path::Path) -> Vec<u8> {
break;
}
}
} else if file_path.extension().unwrap().to_ascii_lowercase() == "7z" {
} else if file_path.extension().unwrap().eq_ignore_ascii_case("7z") {
let mut archive =
sevenz_rust::SevenZReader::open(file_path, sevenz_rust::Password::empty()).unwrap();
@@ -144,12 +147,12 @@ pub struct Device {
pif: pif::Pif,
cart: cart_rom::Cart,
memory: memory::Memory,
rsp: rsp_interface::Rsp,
rdp: rdp::Rdp,
pub rsp: rsp_interface::Rsp,
pub rdp: rdp::Rdp,
pub rdram: rdram::Rdram,
mi: mi::Mi,
pi: pi::Pi,
vi: vi::Vi,
pub vi: vi::Vi,
ai: ai::Ai,
si: si::Si,
ri: ri::Ri,
@@ -239,7 +242,7 @@ impl Device {
llbit: false,
lo: 0,
hi: 0,
running: 0,
running: false,
instrs: [cop0::reserved; 64],
special_instrs: [cop0::reserved; 64],
regimm_instrs: [cop0::reserved; 32],
@@ -292,7 +295,10 @@ impl Device {
words: [0; 4],
}; 512],
},
rdram: rdram::Rdram { mem: vec![] },
rdram: rdram::Rdram {
mem: vec![],
size: 0x800000,
},
rsp: rsp_interface::Rsp {
cpu: rsp_cpu::Cpu {
instructions: [rsp_cpu::Instructions {
@@ -380,6 +386,7 @@ impl Device {
delay: 0,
count_per_scanline: 0,
limiter: None,
vi_counter: 0,
},
flashram: sram::Flashram {
status: 0,
+1 -1
View File
@@ -159,7 +159,7 @@ pub fn af_rtc_write_block(device: &mut device::Device, block: usize, offset: usi
match device.pif.ram[block] {
0 => {
device.cart.rtc.control =
(device.pif.ram[offset + 1] as u16) << 8 | device.pif.ram[offset] as u16;
((device.pif.ram[offset + 1] as u16) << 8) | device.pif.ram[offset] as u16;
device.pif.ram[status] = 0x00;
}
1 => {
+2 -2
View File
@@ -75,7 +75,7 @@ pub fn pak_read_block(
channel: usize,
) {
let address =
(device.pif.ram[addr_acrc] as u16) << 8 | (device.pif.ram[addr_acrc + 1] & 0xe0) as u16;
((device.pif.ram[addr_acrc] as u16) << 8) | (device.pif.ram[addr_acrc + 1] & 0xe0) as u16;
let handler = device.pif.channels[channel].pak_handler;
if handler.is_some() {
@@ -94,7 +94,7 @@ pub fn pak_write_block(
channel: usize,
) {
let address =
(device.pif.ram[addr_acrc] as u16) << 8 | (device.pif.ram[addr_acrc + 1] & 0xe0) as u16;
((device.pif.ram[addr_acrc] as u16) << 8) | (device.pif.ram[addr_acrc + 1] & 0xe0) as u16;
let handler = device.pif.channels[channel].pak_handler;
if handler.is_some() {
+1 -1
View File
@@ -359,7 +359,7 @@ pub fn set_control_registers_fpu(device: &mut device::Device, index: u32, data:
device.cpu.cop1.fcr31 = data & FCR31_WRITE_MASK;
// the Cause bits are ANDed with the Enable bits to check for exceptions
// "Unimplemented Operation" has no Enable bit and always causes an exception
if (device.cpu.cop1.fcr31 & FCR31_CAUSE_MASK) >> 5
if ((device.cpu.cop1.fcr31 & FCR31_CAUSE_MASK) >> 5)
& (device.cpu.cop1.fcr31 & FCR31_ENABLE_MASK)
!= 0
|| device.cpu.cop1.fcr31 & FCR31_CAUSE_UNIMP_BIT != 0
+3 -3
View File
@@ -25,7 +25,7 @@ pub struct Cpu {
pub pc_phys: u64,
pub lo: u64,
pub hi: u64,
pub running: u8,
pub running: bool,
pub llbit: bool,
pub clock_rate: u64,
pub instrs: [fn(&mut device::Device, u32); 64],
@@ -231,8 +231,8 @@ pub fn in_delay_slot_taken(device: &device::Device) -> bool {
}
pub fn run(device: &mut device::Device) {
device.cpu.running = 1;
while device.cpu.running == 1 {
device.cpu.running = true;
while device.cpu.running {
device.cpu.gpr[0] = 0; // gpr 0 is read only
let (cached, err);
(device.cpu.pc_phys, cached, err) = device::memory::translate_address(
+4 -4
View File
@@ -346,12 +346,12 @@ pub fn lwl(device: &mut device::Device, opcode: u32) {
device.cpu.gpr[rt(opcode) as usize] = se32(
((device.cpu.gpr[rt(opcode) as usize] as u32) & mask
| device::memory::data_read(
| (device::memory::data_read(
device,
phys_address,
device::memory::AccessSize::Word,
cached,
) << shift) as i32,
) << shift)) as i32,
)
}
@@ -431,12 +431,12 @@ pub fn lwr(device: &mut device::Device, opcode: u32) {
device.cpu.gpr[rt(opcode) as usize] = se32(
((device.cpu.gpr[rt(opcode) as usize] as u32) & (mask as u32)
| device::memory::data_read(
| (device::memory::data_read(
device,
phys_address,
device::memory::AccessSize::Word,
cached,
) >> shift) as i32,
) >> shift)) as i32,
)
}
+4 -2
View File
@@ -92,7 +92,8 @@ pub fn write_regs_dpc(device: &mut device::Device, address: u64, value: u32, mas
}
pub fn run_rdp(device: &mut device::Device) {
let timer = ui::video::process_rdp_list(&mut device.rdp.regs_dpc, &mut device.rsp.mem);
let timer = ui::video::process_rdp_list();
if timer != 0 {
device::events::create_event(
device,
@@ -173,6 +174,7 @@ pub fn init(device: &mut device::Device) {
}
pub fn rdp_interrupt_event(device: &mut device::Device) {
ui::video::rdp_full_sync();
ui::video::full_sync();
device::mi::set_rcp_interrupt(device, device::mi::MI_INTR_DP)
}
+10 -11
View File
@@ -3,10 +3,9 @@ use std::alloc::{alloc_zeroed, Layout};
pub const RDRAM_MASK: usize = 0xFFFFFF;
pub const RDRAM_SIZE: usize = 0x800000; // 8MB
pub struct Rdram {
pub mem: Vec<u8>,
pub size: u32,
}
pub fn read_mem_fast(
@@ -32,7 +31,7 @@ pub fn read_mem(
rdram_calculate_cycles(access_size as u64) / (access_size as u64 / 4),
);
let masked_address = address as usize & RDRAM_MASK;
if address < RDRAM_SIZE as u64 {
if address < device.rdram.size as u64 {
u32::from_ne_bytes(
device.rdram.mem[masked_address..masked_address + 4]
.try_into()
@@ -44,7 +43,7 @@ pub fn read_mem(
}
pub fn write_mem(device: &mut device::Device, address: u64, value: u32, mask: u32) {
if address < RDRAM_SIZE as u64 {
if address < device.rdram.size as u64 {
let mut data = u32::from_ne_bytes(
device.rdram.mem[address as usize..(address + 4) as usize]
.try_into()
@@ -68,19 +67,19 @@ pub fn write_regs(_device: &mut device::Device, _address: u64, _value: u32, _mas
panic!("rdram write reg");
}
pub fn init(device: &mut device::Device) -> (*mut u8, usize) {
pub fn init(device: &mut device::Device) {
let alignment = 64 * 1024;
let layout = Layout::from_size_align(RDRAM_SIZE, alignment).expect("Invalid layout");
let layout =
Layout::from_size_align(device.rdram.size as usize, alignment).expect("Invalid layout");
let ptr = unsafe { alloc_zeroed(layout) };
device.rdram.mem = unsafe { Vec::from_raw_parts(ptr, RDRAM_SIZE, RDRAM_SIZE) };
device.rdram.mem =
unsafe { Vec::from_raw_parts(ptr, device.rdram.size as usize, device.rdram.size as usize) };
// hack, skip RDRAM initialization
let data: u32 = RDRAM_SIZE as u32;
device.rdram.mem[device.cart.rdram_size_offset..device.cart.rdram_size_offset + 4]
.copy_from_slice(&data.to_ne_bytes());
(ptr, RDRAM_SIZE)
.copy_from_slice(&device.rdram.size.to_ne_bytes());
}
pub fn rdram_calculate_cycles(length: u64) -> u64 {
31 + (length / 3)// https://hcs64.com/dma.html, https://github.com/rasky/n64-systembench
31 + (length / 3) // https://hcs64.com/dma.html, https://github.com/rasky/n64-systembench
}
+1 -1
View File
@@ -166,7 +166,7 @@ pub fn do_dma(device: &mut device::Device, dma: RspDma) {
let mut i = 0;
while i < length {
let mut data = 0;
if dram_addr < device::rdram::RDRAM_SIZE as u32 {
if dram_addr < device.rdram.size {
data = u32::from_ne_bytes(
device.rdram.mem[dram_addr as usize..dram_addr as usize + 4]
.try_into()
+6 -5
View File
@@ -208,7 +208,7 @@ pub fn lw(device: &mut device::Device, opcode: u32) {
w[3] = device.rsp.mem[(address as usize + 3) & 0xFFF];
device.rsp.cpu.gpr[rt(opcode) as usize] =
(w[0] as u32) << 24 | (w[1] as u32) << 16 | (w[2] as u32) << 8 | (w[3] as u32)
((w[0] as u32) << 24) | ((w[1] as u32) << 16) | ((w[2] as u32) << 8) | (w[3] as u32)
}
pub fn lbu(device: &mut device::Device, opcode: u32) {
@@ -240,7 +240,7 @@ pub fn lwu(device: &mut device::Device, opcode: u32) {
w[3] = device.rsp.mem[(address as usize + 3) & 0xFFF];
device.rsp.cpu.gpr[rt(opcode) as usize] =
(w[0] as u32) << 24 | (w[1] as u32) << 16 | (w[2] as u32) << 8 | (w[3] as u32)
((w[0] as u32) << 24) | ((w[1] as u32) << 16) | ((w[2] as u32) << 8) | (w[3] as u32)
}
pub fn sb(device: &mut device::Device, opcode: u32) {
@@ -486,7 +486,8 @@ pub fn mfc2(device: &mut device::Device, opcode: u32) {
device.rsp.cpu.vpr[rd(opcode) as usize],
velement(opcode) + 1,
);
device.rsp.cpu.gpr[rt(opcode) as usize] = ((hi as u16) << 8 | (lo as u16)) as i16 as i32 as u32
device.rsp.cpu.gpr[rt(opcode) as usize] =
(((hi as u16) << 8) | (lo as u16)) as i16 as i32 as u32
}
pub fn cfc2(device: &mut device::Device, opcode: u32) {
@@ -936,8 +937,8 @@ pub fn shv(device: &mut device::Device, opcode: u32) {
let mut offset = 0;
while offset < 8 {
let byte_val = element + offset * 2;
let value = get_vpr_byte(device.rsp.cpu.vpr[rt(opcode) as usize], byte_val) << 1
| get_vpr_byte(device.rsp.cpu.vpr[rt(opcode) as usize], byte_val + 1) >> 7;
let value = (get_vpr_byte(device.rsp.cpu.vpr[rt(opcode) as usize], byte_val) << 1)
| (get_vpr_byte(device.rsp.cpu.vpr[rt(opcode) as usize], byte_val + 1) >> 7);
device.rsp.mem[((address + ((index + offset * 2) & 15) as u32) & 0xFFF) as usize] = value;
offset += 1;
}
+3 -3
View File
@@ -478,7 +478,7 @@ pub fn vmacq(device: &mut device::Device, opcode: u32) {
let mut n = 0;
while n < 8 {
let mut product =
(get_vpr_element(*acch, n) as i32) << 16 | (get_vpr_element(*accm, n) as i32);
((get_vpr_element(*acch, n) as i32) << 16) | (get_vpr_element(*accm, n) as i32);
if product < 0 && (product & (1 << 5)) == 0 {
product += 32
} else if product >= 32 && (product & (1 << 5)) == 0 {
@@ -1275,7 +1275,7 @@ pub fn vrcpl(device: &mut device::Device, opcode: u32) {
let mut result;
let vte = vte(device, vt(opcode), ve(opcode) as usize);
let input = if device.rsp.cpu.divdp {
(device.rsp.cpu.divin as i32) << 16
((device.rsp.cpu.divin as i32) << 16)
| get_vpr_element(device.rsp.cpu.vpr[vt(opcode) as usize], ve(opcode) as u8) as u16
as i32
} else {
@@ -1371,7 +1371,7 @@ pub fn vrsql(device: &mut device::Device, opcode: u32) {
let mut result;
let vte = vte(device, vt(opcode), ve(opcode) as usize);
let input = if device.rsp.cpu.divdp {
(device.rsp.cpu.divin as i32) << 16
((device.rsp.cpu.divin as i32) << 16)
| get_vpr_element(device.rsp.cpu.vpr[vt(opcode) as usize], ve(opcode) as u8) as u16
as i32
} else {
+2 -2
View File
@@ -141,7 +141,7 @@ pub fn dma_read_sram(
format_sram(device);
while i < dram_addr + length && i < device::rdram::RDRAM_SIZE as u32 {
while i < dram_addr + length && i < device.rdram.size {
device.ui.saves.sram.0[j as usize] = device.rdram.mem[i as usize ^ device.byte_swap];
i += 1;
j += 1;
@@ -191,7 +191,7 @@ pub fn dma_write_sram(
format_sram(device);
while i < dram_addr + length && i < device::rdram::RDRAM_SIZE as u32 {
while i < dram_addr + length && i < device.rdram.size {
device.rdram.mem[i as usize ^ device.byte_swap] = device.ui.saves.sram.0[j as usize];
i += 1;
j += 1;
+14 -7
View File
@@ -38,8 +38,8 @@ pub fn read(device: &mut device::Device, index: u64) {
device.cpu.cop0.tlb_entries[index as usize].mask << 13;
device.cpu.cop0.regs[device::cop0::COP0_ENTRYHI_REG as usize] =
(device.cpu.cop0.tlb_entries[index as usize].region as u64) << 62
| device.cpu.cop0.tlb_entries[index as usize].vpn2 << 13
((device.cpu.cop0.tlb_entries[index as usize].region as u64) << 62)
| (device.cpu.cop0.tlb_entries[index as usize].vpn2 << 13)
| (device.cpu.cop0.tlb_entries[index as usize].asid) as u64;
device.cpu.cop0.regs[device::cop0::COP0_ENTRYLO0_REG as usize] =
@@ -136,7 +136,8 @@ pub fn probe(device: &mut device::Device) {
if e.region != (device.cpu.cop0.regs[device::cop0::COP0_ENTRYHI_REG as usize] >> 62) as u8 {
continue;
}
if e.g == 0 && e.asid != device.cpu.cop0.regs[device::cop0::COP0_ENTRYHI_REG as usize] as u8 {
if e.g == 0 && e.asid != device.cpu.cop0.regs[device::cop0::COP0_ENTRYHI_REG as usize] as u8
{
continue;
}
device.cpu.cop0.regs[device::cop0::COP0_INDEX_REG as usize] = pos as u64;
@@ -185,8 +186,11 @@ pub fn tlb_unmap(device: &mut device::Device, index: u64) {
pub fn tlb_map(device: &mut device::Device, index: u64) {
let e = &mut device.cpu.cop0.tlb_entries[index as usize];
if e.v_even != 0 && e.start_even < e.end_even
&& !(e.start_even >= 0x80000000 && e.end_even < 0xC0000000) && e.phys_even < 0x20000000 {
if e.v_even != 0
&& e.start_even < e.end_even
&& !(e.start_even >= 0x80000000 && e.end_even < 0xC0000000)
&& e.phys_even < 0x20000000
{
let mut i = e.start_even;
while i < e.end_even {
device.cpu.cop0.tlb_lut_r[(i >> 12) as usize].address =
@@ -205,8 +209,11 @@ pub fn tlb_map(device: &mut device::Device, index: u64) {
}
}
if e.v_odd != 0 && e.start_odd < e.end_odd
&& !(e.start_odd >= 0x80000000 && e.end_odd < 0xC0000000) && e.phys_odd < 0x20000000 {
if e.v_odd != 0
&& e.start_odd < e.end_odd
&& !(e.start_odd >= 0x80000000 && e.end_odd < 0xC0000000)
&& e.phys_odd < 0x20000000
{
let mut i = e.start_odd;
while i < e.end_odd {
device.cpu.cop0.tlb_lut_r[(i >> 12) as usize].address =
+15 -11
View File
@@ -4,18 +4,18 @@ use governor::clock::Clock;
pub const VI_STATUS_REG: u32 = 0;
//pub const VI_ORIGIN_REG: u32 = 1;
//pub const VI_WIDTH_REG: u32 = 2;
pub const VI_WIDTH_REG: u32 = 2;
//pub const VI_V_INTR_REG: u32 = 3;
pub const VI_CURRENT_REG: u32 = 4;
//pub const VI_BURST_REG: u32 = 5;
pub const VI_V_SYNC_REG: u32 = 6;
pub const VI_H_SYNC_REG: u32 = 7;
//pub const VI_LEAP_REG: u32 = 8;
//pub const VI_H_START_REG: u32 = 9;
//pub const VI_V_START_REG: u32 = 10;
pub const VI_H_START_REG: u32 = 9;
pub const VI_V_START_REG: u32 = 10;
//pub const VI_V_BURST_REG: u32 = 11;
//pub const VI_X_SCALE_REG: u32 = 12;
//pub const VI_Y_SCALE_REG: u32 = 13;
pub const VI_X_SCALE_REG: u32 = 12;
pub const VI_Y_SCALE_REG: u32 = 13;
pub const VI_REGS_COUNT: u32 = 14;
pub struct Vi {
@@ -25,10 +25,13 @@ pub struct Vi {
pub field: u32,
pub limiter: Option<governor::DefaultDirectRateLimiter>,
pub count_per_scanline: u64,
pub vi_counter: u64,
}
//static mut FRAME_COUNTER: u64 = 0;
const LIMIT_BUFFER: u64 = 3;
pub fn set_expected_refresh_rate(device: &mut device::Device) {
let expected_refresh_rate = device.vi.clock as f64
/ (device.vi.regs[VI_V_SYNC_REG as usize] + 1) as f64
@@ -39,7 +42,7 @@ pub fn set_expected_refresh_rate(device: &mut device::Device) {
device.vi.delay / (device.vi.regs[VI_V_SYNC_REG as usize] + 1) as u64;
let quota = governor::Quota::with_period(std::time::Duration::from_secs_f64(
1.0 / expected_refresh_rate,
(1.0 / expected_refresh_rate) * LIMIT_BUFFER as f64,
))
.unwrap();
device.vi.limiter = Some(governor::RateLimiter::direct(quota))
@@ -97,21 +100,19 @@ pub fn write_regs(device: &mut device::Device, address: u64, value: u32, mask: u
device::memory::masked_write_32(&mut device.vi.regs[reg as usize], value, mask);
set_vertical_interrupt(device);
set_expected_refresh_rate(device);
ui::video::set_register(reg as u32, device.vi.regs[reg as usize])
}
}
VI_H_SYNC_REG => {
if device.vi.regs[reg as usize] != value & mask {
device::memory::masked_write_32(&mut device.vi.regs[reg as usize], value, mask);
set_expected_refresh_rate(device);
ui::video::set_register(reg as u32, device.vi.regs[reg as usize])
}
}
_ => {
device::memory::masked_write_32(&mut device.vi.regs[reg as usize], value, mask);
ui::video::set_register(reg as u32, device.vi.regs[reg as usize])
}
}
ui::video::set_register(reg as u32, device.vi.regs[reg as usize])
}
pub fn vertical_interrupt_event(device: &mut device::Device) {
@@ -123,7 +124,10 @@ pub fn vertical_interrupt_event(device: &mut device::Device) {
}
*/
speed_limiter(device);
device.vi.vi_counter += 1;
if device.vi.vi_counter % LIMIT_BUFFER == 0 {
speed_limiter(device);
}
/* toggle vi field if in interlaced mode */
device.vi.field ^= (device.vi.regs[VI_STATUS_REG as usize] >> 6) & 0x1;
@@ -163,7 +167,7 @@ pub fn speed_limiter(device: &device::Device) {
if result.is_err() {
let outcome = result.unwrap_err();
let dur = outcome.wait_time_from(governor::clock::DefaultClock::default().now());
std::thread::sleep(dur);
spin_sleep::sleep(dur);
device.vi.limiter.as_ref().unwrap().check().unwrap();
}
-1
View File
@@ -26,7 +26,6 @@ pub struct Ui {
impl Drop for Ui {
fn drop(&mut self) {
storage::write_saves(self);
write_config(self);
}
}
+2 -2
View File
@@ -24,11 +24,11 @@ pub fn play_audio(device: &mut device::Device, dram_addr: usize, length: u64) {
while i < length as usize / 2 {
// Left channel
primary_buffer[i] = device.rdram.mem[dram_addr + (i * 2) + 2] as i16
| (device.rdram.mem[dram_addr + (i * 2) + 3] as i16) << 8;
| ((device.rdram.mem[dram_addr + (i * 2) + 3] as i16) << 8);
// Right channel
primary_buffer[i + 1] = device.rdram.mem[dram_addr + (i * 2)] as i16
| (device.rdram.mem[dram_addr + (i * 2) + 1] as i16) << 8;
| ((device.rdram.mem[dram_addr + (i * 2) + 1] as i16) << 8);
i += 2;
}
+2
View File
@@ -215,6 +215,8 @@ impl eframe::App for GopherEguiApp {
ui.end_row();
}
});
ui.add_space(32.0);
ui.label(format!("Version: {}", env!("CARGO_PKG_VERSION")));
});
}
}
+63 -21
View File
@@ -1,36 +1,79 @@
use crate::ui;
use crate::device;
unsafe extern "C" {
pub fn vk_init(rdram_ptr: usize, rdram_size: u32, fullscreen: u8);
pub fn set_sdl_window(window: usize);
pub fn rdp_update_screen() -> u8;
pub fn rdp_set_vi_register(reg: u32, value: u32);
pub fn rdp_process_commands(dpc_regs: &mut [u32; 8], SP_DMEM: &mut [u8; 8192]) -> u64;
pub fn full_sync();
#[repr(C)]
pub struct GfxInfo {
pub rdram: *mut u8,
pub dmem: *mut u8,
pub rdram_size: u32,
pub dpc_current_reg: *mut u32,
pub dpc_start_reg: *mut u32,
pub dpc_end_reg: *mut u32,
pub dpc_status_reg: *mut u32,
pub vi_h_start_reg: *mut u32,
pub vi_v_start_reg: *mut u32,
pub vi_x_scale_reg: *mut u32,
pub vi_y_scale_reg: *mut u32,
pub vi_width_reg: *mut u32,
}
pub fn init(ui: &mut ui::Ui, rdram_ptr: *mut u8, rdram_size: usize, fullscreen: bool) {
let mut builder = ui
unsafe extern "C" {
pub fn rdp_init(window: usize, gfx_info: GfxInfo, fullscreen: bool);
pub fn rdp_close();
pub fn rdp_update_screen() -> bool;
pub fn rdp_set_vi_register(reg: u32, value: u32);
pub fn rdp_process_commands() -> u64;
pub fn rdp_full_sync();
}
pub fn init(device: &mut device::Device, fullscreen: bool) {
let mut builder = device
.ui
.video_subsystem
.as_ref()
.unwrap()
.window("gopher64", 640, 480);
builder.position_centered().vulkan();
if fullscreen {
builder.fullscreen_desktop();
} else {
builder.resizable();
}
ui.window = Some(builder.build().unwrap());
device.ui.window = Some(builder.build().unwrap());
let gfx_info = GfxInfo {
rdram: device.rdram.mem.as_mut_ptr(),
dmem: device.rsp.mem.as_mut_ptr(),
rdram_size: device.rdram.size,
dpc_current_reg: &mut device.rdp.regs_dpc[device::rdp::DPC_CURRENT_REG as usize],
dpc_start_reg: &mut device.rdp.regs_dpc[device::rdp::DPC_START_REG as usize],
dpc_end_reg: &mut device.rdp.regs_dpc[device::rdp::DPC_END_REG as usize],
dpc_status_reg: &mut device.rdp.regs_dpc[device::rdp::DPC_STATUS_REG as usize],
vi_h_start_reg: &mut device.vi.regs[device::vi::VI_H_START_REG as usize],
vi_v_start_reg: &mut device.vi.regs[device::vi::VI_V_START_REG as usize],
vi_x_scale_reg: &mut device.vi.regs[device::vi::VI_X_SCALE_REG as usize],
vi_y_scale_reg: &mut device.vi.regs[device::vi::VI_Y_SCALE_REG as usize],
vi_width_reg: &mut device.vi.regs[device::vi::VI_WIDTH_REG as usize],
};
unsafe {
set_sdl_window(ui.window.as_mut().unwrap().raw() as usize);
vk_init(rdram_ptr as usize, rdram_size as u32, fullscreen as u8)
rdp_init(
device.ui.window.as_mut().unwrap().raw() as usize,
gfx_info,
fullscreen,
)
}
}
pub fn update_screen() -> u8 {
// when the window is closed, running is set to 0
pub fn close() {
unsafe {
rdp_close();
}
}
pub fn update_screen() -> bool {
// when the window is closed, running is set to false
unsafe { rdp_update_screen() }
}
@@ -40,14 +83,13 @@ pub fn set_register(reg: u32, value: u32) {
}
}
pub fn process_rdp_list(dpc_regs: &mut [u32; 8], sp_dmem: &mut [u8; 8192]) -> u64 {
unsafe { rdp_process_commands(dpc_regs, sp_dmem) }
pub fn process_rdp_list() -> u64 {
unsafe { rdp_process_commands() }
}
pub fn rdp_full_sync() {
pub fn full_sync() {
unsafe {
full_sync();
rdp_full_sync();
}
}