Example Forth Words
PICK
code pick
; Interrupts must be disabled before calling this function
; because it modifies the SP
;
call _disable_ints
movpf SP, WREG ; save the current stack pointer
incf TLOW, F ; ensure pick index > 0
__pickloop
incf SP, F ; point to the next stack item
incf SP, F
decfsz TLOW, F ; decrement pick index
goto __pickloop ; loop if necessary
movfp STACK, THIGH ; copy the picked item to the top
incf SP, F
movfp STACK, TLOW
movfp WREG, SP ; restore the stack pointer
goto _enable_ints ; defer a return
endcode
@
code @
; Interrupts must be disabled before calling this function
; because it modifies the RSP
;
call _disable_ints
; fetch
movpf RSP, XLOW ; save RSP
movfp TLOW, FSR ; load address
swapf THIGH, W ; put variable bank in WREG
iorwf BSR,F ; set the bank (ASSUMES HIGH NIBBLE BSR WAS 0 !!)
movpf INDF, THIGH ; fetch high byte
incf FSR, F ; address low byte
movpf INDF, TLOW ; fetch low byte
gprbank BANKSTACKS
movfp XLOW, RSP ; restore RSP
goto _enable_ints ; defer a return
endcode
A Peripheral Interrupt Handler (Inline Assembly)
fload interupt.f \ Load the interrupt support file.
32 :interrupt (peripheral_interrupt)
[code]
; vector to the appropriate interrupt handler.
dup ; make space on the stack
periphbank BANK1
movpf PIR, TLOW ; store PIR in TLOW
movpf PIE, WREG
andwf TLOW, F ; PIR AND PIE => TLOW
btfsc TLOW, TMR1IF ; check for timer 1 interrupt
call _timer1_int
btfsc TLOW, TMR3IF ; check for timer 3 interrupt
call _timer3_int
drop ; return space to the stack
; check for a hardware stack overrun.
btfss CPUSTA, STKAV
call _hstack_error
endcode
interrupt;
A Normal Forth Word
: max 2dup < if nip else drop then ;