Initial rough rust integration

This commit is contained in:
Travis Burtrum 2021-10-25 01:59:55 -04:00
parent ba432bb8c8
commit dd5d9d0bc0
11 changed files with 122 additions and 11 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
*.o
*.bin
*.dfs
*/target/
## OSX junk
.DS_Store

View File

@ -19,9 +19,9 @@ OBJDIR = ./obj
BINDIR = ./bin
TOOLSDIR = ./tools
LINK_FLAGS = -O1 -L$(ROOTDIR)/lib -L$(ROOTDIR)/mips64-elf/lib -ldragon -lmad -lyaml -lc -lm -ldragonsys -lnosys $(LIBS) -Tn64ld.x
LINK_FLAGS = -O1 -L$(ROOTDIR)/lib -L$(ROOTDIR)/mips64-elf/lib -ldragon -lmad -lyaml -lc -lm -ldragonsys -lnosys -L./rust/target/mips-nintendo64-none/release -laltra64 $(LIBS) -Tn64ld.x
PROG_NAME = OS64P
CFLAGS = -std=gnu99 -march=vr4300 -mtune=vr4300 -O1 -I$(INCDIR) -I$(ROOTDIR)/include -I$(ROOTDIR)/mips64-elf/include -lpthread -lrt -D_REENTRANT -DUSE_TRUETYPE $(SET_DEBUG)
CFLAGS = -std=gnu99 -march=vr4300 -mtune=vr4300 -O1 -I$(INCDIR) -I$(ROOTDIR)/include -I$(ROOTDIR)/mips64-elf/include -I./rust/target/mips-nintendo64-none/release -lpthread -lrt -D_REENTRANT -DUSE_TRUETYPE $(SET_DEBUG)
ASFLAGS = -mtune=vr4300 -march=vr4300
CC = $(GCCN64PREFIX)gcc
AS = $(GCCN64PREFIX)as
@ -37,11 +37,17 @@ $(PROG_NAME).v64: $ $(PROG_NAME).elf $(PROG_NAME).dfs
$(N64TOOL) -l 4M -t $(HEADERTITLE) -h $(RESDIR)/$(HEADERNAME) -o $(BINDIR)/$(PROG_NAME).v64 $(BINDIR)/$(PROG_NAME).bin -s 1M $(BINDIR)/$(PROG_NAME).dfs
$(CHKSUM64PATH) $(BINDIR)/$(PROG_NAME).v64
$(PROG_NAME).elf : $(OBJECTS)
./rust/target/mips-nintendo64-none/release/libaltra64.a:
cd rust && cargo build --release --verbose --target mips-nintendo64-none.json -Z build-std=core
./rust/target/mips-nintendo64-none/release/altra64.h:
cd rust && cbindgen -o ./target/mips-nintendo64-none/release/altra64.h
$(PROG_NAME).elf : $(OBJECTS) ./rust/target/mips-nintendo64-none/release/libaltra64.a
@mkdir -p $(BINDIR)
$(LD) -o $(BINDIR)/$(PROG_NAME).elf $(OBJECTS) $(LINK_FLAGS)
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c ./rust/target/mips-nintendo64-none/release/altra64.h
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
@ -59,3 +65,4 @@ debug: SET_DEBUG=-DDEBUG
clean:
rm -f $(BINDIR)/*.v64 $(BINDIR)/*.elf $(OBJDIR)/*.o $(BINDIR)/*.bin $(BINDIR)/*.dfs
rm -rf ./rust/target

View File

@ -14,7 +14,7 @@ The original version overwrote 1 save file per game on system reset, but if you
If you want to build the menu, you need an n64 toolchain. This is terrible to build, I ended up creating a Dockerfile in the docker folder, instructions included in it.
Or if you trust me, you can use the one I built and pushed to docker hub, [moparisthebest/altra64-dev](https://hub.docker.com/r/moparisthebest/altra64-dev)
Or if you trust me, you can use the one I built and pushed to docker hub, [moparisthebest/altra64-rust-dev](https://hub.docker.com/r/moparisthebest/altra64-rust-dev)
### Build `Altra64`
@ -23,7 +23,7 @@ To build the Rom
from the projects root directory, with docker installed
```
$ docker run --rm -v "$(pwd):/build" moparisthebest/altra64-dev make
$ docker run --rm -v "$(pwd):/build" moparisthebest/altra64-rust-dev make
```
If it all worked, you will find `OS64.v64` in the `bin` directory.
@ -34,7 +34,7 @@ Finally, we can clean the build objects from the project
from the projects root directory
```
$ docker run --rm -v "$(pwd):/build" moparisthebest/altra64-dev make clean
$ docker run --rm -v "$(pwd):/build" moparisthebest/altra64-rust-dev make clean
```
Enjoy!

7
rust/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "altra64"
version = "0.1.0"

18
rust/Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "altra64"
version = "0.1.0"
authors = ["moparisthebest <admin@moparisthebest.com>"]
# the profile used for `cargo build`
[profile.dev]
panic = "abort" # disable stack unwinding on panic
lto = "off"
# the profile used for `cargo build --release`
[profile.release]
panic = "abort" # disable stack unwinding on panic
lto = "off"
[lib]
name = "altra64"
crate-type = ["staticlib"]

16
rust/Dockerfile Normal file
View File

@ -0,0 +1,16 @@
# to build and test:
# docker build -t altra64-rust-dev . && docker run --rm -v "$HOME/.cargo/registry:/root/.cargo/registry" -v "$(pwd):/build" -it altra64-rust-dev
# to use to compile altra64 (or other n64 stuff I guess)
# docker run --rm -v "$HOME/.cargo/registry:/root/.cargo/registry" -v "$(pwd):/build" -it altra64-rust-dev ./build.sh
FROM moparisthebest/altra64-dev
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/libdragon/bin:/root/.cargo/bin
RUN apt-get -y install curl && \
curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path --profile minimal --default-toolchain nightly --component rust-src && \
apt-get -y purge curl && \
cargo install cbindgen && \
rm -rf ~/.cargo/registry/

17
rust/cbindgen.toml Normal file
View File

@ -0,0 +1,17 @@
# This is a template cbindgen.toml file with all of the default values.
# Some values are commented out because their absence is the real default.
#
# See https://github.com/eqrion/cbindgen/blob/master/docs.md#cbindgentoml
# for detailed documentation of every option here.
language = "C"
############## Options for Wrapping the Contents of the Header #################
# header = "/* Text to put at the beginning of the generated file. Probably a license. */"
# trailer = "/* Text to put at the end of the generated file */"
include_guard = "ALTRA64_H"
#pragma_once = true
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
include_version = false

