summaryrefslogtreecommitdiff
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
parent48e9262f550ba8c5ff2d7c48c8f8c7f7e5ccfec6 (diff)
examples/natmod: Add urandom native module example.
-rw-r--r--examples/natmod/urandom/Makefile13
-rw-r--r--examples/natmod/urandom/urandom.c34
-rw-r--r--extmod/modurandom.c4
-rwxr-xr-xtests/run-natmodtests.py1
4 files changed, 52 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
+}
diff --git a/extmod/modurandom.c b/extmod/modurandom.c
index 2e667570d..e3c7fc7f5 100644
--- a/extmod/modurandom.c
+++ b/extmod/modurandom.c
@@ -36,8 +36,10 @@
// http://www.literatecode.com/yasmarang
// Public Domain
+#if !MICROPY_ENABLE_DYNRUNTIME
STATIC uint32_t yasmarang_pad = 0xeda4baba, yasmarang_n = 69, yasmarang_d = 233;
STATIC uint8_t yasmarang_dat = 0;
+#endif
STATIC uint32_t yasmarang(void)
{
@@ -208,6 +210,7 @@ STATIC mp_obj_t mod_urandom___init__() {
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom___init___obj, mod_urandom___init__);
#endif
+#if !MICROPY_ENABLE_DYNRUNTIME
STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_urandom) },
#ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC
@@ -232,5 +235,6 @@ const mp_obj_module_t mp_module_urandom = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_urandom_globals,
};
+#endif
#endif //MICROPY_PY_URANDOM
diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py
index 186c30b83..3f49a1d68 100755
--- a/tests/run-natmodtests.py
+++ b/tests/run-natmodtests.py
@@ -23,6 +23,7 @@ TEST_MAPPINGS = {
'btree': 'btree/btree_$(ARCH).mpy',
'framebuf': 'framebuf/framebuf_$(ARCH).mpy',
'uheapq': 'uheapq/uheapq_$(ARCH).mpy',
+ 'urandom': 'urandom/urandom_$(ARCH).mpy',
'ure': 'ure/ure_$(ARCH).mpy',
'uzlib': 'uzlib/uzlib_$(ARCH).mpy',
}