From 3263def5c8b2c3997c067fde87065e98d002e07d Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 29 Sep 2016 14:04:42 +0000 Subject: [PATCH] Rust: use generated CSR functions. --- artiq/runtime.rs/src/board.rs | 15 +++++++++++++++ artiq/runtime.rs/src/buffer_logger.rs | 4 ++-- artiq/runtime.rs/src/lib.rs | 1 + artiq/runtime.rs/src/session/mod.rs | 11 +---------- 4 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 artiq/runtime.rs/src/board.rs diff --git a/artiq/runtime.rs/src/board.rs b/artiq/runtime.rs/src/board.rs new file mode 100644 index 000000000..aa97b1edb --- /dev/null +++ b/artiq/runtime.rs/src/board.rs @@ -0,0 +1,15 @@ +use core::{cmp, ptr, str}; + +include!(concat!(env!("BUILDINC_DIRECTORY"), "/generated/mem.rs")); +include!(concat!(env!("BUILDINC_DIRECTORY"), "/generated/csr.rs")); + +pub fn ident(buf: &mut [u8]) -> &str { + unsafe { + let len = ptr::read_volatile(csr::IDENTIFIER_MEM_BASE); + let len = cmp::min(len as usize, buf.len()); + for i in 0..len { + buf[i] = ptr::read_volatile(csr::IDENTIFIER_MEM_BASE.offset(1 + i as isize)) as u8 + } + str::from_utf8_unchecked(&buf[..len]) + } +} diff --git a/artiq/runtime.rs/src/buffer_logger.rs b/artiq/runtime.rs/src/buffer_logger.rs index 4ec9927eb..e10a5a876 100644 --- a/artiq/runtime.rs/src/buffer_logger.rs +++ b/artiq/runtime.rs/src/buffer_logger.rs @@ -48,9 +48,9 @@ impl Log for BufferLogger { fn log(&self, record: &LogRecord) { if self.enabled(record.metadata()) { use core::fmt::Write; - writeln!(self.buffer.borrow_mut(), "{}({}): {}", + writeln!(self.buffer.borrow_mut(), "{:>5}({}): {}", record.level(), record.location().module_path(), record.args()).unwrap(); - println!("{}({}): {}", + println!("{:>5}({}): {}", record.level(), record.location().module_path(), record.args()); } } diff --git a/artiq/runtime.rs/src/lib.rs b/artiq/runtime.rs/src/lib.rs index afb7003c1..22feccda2 100644 --- a/artiq/runtime.rs/src/lib.rs +++ b/artiq/runtime.rs/src/lib.rs @@ -11,6 +11,7 @@ extern crate byteorder; use std::prelude::v1::*; use buffer_logger::BufferLogger; +pub mod board; pub mod io; pub mod buffer_logger; pub mod session; diff --git a/artiq/runtime.rs/src/session/mod.rs b/artiq/runtime.rs/src/session/mod.rs index f4bec57a9..b4d05bb99 100644 --- a/artiq/runtime.rs/src/session/mod.rs +++ b/artiq/runtime.rs/src/session/mod.rs @@ -68,16 +68,7 @@ fn handle_request(stream: &mut ::io::TcpStream, match try!(read_request(stream)) { Request::Ident => { - let mut ident: [u8; 256]; - let ident = unsafe { - extern { fn get_ident(ident: *mut u8); } - - ident = ::core::mem::uninitialized(); - get_ident(ident.as_mut_ptr()); - &ident[..ident.iter().position(|&c| c == 0).unwrap()] - }; - - write_reply(stream, Reply::Ident(str::from_utf8(ident).unwrap())) + write_reply(stream, Reply::Ident(::board::ident(&mut [0; 64]))) } Request::Log => {