Skip to content

cload: Initial import. #3951

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions apps/cload/ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.01: attempt to import
45 changes: 45 additions & 0 deletions apps/cload/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// === Makefile ===
TARGET=cload
ARCH_FLAGS=
CROSS=arm-linux-gnueabihf-
CC=$(CROSS)gcc
# -nostdinc -- ouch, olive needs stddef.h
# -mfloat-abi=soft -- breaks build with __aeabi_fsub references
CFLAGS=-ffreestanding -Os -nostdlib -fPIC $(ARCH_FLAGS) -mlittle-endian -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd -mthumb-interwork
# --emit-relocs
LDFLAGS=-Ttext=0x0100 $(ARCH_FLAGS)

all: $(TARGET).bin
Copy link
Collaborator

Choose a reason for hiding this comment

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

We probably want .PHONY: all and the same for dump, etc below here


$(TARGET).o: $(TARGET).c lib.c
#-Wl,--emit-relocs
$(CC) $(CFLAGS) -c -o $@ $<

triangle3d.o: olive/triangle3d.c olive/vc.c olive/olive.c
$(CC) $(CFLAGS) -DVC_PLATFORM=VC_ESPRUINO_PLATFORM -Iolive/ -c -o $@ $<

triangle.o: olive/triangle.c olive/vc.c olive/olive.c
$(CC) $(CFLAGS) -DVC_PLATFORM=VC_ESPRUINO_PLATFORM -Iolive/ -c -o $@ $<

$(TARGET).elf: $(TARGET).o $(TARGET).ld triangle.o
#$(CC) $(LDFLAGS) -o $@ $<
# --emit-relocs
arm-linux-gnueabihf-ld -T cload.ld -o cload.elf cload.o triangle.o

$(TARGET).bin: $(TARGET).elf
# --only-section=.text
$(CROSS)objcopy -O binary $< $@

dump: cload.elf
$(CROSS)objdump --disassemble-all cload.elf

qemu:
$(CC) cload.c -o cload-static -static
qemu-arm-static ./cload-static

put: cload.bin
../wt put cload.bin

clean:
rm -f *.o *.elf *.bin

16 changes: 16 additions & 0 deletions apps/cload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Code Load

This allows running arm binaries as an application on
Bangle.js2. Obvious use is to run C code, and example doing that is
provided, but you should be able to run other languages, too, as long
as you can provide suitable binary (and fit within constraints), and
hack suitable basic library.

Binary code can ask javascript to do "stuff" on its behalf, currently
it can display text and display bitmap.

olive:

https://tsoding.github.io/olive.c/
https://github.com/tsoding/olive.c/tree/master

1 change: 1 addition & 0 deletions apps/cload/app-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("heatshrink").decompress(atob("mEwgIzwgPwAocf/4FDv//wAFC/ED4AWC8EAAoQJCoAFCCocAg4FFGgkPApUOAtw7LJopZFMopxFPoqJFSo0f/ytJAHQ="))
Binary file added apps/cload/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 118 additions & 0 deletions apps/cload/cload.app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Code Load, or C load.

I'm looking for loader for binaries for bangle.js2. Idea would be
I'd compile binary (likely gcc -fpic -nostdlibs -o ...) and than
have a javascript loader/linker that would load the binary into
memory and execute it.

Ideally I'd like some way to do "syscalls" from loaded
binary. Perhaps start with implementing print() syscall.

I'd like two word name for this, ideally steampunk-themed and
rhyming. I'm thinking about "Tin Bin".

(Bin Spin, Bit kit, Code Load -- Tin Bin)

Proof of concept, please. You can include design notes. And yes, code
is for Espruino.

*/

// === loader.js ===
// Espruino-side loader (run on Bangle.js2)
// Copy example.bin to Bangle.js2 Storage manually before use
// For example, upload using Espruino IDE and name it 'example.bin'

var img = {
width : 176, height : 176, bpp : 3,
buffer : new Uint8Array(176*176*(3/8)).buffer
};

function msg(s) {
g.reset().clear().setFont("Vector", 33).drawString(s, 10, 30).flip();
}

var bin, io, ios, ram, rams, code, ccode;

function step() {
io[0] = ios;
io[1] = rams;
io[2] = code;
print("Running at: ", io[2].toString(16));
let a1 = E.getAddressOf(io.buffer, true);
let a2 = E.getAddressOf(ram.buffer, true);
let a3 = E.getAddressOf(img.buffer, true);
print(a1, a2, a3);
return ccode(0x8a61c, a1, a2, a3);
}

var binData;

function load() {
bin = new Uint8Array(12*1024);
binData = Uint8Array(require("Storage").readArrayBuffer("cload.bin"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this just be binData = bin (or make them the same) ?

if (!binData) {
console.log("Binary not found!");
return;
}
bin.set(binData);
code = E.getAddressOf(bin.buffer, true);

// Create native function from binary
// '1' = use Thumb mode (bit 0 set in address)
// First 256 bytes are .got, +1 for thumb mode
ccode = E.nativeCall(code+257, "int(int,int,int,int)");

g.reset().clear();
print("Fill");
//j_test(img.buffer);

print("Buffers");
ios = 128;
io = new Uint32Array(ios);
rams = 2*1024;
ram = new Uint8Array(rams);

msg("Reloc");
io[3] = 0x51a87;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might it be worth noting down the source of these addresses?

let relocated = step();
if (relocated != 0xa11600d) {
print("Relocation failed:", relocated.toString(16));
return;
}

msg("Go");
while (1) {
io[3] = 0x606060;
let going = step();
if (going != 0x60176) {
print("Step failed", going.toString(16));
return;
}
switch(io[1]) {
case 1: // print: start, len
print("Should print");
{
let slice = ram.subarray(io[2], io[2]+io[3]);
let text = String.fromCharCode.apply(null, slice);
print(text);
msg(text);
}
break;
case 2: // draw
g.drawImage(img, 0, 0);
break;
default:
print("Unknown op: ", io[1]);
return;
}
g.flip();
}
}

msg("Cload\nready");
function go() {
load();
}
go();
Binary file added apps/cload/cload.bin
Binary file not shown.
Loading