diff --git a/code/VProc.h b/code/VProc.h index f082b19..a4212d7 100644 --- a/code/VProc.h +++ b/code/VProc.h @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -63,6 +64,7 @@ // Range of valid interrupt states #define MIN_INTERRUPT_LEVEL 1 #define MAX_INTERRUPT_LEVEL 7 +#define MAX_QUEUED_VEC_IRQ 1024 // User thread to simulation exchange structure typedef struct { @@ -85,8 +87,15 @@ typedef void * handle_t; // Callback pointer types typedef int (*pVUserInt_t) (void); typedef int (*pVUserIrqCB_t) (int); +typedef int (*pPyIrqCB_t) (int, int); typedef int (*pVUserCB_t) (int); +typedef struct { + uint32_t eventPtr; + uint32_t eventPopPtr; + uint32_t eventQueue [MAX_QUEUED_VEC_IRQ]; +} vecIrqState_t; + // Scheduler node state structure typedef struct { sem_t snd; @@ -95,10 +104,11 @@ typedef struct { rcv_buf_t rcv_buf; pVUserInt_t VInt_table[MAX_INTERRUPT_LEVEL+1]; pVUserIrqCB_t VUserIrqCB; + pPyIrqCB_t PyIrqCB; + vecIrqState_t irqState; pVUserCB_t VUserCB; } SchedState_t, *pSchedState_t; // Reference to node state array extern pSchedState_t ns[VP_MAX_NODES]; - #endif diff --git a/code/VSched.c b/code/VSched.c index 76ca9f5..d069b05 100644 --- a/code/VSched.c +++ b/code/VSched.c @@ -276,7 +276,7 @@ VPROC_RTN_TYPE VInit (VINIT_PARAMS) debug_io_printf("VInit(): node = %d\n", node); // Allocate some space for the node state and update pointer - ns[node] = (pSchedState_t) malloc(sizeof(SchedState_t)); + ns[node] = (pSchedState_t) calloc(1, sizeof(SchedState_t)); // Set up semaphores for this node debug_io_printf("VInit(): initialising semaphores for node %d\n", node); @@ -378,10 +378,10 @@ VPROC_RTN_TYPE VSched (VSCHED_PARAMS) // Sample inputs and update node state ns[node]->rcv_buf.data_in = VPDataIn; ns[node]->rcv_buf.interrupt = Interrupt; - + // If call to VSched is for interrupt and vector IRQ enabled (with callback registered) // don't process here with the level interrupt code and just return. - if (Interrupt && ns[node]->VUserIrqCB != NULL) + if (Interrupt && (ns[node]->VUserIrqCB != NULL || ns[node]->PyIrqCB != NULL)) { #ifndef VPROC_VHDL return 0; @@ -537,7 +537,7 @@ VPROC_RTN_TYPE VIrq(VIRQ_PARAMS) int node, value; int args[10]; - getVhpiParams(cb, &args[1], VPROCUSER_NUM_ARGS); + getVhpiParams(cb, &args[1], VIRQ_NUM_ARGS); // Get argument values of VProcUser VHPI call node = args[VPNODENUM_ARG]; @@ -545,11 +545,14 @@ VPROC_RTN_TYPE VIrq(VIRQ_PARAMS) # endif #endif - if (ns[node]->VUserIrqCB != NULL) { (*(ns[node]->VUserIrqCB))(value); } + else if (ns[node]->PyIrqCB != NULL) + { + (*(ns[node]->PyIrqCB))(value, node); + } #ifndef VPROC_VHDL return 0; @@ -618,3 +621,36 @@ VPROC_RTN_TYPE VAccess(VACCESS_PARAMS) return 0; #endif } + +///////////////////////////////////////////////////////////// +// Python vectored irq callback function +// +int PyIrqCB(int vec, int node) +{ + // Implements a ring buffer + ns[node]->irqState.eventQueue[ns[node]->irqState.eventPtr] = vec; + ns[node]->irqState.eventPtr = (ns[node]->irqState.eventPtr + 1) % MAX_QUEUED_VEC_IRQ; + + return 0; +} + +///////////////////////////////////////////////////////////// +// Python IRQ state fetch function +// +uint32_t PyFetchIrq (uint32_t *irq, const uint32_t node) +{ + //fprintf (stderr, "PyFetchIrq(): irq = %p node = %d", irq, node); + + uint32_t eventsInQueue = ns[node]->irqState.eventPtr - ns[node]->irqState.eventPopPtr; + + if (eventsInQueue) + { + // Get event from the beginning of the queue + *irq = ns[node]->irqState.eventQueue[ns[node]->irqState.eventPopPtr]; + + // Remove returned event from queue + ns[node]->irqState.eventPopPtr = (ns[node]->irqState.eventPopPtr + 1) % MAX_QUEUED_VEC_IRQ;; + } + + return eventsInQueue; +} diff --git a/code/VSched_pli.h b/code/VSched_pli.h index 6ac74e5..c4e2eed 100644 --- a/code/VSched_pli.h +++ b/code/VSched_pli.h @@ -71,6 +71,7 @@ #define VINIT_NUM_ARGS 1 #define VSCHED_NUM_ARGS 7 #define VPROCUSER_NUM_ARGS 2 +#define VIRQ_NUM_ARGS 2 #define VACCESS_NUM_ARGS 4 #define VPROC_RTN_TYPE void diff --git a/code/VUser.c b/code/VUser.c index ad3aaea..0e54f0c 100644 --- a/code/VUser.c +++ b/code/VUser.c @@ -322,11 +322,20 @@ void VRegInterrupt (const int level, const pVUserInt_t func, const unsigned node // void VRegIrq (const pVUserIrqCB_t func, const unsigned node) { - debug_io_printf("VRegIrq(): at node %d, registering irq callback\n", node); + printf("VRegIrq(): at node %d, registering irq callback\n", node); ns[node]->VUserIrqCB = func; } +///////////////////////////////////////////////////////////// +// Registers a python interface function as a vector IRQ +// callback +// +void VRegIrqPy (const pPyIrqCB_t func, const unsigned node) +{ + ns[node]->PyIrqCB = func; +} + ///////////////////////////////////////////////////////////// // Registers a user function as a callback against // $vprocuser diff --git a/code/VUser.h b/code/VUser.h index fb5de90..434a764 100644 --- a/code/VUser.h +++ b/code/VUser.h @@ -48,6 +48,7 @@ extern int VBurstRead (const unsigned addr, void *data, c extern int VTick (const unsigned ticks, const unsigned node); extern void VRegInterrupt (const int level, const pVUserInt_t func, const unsigned node); extern void VRegIrq (const pVUserIrqCB_t func, const unsigned node); +extern void VRegIrqPy (const pPyIrqCB_t func, const unsigned node); extern void VRegUser (const pVUserCB_t func, const unsigned node); // VUser function prototype for VInit in VSched.c diff --git a/python/modules/pyvproc.py b/python/modules/pyvproc.py index fa7b53e..f4ddd8e 100644 --- a/python/modules/pyvproc.py +++ b/python/modules/pyvproc.py @@ -31,6 +31,7 @@ class PyVProcClass : node = -1 api = None + __irqcb = None # Constructor def __init__(self, nodeIn, cmodulename = "./PyVProc.so") : @@ -48,26 +49,32 @@ class PyVProcClass : # API method to write a word def write (self, addr, data, delta = 0) : + self.__processIrq() self.api.PyWrite(addr, data, delta, self.node) # API method to read a word def read (self, addr, delta = 0) : + self.__processIrq() return self.api.PyRead(addr, delta, self.node) # API method to read a word as unsigned value def uread (self, addr, delta = 0) : + self.__processIrq() return c_uint32(self.read(addr, delta)).value # API method to tick for specified number of clocks def tick (self, ticks) : + self.__processIrq() self.api.PyTick(ticks, self.node) # API method to do a burst write def burstWrite(self, addr, data, length) : + self.__processIrq() self.api.PyBurstWrite(addr, (c_int * len(data))(*data), length, self.node) # API method to do a burst read def burstRead(self, addr, length) : + self.__processIrq() data = [] cdata = (c_int * length)(0) self.api.PyBurstRead(addr, cdata, length, self.node) @@ -77,9 +84,18 @@ class PyVProcClass : # API method to register a vectored interrupt callback def regIrq(self, irqCb) : - cb_ftype = CFUNCTYPE(c_int, c_int) - cb_wrap = cb_ftype(irqCb) - self.api.PyRegIrq(cb_wrap, self.node) + self.__irqcb = irqCb + + def __processIrq (self) : + while True : + irq = (c_int * 1)(0) + count = self.api.PyFetchIrq(irq, self.node) + + if count : + if self.__irqcb != None : + self.__irqcb(c_uint32(irq[0]).value) + else : + break def VPrint(self, printstr) : bytestring = printstr.encode('utf-8') diff --git a/python/src/PythonVProc.c b/python/src/PythonVProc.c index 94571f6..acf8b49 100644 --- a/python/src/PythonVProc.c +++ b/python/src/PythonVProc.c @@ -23,21 +23,15 @@ #include "PythonVProc.h" -// Pointer types for external API functions. Must match prototypes in VUser.h -typedef int (*wfunc_p) (const unsigned, const unsigned, const int, const unsigned); -typedef int (*rfunc_p) (const unsigned, unsigned *, const int, const unsigned); -typedef int (*wbfunc_p) (const unsigned, void *, const unsigned, const unsigned); -typedef int (*rbfunc_p) (const unsigned, void *, const unsigned, const unsigned); -typedef int (*tkfunc_p) (const unsigned, const unsigned ); -typedef void (*regirqfunc_p) (const pVUserIrqCB_t, const unsigned); - // Define pointers to the external API functions static wfunc_p Vwrite; static rfunc_p Vread; static wbfunc_p VburstWrite; static rbfunc_p VburstRead; static tkfunc_p Vtick; -static regirqfunc_p VregIrq; +static regirqfunc_p VregIrqPy; +static pyirqcb_p PyIrqCB; +static pyfetchirq_p PyFetchIrq_; // ------------------------------------------------------------ // Function to load VProc shared object and create bindings to @@ -68,13 +62,13 @@ static int BindToApiFuncs(void) fprintf(stderr, "***ERROR: failed to find symbol VRead\n"); return 1; } - + if ((VburstWrite = (wbfunc_p)dlsym(hdl, "VBurstWrite")) == NULL) { fprintf(stderr, "***ERROR: failed to find symbol VBurstWrite\n"); return 1; } - + if ((VburstRead = (rbfunc_p)dlsym(hdl, "VBurstRead")) == NULL) { fprintf(stderr, "***ERROR: failed to find symbol VBurstRead\n"); @@ -87,9 +81,21 @@ static int BindToApiFuncs(void) return 1; } - if ((VregIrq = (regirqfunc_p)dlsym(hdl, "VRegIrq")) == NULL) + if ((VregIrqPy = (regirqfunc_p)dlsym(hdl, "VRegIrqPy")) == NULL) { - fprintf(stderr, "***ERROR: failed to find symbol VRegIrq\n"); + fprintf(stderr, "***ERROR: failed to find symbol VRegIrqPy\n"); + return 1; + } + + if ((PyIrqCB = (pyirqcb_p)dlsym(hdl, "PyIrqCB")) == NULL) + { + fprintf(stderr, "***ERROR: failed to find symbol PyIrqCB\n"); + return 1; + } + + if ((PyFetchIrq_ = (pyfetchirq_p)dlsym(hdl, "PyFetchIrq")) == NULL) + { + fprintf(stderr, "***ERROR: failed to find symbol PyFetchIrq\n"); return 1; } @@ -117,6 +123,9 @@ int RunPython(const int node) return status; } + // Register the Python interface interrupt callback function + PyRegIrq(PyIrqCB, node); + Py_Initialize(); sprintf(strbuf, "VUserMain%d", node); @@ -195,8 +204,6 @@ uint32_t PyPrint(const char* str) uint32_t PyWrite (const uint32_t addr, const uint32_t data, const int delta, const uint32_t node) { - //printf("PyWrite: addr=0x%08x data=0x%08x delta=%d node=%d\n", addr, data, delta, node); - return Vwrite(addr, data, delta, node); } @@ -206,8 +213,6 @@ uint32_t PyWrite (const uint32_t addr, const uint32_t data, const int delta, con uint32_t PyRead (const uint32_t addr, const int delta, const uint32_t node) { - //printf("PyRead: addr=0x%08x delta=%d node=%d\n", addr, delta, node); - uint32_t rdata; int status = Vread(addr, &rdata, delta, node); @@ -221,8 +226,6 @@ uint32_t PyRead (const uint32_t addr, const int delta, const uint32_t node) uint32_t PyTick (const uint32_t ticks, const uint32_t node) { - //printf("PyTick: ticks=%d node=%d\n", ticks, node); - return Vtick(ticks, node); } @@ -232,7 +235,6 @@ uint32_t PyTick (const uint32_t ticks, const uint32_t node) uint32_t PyBurstWrite (const uint32_t addr, void *data, const uint32_t len, const uint32_t node) { - //printf("addr=0x%08x data_p=0x%p data[1]=0x%08x len = %d node = %d", addr, data, ((uint32_t*)data)[1], len, node); return VburstWrite(addr, data, len, node); } @@ -242,7 +244,6 @@ uint32_t PyBurstWrite (const uint32_t addr, void *data, const uint32_t len, cons uint32_t PyBurstRead (const uint32_t addr, void *data, const uint32_t len, const uint32_t node) { - return VburstRead(addr, data, len, node); } @@ -250,11 +251,18 @@ uint32_t PyBurstRead (const uint32_t addr, void *data, const uint32_t len, const // VRegIrq wrapper function for Python // ------------------------------------------------------------ -uint32_t PyRegIrq (const pVUserIrqCB_t func, const uint32_t node) +uint32_t PyRegIrq (const pPyIrqCB_t func, const uint32_t node) { - //printf("PyRegIrq %p %d\n", func, node); - - VregIrq(func, node); + VregIrqPy(func, node); return 0; -} \ No newline at end of file +} + +// ------------------------------------------------------------ +// PyFetchIrq wrapper function +// ------------------------------------------------------------ + +uint32_t PyFetchIrq (void *irq, const uint32_t node) +{ + PyFetchIrq_(irq, node); +} diff --git a/python/src/PythonVProc.h b/python/src/PythonVProc.h index 5d65ee7..6e3fe56 100644 --- a/python/src/PythonVProc.h +++ b/python/src/PythonVProc.h @@ -31,12 +31,25 @@ #define DEFAULTSTRBUFSIZE 256 +// Pointer types for external API functions. Must match prototypes in VUser.h +typedef int (*wfunc_p) (const unsigned, const unsigned, const int, const unsigned); +typedef int (*rfunc_p) (const unsigned, unsigned *, const int, const unsigned); +typedef int (*wbfunc_p) (const unsigned, void *, const unsigned, const unsigned); +typedef int (*rbfunc_p) (const unsigned, void *, const unsigned, const unsigned); +typedef int (*tkfunc_p) (const unsigned, const unsigned ); +typedef void (*regirqfunc_p) (const pPyIrqCB_t, const unsigned); +typedef int (*pyirqcb_p) (const int, const int); +typedef uint32_t (*pyfetchirq_p) (void *, const uint32_t); + +// API functions called from python int RunPython (const int node); -uint32_t PyWrite (const uint32_t addr, const uint32_t data, const int delta, const uint32_t node); +uint32_t PyWrite (const uint32_t addr, const uint32_t data, const int delta, const uint32_t node); uint32_t PyRead (const uint32_t addr, const int delta, const uint32_t node); uint32_t PyTick (const uint32_t ticks, const uint32_t node); -uint32_t PyBurstWrite (const uint32_t addr, void *data, const uint32_t len, const uint32_t node); -uint32_t PyBurstRead (const uint32_t addr, void *data, const uint32_t len, const uint32_t node); -uint32_t PyRegIrq (const pVUserIrqCB_t func, const uint32_t node); +uint32_t PyBurstWrite (const uint32_t addr, void *data, const uint32_t len, const uint32_t node); +uint32_t PyBurstRead (const uint32_t addr, void *data, const uint32_t len, const uint32_t node); + +uint32_t PyRegIrq (const pPyIrqCB_t func, const uint32_t node); +uint32_t PyFetchIrq (void *irq, const uint32_t node); #endif diff --git a/python/src/VUserMainPy.c b/python/src/VUserMainPy.c index cad0044..10f9550 100644 --- a/python/src/VUserMainPy.c +++ b/python/src/VUserMainPy.c @@ -20,36 +20,58 @@ // along with VProc. If not, see . // //===================================================================== + +#include + #include "VUser.h" #include "PythonVProc.h" #define SLEEPFOREVER {while(1) VTick(0x7fffffff, node);} +// In Python 64 nodes is the maximum (the default), but check this hasn't been +// overridden to a large number. +#if VP_MAX_NODES > 64 +#error "**ERROR: VP_MAX_NODES > 64 is unsupported" +#endif + +static uint64_t active_node = 0; + static void VUserMain(int node) { - int status = RunPython(node); - - if (status) + if (active_node) { - printf("***ERROR: RunPython(%d) returned error status %d\n", node, status); + fprintf(stderr, "***ERROR: Under Python only a single instance of VProc is supported at this time.\n"); + } + else + { + active_node |= (uint64_t)1 << node; + + int status = RunPython(node); + + if (status) + { + fprintf(stderr, "***ERROR: RunPython(%d) returned error status %d\n", node, status); + } } SLEEPFOREVER; } -void VUserMain0() {VUserMain(0);} -void VUserMain1() {VUserMain(1);} -void VUserMain2() {VUserMain(2);} -void VUserMain3() {VUserMain(3);} -void VUserMain4() {VUserMain(4);} -void VUserMain5() {VUserMain(5);} -void VUserMain6() {VUserMain(6);} -void VUserMain7() {VUserMain(7);} -void VUserMain8() {VUserMain(8);} -void VUserMain9() {VUserMain(9);} -void VUserMain10() {VUserMain(10);} -void VUserMain11() {VUserMain(11);} -void VUserMain12() {VUserMain(12);} -void VUserMain13() {VUserMain(13);} -void VUserMain14() {VUserMain(14);} -void VUserMain15() {VUserMain(15);} \ No newline at end of file +// Bindings for VUserMain entry points. + +void VUserMain0() {VUserMain(0);} void VUserMain1() {VUserMain(1);} void VUserMain2() {VUserMain(2);} void VUserMain3() {VUserMain(3);} +void VUserMain4() {VUserMain(4);} void VUserMain5() {VUserMain(5);} void VUserMain6() {VUserMain(6);} void VUserMain7() {VUserMain(7);} +void VUserMain8() {VUserMain(8);} void VUserMain9() {VUserMain(9);} void VUserMain10() {VUserMain(10);} void VUserMain11() {VUserMain(11);} +void VUserMain12() {VUserMain(12);} void VUserMain13() {VUserMain(13);} void VUserMain14() {VUserMain(14);} void VUserMain15() {VUserMain(15);} +void VUserMain16() {VUserMain(16);} void VUserMain17() {VUserMain(17);} void VUserMain18() {VUserMain(18);} void VUserMain19() {VUserMain(19);} +void VUserMain20() {VUserMain(20);} void VUserMain21() {VUserMain(21);} void VUserMain22() {VUserMain(22);} void VUserMain23() {VUserMain(23);} +void VUserMain24() {VUserMain(24);} void VUserMain25() {VUserMain(25);} void VUserMain26() {VUserMain(26);} void VUserMain27() {VUserMain(27);} +void VUserMain28() {VUserMain(28);} void VUserMain29() {VUserMain(29);} void VUserMain30() {VUserMain(30);} void VUserMain31() {VUserMain(31);} +void VUserMain32() {VUserMain(32);} void VUserMain33() {VUserMain(33);} void VUserMain34() {VUserMain(34);} void VUserMain35() {VUserMain(35);} +void VUserMain36() {VUserMain(36);} void VUserMain37() {VUserMain(37);} void VUserMain38() {VUserMain(38);} void VUserMain39() {VUserMain(39);} +void VUserMain40() {VUserMain(40);} void VUserMain41() {VUserMain(41);} void VUserMain42() {VUserMain(42);} void VUserMain43() {VUserMain(43);} +void VUserMain44() {VUserMain(44);} void VUserMain45() {VUserMain(45);} void VUserMain46() {VUserMain(46);} void VUserMain47() {VUserMain(47);} +void VUserMain48() {VUserMain(48);} void VUserMain49() {VUserMain(49);} void VUserMain50() {VUserMain(50);} void VUserMain51() {VUserMain(51);} +void VUserMain52() {VUserMain(52);} void VUserMain53() {VUserMain(53);} void VUserMain54() {VUserMain(54);} void VUserMain55() {VUserMain(55);} +void VUserMain56() {VUserMain(56);} void VUserMain57() {VUserMain(57);} void VUserMain58() {VUserMain(58);} void VUserMain59() {VUserMain(59);} +void VUserMain60() {VUserMain(60);} void VUserMain61() {VUserMain(61);} void VUserMain62() {VUserMain(62);} void VUserMain63() {VUserMain(63);} \ No newline at end of file