Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems:...

38
Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12

Transcript of Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems:...

Page 1: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Ch. 6 Pointers and Data Structures

From the text by Valvano: Introduction to Embedded Systems:

Interfacing to the Freescale 9S12

Page 2: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Objectives

• Implement pointers using indexed addressing modes

• Use pointers to access arrays, strings, structures, tables, and matrices

• Present finite-state machines as an abstractive design methodology

• Describe how the paged memory system allows memory sizes above 64KiB

• Present minimally intrusive methods for functional debugging

Page 3: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.1 Indexed Addressing Modes Used to Implement Pointers

• 9S12 has 15 addressing modes.

• 9 are related to pointers.

• Assembly—pointers are implemented using indexed addressing.

• The address is placed into RegisterX or RegisterY, then the data is accessed using indexed addressing mode.

Page 4: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Figure 6.2, page 193

• Pointers—PT, SP, GetPt, PutPt• Arrows show the memory location to

which they point.• Shaded regions represent data.• Array or string —simple structure with

multiple, equal-sized elements—the pointer points to the first element then the indexed addressing mode is used to access the rest of the structure.

Page 5: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Figure 6.2 (cont.)

• The Stack —SP points to the top element of the stack.

• Linked List – contains some data elements that are pointers.

• FIFO Queue – important for I/O programming.

Page 6: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.1.1 Indexed Addressing Mode

• Uses a fixed offset with the 16-bit registers (X, Y, SP, and PC).

• The offsets that are possible include:– 5-bits (-16 to +15)– 9-bits (-256 to + 127)– 16- bits

• Example of 5 bit indexing: (5C is index mode operand—1 byte)– $6A5C staa -4,Y ; [Y-4] = Reg A– Assume RegA is $56, RegY is $0823– $0823 -4 = $081F– See Figure 6.3

Page 7: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

More Examples of Indexed Addressing Mode

• Example of 9-bit indexed mode requires 2 bytes to encode the operand:– $6AE840 staa $40,Y ; [Y+$40] = Reg A– See Figure 6.4 (note: $40 + $8023 = $8063)

• Example of 16-bit indexed mode requires 3 bytes to encode the operand:– $6AEA2000 staa $200,Y ; [Y + $200]

=RegA– See Figure 6.5 (note: $200 + $0823=$0A23)

Page 8: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.1.2 Auto Pre/Post Decrement/Increment Indexed Addressing Mode

• The 16-bit pointers are used (X, Y, SP).• These pointers are modified either before

(PRE) or after (POST) the access of the memory.

• Used for accessing memory sequentially.• Example: Post-increment addressing.

– Staa 1,Y+ ; Store a copy of value in Reg A at 2345, then Reg Y = 2346.

– More examples on the bottom of page 195.

Page 9: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Other Addressing Modes

• 6.1.3 Accumulator-Offset Indexed Addressing mode—Accumulator A,B, or D are used with registers X, Y, SP, and PC.– Example: staa B,Y– Efficient for accessing arrays.

• 6.1.4 Indexed-Indirect Addressing Mode– A fixed offset of 16 bits is used (X, Y, SP or PC).– A second address is fetched; the load or store is

operated on the second address.– Useful when data structures include pointers (pg.

196).

Page 10: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

And More Addressing Modes

• 6.1.5 Accumulator D Offset Indexed-Indirect Addressing Mode.– Also useful when data structures includes pointers.– Offset is in D and is added to X, Y, SP, or PC.

• 6.1.6 Post-Byte machine Coded for Indexed Addressing

• 6.1.7 Load Effective Address Instructions• 6.1.8 Call-By-Reference Parameter Passing

Page 11: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.2 Arrays

• Random Access– Read and write data in any order.– Allowed for all indexed data structures.– An indexed data structure has elements of the

same size—it is accessed knowing the name of the structure, the size of the elements and the number of elements.

Page 12: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.2 (cont.)

• Sequential access– The elements are read and written in order.– Examples: strings, linked lists, stacks,

queues, and trees.

• Arrays– Made of elements of equal precision and

allows random access.– Length is the number of elements.– Origin is the index of the first element.

Page 13: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Example 6.1

• Write a stepper module to control the read/write head of an audio tape recorder.– Three public functions

• Initialization• Rotate one step clockwise• Rotate one step counterclockwise

– Stepper motor has four digital control lines• To make it spin, output 5, 6, 10, and 9 over and over.• To make it spin in the opposite direction, output the values in

reverse order.• Figure 6.9 illustrates the byte array.• Program 6.1 shows the code (fcb means form constant byte)

Page 14: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Example 6.2

• Design an exponential function y=10x, with a 16-bit output.– X is an input from 0 to 4.– Figure 6.10 illustrates a word array– Program 6.2 (page 201) illustrates the use of

the array (fdb means form double byte and is used to define word constants.)

Page 15: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Termination Codes

• Termination codes can be used to handle variable length.

• Table 6.3 illustrates typical termination codes.

• Program 6.3 illustrates the use of the null termination code.

