I too found bank selection very difficult to understand.
I am starting a project using PIC12F1822s, for their I2C functionality. Researching the background is rather like untangling a skein of threads, each one needs a lot of struggling before it comes clear. One of the threads I have managed to pull out is an explanation of the "BANKSEL" directive.
Background. There are several dozen SFRs -- Special Function Registers -- that assist in the operation of the device, mapped into the lower data memory. Because there are so many they are organised into 32 Banks, numbered 0 to 31, of 32 SFRs each. The SFRs are numbered sequentially in the form (bits) bbbbbfffffff where bbbbb is the bank number and fffffff is the offset in the bank. Their values are defined in the .INC file for the PIC, and there are many gaps in the sequence. Note that for the SFR offsets in banks 0 to 30 just five bits are sufficient, but for bank 31 seven bits are necessary.
When accessing one of these SFRs its Bank number must be in the BSR register, which is set by the "MOVLB " assembler instruction. To make this easy there is a Directive "BANKSEL " that can be used before each access of an SFR. (In other PICs, bits in the STATUS register hold the bank number) After successful testing, any superfluous BANKSELs can be removed. My puzzle (after establishing this so far -- the information in the documentation is sparse and scattered) was how this directive works. It is, of course, evaluated by the assembler before any code is produced, and this is my test code to check it out, using EQU to do the calculations, and explain it (note locn is "Location" i.e the address of the instruction.):
;BANKSEL is a directive that does the equivalent of
; movlb (<SFRname> & 0XF0) >> 7
;For example TRISA is defined in P12F1822.INC as:
;-----Bank1------------------
TRISA EQU H'008C'
Assembler:
Locn Resulting value Line Original code line content ";" is a comment
~~~~ ~~~~~~~~~~~~~~~ ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00047 ; Test of equivalent of BANKSEL directive
0000008C 00048 selbank equ TRISA
00000080 00049 selbnk1 equ selbank & 0XF80 ; Extract bank no. ..
00000001 00050 selbnk2 equ selbnk1 >> 7 ; .. move it to the right
0000000C 00051 selbnk3 equ TRISA & 0XF80 >> 7
[ Operator precedence: >> (bit shift right) higher than & (bitwise AND) ]
0000000C 00052 selbnk4 equ TRISA & (0XF80 >> 7) ; default
00000001 00053 selbnk5 equ (TRISA & 0XF80) >> 7 ; as needed`
. . .
006C 0021 00100 movlb 1 ; Should be same as next line
006D 0021 00101 banksel TRISA