Skip to content

Commit ba84453

Browse files
committed
examples/natmod: Add urandom native module example.
1 parent 48e9262 commit ba84453

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

examples/natmod/urandom/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Location of top-level MicroPython directory
2+
MPY_DIR = ../../..
3+
4+
# Name of module (different to built-in urandom so it can coexist)
5+
MOD = urandom_$(ARCH)
6+
7+
# Source files (.c or .py)
8+
SRC = urandom.c
9+
10+
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
11+
ARCH = x64
12+
13+
include $(MPY_DIR)/py/dynruntime.mk

examples/natmod/urandom/urandom.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#define MICROPY_ENABLE_DYNRUNTIME (1)
2+
#define MICROPY_PY_URANDOM (1)
3+
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
4+
5+
#include "py/dynruntime.h"
6+
7+
// Dynamic native modules don't support a data section so these must go in the BSS
8+
uint32_t yasmarang_pad, yasmarang_n, yasmarang_d;
9+
uint8_t yasmarang_dat;
10+
11+
#include "extmod/modurandom.c"
12+
13+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
14+
MP_DYNRUNTIME_INIT_ENTRY
15+
16+
yasmarang_pad = 0xeda4baba;
17+
yasmarang_n = 69;
18+
yasmarang_d = 233;
19+
20+
mp_store_global(MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR_urandom));
21+
mp_store_global(MP_QSTR_getrandbits, MP_OBJ_FROM_PTR(&mod_urandom_getrandbits_obj));
22+
mp_store_global(MP_QSTR_seed, MP_OBJ_FROM_PTR(&mod_urandom_seed_obj));
23+
#if MICROPY_PY_URANDOM_EXTRA_FUNCS
24+
mp_store_global(MP_QSTR_randrange, MP_OBJ_FROM_PTR(&mod_urandom_randrange_obj));
25+
mp_store_global(MP_QSTR_randint, MP_OBJ_FROM_PTR(&mod_urandom_randint_obj));
26+
mp_store_global(MP_QSTR_choice, MP_OBJ_FROM_PTR(&mod_urandom_choice_obj));
27+
#if MICROPY_PY_BUILTINS_FLOAT
28+
mp_store_global(MP_QSTR_random, MP_OBJ_FROM_PTR(&mod_urandom_random_obj));
29+
mp_store_global(MP_QSTR_uniform, MP_OBJ_FROM_PTR(&mod_urandom_uniform_obj));
30+
#endif
31+
#endif
32+
33+
MP_DYNRUNTIME_INIT_EXIT
34+
}

extmod/modurandom.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
// http://www.literatecode.com/yasmarang
3737
// Public Domain
3838

39+
#if !MICROPY_ENABLE_DYNRUNTIME
3940
STATIC uint32_t yasmarang_pad = 0xeda4baba, yasmarang_n = 69, yasmarang_d = 233;
4041
STATIC uint8_t yasmarang_dat = 0;
42+
#endif
4143

4244
STATIC uint32_t yasmarang(void)
4345
{
@@ -208,6 +210,7 @@ STATIC mp_obj_t mod_urandom___init__() {
208210
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom___init___obj, mod_urandom___init__);
209211
#endif
210212

213+
#if !MICROPY_ENABLE_DYNRUNTIME
211214
STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table[] = {
212215
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_urandom) },
213216
#ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC
@@ -232,5 +235,6 @@ const mp_obj_module_t mp_module_urandom = {
232235
.base = { &mp_type_module },
233236
.globals = (mp_obj_dict_t*)&mp_module_urandom_globals,
234237
};
238+
#endif
235239

236240
#endif //MICROPY_PY_URANDOM

tests/run-natmodtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
'btree': 'btree/btree_$(ARCH).mpy',
2424
'framebuf': 'framebuf/framebuf_$(ARCH).mpy',
2525
'uheapq': 'uheapq/uheapq_$(ARCH).mpy',
26+
'urandom': 'urandom/urandom_$(ARCH).mpy',
2627
'ure': 'ure/ure_$(ARCH).mpy',
2728
'uzlib': 'uzlib/uzlib_$(ARCH).mpy',
2829
}

0 commit comments

Comments
 (0)