00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "types.h"
00027 #include "decoder.h"
00028 #include "avrdevice.h"
00029
00030 #include "hwsreg.h"
00031 #include "helper.h"
00032 #include "flash.h"
00033 #include "rwmem.h"
00034 #include "ioregs.h"
00035 #include "avrerror.h"
00036
00037 #define MONSREG traceOut << (string)(*(core->status))
00038
00039 using namespace std;
00040
00042 #define INDEX_FROM_BITMASK(mask) \
00043 ( (mask) == 0x01 ? 0 \
00044 : (mask) == 0x02 ? 1 \
00045 : (mask) == 0x04 ? 2 \
00046 : (mask) == 0x08 ? 3 \
00047 : (mask) == 0x10 ? 4 \
00048 : (mask) == 0x20 ? 5 \
00049 : (mask) == 0x40 ? 6 \
00050 : (mask) == 0x80 ? 7 \
00051 : abort_in_expression() )
00052
00053 int abort_in_expression()
00054 {
00055 abort();
00056 return 0;
00057 }
00058
00059 int avr_op_ADC::Trace() {
00060 traceOut << "ADC R" << (int)R1 << ", R" << (int)R2 << " ";
00061 int ret = this->operator()();
00062 MONSREG;
00063 return ret;
00064 }
00065
00066 int avr_op_ADD::Trace() {
00067 traceOut << "ADD R" << (int)R1 << ", R" << (int)R2 << " ";
00068 int ret = this->operator()();
00069 MONSREG;
00070 return ret;
00071 }
00072
00073 int avr_op_ADIW::Trace() {
00074 traceOut << "ADIW R" << (int)Rl << ", " << (int)K << " ";
00075 int ret = this->operator()();
00076 MONSREG;
00077 return ret;
00078 }
00079
00080 int avr_op_AND::Trace() {
00081 traceOut << "AND R" << (int)R1 << ", R" << (int)R2 << " ";
00082 int ret=this->operator()();
00083 MONSREG;
00084 return ret;
00085 }
00086
00087 int avr_op_ANDI::Trace() {
00088 traceOut << "ANDI R" << (int)R1 << ", " << HexChar(K) << " ";
00089 int ret=this->operator()();
00090 MONSREG;
00091 return ret;
00092 }
00093
00094 int avr_op_ASR::Trace() {
00095 traceOut << "ASR R" << (int)R1 << " ";
00096 int ret = this->operator()();
00097 MONSREG;
00098 return ret;
00099 }
00100
00101 const char *opcodes_bclr[8]= {
00102 "CLC",
00103 "CLZ",
00104 "CLN",
00105 "CLV",
00106 "CLS",
00107 "CLH",
00108 "CLT",
00109 "CLI"
00110 };
00111
00112 int avr_op_BCLR::Trace() {
00113 traceOut << opcodes_bclr[Kbit] << " ";
00114 int ret = this->operator()();
00115 MONSREG;
00116 return ret;
00117 }
00118
00119 int avr_op_BLD::Trace() {
00120 traceOut << "BLD R" << (int)R1 << ", " << (int)Kbit << " ";
00121 int ret = this->operator()();
00122 return ret;
00123 }
00124
00125 const char *branch_opcodes_clear[8] = {
00126 "BRCC",
00127 "BRNE",
00128 "BRPL",
00129 "BRVC",
00130 "BRGE",
00131 "BRHC",
00132 "BRTC",
00133 "BRID"
00134 };
00135
00136 int avr_op_BRBC::Trace() {
00137 traceOut << branch_opcodes_clear[INDEX_FROM_BITMASK(bitmask)]
00138 << " ->" << HexShort(offset * 2) << " ";
00139 unsigned int oldPC = core->PC;
00140 int ret = this->operator()();
00141
00142 string sym(core->Flash->GetSymbolAtAddress(core->PC * 2));
00143 if(oldPC + 1 != core->PC) {
00144 traceOut << sym << " ";
00145 for(int len = sym.length(); len < 30; len++)
00146 traceOut << " ";
00147 }
00148
00149 return ret;
00150 }
00151
00152 const char *branch_opcodes_set[8] = {
00153 "BRCS",
00154 "BREQ",
00155 "BRMO",
00156 "BRVS",
00157 "BRLT",
00158 "BRHS",
00159 "BRTS",
00160 "BRIE"
00161 };
00162
00163 int avr_op_BRBS::Trace() {
00164 traceOut << branch_opcodes_set[INDEX_FROM_BITMASK(bitmask)]
00165 << " ->" << HexShort(offset * 2) << " ";
00166 unsigned int oldPC = core->PC;
00167 int ret=this->operator()();
00168
00169 string sym(core->Flash->GetSymbolAtAddress(core->PC * 2));
00170 if(oldPC + 1 != core->PC) {
00171 traceOut << sym << " ";
00172 for(int len = sym.length(); len < 30; len++)
00173 traceOut << " ";
00174 }
00175
00176 return ret;
00177 }
00178
00179 const char *opcodes_bset[8]= {
00180 "SEC",
00181 "SEZ",
00182 "SEN",
00183 "SEV",
00184 "SES",
00185 "SEH",
00186 "SET",
00187 "SEI"
00188 };
00189
00190 int avr_op_BSET::Trace() {
00191 traceOut << opcodes_bset[Kbit] << " ";
00192 int ret = this->operator()();
00193 MONSREG;
00194 return ret;
00195 }
00196
00197 int avr_op_BST::Trace() {
00198 traceOut << "BST R" << (int)R1 << ", " << (int)Kbit << " ";
00199 int ret = this->operator()();
00200 MONSREG;
00201 return ret;
00202 }
00203
00204 int avr_op_CALL::Trace() {
00205 word K_lsb = core->Flash->ReadMemWord((core->PC + 1) * 2);
00206 int k = (KH << 16) | K_lsb;
00207 traceOut << "CALL 0x" << hex << k * 2 << dec << " ";
00208 int ret = this->operator()();
00209 return ret;
00210 }
00211
00212 int avr_op_CBI::Trace() {
00213 traceOut << "CBI " << HexChar(ioreg) << ", " << (int)Kbit << " ";
00214 int ret = this->operator()();
00215 return ret;
00216 }
00217
00218 int avr_op_COM::Trace() {
00219 traceOut << "COM R" << (int)R1 << " ";
00220 int ret = this->operator()();
00221 MONSREG;
00222 return ret;
00223 }
00224
00225 int avr_op_CP::Trace() {
00226 traceOut << "CP R" << (int)R1 << ", R" << (int)R2 << " ";
00227 int ret = this->operator()();
00228 MONSREG;
00229 return ret;
00230 }
00231
00232 int avr_op_CPC::Trace() {
00233 traceOut << "CPC R" << (int)R1 << ", R" << (int)R2 << " ";
00234 int ret = this->operator()();
00235 MONSREG;
00236 return ret;
00237 }
00238
00239 int avr_op_CPI::Trace() {
00240 traceOut << "CPI R" << (int)R1 << ", " << HexChar(K) << " ";
00241 int ret = this->operator()();
00242 MONSREG;
00243 return ret;
00244 }
00245
00246 int avr_op_CPSE::Trace() {
00247 traceOut << "CPSE R" << (int)R1 << ", R" << (int)R2 << " ";
00248 int ret = this->operator()();
00249 return ret;
00250 }
00251
00252 int avr_op_DEC::Trace() {
00253 traceOut << "DEC R" << (int)R1 << " ";
00254 int ret = this->operator()();
00255 MONSREG;
00256 return ret;
00257 }
00258
00259 int avr_op_EICALL::Trace() {
00260 traceOut << "EICALL ";
00261 int ret = this->operator()();
00262 return ret;
00263 }
00264
00265 int avr_op_EIJMP::Trace() {
00266 traceOut << "EIJMP ";
00267 int ret = this->operator()();
00268 return ret;
00269 }
00270
00271 int avr_op_ELPM_Z::Trace() {
00272 traceOut << "ELPM R" << (int)R1 << ", Z " ;
00273 int ret = this->operator()();
00274
00275 unsigned char rampz = 0;
00276 if(core->rampz != NULL)
00277 rampz = core->rampz->GetRegVal();
00278 unsigned int Z = (rampz << 16) + core->GetRegZ();
00279
00280 traceOut << " Flash[0x" << hex << Z << dec << "] ";
00281
00282 return ret;
00283 }
00284
00285 int avr_op_ELPM_Z_incr::Trace() {
00286 traceOut << "ELPM R" << (int)R1 << ", Z+ ";
00287 unsigned char rampz = 0;
00288 if(core->rampz != NULL)
00289 rampz = core->rampz->GetRegVal();
00290 unsigned int Z = (rampz << 16) + core->GetRegZ();
00291 int ret = this->operator()();
00292
00293 traceOut << " Flash[0x" << hex << Z << dec << "] ";
00294
00295 return ret;
00296 }
00297
00298 int avr_op_ELPM::Trace() {
00299 traceOut << "ELPM ";
00300 int ret = this->operator()();
00301
00302 unsigned char rampz = 0;
00303 if(core->rampz != NULL)
00304 rampz = core->rampz->GetRegVal();
00305 unsigned int Z = (rampz << 16) + core->GetRegZ();
00306
00307 traceOut << " Flash[0x" << hex << Z << dec << "] ";
00308
00309 return ret;
00310 }
00311
00312 int avr_op_EOR::Trace() {
00313 traceOut << "EOR R" << (int)R1 << ", R" << (int)R2 << " ";
00314 int ret = this->operator()();
00315 MONSREG;
00316 return ret;
00317 }
00318
00319 int avr_op_ESPM::Trace() {
00320 traceOut << "SPM Z+ ";
00321 int ret = this->operator()();
00322 return ret;
00323 }
00324
00325 int avr_op_FMUL::Trace() {
00326 traceOut << "FMUL R" << (int)Rd << ", R" << (int)Rr << " ";
00327 int ret = this->operator()();
00328 MONSREG;
00329 return ret;
00330 }
00331
00332 int avr_op_FMULS::Trace() {
00333 traceOut << "FMULS R" << (int)Rd << ", R" << (int)Rr << " ";
00334 int ret = this->operator()();
00335 MONSREG;
00336 return ret;
00337 }
00338
00339 int avr_op_FMULSU::Trace() {
00340 traceOut << "FMULSU R" << (int)Rd << ", R" << (int)Rr << " ";
00341 int ret = this->operator()();
00342 MONSREG;
00343 return ret;
00344 }
00345
00346 int avr_op_ICALL::Trace() {
00347 traceOut << "ICALL Z " ;
00348 int ret = this->operator()();
00349 return ret;
00350 }
00351
00352 int avr_op_IJMP::Trace() {
00353 traceOut << "IJMP Z " ;
00354 int ret = this->operator()();
00355 return ret;
00356 }
00357
00358 int avr_op_IN::Trace() {
00359 traceOut << "IN R" << (int)R1 << ", " << HexChar(ioreg) << " ";
00360 int ret = this->operator()();
00361 return ret;
00362 }
00363
00364 int avr_op_INC::Trace() {
00365 traceOut << "INC R" << (int)R1 << " ";
00366 int ret = this->operator()();
00367 MONSREG;
00368 return ret;
00369 }
00370
00371 int avr_op_JMP::Trace() {
00372 traceOut << "JMP ";
00373 word offset = core->Flash->ReadMemWord((core->PC + 1) * 2);
00374 int ret = this->operator()();
00375 traceOut << hex << 2 * offset << dec << " ";
00376
00377 string sym(core->Flash->GetSymbolAtAddress(offset));
00378 traceOut << sym << " ";
00379 for(int len = sym.length(); len < 30; len++)
00380 traceOut << " " ;
00381
00382 return ret;
00383 }
00384
00385 int avr_op_LDD_Y::Trace() {
00386 traceOut << "LDD R" << (int)Rd << ", Y+" << (int)K << " ";
00387 int ret = this->operator()();
00388 return ret;
00389 }
00390
00391 int avr_op_LDD_Z::Trace() {
00392 traceOut << "LDD R" << (int)Rd << ", Z+" << (int)K << " ";
00393 int ret = this->operator()();
00394 return ret;
00395 }
00396
00397 int avr_op_LDI::Trace() {
00398 traceOut << "LDI R" << (int)R1 << ", " << HexChar(K) << " ";
00399 int ret = this->operator()();
00400 return ret;
00401 }
00402
00403 int avr_op_LDS::Trace() {
00404 word offset = core->Flash->ReadMemWord((core->PC + 1) * 2);
00405 traceOut << "LDS R" << (int)R1 << ", " << hex << "0x" << offset << dec << " ";
00406 int ret = this->operator()();
00407 return ret;
00408 }
00409
00410 int avr_op_LD_X::Trace() {
00411 traceOut << "LD R" << (int)Rd << ", X ";
00412 int ret = this->operator()();
00413 return ret;
00414 }
00415
00416 int avr_op_LD_X_decr::Trace() {
00417 traceOut << "LD R" << (int)Rd << ", -X ";
00418 int ret = this->operator()();
00419 return ret;
00420 }
00421
00422 int avr_op_LD_X_incr::Trace() {
00423 traceOut << "LD R" << (int)Rd << ", X+ ";
00424 int ret = this->operator()();
00425 return ret;
00426 }
00427
00428 int avr_op_LD_Y_decr::Trace() {
00429 traceOut << "LD R" << (int)Rd << ", -Y ";
00430 int ret = this->operator()();
00431 return ret;
00432 }
00433
00434 int avr_op_LD_Y_incr::Trace() {
00435 traceOut << "LD R" << (int)Rd << ", Y+ " ;
00436 int ret = this->operator()();
00437 return ret;
00438 }
00439
00440 int avr_op_LD_Z_incr::Trace() {
00441 traceOut << "LD R" << (int)Rd << ", Z+ ";
00442 int ret = this->operator()();
00443 return ret;
00444 }
00445
00446 int avr_op_LD_Z_decr::Trace() {
00447 traceOut << "LD R" << (int)Rd << ", -Z";
00448 int ret = this->operator()();
00449 return ret;
00450 }
00451
00452 int avr_op_LPM_Z::Trace() {
00453 traceOut << "LPM R" << (int)Rd << ", Z ";
00454 int ret = this->operator()();
00455
00456
00457 unsigned int Z = core->GetRegZ();
00458 string sym(core->Flash->GetSymbolAtAddress(Z));
00459 traceOut << "FLASH[" << hex << Z << dec << "," << sym << "] ";
00460
00461 return ret;
00462 }
00463
00464 int avr_op_LPM::Trace() {
00465 traceOut << "LPM R0, Z ";
00466 int ret = this->operator()();
00467
00468
00469 unsigned int Z = core->GetRegZ();
00470 string sym(core->Flash->GetSymbolAtAddress(Z));
00471 traceOut << "FLASH[" << hex << Z << dec << "," << sym << "] ";
00472
00473 return ret;
00474 }
00475
00476 int avr_op_LPM_Z_incr::Trace() {
00477 traceOut << "LPM R" << (int)Rd << ", Z+ " ;
00478
00479 unsigned int Z = core->GetRegZ();
00480 int ret = this->operator()();
00481
00482 string sym(core->Flash->GetSymbolAtAddress(Z));
00483 traceOut << "FLASH[" << hex << Z << dec << "," << sym << "] ";
00484 return ret;
00485 }
00486
00487 int avr_op_LSR::Trace() {
00488 traceOut << "LSR R" << (int)Rd << " ";
00489 int ret = this->operator()();
00490 MONSREG;
00491 return ret;
00492 }
00493
00494 int avr_op_MOV::Trace() {
00495 traceOut << "MOV R" << (int)R1 << ", R" << (int)R2 << " ";
00496 int ret = this->operator()();
00497 return ret;
00498 }
00499
00500 int avr_op_MOVW::Trace() {
00501 traceOut << "MOVW R" << (int)Rd << ", R" << (int)Rs << " ";
00502 int ret = this->operator()();
00503 return ret;
00504 }
00505
00506 int avr_op_MUL::Trace() {
00507 traceOut << "MUL R" << (int)Rd << ", R" << (int)Rr << " ";
00508 int ret = this->operator()();
00509 MONSREG;
00510 return ret;
00511 }
00512
00513 int avr_op_MULS::Trace() {
00514 traceOut << "MULS R" << (int)Rd << ", R" << (int)Rr << " ";
00515 int ret = this->operator()();
00516 MONSREG;
00517 return ret;
00518 }
00519
00520 int avr_op_MULSU::Trace() {
00521 traceOut << "MULSU R" << (int)Rd << ", R" << (int)Rr << " ";
00522 int ret = this->operator()();
00523 MONSREG;
00524 return ret;
00525 }
00526
00527 int avr_op_NEG::Trace() {
00528 traceOut << "NEG R" << (int)Rd <<" ";
00529 int ret = this->operator()();
00530 MONSREG;
00531 return ret;
00532 }
00533
00534 int avr_op_NOP::Trace() {
00535 traceOut << "NOP ";
00536 int ret = this->operator()();
00537 return ret;
00538 }
00539
00540 int avr_op_OR::Trace() {
00541 traceOut << "OR R" << (int)Rd << ", R" << (int)Rr << " ";
00542 int ret = this->operator()();
00543 MONSREG;
00544 return ret;
00545 }
00546
00547 int avr_op_ORI::Trace() {
00548 traceOut << "ORI R" << (int)R1 << ", " << HexChar(K) << " ";
00549 int ret = this->operator()();
00550 MONSREG;
00551 return ret;
00552 }
00553
00554 int avr_op_OUT::Trace() {
00555 traceOut << "OUT " << HexChar(ioreg) << ", R" << (int)R1 << " ";
00556 int ret = this->operator()();
00557 return ret;
00558 }
00559
00560 int avr_op_POP::Trace() {
00561 traceOut << "POP R" << (int)R1 << " ";
00562 int ret = this->operator()();
00563 return ret;
00564 }
00565
00566 int avr_op_PUSH::Trace() {
00567 traceOut << "PUSH R" << (int)R1 << " ";
00568 int ret = this->operator()();
00569 return ret;
00570 }
00571
00572 int avr_op_RCALL::Trace() {
00573 traceOut << "RCALL " << hex << ((core->PC + K + 1) << 1) << dec << " ";
00574 int ret = this->operator()();
00575 return ret;
00576 }
00577
00578 int avr_op_RET::Trace() {
00579 traceOut << "RET " ;
00580 int ret = this->operator()();
00581 return ret;
00582 }
00583
00584 int avr_op_RETI::Trace() {
00585 traceOut << "RETI ";
00586 int ret = this->operator()();
00587 return ret;
00588 }
00589
00590 int avr_op_RJMP::Trace() {
00591 traceOut << "RJMP " << hex << ((core->PC + K + 1) << 1) << dec << " ";
00592 int ret = this->operator()();
00593 return ret;
00594 }
00595
00596 int avr_op_ROR::Trace() {
00597 traceOut << "ROR R" << (int)R1 << " ";
00598 int ret = this->operator()();
00599 MONSREG;
00600 return ret;
00601 }
00602
00603 int avr_op_SBC::Trace() {
00604 traceOut << "SBC R" << (int)R1 << ", R" << (int)R2 << " ";
00605 int ret = this->operator()();
00606 MONSREG;
00607 return ret;
00608 }
00609
00610 int avr_op_SBCI::Trace() {
00611 traceOut << "SBCI R" << (int)R1 << ", " << HexChar(K) << " ";
00612 int ret = this->operator()();
00613 MONSREG;
00614 return ret;
00615 }
00616
00617 int avr_op_SBI::Trace() {
00618 traceOut << "SBI " << HexChar(ioreg) << ", " << (int)Kbit << " ";
00619 int ret = this->operator()();
00620 return ret;
00621 }
00622
00623 int avr_op_SBIC::Trace() {
00624 traceOut << "SBIC " << HexChar(ioreg) << ", " << (int)Kbit << " ";
00625 int ret = this->operator()();
00626 return ret;
00627 }
00628
00629 int avr_op_SBIS::Trace() {
00630 traceOut << "SBIS " << HexChar(ioreg) << ", " << (int)Kbit << " ";
00631 int ret = this->operator()();
00632 return ret;
00633 }
00634
00635 int avr_op_SBIW::Trace( ) {
00636 traceOut << "SBIW R" << (int)R1 << ", " << HexChar(K) << " ";
00637 int ret=this->operator()();
00638 MONSREG;
00639 return ret;
00640 }
00641
00642 int avr_op_SBRC::Trace() {
00643 traceOut << "SBRC R" << (int)R1 << ", " << (int)Kbit << " ";
00644 int ret = this->operator()();
00645 return ret;
00646 }
00647
00648 int avr_op_SBRS::Trace() {
00649 traceOut << "SBRS R" << (int)R1 << ", " << (int)Kbit << " ";
00650 int ret = this->operator()();
00651 return ret;
00652 }
00653
00654 int avr_op_SLEEP::Trace() {
00655 traceOut << "SLEEP " ;
00656 int ret = this->operator()();
00657 return ret;
00658 }
00659
00660 int avr_op_SPM::Trace() {
00661 traceOut << "SPM " ;
00662 int ret = this->operator()();
00663 return ret;
00664 }
00665
00666 int avr_op_STD_Y::Trace() {
00667 traceOut << "STD Y+" << (int)K << ", R" << (int)R1 << " ";
00668 int ret = this->operator()();
00669 return ret;
00670 }
00671
00672 int avr_op_STD_Z::Trace() {
00673 traceOut << "STD Z+" << (int)K << ", R" << (int)R1 << " ";
00674 int ret = this->operator()();
00675 return ret;
00676 }
00677
00678 int avr_op_STS::Trace() {
00679 word offset = core->Flash->ReadMemWord((core->PC + 1) * 2);
00680 traceOut << "STS " << "0x" << hex << offset << dec << ", R" << (int)R1 << " ";
00681 int ret = this->operator()();
00682 return ret;
00683 }
00684
00685 int avr_op_ST_X::Trace() {
00686 traceOut << "ST X, R" << (int)R1 << " ";
00687 int ret = this->operator()();
00688 return ret;
00689 }
00690
00691 int avr_op_ST_X_decr::Trace() {
00692 traceOut << "ST -X, R" << (int)R1 << " ";
00693 int ret = this->operator()();
00694 return ret;
00695 }
00696
00697 int avr_op_ST_X_incr::Trace() {
00698 traceOut << "ST X+, R" << (int)R1 << " ";
00699 int ret = this->operator()();
00700 return ret;
00701 }
00702
00703 int avr_op_ST_Y_decr::Trace() {
00704 traceOut << "ST -Y, R" << (int)R1 << " ";
00705 int ret = this->operator()();
00706 return ret;
00707 }
00708
00709 int avr_op_ST_Y_incr::Trace() {
00710 traceOut << "ST Y+, R" << (int)R1 << " ";
00711 int ret = this->operator()();
00712 return ret;
00713 }
00714
00715 int avr_op_ST_Z_decr::Trace() {
00716 traceOut << "ST -Z, R" << (int)R1 << " ";
00717 int ret = this->operator()();
00718 return ret;
00719 }
00720
00721 int avr_op_ST_Z_incr::Trace() {
00722 traceOut << "ST Z+, R" << (int)R1 << " ";
00723 int ret = this->operator()();
00724 return ret;
00725 }
00726
00727 int avr_op_SUB::Trace() {
00728 traceOut << "SUB R" << (int)R1 << ", R" << (int)R2 << " ";
00729 int ret = this->operator()();
00730 MONSREG;
00731 return ret;
00732 }
00733
00734 int avr_op_SUBI::Trace() {
00735 traceOut << "SUBI R" << (int)R1 << ", " << HexChar(K) << " ";
00736 int ret = this->operator()();
00737 MONSREG;
00738 return ret;
00739 }
00740
00741 int avr_op_SWAP::Trace() {
00742 traceOut << "SWAP R" << (int)R1 << " ";
00743 int ret = this->operator()();
00744 return ret;
00745 }
00746
00747 int avr_op_WDR::Trace() {
00748 traceOut << "WDR ";
00749 int ret = this->operator()();
00750 return ret;
00751 }
00752
00753 int avr_op_BREAK::Trace() {
00754 traceOut << "BREAK ";
00755 int ret = this->operator()();
00756 return ret;
00757 }
00758
00759 int avr_op_ILLEGAL::Trace() {
00760 traceOut << "Invalid Instruction! ";
00761 int ret = this->operator()();
00762 return ret;
00763 }
00764
00765