Branch data Line data Source code
1 : : /*
2 : : * This file is part of the MicroPython 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 "py/runtime.h"
28 : : #include "extmod/modmachine.h"
29 : :
30 : : #if MICROPY_PY_MACHINE_MEMX
31 : :
32 : : // If you wish to override the functions for mapping the machine_mem read/write
33 : : // address, then add a #define for MICROPY_MACHINE_MEM_GET_READ_ADDR and/or
34 : : // MICROPY_MACHINE_MEM_GET_WRITE_ADDR in your mpconfigport.h. Since the
35 : : // prototypes are identical, it is allowable for both of the macros to evaluate
36 : : // the to same function.
37 : : //
38 : : // It is expected that the modmachine.c file for a given port will provide the
39 : : // implementations, if the default implementation isn't used.
40 : :
41 : : #if !defined(MICROPY_MACHINE_MEM_GET_READ_ADDR) || !defined(MICROPY_MACHINE_MEM_GET_WRITE_ADDR)
42 : : static uintptr_t machine_mem_get_addr(mp_obj_t addr_o, uint align) {
43 : : uintptr_t addr = mp_obj_get_int_truncated(addr_o);
44 : : if ((addr & (align - 1)) != 0) {
45 : : mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes"), addr, align);
46 : : }
47 : : return addr;
48 : : }
49 : : #if !defined(MICROPY_MACHINE_MEM_GET_READ_ADDR)
50 : : #define MICROPY_MACHINE_MEM_GET_READ_ADDR machine_mem_get_addr
51 : : #endif
52 : : #if !defined(MICROPY_MACHINE_MEM_GET_WRITE_ADDR)
53 : : #define MICROPY_MACHINE_MEM_GET_WRITE_ADDR machine_mem_get_addr
54 : : #endif
55 : : #endif
56 : :
57 : 2 : static void machine_mem_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
58 : 2 : (void)kind;
59 : 2 : machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
60 : 2 : mp_printf(print, "<%u-bit memory>", 8 * self->elem_size);
61 : 2 : }
62 : :
63 : 14 : static mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
64 : : // TODO support slice index to read/write multiple values at once
65 : 14 : machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
66 [ + + ]: 14 : if (value == MP_OBJ_NULL) {
67 : : // delete
68 : : return MP_OBJ_NULL; // op not supported
69 [ + + ]: 12 : } else if (value == MP_OBJ_SENTINEL) {
70 : : // load
71 : 6 : uintptr_t addr = MICROPY_MACHINE_MEM_GET_READ_ADDR(index, self->elem_size);
72 : 0 : uint32_t val;
73 [ # # # ]: 0 : switch (self->elem_size) {
74 : 0 : case 1:
75 : 0 : val = (*(uint8_t *)addr);
76 : 0 : break;
77 : 0 : case 2:
78 : 0 : val = (*(uint16_t *)addr);
79 : 0 : break;
80 : 0 : default:
81 : 0 : val = (*(uint32_t *)addr);
82 : 0 : break;
83 : : }
84 : 0 : return mp_obj_new_int(val);
85 : : } else {
86 : : // store
87 : 6 : uintptr_t addr = MICROPY_MACHINE_MEM_GET_WRITE_ADDR(index, self->elem_size);
88 : 0 : uint32_t val = mp_obj_get_int_truncated(value);
89 [ # # # ]: 0 : switch (self->elem_size) {
90 : 0 : case 1:
91 : 0 : (*(uint8_t *)addr) = val;
92 : 0 : break;
93 : 0 : case 2:
94 : 0 : (*(uint16_t *)addr) = val;
95 : 0 : break;
96 : 0 : default:
97 : 0 : (*(uint32_t *)addr) = val;
98 : 0 : break;
99 : : }
100 : 0 : return mp_const_none;
101 : : }
102 : : }
103 : :
104 : : MP_DEFINE_CONST_OBJ_TYPE(
105 : : machine_mem_type,
106 : : MP_QSTR_mem,
107 : : MP_TYPE_FLAG_NONE,
108 : : print, machine_mem_print,
109 : : subscr, machine_mem_subscr
110 : : );
111 : :
112 : : const machine_mem_obj_t machine_mem8_obj = {{&machine_mem_type}, 1};
113 : : const machine_mem_obj_t machine_mem16_obj = {{&machine_mem_type}, 2};
114 : : const machine_mem_obj_t machine_mem32_obj = {{&machine_mem_type}, 4};
115 : :
116 : : #endif // MICROPY_PY_MACHINE_MEMX
|