diff mbox

[v4,5/9] target-avr: adding AVR interrupt handling

Message ID 1465209480-71364-6-git-send-email-rolnik@amazon.com
State New
Headers show

Commit Message

Michael Rolnik June 6, 2016, 10:37 a.m. UTC
Signed-off-by: Michael Rolnik <mrolnik@gmail.com>
---
 target-avr/helper.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)

Comments

Richard Henderson June 6, 2016, 9:44 p.m. UTC | #1
On 06/06/2016 03:37 AM, Michael Rolnik wrote:
> +    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);
> +    } 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);
> +    } else {
> +        stb_phys(cs->as, env->sp--, (ret & 0x0000ff));
> +    }

It would be better to use cpu_stb_data.


r~
Peter Maydell June 6, 2016, 10:17 p.m. UTC | #2
On 6 June 2016 at 22:44, Richard Henderson <rth@twiddle.net> wrote:
> On 06/06/2016 03:37 AM, Michael Rolnik wrote:
>>
>> +    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);
>> +    } 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);
>> +    } else {
>> +        stb_phys(cs->as, env->sp--, (ret & 0x0000ff));
>> +    }
>
>
> It would be better to use cpu_stb_data.

Or address_space_stb() if you want to care about getting
the right behaviour if the store fails (we don't really
care about this on many targets at the moment though).

thanks
-- PMM
diff mbox

Patch

diff --git a/target-avr/helper.c b/target-avr/helper.c
index fbab91d..e798dd9 100644
--- a/target-avr/helper.c
+++ b/target-avr/helper.c
@@ -31,11 +31,68 @@ 
 
 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   = ctz32(env->intsrc);
+            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  = ctz32(env->intsrc) + 1;
+    }
+
+    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);
+    } 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);
+    } 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(CPUState *cs, vaddr addr, uint8_t *buf, int len, bool is_write)