Computer Archtecture Lecture Notes PDF
Computer Archtecture Lecture Notes PDF
COMPUTER ARCHITECTURE
(MICROCONTROLLERS)
Prepared by
Dr. Abdulrahman Ikram Siddiq
2015
1
Contents
Topic Page
1 Background 3
2 Hardware Components 4
3 System Busses 4
4 Memory 5
5 Memory Organization 6
6 Memory Classification 7
7 I/O Device Interfacing 22
8 C and General Purpose P 23
9 AVR Microcontrollers 24
10 Device General Architecture 26
11 Development Boards 28
12 ATMEGA328 28
13 AVR CPU Core 32
14 Instruction Execution Timing 38
15 AVR Memories 41
16 I/O Ports 45
17 Reset and Interrupt Handling 51
18 External Interrupts 54
19 Analog Comparator 55
20 Analog-to-Digital Convertors 55
References:
[2] Muhammad Ali Mazidi, The AVR Microcontroller and Embedded System using
assembly and C, Prentice Hall, 2011.
2
Background
3
HARDWARE COMPONENTS
SYSTEM BUSES
The hardware of a computer system consists of components connected by wire lines called
busses. There are three types of busses, namely the data bus, address bus, and control bus.
Control Bus
Input Output
Memory
devices devices
Processor
Address Bus
Data Bus
4
Data bus: is a set of bi-directional lines used to transfer data between different hardware
components. The number of lines of a data bus depends on the CPU data word size (8 bits,
16 bits, …).
Address bus: is a set of uni-directional lines used by the processor to transfer the address
information to determine a memory location or an I/O device. The number of address lines
depends on the capability of the processor. An n-bit address bus 2n different addresses.
Foe 16-bit address bus, the number of addressable locations is Kbytes.
Control bus: a number of wires used by the processor to control the operation of the other
components of the system, such as memory read, memory write, I/O read, I/O write, … .
MEMORY
A memory chip consists of a large number of memory locations. Each location stores binary
data. The size of a memory location is typically 8 bits (1 Byte). The processor identifies a
location by its address.
Chip Select
Mem. Read 0 01001111
Mem. Write 1 00110011
2 10101010
3 00111100 Memory
Addresses
4 01001101 Locations
5 10011000
6 00011100
ADDRESS
7 10111000
DATA
5
6
Memory Classification
7
Draw it on the board
8
9
10
11
12
13
14
15
16
17
18
19
20
21
I/O Port Interfacing
The data flow between the CPU and the I/O devices is performed through connection
points called ports.
The ports are under the control of the CPU.
The CPU identifies (activates) an input or output device by placing its address on the
address bus and enabling the associated port. Then, the data flows through this port.
For example, in the figures below, the address of the output device is 0xFF00 and the
input device is 0xF000.
CLASS WORK: Design, draw and specify the addresses of a microcomputer system with the
following components:
CPU (8-bit data bus and 16-bit address bus)
1 KB ROM and 4 KB RAM
One input port and one output port (the ports are memory mapped)
22
23
The AVR is a modified Harvard architecture 8-bit RISC single-chip microcontroller,
which was developed by Atmel in 1996.
The AVR was one of the first microcontroller families to use on-chip flash memory for
program storage, as opposed to one-time programmable ROM, EPROM, or EEPROM
used by other microcontrollers.
The AVR architecture was conceived by two students at the Norwegian Institute of
Technology (NTH), Alf-Egil Bogen and Vegard Wollan.
When the technology was sold to Atmel from Nordic VLSI, the internal architecture
was further developed by Bogen and Wollan at Atmel Norway, a subsidiary of Atmel.
It is commonly accepted that AVR stands for Alf and Vegard RISC processor.
Among the first of the AVR line was the AT90S8515, which is a 40-pin DIP package
has the same pinout as an 8051 microcontroller, including the external multiplexed
address and data bus.
The AVR 8-bit microcontroller architecture was introduced in 1997. By 2003, Atmel
had shipped 500 million AVR flash microcontrollers.
BASIC FAMILIES
24
megaAVR — the ATmega series
Application-specific AVR
o megaAVRs with special features not found on the other members of the AVR
family, such as LCD controller, USB controller, advanced PWM, CAN, etc.
32-bit AVRs
In 2006 Atmel released microcontrollers based on the 32-bit AVR32 architecture. They
include SIMD and DSP instructions, along with other audio- and video-processing
features. This 32-bit family of devices is intended to compete with the ARM-based
processors. The instruction set is similar to other RISC cores, but it is not compatible
with the original AVR or any of the various ARM cores.
25
DEVICE GENERAL ARCHITECTURE
The AVR is a modified Harvard architecture machine, where program and data are
stored in separate physical memory systems that appear in different address spaces,
but having the ability to read data items from program memory using special
instructions.
Harvard Architecture
26
Harvard Architecture Advantages
Flash, EEPROM, and SRAM are all integrated onto a single chip, removing the need
for external memory in most applications. Some devices have a parallel external bus
option to allow adding additional data memory or memory-mapped devices. Almost
all devices (except the smallest TinyAVR chips) have serial interfaces, which can be
used to connect larger serial EEPROMs or flash chips.
Program memory
Program instructions are stored in non-volatile flash memory. Although the MCUs are
8-bit, each instruction takes one or two 16-bit words.
The size of the program memory is usually indicated in the naming of the device itself
(e.g., the ATmega64x line has 64 KB of flash, while the ATmega32x line has 32 KB).
There is no provision for off-chip program memory; all code executed by the AVR
core must reside in the on-chip flash. However, this limitation does not apply to the
AT94 FPSLIC AVR/FPGA chips.
The data address space consists of the register file, I/O registers, and SRAM.
27
DEVELOPMENT BOARDS
Compatibility between chips in each family is fairly good, although I/O controller
features may vary.
The ATmega1280 and ATmega2560, with more pinout and memory capabilities, have also been employed
to develop the Arduino Mega platform.
Arduino boards can be used with conventional programming environments (C, assembler, etc.).
USB-based AVRs have been used in the Microsoft Xbox hand controllers. The link between the controllers
and Xbox is USB.
28
High Endurance Non-volatile Memory Segments
4/8/16/32KBytes of In-System Self-Programmable Flash program memory
256/512/512/1KBytes EEPROM
512/1K/1K/2KBytes Internal SRAM
Data retention: 20 years at 85oC/100 years at 25oC
Optional Boot Code Section with Independent Lock Bits
In-System Programming by On-chip Boot Program
True Read-While-Write Operation
Programming Lock for Software Security
Peripheral Features
Two 8-bit Timer/Counters with Separate Prescaler and Compare Mode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and
Capture Mode
Real Time Counter with Separate Oscillator
Six PWM Channels
6-channel 10-bit ADC
Programmable Serial USART
Master/Slave SPI Serial Interface
Byte-oriented 2-wire Serial Interface (Philips I2C compatible)
Programmable Watchdog Timer with Separate On-chip Oscillator
On-chip Analog Comparator
Interrupt and Wake-up on Pin Change
29
PIN CONFIGURATIONS
The Pinout of ATmega48A/PA/88A/PA/168A/PA/328/P is shown below:
30
PIN DESCRIPTIONS
1. VCC: Digital supply voltage.
2. GND: Ground.
3. Port B (PB7:0) XTAL1/XTAL2/TOSC1/TOSC2
Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for
each bit).
The Port B output buffers have symmetrical drive characteristics with both high sink
and source capability.
As inputs, Port B pins that are externally pulled low will source current if the pull-up
resistors are activated.
The Port B pins are tristated when a reset condition becomes active, even if the clock
is not running.
4.Port C (PC5:0)
Port C is a 7-bit bi-directional I/O port with internal pull-up resistors (selected for
each bit).
The PC5...0 output buffers have symmetrical drive characteristics with both high sink
and source capability.
As inputs, Port C pins that are externally pulled low will source current if the pull-up
resistors are activated.
The Port C pins are tristated when a reset condition becomes active, even if the clock
is not running.
5. PC6/RESET
If the RSTDISBL Fuse is programmed, PC6 is used as an I/O pin.
Note that the electrical characteristics of PC6 differ from those of the other pins of
Port C.
If the RSTDISBL Fuse is unprogrammed, PC6 is used as a Reset input. A low level on
this pin for longer than the minimum pulse length will generate a Reset, even if the
clock is not running.
6. Port D (PD7:0)
Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for
each bit).
The Port D output buffers have symmetrical drive characteristics with both high sink
and source capability.
As inputs, Port D pins that are externally pulled low will source current if the pull-up
resistors are activated.
31
The Port D pins are tristated when a reset condition becomes active, even if the clock
is not running. PC 0:5 ADC(0:5)
7. AVCC is the supply voltage pin for the A/D Converter, PC3:0, and ADC7:6. It should be
externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be
connected to VCC through a low-pass filter.
8. AREF is the analog reference pin for the A/D Converter.
32
In order to maximize performance and parallelism, the AVR uses a Harvard
architecture – with separate memories and buses for program and data.
Instructions in the program memory are executed with a single level pipelining. While
one instruction is being executed, the next instruction is pre-fetched from the
program memory. This concept enables instructions to be executed in every clock
cycle.
The program memory is In-System Reprogrammable Flash memory.
The fast-access Register File contains 32 x 8-bit general purpose working registers
with a single clock cycle access time. This allows single-cycle Arithmetic Logic Unit
(ALU) operation.
In a typical ALU operation, two operands are output from the Register File, the
operation is executed, and the result is stored back in the Register File – in one
clock cycle.
Six of the 32 registers can be used as three 16-bit indirect address register pointers
for Data Space addressing – enabling efficient address calculations. One of these
address pointers can also be used as an address pointer for look up tables in Flash
program memory. These added function registers are the 16-bit X-, Y-, and Z-
register, described later in this section.
The ALU supports arithmetic and logic operations between registers or between a
constant and a register. Single register operations can also be executed in the ALU.
Most AVR instructions have a single 16-bit word format. Every program memory
address contains a 16- or 32-bit instruction.
Program Flash memory space is divided in two sections, the Boot Program section
and the Application Program section.
Both sections have dedicated Lock bits for write and read/write protection. The SPM
instruction that writes into the Application Flash memory section must reside in the
Boot Program section.
Stack is effectively allocated in the general data SRAM, and consequently the Stack
size is only limited by the total SRAM size and the usage of the SRAM. All user
programs must initialize the SP in the Reset routine (before subroutines or interrupts
are executed). The Stack Pointer (SP) is read/write accessible in the I/O space.
The data SRAM can easily be accessed through the five different addressing modes
supported in the AVR architecture.
33
A flexible interrupt module has its control registers in the I/O space with an additional
Global Interrupt Enable bit in the Status Register. All interrupts have a separate
Interrupt Vector in the Interrupt Vector table.
The interrupts have priority in accordance with their Interrupt Vector position. The
lower the Interrupt Vector address, the higher the priority.
The I/O memory space contains 64 addresses for CPU peripheral functions as
Control Registers, SPI, and other I/O functions. The I/O Memory can be accessed
directly, or as the Data Space locations following those of the Register File, 0x20 -
0x5F. In addition, the ATmega48A/PA/88A/PA/168A/PA/328/P has Extended I/O
space from 0x60 - 0xFF in SRAM where only the ST/STS/STD and LD/LDS/LDD
instructions can be used.
Status Register
The Status Register contains information about the result of the most recently
executed arithmetic instruction.
This information can be used for altering program flow in order to perform conditional
operations.
Note that the Status Register is updated after all ALU operations, as specified in the
Instruction Set Reference.
34
If the Global Interrupt Enable Register is cleared (=0), none of the interrupts are
enabled independent of the individual interrupt enable settings. The I-bit is cleared
by hardware after an interrupt has occurred, and is set by the RETI instruction to
enable subsequent interrupts.
The I-bit can also be set and cleared by the application with the SEI and CLI
instructions, as described in the instruction set reference.
35
General Purpose Register File
The Register File is optimized for the AVR Enhanced RISC instruction set. In order to
achieve the required performance and flexibility, the following input/output schemes are
supported by the Register File:
One 8-bit output operand and one 8-bit result input
Two 8-bit output operands and one 8-bit result input
Two 8-bit output operands and one 16-bit result input
One 16-bit output operand and one 16-bit result input
Most of the instructions operating on the Register File have direct access to all
registers, and most of them are single cycle instructions.
Each register is also assigned a data memory address, mapping them directly
into the first 32 locations of the user Data Space.
Although not being physically implemented as SRAM locations, this memory
organization provides great flexibility in access of the registers, as the X-, Y-
and Z-pointer registers can be set to index any register in the file.
36
The X-register, Y-register, and Z-register
The registers R26...R31 have some added functions to their general purpose
usage. These registers are 16-bit address pointers for indirect addressing of
the data space.
The three indirect address registers X, Y, and Z are defined as shown below.
In the different addressing modes these address registers have functions as
fixed displacement, automatic increment, and automatic decrement.
Stack Pointer
The Stack is mainly used for storing temporary data, for storing local variables
and for storing return addresses after interrupts and subroutine calls.
Note that the Stack is implemented as growing from higher to lower memory
locations. The Stack Pointer Register always points to the top of the Stack.
The Stack Pointer points to the data SRAM Stack area where the Subroutine
and Interrupt Stacks are located.
A Stack PUSH command will decrease the Stack Pointer.
The Stack in the data SRAM must be defined by the program before any
subroutine calls are executed or interrupts are enabled.
Initial Stack Pointer value equals the last address of the internal SRAM and
the Stack Pointer must be set to point above start of the SRAM.
The AVR Stack Pointer is implemented as two 8-bit registers in the I/O space.
The number of bits actually used is implementation dependent.
37
Note that the data space in some implementations of the AVR architecture is
so small that only SPL is needed. In this case, the SPH Register will not be
present.
38
Let’s consider the case of how the ADD instruction is represented as a machine-
executable instruction
39
40
The figure below shows the parallel instruction fetches and instruction
executions enabled by the Harvard architecture and the fast-access Register
File concept. This is the basic pipelining concept to obtain up to 1 MIPS per
MHz with the corresponding unique results for functions per cost, functions per
clocks, and functions per power-unit.
The figure below shows the internal timing concept for the Register File. In a
single clock cycle an ALU operation using two register operands is executed,
and the result is stored back to the destination register.
AVR Memories
The AVR architecture has two main memory spaces, the Data Memory and the
Program Memory space.
In addition, the ATmega48A/PA/88A/PA/168A/PA/328/P features an EEPROM
Memory for data storage.
All three memory spaces are linear and regular.
41
1. In-System Reprogrammable Flash Program Memory
The following figure shows the Program Memory Map ATmega88A, ATmega88PA,
ATmega168A, ATmega168PA, ATmega328 and ATmega328P
42
2. SRAM Data Memory
43
The internal data SRAM access is performed in two clkCPU cycles as described
in the figure below:
44
I/O Ports
All AVR ports have true Read-Modify-Write functionality when used as general
digital I/O ports.
This means that the direction of one port pin can be changed without
unintentionally changing the direction of any other pin with the SBI and CBI
instructions. The same applies when changing drive value (if configured as
output) or enabling/disabling of pull-up resistors (if configured as input).
Each output buffer has symmetrical drive characteristics with both high sink
and source capability. The pin driver is strong enough to drive LED displays
directly.
All port pins have individually selectable pull-up resistors with a supply-voltage
invariant resistance.
All I/O pins have protection diodes to both VCC and Ground as indicated in the
figure below.
45
A lower case “x” represents the numbering letter for the port, and a lower case
“n” represents the bit number. However, when using the register or bit defines
in a program, the precise form must be used. For example, PORTB3 for bit no.
3 in Port B, here documented generally as PORTxn.
Three I/O memory address locations are allocated for each port,
Data Direction Register – DDRx, and
Data Register – PORTx,
Port Input Pins – PINx.
46
47
48
0
49
50
00
The AVR provides several different interrupt sources. These interrupts and the
separate Reset Vector each have a separate program vector in the program
memory space.
All interrupts are assigned individual enable bits which must be written logic
one together with the Global Interrupt Enable bit in the Status Register in order
to enable the interrupt.
Depending on the Program Counter value, interrupts may be automatically
disabled when Boot Lock bits BLB02 or BLB12 are programmed. This feature
improves software security.
The lowest addresses in the program memory space are by default defined as
the Reset and Interrupt Vectors.
The complete list of vectors is shown below. The list also determines the
priority levels of the different interrupts.
51
Interrupt Vectors in ATmega328 and ATmega328P
The lower the address the higher is the priority level. RESET has the highest
priority, and next is INT0 – the External Interrupt Request 0. The Interrupt
Vectors can be moved to the start of the Boot Flash section by setting the
IVSEL bit in the MCU Control Register (MCUCR).
When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all
interrupts are disabled. The user software can write logic one to the I-bit to
enable nested interrupts.
52
All enabled interrupts can then interrupt the current interrupt routine. The I-bit
is automatically set when a Return from Interrupt instruction – RETI – is
executed.
There are basically two types of interrupts. The first type is triggered by an
event that sets the Interrupt Flag. For these interrupts, the Program Counter is
vectored to the actual Interrupt Vector in order to execute the interrupt
handling routine, and hardware clears the corresponding Interrupt Flag.
Interrupt Flags can also be cleared by writing a logic one to the flag bit
position(s) to be cleared. If an interrupt condition occurs while the
corresponding interrupt enable bit is cleared, the Interrupt Flag will be set and
remembered until the interrupt is enabled, or the flag is cleared by software.
Similarly, if one or more interrupt conditions occur while the Global Interrupt
Enable bit is cleared, the corresponding Interrupt Flag(s) will be set and
remembered until the Global Interrupt Enable bit is set, and will then be
executed by order of priority.
The second type of interrupts will trigger as long as the interrupt condition is
present. These interrupts do not necessarily have Interrupt Flags. If the
interrupt condition disappears before the interrupt is enabled, the interrupt will
not be triggered.
When the AVR exits from an interrupt, it will always return to the main program
and execute one more instruction before any pending interrupt is served.
Note that the Status Register is not automatically stored when entering an
interrupt routine, nor restored when returning from an interrupt routine. This
must be handled by software.
When using the CLI instruction to disable interrupts, the interrupts will be
immediately disabled. No interrupt will be executed after the CLI instruction,
even if it occurs simultaneously with the CLI instruction.
The following example shows how this can be used to avoid interrupts during
the timed EEPROM write sequence.
C Code Example
char cSREG;
cSREG = SREG; /* store SREG value */
/* disable interrupts during timed sequence */
_CLI();
EECR |= (1<<EEMPE); /* start EEPROM write */
EECR |= (1<<EEPE);
SREG = cSREG; /* restore SREG value (I-bit) */
53
When using the SEI instruction to enable interrupts, the instruction following
SEI will be executed before any pending interrupts, as shown in this example.
C Code Example
__enable_interrupt(); /* set Global Interrupt Enable */
__sleep(); /* enter sleep, waiting for interrupt */
/* note: will enter sleep before any pending interrupt(s) */
External Interrupts
The External Interrupts are triggered by the INT0 and INT1 pins or any of the
PCINT23...0 pins.
Observe that, if enabled, the interrupts will trigger even if the INT0 and INT1 or
PCINT23...0 pins are configured as outputs. This feature provides a way of
generating a software interrupt.
The pin change interrupt PCI2 will trigger if any enabled PCINT[23:16] pin
toggles.
The pin change interrupt PCI1 will trigger if any enabled PCINT[14:8] pin
toggles.
The pin change interrupt PCI0 will trigger if any enabled PCINT[7:0] pin
toggles.
54
The PCMSK2, PCMSK1 and PCMSK0 Registers control which pins contribute
to the pin change interrupts.
Pin change interrupts on PCINT23...0 are detected asynchronously. This
implies that these interrupts can be used for waking the part also from sleep
modes other than idle mode.
The External Interrupts can be triggered by a falling or rising edge or a low
level. This is set up as indicated in the specification for the External Interrupt
Control Registers – EICRA (INT2:0).
When the external interrupt is enabled and is configured as level triggered, the
interrupt will trigger as long as the pin is held low.
Low level interrupts and the edge interrupt on INT2:0 are detected
asynchronously. This implies that these interrupts can be used for waking the
part also from sleep modes other than idle mode. The I/O clock is halted in all
sleep modes except idle mode.
Analog Comparator
The Analog Comparator compares the input values on the positive pin AIN0
and negative pin AIN1.
When the voltage on the positive pin AIN0 is higher than the voltage on the
negative pin AIN1, the Analog Comparator output, ACO, is set (=1).
Analog-to-Digital Converter
Features:
• 10-bit Resolution
• 13 - 260μs Conversion Time
• 6 Multiplexed Single Ended Input Channels from the pins of Port A.
• 0 - VCC ADC Input Voltage Range
• Interrupt on ADC Conversion Complete
• Sleep Mode Noise Canceler
55
Example
More instructions