# Introduction

The Central Processing Unit is the part of the AT32UC3A chip which controls what the rest of the hardware is doing. It does this by following a program that is stored in its RAM or flash memory.

# Hardware view

The Mizar32's CPU is an Atmel AVR32A UC3A RISC processor running at 66MHz with
* 15 general-purpose 32-bit registers
* A 32-bit stack pointer, program counter, link register and status register
* 187 different instructions, most of which take one clock cycle to complete
* Exceptions and interrupts
* User and supervisor modes (not used in eLua)
* Debug mode
The processor implements a load-store architecture, where values are fetched from the memory into registers, worked on there and the results are then stored back into memory. It has 187 different instructions which are:
* MOVE instructions to copy values between the registers or load them with constant values
* Load/store instructions to copy 8-, 16-, 32- or 64-bit data between the registers and the main memory
* Load/store multiple instructions to copy several registers from/to main memory or to the stack at once
* Arithmetic instructions to add, subtract, negate, test and compare values in registers and maximum and minimum to find the highest or the lowest of the values in two registers
* Multiplication: 16x16-bit and 32x32-bit multiply giving 32- or 64-bit results
* DSP instructions: multiply-and-accumulate instructions and saturating or rounding arithmetic
* Logical operations: AND, OR, exclusive OR, one's complement
* Bit operations: Bitfield extraction and assignment, bit set/clear/test, bit reverse, swap bytes/halfwords, count leading zeros
* Shifts and rotates
* Branches and subroutine call and return
* System control instructions
Thanks to its 3-stage pipeline, the processor normally executes one instruction per clock cycle, giving a maximum of 66 million instructions per second.
The CPU is connected to a High Speed Bus Matrix which, in turn communicates with the RAM and Flash memories, the USB and Ethernet hardware and the HSB bridge. The HSB bridge then talks to the rest of the peripheral devices on the chip at a lower speed of 16.5 MHz.

# Software view

The program that the CPU executes is usually the eLua interpreter, which either responds to commands that you type on the Mizar32's console, executes them and prints the results, or reads a Lua program from the SD card and does what the program says.
In eLua there are some functions to gain access to low-level features of the CPU:
* cpu.clock returns the CPU's clock frequency (66,000,000Hz)
* cpu.r32/w32/r16/w16/r8/w8 to read/write 32-, 16- and 8-bit data from/to the memory or the registers of peripheral devices
* cpu.sei/cli to enable/disable interrupts
* cpu.set_int_handler/get_int_handler/get_int_flag to manage how interrupts are handled
For examples of how to use interrupts from Lua, see the examples in the PIO, Timers and UART sections.
In PicoLisp,
* (cpu-clock) returns the CPU's clock frequency (66,000,000Hz)
* (cpu-r32)/(cpu-w32), (cpu-r16)/(cpu-w16), (cpu-r8)/(cpu-w8) to read/write 32-, 16- and 8-bit data from/to the memory or the registers of peripheral devices.
Please note: There is currently no support for interrupt handling in PicoLisp. See issue #12.

# Further reading

* The Atmel AT32UC3A datasheet Chapter 9: Processor and Architecture
* The AVR32 Architecture Manual for a detailed description of the instructions
* The eLua's cpu module reference manual