Skip to content

Commit fb63563

Browse files
committed
feat: initial commit
0 parents  commit fb63563

File tree

12 files changed

+177
-0
lines changed

12 files changed

+177
-0
lines changed

.cargo/config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
[build]
3+
target = "wasm32-unknown-unknown"
4+
rustflags = ["-Clink-arg=--export-table", "-Clink-arg=--strip-debug"]

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "chip_framebuffer_example"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
wokwi_chip_ll = "0.1"
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[profile.release]
13+
lto = true
14+
opt-level = 's'

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Wokwi Framebuffer Chip in Rust example
2+
3+
To build:
4+
5+
```
6+
cargo build --target wasm32-unknown-unknown --release
7+
```
8+
9+
Find the resulting binary in `target/wasm32-unknown-unknown/release/chip_framebuffer_test.wasm`

build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
println!("cargo:rerun-if-changed=wokwi-chip.json");
3+
std::fs::copy(
4+
"wokwi-chip.json",
5+
format!(
6+
"target/{}/{}/chip_framebuffer_example.json",
7+
std::env::var("TARGET").unwrap(),
8+
std::env::var("PROFILE").unwrap(),
9+
),
10+
)
11+
.unwrap();
12+
}

diagram.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"author": "Uri Shaked",
4+
"editor": "wokwi",
5+
"parts": [
6+
{ "type": "chip-framebuffer-example", "id": "chip1", "top": -85.38, "left": 168, "attrs": {} },
7+
{ "type": "wokwi-gnd", "id": "gnd1", "top": -28.8, "left": 133.8, "attrs": {} },
8+
{ "type": "wokwi-vcc", "id": "vcc1", "top": -85.64, "left": 144, "attrs": {} }
9+
],
10+
"connections": [ [ "gnd1:GND", "chip1:GND", "black", [ "v0" ] ], [ "chip1:VCC", "vcc1:VCC", "red", [ "h0" ] ] ],
11+
"dependencies": {}
12+
}

src/lib.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Wokwi Custom Chips with Rust
2+
//
3+
// Very rough prototype by Uri Shaked
4+
//
5+
// Look at chipInit() at the bottom, and open Chrome devtools console to see the debugPrint().
6+
7+
use std::ffi::{c_void, CString};
8+
9+
use wokwi_chip_ll::{
10+
bufferWrite, debugPrint, framebufferInit, timerInit, timerStart, BufferId, TimerConfig,
11+
};
12+
13+
const MS: u32 = 1000; // micros
14+
15+
// Colors:
16+
const DEEP_GREEN: u32 = 0xff003000; // ARGB
17+
const PURPLE: u32 = 0xffff00ff; // ARGB
18+
19+
struct Chip {
20+
frame_buffer: BufferId,
21+
width: u32,
22+
height: u32,
23+
current_row: u32,
24+
}
25+
26+
fn draw_line(chip: &Chip, row: u32, color: u32) {
27+
let color_bytes_ptr = &color as *const u32 as *const u8;
28+
29+
let offset = chip.width * 4 * row;
30+
for x in (0..chip.width * 4).step_by(4) {
31+
unsafe {
32+
bufferWrite(chip.frame_buffer, offset + x, color_bytes_ptr, 4);
33+
}
34+
}
35+
}
36+
37+
pub unsafe fn on_timer_fired(user_data: *const c_void) {
38+
let chip = &mut *(user_data as *mut Chip);
39+
40+
if chip.current_row == 0 {
41+
debugPrint(
42+
CString::new("First row!")
43+
.unwrap()
44+
.into_raw(),
45+
);
46+
}
47+
48+
draw_line(chip, chip.current_row, DEEP_GREEN);
49+
50+
chip.current_row = (chip.current_row + 1) % chip.height;
51+
draw_line(chip, chip.current_row, PURPLE);
52+
}
53+
54+
#[no_mangle]
55+
pub unsafe extern "C" fn chipInit() {
56+
debugPrint(
57+
CString::new("Hello from Framebuffer Chip!")
58+
.unwrap()
59+
.into_raw(),
60+
);
61+
62+
let mut width = 0;
63+
let mut height = 0;
64+
let frame_buffer = framebufferInit(&mut width, &mut height);
65+
66+
let chip = Chip {
67+
frame_buffer: frame_buffer,
68+
width: width,
69+
height: height,
70+
current_row: 0,
71+
};
72+
73+
let timer_config = TimerConfig {
74+
user_data: &chip as *const _ as *const c_void,
75+
callback: on_timer_fired as *const c_void,
76+
};
77+
78+
let timer = timerInit(&timer_config);
79+
timerStart(timer, 10 * MS, true);
80+
}

test/blank.elf

Whitespace-only changes.

test/blank.hex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:100000000C945F000C945F000C945F000C945F00F4

0 commit comments

Comments
 (0)