Skip to content

Commit

Permalink
drop write macro in favor of IO objects + fmt::Write
Browse files Browse the repository at this point in the history
closes #4
closes #5
closes #6
closes #7
closes #11
  • Loading branch information
japaric committed Jul 3, 2017
1 parent 6deed40 commit a295a73
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 139 deletions.
2 changes: 2 additions & 0 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
/// This values are taken from section 5.5.2 of
/// ADS Debug Target Guide (DUI0058).
// TODO document
#[allow(missing_docs)]
pub enum Exception {
// Hardware reason codes
BranchThroughZero = 0x20000,
Expand Down
78 changes: 78 additions & 0 deletions src/hio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! Host I/O
use core::{fmt, slice};
use nr;

/// Host's standard error
pub struct HStderr {
fd: usize,
}

impl HStderr {
/// Attempts to write an entire `buffer` into this sink
pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
write_all(self.fd, buffer)
}
}

impl fmt::Write for HStderr {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
}
}

/// Host's standard output
pub struct HStdout {
fd: usize,
}

impl HStdout {
/// Attempts to write an entire `buffer` into this sink
pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
write_all(self.fd, buffer)
}
}

impl fmt::Write for HStdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
}
}

/// Construct a new handle to the host's standard error.
pub fn hstderr() -> Result<HStderr, ()> {
open(":tt\0", nr::open::W_APPEND).map(|fd| HStderr { fd })
}

/// Construct a new handle to the host's standard output.
pub fn hstdout() -> Result<HStdout, ()> {
open(":tt\0", nr::open::W_TRUNC).map(|fd| HStdout { fd })
}

fn open(name: &str, mode: usize) -> Result<usize, ()> {
let name = name.as_bytes();
match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as
isize {
-1 => Err(()),
fd => Ok(fd as usize),
}
}

fn write_all(fd: usize, mut buffer: &[u8]) -> Result<(), ()> {
while !buffer.is_empty() {
match unsafe { syscall!(WRITE, fd, buffer.as_ptr(), buffer.len()) } {
// Done
0 => return Ok(()),
// `n` bytes were not written
n if n <= buffer.len() => {
let offset = (buffer.len() - n) as isize;
buffer = unsafe {
slice::from_raw_parts(buffer.as_ptr().offset(offset), n)
}
}
// Error
_ => return Err(()),
}
}
Ok(())
}
106 changes: 0 additions & 106 deletions src/io.rs

This file was deleted.

8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//!
//! fn main() {
//! // File descriptor (on the host)
//! const STDOUT: usize = 1;
//! const STDOUT: usize = 1; // NOTE the host stdout may not always be fd 1
//! static MSG: &'static [u8] = b"Hello, world!\n";
//!
//! // Signature: fn write(fd: usize, ptr: *const u8, len: usize) -> usize
Expand Down Expand Up @@ -107,15 +107,17 @@
//!
//! [pdf]: http://infocenter.arm.com/help/topic/com.arm.doc.dui0471e/DUI0471E_developing_for_arm_processors.pdf
#![deny(missing_docs)]
#![deny(warnings)]
#![feature(asm)]
#![no_std]

#[macro_use]
mod macros;

pub mod io;
pub mod nr;
pub mod debug;
pub mod hio;
pub mod nr;

/// Performs a semihosting operation, takes a pointer to an argument block
#[inline(always)]
Expand Down
30 changes: 0 additions & 30 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,3 @@ macro_rules! syscall1 {
$crate::syscall1($crate::nr::$nr, $a1 as usize)
};
}

/// Macro for printing to the **host's** standard stderr
#[macro_export]
macro_rules! ehprint {
($s:expr) => ($crate::io::ewrite_str($s));
($($arg:tt)*) => ($crate::io::ewrite_fmt(format_args!($($arg)*)));
}

/// Macro for printing to the **host's** standard error, with a newline.
#[macro_export]
macro_rules! ehprintln {
() => (ehprint!("\n"));
($fmt:expr) => (ehprint!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (ehprint!(concat!($fmt, "\n"), $($arg)*));
}

/// Macro for printing to the **host's** standard output
#[macro_export]
macro_rules! hprint {
($s:expr) => ($crate::io::write_str($s));
($($arg:tt)*) => ($crate::io::write_fmt(format_args!($($arg)*)));
}

/// Macro for printing to the **host's** standard output, with a newline.
#[macro_export]
macro_rules! hprintln {
() => (hprint!("\n"));
($fmt:expr) => (hprint!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (hprint!(concat!($fmt, "\n"), $($arg)*));
}
3 changes: 3 additions & 0 deletions src/nr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Semihosting operations
// TODO document
#![allow(missing_docs)]

pub const CLOCK: usize = 0x10;
pub const CLOSE: usize = 0x05;
pub const ELAPSED: usize = 0x30;
Expand Down

0 comments on commit a295a73

Please sign in to comment.