sys.py

00001 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 #
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING in this directory.
00012 
00013 ## @package sys
00014 #  @brief Provides PyMite's system module, sys
00015 #
00016 #  USAGE
00017 #  -----
00018 #
00019 #  import sys
00020 #
00021 
00022 #### TODO
00023 # modules = None #set ptr to dict w/native func
00024 # platform string or device id, rand
00025 # ver = "0.1"            # XXX compile date & platform?
00026 # Example: sys.version = '2.4.1 (#1, Feb 26 2006, 16:26:36) \n[GCC 4.0.0 20041026 (Apple Computer, Inc. build 4061)]'
00027 
00028 
00029 maxint = 0x7FFFFFFF     # 2147483647
00030 
00031 
00032 def exit(val):
00033     """__NATIVE__
00034     pPmObj_t pval = C_NULL;
00035     PmReturn_t retval;
00036 
00037     /* If no arg given, assume return 0 */
00038     if (NATIVE_GET_NUM_ARGS() == 0)
00039     {
00040         NATIVE_SET_TOS(PM_ZERO);
00041     }
00042 
00043     /* If 1 arg given, put it on stack */
00044     else if (NATIVE_GET_NUM_ARGS() == 1)
00045     {
00046         pval = NATIVE_GET_LOCAL(0);
00047         NATIVE_SET_TOS(pval);
00048     }
00049 
00050     /* If wrong number of args, raise TypeError */
00051     else
00052     {
00053         PM_RAISE(retval, PM_RET_EX_TYPE);
00054         return retval;
00055     }
00056 
00057     /* Raise the SystemExit exception */
00058     PM_RAISE(retval, PM_RET_EX_EXIT);
00059     return retval;
00060     """
00061     pass
00062 
00063 
00064 #
00065 # Runs the Garbage Collector
00066 #
00067 def gc():
00068     """__NATIVE__
00069     PmReturn_t retval = PM_RET_OK;
00070 #ifdef HAVE_GC
00071     /* If wrong number of args, raise TypeError */
00072     if (NATIVE_GET_NUM_ARGS() != 0)
00073     {
00074         PM_RAISE(retval, PM_RET_EX_TYPE);
00075         return retval;
00076     }
00077 
00078     retval = heap_gcRun();
00079 #endif
00080     NATIVE_SET_TOS(PM_NONE);
00081 
00082     return retval;
00083     """
00084     pass
00085 
00086 
00087 #
00088 # Gets a byte from the platform's default I/O
00089 # Returns the byte in the LSB of the returned integer
00090 #
00091 def getb():
00092     """__NATIVE__
00093     uint8_t b;
00094     pPmObj_t pb;
00095     PmReturn_t retval;
00096 
00097     /* If wrong number of args, raise TypeError */
00098     if (NATIVE_GET_NUM_ARGS() != 0)
00099     {
00100         PM_RAISE(retval, PM_RET_EX_TYPE);
00101         return retval;
00102     }
00103 
00104     retval = plat_getByte(&b);
00105     PM_RETURN_IF_ERROR(retval);
00106 
00107     retval = int_new((int32_t)b, &pb);
00108     NATIVE_SET_TOS(pb);
00109     return retval;
00110     """
00111     pass
00112 
00113 
00114 #
00115 # Returns a tuple containing the amout of heap available and the maximum
00116 #
00117 def heap():
00118     """__NATIVE__
00119     PmReturn_t retval;
00120     pPmObj_t pavail;
00121     pPmObj_t pmax;
00122     pPmObj_t ptup;
00123     uint8_t objid;
00124 
00125     /* If wrong number of args, raise TypeError */
00126     if (NATIVE_GET_NUM_ARGS() != 0)
00127     {
00128         PM_RAISE(retval, PM_RET_EX_TYPE);
00129         return retval;
00130     }
00131 
00132     /* Allocate a tuple to store the return values */
00133     retval = tuple_new(2, &ptup);
00134     PM_RETURN_IF_ERROR(retval);
00135 
00136     /* Get the maximum heap size */
00137     heap_gcPushTempRoot(ptup, &objid);
00138     retval = int_new(PM_HEAP_SIZE, &pmax);
00139     if (retval != PM_RET_OK)
00140     {
00141         heap_gcPopTempRoot(objid);
00142         return retval;
00143     }
00144 
00145     /* Allocate an int to hold the amount of heap available */
00146     retval = int_new(heap_getAvail() - sizeof(PmInt_t), &pavail);
00147     heap_gcPopTempRoot(objid);
00148     PM_RETURN_IF_ERROR(retval);
00149 
00150     /* Put the two heap values in the tuple */
00151     ((pPmTuple_t)ptup)->val[0] = pavail;
00152     ((pPmTuple_t)ptup)->val[1] = pmax;
00153 
00154     /* Return the tuple on the stack */
00155     NATIVE_SET_TOS(ptup);
00156 
00157     return retval;
00158     """
00159     pass
00160 
00161 
00162 #
00163 # Sends the LSB of the integer out the platform's default I/O
00164 #
00165 def putb(b):
00166     """__NATIVE__
00167     uint8_t b;
00168     pPmObj_t pb;
00169     PmReturn_t retval;
00170 
00171     pb = NATIVE_GET_LOCAL(0);
00172 
00173     /* If wrong number of args, raise TypeError */
00174     if (NATIVE_GET_NUM_ARGS() != 1)
00175     {
00176         PM_RAISE(retval, PM_RET_EX_TYPE);
00177         return retval;
00178     }
00179 
00180     /* If arg is not an int, raise TypeError */
00181     if (OBJ_GET_TYPE(pb) != OBJ_TYPE_INT)
00182     {
00183         PM_RAISE(retval, PM_RET_EX_TYPE);
00184         return retval;
00185     }
00186 
00187     b = ((pPmInt_t)pb)->val & 0xFF;
00188     retval = plat_putByte(b);
00189     NATIVE_SET_TOS(PM_NONE);
00190     return retval;
00191     """
00192     pass
00193 
00194 
00195 #
00196 # Runs the given function in a thread sharing the current global namespace
00197 #
00198 def runInThread(f):
00199     """__NATIVE__
00200     PmReturn_t retval;
00201     pPmObj_t pf;
00202 
00203     /* If wrong number of args, raise TypeError */
00204     if (NATIVE_GET_NUM_ARGS() != 1)
00205     {
00206         PM_RAISE(retval, PM_RET_EX_TYPE);
00207         return retval;
00208     }
00209 
00210     /* If arg is not a function, raise TypeError */
00211     pf = NATIVE_GET_LOCAL(0);
00212     if (OBJ_GET_TYPE(pf) != OBJ_TYPE_FXN)
00213     {
00214         PM_RAISE(retval, PM_RET_EX_TYPE);
00215         return retval;
00216     }
00217 
00218     retval = interp_addThread((pPmFunc_t)pf);
00219     NATIVE_SET_TOS(PM_NONE);
00220     return retval;
00221     """
00222     pass
00223 
00224 
00225 #
00226 # Returns the number of milliseconds since the PyMite VM was initialized
00227 #
00228 def time():
00229     """__NATIVE__
00230     uint32_t t;
00231     pPmObj_t pt;
00232     PmReturn_t retval;
00233 
00234     /* If wrong number of args, raise TypeError */
00235     if (NATIVE_GET_NUM_ARGS() != 0)
00236     {
00237         PM_RAISE(retval, PM_RET_EX_TYPE);
00238         return retval;
00239     }
00240 
00241     /* Get the system time (milliseconds since init) */
00242     retval = plat_getMsTicks(&t);
00243     PM_RETURN_IF_ERROR(retval);
00244 
00245     /*
00246      * Raise ValueError if there is an overflow
00247      * (plat_getMsTicks is unsigned; int is signed)
00248      */
00249     if ((int32_t)t < 0)
00250     {
00251         PM_RAISE(retval, PM_RET_EX_VAL);
00252         return retval;
00253     }
00254 
00255     /* Return an int object with the time value */
00256     retval = int_new((int32_t)t, &pt);
00257     NATIVE_SET_TOS(pt);
00258     return retval;
00259     """
00260     pass
00261 
00262 
00263 #
00264 # Waits in a busy loop for the given number of milliseconds
00265 #
00266 def wait(ms):
00267     t = time() + ms
00268     while time() < t:
00269         pass
00270 
00271 
00272 # :mode=c:

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