From 5130b81eecfdd502e8f500f58f39356290f970a3 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Wed, 16 Apr 2014 17:27:23 +0100 Subject: [PATCH] cortex-m3-qemu: a working port --- cortex-m3-qemu/Makefile | 54 +++ cortex-m3-qemu/gccollect.c | 56 +++ cortex-m3-qemu/gccollect.h | 17 + cortex-m3-qemu/gchelper.s | 62 +++ cortex-m3-qemu/help.c | 95 ++++ cortex-m3-qemu/main.c | 127 ++++++ cortex-m3-qemu/math.c | 791 ++++++++++++++++++++++++++++++++++ cortex-m3-qemu/mpconfigport.h | 33 ++ cortex-m3-qemu/pyexec.c | 237 ++++++++++ cortex-m3-qemu/pyexec.h | 12 + cortex-m3-qemu/qstrdefsport.h | 148 +++++++ 11 files changed, 1632 insertions(+) create mode 100644 cortex-m3-qemu/Makefile create mode 100644 cortex-m3-qemu/gccollect.c create mode 100644 cortex-m3-qemu/gccollect.h create mode 100644 cortex-m3-qemu/gchelper.s create mode 100644 cortex-m3-qemu/help.c create mode 100644 cortex-m3-qemu/main.c create mode 100644 cortex-m3-qemu/math.c create mode 100644 cortex-m3-qemu/mpconfigport.h create mode 100644 cortex-m3-qemu/pyexec.c create mode 100644 cortex-m3-qemu/pyexec.h create mode 100644 cortex-m3-qemu/qstrdefsport.h diff --git a/cortex-m3-qemu/Makefile b/cortex-m3-qemu/Makefile new file mode 100644 index 0000000000..64c3caced3 --- /dev/null +++ b/cortex-m3-qemu/Makefile @@ -0,0 +1,54 @@ +include ../py/mkenv.mk +-include mpconfigport.mk + +# qstr definitions (must come before including py.mk) +QSTR_DEFS = qstrdefsport.h + +# include py core make definitions +include ../py/py.mk + +CROSS_COMPILE = arm-none-eabi- + +CFLAGS_CORTEX_M3 = -mthumb -mcpu=cortex-m3 +CFLAGS = -I. -I$(PY_SRC) -Wall -ansi -std=gnu99 $(CFLAGS_CORTEX_M3) $(COPT) + +#Debugging/Optimization +ifeq ($(DEBUG), 1) +CFLAGS += -g -DPENDSV_DEBUG +COPT = -O0 +else +COPT += -Os -DNDEBUG +endif + +#LDFLAGS = --nostdlib -T stm32f405.ld -Map=$(@:.elf=.map) --cref +LDFLAGS = -lm -T generic-m-hosted.ld # -Map=$(@:.elf=.map) --cref +LIBS = + +# uncomment this if you want libgcc +#LIBS += $(shell $(CC) -print-libgcc-file-name) + +SRC_C = \ + help.c \ + math.c \ + + #gccollect.c \ + #pyexec.c \ + +SRC_S = \ + gchelper.s \ + +OBJ = +OBJ += $(PY_O) +OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) + +all: $(BUILD)/flash.elf + +run: + qemu-system-arm -cpu cortex-m3 -nographic -monitor null -serial null -semihosting -kernel $(BUILD)/flash.elf + +$(BUILD)/flash.elf: $(OBJ) + $(Q)$(CC) $(CFLAGS) $(LDFLAGS) main.c -o $@ $(OBJ) $(LIBS) + $(Q)$(SIZE) $@ + +include ../py/mkrules.mk diff --git a/cortex-m3-qemu/gccollect.c b/cortex-m3-qemu/gccollect.c new file mode 100644 index 0000000000..7a48c18f17 --- /dev/null +++ b/cortex-m3-qemu/gccollect.c @@ -0,0 +1,56 @@ +#include + +//#include + +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "gc.h" +#include "gccollect.h" + +machine_uint_t gc_helper_get_regs_and_sp(machine_uint_t *regs); + +// obsolete +// void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end); + +void gc_collect(void) { + // get current time, in case we want to time the GC + uint32_t start = HAL_GetTick(); + + // start the GC + gc_collect_start(); + + // scan everything in RAM before the heap + // this includes the data and bss segments + // TODO possibly don't need to scan data, since all pointers should start out NULL and be in bss + gc_collect_root((void**)&_ram_start, ((uint32_t)&_ebss - (uint32_t)&_ram_start) / sizeof(uint32_t)); + + // get the registers and the sp + machine_uint_t regs[10]; + machine_uint_t sp = gc_helper_get_regs_and_sp(regs); + + // trace the stack, including the registers (since they live on the stack in this function) + gc_collect_root((void**)sp, ((uint32_t)&_ram_end - sp) / sizeof(uint32_t)); + + // end the GC + gc_collect_end(); + + if (0) { + // print GC info + uint32_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly + gc_info_t info; + gc_info(&info); + printf("GC@%lu %lums\n", start, ticks); + printf(" %lu total\n", info.total); + printf(" %lu : %lu\n", info.used, info.free); + printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block); + } +} + +static mp_obj_t pyb_gc(void) { + gc_collect(); + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc); diff --git a/cortex-m3-qemu/gccollect.h b/cortex-m3-qemu/gccollect.h new file mode 100644 index 0000000000..9aa0a44413 --- /dev/null +++ b/cortex-m3-qemu/gccollect.h @@ -0,0 +1,17 @@ +// variables defining memory layout +// (these probably belong somewhere else...) +extern uint32_t _etext; +extern uint32_t _sidata; +extern uint32_t _ram_start; +extern uint32_t _sdata; +extern uint32_t _edata; +extern uint32_t _sbss; +extern uint32_t _ebss; +extern uint32_t _heap_start; +extern uint32_t _heap_end; +extern uint32_t _estack; +extern uint32_t _ram_end; + +void gc_collect(void); + +MP_DECLARE_CONST_FUN_OBJ(pyb_gc_obj); diff --git a/cortex-m3-qemu/gchelper.s b/cortex-m3-qemu/gchelper.s new file mode 100644 index 0000000000..6baedcdd0e --- /dev/null +++ b/cortex-m3-qemu/gchelper.s @@ -0,0 +1,62 @@ + .syntax unified + .cpu cortex-m4 + .thumb + .text + .align 2 + +@ uint gc_helper_get_regs_and_sp(r0=uint regs[10]) + .global gc_helper_get_regs_and_sp + .thumb + .thumb_func + .type gc_helper_get_regs_and_sp, %function +gc_helper_get_regs_and_sp: + @ store registers into given array + str r4, [r0], #4 + str r5, [r0], #4 + str r6, [r0], #4 + str r7, [r0], #4 + str r8, [r0], #4 + str r9, [r0], #4 + str r10, [r0], #4 + str r11, [r0], #4 + str r12, [r0], #4 + str r13, [r0], #4 + + @ return the sp + mov r0, sp + bx lr + + +@ this next function is now obsolete + + .size gc_helper_get_regs_and_clean_stack, .-gc_helper_get_regs_and_clean_stack +@ void gc_helper_get_regs_and_clean_stack(r0=uint regs[10], r1=heap_end) + .global gc_helper_get_regs_and_clean_stack + .thumb + .thumb_func + .type gc_helper_get_regs_and_clean_stack, %function +gc_helper_get_regs_and_clean_stack: + @ store registers into given array + str r4, [r0], #4 + str r5, [r0], #4 + str r6, [r0], #4 + str r7, [r0], #4 + str r8, [r0], #4 + str r9, [r0], #4 + str r10, [r0], #4 + str r11, [r0], #4 + str r12, [r0], #4 + str r13, [r0], #4 + + @ clean the stack from given pointer up to current sp + movs r0, #0 + mov r2, sp + b.n .entry +.loop: + str r0, [r1], #4 +.entry: + cmp r1, r2 + bcc.n .loop + bx lr + + .size gc_helper_get_regs_and_clean_stack, .-gc_helper_get_regs_and_clean_stack diff --git a/cortex-m3-qemu/help.c b/cortex-m3-qemu/help.c new file mode 100644 index 0000000000..76dcb2f4ee --- /dev/null +++ b/cortex-m3-qemu/help.c @@ -0,0 +1,95 @@ +#include + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" + +STATIC const char *help_text = +"Welcome to Micro Python!\n" +"\n" +"For online help please visit http://micropython.org/help/.\n" +"\n" +"Specific commands for the board:\n" +" pyb.info() -- print some general information\n" +" pyb.gc() -- run the garbage collector\n" +" pyb.repl_info(val) -- enable/disable printing of info after each command\n" +" pyb.delay(n) -- wait for n milliseconds\n" +" pyb.udelay(n) -- wait for n microseconds\n" +" pyb.switch() -- return True/False if switch pressed or not\n" +" pyb.switch(f) -- call the given function when the switch is pressed\n" +" pyb.Led(n) -- create Led object for LED n (n=1,2,3,4)\n" +" Led methods: on(), off(), toggle(), intensity()\n" +" pyb.Servo(n) -- create Servo object for servo n (n=1,2,3,4)\n" +" Servo methods: calibrate(...), pulse_width([p]), angle([x, [t]]), speed([x, [t]])\n" +" pyb.Accel() -- create an Accelerometer object\n" +" Accelerometer methods: x(), y(), z(), tilt(), filtered_xyz()\n" +" pyb.rng() -- get a 30-bit hardware random number\n" +" pyb.gpio_in(port, [m]) -- set IO port to input, mode m\n" +" pyb.gpio_out(port, [m]) -- set IO port to output, mode m\n" +" pyb.gpio(port) -- get digital port value\n" +" pyb.gpio(port, val) -- set digital port value, True or False, 1 or 0\n" +" pyb.ADC(port) -- make an analog port object\n" +" ADC methods: read()\n" +"\n" +"Ports are numbered X1-X12, X17-X22, Y1-Y12, or by their MCU name\n" +"Port input modes are: pyb.PULL_NONE, pyb.PULL_UP, pyb.PULL_DOWN\n" +"Port output modes are: pyb.PUSH_PULL, pyb.OPEN_DRAIN\n" +"\n" +"Control commands:\n" +" CTRL-A -- on a blank line, enter raw REPL mode\n" +" CTRL-B -- on a blank line, enter normal REPL mode\n" +" CTRL-C -- interrupt a running program\n" +" CTRL-D -- on a blank line, do a soft reset of the board\n" +"\n" +"For further help on a specific object, type help(obj)\n" +; + +STATIC void pyb_help_print_info_about_object(mp_obj_t name_o, mp_obj_t value) { + printf(" "); + mp_obj_print(name_o, PRINT_STR); + printf(" -- "); + mp_obj_print(value, PRINT_STR); + printf("\n"); +} + +STATIC mp_obj_t pyb_help(uint n_args, const mp_obj_t *args) { + if (n_args == 0) { + // print a general help message + printf("%s", help_text); + + } else { + // try to print something sensible about the given object + + printf("object "); + mp_obj_print(args[0], PRINT_STR); + printf(" is of type %s\n", mp_obj_get_type_str(args[0])); + + mp_map_t *map = NULL; + if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) { + map = mp_obj_dict_get_map(mp_obj_module_get_globals(args[0])); + } else { + mp_obj_type_t *type; + if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) { + type = args[0]; + } else { + type = mp_obj_get_type(args[0]); + } + if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) { + map = mp_obj_dict_get_map(type->locals_dict); + } + } + if (map != NULL) { + for (uint i = 0; i < map->alloc; i++) { + if (map->table[i].key != MP_OBJ_NULL) { + pyb_help_print_info_about_object(map->table[i].key, map->table[i].value); + } + } + } + } + + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, pyb_help); diff --git a/cortex-m3-qemu/main.c b/cortex-m3-qemu/main.c new file mode 100644 index 0000000000..d0e96f8832 --- /dev/null +++ b/cortex-m3-qemu/main.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "lexer.h" +#include "lexerunix.h" +#include "parse.h" +#include "obj.h" +#include "parsehelper.h" +#include "compile.h" +#include "runtime0.h" +#include "runtime.h" +#include "repl.h" +#include "gc.h" + +mp_obj_t mem_info(void) { + printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated()); + return mp_const_none; +} + +mp_obj_t qstr_info(void) { + uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes; + qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); + printf("qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes); + return mp_const_none; +} + +static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { + if (lex == NULL) { + return; + } + + if (0) { + // just tokenise + while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) { + mp_token_show(mp_lexer_cur(lex)); + mp_lexer_to_next(lex); + } + mp_lexer_free(lex); + return; + } + + mp_parse_error_kind_t parse_error_kind; + mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind); + + if (pn == MP_PARSE_NODE_NULL) { + // parse error + mp_parse_show_exception(lex, parse_error_kind); + mp_lexer_free(lex); + return; + } + + qstr source_name = mp_lexer_source_name(lex); + mp_lexer_free(lex); + + /* + printf("----------------\n"); + mp_parse_node_print(pn, 0); + printf("----------------\n"); + */ + + mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_ASM_THUMB, is_repl); + + if (module_fun == mp_const_none) { + // compile error + return; + } + + // execute it + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_call_function_0(module_fun); + nlr_pop(); + } else { + // uncaught exception + mp_obj_print_exception((mp_obj_t)nlr.ret_val); + } +} + +mp_import_stat_t mp_import_stat(const char *path) { + struct stat st; + if (stat(path, &st) == 0) { + if (S_ISDIR(st.st_mode)) { + return MP_IMPORT_STAT_DIR; + } else if (S_ISREG(st.st_mode)) { + return MP_IMPORT_STAT_FILE; + } + } + return MP_IMPORT_STAT_NO_EXIST; +} + +static void do_str(const char *str) { + mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false); + execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false); +} + +void nlr_jump_fail(void *val) { + printf("FATAL: uncaught exception %p\n", val); + //__fatal_error(""); +} + +mp_lexer_t *mp_lexer_new_from_file(const char *filename) { + return NULL; +} + +int main() { + qstr_init(); + mp_init(); + + //mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys); + //mp_obj_t py_argv = mp_obj_new_list(0, NULL); + //mp_store_attr(m_sys, MP_QSTR_argv, py_argv); + + mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info)); + mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info)); + + do_str("print(123)"); +} diff --git a/cortex-m3-qemu/math.c b/cortex-m3-qemu/math.c new file mode 100644 index 0000000000..0226d6674c --- /dev/null +++ b/cortex-m3-qemu/math.c @@ -0,0 +1,791 @@ +#include +#include + +typedef float float_t; +typedef union { + float f; + struct { + uint64_t m : 23; + uint64_t e : 8; + uint64_t s : 1; + }; +} float_s_t; + +typedef union { + double d; + struct { + uint64_t m : 52; + uint64_t e : 11; + uint64_t s : 1; + }; +} double_s_t; + +float sqrtf(float x) { + return x; +} + +// some compilers define log2f in terms of logf +#ifdef log2f +#undef log2f +#endif +float log2f(float x) { return 0; } + +static const float _M_LN10 = 2.30258509299404; // 0x40135d8e +float log10f(float x) { return logf(x) / (float)_M_LN10; } + +float tanhf(float x) { return sinhf(x) / coshf(x); } + +// TODO we need import these functions from some library (eg musl or newlib) +float acoshf(float x) { return 0.0; } +float asinhf(float x) { return 0.0; } +float atanhf(float x) { return 0.0; } +float cosf(float x) { return 0.0; } +float sinf(float x) { return 0.0; } +float tanf(float x) { return 0.0; } +float acosf(float x) { return 0.0; } +float asinf(float x) { return 0.0; } +float atanf(float x) { return 0.0; } +float atan2f(float x, float y) { return 0.0; } +float ceilf(float x) { return 0.0; } +float floorf(float x) { return 0.0; } +float truncf(float x) { return 0.0; } +float fmodf(float x, float y) { return 0.0; } +float tgammaf(float x) { return 0.0; } +float lgammaf(float x) { return 0.0; } +float erff(float x) { return 0.0; } +float erfcf(float x) { return 0.0; } +float modff(float x, float *y) { return 0.0; } +float frexpf(float x, int *exp) { return 0.0; } +float ldexpf(float x, int exp) { return 0.0; } + +/*****************************************************************************/ +/*****************************************************************************/ +// from musl-0.9.15 libm.h +/*****************************************************************************/ +/*****************************************************************************/ + +/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#define FORCE_EVAL(x) do { \ + if (sizeof(x) == sizeof(float)) { \ + volatile float __x; \ + __x = (x); \ + (void)__x; \ + } else if (sizeof(x) == sizeof(double)) { \ + volatile double __x; \ + __x = (x); \ + (void)__x; \ + } else { \ + volatile long double __x; \ + __x = (x); \ + (void)__x; \ + } \ +} while(0) + +/* Get a 32 bit int from a float. */ +#define GET_FLOAT_WORD(w,d) \ +do { \ + union {float f; uint32_t i;} __u; \ + __u.f = (d); \ + (w) = __u.i; \ +} while (0) + +/* Set a float from a 32 bit int. */ +#define SET_FLOAT_WORD(d,w) \ +do { \ + union {float f; uint32_t i;} __u; \ + __u.i = (w); \ + (d) = __u.f; \ +} while (0) + +/*****************************************************************************/ +/*****************************************************************************/ +// __fpclassifyf from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +int __fpclassifyf(float x) +{ + union {float f; uint32_t i;} u = {x}; + int e = u.i>>23 & 0xff; + if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO; + if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE; + return FP_NORMAL; +} + +/*****************************************************************************/ +/*****************************************************************************/ +// scalbnf from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +float scalbnf(float x, int n) +{ + union {float f; uint32_t i;} u; + float_t y = x; + + if (n > 127) { + y *= 0x1p127f; + n -= 127; + if (n > 127) { + y *= 0x1p127f; + n -= 127; + if (n > 127) + n = 127; + } + } else if (n < -126) { + y *= 0x1p-126f; + n += 126; + if (n < -126) { + y *= 0x1p-126f; + n += 126; + if (n < -126) + n = -126; + } + } + u.i = (uint32_t)(0x7f+n)<<23; + x = y * u.f; + return x; +} + +/*****************************************************************************/ +/*****************************************************************************/ +// powf from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +/* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */ +/* + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +static const float +bp[] = {1.0, 1.5,}, +dp_h[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */ +dp_l[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */ +two24 = 16777216.0, /* 0x4b800000 */ +huge = 1.0e30, +tiny = 1.0e-30, +/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ +L1 = 6.0000002384e-01, /* 0x3f19999a */ +L2 = 4.2857143283e-01, /* 0x3edb6db7 */ +L3 = 3.3333334327e-01, /* 0x3eaaaaab */ +L4 = 2.7272811532e-01, /* 0x3e8ba305 */ +L5 = 2.3066075146e-01, /* 0x3e6c3255 */ +L6 = 2.0697501302e-01, /* 0x3e53f142 */ +P1 = 1.6666667163e-01, /* 0x3e2aaaab */ +P2 = -2.7777778450e-03, /* 0xbb360b61 */ +P3 = 6.6137559770e-05, /* 0x388ab355 */ +P4 = -1.6533901999e-06, /* 0xb5ddea0e */ +P5 = 4.1381369442e-08, /* 0x3331bb4c */ +lg2 = 6.9314718246e-01, /* 0x3f317218 */ +lg2_h = 6.93145752e-01, /* 0x3f317200 */ +lg2_l = 1.42860654e-06, /* 0x35bfbe8c */ +ovt = 4.2995665694e-08, /* -(128-log2(ovfl+.5ulp)) */ +cp = 9.6179670095e-01, /* 0x3f76384f =2/(3ln2) */ +cp_h = 9.6191406250e-01, /* 0x3f764000 =12b cp */ +cp_l = -1.1736857402e-04, /* 0xb8f623c6 =tail of cp_h */ +ivln2 = 1.4426950216e+00, /* 0x3fb8aa3b =1/ln2 */ +ivln2_h = 1.4426879883e+00, /* 0x3fb8aa00 =16b 1/ln2*/ +ivln2_l = 7.0526075433e-06; /* 0x36eca570 =1/ln2 tail*/ + +float powf(float x, float y) +{ + float z,ax,z_h,z_l,p_h,p_l; + float y1,t1,t2,r,s,sn,t,u,v,w; + int32_t i,j,k,yisint,n; + int32_t hx,hy,ix,iy,is; + + GET_FLOAT_WORD(hx, x); + GET_FLOAT_WORD(hy, y); + ix = hx & 0x7fffffff; + iy = hy & 0x7fffffff; + + /* x**0 = 1, even if x is NaN */ + if (iy == 0) + return 1.0f; + /* 1**y = 1, even if y is NaN */ + if (hx == 0x3f800000) + return 1.0f; + /* NaN if either arg is NaN */ + if (ix > 0x7f800000 || iy > 0x7f800000) + return x + y; + + /* determine if y is an odd int when x < 0 + * yisint = 0 ... y is not an integer + * yisint = 1 ... y is an odd int + * yisint = 2 ... y is an even int + */ + yisint = 0; + if (hx < 0) { + if (iy >= 0x4b800000) + yisint = 2; /* even integer y */ + else if (iy >= 0x3f800000) { + k = (iy>>23) - 0x7f; /* exponent */ + j = iy>>(23-k); + if ((j<<(23-k)) == iy) + yisint = 2 - (j & 1); + } + } + + /* special value of y */ + if (iy == 0x7f800000) { /* y is +-inf */ + if (ix == 0x3f800000) /* (-1)**+-inf is 1 */ + return 1.0f; + else if (ix > 0x3f800000) /* (|x|>1)**+-inf = inf,0 */ + return hy >= 0 ? y : 0.0f; + else if (ix != 0) /* (|x|<1)**+-inf = 0,inf if x!=0 */ + return hy >= 0 ? 0.0f: -y; + } + if (iy == 0x3f800000) /* y is +-1 */ + return hy >= 0 ? x : 1.0f/x; + if (hy == 0x40000000) /* y is 2 */ + return x*x; + if (hy == 0x3f000000) { /* y is 0.5 */ + if (hx >= 0) /* x >= +0 */ + return sqrtf(x); + } + + ax = fabsf(x); + /* special value of x */ + if (ix == 0x7f800000 || ix == 0 || ix == 0x3f800000) { /* x is +-0,+-inf,+-1 */ + z = ax; + if (hy < 0) /* z = (1/|x|) */ + z = 1.0f/z; + if (hx < 0) { + if (((ix-0x3f800000)|yisint) == 0) { + z = (z-z)/(z-z); /* (-1)**non-int is NaN */ + } else if (yisint == 1) + z = -z; /* (x<0)**odd = -(|x|**odd) */ + } + return z; + } + + sn = 1.0f; /* sign of result */ + if (hx < 0) { + if (yisint == 0) /* (x<0)**(non-int) is NaN */ + return (x-x)/(x-x); + if (yisint == 1) /* (x<0)**(odd int) */ + sn = -1.0f; + } + + /* |y| is huge */ + if (iy > 0x4d000000) { /* if |y| > 2**27 */ + /* over/underflow if x is not close to one */ + if (ix < 0x3f7ffff8) + return hy < 0 ? sn*huge*huge : sn*tiny*tiny; + if (ix > 0x3f800007) + return hy > 0 ? sn*huge*huge : sn*tiny*tiny; + /* now |1-x| is tiny <= 2**-20, suffice to compute + log(x) by x-x^2/2+x^3/3-x^4/4 */ + t = ax - 1; /* t has 20 trailing zeros */ + w = (t*t)*(0.5f - t*(0.333333333333f - t*0.25f)); + u = ivln2_h*t; /* ivln2_h has 16 sig. bits */ + v = t*ivln2_l - w*ivln2; + t1 = u + v; + GET_FLOAT_WORD(is, t1); + SET_FLOAT_WORD(t1, is & 0xfffff000); + t2 = v - (t1-u); + } else { + float s2,s_h,s_l,t_h,t_l; + n = 0; + /* take care subnormal number */ + if (ix < 0x00800000) { + ax *= two24; + n -= 24; + GET_FLOAT_WORD(ix, ax); + } + n += ((ix)>>23) - 0x7f; + j = ix & 0x007fffff; + /* determine interval */ + ix = j | 0x3f800000; /* normalize ix */ + if (j <= 0x1cc471) /* |x|>1) & 0xfffff000) | 0x20000000; + SET_FLOAT_WORD(t_h, is + 0x00400000 + (k<<21)); + t_l = ax - (t_h - bp[k]); + s_l = v*((u - s_h*t_h) - s_h*t_l); + /* compute log(ax) */ + s2 = s*s; + r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); + r += s_l*(s_h+s); + s2 = s_h*s_h; + t_h = 3.0f + s2 + r; + GET_FLOAT_WORD(is, t_h); + SET_FLOAT_WORD(t_h, is & 0xfffff000); + t_l = r - ((t_h - 3.0f) - s2); + /* u+v = s*(1+...) */ + u = s_h*t_h; + v = s_l*t_h + t_l*s; + /* 2/(3log2)*(s+...) */ + p_h = u + v; + GET_FLOAT_WORD(is, p_h); + SET_FLOAT_WORD(p_h, is & 0xfffff000); + p_l = v - (p_h - u); + z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ + z_l = cp_l*p_h + p_l*cp+dp_l[k]; + /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ + t = (float)n; + t1 = (((z_h + z_l) + dp_h[k]) + t); + GET_FLOAT_WORD(is, t1); + SET_FLOAT_WORD(t1, is & 0xfffff000); + t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); + } + + /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ + GET_FLOAT_WORD(is, y); + SET_FLOAT_WORD(y1, is & 0xfffff000); + p_l = (y-y1)*t1 + y*t2; + p_h = y1*t1; + z = p_l + p_h; + GET_FLOAT_WORD(j, z); + if (j > 0x43000000) /* if z > 128 */ + return sn*huge*huge; /* overflow */ + else if (j == 0x43000000) { /* if z == 128 */ + if (p_l + ovt > z - p_h) + return sn*huge*huge; /* overflow */ + } else if ((j&0x7fffffff) > 0x43160000) /* z < -150 */ // FIXME: check should be (uint32_t)j > 0xc3160000 + return sn*tiny*tiny; /* underflow */ + else if (j == 0xc3160000) { /* z == -150 */ + if (p_l <= z-p_h) + return sn*tiny*tiny; /* underflow */ + } + /* + * compute 2**(p_h+p_l) + */ + i = j & 0x7fffffff; + k = (i>>23) - 0x7f; + n = 0; + if (i > 0x3f000000) { /* if |z| > 0.5, set n = [z+0.5] */ + n = j + (0x00800000>>(k+1)); + k = ((n&0x7fffffff)>>23) - 0x7f; /* new k for n */ + SET_FLOAT_WORD(t, n & ~(0x007fffff>>k)); + n = ((n&0x007fffff)|0x00800000)>>(23-k); + if (j < 0) + n = -n; + p_h -= t; + } + t = p_l + p_h; + GET_FLOAT_WORD(is, t); + SET_FLOAT_WORD(t, is & 0xffff8000); + u = t*lg2_h; + v = (p_l-(t-p_h))*lg2 + t*lg2_l; + z = u + v; + w = v - (z - u); + t = z*z; + t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); + r = (z*t1)/(t1-2.0f) - (w+z*w); + z = 1.0f - (r - z); + GET_FLOAT_WORD(j, z); + j += n<<23; + if ((j>>23) <= 0) /* subnormal output */ + z = scalbnf(z, n); + else + SET_FLOAT_WORD(z, j); + return sn*z; +} + +/*****************************************************************************/ +/*****************************************************************************/ +// expf from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +/* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */ +/* + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +static const float +half[2] = {0.5,-0.5}, +ln2hi = 6.9314575195e-1f, /* 0x3f317200 */ +ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */ +invln2 = 1.4426950216e+0f, /* 0x3fb8aa3b */ +/* + * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]: + * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74 + */ +expf_P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */ +expf_P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */ + +float expf(float x) +{ + float_t hi, lo, c, xx, y; + int k, sign; + uint32_t hx; + + GET_FLOAT_WORD(hx, x); + sign = hx >> 31; /* sign bit of x */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* special cases */ + if (hx >= 0x42aeac50) { /* if |x| >= -87.33655f or NaN */ + if (hx >= 0x42b17218 && !sign) { /* x >= 88.722839f */ + /* overflow */ + x *= 0x1p127f; + return x; + } + if (sign) { + /* underflow */ + FORCE_EVAL(-0x1p-149f/x); + if (hx >= 0x42cff1b5) /* x <= -103.972084f */ + return 0; + } + } + + /* argument reduction */ + if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ + if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */ + k = invln2*x + half[sign]; + else + k = 1 - sign - sign; + hi = x - k*ln2hi; /* k*ln2hi is exact here */ + lo = k*ln2lo; + x = hi - lo; + } else if (hx > 0x39000000) { /* |x| > 2**-14 */ + k = 0; + hi = x; + lo = 0; + } else { + /* raise inexact */ + FORCE_EVAL(0x1p127f + x); + return 1 + x; + } + + /* x is now in primary range */ + xx = x*x; + c = x - xx*(expf_P1+xx*expf_P2); + y = 1 + (x*c/(2-c) - lo + hi); + if (k == 0) + return y; + return scalbnf(y, k); +} + +/*****************************************************************************/ +/*****************************************************************************/ +// expm1f from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +/* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */ +/* + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +static const float +o_threshold = 8.8721679688e+01, /* 0x42b17180 */ +ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ +ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ +//invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */ +/* + * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]: + * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04 + * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c): + */ +Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */ +Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */ + +float expm1f(float x) +{ + float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk; + union {float f; uint32_t i;} u = {x}; + uint32_t hx = u.i & 0x7fffffff; + int k, sign = u.i >> 31; + + /* filter out huge and non-finite argument */ + if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */ + if (hx > 0x7f800000) /* NaN */ + return x; + if (sign) + return -1; + if (x > o_threshold) { + x *= 0x1p127f; + return x; + } + } + + /* argument reduction */ + if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ + if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ + if (!sign) { + hi = x - ln2_hi; + lo = ln2_lo; + k = 1; + } else { + hi = x + ln2_hi; + lo = -ln2_lo; + k = -1; + } + } else { + k = invln2*x + (sign ? -0.5f : 0.5f); + t = k; + hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ + lo = t*ln2_lo; + } + x = hi-lo; + c = (hi-x)-lo; + } else if (hx < 0x33000000) { /* when |x|<2**-25, return x */ + if (hx < 0x00800000) + FORCE_EVAL(x*x); + return x; + } else + k = 0; + + /* x is now in primary range */ + hfx = 0.5f*x; + hxs = x*hfx; + r1 = 1.0f+hxs*(Q1+hxs*Q2); + t = 3.0f - r1*hfx; + e = hxs*((r1-t)/(6.0f - x*t)); + if (k == 0) /* c is 0 */ + return x - (x*e-hxs); + e = x*(e-c) - c; + e -= hxs; + /* exp(x) ~ 2^k (x_reduced - e + 1) */ + if (k == -1) + return 0.5f*(x-e) - 0.5f; + if (k == 1) { + if (x < -0.25f) + return -2.0f*(e-(x+0.5f)); + return 1.0f + 2.0f*(x-e); + } + u.i = (0x7f+k)<<23; /* 2^k */ + twopk = u.f; + if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */ + y = x - e + 1.0f; + if (k == 128) + y = y*2.0f*0x1p127f; + else + y = y*twopk; + return y - 1.0f; + } + u.i = (0x7f-k)<<23; /* 2^-k */ + if (k < 23) + y = (x-e+(1-u.f))*twopk; + else + y = (x-(e+u.f)+1)*twopk; + return y; +} + +/*****************************************************************************/ +/*****************************************************************************/ +// __expo2f from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */ +static const int k = 235; +static const float kln2 = 0x1.45c778p+7f; + +/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */ +float __expo2f(float x) +{ + float scale; + + /* note that k is odd and scale*scale overflows */ + SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23); + /* exp(x - k ln2) * 2**(k-1) */ + return expf(x - kln2) * scale * scale; +} + +/*****************************************************************************/ +/*****************************************************************************/ +// logf from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +/* origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */ +/* + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +static const float +/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */ +Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */ +Lg2 = 0xccce13.0p-25, /* 0.40000972152 */ +Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */ +Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */ + +float logf(float x) +{ + union {float f; uint32_t i;} u = {x}; + float_t hfsq,f,s,z,R,w,t1,t2,dk; + uint32_t ix; + int k; + + ix = u.i; + k = 0; + if (ix < 0x00800000 || ix>>31) { /* x < 2**-126 */ + if (ix<<1 == 0) + return -1/(x*x); /* log(+-0)=-inf */ + if (ix>>31) + return (x-x)/0.0f; /* log(-#) = NaN */ + /* subnormal number, scale up x */ + k -= 25; + x *= 0x1p25f; + u.f = x; + ix = u.i; + } else if (ix >= 0x7f800000) { + return x; + } else if (ix == 0x3f800000) + return 0; + + /* reduce x into [sqrt(2)/2, sqrt(2)] */ + ix += 0x3f800000 - 0x3f3504f3; + k += (int)(ix>>23) - 0x7f; + ix = (ix&0x007fffff) + 0x3f3504f3; + u.i = ix; + x = u.f; + + f = x - 1.0f; + s = f/(2.0f + f); + z = s*s; + w = z*z; + t1= w*(Lg2+w*Lg4); + t2= z*(Lg1+w*Lg3); + R = t2 + t1; + hfsq = 0.5f*f*f; + dk = k; + return s*(hfsq+R) + dk*ln2_lo - hfsq + f + dk*ln2_hi; +} + +/*****************************************************************************/ +/*****************************************************************************/ +// coshf from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +float coshf(float x) +{ + union {float f; uint32_t i;} u = {.f = x}; + uint32_t w; + float t; + + /* |x| */ + u.i &= 0x7fffffff; + x = u.f; + w = u.i; + + /* |x| < log(2) */ + if (w < 0x3f317217) { + if (w < 0x3f800000 - (12<<23)) { + FORCE_EVAL(x + 0x1p120f); + return 1; + } + t = expm1f(x); + return 1 + t*t/(2*(1+t)); + } + + /* |x| < log(FLT_MAX) */ + if (w < 0x42b17217) { + t = expf(x); + return 0.5f*(t + 1/t); + } + + /* |x| > log(FLT_MAX) or nan */ + t = __expo2f(x); + return t; +} + +/*****************************************************************************/ +/*****************************************************************************/ +// sinhf from musl-0.9.15 +/*****************************************************************************/ +/*****************************************************************************/ + +float sinhf(float x) +{ + union {float f; uint32_t i;} u = {.f = x}; + uint32_t w; + float t, h, absx; + + h = 0.5; + if (u.i >> 31) + h = -h; + /* |x| */ + u.i &= 0x7fffffff; + absx = u.f; + w = u.i; + + /* |x| < log(FLT_MAX) */ + if (w < 0x42b17217) { + t = expm1f(absx); + if (w < 0x3f800000) { + if (w < 0x3f800000 - (12<<23)) + return x; + return h*(2*t - t*t/(t+1)); + } + return h*(t + t/(t+1)); + } + + /* |x| > logf(FLT_MAX) or nan */ + t = 2*h*__expo2f(absx); + return t; +} diff --git a/cortex-m3-qemu/mpconfigport.h b/cortex-m3-qemu/mpconfigport.h new file mode 100644 index 0000000000..ad7bb74df0 --- /dev/null +++ b/cortex-m3-qemu/mpconfigport.h @@ -0,0 +1,33 @@ +#include + +// options to control how Micro Python is built + +#define MICROPY_EMIT_THUMB (0) +#define MICROPY_EMIT_INLINE_THUMB (0) +#define MICROPY_MEM_STATS (0) +#define MICROPY_DEBUG_PRINTERS (0) +#define MICROPY_ENABLE_GC (0) +#define MICROPY_ENABLE_FINALISER (0) +#define MICROPY_ENABLE_REPL_HELPERS (1) +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) +#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) +#define MICROPY_PATH_MAX (128) +#define MICROPY_ENABLE_MOD_IO (0) + +// extra built in names to add to the global namespace +extern const struct _mp_obj_fun_native_t mp_builtin_help_obj; +#define MICROPY_EXTRA_BUILTINS \ + { MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj }, + +// type definitions for the specific machine + +#define BYTES_PER_WORD (4) + +#define UINT_FMT "%lu" +#define INT_FMT "%ld" + +typedef int32_t machine_int_t; // must be pointer size +typedef uint32_t machine_uint_t; // must be pointer size +typedef void *machine_ptr_t; // must be of pointer size +typedef const void *machine_const_ptr_t; // must be of pointer size + diff --git a/cortex-m3-qemu/pyexec.c b/cortex-m3-qemu/pyexec.c new file mode 100644 index 0000000000..298e58a5fd --- /dev/null +++ b/cortex-m3-qemu/pyexec.c @@ -0,0 +1,237 @@ +#include +#include + +#include + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "misc.h" +#include "lexer.h" +#include "parse.h" +#include "obj.h" +#include "parsehelper.h" +#include "compile.h" +#include "runtime.h" +#include "repl.h" +#include "gc.h" +#include "gccollect.h" +#include "systick.h" +#include "pybstdio.h" +#include "readline.h" +#include "pyexec.h" +#include "storage.h" +#include "usb.h" +#include "build/py/py-version.h" + +pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; +STATIC bool repl_display_debugging_info = 0; + +// parses, compiles and executes the code in the lexer +// frees the lexer before returning +bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { + mp_parse_error_kind_t parse_error_kind; + mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind); + qstr source_name = mp_lexer_source_name(lex); + + if (pn == MP_PARSE_NODE_NULL) { + // parse error + mp_parse_show_exception(lex, parse_error_kind); + mp_lexer_free(lex); + return false; + } + + mp_lexer_free(lex); + + mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl); + mp_parse_node_free(pn); + + if (module_fun == mp_const_none) { + return false; + } + + nlr_buf_t nlr; + bool ret; + uint32_t start = HAL_GetTick(); + if (nlr_push(&nlr) == 0) { + usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); // allow ctrl-C to interrupt us + mp_call_function_0(module_fun); + usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt + nlr_pop(); + ret = true; + } else { + // uncaught exception + // FIXME it could be that an interrupt happens just before we disable it here + usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt + mp_obj_print_exception((mp_obj_t)nlr.ret_val); + ret = false; + } + + // display debugging info if wanted + if (is_repl && repl_display_debugging_info) { + uint32_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly + printf("took %lu ms\n", ticks); + gc_collect(); + // qstr info + { + uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes; + qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); + printf("qstr:\n n_pool=%u\n n_qstr=%u\n n_str_data_bytes=%u\n n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes); + } + + // GC info + { + gc_info_t info; + gc_info(&info); + printf("GC:\n"); + printf(" %lu total\n", info.total); + printf(" %lu : %lu\n", info.used, info.free); + printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block); + } + } + + return ret; +} + +int pyexec_raw_repl(void) { + vstr_t line; + vstr_init(&line, 32); + +raw_repl_reset: + stdout_tx_str("raw REPL; CTRL-B to exit\r\n"); + + for (;;) { + vstr_reset(&line); + stdout_tx_str(">"); + for (;;) { + char c = stdin_rx_chr(); + if (c == VCP_CHAR_CTRL_A) { + // reset raw REPL + goto raw_repl_reset; + } else if (c == VCP_CHAR_CTRL_B) { + // change to friendly REPL + stdout_tx_str("\r\n"); + vstr_clear(&line); + pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; + return 0; + } else if (c == VCP_CHAR_CTRL_C) { + // clear line + vstr_reset(&line); + } else if (c == VCP_CHAR_CTRL_D) { + // input finished + break; + } else if (c <= 127) { + // let through any other ASCII character + vstr_add_char(&line, c); + } + } + + // indicate reception of command + stdout_tx_str("OK"); + + if (line.len == 0) { + // exit for a soft reset + stdout_tx_str("\r\n"); + vstr_clear(&line); + return 1; + } + + mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0); + parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false); + + // indicate end of output with EOF character + stdout_tx_str("\004"); + } +} + +int pyexec_friendly_repl(void) { + vstr_t line; + vstr_init(&line, 32); + +#if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD + // in host mode, we enable the LCD for the repl + mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD"))); + mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true); +#endif + +friendly_repl_reset: + stdout_tx_str("Micro Python build " MICROPY_GIT_HASH " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with STM32F405RG\r\n"); + stdout_tx_str("Type \"help()\" for more information.\r\n"); + + // to test ctrl-C + /* + { + uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef}; + for (;;) { + nlr_buf_t nlr; + printf("pyexec_repl: %p\n", x); + usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); + if (nlr_push(&nlr) == 0) { + for (;;) { + } + } else { + printf("break\n"); + } + } + } + */ + + for (;;) { + vstr_reset(&line); + int ret = readline(&line, ">>> "); + + if (ret == VCP_CHAR_CTRL_A) { + // change to raw REPL + stdout_tx_str("\r\n"); + vstr_clear(&line); + pyexec_mode_kind = PYEXEC_MODE_RAW_REPL; + return 0; + } else if (ret == VCP_CHAR_CTRL_B) { + // reset friendly REPL + stdout_tx_str("\r\n"); + goto friendly_repl_reset; + } else if (ret == VCP_CHAR_CTRL_C) { + // break + stdout_tx_str("\r\n"); + continue; + } else if (ret == VCP_CHAR_CTRL_D) { + // exit for a soft reset + stdout_tx_str("\r\n"); + vstr_clear(&line); + return 1; + } else if (vstr_len(&line) == 0) { + continue; + } + + while (mp_repl_continue_with_input(vstr_str(&line))) { + vstr_add_char(&line, '\n'); + int ret = readline(&line, "... "); + if (ret == VCP_CHAR_CTRL_D) { + // stop entering compound statement + break; + } + } + + mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0); + parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, true); + } +} + +bool pyexec_file(const char *filename) { + mp_lexer_t *lex = mp_lexer_new_from_file(filename); + + if (lex == NULL) { + printf("could not open file '%s' for reading\n", filename); + return false; + } + + return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false); +} + +mp_obj_t pyb_set_repl_info(mp_obj_t o_value) { + repl_display_debugging_info = mp_obj_get_int(o_value); + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info); diff --git a/cortex-m3-qemu/pyexec.h b/cortex-m3-qemu/pyexec.h new file mode 100644 index 0000000000..d191a2fc4e --- /dev/null +++ b/cortex-m3-qemu/pyexec.h @@ -0,0 +1,12 @@ +typedef enum { + PYEXEC_MODE_RAW_REPL, + PYEXEC_MODE_FRIENDLY_REPL, +} pyexec_mode_kind_t; + +extern pyexec_mode_kind_t pyexec_mode_kind; + +int pyexec_raw_repl(void); +int pyexec_friendly_repl(void); +bool pyexec_file(const char *filename); + +MP_DECLARE_CONST_FUN_OBJ(pyb_set_repl_info_obj); diff --git a/cortex-m3-qemu/qstrdefsport.h b/cortex-m3-qemu/qstrdefsport.h new file mode 100644 index 0000000000..546b5b1430 --- /dev/null +++ b/cortex-m3-qemu/qstrdefsport.h @@ -0,0 +1,148 @@ +// qstrs specific to this port + +Q(help) +Q(pyb) +Q(info) +Q(sd_test) +Q(present) +Q(power) +Q(stop) +Q(standby) +Q(source_dir) +Q(main) +Q(usb_mode) +Q(sync) +Q(gc) +Q(repl_info) +Q(delay) +Q(udelay) +Q(switch) +Q(SW) +Q(servo) +Q(pwm) +Q(read) +Q(readall) +Q(readline) +Q(write) +Q(hid) +Q(time) +Q(rng) +Q(LCD) +Q(SD) +Q(SDcard) +Q(gpio) +Q(gpio_in) +Q(gpio_out) +Q(FileIO) +// Entries for sys.path +Q(0:/) +Q(0:/src) +Q(0:/lib) +Q(Pin) +Q(PinMap) +Q(PinAF) +Q(PinNamed) +Q(rtc_info) +Q(millis) +Q(PULL_NONE) +Q(PULL_UP) +Q(PULL_DOWN) +Q(PUSH_PULL) +Q(OPEN_DRAIN) + +// for Led object +Q(Led) +Q(on) +Q(off) +Q(toggle) +Q(intensity) + +// for Usart object +Q(Usart) +Q(status) +Q(recv_chr) +Q(send_chr) +Q(send) + +// for exti object +Q(Exti) +Q(line) +Q(enable) +Q(disable) +Q(swint) +Q(regs) +Q(MODE_IRQ_RISING) +Q(MODE_IRQ_FALLING) +Q(MODE_IRQ_RISING_FALLING) +Q(MODE_EVT_RISING) +Q(MODE_EVT_FALLING) +Q(MODE_EVT_RISING_FALLING) + +// for I2C object +Q(I2C) +Q(is_ready) +Q(mem_read) +Q(mem_write) + +// for Accel object +Q(Accel) +Q(x) +Q(y) +Q(z) +Q(tilt) +Q(filtered_xyz) + +// for ADC object +Q(ADC) +Q(ADC_all) +Q(read_channel) +Q(read_core_temp) +Q(read_core_vbat) +Q(read_core_vref) + +// for DAC object +Q(DAC) +Q(noise) +Q(triangle) +Q(dma) + +// for Servo object +Q(Servo) +Q(pulse_width) +Q(calibrate) +Q(angle) +Q(speed) + +// for os module +Q(os) +Q(/) +Q(listdir) +Q(mkdir) +Q(remove) +Q(rmdir) +Q(unlink) +Q(sep) +Q(urandom) + +// for time module +Q(time) +Q(sleep) + +// for input +Q(input) + +// for stm module +Q(stm) +Q(read8) +Q(read16) +Q(read32) +Q(write8) +Q(write16) +Q(write32) +Q(GPIOA) +Q(GPIOB) +Q(GPIOC) +Q(GPIOD) +Q(GPIO_IDR) +Q(GPIO_BSRRL) +Q(GPIO_BSRRH)