blob: 88c176803feae26298fd864b4e2f1e4327468611 [file] [log] [blame]
Dave Hylandsf9251652015-12-12 17:15:33 -05001/*
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"
Dave Hylands755b0142015-12-13 16:11:20 -080028#include "py/nlr.h"
Dave Hylandsf9251652015-12-12 17:15:33 -050029
30#if MICROPY_PY_MACHINE
31
Dave Hylands755b0142015-12-13 16:11:20 -080032// 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
Paul Sokolovsky5362bcc2016-11-21 01:09:17 +030034// MICROPY_MACHINE_MEM_GET_WRITE_ADDR in your mpconfigport.h. Since the
Dave Hylands755b0142015-12-13 16:11:20 -080035// 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)
42STATIC uintptr_t machine_mem_get_addr(mp_obj_t addr_o, uint align) {
43 uintptr_t addr = mp_obj_int_get_truncated(addr_o);
44 if ((addr & (align - 1)) != 0) {
45 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "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
Dave Hylandsf9251652015-12-12 17:15:33 -050057STATIC 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
63STATIC 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
Dave Hylands755b0142015-12-13 16:11:20 -080071 uintptr_t addr = MICROPY_MACHINE_MEM_GET_READ_ADDR(index, self->elem_size);
Dave Hylandsf9251652015-12-12 17:15:33 -050072 uint32_t val;
73 switch (self->elem_size) {
74 case 1: val = (*(uint8_t*)addr); break;
75 case 2: val = (*(uint16_t*)addr); break;
76 default: val = (*(uint32_t*)addr); break;
77 }
78 return mp_obj_new_int(val);
79 } else {
80 // store
Dave Hylands755b0142015-12-13 16:11:20 -080081 uintptr_t addr = MICROPY_MACHINE_MEM_GET_WRITE_ADDR(index, self->elem_size);
Damien George3a042fb2016-02-10 17:05:03 +000082 uint32_t val = mp_obj_get_int_truncated(value);
Dave Hylandsf9251652015-12-12 17:15:33 -050083 switch (self->elem_size) {
84 case 1: (*(uint8_t*)addr) = val; break;
85 case 2: (*(uint16_t*)addr) = val; break;
86 default: (*(uint32_t*)addr) = val; break;
87 }
88 return mp_const_none;
89 }
90}
91
92const mp_obj_type_t machine_mem_type = {
93 { &mp_type_type },
94 .name = MP_QSTR_mem,
95 .print = machine_mem_print,
96 .subscr = machine_mem_subscr,
97};
98
99const machine_mem_obj_t machine_mem8_obj = {{&machine_mem_type}, 1};
100const machine_mem_obj_t machine_mem16_obj = {{&machine_mem_type}, 2};
101const machine_mem_obj_t machine_mem32_obj = {{&machine_mem_type}, 4};
102
103#endif // MICROPY_PY_MACHINE