misc: Fixed clang static analizer warnings
[simavr] / simavr / sim / sim_core.c
1 /*
2         sim_core.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include "sim_avr.h"
27 #include "sim_core.h"
28 #include "avr_flash.h"
29 #include "avr_watchdog.h"
30
31 // SREG bit names
32 const char * _sreg_bit_name = "cznvshti";
33
34 /*
35  * Handle "touching" registers, marking them changed.
36  * This is used only for debugging purposes to be able to
37  * print the effects of each instructions on registers
38  */
39 #if CONFIG_SIMAVR_TRACE
40
41 #define T(w) w
42
43 #define REG_TOUCH(a, r) (a)->touched[(r) >> 5] |= (1 << ((r) & 0x1f))
44 #define REG_ISTOUCHED(a, r) ((a)->touched[(r) >> 5] & (1 << ((r) & 0x1f)))
45
46 /*
47  * This allows a "special case" to skip indtruction tracing when in these
48  * symbols. since printf() is useful to have, but generates a lot of cycles
49  */
50 int dont_trace(const char * name)
51 {
52         return (
53                 !strcmp(name, "uart_putchar") ||
54                 !strcmp(name, "fputc") ||
55                 !strcmp(name, "printf") ||
56                 !strcmp(name, "vfprintf") ||
57                 !strcmp(name, "__ultoa_invert") ||
58                 !strcmp(name, "__prologue_saves__") ||
59                 !strcmp(name, "__epilogue_restores__"));
60 }
61
62 int donttrace = 0;
63
64 #define STATE(_f, args...) { \
65         if (avr->trace) {\
66                 if (avr->codeline && avr->codeline[avr->pc>>1]) {\
67                         const char * symn = avr->codeline[avr->pc>>1]->symbol; \
68                         int dont = 0 && dont_trace(symn);\
69                         if (dont!=donttrace) { \
70                                 donttrace = dont;\
71                                 DUMP_REG();\
72                         }\
73                         if (donttrace==0)\
74                                 printf("%04x: %-25s " _f, avr->pc, symn, ## args);\
75                 } else \
76                         printf("%s: %04x: " _f, __FUNCTION__, avr->pc, ## args);\
77                 }\
78         }
79 #define SREG() if (avr->trace && donttrace == 0) {\
80         printf("%04x: \t\t\t\t\t\t\t\t\tSREG = ", avr->pc); \
81         for (int _sbi = 0; _sbi < 8; _sbi++)\
82                 printf("%c", avr->sreg[_sbi] ? toupper(_sreg_bit_name[_sbi]) : '.');\
83         printf("\n");\
84 }
85 #else
86 #define T(w)
87 #define REG_TOUCH(a, r)
88 #define STATE(_f, args...)
89 #define SREG()
90 #endif
91
92 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
93 {
94         if (addr > avr->ramend) {
95                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
96                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
97                 CRASH();
98         }
99         if (addr < 32) {
100                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
101                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
102                 CRASH();
103         }
104 #if AVR_STACK_WATCH
105         /*
106          * this checks that the current "function" is not doctoring the stack frame that is located
107          * higher on the stack than it should be. It's a sign of code that has overrun it's stack
108          * frame and is munching on it's own return address.
109          */
110         if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
111                 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
112         }
113 #endif
114         avr->data[addr] = v;
115 }
116
117 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
118 {
119         if (addr > avr->ramend) {
120                 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
121                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
122                 CRASH();
123         }
124         return avr->data[addr];
125 }
126
127 /*
128  * Set a register (r < 256)
129  * if it's an IO regisrer (> 31) also (try to) call any callback that was
130  * registered to track changes to that register.
131  */
132 static inline void _avr_set_r(avr_t * avr, uint8_t r, uint8_t v)
133 {
134         REG_TOUCH(avr, r);
135
136         if (r == R_SREG) {
137                 avr->data[R_SREG] = v;
138                 // unsplit the SREG
139                 for (int i = 0; i < 8; i++)
140                         avr->sreg[i] = (v & (1 << i)) != 0;
141                 SREG();
142         }
143         if (r > 31) {
144                 uint8_t io = AVR_DATA_TO_IO(r);
145                 if (avr->io[io].w.c)
146                         avr->io[io].w.c(avr, r, v, avr->io[io].w.param);
147                 else
148                         avr->data[r] = v;
149                 if (avr->io[io].irq) {
150                         avr_raise_irq(avr->io[io].irq + AVR_IOMEM_IRQ_ALL, v);
151                         for (int i = 0; i < 8; i++)
152                                 avr_raise_irq(avr->io[io].irq + i, (v >> i) & 1);                               
153                 }
154         } else
155                 avr->data[r] = v;
156 }
157
158 /*
159  * Stack pointer access
160  */
161 inline uint16_t _avr_sp_get(avr_t * avr)
162 {
163         return avr->data[R_SPL] | (avr->data[R_SPH] << 8);
164 }
165
166 inline void _avr_sp_set(avr_t * avr, uint16_t sp)
167 {
168         _avr_set_r(avr, R_SPL, sp);
169         _avr_set_r(avr, R_SPH, sp >> 8);
170 }
171
172 /*
173  * Set any address to a value; split between registers and SRAM
174  */
175 static inline void _avr_set_ram(avr_t * avr, uint16_t addr, uint8_t v)
176 {
177         if (addr < 256)
178                 _avr_set_r(avr, addr, v);
179         else
180                 avr_core_watch_write(avr, addr, v);
181 }
182
183 /*
184  * Get a value from SRAM.
185  */
186 static inline uint8_t _avr_get_ram(avr_t * avr, uint16_t addr)
187 {
188         if (addr == R_SREG) {
189                 /*
190                  * SREG is special it's reconstructed when read
191                  * while the core itself uses the "shortcut" array
192                  */
193                 avr->data[R_SREG] = 0;
194                 for (int i = 0; i < 8; i++)
195                         if (avr->sreg[i] > 1) {
196                                 printf("** Invalid SREG!!\n");
197                                 CRASH();
198                         } else if (avr->sreg[i])
199                                 avr->data[R_SREG] |= (1 << i);
200                 
201         } else if (addr > 31 && addr < 256) {
202                 uint8_t io = AVR_DATA_TO_IO(addr);
203                 
204                 if (avr->io[io].r.c)
205                         avr->data[addr] = avr->io[io].r.c(avr, addr, avr->io[io].r.param);
206                 
207                 if (avr->io[io].irq) {
208                         uint8_t v = avr->data[addr];
209                         avr_raise_irq(avr->io[io].irq + AVR_IOMEM_IRQ_ALL, v);
210                         for (int i = 0; i < 8; i++)
211                                 avr_raise_irq(avr->io[io].irq + i, (v >> i) & 1);                               
212                 }
213         }
214         return avr_core_watch_read(avr, addr);
215 }
216
217 /*
218  * Stack push accessors. Push/pop 8 and 16 bits
219  */
220 static inline void _avr_push8(avr_t * avr, uint16_t v)
221 {
222         uint16_t sp = _avr_sp_get(avr);
223         _avr_set_ram(avr, sp, v);
224         _avr_sp_set(avr, sp-1);
225 }
226
227 static inline uint8_t _avr_pop8(avr_t * avr)
228 {
229         uint16_t sp = _avr_sp_get(avr) + 1;
230         uint8_t res = _avr_get_ram(avr, sp);
231         _avr_sp_set(avr, sp);
232         return res;
233 }
234
235 inline void _avr_push16(avr_t * avr, uint16_t v)
236 {
237         _avr_push8(avr, v);
238         _avr_push8(avr, v >> 8);
239 }
240
241 static inline uint16_t _avr_pop16(avr_t * avr)
242 {
243         uint16_t res = _avr_pop8(avr) << 8;
244         res |= _avr_pop8(avr);
245         return res;
246 }
247
248 /*
249  * "Pretty" register names
250  */
251 const char * reg_names[255] = {
252                 [R_XH] = "XH", [R_XL] = "XL",
253                 [R_YH] = "YH", [R_YL] = "YL",
254                 [R_ZH] = "ZH", [R_ZL] = "ZL",
255                 [R_SPH] = "SPH", [R_SPL] = "SPL",
256                 [R_SREG] = "SREG",
257 };
258
259
260 const char * avr_regname(uint8_t reg)
261 {
262         if (!reg_names[reg]) {
263                 char tt[16];
264                 if (reg < 32)
265                         sprintf(tt, "r%d", reg);
266                 else
267                         sprintf(tt, "io:%02x", reg);
268                 reg_names[reg] = strdup(tt);
269         }
270         return reg_names[reg];
271 }
272
273 /*
274  * Called when an invalid opcode is decoded
275  */
276 static void _avr_invalid_opcode(avr_t * avr)
277 {
278 #if CONFIG_SIMAVR_TRACE
279         printf("\e[31m*** %04x: %-25s Invalid Opcode SP=%04x O=%04x \e[0m\n",
280                         avr->pc, avr->codeline[avr->pc>>1]->symbol, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc+1]<<8));
281 #else
282         printf("\e[31m*** %04x: Invalid Opcode SP=%04x O=%04x \e[0m\n",
283                         avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc+1]<<8));
284 #endif
285 }
286
287 #if CONFIG_SIMAVR_TRACE
288 /*
289  * Dump changed registers when tracing
290  */
291 void avr_dump_state(avr_t * avr)
292 {
293         if (!avr->trace || donttrace)
294                 return;
295
296         int doit = 0;
297
298         for (int r = 0; r < 3 && !doit; r++)
299                 if (avr->touched[r])
300                         doit = 1;
301         if (!doit)
302                 return;
303         printf("                                       ->> ");
304         const int r16[] = { R_SPL, R_XL, R_YL, R_ZL };
305         for (int i = 0; i < 4; i++)
306                 if (REG_ISTOUCHED(avr, r16[i]) || REG_ISTOUCHED(avr, r16[i]+1)) {
307                         REG_TOUCH(avr, r16[i]);
308                         REG_TOUCH(avr, r16[i]+1);
309                 }
310
311         for (int i = 0; i < 3*32; i++)
312                 if (REG_ISTOUCHED(avr, i)) {
313                         printf("%s=%02x ", avr_regname(i), avr->data[i]);
314                 }
315         printf("\n");
316 }
317 #endif
318
319 #define get_r_d_10(o) \
320                 const uint8_t r = ((o >> 5) & 0x10) | (o & 0xf); \
321                 const uint8_t d = (o >> 4) & 0x1f;\
322                 const uint8_t vd = avr->data[d], vr = avr->data[r];
323 #define get_k_r16(o) \
324                 const uint8_t r = 16 + ((o >> 4) & 0xf); \
325                 const uint8_t k = ((o & 0x0f00) >> 4) | (o & 0xf);
326
327 /*
328  * Add a "jump" address to the jump trace buffer
329  */
330 #if CONFIG_SIMAVR_TRACE
331 #define TRACE_JUMP()\
332         avr->old[avr->old_pci].pc = avr->pc;\
333         avr->old[avr->old_pci].sp = _avr_sp_get(avr);\
334         avr->old_pci = (avr->old_pci + 1) & (OLD_PC_SIZE-1);\
335
336 #if AVR_STACK_WATCH
337 #define STACK_FRAME_PUSH()\
338         avr->stack_frame[avr->stack_frame_index].pc = avr->pc;\
339         avr->stack_frame[avr->stack_frame_index].sp = _avr_sp_get(avr);\
340         avr->stack_frame_index++; 
341 #define STACK_FRAME_POP()\
342         if (avr->stack_frame_index > 0) \
343                 avr->stack_frame_index--;
344 #else
345 #define STACK_FRAME_PUSH()
346 #define STACK_FRAME_POP()
347 #endif
348 #else /* CONFIG_SIMAVR_TRACE */
349
350 #define TRACE_JUMP()
351 #define STACK_FRAME_PUSH()
352 #define STACK_FRAME_POP()
353
354 #endif
355
356 /****************************************************************************\
357  *
358  * Helper functions for calculating the status register bit values.
359  * See the Atmel data sheet for the instruction set for more info.
360  *
361 \****************************************************************************/
362
363 static uint8_t
364 get_add_carry (uint8_t res, uint8_t rd, uint8_t rr, int b)
365 {
366     uint8_t resb = res >> b & 0x1;
367     uint8_t rdb = rd >> b & 0x1;
368     uint8_t rrb = rr >> b & 0x1;
369     return (rdb & rrb) | (rrb & ~resb) | (~resb & rdb);
370 }
371
372 static  uint8_t
373 get_add_overflow (uint8_t res, uint8_t rd, uint8_t rr)
374 {
375     uint8_t res7 = res >> 7 & 0x1;
376     uint8_t rd7 = rd >> 7 & 0x1;
377     uint8_t rr7 = rr >> 7 & 0x1;
378     return (rd7 & rr7 & ~res7) | (~rd7 & ~rr7 & res7);
379 }
380
381 static  uint8_t
382 get_sub_carry (uint8_t res, uint8_t rd, uint8_t rr, int b)
383 {
384     uint8_t resb = res >> b & 0x1;
385     uint8_t rdb = rd >> b & 0x1;
386     uint8_t rrb = rr >> b & 0x1;
387     return (~rdb & rrb) | (rrb & resb) | (resb & ~rdb);
388 }
389
390 static  uint8_t
391 get_sub_overflow (uint8_t res, uint8_t rd, uint8_t rr)
392 {
393     uint8_t res7 = res >> 7 & 0x1;
394     uint8_t rd7 = rd >> 7 & 0x1;
395     uint8_t rr7 = rr >> 7 & 0x1;
396     return (rd7 & ~rr7 & ~res7) | (~rd7 & rr7 & res7);
397 }
398
399 static  uint8_t
400 get_compare_carry (uint8_t res, uint8_t rd, uint8_t rr, int b)
401 {
402     uint8_t resb = (res >> b) & 0x1;
403     uint8_t rdb = (rd >> b) & 0x1;
404     uint8_t rrb = (rr >> b) & 0x1;
405     return (~rdb & rrb) | (rrb & resb) | (resb & ~rdb);
406 }
407
408 static  uint8_t
409 get_compare_overflow (uint8_t res, uint8_t rd, uint8_t rr)
410 {
411     res >>= 7; rd >>= 7; rr >>= 7;
412     /* The atmel data sheet says the second term is ~rd7 for CP
413      * but that doesn't make any sense. You be the judge. */
414     return (rd & ~rr & ~res) | (~rd & rr & res);
415 }
416
417 static inline int _avr_is_instruction_32_bits(avr_t * avr, uint32_t pc)
418 {
419         uint16_t o = (avr->flash[pc] | (avr->flash[pc+1] << 8)) & 0xfc0f;
420         return  o == 0x9200 || // STS ! Store Direct to Data Space
421                         o == 0x9000 || // LDS Load Direct from Data Space
422                         o == 0x940c || // JMP Long Jump
423                         o == 0x940d || // JMP Long Jump
424                         o == 0x940e ||  // CALL Long Call to sub
425                         o == 0x940f; // CALL Long Call to sub
426 }
427
428 /*
429  * Main opcode decoder
430  * 
431  * The decoder was written by following the datasheet in no particular order.
432  * As I went along, I noticed "bit patterns" that could be used to factor opcodes
433  * However, a lot of these only became apparent later on, so SOME instructions
434  * (skip of bit set etc) are compact, and some could use some refactoring (the ALU
435  * ones scream to be factored).
436  * I assume that the decoder could easily be 2/3 of it's current size.
437  * 
438  * + It lacks the "extended" XMega jumps. 
439  * + It also doesn't check whether the core it's
440  *   emulating is supposed to have the fancy instructions, like multiply and such.
441  * 
442  * for now all instructions take "one" cycle, the cycle+=<extra> needs to be added.
443  */
444 uint16_t avr_run_one(avr_t * avr)
445 {
446 #if CONFIG_SIMAVR_TRACE
447         /*
448          * this traces spurious reset or bad jumps
449          */
450         if ((avr->pc == 0 && avr->cycle > 0) || avr->pc >= avr->codeend) {
451                 avr->trace = 1;
452                 STATE("RESET\n");
453                 CRASH();
454         }
455         avr->touched[0] = avr->touched[1] = avr->touched[2] = 0;
456 #endif
457
458         uint32_t        opcode = (avr->flash[avr->pc + 1] << 8) | avr->flash[avr->pc];
459         uint32_t        new_pc = avr->pc + 2;   // future "default" pc
460         int             cycle = 1;
461
462         switch (opcode & 0xf000) {
463                 case 0x0000: {
464                         switch (opcode) {
465                                 case 0x0000: {  // NOP
466                                         STATE("nop\n");
467                                 }       break;
468                                 default: {
469                                         switch (opcode & 0xfc00) {
470                                                 case 0x0400: {  // CPC compare with carry 0000 01rd dddd rrrr
471                                                         get_r_d_10(opcode);
472                                                         uint8_t res = vd - vr - avr->sreg[S_C];
473                                                         STATE("cpc %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
474                                                         if (res)
475                                                                 avr->sreg[S_Z] = 0;
476                                                         avr->sreg[S_H] = get_compare_carry(res, vd, vr, 3);
477                                                         avr->sreg[S_V] = get_compare_overflow(res, vd, vr);
478                                                         avr->sreg[S_N] = (res >> 7) & 1;
479                                                         avr->sreg[S_C] = get_compare_carry(res, vd, vr, 7);
480                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
481                                                         SREG();
482                                                 }       break;
483                                                 case 0x0c00: {  // ADD without carry 0000 11 rd dddd rrrr
484                                                         get_r_d_10(opcode);
485                                                         uint8_t res = vd + vr;
486                                                         if (r == d) {
487                                                                 STATE("lsl %s[%02x] = %02x\n", avr_regname(d), vd, res & 0xff);
488                                                         } else {
489                                                                 STATE("add %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
490                                                         }
491                                                         _avr_set_r(avr, d, res);
492                                                         avr->sreg[S_Z] = res == 0;
493                                                         avr->sreg[S_H] = get_add_carry(res, vd, vr, 3);
494                                                         avr->sreg[S_V] = get_add_overflow(res, vd, vr);
495                                                         avr->sreg[S_N] = (res >> 7) & 1;
496                                                         avr->sreg[S_C] = get_add_carry(res, vd, vr, 7);
497                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
498                                                         SREG();
499                                                 }       break;
500                                                 case 0x0800: {  // SBC substract with carry 0000 10rd dddd rrrr
501                                                         get_r_d_10(opcode);
502                                                         uint8_t res = vd - vr - avr->sreg[S_C];
503                                                         STATE("sbc %s[%02x], %s[%02x] = %02x\n", avr_regname(d), avr->data[d], avr_regname(r), avr->data[r], res);
504                                                         _avr_set_r(avr, d, res);
505                                                         if (res)
506                                                                 avr->sreg[S_Z] = 0;
507                                                         avr->sreg[S_H] = get_sub_carry(res, vd, vr, 3);
508                                                         avr->sreg[S_V] = get_sub_overflow(res, vd, vr);
509                                                         avr->sreg[S_N] = (res >> 7) & 1;
510                                                         avr->sreg[S_C] = get_sub_carry(res, vd, vr, 7);
511                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
512                                                         SREG();
513                                                 }       break;
514                                                 default:
515                                                         switch (opcode & 0xff00) {
516                                                                 case 0x0100: {  // MOVW – Copy Register Word 0000 0001 dddd rrrr
517                                                                         uint8_t d = ((opcode >> 4) & 0xf) << 1;
518                                                                         uint8_t r = ((opcode) & 0xf) << 1;
519                                                                         STATE("movw %s:%s, %s:%s[%02x%02x]\n", avr_regname(d), avr_regname(d+1), avr_regname(r), avr_regname(r+1), avr->data[r+1], avr->data[r]);
520                                                                         _avr_set_r(avr, d, avr->data[r]);
521                                                                         _avr_set_r(avr, d+1, avr->data[r+1]);
522                                                                 }       break;
523                                                                 case 0x0200: {  // MULS – Multiply Signed 0000 0010 dddd rrrr
524                                                                         int8_t r = 16 + (opcode & 0xf);
525                                                                         int8_t d = 16 + ((opcode >> 4) & 0xf);
526                                                                         int16_t res = ((int8_t)avr->data[r]) * ((int8_t)avr->data[d]);
527                                                                         STATE("muls %s[%d], %s[%02x] = %d\n", avr_regname(d), ((int8_t)avr->data[d]), avr_regname(r), ((int8_t)avr->data[r]), res);
528                                                                         _avr_set_r(avr, 0, res);
529                                                                         _avr_set_r(avr, 1, res >> 8);
530                                                                         avr->sreg[S_C] = (res >> 15) & 1;
531                                                                         avr->sreg[S_Z] = res == 0;
532                                                                         SREG();
533                                                                 }       break;
534                                                                 case 0x0300: {  // multiplications
535                                                                         int8_t r = 16 + (opcode & 0x7);
536                                                                         int8_t d = 16 + ((opcode >> 4) & 0x7);
537                                                                         int16_t res = 0;
538                                                                         uint8_t c = 0;
539                                                                         T(const char * name = "";)
540                                                                         switch (opcode & 0x88) {
541                                                                                 case 0x00:      // MULSU – Multiply Signed Unsigned 0000 0011 0ddd 0rrr
542                                                                                         res = ((uint8_t)avr->data[r]) * ((int8_t)avr->data[d]);
543                                                                                         c = (res >> 15) & 1;
544                                                                                         T(name = "mulsu";)
545                                                                                         break;
546                                                                                 case 0x08:      // FMUL Fractional Multiply Unsigned 0000 0011 0ddd 1rrr
547                                                                                         res = ((uint8_t)avr->data[r]) * ((uint8_t)avr->data[d]);
548                                                                                         c = (res >> 15) & 1;
549                                                                                         res <<= 1;
550                                                                                         T(name = "fmul";)
551                                                                                         break;
552                                                                                 case 0x80:      // FMULS – Multiply Signed  0000 0011 1ddd 0rrr
553                                                                                         res = ((int8_t)avr->data[r]) * ((int8_t)avr->data[d]);
554                                                                                         c = (res >> 15) & 1;
555                                                                                         res <<= 1;
556                                                                                         T(name = "fmuls";)
557                                                                                         break;
558                                                                                 case 0x88:      // FMULSU – Multiply Signed Unsigned 0000 0011 1ddd 0rrr
559                                                                                         res = ((uint8_t)avr->data[r]) * ((int8_t)avr->data[d]);
560                                                                                         c = (res >> 15) & 1;
561                                                                                         res <<= 1;
562                                                                                         T(name = "fmulsu";)
563                                                                                         break;
564                                                                         }
565                                                                         cycle++;
566                                                                         STATE("%s %s[%d], %s[%02x] = %d\n", name, avr_regname(d), ((int8_t)avr->data[d]), avr_regname(r), ((int8_t)avr->data[r]), res);
567                                                                         _avr_set_r(avr, 0, res);
568                                                                         _avr_set_r(avr, 1, res >> 8);
569                                                                         avr->sreg[S_C] = c;
570                                                                         avr->sreg[S_Z] = res == 0;
571                                                                         SREG();
572                                                                 }       break;
573                                                                 default: _avr_invalid_opcode(avr);
574                                                         }
575                                         }
576                                 }
577                         }
578                 }       break;
579
580                 case 0x1000: {
581                         switch (opcode & 0xfc00) {
582                                 case 0x1800: {  // SUB without carry 0000 10 rd dddd rrrr
583                                         get_r_d_10(opcode);
584                                         uint8_t res = vd - vr;
585                                         STATE("sub %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
586                                         _avr_set_r(avr, d, res);
587                                         avr->sreg[S_Z] = res == 0;
588                                         avr->sreg[S_H] = get_sub_carry(res, vd, vr, 3);
589                                         avr->sreg[S_V] = get_sub_overflow(res, vd, vr);
590                                         avr->sreg[S_N] = (res >> 7) & 1;
591                                         avr->sreg[S_C] = get_sub_carry(res, vd, vr, 7);
592                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
593                                         SREG();
594                                 }       break;
595                                 case 0x1000: {  // CPSE Compare, skip if equal 0000 10 rd dddd rrrr
596                                         get_r_d_10(opcode);
597                                         uint16_t res = vd == vr;
598                                         STATE("cpse %s[%02x], %s[%02x]\t; Will%s skip\n", avr_regname(d), avr->data[d], avr_regname(r), avr->data[r], res ? "":"not ");
599                                         if (res) {
600                                                 if (_avr_is_instruction_32_bits(avr, new_pc)) {
601                                                         new_pc += 4; cycle += 2;
602                                                 } else {
603                                                         new_pc += 2; cycle++;
604                                                 }
605                                         }
606                                 }       break;
607                                 case 0x1400: {  // CP Compare 0000 10 rd dddd rrrr
608                                         get_r_d_10(opcode);
609                                         uint8_t res = vd - vr;
610                                         STATE("cp %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
611                                         avr->sreg[S_Z] = res == 0;
612                                         avr->sreg[S_H] = get_compare_carry(res, vd, vr, 3);
613                                         avr->sreg[S_V] = get_compare_overflow(res, vd, vr);
614                                         avr->sreg[S_N] = res >> 7;
615                                         avr->sreg[S_C] = get_compare_carry(res, vd, vr, 7);
616                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
617                                         SREG();
618                                 }       break;
619                                 case 0x1c00: {  // ADD with carry 0001 11 rd dddd rrrr
620                                         get_r_d_10(opcode);
621                                         uint8_t res = vd + vr + avr->sreg[S_C];
622                                         if (r == d) {
623                                                 STATE("rol %s[%02x] = %02x\n", avr_regname(d), avr->data[d], res);
624                                         } else {
625                                                 STATE("addc %s[%02x], %s[%02x] = %02x\n", avr_regname(d), avr->data[d], avr_regname(r), avr->data[r], res);
626                                         }
627                                         _avr_set_r(avr, d, res);
628                                         avr->sreg[S_Z] = res == 0;
629                                         avr->sreg[S_H] = get_add_carry(res, vd, vr, 3);
630                                         avr->sreg[S_V] = get_add_overflow(res, vd, vr);
631                                         avr->sreg[S_N] = (res >> 7) & 1;
632                                         avr->sreg[S_C] = get_add_carry(res, vd, vr, 7);
633                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
634                                         SREG();
635                                 }       break;
636                                 default: _avr_invalid_opcode(avr);
637                         }
638                 }       break;
639
640                 case 0x2000: {
641                         switch (opcode & 0xfc00) {
642                                 case 0x2000: {  // AND  0010 00rd dddd rrrr
643                                         get_r_d_10(opcode);
644                                         uint8_t res = vd & vr;
645                                         if (r == d) {
646                                                 STATE("tst %s[%02x]\n", avr_regname(d), avr->data[d]);
647                                         } else {
648                                                 STATE("and %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
649                                         }
650                                         _avr_set_r(avr, d, res);
651                                         avr->sreg[S_Z] = res == 0;
652                                         avr->sreg[S_N] = (res >> 7) & 1;
653                                         avr->sreg[S_V] = 0;
654                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
655                                         SREG();
656                                 }       break;
657                                 case 0x2400: {  // EOR  0010 01rd dddd rrrr
658                                         get_r_d_10(opcode);
659                                         uint8_t res = vd ^ vr;
660                                         if (r==d) {
661                                                 STATE("clr %s[%02x]\n", avr_regname(d), avr->data[d]);
662                                         } else {
663                                                 STATE("eor %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
664                                         }
665                                         _avr_set_r(avr, d, res);
666                                         avr->sreg[S_Z] = res == 0;
667                                         avr->sreg[S_N] = (res >> 7) & 1;
668                                         avr->sreg[S_V] = 0;
669                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
670                                         SREG();
671                                 }       break;
672                                 case 0x2800: {  // OR Logical OR        0010 10rd dddd rrrr
673                                         get_r_d_10(opcode);
674                                         uint8_t res = vd | vr;
675                                         STATE("or %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
676                                         _avr_set_r(avr, d, res);
677                                         avr->sreg[S_Z] = res == 0;
678                                         avr->sreg[S_N] = (res >> 7) & 1;
679                                         avr->sreg[S_V] = 0;
680                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
681                                         SREG();
682                                 }       break;
683                                 case 0x2c00: {  // MOV  0010 11rd dddd rrrr
684                                         get_r_d_10(opcode);
685                                         uint8_t res = vr;
686                                         STATE("mov %s[%02x], %s[%02x] = %02x\n", avr_regname(d), vd, avr_regname(r), vr, res);
687                                         _avr_set_r(avr, d, res);
688                                 }       break;
689                                 default: _avr_invalid_opcode(avr);
690                         }
691                 }       break;
692
693                 case 0x3000: {  // CPI 0011 KKKK rrrr KKKK
694                         get_k_r16(opcode);
695                         uint8_t vr = avr->data[r];
696                         uint8_t res = vr - k;
697                         STATE("cpi %s[%02x], 0x%02x\n", avr_regname(r), vr, k);
698
699                         avr->sreg[S_Z] = res == 0;
700                         avr->sreg[S_H] = get_compare_carry(res, vr, k, 3);
701                         avr->sreg[S_V] = get_compare_overflow(res, vr, k);
702                         avr->sreg[S_N] = (res >> 7) & 1;
703                         avr->sreg[S_C] = get_compare_carry(res, vr, k, 7);
704                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
705                         SREG();
706                 }       break;
707
708                 case 0x4000: {  // SBCI Subtract Immediate With Carry 0101 10 kkkk dddd kkkk
709                         get_k_r16(opcode);
710                         uint8_t vr = avr->data[r];
711                         uint8_t res = vr - k - avr->sreg[S_C];
712                         STATE("sbci %s[%02x], 0x%02x = %02x\n", avr_regname(r), avr->data[r], k, res);
713                         _avr_set_r(avr, r, res);
714                         if (res)
715                                 avr->sreg[S_Z] = 0;
716                         avr->sreg[S_N] = (res >> 7) & 1;
717                         avr->sreg[S_C] = (k + avr->sreg[S_C]) > vr;
718                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
719                         SREG();
720                 }       break;
721
722                 case 0x5000: {  // SUB Subtract Immediate 0101 10 kkkk dddd kkkk
723                         get_k_r16(opcode);
724                         uint8_t vr = avr->data[r];
725                         uint8_t res = vr - k;
726                         STATE("subi %s[%02x], 0x%02x = %02x\n", avr_regname(r), avr->data[r], k, res);
727                         _avr_set_r(avr, r, res);
728                         avr->sreg[S_Z] = res  == 0;
729                         avr->sreg[S_N] = (res >> 7) & 1;
730                         avr->sreg[S_C] = k > vr;
731                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
732                         SREG();
733                 }       break;
734
735                 case 0x6000: {  // ORI aka SBR  Logical AND with Immediate      0110 kkkk dddd kkkk
736                         get_k_r16(opcode);
737                         uint8_t res = avr->data[r] | k;
738                         STATE("ori %s[%02x], 0x%02x\n", avr_regname(r), avr->data[r], k);
739                         _avr_set_r(avr, r, res);
740                         avr->sreg[S_Z] = res == 0;
741                         avr->sreg[S_N] = (res >> 7) & 1;
742                         avr->sreg[S_V] = 0;
743                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
744                         SREG();
745                 }       break;
746
747                 case 0x7000: {  // ANDI Logical AND with Immediate      0111 kkkk dddd kkkk
748                         get_k_r16(opcode);
749                         uint8_t res = avr->data[r] & k;
750                         STATE("andi %s[%02x], 0x%02x\n", avr_regname(r), avr->data[r], k);
751                         _avr_set_r(avr, r, res);
752                         avr->sreg[S_Z] = res == 0;
753                         avr->sreg[S_N] = (res >> 7) & 1;
754                         avr->sreg[S_V] = 0;
755                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
756                         SREG();
757                 }       break;
758
759                 case 0xa000:
760                 case 0x8000: {
761                         switch (opcode & 0xd008) {
762                                 case 0xa000:
763                                 case 0x8000: {  // LD (LDD) – Load Indirect using Z 10q0 qq0r rrrr 0qqq
764                                         uint16_t v = avr->data[R_ZL] | (avr->data[R_ZH] << 8);
765                                         uint8_t r = (opcode >> 4) & 0x1f;
766                                         uint8_t q = ((opcode & 0x2000) >> 8) | ((opcode & 0x0c00) >> 7) | (opcode & 0x7);
767
768                                         if (opcode & 0x0200) {
769                                                 STATE("st (Z+%d[%04x]), %s[%02x]\n", q, v+q, avr_regname(r), avr->data[r]);
770                                                 _avr_set_ram(avr, v+q, avr->data[r]);
771                                         } else {
772                                                 STATE("ld %s, (Z+%d[%04x])=[%02x]\n", avr_regname(r), q, v+q, avr->data[v+q]);
773                                                 _avr_set_r(avr, r, _avr_get_ram(avr, v+q));
774                                         }
775                                         cycle += 2;
776                                 }       break;
777                                 case 0xa008:
778                                 case 0x8008: {  // LD (LDD) – Load Indirect using Y 10q0 qq0r rrrr 1qqq
779                                         uint16_t v = avr->data[R_YL] | (avr->data[R_YH] << 8);
780                                         uint8_t r = (opcode >> 4) & 0x1f;
781                                         uint8_t q = ((opcode & 0x2000) >> 8) | ((opcode & 0x0c00) >> 7) | (opcode & 0x7);
782
783                                         if (opcode & 0x0200) {
784                                                 STATE("st (Y+%d[%04x]), %s[%02x]\n", q, v+q, avr_regname(r), avr->data[r]);
785                                                 _avr_set_ram(avr, v+q, avr->data[r]);
786                                         } else {
787                                                 STATE("ld %s, (Y+%d[%04x])=[%02x]\n", avr_regname(r), q, v+q, avr->data[v+q]);
788                                                 _avr_set_r(avr, r, _avr_get_ram(avr, v+q));
789                                         }
790                                         cycle += 2;
791                                 }       break;
792                                 default: _avr_invalid_opcode(avr);
793                         }
794                 }       break;
795
796                 case 0x9000: {
797                         /* this is an annoying special case, but at least these lines handle all the SREG set/clear opcodes */
798                         if ((opcode & 0xff0f) == 0x9408) {
799                                 uint8_t b = (opcode >> 4) & 7;
800                                 STATE("%s%c\n", opcode & 0x0080 ? "cl" : "se", _sreg_bit_name[b]);
801                                 avr->sreg[b] = (opcode & 0x0080) == 0;
802                                 SREG();
803                         } else switch (opcode) {
804                                 case 0x9588: { // SLEEP
805                                         STATE("sleep\n");
806                                         avr->state = cpu_Sleeping;
807                                 }       break;
808                                 case 0x9598: { // BREAK
809                                         STATE("break\n");
810                                         if (avr->gdb) {
811                                                 // if gdb is on, we break here as in here
812                                                 // and we do so until gdb restores the instruction
813                                                 // that was here before
814                                                 avr->state = cpu_StepDone;
815                                                 new_pc = avr->pc;
816                                                 cycle = 0;
817                                         }
818                                 }       break;
819                                 case 0x95a8: { // WDR
820                                         STATE("wdr\n");
821                                         avr_ioctl(avr, AVR_IOCTL_WATCHDOG_RESET, 0);
822                                 }       break;
823                                 case 0x95e8: { // SPM
824                                         STATE("spm\n");
825                                         avr_ioctl(avr, AVR_IOCTL_FLASH_SPM, 0);
826                                 }       break;
827                                 case 0x9409:   // IJMP Indirect jump                                    1001 0100 0000 1001
828                                 case 0x9419:   // EIJMP Indirect jump                                   1001 0100 0001 1001   bit 4 is "indirect"
829                                 case 0x9509:   // ICALL Indirect Call to Subroutine             1001 0101 0000 1001
830                                 case 0x9519: { // EICALL Indirect Call to Subroutine    1001 0101 0001 1001   bit 8 is "push pc"
831                                         int e = opcode & 0x10;
832                                         int p = opcode & 0x100;
833                                         if (e && !avr->eind)
834                                                 _avr_invalid_opcode(avr);
835                                         uint16_t z = avr->data[R_ZL] | (avr->data[R_ZH] << 8);
836                                         if (e)
837                                                 z |= avr->data[avr->eind] << 16;
838                                         STATE("%si%s Z[%04x]\n", e?"e":"", p?"call":"jmp", z << 1);
839                                         if (p) {
840                                                 cycle++;
841                                                 _avr_push16(avr, new_pc >> 1);
842                                         }
843                                         new_pc = z << 1;
844                                         cycle++;
845                                         TRACE_JUMP();
846                                 }       break;
847                                 case 0x9518:    // RETI
848                                 case 0x9508: {  // RET
849                                         new_pc = _avr_pop16(avr) << 1;
850                                         if (opcode & 0x10)      // reti
851                                                 avr->sreg[S_I] = 1;
852                                         cycle += 3;
853                                         STATE("ret%s\n", opcode & 0x10 ? "i" : "");
854                                         TRACE_JUMP();
855                                         STACK_FRAME_POP();
856                                 }       break;
857                                 case 0x95c8: {  // LPM Load Program Memory R0 <- (Z)
858                                         uint16_t z = avr->data[R_ZL] | (avr->data[R_ZH] << 8);
859                                         STATE("lpm %s, (Z[%04x])\n", avr_regname(0), z);
860                                         _avr_set_r(avr, 0, avr->flash[z]);
861                                 }       break;
862                                 case 0x9408:case 0x9418:case 0x9428:case 0x9438:case 0x9448:case 0x9458:case 0x9468:
863                                 case 0x9478:
864                                 {       // BSET 1001 0100 0ddd 1000
865                                         uint8_t b = (opcode >> 4) & 7;
866                                         avr->sreg[b] = 1;
867                                         STATE("bset %c\n", _sreg_bit_name[b]);
868                                         SREG();
869                                 }       break;
870                                 case 0x9488:case 0x9498:case 0x94a8:case 0x94b8:case 0x94c8:case 0x94d8:case 0x94e8:
871                                 case 0x94f8:
872                                 {       // BSET 1001 0100 0ddd 1000
873                                         uint8_t b = (opcode >> 4) & 7;
874                                         avr->sreg[b] = 0;
875                                         STATE("bclr %c\n", _sreg_bit_name[b]);
876                                         SREG();
877                                 }       break;
878                                 default:  {
879                                         switch (opcode & 0xfe0f) {
880                                                 case 0x9000: {  // LDS Load Direct from Data Space, 32 bits
881                                                         uint8_t r = (opcode >> 4) & 0x1f;
882                                                         uint16_t x = (avr->flash[new_pc+1] << 8) | avr->flash[new_pc];
883                                                         new_pc += 2;
884                                                         STATE("lds %s[%02x], 0x%04x\n", avr_regname(r), avr->data[r], x);
885                                                         _avr_set_r(avr, r, _avr_get_ram(avr, x));
886                                                         cycle++;
887                                                 }       break;
888                                                 case 0x9005:
889                                                 case 0x9004: {  // LPM Load Program Memory 1001 000d dddd 01oo
890                                                         uint16_t z = avr->data[R_ZL] | (avr->data[R_ZH] << 8);
891                                                         uint8_t r = (opcode >> 4) & 0x1f;
892                                                         int op = opcode & 3;
893                                                         STATE("lpm %s, (Z[%04x]%s)\n", avr_regname(r), z, opcode?"+":"");
894                                                         _avr_set_r(avr, r, avr->flash[z]);
895                                                         if (op == 1) {
896                                                                 z++;
897                                                                 _avr_set_r(avr, R_ZH, z >> 8);
898                                                                 _avr_set_r(avr, R_ZL, z);
899                                                         }
900                                                         cycle += 2;
901                                                 }       break;
902                                                 case 0x9006:
903                                                 case 0x9007: {  // ELPM Extended Load Program Memory 1001 000d dddd 01oo
904                                                         if (!avr->rampz)
905                                                                 _avr_invalid_opcode(avr);
906                                                         uint16_t z = avr->data[R_ZL] | (avr->data[R_ZH] << 8) | (avr->data[avr->rampz] << 16);
907                                                         uint8_t r = (opcode >> 4) & 0x1f;
908                                                         int op = opcode & 3;
909                                                         STATE("elpm %s, (Z[%02x:%04x]%s)\n", avr_regname(r), z >> 16, z&0xffff, opcode?"+":"");
910                                                         _avr_set_r(avr, r, avr->flash[z]);
911                                                         if (op == 3) {
912                                                                 z++;
913                                                                 _avr_set_r(avr, avr->rampz, z >> 16);
914                                                                 _avr_set_r(avr, R_ZH, z >> 8);
915                                                                 _avr_set_r(avr, R_ZL, z);
916                                                         }
917                                                         cycle += 2;
918                                                 }       break;
919                                                 case 0x900c:
920                                                 case 0x900d:
921                                                 case 0x900e: {  // LD Load Indirect from Data using X 1001 000r rrrr 11oo
922                                                         int op = opcode & 3;
923                                                         uint8_t r = (opcode >> 4) & 0x1f;
924                                                         uint16_t x = (avr->data[R_XH] << 8) | avr->data[R_XL];
925                                                         STATE("ld %s, %sX[%04x]%s\n", avr_regname(r), op == 2 ? "--" : "", x, op == 1 ? "++" : "");
926
927                                                         if (op == 2) x--;
928                                                         _avr_set_r(avr, r, _avr_get_ram(avr, x));
929                                                         if (op == 1) x++;
930                                                         _avr_set_r(avr, R_XH, x >> 8);
931                                                         _avr_set_r(avr, R_XL, x);
932                                                 }       break;
933                                                 case 0x920c:
934                                                 case 0x920d:
935                                                 case 0x920e: {  // ST Store Indirect Data Space X 1001 001r rrrr 11oo
936                                                         int op = opcode & 3;
937                                                         uint8_t r = (opcode >> 4) & 0x1f;
938                                                         uint16_t x = (avr->data[R_XH] << 8) | avr->data[R_XL];
939                                                         STATE("st %sX[%04x]%s, %s[%02x] \n", op == 2 ? "--" : "", x, op == 1 ? "++" : "", avr_regname(r), avr->data[r]);
940                                                         cycle++;
941                                                         if (op == 2) x--;
942                                                         _avr_set_ram(avr, x, avr->data[r]);
943                                                         if (op == 1) x++;
944                                                         _avr_set_r(avr, R_XH, x >> 8);
945                                                         _avr_set_r(avr, R_XL, x);
946                                                 }       break;
947                                                 case 0x9009:
948                                                 case 0x900a: {  // LD Load Indirect from Data using Y 1001 000r rrrr 10oo
949                                                         int op = opcode & 3;
950                                                         uint8_t r = (opcode >> 4) & 0x1f;
951                                                         uint16_t y = (avr->data[R_YH] << 8) | avr->data[R_YL];
952                                                         STATE("ld %s, %sY[%04x]%s\n", avr_regname(r), op == 2 ? "--" : "", y, op == 1 ? "++" : "");
953                                                         cycle++;
954                                                         if (op == 2) y--;
955                                                         _avr_set_r(avr, r, _avr_get_ram(avr, y));
956                                                         if (op == 1) y++;
957                                                         _avr_set_r(avr, R_YH, y >> 8);
958                                                         _avr_set_r(avr, R_YL, y);
959                                                 }       break;
960                                                 case 0x9209:
961                                                 case 0x920a: {  // ST Store Indirect Data Space Y 1001 001r rrrr 10oo
962                                                         int op = opcode & 3;
963                                                         uint8_t r = (opcode >> 4) & 0x1f;
964                                                         uint16_t y = (avr->data[R_YH] << 8) | avr->data[R_YL];
965                                                         STATE("st %sY[%04x]%s, %s[%02x] \n", op == 2 ? "--" : "", y, op == 1 ? "++" : "", avr_regname(r), avr->data[r]);
966                                                         cycle++;
967                                                         if (op == 2) y--;
968                                                         _avr_set_ram(avr, y, avr->data[r]);
969                                                         if (op == 1) y++;
970                                                         _avr_set_r(avr, R_YH, y >> 8);
971                                                         _avr_set_r(avr, R_YL, y);
972                                                 }       break;
973                                                 case 0x9200: {  // STS ! Store Direct to Data Space, 32 bits
974                                                         uint8_t r = (opcode >> 4) & 0x1f;
975                                                         uint16_t x = (avr->flash[new_pc+1] << 8) | avr->flash[new_pc];
976                                                         new_pc += 2;
977                                                         STATE("sts 0x%04x, %s[%02x]\n", x, avr_regname(r), avr->data[r]);
978                                                         _avr_set_ram(avr, x, avr->data[r]);
979                                                 }       break;
980                                                 case 0x9001:
981                                                 case 0x9002: {  // LD Load Indirect from Data using Z 1001 001r rrrr 00oo
982                                                         int op = opcode & 3;
983                                                         uint8_t r = (opcode >> 4) & 0x1f;
984                                                         uint16_t z = (avr->data[R_ZH] << 8) | avr->data[R_ZL];
985                                                         STATE("ld %s, %sZ[%04x]%s\n", avr_regname(r), op == 2 ? "--" : "", z, op == 1 ? "++" : "");
986                                                         if (op == 2) z--;
987                                                         _avr_set_r(avr, r, _avr_get_ram(avr, z));
988                                                         if (op == 1) z++;
989                                                         _avr_set_r(avr, R_ZH, z >> 8);
990                                                         _avr_set_r(avr, R_ZL, z);
991                                                 }       break;
992                                                 case 0x9201:
993                                                 case 0x9202: {  // ST Store Indirect Data Space Z 1001 001r rrrr 00oo
994                                                         int op = opcode & 3;
995                                                         uint8_t r = (opcode >> 4) & 0x1f;
996                                                         uint16_t z = (avr->data[R_ZH] << 8) | avr->data[R_ZL];
997                                                         STATE("st %sZ[%04x]%s, %s[%02x] \n", op == 2 ? "--" : "", z, op == 1 ? "++" : "", avr_regname(r), avr->data[r]);
998                                                         if (op == 2) z--;
999                                                         _avr_set_ram(avr, z, avr->data[r]);
1000                                                         if (op == 1) z++;
1001                                                         _avr_set_r(avr, R_ZH, z >> 8);
1002                                                         _avr_set_r(avr, R_ZL, z);
1003                                                 }       break;
1004                                                 case 0x900f: {  // POP 1001 000d dddd 1111
1005                                                         uint8_t r = (opcode >> 4) & 0x1f;
1006                                                         _avr_set_r(avr, r, _avr_pop8(avr));
1007                                                         T(uint16_t sp = _avr_sp_get(avr);)
1008                                                         STATE("pop %s (@%04x)[%02x]\n", avr_regname(r), sp, avr->data[sp]);
1009                                                         cycle++;
1010                                                 }       break;
1011                                                 case 0x920f: {  // PUSH 1001 001d dddd 1111
1012                                                         uint8_t r = (opcode >> 4) & 0x1f;
1013                                                         _avr_push8(avr, avr->data[r]);
1014                                                         T(uint16_t sp = _avr_sp_get(avr);)
1015                                                         STATE("push %s[%02x] (@%04x)\n", avr_regname(r), avr->data[r], sp);
1016                                                         cycle++;
1017                                                 }       break;
1018                                                 case 0x9400: {  // COM – One’s Complement
1019                                                         uint8_t r = (opcode >> 4) & 0x1f;
1020                                                         uint8_t res = 0xff - avr->data[r];
1021                                                         STATE("com %s[%02x] = %02x\n", avr_regname(r), avr->data[r], res);
1022                                                         _avr_set_r(avr, r, res);
1023                                                         avr->sreg[S_Z] = res == 0;
1024                                                         avr->sreg[S_N] = res >> 7;
1025                                                         avr->sreg[S_V] = 0;
1026                                                         avr->sreg[S_C] = 1;
1027                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1028                                                         SREG();
1029                                                 }       break;
1030                                                 case 0x9401: {  // NEG – One’s Complement
1031                                                         uint8_t r = (opcode >> 4) & 0x1f;
1032                                                         uint8_t rd = avr->data[r];
1033                                                         uint8_t res = 0x00 - rd;
1034                                                         STATE("neg %s[%02x] = %02x\n", avr_regname(r), rd, res);
1035                                                         _avr_set_r(avr, r, res);
1036                                                         avr->sreg[S_H] = ((res >> 3) | (rd >> 3)) & 1;
1037                                                         avr->sreg[S_Z] = res == 0;
1038                                                         avr->sreg[S_N] = res >> 7;
1039                                                         avr->sreg[S_V] = res == 0x80;
1040                                                         avr->sreg[S_C] = res != 0;
1041                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1042                                                         SREG();
1043                                                 }       break;
1044                                                 case 0x9402: {  // SWAP – Swap Nibbles
1045                                                         uint8_t r = (opcode >> 4) & 0x1f;
1046                                                         uint8_t res = (avr->data[r] >> 4) | (avr->data[r] << 4) ;
1047                                                         STATE("swap %s[%02x] = %02x\n", avr_regname(r), avr->data[r], res);
1048                                                         _avr_set_r(avr, r, res);
1049                                                 }       break;
1050                                                 case 0x9403: {  // INC – Increment
1051                                                         uint8_t r = (opcode >> 4) & 0x1f;
1052                                                         uint8_t res = avr->data[r] + 1;
1053                                                         STATE("inc %s[%02x] = %02x\n", avr_regname(r), avr->data[r], res);
1054                                                         _avr_set_r(avr, r, res);
1055                                                         avr->sreg[S_Z] = res == 0;
1056                                                         avr->sreg[S_N] = res >> 7;
1057                                                         avr->sreg[S_V] = res == 0x7f;
1058                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1059                                                         SREG();
1060                                                 }       break;
1061                                                 case 0x9405: {  // ASR – Arithmetic Shift Right 1001 010d dddd 0101
1062                                                         uint8_t r = (opcode >> 4) & 0x1f;
1063                                                         uint8_t vr = avr->data[r];
1064                                                         uint8_t res = (vr >> 1) | (vr & 0x80);
1065                                                         STATE("asr %s[%02x]\n", avr_regname(r), vr);
1066                                                         _avr_set_r(avr, r, res);
1067                                                         avr->sreg[S_Z] = res == 0;
1068                                                         avr->sreg[S_C] = vr & 1;
1069                                                         avr->sreg[S_N] = res >> 7;
1070                                                         avr->sreg[S_V] = avr->sreg[S_N] ^ avr->sreg[S_C];
1071                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1072                                                         SREG();
1073                                                 }       break;
1074                                                 case 0x9406: {  // LSR 1001 010d dddd 0110
1075                                                         uint8_t r = (opcode >> 4) & 0x1f;
1076                                                         uint8_t vr = avr->data[r];
1077                                                         uint8_t res = vr >> 1;
1078                                                         STATE("lsr %s[%02x]\n", avr_regname(r), vr);
1079                                                         _avr_set_r(avr, r, res);
1080                                                         avr->sreg[S_Z] = res == 0;
1081                                                         avr->sreg[S_C] = vr & 1;
1082                                                         avr->sreg[S_N] = 0;
1083                                                         avr->sreg[S_V] = avr->sreg[S_N] ^ avr->sreg[S_C];
1084                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1085                                                         SREG();
1086                                                 }       break;
1087                                                 case 0x9407: {  // ROR 1001 010d dddd 0111
1088                                                         uint8_t r = (opcode >> 4) & 0x1f;
1089                                                         uint8_t vr = avr->data[r];
1090                                                         uint8_t res = (avr->sreg[S_C] ? 0x80 : 0) | vr >> 1;
1091                                                         STATE("ror %s[%02x]\n", avr_regname(r), vr);
1092                                                         _avr_set_r(avr, r, res);
1093                                                         avr->sreg[S_Z] = res == 0;
1094                                                         avr->sreg[S_C] = vr & 1;
1095                                                         avr->sreg[S_N] = 0;
1096                                                         avr->sreg[S_V] = avr->sreg[S_N] ^ avr->sreg[S_C];
1097                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1098                                                         SREG();
1099                                                 }       break;
1100                                                 case 0x940a: {  // DEC – Decrement
1101                                                         uint8_t r = (opcode >> 4) & 0x1f;
1102                                                         uint8_t res = avr->data[r] - 1;
1103                                                         STATE("dec %s[%02x] = %02x\n", avr_regname(r), avr->data[r], res);
1104                                                         _avr_set_r(avr, r, res);
1105                                                         avr->sreg[S_Z] = res == 0;
1106                                                         avr->sreg[S_N] = res >> 7;
1107                                                         avr->sreg[S_V] = res == 0x80;
1108                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1109                                                         SREG();
1110                                                 }       break;
1111                                                 case 0x940c:
1112                                                 case 0x940d: {  // JMP Long Call to sub, 32 bits
1113                                                         uint32_t a = ((opcode & 0x01f0) >> 3) | (opcode & 1);
1114                                                         uint16_t x = (avr->flash[new_pc+1] << 8) | avr->flash[new_pc];
1115                                                         a = (a << 16) | x;
1116                                                         STATE("jmp 0x%06x\n", a);
1117                                                         new_pc = a << 1;
1118                                                         cycle += 2;
1119                                                         TRACE_JUMP();
1120                                                 }       break;
1121                                                 case 0x940e:
1122                                                 case 0x940f: {  // CALL Long Call to sub, 32 bits
1123                                                         uint32_t a = ((opcode & 0x01f0) >> 3) | (opcode & 1);
1124                                                         uint16_t x = (avr->flash[new_pc+1] << 8) | avr->flash[new_pc];
1125                                                         a = (a << 16) | x;
1126                                                         STATE("call 0x%06x\n", a);
1127                                                         new_pc += 2;
1128                                                         _avr_push16(avr, new_pc >> 1);
1129                                                         new_pc = a << 1;
1130                                                         cycle += 3;     // 4 cycles
1131                                                         TRACE_JUMP();
1132                                                         STACK_FRAME_PUSH();
1133                                                 }       break;
1134
1135                                                 default: {
1136                                                         switch (opcode & 0xff00) {
1137                                                                 case 0x9600: {  // ADIW - Add Immediate to Word 1001 0110 KKdd KKKK
1138                                                                         uint8_t r = 24 + ((opcode >> 3) & 0x6);
1139                                                                         uint8_t k = ((opcode & 0x00c0) >> 2) | (opcode & 0xf);
1140                                                                         uint8_t rdl = avr->data[r], rdh = avr->data[r+1];
1141                                                                         uint32_t res = rdl | (rdh << 8);
1142                                                                         STATE("adiw %s:%s[%04x], 0x%02x\n", avr_regname(r), avr_regname(r+1), res, k);
1143                                                                         res += k;
1144                                                                         _avr_set_r(avr, r + 1, res >> 8);
1145                                                                         _avr_set_r(avr, r, res);
1146                                                                         avr->sreg[S_V] = ~(rdh >> 7) & ((res >> 15) & 1);
1147                                                                         avr->sreg[S_Z] = (res & 0xffff) == 0;
1148                                                                         avr->sreg[S_N] = (res >> 15) & 1;
1149                                                                         avr->sreg[S_C] = ~((res >> 15) & 1) & (rdh >> 7);
1150                                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1151                                                                         SREG();
1152                                                                         cycle++;
1153                                                                 }       break;
1154                                                                 case 0x9700: {  // SBIW - Subtract Immediate from Word 1001 0110 KKdd KKKK
1155                                                                         uint8_t r = 24 + ((opcode >> 3) & 0x6);
1156                                                                         uint8_t k = ((opcode & 0x00c0) >> 2) | (opcode & 0xf);
1157                                                                         uint8_t rdl = avr->data[r], rdh = avr->data[r+1];
1158                                                                         uint32_t res = rdl | (rdh << 8);
1159                                                                         STATE("sbiw %s:%s[%04x], 0x%02x\n", avr_regname(r), avr_regname(r+1), res, k);
1160                                                                         res -= k;
1161                                                                         _avr_set_r(avr, r + 1, res >> 8);
1162                                                                         _avr_set_r(avr, r, res);
1163                                                                         avr->sreg[S_V] = (rdh >> 7) & (~(res >> 15) & 1);
1164                                                                         avr->sreg[S_Z] = (res & 0xffff) == 0;
1165                                                                         avr->sreg[S_N] = (res >> 15) & 1;
1166                                                                         avr->sreg[S_C] = ((res >> 15) & 1) & (~rdh >> 7);
1167                                                                         avr->sreg[S_S] = avr->sreg[S_N] ^ avr->sreg[S_V];
1168                                                                         SREG();
1169                                                                         cycle++;
1170                                                                 }       break;
1171                                                                 case 0x9800: {  // CBI - Clear Bit in I/O Registe 1001 1000 AAAA Abbb
1172                                                                         uint8_t io = ((opcode >> 3) & 0x1f) + 32;
1173                                                                         uint8_t b = opcode & 0x7;
1174                                                                         uint8_t res = _avr_get_ram(avr, io) & ~(1 << b);
1175                                                                         STATE("cbi %s[%04x], 0x%02x = %02x\n", avr_regname(io), avr->data[io], 1<<b, res);
1176                                                                         _avr_set_ram(avr, io, res);
1177                                                                         cycle++;
1178                                                                 }       break;
1179                                                                 case 0x9900: {  // SBIC - Skip if Bit in I/O Register is Cleared 1001 0111 AAAA Abbb
1180                                                                         uint8_t io = ((opcode >> 3) & 0x1f) + 32;
1181                                                                         uint8_t b = opcode & 0x7;
1182                                                                         uint8_t res = _avr_get_ram(avr, io) & (1 << b);
1183                                                                         STATE("sbic %s[%04x], 0x%02x\t; Will%s branch\n", avr_regname(io), avr->data[io], 1<<b, !res?"":"not ");
1184                                                                         if (!res) {
1185                                                                                 if (_avr_is_instruction_32_bits(avr, new_pc)) {
1186                                                                                         new_pc += 4; cycle += 2;
1187                                                                                 } else {
1188                                                                                         new_pc += 2; cycle++;
1189                                                                                 }
1190                                                                         }
1191                                                                 }       break;
1192                                                                 case 0x9a00: {  // SBI - Set Bit in I/O Register 1001 1000 AAAA Abbb
1193                                                                         uint8_t io = ((opcode >> 3) & 0x1f) + 32;
1194                                                                         uint8_t b = opcode & 0x7;
1195                                                                         uint8_t res = _avr_get_ram(avr, io) | (1 << b);
1196                                                                         STATE("sbi %s[%04x], 0x%02x = %02x\n", avr_regname(io), avr->data[io], 1<<b, res);
1197                                                                         _avr_set_ram(avr, io, res);
1198                                                                         cycle++;
1199                                                                 }       break;
1200                                                                 case 0x9b00: {  // SBIS - Skip if Bit in I/O Register is Cleared 1001 0111 AAAA Abbb
1201                                                                         uint8_t io = (opcode >> 3) & 0x1f;
1202                                                                         uint8_t b = opcode & 0x7;
1203                                                                         uint8_t res = _avr_get_ram(avr, io + 32) & (1 << b);
1204                                                                         STATE("sbis %s[%04x], 0x%02x\t; Will%s branch\n", avr_regname(io), avr->data[io], 1<<b, res?"":"not ");
1205                                                                         if (res) {
1206                                                                                 if (_avr_is_instruction_32_bits(avr, new_pc)) {
1207                                                                                         new_pc += 4; cycle += 2;
1208                                                                                 } else {
1209                                                                                         new_pc += 2; cycle++;
1210                                                                                 }
1211                                                                         }
1212                                                                 }       break;
1213                                                                 default:
1214                                                                         switch (opcode & 0xfc00) {
1215                                                                                 case 0x9c00: {  // MUL - Multiply Unsigned 1001 11rd dddd rrrr
1216                                                                                         get_r_d_10(opcode);
1217                                                                                         uint16_t res = vd * vr;
1218                                                                                         STATE("mul %s[%02x], %s[%02x] = %04x\n", avr_regname(d), vd, avr_regname(r), vr, res);
1219                                                                                         _avr_set_r(avr, 0, res);
1220                                                                                         _avr_set_r(avr, 1, res >> 8);
1221                                                                                         avr->sreg[S_Z] = res == 0;
1222                                                                                         avr->sreg[S_C] = (res >> 15) & 1;
1223                                                                                         SREG();
1224                                                                                 }       break;
1225                                                                                 default: _avr_invalid_opcode(avr);
1226                                                                         }
1227                                                         }
1228                                                 }       break;
1229                                         }
1230                                 }       break;
1231                         }
1232                 }       break;
1233
1234                 case 0xb000: {
1235                         switch (opcode & 0xf800) {
1236                                 case 0xb800: {  // OUT A,Rr 1011 1AAr rrrr AAAA
1237                                         uint8_t r = (opcode >> 4) & 0x1f;
1238                                         uint8_t A = ((((opcode >> 9) & 3) << 4) | ((opcode) & 0xf)) + 32;
1239                                         STATE("out %s, %s[%02x]\n", avr_regname(A), avr_regname(r), avr->data[r]);
1240                                         _avr_set_ram(avr, A, avr->data[r]);
1241                                 }       break;
1242                                 case 0xb000: {  // IN Rd,A 1011 0AAr rrrr AAAA
1243                                         uint8_t r = (opcode >> 4) & 0x1f;
1244                                         uint8_t A = ((((opcode >> 9) & 3) << 4) | ((opcode) & 0xf)) + 32;
1245                                         STATE("in %s, %s[%02x]\n", avr_regname(r), avr_regname(A), avr->data[A]);
1246                                         _avr_set_r(avr, r, _avr_get_ram(avr, A));
1247                                 }       break;
1248                                 default: _avr_invalid_opcode(avr);
1249                         }
1250                 }       break;
1251
1252                 case 0xc000: {
1253                         // RJMP 1100 kkkk kkkk kkkk
1254                         short o = ((short)(opcode << 4)) >> 4;
1255                         STATE("rjmp .%d [%04x]\n", o, new_pc + (o << 1));
1256                         new_pc = new_pc + (o << 1);
1257                         cycle++;
1258                         TRACE_JUMP();
1259                 }       break;
1260
1261                 case 0xd000: {
1262                         // RCALL 1100 kkkk kkkk kkkk
1263                         short o = ((short)(opcode << 4)) >> 4;
1264                         STATE("rcall .%d [%04x]\n", o, new_pc + (o << 1));
1265                         _avr_push16(avr, new_pc >> 1);
1266                         new_pc = new_pc + (o << 1);
1267                         cycle += 2;
1268                         // 'rcall .1' is used as a cheap "push 16 bits of room on the stack"
1269                         if (o != 0) {
1270                                 TRACE_JUMP();
1271                                 STACK_FRAME_PUSH();
1272                         }
1273                 }       break;
1274
1275                 case 0xe000: {  // LDI Rd, K 1110 KKKK RRRR KKKK -- aka SER (LDI r, 0xff)
1276                         uint8_t d = 16 + ((opcode >> 4) & 0xf);
1277                         uint8_t k = ((opcode & 0x0f00) >> 4) | (opcode & 0xf);
1278                         STATE("ldi %s, 0x%02x\n", avr_regname(d), k);
1279                         _avr_set_r(avr, d, k);
1280                 }       break;
1281
1282                 case 0xf000: {
1283                         switch (opcode & 0xfe00) {
1284                                 case 0xf000:
1285                                 case 0xf200:
1286                                 case 0xf400:
1287                                 case 0xf600: {  // All the SREG branches
1288                                         short o = ((short)(opcode << 6)) >> 9; // offset
1289                                         uint8_t s = opcode & 7;
1290                                         int set = (opcode & 0x0400) == 0;               // this bit means BRXC otherwise BRXS
1291                                         int branch = (avr->sreg[s] && set) || (!avr->sreg[s] && !set);
1292                                         const char *names[2][8] = {
1293                                                         { "brcc", "brne", "brpl", "brvc", NULL, "brhc", "brtc", "brid"},
1294                                                         { "brcs", "breq", "brmi", "brvs", NULL, "brhs", "brts", "brie"},
1295                                         };
1296                                         if (names[set][s]) {
1297                                                 STATE("%s .%d [%04x]\t; Will%s branch\n", names[set][s], o, new_pc + (o << 1), branch ? "":" not");
1298                                         } else {
1299                                                 STATE("%s%c .%d [%04x]\t; Will%s branch\n", set ? "brbs" : "brbc", _sreg_bit_name[s], o, new_pc + (o << 1), branch ? "":" not");
1300                                         }
1301                                         if (branch) {
1302                                                 cycle++;
1303                                                 new_pc = new_pc + (o << 1);
1304                                         }
1305                                 }       break;
1306                                 case 0xf800:
1307                                 case 0xf900: {  // BLD – Bit Store from T into a Bit in Register 1111 100r rrrr 0bbb
1308                                         uint8_t r = (opcode >> 4) & 0x1f; // register index
1309                                         uint8_t s = opcode & 7;
1310                                         uint8_t v = avr->data[r] | (avr->sreg[S_T] ? (1 << s) : 0);
1311                                         STATE("bld %s[%02x], 0x%02x = %02x\n", avr_regname(r), avr->data[r], 1 << s, v);
1312                                         _avr_set_r(avr, r, v);
1313                                 }       break;
1314                                 case 0xfa00:
1315                                 case 0xfb00:{   // BST – Bit Store into T from bit in Register 1111 100r rrrr 0bbb
1316                                         uint8_t r = (opcode >> 4) & 0x1f; // register index
1317                                         uint8_t s = opcode & 7;
1318                                         STATE("bst %s[%02x], 0x%02x\n", avr_regname(r), avr->data[r], 1 << s);
1319                                         avr->sreg[S_T] = (avr->data[r] >> s) & 1;
1320                                         SREG();
1321                                 }       break;
1322                                 case 0xfc00:
1323                                 case 0xfe00: {  // SBRS/SBRC – Skip if Bit in Register is Set/Clear 1111 11sr rrrr 0bbb
1324                                         uint8_t r = (opcode >> 4) & 0x1f; // register index
1325                                         uint8_t s = opcode & 7;
1326                                         int set = (opcode & 0x0200) != 0;
1327                                         int branch = ((avr->data[r] & (1 << s)) && set) || (!(avr->data[r] & (1 << s)) && !set);
1328                                         STATE("%s %s[%02x], 0x%02x\t; Will%s branch\n", set ? "sbrs" : "sbrc", avr_regname(r), avr->data[r], 1 << s, branch ? "":" not");
1329                                         if (branch) {
1330                                                 if (_avr_is_instruction_32_bits(avr, new_pc)) {
1331                                                         new_pc += 4; cycle += 2;
1332                                                 } else {
1333                                                         new_pc += 2; cycle++;
1334                                                 }
1335                                         }
1336                                 }       break;
1337                                 default: _avr_invalid_opcode(avr);
1338                         }
1339                 }       break;
1340
1341                 default: _avr_invalid_opcode(avr);
1342
1343         }
1344         avr->cycle += cycle;
1345         return new_pc;
1346 }
1347
1348