2 * avr-sim: An atmel AVR simulator
3 * Copyright (C) 2008 Tom Haber
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "ScriptEngine.h"
20 #include "script/ScriptException.h"
27 using script::ScriptMethod
;
28 using script::ScriptUtil
;
33 ScriptMethod
<ScriptProgram
> ScriptProgram::sm_methods
[] = {
34 ScriptMethod
<ScriptProgram
>( "functionName", &ScriptProgram::script_functionName
),
35 ScriptMethod
<ScriptProgram
>( "dataName", &ScriptProgram::script_dataName
),
36 ScriptMethod
<ScriptProgram
>( "functionAddress", &ScriptProgram::script_functionAddress
),
37 ScriptMethod
<ScriptProgram
>( "dataAddress", &ScriptProgram::script_dataAddress
)
40 ScriptProgram::ScriptProgram(script::VM
*vm
, const Program
& program
)
41 : Scriptable(vm
), program(program
), methodBase( -1 ) {
42 methodBase
= ScriptUtil
<ScriptProgram
,Scriptable
>::addMethods( this,
43 sm_methods
, sizeof(sm_methods
)/sizeof(sm_methods
[0]) );
45 registerClass("Program");
48 ScriptProgram::~ScriptProgram() {
49 unregisterClass("Program");
52 int ScriptProgram::methodCall(int i
) {
53 return ScriptUtil
<ScriptProgram
,Scriptable
>::methodCall(this, i
, methodBase
,
54 sm_methods
, sizeof(sm_methods
)/sizeof(sm_methods
[0]) );
57 int ScriptProgram::script_functionName(const char *funcName
) {
58 if ( vm
->top() != 1 || vm
->getType(1) != VM::Number
)
59 throw script::ScriptException("expects a function address");
61 unsigned int addr
= (unsigned int)vm
->toNumber(1);
62 vm
->pushString( program
.functionName(addr
) );
66 int ScriptProgram::script_dataName(const char *funcName
) {
67 if ( vm
->top() != 1 || vm
->getType(1) != VM::Number
)
68 throw script::ScriptException("expects a data address");
70 unsigned int addr
= (unsigned int)vm
->toNumber(1);
71 vm
->pushString( program
.dataName(addr
) );
75 int ScriptProgram::script_functionAddress(const char *funcName
) {
76 if ( vm
->top() != 1 || vm
->getType(1) != VM::String
)
77 throw script::ScriptException("expects a function name");
79 const char *name
= vm
->toString(1);
80 int addr
= program
.functionAddress(name
);
82 throw script::ScriptException("Unable to find function address");
84 vm
->pushNumber( addr
);
88 int ScriptProgram::script_dataAddress(const char *funcName
) {
89 if ( vm
->top() != 1 || vm
->getType(1) != VM::String
)
90 throw script::ScriptException("expects a data name");
92 const char *name
= vm
->toString(1);
93 int addr
= program
.dataAddress(name
);
95 throw script::ScriptException("Unable to find data address");
97 vm
->pushNumber( addr
);
101 ScriptEngine::ScriptEngine() : program(0) {
103 std::cout
<< "Booting virtual machine" << std::endl
;
107 ScriptEngine::~ScriptEngine() {
109 std::cout
<< "Unloading virtual machine" << std::endl
;
116 void ScriptEngine::setProgram(const Program
& prog
) {
117 program
= new ScriptProgram( this, prog
);
120 void ScriptEngine::loadConfig(const char *filename
) {
122 compileFile(filename
);
123 } catch( script::ScriptException
& ex
) {
124 throw ScriptException( ex
.message() );