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/img.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__ 0x07 |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief Image routines |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * Created to eliminate a circular include |
va009039 | 0:65f1469d6bfb | 18 | * among mem, string and obj. |
va009039 | 0:65f1469d6bfb | 19 | */ |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | |
va009039 | 0:65f1469d6bfb | 22 | #include "pm.h" |
va009039 | 0:65f1469d6bfb | 23 | |
va009039 | 0:65f1469d6bfb | 24 | |
va009039 | 0:65f1469d6bfb | 25 | /* |
va009039 | 0:65f1469d6bfb | 26 | * Searches for a module's name in a contiguous array of images |
va009039 | 0:65f1469d6bfb | 27 | * in the given namespace starting at the given address. |
va009039 | 0:65f1469d6bfb | 28 | * A module's name is stored in the last index of the names tuple of an image. |
va009039 | 0:65f1469d6bfb | 29 | */ |
va009039 | 0:65f1469d6bfb | 30 | static PmReturn_t |
va009039 | 0:65f1469d6bfb | 31 | img_findInPath(uint8_t *cname, uint16_t cnamelen, PmMemSpace_t memspace, |
va009039 | 0:65f1469d6bfb | 32 | uint8_t const **paddr) |
va009039 | 0:65f1469d6bfb | 33 | { |
va009039 | 0:65f1469d6bfb | 34 | uint8_t const *imgtop; |
va009039 | 0:65f1469d6bfb | 35 | PmType_t type; |
va009039 | 0:65f1469d6bfb | 36 | uint16_t len; |
va009039 | 0:65f1469d6bfb | 37 | int16_t size = 0; |
va009039 | 0:65f1469d6bfb | 38 | uint8_t i = 0; |
va009039 | 0:65f1469d6bfb | 39 | |
va009039 | 0:65f1469d6bfb | 40 | /* Addr is top of img */ |
va009039 | 0:65f1469d6bfb | 41 | imgtop = *paddr; |
va009039 | 0:65f1469d6bfb | 42 | |
va009039 | 0:65f1469d6bfb | 43 | /* Get img's type byte */ |
va009039 | 0:65f1469d6bfb | 44 | type = (PmType_t)mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 45 | |
va009039 | 0:65f1469d6bfb | 46 | /* Search all sequential images */ |
va009039 | 0:65f1469d6bfb | 47 | while (type == OBJ_TYPE_CIM) |
va009039 | 0:65f1469d6bfb | 48 | { |
va009039 | 0:65f1469d6bfb | 49 | /* Use size field to calc addr of next potential img */ |
va009039 | 0:65f1469d6bfb | 50 | size = mem_getWord(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 51 | |
va009039 | 0:65f1469d6bfb | 52 | /* Point to names tuple */ |
va009039 | 0:65f1469d6bfb | 53 | *paddr = imgtop + CI_NAMES_FIELD; |
va009039 | 0:65f1469d6bfb | 54 | |
va009039 | 0:65f1469d6bfb | 55 | /* Ensure it's a tuple */ |
va009039 | 0:65f1469d6bfb | 56 | type = (PmType_t)mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 57 | C_ASSERT(type == OBJ_TYPE_TUP); |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | /* Scan to last name in tuple (it's the module's name) */ |
va009039 | 0:65f1469d6bfb | 60 | i = mem_getByte(memspace, paddr) - (uint8_t)1; |
va009039 | 0:65f1469d6bfb | 61 | for (; i > 0; i--) |
va009039 | 0:65f1469d6bfb | 62 | { |
va009039 | 0:65f1469d6bfb | 63 | /* Ensure obj is a string */ |
va009039 | 0:65f1469d6bfb | 64 | type = (PmType_t)mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 65 | C_ASSERT(type == OBJ_TYPE_STR); |
va009039 | 0:65f1469d6bfb | 66 | |
va009039 | 0:65f1469d6bfb | 67 | /* Skip the length of the string */ |
va009039 | 0:65f1469d6bfb | 68 | len = mem_getWord(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 69 | (*paddr) += len; |
va009039 | 0:65f1469d6bfb | 70 | } |
va009039 | 0:65f1469d6bfb | 71 | |
va009039 | 0:65f1469d6bfb | 72 | /* Ensure it's a string */ |
va009039 | 0:65f1469d6bfb | 73 | type = (PmType_t)mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 74 | C_ASSERT(type == OBJ_TYPE_STR); |
va009039 | 0:65f1469d6bfb | 75 | |
va009039 | 0:65f1469d6bfb | 76 | /* If strings match, return the address of this image */ |
va009039 | 0:65f1469d6bfb | 77 | if ((cnamelen == mem_getWord(memspace, paddr)) |
va009039 | 0:65f1469d6bfb | 78 | && (PM_RET_OK == mem_cmpn(cname, cnamelen, memspace, paddr))) |
va009039 | 0:65f1469d6bfb | 79 | { |
va009039 | 0:65f1469d6bfb | 80 | *paddr = imgtop; |
va009039 | 0:65f1469d6bfb | 81 | return PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 82 | } |
va009039 | 0:65f1469d6bfb | 83 | |
va009039 | 0:65f1469d6bfb | 84 | /* Calc imgtop for next iteration */ |
va009039 | 0:65f1469d6bfb | 85 | imgtop += size; |
va009039 | 0:65f1469d6bfb | 86 | |
va009039 | 0:65f1469d6bfb | 87 | /* Point to next potential img */ |
va009039 | 0:65f1469d6bfb | 88 | *paddr = imgtop; |
va009039 | 0:65f1469d6bfb | 89 | |
va009039 | 0:65f1469d6bfb | 90 | /* Check if another img follows this one */ |
va009039 | 0:65f1469d6bfb | 91 | type = (PmType_t)mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 92 | } |
va009039 | 0:65f1469d6bfb | 93 | return PM_RET_NO; |
va009039 | 0:65f1469d6bfb | 94 | } |
va009039 | 0:65f1469d6bfb | 95 | |
va009039 | 0:65f1469d6bfb | 96 | |
va009039 | 0:65f1469d6bfb | 97 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 98 | img_findInPaths(pPmObj_t pname, PmMemSpace_t *r_memspace, |
va009039 | 0:65f1469d6bfb | 99 | uint8_t const **r_imgaddr) |
va009039 | 0:65f1469d6bfb | 100 | { |
va009039 | 0:65f1469d6bfb | 101 | uint8_t i; |
va009039 | 0:65f1469d6bfb | 102 | PmReturn_t retval = PM_RET_NO; |
va009039 | 0:65f1469d6bfb | 103 | |
va009039 | 0:65f1469d6bfb | 104 | /* Search in each path in the paths */ |
va009039 | 0:65f1469d6bfb | 105 | for (i = 0; i < gVmGlobal.imgPaths.pathcount; i++) |
va009039 | 0:65f1469d6bfb | 106 | { |
va009039 | 0:65f1469d6bfb | 107 | *r_imgaddr = gVmGlobal.imgPaths.pimg[i]; |
va009039 | 0:65f1469d6bfb | 108 | *r_memspace = gVmGlobal.imgPaths.memspace[i]; |
va009039 | 0:65f1469d6bfb | 109 | retval = img_findInPath(((pPmString_t)pname)->val, |
va009039 | 0:65f1469d6bfb | 110 | ((pPmString_t)pname)->length, |
va009039 | 0:65f1469d6bfb | 111 | *r_memspace, r_imgaddr); |
va009039 | 0:65f1469d6bfb | 112 | if (retval == PM_RET_NO) |
va009039 | 0:65f1469d6bfb | 113 | { |
va009039 | 0:65f1469d6bfb | 114 | continue; |
va009039 | 0:65f1469d6bfb | 115 | } |
va009039 | 0:65f1469d6bfb | 116 | else if (retval == PM_RET_OK) |
va009039 | 0:65f1469d6bfb | 117 | { |
va009039 | 0:65f1469d6bfb | 118 | break; |
va009039 | 0:65f1469d6bfb | 119 | } |
va009039 | 0:65f1469d6bfb | 120 | else |
va009039 | 0:65f1469d6bfb | 121 | { |
va009039 | 0:65f1469d6bfb | 122 | return retval; |
va009039 | 0:65f1469d6bfb | 123 | } |
va009039 | 0:65f1469d6bfb | 124 | } |
va009039 | 0:65f1469d6bfb | 125 | |
va009039 | 0:65f1469d6bfb | 126 | return retval; |
va009039 | 0:65f1469d6bfb | 127 | } |
va009039 | 0:65f1469d6bfb | 128 | |
va009039 | 0:65f1469d6bfb | 129 | |
va009039 | 0:65f1469d6bfb | 130 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 131 | img_appendToPath(PmMemSpace_t memspace, uint8_t const * const paddr) |
va009039 | 0:65f1469d6bfb | 132 | { |
va009039 | 0:65f1469d6bfb | 133 | uint8_t i; |
va009039 | 0:65f1469d6bfb | 134 | |
va009039 | 0:65f1469d6bfb | 135 | if (gVmGlobal.imgPaths.pathcount >= PM_NUM_IMG_PATHS) |
va009039 | 0:65f1469d6bfb | 136 | { |
va009039 | 0:65f1469d6bfb | 137 | return PM_RET_NO; |
va009039 | 0:65f1469d6bfb | 138 | } |
va009039 | 0:65f1469d6bfb | 139 | |
va009039 | 0:65f1469d6bfb | 140 | i = gVmGlobal.imgPaths.pathcount; |
va009039 | 0:65f1469d6bfb | 141 | |
va009039 | 0:65f1469d6bfb | 142 | gVmGlobal.imgPaths.memspace[i] = memspace; |
va009039 | 0:65f1469d6bfb | 143 | gVmGlobal.imgPaths.pimg[i] = paddr; |
va009039 | 0:65f1469d6bfb | 144 | gVmGlobal.imgPaths.pathcount++; |
va009039 | 0:65f1469d6bfb | 145 | |
va009039 | 0:65f1469d6bfb | 146 | return PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 147 | } |