mem.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__ 0x0D
00018 
00019 
00030 #include "pm.h"
00031 
00032 
00033 uint16_t
00034 mem_getWord(PmMemSpace_t memspace, uint8_t const **paddr)
00035 {
00036     /* PyMite is little endian; get low byte first */
00037     uint8_t blo = mem_getByte(memspace, paddr);
00038     uint8_t bhi = mem_getByte(memspace, paddr);
00039 
00040     return (uint16_t)(blo | (bhi << (int8_t)8));
00041 }
00042 
00043 
00044 uint32_t
00045 mem_getInt(PmMemSpace_t memspace, uint8_t const **paddr)
00046 {
00047     /* PyMite is little endian; get low word first */
00048     uint16_t wlo = mem_getWord(memspace, paddr);
00049     uint32_t whi = mem_getWord(memspace, paddr);
00050 
00051     return (uint32_t)(wlo | (whi << (int8_t)16));
00052 }
00053 
00054 
00055 #ifdef HAVE_FLOAT
00056 float
00057 mem_getFloat(PmMemSpace_t memspace, uint8_t const **paddr)
00058 {
00059     union
00060     {
00061         char c[4];
00062         float f;
00063     }
00064     v;
00065 
00066 #ifdef PM_FLOAT_BIG_ENDIAN
00067     /* If the architecture is Big Endian, reverse the bytes of the float */
00068     v.c[3] = mem_getByte(memspace, paddr);
00069     v.c[2] = mem_getByte(memspace, paddr);
00070     v.c[1] = mem_getByte(memspace, paddr);
00071     v.c[0] = mem_getByte(memspace, paddr);
00072 
00073 #else
00074     v.c[0] = mem_getByte(memspace, paddr);
00075     v.c[1] = mem_getByte(memspace, paddr);
00076     v.c[2] = mem_getByte(memspace, paddr);
00077     v.c[3] = mem_getByte(memspace, paddr);
00078 
00079 #ifndef PM_FLOAT_LITTLE_ENDIAN
00080 #warning Neither PM_FLOAT_LITTLE_ENDIAN nor PM_FLOAT_BIG_ENDIAN is defined \
00081          for this platform; defaulting to little endian.
00082 #endif
00083 #endif
00084 
00085     return v.f;
00086 }
00087 #endif /* HAVE_FLOAT */
00088 
00089 
00090 void
00091 mem_copy(PmMemSpace_t memspace,
00092          uint8_t **pdest, uint8_t const **psrc, uint16_t count)
00093 {
00094     /* Copy memory from RAM */
00095     if (memspace == MEMSPACE_RAM)
00096     {
00097         sli_memcpy(*pdest, *psrc, count);
00098         *psrc += count;
00099         *pdest += count;
00100         return;
00101     }
00102 
00103     /* Copy memory from non-RAM to RAM */
00104     else
00105     {
00106         uint8_t b;
00107 
00108         for (; count > 0; count--)
00109         {
00110             b = mem_getByte(memspace, psrc);
00111             **pdest = b;
00112             (*pdest)++;
00113         }
00114         return;
00115     }
00116 }
00117 
00118 
00119 uint16_t
00120 mem_getStringLength(PmMemSpace_t memspace, uint8_t const *const pstr)
00121 {
00122     uint8_t const *psrc;
00123 
00124     /* If source is in RAM, use a possibly optimized strlen */
00125     if (memspace == MEMSPACE_RAM)
00126     {
00127         return sli_strlen((char const *)pstr);
00128     }
00129 
00130     /* Otherwise calculate string length */
00131     psrc = pstr;
00132     while (mem_getByte(memspace, &psrc) != (uint8_t)0);
00133     return psrc - pstr - 1;
00134 }
00135 
00136 
00137 PmReturn_t
00138 mem_cmpn(uint8_t *cname, uint8_t cnamelen, PmMemSpace_t memspace,
00139          uint8_t const **paddr)
00140 {
00141     uint8_t i;
00142     uint8_t b;
00143 
00144     /* Iterate over all characters */
00145     for (i = 0; i < cnamelen; i++)
00146     {
00147         b = mem_getByte(memspace, paddr);
00148         if (cname[i] != b)
00149         {
00150             return PM_RET_NO;
00151         }
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