Dependents:   WeatherStation

Committer:
okini3939
Date:
Sun Jun 26 16:53:49 2011 +0000
Revision:
1:f8c5afc27878
Parent:
0:684b6fdf080c
Child:
2:64bc38078592

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:684b6fdf080c 1 /** @file
okini3939 0:684b6fdf080c 2 * @brief Instruction List interpreter
okini3939 0:684b6fdf080c 3 */
okini3939 0:684b6fdf080c 4
okini3939 0:684b6fdf080c 5 #include "mbed.h"
okini3939 0:684b6fdf080c 6 #include "ILinterpreter.h"
okini3939 0:684b6fdf080c 7
okini3939 0:684b6fdf080c 8 #undef DEBUG
okini3939 0:684b6fdf080c 9
okini3939 0:684b6fdf080c 10 struct MNE_str {
okini3939 0:684b6fdf080c 11 eMNEMONIC mne;
okini3939 0:684b6fdf080c 12 char str[5];
okini3939 0:684b6fdf080c 13 };
okini3939 0:684b6fdf080c 14
okini3939 0:684b6fdf080c 15 #define IL_MNE_NUM 22
okini3939 1:f8c5afc27878 16 static const struct MNE_str mne_str[IL_MNE_NUM] = {
okini3939 0:684b6fdf080c 17 {MNE_DEF, "DEF"},
okini3939 0:684b6fdf080c 18 {MNE_LD, "LD"}, {MNE_LDI, "LDI"}, {MNE_LDP, "LDP"}, {MNE_LDF, "LDF"},
okini3939 0:684b6fdf080c 19 {MNE_OR, "OR"}, {MNE_ORI, "ORI"}, {MNE_ORP, "ORP"}, {MNE_ORF, "ORF"},
okini3939 0:684b6fdf080c 20 {MNE_AND, "AND"}, {MNE_ANI, "ANI"}, {MNE_ANDP, "ANDP"}, {MNE_ANDF, "ANDF"},
okini3939 0:684b6fdf080c 21 {MNE_ORB, "ORB"}, {MNE_ANB, "ANB"}, {MNE_INV, "INV"},
okini3939 0:684b6fdf080c 22 {MNE_MPS, "MPS"}, {MNE_MRD, "MRD"}, {MNE_MPP, "MPP"},
okini3939 0:684b6fdf080c 23 {MNE_OUT, "OUT"}, {MNE_SET, "SET"}, {MNE_RST, "RST"},
okini3939 0:684b6fdf080c 24 };
okini3939 0:684b6fdf080c 25
okini3939 0:684b6fdf080c 26 struct EXP_str {
okini3939 0:684b6fdf080c 27 eEXPRESSION exp;
okini3939 0:684b6fdf080c 28 char str[3];
okini3939 0:684b6fdf080c 29 };
okini3939 0:684b6fdf080c 30
okini3939 0:684b6fdf080c 31 #define IL_EXP_NUM 10
okini3939 1:f8c5afc27878 32 static const struct EXP_str exp_str[IL_EXP_NUM] = {
okini3939 0:684b6fdf080c 33 {EXP_EQ, "=="}, {EXP_EQ, "="}, {EXP_NE, "!="}, {EXP_NE, "<>"},
okini3939 0:684b6fdf080c 34 {EXP_LT, "<="}, {EXP_LE, "<"}, {EXP_GT, ">"}, {EXP_GE, ">="},
okini3939 0:684b6fdf080c 35 {EXP_MOD, "%"}, {EXP_NMOD, "!%"},
okini3939 0:684b6fdf080c 36 };
okini3939 0:684b6fdf080c 37
okini3939 0:684b6fdf080c 38
okini3939 1:f8c5afc27878 39 ILinterpreter::ILinterpreter () {
okini3939 0:684b6fdf080c 40 il_count = 0;
okini3939 1:f8c5afc27878 41 printf("<%x>\r\n", &inout);
okini3939 0:684b6fdf080c 42 memset(&inout, 0, sizeof(inout));
okini3939 0:684b6fdf080c 43 inout.sec = time(NULL) + (60 * 60 * 9);
okini3939 0:684b6fdf080c 44 inout_old = inout;
okini3939 0:684b6fdf080c 45 cb_input = NULL;
okini3939 0:684b6fdf080c 46 cb_output = NULL;
okini3939 0:684b6fdf080c 47 }
okini3939 0:684b6fdf080c 48
okini3939 0:684b6fdf080c 49 // input relay, expression
okini3939 0:684b6fdf080c 50 int ILinterpreter::input (tInOut *io, int i, int old) {
okini3939 0:684b6fdf080c 51 int keynum;
okini3939 0:684b6fdf080c 52 float value, check;
okini3939 0:684b6fdf080c 53 struct tm *tim;
okini3939 0:684b6fdf080c 54
okini3939 0:684b6fdf080c 55 tim = localtime(&io->sec);
okini3939 0:684b6fdf080c 56 keynum = il[i].keynum;
okini3939 0:684b6fdf080c 57 // right value
okini3939 0:684b6fdf080c 58 check = il[i].value;
okini3939 0:684b6fdf080c 59
okini3939 0:684b6fdf080c 60 // left value
okini3939 0:684b6fdf080c 61 value = 0;
okini3939 0:684b6fdf080c 62 switch (il[i].key) {
okini3939 0:684b6fdf080c 63
okini3939 0:684b6fdf080c 64 case 'y':
okini3939 0:684b6fdf080c 65 value = tim->tm_year + 1900;
okini3939 0:684b6fdf080c 66 break;
okini3939 0:684b6fdf080c 67 case 'm':
okini3939 0:684b6fdf080c 68 value = tim->tm_mon;
okini3939 0:684b6fdf080c 69 break;
okini3939 0:684b6fdf080c 70 case 'd':
okini3939 0:684b6fdf080c 71 value = tim->tm_mday;
okini3939 0:684b6fdf080c 72 break;
okini3939 0:684b6fdf080c 73 case 'h':
okini3939 0:684b6fdf080c 74 value = tim->tm_hour;
okini3939 0:684b6fdf080c 75 break;
okini3939 0:684b6fdf080c 76 case 'i':
okini3939 0:684b6fdf080c 77 value = tim->tm_min;
okini3939 0:684b6fdf080c 78 break;
okini3939 0:684b6fdf080c 79 case 's':
okini3939 0:684b6fdf080c 80 value = tim->tm_sec;
okini3939 0:684b6fdf080c 81 break;
okini3939 0:684b6fdf080c 82
okini3939 0:684b6fdf080c 83 case '0':
okini3939 0:684b6fdf080c 84 value = 0;
okini3939 0:684b6fdf080c 85 break;
okini3939 0:684b6fdf080c 86 case '1':
okini3939 0:684b6fdf080c 87 value = 1;
okini3939 0:684b6fdf080c 88 break;
okini3939 0:684b6fdf080c 89
okini3939 0:684b6fdf080c 90 case 'M': // RELAY
okini3939 0:684b6fdf080c 91 if (keynum >= IL_RELAY_NUM) break;
okini3939 0:684b6fdf080c 92 value = io->relay[keynum];
okini3939 0:684b6fdf080c 93 break;
okini3939 0:684b6fdf080c 94
okini3939 0:684b6fdf080c 95 case 'T': // Timer
okini3939 0:684b6fdf080c 96 if (keynum >= IL_TIMER_NUM) break;
okini3939 0:684b6fdf080c 97 if (il[i].expression == EXP_NULL) {
okini3939 0:684b6fdf080c 98 value = io->timer_flg[keynum] && io->timer_cnt[keynum] >= io->timer_set[keynum];
okini3939 0:684b6fdf080c 99 } else {
okini3939 0:684b6fdf080c 100 value = io->timer_cnt[keynum];
okini3939 0:684b6fdf080c 101 }
okini3939 0:684b6fdf080c 102 break;
okini3939 0:684b6fdf080c 103
okini3939 0:684b6fdf080c 104 case 'C': // Counter
okini3939 0:684b6fdf080c 105 if (keynum >= IL_COUNTER_NUM) break;
okini3939 0:684b6fdf080c 106 if (il[i].expression == EXP_NULL) {
okini3939 0:684b6fdf080c 107 value = io->count_cnt[keynum] >= io->count_set[keynum];
okini3939 0:684b6fdf080c 108 } else {
okini3939 0:684b6fdf080c 109 value = io->count_cnt[keynum];
okini3939 0:684b6fdf080c 110 }
okini3939 0:684b6fdf080c 111 break;
okini3939 0:684b6fdf080c 112
okini3939 0:684b6fdf080c 113 default: // order input
okini3939 0:684b6fdf080c 114 if (cb_input) {
okini3939 0:684b6fdf080c 115 value = (*cb_input)(il[i].key, keynum, il[i].expression, old);
okini3939 0:684b6fdf080c 116 }
okini3939 0:684b6fdf080c 117 break;
okini3939 0:684b6fdf080c 118 }
okini3939 0:684b6fdf080c 119
okini3939 0:684b6fdf080c 120 // expression
okini3939 0:684b6fdf080c 121 switch (il[i].expression) {
okini3939 0:684b6fdf080c 122 case EXP_EQ:
okini3939 0:684b6fdf080c 123 return value == check;
okini3939 0:684b6fdf080c 124 case EXP_NE:
okini3939 0:684b6fdf080c 125 return value != check;
okini3939 0:684b6fdf080c 126 case EXP_LE:
okini3939 0:684b6fdf080c 127 return value <= check;
okini3939 0:684b6fdf080c 128 case EXP_LT:
okini3939 0:684b6fdf080c 129 return value < check;
okini3939 0:684b6fdf080c 130 case EXP_GE:
okini3939 0:684b6fdf080c 131 return value >= check;
okini3939 0:684b6fdf080c 132 case EXP_GT:
okini3939 0:684b6fdf080c 133 return value > check;
okini3939 0:684b6fdf080c 134 case EXP_MOD:
okini3939 0:684b6fdf080c 135 return (int)value % (int)check;
okini3939 0:684b6fdf080c 136 case EXP_NMOD:
okini3939 0:684b6fdf080c 137 return ! ((int)value % (int)check);
okini3939 0:684b6fdf080c 138 }
okini3939 0:684b6fdf080c 139
okini3939 0:684b6fdf080c 140 return value != 0;
okini3939 0:684b6fdf080c 141 }
okini3939 0:684b6fdf080c 142
okini3939 0:684b6fdf080c 143 // output relay
okini3939 0:684b6fdf080c 144 void ILinterpreter::output (int i, int reg, eMNEMONIC mne) {
okini3939 0:684b6fdf080c 145 int keynum;
okini3939 0:684b6fdf080c 146
okini3939 0:684b6fdf080c 147 keynum = il[i].keynum;
okini3939 0:684b6fdf080c 148 #ifdef DEBUG
okini3939 0:684b6fdf080c 149 printf("output [%c%d] reg=%d sr=%d\r\n", il[i].key, keynum, reg, mne);
okini3939 0:684b6fdf080c 150 #endif
okini3939 0:684b6fdf080c 151
okini3939 0:684b6fdf080c 152 switch (il[i].key) {
okini3939 0:684b6fdf080c 153 case 'M': // relay
okini3939 0:684b6fdf080c 154 if (keynum >= IL_RELAY_NUM) break;
okini3939 0:684b6fdf080c 155 if (mne == MNE_OUT) {
okini3939 0:684b6fdf080c 156 inout.relay[keynum] = reg;
okini3939 0:684b6fdf080c 157 } else
okini3939 0:684b6fdf080c 158 if (mne == MNE_SET && reg) {
okini3939 0:684b6fdf080c 159 inout.relay[keynum] = 1;
okini3939 0:684b6fdf080c 160 } else
okini3939 0:684b6fdf080c 161 if (mne == MNE_RST && reg) {
okini3939 0:684b6fdf080c 162 inout.relay[keynum] = 0;
okini3939 0:684b6fdf080c 163 }
okini3939 0:684b6fdf080c 164 break;
okini3939 0:684b6fdf080c 165
okini3939 0:684b6fdf080c 166 case 'T': // Timer
okini3939 0:684b6fdf080c 167 if (keynum >= IL_TIMER_NUM) break;
okini3939 0:684b6fdf080c 168 if (mne == MNE_OUT) {
okini3939 0:684b6fdf080c 169 if (! inout.timer_flg[keynum]) {
okini3939 0:684b6fdf080c 170 // set timer
okini3939 0:684b6fdf080c 171 inout.timer_cnt[keynum] = 0;
okini3939 0:684b6fdf080c 172 }
okini3939 0:684b6fdf080c 173 inout.timer_flg[keynum] = reg;
okini3939 0:684b6fdf080c 174 } else
okini3939 0:684b6fdf080c 175 if (mne == MNE_RST && reg) {
okini3939 0:684b6fdf080c 176 inout.timer_cnt[keynum] = 0;
okini3939 0:684b6fdf080c 177 }
okini3939 0:684b6fdf080c 178 break;
okini3939 0:684b6fdf080c 179
okini3939 0:684b6fdf080c 180 case 'C': // Counter
okini3939 0:684b6fdf080c 181 if (keynum >= IL_COUNTER_NUM) break;
okini3939 0:684b6fdf080c 182 if (mne == MNE_OUT && reg) {
okini3939 0:684b6fdf080c 183 if (inout.count_rev[keynum]) {
okini3939 0:684b6fdf080c 184 inout.count_cnt[keynum] --;
okini3939 0:684b6fdf080c 185 } else {
okini3939 0:684b6fdf080c 186 inout.count_cnt[keynum] ++;
okini3939 0:684b6fdf080c 187 }
okini3939 0:684b6fdf080c 188 } else
okini3939 0:684b6fdf080c 189 if (mne == MNE_RST && reg) {
okini3939 0:684b6fdf080c 190 inout.count_cnt[keynum] = 0;
okini3939 0:684b6fdf080c 191 }
okini3939 0:684b6fdf080c 192 case 'R': // Counter (reverse)
okini3939 0:684b6fdf080c 193 if (keynum >= IL_COUNTER_NUM) break;
okini3939 0:684b6fdf080c 194 if (mne == MNE_OUT) {
okini3939 0:684b6fdf080c 195 inout.count_rev[keynum] = reg;
okini3939 0:684b6fdf080c 196 }
okini3939 0:684b6fdf080c 197 break;
okini3939 0:684b6fdf080c 198
okini3939 0:684b6fdf080c 199 default: // order output
okini3939 0:684b6fdf080c 200 if (cb_output) {
okini3939 0:684b6fdf080c 201 (*cb_output)(il[i].key, keynum, reg, mne);
okini3939 0:684b6fdf080c 202 }
okini3939 0:684b6fdf080c 203 break;
okini3939 0:684b6fdf080c 204 }
okini3939 0:684b6fdf080c 205 }
okini3939 0:684b6fdf080c 206
okini3939 0:684b6fdf080c 207 // execute IL
okini3939 0:684b6fdf080c 208 int ILinterpreter::exec () {
okini3939 0:684b6fdf080c 209 int i, j, reg = -1;
okini3939 0:684b6fdf080c 210 tInOut inout_tmp;
okini3939 0:684b6fdf080c 211
okini3939 0:684b6fdf080c 212 if (! il_count) return 0;
okini3939 0:684b6fdf080c 213
okini3939 1:f8c5afc27878 214 addr = 0;
okini3939 0:684b6fdf080c 215 inout.sec = time(NULL);
okini3939 0:684b6fdf080c 216 inout_tmp = inout;
okini3939 0:684b6fdf080c 217
okini3939 0:684b6fdf080c 218 // mnemonic decode
okini3939 0:684b6fdf080c 219 for(i = 0; i < il_count; i ++) {
okini3939 0:684b6fdf080c 220 switch (il[i].mnemonic) {
okini3939 0:684b6fdf080c 221 case MNE_LD:
okini3939 1:f8c5afc27878 222 push(reg);
okini3939 0:684b6fdf080c 223 reg = input(&inout, i);
okini3939 0:684b6fdf080c 224 break;
okini3939 0:684b6fdf080c 225 case MNE_LDI:
okini3939 1:f8c5afc27878 226 push(reg);
okini3939 0:684b6fdf080c 227 reg = ! input(&inout, i);
okini3939 0:684b6fdf080c 228 break;
okini3939 0:684b6fdf080c 229 case MNE_LDP:
okini3939 1:f8c5afc27878 230 push(reg);
okini3939 0:684b6fdf080c 231 reg = input(&inout, i) && ! input(&inout_old, i, 1);
okini3939 0:684b6fdf080c 232 break;
okini3939 0:684b6fdf080c 233 case MNE_LDF:
okini3939 1:f8c5afc27878 234 push(reg);
okini3939 0:684b6fdf080c 235 reg = ! input(&inout, i) && input(&inout_old, i, 1);
okini3939 0:684b6fdf080c 236 break;
okini3939 0:684b6fdf080c 237
okini3939 0:684b6fdf080c 238 case MNE_AND:
okini3939 0:684b6fdf080c 239 reg = reg && input(&inout, i);
okini3939 0:684b6fdf080c 240 break;
okini3939 0:684b6fdf080c 241 case MNE_ANI:
okini3939 0:684b6fdf080c 242 reg = reg && ! input(&inout, i);
okini3939 0:684b6fdf080c 243 break;
okini3939 0:684b6fdf080c 244 case MNE_ANDP:
okini3939 0:684b6fdf080c 245 reg = reg && (input(&inout, i) && ! input(&inout_old, i), 1);
okini3939 0:684b6fdf080c 246 break;
okini3939 0:684b6fdf080c 247 case MNE_ANDF:
okini3939 0:684b6fdf080c 248 reg = reg && (! input(&inout, i) && input(&inout_old, i), 1);
okini3939 0:684b6fdf080c 249 break;
okini3939 0:684b6fdf080c 250
okini3939 0:684b6fdf080c 251 case MNE_OR:
okini3939 0:684b6fdf080c 252 reg = reg || input(&inout, i);
okini3939 0:684b6fdf080c 253 break;
okini3939 0:684b6fdf080c 254 case MNE_ORI:
okini3939 0:684b6fdf080c 255 reg = reg || ! input(&inout, i);
okini3939 0:684b6fdf080c 256 break;
okini3939 0:684b6fdf080c 257 case MNE_ORP:
okini3939 0:684b6fdf080c 258 reg = reg || (input(&inout, i) && ! input(&inout_old, i), 1);
okini3939 0:684b6fdf080c 259 break;
okini3939 0:684b6fdf080c 260 case MNE_ORF:
okini3939 0:684b6fdf080c 261 reg = reg || (! input(&inout, i) && input(&inout_old, i), 1);
okini3939 0:684b6fdf080c 262 break;
okini3939 0:684b6fdf080c 263
okini3939 0:684b6fdf080c 264 case MNE_ANB:
okini3939 1:f8c5afc27878 265 if (pop(&j) || j == -1) return -1;
okini3939 0:684b6fdf080c 266 reg = reg && j;
okini3939 0:684b6fdf080c 267 break;
okini3939 0:684b6fdf080c 268 case MNE_ORB:
okini3939 1:f8c5afc27878 269 if (pop(&j) || j == -1) return -1;
okini3939 0:684b6fdf080c 270 reg = reg || j;
okini3939 0:684b6fdf080c 271 break;
okini3939 0:684b6fdf080c 272
okini3939 0:684b6fdf080c 273 case MNE_INV:
okini3939 0:684b6fdf080c 274 reg = ! reg;
okini3939 0:684b6fdf080c 275 break;
okini3939 0:684b6fdf080c 276
okini3939 0:684b6fdf080c 277 case MNE_MPS:
okini3939 1:f8c5afc27878 278 if (push(reg)) return -1;
okini3939 0:684b6fdf080c 279 break;
okini3939 0:684b6fdf080c 280 case MNE_MRD:
okini3939 1:f8c5afc27878 281 if (read(&reg) || reg == -1) return -1;
okini3939 0:684b6fdf080c 282 break;
okini3939 0:684b6fdf080c 283 case MNE_MPP:
okini3939 1:f8c5afc27878 284 if (pop(&reg) || reg == -1) return -1;
okini3939 0:684b6fdf080c 285 break;
okini3939 0:684b6fdf080c 286
okini3939 0:684b6fdf080c 287 case MNE_OUT:
okini3939 0:684b6fdf080c 288 case MNE_SET:
okini3939 0:684b6fdf080c 289 case MNE_RST:
okini3939 0:684b6fdf080c 290 output(i, reg, il[i].mnemonic);
okini3939 0:684b6fdf080c 291 break;
okini3939 0:684b6fdf080c 292 }
okini3939 0:684b6fdf080c 293 }
okini3939 0:684b6fdf080c 294
okini3939 0:684b6fdf080c 295 inout_old = inout_tmp;
okini3939 0:684b6fdf080c 296
okini3939 0:684b6fdf080c 297 #ifdef DEBUG
okini3939 0:684b6fdf080c 298 printf("timer0=%d(%d) timer1=%d(%d)\r\n", inout.timer_cnt[0], inout.timer_set[0], inout.timer_cnt[1], inout.timer_set[1]);
okini3939 0:684b6fdf080c 299 #endif
okini3939 0:684b6fdf080c 300 return 0;
okini3939 0:684b6fdf080c 301 }
okini3939 0:684b6fdf080c 302
okini3939 0:684b6fdf080c 303 // set callback function
okini3939 1:f8c5afc27878 304 struct tInOut* ILinterpreter::attach (float (*pf_i)(char, int, eEXPRESSION, int), void (*pf_o)(char, int, int, eMNEMONIC)) {
okini3939 0:684b6fdf080c 305 cb_input = pf_i;
okini3939 0:684b6fdf080c 306 cb_output = pf_o;
okini3939 1:f8c5afc27878 307 return &inout;
okini3939 0:684b6fdf080c 308 }
okini3939 0:684b6fdf080c 309
okini3939 0:684b6fdf080c 310 // timer 10Hz
okini3939 0:684b6fdf080c 311 void ILinterpreter::pool () {
okini3939 0:684b6fdf080c 312 int i;
okini3939 0:684b6fdf080c 313
okini3939 0:684b6fdf080c 314 if (! il_count) return;
okini3939 0:684b6fdf080c 315
okini3939 0:684b6fdf080c 316 // timer
okini3939 0:684b6fdf080c 317 for (i = 0; i < IL_TIMER_NUM; i ++) {
okini3939 0:684b6fdf080c 318 if (inout.timer_flg[i] && inout.timer_cnt[i] < inout.timer_set[i]) {
okini3939 0:684b6fdf080c 319 inout.timer_cnt[i] ++;
okini3939 0:684b6fdf080c 320 }
okini3939 0:684b6fdf080c 321 }
okini3939 0:684b6fdf080c 322 }
okini3939 0:684b6fdf080c 323
okini3939 0:684b6fdf080c 324 // load sub
okini3939 0:684b6fdf080c 325 void ILinterpreter::load_exp (int i, char *buf) {
okini3939 0:684b6fdf080c 326 int j, len;
okini3939 0:684b6fdf080c 327 char *tmp;
okini3939 0:684b6fdf080c 328
okini3939 0:684b6fdf080c 329 // MNEMONIC "KEY""num"==value
okini3939 0:684b6fdf080c 330 il[i].key = buf[0];
okini3939 0:684b6fdf080c 331 il[i].keynum = strtol(&buf[1], &tmp, 10);
okini3939 0:684b6fdf080c 332 il[i].expression = EXP_NULL;
okini3939 0:684b6fdf080c 333 il[i].value = 0;
okini3939 0:684b6fdf080c 334
okini3939 0:684b6fdf080c 335 if (tmp) {
okini3939 0:684b6fdf080c 336 // expression
okini3939 0:684b6fdf080c 337 for (j = 0; j < IL_EXP_NUM; j ++) {
okini3939 0:684b6fdf080c 338 len = strlen(exp_str[j].str);
okini3939 0:684b6fdf080c 339 if (strncmp(tmp, exp_str[j].str, len) == 0 && tmp[len] >= '0' && tmp[len] <= '9') {
okini3939 0:684b6fdf080c 340 // MNEMONIC KEYnum"==""value"
okini3939 0:684b6fdf080c 341 il[i].expression = exp_str[j].exp;
okini3939 0:684b6fdf080c 342 il[i].value = atof(&tmp[len]);
okini3939 0:684b6fdf080c 343 break;
okini3939 0:684b6fdf080c 344 }
okini3939 0:684b6fdf080c 345 }
okini3939 0:684b6fdf080c 346 }
okini3939 0:684b6fdf080c 347 }
okini3939 0:684b6fdf080c 348
okini3939 0:684b6fdf080c 349 // load IL file
okini3939 0:684b6fdf080c 350 int ILinterpreter::load (char *file) {
okini3939 0:684b6fdf080c 351 FILE *fp;
okini3939 0:684b6fdf080c 352 char c;
okini3939 0:684b6fdf080c 353 int i, j, len;
okini3939 0:684b6fdf080c 354 char buf[20];
okini3939 0:684b6fdf080c 355
okini3939 0:684b6fdf080c 356 il_count = 0;
okini3939 0:684b6fdf080c 357
okini3939 0:684b6fdf080c 358 fp = fopen(file, "r");
okini3939 0:684b6fdf080c 359 if (fp == NULL) return -1;
okini3939 0:684b6fdf080c 360
okini3939 0:684b6fdf080c 361 i = 0;
okini3939 0:684b6fdf080c 362 for (;;) {
okini3939 0:684b6fdf080c 363 if (il_count >= IL_NUM) break;
okini3939 0:684b6fdf080c 364 c = fgetc(fp);
okini3939 0:684b6fdf080c 365 if (feof(fp)) break;
okini3939 0:684b6fdf080c 366
okini3939 0:684b6fdf080c 367 if (c != '\r' && c != '\n' && i < 40 - 1) {
okini3939 0:684b6fdf080c 368 // load
okini3939 0:684b6fdf080c 369 buf[i] = c;
okini3939 0:684b6fdf080c 370 i ++;
okini3939 0:684b6fdf080c 371 continue;
okini3939 0:684b6fdf080c 372 }
okini3939 0:684b6fdf080c 373 buf[i] = 0;
okini3939 0:684b6fdf080c 374
okini3939 0:684b6fdf080c 375 if (i == 0 || buf[0] == '#' || buf[0] == ';') {
okini3939 0:684b6fdf080c 376 // comment
okini3939 0:684b6fdf080c 377 i = 0;
okini3939 0:684b6fdf080c 378 continue;
okini3939 0:684b6fdf080c 379 }
okini3939 0:684b6fdf080c 380
okini3939 0:684b6fdf080c 381 // mnemonic
okini3939 0:684b6fdf080c 382 for (j = 0; j < IL_MNE_NUM; j ++) {
okini3939 0:684b6fdf080c 383 len = strlen(mne_str[j].str);
okini3939 0:684b6fdf080c 384 if (strncmp(buf, mne_str[j].str, len) == 0 && (buf[len] == ' ' || buf[len] == 0)) {
okini3939 0:684b6fdf080c 385 // "MNEMONIC" KEYnum==value
okini3939 0:684b6fdf080c 386 il[il_count].mnemonic = mne_str[j].mne;
okini3939 0:684b6fdf080c 387 load_exp(il_count, &buf[len + 1]);
okini3939 0:684b6fdf080c 388 il_count ++;
okini3939 0:684b6fdf080c 389 break;
okini3939 0:684b6fdf080c 390 }
okini3939 0:684b6fdf080c 391 }
okini3939 0:684b6fdf080c 392 if (strncmp(buf, "END", 3) == 0) break;
okini3939 0:684b6fdf080c 393 if (j == IL_MNE_NUM) return -1;
okini3939 0:684b6fdf080c 394
okini3939 0:684b6fdf080c 395 i = 0;
okini3939 0:684b6fdf080c 396 }
okini3939 0:684b6fdf080c 397
okini3939 0:684b6fdf080c 398 // init
okini3939 0:684b6fdf080c 399 for (i = 0; i < il_count; i ++) {
okini3939 0:684b6fdf080c 400 if (il[i].mnemonic == MNE_DEF) {
okini3939 0:684b6fdf080c 401 switch (il[i].key) {
okini3939 0:684b6fdf080c 402 case 't': // Timer
okini3939 0:684b6fdf080c 403 inout.timer_set[il[i].keynum] = (int)il[i].value * 10;
okini3939 0:684b6fdf080c 404 break;
okini3939 0:684b6fdf080c 405
okini3939 0:684b6fdf080c 406 case 'c': // Counter
okini3939 0:684b6fdf080c 407 inout.count_set[il[i].keynum] = (int)il[i].value;
okini3939 0:684b6fdf080c 408 break;
okini3939 0:684b6fdf080c 409 }
okini3939 0:684b6fdf080c 410 }
okini3939 0:684b6fdf080c 411 }
okini3939 0:684b6fdf080c 412
okini3939 0:684b6fdf080c 413 fclose(fp);
okini3939 0:684b6fdf080c 414
okini3939 0:684b6fdf080c 415 #ifdef DEBUG
okini3939 0:684b6fdf080c 416 for (i = 0; i < il_count; i ++) {
okini3939 0:684b6fdf080c 417 printf("M=%d [%c%d] E=%d V=%f\r\n", il[i].mnemonic, il[i].key, il[i].keynum, il[i].expression, il[i].value);
okini3939 0:684b6fdf080c 418 }
okini3939 0:684b6fdf080c 419 #endif
okini3939 0:684b6fdf080c 420 return 0;
okini3939 0:684b6fdf080c 421 }
okini3939 1:f8c5afc27878 422
okini3939 1:f8c5afc27878 423 int ILinterpreter::push (int dat) {
okini3939 1:f8c5afc27878 424
okini3939 1:f8c5afc27878 425 if (addr >= IL_STACK) {
okini3939 1:f8c5afc27878 426 return -1;
okini3939 1:f8c5afc27878 427 }
okini3939 1:f8c5afc27878 428 stack[addr] = dat;
okini3939 1:f8c5afc27878 429 addr ++;
okini3939 1:f8c5afc27878 430 return dat;
okini3939 1:f8c5afc27878 431 }
okini3939 1:f8c5afc27878 432
okini3939 1:f8c5afc27878 433 int ILinterpreter::pop (int *dat) {
okini3939 1:f8c5afc27878 434
okini3939 1:f8c5afc27878 435 if (addr == 0) {
okini3939 1:f8c5afc27878 436 return -1;
okini3939 1:f8c5afc27878 437 }
okini3939 1:f8c5afc27878 438 addr --;
okini3939 1:f8c5afc27878 439 *dat = stack[addr];
okini3939 1:f8c5afc27878 440 return 0;
okini3939 1:f8c5afc27878 441 }
okini3939 1:f8c5afc27878 442
okini3939 1:f8c5afc27878 443 int ILinterpreter::read (int *dat) {
okini3939 1:f8c5afc27878 444
okini3939 1:f8c5afc27878 445 if (addr == 0) {
okini3939 1:f8c5afc27878 446 return -1;
okini3939 1:f8c5afc27878 447 }
okini3939 1:f8c5afc27878 448 *dat = stack[addr];
okini3939 1:f8c5afc27878 449 return 0;
okini3939 1:f8c5afc27878 450 }