Core Architecture
The PIC architecture is distinctively minimalist. It is characterized by the following features:
- separate code and data spaces (Harvard architecture)
- a small number of fixed length instructions
- most instructions are single cycle execution (4 clock cycles), with single delay cycles upon branches and skips
- a single accumulator (W), the use of which (as source operand) is implied (ie is not encoded in the opcode)
- All RAM locations function as registers as both source and/or destination of math and other functions.
- a hardware stack for storing return addresses
- a fairly small amount of addressable data space (typically 256 bytes), extended through banking
- data space mapped CPU, port, and peripheral registers
- the program counter is also mapped into the data space and writable (this is used to implement indirect jumps)
Unlike most other CPUs, there is no distinction between "memory" and "register" space because the RAM serves the job of both memory and registers, and the RAM is usually just referred to as the register file or simply as the registers.
Data Space (RAM)
PICs have a set of registers that function as general purpose ram. Special purpose control registers for on-chip hardware resources are also mapped into the data space. The addressability of memory varies depending on device series, and all PIC devices have some banking mechanism to extend the addressing to additional memory. Later series of devices feature move instructions which can cover the whole addressable space, independent of the selected bank. In earlier devices (ie. the baseline and mid-range cores), any register move had to be achieved via the accumulator.
To implement indirect addressing, a "file select register" (FSR) and "indirect register" (INDF) are used: A read or write to or from INDF will actually be to or from the register pointed to by FSR. Later devices extended this concept with post- and pre- increment/decrement for greater efficiency in accessing sequentially stored data. This also allows FSR to be treated almost like a stack pointer.
External data memory is not directly addressable except in some high pin count PIC18 devices.
Code Space
All PICs feature Harvard architecture, so the code space and the data space are separate. PIC code space is generally implemented as EPROM, ROM, or flash ROM.
In general, external code memory is not directly addressable due to the lack of an external memory interface. The exceptions are PIC17 and select high pin count PIC18 devices.
Word Size
The word size of PICs can be a source of confusion. All PICs handle (and address) data in 8-bit chunks, so they should be called 8-bit microcontrollers. However, the unit of addressability of the code space is not generally the same as the data space. For example, PICs in the baseline and mid-range families have program memory addressable in the same wordsize as the instruction width, ie. 12 or 14 bits respectively. In contrast, in the PIC18 series, the program memory is addressed in 8-bit (bytes), which differs from the instruction width of 16 bits.
In order to be clear, the program memory capacity is usually stated in number of (single word) instructions, rather than in bytes.
Stacks
PICs have a hardware call stack, which is used to save return addresses. The hardware stack is not software accessible on earlier devices, but this changed with the 18 series devices.
Hardware support for a general purpose parameter stack was lacking in early series, but this greatly improved in the 18 series, making the 18 series architecture more friendly to high level language compilers.
Instruction Set
A PIC's instructions vary in number from about 35 instructions for the low-end PICs to about 70 instructions for the high-end PICs. The instruction set includes instructions to perform a variety of operations on registers directly, the accumulator and a literal constant or the accumulator and a register, as well as for conditional execution, and program branching.
Some operations, such as bit setting and testing, can be performed on any register, but bi-operand arithmetic operations always involve W -- writing the result back to either W or the other operand register. To load a constant, it is necessary to load it into W before it can be moved into another register. On the older cores, all register moves needed to pass through W, but this changed on the "high end" cores.
PIC cores have skip instructions which are used for conditional execution and branching. The skip instructions are: 'skip if bit set', and, 'skip if bit not set'. Because cores before PIC18 had only unconditional branch instructions, conditional jumps are implemented by a conditional skip (with the opposite condition) followed by an unconditional branch. Skips are also of utility for conditional execution of any immediate single following instruction.
The PIC architecture has no (or very meager) hardware support for automatically saving processor state when servicing interrupts. The 18 series improved this situation by implementing shadow registers which save several important registers during an interrupt.
Interrupt Latency
A very useful and unique property of PICs is that their interrupt latency is constant (it's also low: 3 instruction cycles). The delay is constant even though instructions can take one or two instruction cycles: a dead cycle is optionally inserted into the interrupt response sequence to make this true. External interrupts have to be synchronized with the four clock instruction cycle, otherwise there can be a one instruction cycle jitter. Internal interrupts are already synchronized.
The constant interrupt latency allows PICs to achieve interrupt driven low jitter timing sequences. An example of this is a video sync pulse generator. Other microcontrollers can do this in some cases, but it's awkward. The non-interrupt code has to anticipate the interrupt and enter into a sleep state before it arrives. On PICs, there is no need for this.
Programming
The PIC architectures have several limitations:
- Only a single accumulator
- A small instruction set
- Operations and registers are not orthogonal; some instructions can address RAM and/or immediate constants, while others can only use the accumulator
- Memory must be directly referenced in arithmetic and logic operations
- Register-bank switching is required to access the entire RAM of many devices, making position-independent code complex and inefficient
- Conditional skip instructions are used instead of conditional branch instructions (as most other architectures use)
The following limitations have been addressed in the PIC18, but still apply to earlier cores:
- There is no indexed addressing mode.
- Stack:
- The hardware call stack is so small that program structure must often be flattened
- The hardware call stack is not addressable, so pre-emptive task switching cannot be implemented
- A software-implemented stack cannot be implemented efficiently, so it is difficult to generate reentrant code and support local variables
- Program memory:
- Data is space inefficient and/or time consuming to access, as it is not directly addressable. This is true of most Harvard architecture microcontrollers.
With paged program memory, there are two page sizes to worry about: one for CALL and GOTO and another for computed GOTO (typically used for table lookups). For example, on PIC16, CALL and GOTO have 11 bits of addressing, so the page size is 2KB. For computed GOTOs, where you add to PCL, the page size is 256 bytes. In both cases, the upper address bits are provided by the PCLATH register. This register must be changed every time control transfers between pages. PCLATH must also be preserved by any interrupt handler.[5]
These properties have made it difficult to develop compilers that target PIC microcontrollers. While several commercial compilers are available, the Free software Small Device C Compiler has not yet completed support for PIC as of 2007. By contrast, Atmel's AVR microcontrollers—which are competitive with PIC in terms of hardware capabilities and price, but feature a RISC instruction set—have long been supported by the GNU C Compiler.
Also, because of these properties, PIC assembly language code can be difficult to comprehend. However, judicious use of simple macros can make PIC assembly language much more palatable, but at the cost of a reduction in performance. For example, the original Parallax PIC assembler "pasm" has macros which hide W and make the PIC look like a two-address machine. It has macro instructions like "mov b,a
" (move the data from address a to address b) and "add b,a
" (add data from address a to data in address b). It also hides the skip instructions by providing three operand branch macro instructions such as "cjne a,b,dest
" (compare a with b and jump to dest if they are not equal)
No comments:
Post a Comment