Searching \ for '[PIC] Code packing: yet another set of banking mac' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/memory.htm?key=bank
Search entire site for: 'Code packing: yet another set of banking mac'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Code packing: yet another set of banking mac'
2008\07\10@142348 by Bob Ammerman

picon face

{Quote hidden}

I have a set of macros (see below) similar to Olin's DBANKIF, but I carry
things a step further.

I am sure most of you are familiar with the dread '302' warning from the
assembler (ie 'possibly wrong bank') and I am sure that most of you simply
disable that warning.

Well, believe it or not, I leave it enabled in many cases. This is because
my macros take advantage of it by generating warnings when I try to refences
an GPR/SFR that is not in the current bank.

-- Bob Ammerman
RAm Systems

;=============================================================================
;=============================================================================
;=============================================================================
;   B A N K I N G    M A C R O S
;=============================================================================
; All references to GPRs/SFRs should be written as _B(file), _N(file) or
_(file)
;
; _B(file) means you are sure the register is in the current bank
;_N(file) means you are sure the register is in unbanked space
;        (ie: INDF,PCL,STATUS,FSR,PCLATH,INTCON and 0x70 thru 0x7F)
;_(file) means you don't care which
;
; "INBANKN bnk", where bnk is in {0,1,2,3}, TELLS the assembler that the
;     processor is already in the specified bank. No code is generated
;
; "INBANK file", where file is a GPR or SFR, TELLS the assembler that the
;    processor is already in 'file's bank. However, if 'file' is unbanked
then
;    the 'remembered' bank is 'no bank selected'. In any case, no code is
;    generated.
;
; "NOBANK" tells the assembler that the current bank is unknown. No code
;    is generated.
;
; "TOBANKN bnk", where bnk is in {0,1,2,3}, conditionally generates the
;    necessary BSF/BCF instructions to move to the specified bank, and
;    remembers the newly selected bank.
;
; "TOBANK file", where file is a GPR or SFR, conditionally generates the
;    necessary BSF/BCF instructions to move to 'file's bank, and remembers
;    the new bank. However, if 'file' is an unbanked register than nothing
is
;    done (the previously remembered bank, if any, is left unchanged).
;
; "FRCBANKN bnk", where bnk is in {0,1,2,3}, unconditionally generates
;    the BSF/BCF instructions to move to the specified bank, and remembers
;    the new bank.
;
; "FRCBANK file", where file is a GPR or SFR, unconditionally generates
;    the BSF/BCF instructions to move to 'file's bank, and remembers the
;    new bank. However, if 'file' is an unbanked regsiter then nothing is
;    done (the previously remembered bank, if any, is left unchanged).
;
; "ZAPBANK" forces the processor to bank 0 and remembers that fact.
;    A single "clrf status" instruction is generated.
;=============================================================================
;=============================================================================

; Saying _B(file) is asserting that file is in the current bank. If it isn't
in the current bank the resulting address
; will not be in bank 0 and we'll get a 302 warning
#define _B(r)  ((r) ^ __CURBANK)

; Saying _N(file) is asserting that file is accessible in any bank. If it
isn't an unbanked register
; (INDF,PCL,STATUS,FSR,PCLATH,INTCON and 0x70 thru 0x7F) the resulting
address will be -1
; and we'll get a warning
#define _N(r)
((r)|(-1*((r)!=INDF&(r)!=PCL&(r)!=STATUS&(r)!=FSR&(r)!=PCLATH&(r)!=INTCON&(((r)&0xFFF0)!=0x70))))

; Saying _(file) causes an error if the register is not in the current bank
and is not an unbanked register
; This #define combines the _B and _N defines from above
#define _(r) (((_N(r)==-1)*(_B(r)))|((_N(r)!=-1)*(r)))

; INBANKN macro TELLs the asssembler that the processor is (already) in the
specified bank.

INBANKN macro bnk
__CURBANK = bnk << 7
endm

; INBANK macro TELLs the assembler that the current bank is that of its
argument.
; If the argument is an unbanked register then INBANK tells the assembler
the current
; bank is unknown

INBANK macro file
if (_N(r))!=-1)
_CURBANK = 0xFF00        ; we don't have a known bank
else
_CURBANK = (r) & 0x180    ; use bank of argument
endif
endm

; NOBANK macro TELLs the assembler that the current bank is unknown. This is
used
; at interrupt entry points and at labels where multiple code paths have
different banking values.

NOBANK macro
__CURBANK = 0xFF00 ; force error on any banked reference
endm

NOBANK   ; start in unknown bank

; ZAPBANK macro forces the processor to bank 0, and remembers
; that fact.
ZAPBANK macro
  clrf _N(STATUS) ; Quick return to bank 0
__CURBANK = 0x000
endm

; TOBANKN macro generates the necessary BSF/BCF instructions to
; go to the specified bank, and remembers that fact.

TOBANKN macro bank
 local temp
temp = __CURBANK >> 7 ; current bank as a number

 if (temp == (0xFF00 >> 7))
; we have no idea what bank we're in, so force both bits to change
temp = bank ^ 3
endif

 if (temp^bank) & 1
    if (bank & 1)
 list x=on
bsf _N(STATUS),RP0
 list x=off
    else
 list x=on
bcf _N(STATUS),RP0
 list x=off
    endif
 endif

 if (temp^bank) & 2
    if (bank & 2)
 list x=on
bsf _N(STATUS),RP1
 list x=off
    else
 list x=on
bcf _N(STATUS),RP1
 list x=off
    endif
 endif

__CURBANK = bank << 7 ; new bank as an offset
 list x=off
endm

; TOBANK macro sets the bank to that of the argument register, unless
; the argument register is an unbanked value, in which case the bank isn't
; changed. The resulting new bank is remembered.

TOBANK macro file

if (_N(file))!=-1    ; ignore unbanked registers
 exitm
endif

TOBANKN (file>>7)    ; convert reg address to bank number
endm

; FRCBANKN macro generates the necessary BCF/BSF instructions to go
; to the specified bank unconditionally

FRCBANKN macro bank
NOBANK                ; pretend we don't know where we are
TOBANKN bank     ; unconditonally generate banking instructions
endm

; FRCBANK macro generates the necessary BCF/BSF instructions to
; go unconditionally to the file's bank, unless the file is non-banked

FRCBANK macro file
if (_N(file))!=-1    ;ignore unbanked registers
   exitm
endif

NOBANK            ; pretend we don't know where we are
TOBANK file       ; unconditionally generate banking instructions
endm

2008\07\10@145331 by Rolf

face picon face
Now that makes some sense to me, it's very clever. I still need to
understand it more ....

Rolf

Bob Ammerman wrote:
{Quote hidden}

2008\07\10@214413 by Bob Ammerman

picon face
Maybe some sample code:


   TOBANK    somevar
   movf    _(somevar),w
   movwf    _(status)
   goto        xxx

yyy:
   TOBANK    anothervar
   addwf    _(anothervar)
   ...
   goto       xxx
   ...
xxx:  NOBANK



{Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2008 , 2009 only
- Today
- New search...