Skip to content
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

basic support for playdate #1162

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
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
19 changes: 12 additions & 7 deletions libs/base/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
#define ALIGN_TO_WORD(x) (((x) + 7) & (~7ULL))
#define VAR_BLOCK_WORDS(vt) ((uint32_t)(vt) >> 2)
#else
#ifdef PLAYDATE
#define HIGH_SHIFT 24
#else
#define HIGH_SHIFT 28
#endif
#define BYTES_TO_WORDS(x) ((x) >> 2)
#define WORDS_TO_BYTES(x) ((x) << 2)
#define ALIGN_TO_WORD(x) (((x) + 3) & (~3U))
#define VAR_BLOCK_WORDS(vt) (((uint32_t)(vt) << 4) >> (4 + 2))
#define VAR_BLOCK_WORDS(vt) (((uint32_t)(vt) << (32 - HIGH_SHIFT)) >> ((32 - HIGH_SHIFT) + 2))
#endif

#define FREE_MASK (1ULL << (HIGH_SHIFT + 3))
Expand All @@ -33,20 +37,20 @@
#define ANY_MARKED_MASK 0x3

// the bit operations should be faster than loading large constants
#define IS_FREE(vt) ((uintptr_t)(vt) >> (HIGH_SHIFT + 3))
#define IS_FREE(vt) (((uintptr_t)(vt) >> (HIGH_SHIFT + 3)) & 1)
#define IS_ARRAY(vt) (((uintptr_t)(vt) >> (HIGH_SHIFT + 2)) & 1)
#define IS_PERMA(vt) (((uintptr_t)(vt) >> (HIGH_SHIFT + 1)) & 1)
#define IS_VAR_BLOCK(vt) ((uintptr_t)(vt) >> (HIGH_SHIFT + 2))
#define IS_VAR_BLOCK(vt) (((uintptr_t)(vt) >> (HIGH_SHIFT + 2)) & 3)
#define IS_MARKED(vt) ((uintptr_t)(vt)&MARKED_MASK)
#define IS_LIVE(vt) (IS_MARKED(vt) || (((uintptr_t)(vt) >> (HIGH_SHIFT)) == 0x6))
#define IS_LIVE(vt) (IS_MARKED(vt) || ((((uintptr_t)(vt) >> (HIGH_SHIFT)) & 0xf) == 0x6))

//#define PXT_GC_DEBUG 1
#ifndef PXT_GC_CHECKS
#define PXT_GC_CHECKS 1
#endif
//#define PXT_GC_STRESS 1

//#define PXT_GC_CHECKS 1
#define PXT_GC_CHECKS 1

