Commit 45e7712b4debe764d31b66706abebb18cbc7c6a5
authorMichel Pollet <buserror@gmail.com>
Tue, 14 Jul 2015 18:04:05 +0000 (19:04 +0100)
committerMichel Pollet <buserror@gmail.com>
Tue, 14 Jul 2015 18:04:05 +0000 (19:04 +0100)
There's already a standard way to make FIFO's, so converted the
interrupt code to use that.

Signed-off-by: Michel Pollet <buserror@gmail.com>
3 files changed:
simavr/sim/fifo_declare.h
simavr/sim/sim_interrupts.c
simavr/sim/sim_interrupts.h

index 35f45fee5826131d7862fdc94458c40bb0e40d86..8a3b2fb68d2f1a34e00a6912fbd08c9819ef2fec 100644 (file)
@@ -53,7 +53,7 @@ extern "C" {
                void myfifo_write_at(myfifo_t *c, uint16_t o, uint8_t b);
 
        In your .c you need to 'implement' the fifo:
-       DEFINE_FIFO(uint8_t, myfifo, 128)
+       DEFINE_FIFO(uint8_t, myfifo)
 
        To use the fifo, you must declare at least one :
        myfifo_t fifo = FIFO_NULL;
index b1539075077ed6b4d07b70aa5018386b73a84626..0022795a3a369aba19b74ca7a2bad92d710dc271 100644 (file)
@@ -28,9 +28,7 @@
 #include "sim_avr.h"
 #include "sim_core.h"
 
-// modulo a cursor value on the pending interrupt fifo
-#define INT_FIFO_SIZE (sizeof(table->pending) / sizeof(avr_int_vector_t *))
-#define INT_FIFO_MOD(_v) ((_v) &  (INT_FIFO_SIZE - 1))
+DEFINE_FIFO(avr_int_vector_p, avr_int_pending);
 
 void
 avr_interrupt_init(
@@ -39,7 +37,6 @@ avr_interrupt_init(
        avr_int_table_p table = &avr->interrupts;
        memset(table, 0, sizeof(*table));
 
-       printf("%s\n", __func__);
        static const char *names[] = { ">global_int_pending", ">global_int_running" };
        avr_init_irq(&avr->irq_pool, table->irq,
                        0, // base number
@@ -53,7 +50,7 @@ avr_interrupt_reset(
        avr_int_table_p table = &avr->interrupts;
 
        table->running_ptr = 0;
-       table->pending_r = table->pending_w = 0;
+       avr_int_pending_reset(&table->pending);
        avr->interrupt_state = 0;
        for (int i = 0; i < table->vector_count; i++)
                table->vector[i]->pending = 0;
@@ -73,7 +70,6 @@ avr_register_vector(
        avr_init_irq(&avr->irq_pool, vector->irq,
                        vector->vector * 256, // base number
                        AVR_INT_IRQ_COUNT, names);
-//     vector->irq.irq = vector->vector;
        table->vector[table->vector_count++] = vector;
        if (vector->trace)
                printf("%s register vector %d (enabled %04x:%d)\n", __FUNCTION__, vector->vector, vector->enable.reg, vector->enable.bit);
@@ -87,7 +83,7 @@ avr_has_pending_interrupts(
                avr_t * avr)
 {
        avr_int_table_p table = &avr->interrupts;
-       return table->pending_r != table->pending_w;
+       return !avr_int_pending_isempty(&table->pending); // table->pending_r != table->pending_w;
 }
 
 int
@@ -136,8 +132,7 @@ avr_raise_interrupt(
 
                avr_int_table_p table = &avr->interrupts;
 
-               table->pending[table->pending_w++] = vector;
-               table->pending_w = INT_FIFO_MOD(table->pending_w);
+               avr_int_pending_write(&table->pending, vector);
 
                if (avr->sreg[S_I] && avr->interrupt_state == 0)
                        avr->interrupt_state = 1;
@@ -238,25 +233,23 @@ avr_service_interrupts(
        avr_int_table_p table = &avr->interrupts;
 
        // how many are pending...
-       int cnt = INT_FIFO_MOD(
-                               (table->pending_w + INT_FIFO_SIZE) - table->pending_r);
+       int cnt = avr_int_pending_get_read_size(&table->pending);
        // locate the highest priority one
        int min = 0xff;
        int mini = 0;
        for (int ii = 0; ii < cnt; ii++) {
-               int vi = INT_FIFO_MOD(table->pending_r + ii);
-               avr_int_vector_t * v = table->pending[vi];
+               avr_int_vector_t * v = avr_int_pending_read_at(&table->pending, ii);
                if (v->vector < min) {
                        min = v->vector;
-                       mini = vi;
+                       mini = ii;
                }
        }
-       avr_int_vector_t * vector = table->pending[mini];
+       avr_int_vector_t * vector = avr_int_pending_read_at(&table->pending, mini);
 
        // now move the one at the front of the fifo in the slot of
        // the one we service
-       table->pending[mini] = table->pending[table->pending_r++];
-       table->pending_r = INT_FIFO_MOD(table->pending_r);
+       table->pending.buffer[mini % avr_int_pending_fifo_size] = 
+                       avr_int_pending_read(&table->pending);
        avr_raise_irq(avr->interrupts.irq + AVR_INT_IRQ_PENDING,
                        avr_has_pending_interrupts(avr));
 
index 0ee929430eb167ea1864ff000a4d0007484c18b7..b55ad17d39b89bd7eab3472ad4aa2dae4577e96d 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "sim_avr_types.h"
 #include "sim_irq.h"
+#include "fifo_declare.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -47,15 +48,16 @@ typedef struct avr_int_vector_t {
                                        trace : 1,              // only for debug of a vector
                                        raise_sticky : 1;       // 1 if the interrupt flag (= the raised regbit) is not cleared
                                                                                // by the hardware when executing the interrupt routine (see TWINT)
-} avr_int_vector_t;
+} avr_int_vector_t, *avr_int_vector_p;
+
+// Size needs to be >= max number of vectors, and a power of two
+DECLARE_FIFO(avr_int_vector_p, avr_int_pending, 64);
 
 // interrupt vectors, and their enable/clear registers
 typedef struct  avr_int_table_t {
        avr_int_vector_t * vector[64];
        uint8_t                 vector_count;
-       avr_int_vector_t * pending[64]; // needs to be >= vectors and a power of two
-       uint8_t                 pending_w,
-                                       pending_r;      // fifo cursors
+       avr_int_pending_t pending;
        uint8_t                 running_ptr;
        avr_int_vector_t *running[64]; // stack of nested interrupts
        // global status for pending + running in interrupt context