Skip to content

Commit

Permalink
Use string instead of float as clock device output format (#696)
Browse files Browse the repository at this point in the history
* Use string instead of float as clock device output format

* Update lisp library

* Simplify read command

* Update comments
  • Loading branch information
vinc authored Oct 31, 2024
1 parent 0b749c4 commit e6f8911
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions dsk/lib/lisp/file.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@

(def (clock/boot)
"Returns the number of seconds since boot"
(binary->number (read-binary "/dev/clk/boot") "float"))
(str->num (read "/dev/clk/boot")))

(def (clock/epoch)
"Returns the number of seconds since epoch"
(binary->number (read-binary "/dev/clk/epoch") "float"))
(str->num (read "/dev/clk/epoch")))

# Path

Expand Down
9 changes: 6 additions & 3 deletions src/sys/clk/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::timer;

use crate::api::fs::{FileIO, IO};

use alloc::format;

#[derive(Debug, Clone)]
pub struct BootTime;

Expand All @@ -11,16 +13,17 @@ impl BootTime {
}

pub fn size() -> usize {
core::mem::size_of::<f64>()
// Must be at least 20 + 1 + 6 bytes: "<seconds>.<nanoseconds>"
32
}
}

impl FileIO for BootTime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
let time = boot_time().to_be_bytes();
let time = format!("{:.6}", boot_time());
let n = time.len();
if buf.len() >= n {
buf[0..n].clone_from_slice(&time);
buf[0..n].clone_from_slice(time.as_bytes());
Ok(n)
} else {
Err(())
Expand Down
9 changes: 6 additions & 3 deletions src/sys/clk/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::timer;

use crate::api::fs::{FileIO, IO};

use alloc::format;

const DAYS_BEFORE_MONTH: [u64; 13] = [
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
];
Expand All @@ -16,16 +18,17 @@ impl EpochTime {
}

pub fn size() -> usize {
core::mem::size_of::<f64>()
// Must be at least 20 + 1 + 6 bytes: "<seconds>.<nanoseconds>"
32
}
}

impl FileIO for EpochTime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
let time = epoch_time().to_be_bytes();
let time = format!("{:.6}", epoch_time());
let n = time.len();
if buf.len() >= n {
buf[0..n].clone_from_slice(&time);
buf[0..n].clone_from_slice(time.as_bytes());
Ok(n)
} else {
Err(())
Expand Down
3 changes: 2 additions & 1 deletion src/sys/vga/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl VgaMode {
}

pub fn size() -> usize {
16 // Must be larger than 8 bytes to be readable as a block device
// Must be at least 4 + 1 + 4 bytes: "<width>x<height>"
16
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/usr/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{api, usr};
use alloc::borrow::ToOwned;
use alloc::format;
use alloc::vec::Vec;
use core::convert::TryInto;

pub fn main(args: &[&str]) -> Result<(), ExitCode> {
if args.len() != 2 {
Expand Down Expand Up @@ -94,7 +93,6 @@ pub fn main(args: &[&str]) -> Result<(), ExitCode> {
// file.
let n = info.size();
let is_char_device = n == 4;
let is_float_device = n == 8;
let is_block_device = n > 8;
loop {
if console::end_of_text() || console::end_of_transmission() {
Expand All @@ -115,12 +113,6 @@ pub fn main(args: &[&str]) -> Result<(), ExitCode> {
_ => {}
}
}
if is_float_device && bytes.len() == 8 {
let f = f64::from_be_bytes(bytes[0..8].try_into().
unwrap());
println!("{:.6}", f);
return Ok(());
}
for b in bytes {
print!("{}", b as char);
}
Expand Down

0 comments on commit e6f8911

Please sign in to comment.