View File

@ -0,0 +1,25 @@
{
"arch": "mips",
"cpu": "mips3",
"data-layout": "E-m:m-p:64:64-i8:8:32-i16:16:32-i64:64-n64-S64",
"disable-redzone": true,
"env": "unknown",
"executables": true,
"features": "+mips3,+gp64,+noabicalls",
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"llvm-target": "mips-unknown-unknown",
"llvm-abiname": "n64",
"os": "none",
"panic-strategy": "abort",
"pre-link-args": {
"ld.lld": [
"--script={}"
]
},
"relocation-model": "static",
"target-c-int-width": "64",
"target-endian": "big",
"target-pointer-width": "64",
"vendor": "nintendo64"
}

15
rust/src/lib.rs Normal file
View File

@ -0,0 +1,15 @@
#![no_std]
use core::panic::PanicInfo;
/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
#[no_mangle]
pub extern "C" fn get_rust_u8() -> u8 {
9
}

View File

@ -40,6 +40,8 @@
// YAML parser
#include <yaml.h>
#include <altra64.h>
#include "constants.h"
#include "debug.h"
#include "mem.h"
@ -1751,9 +1753,9 @@ int saveTypeToSd(display_context_t disp, char *rom_name, int stype)
if (result == FR_OK)
{
//for savegame
uint8_t cartsave_data[size]; //TODO: bring back old initialisation if this doesn't work
uint8_t cartsave_data[size];
// zero the memory so we can detect+abort if we can't get a save from the cart
memset(cartsave_data, 0, size * sizeof(cartsave_data[0]));
TRACEF(disp, "cartsave_data=%p", &cartsave_data);

View File

@ -1,6 +1,9 @@
#include <libdragon.h>
#include <stdio.h>
#include <altra64.h>
#include "types.h"
#include "menu.h"
#include "version.h"
@ -13,7 +16,7 @@ void menu_about(display_context_t disp)
char version_str[32];
char firmware_str[32];
sprintf(version_str, "Altra64: v%s", Altra64_GetVersionString());
sprintf(version_str, "Altra64 %d: v%s", get_rust_u8(), Altra64_GetVersionString());
printText(version_str, 9, 8, disp);
sprintf(firmware_str, "ED64 firmware: v%03x", evd_getFirmVersion());
printText(firmware_str, 9, -1, disp);