mirror of
https://github.com/ParkerTenBroeck/hdl_sim.git
synced 2026-06-07 05:28:45 -04:00
better workspace support
This commit is contained in:
parent
53eb596861
commit
c6136920cb
6 changed files with 78 additions and 70 deletions
|
|
@ -6,6 +6,7 @@ use tokio::process::{Child, Command};
|
|||
use crate::HResult;
|
||||
|
||||
const EMBEDDED_VHDL_UI_LIB: &[u8] = include_bytes!(env!("EMBEDDED_VHDL_CONN_LIB_PATH"));
|
||||
const EMBEDDED_TB_VHDL: &str = include_str!("../../rtl/tb.vhdl");
|
||||
|
||||
async fn ensure_ok(child: Child) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let result = child.wait_with_output().await?;
|
||||
|
|
@ -73,10 +74,12 @@ pub async fn copy_and_build(
|
|||
}
|
||||
|
||||
|
||||
pub async fn build(path: &Path, src: &Path) -> HResult<()>{
|
||||
std::fs::create_dir_all(path)?;
|
||||
let embedded_lib_path = path.join("libvhdl_conn.a");
|
||||
pub async fn build(build: &Path, src: &Path) -> HResult<()>{
|
||||
std::fs::create_dir_all(build)?;
|
||||
let embedded_lib_path = build.join("libvhdl_conn.a");
|
||||
let embedded_tb_path = build.join("tb.vhdl");
|
||||
std::fs::write(&embedded_lib_path, EMBEDDED_VHDL_UI_LIB)?;
|
||||
std::fs::write(&embedded_tb_path, EMBEDDED_TB_VHDL)?;
|
||||
|
||||
let mut cmd = Command::new("ghdl");
|
||||
cmd.kill_on_drop(true);
|
||||
|
|
@ -84,32 +87,28 @@ pub async fn build(path: &Path, src: &Path) -> HResult<()>{
|
|||
|
||||
for file in src.read_dir().unwrap().flatten(){
|
||||
if Path::new(&file.file_name()).extension() == Some(OsStr::new("vhdl")) {
|
||||
cmd.arg(file.path());
|
||||
cmd.arg(file.path().canonicalize()?);
|
||||
}
|
||||
}
|
||||
|
||||
cmd.arg(std::fs::canonicalize("../rtl/tb.vhdl")?);
|
||||
cmd.arg(&embedded_tb_path.canonicalize()?);
|
||||
|
||||
cmd.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped());
|
||||
|
||||
cmd.current_dir(path);
|
||||
cmd.current_dir(build);
|
||||
ensure_ok(cmd.spawn()?).await?;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
let mut cmd = Command::new("ghdl");
|
||||
cmd.kill_on_drop(true);
|
||||
cmd.args(["-m", "--std=08"]);
|
||||
cmd.arg(format!(
|
||||
"-Wl,{}",
|
||||
embedded_lib_path.display()
|
||||
embedded_lib_path.canonicalize()?.display()
|
||||
));
|
||||
cmd.arg("tb");
|
||||
cmd.current_dir(path);
|
||||
cmd.current_dir(build);
|
||||
cmd.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped());
|
||||
|
|
|
|||
|
|
@ -13,17 +13,13 @@ use std::{
|
|||
|
||||
pub mod build;
|
||||
pub mod run;
|
||||
pub mod local;
|
||||
pub mod remote;
|
||||
pub mod uploaded;
|
||||
pub mod workspace;
|
||||
|
||||
const UI_INDEX_HTML: &str = include_str!("../ui/index.html");
|
||||
const UI_STYLES_CSS: &str = include_str!("../ui/styles.css");
|
||||
const UI_APP_JS: &str = include_str!("../ui/app.js");
|
||||
|
||||
async fn serve_index() -> impl IntoResponse {
|
||||
Html(UI_INDEX_HTML)
|
||||
}
|
||||
|
||||
async fn serve_styles() -> impl IntoResponse {
|
||||
(
|
||||
[(header::CONTENT_TYPE, "text/css; charset=utf-8")],
|
||||
|
|
@ -38,6 +34,10 @@ async fn serve_app_js() -> impl IntoResponse {
|
|||
)
|
||||
}
|
||||
|
||||
async fn serve_index() -> impl IntoResponse {
|
||||
Html(UI_INDEX_HTML)
|
||||
}
|
||||
|
||||
async fn not_found() -> impl IntoResponse {
|
||||
(StatusCode::NOT_FOUND, "not found")
|
||||
}
|
||||
|
|
@ -47,6 +47,7 @@ struct Config {
|
|||
ip: IpAddr,
|
||||
port: u16,
|
||||
update_ms: u64,
|
||||
workspace_ws: bool,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
|
|
@ -55,6 +56,7 @@ impl Default for Config {
|
|||
ip: IpAddr::from([127, 0, 0, 1]),
|
||||
port: 8080,
|
||||
update_ms: 30,
|
||||
workspace_ws: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -84,12 +86,17 @@ fn parse_config_from_args() -> Result<Config, String> {
|
|||
.parse::<u64>()
|
||||
.map_err(|err| format!("invalid --update-ms `{value}`: {err}"))?;
|
||||
}
|
||||
"--workspace" => {
|
||||
cfg.workspace_ws = true;
|
||||
}
|
||||
"--help" | "-h" => {
|
||||
return Err("usage: relay [--ip <ip>] [--port <port>] [--update-ms <ms>]".into());
|
||||
return Err(
|
||||
"usage: relay [--ip <ip>] [--port <port>] [--update-ms <ms>] [--workspace]".into(),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
return Err(format!(
|
||||
"unknown argument `{arg}`\nusage: relay [--ip <ip>] [--port <port>] [--update-ms <ms>]"
|
||||
"unknown argument `{arg}`\nusage: relay [--ip <ip>] [--port <port>] [--update-ms <ms>] [--workspace]"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -109,39 +116,44 @@ async fn main() {
|
|||
};
|
||||
|
||||
let update_interval = Duration::from_millis(cfg.update_ms);
|
||||
let app = Router::new()
|
||||
.route("/", get(serve_index))
|
||||
.route("/index.html", get(serve_index))
|
||||
let mut app = Router::new()
|
||||
.route(
|
||||
"/",
|
||||
get(move || {
|
||||
async move { serve_index().await }
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/index.html",
|
||||
get(move || {
|
||||
async move { serve_index().await }
|
||||
}),
|
||||
)
|
||||
.route("/styles.css", get(serve_styles))
|
||||
.route("/app.js", get(serve_app_js))
|
||||
.route(
|
||||
"/ws/remote",
|
||||
"/ws/uploaded",
|
||||
get(move |ws: WebSocketUpgrade| {
|
||||
let update_interval = update_interval;
|
||||
async move {
|
||||
ws.on_upgrade(move |socket| remote::ws_handler(socket, update_interval))
|
||||
ws.on_upgrade(move |socket| uploaded::ws_handler(socket, update_interval))
|
||||
}
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/ws/local",
|
||||
);
|
||||
|
||||
if cfg.workspace_ws {
|
||||
app = app.route(
|
||||
"/ws/workspace",
|
||||
get(move |ws: WebSocketUpgrade| {
|
||||
let update_interval = update_interval;
|
||||
async move {
|
||||
ws.on_upgrade(move |socket| local::ws_handler(socket, update_interval))
|
||||
ws.on_upgrade(move |socket| workspace::ws_handler(socket, update_interval))
|
||||
}
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/ws",
|
||||
get(move |ws: WebSocketUpgrade| {
|
||||
let update_interval = update_interval;
|
||||
async move {
|
||||
ws.on_upgrade(move |socket| remote::ws_handler(socket, update_interval))
|
||||
}
|
||||
}),
|
||||
)
|
||||
.fallback(get(not_found));
|
||||
);
|
||||
}
|
||||
|
||||
app = app.fallback(get(not_found));
|
||||
|
||||
let addr = SocketAddr::new(cfg.ip, cfg.port);
|
||||
println!("Open UI: http://{}/", addr);
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ struct Handler {
|
|||
|
||||
impl Handler {
|
||||
|
||||
fn local(socket: WebSocket, build: PathBuf, src: PathBuf, refresh_time: Duration) -> Self {
|
||||
fn workspace(socket: WebSocket, build: PathBuf, src: PathBuf, refresh_time: Duration) -> Self {
|
||||
let (sender, receiver) = socket.split();
|
||||
Self {
|
||||
sender,
|
||||
|
|
@ -208,5 +208,5 @@ impl Handler {
|
|||
}
|
||||
|
||||
pub async fn ws_handler(socket: WebSocket, refresh_time: Duration) {
|
||||
Handler::local(socket, "../target".into(), "../src".into(), refresh_time).run().await;
|
||||
Handler::workspace(socket, "./target".into(), "./src".into(), refresh_time).run().await;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue