Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
- http://pymbed.appspot.com/
- https://code.google.com/p/python-on-a-chip/
- http://www.youtube.com/watch?v=Oyqc2bFRW9I
- https://bitbucket.org/va009039/pymbed/
more info: python-on-a-chip
vm/mem.c@15:94ca5c8003e5, 2016-04-14 (annotated)
- Committer:
- va009039
- Date:
- Thu Apr 14 22:32:57 2016 +0000
- Revision:
- 15:94ca5c8003e5
- Parent:
- 0:65f1469d6bfb
update Nucleo-F401RE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:65f1469d6bfb | 1 | /* |
va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2002 Dean Hall. |
va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
va009039 | 0:65f1469d6bfb | 6 | */ |
va009039 | 0:65f1469d6bfb | 7 | |
va009039 | 0:65f1469d6bfb | 8 | |
va009039 | 0:65f1469d6bfb | 9 | #undef __FILE_ID__ |
va009039 | 0:65f1469d6bfb | 10 | #define __FILE_ID__ 0x0D |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief VM Memory |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * VM memory operations. |
va009039 | 0:65f1469d6bfb | 18 | * Implementations and stubs for getByte and memCopy functions. |
va009039 | 0:65f1469d6bfb | 19 | * Functions to load object images from static memory. |
va009039 | 0:65f1469d6bfb | 20 | */ |
va009039 | 0:65f1469d6bfb | 21 | |
va009039 | 0:65f1469d6bfb | 22 | |
va009039 | 0:65f1469d6bfb | 23 | #include "pm.h" |
va009039 | 0:65f1469d6bfb | 24 | |
va009039 | 0:65f1469d6bfb | 25 | |
va009039 | 0:65f1469d6bfb | 26 | uint16_t |
va009039 | 0:65f1469d6bfb | 27 | mem_getWord(PmMemSpace_t memspace, uint8_t const **paddr) |
va009039 | 0:65f1469d6bfb | 28 | { |
va009039 | 0:65f1469d6bfb | 29 | /* PyMite is little endian; get low byte first */ |
va009039 | 0:65f1469d6bfb | 30 | uint8_t blo = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 31 | uint8_t bhi = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 32 | |
va009039 | 0:65f1469d6bfb | 33 | return (uint16_t)(blo | (bhi << (int8_t)8)); |
va009039 | 0:65f1469d6bfb | 34 | } |
va009039 | 0:65f1469d6bfb | 35 | |
va009039 | 0:65f1469d6bfb | 36 | |
va009039 | 0:65f1469d6bfb | 37 | uint32_t |
va009039 | 0:65f1469d6bfb | 38 | mem_getInt(PmMemSpace_t memspace, uint8_t const **paddr) |
va009039 | 0:65f1469d6bfb | 39 | { |
va009039 | 0:65f1469d6bfb | 40 | /* PyMite is little endian; get low word first */ |
va009039 | 0:65f1469d6bfb | 41 | uint16_t wlo = mem_getWord(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 42 | uint32_t whi = mem_getWord(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 43 | |
va009039 | 0:65f1469d6bfb | 44 | return (uint32_t)(wlo | (whi << (int8_t)16)); |
va009039 | 0:65f1469d6bfb | 45 | } |
va009039 | 0:65f1469d6bfb | 46 | |
va009039 | 0:65f1469d6bfb | 47 | |
va009039 | 0:65f1469d6bfb | 48 | #ifdef HAVE_FLOAT |
va009039 | 0:65f1469d6bfb | 49 | float |
va009039 | 0:65f1469d6bfb | 50 | mem_getFloat(PmMemSpace_t memspace, uint8_t const **paddr) |
va009039 | 0:65f1469d6bfb | 51 | { |
va009039 | 0:65f1469d6bfb | 52 | union |
va009039 | 0:65f1469d6bfb | 53 | { |
va009039 | 0:65f1469d6bfb | 54 | char c[4]; |
va009039 | 0:65f1469d6bfb | 55 | float f; |
va009039 | 0:65f1469d6bfb | 56 | } |
va009039 | 0:65f1469d6bfb | 57 | v; |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | #ifdef PM_FLOAT_BIG_ENDIAN |
va009039 | 0:65f1469d6bfb | 60 | /* If the architecture is Big Endian, reverse the bytes of the float */ |
va009039 | 0:65f1469d6bfb | 61 | v.c[3] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 62 | v.c[2] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 63 | v.c[1] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 64 | v.c[0] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 65 | |
va009039 | 0:65f1469d6bfb | 66 | #else |
va009039 | 0:65f1469d6bfb | 67 | v.c[0] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 68 | v.c[1] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 69 | v.c[2] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 70 | v.c[3] = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 71 | |
va009039 | 0:65f1469d6bfb | 72 | #ifndef PM_FLOAT_LITTLE_ENDIAN |
va009039 | 0:65f1469d6bfb | 73 | #warning Neither PM_FLOAT_LITTLE_ENDIAN nor PM_FLOAT_BIG_ENDIAN is defined \ |
va009039 | 0:65f1469d6bfb | 74 | for this platform; defaulting to little endian. |
va009039 | 0:65f1469d6bfb | 75 | #endif |
va009039 | 0:65f1469d6bfb | 76 | #endif |
va009039 | 0:65f1469d6bfb | 77 | |
va009039 | 0:65f1469d6bfb | 78 | return v.f; |
va009039 | 0:65f1469d6bfb | 79 | } |
va009039 | 0:65f1469d6bfb | 80 | #endif /* HAVE_FLOAT */ |
va009039 | 0:65f1469d6bfb | 81 | |
va009039 | 0:65f1469d6bfb | 82 | |
va009039 | 0:65f1469d6bfb | 83 | void |
va009039 | 0:65f1469d6bfb | 84 | mem_copy(PmMemSpace_t memspace, |
va009039 | 0:65f1469d6bfb | 85 | uint8_t **pdest, uint8_t const **psrc, uint16_t count) |
va009039 | 0:65f1469d6bfb | 86 | { |
va009039 | 0:65f1469d6bfb | 87 | /* Copy memory from RAM */ |
va009039 | 0:65f1469d6bfb | 88 | if (memspace == MEMSPACE_RAM) |
va009039 | 0:65f1469d6bfb | 89 | { |
va009039 | 0:65f1469d6bfb | 90 | sli_memcpy(*pdest, *psrc, count); |
va009039 | 0:65f1469d6bfb | 91 | *psrc += count; |
va009039 | 0:65f1469d6bfb | 92 | *pdest += count; |
va009039 | 0:65f1469d6bfb | 93 | return; |
va009039 | 0:65f1469d6bfb | 94 | } |
va009039 | 0:65f1469d6bfb | 95 | |
va009039 | 0:65f1469d6bfb | 96 | /* Copy memory from non-RAM to RAM */ |
va009039 | 0:65f1469d6bfb | 97 | else |
va009039 | 0:65f1469d6bfb | 98 | { |
va009039 | 0:65f1469d6bfb | 99 | uint8_t b; |
va009039 | 0:65f1469d6bfb | 100 | |
va009039 | 0:65f1469d6bfb | 101 | for (; count > 0; count--) |
va009039 | 0:65f1469d6bfb | 102 | { |
va009039 | 0:65f1469d6bfb | 103 | b = mem_getByte(memspace, psrc); |
va009039 | 0:65f1469d6bfb | 104 | **pdest = b; |
va009039 | 0:65f1469d6bfb | 105 | (*pdest)++; |
va009039 | 0:65f1469d6bfb | 106 | } |
va009039 | 0:65f1469d6bfb | 107 | return; |
va009039 | 0:65f1469d6bfb | 108 | } |
va009039 | 0:65f1469d6bfb | 109 | } |
va009039 | 0:65f1469d6bfb | 110 | |
va009039 | 0:65f1469d6bfb | 111 | |
va009039 | 0:65f1469d6bfb | 112 | uint16_t |
va009039 | 0:65f1469d6bfb | 113 | mem_getStringLength(PmMemSpace_t memspace, uint8_t const *const pstr) |
va009039 | 0:65f1469d6bfb | 114 | { |
va009039 | 0:65f1469d6bfb | 115 | uint8_t const *psrc; |
va009039 | 0:65f1469d6bfb | 116 | |
va009039 | 0:65f1469d6bfb | 117 | /* If source is in RAM, use a possibly optimized strlen */ |
va009039 | 0:65f1469d6bfb | 118 | if (memspace == MEMSPACE_RAM) |
va009039 | 0:65f1469d6bfb | 119 | { |
va009039 | 0:65f1469d6bfb | 120 | return sli_strlen((char const *)pstr); |
va009039 | 0:65f1469d6bfb | 121 | } |
va009039 | 0:65f1469d6bfb | 122 | |
va009039 | 0:65f1469d6bfb | 123 | /* Otherwise calculate string length */ |
va009039 | 0:65f1469d6bfb | 124 | psrc = pstr; |
va009039 | 0:65f1469d6bfb | 125 | while (mem_getByte(memspace, &psrc) != (uint8_t)0); |
va009039 | 0:65f1469d6bfb | 126 | return psrc - pstr - 1; |
va009039 | 0:65f1469d6bfb | 127 | } |
va009039 | 0:65f1469d6bfb | 128 | |
va009039 | 0:65f1469d6bfb | 129 | |
va009039 | 0:65f1469d6bfb | 130 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 131 | mem_cmpn(uint8_t *cname, uint16_t cnamelen, PmMemSpace_t memspace, |
va009039 | 0:65f1469d6bfb | 132 | uint8_t const **paddr) |
va009039 | 0:65f1469d6bfb | 133 | { |
va009039 | 0:65f1469d6bfb | 134 | uint16_t i; |
va009039 | 0:65f1469d6bfb | 135 | uint8_t b; |
va009039 | 0:65f1469d6bfb | 136 | |
va009039 | 0:65f1469d6bfb | 137 | /* Iterate over all characters */ |
va009039 | 0:65f1469d6bfb | 138 | for (i = 0; i < cnamelen; i++) |
va009039 | 0:65f1469d6bfb | 139 | { |
va009039 | 0:65f1469d6bfb | 140 | b = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 141 | if (cname[i] != b) |
va009039 | 0:65f1469d6bfb | 142 | { |
va009039 | 0:65f1469d6bfb | 143 | return PM_RET_NO; |
va009039 | 0:65f1469d6bfb | 144 | } |
va009039 | 0:65f1469d6bfb | 145 | } |
va009039 | 0:65f1469d6bfb | 146 | return PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 147 | } |