3

I am trying to simulate the PIC16F84 and now need to implement PCL / PCLATH registers.

The PIC16F84 has 1K of Program memory.

The PCL is 8Bit wide, so in this case Bit 0 and 1 of PCLATH is used to switch between the four Pages each having a size of 256B, am I right so far?

Based on this, I do not understand the following:

The Datasheet states for a GOTO:

The upper bits of PC are loaded from PCLATH<4:3>. GOTO is a two- cycle instruction.

But aren't the upper Bits of PCLATH too much? I mean there are only 4 Pages, each 256B, hence only bit 0 and 1 of PCLATH are needed. Or in other words - Bit 3 and 4 of PCLATH are always 0 ? Why would I then need to care about 'PCLATH' when performing a 'CALL' or 'GOTO' ?

2 Answers2

3

PIC16F84 has 13 bit program counter (PC). GOTO and CALL instructions have 11 bit address operands and the remaining 2 bits needs to come from somewhere, which is PCLATH<4:3>. As PIC16F84 has only 1K-word program memory, you don't need to care about PCLATH when using GOTO & CALL. Even having a non-zero random value won't affect the addressing, because datasheet states that:

Accessing a location above the physically implemented address will cause a wraparound.

Still, it's best to keep PCLATH<4:3> bits clean for future compatibility to other PIC models that have bigger flash memories.

So, is PCLATH completely irrelevant for PIC16F84? No. There is one more situation where PCLATH is used: Modifiying PCL, the lower 8 bits of the PC. When PCL is modified, the remaining 5 bits of the PC comes from PCLATH<4:0>. Modifiying PCL, mostly by adding some values to it, used for creating RETLW tables, which can be used to embed arrays of constant values into flash memory. So, it's good idea to always have a proper & valid value in PCLATH.

Tagli
  • 2,412
  • 2
  • 11
  • 14
  • 1
    Thanks a lot @Tagli , this solved the confusion.. So even when `PCL` is modified and the high bits of PC come from `PCLATH<4:0>` , the `PCLATH<4:3>` are actually zero and meant for future compatibility.. thats what confused us, that it has actually no impact due to it's program-memory size of 1K (Sure it's always better to have valid value in `PCLATH`) – totallynotatallno Feb 04 '22 at 15:48
1

The Program Counter is 13 bits. The operand for GOTO is 11 bits, so for the remaining bits you want the two bits of PCLATH starting at 11-sizeof(PCL), ie 3.

Here's a figure to illustrate this:

          12 11 10  9  8  7  6  5  4  3  2  1  0
-------------------------------------------------
|--|--|--|  |  |  |  |  |  |  |  |  |  |  |  |  |  PC (PCH:PCL)
-------------------------------------------------
          ^  ^  ^  ^  ^  ^  ^  ^  ^  ^  ^  ^  ^
          |  |  |  |  |  |  |  |  |  |  |  |  |  
          |  | 10  9  8  7  6  5  4  3  2  1  0
          |  | ---------------------------------- 
          |  | |  |  |  |  |  |  |  |  |  |  |  |  GOTO operand
          |  | ----------------------------------
 7  6  5  4  3  2  1  0
-------------------------  
|--|--|--|  |  |  |  |  |  PCLATH
-------------------------

PCH (the upper byte of PC) is not directly accessible. Instead, you write to it through PCLATH. Hence why bit 0 of PCLATH lines up with bit 8 of PC in my diagram.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • we have a Assembly code given which states: `movlw 1` `movwf pclath` So `PCLATH` is now: 00001 -> Bit 3 & 4 are 0 but bit 0 is 1 There I don't see where PCLATH 3&4 are set to any value other than 0 – totallynotatallno Feb 04 '22 at 11:21
  • Thanks @Michael ! concerning your figure: The GOTO Operand bit 8-10 overwrites then bit 0-2 of PCLATH, so Bit 3&4 are for page switching? – totallynotatallno Feb 04 '22 at 11:27
  • I'm not familiar with the terminology used in PIC documentations, so I can't say whether "page switching" is the correct term. But the address will be formed by the 11-bit operand and bits 3&4 from `PCLATH`. – Michael Feb 04 '22 at 11:32
  • @totallynotatallno , `PCLATH<2:0>` bits are ignored by `GOTO` & `CALL` instructions. But the contents of `PCLATH` is not overwritten by these instructions and remains unmodified unless you manually change it. – Tagli Feb 04 '22 at 12:15
  • Your ASCII art diagram doesn't line up perfectly. The GOTO operand is one space left of where I think you want it. – Peter Cordes Feb 04 '22 at 13:23