Escolar Documentos
Profissional Documentos
Cultura Documentos
Verónica Gaspes
www2.hh.se/staff/vero
November 6, 2008
The C preprocessor Hardware interfacing The C execution model
The preprocessor
Macro expansion
Textually replace definitions.
File insertion
Include files as if you had written the code in your files.
The preprocessor
Macro expansion
Textually replace definitions.
File insertion
Include files as if you had written the code in your files.
The preprocessor
Macro expansion
Textually replace definitions.
File insertion
Include files as if you had written the code in your files.
The preprocessor
Macro expansion
Textually replace definitions.
File insertion
Include files as if you had written the code in your files.
Macros
Macros
Including Files
#include "point.h"
#include <math.h>
Including Files
#include "point.h"
#include <math.h>
Including Files
#include "point.h"
#include <math.h>
Including Files
#include "point.h"
#include <math.h>
Including files
Including files
Including files
Including files
The program . . .
#include "debug.h" Is realy two programs, depending
#include <stdio.h> on the content of debug.h! If the
main(){ definition #define DEBUG is
#ifdef DEBUG there the preprocessor leaves the
printf("in debug mode"); debugging statement, otherwise it
#endif removes it (a smaller program
printf("what has to gets compiled)
be done ...");
}
The C preprocessor Hardware interfacing The C execution model
The program . . .
#include "debug.h" Is realy two programs, depending
#include <stdio.h> on the content of debug.h! If the
main(){ definition #define DEBUG is
#ifdef DEBUG there the preprocessor leaves the
printf("in debug mode"); debugging statement, otherwise it
#endif removes it (a smaller program
printf("what has to gets compiled)
be done ...");
}
The C preprocessor Hardware interfacing The C execution model
The program . . .
#include "debug.h" Is realy two programs, depending
#include <stdio.h> on the content of debug.h! If the
main(){ definition #define DEBUG is
#ifdef DEBUG there the preprocessor leaves the
printf("in debug mode"); debugging statement, otherwise it
#endif removes it (a smaller program
printf("what has to gets compiled)
be done ...");
}
The C preprocessor Hardware interfacing The C execution model
CPU
When
CPU ever
r the finds a the CPU
neve
Whe ds a read w
instru rite
n
fi ction ction
instru ite
write wr
re
ad
rea
d
RAM Port
Port
ser
eu
r th g Every ?
e n eve ethin
Wh s som seconds
e
typ
The C preprocessor Hardware interfacing The C execution model
CPU
When
CPU ever
r the finds a the CPU
neve
Whe ds a read w
instru rite
fin ction ction
instru ite
write wr
re
ad
rea
d
RAM Port
Port
The C preprocessor Hardware interfacing The C execution model
IO hardware
Memory mapped
Separate bus
Some addresses are reserved for
Different assembler instructions
device registers! Typically they
for memory access and for device
have names provided in some
registers.
platform specific header file.
The C preprocessor Hardware interfacing The C execution model
IO hardware
Memory mapped
Some addresses are reserved for
device registers! Typically they
have names provided in some Separate bus
platform specific header file. Different assembler instructions
for memory access and for device
registers.
The C preprocessor Hardware interfacing The C execution model
IO hardware
example
#define IS_READY (1 << 5)
#define CONVERT (1 << 5)
#define STATUS_REG *((char*)0x34c)
#define CMD_REG *((char*)0x34c)
Shadowing
Notice
All changes to CMD REG should be reflected in cmd shadow!
The C preprocessor Hardware interfacing The C execution model
Misc
Single write
It is not always needed to read the value of the port when doing a
modification. In some cases you know exactly what value should
be written to the port.
QNX real-time OS
Macros like in8, out8, in16, out16 that are used as in
I/O Synchronisation
CPU
How does the software
When
neve
r the
CPU ever
finds a the CPU become aware of
Whe ds a read w
instru rite
fin on
instr u c ti
write wr
ite
ction
changes in the key
re
ad
status?
rea
d
RAM Port
Port
the
user
Every ?
2 models
ver thing
ene e seconds
Wh s som
typ
e interrupt driven
(more on this later
in the course)
status driven (today
and lab1)
The C preprocessor Hardware interfacing The C execution model
Busy Waiting
In the status driven model the CPU polls the status registers until
a change occurs
Example
int old = KEY_STATUS_REG;
int val = old;
while(old==val){
val = KEY_STATUS_REG;
}
Busy Waiting
In the status driven model the CPU polls the status registers until
a change occurs
Example
int old = KEY_STATUS_REG;
int val = old;
while(old==val){
val = KEY_STATUS_REG;
}
Busy Waiting
In the status driven model the CPU polls the status registers until
a change occurs
Example
int old = KEY_STATUS_REG;
int val = old;
while(old==val){
val = KEY_STATUS_REG;
}
Busy Waiting
In the status driven model the CPU polls the status registers until
a change occurs
Example
int old = KEY_STATUS_REG;
int val = old;
while(old==val){
val = KEY_STATUS_REG;
}
Busy waiting
Why is it so appealing?
It can be used to define functions that make input look like reading
variables (reading from memory!)
char getchar(){
while(KEY_STATUS_REG & PRESSED);
while(!(KEY_STATUS_REG & PRESSED));
return KEY_VALUE_REG;
}
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
SP x =
PC
...
Globals
v = 0
w = 0
v
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
SP x =
PC
...
Globals
v = 0
w = 0
v
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
SP x = 9
x = 9;
PC
w = 6;
...
Globals
v = 0
w = 6
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
SP x = 9
x = 9;
w = 6;
PC b(55);
Globals
v = 0
w = 6
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
x = 9 b(int y){
Locals of b() x = 9;
w = 6; PC ...
SP y = 55 b(55);
}
Globals
v = 0
w = 6
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
x = 9 b(int y){
Locals of b() x = 9;
w = 6; PC
...
SP y = 55 b(55); y = c();
}
Globals
v = 0
w = 6
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
x = 9 b(int y){ c(){
int z = 23;
Locals of b() x = 9;
w = 6; ... ...
y = 55 b(55); y = c(); PC
} }
Locals of c()
SP z = 23
}
Globals
v = 0
w = 6
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
x = 9 b(int y){ c(){
int z = 23;
Locals of b() x = 9;
w = 6; ... w = 77;
y = 55 b(55); y = c(); ...
PC }
Locals of c() }
SP z = 23
}
Globals
v = 0
w = 77
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
x = 9 b(int y){ c(){
int z = 23;
Locals of b() x = 9;
w = 6; ... w = 77;
y = 55 b(55); y = c();
return z;
Locals of c() }
PC }
SP z = 23
}
Globals
v = 0
w = 77
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
x = 9 b(int y){ c(){
int z = 23;
Locals of b() x = 9;
w = 6; ... w = 77;
SP y = 23 b(55); y = c();
return z;
PC }
}
Globals
v = 0
w = 77
u = 0
The C preprocessor Hardware interfacing The C execution model
Execution of a C program
Stack Code
Locals of a() a(){
int x;
SP x = 9 b(int y){ c(){
int z = 23;
x = 9;
w = 6; ... w = 77;
PC b(55); y = c();
... return z;
}
}
Globals
v = 0
w = 77
u = 0
The C preprocessor Hardware interfacing The C execution model
2 CPUs?
Comming next
Lecture 3
Discuss an example from embedded systems programming.
Show a cyclic executive design
Motivate the need for concurrent programming.
Discuss the mutual exclusion problem.
Lecture 4
Show what is needed to implement threads (maybe also mutexes).