misc: Point to correct simavr include dirs
[simavr] / simavr / sim / sim_core.c
index f3106ef..e62938f 100644 (file)
@@ -101,12 +101,12 @@ int donttrace = 0;
 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
 {
        if (addr > avr->ramend) {
-               printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
+               AVR_LOG(avr, LOG_ERROR, "CORE: *** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
                                avr->pc, _avr_sp_get(avr), avr->flash[avr->pc + 1] | (avr->flash[avr->pc]<<8), addr, v);
                CRASH();
        }
        if (addr < 32) {
-               printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
+               AVR_LOG(avr, LOG_ERROR, "CORE: *** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
                                avr->pc, _avr_sp_get(avr), avr->flash[avr->pc + 1] | (avr->flash[avr->pc]<<8), addr, v);
                CRASH();
        }
@@ -131,7 +131,7 @@ void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
 {
        if (addr > avr->ramend) {
-               printf( FONT_RED "*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n" FONT_DEFAULT,
+               AVR_LOG(avr, LOG_ERROR, FONT_RED "CORE: *** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n" FONT_DEFAULT,
                                avr->pc, _avr_sp_get(avr), avr->flash[avr->pc + 1] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
                CRASH();
        }
@@ -155,8 +155,7 @@ static inline void _avr_set_r(avr_t * avr, uint8_t r, uint8_t v)
        if (r == R_SREG) {
                avr->data[R_SREG] = v;
                // unsplit the SREG
-               for (int i = 0; i < 8; i++)
-                       avr->sreg[i] = (v & (1 << i)) != 0;
+               SET_SREG_FROM(avr, v);
                SREG();
        }
        if (r > 31) {
@@ -209,13 +208,7 @@ static inline uint8_t _avr_get_ram(avr_t * avr, uint16_t addr)
                 * SREG is special it's reconstructed when read
                 * while the core itself uses the "shortcut" array
                 */
-               avr->data[R_SREG] = 0;
-               for (int i = 0; i < 8; i++)
-                       if (avr->sreg[i] > 1) {
-                               printf("** Invalid SREG!!\n");
-                               CRASH();
-                       } else if (avr->sreg[i])
-                               avr->data[R_SREG] |= (1 << i);
+               READ_SREG_INTO(avr, avr->data[R_SREG]);
                
        } else if (addr > 31 && addr < 256) {
                uint8_t io = AVR_DATA_TO_IO(addr);
@@ -298,7 +291,7 @@ static void _avr_invalid_opcode(avr_t * avr)
        printf( FONT_RED "*** %04x: %-25s Invalid Opcode SP=%04x O=%04x \n" FONT_DEFAULT,
                        avr->pc, avr->trace_data->codeline[avr->pc>>1]->symbol, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc+1]<<8));
 #else
-       printf( FONT_RED "*** %04x: Invalid Opcode SP=%04x O=%04x \n" FONT_DEFAULT,
+       AVR_LOG(avr, LOG_ERROR, FONT_RED "CORE: *** %04x: Invalid Opcode SP=%04x O=%04x \n" FONT_DEFAULT,
                        avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc+1]<<8));
 #endif
 }
@@ -827,7 +820,12 @@ avr_flashaddr_t avr_run_one(avr_t * avr)
                        } else switch (opcode) {
                                case 0x9588: { // SLEEP
                                        STATE("sleep\n");
-                                       avr->state = cpu_Sleeping;
+                                       /* Don't sleep if there are interrupts about to be serviced.
+                                        * Without this check, it was possible to incorrectly enter a state
+                                        * in which the cpu was sleeping and interrupts were disabled. For more
+                                        * details, see the commit message. */
+                                       if (!avr_has_pending_interrupts(avr) || !avr->sreg[S_I])
+                                               avr->state = cpu_Sleeping;
                                }       break;
                                case 0x9598: { // BREAK
                                        STATE("break\n");