├── obj └── .dummy ├── out └── .dummy ├── src ├── FSIO.c ├── FSIO.h ├── FSDefs.h ├── SD-SPI.h ├── asINT.h ├── lapi.h ├── asLUA_PIC.h ├── asDBG.h ├── asDBG.c ├── asIO.h ├── lstring.h ├── Compiler.h ├── linit.c ├── lundump.h ├── asUART1.h ├── ldebug.h ├── ltm.h ├── lfunc.h ├── lvm.h ├── asCARD.h ├── ltable.h ├── lualib.h ├── asCON.h ├── lmem.h ├── lzio.h ├── asINT.c ├── ltm.c ├── lzio.c ├── ldo.h ├── llex.h ├── lmem.c ├── lparser.h ├── llimits.h ├── lcode.h ├── lopcodes.c ├── lstring.c ├── lgc.h ├── ldump.c ├── asCARD.c ├── lfunc.c ├── lundump.c ├── lstate.h ├── print.c ├── HardwareProfile.h ├── lauxlib.h ├── asCON.c ├── lstate.c ├── lobject.c ├── lmathlib.c ├── loslib.c ├── asPIC.h ├── asUART1.c ├── FSconfig.h ├── ltablib.c ├── asIO.c └── lopcodes.h ├── chipKIT-Max32-tools ├── bin │ ├── avrdude.exe │ └── libusb0.dll ├── readme.txt ├── ProgFileForMPIDE │ ├── readme.txt │ └── asProgPIC32Lua.bat └── asProgPIC32Lua.bat ├── .gitignore ├── appslua ├── boot32.lua ├── v04a_tests.lua ├── fibonacci.lua ├── v02b_tests.lua ├── v02a_tests.lua ├── v01b_tests.lua └── v01a_tests.lua ├── README.md ├── PIC32Lua.mcp └── PIC32Lua-ChipkitMAX32.mcp /obj/.dummy: -------------------------------------------------------------------------------- 1 | a placeholder 2 | -------------------------------------------------------------------------------- /out/.dummy: -------------------------------------------------------------------------------- 1 | a placeholder 2 | -------------------------------------------------------------------------------- /src/FSIO.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FMMT666/PIC32Lua/HEAD/src/FSIO.c -------------------------------------------------------------------------------- /src/FSIO.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FMMT666/PIC32Lua/HEAD/src/FSIO.h -------------------------------------------------------------------------------- /src/FSDefs.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FMMT666/PIC32Lua/HEAD/src/FSDefs.h -------------------------------------------------------------------------------- /src/SD-SPI.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FMMT666/PIC32Lua/HEAD/src/SD-SPI.h -------------------------------------------------------------------------------- /src/asINT.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __asINT 3 | 4 | 5 | #else 6 | 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /chipKIT-Max32-tools/bin/avrdude.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FMMT666/PIC32Lua/HEAD/chipKIT-Max32-tools/bin/avrdude.exe -------------------------------------------------------------------------------- /chipKIT-Max32-tools/bin/libusb0.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FMMT666/PIC32Lua/HEAD/chipKIT-Max32-tools/bin/libusb0.dll -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | PIC32Lua.mcs 2 | PIC32Lua.mcw 3 | PIC32Lua.mptags 4 | PIC32Lua.tagsrc 5 | PIC32Lua-ChipkitMAX32.mcs 6 | PIC32Lua-ChipkitMAX32.mcw 7 | PIC32Lua-ChipkitMAX32.mptags 8 | PIC32Lua-ChipkitMAX32.tagsrc 9 | *.nogit.* 10 | -------------------------------------------------------------------------------- /src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /chipKIT-Max32-tools/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | Minimal PIC32Lua-chipKIT-Max32 programming helper. 3 | 4 | ONLY FOR CHIPKIT-MAX32! 5 | 6 | 7 | USAGE: 8 | asProgPIC32Lua 9 | 10 | Where "COMPORT" is the name of your "chipKIT-Max32" FTDI interface. 11 | E.g. COM1, COM2, COM6... 12 | 13 | 14 | ASkr 3/2013 15 | www.askrprojects.net 16 | -------------------------------------------------------------------------------- /src/asLUA_PIC.h: -------------------------------------------------------------------------------- 1 | 2 | //-------------------------------------------------------------------------------------- 3 | // DEFINE PIC32 PORT NAME VISIBILITY 4 | // If defined, the PIC's ports can be accessed by 5 | // "names" instead of numbers: 6 | // Instead of 7 | // pic.PortPinsHigh(0,7) 8 | // you now can use 9 | // pic.PortPinsHigh(pic.port.A,7) 10 | #define DEFINE_PORT_NAMES 11 | 12 | -------------------------------------------------------------------------------- /appslua/boot32.lua: -------------------------------------------------------------------------------- 1 | -- example BOOT32.LUA file 2 | 3 | print("- start OF BOOT32.LUA file") 4 | 5 | function hidden() 6 | local i 7 | 8 | print("- inside hidden() function in BOOT32.LUA file") 9 | 10 | for i=1,3 do 11 | print(" blabla "..i) 12 | end 13 | 14 | end 15 | 16 | function BOOT32() 17 | print("- inside BOOT32() function in BOOT32.LUA file") 18 | hidden() 19 | end 20 | 21 | print("- end of BOOT32.LUA file") 22 | -------------------------------------------------------------------------------- /chipKIT-Max32-tools/ProgFileForMPIDE/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | Minimal PIC32Lua-chipKIT-Max32 programming helper 3 | 4 | Move this batch file and the "PIC32Lua-ChipkitMAX32.hex" file to 5 | your "MPide" root directory. 6 | 7 | 8 | USAGE: 9 | asProgPIC32Lua 10 | 11 | Where "COMPORT" is the name of your "chipKIT-Max32" FTDI interface. 12 | E.g. COM1, COM2, COM6... 13 | 14 | 15 | ASkr 11/2011, 3/2013 16 | www.askrprojects.net 17 | -------------------------------------------------------------------------------- /appslua/v04a_tests.lua: -------------------------------------------------------------------------------- 1 | 2 | -- Just a stupid "SOS" LED toggle for the 3 | -- chipKIT-Max32. 4 | function cksos() 5 | local i,n 6 | local stim 7 | 8 | pic.PortDir(pic.port.A,0b1000,0b1000) 9 | pic.PortState(pic.port.A,0,0b1000) 10 | 11 | n = 0.15 12 | stim=pic.TimerRead() 13 | 14 | while(1) do 15 | 16 | for i=1,6 do 17 | 18 | stim=stim+n/pic.TIMERTICK 19 | pic.TimerMatch(stim) 20 | 21 | pic.PortPinsInv(pic.port.A,0b1000) 22 | 23 | end 24 | 25 | if n==0.15 then 26 | n=0.3 27 | else 28 | n=0.15 29 | end 30 | 31 | if pic.ConsoleReadChar() == 27 then 32 | break 33 | end 34 | 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /src/asDBG.h: -------------------------------------------------------------------------------- 1 | 2 | // initialises some debug pins 3 | //#define PIC32_DEBUG 4 | 5 | 6 | #ifndef __asDBG 7 | 8 | extern volatile unsigned char pPin1; 9 | extern volatile unsigned char pPin2; 10 | 11 | #define asDBG_Pin1Init() (TRISDbits.TRISD0=0) 12 | #define asDBG_Pin1(s) (LATDbits.LATD0=(s)) 13 | #define asDBG_Pin1Tog() (LATDbits.LATD0=((pPin1=!pPin1))) 14 | 15 | #define asDBG_Pin2Init() (TRISDbits.TRISD1=0) 16 | #define asDBG_Pin2(s) (LATDbits.LATD1=(s)) 17 | #define asDBG_Pin2Tog() (LATDbits.LATD1=((pPin2=!pPin2))) 18 | 19 | extern void asDBG_DumpReg(unsigned int reg); 20 | 21 | 22 | #else 23 | 24 | volatile unsigned char pPin1=0; 25 | volatile unsigned char pPin2=0; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /appslua/fibonacci.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- The obligatory speed test... 3 | -- Ok, somehow ridiculous ;-) 4 | -- 5 | 6 | 7 | -- fastes Fibonacci calculation in Lua 8 | function fib2(n) 9 | local a,b = 0,1 10 | while n > 0 do 11 | a,b = b,a+b 12 | n=n-1 13 | end 14 | return a 15 | end 16 | 17 | -- 300 Fibonacci calculations in ~1s 18 | -- (without recursion, of course ;-) 19 | function gofib(n) 20 | local stim 21 | local i,j 22 | 23 | if n==nil then 24 | n=300 25 | end 26 | 27 | stim=pic.TimerRead() 28 | for i=1,n do 29 | io.write(" "..fib2(i)) 30 | -- fib2(i) 31 | end 32 | stim=pic.TimerRead()-stim 33 | io.flush() 34 | print() print() 35 | print("CALCULATION TOOK "..stim*pic.TIMERTICK.."s FOR "..n.." NUMBERS") 36 | end 37 | 38 | -------------------------------------------------------------------------------- /src/asDBG.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #ifndef __asDBG 8 | #define __asDBG 9 | #endif 10 | 11 | 12 | #include "asPIC.h" 13 | 14 | #include "asDBG.h" 15 | #include "asCON.h" 16 | 17 | 18 | // ADDITIONAL STUFF (MACROS) IN THE HEADER FILE! 19 | 20 | 21 | #ifdef PIC32_DEBUG 22 | 23 | 24 | 25 | //************************************************************************************** 26 | //*** asDBG_DumpReg 27 | //*** 28 | //*** Dumps the contents of (to wherever) 29 | //************************************************************************************** 30 | void asDBG_DumpReg(unsigned int reg) 31 | { 32 | char tmp[11]; 33 | 34 | sprintf((char *)&tmp,"%08x\n",reg); 35 | asCON_SerialWriteString((char *)&tmp); 36 | } 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | #endif // PIC32DEBUG 51 | 52 | -------------------------------------------------------------------------------- /src/asIO.h: -------------------------------------------------------------------------------- 1 | #include "asPIC.h" 2 | 3 | #ifndef __asIO 4 | 5 | #ifdef CONSOLE_SWAP 6 | extern void _mon_putc(char ch); 7 | #endif 8 | 9 | extern int ffferror(FILE *fin); 10 | extern int ffflush (FILE *fin); 11 | extern char *fgets (char *, int , FILE *); 12 | extern int fgetc (FILE *fin); 13 | extern int fclose (FILE *fin); 14 | extern FILE *fopen (const char *a, const char *b); 15 | 16 | extern size_t fread (void *a, size_t b, size_t c, FILE *fin); 17 | 18 | extern FILE *open (const char *a, const char *b); 19 | extern int close (FILE *fin); 20 | extern int lseek (FILE *fin, long int a, int b); 21 | extern long int ftell (FILE *fin); 22 | extern size_t read (void *a, size_t b, size_t c, FILE *fin); 23 | extern int write (void *fin, char *dat); 24 | extern int remove (const char *fnam); 25 | extern void unlink(); 26 | extern void link(); 27 | extern int gettimeofday (struct timeval *a, void *b); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | 11 | #include "lgc.h" 12 | #include "lobject.h" 13 | #include "lstate.h" 14 | 15 | 16 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 17 | 18 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 19 | 20 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 25 | 26 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 27 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 28 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/Compiler.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __COMPILER_H 3 | #define __COMPILER_H 4 | 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | #define PTR_BASE unsigned long 15 | #define ROM_PTR_BASE unsigned long 16 | 17 | 18 | #define memcmppgm2ram(a,b,c) memcmp(a,b,c) 19 | #define strcmppgm2ram(a,b) strcmp(a,b) 20 | #define memcpypgm2ram(a,b,c) memcpy(a,b,c) 21 | #define strcpypgm2ram(a,b) strcpy(a,b) 22 | #define strncpypgm2ram(a,b,c) strncpy(a,b,c) 23 | #define strstrrampgm(a,b) strstr(a,b) 24 | #define strlenpgm(a) strlen(a) 25 | #define strchrpgm(a,b) strchr(a,b) 26 | #define strcatpgm2ram(a,b) strcat(a,b) 27 | 28 | 29 | #define ROM const 30 | 31 | 32 | #define persistent 33 | #define far 34 | #define FAR 35 | #define Reset() SoftReset() 36 | #define ClrWdt() (WDTCONSET = _WDTCON_WDTCLR_MASK) 37 | 38 | #if !defined(Nop) 39 | #define Nop() asm("nop") 40 | #endif 41 | 42 | 43 | #endif //__COMPILER_H 44 | -------------------------------------------------------------------------------- /src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Initialization of libraries for lua.c 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | #include "lua.h" 12 | 13 | #include "lualib.h" 14 | #include "lauxlib.h" 15 | 16 | 17 | // ASkr 18 | static const luaL_Reg lualibs[] = { 19 | {"", luaopen_base}, 20 | // {LUA_LOADLIBNAME, luaopen_package}, 21 | {LUA_TABLIBNAME, luaopen_table}, 22 | {LUA_IOLIBNAME, luaopen_io}, 23 | // {LUA_OSLIBNAME, luaopen_os}, 24 | {LUA_STRLIBNAME, luaopen_string}, 25 | {LUA_MATHLIBNAME, luaopen_math}, 26 | // {LUA_DBLIBNAME, luaopen_debug}, 27 | {LUA_PIC_LIBNAME, luaopen_pic}, 28 | {NULL, NULL} 29 | }; 30 | 31 | 32 | 33 | 34 | 35 | LUALIB_API void luaL_openlibs (lua_State *L) { 36 | const luaL_Reg *lib = lualibs; 37 | for (; lib->func; lib++) { 38 | lua_pushcfunction(L, lib->func); 39 | lua_pushstring(L, lib->name); 40 | lua_call(L, 1, 0); 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (char* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | #ifdef luac_c 23 | /* print one chunk; from print.c */ 24 | LUAI_FUNC void luaU_print (const Proto* f, int full); 25 | #endif 26 | 27 | /* for header of binary files -- this is Lua 5.1 */ 28 | #define LUAC_VERSION 0x51 29 | 30 | /* for header of binary files -- this is the official format */ 31 | #define LUAC_FORMAT 0 32 | 33 | /* size of header of binary files */ 34 | #define LUAC_HEADERSIZE 12 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /chipKIT-Max32-tools/asProgPIC32Lua.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | rem 3 | rem Minimal PIC32Lua programming helper 4 | rem 5 | rem USAGE: 6 | rem asProgPIC32Lua 7 | rem 8 | rem Where "COMPORT" is the name of your "chipKIT-Max32" FTDI interface. 9 | rem E.g. COM1, COM2, COM6... 10 | rem 11 | rem 12 | rem ASkr 11/2011 13 | rem 14 | rem 15 | rem CHANGES 3/2013: 16 | rem - avrdude now included, batch file can be run from in here 17 | rem 18 | 19 | 20 | set PIC32LUAHEXFILE=..\out\PIC32Lua-ChipkitMAX32.hex 21 | set AVRDUDE=bin\avrdude.exe 22 | set AVRDUDECFG=bin\avrdude.conf 23 | 24 | 25 | rem --------------------------------------------------------------------------------- 26 | if "%1" == "" goto USAGE 27 | 28 | echo PROGRAMMING %PIC32LUAHEXFILE% 29 | 30 | %AVRDUDE% -C %AVRDUDECFG% -c stk500v2 -P %1 -p pic32 -b 115200 -U flash:w:%PIC32LUAHEXFILE%:i -v 31 | 32 | goto QUIT 33 | 34 | :USAGE 35 | echo --- 36 | echo USAGE: 37 | echo asProgPIC32Lua COMPORT 38 | echo where COMPORT is COM1, COM2, COM6... 39 | echo --- 40 | 41 | :QUIT 42 | -------------------------------------------------------------------------------- /chipKIT-Max32-tools/ProgFileForMPIDE/asProgPIC32Lua.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | rem 3 | rem Minimal PIC32Lua programming helper 4 | rem 5 | rem Move this batch file and the "PIC32Lua-ChipkitMAX32.hex" file to 6 | rem your "MPide" root directory. 7 | rem 8 | rem 9 | rem USAGE: 10 | rem asProgPIC32Lua 11 | rem 12 | rem Where "COMPORT" is the name of your "chipKIT-Max32" FTDI interface. 13 | rem E.g. COM1, COM2, COM6... 14 | rem 15 | rem 16 | rem ASkr 11/2011 17 | rem 18 | 19 | 20 | set PIC32LUAHEXFILE=PIC32Lua-ChipkitMAX32.hex 21 | set AVRDUDE=hardware\tools\avr\bin\avrdude.exe 22 | set AVRDUDECFG=hardware\tools\avr\etc\avrdude.conf 23 | 24 | 25 | rem --------------------------------------------------------------------------------- 26 | if "%1" == "" goto USAGE 27 | 28 | echo PROGRAMMING %PIC32LUAHEXFILE% 29 | 30 | %AVRDUDE% -C %AVRDUDECFG% -c stk500v2 -P %1 -p pic32 -b 115200 -U flash:w:%PIC32LUAHEXFILE%:i -v 31 | 32 | goto QUIT 33 | 34 | :USAGE 35 | echo --- 36 | echo USAGE: 37 | echo asProgPIC32Lua COMPORT 38 | echo where COMPORT is COM1, COM2, COM6... 39 | echo --- 40 | 41 | :QUIT 42 | -------------------------------------------------------------------------------- /src/asUART1.h: -------------------------------------------------------------------------------- 1 | 2 | // length of the UART1 receive buffer (in bytes/characters) 3 | #define UART1_RXBUFSIZE 256 4 | 5 | 6 | #ifndef __asUART1 7 | 8 | extern void asUART1_Enable(unsigned baud); 9 | extern void asUART1_Disable(void); 10 | extern void asUART1_Baud(unsigned baud); 11 | extern inline void asUART1_RXBufInit(void); 12 | extern int asUART1_RXBufWriteChar(char ch); 13 | extern inline int asUART1_RXBufReadChar(int blocked); 14 | extern inline void asUART1_RXBufFlushIn(void); 15 | extern inline unsigned asUART1_RXBufCount(void); 16 | extern inline void asUART1_FlushOut(void); 17 | extern void asUART1_WriteString(char *ch); 18 | extern inline void asUART1_WriteChar(char ch); 19 | 20 | 21 | #else 22 | 23 | void asUART1_Baud(unsigned baud); 24 | inline void asUART1_RXBufInit(void); 25 | int asUART1_RXBufWriteChar(char ch); 26 | inline int asUART1_RXBufReadChar(int blocked); 27 | inline void asUART1_RXBufFlushIn(void); 28 | inline unsigned asUART1_RXBufCount(void); 29 | inline void asUART1_FlushOut(void); 30 | void asUART1_WriteString(char *ch); 31 | inline void asUART1_WriteChar(char ch); 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 24 | LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 25 | const TValue *p2); 26 | LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 29 | LUAI_FUNC void luaG_errormsg (lua_State *L); 30 | LUAI_FUNC int luaG_checkcode (const Proto *pt); 31 | LUAI_FUNC int luaG_checkopenop (Instruction i); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_EQ, /* last tag method with `fast' access */ 24 | TM_ADD, 25 | TM_SUB, 26 | TM_MUL, 27 | TM_DIV, 28 | TM_MOD, 29 | TM_POW, 30 | TM_UNM, 31 | TM_LEN, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | LUAI_DATA const char *const luaT_typenames[]; 47 | 48 | 49 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 50 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 51 | TMS event); 52 | LUAI_FUNC void luaT_init (lua_State *L); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 19 | (((o) = luaV_tonumber(o,n)) != NULL)) 20 | 21 | #define equalobj(L,o1,o2) \ 22 | (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 23 | 24 | 25 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 26 | LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 27 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 28 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 29 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 30 | StkId val); 31 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 32 | StkId val); 33 | LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); 34 | LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/asCARD.h: -------------------------------------------------------------------------------- 1 | 2 | //-------------------------------------------------------------------------------------- 3 | // DEFINE SD-CARD PRESENCE AND BEHAVIOUR 4 | // Undefine to exclude Lua SD-card bindings from PIC32Lua or 5 | // to skip booting the file "BOOT32.LUA" 6 | #define USE_SDCARD_CODE 7 | #define USE_SDCARD_BOOTFILE 8 | 9 | 10 | 11 | 12 | 13 | 14 | //-------------------------------------------------------------------------------------- 15 | //-------------------------------------------------------------------------------------- 16 | //-------------------------------------------------------------------------------------- 17 | // no changes below this line 18 | 19 | #ifdef USE_SDCARD_CODE 20 | 21 | #define CARD_DETECTED 1 22 | #define CARD_MOUNTED 2 23 | 24 | #define CARD_BOOT_ERR_NOCODE 1 25 | #define CARD_BOOT_ERR_NOBOOT 2 26 | #define CARD_BOOT_ERR_NOMOUNT 4 27 | #define CARD_BOOT_ERR_NOFILE 8 28 | #define CARD_BOOT_ERR_NORUN 16 29 | #define CARD_BOOT_ERR_NOFCT 32 30 | 31 | 32 | extern unsigned gCardState; 33 | extern SearchRec CardRec; 34 | extern int asCARD_Detect(void); 35 | extern int asCARD_Mount(void); 36 | extern int asCARD_FindFirst(char *fname, unsigned int); 37 | extern int asCARD_FindNext(void); 38 | 39 | extern int asCARD_Boot(lua_State *L); 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.nk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define key2tval(n) (&(n)->i_key.tvk) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 22 | LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 25 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 28 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 29 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 30 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 31 | LUAI_FUNC int luaH_getn (Table *t); 32 | 33 | 34 | #if defined(LUA_DEBUG) 35 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 36 | LUAI_FUNC int luaH_isdummy (Node *n); 37 | #endif 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* Key to file-handle type */ 15 | #define LUA_FILEHANDLE "FILE*" 16 | 17 | 18 | #define LUA_COLIBNAME "coroutine" 19 | LUALIB_API int (luaopen_base) (lua_State *L); 20 | 21 | #define LUA_TABLIBNAME "table" 22 | LUALIB_API int (luaopen_table) (lua_State *L); 23 | 24 | #define LUA_IOLIBNAME "io" 25 | LUALIB_API int (luaopen_io) (lua_State *L); 26 | 27 | #define LUA_OSLIBNAME "os" 28 | LUALIB_API int (luaopen_os) (lua_State *L); 29 | 30 | #define LUA_STRLIBNAME "string" 31 | LUALIB_API int (luaopen_string) (lua_State *L); 32 | 33 | #define LUA_MATHLIBNAME "math" 34 | LUALIB_API int (luaopen_math) (lua_State *L); 35 | 36 | #define LUA_DBLIBNAME "debug" 37 | LUALIB_API int (luaopen_debug) (lua_State *L); 38 | 39 | #define LUA_LOADLIBNAME "package" 40 | LUALIB_API int (luaopen_package) (lua_State *L); 41 | 42 | 43 | 44 | // ASkr 45 | #define LUA_PIC_LIBNAME "pic" 46 | LUALIB_API int (luaopen_pic) (lua_State *L); 47 | 48 | 49 | 50 | /* open all previous libraries */ 51 | LUALIB_API void (luaL_openlibs) (lua_State *L); 52 | 53 | 54 | 55 | #ifndef lua_assert 56 | #define lua_assert(x) ((void)0) 57 | #endif 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/asCON.h: -------------------------------------------------------------------------------- 1 | 2 | // length of the UART2 receive buffer (in bytes/characters) 3 | #define RXBUFSIZE 1024 4 | 5 | 6 | // undef if backspace should not be used 7 | #define USE_DIRTY_BACKSPACE 8 | 9 | #ifdef USE_DIRTY_BACKSPACE 10 | #define KEY_BACKSPACE 127 // ascii code: backspace 11 | #endif 12 | 13 | 14 | //-------------------------------------------------------------------------------------- 15 | //-------------------------------------------------------------------------------------- 16 | //-------------------------------------------------------------------------------------- 17 | // no changes below this line 18 | 19 | #define NONBLOCKING 0 20 | #define BLOCKING 1 21 | #define RXBUFEMPTY -1 22 | #define RXOVERFLOW -2 23 | 24 | 25 | #ifndef __asCON 26 | 27 | extern inline void asCON_RXBufFlushIn(void); 28 | extern inline unsigned asCON_RXBufCount(void); 29 | extern inline void asCON_RXBufInit(void); 30 | extern int asCON_RXBufWriteChar(char ch); 31 | extern inline char asCON_RXBufReadChar(int blocked); 32 | extern void asCON_SerialWriteString(char *ch); 33 | extern inline void asCON_SerialFlush(); 34 | extern inline void asCON_SerialWriteChar(char ch); 35 | extern unsigned int asCON_SerialReadLine(unsigned int maxdat,char *buffer, unsigned int uart_data_wait); 36 | 37 | 38 | #else 39 | 40 | void asCON_SerialWriteString(char *ch); 41 | inline void asCON_SerialFlush(); 42 | inline void asCON_SerialWriteChar(char ch); 43 | unsigned int asCON_SerialReadLine(unsigned int maxdat,char *buffer, unsigned int uart_data_wait); 44 | 45 | #endif 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | #define luaM_reallocv(L,b,on,n,e) \ 20 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 21 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 22 | luaM_toobig(L)) 23 | 24 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 25 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 27 | 28 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 29 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 30 | #define luaM_newvector(L,n,t) \ 31 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | 41 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 42 | size_t size); 43 | LUAI_FUNC void *luaM_toobig (lua_State *L); 44 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 45 | size_t size_elem, int limit, 46 | const char *errormsg); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define char2int(c) cast(int, cast(unsigned char, (c))) 21 | 22 | #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 23 | 24 | typedef struct Mbuffer { 25 | char *buffer; 26 | size_t n; 27 | size_t buffsize; 28 | } Mbuffer; 29 | 30 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 31 | 32 | #define luaZ_buffer(buff) ((buff)->buffer) 33 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 34 | #define luaZ_bufflen(buff) ((buff)->n) 35 | 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 41 | (buff)->buffsize = size) 42 | 43 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 44 | 45 | 46 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 50 | LUAI_FUNC int luaZ_lookahead (ZIO *z); 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; 60 | void* data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /src/asINT.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | 11 | #ifndef __asINT 12 | #define __asINT 13 | #endif 14 | 15 | #include "asPIC.h" 16 | 17 | #include "asIO.h" 18 | #include "asINT.h" 19 | #include "asDBG.h" 20 | #include "asCON.h" 21 | #include "asUART1.h" 22 | 23 | 24 | // ASkr TODO priorities 25 | 26 | 27 | 28 | 29 | //************************************************************************************** 30 | //*** UART1 RX INTERRUPT 31 | //*** 32 | //*** 33 | //************************************************************************************** 34 | void __ISR(_UART_1_VECTOR, ipl3) _U1RXInterrupt(void) 35 | { 36 | if(mU1RXGetIntFlag()) 37 | { 38 | while(U1STAbits.URXDA) 39 | 40 | #ifndef CONSOLE_SWAP 41 | asUART1_RXBufWriteChar(U1RXREG & 0xFF); 42 | #else 43 | asCON_RXBufWriteChar(U1RXREG & 0xFF); 44 | #endif 45 | 46 | mU1RXClearIntFlag(); 47 | } 48 | } 49 | //************************************************************************************** 50 | //*** UART2 RX INTERRUPT 51 | //*** 52 | //*** 53 | //************************************************************************************** 54 | void __ISR(_UART_2_VECTOR, ipl4) _U2RXInterrupt(void) 55 | { 56 | if(mU2RXGetIntFlag()) 57 | { 58 | while(U2STAbits.URXDA) 59 | 60 | #ifndef CONSOLE_SWAP 61 | asCON_RXBufWriteChar(U2RXREG & 0xFF); 62 | #else 63 | asUART1_RXBufWriteChar(U2RXREG & 0xFF); 64 | #endif 65 | 66 | mU2RXClearIntFlag(); 67 | } 68 | } 69 | 70 | 71 | 72 | //************************************************************************************** 73 | //*** TIMER 4/5 INTERRUPT 74 | //*** 75 | //*** 76 | //************************************************************************************** 77 | 78 | // stopped working with newer C32 dist (V2.02) 79 | void __ISR(_TIMER_5_VECTOR, ipl5) _T5Interrupt(void) 80 | { 81 | gTimerHigh++; 82 | mT5ClearIntFlag(); 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | 23 | const char *const luaT_typenames[] = { 24 | "nil", "boolean", "userdata", "number", 25 | "string", "table", "function", "userdata", "thread", 26 | "proto", "upval" 27 | }; 28 | 29 | 30 | void luaT_init (lua_State *L) { 31 | static const char *const luaT_eventname[] = { /* ORDER TM */ 32 | "__index", "__newindex", 33 | "__gc", "__mode", "__eq", 34 | "__add", "__sub", "__mul", "__div", "__mod", 35 | "__pow", "__unm", "__len", "__lt", "__le", 36 | "__concat", "__call" 37 | }; 38 | int i; 39 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 41 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 42 | } 43 | } 44 | 45 | 46 | /* 47 | ** function to be used with macro "fasttm": optimized for absence of 48 | ** tag methods 49 | */ 50 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 51 | const TValue *tm = luaH_getstr(events, ename); 52 | lua_assert(event <= TM_EQ); 53 | if (ttisnil(tm)) { /* no tag method? */ 54 | events->flags |= cast_byte(1u<metatable; 66 | break; 67 | case LUA_TUSERDATA: 68 | mt = uvalue(o)->metatable; 69 | break; 70 | default: 71 | mt = G(L)->mt[ttype(o)]; 72 | } 73 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 74 | } 75 | 76 | -------------------------------------------------------------------------------- /src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** a generic input stream interface 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) return EOZ; 29 | z->n = size - 1; 30 | z->p = buff; 31 | return char2int(*(z->p++)); 32 | } 33 | 34 | 35 | int luaZ_lookahead (ZIO *z) { 36 | if (z->n == 0) { 37 | if (luaZ_fill(z) == EOZ) 38 | return EOZ; 39 | else { 40 | z->n++; /* luaZ_fill removed first byte; put back it */ 41 | z->p--; 42 | } 43 | } 44 | return char2int(*z->p); 45 | } 46 | 47 | 48 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 49 | z->L = L; 50 | z->reader = reader; 51 | z->data = data; 52 | z->n = 0; 53 | z->p = NULL; 54 | } 55 | 56 | 57 | /* --------------------------------------------------------------- read --- */ 58 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 59 | while (n) { 60 | size_t m; 61 | if (luaZ_lookahead(z) == EOZ) 62 | return n; /* return number of missing bytes */ 63 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 64 | memcpy(b, z->p, m); 65 | z->n -= m; 66 | z->p += m; 67 | b = (char *)b + m; 68 | n -= m; 69 | } 70 | return 0; 71 | } 72 | 73 | /* ------------------------------------------------------------------------ */ 74 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 75 | if (n > buff->buffsize) { 76 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 77 | luaZ_resizebuffer(L, buff, n); 78 | } 79 | return buff->buffer; 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) \ 17 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 18 | luaD_growstack(L, n); \ 19 | else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 20 | 21 | 22 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 23 | 24 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 25 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 26 | 27 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 28 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 29 | 30 | 31 | /* results from luaD_precall */ 32 | #define PCRLUA 0 /* initiated a call to a Lua function */ 33 | #define PCRC 1 /* did a call to a C function */ 34 | #define PCRYIELD 2 /* C funtion yielded */ 35 | 36 | 37 | /* type of protected functions, to be ran by `runprotected' */ 38 | typedef void (*Pfunc) (lua_State *L, void *ud); 39 | 40 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 41 | LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 45 | ptrdiff_t oldtop, ptrdiff_t ef); 46 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 47 | LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 48 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 49 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 50 | 51 | LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 52 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 53 | 54 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /appslua/v02b_tests.lua: -------------------------------------------------------------------------------- 1 | 2 | -- AD converter; read values from AN0 (B0) 3 | -- Use precise 200ms timing via TimerMatch() 4 | -- ESC from console quits 5 | function ad() 6 | local i 7 | local stim 8 | 9 | -- pin to input 10 | pic.PortDir(pic.port.B,0,0b1) 11 | -- pin to analog 12 | pic.ADPins(0b1) 13 | -- select Vdd and Vss as reference 14 | pic.ADRef(0b000) 15 | -- select AN0 as input for MUX A+ 16 | -- 2nd '0' (MUX B+) has no effect 17 | pic.ADMux(0,0) 18 | 19 | stim=pic.TimerRead() 20 | while 1 do 21 | stim=stim+0.2/pic.TIMERTICK 22 | pic.TimerMatch(stim) 23 | i=pic.ADRead() 24 | 25 | -- separate print() to eliminate jitter 26 | -- (not really required ;-) 27 | print(i) 28 | 29 | if pic.ConsoleReadChar() == 27 then 30 | break 31 | end 32 | end -- while 33 | end 34 | 35 | 36 | -- "Multi AD" on AN0-3 37 | -- Write RAW values to SD-card. 38 | -- Precise 250ms timing via TimerMatch() 39 | -- ESC from console wuits 40 | -- All in one function; no Lua tricks; only a demonstration... 41 | function mad() 42 | local ad={} 43 | local i 44 | local stim 45 | local fout 46 | 47 | if pic.CardMount() == 0 then 48 | print("unable to mount card") 49 | return -1 50 | end 51 | 52 | fout=io.open("mess.txt","w") 53 | 54 | if fout == nil then 55 | print("unable to open file") 56 | return -2 57 | end 58 | 59 | -- pins to input 60 | pic.PortDir(pic.port.B,0,0b1) 61 | -- pins to analog 62 | pic.ADPins(0b1) 63 | -- select Vdd and Vss as reference 64 | pic.ADRef(0b000) 65 | 66 | stim=pic.TimerRead() 67 | while 1 do 68 | local stmp 69 | 70 | -- wait until time matches n*250ms 71 | stim=stim+0.25/pic.TIMERTICK 72 | pic.TimerMatch(stim) 73 | 74 | -- read all four channels 75 | -- from 1 to avoid hashed tables (index 0) 76 | for i=1,4 do 77 | pic.ADMux(i-1,0) 78 | ad[i]=pic.ADRead() 79 | end 80 | 81 | -- create output string 82 | stmp="" 83 | for i=1,4 do 84 | stmp=stmp..string.format("%4d ",ad[i]) 85 | end 86 | stmp=stmp.."\r\n" 87 | 88 | -- write to SD-card 89 | fout:write(stmp) 90 | 91 | -- write to console 92 | io.write(stmp) 93 | 94 | -- quit? 95 | if pic.ConsoleReadChar() == 27 then 96 | break 97 | end 98 | 99 | end -- while 100 | 101 | fout:close() 102 | 103 | return 0 104 | 105 | end 106 | 107 | -------------------------------------------------------------------------------- /src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | /* maximum length of a reserved word */ 17 | #define TOKEN_LEN (sizeof("function")/sizeof(char)) 18 | 19 | 20 | /* 21 | * WARNING: if you change the order of this enumeration, 22 | * grep "ORDER RESERVED" 23 | */ 24 | enum RESERVED { 25 | /* terminal symbols denoted by reserved words */ 26 | TK_AND = FIRST_RESERVED, TK_BREAK, 27 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 28 | TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 29 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 30 | /* other terminal symbols */ 31 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 32 | TK_NAME, TK_STRING, TK_EOS 33 | }; 34 | 35 | /* number of reserved words */ 36 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 37 | 38 | 39 | /* array with token `names' */ 40 | LUAI_DATA const char *const luaX_tokens []; 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | TString *ts; 46 | } SemInfo; /* semantics information */ 47 | 48 | 49 | typedef struct Token { 50 | int token; 51 | SemInfo seminfo; 52 | } Token; 53 | 54 | 55 | typedef struct LexState { 56 | int current; /* current character (charint) */ 57 | int linenumber; /* input line counter */ 58 | int lastline; /* line of last token `consumed' */ 59 | Token t; /* current token */ 60 | Token lookahead; /* look ahead token */ 61 | struct FuncState *fs; /* `FuncState' is private to the parser */ 62 | struct lua_State *L; 63 | ZIO *z; /* input stream */ 64 | Mbuffer *buff; /* buffer for tokens */ 65 | TString *source; /* current source name */ 66 | char decpoint; /* locale decimal point */ 67 | } LexState; 68 | 69 | 70 | LUAI_FUNC void luaX_init (lua_State *L); 71 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 72 | TString *source); 73 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 74 | LUAI_FUNC void luaX_next (LexState *ls); 75 | LUAI_FUNC void luaX_lookahead (LexState *ls); 76 | LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 77 | LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 78 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 79 | 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | /* 24 | ** About the realloc function: 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26 | ** (`osize' is the old size, `nsize' is the new size) 27 | ** 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). 29 | ** 30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL). 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *errormsg) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, errormsg); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | void *luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | return NULL; /* to avoid warnings */ 69 | } 70 | 71 | 72 | 73 | /* 74 | ** generic allocation routine. 75 | */ 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 77 | global_State *g = G(L); 78 | lua_assert((osize == 0) == (block == NULL)); 79 | block = (*g->frealloc)(g->ud, block, osize, nsize); 80 | if (block == NULL && nsize > 0) 81 | luaD_throw(L, LUA_ERRMEM); 82 | lua_assert((nsize == 0) == (block == NULL)); 83 | g->totalbytes = (g->totalbytes - osize) + nsize; 84 | return block; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /appslua/v02a_tests.lua: -------------------------------------------------------------------------------- 1 | -- simple delay function -- easier to use than TimerMatch() (but not that versatile) function dt() local i for i=20,0,-1 do print(i) pic.TimerDelay(i*50) end end -- detect and mount SD-card -- exits on ESC from console function dm() print("wating for card insertion...") while pic.CardDetect() == 0 do io.write(".") if pic.ConsoleReadChar() == 27 then print("\r\ngiving up...") return 0 end end io.flush() io.write("\ncard found, about to mount: ") io.flush() pic.TimerDelay(500) if pic.CardMount() == 0 then print("ERROR") return end print("OK") return 1 end -- mount card, if available -- list all files, directories and sizes of the root dir entries function lf() local nam, size if pic.CardMount() == 0 then print("ERROR MOUNTING CARD") return 0 end print() nam,size=pic.CardFindFirst("*.*",2) if nam ~= nil then while 1 do print(nam,size) nam,size=pic.CardFindNext() if nam==nil then break end end else print("empty") end end -- open a file by name and display its contents -- if no file name is specified, the first file found is used -- make sure this is a text file ;-) -- Note: It might look strange depending on your terminal -- settings (newline, linefeed, ...) function of(fname) local fin if pic.CardMount() == 0 then print("ERROR MOUNTING CARD") return 0 end if fname==nil then fname=pic.CardFindFirst("*.*") if fname==nil then print("NO FILES FOUND") return 0 end end print("OPENING "..fname) fin=io.open(fname,"r") if fin==nil then print("ERROR OPENING FILE") return 0 end print("---") for lines in fin:lines() do print(lines) end print("---") fin:close() print("FILE CLOSED") end -- enter a file name, create that file on SD-card and -- enter some text, empty line quits! -- Notes (READ EM!): -- - Do not use delete, backspace or other "special" characters. -- - use a "8+3" filename -- - This is just a demonstration! function cf() local tmp local fout if pic.CardMount() == 0 then print("ERROR MOUNTING CARD") return 0 end io.write("ENTER FILENAME: ") tmp=io.read() if string.len(tmp)==0 then print("QUIT...") return 0 end fout=io.open(tmp,"w") if fout==nil then print("UNABLE TO OPEN FILE") return 0 end print("ENTER SOME TEXT, QUIT WITH AN EMPTY LINE") while 1 do tmp=io.read() print() if string.len(tmp)==0 then break end fout:write("\r\n") fout:write(tmp) end fout:flush() fout:close() print("\r\nNOW STICK THAT CARD INTO YOUR DIRTY") print("PC CARD-READER AND LOOK WHAT YOU HAVE DONE...") return 1 end -------------------------------------------------------------------------------- /appslua/v01b_tests.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- A few PIC32Lua test functions 3 | -- ASkr 2010; www.askrprojects.net 4 | -- 5 | -- V0.1b 6 | -- 7 | 8 | 9 | 10 | -- binary number notation, timer, console 11 | -- toggles PORTD 0-2; leaves on ESC from console 12 | -- Can be used with the LEDs on a PIC32 starter kit. 13 | function bn() 14 | local LEDTAB={0b111, 0b000, 0b001, 0b010, 0b100, 0b010, 0b001, 0b000} 15 | local i 16 | local stim 17 | 18 | -- set PORTD 0-2 to low 19 | pic.PortPinsLow(pic.port.D,0b111) 20 | -- set PORTD 0-2 to output 21 | pic.PortDir(pic.port.D,0b111,0b111) 22 | 23 | -- read timer value 24 | stim=pic.TimerRead() 25 | 26 | while 1 do 27 | for i=1,table.getn(LEDTAB) do 28 | stim=stim+0.1/pic.TIMERTICK 29 | pic.PortState(pic.port.D,LEDTAB[i],0b111) 30 | pic.TimerMatch(stim) 31 | -- quit on ESC from console 32 | if pic.ConsoleReadChar()==27 then 33 | return 34 | end 35 | 36 | print(collectgarbage("count")) 37 | 38 | end -- for 39 | end -- while 40 | 41 | end 42 | 43 | 44 | 45 | -- end of memory test 46 | -- will fill up the memory until the interpreter stops 47 | function em() 48 | -- stop the garbage collector 49 | collectgarbage("stop") 50 | while(1) do 51 | print(collectgarbage("count")) 52 | end 53 | end 54 | 55 | 56 | 57 | -- LED control with upvalues (aka.: external local variables) 58 | -- just a little demonstration of Lua's features 59 | -- Do not call this directly. 60 | -- See "golc()" below... 61 | function lc(port,pin) 62 | return function(state) 63 | if state==0 then 64 | uvState=0 65 | pic.PortPinsLow(port,pin) 66 | else 67 | uvState=1 68 | pic.PortPinsHigh(port,pin) 69 | end 70 | end 71 | end 72 | 73 | 74 | -- using upvalues 75 | function golc() 76 | local stim 77 | 78 | -- clear lower 3 bits of PORTD 79 | pic.PortPinsLow(pic.port.D,0b111) 80 | -- set lower 3 bits of PORTD to output 81 | pic.PortDir(pic.port.D,0b111,0b111) 82 | 83 | -- create 3 LED closures 84 | led1=lc(pic.port.D,0b001) 85 | led2=lc(pic.port.D,0b010) 86 | led3=lc(pic.port.D,0b100) 87 | 88 | -- read timer value 89 | stim=pic.TimerRead() 90 | 91 | while 1 do 92 | stim=stim+0.1/pic.TIMERTICK 93 | 94 | -- use our new closures... 95 | led1(math.random(2)-1) 96 | led2(math.random(2)-1) 97 | led3(math.random(2)-1) 98 | 99 | pic.TimerMatch(stim) 100 | 101 | -- quit on ESC from console 102 | if pic.ConsoleReadChar()==27 then 103 | return 104 | end 105 | end 106 | 107 | end 108 | 109 | 110 | -------------------------------------------------------------------------------- /src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VLOCAL, /* info = local register */ 27 | VUPVAL, /* info = index of upvalue in `upvalues' */ 28 | VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 29 | VINDEXED, /* info = table register; aux = index register (or `k') */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VNONRELOC, /* info = result register */ 33 | VCALL, /* info = instruction pc */ 34 | VVARARG /* info = instruction pc */ 35 | } expkind; 36 | 37 | typedef struct expdesc { 38 | expkind k; 39 | union { 40 | struct { int info, aux; } s; 41 | lua_Number nval; 42 | } u; 43 | int t; /* patch list of `exit when true' */ 44 | int f; /* patch list of `exit when false' */ 45 | } expdesc; 46 | 47 | 48 | typedef struct upvaldesc { 49 | lu_byte k; 50 | lu_byte info; 51 | } upvaldesc; 52 | 53 | 54 | struct BlockCnt; /* defined in lparser.c */ 55 | 56 | 57 | /* state needed to generate code for a given function */ 58 | typedef struct FuncState { 59 | Proto *f; /* current function header */ 60 | Table *h; /* table to find (and reuse) elements in `k' */ 61 | struct FuncState *prev; /* enclosing function */ 62 | struct LexState *ls; /* lexical state */ 63 | struct lua_State *L; /* copy of the Lua state */ 64 | struct BlockCnt *bl; /* chain of current blocks */ 65 | int pc; /* next position to code (equivalent to `ncode') */ 66 | int lasttarget; /* `pc' of last `jump target' */ 67 | int jpc; /* list of pending jumps to `pc' */ 68 | int freereg; /* first free register */ 69 | int nk; /* number of elements in `k' */ 70 | int np; /* number of elements in `p' */ 71 | short nlocvars; /* number of elements in `locvars' */ 72 | lu_byte nactvar; /* number of active local variables */ 73 | upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 74 | unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 75 | } FuncState; 76 | 77 | 78 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 79 | const char *name); 80 | 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | PIC32Lua 3 | ======== 4 | 5 | Lua for PIC32 Processors. 6 | 7 | A newly created Git playground. 8 | For binary releases see [www.askrprojects.net/software/pic32lua/index.html][1] 9 | 10 | 2015/05: Also notice the new PIC32LuaMZ playground [here][2]. 11 | 12 | --- 13 | 14 | ## CHANGES 15 | 16 | ### V0.1a 17 | - NEW: port "names" ("pic.port.A", "pic.port.B", ...) 18 | - NEW: PortLatch(, ) 19 | - FIX: some cleanups in asIO.c (stdio, stderr, io.write, ...) 20 | 21 | 22 | ### V0.1b 23 | - NEW: binary number representation with, e.g.: '0b001110' 24 | - NEW: timer tick-time variable (100ns, by default) in "pic.TIMERTICK" 25 | 26 | 27 | ### V0.2a 28 | - NEW: simple delay function in msec and usec; TimerDelay(, []) 29 | - SD-CARD FUNCTIONS: 30 | - NEW: check card insertion: CardDetect() 31 | - NEW: mount card: CardMount() 32 | - NEW: find first file/dir: FindFirst(, ) 33 | - NEW: find next file/dir: FindNext() 34 | - NEW: almost complete (but yet not fully tested) IOLIB sd-card integration; 35 | reading, writing, seeking, ..., can all be done through Lua's iolib 36 | - NEW: interface to IOLIB's console interface. E.g.: io.write, io.read, ... 37 | 38 | 39 | ### V0.2b 40 | - NEW: configuration bits now in code (by popular demand ;-) 41 | - FIX: PortDir() now returns the correct values (~TRISx) 42 | - AD CONVERTER: 43 | - NEW: select analog input pins; ADPins() 44 | - NEW: select multiplexer input pins; ADMux(,,,) 45 | - NEW: select Vref; ADRef() 46 | - NEW: start conversion and read result; =ADRead() 47 | 48 | 49 | ### V0.2c 50 | - NEW: limited support for "dofile()" and "loadfile()" (no real error checks) 51 | - FIX: wrong buffer index in asUART1.c (UART1_RXBUFSIZE replaces RXBUFSIZE) 52 | - FIX: stupid buffer overrun error in asUART1.c and asCON.c 53 | 54 | 55 | ### V0.3a 56 | - FIX: support for C32 v2.01 57 | - CHG: CardMount() does not check for card insertion anymore 58 | - CHG: minor code cleanups (replaced default 'stderr' output by 'stdin', ...) 59 | 60 | 61 | ### V0.4a 62 | - NEW: support for chipKIT-Max32 63 | - NEW: console and serial port UARTs can now be swapped 64 | - FIX: minor cleanups (still a LOT to do ;-) 65 | 66 | 67 | ### V0.4b 68 | - NEW: SD-card boot file support; boots function "BOOT32()" in file "BOOT32.LUA" 69 | - NEW: added SPI pin documentation 70 | - FIX: corrected pins for SPI1 71 | SD-card now works on both SPI interfaces 72 | 73 | 74 | ### V0.4c: 75 | - FIX: 64-bit timer bug (lock-up after 430s) 76 | 77 | 78 | Have fun 79 | FMMT666(ASkr) 80 | 81 | 82 | [1]: http://www.askrprojects.net/software/pic32lua/index.html 83 | [2]: https://github.com/FMMT666/PIC32LuaMZ 84 | -------------------------------------------------------------------------------- /src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef LUAI_UINT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 48 | 49 | 50 | /* result of a `usual argument conversion' over lua_Number */ 51 | typedef LUAI_UACNUMBER l_uacNumber; 52 | 53 | 54 | /* internal assertions for in-house debugging */ 55 | #ifdef lua_assert 56 | 57 | #define check_exp(c,e) (lua_assert(c), (e)) 58 | #define api_check(l,e) lua_assert(e) 59 | 60 | #else 61 | 62 | #define lua_assert(c) ((void)0) 63 | #define check_exp(c,e) (e) 64 | #define api_check luai_apicheck 65 | 66 | #endif 67 | 68 | 69 | #ifndef UNUSED 70 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 71 | #endif 72 | 73 | 74 | #ifndef cast 75 | #define cast(t, exp) ((t)(exp)) 76 | #endif 77 | 78 | #define cast_byte(i) cast(lu_byte, (i)) 79 | #define cast_num(i) cast(lua_Number, (i)) 80 | #define cast_int(i) cast(int, (i)) 81 | 82 | 83 | 84 | /* 85 | ** type for virtual-machine instructions 86 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 87 | */ 88 | typedef lu_int32 Instruction; 89 | 90 | 91 | 92 | /* maximum stack for a Lua function */ 93 | #define MAXSTACK 250 94 | 95 | 96 | 97 | /* minimum size for the string table (must be power of 2) */ 98 | #ifndef MINSTRTABSIZE 99 | #define MINSTRTABSIZE 32 100 | #endif 101 | 102 | 103 | /* minimum size for string buffer */ 104 | #ifndef LUA_MINBUFFER 105 | #define LUA_MINBUFFER 32 106 | #endif 107 | 108 | 109 | #ifndef lua_lock 110 | #define lua_lock(L) ((void) 0) 111 | #define lua_unlock(L) ((void) 0) 112 | #endif 113 | 114 | #ifndef luai_threadyield 115 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 116 | #endif 117 | 118 | 119 | /* 120 | ** macro to control inclusion of some hard tests on stack reallocation 121 | */ 122 | #ifndef HARDSTACKTESTS 123 | #define condhardstacktests(x) ((void)0) 124 | #else 125 | #define condhardstacktests(x) x 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_NE, OPR_EQ, 30 | OPR_LT, OPR_LE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 46 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 47 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 48 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 49 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 50 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 51 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 52 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 53 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 54 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 55 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 56 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 59 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 60 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 61 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 62 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 63 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 64 | LUAI_FUNC int luaK_jump (FuncState *fs); 65 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 66 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 67 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 68 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 69 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 70 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 71 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 72 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 73 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | const char *const luaP_opnames[NUM_OPCODES+1] = { 17 | "MOVE", 18 | "LOADK", 19 | "LOADBOOL", 20 | "LOADNIL", 21 | "GETUPVAL", 22 | "GETGLOBAL", 23 | "GETTABLE", 24 | "SETGLOBAL", 25 | "SETUPVAL", 26 | "SETTABLE", 27 | "NEWTABLE", 28 | "SELF", 29 | "ADD", 30 | "SUB", 31 | "MUL", 32 | "DIV", 33 | "MOD", 34 | "POW", 35 | "UNM", 36 | "NOT", 37 | "LEN", 38 | "CONCAT", 39 | "JMP", 40 | "EQ", 41 | "LT", 42 | "LE", 43 | "TEST", 44 | "TESTSET", 45 | "CALL", 46 | "TAILCALL", 47 | "RETURN", 48 | "FORLOOP", 49 | "FORPREP", 50 | "TFORLOOP", 51 | "SETLIST", 52 | "CLOSE", 53 | "CLOSURE", 54 | "VARARG", 55 | NULL 56 | }; 57 | 58 | 59 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 60 | 61 | const lu_byte luaP_opmodes[NUM_OPCODES] = { 62 | /* T A B C mode opcode */ 63 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 64 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 65 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 66 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 67 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 68 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 69 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 70 | ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 71 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 72 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 73 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 74 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 75 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 76 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 77 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 78 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 81 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 82 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 83 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 84 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 85 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 86 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 87 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 88 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 89 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 90 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 91 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 92 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 93 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 94 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 95 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 96 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 97 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 98 | ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 99 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 100 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 101 | }; 102 | 103 | -------------------------------------------------------------------------------- /src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | 22 | void luaS_resize (lua_State *L, int newsize) { 23 | GCObject **newhash; 24 | stringtable *tb; 25 | int i; 26 | if (G(L)->gcstate == GCSsweepstring) 27 | return; /* cannot resize during GC traverse */ 28 | newhash = luaM_newvector(L, newsize, GCObject *); 29 | tb = &G(L)->strt; 30 | for (i=0; isize; i++) { 33 | GCObject *p = tb->hash[i]; 34 | while (p) { /* for each node in the list */ 35 | GCObject *next = p->gch.next; /* save next */ 36 | unsigned int h = gco2ts(p)->hash; 37 | int h1 = lmod(h, newsize); /* new position */ 38 | lua_assert(cast_int(h%newsize) == lmod(h, newsize)); 39 | p->gch.next = newhash[h1]; /* chain it */ 40 | newhash[h1] = p; 41 | p = next; 42 | } 43 | } 44 | luaM_freearray(L, tb->hash, tb->size, TString *); 45 | tb->size = newsize; 46 | tb->hash = newhash; 47 | } 48 | 49 | 50 | static TString *newlstr (lua_State *L, const char *str, size_t l, 51 | unsigned int h) { 52 | TString *ts; 53 | stringtable *tb; 54 | if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 55 | luaM_toobig(L); 56 | ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); 57 | ts->tsv.len = l; 58 | ts->tsv.hash = h; 59 | ts->tsv.marked = luaC_white(G(L)); 60 | ts->tsv.tt = LUA_TSTRING; 61 | ts->tsv.reserved = 0; 62 | memcpy(ts+1, str, l*sizeof(char)); 63 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 64 | tb = &G(L)->strt; 65 | h = lmod(h, tb->size); 66 | ts->tsv.next = tb->hash[h]; /* chain new entry */ 67 | tb->hash[h] = obj2gco(ts); 68 | tb->nuse++; 69 | if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 70 | luaS_resize(L, tb->size*2); /* too crowded */ 71 | return ts; 72 | } 73 | 74 | 75 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 76 | GCObject *o; 77 | unsigned int h = cast(unsigned int, l); /* seed */ 78 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 79 | size_t l1; 80 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 81 | h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 82 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 83 | o != NULL; 84 | o = o->gch.next) { 85 | TString *ts = rawgco2ts(o); 86 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { 87 | /* string may be dead */ 88 | if (isdead(G(L), o)) changewhite(o); 89 | return ts; 90 | } 91 | } 92 | return newlstr(L, str, l, h); /* not found */ 93 | } 94 | 95 | 96 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 97 | Udata *u; 98 | if (s > MAX_SIZET - sizeof(Udata)) 99 | luaM_toobig(L); 100 | u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 101 | u->uv.marked = luaC_white(G(L)); /* is not finalized */ 102 | u->uv.tt = LUA_TUSERDATA; 103 | u->uv.len = s; 104 | u->uv.metatable = NULL; 105 | u->uv.env = e; 106 | /* chain it on udata list (after main thread) */ 107 | u->uv.next = G(L)->mainthread->next; 108 | G(L)->mainthread->next = obj2gco(u); 109 | return u; 110 | } 111 | 112 | -------------------------------------------------------------------------------- /src/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | ** Possible states of the Garbage Collector 16 | */ 17 | #define GCSpause 0 18 | #define GCSpropagate 1 19 | #define GCSsweepstring 2 20 | #define GCSsweep 3 21 | #define GCSfinalize 4 22 | 23 | 24 | /* 25 | ** some userful bit tricks 26 | */ 27 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 28 | #define setbits(x,m) ((x) |= (m)) 29 | #define testbits(x,m) ((x) & (m)) 30 | #define bitmask(b) (1<<(b)) 31 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 32 | #define l_setbit(x,b) setbits(x, bitmask(b)) 33 | #define resetbit(x,b) resetbits(x, bitmask(b)) 34 | #define testbit(x,b) testbits(x, bitmask(b)) 35 | #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 36 | #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 37 | #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 38 | 39 | 40 | 41 | /* 42 | ** Layout for bit use in `marked' field: 43 | ** bit 0 - object is white (type 0) 44 | ** bit 1 - object is white (type 1) 45 | ** bit 2 - object is black 46 | ** bit 3 - for userdata: has been finalized 47 | ** bit 3 - for tables: has weak keys 48 | ** bit 4 - for tables: has weak values 49 | ** bit 5 - object is fixed (should not be collected) 50 | ** bit 6 - object is "super" fixed (only the main thread) 51 | */ 52 | 53 | 54 | #define WHITE0BIT 0 55 | #define WHITE1BIT 1 56 | #define BLACKBIT 2 57 | #define FINALIZEDBIT 3 58 | #define KEYWEAKBIT 3 59 | #define VALUEWEAKBIT 4 60 | #define FIXEDBIT 5 61 | #define SFIXEDBIT 6 62 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 63 | 64 | 65 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 66 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 67 | #define isgray(x) (!isblack(x) && !iswhite(x)) 68 | 69 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 70 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 71 | 72 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 73 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 74 | 75 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 76 | 77 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 78 | 79 | 80 | #define luaC_checkGC(L) { \ 81 | condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 82 | if (G(L)->totalbytes >= G(L)->GCthreshold) \ 83 | luaC_step(L); } 84 | 85 | 86 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 87 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 88 | 89 | #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 90 | luaC_barrierback(L,t); } 91 | 92 | #define luaC_objbarrier(L,p,o) \ 93 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 94 | luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 95 | 96 | #define luaC_objbarriert(L,t,o) \ 97 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 98 | 99 | LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 100 | LUAI_FUNC void luaC_callGCTM (lua_State *L); 101 | LUAI_FUNC void luaC_freeall (lua_State *L); 102 | LUAI_FUNC void luaC_step (lua_State *L); 103 | LUAI_FUNC void luaC_fullgc (lua_State *L); 104 | LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 105 | LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 106 | LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 107 | LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 108 | 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL || getstr(s)==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size,D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttype(o),D); 88 | switch (ttype(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: 102 | lua_assert(0); /* cannot happen */ 103 | break; 104 | } 105 | } 106 | n=f->sizep; 107 | DumpInt(n,D); 108 | for (i=0; ip[i],f->source,D); 109 | } 110 | 111 | static void DumpDebug(const Proto* f, DumpState* D) 112 | { 113 | int i,n; 114 | n= (D->strip) ? 0 : f->sizelineinfo; 115 | DumpVector(f->lineinfo,n,sizeof(int),D); 116 | n= (D->strip) ? 0 : f->sizelocvars; 117 | DumpInt(n,D); 118 | for (i=0; ilocvars[i].varname,D); 121 | DumpInt(f->locvars[i].startpc,D); 122 | DumpInt(f->locvars[i].endpc,D); 123 | } 124 | n= (D->strip) ? 0 : f->sizeupvalues; 125 | DumpInt(n,D); 126 | for (i=0; iupvalues[i],D); 127 | } 128 | 129 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) 130 | { 131 | DumpString((f->source==p || D->strip) ? NULL : f->source,D); 132 | DumpInt(f->linedefined,D); 133 | DumpInt(f->lastlinedefined,D); 134 | DumpChar(f->nups,D); 135 | DumpChar(f->numparams,D); 136 | DumpChar(f->is_vararg,D); 137 | DumpChar(f->maxstacksize,D); 138 | DumpCode(f,D); 139 | DumpConstants(f,D); 140 | DumpDebug(f,D); 141 | } 142 | 143 | static void DumpHeader(DumpState* D) 144 | { 145 | char h[LUAC_HEADERSIZE]; 146 | luaU_header(h); 147 | DumpBlock(h,LUAC_HEADERSIZE,D); 148 | } 149 | 150 | /* 151 | ** dump Lua function as precompiled chunk 152 | */ 153 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 154 | { 155 | DumpState D; 156 | D.L=L; 157 | D.writer=w; 158 | D.data=data; 159 | D.strip=strip; 160 | D.status=0; 161 | DumpHeader(&D); 162 | DumpFunction(f,NULL,&D); 163 | return D.status; 164 | } 165 | -------------------------------------------------------------------------------- /appslua/v01a_tests.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- A few PIC32Lua test functions 3 | -- ASkr 2010; www.askrprojects.net 4 | -- 5 | -- V0.1a 6 | -- 7 | 8 | 9 | -- browse available pic functions & variables 10 | function bp() 11 | local i,j 12 | 13 | for i,j in pairs(pic) do 14 | print(i,j) 15 | end 16 | end 17 | 18 | 19 | -- simple port pin toggle 20 | function pt() 21 | local i 22 | 23 | pic.PortDir(1,1,1) 24 | 25 | for i=1,1000 do 26 | pic.PortState(1,1,1) 27 | pic.PortState(1,0,1) 28 | end 29 | end 30 | 31 | 32 | -- more port pin stuff 33 | function mp() 34 | local i,j 35 | 36 | -- PORTD 0-3 out 37 | pic.PortDir(3,7,7) 38 | 39 | for i=1,100 do 40 | pic.PinsHigh(3,7) 41 | pic.PinsLow(3,7) 42 | 43 | for j=1,10 do 44 | pic.PinsInv(3,7) 45 | end 46 | 47 | end 48 | end 49 | 50 | 51 | -- read timer 52 | -- ESC quits 53 | function rt() 54 | local i 55 | 56 | while(1) do 57 | print(pic.TimerRead()) 58 | end 59 | 60 | if pic.ConsoleReadChar()==27 then 61 | break 62 | end 63 | 64 | end 65 | 66 | 67 | -- timer match (BLOCKING) 68 | -- ESC quits 69 | function tm() 70 | local stim 71 | local tim,timh 72 | local i 73 | 74 | tim=pic.TimerRead() 75 | while(1) do 76 | -- 500ms 77 | tim=tim+(0.5/100e-9) 78 | print(tim) 79 | pic.TimerMatch(tim) 80 | 81 | if pic.ConsoleReadChar()==27 then 82 | break 83 | end 84 | 85 | end 86 | end 87 | 88 | 89 | -- timer match 2 (NONBLOCKING) 90 | -- ESC quits 91 | function tm2() 92 | local stim 93 | local tim,timh 94 | local i 95 | 96 | tim=pic.TimerRead() 97 | while(1) do 98 | -- 500ms 99 | tim=tim+(0.5/100e-9) 100 | print(tim) 101 | 102 | while pic.TimerMatch(tim,0)==0 do 103 | -- do something, in here... 104 | end 105 | 106 | if pic.ConsoleReadChar()==27 then 107 | break 108 | end 109 | 110 | end 111 | end 112 | 113 | 114 | -- timer match 3 (NONBLOCKING) 115 | -- ESC quits 116 | function tm3() 117 | local stim 118 | local tim,timh 119 | local i 120 | 121 | tim=pic.TimerRead() 122 | while(1) do 123 | -- 500ms 124 | tim=tim+(0.5/100e-9) 125 | print(tim) 126 | 127 | i=0 128 | while(1) do 129 | i=i+1 130 | if pic.TimerMatch(tim,0)==1 then 131 | break 132 | end 133 | end 134 | 135 | print("i: "..i) 136 | 137 | if pic.ConsoleReadChar()==27 then 138 | break 139 | end 140 | 141 | end 142 | end 143 | 144 | 145 | -- console read 146 | -- ESC quits 147 | function cr() 148 | local ch 149 | 150 | repeat 151 | ch=pic.ConsoleReadChar() 152 | 153 | if ch~=nil then 154 | print(ch) 155 | end 156 | 157 | until ch==27 158 | 159 | end 160 | 161 | 162 | -- console count 163 | -- quits after receiving 10 characters 164 | function cc() 165 | local i 166 | 167 | repeat 168 | i=pic.ConsoleCount() 169 | print(i) 170 | until i>10 171 | 172 | pic.ConsoleFlushIn() 173 | 174 | end 175 | 176 | 177 | -- UART1 loopback 178 | -- connect UART1 TX and RX together 179 | function ul() 180 | local i 181 | 182 | pic.UART1Enable(115200) 183 | 184 | print("filling RX buffer:") 185 | for i=65,65+9 do 186 | print(" sending "..string.char(i)) 187 | pic.UART1WriteChar(i) 188 | end 189 | 190 | print("check reception:") 191 | print("buffer contains:"..pic.UART1Count().." characters") 192 | while(1) do 193 | i=pic.UART1ReadChar() 194 | if i ~= nil then 195 | print(" received "..string.char(i)) 196 | else 197 | break 198 | end 199 | end 200 | 201 | pic.UART1Disable() 202 | 203 | end 204 | 205 | 206 | -- UART1 overflow 207 | -- connect UART1 TX and RX together 208 | function uo() 209 | local i 210 | 211 | pic.UART1Enable(115200) 212 | 213 | print("filling RX buffer:") 214 | for i=1,260 do 215 | -- reports -1 on overflow 216 | pic.UART1WriteChar(i) 217 | print("buffer: "..pic.UART1Count()) 218 | end 219 | 220 | print("flushing buffer...") 221 | pic.UART1FlushIn() 222 | print("buffer: "..pic.UART1Count()) 223 | 224 | pic.UART1Disable() 225 | 226 | end 227 | 228 | -------------------------------------------------------------------------------- /src/asCARD.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | #ifndef __asCARD 10 | #define __asCARD 11 | #endif 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | #include "asPIC.h" 19 | 20 | #include "FSIO.h" 21 | 22 | #include "asIO.h" 23 | #include "asDBG.h" 24 | #include "asCON.h" 25 | #include "asLUA_PIC.h" 26 | #include "asCARD.h" 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | #ifdef USE_SDCARD_CODE 35 | 36 | 37 | unsigned gCardState=0; 38 | 39 | SearchRec CardRec; 40 | 41 | 42 | 43 | 44 | //************************************************************************************** 45 | //*** asCARD_Detect 46 | //*** 47 | //*** 48 | //************************************************************************************** 49 | int asCARD_Detect() 50 | { 51 | if(MDD_MediaDetect()) 52 | { 53 | gCardState |= CARD_DETECTED; 54 | return 1; 55 | } 56 | else 57 | { 58 | gCardState &= ~CARD_DETECTED; 59 | return 0; 60 | } 61 | } 62 | 63 | 64 | 65 | //************************************************************************************** 66 | //*** asCARD_Mount 67 | //*** 68 | //*** 69 | //************************************************************************************** 70 | int asCARD_Mount() 71 | { 72 | // changed default behaviour 73 | /* 74 | if(asCARD_Detect()) 75 | { 76 | */ 77 | if(FSInit()) 78 | { 79 | gCardState|=CARD_MOUNTED; 80 | return 1; 81 | } 82 | else 83 | { 84 | gCardState&=~CARD_MOUNTED; 85 | return 0; 86 | } 87 | /* 88 | } 89 | else 90 | return 0; 91 | */ 92 | } 93 | 94 | 95 | 96 | //************************************************************************************** 97 | //*** asCARD_FindFirst 98 | //*** 99 | //*** 100 | //*** type: 0=files, 1=dirs; 2=files+dirs 101 | //*** returns 0 if file found 102 | //************************************************************************************** 103 | int asCARD_FindFirst(char *fname, unsigned int type) 104 | { 105 | // this is not really failsafe... 106 | if( gCardState & CARD_MOUNTED ) 107 | return FindFirst(fname, type, &CardRec); 108 | else 109 | return -1; 110 | 111 | } 112 | 113 | 114 | 115 | //************************************************************************************** 116 | //*** asCARD_FindNext 117 | //*** 118 | //*** returns 0 if file found 119 | //************************************************************************************** 120 | int asCARD_FindNext() 121 | { 122 | // this is not really failsafe... 123 | if(gCardState&CARD_MOUNTED) 124 | return FindNext(&CardRec); 125 | else 126 | return -1; 127 | } 128 | 129 | 130 | 131 | 132 | 133 | //************************************************************************************** 134 | //*** asCARD_Boot 135 | //*** 136 | //*** 137 | //************************************************************************************** 138 | int asCARD_Boot(lua_State *L) 139 | { 140 | int boot32 = 0; 141 | 142 | if( asCARD_Mount() ) 143 | { 144 | 145 | if( asCARD_FindFirst("BOOT32.LUA",ATTR_READ_ONLY|ATTR_HIDDEN|ATTR_SYSTEM|ATTR_VOLUME|ATTR_ARCHIVE) == 0 ) 146 | { 147 | // TODO: SD-card boot file errors 148 | lua_getglobal(L, "dofile"); 149 | lua_pushstring(L,"BOOT32.LUA"); 150 | if (lua_pcall(L, 1, 0, 0) != 0) 151 | boot32 |= CARD_BOOT_ERR_NORUN; 152 | else 153 | { 154 | lua_getglobal(L, "BOOT32"); 155 | if (lua_pcall(L, 0, 0, 0) != 0) 156 | boot32 |= CARD_BOOT_ERR_NOFCT; 157 | } 158 | 159 | } 160 | else 161 | boot32 |= CARD_BOOT_ERR_NOFILE; 162 | 163 | }// unable to mount card 164 | else 165 | boot32 |= CARD_BOOT_ERR_NOMOUNT; 166 | 167 | return boot32; 168 | } 169 | 170 | 171 | /* 172 | int asCARD_Boot(lua_State *asL) 173 | { 174 | int boot32 = 0; 175 | 176 | if( asCARD_Mount() ) 177 | { 178 | 179 | if( asCARD_FindFirst("BOOT32.LUA",ATTR_READ_ONLY|ATTR_HIDDEN|ATTR_SYSTEM|ATTR_VOLUME|ATTR_ARCHIVE) == 0 ) 180 | { 181 | // TODO: SD-card boot file errors 182 | lua_getglobal(asL, "dofile"); 183 | lua_pushstring(asL,"BOOT32.LUA"); 184 | if (lua_pcall(asL, 1, 0, 0) != 0) 185 | boot32 |= CARD_BOOT_ERR_NORUN; 186 | else 187 | { 188 | lua_getglobal(asL, "BOOT32"); 189 | if (lua_pcall(asL, 0, 0, 0) != 0) 190 | boot32 |= CARD_BOOT_ERR_NOFCT; 191 | } 192 | 193 | } 194 | else 195 | boot32 |= CARD_BOOT_ERR_NOFILE; 196 | 197 | }// unable to mount card 198 | else 199 | boot32 |= CARD_BOOT_ERR_NOMOUNT; 200 | 201 | return boot32; 202 | } 203 | */ 204 | 205 | 206 | #endif // USE SD CODE 207 | 208 | -------------------------------------------------------------------------------- /src/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 24 | Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 25 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 26 | c->c.isC = 1; 27 | c->c.env = e; 28 | c->c.nupvalues = cast_byte(nelems); 29 | return c; 30 | } 31 | 32 | 33 | Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { 34 | Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 35 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 36 | c->l.isC = 0; 37 | c->l.env = e; 38 | c->l.nupvalues = cast_byte(nelems); 39 | while (nelems--) c->l.upvals[nelems] = NULL; 40 | return c; 41 | } 42 | 43 | 44 | UpVal *luaF_newupval (lua_State *L) { 45 | UpVal *uv = luaM_new(L, UpVal); 46 | luaC_link(L, obj2gco(uv), LUA_TUPVAL); 47 | uv->v = &uv->u.value; 48 | setnilvalue(uv->v); 49 | return uv; 50 | } 51 | 52 | 53 | UpVal *luaF_findupval (lua_State *L, StkId level) { 54 | global_State *g = G(L); 55 | GCObject **pp = &L->openupval; 56 | UpVal *p; 57 | UpVal *uv; 58 | while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { 59 | lua_assert(p->v != &p->u.value); 60 | if (p->v == level) { /* found a corresponding upvalue? */ 61 | if (isdead(g, obj2gco(p))) /* is it dead? */ 62 | changewhite(obj2gco(p)); /* ressurect it */ 63 | return p; 64 | } 65 | pp = &p->next; 66 | } 67 | uv = luaM_new(L, UpVal); /* not found: create a new one */ 68 | uv->tt = LUA_TUPVAL; 69 | uv->marked = luaC_white(g); 70 | uv->v = level; /* current value lives in the stack */ 71 | uv->next = *pp; /* chain it in the proper position */ 72 | *pp = obj2gco(uv); 73 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 74 | uv->u.l.next = g->uvhead.u.l.next; 75 | uv->u.l.next->u.l.prev = uv; 76 | g->uvhead.u.l.next = uv; 77 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 78 | return uv; 79 | } 80 | 81 | 82 | static void unlinkupval (UpVal *uv) { 83 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 84 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 85 | uv->u.l.prev->u.l.next = uv->u.l.next; 86 | } 87 | 88 | 89 | void luaF_freeupval (lua_State *L, UpVal *uv) { 90 | if (uv->v != &uv->u.value) /* is it open? */ 91 | unlinkupval(uv); /* remove from open list */ 92 | luaM_free(L, uv); /* free upvalue */ 93 | } 94 | 95 | 96 | void luaF_close (lua_State *L, StkId level) { 97 | UpVal *uv; 98 | global_State *g = G(L); 99 | while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { 100 | GCObject *o = obj2gco(uv); 101 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 102 | L->openupval = uv->next; /* remove from `open' list */ 103 | if (isdead(g, o)) 104 | luaF_freeupval(L, uv); /* free upvalue */ 105 | else { 106 | unlinkupval(uv); 107 | setobj(L, &uv->u.value, uv->v); 108 | uv->v = &uv->u.value; /* now current value lives here */ 109 | luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ 110 | } 111 | } 112 | } 113 | 114 | 115 | Proto *luaF_newproto (lua_State *L) { 116 | Proto *f = luaM_new(L, Proto); 117 | luaC_link(L, obj2gco(f), LUA_TPROTO); 118 | f->k = NULL; 119 | f->sizek = 0; 120 | f->p = NULL; 121 | f->sizep = 0; 122 | f->code = NULL; 123 | f->sizecode = 0; 124 | f->sizelineinfo = 0; 125 | f->sizeupvalues = 0; 126 | f->nups = 0; 127 | f->upvalues = NULL; 128 | f->numparams = 0; 129 | f->is_vararg = 0; 130 | f->maxstacksize = 0; 131 | f->lineinfo = NULL; 132 | f->sizelocvars = 0; 133 | f->locvars = NULL; 134 | f->linedefined = 0; 135 | f->lastlinedefined = 0; 136 | f->source = NULL; 137 | return f; 138 | } 139 | 140 | 141 | void luaF_freeproto (lua_State *L, Proto *f) { 142 | luaM_freearray(L, f->code, f->sizecode, Instruction); 143 | luaM_freearray(L, f->p, f->sizep, Proto *); 144 | luaM_freearray(L, f->k, f->sizek, TValue); 145 | luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 146 | luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 147 | luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 148 | luaM_free(L, f); 149 | } 150 | 151 | 152 | void luaF_freeclosure (lua_State *L, Closure *c) { 153 | int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 154 | sizeLclosure(c->l.nupvalues); 155 | luaM_freemem(L, c, size); 156 | } 157 | 158 | 159 | /* 160 | ** Look for n-th local variable at line `line' in function `func'. 161 | ** Returns NULL if not found. 162 | */ 163 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 164 | int i; 165 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 166 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 167 | local_number--; 168 | if (local_number == 0) 169 | return getstr(f->locvars[i].varname); 170 | } 171 | } 172 | return NULL; /* not found */ 173 | } 174 | 175 | -------------------------------------------------------------------------------- /src/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define lundump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lfunc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstring.h" 20 | #include "lundump.h" 21 | #include "lzio.h" 22 | 23 | typedef struct { 24 | lua_State* L; 25 | ZIO* Z; 26 | Mbuffer* b; 27 | const char* name; 28 | } LoadState; 29 | 30 | #ifdef LUAC_TRUST_BINARIES 31 | #define IF(c,s) 32 | #define error(S,s) 33 | #else 34 | #define IF(c,s) if (c) error(S,s) 35 | 36 | static void error(LoadState* S, const char* why) 37 | { 38 | luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 39 | luaD_throw(S->L,LUA_ERRSYNTAX); 40 | } 41 | #endif 42 | 43 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 44 | #define LoadByte(S) (lu_byte)LoadChar(S) 45 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 46 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 47 | 48 | static void LoadBlock(LoadState* S, void* b, size_t size) 49 | { 50 | size_t r=luaZ_read(S->Z,b,size); 51 | IF (r!=0, "unexpected end"); 52 | } 53 | 54 | static int LoadChar(LoadState* S) 55 | { 56 | char x; 57 | LoadVar(S,x); 58 | return x; 59 | } 60 | 61 | static int LoadInt(LoadState* S) 62 | { 63 | int x; 64 | LoadVar(S,x); 65 | IF (x<0, "bad integer"); 66 | return x; 67 | } 68 | 69 | static lua_Number LoadNumber(LoadState* S) 70 | { 71 | lua_Number x; 72 | LoadVar(S,x); 73 | return x; 74 | } 75 | 76 | static TString* LoadString(LoadState* S) 77 | { 78 | size_t size; 79 | LoadVar(S,size); 80 | if (size==0) 81 | return NULL; 82 | else 83 | { 84 | char* s=luaZ_openspace(S->L,S->b,size); 85 | LoadBlock(S,s,size); 86 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 87 | } 88 | } 89 | 90 | static void LoadCode(LoadState* S, Proto* f) 91 | { 92 | int n=LoadInt(S); 93 | f->code=luaM_newvector(S->L,n,Instruction); 94 | f->sizecode=n; 95 | LoadVector(S,f->code,n,sizeof(Instruction)); 96 | } 97 | 98 | static Proto* LoadFunction(LoadState* S, TString* p); 99 | 100 | static void LoadConstants(LoadState* S, Proto* f) 101 | { 102 | int i,n; 103 | n=LoadInt(S); 104 | f->k=luaM_newvector(S->L,n,TValue); 105 | f->sizek=n; 106 | for (i=0; ik[i]); 107 | for (i=0; ik[i]; 110 | int t=LoadChar(S); 111 | switch (t) 112 | { 113 | case LUA_TNIL: 114 | setnilvalue(o); 115 | break; 116 | case LUA_TBOOLEAN: 117 | setbvalue(o,LoadChar(S)!=0); 118 | break; 119 | case LUA_TNUMBER: 120 | setnvalue(o,LoadNumber(S)); 121 | break; 122 | case LUA_TSTRING: 123 | setsvalue2n(S->L,o,LoadString(S)); 124 | break; 125 | default: 126 | error(S,"bad constant"); 127 | break; 128 | } 129 | } 130 | n=LoadInt(S); 131 | f->p=luaM_newvector(S->L,n,Proto*); 132 | f->sizep=n; 133 | for (i=0; ip[i]=NULL; 134 | for (i=0; ip[i]=LoadFunction(S,f->source); 135 | } 136 | 137 | static void LoadDebug(LoadState* S, Proto* f) 138 | { 139 | int i,n; 140 | n=LoadInt(S); 141 | f->lineinfo=luaM_newvector(S->L,n,int); 142 | f->sizelineinfo=n; 143 | LoadVector(S,f->lineinfo,n,sizeof(int)); 144 | n=LoadInt(S); 145 | f->locvars=luaM_newvector(S->L,n,LocVar); 146 | f->sizelocvars=n; 147 | for (i=0; ilocvars[i].varname=NULL; 148 | for (i=0; ilocvars[i].varname=LoadString(S); 151 | f->locvars[i].startpc=LoadInt(S); 152 | f->locvars[i].endpc=LoadInt(S); 153 | } 154 | n=LoadInt(S); 155 | f->upvalues=luaM_newvector(S->L,n,TString*); 156 | f->sizeupvalues=n; 157 | for (i=0; iupvalues[i]=NULL; 158 | for (i=0; iupvalues[i]=LoadString(S); 159 | } 160 | 161 | static Proto* LoadFunction(LoadState* S, TString* p) 162 | { 163 | Proto* f; 164 | if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); 165 | f=luaF_newproto(S->L); 166 | setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 167 | f->source=LoadString(S); if (f->source==NULL) f->source=p; 168 | f->linedefined=LoadInt(S); 169 | f->lastlinedefined=LoadInt(S); 170 | f->nups=LoadByte(S); 171 | f->numparams=LoadByte(S); 172 | f->is_vararg=LoadByte(S); 173 | f->maxstacksize=LoadByte(S); 174 | LoadCode(S,f); 175 | LoadConstants(S,f); 176 | LoadDebug(S,f); 177 | IF (!luaG_checkcode(f), "bad code"); 178 | S->L->top--; 179 | S->L->nCcalls--; 180 | return f; 181 | } 182 | 183 | static void LoadHeader(LoadState* S) 184 | { 185 | char h[LUAC_HEADERSIZE]; 186 | char s[LUAC_HEADERSIZE]; 187 | luaU_header(h); 188 | LoadBlock(S,s,LUAC_HEADERSIZE); 189 | IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 190 | } 191 | 192 | /* 193 | ** load precompiled chunk 194 | */ 195 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 196 | { 197 | LoadState S; 198 | if (*name=='@' || *name=='=') 199 | S.name=name+1; 200 | else if (*name==LUA_SIGNATURE[0]) 201 | S.name="binary string"; 202 | else 203 | S.name=name; 204 | S.L=L; 205 | S.Z=Z; 206 | S.b=buff; 207 | LoadHeader(&S); 208 | return LoadFunction(&S,luaS_newliteral(L,"=?")); 209 | } 210 | 211 | /* 212 | * make header 213 | */ 214 | void luaU_header (char* h) 215 | { 216 | int x=1; 217 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 218 | h+=sizeof(LUA_SIGNATURE)-1; 219 | *h++=(char)LUAC_VERSION; 220 | *h++=(char)LUAC_FORMAT; 221 | *h++=(char)*(char*)&x; /* endianness */ 222 | *h++=(char)sizeof(int); 223 | *h++=(char)sizeof(size_t); 224 | *h++=(char)sizeof(Instruction); 225 | *h++=(char)sizeof(lua_Number); 226 | *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 227 | } 228 | -------------------------------------------------------------------------------- /src/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | 18 | struct lua_longjmp; /* defined in ldo.c */ 19 | 20 | 21 | /* table of globals */ 22 | #define gt(L) (&L->l_gt) 23 | 24 | /* registry */ 25 | #define registry(L) (&G(L)->l_registry) 26 | 27 | 28 | /* extra stack space to handle TM calls and some other extras */ 29 | #define EXTRA_STACK 5 30 | 31 | 32 | #define BASIC_CI_SIZE 8 33 | 34 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 35 | 36 | 37 | 38 | typedef struct stringtable { 39 | GCObject **hash; 40 | lu_int32 nuse; /* number of elements */ 41 | int size; 42 | } stringtable; 43 | 44 | 45 | /* 46 | ** informations about a call 47 | */ 48 | typedef struct CallInfo { 49 | StkId base; /* base for this function */ 50 | StkId func; /* function index in the stack */ 51 | StkId top; /* top for this function */ 52 | const Instruction *savedpc; 53 | int nresults; /* expected number of results from this function */ 54 | int tailcalls; /* number of tail calls lost under this entry */ 55 | } CallInfo; 56 | 57 | 58 | 59 | #define curr_func(L) (clvalue(L->ci->func)) 60 | #define ci_func(ci) (clvalue((ci)->func)) 61 | #define f_isLua(ci) (!ci_func(ci)->c.isC) 62 | #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 63 | 64 | 65 | /* 66 | ** `global state', shared by all threads of this state 67 | */ 68 | typedef struct global_State { 69 | stringtable strt; /* hash table for strings */ 70 | lua_Alloc frealloc; /* function to reallocate memory */ 71 | void *ud; /* auxiliary data to `frealloc' */ 72 | lu_byte currentwhite; 73 | lu_byte gcstate; /* state of garbage collector */ 74 | int sweepstrgc; /* position of sweep in `strt' */ 75 | GCObject *rootgc; /* list of all collectable objects */ 76 | GCObject **sweepgc; /* position of sweep in `rootgc' */ 77 | GCObject *gray; /* list of gray objects */ 78 | GCObject *grayagain; /* list of objects to be traversed atomically */ 79 | GCObject *weak; /* list of weak tables (to be cleared) */ 80 | GCObject *tmudata; /* last element of list of userdata to be GC */ 81 | Mbuffer buff; /* temporary buffer for string concatentation */ 82 | lu_mem GCthreshold; 83 | lu_mem totalbytes; /* number of bytes currently allocated */ 84 | lu_mem estimate; /* an estimate of number of bytes actually in use */ 85 | lu_mem gcdept; /* how much GC is `behind schedule' */ 86 | int gcpause; /* size of pause between successive GCs */ 87 | int gcstepmul; /* GC `granularity' */ 88 | lua_CFunction panic; /* to be called in unprotected errors */ 89 | TValue l_registry; 90 | struct lua_State *mainthread; 91 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 92 | struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 93 | TString *tmname[TM_N]; /* array with tag-method names */ 94 | } global_State; 95 | 96 | 97 | /* 98 | ** `per thread' state 99 | */ 100 | struct lua_State { 101 | CommonHeader; 102 | lu_byte status; 103 | StkId top; /* first free slot in the stack */ 104 | StkId base; /* base of current function */ 105 | global_State *l_G; 106 | CallInfo *ci; /* call info for current function */ 107 | const Instruction *savedpc; /* `savedpc' of current function */ 108 | StkId stack_last; /* last free slot in the stack */ 109 | StkId stack; /* stack base */ 110 | CallInfo *end_ci; /* points after end of ci array*/ 111 | CallInfo *base_ci; /* array of CallInfo's */ 112 | int stacksize; 113 | int size_ci; /* size of array `base_ci' */ 114 | unsigned short nCcalls; /* number of nested C calls */ 115 | unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 116 | lu_byte hookmask; 117 | lu_byte allowhook; 118 | int basehookcount; 119 | int hookcount; 120 | lua_Hook hook; 121 | TValue l_gt; /* table of globals */ 122 | TValue env; /* temporary place for environments */ 123 | GCObject *openupval; /* list of open upvalues in this stack */ 124 | GCObject *gclist; 125 | struct lua_longjmp *errorJmp; /* current error recover point */ 126 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 127 | }; 128 | 129 | 130 | #define G(L) (L->l_G) 131 | 132 | 133 | /* 134 | ** Union of all collectable objects 135 | */ 136 | union GCObject { 137 | GCheader gch; 138 | union TString ts; 139 | union Udata u; 140 | union Closure cl; 141 | struct Table h; 142 | struct Proto p; 143 | struct UpVal uv; 144 | struct lua_State th; /* thread */ 145 | }; 146 | 147 | 148 | /* macros to convert a GCObject into a specific value */ 149 | #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 150 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 151 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 152 | #define gco2u(o) (&rawgco2u(o)->uv) 153 | #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 154 | #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 155 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 156 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 157 | #define ngcotouv(o) \ 158 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 159 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 160 | 161 | /* macro to convert any Lua object into a GCObject */ 162 | #define obj2gco(v) (cast(GCObject *, (v))) 163 | 164 | 165 | LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 166 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 167 | 168 | #endif 169 | 170 | -------------------------------------------------------------------------------- /src/print.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ 3 | ** print bytecodes 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #define luac_c 11 | #define LUA_CORE 12 | 13 | #include "ldebug.h" 14 | #include "lobject.h" 15 | #include "lopcodes.h" 16 | #include "lundump.h" 17 | 18 | #define PrintFunction luaU_print 19 | 20 | #define Sizeof(x) ((int)sizeof(x)) 21 | #define VOID(p) ((const void*)(p)) 22 | 23 | static void PrintString(const TString* ts) 24 | { 25 | const char* s=getstr(ts); 26 | size_t i,n=ts->tsv.len; 27 | putchar('"'); 28 | for (i=0; ik[i]; 54 | switch (ttype(o)) 55 | { 56 | case LUA_TNIL: 57 | printf("nil"); 58 | break; 59 | case LUA_TBOOLEAN: 60 | printf(bvalue(o) ? "true" : "false"); 61 | break; 62 | case LUA_TNUMBER: 63 | printf(LUA_NUMBER_FMT,nvalue(o)); 64 | break; 65 | case LUA_TSTRING: 66 | PrintString(rawtsvalue(o)); 67 | break; 68 | default: /* cannot happen */ 69 | printf("? type=%d",ttype(o)); 70 | break; 71 | } 72 | } 73 | 74 | static void PrintCode(const Proto* f) 75 | { 76 | const Instruction* code=f->code; 77 | int pc,n=f->sizecode; 78 | for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); 90 | printf("%-9s\t",luaP_opnames[o]); 91 | switch (getOpMode(o)) 92 | { 93 | case iABC: 94 | printf("%d",a); 95 | if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); 96 | if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); 97 | break; 98 | case iABx: 99 | if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); 100 | break; 101 | case iAsBx: 102 | if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); 103 | break; 104 | } 105 | switch (o) 106 | { 107 | case OP_LOADK: 108 | printf("\t; "); PrintConstant(f,bx); 109 | break; 110 | case OP_GETUPVAL: 111 | case OP_SETUPVAL: 112 | printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); 113 | break; 114 | case OP_GETGLOBAL: 115 | case OP_SETGLOBAL: 116 | printf("\t; %s",svalue(&f->k[bx])); 117 | break; 118 | case OP_GETTABLE: 119 | case OP_SELF: 120 | if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 121 | break; 122 | case OP_SETTABLE: 123 | case OP_ADD: 124 | case OP_SUB: 125 | case OP_MUL: 126 | case OP_DIV: 127 | case OP_POW: 128 | case OP_EQ: 129 | case OP_LT: 130 | case OP_LE: 131 | if (ISK(b) || ISK(c)) 132 | { 133 | printf("\t; "); 134 | if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 135 | printf(" "); 136 | if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 137 | } 138 | break; 139 | case OP_JMP: 140 | case OP_FORLOOP: 141 | case OP_FORPREP: 142 | printf("\t; to %d",sbx+pc+2); 143 | break; 144 | case OP_CLOSURE: 145 | printf("\t; %p",VOID(f->p[bx])); 146 | break; 147 | case OP_SETLIST: 148 | if (c==0) printf("\t; %d",(int)code[++pc]); 149 | else printf("\t; %d",c); 150 | break; 151 | default: 152 | break; 153 | } 154 | printf("\n"); 155 | } 156 | } 157 | 158 | #define SS(x) (x==1)?"":"s" 159 | #define S(x) x,SS(x) 160 | 161 | static void PrintHeader(const Proto* f) 162 | { 163 | const char* s=getstr(f->source); 164 | if (*s=='@' || *s=='=') 165 | s++; 166 | else if (*s==LUA_SIGNATURE[0]) 167 | s="(bstring)"; 168 | else 169 | s="(string)"; 170 | printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", 171 | (f->linedefined==0)?"main":"function",s, 172 | f->linedefined,f->lastlinedefined, 173 | S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); 174 | printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 175 | f->numparams,f->is_vararg?"+":"",SS(f->numparams), 176 | S(f->maxstacksize),S(f->nups)); 177 | printf("%d local%s, %d constant%s, %d function%s\n", 178 | S(f->sizelocvars),S(f->sizek),S(f->sizep)); 179 | } 180 | 181 | static void PrintConstants(const Proto* f) 182 | { 183 | int i,n=f->sizek; 184 | printf("constants (%d) for %p:\n",n,VOID(f)); 185 | for (i=0; isizelocvars; 196 | printf("locals (%d) for %p:\n",n,VOID(f)); 197 | for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 201 | } 202 | } 203 | 204 | static void PrintUpvalues(const Proto* f) 205 | { 206 | int i,n=f->sizeupvalues; 207 | printf("upvalues (%d) for %p:\n",n,VOID(f)); 208 | if (f->upvalues==NULL) return; 209 | for (i=0; iupvalues[i])); 212 | } 213 | } 214 | 215 | void PrintFunction(const Proto* f, int full) 216 | { 217 | int i,n=f->sizep; 218 | PrintHeader(f); 219 | PrintCode(f); 220 | if (full) 221 | { 222 | PrintConstants(f); 223 | PrintLocals(f); 224 | PrintUpvalues(f); 225 | } 226 | for (i=0; ip[i],full); 227 | } 228 | -------------------------------------------------------------------------------- /src/HardwareProfile.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _HARDWAREPROFILE_H_ 3 | #define _HARDWAREPROFILE_H_ 4 | 5 | 6 | #include "asPIC.h" 7 | 8 | 9 | 10 | //-------------------------------------------------------------------------------------- 11 | // DEFINE SD-CARD SPI PORT 12 | // #Note1: Choose only 1 ;) 13 | //#define MDD_USE_SPI_1 14 | #define MDD_USE_SPI_2 15 | 16 | 17 | 18 | 19 | 20 | 21 | //=========================================================================================== 22 | //=========================================================================================== 23 | // NO CHANGES BELOW THIS LINE 24 | // (EXCEPT YOU KNOW WHAT YOU ARE DOING ;-) 25 | 26 | 27 | // Clock values 28 | #define MILLISECONDS_PER_TICK 10 // Definition for use with a tick timer 29 | #define TIMER_PRESCALER TIMER_PRESCALER_8 // Definition for use with a tick timer 30 | #define TIMER_PERIOD 37500 // Definition for use with a tick timer 31 | 32 | //SPI Configuration 33 | #define SPI_START_CFG_1 (PRI_PRESCAL_64_1 | SEC_PRESCAL_8_1 | MASTER_ENABLE_ON | SPI_CKE_ON | SPI_SMP_ON) 34 | #define SPI_START_CFG_2 (SPI_ENABLE) 35 | 36 | // Define the SPI frequency 37 | #define SPI_FREQUENCY (20000000) 38 | 39 | 40 | #if defined MDD_USE_SPI_1 41 | // Description: SD-SPI Chip Select Output bit 42 | #define SD_CS LATBbits.LATB1 43 | // Description: SD-SPI Chip Select TRIS bit 44 | #define SD_CS_TRIS TRISBbits.TRISB1 45 | 46 | // Description: SD-SPI Card Detect Input bit 47 | #define SD_CD PORTFbits.RF0 48 | // Description: SD-SPI Card Detect TRIS bit 49 | #define SD_CD_TRIS TRISFbits.TRISF0 50 | 51 | // Description: SD-SPI Write Protect Check Input bit 52 | #define SD_WE PORTFbits.RF1 53 | // Description: SD-SPI Write Protect Check TRIS bit 54 | #define SD_WE_TRIS TRISFbits.TRISF1 55 | 56 | // Description: The main SPI control register 57 | #define SPICON1 SPI1CON 58 | // Description: The SPI status register 59 | #define SPISTAT SPI1STAT 60 | // Description: The SPI Buffer 61 | #define SPIBUF SPI1BUF 62 | // Description: The receive buffer full bit in the SPI status register 63 | #define SPISTAT_RBF SPI1STATbits.SPIRBF 64 | // Description: The bitwise define for the SPI control register (i.e. _____bits) 65 | #define SPICON1bits SPI1CONbits 66 | // Description: The bitwise define for the SPI status register (i.e. _____bits) 67 | #define SPISTATbits SPI1STATbits 68 | // Description: The enable bit for the SPI module 69 | #define SPIENABLE SPICON1bits.ON 70 | // Description: The definition for the SPI baud rate generator register (PIC32) 71 | #define SPIBRG SPI1BRG 72 | 73 | // Tris pins for SCK/SDI/SDO lines 74 | 75 | 76 | // changed by ASkr 77 | // the 795F512L does not have F6 and F7 78 | /* 79 | // Description: The TRIS bit for the SCK pin 80 | #define SPICLOCK TRISFbits.TRISF6 81 | // Description: The TRIS bit for the SDI pin 82 | #define SPIIN TRISFbits.TRISF7 83 | // Description: The TRIS bit for the SDO pin 84 | #define SPIOUT TRISFbits.TRISF8 85 | */ 86 | #define SPICLOCK TRISDbits.TRISD15 87 | #define SPIIN TRISFbits.TRISF2 88 | #define SPIOUT TRISFbits.TRISF8 89 | 90 | 91 | 92 | 93 | //SPI library functions 94 | #define putcSPI putcSPI1 95 | #define getcSPI getcSPI1 96 | #define OpenSPI(config1, config2) OpenSPI1(config1, config2) 97 | 98 | #elif defined MDD_USE_SPI_2 99 | // Description: SD-SPI Chip Select Output bit 100 | #define SD_CS LATBbits.LATB9 101 | // Description: SD-SPI Chip Select TRIS bit 102 | #define SD_CS_TRIS TRISBbits.TRISB9 103 | 104 | // Description: SD-SPI Card Detect Input bit 105 | #define SD_CD PORTGbits.RG0 106 | // Description: SD-SPI Card Detect TRIS bit 107 | #define SD_CD_TRIS TRISGbits.TRISG0 108 | 109 | // Description: SD-SPI Write Protect Check Input bit 110 | #define SD_WE PORTGbits.RG1 111 | // Description: SD-SPI Write Protect Check TRIS bit 112 | #define SD_WE_TRIS TRISGbits.TRISG1 113 | 114 | // Description: The main SPI control register 115 | #define SPICON1 SPI2CON 116 | // Description: The SPI status register 117 | #define SPISTAT SPI2STAT 118 | // Description: The SPI Buffer 119 | #define SPIBUF SPI2BUF 120 | // Description: The receive buffer full bit in the SPI status register 121 | #define SPISTAT_RBF SPI2STATbits.SPIRBF 122 | // Description: The bitwise define for the SPI control register (i.e. _____bits) 123 | #define SPICON1bits SPI2CONbits 124 | // Description: The bitwise define for the SPI status register (i.e. _____bits) 125 | #define SPISTATbits SPI2STATbits 126 | // Description: The enable bit for the SPI module 127 | #define SPIENABLE SPI2CONbits.ON 128 | // Description: The definition for the SPI baud rate generator register (PIC32) 129 | #define SPIBRG SPI2BRG 130 | 131 | // Tris pins for SCK/SDI/SDO lines 132 | 133 | // Description: The TRIS bit for the SCK pin 134 | #define SPICLOCK TRISGbits.TRISG6 135 | // Description: The TRIS bit for the SDI pin 136 | #define SPIIN TRISGbits.TRISG7 137 | // Description: The TRIS bit for the SDO pin 138 | #define SPIOUT TRISGbits.TRISG8 139 | //SPI library functions 140 | #define putcSPI putcSPI2 141 | #define getcSPI getcSPI2 142 | #define OpenSPI(config1, config2) OpenSPI2(config1, config2) 143 | #endif 144 | 145 | 146 | // Will generate an error if the clock speed is too low to interface to the card 147 | #if (GetSystemClock() < 100000) 148 | #error Clock speed must exceed 100 kHz 149 | #endif 150 | 151 | 152 | 153 | #endif 154 | -------------------------------------------------------------------------------- /src/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | #if defined(LUA_COMPAT_GETN) 19 | LUALIB_API int (luaL_getn) (lua_State *L, int t); 20 | LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); 21 | #else 22 | #define luaL_getn(L,i) ((int)lua_objlen(L, i)) 23 | #define luaL_setn(L,i,j) ((void)0) /* no op! */ 24 | #endif 25 | 26 | #if defined(LUA_COMPAT_OPENLIB) 27 | #define luaI_openlib luaL_openlib 28 | #endif 29 | 30 | 31 | /* extra error code for `luaL_load' */ 32 | #define LUA_ERRFILE (LUA_ERRERR+1) 33 | 34 | 35 | typedef struct luaL_Reg { 36 | const char *name; 37 | lua_CFunction func; 38 | } luaL_Reg; 39 | 40 | 41 | 42 | LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, 43 | const luaL_Reg *l, int nup); 44 | LUALIB_API void (luaL_register) (lua_State *L, const char *libname, 45 | const luaL_Reg *l); 46 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 47 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 48 | LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); 49 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 50 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 51 | size_t *l); 52 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 53 | const char *def, size_t *l); 54 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 55 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 56 | 57 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 58 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 59 | lua_Integer def); 60 | 61 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 62 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 63 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 64 | 65 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 66 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 67 | 68 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 69 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 70 | 71 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 72 | const char *const lst[]); 73 | 74 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 75 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 76 | 77 | LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); 78 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, 79 | const char *name); 80 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 81 | 82 | LUALIB_API lua_State *(luaL_newstate) (void); 83 | 84 | 85 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 86 | const char *r); 87 | 88 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, 89 | const char *fname, int szhint); 90 | 91 | 92 | 93 | 94 | /* 95 | ** =============================================================== 96 | ** some useful macros 97 | ** =============================================================== 98 | */ 99 | 100 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 101 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 102 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 103 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 104 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 105 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 106 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 107 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 108 | 109 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 110 | 111 | #define luaL_dofile(L, fn) \ 112 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 113 | 114 | #define luaL_dostring(L, s) \ 115 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 116 | 117 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 118 | 119 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 120 | 121 | /* 122 | ** {====================================================== 123 | ** Generic Buffer manipulation 124 | ** ======================================================= 125 | */ 126 | 127 | 128 | 129 | typedef struct luaL_Buffer { 130 | char *p; /* current position in buffer */ 131 | int lvl; /* number of strings in the stack (level) */ 132 | lua_State *L; 133 | char buffer[LUAL_BUFFERSIZE]; 134 | } luaL_Buffer; 135 | 136 | #define luaL_addchar(B,c) \ 137 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ 138 | (*(B)->p++ = (char)(c))) 139 | 140 | /* compatibility only */ 141 | #define luaL_putchar(B,c) luaL_addchar(B,c) 142 | 143 | #define luaL_addsize(B,n) ((B)->p += (n)) 144 | 145 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 146 | LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); 147 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 148 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 149 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 150 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 151 | 152 | 153 | /* }====================================================== */ 154 | 155 | 156 | /* compatibility with ref system */ 157 | 158 | /* pre-defined references */ 159 | #define LUA_NOREF (-2) 160 | #define LUA_REFNIL (-1) 161 | 162 | #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ 163 | (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) 164 | 165 | #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) 166 | 167 | #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) 168 | 169 | 170 | #define luaL_reg luaL_Reg 171 | 172 | #endif 173 | 174 | 175 | -------------------------------------------------------------------------------- /src/asCON.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | #include 6 | 7 | #include "asIO.h" 8 | 9 | //#include 10 | #include 11 | 12 | 13 | #ifndef __asCON 14 | #define __asCON 15 | #endif 16 | 17 | #include "asPIC.h" 18 | 19 | #include "asCON.h" 20 | #include "asDBG.h" 21 | 22 | 23 | 24 | 25 | 26 | // The dynamically handled buffer, with auto-size-adaption, 27 | // was a nice idea, but too slow. 28 | // Back to the easy stuff, for now... 29 | // May the future bring DMA :) 30 | char pRXBuf[RXBUFSIZE]; 31 | volatile unsigned pRXBufW=0; // write buffer index 32 | volatile unsigned pRXBufR=0; // read buffer index 33 | volatile unsigned pRXBufC=0; // number of bytes in buffer 34 | volatile unsigned pRXBufO=0; // overflow flag 35 | 36 | 37 | 38 | //************************************************************************************** 39 | //*** asCON__RXBufInit 40 | //*** 41 | //*** 42 | //************************************************************************************** 43 | inline void asCON_RXBufInit() 44 | { 45 | pRXBufW=0; 46 | pRXBufR=0; 47 | pRXBufC=0; 48 | } 49 | 50 | 51 | //************************************************************************************** 52 | //*** asCON__RXBufWriteChar 53 | //*** 54 | //*** Called from interrupt routine. 55 | //************************************************************************************** 56 | int asCON_RXBufWriteChar(char ch) 57 | { 58 | // check end of ringbuffer 59 | if( ++pRXBufW >= RXBUFSIZE ) 60 | pRXBufW=0; 61 | 62 | // check overflow 63 | if(pRXBufW == pRXBufR ) 64 | { 65 | asCON_RXBufInit(); 66 | pRXBufO=1; 67 | return -1; 68 | } 69 | 70 | pRXBuf[pRXBufW]=ch; 71 | pRXBufC++; 72 | 73 | return 1; 74 | } 75 | 76 | 77 | //************************************************************************************** 78 | //*** asCON__RXBufReadChar 79 | //*** 80 | //*** inline? well... ;) 81 | //************************************************************************************** 82 | inline int asCON_RXBufReadChar(int blocked) 83 | { 84 | if(blocked) 85 | { 86 | while (pRXBufW == pRXBufR) 87 | {;} 88 | } 89 | else 90 | { 91 | if(pRXBufW == pRXBufR) 92 | return RXBUFEMPTY; 93 | } 94 | 95 | if( ++pRXBufR >= RXBUFSIZE ) 96 | pRXBufR=0; 97 | 98 | // TOCHK! 99 | // The RX interrupt routine may interfere right here! 100 | if(pRXBufC>0) 101 | pRXBufC--; 102 | return pRXBuf[pRXBufR]; 103 | } 104 | 105 | 106 | //************************************************************************************** 107 | //*** asCON__RXBufFlushIn 108 | //*** 109 | //*** Flushes the RX input buffer. 110 | //*** There are faster ways, indeed, but this one is the safe method while the RX 111 | //*** interrupt is active... 112 | //************************************************************************************** 113 | inline void asCON_RXBufFlushIn() 114 | { 115 | while(asCON_RXBufReadChar(NONBLOCKING) >= 0) 116 | {;} 117 | pRXBufO=0; 118 | } 119 | 120 | 121 | 122 | //************************************************************************************** 123 | //*** asCON__RXBufCount 124 | //*** 125 | //*** 126 | //*** Returns the number of bytes in the receive buffer. 127 | // If an overflow occured, a '-1' will be returned. 128 | //************************************************************************************** 129 | inline unsigned asCON_RXBufCount() 130 | { 131 | if(pRXBufO) 132 | return -1; 133 | else 134 | return pRXBufC; 135 | } 136 | 137 | 138 | 139 | //************************************************************************************** 140 | //*** asCON_SerialWriteString 141 | //*** 142 | //*** Sends a string over UART2. 143 | //************************************************************************************** 144 | void asCON_SerialWriteString(char *ch) 145 | { 146 | if(ch==NULL) 147 | return; 148 | 149 | while(*ch!=0) 150 | asCON_SerialWriteChar(*ch++); 151 | } 152 | 153 | 154 | //************************************************************************************** 155 | //*** asCON_SerialFlush 156 | //*** 157 | //*** 158 | //************************************************************************************** 159 | inline void asCON_SerialFlush() 160 | { 161 | 162 | #ifndef CONSOLE_SWAP 163 | 164 | while(U2STAbits.UTXBF) 165 | {;} 166 | 167 | #else 168 | 169 | while(U1STAbits.UTXBF) 170 | {;} 171 | 172 | #endif 173 | } 174 | 175 | 176 | //************************************************************************************** 177 | //*** asCON_SerialWriteChar 178 | //*** 179 | //*** Sends a single character over UART2. 180 | //*** For every '\n', an additional '\r' is sent too. 181 | //*** (Possibly not the best solution...) 182 | //************************************************************************************** 183 | inline void asCON_SerialWriteChar(char ch) 184 | { 185 | asCON_SerialFlush(); 186 | 187 | #ifndef CONSOLE_SWAP 188 | 189 | U2TXREG = ch; 190 | if(ch == '\r') 191 | { 192 | asCON_SerialFlush(); 193 | U2TXREG = '\n'; 194 | } 195 | 196 | #else 197 | 198 | U1TXREG = ch; 199 | if(ch == '\r') 200 | { 201 | asCON_SerialFlush(); 202 | U1TXREG = '\n'; 203 | } 204 | 205 | #endif 206 | 207 | } 208 | 209 | 210 | //************************************************************************************** 211 | //*** asCON_SerialReadLine (fishy...) 212 | //*** 213 | //*** Reads a line from UART buffer. End is determined by '\r' ("ENTER" ;-) 214 | //************************************************************************************** 215 | unsigned int asCON_SerialReadLine(unsigned int maxdat,char *buffer, unsigned int uart_data_wait) 216 | { 217 | char *temp_ptr = (char *) buffer; 218 | int len=0; 219 | int i; 220 | 221 | 222 | // ASkr DEBUG 223 | // can be useful to understand how Lua works ;-) 224 | // sprintf(buffer,"print(\"kekse\")\r"); 225 | // sprintf(buffer,"io.write(\"kekse\")\r"); 226 | // return 1; 227 | 228 | while(len < maxdat) 229 | { 230 | *temp_ptr = asCON_RXBufReadChar(BLOCKING); 231 | 232 | // sucks, but works ;-) 233 | #ifdef USE_DIRTY_BACKSPACE 234 | 235 | if(*(temp_ptr) == KEY_BACKSPACE) 236 | { 237 | asCON_SerialWriteChar('\r'); 238 | temp_ptr--; 239 | *temp_ptr=0; 240 | if(len>0) 241 | len--; 242 | for(i=0;i 9 | 10 | #define lstate_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "llex.h" 20 | #include "lmem.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "ltable.h" 24 | #include "ltm.h" 25 | 26 | 27 | #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) 28 | #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) 29 | #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) 30 | 31 | 32 | /* 33 | ** Main thread combines a thread state and the global state 34 | */ 35 | typedef struct LG { 36 | lua_State l; 37 | global_State g; 38 | } LG; 39 | 40 | 41 | 42 | static void stack_init (lua_State *L1, lua_State *L) { 43 | /* initialize CallInfo array */ 44 | L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); 45 | L1->ci = L1->base_ci; 46 | L1->size_ci = BASIC_CI_SIZE; 47 | L1->end_ci = L1->base_ci + L1->size_ci - 1; 48 | /* initialize stack array */ 49 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); 50 | L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 51 | L1->top = L1->stack; 52 | L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; 53 | /* initialize first ci */ 54 | L1->ci->func = L1->top; 55 | setnilvalue(L1->top++); /* `function' entry for this `ci' */ 56 | L1->base = L1->ci->base = L1->top; 57 | L1->ci->top = L1->top + LUA_MINSTACK; 58 | } 59 | 60 | 61 | static void freestack (lua_State *L, lua_State *L1) { 62 | luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); 63 | luaM_freearray(L, L1->stack, L1->stacksize, TValue); 64 | } 65 | 66 | 67 | /* 68 | ** open parts that may cause memory-allocation errors 69 | */ 70 | static void f_luaopen (lua_State *L, void *ud) { 71 | global_State *g = G(L); 72 | UNUSED(ud); 73 | stack_init(L, L); /* init stack */ 74 | sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ 75 | sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ 76 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 77 | luaT_init(L); 78 | luaX_init(L); 79 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); 80 | g->GCthreshold = 4*g->totalbytes; 81 | } 82 | 83 | 84 | static void preinit_state (lua_State *L, global_State *g) { 85 | G(L) = g; 86 | L->stack = NULL; 87 | L->stacksize = 0; 88 | L->errorJmp = NULL; 89 | L->hook = NULL; 90 | L->hookmask = 0; 91 | L->basehookcount = 0; 92 | L->allowhook = 1; 93 | resethookcount(L); 94 | L->openupval = NULL; 95 | L->size_ci = 0; 96 | L->nCcalls = L->baseCcalls = 0; 97 | L->status = 0; 98 | L->base_ci = L->ci = NULL; 99 | L->savedpc = NULL; 100 | L->errfunc = 0; 101 | setnilvalue(gt(L)); 102 | } 103 | 104 | 105 | static void close_state (lua_State *L) { 106 | global_State *g = G(L); 107 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 108 | luaC_freeall(L); /* collect all objects */ 109 | lua_assert(g->rootgc == obj2gco(L)); 110 | lua_assert(g->strt.nuse == 0); 111 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); 112 | luaZ_freebuffer(L, &g->buff); 113 | freestack(L, L); 114 | lua_assert(g->totalbytes == sizeof(LG)); 115 | (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); 116 | } 117 | 118 | 119 | lua_State *luaE_newthread (lua_State *L) { 120 | lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); 121 | luaC_link(L, obj2gco(L1), LUA_TTHREAD); 122 | preinit_state(L1, G(L)); 123 | stack_init(L1, L); /* init stack */ 124 | setobj2n(L, gt(L1), gt(L)); /* share table of globals */ 125 | L1->hookmask = L->hookmask; 126 | L1->basehookcount = L->basehookcount; 127 | L1->hook = L->hook; 128 | resethookcount(L1); 129 | lua_assert(iswhite(obj2gco(L1))); 130 | return L1; 131 | } 132 | 133 | 134 | void luaE_freethread (lua_State *L, lua_State *L1) { 135 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 136 | lua_assert(L1->openupval == NULL); 137 | luai_userstatefree(L1); 138 | freestack(L, L1); 139 | luaM_freemem(L, fromstate(L1), state_size(lua_State)); 140 | } 141 | 142 | 143 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 144 | int i; 145 | lua_State *L; 146 | global_State *g; 147 | void *l = (*f)(ud, NULL, 0, state_size(LG)); 148 | if (l == NULL) return NULL; 149 | L = tostate(l); 150 | g = &((LG *)L)->g; 151 | L->next = NULL; 152 | L->tt = LUA_TTHREAD; 153 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 154 | L->marked = luaC_white(g); 155 | set2bits(L->marked, FIXEDBIT, SFIXEDBIT); 156 | preinit_state(L, g); 157 | g->frealloc = f; 158 | g->ud = ud; 159 | g->mainthread = L; 160 | g->uvhead.u.l.prev = &g->uvhead; 161 | g->uvhead.u.l.next = &g->uvhead; 162 | g->GCthreshold = 0; /* mark it as unfinished state */ 163 | g->strt.size = 0; 164 | g->strt.nuse = 0; 165 | g->strt.hash = NULL; 166 | setnilvalue(registry(L)); 167 | luaZ_initbuffer(L, &g->buff); 168 | g->panic = NULL; 169 | g->gcstate = GCSpause; 170 | g->rootgc = obj2gco(L); 171 | g->sweepstrgc = 0; 172 | g->sweepgc = &g->rootgc; 173 | g->gray = NULL; 174 | g->grayagain = NULL; 175 | g->weak = NULL; 176 | g->tmudata = NULL; 177 | g->totalbytes = sizeof(LG); 178 | g->gcpause = LUAI_GCPAUSE; 179 | g->gcstepmul = LUAI_GCMUL; 180 | g->gcdept = 0; 181 | for (i=0; imt[i] = NULL; 182 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { 183 | /* memory allocation error: free partial state */ 184 | close_state(L); 185 | L = NULL; 186 | } 187 | else 188 | luai_userstateopen(L); 189 | return L; 190 | } 191 | 192 | 193 | static void callallgcTM (lua_State *L, void *ud) { 194 | UNUSED(ud); 195 | luaC_callGCTM(L); /* call GC metamethods for all udata */ 196 | } 197 | 198 | 199 | LUA_API void lua_close (lua_State *L) { 200 | L = G(L)->mainthread; /* only the main thread can be closed */ 201 | lua_lock(L); 202 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 203 | luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ 204 | L->errfunc = 0; /* no error function during GC metamethods */ 205 | do { /* repeat until no more errors */ 206 | L->ci = L->base_ci; 207 | L->base = L->top = L->ci->base; 208 | L->nCcalls = L->baseCcalls = 0; 209 | } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); 210 | lua_assert(G(L)->tmudata == NULL); 211 | luai_userstateclose(L); 212 | close_state(L); 213 | } 214 | 215 | -------------------------------------------------------------------------------- /src/lobject.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Some generic functions over Lua objects 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define lobject_c 14 | #define LUA_CORE 15 | 16 | #include "lua.h" 17 | 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "lvm.h" 24 | 25 | 26 | 27 | const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; 28 | 29 | 30 | /* 31 | ** converts an integer to a "floating point byte", represented as 32 | ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 33 | ** eeeee != 0 and (xxx) otherwise. 34 | */ 35 | int luaO_int2fb (unsigned int x) { 36 | int e = 0; /* expoent */ 37 | while (x >= 16) { 38 | x = (x+1) >> 1; 39 | e++; 40 | } 41 | if (x < 8) return x; 42 | else return ((e+1) << 3) | (cast_int(x) - 8); 43 | } 44 | 45 | 46 | /* converts back */ 47 | int luaO_fb2int (int x) { 48 | int e = (x >> 3) & 31; 49 | if (e == 0) return x; 50 | else return ((x & 7)+8) << (e - 1); 51 | } 52 | 53 | 54 | int luaO_log2 (unsigned int x) { 55 | static const lu_byte log_2[256] = { 56 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 57 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 58 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 59 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 60 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 61 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 62 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 63 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 64 | }; 65 | int l = -1; 66 | while (x >= 256) { l += 8; x >>= 8; } 67 | return l + log_2[x]; 68 | 69 | } 70 | 71 | 72 | int luaO_rawequalObj (const TValue *t1, const TValue *t2) { 73 | if (ttype(t1) != ttype(t2)) return 0; 74 | else switch (ttype(t1)) { 75 | case LUA_TNIL: 76 | return 1; 77 | case LUA_TNUMBER: 78 | return luai_numeq(nvalue(t1), nvalue(t2)); 79 | case LUA_TBOOLEAN: 80 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ 81 | case LUA_TLIGHTUSERDATA: 82 | return pvalue(t1) == pvalue(t2); 83 | default: 84 | lua_assert(iscollectable(t1)); 85 | return gcvalue(t1) == gcvalue(t2); 86 | } 87 | } 88 | 89 | 90 | int luaO_str2d (const char *s, lua_Number *result) { 91 | char *endptr; 92 | *result = lua_str2number(s, &endptr); 93 | if (endptr == s) 94 | return 0; /* conversion failed */ 95 | 96 | if (*endptr == 'x' || *endptr == 'X') // hex 97 | *result = cast_num(strtoul(s, &endptr, 16)); 98 | 99 | // ASkr 100 | // we like to have binary numbers too... 101 | if (*endptr == 'b' || *endptr == 'B') // bin 102 | // only pass the rest of the string, behind 'b', to strtoul() 103 | *result = cast_num(strtoul(endptr+1, &endptr, 2)); 104 | 105 | if (*endptr == '\0') 106 | return 1; /* most common case */ 107 | 108 | while (isspace(cast(unsigned char, *endptr))) 109 | endptr++; 110 | 111 | if (*endptr != '\0') 112 | return 0; /* invalid trailing characters? */ 113 | 114 | return 1; 115 | } 116 | 117 | 118 | 119 | static void pushstr (lua_State *L, const char *str) { 120 | setsvalue2s(L, L->top, luaS_new(L, str)); 121 | incr_top(L); 122 | } 123 | 124 | 125 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 126 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 127 | int n = 1; 128 | pushstr(L, ""); 129 | for (;;) { 130 | const char *e = strchr(fmt, '%'); 131 | if (e == NULL) break; 132 | setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); 133 | incr_top(L); 134 | switch (*(e+1)) { 135 | case 's': { 136 | const char *s = va_arg(argp, char *); 137 | if (s == NULL) s = "(null)"; 138 | pushstr(L, s); 139 | break; 140 | } 141 | case 'c': { 142 | char buff[2]; 143 | buff[0] = cast(char, va_arg(argp, int)); 144 | buff[1] = '\0'; 145 | pushstr(L, buff); 146 | break; 147 | } 148 | case 'd': { 149 | setnvalue(L->top, cast_num(va_arg(argp, int))); 150 | incr_top(L); 151 | break; 152 | } 153 | case 'f': { 154 | setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 155 | incr_top(L); 156 | break; 157 | } 158 | case 'p': { 159 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ 160 | sprintf(buff, "%p", va_arg(argp, void *)); 161 | pushstr(L, buff); 162 | break; 163 | } 164 | case '%': { 165 | pushstr(L, "%"); 166 | break; 167 | } 168 | default: { 169 | char buff[3]; 170 | buff[0] = '%'; 171 | buff[1] = *(e+1); 172 | buff[2] = '\0'; 173 | pushstr(L, buff); 174 | break; 175 | } 176 | } 177 | n += 2; 178 | fmt = e+2; 179 | } 180 | pushstr(L, fmt); 181 | luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); 182 | L->top -= n; 183 | return svalue(L->top - 1); 184 | } 185 | 186 | 187 | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 188 | const char *msg; 189 | va_list argp; 190 | va_start(argp, fmt); 191 | msg = luaO_pushvfstring(L, fmt, argp); 192 | va_end(argp); 193 | return msg; 194 | } 195 | 196 | 197 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { 198 | if (*source == '=') { 199 | strncpy(out, source+1, bufflen); /* remove first char */ 200 | out[bufflen-1] = '\0'; /* ensures null termination */ 201 | } 202 | else { /* out = "source", or "...source" */ 203 | if (*source == '@') { 204 | size_t l; 205 | source++; /* skip the `@' */ 206 | bufflen -= sizeof(" '...' "); 207 | l = strlen(source); 208 | strcpy(out, ""); 209 | if (l > bufflen) { 210 | source += (l-bufflen); /* get last part of file name */ 211 | strcat(out, "..."); 212 | } 213 | strcat(out, source); 214 | } 215 | else { /* out = [string "string"] */ 216 | size_t len = strcspn(source, "\n\r"); /* stop at first newline */ 217 | bufflen -= sizeof(" [string \"...\"] "); 218 | if (len > bufflen) len = bufflen; 219 | strcpy(out, "[string \""); 220 | if (source[len] != '\0') { /* must truncate? */ 221 | strncat(out, source, len); 222 | strcat(out, "..."); 223 | } 224 | else 225 | strcat(out, source); 226 | strcat(out, "\"]"); 227 | } 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /src/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Standard mathematical library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | 11 | #define lmathlib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | #undef PI 21 | #define PI (3.14159265358979323846) 22 | #define RADIANS_PER_DEGREE (PI/180.0) 23 | 24 | 25 | 26 | static int math_abs (lua_State *L) { 27 | lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); 28 | return 1; 29 | } 30 | 31 | static int math_sin (lua_State *L) { 32 | lua_pushnumber(L, sin(luaL_checknumber(L, 1))); 33 | return 1; 34 | } 35 | 36 | static int math_sinh (lua_State *L) { 37 | lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); 38 | return 1; 39 | } 40 | 41 | static int math_cos (lua_State *L) { 42 | lua_pushnumber(L, cos(luaL_checknumber(L, 1))); 43 | return 1; 44 | } 45 | 46 | static int math_cosh (lua_State *L) { 47 | lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); 48 | return 1; 49 | } 50 | 51 | static int math_tan (lua_State *L) { 52 | lua_pushnumber(L, tan(luaL_checknumber(L, 1))); 53 | return 1; 54 | } 55 | 56 | static int math_tanh (lua_State *L) { 57 | lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); 58 | return 1; 59 | } 60 | 61 | static int math_asin (lua_State *L) { 62 | lua_pushnumber(L, asin(luaL_checknumber(L, 1))); 63 | return 1; 64 | } 65 | 66 | static int math_acos (lua_State *L) { 67 | lua_pushnumber(L, acos(luaL_checknumber(L, 1))); 68 | return 1; 69 | } 70 | 71 | static int math_atan (lua_State *L) { 72 | lua_pushnumber(L, atan(luaL_checknumber(L, 1))); 73 | return 1; 74 | } 75 | 76 | static int math_atan2 (lua_State *L) { 77 | lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 78 | return 1; 79 | } 80 | 81 | static int math_ceil (lua_State *L) { 82 | lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); 83 | return 1; 84 | } 85 | 86 | static int math_floor (lua_State *L) { 87 | lua_pushnumber(L, floor(luaL_checknumber(L, 1))); 88 | return 1; 89 | } 90 | 91 | static int math_fmod (lua_State *L) { 92 | lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 93 | return 1; 94 | } 95 | 96 | static int math_modf (lua_State *L) { 97 | double ip; 98 | double fp = modf(luaL_checknumber(L, 1), &ip); 99 | lua_pushnumber(L, ip); 100 | lua_pushnumber(L, fp); 101 | return 2; 102 | } 103 | 104 | static int math_sqrt (lua_State *L) { 105 | lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 106 | return 1; 107 | } 108 | 109 | static int math_pow (lua_State *L) { 110 | lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 111 | return 1; 112 | } 113 | 114 | static int math_log (lua_State *L) { 115 | lua_pushnumber(L, log(luaL_checknumber(L, 1))); 116 | return 1; 117 | } 118 | 119 | static int math_log10 (lua_State *L) { 120 | lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 121 | return 1; 122 | } 123 | 124 | static int math_exp (lua_State *L) { 125 | lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 126 | return 1; 127 | } 128 | 129 | static int math_deg (lua_State *L) { 130 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 131 | return 1; 132 | } 133 | 134 | static int math_rad (lua_State *L) { 135 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 136 | return 1; 137 | } 138 | 139 | static int math_frexp (lua_State *L) { 140 | int e; 141 | lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 142 | lua_pushinteger(L, e); 143 | return 2; 144 | } 145 | 146 | static int math_ldexp (lua_State *L) { 147 | lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 148 | return 1; 149 | } 150 | 151 | 152 | 153 | static int math_min (lua_State *L) { 154 | int n = lua_gettop(L); /* number of arguments */ 155 | lua_Number dmin = luaL_checknumber(L, 1); 156 | int i; 157 | for (i=2; i<=n; i++) { 158 | lua_Number d = luaL_checknumber(L, i); 159 | if (d < dmin) 160 | dmin = d; 161 | } 162 | lua_pushnumber(L, dmin); 163 | return 1; 164 | } 165 | 166 | 167 | static int math_max (lua_State *L) { 168 | int n = lua_gettop(L); /* number of arguments */ 169 | lua_Number dmax = luaL_checknumber(L, 1); 170 | int i; 171 | for (i=2; i<=n; i++) { 172 | lua_Number d = luaL_checknumber(L, i); 173 | if (d > dmax) 174 | dmax = d; 175 | } 176 | lua_pushnumber(L, dmax); 177 | return 1; 178 | } 179 | 180 | 181 | static int math_random (lua_State *L) { 182 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 183 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 184 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 185 | switch (lua_gettop(L)) { /* check number of arguments */ 186 | case 0: { /* no arguments */ 187 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 188 | break; 189 | } 190 | case 1: { /* only upper limit */ 191 | int u = luaL_checkint(L, 1); 192 | luaL_argcheck(L, 1<=u, 1, "interval is empty"); 193 | lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ 194 | break; 195 | } 196 | case 2: { /* lower and upper limits */ 197 | int l = luaL_checkint(L, 1); 198 | int u = luaL_checkint(L, 2); 199 | luaL_argcheck(L, l<=u, 2, "interval is empty"); 200 | lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ 201 | break; 202 | } 203 | default: return luaL_error(L, "wrong number of arguments"); 204 | } 205 | return 1; 206 | } 207 | 208 | 209 | static int math_randomseed (lua_State *L) { 210 | srand(luaL_checkint(L, 1)); 211 | return 0; 212 | } 213 | 214 | 215 | static const luaL_Reg mathlib[] = { 216 | {"abs", math_abs}, 217 | {"acos", math_acos}, 218 | {"asin", math_asin}, 219 | {"atan2", math_atan2}, 220 | {"atan", math_atan}, 221 | {"ceil", math_ceil}, 222 | {"cosh", math_cosh}, 223 | {"cos", math_cos}, 224 | {"deg", math_deg}, 225 | {"exp", math_exp}, 226 | {"floor", math_floor}, 227 | {"fmod", math_fmod}, 228 | {"frexp", math_frexp}, 229 | {"ldexp", math_ldexp}, 230 | {"log10", math_log10}, 231 | {"log", math_log}, 232 | {"max", math_max}, 233 | {"min", math_min}, 234 | {"modf", math_modf}, 235 | {"pow", math_pow}, 236 | {"rad", math_rad}, 237 | {"random", math_random}, 238 | {"randomseed", math_randomseed}, 239 | {"sinh", math_sinh}, 240 | {"sin", math_sin}, 241 | {"sqrt", math_sqrt}, 242 | {"tanh", math_tanh}, 243 | {"tan", math_tan}, 244 | {NULL, NULL} 245 | }; 246 | 247 | 248 | /* 249 | ** Open math library 250 | */ 251 | LUALIB_API int luaopen_math (lua_State *L) { 252 | luaL_register(L, LUA_MATHLIBNAME, mathlib); 253 | lua_pushnumber(L, PI); 254 | lua_setfield(L, -2, "pi"); 255 | lua_pushnumber(L, HUGE_VAL); 256 | lua_setfield(L, -2, "huge"); 257 | #if defined(LUA_COMPAT_MOD) 258 | lua_getfield(L, -1, "fmod"); 259 | lua_setfield(L, -2, "mod"); 260 | #endif 261 | return 1; 262 | } 263 | 264 | -------------------------------------------------------------------------------- /src/loslib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 3 | ** Standard Operating System library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define loslib_c 15 | #define LUA_LIB 16 | 17 | #include "lua.h" 18 | 19 | #include "lauxlib.h" 20 | #include "lualib.h" 21 | 22 | 23 | 24 | static int os_pushresult (lua_State *L, int i, const char *filename) { 25 | int en = errno; /* calls to Lua API may change this value */ 26 | if (i) { 27 | lua_pushboolean(L, 1); 28 | return 1; 29 | } 30 | else { 31 | lua_pushnil(L); 32 | lua_pushfstring(L, "%s: %s", filename, strerror(en)); 33 | lua_pushinteger(L, en); 34 | return 3; 35 | } 36 | } 37 | 38 | 39 | static int os_execute (lua_State *L) { 40 | lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); 41 | return 1; 42 | } 43 | 44 | 45 | static int os_remove (lua_State *L) { 46 | const char *filename = luaL_checkstring(L, 1); 47 | return os_pushresult(L, remove(filename) == 0, filename); 48 | } 49 | 50 | 51 | static int os_rename (lua_State *L) { 52 | const char *fromname = luaL_checkstring(L, 1); 53 | const char *toname = luaL_checkstring(L, 2); 54 | return os_pushresult(L, rename(fromname, toname) == 0, fromname); 55 | } 56 | 57 | 58 | static int os_tmpname (lua_State *L) { 59 | char buff[LUA_TMPNAMBUFSIZE]; 60 | int err; 61 | lua_tmpnam(buff, err); 62 | if (err) 63 | return luaL_error(L, "unable to generate a unique filename"); 64 | lua_pushstring(L, buff); 65 | return 1; 66 | } 67 | 68 | 69 | static int os_getenv (lua_State *L) { 70 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 71 | return 1; 72 | } 73 | 74 | 75 | static int os_clock (lua_State *L) { 76 | lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); 77 | return 1; 78 | } 79 | 80 | 81 | /* 82 | ** {====================================================== 83 | ** Time/Date operations 84 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 85 | ** wday=%w+1, yday=%j, isdst=? } 86 | ** ======================================================= 87 | */ 88 | 89 | static void setfield (lua_State *L, const char *key, int value) { 90 | lua_pushinteger(L, value); 91 | lua_setfield(L, -2, key); 92 | } 93 | 94 | static void setboolfield (lua_State *L, const char *key, int value) { 95 | if (value < 0) /* undefined? */ 96 | return; /* does not set field */ 97 | lua_pushboolean(L, value); 98 | lua_setfield(L, -2, key); 99 | } 100 | 101 | static int getboolfield (lua_State *L, const char *key) { 102 | int res; 103 | lua_getfield(L, -1, key); 104 | res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 105 | lua_pop(L, 1); 106 | return res; 107 | } 108 | 109 | 110 | static int getfield (lua_State *L, const char *key, int d) { 111 | int res; 112 | lua_getfield(L, -1, key); 113 | if (lua_isnumber(L, -1)) 114 | res = (int)lua_tointeger(L, -1); 115 | else { 116 | if (d < 0) 117 | return luaL_error(L, "field " LUA_QS " missing in date table", key); 118 | res = d; 119 | } 120 | lua_pop(L, 1); 121 | return res; 122 | } 123 | 124 | 125 | static int os_date (lua_State *L) { 126 | const char *s = luaL_optstring(L, 1, "%c"); 127 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 128 | struct tm *stm; 129 | if (*s == '!') { /* UTC? */ 130 | stm = gmtime(&t); 131 | s++; /* skip `!' */ 132 | } 133 | else 134 | stm = localtime(&t); 135 | if (stm == NULL) /* invalid date? */ 136 | lua_pushnil(L); 137 | else if (strcmp(s, "*t") == 0) { 138 | lua_createtable(L, 0, 9); /* 9 = number of fields */ 139 | setfield(L, "sec", stm->tm_sec); 140 | setfield(L, "min", stm->tm_min); 141 | setfield(L, "hour", stm->tm_hour); 142 | setfield(L, "day", stm->tm_mday); 143 | setfield(L, "month", stm->tm_mon+1); 144 | setfield(L, "year", stm->tm_year+1900); 145 | setfield(L, "wday", stm->tm_wday+1); 146 | setfield(L, "yday", stm->tm_yday+1); 147 | setboolfield(L, "isdst", stm->tm_isdst); 148 | } 149 | else { 150 | char cc[3]; 151 | luaL_Buffer b; 152 | cc[0] = '%'; cc[2] = '\0'; 153 | luaL_buffinit(L, &b); 154 | for (; *s; s++) { 155 | if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 156 | luaL_addchar(&b, *s); 157 | else { 158 | size_t reslen; 159 | char buff[200]; /* should be big enough for any conversion result */ 160 | cc[1] = *(++s); 161 | reslen = strftime(buff, sizeof(buff), cc, stm); 162 | luaL_addlstring(&b, buff, reslen); 163 | } 164 | } 165 | luaL_pushresult(&b); 166 | } 167 | return 1; 168 | } 169 | 170 | 171 | static int os_time (lua_State *L) { 172 | time_t t; 173 | if (lua_isnoneornil(L, 1)) /* called without args? */ 174 | t = time(NULL); /* get current time */ 175 | else { 176 | struct tm ts; 177 | luaL_checktype(L, 1, LUA_TTABLE); 178 | lua_settop(L, 1); /* make sure table is at the top */ 179 | ts.tm_sec = getfield(L, "sec", 0); 180 | ts.tm_min = getfield(L, "min", 0); 181 | ts.tm_hour = getfield(L, "hour", 12); 182 | ts.tm_mday = getfield(L, "day", -1); 183 | ts.tm_mon = getfield(L, "month", -1) - 1; 184 | ts.tm_year = getfield(L, "year", -1) - 1900; 185 | ts.tm_isdst = getboolfield(L, "isdst"); 186 | t = mktime(&ts); 187 | } 188 | if (t == (time_t)(-1)) 189 | lua_pushnil(L); 190 | else 191 | lua_pushnumber(L, (lua_Number)t); 192 | return 1; 193 | } 194 | 195 | 196 | static int os_difftime (lua_State *L) { 197 | lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 198 | (time_t)(luaL_optnumber(L, 2, 0)))); 199 | return 1; 200 | } 201 | 202 | /* }====================================================== */ 203 | 204 | 205 | static int os_setlocale (lua_State *L) { 206 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 207 | LC_NUMERIC, LC_TIME}; 208 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 209 | "numeric", "time", NULL}; 210 | const char *l = luaL_optstring(L, 1, NULL); 211 | int op = luaL_checkoption(L, 2, "all", catnames); 212 | lua_pushstring(L, setlocale(cat[op], l)); 213 | return 1; 214 | } 215 | 216 | 217 | static int os_exit (lua_State *L) { 218 | exit(luaL_optint(L, 1, EXIT_SUCCESS)); 219 | } 220 | 221 | static const luaL_Reg syslib[] = { 222 | {"clock", os_clock}, 223 | {"date", os_date}, 224 | {"difftime", os_difftime}, 225 | {"execute", os_execute}, 226 | {"exit", os_exit}, 227 | {"getenv", os_getenv}, 228 | {"remove", os_remove}, 229 | {"rename", os_rename}, 230 | {"setlocale", os_setlocale}, 231 | {"time", os_time}, 232 | {"tmpname", os_tmpname}, 233 | {NULL, NULL} 234 | }; 235 | 236 | /* }====================================================== */ 237 | 238 | 239 | 240 | LUALIB_API int luaopen_os (lua_State *L) { 241 | luaL_register(L, LUA_OSLIBNAME, syslib); 242 | return 1; 243 | } 244 | 245 | -------------------------------------------------------------------------------- /PIC32Lua.mcp: -------------------------------------------------------------------------------- 1 | [HEADER] 2 | magic_cookie={66E99B07-E706-4689-9E80-9B2582898A13} 3 | file_version=1.0 4 | device=PIC32MX795F512L 5 | [PATH_INFO] 6 | BuildDirPolicy=BuildDirIsProjectDir 7 | dir_src= 8 | dir_bin=out 9 | dir_tmp=obj 10 | dir_sin= 11 | dir_inc= 12 | dir_lib=C:\WINDOWS.PRG\MPLab850\MPLAB C32 Suite\lib;C:\WINDOWS.PRG\MPLab850\MPLAB C32 Suite\pic32mx\lib 13 | dir_lkr= 14 | [CAT_FILTERS] 15 | filter_src=*.s;*.c 16 | filter_inc=*.h;*.inc 17 | filter_obj=*.o 18 | filter_lib=*.a 19 | filter_lkr=*.ld 20 | [CAT_SUBFOLDERS] 21 | subfolder_src= 22 | subfolder_inc= 23 | subfolder_obj= 24 | subfolder_lib= 25 | subfolder_lkr= 26 | [FILE_SUBFOLDERS] 27 | file_000=. 28 | file_001=. 29 | file_002=. 30 | file_003=. 31 | file_004=. 32 | file_005=. 33 | file_006=. 34 | file_007=. 35 | file_008=. 36 | file_009=. 37 | file_010=. 38 | file_011=. 39 | file_012=. 40 | file_013=. 41 | file_014=. 42 | file_015=. 43 | file_016=. 44 | file_017=. 45 | file_018=. 46 | file_019=. 47 | file_020=. 48 | file_021=. 49 | file_022=. 50 | file_023=. 51 | file_024=. 52 | file_025=. 53 | file_026=. 54 | file_027=. 55 | file_028=. 56 | file_029=. 57 | file_030=. 58 | file_031=. 59 | file_032=. 60 | file_033=. 61 | file_034=. 62 | file_035=. 63 | file_036=. 64 | file_037=. 65 | file_038=. 66 | file_039=. 67 | file_040=. 68 | file_041=. 69 | file_042=. 70 | file_043=. 71 | file_044=. 72 | file_045=. 73 | file_046=. 74 | file_047=. 75 | file_048=. 76 | file_049=. 77 | file_050=. 78 | file_051=. 79 | file_052=. 80 | file_053=. 81 | file_054=. 82 | file_055=. 83 | file_056=. 84 | file_057=. 85 | file_058=. 86 | file_059=. 87 | file_060=. 88 | file_061=. 89 | file_062=. 90 | file_063=. 91 | file_064=. 92 | file_065=. 93 | file_066=. 94 | file_067=. 95 | file_068=. 96 | file_069=. 97 | file_070=. 98 | file_071=. 99 | file_072=. 100 | file_073=. 101 | file_074=. 102 | file_075=. 103 | file_076=. 104 | file_077=. 105 | [GENERATED_FILES] 106 | file_000=no 107 | file_001=no 108 | file_002=no 109 | file_003=no 110 | file_004=no 111 | file_005=no 112 | file_006=no 113 | file_007=no 114 | file_008=no 115 | file_009=no 116 | file_010=no 117 | file_011=no 118 | file_012=no 119 | file_013=no 120 | file_014=no 121 | file_015=no 122 | file_016=no 123 | file_017=no 124 | file_018=no 125 | file_019=no 126 | file_020=no 127 | file_021=no 128 | file_022=no 129 | file_023=no 130 | file_024=no 131 | file_025=no 132 | file_026=no 133 | file_027=no 134 | file_028=no 135 | file_029=no 136 | file_030=no 137 | file_031=no 138 | file_032=no 139 | file_033=no 140 | file_034=no 141 | file_035=no 142 | file_036=no 143 | file_037=no 144 | file_038=no 145 | file_039=no 146 | file_040=no 147 | file_041=no 148 | file_042=no 149 | file_043=no 150 | file_044=no 151 | file_045=no 152 | file_046=no 153 | file_047=no 154 | file_048=no 155 | file_049=no 156 | file_050=no 157 | file_051=no 158 | file_052=no 159 | file_053=no 160 | file_054=no 161 | file_055=no 162 | file_056=no 163 | file_057=no 164 | file_058=no 165 | file_059=no 166 | file_060=no 167 | file_061=no 168 | file_062=no 169 | file_063=no 170 | file_064=no 171 | file_065=no 172 | file_066=no 173 | file_067=no 174 | file_068=no 175 | file_069=no 176 | file_070=no 177 | file_071=no 178 | file_072=no 179 | file_073=no 180 | file_074=no 181 | file_075=no 182 | file_076=no 183 | file_077=no 184 | [OTHER_FILES] 185 | file_000=no 186 | file_001=no 187 | file_002=no 188 | file_003=no 189 | file_004=no 190 | file_005=no 191 | file_006=no 192 | file_007=no 193 | file_008=no 194 | file_009=no 195 | file_010=no 196 | file_011=no 197 | file_012=no 198 | file_013=no 199 | file_014=no 200 | file_015=no 201 | file_016=no 202 | file_017=no 203 | file_018=no 204 | file_019=no 205 | file_020=no 206 | file_021=no 207 | file_022=no 208 | file_023=no 209 | file_024=no 210 | file_025=no 211 | file_026=no 212 | file_027=no 213 | file_028=no 214 | file_029=no 215 | file_030=no 216 | file_031=no 217 | file_032=no 218 | file_033=no 219 | file_034=no 220 | file_035=no 221 | file_036=no 222 | file_037=no 223 | file_038=no 224 | file_039=no 225 | file_040=no 226 | file_041=no 227 | file_042=no 228 | file_043=no 229 | file_044=no 230 | file_045=no 231 | file_046=no 232 | file_047=no 233 | file_048=no 234 | file_049=no 235 | file_050=no 236 | file_051=no 237 | file_052=no 238 | file_053=no 239 | file_054=no 240 | file_055=no 241 | file_056=no 242 | file_057=no 243 | file_058=no 244 | file_059=no 245 | file_060=no 246 | file_061=no 247 | file_062=no 248 | file_063=no 249 | file_064=no 250 | file_065=no 251 | file_066=no 252 | file_067=no 253 | file_068=no 254 | file_069=no 255 | file_070=no 256 | file_071=no 257 | file_072=no 258 | file_073=no 259 | file_074=no 260 | file_075=no 261 | file_076=yes 262 | file_077=yes 263 | [FILE_INFO] 264 | file_000=src\lapi.c 265 | file_001=src\lauxlib.c 266 | file_002=src\lbaselib.c 267 | file_003=src\lcode.c 268 | file_004=src\ldblib.c 269 | file_005=src\ldebug.c 270 | file_006=src\ldo.c 271 | file_007=src\ldump.c 272 | file_008=src\lfunc.c 273 | file_009=src\lgc.c 274 | file_010=src\linit.c 275 | file_011=src\liolib.c 276 | file_012=src\llex.c 277 | file_013=src\lmathlib.c 278 | file_014=src\lmem.c 279 | file_015=src\loadlib.c 280 | file_016=src\lobject.c 281 | file_017=src\lopcodes.c 282 | file_018=src\loslib.c 283 | file_019=src\lparser.c 284 | file_020=src\lstate.c 285 | file_021=src\lstring.c 286 | file_022=src\lstrlib.c 287 | file_023=src\ltable.c 288 | file_024=src\ltablib.c 289 | file_025=src\ltm.c 290 | file_026=src\lua.c 291 | file_027=src\lundump.c 292 | file_028=src\lvm.c 293 | file_029=src\lzio.c 294 | file_030=src\asPIC.c 295 | file_031=src\asIO.c 296 | file_032=src\asDBG.c 297 | file_033=src\asINT.c 298 | file_034=src\asCON.c 299 | file_035=src\asLUA_PIC.c 300 | file_036=src\asUART1.c 301 | file_037=src\SD-SPI.c 302 | file_038=src\FSIO.c 303 | file_039=src\asCARD.c 304 | file_040=src\lzio.h 305 | file_041=src\lapi.h 306 | file_042=src\lauxlib.h 307 | file_043=src\lcode.h 308 | file_044=src\ldebug.h 309 | file_045=src\ldo.h 310 | file_046=src\lfunc.h 311 | file_047=src\lgc.h 312 | file_048=src\llex.h 313 | file_049=src\llimits.h 314 | file_050=src\lmem.h 315 | file_051=src\lobject.h 316 | file_052=src\lopcodes.h 317 | file_053=src\lparser.h 318 | file_054=src\lstate.h 319 | file_055=src\lstring.h 320 | file_056=src\ltable.h 321 | file_057=src\ltm.h 322 | file_058=src\lua.h 323 | file_059=src\luaconf.h 324 | file_060=src\lualib.h 325 | file_061=src\lundump.h 326 | file_062=src\lvm.h 327 | file_063=src\asPIC.h 328 | file_064=src\asDBG.h 329 | file_065=src\asINT.h 330 | file_066=src\asCON.h 331 | file_067=src\asIO.h 332 | file_068=src\asLUA_PIC.h 333 | file_069=src\SD-SPI.h 334 | file_070=src\Compiler.h 335 | file_071=src\FSconfig.h 336 | file_072=src\FSDefs.h 337 | file_073=src\FSIO.h 338 | file_074=src\HardwareProfile.h 339 | file_075=src\asCARD.h 340 | file_076=appslua\001_loop.lua 341 | file_077=P:\Programs\MPLab\PIC32-USBTest\asUSB01.map 342 | [SUITE_INFO] 343 | suite_guid={14495C23-81F8-43F3-8A44-859C583D7760} 344 | suite_state= 345 | [TOOL_SETTINGS] 346 | TS{CB0AF4B8-4022-429D-8F99-8A56782B2C6D}= 347 | TS{9C698E0A-CBC9-4EFF-AE7D-B569F93E7322}=-Wall -O2 -legacy-libc 348 | TS{9C698E0A-CBC9-4EFF-AE7D-B569F93E7322}_alt=yes 349 | TS{77F59DA1-3C53-4677-AC5F-A03EB0125170}=--defsym=_min_heap_size=121000 --defsym=_min_stack_size=2048 -Map="$(BINDIR_)$(TARGETBASE).map" -s -o"$(BINDIR_)$(TARGETBASE).$(TARGETSUFFIX)" -O2 -legacy-libc 350 | TS{77F59DA1-3C53-4677-AC5F-A03EB0125170}_alt=yes 351 | TS{0396C0A1-9052-4E4F-8B84-EF0162B1B4E9}= 352 | [INSTRUMENTED_TRACE] 353 | enable=0 354 | transport=0 355 | format=0 356 | [CUSTOM_BUILD] 357 | Pre-Build= 358 | Pre-BuildEnabled=1 359 | Post-Build= 360 | Post-BuildEnabled=1 361 | -------------------------------------------------------------------------------- /src/asPIC.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | //-------------------------------------------------------------------------------------- 6 | // DEFINE SYSTEM AND PERIPHERAL FREQUENCY 7 | // Note #1: This is used to determine or measure "time" 8 | // (e.g.: UART speed, timers, ...) 9 | // Note #2: This can NOT be used to change peripheral clock frequency! 10 | // Additionally, changing these values might require changing 11 | // some configuration bits or other stuff... 12 | #define SYS_FREQ 80000000L 13 | #define PER_FREQ 40000000L 14 | 15 | 16 | //-------------------------------------------------------------------------------------- 17 | // DEFINE CONSOLE BAUDRATE AND PORT (UART) 18 | // Note #1: By default, the console uses UART2 and the PIC32Lua configurable 19 | // serial interface is routed to UART1. 20 | // This can be changed by setting CONSOLE_SWAP 21 | // console -> UART1 22 | // PIC32Lua-Serial -> UART2 23 | // and is the default setting for the Chipkit-Max32. 24 | #define CONSOLE_BAUD 115200 25 | #if defined(CHIPKITMAX32) 26 | // set Chipkit-Max32 behaviour 27 | #define CONSOLE_SWAP 28 | #else 29 | // set behaviour on other hardware 30 | // #define CONSOLE_SWAP 31 | #endif 32 | 33 | 34 | //-------------------------------------------------------------------------------------- 35 | // DEFINE AVAILABLE TRISx REGISTERS AND PERMISSIONS (FROM WITHIN LUA) 36 | // undefine any TRIS registers that should not be available from within Lua 37 | // (or do not exist for your processor) 38 | #define PIC_MAXPORTS 8 39 | #define PIC_UNUSED NULL // <- do not change this 40 | 41 | #define PIC_TRISA (unsigned long)&TRISA 42 | #define PIC_TRISB (unsigned long)&TRISB 43 | #define PIC_TRISC (unsigned long)&TRISC 44 | #define PIC_TRISD (unsigned long)&TRISD 45 | #define PIC_TRISE (unsigned long)&TRISE 46 | #define PIC_TRISF (unsigned long)&TRISF 47 | #define PIC_TRISG (unsigned long)PIC_UNUSED 48 | #define PIC_TRISH (unsigned long)PIC_UNUSED 49 | 50 | //static const unsigned long PIC_TRISx[PIC_MAXPORTS]={PIC_TRISA, PIC_TRISB, PIC_TRISC, PIC_TRISD, PIC_TRISE, PIC_TRISF, PIC_TRISG, PIC_TRISH}; 51 | 52 | //-------------------------------------------------------------------------------------- 53 | // DEFINE AVAILABLE PORTx REGISTERS AND PERMISSIONS (FROM WITHIN LUA) 54 | #define PIC_PORTA (unsigned long)&PORTA 55 | #define PIC_PORTB (unsigned long)&PORTB 56 | #define PIC_PORTC (unsigned long)&PORTC 57 | #define PIC_PORTD (unsigned long)&PORTD 58 | #define PIC_PORTE (unsigned long)&PORTE 59 | #define PIC_PORTF (unsigned long)&PORTF 60 | #define PIC_PORTG (unsigned long)PIC_UNUSED 61 | #define PIC_PORTH (unsigned long)PIC_UNUSED 62 | 63 | //static const unsigned long PIC_PORTx[PIC_MAXPORTS]={PIC_PORTA, PIC_PORTB, PIC_PORTC, PIC_PORTD, PIC_PORTE, PIC_PORTF, PIC_PORTG, PIC_PORTH}; 64 | 65 | //-------------------------------------------------------------------------------------- 66 | // DEFINE AVAILABLE LATx REGISTERS AND PERMISSIONS (FROM WITHIN LUA) 67 | #define PIC_LATA (unsigned long)&LATA 68 | #define PIC_LATB (unsigned long)&LATB 69 | #define PIC_LATC (unsigned long)&LATC 70 | #define PIC_LATD (unsigned long)&LATD 71 | #define PIC_LATE (unsigned long)&LATE 72 | #define PIC_LATF (unsigned long)&LATF 73 | #define PIC_LATG (unsigned long)PIC_UNUSED 74 | #define PIC_LATH (unsigned long)PIC_UNUSED 75 | 76 | //static const unsigned long PIC_LATx[PIC_MAXPORTS]={PIC_LATA, PIC_LATB, PIC_LATC, PIC_LATD, PIC_LATE, PIC_LATF, PIC_LATG, PIC_LATH}; 77 | 78 | //-------------------------------------------------------------------------------------- 79 | // DEFINE AVAILABLE LATxSET REGISTERS AND PERMISSIONS (FROM WITHIN LUA) 80 | #define PIC_LATASET (unsigned long)&LATASET 81 | #define PIC_LATBSET (unsigned long)&LATBSET 82 | #define PIC_LATCSET (unsigned long)&LATCSET 83 | #define PIC_LATDSET (unsigned long)&LATDSET 84 | #define PIC_LATESET (unsigned long)&LATESET 85 | #define PIC_LATFSET (unsigned long)&LATFSET 86 | #define PIC_LATGSET (unsigned long)PIC_UNUSED 87 | #define PIC_LATHSET (unsigned long)PIC_UNUSED 88 | 89 | //static const unsigned long PIC_LATxSET[PIC_MAXPORTS]={PIC_LATASET, PIC_LATBSET, PIC_LATCSET, PIC_LATDSET, PIC_LATESET, PIC_LATFSET, PIC_LATGSET, PIC_LATHSET}; 90 | 91 | //-------------------------------------------------------------------------------------- 92 | // DEFINE AVAILABLE LATxCLR REGISTERS AND PERMISSIONS (FROM WITHIN LUA) 93 | #define PIC_LATACLR (unsigned long)&LATACLR 94 | #define PIC_LATBCLR (unsigned long)&LATBCLR 95 | #define PIC_LATCCLR (unsigned long)&LATCCLR 96 | #define PIC_LATDCLR (unsigned long)&LATDCLR 97 | #define PIC_LATECLR (unsigned long)&LATECLR 98 | #define PIC_LATFCLR (unsigned long)&LATFCLR 99 | #define PIC_LATGCLR (unsigned long)PIC_UNUSED 100 | #define PIC_LATHCLR (unsigned long)PIC_UNUSED 101 | 102 | //static const unsigned long PIC_LATxCLR[PIC_MAXPORTS]={PIC_LATACLR, PIC_LATBCLR, PIC_LATCCLR, PIC_LATDCLR, PIC_LATECLR, PIC_LATFCLR, PIC_LATGCLR, PIC_LATHCLR}; 103 | 104 | //-------------------------------------------------------------------------------------- 105 | // DEFINE AVAILABLE LATxINV REGISTERS AND PERMISSIONS (FROM WITHIN LUA) 106 | #define PIC_LATAINV (unsigned long)&LATAINV 107 | #define PIC_LATBINV (unsigned long)&LATBINV 108 | #define PIC_LATCINV (unsigned long)&LATCINV 109 | #define PIC_LATDINV (unsigned long)&LATDINV 110 | #define PIC_LATEINV (unsigned long)&LATEINV 111 | #define PIC_LATFINV (unsigned long)&LATFINV 112 | #define PIC_LATGINV (unsigned long)PIC_UNUSED 113 | #define PIC_LATHINV (unsigned long)PIC_UNUSED 114 | 115 | //static const unsigned long PIC_LATxINV[PIC_MAXPORTS]={PIC_LATAINV, PIC_LATBINV, PIC_LATCINV, PIC_LATDINV, PIC_LATEINV, PIC_LATFINV, PIC_LATGINV, PIC_LATHINV}; 116 | 117 | 118 | 119 | //-------------------------------------------------------------------------------------- 120 | //-------------------------------------------------------------------------------------- 121 | //-------------------------------------------------------------------------------------- 122 | // no changes below this line 123 | 124 | // the default timer45 tick-time (100ns) 125 | #define TIMER45_TICK 4.0/PER_FREQ 126 | #define GetSystemClock() (SYS_FREQ) 127 | #define GetPeripheralClock() (PER_FREQ) 128 | #define GetInstructionClock() (SYS_FREQ) 129 | 130 | 131 | //-------------------------------------------------------------------------------------- 132 | // THE REST 133 | #ifndef __asPIC 134 | 135 | extern volatile unsigned long gTimerHigh; 136 | 137 | extern const unsigned long PIC_TRISx[PIC_MAXPORTS]; 138 | extern const unsigned long PIC_PORTx[PIC_MAXPORTS]; 139 | extern const unsigned long PIC_LATx[PIC_MAXPORTS]; 140 | extern const unsigned long PIC_LATxSET[PIC_MAXPORTS]; 141 | extern const unsigned long PIC_LATxCLR[PIC_MAXPORTS]; 142 | extern const unsigned long PIC_LATxINV[PIC_MAXPORTS]; 143 | 144 | extern void asPIC_Init(); 145 | 146 | extern int asPIC_AD_Read(void); 147 | extern int asPIC_AD_Mux(unsigned muxapos, unsigned muxbpos, unsigned muxaneg, unsigned muxbneg); 148 | extern unsigned long long asPIC_Timer_Read(void); 149 | extern int asPIC_Timer_Match(unsigned long long match, int blocked); 150 | extern int asPIC_Port_Dir(int port, int dir, int mask); 151 | extern int asPIC_Port_State(int port, int pins, int mask); 152 | extern int asPIC_Port_Latch(int port, int state); 153 | extern int asPIC_Port_SetPins(int port, int pins); 154 | extern int asPIC_Port_ClrPins(int port, int pins); 155 | extern int asPIC_Port_InvPins(int port, int pins); 156 | 157 | 158 | #else 159 | 160 | inline void asPIC_UART1_WriteChar(char ch); 161 | 162 | #endif 163 | -------------------------------------------------------------------------------- /PIC32Lua-ChipkitMAX32.mcp: -------------------------------------------------------------------------------- 1 | [HEADER] 2 | magic_cookie={66E99B07-E706-4689-9E80-9B2582898A13} 3 | file_version=1.0 4 | device=PIC32MX795F512L 5 | [PATH_INFO] 6 | BuildDirPolicy=BuildDirIsProjectDir 7 | dir_src= 8 | dir_bin=out 9 | dir_tmp=obj 10 | dir_sin= 11 | dir_inc= 12 | dir_lib=C:\WINDOWS.PRG\MPLab850\MPLAB C32 Suite\lib;C:\WINDOWS.PRG\MPLab850\MPLAB C32 Suite\pic32mx\lib 13 | dir_lkr= 14 | [CAT_FILTERS] 15 | filter_src=*.s;*.c 16 | filter_inc=*.h;*.inc 17 | filter_obj=*.o 18 | filter_lib=*.a 19 | filter_lkr=*.ld 20 | [CAT_SUBFOLDERS] 21 | subfolder_src= 22 | subfolder_inc= 23 | subfolder_obj= 24 | subfolder_lib= 25 | subfolder_lkr= 26 | [FILE_SUBFOLDERS] 27 | file_000=. 28 | file_001=. 29 | file_002=. 30 | file_003=. 31 | file_004=. 32 | file_005=. 33 | file_006=. 34 | file_007=. 35 | file_008=. 36 | file_009=. 37 | file_010=. 38 | file_011=. 39 | file_012=. 40 | file_013=. 41 | file_014=. 42 | file_015=. 43 | file_016=. 44 | file_017=. 45 | file_018=. 46 | file_019=. 47 | file_020=. 48 | file_021=. 49 | file_022=. 50 | file_023=. 51 | file_024=. 52 | file_025=. 53 | file_026=. 54 | file_027=. 55 | file_028=. 56 | file_029=. 57 | file_030=. 58 | file_031=. 59 | file_032=. 60 | file_033=. 61 | file_034=. 62 | file_035=. 63 | file_036=. 64 | file_037=. 65 | file_038=. 66 | file_039=. 67 | file_040=. 68 | file_041=. 69 | file_042=. 70 | file_043=. 71 | file_044=. 72 | file_045=. 73 | file_046=. 74 | file_047=. 75 | file_048=. 76 | file_049=. 77 | file_050=. 78 | file_051=. 79 | file_052=. 80 | file_053=. 81 | file_054=. 82 | file_055=. 83 | file_056=. 84 | file_057=. 85 | file_058=. 86 | file_059=. 87 | file_060=. 88 | file_061=. 89 | file_062=. 90 | file_063=. 91 | file_064=. 92 | file_065=. 93 | file_066=. 94 | file_067=. 95 | file_068=. 96 | file_069=. 97 | file_070=. 98 | file_071=. 99 | file_072=. 100 | file_073=. 101 | file_074=. 102 | file_075=. 103 | file_076=. 104 | file_077=. 105 | [GENERATED_FILES] 106 | file_000=no 107 | file_001=no 108 | file_002=no 109 | file_003=no 110 | file_004=no 111 | file_005=no 112 | file_006=no 113 | file_007=no 114 | file_008=no 115 | file_009=no 116 | file_010=no 117 | file_011=no 118 | file_012=no 119 | file_013=no 120 | file_014=no 121 | file_015=no 122 | file_016=no 123 | file_017=no 124 | file_018=no 125 | file_019=no 126 | file_020=no 127 | file_021=no 128 | file_022=no 129 | file_023=no 130 | file_024=no 131 | file_025=no 132 | file_026=no 133 | file_027=no 134 | file_028=no 135 | file_029=no 136 | file_030=no 137 | file_031=no 138 | file_032=no 139 | file_033=no 140 | file_034=no 141 | file_035=no 142 | file_036=no 143 | file_037=no 144 | file_038=no 145 | file_039=no 146 | file_040=no 147 | file_041=no 148 | file_042=no 149 | file_043=no 150 | file_044=no 151 | file_045=no 152 | file_046=no 153 | file_047=no 154 | file_048=no 155 | file_049=no 156 | file_050=no 157 | file_051=no 158 | file_052=no 159 | file_053=no 160 | file_054=no 161 | file_055=no 162 | file_056=no 163 | file_057=no 164 | file_058=no 165 | file_059=no 166 | file_060=no 167 | file_061=no 168 | file_062=no 169 | file_063=no 170 | file_064=no 171 | file_065=no 172 | file_066=no 173 | file_067=no 174 | file_068=no 175 | file_069=no 176 | file_070=no 177 | file_071=no 178 | file_072=no 179 | file_073=no 180 | file_074=no 181 | file_075=no 182 | file_076=no 183 | file_077=no 184 | [OTHER_FILES] 185 | file_000=no 186 | file_001=no 187 | file_002=no 188 | file_003=no 189 | file_004=no 190 | file_005=no 191 | file_006=no 192 | file_007=no 193 | file_008=no 194 | file_009=no 195 | file_010=no 196 | file_011=no 197 | file_012=no 198 | file_013=no 199 | file_014=no 200 | file_015=no 201 | file_016=no 202 | file_017=no 203 | file_018=no 204 | file_019=no 205 | file_020=no 206 | file_021=no 207 | file_022=no 208 | file_023=no 209 | file_024=no 210 | file_025=no 211 | file_026=no 212 | file_027=no 213 | file_028=no 214 | file_029=no 215 | file_030=no 216 | file_031=no 217 | file_032=no 218 | file_033=no 219 | file_034=no 220 | file_035=no 221 | file_036=no 222 | file_037=no 223 | file_038=no 224 | file_039=no 225 | file_040=no 226 | file_041=no 227 | file_042=no 228 | file_043=no 229 | file_044=no 230 | file_045=no 231 | file_046=no 232 | file_047=no 233 | file_048=no 234 | file_049=no 235 | file_050=no 236 | file_051=no 237 | file_052=no 238 | file_053=no 239 | file_054=no 240 | file_055=no 241 | file_056=no 242 | file_057=no 243 | file_058=no 244 | file_059=no 245 | file_060=no 246 | file_061=no 247 | file_062=no 248 | file_063=no 249 | file_064=no 250 | file_065=no 251 | file_066=no 252 | file_067=no 253 | file_068=no 254 | file_069=no 255 | file_070=no 256 | file_071=no 257 | file_072=no 258 | file_073=no 259 | file_074=no 260 | file_075=no 261 | file_076=no 262 | file_077=yes 263 | [FILE_INFO] 264 | file_000=src\lapi.c 265 | file_001=src\lauxlib.c 266 | file_002=src\lbaselib.c 267 | file_003=src\lcode.c 268 | file_004=src\ldblib.c 269 | file_005=src\ldebug.c 270 | file_006=src\ldo.c 271 | file_007=src\ldump.c 272 | file_008=src\lfunc.c 273 | file_009=src\lgc.c 274 | file_010=src\linit.c 275 | file_011=src\liolib.c 276 | file_012=src\llex.c 277 | file_013=src\lmathlib.c 278 | file_014=src\lmem.c 279 | file_015=src\loadlib.c 280 | file_016=src\lobject.c 281 | file_017=src\lopcodes.c 282 | file_018=src\loslib.c 283 | file_019=src\lparser.c 284 | file_020=src\lstate.c 285 | file_021=src\lstring.c 286 | file_022=src\lstrlib.c 287 | file_023=src\ltable.c 288 | file_024=src\ltablib.c 289 | file_025=src\ltm.c 290 | file_026=src\lua.c 291 | file_027=src\lundump.c 292 | file_028=src\lvm.c 293 | file_029=src\lzio.c 294 | file_030=src\asPIC.c 295 | file_031=src\asIO.c 296 | file_032=src\asDBG.c 297 | file_033=src\asINT.c 298 | file_034=src\asCON.c 299 | file_035=src\asLUA_PIC.c 300 | file_036=src\asUART1.c 301 | file_037=src\SD-SPI.c 302 | file_038=src\FSIO.c 303 | file_039=src\asCARD.c 304 | file_040=src\lzio.h 305 | file_041=src\lapi.h 306 | file_042=src\lauxlib.h 307 | file_043=src\lcode.h 308 | file_044=src\ldebug.h 309 | file_045=src\ldo.h 310 | file_046=src\lfunc.h 311 | file_047=src\lgc.h 312 | file_048=src\llex.h 313 | file_049=src\llimits.h 314 | file_050=src\lmem.h 315 | file_051=src\lobject.h 316 | file_052=src\lopcodes.h 317 | file_053=src\lparser.h 318 | file_054=src\lstate.h 319 | file_055=src\lstring.h 320 | file_056=src\ltable.h 321 | file_057=src\ltm.h 322 | file_058=src\lua.h 323 | file_059=src\luaconf.h 324 | file_060=src\lualib.h 325 | file_061=src\lundump.h 326 | file_062=src\lvm.h 327 | file_063=src\asPIC.h 328 | file_064=src\asDBG.h 329 | file_065=src\asINT.h 330 | file_066=src\asCON.h 331 | file_067=src\asIO.h 332 | file_068=src\asLUA_PIC.h 333 | file_069=src\SD-SPI.h 334 | file_070=src\Compiler.h 335 | file_071=src\FSconfig.h 336 | file_072=src\FSDefs.h 337 | file_073=src\FSIO.h 338 | file_074=src\HardwareProfile.h 339 | file_075=src\asCARD.h 340 | file_076=linker\chipKIT-MAX32-application-32MX795F512L-PIC32Lua.ld 341 | file_077=out\PIC32Lua-ChipkitMAX32.map 342 | [SUITE_INFO] 343 | suite_guid={14495C23-81F8-43F3-8A44-859C583D7760} 344 | suite_state= 345 | [TOOL_SETTINGS] 346 | TS{CB0AF4B8-4022-429D-8F99-8A56782B2C6D}= 347 | TS{9C698E0A-CBC9-4EFF-AE7D-B569F93E7322}=-Wall -O2 -legacy-libc -DCHIPKITMAX32 348 | TS{9C698E0A-CBC9-4EFF-AE7D-B569F93E7322}_alt=yes 349 | TS{77F59DA1-3C53-4677-AC5F-A03EB0125170}=--defsym=_min_heap_size=121000 --defsym=_min_stack_size=2048 -Map="$(BINDIR_)$(TARGETBASE).map" -s -o"$(BINDIR_)$(TARGETBASE).$(TARGETSUFFIX)" -O2 -legacy-libc 350 | TS{77F59DA1-3C53-4677-AC5F-A03EB0125170}_alt=yes 351 | TS{0396C0A1-9052-4E4F-8B84-EF0162B1B4E9}= 352 | [INSTRUMENTED_TRACE] 353 | enable=0 354 | transport=0 355 | format=0 356 | [CUSTOM_BUILD] 357 | Pre-Build= 358 | Pre-BuildEnabled=1 359 | Post-Build= 360 | Post-BuildEnabled=1 361 | -------------------------------------------------------------------------------- /src/asUART1.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | #ifndef __asUART1 10 | #define __asUART1 11 | #endif 12 | 13 | #include "asPIC.h" 14 | 15 | #include "asIO.h" 16 | #include "asUART1.h" 17 | #include "asDBG.h" 18 | #include "asCON.h" 19 | 20 | 21 | 22 | 23 | // The dynamically handled buffer, with auto-size-adaption, 24 | // was a nice idea, but too slow. 25 | // Back to the easy stuff, for now... 26 | // May the future bring DMA :) 27 | volatile char pUART1_RXBuf[UART1_RXBUFSIZE]; 28 | unsigned UART1_Enabled=0; // UART1 enable flag 29 | volatile unsigned pUART1_RXBufW=0; // write buffer index 30 | volatile unsigned pUART1_RXBufR=0; // read buffer index 31 | volatile unsigned pUART1_RXBufC=0; // number of bytes in buffer 32 | volatile unsigned pUART1_RXBufO=0; // overflow flag 33 | 34 | 35 | 36 | 37 | //************************************************************************************** 38 | //*** asUART1_Enable 39 | //*** 40 | //*** 41 | //************************************************************************************** 42 | void asUART1_Enable(unsigned baud) 43 | { 44 | 45 | asUART1_RXBufInit(); 46 | 47 | 48 | #ifndef CONSOLE_SWAP 49 | // use UART1 50 | U1MODE = 0b1000100000000000; 51 | U1STAbits.UTXEN=1; 52 | U1STAbits.URXEN=1; 53 | asUART1_Baud(baud); 54 | 55 | // enable UART1 RX interrupt 56 | mU1RXClearIntFlag(); 57 | ConfigIntUART1(UART_RX_INT_EN|UART_INT_PR2); 58 | 59 | // mark settings 60 | UART1_Enabled=1; 61 | 62 | #else 63 | // use UART2 64 | U2MODE = 0b1000100000000000; 65 | U2STAbits.UTXEN=1; 66 | U2STAbits.URXEN=1; 67 | asUART1_Baud(baud); 68 | 69 | // enable UART1 RX interrupt 70 | mU2RXClearIntFlag(); 71 | ConfigIntUART2(UART_RX_INT_EN|UART_INT_PR2); 72 | 73 | // mark settings 74 | UART1_Enabled = 1; 75 | #endif 76 | } 77 | 78 | 79 | 80 | //************************************************************************************** 81 | //*** asUART1_Disable 82 | //*** 83 | //*** 84 | //************************************************************************************** 85 | void asUART1_Disable() 86 | { 87 | // 88 | UART1_Enabled = 0; 89 | 90 | #ifndef CONSOLE_SWAP 91 | 92 | // disable RX interrupt 93 | ConfigIntUART1(0); 94 | mU1RXClearIntFlag(); 95 | 96 | // turn UART1 off 97 | U1MODEbits.ON = 0; 98 | U1STAbits.UTXEN = 0; 99 | U1STAbits.URXEN = 0; 100 | 101 | #else 102 | 103 | // disable RX interrupt 104 | ConfigIntUART2(0); 105 | mU2RXClearIntFlag(); 106 | 107 | // turn UART1 off 108 | U2MODEbits.ON = 0; 109 | U2STAbits.UTXEN = 0; 110 | U2STAbits.URXEN = 0; 111 | 112 | #endif 113 | 114 | // clear RX buffer 115 | asUART1_RXBufInit(); 116 | 117 | } 118 | 119 | 120 | 121 | //************************************************************************************** 122 | //*** asUART1_Baud 123 | //*** 124 | //*** 125 | //************************************************************************************** 126 | void asUART1_Baud(unsigned baud) 127 | { 128 | if(baud < 110) 129 | baud=110; 130 | else 131 | { 132 | if(baud>500000L) 133 | baud=500000L; 134 | } 135 | 136 | #ifndef CONSOLE_SWAP 137 | 138 | U1BRG=(PER_FREQ/(16*baud))-1; 139 | 140 | #else 141 | 142 | U2BRG=(PER_FREQ/(16*baud))-1; 143 | 144 | #endif 145 | } 146 | 147 | 148 | //************************************************************************************** 149 | //*** asUART1_RXBufInit 150 | //*** 151 | //*** 152 | //************************************************************************************** 153 | inline void asUART1_RXBufInit() 154 | { 155 | pUART1_RXBufW=0; 156 | pUART1_RXBufR=0; 157 | pUART1_RXBufC=0; 158 | } 159 | 160 | 161 | //************************************************************************************** 162 | //*** asUART1_RXBufWriteChar 163 | //*** 164 | //*** Called from interrupt routine. 165 | //************************************************************************************** 166 | int asUART1_RXBufWriteChar(char ch) 167 | { 168 | if(!UART1_Enabled) 169 | return 0; 170 | 171 | // check end of ringbuffer 172 | if( ++pUART1_RXBufW >= UART1_RXBUFSIZE ) 173 | pUART1_RXBufW = 0; 174 | 175 | // check overflow 176 | if(pUART1_RXBufW == pUART1_RXBufR ) 177 | { 178 | asUART1_RXBufInit(); 179 | pUART1_RXBufO = 1; 180 | return -1; 181 | } 182 | 183 | pUART1_RXBuf[pUART1_RXBufW]=ch; 184 | pUART1_RXBufC++; 185 | 186 | return 1; 187 | } 188 | 189 | 190 | //************************************************************************************** 191 | //*** asUART1_RXBufReadChar 192 | //*** 193 | //*** inline? well... ;) 194 | //************************************************************************************** 195 | inline int asUART1_RXBufReadChar(int blocked) 196 | { 197 | if( !UART1_Enabled ) 198 | return RXBUFEMPTY; 199 | 200 | if( blocked ) 201 | { 202 | while ( pUART1_RXBufW == pUART1_RXBufR ) 203 | {;} 204 | } 205 | else 206 | { 207 | if( pUART1_RXBufW == pUART1_RXBufR ) 208 | return RXBUFEMPTY; 209 | } 210 | 211 | if( ++pUART1_RXBufR >= UART1_RXBUFSIZE ) 212 | pUART1_RXBufR = 0; 213 | 214 | // TOCHK! 215 | // The RX interrupt routine might interfere right here! 216 | if( pUART1_RXBufC > 0 ) 217 | pUART1_RXBufC--; 218 | return pUART1_RXBuf[pUART1_RXBufR]; 219 | } 220 | 221 | 222 | //************************************************************************************** 223 | //*** asUART1_RXBufFlushIn 224 | //*** 225 | //*** Flushes the RX input buffer. 226 | //*** There are faster ways, indeed, but this one is the safe method while the RX 227 | //*** interrupt is active... 228 | //************************************************************************************** 229 | inline void asUART1_RXBufFlushIn() 230 | { 231 | if(!UART1_Enabled) 232 | return; 233 | 234 | while(asUART1_RXBufReadChar(NONBLOCKING) >= 0) 235 | {;} 236 | 237 | pUART1_RXBufO=0; 238 | } 239 | 240 | 241 | 242 | //************************************************************************************** 243 | //*** asUART1_RXBufCount 244 | //*** 245 | //*** 246 | //*** Returns the number of bytes in the receive buffer. 247 | //*** If an overflow occured, '-1' will be returned. 248 | //************************************************************************************** 249 | inline unsigned asUART1_RXBufCount() 250 | { 251 | // TODO: 252 | // "-1" ??? ;-) 253 | 254 | if(pUART1_RXBufO) 255 | return -1; 256 | else 257 | return pUART1_RXBufC; 258 | } 259 | 260 | 261 | 262 | 263 | //************************************************************************************** 264 | //*** asPIC_UART1_FlushOut 265 | //*** 266 | //*** 267 | //************************************************************************************** 268 | inline void asUART1_FlushOut() 269 | { 270 | if(!UART1_Enabled) 271 | return; 272 | 273 | #ifndef CONSOLE_SWAP 274 | 275 | while(U1STAbits.UTXBF) 276 | {;} 277 | 278 | #else 279 | 280 | while(U2STAbits.UTXBF) 281 | {;} 282 | 283 | #endif 284 | } 285 | 286 | 287 | 288 | //************************************************************************************** 289 | //*** asUART1_WriteString 290 | //*** 291 | //*** Sends a string over UART1. 292 | //************************************************************************************** 293 | void asUART1_WriteString(char *ch) 294 | { 295 | if(!UART1_Enabled) 296 | return; 297 | 298 | if(ch==NULL) 299 | return; 300 | 301 | while(*ch!=0) 302 | asUART1_WriteChar(*ch++); 303 | } 304 | 305 | 306 | 307 | //************************************************************************************** 308 | //*** asUART1_WriteChar 309 | //*** 310 | //*** Sends a single character over UART1. 311 | //************************************************************************************** 312 | inline void asUART1_WriteChar(char ch) 313 | { 314 | if(!UART1_Enabled) 315 | return; 316 | 317 | asUART1_FlushOut(); 318 | 319 | #ifndef CONSOLE_SWAP 320 | 321 | U1TXREG=ch; 322 | 323 | #else 324 | 325 | U2TXREG=ch; 326 | 327 | #endif 328 | } 329 | 330 | 331 | -------------------------------------------------------------------------------- /src/FSconfig.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _FS_DEF_ 3 | 4 | #include "Compiler.h" 5 | #include "HardwareProfile.h" 6 | 7 | 8 | // Summary: A macro indicating the maximum number of concurrently open files 9 | // Description: The FS_MAX_FILES_OPEN #define is only applicable when dynamic memory allocation is not used (FS_DYNAMIC_MEM is not defined). 10 | // This macro defines the maximum number of open files at any given time. The amount of RAM used by FSFILE objects will 11 | // be equal to the size of an FSFILE object multipled by this macro value. This value should be kept as small as possible 12 | // as dictated by the application. This will reduce memory usage. 13 | #define FS_MAX_FILES_OPEN 2 14 | 15 | 16 | // Summary: A macro defining the size of a sector 17 | // Description: The MEDIA_SECTOR_SIZE macro will define the size of a sector on the FAT file system. This value must equal 512 bytes, 18 | // 1024 bytes, 2048 bytes, or 4096 bytes. The value of a sector will usually be 512 bytes. 19 | #define MEDIA_SECTOR_SIZE 512 20 | 21 | 22 | /* *******************************************************************************************************/ 23 | /************** Compiler options to enable/Disable Features based on user's application ******************/ 24 | /* *******************************************************************************************************/ 25 | 26 | 27 | // Summary: A macro to enable/disable file search functions. 28 | // Description: The ALLOW_FILESEARCH definition can be commented out to disable file search functions in the library. This will 29 | // prevent the use of the FindFirst and FindNext functions and reduce code size. 30 | #define ALLOW_FILESEARCH 31 | 32 | // Summary: A macro to enable/disable write functionality 33 | // Description: The ALLOW_WRITES definition can be commented out to disable all operations that write to the device. This will 34 | // greatly reduce code size. 35 | #define ALLOW_WRITES 36 | 37 | 38 | // Summary: A macro to enable/disable format functionality 39 | // Description: The ALLOW_FORMATS definition can be commented out to disable formatting functionality. This will prevent the use of 40 | // the FSformat function. If formats are enabled, write operations must also be enabled by uncommenting ALLOW_WRITES. 41 | #define ALLOW_FORMATS 42 | 43 | // Summary: A macro to enable/disable directory operations. 44 | // Description: The ALLOW_DIRS definition can be commented out to disable all directory functionality. This will reduce code size. 45 | // If directories are enabled, write operations must also be enabled by uncommenting ALLOW_WRITES in order to use 46 | // the FSmkdir or FSrmdir functions. 47 | #define ALLOW_DIRS 48 | 49 | // Summary: A macro to enable/disable PIC18 ROM functions. 50 | // Description: The ALLOW_PGMFUNCTIONS definition can be commented out to disable all PIC18 functions that allow the user to pass string 51 | // arguments in ROM (denoted by the suffix -pgm). Note that this functionality must be disabled when not using PIC18. 52 | //#define ALLOW_PGMFUNCTIONS 53 | 54 | // Summary: A macro to enable/disable the FSfprintf function. 55 | // Description: The ALLOW_FSFPRINTF definition can be commented out to disable the FSfprintf function. This will save code space. Note that 56 | // if FSfprintf is enabled and the PIC18 architecture is used, integer promotions must be enabled in the Project->Build Options 57 | // menu. Write operations must be enabled to use FSfprintf. 58 | //#define ALLOW_FSFPRINTF 59 | 60 | // Summary: A macro to enable/disable FAT32 support. 61 | // Description: The SUPPORT_FAT32 definition can be commented out to disable support for FAT32 functionality. This will save a small amount 62 | // of code space. 63 | #define SUPPORT_FAT32 64 | 65 | 66 | 67 | /**************************************************************************************************/ 68 | // Select a method for updating file timestamps 69 | /**************************************************************************************************/ 70 | 71 | // Summary: A macro to enable RTCC based timestamp generation 72 | // Description: The USEREALTIMECLOCK macro will configure the code to automatically 73 | // generate timestamp information for files from the RTCC module. The user 74 | // must enable and configure the RTCC module before creating or modifying 75 | // files. 76 | #define USEREALTIMECLOCK 77 | 78 | // Summary: A macro to enable manual timestamp generation 79 | // Description: The USERDEFINEDCLOCK macro will allow the user to manually set 80 | // timestamp information using the SetClockVars function. The user will 81 | // need to set the time variables immediately before creating or closing a 82 | // file or directory. 83 | //#define USERDEFINEDCLOCK 84 | 85 | // Summary: A macro to enable don't-care timestamp generation 86 | // Description: The INCREMENTTIMESTAMP macro will set the create time of a file to a 87 | // static value and increment it when a file is updated. This timestamp 88 | // generation method should only be used in applications where file times 89 | // are not necessary. 90 | //#define INCREMENTTIMESTAMP 91 | 92 | 93 | #ifdef ALLOW_PGMFUNCTIONS 94 | #ifndef __18CXX 95 | #error The pgm functions are unneccessary when not using PIC18 96 | #endif 97 | #endif 98 | 99 | #ifndef USEREALTIMECLOCK 100 | #ifndef USERDEFINEDCLOCK 101 | #ifndef INCREMENTTIMESTAMP 102 | #error Please enable USEREALTIMECLOCK, USERDEFINEDCLOCK, or INCREMENTTIMESTAMP 103 | #endif 104 | #endif 105 | #endif 106 | 107 | /************************************************************************/ 108 | // Set this preprocessor option to '1' to use dynamic FSFILE object allocation. It will 109 | // be necessary to allocate a heap when dynamically allocating FSFILE objects. 110 | // Set this option to '0' to use static FSFILE object allocation. 111 | /************************************************************************/ 112 | 113 | // ASkr FS dyn/stat: not dyn for Lua 114 | #if 0 115 | // Summary: A macro indicating that FSFILE objects will be allocated dynamically 116 | // Description: The FS_DYNAMIC_MEM macro will cause FSFILE objects to be allocated from a dynamic heap. If it is undefined, 117 | // the file objects will be allocated using a static array. 118 | #define FS_DYNAMIC_MEM 119 | #ifdef __18CXX 120 | // Description: Function pointer to a dynamic memory allocation function 121 | #define FS_malloc SRAMalloc 122 | // Description: Function pointer to a dynamic memory free function 123 | #define FS_free SRAMfree 124 | #else 125 | #define FS_malloc malloc 126 | #define FS_free free 127 | #endif 128 | #endif 129 | 130 | 131 | 132 | 133 | // Description: Function pointer to the Media Initialize Physical Layer function 134 | #define MDD_MediaInitialize MDD_SDSPI_MediaInitialize 135 | 136 | // Description: Function pointer to the Media Detect Physical Layer function 137 | #define MDD_MediaDetect MDD_SDSPI_MediaDetect 138 | 139 | // Description: Function pointer to the Sector Read Physical Layer function 140 | #define MDD_SectorRead MDD_SDSPI_SectorRead 141 | 142 | // Description: Function pointer to the Sector Write Physical Layer function 143 | #define MDD_SectorWrite MDD_SDSPI_SectorWrite 144 | 145 | // Description: Function pointer to the I/O Initialization Physical Layer function 146 | #define MDD_InitIO MDD_SDSPI_InitIO 147 | 148 | // Description: Function pointer to the Media Shutdown Physical Layer function 149 | #define MDD_ShutdownMedia MDD_SDSPI_ShutdownMedia 150 | 151 | // Description: Function pointer to the Write Protect Check Physical Layer function 152 | #define MDD_WriteProtectState MDD_SDSPI_WriteProtectState 153 | 154 | // Description: Function pointer to the Read Capacity Physical Layer function 155 | #define MDD_ReadCapacity MDD_SDSPI_ReadCapacity 156 | 157 | // Description: Function pointer to the Read Sector Size Physical Layer Function 158 | #define MDD_ReadSectorSize MDD_SDSPI_ReadSectorSize 159 | 160 | 161 | 162 | 163 | 164 | #endif 165 | -------------------------------------------------------------------------------- /src/ltablib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $ 3 | ** Library for Table Manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltablib_c 11 | #define LUA_LIB 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) 20 | 21 | 22 | static int foreachi (lua_State *L) { 23 | int i; 24 | int n = aux_getn(L, 1); 25 | luaL_checktype(L, 2, LUA_TFUNCTION); 26 | for (i=1; i <= n; i++) { 27 | lua_pushvalue(L, 2); /* function */ 28 | lua_pushinteger(L, i); /* 1st argument */ 29 | lua_rawgeti(L, 1, i); /* 2nd argument */ 30 | lua_call(L, 2, 1); 31 | if (!lua_isnil(L, -1)) 32 | return 1; 33 | lua_pop(L, 1); /* remove nil result */ 34 | } 35 | return 0; 36 | } 37 | 38 | 39 | static int foreach (lua_State *L) { 40 | luaL_checktype(L, 1, LUA_TTABLE); 41 | luaL_checktype(L, 2, LUA_TFUNCTION); 42 | lua_pushnil(L); /* first key */ 43 | while (lua_next(L, 1)) { 44 | lua_pushvalue(L, 2); /* function */ 45 | lua_pushvalue(L, -3); /* key */ 46 | lua_pushvalue(L, -3); /* value */ 47 | lua_call(L, 2, 1); 48 | if (!lua_isnil(L, -1)) 49 | return 1; 50 | lua_pop(L, 2); /* remove value and result */ 51 | } 52 | return 0; 53 | } 54 | 55 | 56 | static int maxn (lua_State *L) { 57 | lua_Number max = 0; 58 | luaL_checktype(L, 1, LUA_TTABLE); 59 | lua_pushnil(L); /* first key */ 60 | while (lua_next(L, 1)) { 61 | lua_pop(L, 1); /* remove value */ 62 | if (lua_type(L, -1) == LUA_TNUMBER) { 63 | lua_Number v = lua_tonumber(L, -1); 64 | if (v > max) max = v; 65 | } 66 | } 67 | lua_pushnumber(L, max); 68 | return 1; 69 | } 70 | 71 | 72 | static int getn (lua_State *L) { 73 | lua_pushinteger(L, aux_getn(L, 1)); 74 | return 1; 75 | } 76 | 77 | 78 | static int setn (lua_State *L) { 79 | luaL_checktype(L, 1, LUA_TTABLE); 80 | #ifndef luaL_setn 81 | luaL_setn(L, 1, luaL_checkint(L, 2)); 82 | #else 83 | luaL_error(L, LUA_QL("setn") " is obsolete"); 84 | #endif 85 | lua_pushvalue(L, 1); 86 | return 1; 87 | } 88 | 89 | 90 | static int tinsert (lua_State *L) { 91 | int e = aux_getn(L, 1) + 1; /* first empty element */ 92 | int pos; /* where to insert new element */ 93 | switch (lua_gettop(L)) { 94 | case 2: { /* called with only 2 arguments */ 95 | pos = e; /* insert new element at the end */ 96 | break; 97 | } 98 | case 3: { 99 | int i; 100 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ 101 | if (pos > e) e = pos; /* `grow' array if necessary */ 102 | for (i = e; i > pos; i--) { /* move up elements */ 103 | lua_rawgeti(L, 1, i-1); 104 | lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ 105 | } 106 | break; 107 | } 108 | default: { 109 | return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); 110 | } 111 | } 112 | luaL_setn(L, 1, e); /* new size */ 113 | lua_rawseti(L, 1, pos); /* t[pos] = v */ 114 | return 0; 115 | } 116 | 117 | 118 | static int tremove (lua_State *L) { 119 | int e = aux_getn(L, 1); 120 | int pos = luaL_optint(L, 2, e); 121 | if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ 122 | return 0; /* nothing to remove */ 123 | luaL_setn(L, 1, e - 1); /* t.n = n-1 */ 124 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ 125 | for ( ;pos= P */ 226 | while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { 227 | if (i>u) luaL_error(L, "invalid order function for sorting"); 228 | lua_pop(L, 1); /* remove a[i] */ 229 | } 230 | /* repeat --j until a[j] <= P */ 231 | while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { 232 | if (j 3 | 4 | #ifndef __asIO 5 | #define __asIO 6 | #endif 7 | 8 | 9 | #include "asPIC.h" 10 | #include "FSIO.h" 11 | #include "asCON.h" 12 | 13 | 14 | 15 | // JUST SOME DIRTY AND QUICK HACKS 16 | // 17 | // Especially the new (V0.4) CONSOLE_SWAP feature (UART1 <-> UART2) 18 | // needs some intense testing... 19 | // 20 | 21 | 22 | 23 | #ifdef CONSOLE_SWAP 24 | //************************************************************************************** 25 | //*** used by (f)printf() on console swap (UART1) 26 | //*** 27 | //*** 28 | //************************************************************************************** 29 | void _mon_putc(char ch) 30 | { 31 | asCON_SerialWriteChar(ch); 32 | } 33 | 34 | #endif 35 | 36 | 37 | 38 | //************************************************************************************** 39 | //*** 40 | //*** 41 | //*** 42 | //************************************************************************************** 43 | int ffferror(FILE *fin) 44 | { 45 | // Just returns the result of the last operation, 46 | // not related to ... 47 | return FSerror(); 48 | } 49 | 50 | 51 | 52 | //************************************************************************************** 53 | //*** 54 | //*** 55 | //*** 56 | //************************************************************************************** 57 | int ffflush (FILE *fin) 58 | { 59 | // should not be required... 60 | return 0; 61 | } 62 | 63 | 64 | 65 | //************************************************************************************** 66 | //*** 67 | //*** 68 | //*** 69 | //************************************************************************************** 70 | char *fgets (char *str, int max, FILE *fin) 71 | { 72 | int ch=0; 73 | char *ptr=str; 74 | 75 | while (--max > 0 && ((ch = fgetc(fin)) != EOF)) 76 | { 77 | *ptr++ = (char)ch; 78 | if ( (char)ch == '\n') 79 | break; 80 | } 81 | 82 | 83 | if (ch == EOF) 84 | { 85 | if (FSfeof((FSFILE *)fin)) 86 | { 87 | if (ptr == str) 88 | return NULL; 89 | } 90 | else 91 | return NULL; 92 | } 93 | *ptr = '\0'; 94 | return str; 95 | } 96 | 97 | 98 | 99 | //************************************************************************************** 100 | //*** 101 | //*** 102 | //*** 103 | //************************************************************************************** 104 | int fgetc (FILE *fin) 105 | { 106 | int ch; 107 | 108 | if( fin == stdin ) 109 | { 110 | // not that nice, but useful 111 | ch=asCON_RXBufReadChar(BLOCKING); 112 | asCON_SerialWriteChar(ch); 113 | // ENTER key returns '\r' 114 | if((char)ch=='\r') 115 | ch='\n'; 116 | return ch; 117 | } 118 | else 119 | { 120 | if(FSfread((int *)&ch,1,1,(FSFILE *)fin) != 1 ) 121 | return EOF; 122 | else 123 | return ch; 124 | } 125 | } 126 | 127 | 128 | 129 | //************************************************************************************** 130 | //*** 131 | //*** 132 | //*** 133 | //************************************************************************************** 134 | FILE *fopen (const char *a, const char *b) 135 | { 136 | return (FILE *)FSfopen(a,b); 137 | } 138 | 139 | 140 | 141 | //************************************************************************************** 142 | //*** 143 | //*** 144 | //*** 145 | //************************************************************************************** 146 | FILE *open (const char *a, const char *b) 147 | { 148 | // This should not be required... 149 | // DEBUG ONLY 150 | asCON_SerialWriteString("OWN OPEN\n\r"); 151 | return (FILE *)FSfopen(a,b); 152 | 153 | } 154 | 155 | 156 | 157 | //************************************************************************************** 158 | //*** 159 | //*** 160 | //*** 161 | //************************************************************************************** 162 | int fclose (FILE *fin) 163 | { 164 | return FSfclose((FSFILE *)fin); 165 | } 166 | 167 | 168 | 169 | //************************************************************************************** 170 | //*** 171 | //*** 172 | //*** 173 | //************************************************************************************** 174 | int close (FILE *fin) 175 | { 176 | // This should not be required... 177 | // DEBUG ONLY 178 | asCON_SerialWriteString("close\n\r"); 179 | return FSfclose((FSFILE *)fin); 180 | } 181 | 182 | 183 | 184 | //************************************************************************************** 185 | //*** 186 | //*** 187 | //*** 188 | //************************************************************************************** 189 | int lseek (FILE *fin, long int a, int b) 190 | { 191 | return FSfseek((FSFILE *)fin,a,b); 192 | } 193 | 194 | 195 | 196 | //************************************************************************************** 197 | //*** 198 | //*** 199 | //*** 200 | //************************************************************************************** 201 | long ftell (FILE *fin) 202 | { 203 | return FSftell((FSFILE *)fin); 204 | } 205 | 206 | 207 | 208 | //************************************************************************************** 209 | //*** 210 | //*** 211 | //*** 212 | //************************************************************************************** 213 | size_t fread (void *a, size_t b, size_t c, FILE *fin) 214 | { 215 | if(fin==stdin) 216 | { 217 | // not required (direct call of "fgets()" above) 218 | 219 | // ASkr DEBUG 220 | asCON_SerialWriteString("*ouch*\n\r"); 221 | 222 | return 0; 223 | } 224 | else 225 | 226 | return FSfread(a,b,c,(FSFILE *)fin); 227 | } 228 | 229 | 230 | 231 | //************************************************************************************** 232 | //*** fwrite 233 | //*** 234 | //*** output for: 235 | //*** - io.write (file and "console") 236 | //************************************************************************************** 237 | size_t fwrite (const void *ptr, size_t size, size_t count, FILE *fin ) 238 | { 239 | char *pc=(char *)ptr; 240 | 241 | if( ( fin == stdout) || ( fin == stderr ) ) 242 | { 243 | while(count--) 244 | asCON_SerialWriteChar(*pc++); 245 | return count; 246 | } 247 | else 248 | return FSfwrite(ptr,size,count,(FSFILE *)fin); 249 | } 250 | 251 | 252 | 253 | 254 | //************************************************************************************** 255 | //*** 256 | //*** 257 | //*** 258 | //************************************************************************************** 259 | size_t read (void *a, size_t b, size_t c, FILE *fin) 260 | { 261 | // This should not be required... 262 | // DEBUG ONLY 263 | asCON_SerialWriteString("read\n\r"); 264 | return FSfread(a,b,c,(FSFILE *)fin); 265 | } 266 | 267 | 268 | 269 | 270 | //************************************************************************************** 271 | //*** write() 272 | //*** 273 | //*** This is the main UART2 console output function for: 274 | //*** - fputs(..., stderr) 275 | //*** - fputs(..., stdout) 276 | //*** - printf(...) 277 | //*** - fprintf(stdout, ...) 278 | //*** - fprintf(stderr, ...) 279 | //*** 280 | //************************************************************************************** 281 | size_t write(int fd, char *dat, size_t len) 282 | { 283 | // ASkr TODO 284 | // ouch... 285 | if( ( fd==1 ) || ( fd==2 ) ) 286 | asCON_SerialWriteChar(*dat); 287 | else 288 | { 289 | // ASkr DEBUG 290 | asCON_SerialWriteString("*ouch*\n\r"); 291 | } 292 | return 1; 293 | } 294 | 295 | 296 | 297 | //************************************************************************************** 298 | //*** 299 | //*** 300 | //*** 301 | //************************************************************************************** 302 | int remove (const char *fnam) 303 | { 304 | // DEBUG ONLY 305 | asCON_SerialWriteString("remove\n\r"); 306 | return 0; 307 | } 308 | 309 | 310 | //************************************************************************************** 311 | //*** 312 | //*** 313 | //*** 314 | //************************************************************************************** 315 | void unlink() 316 | { 317 | // DEBUG ONLY 318 | asCON_SerialWriteString("unlink\n\r"); 319 | } 320 | 321 | 322 | //************************************************************************************** 323 | //*** 324 | //*** 325 | //*** 326 | //************************************************************************************** 327 | void link() 328 | { 329 | // DEBUG ONLY 330 | asCON_SerialWriteString("link\n\r"); 331 | } 332 | 333 | 334 | //************************************************************************************** 335 | //*** 336 | //*** 337 | //*** 338 | //************************************************************************************** 339 | int gettimeofday (struct timeval *a, void *b) 340 | { 341 | // DEBUG ONLY 342 | return 0; 343 | } 344 | 345 | -------------------------------------------------------------------------------- /src/lopcodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lopcodes_h 8 | #define lopcodes_h 9 | 10 | #include "llimits.h" 11 | 12 | 13 | /*=========================================================================== 14 | We assume that instructions are unsigned numbers. 15 | All instructions have an opcode in the first 6 bits. 16 | Instructions can have the following fields: 17 | `A' : 8 bits 18 | `B' : 9 bits 19 | `C' : 9 bits 20 | `Bx' : 18 bits (`B' and `C' together) 21 | `sBx' : signed Bx 22 | 23 | A signed argument is represented in excess K; that is, the number 24 | value is the unsigned value minus K. K is exactly the maximum value 25 | for that argument (so that -max is represented by 0, and +max is 26 | represented by 2*max), which is half the maximum for the corresponding 27 | unsigned argument. 28 | ===========================================================================*/ 29 | 30 | 31 | enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ 32 | 33 | 34 | /* 35 | ** size and position of opcode arguments. 36 | */ 37 | #define SIZE_C 9 38 | #define SIZE_B 9 39 | #define SIZE_Bx (SIZE_C + SIZE_B) 40 | #define SIZE_A 8 41 | 42 | #define SIZE_OP 6 43 | 44 | #define POS_OP 0 45 | #define POS_A (POS_OP + SIZE_OP) 46 | #define POS_C (POS_A + SIZE_A) 47 | #define POS_B (POS_C + SIZE_C) 48 | #define POS_Bx POS_C 49 | 50 | 51 | /* 52 | ** limits for opcode arguments. 53 | ** we use (signed) int to manipulate most arguments, 54 | ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) 55 | */ 56 | #if SIZE_Bx < LUAI_BITSINT-1 57 | #define MAXARG_Bx ((1<>1) /* `sBx' is signed */ 59 | #else 60 | #define MAXARG_Bx MAX_INT 61 | #define MAXARG_sBx MAX_INT 62 | #endif 63 | 64 | 65 | #define MAXARG_A ((1<>POS_OP) & MASK1(SIZE_OP,0))) 81 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ 82 | ((cast(Instruction, o)<>POS_A) & MASK1(SIZE_A,0))) 85 | #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ 86 | ((cast(Instruction, u)<>POS_B) & MASK1(SIZE_B,0))) 89 | #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ 90 | ((cast(Instruction, b)<>POS_C) & MASK1(SIZE_C,0))) 93 | #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ 94 | ((cast(Instruction, b)<>POS_Bx) & MASK1(SIZE_Bx,0))) 97 | #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ 98 | ((cast(Instruction, b)< C) then pc++ */ 190 | OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ 191 | 192 | OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ 193 | OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ 194 | OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ 195 | 196 | OP_FORLOOP,/* A sBx R(A)+=R(A+2); 197 | if R(A) =) R(A)*/ 205 | OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ 206 | 207 | OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ 208 | } OpCode; 209 | 210 | 211 | #define NUM_OPCODES (cast(int, OP_VARARG) + 1) 212 | 213 | 214 | 215 | /*=========================================================================== 216 | Notes: 217 | (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1, 218 | and can be 0: OP_CALL then sets `top' to last_result+1, so 219 | next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. 220 | 221 | (*) In OP_VARARG, if (B == 0) then use actual number of varargs and 222 | set top (like in OP_CALL with C == 0). 223 | 224 | (*) In OP_RETURN, if (B == 0) then return up to `top' 225 | 226 | (*) In OP_SETLIST, if (B == 0) then B = `top'; 227 | if (C == 0) then next `instruction' is real C 228 | 229 | (*) For comparisons, A specifies what condition the test should accept 230 | (true or false). 231 | 232 | (*) All `skips' (pc++) assume that next instruction is a jump 233 | ===========================================================================*/ 234 | 235 | 236 | /* 237 | ** masks for instruction properties. The format is: 238 | ** bits 0-1: op mode 239 | ** bits 2-3: C arg mode 240 | ** bits 4-5: B arg mode 241 | ** bit 6: instruction set register A 242 | ** bit 7: operator is a test 243 | */ 244 | 245 | enum OpArgMask { 246 | OpArgN, /* argument is not used */ 247 | OpArgU, /* argument is used */ 248 | OpArgR, /* argument is a register or a jump offset */ 249 | OpArgK /* argument is a constant or register/constant */ 250 | }; 251 | 252 | LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES]; 253 | 254 | #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) 255 | #define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) 256 | #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) 257 | #define testAMode(m) (luaP_opmodes[m] & (1 << 6)) 258 | #define testTMode(m) (luaP_opmodes[m] & (1 << 7)) 259 | 260 | 261 | LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ 262 | 263 | 264 | /* number of list items to accumulate before a SETLIST instruction */ 265 | #define LFIELDS_PER_FLUSH 50 266 | 267 | 268 | #endif 269 | --------------------------------------------------------------------------------