-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pull request to a new branch #1
base: mastesr
Are you sure you want to change the base?
Changes from 38 commits
3efccb2
8c236b2
6abe977
2bea3b6
c4d9c79
261a6fa
adfb73a
17b899e
a127a7f
b9f4b30
7a362ef
feccc7a
79428ef
dd60176
b30ad1f
678315f
097754b
43bbd59
5db36e7
cc2b1ae
1624a37
f87343c
5dbcd91
06c07d6
3136b8e
632c964
73a1294
ab365e7
efe0538
9840cf0
f19ea40
b1eec7b
c54e01b
4da6837
9f15eda
153db15
37b1e0b
28f49aa
d390d01
9aef4c1
b3ee694
5837a9a
847c947
1db7f0c
56199e3
55e4b57
0f8954a
2eba386
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
name: "A+: CI" | ||
|
||
permissions: | ||
contents: write | ||
|
||
on: | ||
workflow_dispatch: | ||
workflow_call: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
cargo-build: | ||
name: Cargo Build | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Fetch Repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install stable toolchain | ||
uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
|
||
- name: Build rlib | ||
run: cd bluesim-rlib && cargo b --workspace --all-targets --all-features | ||
|
||
- name: Build rb_link | ||
run: cd rb_link && cargo b --workspace --all-targets --all-features | ||
|
||
cargo-fmt: | ||
name: Cargo fmt | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Fetch Repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install stable toolchain | ||
uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
with: | ||
components: rustfmt | ||
|
||
- name: Rustfmt Check rlib | ||
run: cd bluesim-rlib && cargo fmt --all --check | ||
|
||
- name: Rustfmt Check rb_link | ||
run: cd rb_link && cargo fmt --all --check | ||
|
||
cargo-clippy: | ||
name: Cargo clippy | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Fetch Repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install stable toolchain | ||
uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
with: | ||
components: clippy | ||
|
||
- name: Clippy Check rlib | ||
run: cd bluesim-rlib && cargo clippy --workspace --all-targets --all-features -- -Dwarnings | ||
|
||
- name: Clippy Check rb_link | ||
run: cd rb_link && cargo clippy --workspace --all-targets --all-features -- -Dwarnings | ||
|
||
cargo-test: | ||
name: Cargo test | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Fetch Repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install stable toolchain | ||
uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
|
||
- name: Cargo test rb_link | ||
run: cd rb_link && cargo test -- --test-threads=1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/examples/adder-pipeline/build/ | ||
/examples/TenStage/build/ | ||
/probe-blue/build/ | ||
**/target/ | ||
**.out** | ||
command | ||
log | ||
Cargo.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 hercule-karuha | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,90 @@ | ||
# b2rr2b | ||
# b2rr2b | ||
## A Bluesim simulation probe framework. | ||
|
||
|
||
|
||
This framework includes the following components: | ||
|
||
- `/bluesim-rlib` : A Rust library called by the Bluesim program for receiving and sending data. | ||
- `/probe-blue` : BSV code for the probe, calling Rust functions through the BDPI interface. | ||
- `/rb_link` :A Rust framework for interacting with probes, capable of getting and sending data. | ||
|
||
|
||
|
||
### Uasge | ||
|
||
First, you need to write BSV code and instantiate RProbe within it. | ||
|
||
``` | ||
import RProbe::*; | ||
import FIFOF::*; | ||
|
||
(* synthesize *) | ||
module mkAdderPipeline(Empty); | ||
FIFOF#(Bit#(32)) f2d <- mkFIFOF; | ||
RProbe#(Bit#(32), Bit#(32)) probe <- mkRProbe(0); | ||
|
||
Reg#(Bit#(32)) fetch_times <- mkReg(0); | ||
Reg#(Bit#(32)) put_times <- mkReg(0); | ||
|
||
|
||
rule doGet if (fetch_times < 10); | ||
Bit#(32) data = probe.get_data(); | ||
f2d.enq(data); | ||
fetch_times <= fetch_times + 1; | ||
endrule | ||
|
||
rule doPut; | ||
Bit#(32) data = f2d.first; | ||
f2d.deq; | ||
probe.put_data(data + 1); | ||
put_times <= put_times + 1; | ||
if(put_times == 9) begin | ||
$finish; | ||
end | ||
endrule | ||
endmodule | ||
``` | ||
|
||
You need to link the Rust library file when linking Bluesim. | ||
|
||
``` | ||
$ cd bluesim-rilb | ||
$ cargo build | ||
$ cd <your bsv projest path> | ||
// add the .a file after the link command | ||
$ <your link command> ../../bluesim-rlib/target/debug/libblue.a | ||
``` | ||
|
||
Next, write your Rust code for analyzing the data. | ||
|
||
``` | ||
fn main() { | ||
let mut server = B2RServer::new(); | ||
// ensure the data length equal to the defination in bsv | ||
for i in 0..10 { | ||
let num: u32 = i; | ||
server.put(0, num.to_le_bytes().to_vec()) | ||
} | ||
|
||
let handlle = server.serve(); | ||
|
||
thread::sleep(Duration::from_secs(3)); | ||
// get data from bluesim | ||
let msg_vec = server.get_id_all(0); | ||
for msg in msg_vec { | ||
println!( | ||
"get from blue id:{}, cycle:{}, data:{}", | ||
msg.id, | ||
msg.cycles, | ||
u32::from_le_bytes([ | ||
msg.message[0], | ||
msg.message[1], | ||
msg.message[2], | ||
msg.message[3] | ||
]) | ||
); | ||
} | ||
} | ||
``` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "bluesim-rlib" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
name = "blue" | ||
|
||
[dependencies] | ||
rb_link = { path = "../rb_link" } | ||
bincode = "1.3.3" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//! The Rust library called by bluesim. | ||
//! If you want to use RProbe in your bluespec project, | ||
//! please compile this crate into an .a file and then link it to your bluesim executable. | ||
#![warn(clippy::unwrap_used)] | ||
use rb_link::{B2RMessage, GetPutMessage, MsgSizeType, MSG_SIZE_BYTES}; | ||
use std::io::{Read, Write}; | ||
use std::os::unix::net::UnixStream; | ||
use std::sync::OnceLock; | ||
|
||
static mut STREAM: OnceLock<UnixStream> = OnceLock::new(); | ||
|
||
/// # Safety | ||
/// This function should not be called by Rust code. | ||
/// Get data from your rust program. | ||
/// called by RProbe::get_data() | ||
#[no_mangle] | ||
pub unsafe extern "C" fn get(res_ptr: *mut u8, id: u32, _cycles: u32, size: u32) { | ||
// println!("send get"); | ||
// check the ptr is not null | ||
if res_ptr.is_null() { | ||
panic!("res_ptr is a null pointer!"); | ||
} | ||
if _cycles == u32::MAX { | ||
panic!("cycles over flow!"); | ||
} | ||
let mut stream = STREAM.get_or_init(|| { | ||
UnixStream::connect(String::from("/tmp/b2rr2b")).expect("Failed to connect to socket") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, .expect and .unwrap is almost the same. Both of them will panic the process. Rust use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beacuse i just don't think the errors are recoverable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to speed up test, it's common to run multi test process at the same time. A hardcoded local unix-stream will not work when there is multi process running different tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
}); | ||
|
||
let get_message = GetPutMessage::Get(id); | ||
let serialized = bincode::serialize(&get_message).expect("Serialization failed"); | ||
|
||
// The initial 4-byte data specifies the byte count of the message in the u32 format. | ||
let msg_size = serialized.len() as MsgSizeType; | ||
let mut msg_with_size = Vec::with_capacity(MSG_SIZE_BYTES + serialized.len()); | ||
msg_with_size.extend_from_slice(&msg_size.to_le_bytes()); | ||
msg_with_size.extend(serialized.iter()); | ||
stream | ||
.write_all(&msg_with_size) | ||
.expect("Failed to write to stream"); | ||
|
||
let res_slice = std::slice::from_raw_parts_mut(res_ptr, size as usize); | ||
stream | ||
.read_exact(res_slice) | ||
.expect("Failed to read from stream"); | ||
} | ||
|
||
/// # Safety | ||
/// This function should not be called by Rust code. | ||
/// Put data to your rust program. | ||
/// called by RProbe::put_data() | ||
#[no_mangle] | ||
pub unsafe extern "C" fn put(id: u32, cycles: u32, data_ptr: *mut u8, size: u32) { | ||
// println!("send put"); | ||
// check the ptr is not null | ||
if data_ptr.is_null() { | ||
panic!("data_ptr is a null pointer!"); | ||
} | ||
if cycles == u32::MAX { | ||
panic!("cycles over flow!"); | ||
} | ||
|
||
let mut stream = STREAM.get_or_init(|| { | ||
UnixStream::connect(String::from("/tmp/b2rr2b")).expect("Failed to connect to socket") | ||
}); | ||
|
||
let data_slice = std::slice::from_raw_parts(data_ptr, size as usize); | ||
let b2r_message = B2RMessage { | ||
id, | ||
cycles, | ||
message: data_slice.to_vec(), | ||
}; | ||
let put_message = GetPutMessage::Put(b2r_message); | ||
let serialized = bincode::serialize(&put_message).expect("Serialization failed"); | ||
|
||
// The initial 4-byte data specifies the byte count of the message in the u32 format. | ||
let msg_size = serialized.len() as MsgSizeType; | ||
let mut msg_with_size = Vec::with_capacity(MSG_SIZE_BYTES + serialized.len()); | ||
msg_with_size.extend_from_slice(msg_size.to_le_bytes().as_slice()); | ||
msg_with_size.extend(serialized.iter()); | ||
|
||
stream | ||
.write_all(&msg_with_size) | ||
.expect("Failed to write to stream"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should have a runable command line, maybe the user doesn't known how to link a
.a
file to their bluespec simulator. After all, linking a library to simulator is not a normal operation. bluespec also doesn't have much doc about it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed