From b76009d0d5f2efc12888bd10b0404bb439797f82 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Thu, 9 Jun 2016 03:19:42 -0700 Subject: [PATCH 1/7] moved system libs to sys folder --- ctru-sys/src/lib.rs | 14 ++++++-------- ctru-sys/src/{ => sys}/libc.rs | 0 ctru-sys/src/{ => sys}/lock.rs | 2 -- ctru-sys/src/sys/mod.rs | 2 ++ 4 files changed, 8 insertions(+), 10 deletions(-) rename ctru-sys/src/{ => sys}/libc.rs (100%) rename ctru-sys/src/{ => sys}/lock.rs (95%) create mode 100644 ctru-sys/src/sys/mod.rs diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index 6676277..15e4005 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -4,27 +4,25 @@ */ #![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 100% rename from ctru-sys/src/libc.rs rename to ctru-sys/src/sys/libc.rs 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; From 1e6694132048120a9bac2987aa60723ce9c8678b Mon Sep 17 00:00:00 2001 From: Fenrir Date: Mon, 1 Aug 2016 21:26:02 -0700 Subject: [PATCH 2/7] Update bindgen comment --- ctru-sys/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index 15e4005..b3cbfcb 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -1,5 +1,7 @@ /* * 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 */ From b75b7f804a34081e92388db231cc768095345e85 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Mon, 1 Aug 2016 21:28:32 -0700 Subject: [PATCH 3/7] Use abort() when panicking --- ctru-sys/src/sys/libc.rs | 1 + src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ctru-sys/src/sys/libc.rs b/ctru-sys/src/sys/libc.rs index 95e37db..b4d4a12 100644 --- a/ctru-sys/src/sys/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/src/lib.rs b/src/lib.rs index 43d72a7..ca5c249 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,5 +20,5 @@ pub use sdmc::Sdmc; extern "C" fn eh_personality() {} #[lang = "panic_fmt"] fn panic_fmt() -> ! { - loop {} + unsafe { libctru::libc::abort() } } From aba0449bffae55c48cf23b21e299689735abe969 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Mon, 1 Aug 2016 21:32:59 -0700 Subject: [PATCH 4/7] remove unnecessary mutable self-borrows --- src/services/apt.rs | 2 +- src/services/hid.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/apt.rs b/src/services/apt.rs index 612fdf7..8f50570 100644 --- a/src/services/apt.rs +++ b/src/services/apt.rs @@ -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..1719c32 100644 --- a/src/services/hid.rs +++ b/src/services/hid.rs @@ -89,7 +89,7 @@ impl Hid { } } - pub fn scan_input(&mut self) { + pub fn scan_input(&self) { unsafe { hid::hidScanInput() }; } From 62e5cccff394abf7ef14d49c10e75deaa619db3e Mon Sep 17 00:00:00 2001 From: Fenrir Date: Mon, 1 Aug 2016 21:41:02 -0700 Subject: [PATCH 5/7] Rename ::new() to ::init() --- src/sdmc.rs | 2 +- src/services/apt.rs | 2 +- src/services/hid.rs | 2 +- src/srv.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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 8f50570..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 { diff --git a/src/services/hid.rs b/src/services/hid.rs index 1719c32..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 { 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 { From 189fa64b3f1daf21ce2909edb61bff935223eaaf Mon Sep 17 00:00:00 2001 From: Fenrir Date: Fri, 5 Aug 2016 02:23:52 -0700 Subject: [PATCH 6/7] Implement proper panic messages --- src/lib.rs | 8 +------- src/panic.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 src/panic.rs diff --git a/src/lib.rs b/src/lib.rs index ca5c249..618bed9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,14 +11,8 @@ 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() -> ! { - unsafe { libctru::libc::abort() } -} diff --git a/src/panic.rs b/src/panic.rs new file mode 100644 index 0000000..c0e4d0a --- /dev/null +++ b/src/panic.rs @@ -0,0 +1,22 @@ +use core::fmt::{Arguments, Write}; + +#[lang = "eh_personality"] +extern "C" fn eh_personality() {} + +#[lang = "panic_fmt"] +extern fn panic_fmt(fmt: Arguments, file: &str, line: u32) -> ! { + use gfx::Screen; + use console::Console; + + let mut error_top = Console::init(Screen::Top); + let mut error_bottom = Console::init(Screen::Bottom); + + writeln!(error_top, "--------------------------------------------------").unwrap(); + writeln!(error_top, "PANIC in {} at line {}:", file, line).unwrap(); + writeln!(error_top, " {}", fmt).unwrap(); + write!(error_top, "\x1b[29;00H--------------------------------------------------").unwrap(); + + writeln!(error_bottom, "").unwrap(); + + loop {} +} From 941dca64dac91a66c3840c218c0d6d427ddd32a0 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Fri, 5 Aug 2016 18:30:38 -0700 Subject: [PATCH 7/7] improve panic implementation --- src/lib.rs | 5 ++++- src/panic.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 618bed9..dc225c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,13 @@ -#![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; diff --git a/src/panic.rs b/src/panic.rs index c0e4d0a..9fb3e82 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -1,22 +1,65 @@ -use core::fmt::{Arguments, Write}; +// 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 "C" fn eh_personality() {} +extern fn eh_personality() {} +/// Entry point of panic from the libcore crate. #[lang = "panic_fmt"] -extern fn panic_fmt(fmt: Arguments, file: &str, line: u32) -> ! { +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); - writeln!(error_top, "--------------------------------------------------").unwrap(); + write!(error_top, "--------------------------------------------------").unwrap(); writeln!(error_top, "PANIC in {} at line {}:", file, line).unwrap(); - writeln!(error_top, " {}", fmt).unwrap(); + writeln!(error_top, " {}", msg).unwrap(); write!(error_top, "\x1b[29;00H--------------------------------------------------").unwrap(); - writeln!(error_bottom, "").unwrap(); + write!(error_bottom, "").unwrap(); loop {} }