Professional Documents
Culture Documents
Embedded Systems Engineering: R.A. Prabhath Buddhika
Embedded Systems Engineering: R.A. Prabhath Buddhika
Embedded Systems Engineering: R.A. Prabhath Buddhika
Engineering
R.A. Prabhath Buddhika
B.Sc.Eng(Hons)(Moratuwa)
Department of Electronic & Computer Engineering
Introduction
Lecture 01
Registers –
Storage locations within the CPU. Faster than memory, used
to hold data and/or memory addresses during
execution of an instruction. “Accumulates” results of ALU
Sequence Controller –
State Machine that decodes program instructions and controls
the execution of the instructions. Also allocates HW resources
within the CPU
ROM - “Read Only Memory”, Non-volatile, keeps data when power is removed.
Good for program instructions
ex) disk drives, CD’s, Flash
RAM - “Random Access Memory”, Volatile, looses data when power is removed.
Faster than ROM, can withstand more access cycles during life-cycle, cheaper
ex) DRAM or CACHE in your PC
- all computer need input and output to make their function useful to the real world
- the input into the computer will change how the program is executed
ex)
Human I/O : Keyboard, Mouse, Monitor, Speakers
Electronic I/O : A/D, DAC, control signals from other electronics, sensors
I/O
Terminology (Input/Output, Keyboard, Display)
Machine Code - The binary #’s representing the program (ex, A+B = 0001 1011)
Absolute Assembler - source code dictates the exact location of programs, all code
memory configuration.
Compiler - Program that translates high-level source code into Machine Instructions
ROM
CPU
- ALU
- Control Unit RAM
- Registers
I/O
(Ports, IRQ’s, Timers, A/D)
Index Registers - 16-bit registers used mainly for addressing operations. The HCS12 has two 16-bit
index registers called X and Y.
Stack Pointer - 16-bit register that points to the STACK. The STACK is a 1 st in, last out structure
(FILO).
The Stack Pointer (SP) points to the address above the TOP element in the Stack.
Program Counter - 16-bit register that holds the location of the next instruction to be executed.
15 IN D E X R E G IS T E R X 0
Programmer’s CPU Model
15 IN D E X R E G IS T E R Y 0
15 S T A C K P O IN T E R 0
15 PROGRAM COUNTER 0
C O N D IT IO N C O D E
S X H I N Z V C
R E G IS T E R
$0000
$0001
:
$FFFE
$FFFF
8-bits
# of Address Lines - 2(# of address lines) = the number of locations we can access with that many lines.
- The HCS12 has 16 address lines : 2 (16)=65,536 addressable memory locations
- 16 address lines can be represented with 4 HEX digits
- The HCS12 has an address range from $0000 to $FFFF (standard notation)
- Each memory location holds 8-bits of data (1 byte), or $00 to $FF (standard notation)
ex) when we say we can access a “64k” byte memory, actually (64)(2 10) = 65,536 bytes
Memory
- responsibility of the programmer to “know the map”
Freescale
Semiconductor, Inc.
MC9S12E-Family
Device User Guide
MC9S12E-Family
Device User Guide
NOTE: Part numbers getting confusing? Try to remember what a microcontroller is. There
is a CPU (with a part number), Memory Configuration (with a part number), and I/O
Configuration (with a part number). This creates a matrix of part numbers that an
engineer can choose from.
NOTE: the PC now points at $e002, which is the beginning of the next instruction
the beginning of the next instruction is an "Opcode"
1) Immediate
2) Direct
3) Extended
4) Indexed
5) Indexed-indirect
6) Inherent
7) Relative
Ex) LDAA #$FF ; this instruction will put %11111111 into accumulator A
Memory
Opcode Operand
$e000 $86
Machine
Code: $86 $FF $e001 $FF How it looks
- in memory
Ex) LDAA $00 ; this instruction will put the contents of memory
; location $00 into ACCA. (ACCA = M($00)) Memory
Ex) LDAA $1234; this instruction will put the contents of memory
; location $1234 into ACCA. (ACCA = M($1234))
Memory
Opcode Operand
$1234 $34
Machine ACCA = $34
Code: $B6 $12, $34 $e000 $b6 after this
instruction is
$e001 $12 executed
The Control Unit knows that $B6 means $e002 $34
Extended Addressing. This instruction uses 3 bytes
EXT and DIR work the same way, why ever use DIR?
Ex) LDX #$0800 ; initializes the X register to $0800 using Immediate Addressing
LDAA 1,X ; loads A with the contents of X, plus an offset of ‘1’
Memory
Opcode Operand
$0801 $56
- ACCA = $56
Machine
Code: $A6 $01 $e000 $A6 after this
instruction is
The Effective address is $0801. $e001 $01 executed
ACCA is loaded with ($0801).
$e002 -
-- Embedded Software Design -- 34
Addressing Modes
Example –
What is in the CPU registers after the following code segment is executed?
What addressing mode is used?
What are the effective addresses for the LOADS? Memory
$0800 $AB
Addressing Effective
Mode Address $0801 $CD
LDX #$0800 IMM n/a
$0802 $EF
LDY #$0801 IMM n/a
LDAA 1,X INDX $0801
LDAB 1,Y INDY $0802
-
We learned Indexed addressing uses an Index Register and an Offset to calculate the
Effective Address (EA).
This gives a method for the program to alter the Effective Address during execution.
Good for accessing pages/arrays of data in memory.
"Automatic Pre-decrement"
LDAA n, -X ; n subtracted from X prior to data fetch
"Automatic Pre-increment"
LDAA n, +X ; n added to X prior to data fetch
"Automatic Post-decrement"
LDAA n, X- ; n subtracted from X after data fetch
"Automatic Post-increment"
LDAA n, X+ ; n added to X after data fetch
- The second thing it does is go the address and manipulate the data
$0800 $AA
-
$e010 $08 $e010:$e011 holds
After instruction ACCA = $AA $e011 $00 the EA to load data
from
Ex)
ABA ; adds ACCA with ACCB and puts the contents in ACCA .(A =
A+B)
INX ; increment register X
Opcode Operand
A = $11
B = $22 $0800 $11
X = $0800 $0801 $22
Y = $0802 After Execution
$0802 $11
What Addressing Modes were used?
$0803 $22
IMM, IND
-- Embedded Software Design -- 47
LOADs and STOREs
So far we have the following instructions:
LOAD STORE
LDAA STAA
LDAB STAB
LDD STD
LDS STS
LDX STX
LDY STY
LEAS (EA) → SP
LEAX (EA) → X
LEAY (EA) → Y
1) Instruction Mnemonics
2) Memory Location for instructions
3) Memory Allocation for program data
4) Variable Names
5) Global Constants
The assembler reads the source text file and decodes the text first depending on
which column the text is in. There are four columns that are called “assembler
fields”
1) Label
2) Opcode
3) Operand
4) Comment
Text that is not proceeded by a white space character (i.e., the first character of a line)
is treated as a label. A label is a piece of text that gets assigned the memory address
of where it is located in the file. This is only used during assembly.
Operand
text that follows an Opcode is treated as an Operand (3 rd column)
LDAA#$ff
$86 $ff
-- Embedded Software Design -- 53
Assembler Fields
Comment
There are three ways to enter a comment
2) placing an asterisk (*) or semicolon (;) as the first character of a line makes it a comment
- Use comments in your code or the lab instructor CAN’T HELP YOU!!!!
Ex)
ROM SECTION
main: ldaa #$ff ; this code section will be inserted into memory by linker
bra main
RAM SECTION
variables….
NOTE: The assembler will insert $ff for CONST on the 2 nd pass through the source code.
SET- Similar to EQU but the program can alter the initial value later in program.
We can now use these memory locations as variables in the rest of our code.
We can now use these memory locations as variables in the rest of our code.
We can now use these memory locations as variables in the rest of our code.
NOTE: The END does not have anything to do with program flow, it only tells the
assembler when you are done writing your code.