Skip to content

Commit

Permalink
prog
Browse files Browse the repository at this point in the history
  • Loading branch information
Yisheng Jiang committed Apr 13, 2024
1 parent 1db5b7f commit f71a3ef
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 0 deletions.
Binary file removed a.out
Binary file not shown.
16 changes: 16 additions & 0 deletions fifolol/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
all: yofi.wasm.js

clean: src/yofi.c
rm -f build/*

build/yofi.ll: clean
clang src/yofi.c -o build/yofi.ll --target=wasm32 -emit-llvm -c -S

build/yofi.o: build/yofi.ll
llc -march=wasm32 -filetype=obj build/yofi.ll -o build/yofi.o

yofi.wasm: build/yofi.o
wasm-ld --features=atomics,mutable-global --no-check-features --allow-undefined --import-memory --no-entry --export-all -o yofi.wasm build/yofi.o

yofi.wasm.js: yofi.wasm
cat yofi.wasm | npx encode-wasm-uint8 > yofi.wasm.js
16 changes: 16 additions & 0 deletions fifolol/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "grep-fifo",
"version": "1.0.0",
"description": "ES6 compliant and (typescript friendly) implementation of circular fifo in C99",
"main": "fifo.wasmodule.js",
"scripts": {
"build": "docker run --rm -v `pwd`:/src trzeci/emscripten-slim emcc yofi.c -s EXPORT_ALL=1 -o yofi.js",
"test": "node ./test.js"
},
"keywords": [
"pain in the arse",
"allegedly faster than javascript"
],
"author": "",
"license": "ISC"
}
Binary file added fifolol/song.mid
Binary file not shown.
25 changes: 25 additions & 0 deletions fifolol/src/yofi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdint.h>

#define QUEUE_SIZE 1 << 16

typedef struct {
uint16_t head;
uint16_t tail;
char data[QUEUE_SIZE];
} queue_t;

queue_t instance[1];

queue_t* getInstance() { return instance; }

uint16_t queue_count(queue_t* queue) { return queue->head - queue->tail; }

char queue_read(queue_t* queue) {
if (queue->tail == queue->head) return 0;
return queue->data[queue->tail++];
}

int queue_write(queue_t* queue, char c) {
queue->data[queue->head++] = c;
return 0;
}
29 changes: 29 additions & 0 deletions fifolol/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="module">
import * as module from "./yofi.wasm.js";
const m = new WebAssembly.Module(module.wasmbin);
const ll = new WebAssembly.Instance(m, {
env: {
memory: new WebAssembly.Memory({initial: 41})
}
});
const qq = ll.exports.getInstance();
fetch("song.mid").then(r => r.arrayBuffer).then(ab => {
const arr = new Uint8Array(ab);
let off = 0;
while (ll.exports.queue_write(qq, arr[off++]));

})


</script>
</body>
</html>
Binary file added fifolol/yofi.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions fifolol/yofi.wasm.js

Large diffs are not rendered by default.

0 comments on commit f71a3ef

Please sign in to comment.