Skip to content

Commit 1a2ff74

Browse files
committed
ci: add a simple CI workflow to test the action
1 parent 621a92a commit 1a2ff74

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Build example chip
16+
uses: ./
17+
with:
18+
sources: test/main.c
19+
chip-json: test/chip.json
20+
21+
- name: Show dist
22+
run: |
23+
ls -lah dist
24+
file dist/chip.wasm || true

test/chip.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": 1,
3+
"name": "Inverter",
4+
"author": "Uri Shaked",
5+
"license": "MIT",
6+
"docs": "https://github.com/wokwi/inverter-chip/blob/main/docs/README.md",
7+
"pins": [
8+
"OUT",
9+
"IN",
10+
"VCC",
11+
"GND"
12+
]
13+
}

test/main.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "wokwi-api.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
typedef struct {
6+
pin_t pin_in;
7+
pin_t pin_out;
8+
} chip_state_t;
9+
10+
static void chip_pin_change(void *user_data, pin_t pin, uint32_t value) {
11+
chip_state_t *chip = (chip_state_t*)user_data;
12+
printf("Pin change: %d %d\n", pin, value);
13+
pin_write(chip->pin_out, !value);
14+
}
15+
16+
void chip_init(void) {
17+
chip_state_t *chip = malloc(sizeof(chip_state_t));
18+
chip->pin_in = pin_init("IN", INPUT);
19+
chip->pin_out = pin_init("OUT", OUTPUT);
20+
21+
const pin_watch_config_t config = {
22+
.edge = BOTH,
23+
.pin_change = chip_pin_change,
24+
.user_data = chip,
25+
};
26+
pin_watch(chip->pin_in, &config);
27+
}

