Skip to content

Commit f925165

Browse files
dhylandspfalcon
authored andcommitted
unix: Move modmachine into unix directory
This leaves behind the common functionality in extmod/machine_mem.c which can be used by all ports.
1 parent f2ed736 commit f925165

File tree

8 files changed

+138
-55
lines changed

8 files changed

+138
-55
lines changed

extmod/machine_mem.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "extmod/machine_mem.h"
28+
29+
#if MICROPY_PY_MACHINE
30+
31+
STATIC void machine_mem_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
32+
(void)kind;
33+
machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
34+
mp_printf(print, "<%u-bit memory>", 8 * self->elem_size);
35+
}
36+
37+
STATIC mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
38+
// TODO support slice index to read/write multiple values at once
39+
machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
40+
if (value == MP_OBJ_NULL) {
41+
// delete
42+
return MP_OBJ_NULL; // op not supported
43+
} else if (value == MP_OBJ_SENTINEL) {
44+
// load
45+
uintptr_t addr = machine_mem_get_read_addr(index, self->elem_size);
46+
uint32_t val;
47+
switch (self->elem_size) {
48+
case 1: val = (*(uint8_t*)addr); break;
49+
case 2: val = (*(uint16_t*)addr); break;
50+
default: val = (*(uint32_t*)addr); break;
51+
}
52+
return mp_obj_new_int(val);
53+
} else {
54+
// store
55+
uintptr_t addr = machine_mem_get_write_addr(index, self->elem_size);
56+
uint32_t val = mp_obj_get_int(value);
57+
switch (self->elem_size) {
58+
case 1: (*(uint8_t*)addr) = val; break;
59+
case 2: (*(uint16_t*)addr) = val; break;
60+
default: (*(uint32_t*)addr) = val; break;
61+
}
62+
return mp_const_none;
63+
}
64+
}
65+
66+
const mp_obj_type_t machine_mem_type = {
67+
{ &mp_type_type },
68+
.name = MP_QSTR_mem,
69+
.print = machine_mem_print,
70+
.subscr = machine_mem_subscr,
71+
};
72+
73+
const machine_mem_obj_t machine_mem8_obj = {{&machine_mem_type}, 1};
74+
const machine_mem_obj_t machine_mem16_obj = {{&machine_mem_type}, 2};
75+
const machine_mem_obj_t machine_mem32_obj = {{&machine_mem_type}, 4};
76+
77+
#endif // MICROPY_PY_MACHINE

extmod/machine_mem.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
28+
#ifndef MICROPY_EXTMOD_MACHINE_MEM
29+
#define MICROPY_EXTMOD_MACHINE_MEM
30+
31+
#include "py/obj.h"
32+
33+
typedef struct _machine_mem_obj_t {
34+
mp_obj_base_t base;
35+
unsigned elem_size; // in bytes
36+
} machine_mem_obj_t;
37+
38+
extern const mp_obj_type_t machine_mem_type;
39+
40+
extern const machine_mem_obj_t machine_mem8_obj;
41+
extern const machine_mem_obj_t machine_mem16_obj;
42+
extern const machine_mem_obj_t machine_mem32_obj;
43+
44+
// It is expected that a port will provide the following 2 functions.
45+
// We define the prototypes here, but the modmachine.c file for a port should
46+
// provide the implementation
47+
uintptr_t machine_mem_get_read_addr(mp_obj_t addr_o, uint align);
48+
uintptr_t machine_mem_get_write_addr(mp_obj_t addr_o, uint align);
49+
50+
#endif /* MICROPY_EXTMOD_MACHINE_MEM */

py/objmodule.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
184184
#if MICROPY_PY_UBINASCII
185185
{ MP_ROM_QSTR(MP_QSTR_ubinascii), MP_ROM_PTR(&mp_module_ubinascii) },
186186
#endif
187-
#if MICROPY_PY_MACHINE
188-
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) },
189-
#endif
190187
#if MICROPY_PY_USSL
191188
{ MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) },
192189
#endif

py/py.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ PY_O_BASENAME = \
165165
../extmod/moduheapq.o \
166166
../extmod/moduhashlib.o \
167167
../extmod/modubinascii.o \
168-
../extmod/modmachine.o \
168+
../extmod/machine_mem.o \
169169
../extmod/modussl.o \
170170
../extmod/fsusermount.o \
171171

unix/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ SRC_C = \
114114
unix_mphal.c \
115115
input.c \
116116
file.c \
117+
modmachine.c \
117118
modos.c \
118119
moduselect.c \
119120
alloc.c \

