python-on-a-chip online compiler

Dependencies:   mbed TSI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers img.c Source File

img.c

Go to the documentation of this file.
00001 /*
00002 # This file is Copyright 2002 Dean Hall.
00003 # This file is part of the PyMite VM.
00004 # This file is licensed under the MIT License.
00005 # See the LICENSE file for details.
00006 */
00007 
00008 
00009 #undef __FILE_ID__
00010 #define __FILE_ID__ 0x07
00011 
00012 
00013 /**
00014  * \file
00015  * \brief Image routines
00016  *
00017  * Created to eliminate a circular include
00018  * among mem, string and obj.
00019  */
00020 
00021 
00022 #include "pm.h"
00023 
00024 
00025 /*
00026  * Searches for a module's name in a contiguous array of images
00027  * in the given namespace starting at the given address.
00028  * A module's name is stored in the last index of the names tuple of an image.
00029  */
00030 static PmReturn_t
00031 img_findInPath(uint8_t *cname, uint16_t cnamelen, PmMemSpace_t memspace,
00032                uint8_t const **paddr)
00033 {
00034     uint8_t const *imgtop;
00035     PmType_t type;
00036     uint16_t len;
00037     int16_t size = 0;
00038     uint8_t i = 0;
00039 
00040     /* Addr is top of img */
00041     imgtop = *paddr;
00042 
00043     /* Get img's type byte */
00044     type = (PmType_t)mem_getByte(memspace, paddr);
00045 
00046     /* Search all sequential images */
00047     while (type == OBJ_TYPE_CIM)
00048     {
00049         /* Use size field to calc addr of next potential img */
00050         size = mem_getWord(memspace, paddr);
00051 
00052         /* Point to names tuple */
00053         *paddr = imgtop + CI_NAMES_FIELD;
00054 
00055         /* Ensure it's a tuple */
00056         type = (PmType_t)mem_getByte(memspace, paddr);
00057         C_ASSERT(type == OBJ_TYPE_TUP);
00058 
00059         /* Scan to last name in tuple (it's the module's name) */
00060         i = mem_getByte(memspace, paddr) - (uint8_t)1;
00061         for (; i > 0; i--)
00062         {
00063             /* Ensure obj is a string */
00064             type = (PmType_t)mem_getByte(memspace, paddr);
00065             C_ASSERT(type == OBJ_TYPE_STR);
00066 
00067             /* Skip the length of the string */
00068             len = mem_getWord(memspace, paddr);
00069             (*paddr) += len;
00070         }
00071 
00072         /* Ensure it's a string */
00073         type = (PmType_t)mem_getByte(memspace, paddr);
00074         C_ASSERT(type == OBJ_TYPE_STR);
00075 
00076         /* If strings match, return the address of this image */
00077         if ((cnamelen == mem_getWord(memspace, paddr))
00078             && (PM_RET_OK == mem_cmpn(cname, cnamelen, memspace, paddr)))
00079         {
00080             *paddr = imgtop;
00081             return PM_RET_OK;
00082         }
00083 
00084         /* Calc imgtop for next iteration */
00085         imgtop += size;
00086 
00087         /* Point to next potential img */
00088         *paddr = imgtop;
00089 
00090         /* Check if another img follows this one */
00091         type = (PmType_t)mem_getByte(memspace, paddr);
00092     }
00093     return PM_RET_NO;
00094 }
00095 
00096 
00097 PmReturn_t
00098 img_findInPaths(pPmObj_t pname, PmMemSpace_t *r_memspace,
00099                 uint8_t const **r_imgaddr)
00100 {
00101     uint8_t i;
00102     PmReturn_t retval = PM_RET_NO;
00103 
00104     /* Search in each path in the paths */
00105     for (i = 0; i < gVmGlobal.imgPaths.pathcount; i++)
00106     {
00107         *r_imgaddr = gVmGlobal.imgPaths.pimg[i];
00108         *r_memspace = gVmGlobal.imgPaths.memspace[i];
00109         retval = img_findInPath(((pPmString_t)pname)->val,
00110                                 ((pPmString_t)pname)->length,
00111                                 *r_memspace, r_imgaddr);
00112         if (retval == PM_RET_NO)
00113         {
00114             continue;
00115         }
00116         else if (retval == PM_RET_OK)
00117         {
00118             break;
00119         }
00120         else
00121         {
00122             return retval;
00123         }
00124     }
00125 
00126     return retval;
00127 }
00128 
00129 
00130 PmReturn_t
00131 img_appendToPath(PmMemSpace_t memspace, uint8_t const * const paddr)
00132 {
00133     uint8_t i;
00134 
00135     if (gVmGlobal.imgPaths.pathcount >= PM_NUM_IMG_PATHS)
00136     {
00137         return PM_RET_NO;
00138     }
00139 
00140     i = gVmGlobal.imgPaths.pathcount;
00141 
00142     gVmGlobal.imgPaths.memspace[i] = memspace;
00143     gVmGlobal.imgPaths.pimg[i] = paddr;
00144     gVmGlobal.imgPaths.pathcount++;
00145 
00146     return PM_RET_OK;
00147 }