| 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/machine_mem.h" | 
|---|
| 29 |  | 
|---|
| 30 | #if MICROPY_PY_MACHINE | 
|---|
| 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 | STATIC void machine_mem_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { | 
|---|
| 58 | (void)kind; | 
|---|
| 59 | machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in); | 
|---|
| 60 | mp_printf(print, "<%u-bit memory>", 8 * self->elem_size); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | 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 | machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in); | 
|---|
| 66 | if (value == MP_OBJ_NULL) { | 
|---|
| 67 | // delete | 
|---|
| 68 | return MP_OBJ_NULL; // op not supported | 
|---|
| 69 | } else if (value == MP_OBJ_SENTINEL) { | 
|---|
| 70 | // load | 
|---|
| 71 | uintptr_t addr = MICROPY_MACHINE_MEM_GET_READ_ADDR(index, self->elem_size); | 
|---|
| 72 | uint32_t val; | 
|---|
| 73 | switch (self->elem_size) { | 
|---|
| 74 | case 1: | 
|---|
| 75 | val = (*(uint8_t *)addr); | 
|---|
| 76 | break; | 
|---|
| 77 | case 2: | 
|---|
| 78 | val = (*(uint16_t *)addr); | 
|---|
| 79 | break; | 
|---|
| 80 | default: | 
|---|
| 81 | val = (*(uint32_t *)addr); | 
|---|
| 82 | break; | 
|---|
| 83 | } | 
|---|
| 84 | return mp_obj_new_int(val); | 
|---|
| 85 | } else { | 
|---|
| 86 | // store | 
|---|
| 87 | uintptr_t addr = MICROPY_MACHINE_MEM_GET_WRITE_ADDR(index, self->elem_size); | 
|---|
| 88 | uint32_t val = mp_obj_get_int_truncated(value); | 
|---|
| 89 | switch (self->elem_size) { | 
|---|
| 90 | case 1: | 
|---|
| 91 | (*(uint8_t *)addr) = val; | 
|---|
| 92 | break; | 
|---|
| 93 | case 2: | 
|---|
| 94 | (*(uint16_t *)addr) = val; | 
|---|
| 95 | break; | 
|---|
| 96 | default: | 
|---|
| 97 | (*(uint32_t *)addr) = val; | 
|---|
| 98 | break; | 
|---|
| 99 | } | 
|---|
| 100 | return mp_const_none; | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | const mp_obj_type_t machine_mem_type = { | 
|---|
| 105 | { &mp_type_type }, | 
|---|
| 106 | .name = MP_QSTR_mem, | 
|---|
| 107 | .print = machine_mem_print, | 
|---|
| 108 | .subscr = machine_mem_subscr, | 
|---|
| 109 | }; | 
|---|
| 110 |  | 
|---|
| 111 | const machine_mem_obj_t machine_mem8_obj = {{&machine_mem_type}, 1}; | 
|---|
| 112 | const machine_mem_obj_t machine_mem16_obj = {{&machine_mem_type}, 2}; | 
|---|
| 113 | const machine_mem_obj_t machine_mem32_obj = {{&machine_mem_type}, 4}; | 
|---|
| 114 |  | 
|---|
| 115 | #endif // MICROPY_PY_MACHINE | 
|---|
| 116 |  | 
|---|