python-on-a-chip online compiler

Dependencies:   mbed TSI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mem.c Source File

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