Bug fix: check if vm exists
[avr-sim.git] / ScriptableAnalyzer.cpp
blob108d3c39c1ae3c09834714f045cd7e46a556734b
1 #include "ScriptableAnalyzer.h"
2 #include "ScriptEngine.h"
3 #include "script/ScriptException.h"
5 namespace avr {
7 ScriptableAnalyzer::ScriptableAnalyzer(Core *core, ScriptEngine *vm, const char *scriptname)
8 : Analyzer(core), script::Scriptable(vm) {
10 try {
11 compileFile(scriptname);
12 } catch( script::ScriptException & ex ) {
13 throw ScriptException( ex.message() );
17 ScriptableAnalyzer::~ScriptableAnalyzer() {
20 void ScriptableAnalyzer::callHelper(const char *method) {
21 try {
22 if( hasMethod(method) ) {
23 pushMethod(method);
24 Scriptable::call(0, 0);
26 } catch( script::ScriptException & ex ) {
27 throw ScriptException( ex.message() );
31 void ScriptableAnalyzer::callHelperb(const char *method, bool b) {
32 try {
33 if( hasMethod(method) ) {
34 pushMethod(method);
35 vm->pushBoolean(b);
36 Scriptable::call(1, 0);
38 } catch( script::ScriptException & ex ) {
39 throw ScriptException( ex.message() );
43 void ScriptableAnalyzer::callHelper(const char *method, float x) {
44 try {
45 if( hasMethod(method) ) {
46 pushMethod(method);
47 vm->pushNumber(x);
48 Scriptable::call(1, 0);
50 } catch( script::ScriptException & ex ) {
51 throw ScriptException( ex.message() );
55 void ScriptableAnalyzer::callHelperb(const char *method, float x, bool b) {
56 try {
57 if( hasMethod(method) ) {
58 pushMethod(method);
59 vm->pushNumber(x);
60 vm->pushBoolean(b);
61 Scriptable::call(2, 0);
63 } catch( script::ScriptException & ex ) {
64 throw ScriptException( ex.message() );
68 void ScriptableAnalyzer::callHelper(const char *method, float x, float y) {
69 try {
70 if( hasMethod(method) ) {
71 pushMethod(method);
72 vm->pushNumber(x);
73 vm->pushNumber(y);
74 Scriptable::call(2, 0);
76 } catch( script::ScriptException & ex ) {
77 throw ScriptException( ex.message() );
81 void ScriptableAnalyzer::reset(unsigned int type) {
82 const char *methodName = "reset";
83 callHelper(methodName, type);
86 void ScriptableAnalyzer::step(dword address, lword ticks) {
87 const char *methodName = "step";
88 callHelper(methodName, address, ticks);
91 void ScriptableAnalyzer::readRegister(int r, byte val) {
92 const char *methodName = "readRegister";
93 callHelper(methodName, r, val);
96 void ScriptableAnalyzer::writeRegister(int r, byte val) {
97 const char *methodName = "writeRegister";
98 callHelper(methodName, r, val);
101 void ScriptableAnalyzer::readStatus(byte val) {
102 const char *methodName = "readStatus";
103 callHelper(methodName, val);
106 void ScriptableAnalyzer::writeStatus(byte val) {
107 const char *methodName = "writeStatus";
108 callHelper(methodName, val);
111 void ScriptableAnalyzer::readIORegister(int r, byte val) {
112 const char *methodName = "readIORegister";
113 callHelper(methodName, r, val);
116 void ScriptableAnalyzer::writeIORegister(int r, byte val) {
117 const char *methodName = "writeIORegister";
118 callHelper(methodName, r, val);
121 void ScriptableAnalyzer::readByte(unsigned int addr, byte val) {
122 const char *methodName = "readByte";
123 callHelper(methodName, addr, val);
126 void ScriptableAnalyzer::writeByte(unsigned int addr, byte val) {
127 const char *methodName = "writeByte";
128 callHelper(methodName, addr, val);
131 void ScriptableAnalyzer::readFlash(unsigned int addr, byte val) {
132 const char *methodName = "readFlash";
133 callHelper(methodName, addr, val);
136 void ScriptableAnalyzer::writeFlash(unsigned int addr, word val) {
137 const char *methodName = "writeFlash";
138 callHelper(methodName, addr, val);
141 void ScriptableAnalyzer::fetchOperand(word val) {
142 const char *methodName = "fetchOperand";
143 callHelper(methodName, val);
146 void ScriptableAnalyzer::push(byte val) {
147 const char *methodName = "push";
148 callHelper(methodName, val);
151 void ScriptableAnalyzer::pop(byte val) {
152 const char *methodName = "pop";
153 callHelper(methodName, val);
156 void ScriptableAnalyzer::jump(sbyte offset, bool push) {
157 const char *methodName = "jump";
158 callHelperb(methodName, offset, push);
161 void ScriptableAnalyzer::skip() {
162 const char *methodName = "skip";
163 callHelper(methodName);
166 void ScriptableAnalyzer::call(dword address, bool push) {
167 const char *methodName = "call";
168 callHelperb(methodName, address, push);
171 void ScriptableAnalyzer::ret(bool interrupt) {
172 const char *methodName = "ret";
173 callHelperb(methodName, interrupt);
176 void ScriptableAnalyzer::sleep(unsigned int mode) {
177 const char *methodName = "sleep";
178 callHelper(methodName, mode);
181 void ScriptableAnalyzer::interrupt(unsigned int vector, unsigned int addr) {
182 const char *methodName = "interrupt";
183 callHelper(methodName, vector, addr);