#define MARK(v) \
do { \
Expand Down Expand Up @@ -526,6 +530,7 @@ static void sweep(int flags) {
if (sz > (int)maxFreeBlock)
maxFreeBlock = sz;
#ifdef PXT_GC_CHECKS
#pragma GCC diagnostic ignored "-Wclass-memaccess"
memset(start, 0xff, WORDS_TO_BYTES(sz));
#endif
start->vtable = (sz << 2) | FREE_MASK;
Expand Down Expand Up @@ -614,7 +619,7 @@ extern "C" void *realloc(void *ptr, size_t size) {

if (ptr != NULL && mem != NULL) {
auto r = (uintptr_t *)ptr;
GC_CHECK((r[-1] >> (HIGH_SHIFT + 1)) == 3, 41);
GC_CHECK(((r[-1] >> (HIGH_SHIFT + 1)) & 7) == 3, 41);
size_t blockSize = VAR_BLOCK_WORDS(r[-1]);
memcpy(mem, ptr, min(blockSize * sizeof(void *), size));
free(ptr);
Expand Down Expand Up @@ -647,7 +652,7 @@ void *app_alloc(int numbytes) {

void *app_free(void *ptr) {
auto r = (uintptr_t *)ptr;
GC_CHECK((r[-1] >> (HIGH_SHIFT + 1)) == 3, 41);
GC_CHECK(((r[-1] >> (HIGH_SHIFT + 1)) & 7) == 3, 41);
r[-1] |= FREE_MASK;
return r;
}
Expand Down
8 changes: 8 additions & 0 deletions libs/base/pxtbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,16 +500,24 @@ extern const VTable RefAction_vtable;
extern uint8_t *gcBase;
#endif

#ifdef PLAYDATE
extern uintptr_t programEnd;
#endif

inline bool isReadOnly(TValue v) {
#ifdef PXT64
#ifdef PXT_IOS
return !isPointer(v) || (((uintptr_t)v - (uintptr_t)gcBase) >> PXT_IOS_HEAP_ALLOC_BITS) != 0;
#else
return !isPointer(v) || !((uintptr_t)v >> 37);
#endif
#else
#ifdef PLAYDATE
return isTagged(v) || (uintptr_t)v < programEnd;
#else
return isTagged(v) || !((uintptr_t)v >> 28);
#endif
#endif
}

// A base abstract class for ref-counted objects.
Expand Down
83 changes: 83 additions & 0 deletions libs/core---playdate/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
include Makefile.inc

PDX_NAME = pxt.pdx

SCHED = playdate-scheduler
SDK = $(SCHED)/C_API

CPU_OPTIONS = -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -D__FPU_USED=1
DEFINES = \
-DLIGHTWEIGHT_EVENTS=1 \
-DDEVICE_DMESG_BUFFER_SIZE=4096 \
-DDEVICE_DAL_VERSION='"playdate"' \
-DTARGET_PLAYDATE=1 \
-DTARGET_EXTENSION=1 \
-DPD=1
COMMON_FLAGS = -W -Wall -Wno-unused-parameter -Wno-cast-function-type -Ipxtapp \
-I$(SCHED) -Ipxtapp -I. -I$(SDK) -fwrapv \
-g $(CPU_OPTIONS) -O2 \
-falign-functions=16 -fomit-frame-pointer -gdwarf-2 -Wno-unused \
-Wno-unknown-pragmas -fverbose-asm -ffunction-sections -fdata-sections \
$(DEFINES) $(NPM_INCLUDES)
LIBS =
CFLAGS = $(COMMON_FLAGS) -Wstrict-prototypes
CXXFLAGS = $(COMMON_FLAGS) -std=c++11 -fno-rtti -fno-exceptions -fno-unwind-tables -fno-threadsafe-statics
LDFLAGS = -Wl,--gc-sections,--cref,--no-warn-mismatch -g -T$(SCHED)/link_map.ld $(CPU_OPTIONS)
PREF = arm-none-eabi-
CC = $(PREF)gcc
LD = $(PREF)gcc
LIBSTDCPP =
NPM_LIBS = $(wildcard node_modules/*/lib/*.a)
NPM_INCLUDES = $(addprefix -I, $(wildcard node_modules/*/include))

SCHED_C_SRC = $(wildcard $(SCHED)/*.c)
SCHED_CPP_SRC = $(wildcard $(SCHED)/*.cpp)
PXT_OBJS += bld/$(SCHED)/CortexContextSwitch.o
PXT_OBJS += $(addprefix bld/,$(SCHED_C_SRC:.c=.o))
PXT_OBJS += $(addprefix bld/,$(SCHED_CPP_SRC:.cpp=.o))

EXE = bld/pxt-app.elf
HEX = $(EXE:.elf=.hex)
BIN = $(EXE:.elf=.bin)

DEPS = $(PXT_HEADERS) package.json Makefile Makefile.inc $(wildcard $(SCHED)/*.h)

all: pdx

$(EXE): $(PXT_OBJS)
$(LD) -o $(EXE) $(LDFLAGS) -Wl,-Map,$(EXE:.elf=.map) $(PXT_OBJS) $(LIBSTDCPP) $(LIBS) $(NPM_LIBS)
$(PREF)objcopy -O binary $(EXE) $(BIN)
node -p 'require("fs").readFileSync("$(BIN)").toString("hex")' > $(HEX)
@ls -l $(BIN)

pdx: $(EXE)
rm -rf $(PDX_NAME)
mkdir -p $(PDX_NAME)
cp $(BIN) $(PDX_NAME)/pdex.bin
cp $(SCHED)/pdxinfo $(PDX_NAME)

PD_SERIAL = $(wildcard /dev/cu.usbmodem00000*1)
deploy: all
ifneq ($(PD_SERIAL),)
echo datadisk > $(PD_SERIAL)
sleep 3
endif
cp -r $(PDX_NAME) /Volumes/PLAYDATE/Games
diskutil eject /Volumes/PLAYDATE

r: deploy

clean:
rm -rf bld

bld/%.o: %.cpp $(DEPS)
@mkdir -p $(dir $@)
$(CC) $(CXXFLAGS) -c $< -o $@

bld/%.o: %.s $(DEPS)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@

bld/%.o: %.c $(DEPS)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
Loading