I would be interested in any thoughts on how best to apply a "modular" programming discipline to 14-bit PIC applications in which the overhead presented by interrupt handling is an important consideration. The conventional approach with MPASM and MPLINK is to separate variables and code for distinct functional blocks into individual source files, with include files providing references to externally visible elements. As I see it, there are unavoidable penalties when attempting to apply this approach to interrupt handlers, as each functional block within the interrupt subroutine (a common subroutine in a 14-bit core) would need to be called as a subroutine. This consumes several extra instruction cycles per interrupt type that is handled in this way, and also an extra stack level globally. I have shown below three approaches to illustrate the issue. The first is the "non-modular" approach of putting everything in a single source file. The second is a fully modular approach with associated penalties. The third is an attempt to work around the performance penalties by using macros, but one which brings its own readability problems. ------------------------------------------------------------------------ 1. NON-MODULAR APPROACH ------------------------ ************ Single source file ************ ; All variable definitions go here ORG H'0004' ; Save W, STATUS, PCLATH etc. ; Timer interrupt handler code goes here ; Other interrupt handler code goes here ; Restore PCLATH, STATUS, W etc. retfie ; All subroutines and main routine go here ------------------------------------------------------------------------ 2. MODULAR APPROACH USING MPLINK --------------------------------- ************ Main source file ************ #INCLUDE "TIMER.INC" ; Repeat for other relevant module header files CODE H'0004' ; Save W, STATUS, PCLATH etc. Check_Timer_Int btfss INTCON,T0IE ; Moved outside handler to avoid goto Check_Next_Int ; PAGESEL/CALL/RETURN overhead ; when timer flag is not set PAGESEL Handle_Timer_Int ; Adds 2 cycles call Handle_Timer_Int ; Adds 4 cycles including RETURN Check_Next_Int ; Repeat above approach for other relevant interrupts ; Restore PCLATH, STATUS, W etc. retfie ; Main routine goes here *********** Timer module include file *********** EXTERN Handle_Timer_Int EXTERN Some_Timer_Subroutine_1 ; etc. *********** Timer module source file *********** ; Timer variable definitions go here Handle_Timer_Int ; Timer interrupt handler code goes here return ; Other timer subroutines go here [Repeat this approach for other relevant modules] ------------------------------------------------------------------------ 3. MODULAR APPROACH USING MACROS --------------------------------- ************ Main source file ************ #INCLUDE "TIMER.INC" CODE H'0004' ; Save W, STATUS, PCLATH etc. Timer_Int_Handler ; MACRO - No overheads ; Repeat for other relevant interrupts ; Restore PCLATH, STATUS, W etc. retfie ; Non-serial subroutines and main code goes down here *********** Timer module include file *********** Timer_Int_Handler MACRO ; Timer interrupt handler code goes here ENDM ; NOTE - There are at least two sub-approaches here: ; ; (a) Add EXTERN definitions for variables and subroutines which ; are then linked in from a separate module source file as in ; approach 2. ; ; Advantage: Enables MPLINK and MPLIB to be used for ; non-interrupt code ; Disadvantage: Splits module source code across two ; files (include and separate source file) ; ; (a) Define similar macro definitions for variables and ; subroutines which are invoked later on in main source file. ; Advantage: Keeps module source code together ; Disadvantage: Messy and still requires tracking of ; macro invocations in main source file ------------------------------------------------------------------------ I would like to be able to adopt a modular approach so that I can re-use functional blocks easily, but I cannot afford this performance hit on interrupt handlers. Am I missing another approach which gives me the best of both worlds? Thanks in advance for any pointers. -- Ian Chapman Chapmip Technology, UK -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics