python

Dependencies:   TSI mbed

Fork of pymite by Norimasa Okamoto

Committer:
va009039
Date:
Sat Mar 02 11:54:20 2013 +0000
Revision:
0:65f1469d6bfb
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:65f1469d6bfb 1 /*
va009039 0:65f1469d6bfb 2 # This file is Copyright 2009 Dean Hall.
va009039 0:65f1469d6bfb 3 #
va009039 0:65f1469d6bfb 4 # This file is part of the Python-on-a-Chip program.
va009039 0:65f1469d6bfb 5 # Python-on-a-Chip is free software: you can redistribute it and/or modify
va009039 0:65f1469d6bfb 6 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
va009039 0:65f1469d6bfb 7 #
va009039 0:65f1469d6bfb 8 # Python-on-a-Chip is distributed in the hope that it will be useful,
va009039 0:65f1469d6bfb 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
va009039 0:65f1469d6bfb 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
va009039 0:65f1469d6bfb 11 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
va009039 0:65f1469d6bfb 12 # is seen in the file COPYING up one directory from this.
va009039 0:65f1469d6bfb 13 */
va009039 0:65f1469d6bfb 14
va009039 0:65f1469d6bfb 15
va009039 0:65f1469d6bfb 16 #undef __FILE_ID__
va009039 0:65f1469d6bfb 17 #define __FILE_ID__ 0x70
va009039 0:65f1469d6bfb 18
va009039 0:65f1469d6bfb 19
va009039 0:65f1469d6bfb 20 /** PyMite platform-specific routines for ARM7 target */
va009039 0:65f1469d6bfb 21
va009039 0:65f1469d6bfb 22
va009039 0:65f1469d6bfb 23 #include "mbed.h"
va009039 0:65f1469d6bfb 24 #include "pm.h"
va009039 0:65f1469d6bfb 25
va009039 0:65f1469d6bfb 26
va009039 0:65f1469d6bfb 27 #define CALLBACK_MS 10
va009039 0:65f1469d6bfb 28
va009039 0:65f1469d6bfb 29
va009039 0:65f1469d6bfb 30 Serial serial(USBTX, USBRX, "serial");
va009039 0:65f1469d6bfb 31 Ticker ticker;
va009039 0:65f1469d6bfb 32
va009039 0:65f1469d6bfb 33
va009039 0:65f1469d6bfb 34 static void
va009039 0:65f1469d6bfb 35 ticker_callback(void)
va009039 0:65f1469d6bfb 36 {
va009039 0:65f1469d6bfb 37 PmReturn_t retval;
va009039 0:65f1469d6bfb 38
va009039 0:65f1469d6bfb 39 retval = pm_vmPeriodic(CALLBACK_MS * 1000);
va009039 0:65f1469d6bfb 40 PM_REPORT_IF_ERROR(retval);
va009039 0:65f1469d6bfb 41 }
va009039 0:65f1469d6bfb 42
va009039 0:65f1469d6bfb 43
va009039 0:65f1469d6bfb 44 PmReturn_t
va009039 0:65f1469d6bfb 45 plat_init(void)
va009039 0:65f1469d6bfb 46 {
va009039 0:65f1469d6bfb 47 serial.baud(19200);
va009039 0:65f1469d6bfb 48 serial.format(8, serial.None, 1);
va009039 0:65f1469d6bfb 49
va009039 0:65f1469d6bfb 50 ticker.attach_us(ticker_callback, CALLBACK_MS * 1000);
va009039 0:65f1469d6bfb 51
va009039 0:65f1469d6bfb 52 return PM_RET_OK;
va009039 0:65f1469d6bfb 53 }
va009039 0:65f1469d6bfb 54
va009039 0:65f1469d6bfb 55
va009039 0:65f1469d6bfb 56 PmReturn_t
va009039 0:65f1469d6bfb 57 plat_deinit(void)
va009039 0:65f1469d6bfb 58 {
va009039 0:65f1469d6bfb 59 /* Detach the callback from the ticker */
va009039 0:65f1469d6bfb 60 ticker.detach();
va009039 0:65f1469d6bfb 61
va009039 0:65f1469d6bfb 62 return PM_RET_OK;
va009039 0:65f1469d6bfb 63 }
va009039 0:65f1469d6bfb 64
va009039 0:65f1469d6bfb 65
va009039 0:65f1469d6bfb 66 uint8_t
va009039 0:65f1469d6bfb 67 plat_memGetByte(PmMemSpace_t memspace, uint8_t const **paddr)
va009039 0:65f1469d6bfb 68 {
va009039 0:65f1469d6bfb 69 uint8_t b = 0;
va009039 0:65f1469d6bfb 70
va009039 0:65f1469d6bfb 71 switch (memspace)
va009039 0:65f1469d6bfb 72 {
va009039 0:65f1469d6bfb 73 case MEMSPACE_RAM:
va009039 0:65f1469d6bfb 74 case MEMSPACE_PROG:
va009039 0:65f1469d6bfb 75 b = **paddr;
va009039 0:65f1469d6bfb 76 *paddr += 1;
va009039 0:65f1469d6bfb 77 return b;
va009039 0:65f1469d6bfb 78
va009039 0:65f1469d6bfb 79 case MEMSPACE_EEPROM:
va009039 0:65f1469d6bfb 80 case MEMSPACE_SEEPROM:
va009039 0:65f1469d6bfb 81 case MEMSPACE_OTHER0:
va009039 0:65f1469d6bfb 82 case MEMSPACE_OTHER1:
va009039 0:65f1469d6bfb 83 case MEMSPACE_OTHER2:
va009039 0:65f1469d6bfb 84 case MEMSPACE_OTHER3:
va009039 0:65f1469d6bfb 85 default:
va009039 0:65f1469d6bfb 86 return 0;
va009039 0:65f1469d6bfb 87 }
va009039 0:65f1469d6bfb 88 }
va009039 0:65f1469d6bfb 89
va009039 0:65f1469d6bfb 90
va009039 0:65f1469d6bfb 91 PmReturn_t
va009039 0:65f1469d6bfb 92 plat_getByte(uint8_t *b)
va009039 0:65f1469d6bfb 93 {
va009039 0:65f1469d6bfb 94 int c;
va009039 0:65f1469d6bfb 95 PmReturn_t retval = PM_RET_OK;
va009039 0:65f1469d6bfb 96
va009039 0:65f1469d6bfb 97 c = serial.getc();
va009039 0:65f1469d6bfb 98 *b = c & 0xFF;
va009039 0:65f1469d6bfb 99
va009039 0:65f1469d6bfb 100 if (c > 0xFF)
va009039 0:65f1469d6bfb 101 {
va009039 0:65f1469d6bfb 102 PM_RAISE(retval, PM_RET_EX_IO);
va009039 0:65f1469d6bfb 103 }
va009039 0:65f1469d6bfb 104
va009039 0:65f1469d6bfb 105 return retval;
va009039 0:65f1469d6bfb 106 }
va009039 0:65f1469d6bfb 107
va009039 0:65f1469d6bfb 108
va009039 0:65f1469d6bfb 109 PmReturn_t
va009039 0:65f1469d6bfb 110 plat_putByte(uint8_t b)
va009039 0:65f1469d6bfb 111 {
va009039 0:65f1469d6bfb 112 while (!serial.writeable());
va009039 0:65f1469d6bfb 113 serial.putc(b);
va009039 0:65f1469d6bfb 114
va009039 0:65f1469d6bfb 115 return PM_RET_OK;
va009039 0:65f1469d6bfb 116 }
va009039 0:65f1469d6bfb 117
va009039 0:65f1469d6bfb 118
va009039 0:65f1469d6bfb 119 PmReturn_t
va009039 0:65f1469d6bfb 120 plat_getMsTicks(uint32_t *r_ticks)
va009039 0:65f1469d6bfb 121 {
va009039 0:65f1469d6bfb 122 *r_ticks = pm_timerMsTicks;
va009039 0:65f1469d6bfb 123
va009039 0:65f1469d6bfb 124 return PM_RET_OK;
va009039 0:65f1469d6bfb 125 }
va009039 0:65f1469d6bfb 126
va009039 0:65f1469d6bfb 127
va009039 0:65f1469d6bfb 128 void
va009039 0:65f1469d6bfb 129 plat_reportError(PmReturn_t result)
va009039 0:65f1469d6bfb 130 {
va009039 0:65f1469d6bfb 131
va009039 0:65f1469d6bfb 132 #ifdef HAVE_DEBUG_INFO
va009039 0:65f1469d6bfb 133 #define LEN_FNLOOKUP 26
va009039 0:65f1469d6bfb 134 #define LEN_EXNLOOKUP 18
va009039 0:65f1469d6bfb 135
va009039 0:65f1469d6bfb 136 uint8_t res;
va009039 0:65f1469d6bfb 137 pPmFrame_t pframe;
va009039 0:65f1469d6bfb 138 pPmObj_t pstr;
va009039 0:65f1469d6bfb 139 PmReturn_t retval;
va009039 0:65f1469d6bfb 140 uint16_t bcindex;
va009039 0:65f1469d6bfb 141 uint16_t bcsum;
va009039 0:65f1469d6bfb 142 uint16_t linesum;
va009039 0:65f1469d6bfb 143 uint16_t len_lnotab;
va009039 0:65f1469d6bfb 144 uint8_t const *plnotab;
va009039 0:65f1469d6bfb 145 uint16_t i;
va009039 0:65f1469d6bfb 146
va009039 0:65f1469d6bfb 147 /* This table should match src/vm/fileid.txt */
va009039 0:65f1469d6bfb 148 char const * const fnlookup[LEN_FNLOOKUP] = {
va009039 0:65f1469d6bfb 149 "<no file>",
va009039 0:65f1469d6bfb 150 "codeobj.c",
va009039 0:65f1469d6bfb 151 "dict.c",
va009039 0:65f1469d6bfb 152 "frame.c",
va009039 0:65f1469d6bfb 153 "func.c",
va009039 0:65f1469d6bfb 154 "global.c",
va009039 0:65f1469d6bfb 155 "heap.c",
va009039 0:65f1469d6bfb 156 "img.c",
va009039 0:65f1469d6bfb 157 "int.c",
va009039 0:65f1469d6bfb 158 "interp.c",
va009039 0:65f1469d6bfb 159 "pmstdlib_nat.c",
va009039 0:65f1469d6bfb 160 "list.c",
va009039 0:65f1469d6bfb 161 "main.c",
va009039 0:65f1469d6bfb 162 "mem.c",
va009039 0:65f1469d6bfb 163 "module.c",
va009039 0:65f1469d6bfb 164 "obj.c",
va009039 0:65f1469d6bfb 165 "seglist.c",
va009039 0:65f1469d6bfb 166 "sli.c",
va009039 0:65f1469d6bfb 167 "strobj.c",
va009039 0:65f1469d6bfb 168 "tuple.c",
va009039 0:65f1469d6bfb 169 "seq.c",
va009039 0:65f1469d6bfb 170 "pm.c",
va009039 0:65f1469d6bfb 171 "thread.c",
va009039 0:65f1469d6bfb 172 "float.c",
va009039 0:65f1469d6bfb 173 "class.c",
va009039 0:65f1469d6bfb 174 "bytearray.c",
va009039 0:65f1469d6bfb 175 };
va009039 0:65f1469d6bfb 176
va009039 0:65f1469d6bfb 177 /* This table should match src/vm/pm.h PmReturn_t */
va009039 0:65f1469d6bfb 178 char const * const exnlookup[LEN_EXNLOOKUP] = {
va009039 0:65f1469d6bfb 179 "Exception",
va009039 0:65f1469d6bfb 180 "SystemExit",
va009039 0:65f1469d6bfb 181 "IoError",
va009039 0:65f1469d6bfb 182 "ZeroDivisionError",
va009039 0:65f1469d6bfb 183 "AssertionError",
va009039 0:65f1469d6bfb 184 "AttributeError",
va009039 0:65f1469d6bfb 185 "ImportError",
va009039 0:65f1469d6bfb 186 "IndexError",
va009039 0:65f1469d6bfb 187 "KeyError",
va009039 0:65f1469d6bfb 188 "MemoryError",
va009039 0:65f1469d6bfb 189 "NameError",
va009039 0:65f1469d6bfb 190 "SyntaxError",
va009039 0:65f1469d6bfb 191 "SystemError",
va009039 0:65f1469d6bfb 192 "TypeError",
va009039 0:65f1469d6bfb 193 "ValueError",
va009039 0:65f1469d6bfb 194 "StopIteration",
va009039 0:65f1469d6bfb 195 "Warning",
va009039 0:65f1469d6bfb 196 "OverflowError",
va009039 0:65f1469d6bfb 197 };
va009039 0:65f1469d6bfb 198
va009039 0:65f1469d6bfb 199 /* Print traceback */
va009039 0:65f1469d6bfb 200 printf("Traceback (most recent call first):\n");
va009039 0:65f1469d6bfb 201
va009039 0:65f1469d6bfb 202 /* Get the top frame */
va009039 0:65f1469d6bfb 203 pframe = gVmGlobal.pthread->pframe;
va009039 0:65f1469d6bfb 204
va009039 0:65f1469d6bfb 205 /* If it's the native frame, print the native function name */
va009039 0:65f1469d6bfb 206 if (pframe == (pPmFrame_t)&(gVmGlobal.nativeframe))
va009039 0:65f1469d6bfb 207 {
va009039 0:65f1469d6bfb 208
va009039 0:65f1469d6bfb 209 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 210 retval = tuple_getItem((pPmObj_t)gVmGlobal.nativeframe.nf_func->
va009039 0:65f1469d6bfb 211 f_co->co_names, -1, &pstr);
va009039 0:65f1469d6bfb 212 if ((retval) != PM_RET_OK)
va009039 0:65f1469d6bfb 213 {
va009039 0:65f1469d6bfb 214 printf(" Unable to get native func name.\n");
va009039 0:65f1469d6bfb 215 return;
va009039 0:65f1469d6bfb 216 }
va009039 0:65f1469d6bfb 217 else
va009039 0:65f1469d6bfb 218 {
va009039 0:65f1469d6bfb 219 printf(" %s() __NATIVE__\n", ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 220 }
va009039 0:65f1469d6bfb 221
va009039 0:65f1469d6bfb 222 /* Get the frame that called the native frame */
va009039 0:65f1469d6bfb 223 pframe = (pPmFrame_t)gVmGlobal.nativeframe.nf_back;
va009039 0:65f1469d6bfb 224 }
va009039 0:65f1469d6bfb 225
va009039 0:65f1469d6bfb 226 /* Print the remaining frame stack */
va009039 0:65f1469d6bfb 227 for (; pframe != C_NULL; pframe = pframe->fo_back)
va009039 0:65f1469d6bfb 228 {
va009039 0:65f1469d6bfb 229 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 230 retval = tuple_getItem((pPmObj_t)pframe->fo_func->f_co->co_names,
va009039 0:65f1469d6bfb 231 -1,
va009039 0:65f1469d6bfb 232 &pstr);
va009039 0:65f1469d6bfb 233 if ((retval) != PM_RET_OK) break;
va009039 0:65f1469d6bfb 234
va009039 0:65f1469d6bfb 235 /*
va009039 0:65f1469d6bfb 236 * Get the line number of the current bytecode. Algorithm comes from:
va009039 0:65f1469d6bfb 237 * http://svn.python.org/view/python/trunk/Objects/lnotab_notes.txt?view=markup
va009039 0:65f1469d6bfb 238 */
va009039 0:65f1469d6bfb 239 bcindex = pframe->fo_ip - pframe->fo_func->f_co->co_codeaddr;
va009039 0:65f1469d6bfb 240 plnotab = pframe->fo_func->f_co->co_lnotab;
va009039 0:65f1469d6bfb 241 len_lnotab = mem_getWord(MEMSPACE_PROG, &plnotab);
va009039 0:65f1469d6bfb 242 bcsum = 0;
va009039 0:65f1469d6bfb 243 linesum = pframe->fo_func->f_co->co_firstlineno;
va009039 0:65f1469d6bfb 244 for (i = 0; i < len_lnotab; i += 2)
va009039 0:65f1469d6bfb 245 {
va009039 0:65f1469d6bfb 246 bcsum += mem_getByte(MEMSPACE_PROG, &plnotab);
va009039 0:65f1469d6bfb 247 if (bcsum > bcindex) break;
va009039 0:65f1469d6bfb 248 linesum += mem_getByte(MEMSPACE_PROG, &plnotab);
va009039 0:65f1469d6bfb 249 }
va009039 0:65f1469d6bfb 250 printf(" File \"%s\", line %d, in %s\n",
va009039 0:65f1469d6bfb 251 ((pPmFrame_t)pframe)->fo_func->f_co->co_filename,
va009039 0:65f1469d6bfb 252 linesum,
va009039 0:65f1469d6bfb 253 ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 254 }
va009039 0:65f1469d6bfb 255
va009039 0:65f1469d6bfb 256 /* Print error */
va009039 0:65f1469d6bfb 257 res = (uint8_t)result;
va009039 0:65f1469d6bfb 258 if ((res > 0) && ((res - PM_RET_EX) < LEN_EXNLOOKUP))
va009039 0:65f1469d6bfb 259 {
va009039 0:65f1469d6bfb 260 printf("%s", exnlookup[res - PM_RET_EX]);
va009039 0:65f1469d6bfb 261 }
va009039 0:65f1469d6bfb 262 else
va009039 0:65f1469d6bfb 263 {
va009039 0:65f1469d6bfb 264 printf("Error code 0x%02X", result);
va009039 0:65f1469d6bfb 265 }
va009039 0:65f1469d6bfb 266 printf(" detected by ");
va009039 0:65f1469d6bfb 267
va009039 0:65f1469d6bfb 268 if ((gVmGlobal.errFileId > 0) && (gVmGlobal.errFileId < LEN_FNLOOKUP))
va009039 0:65f1469d6bfb 269 {
va009039 0:65f1469d6bfb 270 printf("%s:", fnlookup[gVmGlobal.errFileId]);
va009039 0:65f1469d6bfb 271 }
va009039 0:65f1469d6bfb 272 else
va009039 0:65f1469d6bfb 273 {
va009039 0:65f1469d6bfb 274 printf("FileId 0x%02X line ", gVmGlobal.errFileId);
va009039 0:65f1469d6bfb 275 }
va009039 0:65f1469d6bfb 276 printf("%d\n", gVmGlobal.errLineNum);
va009039 0:65f1469d6bfb 277
va009039 0:65f1469d6bfb 278 #else /* HAVE_DEBUG_INFO */
va009039 0:65f1469d6bfb 279
va009039 0:65f1469d6bfb 280 /* Print error */
va009039 0:65f1469d6bfb 281 printf("Error: 0x%02X\n", result);
va009039 0:65f1469d6bfb 282 printf(" Release: 0x%02X\n", gVmGlobal.errVmRelease);
va009039 0:65f1469d6bfb 283 printf(" FileId: 0x%02X\n", gVmGlobal.errFileId);
va009039 0:65f1469d6bfb 284 printf(" LineNum: %d\n", gVmGlobal.errLineNum);
va009039 0:65f1469d6bfb 285
va009039 0:65f1469d6bfb 286 /* Print traceback */
va009039 0:65f1469d6bfb 287 {
va009039 0:65f1469d6bfb 288 pPmObj_t pframe;
va009039 0:65f1469d6bfb 289 pPmObj_t pstr;
va009039 0:65f1469d6bfb 290 PmReturn_t retval;
va009039 0:65f1469d6bfb 291
va009039 0:65f1469d6bfb 292 printf("Traceback (top first):\n");
va009039 0:65f1469d6bfb 293
va009039 0:65f1469d6bfb 294 /* Get the top frame */
va009039 0:65f1469d6bfb 295 pframe = (pPmObj_t)gVmGlobal.pthread->pframe;
va009039 0:65f1469d6bfb 296
va009039 0:65f1469d6bfb 297 /* If it's the native frame, print the native function name */
va009039 0:65f1469d6bfb 298 if (pframe == (pPmObj_t)&(gVmGlobal.nativeframe))
va009039 0:65f1469d6bfb 299 {
va009039 0:65f1469d6bfb 300
va009039 0:65f1469d6bfb 301 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 302 retval = tuple_getItem((pPmObj_t)gVmGlobal.nativeframe.nf_func->
va009039 0:65f1469d6bfb 303 f_co->co_names, -1, &pstr);
va009039 0:65f1469d6bfb 304 if ((retval) != PM_RET_OK)
va009039 0:65f1469d6bfb 305 {
va009039 0:65f1469d6bfb 306 printf(" Unable to get native func name.\n");
va009039 0:65f1469d6bfb 307 return;
va009039 0:65f1469d6bfb 308 }
va009039 0:65f1469d6bfb 309 else
va009039 0:65f1469d6bfb 310 {
va009039 0:65f1469d6bfb 311 printf(" %s() __NATIVE__\n", ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 312 }
va009039 0:65f1469d6bfb 313
va009039 0:65f1469d6bfb 314 /* Get the frame that called the native frame */
va009039 0:65f1469d6bfb 315 pframe = (pPmObj_t)gVmGlobal.nativeframe.nf_back;
va009039 0:65f1469d6bfb 316 }
va009039 0:65f1469d6bfb 317
va009039 0:65f1469d6bfb 318 /* Print the remaining frame stack */
va009039 0:65f1469d6bfb 319 for (;
va009039 0:65f1469d6bfb 320 pframe != C_NULL;
va009039 0:65f1469d6bfb 321 pframe = (pPmObj_t)((pPmFrame_t)pframe)->fo_back)
va009039 0:65f1469d6bfb 322 {
va009039 0:65f1469d6bfb 323 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 324 retval = tuple_getItem((pPmObj_t)((pPmFrame_t)pframe)->
va009039 0:65f1469d6bfb 325 fo_func->f_co->co_names, -1, &pstr);
va009039 0:65f1469d6bfb 326 if ((retval) != PM_RET_OK) break;
va009039 0:65f1469d6bfb 327
va009039 0:65f1469d6bfb 328 printf(" %s()\n", ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 329 }
va009039 0:65f1469d6bfb 330 printf(" <module>.\n");
va009039 0:65f1469d6bfb 331 }
va009039 0:65f1469d6bfb 332 #endif /* HAVE_DEBUG_INFO */
va009039 0:65f1469d6bfb 333 }