• Program 6.4 illustrates a pointer method to access the array.

Page 16: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.3 Strings

• A data structure with equal-sized elements that are only accessed sequentially.

• The length of the string is stored in the first position, when data can take on any value; thus termination codes are not needed.

Page 17: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Example 6.3

• Example 6.3 (page 203)– Write software to output a sequence of

values to a digital to analog converter.– The length is stored in the first byte:

• Data1 fcb 4 ;length• fcb 0,50,100,50 ;data• Data2 fcb 8• fcb 0,25,50,75,100,75,50,25

Page 18: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Example 6.3 (cont.)

• The DAC is connected to Port T.• The function (subroutine) DAC outputs the string

data to the digital to analog converter.– DAC ldab 0,x ;length– loop inx ;next element– ldaa 0,x ;data– staa PTT ;out to the dig/analog conv– decb– bne loop– rts

Page 19: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Example 6.3 (cont.)

• main lds #$4000

• movb #$FF, DDRT

• mloop ldx #Data1 ;first string

• bsr DAC

• ldx #Data2 ;second string

• bra mloop

Page 20: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Example 6.4

• Write software to output a ASCII string to the serial port.

• Call by reference parameter passing is used—a pointer to the string will be passed.

• See Program 6.6, page 204.

Page 21: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.4 Matrices

• Two dimensional data structure accessed by rows and columns.

• Figure 6.11 illustrates “row major” and “column major”.

• Program 6.8 illustrates a function to access a two by three column-major matrix. (page 205)

Page 22: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Example 6.5

• Develop a set of driver functions to manipulate a 32 by 12 graphics display.

• Bit arrays are used to store pixel values.

• 0 represents a blank, 1 turns on a pixel.

• Program 6.11, 6.12 and 6.13 illustrate the access, modification, and reading of the bit matrix.

Page 23: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Structures

• A structure has elements with different types and/or precisions.

• Program 6.14 is an assembly language example of a structure.

Page 24: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Tables

• A table is a collection of identically sized structures.

• Fig. 6.14 shows a table made up of a simple data base.

• Program 6.16 (page 211)

Page 25: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Trees

• A graph is a general linked structure without limitations.

Page 26: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.8 Finite-State Machines with Statically Allocated Linked

Structures• 6.8.1 Abstraction

– Allows the definition of complex problems using basic abstract building blocks.

– Finite-State-Machine is a basic abstraction.

Page 27: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Finite State Machines

• Execution of a Moore FSM– 1. Perform output, which depends on the

current state.– 2. Wait a prescribed amount of time (optional)– 3. Input– 4. Go to next state, which depends on the

input and the current state.

Page 28: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Finite State Machines (cont.)

• Mealy FSM– 1. Wait a prescribed amount of time (optional)– 2. Input– 3. Perform output, which depends on the input

and the current state– 4. Go to next state, which depends on the

input and the current state

Page 29: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Finite State Machines (cont.)

• Sequence should be documented before state graph is drawn.

• States are drawn as circles

• Arrows are drawn from one state to another (with the input causing the transition).

Page 30: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

Linked-Structure of Nodes

• Each node defines one state.

• One or more of the entries in the node is a pointer (or link).

• Embedded systems usually have statically allocated fixed-size linked structures.

• In simple embedded systems, the state graph is fixed and stored in nonvolatile memory.

Page 31: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.8.2 Moore FSMs

• Outputs are only a function of current state.– Example 6.6 Traffic Light Problem

Page 32: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.
Page 33: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.8.3 Mealy Finite-State Machines

• Outputs depend on both the input and the current state.

• Arrows specify both output and next state.

• Used when the output is needed to cause the state to change.

• Example 6.7 Four-Legged Trotting Robot.

Page 34: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.8.4 Functional Abstraction Within FSMs

• Input is obtained by calling a function.

• Output process involves calling a function.

• Function calls adds a layer of abstraction between the FSM and the I/O at the ports.

• Example 6.8—Vending Machine

• Example 6.9—Robot that Sits, stands, and lies down.

Page 35: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.9 Dynamically Allocated Structures

• Dynamic Memory Allocation is used to reuse memory and provide for efficient use of RAM.

• Fixed allocation used in previous examples (size of data structures is specified in the assembly code).

• With dynamic allocation, the size and location of the data structures is determined at run time (not assembly time).

Page 36: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

• Heap—a chunk of RAM that is– 1. Dynamically allocated by the program– 2. Used by the program to store information– 3. Dynamically released by the program when

the structure is no longer needed.

• Implementation of dynamic allocation requires the management of a heap.

Page 37: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.

6.9.1 Fixed-Block Memory Manager

• Figure 6.27– Initial state of the heap has all of the free blocks

linked in a list.• Program 6.28

– Shows the global structures for the heap, defined in RAM.

– SIZE—number of 8-bit bytes in each block.– NUM—number of blocks to be managed.– FreePt--points to the first free block.

• Program 6.29– Software that that partitions the heap into blocks and

links them together.

Page 38: Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.