Bug fix: check if vm exists
[avr-sim.git] / InstructionsTrace.cpp
blob7c404612c2916239cb7a93350836bbbb207f8a85
1 #include "Instructions.h"
2 #include "Core.h"
3 #include "DecoderHelp.h"
5 #include <iostream>
7 #define reg(r) core->getR( (r) )
8 #define hexchar(a) std::hex << int(a) << std::dec
9 #define duoreg(Rh, Rl) core->getR(Rh) << ":" << core->getR(Rl)
11 static std::ostream & operator <<(std::ostream & ostr, const avr::Register & r) {
12 ostr << r.getName();
13 return ostr;
16 namespace avr {
18 namespace op {
20 int ADC::trace(Core *core, std::ostream & ostr) {
21 ostr << "ADC " << reg(reg(Rd)) << ", " << reg(reg(Rr));
22 return 1;
25 int ADD::trace(Core *core, std::ostream & ostr) {
26 ostr << "ADD " << reg(Rd) << ", " << reg(Rr);
27 return 2;
30 int ADIW::trace(Core *core, std::ostream & ostr) {
31 ostr << "ADIW " << duoreg(Rh, Rl) << ", " << hexchar(K);
32 return 1;
35 int AND::trace(Core *core, std::ostream & ostr) {
36 ostr << "AND R" << reg(Rd) << ", R" << reg(Rr);
37 return 1;
40 int ANDI::trace(Core *core, std::ostream & ostr) {
41 ostr << "ANDI R" << reg(Rd) << ", " << hexchar(K);
42 return 1;
45 int ASR::trace(Core *core, std::ostream & ostr) {
46 ostr << "ASR R" << reg(Rd);
47 return 1;
50 char *opcodes_bclr[8]= {
51 "CLC",
52 "CLZ",
53 "CLN",
54 "CLV",
55 "CLS",
56 "CLH",
57 "CLT",
58 "CLI"
61 int BCLR::trace(Core *core, std::ostream & ostr) {
62 int i;
63 for(i = 0; i < 8; ++i)
64 if( K == ~(1<<i) )
65 break;
67 ostr << opcodes_bclr[i];
68 return 1;
71 int BLD::trace(Core *core, std::ostream & ostr) {
72 ostr << "BLD " << reg(Rd) << ", " << hexchar(Kadd);
73 return 1;
76 char *branch_opcodes_clear[8] = {
77 "BRCC",
78 "BRNE",
79 "BRPL",
80 "BRVC",
81 "BRGE",
82 "BRHC",
83 "BRTC",
84 "BRID"
87 int BRBC::trace(Core *core, std::ostream & ostr) {
88 int i;
89 for(i = 0; i < 8; ++i)
90 if( bitmask == ~(1<<i) )
91 break;
93 ostr << branch_opcodes_clear[i] << " " << int(char(offset) * 2);
95 string sym(core->Flash->GetSymbolAtAddress(core->PC+1+p2));
96 ostr << sym << " ";
97 for (int len = sym.length(); len<30;len++) { ostr << " " ; }
99 return 1;
102 char *branch_opcodes_set[8] = {
103 "BRCS",
104 "BREQ",
105 "BRMO",
106 "BRVS",
107 "BRLT",
108 "BRHS",
109 "BRTS",
110 "BRIE"
113 int BRBS::trace(Core *core, std::ostream & ostr) {
114 int i;
115 for(i = 0; i < 8; ++i)
116 if( bitmask == (1<<i) )
117 break;
119 ostr << branch_opcodes_set[i] << " " << int(char(offset)*2);
120 return 1;
123 char *opcodes_bset[8]= {
124 "SEC",
125 "SEZ",
126 "SEN",
127 "SEV",
128 "SES",
129 "SEH",
130 "SET",
131 "SEI"
134 int BSET::trace(Core *core, std::ostream & ostr) {
135 int i;
136 for(i = 0; i < 8; ++i)
137 if( K == ~(1<<i) )
138 break;
140 ostr << opcodes_bset[i];
141 return 1;
144 int BST::trace(Core *core, std::ostream & ostr) {
145 ostr << "BST " << reg(Rd) << ", " << hexchar(K);
146 return 1;
149 int CALL::trace(Core *core, std::ostream & ostr) {
150 ostr << "CALL " << hexchar(KH);
151 return core->pcBytes() + 2;
154 int CBI::trace(Core *core, std::ostream & ostr) {
155 ostr << "CBI " << core->getIoreg(ioreg) << ", " << hexchar(K);
156 return 2;
159 int COM::trace(Core *core, std::ostream & ostr) {
160 ostr << "COM " << reg(Rd);
161 return 1;
164 int CP::trace(Core *core, std::ostream & ostr) {
165 ostr << "CP " << reg(Rd) << ", " << reg(Rr);
166 return 1;
169 int CPC::trace(Core *core, std::ostream & ostr) {
170 ostr << "CPC " << reg(Rd) << ", " << reg(Rr);
171 return 1;
174 int CPI::trace(Core *core, std::ostream & ostr) {
175 ostr << "CPI " << reg(Rd) << ", " << hexchar(K);
176 return 1;
179 int CPSE::trace(Core *core, std::ostream & ostr) {
180 ostr << "CPSE " << reg(Rd) << ", " << reg(Rr);
181 return 1;
184 int DEC::trace(Core *core, std::ostream & ostr) {
185 ostr << "DEC " << reg(Rd);
186 return 1;
189 int EICALL::trace(Core *core, std::ostream & ostr) {
190 ostr << "EICALL";
191 return 4;
194 int EIJMP::trace(Core *core, std::ostream & ostr) {
195 ostr << "EIJMP";
196 return 2;
199 int ELPM_Z::trace(Core *core, std::ostream & ostr) {
200 ostr << "ELPM " << reg(Rd) << ", Z";
202 unsigned int Z = ((core->GetRampz() & 0x3f) << 16) +
203 (((*(core->R))[31]) << 8) +
204 (*(core->R))[30];
206 unsigned int flash_addr = Z;// / 2;
208 ostr << " Flash[0x"<<hex<< (unsigned int) flash_addr<<"] ";
210 return 3;
213 int ELPM_Z_incr::trace(Core *core, std::ostream & ostr) {
214 ostr << "ELPM " << reg(Rd) << ", Z+";
216 /* unsigned int Z = ((core->GetRampz() & 0x3f) << 16) +
217 (((*(core->R))[31]) << 8) +
218 (*(core->R))[30];
220 unsigned int flash_addr = Z; // / 2;
222 ostr << " Flash[0x"<<hex<< (unsigned int) flash_addr<<"] ";
225 return 3;
228 int ELPM::trace(Core *core, std::ostream & ostr) {
229 ostr << "ELPM";
231 /* unsigned int Z = ((core->GetRampz() & 0x3f) << 16) +
232 (((*(core->R))[31]) << 8) +
233 (*(core->R))[30];
235 unsigned int flash_addr = Z; // / 2;
237 ostr << " Flash[0x"<<hex<< (unsigned int) flash_addr<<"] ";
239 return 3;
242 int EOR::trace(Core *core, std::ostream & ostr) {
243 ostr << "EOR " << reg(Rd) << ", " << reg(Rr);
244 return 1;
247 int ESPM::trace(Core *core, std::ostream & ostr) {
248 ostr << "ESPM";
249 return 0;
252 int FMUL::trace(Core *core, std::ostream & ostr) {
253 ostr << "FMUL " << reg(Rd) << ", " << reg(Rr);
254 return 2;
257 int FMULS::trace(Core *core, std::ostream & ostr) {
258 ostr << "FMULS " << reg(Rd) << ", " << reg(Rr);
259 return 2;
262 int FMULSU::trace(Core *core, std::ostream & ostr) {
263 ostr << "FMULSU " << reg(Rd) << ", " << reg(Rr);
264 return 2;
267 int ICALL::trace(Core *core, std::ostream & ostr) {
268 ostr << "ICALL";
269 return core->pcBytes() + 1;
272 int IJMP::trace(Core *core, std::ostream & ostr) {
273 ostr << "IJMP";
274 return 2;
277 int IN::trace(Core *core, std::ostream & ostr) {
278 ostr << "IN " << reg(Rd) << ", " << core->getIoreg(ioreg);
279 return 1;
282 int INC::trace(Core *core, std::ostream & ostr) {
283 ostr << "INC " << reg(Rd);
284 return 1;
287 int JMP::trace(Core *core, std::ostream & ostr) {
288 ostr << "JMP";
289 return 3;
292 int LDD_Y::trace(Core *core, std::ostream & ostr) {
293 ostr << "LD " << reg(Rd) << ", Y+" << hexchar(K);
294 return 2;
297 int LDD_Z::trace(Core *core, std::ostream & ostr) {
298 ostr << "LDD " << reg(Rd) << ", Z";
299 return 2;
302 int LDI::trace(Core *core, std::ostream & ostr) {
303 ostr << "LDI " << reg(Rd) << ", " << hexchar(K);
304 return 1;
307 int LDS::trace(Core *core, std::ostream & ostr) {
308 ostr << "LDS " << reg(Rd);
309 //<< ", " << hex << "0x" << offset << dec << " ";
310 return 2;
313 int LD_X::trace(Core *core, std::ostream & ostr) {
314 ostr << "LD " << reg(Rd) << ", X ";
315 return 2;
318 int LD_X_decr::trace(Core *core, std::ostream & ostr) {
319 ostr << "LD " << reg(Rd) << ", -X";
320 return 2;
323 int LD_X_incr::trace(Core *core, std::ostream & ostr) {
324 ostr << "LD " << reg(Rd) << ", X+";
325 return 2;
328 int LD_Y_decr::trace(Core *core, std::ostream & ostr) {
329 ostr << "LD " << reg(Rd) << ", -Y";
330 return 2;
333 int LD_Y_incr::trace(Core *core, std::ostream & ostr) {
334 ostr << "LD " << reg(Rd) << ", Y+";
335 return 2;
338 int LD_Z_incr::trace(Core *core, std::ostream & ostr) {
339 ostr << "LD " << reg(Rd) << ", Z+";
340 return 2;
343 int LD_Z_decr::trace(Core *core, std::ostream & ostr) {
344 ostr << "LD R" << reg(Rd) << ", -Z";
345 return 2;
348 int LPM_Z::trace(Core *core, std::ostream & ostr) {
349 ostr << "LPM " << reg(Rd) << ", Z";
351 /* Z is R31:R30 */
352 //int Z = ((*(core->R))[ 31] << 8) + (*(core->R))[ 30];
353 //string sym(core->Flash->GetSymbolAtAddressLpm(Z));
354 //string sym(core->Flash->GetSymbolAtAddress(Z));
355 //ostr << "FLASH[" << hex << Z << "," << sym << "] ";
357 return 3;
360 int LPM::trace(Core *core, std::ostream & ostr) {
361 ostr << "LPM";
363 /* Z is R31:R30 */
364 //int Z = ((*(core->R))[ 31] << 8) + (*(core->R))[ 30];
365 //string sym(core->Flash->GetSymbolAtAddressLpm(Z));
366 //string sym(core->Flash->GetSymbolAtAddress(Z));
367 //ostr << "FLASH[" << hex << Z << "," << sym << "] ";
369 return 3;
372 int LPM_Z_incr::trace(Core *core, std::ostream & ostr) {
373 ostr << "LPM " << reg(Rd) << ", Z+";
375 /* Z is R31:R30 */
376 //int Z = ((*(core->R))[ 31] << 8) + (*(core->R))[ 30];
377 //string sym(core->Flash->GetSymbolAtAddressLpm(Z));
378 //string sym(core->Flash->GetSymbolAtAddress(Z));
379 //ostr << "FLASH[" << hex << Z << "," << sym << "] ";
381 return 3;
384 int LSR::trace(Core *core, std::ostream & ostr) {
385 ostr << "LSR " << reg(Rd);
386 return 1;
389 int MOV::trace(Core *core, std::ostream & ostr) {
390 ostr << "MOV " << reg(Rd) << ", " << reg(Rr);
391 return 1;
394 int MOVW::trace(Core *core, std::ostream & ostr) {
395 ostr << "MOVW " << duoreg(Rdh,Rdl) << ", " << duoreg(Rrh,Rrl);
396 return 1;
399 int MUL::trace(Core *core, std::ostream & ostr) {
400 ostr << "MUL " << reg(Rd) << ", " << reg(Rr);
401 return 2;
404 int MULS::trace(Core *core, std::ostream & ostr) {
405 ostr << "MULS " << reg(Rd) << ", " << reg(Rr);
406 return 2;
409 int MULSU::trace(Core *core, std::ostream & ostr) {
410 ostr << "MULSU " << reg(Rd) << ", " << reg(Rr);
411 return 2;
414 int NEG::trace(Core *core, std::ostream & ostr) {
415 ostr << "NEG " << reg(Rd);
416 return 1;
419 int NOP::trace(Core *core, std::ostream & ostr) {
420 ostr << "NOP";
421 return 1;
424 int OR::trace(Core *core, std::ostream & ostr) {
425 ostr << "OR " << reg(Rd) << ", " << reg(Rr);
426 return 1;
429 int ORI::trace(Core *core, std::ostream & ostr) {
430 ostr << "ORI " << reg(Rd) << ", " << hexchar(K);
431 return 1;
434 int OUT::trace(Core *core, std::ostream & ostr) {
435 ostr << "OUT " << core->getIoreg(ioreg) << ", " << reg(Rd);
436 return 1;
439 int POP::trace(Core *core, std::ostream & ostr) {
440 ostr << "POP " << reg(Rd);
441 return 2;
444 int PUSH::trace(Core *core, std::ostream & ostr) {
445 ostr << "PUSH " << reg(Rd);
446 return 2;
449 int RCALL::trace(Core *core, std::ostream & ostr) {
450 ostr << "RCALL " << hexchar(K);
451 return 1;
454 int RET::trace(Core *core, std::ostream & ostr) {
455 ostr << "RET";
456 return 2;
459 int RETI::trace(Core *core, std::ostream & ostr) {
460 ostr << "RETI";
461 return 2;
464 int RJMP::trace(Core *core, std::ostream & ostr) {
465 ostr << "RJMP " << int(char(K))*2;
466 return 2;
469 int ROR::trace(Core *core, std::ostream & ostr) {
470 ostr << "ROR " << reg(Rd);
471 return 1;
474 int SBC::trace(Core *core, std::ostream & ostr) {
475 ostr << "SBC " << reg(Rd) << ", " << reg(Rr);
476 return 1;
479 int SBCI::trace(Core *core, std::ostream & ostr) {
480 ostr << "SBCI " << reg(Rd) << ", " << hexchar(K);
481 return 1;
484 int SBI::trace(Core *core, std::ostream & ostr) {
485 ostr << "SBI " << core->getIoreg(ioreg) << ", " << hexchar(K);
486 return 2;
489 int SBIC::trace(Core *core, std::ostream & ostr) {
490 ostr << "SBIC " << core->getIoreg(ioreg) << ", " << hexchar(K);
491 return 2;
494 int SBIS::trace(Core *core, std::ostream & ostr) {
495 ostr << "SBIS " << core->getIoreg(ioreg) << ", " << hexchar(K);
496 return 2;
499 int SBIW::trace(Core *core, std::ostream & ostr) {
500 ostr << "SBIW " << duoreg(Rh,Rl) << ", " << hexchar(K);
501 return 2;
504 int SBRC::trace(Core *core, std::ostream & ostr) {
505 ostr << "SBRC " << reg(Rd) << ", " << hexchar(K);
506 return 1;
509 int SBRS::trace(Core *core, std::ostream & ostr) {
510 ostr << "SBRS " << reg(Rd) << ", " << hexchar(K);
511 return 1;
514 int SLEEP::trace(Core *core, std::ostream & ostr) {
515 ostr << "SLEEP";
516 return 0;
519 int SPM::trace(Core *core, std::ostream & ostr) {
520 ostr << "SPM";
521 return 0;
524 int STD_Y::trace(Core *core, std::ostream & ostr) {
525 ostr << "STD Y+"<< hexchar(K) <<", " << reg(Rd);
526 return 2;
529 int STD_Z::trace(Core *core, std::ostream & ostr) {
530 ostr << "STD Z, " << reg(Rd);
531 return 2;
534 int STS::trace(Core *core, std::ostream & ostr) {
535 ostr << "STS " << reg(Rd);
536 return 2;
539 int ST_X::trace(Core *core, std::ostream & ostr) {
540 ostr << "ST X, " << reg(Rd);
541 return 2;
544 int ST_X_decr::trace(Core *core, std::ostream & ostr) {
545 ostr << "ST -X, " << reg(Rd);
546 return 2;
549 int ST_X_incr::trace(Core *core, std::ostream & ostr) {
550 ostr << "ST X+, " << reg(Rd);
551 return 2;
554 int ST_Y_decr::trace(Core *core, std::ostream & ostr) {
555 ostr << "ST -Y, " << reg(Rd);
556 return 2;
559 int ST_Y_incr::trace(Core *core, std::ostream & ostr) {
560 ostr << "ST Y+, " << reg(Rd);
561 return 2;
564 int ST_Z_decr::trace(Core *core, std::ostream & ostr) {
565 ostr << "ST -Z, " << reg(Rd);
566 return 2;
569 int ST_Z_incr::trace(Core *core, std::ostream & ostr) {
570 ostr << "ST Z+, " << reg(Rd);
571 return 2;
574 int SUB::trace(Core *core, std::ostream & ostr) {
575 ostr << "SUB " << reg(Rd) << ", " << reg(Rr);
576 return 1;
579 int SUBI::trace(Core *core, std::ostream & ostr) {
580 ostr << "SUBI " << reg(Rd) << ", " << hexchar(K);
581 return 1;
584 int SWAP::trace(Core *core, std::ostream & ostr) {
585 ostr << "SWAP " << reg(Rd);
586 return 1;
589 int WDR::trace(Core *core, std::ostream & ostr) {
590 ostr << "WDR";
591 return 1;
594 int BREAK::trace(Core *core, std::ostream & ostr) {
595 ostr << "BREAK";
596 return 1;
599 int ILLEGAL::trace(Core *core, std::ostream & ostr) {
600 return 0;