From: falcovorbis <fal...@us...> - 2023-11-23 20:38:32
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "A pseudo Operating System for the Dreamcast.". The branch, master has been updated via e0f17b4778bfa4b45e457fc2bde1efc7a792d27e (commit) from b6d26daa07a7e1b0342be19a5a8787d978e9b62b (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit e0f17b4778bfa4b45e457fc2bde1efc7a792d27e Author: Aaron Glazer <Aar...@us...> Date: Wed Nov 22 20:47:41 2023 -0800 Add basic micropython example. (#382) * Add basic micropython example. * respond to comments * add micropython to examples/dreamcast/Makefile and wrap whole demo w/ exception catcher * use EXIT_SUCCESS and print to stderr * update CHANGELOG * update copyright ----------------------------------------------------------------------- Summary of changes: doc/CHANGELOG | 1 + examples/dreamcast/Makefile | 4 +- examples/dreamcast/micropython/Makefile | 37 ++++++++++ examples/dreamcast/micropython/example.c | 91 ++++++++++++++++++++++++ examples/dreamcast/micropython/romdisk/script.py | 26 +++++++ 5 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 examples/dreamcast/micropython/Makefile create mode 100644 examples/dreamcast/micropython/example.c create mode 100644 examples/dreamcast/micropython/romdisk/script.py diff --git a/doc/CHANGELOG b/doc/CHANGELOG index f8c8c7b..d3961eb 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -209,6 +209,7 @@ KallistiOS version 2.1.0 ----------------------------------------------- content from <netinet/in.h> [LS] - *** Added __weak, __used, likely() and unlikely() [FG] - DC Added Maple-specific KOS_INIT_FLAGS() which allow for GC-ing unused drivers [FG] +- DC Added basic example for micropython KOS port [Aaron Glazer == AG] KallistiOS version 2.0.0 ----------------------------------------------- - DC Broadband Adapter driver fixes [Megan Potter == MP] diff --git a/examples/dreamcast/Makefile b/examples/dreamcast/Makefile index 68e59e4..dc867de 100644 --- a/examples/dreamcast/Makefile +++ b/examples/dreamcast/Makefile @@ -5,7 +5,9 @@ # DIRS = 2ndmix basic libdream kgl hello sound png network vmu conio pvr video \ - lua parallax modem dreameye sd g1ata lightgun keyboard sdl random rumble + lua parallax modem dreameye sd g1ata lightgun keyboard sdl random rumble \ + micropython + ifdef KOS_CCPLUS DIRS += cpp tsunami endif diff --git a/examples/dreamcast/micropython/Makefile b/examples/dreamcast/micropython/Makefile new file mode 100644 index 0000000..f00af49 --- /dev/null +++ b/examples/dreamcast/micropython/Makefile @@ -0,0 +1,37 @@ +# KallistiOS ##version## +# +# examples/dreamcast/micropython/Makefile +# Copyright (C) 2023 Aaron Glazer +# + +TARGET = micropython.elf + +CFLAGS += -std=c99 +CFLAGS += -I. +CFLAGS += -Wall -Og + +SRCS += example.c +OBJS += example.o romdisk.o +KOS_ROMDISK_DIR = romdisk + +# The rm-elf step is to remove the target before building, to force the +# re-creation of the rom disk. +all: rm-elf $(TARGET) + +include $(KOS_BASE)/Makefile.rules + +clean: rm-elf + -rm -f $(OBJS) + +rm-elf: + -rm -f $(TARGET) romdisk.* + +$(TARGET): $(OBJS) + kos-cc -o $(TARGET) $(OBJS) -lmicropython + +run: $(TARGET) + $(KOS_LOADER) $(TARGET) + +dist: $(TARGET) + -rm -f $(OBJS) romdisk.img + $(KOS_STRIP) $(TARGET) diff --git a/examples/dreamcast/micropython/example.c b/examples/dreamcast/micropython/example.c new file mode 100644 index 0000000..ae50bbd --- /dev/null +++ b/examples/dreamcast/micropython/example.c @@ -0,0 +1,91 @@ +/* KallistiOS ##version## + + example.c + Copyright (C) 2023 Aaron Glazer + + This example demonstrates basic usage of KOS's port + of micropython via its C API. +*/ + +#include <stdio.h> +#include <stdlib.h> + +#include <kos.h> + +#include <micropython/py/parse.h> +#include <micropython/py/lexer.h> +#include <micropython/py/gc.h> +#include <micropython/py/nlr.h> +#include <micropython/py/compile.h> +#include <micropython/py/runtime.h> +#include <micropython/py/stackctrl.h> +#include <micropython/py/qstr.h> +#include <micropython/py/obj.h> + +static char mp_heap[8 * 1024]; + +static void load_module(void) { + mp_lexer_t *lex = mp_lexer_new_from_file(qstr_from_str("/rd/script.py")); + mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT); + mp_obj_t mod = mp_compile(&parse_tree, lex->source_name, false); + mp_call_function_0(mod); +} + +static void demo(void) { + printf("(entering script)\n"); + load_module(); + printf("(exited script)\n"); + + mp_obj_t fn; + mp_obj_t res; + mp_obj_t five = mp_obj_new_int(5); + + fn = mp_load_name(qstr_from_str("f")); + res = mp_call_function_1(fn, five); + printf("f(5): "); + mp_obj_print(res, PRINT_REPR); + printf("\n"); + + fn = mp_load_name(qstr_from_str("g")); + res = mp_call_function_1(fn, five); + printf("g(5): "); + mp_obj_print(res, PRINT_REPR); + printf("\n"); + + mp_obj_t sum_obj = mp_load_name(qstr_from_str("sum")); + mp_int_t sum = mp_obj_int_get_checked(sum_obj); + printf("sum: %d\n", sum); + + printf("globals:\n"); + mp_obj_dict_t* globals = mp_globals_get(); + mp_map_t* map = &globals->map; + for (size_t i = 0; i < map->alloc; i++) { + printf(" "); + if (map->table[i].key != MP_OBJ_NULL) { + mp_obj_print(map->table[i].key, PRINT_REPR); + } else { + printf("(nil)"); + } + printf(": %p\n", map->table[i].value); + } +} + +int main(int argc, const char* argv[]) { + mp_stack_ctrl_init(); + gc_init(mp_heap, mp_heap + sizeof(mp_heap)); + mp_init(); + + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + demo(); + nlr_pop(); + } else { + fprintf(stderr, "demo ran into an uncaught exception!\n"); + mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); + exit(EXIT_FAILURE); + } + + mp_deinit(); + + return EXIT_SUCCESS; +} diff --git a/examples/dreamcast/micropython/romdisk/script.py b/examples/dreamcast/micropython/romdisk/script.py new file mode 100644 index 0000000..3ee19fc --- /dev/null +++ b/examples/dreamcast/micropython/romdisk/script.py @@ -0,0 +1,26 @@ +# KallistiOS ##version## +# +# script.py +# Copyright (C) 2023 Aaron Glazer +# + +def f(n): + return n * n +def g(n): + return n ** n + +sum = 0 +for i in range(10): + print('iter {:08}'.format(i)) + sum += i + +try: + 1//0 +except Exception as er: + print('caught exception', repr(er)) + +import gc +print('run GC collect') +gc.collect() + +print('finish') hooks/post-receive -- A pseudo Operating System for the Dreamcast. |