Reviving the language that brought us the Jak & Daxter Series
Go to file
water111 d6e82eedb0
[decomp] fix and decomp part tester (#935)
* temp

* update tests
2021-10-23 20:15:31 -04:00
.github ci: Add buildcache to all CI builds (#828) 2021-09-06 14:29:34 -04:00
.vs PAL jak 1 decompiler profile (#891) 2021-10-15 18:17:51 -04:00
.vscode decomp: finish _almost all of_ the remaining camera code (#845) 2021-10-16 21:01:23 -04:00
assets Update documentation and clean up (#129) 2020-11-21 12:52:38 -05:00
bin ci: Add buildcache to all CI builds (#828) 2021-09-06 14:29:34 -04:00
common decomp more level stuff (#932) 2021-10-23 16:00:49 -04:00
decompiler [decomp] fix and decomp part tester (#935) 2021-10-23 20:15:31 -04:00
decompiler_out add decompiler 2020-08-22 23:30:17 -04:00
docs Updated github pages site 2021-10-23 20:02:20 +00:00
game [decomp] fix and decomp part tester (#935) 2021-10-23 20:15:31 -04:00
goal_src [decomp] fix and decomp part tester (#935) 2021-10-23 20:15:31 -04:00
goalc decomp more level stuff (#932) 2021-10-23 16:00:49 -04:00
iso_data check in existing work 2020-08-22 22:30:12 -04:00
log Typo fixes & Windows QoL changes (#189) 2021-01-10 10:39:32 -05:00
out Support dir tpages (#671) 2021-07-02 14:50:58 -04:00
resources memory cards (in progress) (#868) 2021-10-01 23:12:34 -04:00
scripts [decompiler] jak 1 PAL demo support + couple fixes (#934) 2021-10-23 19:03:19 -04:00
test [decomp] fix and decomp part tester (#935) 2021-10-23 20:15:31 -04:00
third-party [decomp] load boundaries (#922) 2021-10-20 19:49:32 -04:00
tools [decomp] add mips2c converter (#842) 2021-09-11 20:52:35 -04:00
.clang-format check in existing work 2020-08-22 22:30:12 -04:00
.editorconfig Compiler - Implementing more VU Instructions (Part 1 of 2) (#221) 2021-02-05 15:00:17 -05:00
.gitattributes add goal enum utils to standard libs (#740) 2021-08-09 19:18:53 -04:00
.gitignore [sparticle] 2d hud particles (#849) 2021-09-26 11:41:58 -04:00
.gitmodules Implement runtime display (test) (#318) 2021-03-09 23:51:28 -05:00
.projectile add emacs and projectile configs (#528) 2021-05-25 18:53:50 -04:00
CMakeLists.txt PAL jak 1 decompiler profile (#891) 2021-10-15 18:17:51 -04:00
CMakeSettings.json [decomp] more of res + change a few macros (#527) 2021-05-25 16:36:36 -04:00
default.nix Nixpkgs support (#228) 2021-02-03 21:29:46 -05:00
flake.lock Nixpkgs support (#228) 2021-02-03 21:29:46 -05:00
flake.nix Correct Nix flake licensing & add ISC License 2021-02-11 19:29:51 -08:00
LICENSE.txt Correct Nix flake licensing & add ISC License 2021-02-11 19:29:51 -08:00
README.md PAL jak 1 decompiler profile (#891) 2021-10-15 18:17:51 -04:00
shell.nix Nixpkgs support (#228) 2021-02-03 21:29:46 -05:00
Taskfile.yml decomp: finish _almost all of_ the remaining camera code (#845) 2021-10-16 21:01:23 -04:00
test_code_coverage.sh Nixpkgs support (#228) 2021-02-03 21:29:46 -05:00
test.sh Nixpkgs support (#228) 2021-02-03 21:29:46 -05:00

Documentation Badge Linux Windows Coverage Status Codacy Badge

Table of Contents

Project Description

This project is to port Jak 1 (NTSC, "black label" version) to PC. Over 99% of this game is written in GOAL, a custom Lisp language developed by Naughty Dog. Our strategy is:

  • decompile the original game code into human-readable GOAL code
  • develop our own compiler for GOAL and recompile game code for x86-64
  • create a tool to extract game assets into formats that can be easily viewed or modified
  • create tools to repack game assets into a format that our port uses.

Our objectives are:

  • make the port a "native application" on x86-64, with high performance. It shouldn't emulated, interpreted, or transpiled.
  • Our GOAL compiler's performance should be around the same as unoptimized C.
  • try to match things from the original game and development as possible. For example, the original GOAL compiler supported live modification of code while the game is running, so we do the same, even though it's not required for just porting the game.
  • support modifications. It should be possible to make edits to the code without everything else breaking.

We support both Linux and Windows on x86-64.

Current Status

So far, we've decompiled around 130,000 lines of GOAL code, out of an estimated 500,000 total lines and we've started work on an OpenGL renderer. Currently, the main display process (*dproc*) runs and sends data to our renderer. We can load textures, text files, and level files. Using keyboard controls, we can open the debug menu and turn on some simple debug visualizations.

Video of debug menu and actor visibility: https://www.youtube.com/watch?v=0AoiYY4S7nI

To help with decompiling, we've built a decompiler that can process GOAL code and unpack game assets. We manually specify function types and locations where the original code had type casts until the decompiler succeeds, then we clean up the output of the decompiled code by adding comments and adjusting formatting, then save it in goal_src. Our decompiler is designed specifically for processing the output of the original GOAL compiler. As a result, when given correct casts, it often produces code that can be directly fed into a compiler and works perfectly. This is tested as part of our unit tests, and so far we have around 130,000 lines (220 files) that pass.

We don't save any assets from the game - you must bring your own copy of the game and use the decompiler to extract assets.

What's Next

We are focusing on three areas:

  • Continue decompilation of GOAL code. Recently we have started on the "gameplay" code for creatures and objects in the game world, which is going pretty fast.
  • Improve the decompiler. We are always finding new features in the GOAL language.
  • Investigate more complicated renderers. The font and debug rendering is much simpler than the highly optimized renderers used for characters and backgrounds. We are starting to look at some of these renderers in detail.

Getting Started - Linux (Ubuntu)

Install packages and init repository:

sudo apt install gcc make cmake build-essential g++ nasm clang-format
git submodule update --init --recursive

Compile:

cmake -B build && cmake --build build -j 8

Run tests:

./test.sh

Note: we have found that clang and lld are significantly faster to compile and link than gcc, generate faster code, and have better warning messages. To install these:

sudo apt install lld clang

and run cmake (in a fresh build directory) with:

cmake -DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=lld" -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ..

this decreases the compile and link time from ~10 seconds to ~4 seconds.

Getting Started - Linux (Arch)

Install packages and init repository:

sudo pacman -S gcc make cmake base-devel g++ nasm
git submodule update --init --recursive

Compile:

cmake -B build && cmake --build build -j 8

Run tests:

./test.sh

Getting Started - Nixpkgs

If your Nix supports flakes:

nix develop # development environment
nix build # package
nix develop '.#jak-asan-dev' # development environment with Clang
nix build '.#jak-asan' # package with Clang ASan build

Otherwise, with traditional Nix:

nix-shell # development environment
nix-build # package
nix-shell -A packages.x86_64-linux.jak-asan-dev # development environment with Clang
nix-build -A packages.x86_64-linux.jak-asan # package with Clang ASan build

Getting Started - Windows

Install Visual Studio 2019 and get the C++ and CMake tools via the Visual Studio Installer

On Windows, it's recommended to get Scoop to use as a package manager, making the follow steps much easier. Follow the steps on the bottom of the homepage here https://scoop.sh/

Once Scoop is installed, run the following command:

scoop install llvm nasm

Initialize the repository's third-party dependencies:

git submodule update --init --recursive

Open the project as a CMake project, browse for the root level CMakeLists.txt:

In the toolbar, you should be able to select an individual component to compile, or combine within the root CMakeLists.txt. In the future we will pre-define configurations to make this easier.

You may also wish to view the files that pertain to each CMake target, rather than the project as it is normally:

Building and Running the Game

Getting a running game involves 4 steps:

  1. Build C++ tools (follow steps above)
  2. Extract assets from game
  3. Build game
  4. Run game

Extract Assets

Running the decompiler on the entire game is slow and not needed, so it is recommended to just run it on data. Edit decompiler/config/jak1-ntsc_black_label.jsonc and disable decompile_code.

  "decompile_code": false, // change this to false, don't decompile code

Place a copy of the game's files in iso_data, then run the decompiler with the scripts/decomp.sh script.

Build Game

Run the OpenGOAL compiler build/goalc/goalc. Enter (mi) to build the "iso" target, which contains everything we have so far.

Run Game

In a separate terminal, start the runtime with build/game/gk -fakeiso -debug. Then, in the OpenGOAL window, run (mi) to create the data for the game and give the REPL information for running code, (lt) to connect, (lg) to load the game engine and (test-play) to start the game engine. If it all works right, it will look something like this:

g > (lt)
[Listener] Socket connected established! (took 0 tries). Waiting for version...
Got version 0.8 OK!
[Debugger] Context: valid = true, s7 = 0x147d24, base = 0x2123000000, tid = 2438049

gc> (lg)
10836466        #xa559f2              0.0000        ("game" "kernel")

gc> (test-play)
(play :use-vis #t :init-game #f) has been called!
0        #x0              0.0000        0

gc> 

Then, in the graphics window, you can use the period key to bring up the debug menu.

Check out the pc_debug and examples folder under goal_src for some examples of GOAL code we wrote. They have instructions for how to run them.

Project Layout

There are four main components to the project.

The first is goalc, which is a GOAL compiler for x86-64. Our implementation of GOAL is called OpenGOAL. All of the compiler source code is in goalc. To run the compiler on Linux, there is a script gc.sh. The compiler is controlled through a prompt which can be used to enter commands to compile, connect to a running GOAL program for interaction, run the OpenGOAL debugger, or, if you are connected to a running GOAL program, can be used as a REPL to run code interactively. In addition to compiling code files, the compiler has features to pack and build data files.

The second component to the project is the decompiler. You must have a copy of the PS2 game and place all files from the DVD into the iso_data folder. Then run decomp.sh (Linux) to run the decompiler. For Windows, it is the decomp-jak1.bat file, and it wants your game's DVD files in a jak1 folder inside iso_data. The decompile will extract assets to the assets folder. These assets will be used by the compiler when building the port, and you may want to turn asset extraction off after running it once. The decompiler will output code and other data intended to be inspected by humans in the decompiler_out folder. Stuff in this folder will not be used by the compiler.

The third is the game source code, written in OpenGOAL. This is located in goal_src. All GOAL and GOOS code should be in this folder. Right now most of this is placeholders, but you can take a look at kernel/gcommon.gc or goal-lib.gc to see some in-progress source code.

The final component is the "runtime", located in game. This is the part of the game that's written in C++. In the port, that includes:

  • The "C Kernel", which contains the GOAL linker and some low-level GOAL language features. GOAL has a completely custom dynamically linked object file format so in order to load the first GOAL code, you need a linker written in C++. Some low-level functions for memory allocation, communicating with the I/O Processor, symbol table, strings, and the type system are also implemented in C, as these are required for the linker. It also listens for incoming messages from the compiler and passes them to the running game. This also initializes the game, by initializing the PS2 hardware, allocating the GOAL heaps, loading the GOAL kernel off of the DVD, and executing the kernel dispatcher function. This is in the game/kernel folder. This should be as close as possible to the game, and all differences should be noted with a comment.

  • Implementation of Sony's standard library. GOAL code can call C library functions, and Naughty Dog used some Sony library functions to access files, memory cards, controllers, and communicate with the separate I/O Processor. The library functions are in game/sce. Implementations of library features specific to the PC port are located in game/system.

  • The I/O Processor driver, Overlord. The PS2 had a separate CPU called the I/O Processor (IOP) that was directly connected to the DVD drive hardware and the sound hardware. Naughty Dog created a custom driver for the IOP that handled streaming data off of the DVD. It is much more complicated than I first expected. It's located in game/overlord. Like the C kernel, we try to keep this as close as possible to the actual game.

  • Sound Code. Naughty Dog used a third party library for sound. We have not started on this yet.

  • PC specific graphics stuff. We have not started on this yet.

Directory Layout

  • .github: GitHub actions CI setup
  • .vs: Visual Studio project configurations
  • assets: extracted assets (textures, translated game text) generated by the decompiler. Not included in the repository. To be used when building the PC port.
  • build: C++ CMake build folder
  • common: common C++ code shared between the compiler, decompiler, and game.
    • audio: tools for decoding the audio files
    • cross_os_debug: platform-independent library for implementing the OpenGOAL debugger. Linux-only currently
    • cross_sockets: platform-independent library for sockets. Used to connect the compiler to a running game. Linux and Windows.
    • goos: the compiler-time macro language and parser for OpenGOAL.
    • type_system: the OpenGOAL type system
    • texture: texture unpacking and format conversion
    • util, math, log: Random utility functions for accessing files, timers, etc.
  • decompiler: Source code for the decompiler
    • analysis: analysis algorithms
    • config: JSON config files for the decompiler and type definition file.
    • data: utilities to extract assets from the game
    • Disasm: MIPS disassembler
    • Function: Tools for analyzing GOAL functions
    • gui: an early prototype of a Python GUI for reading the output of the decompiler
    • IR2: the "Intermediate Representation" for GOAL functions and expressions
    • ObjectFile: Utilities for processing the GOAL object file format.
    • scripts: Useful scripts for setting up the decompilation
    • util: random utilities
    • VuDisasm: disassembler for VU code
  • decompiler_out: output of the decompiler that's not automatically used by the compiler. This is for humans to read and use. Not included in the repository.
  • docs: more documentation!
  • game: the source code for the game executable
    • common: shared stuff between the kernel (EE) and overlord (IOP)
    • graphic: PC Port graphics
    • kernel: the part of the GOAL kernel written in C. The entry point for the game is in kboot.cpp.
    • overlord: the I/O processor driver used to get data off of the DVD
    • sce: the Sony library implementation
    • system: PC-port specific OS-level stuff, like file I/O, threads, controllers, debug network connection
  • goal_src: The GOAL code for the game. It's mostly empty now.
    • build: info related to the GOAL build system.
    • engine: the game engine
    • kernel: The GOAL kernel
    • levels: Level specific code.
  • goalc: The OpenGOAL compiler
    • compiler: The implementation of the OpenGOAL language
    • data_compiler: Tools for packing data
    • debugger: The OpenGOAL debugger (part of the compiler)
    • emitter: x86-64 emitter and object file generator
    • listener: The OpenGOAL listener, which connects the compiler to a running GOAL program for the interactive REPL
    • make: The OpenGOAL build system, builds both code and data files
    • regalloc: Register allocator
  • iso_data:
  • out: Outputs from the build process. Only the iso subfolder should contain assets used by the game.
    • iso: Final outputs that are used by the game.
    • obj: Object files generated by the compiler.
  • resources: To be removed. Contains fake versions of some files required to get things booting.
  • scripts: Utility scripts. Windows-specific batch files are in a batch folder while Unix shell scripts are in a shell folder.
  • test: Unit tests (run on GitHub Actions)
  • third-party: Third party libraries
    • CMake Code Coverage. For code coverage statistics on GitHub builds
    • fmt. String formatting library
    • googletest: Test framework
    • inja: templating library used for generating test code for compiler tests
    • lzokay: decompression code for Jak 2 and later DGOs
    • mman: Windows library used to emulate mmap on Linux
    • run-clang-format: Utility to check and enforce code formatting
    • run-clang-tidy
    • zydis: x86-64 disassembler used in the OpenGOAL debugger
    • json: A JSON library
    • replxx: Used for the REPL input. Supports history and useful editing shortcuts.
    • svpng: Save a PNG file

On Windows / Visual Studio

Until 16.9 Preview 4, when attaching a debugger to the ASan build, you must disable breaking on Win32 Access Violation exceptions. See the relevant section Debugging - Exceptions here https://devblogs.microsoft.com/cppblog/asan-for-windows-x64-and-debug-build-support/#known-issues