extmod/modmachine.c renamed to unix/modmachine.c

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
#include "py/nlr.h"
3131
#include "py/obj.h"
32+
33+
#include "extmod/machine_mem.h"
34+
3235
#if MICROPY_PLAT_DEV_MEM
3336
#include <errno.h>
3437
#include <fcntl.h>
@@ -39,8 +42,7 @@
3942

4043
#if MICROPY_PY_MACHINE
4144

42-
43-
STATIC uintptr_t get_addr(mp_obj_t addr_o, uint align) {
45+
uintptr_t machine_mem_get_read_addr(mp_obj_t addr_o, uint align) {
4446
uintptr_t addr = mp_obj_int_get_truncated(addr_o);
4547
if ((addr & (align - 1)) != 0) {
4648
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
@@ -70,57 +72,10 @@ STATIC uintptr_t get_addr(mp_obj_t addr_o, uint align) {
7072
return addr;
7173
}
7274

73-
typedef struct _machine_mem_obj_t {
74-
mp_obj_base_t base;
75-
unsigned elem_size; // in bytes
76-
} machine_mem_obj_t;
77-
78-
STATIC void machine_mem_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
79-
(void)kind;
80-
machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
81-
mp_printf(print, "<%u-bit memory>", 8 * self->elem_size);
82-
}
83-
84-
STATIC mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
85-
// TODO support slice index to read/write multiple values at once
86-
machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
87-
if (value == MP_OBJ_NULL) {
88-
// delete
89-
return MP_OBJ_NULL; // op not supported
90-
} else if (value == MP_OBJ_SENTINEL) {
91-
// load
92-
uintptr_t addr = get_addr(index, self->elem_size);
93-
uint32_t val;
94-
switch (self->elem_size) {
95-
case 1: val = (*(uint8_t*)addr); break;
96-
case 2: val = (*(uint16_t*)addr); break;
97-
default: val = (*(uint32_t*)addr); break;
98-
}
99-
return mp_obj_new_int(val);
100-
} else {
101-
// store
102-
uintptr_t addr = get_addr(index, self->elem_size);
103-
uint32_t val = mp_obj_get_int(value);
104-
switch (self->elem_size) {
105-
case 1: (*(uint8_t*)addr) = val; break;
106-
case 2: (*(uint16_t*)addr) = val; break;
107-
default: (*(uint32_t*)addr) = val; break;
108-
}
109-
return mp_const_none;
110-
}
75+
uintptr_t machine_mem_get_write_addr(mp_obj_t addr_o, uint align) {
76+
return machine_mem_get_read_addr(addr_o, align);
11177
}
11278

113-
STATIC const mp_obj_type_t machine_mem_type = {
114-
{ &mp_type_type },
115-
.name = MP_QSTR_mem,
116-
.print = machine_mem_print,
117-
.subscr = machine_mem_subscr,
118-
};
119-
120-
STATIC const machine_mem_obj_t machine_mem8_obj = {{&machine_mem_type}, 1};
121-
STATIC const machine_mem_obj_t machine_mem16_obj = {{&machine_mem_type}, 2};
122-
STATIC const machine_mem_obj_t machine_mem32_obj = {{&machine_mem_type}, 4};
123-
12479
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
12580
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) },
12681

unix/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
125125
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
126126

127+
extern const struct _mp_obj_module_t mp_module_machine;
127128
extern const struct _mp_obj_module_t mp_module_os;
128129
extern const struct _mp_obj_module_t mp_module_uselect;
129130
extern const struct _mp_obj_module_t mp_module_time;
@@ -163,6 +164,7 @@ extern const struct _mp_obj_module_t mp_module_jni;
163164
MICROPY_PY_JNI_DEF \
164165
MICROPY_PY_TIME_DEF \
165166
MICROPY_PY_SOCKET_DEF \
167+
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) }, \
166168
{ MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_os) }, \
167169
{ MP_ROM_QSTR(MP_QSTR_uselect), MP_ROM_PTR(&mp_module_uselect) }, \
168170
MICROPY_PY_TERMIOS_DEF \

windows/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SRC_C = \
3232
unix/file.c \
3333
unix/input.c \
3434
unix/modos.c \
35+
unix/modmachine.c \
3536
unix/modtime.c \
3637
unix/gccollect.c \
3738
windows_mphal.c \

0 commit comments

Comments
 (0)