diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index 6676277..b3cbfcb 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -1,30 +1,30 @@ /* * C bindings generation: + * bindgen --match=file.h --use-core --ctypes-prefix=libc -- --sysroot=$DEVKITARM/arm-none-eabi -I$CTRULIB/include $CTRULIB/include/3ds.h + * * bindgen --sysroot=$DEVKITARM/arm-none-eabi -I$CTRULIB/include $CTRULIB/include/3ds.h */ #![no_std] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] -#![allow(overflowing_literals)] +#![feature(question_mark)] +#![allow(non_camel_case_types, non_snake_case, overflowing_literals)] pub mod console; pub mod env; pub mod gfx; pub mod gpu; pub mod ipc; -pub mod lock; -pub mod libc; pub mod os; pub mod sdmc; -pub mod srv; +pub mod services; pub mod svc; +pub mod srv; +pub mod sys; pub mod synchronization; pub mod thread; pub mod types; -pub mod services; - +pub use self::sys::*; pub use self::types::*; pub type Result = i32; diff --git a/ctru-sys/src/libc.rs b/ctru-sys/src/sys/libc.rs similarity index 88% rename from ctru-sys/src/libc.rs rename to ctru-sys/src/sys/libc.rs index 95e37db..b4d4a12 100644 --- a/ctru-sys/src/libc.rs +++ b/ctru-sys/src/sys/libc.rs @@ -7,5 +7,6 @@ pub enum c_void { } extern "C" { + pub fn abort() -> !; pub fn write(fd: i32, buf: *const c_void, count: usize) -> isize; } diff --git a/ctru-sys/src/lock.rs b/ctru-sys/src/sys/lock.rs similarity index 95% rename from ctru-sys/src/lock.rs rename to ctru-sys/src/sys/lock.rs index 6a9bd1b..dcab2d7 100644 --- a/ctru-sys/src/lock.rs +++ b/ctru-sys/src/sys/lock.rs @@ -1,7 +1,5 @@ // from devkitArm, needed for synchronization.rs to compile -//TODO: I don't even know this thing looks really spooky - pub type _LOCK_T = i32; #[repr(C)] #[derive(Copy)] diff --git a/ctru-sys/src/sys/mod.rs b/ctru-sys/src/sys/mod.rs new file mode 100644 index 0000000..ca421e7 --- /dev/null +++ b/ctru-sys/src/sys/mod.rs @@ -0,0 +1,2 @@ +pub mod lock; +pub mod libc; diff --git a/src/lib.rs b/src/lib.rs index 43d72a7..dc225c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,24 +1,21 @@ -#![feature(lang_items)] +#![feature(alloc, collections, lang_items)] #![no_std] #![crate_type = "rlib"] #![crate_name = "ctru"] extern crate ctru_sys as libctru; +extern crate alloc; +extern crate collections; + pub mod console; pub mod srv; pub mod gfx; pub mod sdmc; pub mod services; +pub mod panic; pub use srv::Srv; pub use gfx::Gfx; pub use sdmc::Sdmc; - -#[lang = "eh_personality"] -extern "C" fn eh_personality() {} -#[lang = "panic_fmt"] -fn panic_fmt() -> ! { - loop {} -} diff --git a/src/panic.rs b/src/panic.rs new file mode 100644 index 0000000..9fb3e82 --- /dev/null +++ b/src/panic.rs @@ -0,0 +1,65 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Implementation of various bits and pieces of the `panic!` macro and +//! associated runtime pieces. + +use core::fmt::{self, Display, Write}; +use core::any::Any; + +use collections::String; +use collections::boxed::Box; + +///The compiler wants this to be here. Otherwise it won't be happy. And we like happy compilers. +#[lang = "eh_personality"] +extern fn eh_personality() {} + +/// Entry point of panic from the libcore crate. +#[lang = "panic_fmt"] +extern fn panic_fmt(msg: fmt::Arguments, file: &'static str, line: u32) -> ! { + begin_panic_fmt(&msg, &(file, line)) +} + +/// The entry point for panicking with a formatted message. +/// +/// This is designed to reduce the amount of code required at the call +/// site as much as possible (so that `panic!()` has as low an impact +/// on (e.g.) the inlining of other functions as possible), by moving +/// the actual formatting into this shared place. +#[inline(never)] +#[cold] +pub fn begin_panic_fmt(msg: &fmt::Arguments, file_line: &(&'static str, u32)) -> ! { + let mut s = String::new(); + let _ = s.write_fmt(*msg); + begin_panic(s, file_line); +} + +/// This is where the main panic logic happens. +#[inline(never)] +#[cold] +pub fn begin_panic(msg: M, file_line: &(&'static str, u32)) -> ! { + use gfx::Screen; + use console::Console; + + let msg = Box::new(msg); + let (file, line) = *file_line; + + let mut error_top = Console::init(Screen::Top); + let mut error_bottom = Console::init(Screen::Bottom); + + write!(error_top, "--------------------------------------------------").unwrap(); + writeln!(error_top, "PANIC in {} at line {}:", file, line).unwrap(); + writeln!(error_top, " {}", msg).unwrap(); + write!(error_top, "\x1b[29;00H--------------------------------------------------").unwrap(); + + write!(error_bottom, "").unwrap(); + + loop {} +} diff --git a/src/sdmc.rs b/src/sdmc.rs index 5e98552..75a58a9 100644 --- a/src/sdmc.rs +++ b/src/sdmc.rs @@ -7,7 +7,7 @@ pub struct Sdmc { } impl Sdmc { - pub fn new() -> Result { + pub fn init() -> Result { unsafe { let r = sdmcInit(); if r < 0 { diff --git a/src/services/apt.rs b/src/services/apt.rs index 612fdf7..28e0500 100644 --- a/src/services/apt.rs +++ b/src/services/apt.rs @@ -55,7 +55,7 @@ pub struct Apt { } impl Apt { - pub fn new() -> Result { + pub fn init() -> Result { unsafe { let r = apt::aptInit(); if r < 0 { @@ -86,7 +86,7 @@ impl Apt { unsafe { apt::aptReturnToMenu() } } - pub fn main_loop(&mut self) -> bool { + pub fn main_loop(&self) -> bool { unsafe { match apt::aptMainLoop() { 1 => true, diff --git a/src/services/hid.rs b/src/services/hid.rs index a9e7954..1b90194 100644 --- a/src/services/hid.rs +++ b/src/services/hid.rs @@ -78,7 +78,7 @@ pub struct Hid { } impl Hid { - pub fn new() -> Result { + pub fn init() -> Result { unsafe { let r = hid::hidInit(); if r < 0 { @@ -89,7 +89,7 @@ impl Hid { } } - pub fn scan_input(&mut self) { + pub fn scan_input(&self) { unsafe { hid::hidScanInput() }; } diff --git a/src/srv.rs b/src/srv.rs index bb1bb59..9c4388c 100644 --- a/src/srv.rs +++ b/src/srv.rs @@ -7,7 +7,7 @@ pub struct Srv { } impl Srv { - pub fn new() -> Result { + pub fn init() -> Result { unsafe { let r = srvInit(); if r < 0 {