diff mbox

[05/10] target-avr: adding AVR interrupt handling

Message ID 1464898022-97990-5-git-send-email-rolnik@amazon.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Rolnik June 2, 2016, 8:06 p.m. UTC
Signed-off-by: Michael Rolnik <mrolnik@gmail.com>
---
 target-avr/helper.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

Comments

Richard Henderson June 4, 2016, 10:26 p.m. UTC | #1
On 06/02/2016 01:06 PM, Michael Rolnik wrote:
> +    } else if (env->intsrc != 0) {
> +        vector  = __builtin_ffs(env->intsrc);
> +    }

Use either ffs (no __builtin) or, preferably, ctz32 from qemu/host-utils.h.

> +        env->pc = base + vector * size;
> +        env->pc = base + vector * size;
> +        env->pc = base + vector * size;

No point replicating this 3 times.


r~
Michael Rolnik June 5, 2016, 5:10 a.m. UTC | #2
fixed.

On Sun, Jun 5, 2016 at 1:26 AM, Richard Henderson <rth@twiddle.net> wrote:

> On 06/02/2016 01:06 PM, Michael Rolnik wrote:
>
>> +    } else if (env->intsrc != 0) {
>> +        vector  = __builtin_ffs(env->intsrc);
>> +    }
>>
>
> Use either ffs (no __builtin) or, preferably, ctz32 from qemu/host-utils.h.
>
> +        env->pc = base + vector * size;
>> +        env->pc = base + vector * size;
>> +        env->pc = base + vector * size;
>>
>
> No point replicating this 3 times.
>
>
> r~
>
diff mbox

Patch

diff --git a/target-avr/helper.c b/target-avr/helper.c
index aec37af..ed22b37 100644
--- a/target-avr/helper.c
+++ b/target-avr/helper.c
@@ -33,12 +33,74 @@  bool                avr_cpu_exec_interrupt(
                                 CPUState               *cs,
                                 int                     interrupt_request)
 {
-    return  false;
+    CPUClass    *cc  = CPU_GET_CLASS(cs);
+    AVRCPU      *cpu = AVR_CPU(cs);
+    CPUAVRState *env = &cpu->env;
+
+    bool    ret = false;
+
+    if (interrupt_request & CPU_INTERRUPT_RESET) {
+        if (cpu_interrupts_enabled(env)) {
+            cs->exception_index = EXCP_RESET;
+            cc->do_interrupt(cs);
+
+            cs->interrupt_request   &= ~CPU_INTERRUPT_RESET;
+
+            ret = true;
+        }
+    }
+    if (interrupt_request & CPU_INTERRUPT_HARD) {
+        if (cpu_interrupts_enabled(env) && env->intsrc != 0) {
+            int     index   = __builtin_ffs(env->intsrc) - 1;
+            cs->exception_index = EXCP_INT(index);
+            cc->do_interrupt(cs);
+
+            env->intsrc             &= env->intsrc - 1; /* clear the interrupt */
+            cs->interrupt_request   &= ~CPU_INTERRUPT_HARD;
+
+            ret = true;
+        }
+    }
+    return ret;
 }
 
 void                avr_cpu_do_interrupt(
                                 CPUState           *cs)
 {
+    AVRCPU         *cpu = AVR_CPU(cs);
+    CPUAVRState    *env = &cpu->env;
+
+    uint32_t    ret     = env->pc;
+    int         vector;
+    int         size    = avr_feature(env, AVR_FEATURE_JMP_CALL) ? 2 : 1;
+    int         base    = 0;    /* TODO: where to get it */
+
+    if (cs->exception_index == EXCP_RESET) {
+        vector  = 0;
+    } else if (env->intsrc != 0) {
+        vector  = __builtin_ffs(env->intsrc);
+    }
+
+    if (avr_feature(env, AVR_FEATURE_3_BYTE_PC)) {
+        stb_phys(cs->as, env->sp--, (ret & 0x0000ff));
+        stb_phys(cs->as, env->sp--, (ret & 0x00ff00) >>  8);
+        stb_phys(cs->as, env->sp--, (ret & 0xff0000) >> 16);
+
+        env->pc = base + vector * size;
+    } else if (avr_feature(env, AVR_FEATURE_2_BYTE_PC)) {
+        stb_phys(cs->as, env->sp--, (ret & 0x0000ff));
+        stb_phys(cs->as, env->sp--, (ret & 0x00ff00) >>  8);
+
+        env->pc = base + vector * size;
+    } else {
+        stb_phys(cs->as, env->sp--, (ret & 0x0000ff));
+
+        env->pc = base + vector * size;
+    }
+
+    env->sregI  = 0;    /*  clear Global Interrupt Flag */
+
+    cs->exception_index = -1;
 }
 
 int                 avr_cpu_memory_rw_debug(