From 1f8f4e05410df148b7f3c3fd35f85c9b37d0401f Mon Sep 17 00:00:00 2001 From: Dario Bartussek Date: Mon, 19 Apr 2021 13:50:42 +0200 Subject: Simple hello world program using libctru --- .gitignore | 2 ++ Cargo.toml | 7 ++++--- build.rs | 7 ------- compile.sh | 6 ++++-- package.sh | 2 +- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 18 ------------------ src/start.s | 22 ---------------------- 8 files changed, 57 insertions(+), 53 deletions(-) delete mode 100644 build.rs create mode 100644 src/lib.rs delete mode 100644 src/main.rs delete mode 100644 src/start.s diff --git a/.gitignore b/.gitignore index 4278b3b..d41151c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ Cargo.lock +rust_3ds.elf + rust_3ds.map rust_3ds.s diff --git a/Cargo.toml b/Cargo.toml index 4d2f3dd..e2fa275 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,9 @@ authors = ["Dario Bartussek "] edition = "2018" license = "MIT OR Apache-2.0" +[lib] +crate-type = ["staticlib"] + [profile.dev] panic = "abort" opt-level = "s" @@ -13,7 +16,5 @@ opt-level = "s" panic = "abort" opt-level = "s" -[build-dependencies] -cc = "1.0.67" - [dependencies] +libctru = { git = "https://github.com/dbartussek/libctru-rs.git" } diff --git a/build.rs b/build.rs deleted file mode 100644 index 5a33378..0000000 --- a/build.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - cc::Build::new() - .target("arm-none-eabihf") - .compiler("clang") - .file("src/start.s") - .compile("start"); -} diff --git a/compile.sh b/compile.sh index ac7ae71..c47a7b4 100755 --- a/compile.sh +++ b/compile.sh @@ -1,5 +1,7 @@ cargo fmt -RUSTFLAGS="-C link-args=-T3dsx.ld -C link-args=-Map=rust_3ds.map" cargo xbuild --target 3ds.json +cargo xbuild --target 3ds.json -llvm-objdump -d -C target/3ds/debug/rust_3ds > rust_3ds.s +arm-none-eabi-gcc -specs=3dsx.specs -g -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft -Wl,-Map,rust_3ds.map -Ltarget/3ds/debug -LC:/devkitpro/libctru/lib -lrust_3ds -lctru -lm -o rust_3ds.elf + +llvm-objdump -d -C rust_3ds.elf > rust_3ds.s diff --git a/package.sh b/package.sh index 0446af3..b0f7221 100755 --- a/package.sh +++ b/package.sh @@ -1,4 +1,4 @@ . compile.sh smdhtool --create "rust_3ds" "Description" "Author" "./icon.png" rust_3ds.smdh -3dsxtool ./target/3ds/debug/rust_3ds rust_3ds.3dsx --smdh=rust_3ds.smdh +3dsxtool ./rust_3ds.elf rust_3ds.3dsx --smdh=rust_3ds.smdh diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1970dfd --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,46 @@ +#![no_std] + +use core::{panic::PanicInfo, ptr::null_mut}; +use libctru::raw::{ + aptMainLoop, c_char, c_int, consoleInit, gfxExit, gfxFlushBuffers, gfxInitDefault, + gfxScreen_t_GFX_TOP, gfxSwapBuffers, gspWaitForEvent, hidKeysDown, hidScanInput, + GSPGPU_Event_GSPGPU_EVENT_VBlank0, KEY_START, +}; + +extern "C" { + fn puts(string: *const c_char); +} + +#[no_mangle] +pub extern "C" fn main(_argc: c_int, _argv: *const *const c_char) -> c_int { + unsafe { + gfxInitDefault(); + + consoleInit(gfxScreen_t_GFX_TOP, null_mut()); + + puts("Hello World from Rust".as_ptr()); + + while aptMainLoop() { + hidScanInput(); + let keys = hidKeysDown(); + + if keys & (KEY_START as u32) != 0 { + break; + } + + gfxFlushBuffers(); + gfxSwapBuffers(); + + gspWaitForEvent(GSPGPU_Event_GSPGPU_EVENT_VBlank0, true); + } + + gfxExit(); + } + + 0 +} + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 6133bfd..0000000 --- a/src/main.rs +++ /dev/null @@ -1,18 +0,0 @@ -#![feature(asm)] -#![no_std] -#![no_main] - -use core::panic::PanicInfo; - -#[no_mangle] -pub extern "C" fn pre_main() -> ! { - unsafe { - core::ptr::null::().read_volatile(); - } - loop {} -} - -#[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - loop {} -} diff --git a/src/start.s b/src/start.s deleted file mode 100644 index 158feef..0000000 --- a/src/start.s +++ /dev/null @@ -1,22 +0,0 @@ -.cpu mpcore -.section ".crt0","ax" - -.align 2 -.arm - -_start: - b pre_main - -.ascii "_prm" -__service_ptr: - .word 0 -__apt_appid: - .word 0x300 -__heap_size: - .word 24*1024*1024 -__linear_heap_size: - .word 32*1024*1024 -__system_arglist: - .word 0 -__system_runflags: - .word 0 -- cgit v1.2.3