machine_mem.c 4.1 KB

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