This is the open source Pawn interpreter ported to mbed. See here: http://www.compuphase.com/pawn/pawn.htm and here: http://code.google.com/p/pawnscript/

Dependents:   Pawn4Test

Some instructions:

  • Put the attached include folder next to your source, so when you compile you get all the proper definitions
  • Use the attached main.p as a starting point if you wish
  • Compile your main.p into main.amx - Put your main.amx on the mbed 'drive'
  • Reset and be amazed.

Important Compile Notes:

  • You should use the -S# option to define a smaller default stack size. Start with -S64 and go up from there if needed.
  • To use on the Cortex-M0 version of the mbed (LPC11U24), you MUST include the TARGET=3 command-line option as well, so the pin names are properly defined. In the future this may be handled on the native code side.

Known Issues:

  • At the moment it appears the kbhit() function is not working right - at least on my mac. Will continue testing on Windows. Working fine.

Todo:

  • Add more wrappers for the mbed peripherals
  • Add Pawn overlay support, to allow much larger scripts to run (even on the LPC11U24)
Committer:
Lobo
Date:
Fri May 24 17:49:26 2013 +0000
Revision:
3:185fdbb7ccf0
Parent:
0:3ab1d2d14eb3
Now includes AnalogIn and AnalogOut functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerwilson 0:3ab1d2d14eb3 1 /* Support routines for the Pawn Abstract Machine
tylerwilson 0:3ab1d2d14eb3 2 *
tylerwilson 0:3ab1d2d14eb3 3 * Copyright (c) ITB CompuPhase, 2003-2011
tylerwilson 0:3ab1d2d14eb3 4 *
tylerwilson 0:3ab1d2d14eb3 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
tylerwilson 0:3ab1d2d14eb3 6 * use this file except in compliance with the License. You may obtain a copy
tylerwilson 0:3ab1d2d14eb3 7 * of the License at
tylerwilson 0:3ab1d2d14eb3 8 *
tylerwilson 0:3ab1d2d14eb3 9 * http://www.apache.org/licenses/LICENSE-2.0
tylerwilson 0:3ab1d2d14eb3 10 *
tylerwilson 0:3ab1d2d14eb3 11 * Unless required by applicable law or agreed to in writing, software
tylerwilson 0:3ab1d2d14eb3 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
tylerwilson 0:3ab1d2d14eb3 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
tylerwilson 0:3ab1d2d14eb3 14 * License for the specific language governing permissions and limitations
tylerwilson 0:3ab1d2d14eb3 15 * under the License.
tylerwilson 0:3ab1d2d14eb3 16 *
tylerwilson 0:3ab1d2d14eb3 17 * Version: $Id: amxaux.c 4523 2011-06-21 15:03:47Z thiadmer $
tylerwilson 0:3ab1d2d14eb3 18 */
tylerwilson 0:3ab1d2d14eb3 19 #include <stdio.h>
tylerwilson 0:3ab1d2d14eb3 20 #include <stdlib.h>
tylerwilson 0:3ab1d2d14eb3 21 #include <string.h>
tylerwilson 0:3ab1d2d14eb3 22 #include "amx.h"
tylerwilson 0:3ab1d2d14eb3 23 #include "amxaux.h"
tylerwilson 0:3ab1d2d14eb3 24
tylerwilson 0:3ab1d2d14eb3 25 size_t AMXAPI aux_ProgramSize(const char *filename)
tylerwilson 0:3ab1d2d14eb3 26 {
tylerwilson 0:3ab1d2d14eb3 27 FILE *fp;
tylerwilson 0:3ab1d2d14eb3 28 AMX_HEADER hdr;
tylerwilson 0:3ab1d2d14eb3 29
tylerwilson 0:3ab1d2d14eb3 30 if ((fp=fopen(filename,"rb")) == NULL)
tylerwilson 0:3ab1d2d14eb3 31 return 0;
tylerwilson 0:3ab1d2d14eb3 32 fread(&hdr, sizeof hdr, 1, fp);
tylerwilson 0:3ab1d2d14eb3 33 fclose(fp);
tylerwilson 0:3ab1d2d14eb3 34
tylerwilson 0:3ab1d2d14eb3 35 amx_Align16(&hdr.magic);
tylerwilson 0:3ab1d2d14eb3 36 amx_Align32((uint32_t *)&hdr.stp);
tylerwilson 0:3ab1d2d14eb3 37 return (hdr.magic==AMX_MAGIC) ? (size_t)hdr.stp : 0;
tylerwilson 0:3ab1d2d14eb3 38 }
tylerwilson 0:3ab1d2d14eb3 39
tylerwilson 0:3ab1d2d14eb3 40 int AMXAPI aux_LoadProgram(AMX *amx, const char *filename, void *memblock)
tylerwilson 0:3ab1d2d14eb3 41 {
tylerwilson 0:3ab1d2d14eb3 42 FILE *fp;
tylerwilson 0:3ab1d2d14eb3 43 AMX_HEADER hdr;
tylerwilson 0:3ab1d2d14eb3 44 int result, didalloc;
tylerwilson 0:3ab1d2d14eb3 45
tylerwilson 0:3ab1d2d14eb3 46 /* open the file, read and check the header */
tylerwilson 0:3ab1d2d14eb3 47 if ((fp = fopen(filename, "rb")) == NULL)
tylerwilson 0:3ab1d2d14eb3 48 return AMX_ERR_NOTFOUND;
tylerwilson 0:3ab1d2d14eb3 49 fread(&hdr, sizeof hdr, 1, fp);
tylerwilson 0:3ab1d2d14eb3 50 amx_Align16(&hdr.magic);
tylerwilson 0:3ab1d2d14eb3 51 amx_Align32((uint32_t *)&hdr.size);
tylerwilson 0:3ab1d2d14eb3 52 amx_Align32((uint32_t *)&hdr.stp);
tylerwilson 0:3ab1d2d14eb3 53 if (hdr.magic != AMX_MAGIC) {
tylerwilson 0:3ab1d2d14eb3 54 fclose(fp);
tylerwilson 0:3ab1d2d14eb3 55 return AMX_ERR_FORMAT;
tylerwilson 0:3ab1d2d14eb3 56 } /* if */
tylerwilson 0:3ab1d2d14eb3 57
tylerwilson 0:3ab1d2d14eb3 58 /* allocate the memblock if it is NULL */
tylerwilson 0:3ab1d2d14eb3 59 didalloc = 0;
tylerwilson 0:3ab1d2d14eb3 60 if (memblock == NULL) {
tylerwilson 0:3ab1d2d14eb3 61 if ((memblock = malloc(hdr.stp)) == NULL) {
tylerwilson 0:3ab1d2d14eb3 62 fclose(fp);
tylerwilson 0:3ab1d2d14eb3 63 return AMX_ERR_MEMORY;
tylerwilson 0:3ab1d2d14eb3 64 } /* if */
tylerwilson 0:3ab1d2d14eb3 65 didalloc = 1;
tylerwilson 0:3ab1d2d14eb3 66 /* after amx_Init(), amx->base points to the memory block */
tylerwilson 0:3ab1d2d14eb3 67 } /* if */
tylerwilson 0:3ab1d2d14eb3 68
tylerwilson 0:3ab1d2d14eb3 69 /* read in the file */
tylerwilson 0:3ab1d2d14eb3 70 rewind(fp);
tylerwilson 0:3ab1d2d14eb3 71 fread(memblock, 1, (size_t)hdr.size, fp);
tylerwilson 0:3ab1d2d14eb3 72 fclose(fp);
tylerwilson 0:3ab1d2d14eb3 73
tylerwilson 0:3ab1d2d14eb3 74 /* initialize the abstract machine */
tylerwilson 0:3ab1d2d14eb3 75 memset(amx, 0, sizeof *amx);
tylerwilson 0:3ab1d2d14eb3 76 result = amx_Init(amx, memblock);
tylerwilson 0:3ab1d2d14eb3 77
tylerwilson 0:3ab1d2d14eb3 78 /* free the memory block on error, if it was allocated here */
tylerwilson 0:3ab1d2d14eb3 79 if (result != AMX_ERR_NONE && didalloc) {
tylerwilson 0:3ab1d2d14eb3 80 free(memblock);
tylerwilson 0:3ab1d2d14eb3 81 amx->base = NULL; /* avoid a double free */
tylerwilson 0:3ab1d2d14eb3 82 } /* if */
tylerwilson 0:3ab1d2d14eb3 83
tylerwilson 0:3ab1d2d14eb3 84 return result;
tylerwilson 0:3ab1d2d14eb3 85 }
tylerwilson 0:3ab1d2d14eb3 86
tylerwilson 0:3ab1d2d14eb3 87 int AMXAPI aux_FreeProgram(AMX *amx)
tylerwilson 0:3ab1d2d14eb3 88 {
tylerwilson 0:3ab1d2d14eb3 89 if (amx->base!=NULL) {
tylerwilson 0:3ab1d2d14eb3 90 amx_Cleanup(amx);
tylerwilson 0:3ab1d2d14eb3 91 free(amx->base);
tylerwilson 0:3ab1d2d14eb3 92 memset(amx, 0, sizeof(AMX));
tylerwilson 0:3ab1d2d14eb3 93 } /* if */
tylerwilson 0:3ab1d2d14eb3 94 return AMX_ERR_NONE;
tylerwilson 0:3ab1d2d14eb3 95 }
tylerwilson 0:3ab1d2d14eb3 96
tylerwilson 0:3ab1d2d14eb3 97 char * AMXAPI aux_StrError(int errnum)
tylerwilson 0:3ab1d2d14eb3 98 {
tylerwilson 0:3ab1d2d14eb3 99 static char *messages[] = {
tylerwilson 0:3ab1d2d14eb3 100 /* AMX_ERR_NONE */ "(none)",
tylerwilson 0:3ab1d2d14eb3 101 /* AMX_ERR_EXIT */ "Forced exit",
tylerwilson 0:3ab1d2d14eb3 102 /* AMX_ERR_ASSERT */ "Assertion failed",
tylerwilson 0:3ab1d2d14eb3 103 /* AMX_ERR_STACKERR */ "Stack/heap collision (insufficient stack size)",
tylerwilson 0:3ab1d2d14eb3 104 /* AMX_ERR_BOUNDS */ "Array index out of bounds",
tylerwilson 0:3ab1d2d14eb3 105 /* AMX_ERR_MEMACCESS */ "Invalid memory access",
tylerwilson 0:3ab1d2d14eb3 106 /* AMX_ERR_INVINSTR */ "Invalid instruction",
tylerwilson 0:3ab1d2d14eb3 107 /* AMX_ERR_STACKLOW */ "Stack underflow",
tylerwilson 0:3ab1d2d14eb3 108 /* AMX_ERR_HEAPLOW */ "Heap underflow",
tylerwilson 0:3ab1d2d14eb3 109 /* AMX_ERR_CALLBACK */ "No (valid) native function callback",
tylerwilson 0:3ab1d2d14eb3 110 /* AMX_ERR_NATIVE */ "Native function failed",
tylerwilson 0:3ab1d2d14eb3 111 /* AMX_ERR_DIVIDE */ "Divide by zero",
tylerwilson 0:3ab1d2d14eb3 112 /* AMX_ERR_SLEEP */ "(sleep mode)",
tylerwilson 0:3ab1d2d14eb3 113 /* AMX_ERR_INVSTATE */ "Invalid state",
tylerwilson 0:3ab1d2d14eb3 114 /* 14 */ "(reserved)",
tylerwilson 0:3ab1d2d14eb3 115 /* 15 */ "(reserved)",
tylerwilson 0:3ab1d2d14eb3 116 /* AMX_ERR_MEMORY */ "Out of memory",
tylerwilson 0:3ab1d2d14eb3 117 /* AMX_ERR_FORMAT */ "Invalid/unsupported P-code file format",
tylerwilson 0:3ab1d2d14eb3 118 /* AMX_ERR_VERSION */ "File is for a newer version of the AMX",
tylerwilson 0:3ab1d2d14eb3 119 /* AMX_ERR_NOTFOUND */ "File or function is not found",
tylerwilson 0:3ab1d2d14eb3 120 /* AMX_ERR_INDEX */ "Invalid index parameter (bad entry point)",
tylerwilson 0:3ab1d2d14eb3 121 /* AMX_ERR_DEBUG */ "Debugger cannot run",
tylerwilson 0:3ab1d2d14eb3 122 /* AMX_ERR_INIT */ "AMX not initialized (or doubly initialized)",
tylerwilson 0:3ab1d2d14eb3 123 /* AMX_ERR_USERDATA */ "Unable to set user data field (table full)",
tylerwilson 0:3ab1d2d14eb3 124 /* AMX_ERR_INIT_JIT */ "Cannot initialize the JIT",
tylerwilson 0:3ab1d2d14eb3 125 /* AMX_ERR_PARAMS */ "Parameter error",
tylerwilson 0:3ab1d2d14eb3 126 /* AMX_ERR_DOMAIN */ "Domain error, expression result does not fit in range",
tylerwilson 0:3ab1d2d14eb3 127 /* AMX_ERR_GENERAL */ "General error (unknown or unspecific error)",
tylerwilson 0:3ab1d2d14eb3 128 /* AMX_ERR_OVERLAY */ "Overlays are unsupported (JIT) or uninitialized",
tylerwilson 0:3ab1d2d14eb3 129 };
tylerwilson 0:3ab1d2d14eb3 130 if (errnum < 0 || errnum >= sizeof messages / sizeof messages[0])
tylerwilson 0:3ab1d2d14eb3 131 return "(unknown)";
tylerwilson 0:3ab1d2d14eb3 132 return messages[errnum];
tylerwilson 0:3ab1d2d14eb3 133 }
tylerwilson 0:3ab1d2d14eb3 134
tylerwilson 0:3ab1d2d14eb3 135 int AMXAPI aux_GetSection(const AMX *amx, int section, cell **start, size_t *size)
tylerwilson 0:3ab1d2d14eb3 136 {
tylerwilson 0:3ab1d2d14eb3 137 AMX_HEADER *hdr;
tylerwilson 0:3ab1d2d14eb3 138
tylerwilson 0:3ab1d2d14eb3 139 if (amx == NULL || start == NULL || size == NULL)
tylerwilson 0:3ab1d2d14eb3 140 return AMX_ERR_PARAMS;
tylerwilson 0:3ab1d2d14eb3 141
tylerwilson 0:3ab1d2d14eb3 142 hdr = (AMX_HEADER*)amx->base;
tylerwilson 0:3ab1d2d14eb3 143 switch(section) {
tylerwilson 0:3ab1d2d14eb3 144 case CODE_SECTION:
tylerwilson 0:3ab1d2d14eb3 145 *start = (cell *)(amx->base + hdr->cod);
tylerwilson 0:3ab1d2d14eb3 146 *size = hdr->dat - hdr->cod;
tylerwilson 0:3ab1d2d14eb3 147 break;
tylerwilson 0:3ab1d2d14eb3 148 case DATA_SECTION:
tylerwilson 0:3ab1d2d14eb3 149 *start = (cell *)(amx->data);
tylerwilson 0:3ab1d2d14eb3 150 *size = hdr->hea - hdr->dat;
tylerwilson 0:3ab1d2d14eb3 151 break;
tylerwilson 0:3ab1d2d14eb3 152 case HEAP_SECTION:
tylerwilson 0:3ab1d2d14eb3 153 *start = (cell *)(amx->data + hdr->hea);
tylerwilson 0:3ab1d2d14eb3 154 *size = amx->hea - hdr->hea;
tylerwilson 0:3ab1d2d14eb3 155 break;
tylerwilson 0:3ab1d2d14eb3 156 case STACK_SECTION:
tylerwilson 0:3ab1d2d14eb3 157 *start = (cell *)(amx->data + amx->stk);
tylerwilson 0:3ab1d2d14eb3 158 *size = amx->stp - amx->stk;
tylerwilson 0:3ab1d2d14eb3 159 break;
tylerwilson 0:3ab1d2d14eb3 160 default:
tylerwilson 0:3ab1d2d14eb3 161 return AMX_ERR_PARAMS;
tylerwilson 0:3ab1d2d14eb3 162 } /* switch */
tylerwilson 0:3ab1d2d14eb3 163 return AMX_ERR_NONE;
tylerwilson 0:3ab1d2d14eb3 164 }