Skip to content

Commit

Permalink
Rust! (but better)
Browse files Browse the repository at this point in the history
  • Loading branch information
kosmas12 committed Apr 19, 2022
1 parent 9e9c07f commit b66ab57
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions samples/rust/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "xbox.json"
15 changes: 15 additions & 0 deletions samples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "rustnxdk"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

# Default cargo profile
[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
25 changes: 25 additions & 0 deletions samples/rust/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
PROJECT_NAME = rustnxdk
XISO_NAME = rusttest
XBE_TITLE = rusttest
NXDK_DIR = /home/$(USER)/nxdk
OUTPUT_DIR = bin
RUSTFLAGS := --target xbox.json -Zbuild-std=core

CXBE = $(NXDK_DIR)/tools/cxbe/cxbe
XISO = $(NXDK_DIR)/tools/extract-xiso/build/extract-xiso

EXEC_DIR = target/xbox/release

ifeq ($(DEBUG),y)
EXEC_DIR = target/xbox/debug
else
RUSTFLAGS += --release
endif

all:
@mkdir -p $(OUTPUT_DIR)
@cargo build $(RUSTFLAGS)
@cp $(EXEC_DIR)/$(PROJECT_NAME).exe ./main.exe
$(CXBE) -OUT:default.xbe -TITLE:$(XBE_TITLE) ./main.exe
@mv default.xbe $(OUTPUT_DIR)
$(XISO) -c $(OUTPUT_DIR) $(XISO_NAME).iso
21 changes: 21 additions & 0 deletions samples/rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate core;
use std::env;

fn main() {
let nxdk_dir = match env::var("NXDK_DIR") {
Ok(value) => value,
Err(e) => panic!("Error getting NXDK_DIR variable: {}", e)
};

println!("cargo:build-std=core");
println!("cargo:rustc-link-search={}/lib/xboxkrnl", nxdk_dir);
println!("cargo:rustc-link-lib=static=libxboxkrnl");
println!("cargo:rustc-link-search={}/lib/", nxdk_dir);
println!("cargo:rustc-link-lib=static=libpdclib");
println!("cargo:rustc-link-lib=static=libwinapi");
println!("cargo:rustc-link-lib=static=libxboxrt");
println!("cargo:rustc-link-lib=static=libnxdk_hal");



}
28 changes: 28 additions & 0 deletions samples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![no_std] // don't link the Rust standard library
#![no_main] // disable all Rust-level entry points

use core::panic::PanicInfo;

#[link (name="libnxdk_hal")]
extern "C" {
fn XVideoSetMode(_: i32, _: i32, _: i32, _:i32);
fn debugPrint(_: &str);
}

#[no_mangle]
fn main() -> i32 {
unsafe {
// 640x480, 32bpp, 60Hz
XVideoSetMode(640, 480, 32, 60);
debugPrint("Hello from Rust");
}
loop {

}
}

// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
32 changes: 32 additions & 0 deletions samples/rust/xbox.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"abi-return-struct-as-int": true,
"arch": "x86",
"cpu": "pentium3",
"data-layout": "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32-a:0:32-S32",
"dll-prefix": "",
"dll-suffix": ".dll",
"env": "msvc",
"exe-suffix": ".exe",
"executables": true,
"is-like-msvc": false,
"is-like-windows": true,
"linker-flavor": "lld-link",
"linker-is-gnu": false,
"lld-flavor": "link",
"llvm-target": "i386-pc-win32",
"max-atomic-width": 64,
"os": "none",
"pre-link-args": {
"lld-link": [
"-subsystem:windows",
"-fixed",
"-base:0x00010000",
"-stack:65536",
"-merge:.edata=.edataxb"
]
},
"requires-uwtable": true,
"target-pointer-width": "32",
"target-family": "windows",
"vendor": "pc"
}

0 comments on commit b66ab57

Please sign in to comment.