Skip to content

Commit b721da6

Browse files
Intercept machine.mem32 etc. (#78)
Provide a minimal implementation that allows reading the device ID but otherwise raises an exception. I'm open to adding other FICR but this is the priority to get the code on this support article: https://support.microbit.org/support/solutions/articles/19000070728-how-to-find-the-micro-bit-serial-number ...working in the simulator. Closes #58
1 parent b9f4557 commit b721da6

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ SRC_C += \
6060
microbithal_js.c \
6161
main.c \
6262
mphalport.c \
63+
modmachine.c \
6364

6465
SRC_C += $(addprefix $(CODAL_PORT)/, \
6566
drv_display.c \

src/modmachine.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "py/runtime.h"
2+
3+
// Just defines the memory access functions, otherwise we use codal_port's
4+
// implementation.
5+
6+
uintptr_t machine_mem_get_read_addr(mp_obj_t addr_o, uint align) {
7+
uintptr_t addr = mp_obj_get_int_truncated(addr_o);
8+
if ((addr & (align - 1)) != 0) {
9+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes"), addr, align);
10+
}
11+
12+
static const uint32_t FICR = 0x10000000;
13+
static const uint32_t FICR_DEVICEID_0 = FICR + 0x060;
14+
static const uint32_t FICR_DEVICEID_1 = FICR + 0x064;
15+
16+
static uint32_t mem;
17+
switch (addr) {
18+
case FICR_DEVICEID_0:
19+
case FICR_DEVICEID_1: {
20+
// There's machine.unique_id backed by hal for this
21+
// but existing code reads via FICR.
22+
mem = 0;
23+
break;
24+
}
25+
default: {
26+
mp_raise_NotImplementedError(MP_ERROR_TEXT("simulator limitation: memory read"));
27+
}
28+
}
29+
return (uintptr_t)&mem;
30+
}
31+
32+
uintptr_t machine_mem_get_write_addr(mp_obj_t addr_o, uint align) {
33+
mp_raise_NotImplementedError(MP_ERROR_TEXT("simulator limitation: memory write"));
34+
}

src/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,8 @@ typedef long mp_off_t;
158158
// Needed for MICROPY_PY_URANDOM_SEED_INIT_FUNC.
159159
extern uint32_t rng_generate_random_word(void);
160160

161+
// Intercept modmachine memory access.
162+
#define MICROPY_MACHINE_MEM_GET_READ_ADDR machine_mem_get_read_addr
163+
#define MICROPY_MACHINE_MEM_GET_WRITE_ADDR machine_mem_get_write_addr
164+
161165
#endif

0 commit comments

Comments
 (0)