Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:avr:architecture [2010/03/04 01:32] – Translated to English yllars | en:avr:architecture [2020/07/20 09:00] (current) – external edit 127.0.0.1 |
---|
====== Architecture ====== | ====== Architecture ====== |
| |
AVR has an internal 8-bit data bus, through which the data between arithmetic logic unit (ALU), status register (SREG), program counter (PC), random access memory (SRAM) and peripherals is moved. The program, an array of commands, that get executed in the ALU comes from an address in the flash memory, specified by the program counter. The ALU has 32 8-bit general purpose registers, which are used as operands when carrying out instructions. | AVR has an internal 8-bit data bus, through which the data is moved between the arithmetic logic unit (ALU), status register (SREG), program counter (PC), random access memory (SRAM) and peripherals. The program, an array of commands that is executed in the ALU, comes from an address in the flash memory, specified by the program counter. The ALU has 32 8-bit general purpose registers, which are used as operands when carrying out instructions. |
| |
[{{ :images:avr:avr_atmega128_block_diagram.png?580 |Block diagram of ATmega128}}] | [{{ :images:avr:avr_atmega128_block_diagram.png?580 |Block diagram of ATmega128}}] |
| |
===== Instruction pipeline ===== | ===== Instruction Pipeline ===== |
| |
AVR has a two-stage instruction pipeline. While one instruction is executed, the next is fetched from the program memory. That is the reason why carrying out jump instructions takes 2 cycles on fulfillment of the jump condition. Since the new instruction is always fetched from the next memory address, it is necessary to discard the previously fetched instruction and fetch a new one when jumping to another address, because it was fetched from an old, wrong location. | AVR has a two-stage instruction pipeline. While one instruction is executed, the next is fetched from the program memory. This is why carrying out jump instructions takes 2 cycles on fulfillment of the jump condition. Since the new instruction is always fetched from the next memory address, it is necessary to discard the previously fetched instruction and fetch a new one when jumping to another address, because it was taken from an old, wrong location. |
| |
===== General purpose registers ===== | ===== General Purpose Registers ===== |
| |
General purpose registers R0-R31 are like buffers for storing and operating with memory and peripheral data. They simplify the architecture of the processor, because they are quickly accessible by the ALU and the use of the data bus to read operands from the memory is not necessary for every operation. General purpose registers are used for performing all data-related arithmetical and logical operations. | General purpose registers R0-R31 are like buffers for storing and operating with memory and peripheral data. They simplify the architecture of the processor, because they are quickly accessible by the ALU, and the use of the data bus to read operands from the memory is not necessary for every operation. General purpose registers are used for performing all data-related arithmetical and logical operations. |
| |
When programming in assembler, it is possible to store the urgent data in general purpose registers. When programming in C and there happens to be a need to store a variable in a general purpose register, the variable is additionally defined as "register". | While programming in assembler, it is possible to store the urgent data in general purpose registers. While programming in C and a need to store a variable in a general purpose register arises, the variable is additionally defined as "register". |
For example: | For example: |
| |
</code> | </code> |
| |
===== Instruction set ===== | ===== Instruction Set ===== |
| |
The instruction set of most AVRs consists of 90-133 different instructions. ATmega128 has 133 instructions. Instructions have either one, two or no operands. Most instructions take only one cycle to complete, but the more complex ones can use up to 5 cycles. In XMega, the successor of AVR, several of the instructions have been modified to use less cycles. Most instructions in AVR are used for jumps, moving and comparing data and doing arithmetic calculations. A status register is used for performing calculations and comparisons. It stores the output status of the ALU - whether the result was negative, positive, zero, exceeded the maximum allowed value (8 bits), needed to transfer a bit to the next operation etc (there are a few more complex cases). | The instruction set of most AVRs consists of 90-133 different instructions. ATmega128 has 133 instructions. Instructions have either one, two or no operands. Most instructions take only one cycle to complete, but the more complex ones can use up to 5 cycles. For XMega, the successor of AVR, several instructions have been modified to use less cycles. Most instructions in AVR are used for jumps, moving and comparing data and executing arithmetic calculations. A status register is used for performing calculations and comparisons. It stores the output status of the ALU - whether the result was negative, positive, zero, exceeded the maximum allowed value (8 bits), needed to transfer a bit to the next operation etc (there are a few more complex cases). |
| |
<box 100% round #EEEEEE|Example> | <box 100% round #EEEEEE|Example> |
| |
This is a piece of code written in Assembler and consisting of pure instructions, that adds 5 to the byte at random access memory address $100 (decimal 256). These instructions exist in all AVRs. | This is a piece of code written in Assembler and consists of pure instructions, which adds 5 to the byte at random access memory address $100 (decimal 256). These instructions exist in all AVRs. |
| |
<code asm> | <code asm> |
</box> | </box> |
| |
===== Program stack ===== | ===== Program Stack ===== |
| |
Stack is a data structure, where the last data written to the memory is read out first. AVR's stack can be used when operating with subroutines, interrupts and temporary data. Before executing a subroutine or interrupt, the address in the program counter where the program was interrupted is stored in the stack. When the subroutine or interrupt has finished its execution, this address is read back from the stack and the program continues from the address it left off before. Storing temporary data in the stack is usually used when dealing with shorter chunks of code, which do not require reserved memory throughout the execution of the program. Simpler assembler programs are usually written so that it is not necessary to use the stack, but if the program contains a lot of variables and functions, the compilers automatically make use of it. | Stack is a data structure, where the last data written to the memory is read out first. AVR's stack can be used while operating with subroutines, interrupts and temporary data. Before executing a subroutine or interrupt, the address in the program counter where the program was interrupted is stored in the stack. When the subroutine or interrupt has finished its execution, this address is read back from the stack and the program continues from the address it left off from before. Storing temporary data in the stack is usually used when dealing with shorter chunks of code, which do not require reserved memory throughout the execution of the program. Simpler assembler programs are usually written so that it is not necessary to use the stack, but if the program contains a lot of variables and functions, the compilers automatically make use of it. |
| |
The stack of MegaAVR series microcontrollers is physically located in the random access memory, but some tinyAVR series devices do not have a random access memory at all and the stack is realized as a separate, quite limited memory unit. Typically there are no compilers for such devices, that have no random access memory. | The stack of MegaAVR series microcontrollers is physically located in the random access memory. Some tinyAVR series devices do not have a random access memory at all and the stack is realized as a separate, quite limited memory unit. Typically there are no compilers for devices with no random access memory. |
| |
To program in a high level language (Pascal, C, C++), it is not necessary to be familiar with the inner workings of the microcontroller, because the compiler is capable of selecting general purpose registers and instructions by itself, but knowing what goes on in the controller is certainly beneficial. It is necessary to know the instructions of the microcontroller when developing time-critical applications, where the operations have to be completed in a limited amount of cycles. | To program in a high level language (Pascal, C, C++), it is not necessary to be familiar with the inner workings of the microcontroller, because the compiler is capable of selecting general purpose registers and instructions by itself, but knowing what goes on in the controller is certainly beneficial. It is necessary to know the instructions of the microcontroller when developing time-critical applications, where the operations have to be completed in a limited amount of cycles. |