img.c

Go to the documentation of this file.
00001 /*
00002 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
00003 #
00004 # This file is part of the PyMite VM.
00005 # The PyMite VM is free software: you can redistribute it and/or modify
00006 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
00007 #
00008 # The PyMite VM is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
00012 # is seen in the file COPYING in this directory.
00013 */
00014 
00015 
00016 #undef __FILE_ID__
00017 #define __FILE_ID__ 0x07
00018 
00019 
00029 #include "pm.h"
00030 
00031 
00032 /*
00033  * Searches for a module's name in a contiguous array of images
00034  * in the given namespace starting at the given address.
00035  * A module's name is stored in the last index of the names tuple of an image.
00036  */
00037 static PmReturn_t
00038 img_findInPath(uint8_t *cname, uint8_t cnamelen, PmMemSpace_t memspace,
00039                uint8_t const **paddr)
00040 {
00041     uint8_t const *imgtop;
00042     PmType_t type;
00043     uint16_t len;
00044     int16_t size = 0;
00045     uint8_t i = 0;
00046 
00047     /* Addr is top of img */
00048     imgtop = *paddr;
00049 
00050     /* Get img's type byte */
00051     type = (PmType_t)mem_getByte(memspace, paddr);
00052 
00053     /* Search all sequential images */
00054     while (type == OBJ_TYPE_CIM)
00055     {
00056         /* Use size field to calc addr of next potential img */
00057         size = mem_getWord(memspace, paddr);
00058 
00059         /* Point to names tuple */
00060         *paddr = imgtop + CI_NAMES_FIELD;
00061 
00062         /* Ensure it's a tuple */
00063         type = (PmType_t)mem_getByte(memspace, paddr);
00064         C_ASSERT(type == OBJ_TYPE_TUP);
00065 
00066         /* Scan to last name in tuple (it's the module's name) */
00067         i = mem_getByte(memspace, paddr) - (uint8_t)1;
00068         for (; i > 0; i--)
00069         {
00070             /* Ensure obj is a string */
00071             type = (PmType_t)mem_getByte(memspace, paddr);
00072             C_ASSERT(type == OBJ_TYPE_STR);
00073 
00074             /* Skip the length of the string */
00075             len = mem_getWord(memspace, paddr);
00076             (*paddr) += len;
00077         }
00078 
00079         /* Ensure it's a string */
00080         type = (PmType_t)mem_getByte(memspace, paddr);
00081         C_ASSERT(type == OBJ_TYPE_STR);
00082 
00083         /* If strings match, return the address of this image */
00084         if ((cnamelen == mem_getWord(memspace, paddr))
00085             && (PM_RET_OK == mem_cmpn(cname, cnamelen, memspace, paddr)))
00086         {
00087             *paddr = imgtop;
00088             return PM_RET_OK;
00089         }
00090 
00091         /* Calc imgtop for next iteration */
00092         imgtop += size;
00093 
00094         /* Point to next potential img */
00095         *paddr = imgtop;
00096 
00097         /* Check if another img follows this one */
00098         type = (PmType_t)mem_getByte(memspace, paddr);
00099     }
00100     return PM_RET_NO;
00101 }
00102 
00103 
00104 PmReturn_t
00105 img_findInPaths(pPmObj_t pname, PmMemSpace_t *r_memspace,
00106                 uint8_t const **r_imgaddr)
00107 {
00108     uint8_t i;
00109     PmReturn_t retval = PM_RET_NO;
00110 
00111     /* Search in each path in the paths */
00112     for (i = 0; i < gVmGlobal.imgPaths.pathcount; i++)
00113     {
00114         *r_imgaddr = gVmGlobal.imgPaths.pimg[i];
00115         *r_memspace = gVmGlobal.imgPaths.memspace[i];
00116         retval = img_findInPath(((pPmString_t)pname)->val,
00117                                 ((pPmString_t)pname)->length,
00118                                 *r_memspace, r_imgaddr);
00119         if (retval == PM_RET_NO)
00120         {
00121             continue;
00122         }
00123         else if (retval == PM_RET_OK)
00124         {
00125             break;
00126         }
00127         else
00128         {
00129             return retval;
00130         }
00131     }
00132 
00133     return retval;
00134 }
00135 
00136 
00137 PmReturn_t
00138 img_appendToPath(PmMemSpace_t memspace, uint8_t const * const paddr)
00139 {
00140     uint8_t i;
00141 
00142     if (gVmGlobal.imgPaths.pathcount >= PM_NUM_IMG_PATHS)
00143     {
00144         return PM_RET_NO;
00145     }
00146 
00147     i = gVmGlobal.imgPaths.pathcount;
00148 
00149     gVmGlobal.imgPaths.memspace[i] = memspace;
00150     gVmGlobal.imgPaths.pimg[i] = paddr;
00151     gVmGlobal.imgPaths.pathcount++;
00152 
00153     return PM_RET_OK;
00154 }

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9