forked from MightyPork/crsn
optimize labels, jumps
This commit is contained in:
+42
-14
@@ -13,8 +13,10 @@ use crsn::runtime::run_thread::{RunThread, ThreadToken};
|
||||
use crsn_arith::ArithOps;
|
||||
use crsn_screen::ScreenOps;
|
||||
use crsn_stacks::StackOps;
|
||||
use std::time::Duration;
|
||||
|
||||
mod read_file;
|
||||
mod serde_duration_millis;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct LogConfig {
|
||||
@@ -23,10 +25,13 @@ struct LogConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
struct Config {
|
||||
log: LogConfig,
|
||||
#[serde(skip)]
|
||||
program_file: String,
|
||||
#[serde(with="serde_duration_millis")]
|
||||
cycle_time: Duration,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
@@ -37,6 +42,7 @@ impl Default for Config {
|
||||
modules: Default::default(),
|
||||
},
|
||||
program_file: "".to_string(),
|
||||
cycle_time: Duration::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,17 +61,43 @@ impl AppConfig for Config {
|
||||
/// Add args to later use in the `configure` method.
|
||||
fn add_args<'a: 'b, 'b>(clap: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
|
||||
// Default impl
|
||||
clap.arg(
|
||||
clap::Arg::with_name("input")
|
||||
.value_name("FILE")
|
||||
.help("Program to run")
|
||||
.required_unless("default-config")
|
||||
.takes_value(true),
|
||||
)
|
||||
clap
|
||||
.arg(
|
||||
clap::Arg::with_name("input")
|
||||
.value_name("FILE")
|
||||
.help("Program to run")
|
||||
.required_unless("default-config")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::with_name("cycle")
|
||||
.long("cycle")
|
||||
.short("C")
|
||||
.value_name("MILLIS")
|
||||
.help("Cycle time (ms)")
|
||||
.validator(|s| {
|
||||
let t = s.trim();
|
||||
if t.is_empty() {
|
||||
Err("cycle time requires an argument".into())
|
||||
} else {
|
||||
if t.chars()
|
||||
.find(|c| !c.is_ascii_digit())
|
||||
.is_some() {
|
||||
Err("cycle time requires an integer number".into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
})
|
||||
.takes_value(true),
|
||||
)
|
||||
}
|
||||
|
||||
fn configure(mut self, clap: &ArgMatches) -> anyhow::Result<Self> {
|
||||
self.program_file = clap.value_of("input").unwrap().to_string();
|
||||
if let Some(c) = clap.value_of("cycle") {
|
||||
self.cycle_time = Duration::from_millis(c.parse().unwrap());
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
@@ -88,13 +120,9 @@ fn main() -> anyhow::Result<()> {
|
||||
|
||||
info!("Start runtime");
|
||||
|
||||
let thread = RunThread::new(
|
||||
ThreadToken(0),
|
||||
None,
|
||||
parsed.clone(),
|
||||
Addr(0), // TODO find "main"?
|
||||
&[], // TODO from CLI?
|
||||
);
|
||||
let args = &[];
|
||||
let mut thread = RunThread::new(ThreadToken(0), None, parsed.clone(), Addr(0), args);
|
||||
thread.set_speed(config.cycle_time);
|
||||
|
||||
let a = thread.start();
|
||||
// ...
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
use serde::{self, Deserialize, Deserializer, Serializer};
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn serialize<S>(value: &Duration, se: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
se.serialize_u64(value.as_secs() * 1000 + value.subsec_millis() as u64)
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(de: D) -> Result<Duration, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s: u64 = u64::deserialize(de)?;
|
||||
Ok(Duration::from_millis(s))
|
||||
}
|
||||
Reference in New Issue
Block a user