test/wokwi-api.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include <stdint.h>
2+
#include <stdbool.h>
3+
4+
#ifndef WOKWI_API_H
5+
#define WOKWI_API_H
6+
7+
enum pin_value {
8+
LOW = 0,
9+
HIGH = 1
10+
};
11+
12+
enum pin_mode {
13+
INPUT = 0,
14+
OUTPUT = 1,
15+
INPUT_PULLUP = 2,
16+
INPUT_PULLDOWN = 3,
17+
ANALOG = 4,
18+
19+
OUTPUT_LOW = 16,
20+
OUTPUT_HIGH = 17,
21+
};
22+
23+
enum edge {
24+
RISING = 1,
25+
FALLING = 2,
26+
BOTH = 3,
27+
};
28+
29+
int __attribute__((export_name("__wokwi_api_version_1"))) __attribute__((weak)) __wokwi_api_version_1(void) { return 1; }
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
typedef int32_t pin_t;
36+
#define NO_PIN ((pin_t)-1)
37+
38+
typedef struct {
39+
void *user_data;
40+
uint32_t edge;
41+
void (*pin_change)(void *user_data, pin_t pin, uint32_t value);
42+
} pin_watch_config_t;
43+
44+
extern __attribute__((export_name("chipInit"))) void chip_init(void);
45+
46+
extern __attribute__((import_name("pinInit"))) pin_t pin_init(const char *name, uint32_t mode);
47+
48+
extern __attribute__((import_name("pinRead"))) uint32_t pin_read(pin_t pin);
49+
extern __attribute__((import_name("pinWrite"))) void pin_write(pin_t pin, uint32_t value);
50+
extern __attribute__((import_name("pinWatch"))) bool pin_watch(pin_t pin, const pin_watch_config_t *config);
51+
extern __attribute__((import_name("pinWatchStop"))) void pin_watch_stop(pin_t pin);
52+
extern __attribute__((import_name("pinMode"))) void pin_mode(pin_t pin, uint32_t value);
53+
extern __attribute__((import_name("pinADCRead"))) float pin_adc_read(pin_t pin);
54+
extern __attribute__((import_name("pinDACWrite"))) float pin_dac_write(pin_t pin, float voltage);
55+
56+
extern __attribute__((import_name("attrInit"))) uint32_t attr_init(const char *name, uint32_t default_value);
57+
extern __attribute__((import_name("attrInit"))) uint32_t attr_init_float(const char *name, float default_value);
58+
extern __attribute__((import_name("attrRead"))) uint32_t attr_read(uint32_t attr_id);
59+
extern __attribute__((import_name("attrReadFloat"))) float attr_read_float(uint32_t attr_id);
60+
61+
typedef struct {
62+
void *user_data;
63+
uint32_t address;
64+
pin_t scl;
65+
pin_t sda;
66+
bool (*connect)(void *user_data, uint32_t address, bool connect);
67+
uint8_t (*read)(void *user_data);
68+
bool (*write)(void *user_data, uint8_t data);
69+
void (*disconnect)(void *user_data);
70+
uint32_t reserved[8];
71+
} i2c_config_t;
72+
73+
typedef uint32_t i2c_dev_t;
74+
75+
extern __attribute__((import_name("i2cInit"))) i2c_dev_t i2c_init(const i2c_config_t *config);
76+
77+
typedef struct {
78+
void *user_data;
79+
pin_t rx;
80+
pin_t tx;
81+
uint32_t baud_rate;
82+
void (*rx_data)(void *user_data, uint8_t byte);
83+
void (*write_done)(void *user_data);
84+
uint32_t reserved[8];
85+
} uart_config_t;
86+
87+
typedef uint32_t uart_dev_t;
88+
89+
extern __attribute__((import_name("uartInit"))) uart_dev_t uart_init(const uart_config_t *config);
90+
extern __attribute__((import_name("uartWrite"))) bool uart_write(uart_dev_t uart, uint8_t *buffer, uint32_t count);
91+
92+
typedef struct {
93+
void *user_data;
94+
pin_t sck;
95+
pin_t mosi;
96+
pin_t miso;
97+
uint32_t mode;
98+
void (*done)(void *user_data, uint8_t *buffer, uint32_t count);
99+
uint32_t reserved[8];
100+
} spi_config_t;
101+
typedef uint32_t spi_dev_t;
102+
103+
extern __attribute__((import_name("spiInit"))) spi_dev_t spi_init(const spi_config_t *spi_config);
104+
extern __attribute__((import_name("spiStart"))) void spi_start(const spi_dev_t spi, uint8_t *buffer, uint32_t count);
105+
extern __attribute__((import_name("spiStop"))) void spi_stop(const spi_dev_t spi);
106+
107+
typedef struct {
108+
void *user_data;
109+
void (*callback)(void *user_data);
110+
uint32_t reserved[8];
111+
} timer_config_t;
112+
113+
typedef uint32_t timer_t;
114+
115+
extern __attribute__((import_name("timerInit"))) timer_t timer_init(const timer_config_t *config);
116+
extern __attribute__((import_name("timerStart"))) void timer_start(const timer_t timer, uint32_t micros, bool repeat);
117+
extern __attribute__((import_name("timerStartNanos"))) void timer_start_ns_d(const timer_t timer, double nanos, bool repeat);
118+
static void timer_start_ns(const timer_t timer, uint64_t nanos, bool repeat) {
119+
timer_start_ns_d(timer, (double)nanos, repeat);
120+
}
121+
extern __attribute__((import_name("timerStop"))) void timer_stop(const timer_t timer);
122+
123+
extern __attribute__((import_name("getSimNanos"))) double get_sim_nanos_d(void);
124+
125+
static uint64_t get_sim_nanos(void) {
126+
return (uint64_t)get_sim_nanos_d();
127+
}
128+
129+
typedef uint32_t buffer_t;
130+
extern __attribute__((import_name("framebufferInit"))) buffer_t framebuffer_init(uint32_t *pixel_width, uint32_t *pixel_height);
131+
extern __attribute__((import_name("bufferRead"))) void buffer_read(buffer_t buffer, uint32_t offset, uint8_t *data, uint32_t data_len);
132+
extern __attribute__((import_name("bufferWrite"))) void buffer_write(buffer_t buffer, uint32_t offset, uint8_t *data, uint32_t data_len);
133+
134+
#ifdef __cplusplus
135+
}
136+
#endif
137+
138+
#endif /* WOKWI_API_H */

0 commit comments

Comments
 (0)