summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-12-13 13:30:44 +1100
committerDamien George <damien.p.george@gmail.com>2019-12-13 13:33:40 +1100
commitba84453f77ed8ce5e8ac128bee80d3923a14e9ad (patch)
tree9781a8d02e547834ff49beb542ce06176182d0a5 /examples
parent48e9262f550ba8c5ff2d7c48c8f8c7f7e5ccfec6 (diff)
examples/natmod: Add urandom native module example.
Diffstat (limited to 'examples')
-rw-r--r--examples/natmod/urandom/Makefile13
-rw-r--r--examples/natmod/urandom/urandom.c34
2 files changed, 47 insertions, 0 deletions
diff --git a/examples/natmod/urandom/Makefile b/examples/natmod/urandom/Makefile
new file mode 100644
index 000000000..3f018baaf
--- /dev/null
+++ b/examples/natmod/urandom/Makefile
@@ -0,0 +1,13 @@
+# Location of top-level MicroPython directory
+MPY_DIR = ../../..
+
+# Name of module (different to built-in urandom so it can coexist)
+MOD = urandom_$(ARCH)
+
+# Source files (.c or .py)
+SRC = urandom.c
+
+# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
+ARCH = x64
+
+include $(MPY_DIR)/py/dynruntime.mk
diff --git a/examples/natmod/urandom/urandom.c b/examples/natmod/urandom/urandom.c
new file mode 100644
index 000000000..732e439ee
--- /dev/null
+++ b/examples/natmod/urandom/urandom.c
@@ -0,0 +1,34 @@
+#define MICROPY_ENABLE_DYNRUNTIME (1)
+#define MICROPY_PY_URANDOM (1)
+#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
+
+#include "py/dynruntime.h"
+
+// Dynamic native modules don't support a data section so these must go in the BSS
+uint32_t yasmarang_pad, yasmarang_n, yasmarang_d;
+uint8_t yasmarang_dat;
+
+#include "extmod/modurandom.c"
+
+mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
+ MP_DYNRUNTIME_INIT_ENTRY
+
+ yasmarang_pad = 0xeda4baba;
+ yasmarang_n = 69;
+ yasmarang_d = 233;
+
+ mp_store_global(MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR_urandom));
+ mp_store_global(MP_QSTR_getrandbits, MP_OBJ_FROM_PTR(&mod_urandom_getrandbits_obj));
+ mp_store_global(MP_QSTR_seed, MP_OBJ_FROM_PTR(&mod_urandom_seed_obj));
+ #if MICROPY_PY_URANDOM_EXTRA_FUNCS
+ mp_store_global(MP_QSTR_randrange, MP_OBJ_FROM_PTR(&mod_urandom_randrange_obj));
+ mp_store_global(MP_QSTR_randint, MP_OBJ_FROM_PTR(&mod_urandom_randint_obj));
+ mp_store_global(MP_QSTR_choice, MP_OBJ_FROM_PTR(&mod_urandom_choice_obj));
+ #if MICROPY_PY_BUILTINS_FLOAT
+ mp_store_global(MP_QSTR_random, MP_OBJ_FROM_PTR(&mod_urandom_random_obj));
+ mp_store_global(MP_QSTR_uniform, MP_OBJ_FROM_PTR(&mod_urandom_uniform_obj));
+ #endif
+ #endif
+
+ MP_DYNRUNTIME_INIT_EXIT
+}