Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start work on hacky Xbox port #43

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
801510d
Lower memory requirements
JayFoxRox Nov 9, 2018
f5aec0f
Avoid invalid memory access with icon names
JayFoxRox Nov 10, 2018
f262c43
Stub Xbox; start uc_native for some i386 hosts
JayFoxRox Nov 6, 2018
1a9c8df
Support native code execution
JayFoxRox Nov 8, 2018
197b5ff
add sw renderer + works on xbox (but out of mem)
JayFoxRox Nov 9, 2018
749a52e
fixup: Use proper _WIN32 check for aligned_malloc
JayFoxRox Jun 6, 2019
cde4283
Use smaller stack (hack: start stack in center)
JayFoxRox Jun 6, 2019
3d9a274
hack: Only dummy heap page
JayFoxRox Jun 6, 2019
4ab42d6
Support native malloc/free
JayFoxRox Jun 6, 2019
51b1a8e
Support identity map in native map (FIXME: probably should be fixup?)
JayFoxRox Jun 6, 2019
9089fb9
Free memory from HeapFree
JayFoxRox Jun 6, 2019
8ec3d78
Support i386 linux again
JayFoxRox Nov 10, 2018
91299bd
Little cleanup
JayFoxRox Nov 10, 2018
ff45f36
Report broken address mappings
JayFoxRox Nov 10, 2018
81e6d75
Make logs more similar and smaller memory changes
JayFoxRox Nov 10, 2018
eb94649
hack: Use safe stack region
JayFoxRox Jun 6, 2019
713c941
Swap host and guest FS
JayFoxRox Jun 6, 2019
43e2471
hack: Print memory stats
JayFoxRox Jun 6, 2019
dbcb69a
Set stdout and stderr to unbuffered
JayFoxRox Jun 6, 2019
2b56f8f
fixup: set sdl keys to zero in xbox
JayFoxRox Jun 6, 2019
4d22fd3
Use host mapping
JayFoxRox Nov 11, 2018
75b76b8
Keep copy of EFLAGS and FPU (should fix stability issues)
JayFoxRox Nov 11, 2018
c4154dd
Use custom allocator in CreateHlt
JayFoxRox Nov 12, 2018
9fbcf1a
Map memory in LoadSection
JayFoxRox Nov 12, 2018
fbe1308
Some better printf debugging
JayFoxRox Nov 13, 2018
0974f6c
Allow non-native compilation again
JayFoxRox Nov 15, 2018
c82db03
CMake build system hacks?
JayFoxRox Jun 8, 2019
754fda6
Lower file handle count (FIXME: move to other branch + set higher)
JayFoxRox Jun 8, 2019
a8eefa0
Assert on MapMemory error
JayFoxRox Jun 8, 2019
5ae17a0
Fixups for xbox.c stubs
JayFoxRox Jun 8, 2019
88153c7
Do reference-counting
JayFoxRox Jun 8, 2019
1448f12
Use PBOs
JayFoxRox Jun 8, 2019
ce499f9
hacks: Weird changes to memory-statistics printer
JayFoxRox Jun 8, 2019
0cc9206
Add crash handler for Xbox
JayFoxRox Jun 8, 2019
3c5d3ea
Switch to NtAllocateVirtualMemory as default allocator on Xbox (was C…
JayFoxRox Jun 8, 2019
4ef012f
Reduce stack size to 160kiB
JayFoxRox Jun 8, 2019
977671d
Move Xbox heap to 0x83400000
JayFoxRox Jun 8, 2019
1909ba7
Increase heap size to 200MiB (FIXME: comment buggy - says 16MiB)
JayFoxRox Jun 8, 2019
1090042
fixup: ???
JayFoxRox Jun 8, 2019
a600646
fixup: Don't allocate twice?
JayFoxRox Jun 8, 2019
07bc60b
Support CANARY option
JayFoxRox Jun 8, 2019
866db36
Only check memory for NATIVE mode, and initialize memory to 0xFF
JayFoxRox Jun 8, 2019
842f283
Various hacks for NATIVE mode and emulation
JayFoxRox Jun 8, 2019
13af4c3
Only allow 1 guest thread
JayFoxRox Jun 8, 2019
a0af720
hack: Drastically remove COM object size / reuse vtables
JayFoxRox Jun 8, 2019
3a6279d
Explain error in game, which affects HeapAlloc
JayFoxRox Jun 8, 2019
871861c
Make TlsGetValue silent
JayFoxRox Jun 8, 2019
266fa67
Reduce COM object vtable sizes
JayFoxRox Jun 8, 2019
fa21a44
hack: Patch bugs in webdemo
JayFoxRox Jun 8, 2019
662d5b7
Disable uc_reg_write to certain registers in NATIVE mode
JayFoxRox Jun 8, 2019
3bb9841
Attempt to reach compatibility with upstream nxdk (untested)
JayFoxRox May 18, 2019
677e343
More hacks for upstream nxdk
JayFoxRox May 21, 2019
a57bf4a
Fix out of bounds write
JayFoxRox May 21, 2019
18f8de1
Avoid _begin from being optimized away
JayFoxRox May 21, 2019
7bff6cb
Be aggressive about optimizations (untested)
JayFoxRox May 21, 2019
a65bcda
Hack for deterministic timing
JayFoxRox May 21, 2019
0c6d4c5
Some working rendering
JayFoxRox May 30, 2019
6957b66
textures wip
JayFoxRox May 31, 2019
4e5164a
Fixups: Fix native non-Xbox compilation
JayFoxRox Jun 5, 2019
0dfe700
Dummy allocator for heap
JayFoxRox Jun 6, 2019
a910671
debug.h moved
JayFoxRox Jan 4, 2020
611d03f
Doesn't work with pbkit DBG because too many commands in push
JayFoxRox Jan 4, 2020
1861559
Allocate framebuffer (so debugPrint doesn't corrupt us)
JayFoxRox Jan 4, 2020
e908bec
improve clear
JayFoxRox Jan 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
build/

# nxdk garbage
bin/
*.obj
*.d
*.iso
*.exe
*.lib
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tinyalloc"]
path = tinyalloc
url = https://github.com/thi-ng/tinyalloc
JayFoxRox marked this conversation as resolved.
Show resolved Hide resolved
49 changes: 33 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ if(ENET_FOUND)
)
endif()

message(${LIBUNICORN_INCLUDE_DIR})

include_directories(SYSTEM
${LIBUNICORN_INCLUDE_DIR}
${OPENGL_INCLUDE_DIR}
Expand All @@ -34,6 +36,7 @@ add_executable(openswe1r
emulation.c
export.c
shader.c
alloc.c

dll/kernel32.c

Expand Down Expand Up @@ -76,30 +79,44 @@ target_include_directories(openswe1r PRIVATE ${CMAKE_BINARY_DIR})

include(package.cmake)

if(USE_VM)
target_compile_definitions(openswe1r PUBLIC -DUC_KVM)
if(USE_NATIVE)

target_sources(openswe1r PUBLIC
uc_kvm.c
uc_native.c
xbox.c
)
target_compile_definitions(openswe1r PUBLIC -DUC_NATIVE -DUC_KVM)

set_target_properties(openswe1r PROPERTIES COMPILE_FLAGS "-m32 -O0" LINK_FLAGS "-m32 -static-libgcc -O0")

else()
target_link_libraries(openswe1r
${LIBUNICORN_LIBRARY}
)
endif()

if(ENET_FOUND)
target_compile_definitions(openswe1r PUBLIC -DDPLAY_ENET)
if(USE_VM)
target_compile_definitions(openswe1r PUBLIC -DUC_KVM)
target_sources(openswe1r PUBLIC
uc_kvm.c
)
else()
target_link_libraries(openswe1r
${LIBUNICORN_LIBRARY}
)
endif()

if(ENET_FOUND)
target_compile_definitions(openswe1r PUBLIC -DDPLAY_ENET)
target_link_libraries(openswe1r
${ENET_LIBRARIES}
)
endif()

target_link_libraries(openswe1r
${ENET_LIBRARIES}
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
${SDL2_LIBRARY}
${OPENAL_LIBRARY}
)
endif()

target_link_libraries(openswe1r
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
${SDL2_LIBRARY}
${OPENAL_LIBRARY}
)

if(MSVC)
# Silence MSVC CRT security warnings
Expand Down
45 changes: 45 additions & 0 deletions Makefile.nxdk
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#NXDK_SDL = y

XBE_TITLE = OpenSWE1R
GEN_XISO = $(XBE_TITLE).iso
SRCS =
#SHADER_OBJS = ps.inl vs.inl

SRCS += $(CURDIR)/xbox.c

SRCS += $(CURDIR)/main.c
SRCS += $(CURDIR)/emulation.c
SRCS += $(CURDIR)/export.c
SRCS += $(CURDIR)/shader.c

SRCS += $(CURDIR)/dll/kernel32.c

SRCS += $(CURDIR)/com/a3d.c
SRCS += $(CURDIR)/com/dplay.c

SRCS += $(CURDIR)/uc_native.c

CFLAGS += -isystem$(CURDIR)/xbox-hacks

CFLAGS += -DUC_NATIVE=1 -DUC_KVM=1
CFLAGS += -I/usr/include/unicorn
CFLAGS += -I/usr/include/AL
CFLAGS += -DXBOX -DAPIENTRY= -DGLAPI=

SDL_DIR = $(NXDK_DIR)/lib/sdl/SDL2
CFLAGS += -I$(SDL_DIR)/include

CFLAGS += -Wno-inconsistent-dllimport

#FIXME: Slow mode for pbkit! remove!
CFLAGS += -DXBOX_GPU=1
#CFLAGS += -DDBG=1 #FIXME: !!!

# We are short on memory, so reduce binary footprint
#CFLAGS += -Os
#FIXME: At least one of these is bad: -fno-stack-protector -fomit-frame-pointer -ffunction-sections -fdata-sections
#NXDK_STACKSIZE=16384

SHADER_OBJS = xbox-hacks/ps.inl xbox-hacks/vs.inl

include $(NXDK_DIR)/Makefile
138 changes: 138 additions & 0 deletions alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2019 OpenSWE1R Maintainers
// Licensed under GPLv2 or any later version
// Refer to the included LICENSE.txt file.

// This is a temporary placeholder for a memory allocator.

#include "alloc.h"

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

static unsigned int block_count = 0;
static uint32_t block_size = 0;
static uint8_t* block_usage = NULL;

static void mark_used(unsigned int block, bool used) {
uint8_t mask = 1 << (block % 8);
if (used) {
block_usage[block / 8] |= mask;
} else {
block_usage[block / 8] &= ~mask;
}
}

static bool is_used(unsigned int block) {
uint8_t mask = 1 << (block % 8);
return block_usage[block / 8] & mask;
}

static unsigned int get_block_count(uint32_t size) {
return (size + block_size - 1) / block_size;
}

static unsigned int count_blocks(unsigned int block, bool used) {
unsigned int count = 0;
while(block < block_count) {
if (is_used(block) != used) {
break;
}
block++;
count++;
}
return count;
}

static unsigned int find_free_blocks(unsigned int needed_blocks) {

unsigned int best_count = 0;
unsigned int best_block = 0;

unsigned int block = 0;
while(block < block_count) {

bool block_used = is_used(block);
unsigned int available_blocks = count_blocks(block, block_used);

// Only look at unused blocks that are large enough
if (!block_used && (available_blocks >= needed_blocks)) {

// Check if we don't have a result yet, or if this is a better fit
if ((best_count == 0) || (available_blocks < best_count)) {

// Update result
best_block = block;
best_count = available_blocks;

// If we have an exact fit, use that
if (best_count == needed_blocks) {
break;
}

}
}

block += available_blocks;
}

// Assert that we have a result
assert(best_count != 0);

// Returns best block address
return best_block;
}

void alloc_initialize(uint32_t size, uint32_t _block_size) {

// Sanity check arguments, we expect proper alignment
assert((size % _block_size) == 0);

// Store settings
block_size = _block_size;
block_count = get_block_count(size);

// Allocate flags which tell us wether a block is used
size_t used_flag_size = (block_count + 7) / 8;
block_usage = malloc(used_flag_size);
memset(block_usage, 0x00, used_flag_size);

}

uint32_t alloc_allocate(uint32_t size) {

// Don't allow zero-size allocations (breaks algorithm)
assert(size > 0);

unsigned int needed_blocks = get_block_count(size);

//FIXME: `+ 2` is a hack, so we can recognize block splits
unsigned int block = find_free_blocks(needed_blocks + 2) + 1;

printf("Allocated %u blocks (0x%X / 0x%X bytes)\n", needed_blocks, needed_blocks * block_size, size);

for(unsigned int i = 0; i < needed_blocks; i++) {
mark_used(block + i, true);
}

return block * block_size;
}

void alloc_free(uint32_t offset) {

// Only free addresses that are block-aligned
assert(offset % block_size == 0);
unsigned int block = offset / block_size;

unsigned int used_blocks = count_blocks(block, true);

// Avoid double-free
assert(used_blocks > 0);

for(unsigned int i = 0; i < used_blocks; i++) {
mark_used(block + i, false);
}

}
5 changes: 5 additions & 0 deletions alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdint.h>

void alloc_initialize(uint32_t size, uint32_t _block_size);
uint32_t alloc_allocate(uint32_t size);
void alloc_free(uint32_t offset);
10 changes: 5 additions & 5 deletions cmake/FindUnicorn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# LIBUNICORN_LIBRARY

find_path(LIBUNICORN_INCLUDE_DIR
unicorn/unicorn.h
unicorn.h
HINTS $ENV{UNICORNDIR}
PATH_SUFFIXES include)
PATH_SUFFIXES include/unicorn include)

find_library(LIBUNICORN_LIBRARY
NAMES unicorn
HINTS $ENV{UNICORNDIR})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(unicorn DEFAULT_MSG
LIBUNICORN_LIBRARY LIBUNICORN_INCLUDE_DIR)
mark_as_advanced(LIBUNICORN_INCLUDE_DIR LIBUNICORN_LIBRARY)

find_package_handle_standard_args(unicorn
REQUIRED_VARS LIBUNICORN_LIBRARY LIBUNICORN_INCLUDE_DIR)
4 changes: 2 additions & 2 deletions com/a3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


typedef struct {
void* vtable;
API(IUnknown) iunknown;
ALCdevice *device;
ALCcontext *context;
} A3d4;
Expand All @@ -32,7 +32,7 @@ typedef struct {
} API(WAVEFORMATEX);

typedef struct {
void* vtable;
API(IUnknown) iunknown;
ALuint al_source;
ALuint al_buffer;
API(WAVEFORMATEX) fmt;
Expand Down
2 changes: 1 addition & 1 deletion com/d3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../windows.h"

typedef struct {
void* vtable;
API(IUnknown) iunknown;
Address surface;
GLuint handle;
} API(Direct3DTexture2);
Expand Down
3 changes: 2 additions & 1 deletion com/ddraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ enum {
};

typedef struct {
void* vtable;
API(IUnknown) iunknown;
Address texture; // Direct3DTexture2*
API(DDSURFACEDESC2) desc;
GLuint pbo;
} API(DirectDrawSurface4);

#endif
Loading