2018-05-30 00:55:58 +02:00
|
|
|
#include "pxt.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
PXT_ABI(__aeabi_dadd)
|
|
|
|
PXT_ABI(__aeabi_dcmplt)
|
|
|
|
PXT_ABI(__aeabi_dcmpgt)
|
|
|
|
PXT_ABI(__aeabi_dsub)
|
|
|
|
PXT_ABI(__aeabi_ddiv)
|
|
|
|
PXT_ABI(__aeabi_dmul)
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
extern "C" void target_panic(int error_code) {
|
2018-05-30 00:55:58 +02:00
|
|
|
// wait for serial to flush
|
|
|
|
wait_us(300000);
|
|
|
|
microbit_panic(error_code);
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
extern "C" void target_reset() {
|
2018-05-30 00:55:58 +02:00
|
|
|
microbit_reset();
|
|
|
|
}
|
|
|
|
|
2019-07-02 03:39:12 +02:00
|
|
|
uint32_t device_heap_size(uint8_t heap_index); // defined in microbit-dal
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
namespace pxt {
|
|
|
|
|
|
|
|
MicroBit uBit;
|
|
|
|
MicroBitEvent lastEvent;
|
|
|
|
|
2018-08-01 19:18:34 +02:00
|
|
|
void platform_init() {
|
2019-07-02 03:39:12 +02:00
|
|
|
microbit_seed_random();
|
2018-08-01 19:18:34 +02:00
|
|
|
seedRandom(microbit_random(0x7fffffff));
|
2019-10-02 07:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void initMicrobitGC() {
|
2019-07-17 15:23:13 +02:00
|
|
|
if (device_heap_size(1) > NON_GC_HEAP_RESERVATION + 4)
|
|
|
|
gcPreAllocateBlock(device_heap_size(1) - NON_GC_HEAP_RESERVATION);
|
2018-08-01 19:18:34 +02:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
|
|
|
|
void platform_init();
|
|
|
|
void usb_init();
|
|
|
|
|
|
|
|
struct FreeList {
|
|
|
|
FreeList *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void initCodal() {
|
|
|
|
|
|
|
|
uBit.init();
|
|
|
|
|
|
|
|
// repeat error 4 times and restart as needed
|
|
|
|
microbit_panic_timeout(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpDmesg() {}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// An adapter for the API expected by the run-time.
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// We have the invariant that if [dispatchEvent] is registered against the DAL
|
|
|
|
// for a given event, then [handlersMap] contains a valid entry for that
|
|
|
|
// event.
|
|
|
|
void dispatchEvent(MicroBitEvent e) {
|
|
|
|
lastEvent = e;
|
|
|
|
|
|
|
|
auto curr = findBinding(e.source, e.value);
|
|
|
|
auto value = fromInt(e.value);
|
|
|
|
if (curr)
|
|
|
|
runAction1(curr->action, value);
|
|
|
|
|
|
|
|
curr = findBinding(e.source, DEVICE_EVT_ANY);
|
|
|
|
if (curr)
|
|
|
|
runAction1(curr->action, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerWithDal(int id, int event, Action a, int flags) {
|
|
|
|
// first time?
|
|
|
|
if (!findBinding(id, event))
|
|
|
|
uBit.messageBus.listen(id, event, dispatchEvent, flags);
|
|
|
|
setBinding(id, event, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fiberDone(void *a) {
|
|
|
|
decr((Action)a);
|
2019-02-22 00:41:32 +01:00
|
|
|
unregisterGCPtr((Action)a);
|
2018-05-30 00:55:58 +02:00
|
|
|
release_fiber();
|
|
|
|
}
|
|
|
|
|
|
|
|
void releaseFiber() {
|
|
|
|
release_fiber();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_ms(unsigned ms) {
|
|
|
|
fiber_sleep(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_us(uint64_t us) {
|
|
|
|
wait_us(us);
|
|
|
|
}
|
|
|
|
|
|
|
|
void forever_stub(void *a) {
|
|
|
|
while (true) {
|
|
|
|
runAction0((Action)a);
|
|
|
|
fiber_sleep(20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void runForever(Action a) {
|
|
|
|
if (a != 0) {
|
|
|
|
incr(a);
|
2019-02-22 00:41:32 +01:00
|
|
|
registerGCPtr(a);
|
2018-05-30 00:55:58 +02:00
|
|
|
create_fiber(forever_stub, (void *)a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void runInParallel(Action a) {
|
|
|
|
if (a != 0) {
|
|
|
|
incr(a);
|
2019-02-22 00:41:32 +01:00
|
|
|
registerGCPtr(a);
|
2018-05-30 00:55:58 +02:00
|
|
|
create_fiber((void (*)(void *))runAction0, (void *)a, fiberDone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void waitForEvent(int id, int event) {
|
|
|
|
fiber_wait_for_event(id, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initRuntime() {
|
|
|
|
initCodal();
|
|
|
|
platform_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
//%
|
|
|
|
unsigned afterProgramPage() {
|
|
|
|
unsigned ptr = (unsigned)&bytecode[0];
|
|
|
|
ptr += programSize();
|
|
|
|
ptr = (ptr + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int current_time_ms() {
|
|
|
|
return system_timer_current_time();
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
static void logwriten(const char *msg, int l) {
|
|
|
|
uBit.serial.send((uint8_t *)msg, l);
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
static void logwrite(const char *msg) {
|
2018-05-30 00:55:58 +02:00
|
|
|
logwriten(msg, strlen(msg));
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
static void writeNum(char *buf, uint32_t n, bool full) {
|
2018-05-30 00:55:58 +02:00
|
|
|
int i = 0;
|
|
|
|
int sh = 28;
|
2019-02-22 00:41:32 +01:00
|
|
|
while (sh >= 0) {
|
2018-05-30 00:55:58 +02:00
|
|
|
int d = (n >> sh) & 0xf;
|
2019-02-22 00:41:32 +01:00
|
|
|
if (full || d || sh == 0 || i) {
|
2018-05-30 00:55:58 +02:00
|
|
|
buf[i++] = d > 9 ? 'A' + d - 10 : '0' + d;
|
|
|
|
}
|
|
|
|
sh -= 4;
|
|
|
|
}
|
|
|
|
buf[i] = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
static void logwritenum(uint32_t n, bool full, bool hex) {
|
2018-05-30 00:55:58 +02:00
|
|
|
char buff[20];
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
if (hex) {
|
2018-05-30 00:55:58 +02:00
|
|
|
writeNum(buff, n, full);
|
|
|
|
logwrite("0x");
|
2019-02-22 00:41:32 +01:00
|
|
|
} else {
|
2018-05-30 00:55:58 +02:00
|
|
|
itoa(n, buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
logwrite(buff);
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
void vdebuglog(const char *format, va_list ap) {
|
2018-05-30 00:55:58 +02:00
|
|
|
const char *end = format;
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
while (*end) {
|
|
|
|
if (*end++ == '%') {
|
2018-05-30 00:55:58 +02:00
|
|
|
logwriten(format, end - format - 1);
|
|
|
|
uint32_t val = va_arg(ap, uint32_t);
|
2019-02-22 00:41:32 +01:00
|
|
|
switch (*end++) {
|
2018-05-30 00:55:58 +02:00
|
|
|
case 'c':
|
|
|
|
logwriten((const char *)&val, 1);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
logwritenum(val, false, false);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
logwritenum(val, false, true);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
case 'X':
|
|
|
|
logwritenum(val, true, true);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
logwrite((char *)(void *)val);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
logwrite("%");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logwrite("???");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
format = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logwriten(format, end - format);
|
|
|
|
logwrite("\n");
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
void debuglog(const char *format, ...) {
|
2018-05-30 00:55:58 +02:00
|
|
|
va_list arg;
|
|
|
|
va_start(arg, format);
|
|
|
|
vdebuglog(format, arg);
|
|
|
|
va_end(arg);
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:12:09 +01:00
|
|
|
void sendSerial(const char *data, int len) {
|
|
|
|
logwriten(data, len);
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
#ifdef PXT_GC
|
|
|
|
ThreadContext *getThreadContext() {
|
|
|
|
if (!currentFiber)
|
|
|
|
return NULL;
|
|
|
|
return (ThreadContext *)currentFiber->user_data;
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
void setThreadContext(ThreadContext *ctx) {
|
|
|
|
currentFiber->user_data = ctx;
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
static void *threadAddressFor(Fiber *fib, void *sp) {
|
|
|
|
if (fib == currentFiber)
|
|
|
|
return sp;
|
|
|
|
return (uint8_t *)sp + ((uint8_t *)fib->stack_top - (uint8_t *)fib->tcb.stack_base);
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
void gcProcessStacks(int flags) {
|
|
|
|
// check scheduler is initialized
|
|
|
|
if (!currentFiber) {
|
|
|
|
// make sure we allocate something to at least initalize the memory allocator
|
|
|
|
void * volatile p = xmalloc(1);
|
|
|
|
xfree(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int numFibers = list_fibers(NULL);
|
|
|
|
Fiber **fibers = (Fiber **)xmalloc(sizeof(Fiber *) * numFibers);
|
|
|
|
int num2 = list_fibers(fibers);
|
|
|
|
if (numFibers != num2)
|
|
|
|
oops(12);
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < numFibers; ++i) {
|
|
|
|
auto fib = fibers[i];
|
|
|
|
auto ctx = (ThreadContext *)fib->user_data;
|
|
|
|
if (!ctx)
|
|
|
|
continue;
|
|
|
|
for (auto seg = &ctx->stack; seg; seg = seg->next) {
|
|
|
|
auto ptr = (TValue *)threadAddressFor(fib, seg->top);
|
|
|
|
auto end = (TValue *)threadAddressFor(fib, seg->bottom);
|
|
|
|
if (flags & 2)
|
|
|
|
DMESG("RS%d:%p/%d", cnt++, ptr, end - ptr);
|
|
|
|
// VLOG("mark: %p - %p", ptr, end);
|
|
|
|
while (ptr < end) {
|
|
|
|
gcProcess(*ptr++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xfree(fibers);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace pxt
|