2017-08-05 00:58:55 +03:00
|
|
|
#include "pxt.h"
|
|
|
|
|
2017-10-21 18:30:01 +03:00
|
|
|
void cpu_clock_init(void);
|
|
|
|
|
2018-04-06 05:08:56 +03:00
|
|
|
PXT_ABI(__aeabi_dadd)
|
|
|
|
PXT_ABI(__aeabi_dcmplt)
|
|
|
|
PXT_ABI(__aeabi_dcmpgt)
|
|
|
|
PXT_ABI(__aeabi_dsub)
|
|
|
|
PXT_ABI(__aeabi_ddiv)
|
|
|
|
PXT_ABI(__aeabi_dmul)
|
|
|
|
|
2018-11-13 02:34:52 +03:00
|
|
|
#define PXT_COMM_BASE 0x20001000 // 4k in
|
2018-04-06 05:08:56 +03:00
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
namespace pxt {
|
|
|
|
|
2017-12-12 20:28:16 +03:00
|
|
|
void platform_init();
|
|
|
|
void usb_init();
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
// The first two word are used to tell the bootloader that a single reset should start the
|
|
|
|
// bootloader and the MSD device, not us.
|
|
|
|
// The rest is reserved for partial flashing checksums.
|
|
|
|
__attribute__((section(".binmeta"))) __attribute__((used)) const uint32_t pxt_binmeta[] = {
|
|
|
|
0x87eeb07c, 0x87eeb07c, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff,
|
|
|
|
0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff,
|
|
|
|
};
|
|
|
|
|
2017-09-26 20:32:45 +03:00
|
|
|
Event lastEvent;
|
2017-08-05 00:58:55 +03:00
|
|
|
MessageBus devMessageBus;
|
|
|
|
codal::CodalDevice device;
|
|
|
|
|
2018-04-06 05:08:56 +03:00
|
|
|
struct FreeList {
|
|
|
|
FreeList *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void commInit() {
|
|
|
|
int commSize = bytecode[20];
|
2018-11-13 02:34:52 +03:00
|
|
|
if (!commSize)
|
|
|
|
return;
|
2018-04-06 05:08:56 +03:00
|
|
|
|
2018-11-13 02:34:52 +03:00
|
|
|
FreeList *head = NULL;
|
|
|
|
void *commBase = (void *)PXT_COMM_BASE;
|
2018-04-06 05:08:56 +03:00
|
|
|
for (;;) {
|
2018-11-13 02:34:52 +03:00
|
|
|
void *p = xmalloc(4);
|
|
|
|
// assume 4 byte alloc header; if we're not hitting 8 byte alignment, try allocating 8
|
|
|
|
// bytes, not 4 without the volatile, gcc assumes 8 byte alignment on malloc()
|
2018-04-06 05:08:56 +03:00
|
|
|
volatile unsigned hp = (unsigned)p;
|
|
|
|
if (hp & 4) {
|
2018-12-08 02:06:12 +03:00
|
|
|
xfree(p);
|
2018-11-13 02:34:52 +03:00
|
|
|
p = xmalloc(8);
|
2018-04-06 05:08:56 +03:00
|
|
|
}
|
|
|
|
if (p == commBase) {
|
2018-12-08 02:06:12 +03:00
|
|
|
xfree(p);
|
2018-04-06 05:08:56 +03:00
|
|
|
// allocate the comm section; this is never freed
|
2018-11-13 02:34:52 +03:00
|
|
|
p = xmalloc(commSize);
|
2018-04-06 05:08:56 +03:00
|
|
|
if (p != commBase)
|
2018-11-13 02:34:52 +03:00
|
|
|
oops(10);
|
2018-04-06 05:08:56 +03:00
|
|
|
break;
|
|
|
|
}
|
2018-11-13 02:34:52 +03:00
|
|
|
if (p > commBase)
|
|
|
|
oops(11);
|
|
|
|
auto f = (FreeList *)p;
|
2018-04-06 05:08:56 +03:00
|
|
|
f->next = head;
|
|
|
|
head = f;
|
|
|
|
}
|
|
|
|
// free all the filler stuff
|
|
|
|
while (head) {
|
|
|
|
auto p = head;
|
|
|
|
head = head->next;
|
2018-12-08 02:06:12 +03:00
|
|
|
xfree(p);
|
2018-04-06 05:08:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
static void initCodal() {
|
2017-10-21 18:30:01 +03:00
|
|
|
cpu_clock_init();
|
2017-08-05 00:58:55 +03:00
|
|
|
|
2018-04-06 05:08:56 +03:00
|
|
|
commInit();
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
// Bring up fiber scheduler.
|
|
|
|
scheduler_init(devMessageBus);
|
|
|
|
|
2017-10-21 18:30:01 +03:00
|
|
|
// We probably don't need that - components are initialized when one obtains
|
|
|
|
// the reference to it.
|
2017-10-23 23:48:41 +03:00
|
|
|
// devMessageBus.listen(DEVICE_ID_MESSAGE_BUS_LISTENER, DEVICE_EVT_ANY, this,
|
|
|
|
// &CircuitPlayground::onListenerRegisteredEvent);
|
|
|
|
|
|
|
|
for (int i = 0; i < DEVICE_COMPONENT_COUNT; i++) {
|
|
|
|
if (CodalComponent::components[i])
|
2017-10-21 18:30:01 +03:00
|
|
|
CodalComponent::components[i]->init();
|
|
|
|
}
|
2017-10-23 23:48:41 +03:00
|
|
|
|
2017-12-12 20:28:16 +03:00
|
|
|
usb_init();
|
2018-12-08 02:06:12 +03:00
|
|
|
|
|
|
|
auto led = LOOKUP_PIN(LED);
|
|
|
|
if (led) {
|
|
|
|
led->setDigitalValue(0);
|
|
|
|
}
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// 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(Event e) {
|
|
|
|
lastEvent = e;
|
|
|
|
|
|
|
|
auto curr = findBinding(e.source, e.value);
|
2018-05-17 18:58:58 +03:00
|
|
|
auto value = fromInt(e.value);
|
2019-05-17 18:30:54 +03:00
|
|
|
while (curr) {
|
2018-05-17 18:58:58 +03:00
|
|
|
runAction1(curr->action, value);
|
2019-05-17 18:30:54 +03:00
|
|
|
curr = nextBinding(curr->next, e.source, e.value);
|
|
|
|
}
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
2017-11-08 05:15:30 +03:00
|
|
|
void registerWithDal(int id, int event, Action a, int flags) {
|
2017-08-05 00:58:55 +03:00
|
|
|
// first time?
|
|
|
|
if (!findBinding(id, event))
|
2017-11-08 05:15:30 +03:00
|
|
|
devMessageBus.listen(id, event, dispatchEvent, flags);
|
2017-08-05 00:58:55 +03:00
|
|
|
setBinding(id, event, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fiberDone(void *a) {
|
|
|
|
decr((Action)a);
|
2018-11-13 02:34:52 +03:00
|
|
|
unregisterGCPtr((Action)a);
|
2017-08-05 00:58:55 +03:00
|
|
|
release_fiber();
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:25:27 +03:00
|
|
|
void releaseFiber() {
|
2018-11-13 02:34:52 +03:00
|
|
|
release_fiber();
|
2018-01-26 03:25:27 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 20:32:45 +03:00
|
|
|
void sleep_ms(unsigned ms) {
|
2017-08-05 00:58:55 +03:00
|
|
|
fiber_sleep(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_us(uint64_t us) {
|
2018-09-26 04:26:45 +03:00
|
|
|
target_wait_us(us);
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void forever_stub(void *a) {
|
|
|
|
while (true) {
|
|
|
|
runAction0((Action)a);
|
|
|
|
fiber_sleep(20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void runForever(Action a) {
|
|
|
|
if (a != 0) {
|
|
|
|
incr(a);
|
2018-11-13 02:34:52 +03:00
|
|
|
registerGCPtr(a);
|
2017-08-05 00:58:55 +03:00
|
|
|
create_fiber(forever_stub, (void *)a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 00:14:46 +03:00
|
|
|
void runInParallel(Action a) {
|
2017-08-05 00:58:55 +03:00
|
|
|
if (a != 0) {
|
|
|
|
incr(a);
|
2018-11-13 02:34:52 +03:00
|
|
|
registerGCPtr(a);
|
2017-08-05 00:58:55 +03:00
|
|
|
create_fiber((void (*)(void *))runAction0, (void *)a, fiberDone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void waitForEvent(int id, int event) {
|
|
|
|
fiber_wait_for_event(id, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initRuntime() {
|
|
|
|
initCodal();
|
2017-12-12 20:28:16 +03:00
|
|
|
platform_init();
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//%
|
2017-09-26 20:32:45 +03:00
|
|
|
unsigned afterProgramPage() {
|
|
|
|
unsigned ptr = (unsigned)&bytecode[0];
|
2017-08-05 00:58:55 +03:00
|
|
|
ptr += programSize();
|
|
|
|
ptr = (ptr + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getSerialNumber() {
|
|
|
|
return device.getSerialNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
int current_time_ms() {
|
|
|
|
return system_timer_current_time();
|
|
|
|
}
|
2018-11-13 02:34:52 +03:00
|
|
|
|
|
|
|
#ifdef PXT_GC
|
|
|
|
ThreadContext *getThreadContext() {
|
2019-02-06 20:16:31 +03:00
|
|
|
if (!currentFiber)
|
|
|
|
return NULL;
|
2018-11-13 02:34:52 +03:00
|
|
|
return (ThreadContext *)currentFiber->user_data;
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
2018-11-13 02:34:52 +03:00
|
|
|
|
|
|
|
void setThreadContext(ThreadContext *ctx) {
|
|
|
|
currentFiber->user_data = ctx;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:16:09 +03:00
|
|
|
static void *threadAddressFor(codal::Fiber *fib, void *sp) {
|
2018-11-13 02:34:52 +03:00
|
|
|
if (fib == currentFiber)
|
|
|
|
return sp;
|
|
|
|
return (uint8_t *)sp + ((uint8_t *)fib->stack_top - (uint8_t *)tcb_get_stack_base(fib->tcb));
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:21:05 +03:00
|
|
|
void gcProcessStacks(int flags) {
|
2019-02-06 20:16:31 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:16:09 +03:00
|
|
|
int numFibers = codal::list_fibers(NULL);
|
2018-12-08 02:06:12 +03:00
|
|
|
codal::Fiber **fibers = (codal::Fiber **)xmalloc(sizeof(codal::Fiber *) * numFibers);
|
|
|
|
int num2 = codal::list_fibers(fibers);
|
|
|
|
if (numFibers != num2)
|
|
|
|
oops(12);
|
2018-12-01 01:21:05 +03:00
|
|
|
int cnt = 0;
|
2018-11-20 02:16:09 +03:00
|
|
|
|
|
|
|
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);
|
2018-12-01 01:21:05 +03:00
|
|
|
if (flags & 2)
|
|
|
|
DMESG("RS%d:%p/%d", cnt++, ptr, end - ptr);
|
2018-12-08 02:06:12 +03:00
|
|
|
// VLOG("mark: %p - %p", ptr, end);
|
2018-11-20 02:16:09 +03:00
|
|
|
while (ptr < end) {
|
|
|
|
gcProcess(*ptr++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:06:12 +03:00
|
|
|
xfree(fibers);
|
2018-11-20 02:16:09 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-13 02:34:52 +03:00
|
|
|
} // namespace pxt
|