Embedded Computer Systems Chapter 5 Program design and
Embedded Computer Systems Chapter 5: Program design and analysis Eng. Husam Y. Alzaq Islamic University of Gaza © 2010 Husam Alzaq Computers as Components 1
Program design and analysis z. Software components. z. Representations of programs. z. Assembly and linking. © 2010 Husam Alzaq Computers as Components 2
Software state machine z. State machine keeps internal state as a variable, changes state based on inputs. z. Uses: ycontrol-dominated code; yreactive systems. © 2010 Husam Alzaq Computers as Components 3
State machine example no seat/ buzzer off idle seat/timer on no seat/buzzer Belt/buzzer on belt/ buzzer off seated belt/- belted © 2010 Husam Alzaq Computers as Components no belt and no timer/- no belt/timer on 4
C implementation #define IDLE 0 #define SEATED 1 #define BELTED 2 #define BUZZER 3 switch (state) { case IDLE: if (seat) { state = SEATED; timer_on = TRUE; } break; case SEATED: if (belt) state = BELTED; else if (timer) state = BUZZER; break; … } © 2010 Husam Alzaq Computers as Components 5
Signal processing and circular buffer z. Commonly used in signal processing: ynew data constantly arrives; yeach datum has a limited lifetime t+1 d 2 d 3 d 4 d 5 d 6 d 7 z. Use a circular buffer to hold the data stream. © 2010 Husam Alzaq Computers as Components 6
Circular buffer x 1 x 2 x 3 t 1 x 4 t 2 x 5 x 6 t 3 Data stream x 1 x 5 x 2 x 6 x 3 x 7 x 4 Circular buffer © 2010 Husam Alzaq Computers as Components 7
Circular buffers z. Indexes locate currently used data, current input data: input use d 1 use d 5 d 2 input d 2 d 3 d 4 time t 1+1 time t 1 © 2010 Husam Alzaq Computers as Components 8
Circular buffer implementation: FIR filter int circ_buffer[N], circ_buffer_head = 0; int c[N]; /* coefficients */ … int ibuf, ic; for (f=0, ibuff=circ_buff_head, ic=0; ic<N; ibuff=(ibuff==N-1? 0: ibuff++), ic++) f = f + c[ic]*circ_buffer[ibuf]; © 2010 Husam Alzaq Computers as Components 9
Queues z. Elastic buffer: holds data that arrives irregularly. © 2010 Husam Alzaq Computers as Components 10
Buffer-based queues #define Q_SIZE 32 #define Q_MAX (Q_SIZE-1) int q[Q_MAX], head, tail; void initialize_queue() { head = tail = 0; } void enqueue(int val) { if (((tail+1)%Q_SIZE) == head) error(); q[tail]=val; if (tail == Q_MAX) tail = 0; else tail++; } int dequeue() { int returnval; if (head == tail) error(); returnval = q[head]; if (head == Q_MAX) head = 0; else head++; returnval; } © 2010 Husam Alzaq Computers as Components 11
Models of programs z. Source code is not a good representation for programs: yclumsy; yleaves much information implicit. z. Compilers derive intermediate representations to manipulate and optiize the program. © 2010 Husam Alzaq Computers as Components 12
Data flow graph z. DFG: data flow graph. z. Does not represent control. z. Models basic block: code with no entry or exit. z. Describes the minimal ordering requirements on operations. © 2010 Husam Alzaq Computers as Components 13
Single assignment form x = a + b; y = c - d; z = x * y; y = b + d; x = a + b; y = c - d; z = x * y; y 1 = b + d; original basic block single assignment form © 2010 Husam Alzaq Computers as Components 14
Data flow graph x = a + b; y = c - d; z = x * y; y 1 = b + d; a b c + y x single assignment form * z DFG © 2010 Husam Alzaq Computers as Components d + y 1 15
DFGs and partial orders a b c d + y x * + z y 1 Partial order: z a+b, c-d; b+d and x*y Can do pairs of operations in any order. © 2010 Husam Alzaq Computers as Components 16
Control-data flow graph z. CDFG: represents control and data. z. Uses data flow graphs as components. z. Two types of nodes: ydecision; ydata flow. © 2010 Husam Alzaq Computers as Components 17
Data flow node Encapsulates a data flow graph: x = a + b; y=c+d Write operations in basic block form for simplicity. © 2010 Husam Alzaq Computers as Components 18
Control cond F T v 1 v 4 value v 2 v 3 Equivalent forms © 2010 Husam Alzaq Computers as Components 19
CDFG example if (cond 1) bb 1(); else bb 2(); bb 3(); switch (test 1) { case c 1: bb 4(); break; case c 2: bb 5(); break; case c 3: bb 6(); break; } cond 1 T bb 1() F bb 2() bb 3() c 1 c 3 test 1 c 2 bb 4() © 2010 Husam Alzaq Computers as Components bb 5() bb 6() 20
for loop for (i=0; i<N; i++) loop_body(); i=0 for loop i<N i=0; while (i<N) { loop_body(); i++; } F T loop_body() equivalent © 2010 Husam Alzaq Computers as Components 21
Assembly and linking z. Last steps in compilation: HLL HLL compile link assembly executable © 2010 Husam Alzaq Computers as Components assemble link 22
Multiple-module programs z. Programs may be composed from several files. z. Addresses become more specific during processing: yrelative addresses are measured relative to the start of a module; yabsolute addresses are measured relative to the start of the CPU address space. © 2010 Husam Alzaq Computers as Components 23
Assemblers z. Major tasks: ygenerate binary for symbolic instructions; ytranslate labels into addresses; yhandle pseudo-ops (data, etc. ). z. Generally one-to-one translation. z. Assembly labels: label 1 ORG 100 ADR r 4, c © 2010 Husam Alzaq Computers as Components 24
Symbol table xx yy ADD r 0, r 1, r 2 ADD r 3, r 4, r 5 CMP r 0, r 3 SUB r 5, r 6, r 7 assembly code xx yy 0 x 8 0 x 10 symbol table © 2010 Husam Alzaq Computers as Components 25
Symbol table generation z. Use program location counter (PLC) to determine address of each location. z. Scan program, keeping count of PLC. z. Addresses are generated at assembly time, not execution time. © 2010 Husam Alzaq Computers as Components 26
Symbol table example xx yy ADD r 0, r 1, r 2 ADD r 3, r 4, r 5 CMP r 0, r 3 SUB r 5, r 6, r 7 xx yy 0 x 8 0 x 10 © 2010 Husam Alzaq Computers as Components 27
Two-pass assembly z. Pass 1: ygenerate symbol table z. Pass 2: ygenerate binary instructions © 2010 Husam Alzaq Computers as Components 28
Relative address generation z. Some label values may not be known at assembly time. z. Labels within the module may be kept in relative form. z. Must keep track of external labels---can’t generate full binary for instructions that use external labels. © 2010 Husam Alzaq Computers as Components 29
Pseudo-operations z. Pseudo-ops do not generate instructions: y. ORG sets program location. y. EQU generates symbol table entry without advancing PLC. y. Data statements define data blocks. © 2010 Husam Alzaq Computers as Components 30
Linking z. Combines several object modules into a single executable module. z. Jobs: yput modules in order; yresolve labels across modules. © 2010 Husam Alzaq Computers as Components 31
Externals and entry points entry point xxx yyy a ADD r 1, r 2, r 3 B a external reference %1 ADR r 4, yyy ADD r 3, r 4, r 5 © 2010 Husam Alzaq Computers as Components 32
Module ordering z. Code modules must be placed in absolute positions in the memory space. z. Load map or linker flags control the order of modules. module 1 module 2 module 3 © 2010 Husam Alzaq Computers as Components 33
Dynamic linking z. Some operating systems link modules dynamically at run time: yshares one copy of library among all executing programs; yallows programs to be updated with new versions of libraries. © 2010 Husam Alzaq Computers as Components 34
- Slides: 34