Skip to content
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

Open
wants to merge 48 commits into
base: mastesr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3efccb2
Update README.md
hercule-karuha May 9, 2024
8c236b2
blue probe
hercule-karuha May 12, 2024
6abe977
Create LICENSE
hercule-karuha May 13, 2024
2bea3b6
add put_t and get_t
hercule-karuha May 13, 2024
c4d9c79
add ignore
hercule-karuha May 13, 2024
261a6fa
bluesim probe ilb
hercule-karuha May 13, 2024
adfb73a
rb-link
hercule-karuha May 13, 2024
17b899e
add examples
hercule-karuha May 13, 2024
a127a7f
clippy fix
hercule-karuha May 13, 2024
b9f4b30
remove warning
hercule-karuha May 13, 2024
7a362ef
add ignore
hercule-karuha May 13, 2024
feccc7a
change to server
hercule-karuha May 14, 2024
79428ef
change RProbe type
hercule-karuha May 14, 2024
dd60176
pass simple test
hercule-karuha May 16, 2024
b30ad1f
reduce nest
hercule-karuha May 16, 2024
678315f
add get by id and bycle
hercule-karuha May 16, 2024
097754b
remove debug message
hercule-karuha May 16, 2024
43bbd59
add tenstage
hercule-karuha May 16, 2024
5db36e7
fix tenstage error
hercule-karuha May 16, 2024
cc2b1ae
add simple doc and test
hercule-karuha May 17, 2024
1624a37
remove log file
hercule-karuha May 17, 2024
f87343c
clippy fix
hercule-karuha May 17, 2024
5dbcd91
add defaule
hercule-karuha May 17, 2024
06c07d6
add ci
hercule-karuha May 18, 2024
3136b8e
fmt
hercule-karuha May 18, 2024
632c964
change to deafult
hercule-karuha May 18, 2024
73a1294
remove parentheses
hercule-karuha May 19, 2024
ab365e7
remove cargo lock
hercule-karuha May 20, 2024
efe0538
bule to blue
hercule-karuha May 20, 2024
9840cf0
blue typo in ci and readme
hercule-karuha May 20, 2024
f19ea40
fmt
hercule-karuha May 20, 2024
b1eec7b
use OnceLock
hercule-karuha May 20, 2024
c54e01b
change to signle stream && remove sleep
hercule-karuha May 20, 2024
4da6837
add width defination
hercule-karuha May 20, 2024
9f15eda
change test to pass
hercule-karuha May 20, 2024
153db15
allow clippy in test
hercule-karuha May 20, 2024
37b1e0b
add fifo probe type
hercule-karuha May 21, 2024
28f49aa
add pipeline analysis
hercule-karuha May 22, 2024
d390d01
refactor add getter
hercule-karuha May 24, 2024
9aef4c1
add shut down & new_with
hercule-karuha May 25, 2024
b3ee694
add new test
hercule-karuha May 25, 2024
5837a9a
ci fix
hercule-karuha May 25, 2024
847c947
channge readme
hercule-karuha May 25, 2024
1db7f0c
add makefile
hercule-karuha May 25, 2024
56199e3
add example readme
hercule-karuha May 25, 2024
55e4b57
add Publisher & Subscriber
hercule-karuha May 29, 2024
0f8954a
test Publisher
hercule-karuha May 29, 2024
2eba386
change shutdown
hercule-karuha May 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/ci.yaml
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

8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/examples/AdderPipeline/build/
/examples/TenStage/build/
/probe-blue/build/
**/target/
**.out**
command
log
Cargo.lock
21 changes: 21 additions & 0 deletions LICENSE
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.
124 changes: 123 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,123 @@
# 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.

the example located at: examples/AdderPipeline/AdderPipeline.bsv

```
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
probe.shut_down_server();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No parentheses here

$finish;
end
endrule
endmodule
```

You need to link the Rust library file when linking Bluesim.

```
$ cd bluesim-rilb
$ cargo build
$ cd ../examples/AdderPipeline/
$ bsc -u -sim -bdir build -p .:%/Libraries:../../probe-blue/ -simdir build AdderPipeline.bsv
// add the .a file after the link command
$ bsc -sim -e mkAdderPipeline -bdir build -simdir build -o adder.out ../../bluesim-rlib/target/debug/libblue.a
```

Next, write your Rust code for analyzing the data.

the example located at: examples/adder_analysis/

```
use rb_link::*;
use std::thread;
use std::time::Duration;

fn main() {
let mut server = B2RServer::new_with("/tmp/adder");
// the getter used to get data from bluesim
let mut id_getter = IDGetter::new(&server);
for i in 0..10 {
let num: u32 = i;
server.put(0, num.to_le_bytes().to_vec())
}

//start a new thread to receive the data
let _ = server.serve();

thread::sleep(Duration::from_secs(3));

let msg_vec = id_getter.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]
])
);
}
}
```

Please ensure the rust program runs before the bliuesim

```
$ cd examples/adder_analysis/
$ cargo run
```



The path for the socket used for communication by default is `/tmp/b2rr2b`.

You can set the desired path by configuring the `B2R_SOCKET` variable.

In another terminal:

```
B2R_SOCKET=/tmp/adder ./adder.out
```



14 changes: 14 additions & 0 deletions bluesim-rlib/Cargo.toml
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"
94 changes: 94 additions & 0 deletions bluesim-rlib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//! 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::env;
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(|| {
let socket = match env::var("B2R_SOCKET") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated code should go into a function

Ok(path) => path,
Err(_) => "/tmp/b2rr2b".to_string(),
};
UnixStream::connect(socket).expect("Failed to connect to socket")
});

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(|| {
let socket = match env::var("B2R_SOCKET") {
Ok(path) => path,
Err(_) => "/tmp/b2rr2b".to_string(),
};
UnixStream::connect(socket).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");
}
29 changes: 29 additions & 0 deletions examples/AdderPipeline/AdderPipeline.bsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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
probe.shut_down_server();
$finish;
end
endrule
endmodule
Loading