1 m m m m o o o o t t t t o o o o r r r r o o o o l l l l a a a a p p p p o o o o w w w w e e e e r r r r p p p p c c c c e e e e x x x x c c c c i i i i m m m m e e e e r r r r l l l l a a a a b b b b o o o o r r r r a a a a t t t t o o o o r r r r y y y y m m m m a a a a n n n n u u u u a a a a l l l l jose i. qui?ones, noel serrano, walter guiot, luis narvez, eisen montalvo department of electrical and computer engineering university of puerto rico-mayagez chuck corley powerpc applications engineering motorola editor: jos l. cruz rivera department of electrical and computer engineering university of puerto rico-mayagez volume i instructors manual
2 ? motorola inc. 1999 portions hereof ? international business machines corp. 1991C1995. all rights reserved. this document contains information on a new product under development by motorola and ibm. motorola and ibm re- serve the right to change or discontinue this product without notice. information in this document is provided solely to enable system and software implementers to use powerpc m icroprocessors. there are no express or implied copyright or patent licenses granted hereunder by motorola or ibm to design, modify the design of, or fabricate circuits based on the information in this document. the powerpc 60x microprocessors embody the intellectual property of motorola and of ibm. however, neither motorola nor ibm assumes any responsibility or liability as to any aspects of the performance, operation, or other attributes of the microprocessor as marketed by the other party or by any third party. neither motorola nor ibm is to be considered an agent or representative of the other, and neither has assumed, created, or granted hereby any right or authority to the other, or to any third party, to assume or create any express or im plied obligations on its b ehalf. information such as data sheets, as well as sales terms and conditions such as prices, schedules, and support, for the prod- uct may vary as between parties selling the product. accordingly, customers wishing to learn more information about the products as marketed by a given party should contact that party. both motorola and ibm reserve the right to modify this manual and/or any of the products as described herein without further notice. nothing in this manual, nor in any of the errata sheets, data sheets, and other supporting documentation, shall be interpreted as the conveyance by motorola or ibm of an express warranty of any kind or implied warranty, representation, or guarantee regarding the merchantability or fitness of the products for any particular purpose . neither motorola nor ibm assumes any liability or obligation for damages of any kind arising out of the application or use of these materials. any warranty or other obligations as to the products described herein shall be undertaken solely b y the marketing party to the customer, under a separate sale agreement between the marketing party and the customer. in the absence of such an agreement, no liability is assumed by motorola, ibm, or the marketing party for any damages, actual or otherwise. typical parameters can and do vary in different applications. all operating parameters, including typicals, must be validated for each customer application by customers technical experts. neither motorola nor ibm convey any license under their respective intellectual property rights nor the rights of others. neither motorola nor ibm m akes any claim, warranty, or representation, express or implied, that the products described in this manual are designed, intended, or authorized for use as components in systems intended for surgical implant into the body, or other ap plications intended to support or sustain life, or for any other application in which the failure of the product could create a situation where personal injury or death may occur. should customer purchase or use the products for any such unintended or unauthor- ized application, customer shall indemnify and hold motorola and ibm and their respective officers, employees, s ubsidi- aries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthor- ized use, even if such claim alleges that motorola or ibm was negligent regarding the design or manufacture of the part. motorola and are registered trademarks of motorola, inc. motorola, inc. is an equal opportunity/affirmative action em- ployer. ibm and ibm logo are registered trademarks, and ibm microelectronics is a trademark of international business machines corp. the powerpc name, powerpc logotype, and powerpc 601 are trademarks of international business ma- chines corp. used by motorola under license from international business machines corp. international business ma- chines corp. is an equal opportunity/affirmative action employer. dis claim ers
3 this laboratory manual contains 13 lab experiments for the powerpc excimer board presented in an increasing order of complexity. the experiments range from memory mapping problems and system benchmarking to integer to floating point number representation conversion. it is as- sumed that the student has a basic understanding of c and assembly languages. there is a natural progression in the lab experiments leading up to the dhrystone and linpack benchmarking of the powerpc603e that forms the basis of the excimer board. specifically, the experiments guide the student through the following topics: code com pilation, code download, dink functions (resi- dent monitor program), keyboard i nput, assembly l anguage programming, and linking assembly language to c code. there are also experiments on memory mapping and flash rom program- ming. each lab experiment is structured as follows: problem statement, objectives, b ackground infor- mation, procedure, questions, and references. the problem statement provides a brief indica- tion as to the tasks that will be performed. the objectives section presents the specific educa- tional objectives that will be met upon successful completion of the lab experiment. the back- ground information section presents a brief description of the theory behind the devices, instruc- tions, functional units, and/or methods to be followed in the conduction of the experiment. the procedure section presents a step-by-step guide to the experiment. the questions section seeks to guide the student through a m eaningful analysis of what he/she has performed as part of the experiment. finally, the references section presents additional references with material that is useful for the experiment at hand. in addition to these sections, the instructors manual contains a results and a troubleshooting section. int roduc t ion
4 this laboratory manual contains experiments designed to familiarize students with the powerpc architecture via the excimer laboratory board. the lab m anual is not meant to serve as a stand- alone text book on the powerpc instruction set architecture (isa), but rather is designed as a companion to any powerpc book or t echnical reference. each experiment is designed so that students will end up with a significant number of useful subroutines that can be used in other more complex programming problems. additional references to the powerpc architecture and the excimer board may be found at http://www.motorola.com/sps/powerpc/teksupport .
5 experiment #1: metaware tutorial write and compile a simple c program. 6 experiment #2: dink tutorial download the program to excimer and use some utilities. 10 experiment #3: useful dink functions write a program that will get input from kb and echo to display. discuss various utilities of interest. 14 experiment #4: excimer memory map compile, download, and execute a c program which blinks the on-board leds 18 experiment #5: led control from pc keyboard write and debug a c program to turn the on-board leds on and off for varying integer counts. 23 experiment #6: a simple scanf function for excimer develop a c function for taking character input from the terminal emulators keyboard attached to excimer through the serial port and converting number characters to decimal values used in other programs. 26 experiment #7: introduction to assembly language programming write a simple assembly language program. 32 experiment #8: linking assembly language and c code link previous code fragments. 41 experiment #9: converting integers to floating point develop an assembly language subroutine to convert the 64 bit integer value read from the powerpc time base facility to a 64 bit (double) floating point number representing seconds. (contributed by chuck cor- ley, motorola) 52 experiment #10: dhrystone benchmarking write and debug a c program to count the integer number of cycles required to execute the dhrystone benchmark. 63 experiment #11: linpack benchmarking write and debug a c program to time in microseconds (floating point) the execution of the linpack bench- mark. 72 experiment #12: cache impact on benchmark metrics write a single program to time the performance of dhrystone and linpack with the caches enabled and dis- abled. 76 experiment #13: flash rom write a program that copies itself into flash rom and begins executing from there. 80 contents
6 metaware tutorial problem statement: ? in this experiment the student will develop and compile a c program that will calculate the first 12 fibonacci numbers using the metaware powerpc compiler. (contributed by noel serrano). objectives: upon completion of this laboratory experience, students will be able to: ? write, debug and compile a c program using the metaware and code warrior compilers ? write a recursive function that will generate the first 12 fibonacci numbers background information: this experiment is designed to take you through the major steps required to implement a simple algorithm for the generation of the first 12 fibonacci numbers using the metaware compilers for the excimer board. the metaware compiler facilitates code writing, debugging, and optimization. more information on the compiler may be obtained from www.metaware.com . the fibonacci sequence represents a series that has as its first two elements 0 and 1. the re- maining elements can be obtained by simply adding the last two numbers to get the next. for ex- ample, the first 12 fibonacci numbers (the first element in the sequence, 0, is not included) are: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 experiment 1
7 the fibonacci numbers arose from the solution to the following problem posed in the year 1225: suppose we have one pair of rabbits that can produce another pair of productive offspring when they reach the age of 1 m onth and that each successive pair of offspring can do the same. fur- thermore, assume the rabbits never die. how m any rabbits will t here be after n months? the solution is as follows: if after n months there are k n pairs of rabbits, the number of pairs in month n+ 1 will be k n plus the number of new pairs born. however, since new pairs are only born to pairs at least 1 month old, there will be k n-1 new pairs, that is k n+1 = k n + k n-1 , which is simply the rule for generating the fibonacci numbers. more information on the fascinating world of fi- bonacci num bers and their applications can be found in http://pass.maths.org.uk/issue3/fibonacci/index.html . procedure: 1. write a c language program that will calculate the first 12 fibonacci numbers. hint: use a recursive function. 2. to be able to print the numbers in the dink32 interface (this will be di scussed in more detail in future experiments) you will need to add the following code to your program to redefine the default printf function with the one provided by dink. also the printf function should contain only one variable. #define printf dink_printf unsigned long (*dink_printf)() = (unsigned long (*)())) 0x6270; 3. type your program to a text file using notepad or edit and save it in the directory you have chosen to contain your code. 4. compile the c code with the hcppc command included with the metaware c compiler using the following command on the dos command prompt : hcppc ?ppc603 ? file.c
8 note: file.c stands for the c code file. you may name your c code file as you wish, but remember to use the chosen name in the hcppc command. the result from this command will be file.o, which is the object file. for m ore information a bout the options of the compiler type hcppc -h . 5. link the object files using ldppcl command to invoke the linker program included with metaware c compiler using the following command on the dos command prompt: ldppc ? start_addr=70000 ?m file.o note : file.o is the object file generated in the last step. the object file will be named exactly as you named the c code file.) the result from this step will be the file a. hex, the file that will be later downloaded to the excimer board. the b start_addr=70000 is an option that speci- fies where does your code is going to be paced in the memory of the excimer board. for more information about the linker type ldppc -h . references: ? metaware high c/c++ compiler C http://www.metaware.com suggested code: int fibonacci(int x); #define printf dink_printf unsigned long (*dink_printf)() = (unsigned long (*)()) 0x6270; main() { int fib_no = 0, index = 0; while (index < 12) { fib_no = fibonacci(index); printf("fibonacci number for index %d is", index);
9 printf(" %d\n", fib_no); index++; } return 0; } int fibonacci(int number) { switch (number) { case 0 : return 1; case 1 : return 1; default : return (fibonacci(number-1) + fibonacci(number-2)); } } troubleshooting: if the student is not able to: ? print to the dink 32 interface: verify that the address of the pointer address matches that of the dink version in use. this can be done through an st command in dink and verifying that the address for printf matches the one provided in this manual.
10 dink tutorial problem statement: ? this experiment is designed to introduce the student to the dink int erface. a tutorial on how to download code to the excimer board and some useful dink debugging u tilities are also presented. (contributed by noel serrano). objectives: upon completion of this laboratory experience, students will be able to: ? download their programs to the excimer board using the dink interface. ? debug the programs using the dink built-in debugging tools. background information: the excimer board contains a debugging interface called dink. this interface enables you to connect to the evaluation board through a serial cable using a terminal program. this enables the developer to have continuous communication with the evaluation board, allowing insight into the boards state at all time. the terminal screen of your program should look like this. experiment 2
11 figure 1. dink32 on terminal client more information on dink can be found at www.mot.com/sps/powerpc/teksupport/teklibrary/index.html . procedure: 1. first make sure you have your evaluation board connected using the serial cable provided to serial port 1 (com1). open your terminal client and configure it to connect through com1 using the following parameters. parameter value protocol serial port com1 baud rate 9600 data bits 8 parity n stop bits 1 rts/cts enabled turn on the evaluation board and press connect on your terminal client. you should be able to see the initialization window with the dink32_603e >> prompt as presented in the figure shown below (see step 4). 2. compile the program you created on experiment number one using the metaware compiler.
12 3. now you are ready to download your program to the excimer board for execution. to do so first go to the terminal client running the dink32 interface and type dl k . this command will expect to receive data from the keyboard serial port (com1). now proceed to send the file from the terminal client. this can be done by selecting a comm and like send text file or send ascii (this can vary from one terminal client to the other). now browse for the a.hex created in the directory where you compiled your program. 4. run your program by typing go 70000 in the dink32 program. if your code is correct and if you have been successful in downloading the code you should get an output like the following. hint: the table below presents some useful commands in case you need to debug your program, view memory or register contents, and/or ser breakpoints for program tracing. for m ore informa- tion type help in the dink prompt. command format description memory display md displays the memory area specified by the hex address registry display regdisp rx displays the register specified by rx disassemble ds disassemble the code starting at the specified address location trace tr begin tracing a program at the specified address. to continue tracing type tr +. breakpoint br sets a breakpoint at the specified address assemble as provides you with the option of changing part of the assembly from the dink interface accessing it through the address of the code line.
13 references: [1] motorola, designing a minimal powerpc system, powerpc application note: an1769/d, 1998. conclusions: students should be able to note that: ? dink works similarly to other evaluation board environments ? dink provides functionality that enables the user to modify the memory, registers, and as- sembly code ? dink provides breakpoint and trace capabilities for debugging purposes troubleshooting: if the student is not able to communicate with the dink: ? verify the connections to com1 port and board ? check for correct settings on terminal client
14 useful dink functions problem statement: ? in this experiment the student is introduced to a set of useful functions that are contained within the dink 32 interface. (contributed by noel serrano). objectives: upon completion of this laboratory experience, students will be able to: ? work with more advanced dink functions and use them on future laboratories. background information: the dink 32 interface provides a set of functions that fac ilitate the development of programs for the excimer board. among the functions included in dink are some that allow the program- mer to capture data from the keyboard and to print to the screen. o ther functions control parts of the excimer board configuration like enabling the timer, cache, etc. this laboratory will give you and overview of a basic set of these functions and will teach you how to access them in your c programs. there is a comm and included in dink that display a list of all these functions with their branch labels and corresponding addresses. the comm and is st and the corresponding output will look like this. dink32_603e >>st experiment 3
15 current list of dink branch labels: keyboard: 0x0 get_char: 0x1e4c4 write_char: 0x5eb4 tbaseinit: 0x39e0 tbasereadlower: 0x3a04 tbasereadupper: 0x3a20 cacheinhibit: 0x3a3c invenl1dcache: 0x3a5c disl1dcache: 0x3aa4 invenl1icache: 0x3ac8 disl1icache: 0x3b00 burstmode: 0x3bfc ramincbk: 0x3c3c raminwthru: 0x3c7c dink_loop: 0x55e8 dink_printf: 0x6270 current list of user branch labels: dink32_603e >> all these functions can be accessed through your c code by casting a function that will point to the address in dink. the code that defines the function would look like the following example for the printf function. #define printf dink_printf unsigned long (*dink_printf)() = (unsigned long (*)()) 0x6270; in the following section we present examples of three dink functions. a) get_char C this function enables the programmer to capture characters from the keyboard through the dink interface. the get_cahr function can be accessed by using the following code: #define getchar dink_get_char unsigned long (*dink_get_char)() = (unsigned long (*)()) 0x1e4c4; this will enable you to capture characters from the keyboard. the syntax for r eading character from the keyboard would be: char led; led = getchar();
16 b) write_char C this function enables the programmer to display characters on the terminal screen that is running dink. the write_char function can be accesed by using the following code. #define writechar dink_write_char unsigned long (*dink_write_char)() = (unsigned long (*)())0x5eb4; this will enable you to output characters to the screen. the sysntax for displaying single charac- ters from the keyboard would be: char led = ?? led = writechar(led); c) dink_printf Cthis functions provide the programmer the option of displaying a string of char- acters on the dink interface and also provide the user the ability of including a runtime variable, either char or integer, on this string. it is done by using the dink_printf function using the same syntax as in c. #define printf dink_printf unsigned long (*dink_printfr)() = (unsigned long (*)())0x6270; this will enable you to print any message on the dink and also include any of the variables in- cluded in your code. the dink printf function can only include one variable per statement not like in c which it can contain any number of variables. printf("fibonacci number for index %d is", index); there are other important functions that can be used to control many aspects of the excimer board. these are briefly described in the table below and explained in details in the included dink manuals. functions address description tbaseinit 0x39e0 initializes the time base register tbasereadlower 0x3a04 reads the lower half of the time base register
17 tbasereadupper 0x3a20 reads the upper half of the time base register. cacheinhibit 0x3a3c turns off the caches. invenl1dcache 0x3a5c invalidate and enable the l1 data cache. disl1dcache 0x3aa4 disable l1 data cache. invenl1icache 0x3ac8 invalidate and enable the l1 instruction cache. disl1icache 0x3b00 disable l1 instruction cache. burstmode 0x3bfc sets up burst mode. references: [1] motorola, designing a minimal powerpc system, powerpc application note: an1769/d, 1998. suggested code: /* this section of code can be used to define any of the dink functions in a c l anguage program. the user will only need to modyfy the address and function name. */ #define function_name dink_function_name unsigned long (*dink_function_name)() = (unsigned long (*)()) hex_addr; troubleshooting: if the student is not able to access the dink functions: ? verify the casting is correct. ? verify that he/she is using the correct hex address.
18 excimer memory map problem statement: ? this experiment requires the compilation, downloading, and execution of a c l anguage pro- gram which blinks the excimer boards status and error light emitting diodes (leds). (contributed by noel serrano, jos i. qui?ones, luis navez, w alter guiot, and gunther costas). objectives: upon completion of this laboratory experience, students will be able to: ? write and compile a c program ? download and execute powerpc assembly object code ? locate the leds within the excimers memory map ? apply the methodology needed to turn on and off the leds background information: the powerpc family of microprocessors is based on a memory m apped input/output scheme. under this scheme, an input port can be thought of as read-only memory location, while an out- put port can be treated like a write-only memory l ocation. the microprocessors address bus is used to select the peripheral device (port location), the data bus is used to transmit or receive data to/from the device, and the transfer type signals are used to convey the directionality of the information transfer. experiment 4
19 the memory map for the excimer board is shown in fi gure 2. the memory map indicates that out of a total of 2 32 = 4gb addressable locations, the excimer board allocates 2 30 = 1gb each to static ram, fast i/o devices, slow i/o devices and fl ash rom [1]. of course, the excimer board only uses a fraction of the memory locations all ocated for each t ype of memory and de- vices. the excimer board is configured with 512 kbs of sram, 4mbs of flash rom, and some led indicators. for example, theres a status led located at 0x40200000, while an error led is specified at 0x40600000. static ram 0x0000_0000 0x3fff_ffff fast i/o 0x4000_0000 0x7fff_ffff ? status led : 0x4020_0000 ? error led: 0x4060_0000 slow i/o 0x8000_0000 0xbfff_ffff flash rom 0xc000_0000 0xffff_ffff figure 1: excimer's memory map. in this experiment you are required to write a c program that will blink (repeatedly turn on and off) the status and error leds alternatively. the leds are turned on/off by clear- ing/setting bit 3 (fourth least signifi cant bit) of these locations. the reason for this negative logic is that the leds are connected in a common anode configuration, as shown in fi gure 2 for the case of a seven segment led display.
20 figure 2: common anode led configuration. leds will turn on when the cathode is at ground level ( excimer output asserted low) to successfully blink an led, you must carefully select the delay timing. remember that the mi- croprocessor may turn the led on and off so quickly that you will not see the blinking effect. since your program will be written in c, a simple for loop instruction may do the job. for (counter=0;counter <= parameter; counter++); note : counter must be declared as unsigned long in the program. the value parameter will define the delay time. there are other ways to accomplish a time delay, for example using the powerpcs internal timer register. these techniques will be demonstrated in the successive experiments. procedure: 1. write a simple c code that alternatively blinks the status and error leds ten times. 2. compile the c code with the hcppc command included with the metaware c compiler using the following command on the dos command prompt : hcppc ?ppc603 ? file.c
21 note: file.c stands for the c code file. you may name your c c ode file as you wish, but remember to use the chosen name in the hcppc command. the result from this command will be file.o, which is the object file. 3. link the object files using ldppcl command to invoke the linker program included with metaware c compiler using the following command on the dos command prompt: ldppc ? start_addr=70000 ?m file.o note : file.o is the object file generated in the last step. the object file will be named ex- actly as you named the c code file.) the result from this step will be the file a.hex. 4. run the dink32 application on your windows 95 or nt terminal. download the a. hex file, which resulted from the last step. to do so, write dl ? on the dink monitor. on the terminal it will appear, ?et to keyboard port . press transfer -> send text file on the communication terminals m enu. find your a. hex file and select it. the file will be downloaded to the excimer board. 5. execute the program by writing go 70000? on the terminal. 6. observe the behavior of the on-board leds. what happens if you decrease/increase the value of parameter in your for loop statement? references: [1] motorola, designing a minimal powerpc system, powerpc application note: an1769/d, 1998. suggested code: /* this program will blink the status and error leds alternatively ten times. after that, both leds will be shut down. 0xfffff will cause a visible delay in a 300mhz powerpc*/
22 main () { unsigned long count; int loop; for (loop = 0 ; loop <= 10; loop++) { *(char *) (0x40200000) = 0x00; //turn on status *(char *) (0x40600000) = 0x08; //turn off error for(count = 0; count <= 0xfffff; count ++); *(char *) (0x40200000) = 0x08; //turn off status *(char *) (0x40600000) = 0x00; //turn on error for(count = 0; count <= 0x1fffff; count ++); } *(char *) (0x40600000) = 0x08; } conclusions: students should be able to note that: ? the speed, which drives the powerpc microprocessor, is very fast and thus a blinking effect might not be perceived. ? for different loop parameters, the led will remain on or off for a different time period. ? the leds are configured as common anode (negative terminal connected together). troubleshooting: if the student is not able to turn on or off the led check that: ? the address being written to is either 0x40600000 or 0x40200000. ? a suitable value for the time delay loop has been defined. ? the student has compiled, linked and downloaded the program correctly.
23 led control from keyboard problem statement: ? this experiment requires the compilation, downloading, and execution of a c l anguage pro- gram which blinks the excimer boards error l ight emitting diode (leds) the number of times specified by the user input. (contributed by noel serrano and jos i. qui?ones). objectives: upon completion of this laboratory experience, students will be able to: ? use the dink functions presented in experiment #3 ? print to the dink 32 interface ? capture single characters from the keyboard and echo them to the dink 32 interface procedure: 1. write a c program that will blink the on board leds based on user i nput. the program should ask the user which led he wants to blink and how many times. hint : to create this program use the program you created in the previous experiment and the useful dink functions. references: [1] motorola, designing a minimal powerpc system, powerpc application note: an1769/d, 1998. experiment 5
24 suggested code: #include #define getchar dink_get_char #define putchar dink_write_char #define printf dink_printf void blink_leds(int addr, int i); unsigned long (*dink_get_char)() = (unsigned long (*)()) 0x1e4c4; unsigned long (*dink_write_char)(char) = (unsigned long (*)(char)) 0x5eb4; unsigned long (*dink_printf)() = (unsigned long (*)()) 0x6270; main() { int decimal_no; char led; char number; do { printf ("\nselect the led you want to blink:\n"); printf ("\ts - press s for the status led\n"); printf ("\te - press e for the error led\n"); printf ("\tq - press q to quit\n"); led = getchar(); /* read typed character */ if (led == 'e' || led == 'e') { printf ("\nenter the number of times (1-9) to blink the er- ror led: "); do{ /* is it a number??? */ number = getchar(); }while ( !((number >= '0') && (number <= '9')) ); putchar(number); /* echo typed character */ decimal_no = number - 48; blink_leds(0x40600000, decimal_no); } else if (led == 's' || led == 's') { printf ("\nenter the number of times (1-9) to blink the status led: "); do{ number = getchar(); }while ( !((number >= '0') && (number <= '9')) ); putchar(number); decimal_no = number -48; blink_leds(0x40200000, decimal_no); } } while ( led != 'q' && led != 'q' ); /* x or x */ return 0; }
25 void blink_leds(int addr, int i) { unsigned long count; int loop; for (loop = 0 ; loop < i; loop++) { *(char *) (addr) = 0x00; //turn on error for(count = 0; count <= 0xfff00; count ++); *(char *) (addr) = 0x08; //turn off error for(count = 0; count <= 0xfff00; count ++); } *(char *) (0x40600000) = 0x08; } conclusions: students should be able to note that: ? a powerpc excimer board program can obtain data from a user via the keyboard. ? the get char function is not useful in cases you need more t han character as i nput, so an im- plementation of a scanf function would be useful. troubleshooting: if the student is not able to: ? access dink 32 interface functions: use the st command to verify that the address for the dink functions matches the ones provided in this manual. ? blink the leds: verify the memory mapping for each of the leds.
26 a simple scanf function for excimer problem statement: ? in this experiment the student will develop a c function for taking character input from the terminal emulators keyboard attached to excimer through the serial port and converting number characters to decimal values used in other programs. (contributed by chuck corley, motorola) objectives: upon completion of this laboratory experience, students will be able to: ? substitute the get char and put char equivalent functions available in dink for the same functions normally found in ? recognize the ascii character values returned from getchar() and echo them back via putchar() ? convert digit characters input through the keyboard into decimal integer values for use in other pro- grams ? utilize dinks print output function to display the resulting decimal value background information: texts on programming describe how to get input for a program. for example, the waite groups new c primer plus [1] says: the c library contains several i nput functions, and scanf() is the most general of them, for it can read a variety of formats. of course, input for the keyboard is text because the keys generate text characters: letters, digits, and punctuation. when you de sire to enter, say, the integer 2002, you type the characters 2 0 0 and 2. if you want to store that as a experiment 6
27 numerical value rather than as a string, your program has to convert the string character- by-character to a numerical value. and that is what scanf() does! it converts a string in- put into various forms: integers floating-point numbers, characters, and c strings. it is the inverse of prin tf(), which converts integers, floating-point numbers, characters, and c strings to text that is to be displayed on the screen. l ike prin tf(), scanf() uses a control string followed by a list of arguments. the control string indicates into which formats the input is to be converted. the dink software on excimer provides input and output functions that save the programmer from having to interact directly with the duart that r eceives i nput and sends output to the terminal. how- ever these functions are not at the level of a com plex f unction like scanf(). nevertherless, many of the c programs that we desire to run on excimer call the scanf() function because of its widespread use. in this experiment, you will write your own function my_scanf() and substitute it (by a #define direc- tive) for any scanf() function that the com piler may encounter in programs intended for excimer. likewise you will define dink_printf() to substitute for printf() and link dink_printf() into your pro- grams. then you will have input and output functions for use in other programs. to keep my_scanf() simple we will assume that the only control string for converting inputs is the %d or decimal format. your m y_scanf() function should accept a control string as an argument but then ignore it and return a decimal value to the second (and last) argument in the functional ca ll. l ater ex- periments may require m ore sophisticated substitute functions for scanf(), but this simple decimal in- put routine will be widely applicable. eximers dink_printf() does accept a control string but it ignores floati ng-point and character formats. it will only print decimal numbers (%d), hexadecimal numbers (%x), and strings (%s) and then only one such format per printf statement.
28 procedure: 1. write a c language program which asks the user to i nput a number through the keyboard and then outputs the number input as a positive decimal number. 2. in a separate file write a c program my_scanf(char, int) which reads characters from the keyboard, echoes those that are digits, and at the carriage return assigns a decimal value to the second argument of my_scanf(). hint: while ignoring non-digit characters may be an acceptable simplification, you may want to check for backspace or delete characters and take the appropriate action if the user attempts to correct his numerical input. 3. write a header file which equates the function name scanf to my_scanf and printf to dink_printf. in the header file equate dink_printf to the address where it is stored in ram as revealed by dinks symtab (symbol table) command. include this header file in your test program. example: /* file - support.h * equates functions used in excimer exercise to equivalent * functions defined in dink or in my_scanf.c * note: if dink function addresses change because dink changes, * addresses here must be changed accordingly. * * modification history: * 19jan99,cjc original */ #define printf dink_printf #define scanf my_scanf extern void my_scanf(const char *, ...); unsigned long (*dink_printf)() = (unsigned long (*)()) 0x6368; 4. your my_scanf() function will use getchar() and putchar(). write a header file equates these to dinks get_char and write_char. write a header file which equates dink_get_char() and dink_write_char() to the addresses where they are stored in ram as revealed by dinks symtab (symbol table) command. . include this header file in the my_scanf.c program. example: /* file - excimer.h * provides the addresses of functions defined in dink on the excimer * board and used by programs. the addresses of the functions are * taken from the xref.txt file generated by the linker. * when a new version of dink is downloaded to the target, make sure * the functions' addresses are changed accordingly to match with the
29 * new addresses being generated. * * modification history: * 21oct98,my created for excdemo * 19jan99,cjc modified to run with my_scanf code. */ #define getchar dink_get_char #define putchar dink_write_char /* addresses of dink functions. */ unsigned long (*dink_get_char)() = (unsigned long (*)()) 0x1e5e4; unsigned long (*dink_write_char)(char) = (unsigned long (*)(char)) 0x5fac; 5. link your input/output test program and my_scanf program. 6. download the resulting s-record file to excimer, execute it, confirm that it echoes only digit charac- ters and returns the correct decimal value to your program at the carriage return. references: [1] the waite groups new c primer plus (1990: howard w. sams & co, carmel, in) suggested code: /* file "testscanf.c" * a test harness for excimer experiment to prove out * my_scanf() function. * modification history: * 990121 cjc original */ #include "support.h" void main(void) { int decimal_no; printf ("enter a decimal number: "); scanf("%d", &decimal_no); printf ("\ndecimal number is: %d \n", decimal_no); /* file "support.c" * defines an alternative to the scanf function provided by * stdio.h for use when running the dhrystone benchmarks on dink. * created: 990119 cjc * modified: */ #include "excimer.h" void my_scanf(char *fmt, int *v) { char ch; int no_runs = 0; while ((ch = getchar()) != 0xd) /* carriage return? */ { if ( (ch == 0x7f) || (ch == 0x8)) /* delete? */
30 { putchar(0x8); /* backspace */ putchar(0x20); /* overprint a space. */ putchar(0x8); /* backspace */ /* assume modulo arithmetic to subtract last digit added. */ no_runs = no_runs / 10; } else if ( (ch >= '0') && (ch <= '9')) /* a digit? */ { putchar(ch); /* echo it and */ /* accumulate the value. */ no_runs = (no_runs * 10) + (ch - 48); /* ascii character - 48 equals the digit. */ } } *v = no_runs; /* assign second arg the value. */ } /* file ?akefile? */ support = supportopt = optlev = -o1 cpu = 603 targflags = -hppc$(cpu) cc = c:\sw\metaware\hcppc\bin\hcppc -ic:\sw\metaware\hcppc\inc \ -hnocopyr -c -nofsoft $(optlev) $(targflags) as = c:\sw\metaware\hcppc\bin\asppc -c -big_si lkopt = -bbase=0x70000 -xm -e main -bnoheader -bhardalign \ -xo=$(@) -q -qn -cglobals -csections -csymbols -ccrossref \ > $(@d)\xref.txt link = c:\sw\metaware\hcppc\bin\ldppc $(lkopt) testscanf.src: testscanf.o my_scanf.o $(link) testscanf.o my_scanf.o \ c:\sw\metaware\hcppc\lib\be\fp\libmw.a testscanf.o: testscanf.c $(cc) testscanf.c -o testscanf.o $(supportopt) my_scanf.o: my_scanf.c $(cc) my_scanf.c -o my_scanf.o $(supportopt) conclusions: students should be able to note that: ? characters are received from the keyboard as bytes of ascii encoded information. ? input/output functions normally available in standard c libraries for a given com puter may not be available or may exist in different, simpler forms on a small, embedded evaluation system like exci- mer . ? programmers can write their own input/output routines or link in routines that are provided in the embedded system.
31 ? hard-coding addresses of embedded routines is a dangerous way of linking c ode if the routines are relocated by dink revisions. troubleshooting: if the student is not able to: ? get started. suggest that the student develop and debug the c program on the host com puter by including before substituting the dink routines and downloading to excimer. this should clarify the ascii encoding of digits and conversion to a decimal number. ? recognize the carriage return character. eximer will be in a continous loop of accepting and echoing input. additional printf() statements which output each character as it is read in will reveal the value provided by the duart for the carriage return character.troubleshooting:
32 introduction to assembly language programming problem statement: ? in this experiment the student is introduced to the powerpc instruction set architecture through the development of an assemblyllanguage routine. (contributed by eisen montalvo-ruiz) objectives: upon completion of this laboratory experience, students will be able to: ? write and compile an assembly language subroutine ? use metaware assembler directives ? understand the instruction set and the register set of the powerpc background information: ? powerpc register set the powerpc architecture has two levels of privileges, the user m ode, and the supervisor m ode. in the supervisor m ode all registers are available to the programmer, while in the user m ode only a subset of the registers are available. we are going to focus on the user mode for this laboratory. tin the user m ode the available pow erpc registers include 32 general pur pose registers ( gprs), 32 floating-point registers ( fprs), a condition register (cr), a floating-point status and condition register (fpscr), the xer reg ister, the link register (lr) and the count r egister experiment 7
33 (ctr). in addition, there are two read-only registers, associated with the time base facility (tbu and tbl). the gprs are used to manipulate integer data. they come in two sizes, according to the implementation of processor. 32-bit gprs for the 32-bit powerpc and 64-bits for the 64-bit powerpc. they are used as source and destination registers in the integer instructions. the fprs are used with floating-point instructions. they are 64 b its wide independently of the implementation, and can manipulate single- and double floating-point data. related to these registers is the fpscr. it contains all floating-point exception si gnal bits, excluding summ ary bits, exception summary bits, exception enable bits, and rounding control bits. the cr is a 32-bit reg ister, divided into eight 4-bit fields. this register contains the results of certain arithmetic operations and provides a way for testing and branching. the xer register indicates overflows and carry conditions for integer operations. the lr register and the ctr register are like the gprs, their size depends on the implementation. the lr supplies the branch target address for the br anch conditional to link registers instructions. the ctr holds a loop count that can be decremented during execution of appropriately coded branch instructions. the time base facility consists of a 64-bit reg ister, divided in two 32-bit registers, time base upper (tbu) and time base lower (tb l). these registers will be used in a future laboratory, where you will learn more about them. ? powerpc instruction set the powerpc instruction set is very powerful and extensive. it contains around 200 in structions, excluding suffices. we dont have the space to cover all of them. for now, we are going to work with the integer arithmetic, load and store, and flow control instructions. a general description
34 of the format of the instructions will be given. more information can be obtained from the powerpc programming references. integer instruction set (a) integer arithmetic instructions you can add, subtract, multiply, and divide integer numbers. you can use imm ediate values and registers. also, register to register instructions are available. a general description of the format of the instructions follows. 1. immediate values opcode rd, ra, simm - where rd is the destination register, ra is the source register and simm is a signed immediate value. 2. register to register opcode rd, ra, rb - where rd is the destination register and ra and rb are the source registers. (b) integer compare instructions these instructions can be used in conjunction with the branch instructions to control the flow of a program. they affect the cr, such that the branch instructions can choose their target address based on what happened in the previous instruction. of course, t hey could be used only for comparing. 1. immediate values opcode ra, simm - where ra is the register you want to compare to a signed immediate value 2. register to register opcode ra, rb - where ra is the register you want to compare to register
35 rb load and store instruction set load and store instructions allow data m ovement between memory and register locations. they have t hree addressing modes. in anyone of them, if you use r0, the address calculation will use zero instead of the value in ra. (a) register indirect with immediate index addressing opcode rd, simm(ra) - if loading t hen rd is the destination reg ister. it will con- tain the value that is stored in the memory address that is the sum of simm and the value in the register ra. if storing then the memory address that is the sum of simm with the value in register ra, will contain the value stored in register rd. (b) register indirect with index addressing opcode rd, ra, rb - if loading then rd is the destination register. it will contain the value that is stored in the memory address that is the sum of the value in register ra and the value in the register rb. if storing then the memory address that is the sum of the value in register ra with the value in register rb, will contain the value st ored in register rd. (c) register indirect addressing opcode rd, ra - if loading t hen rd is the destination reg ister. it will contain the value that is stored in the memory address that is the value in the register ra. if storing then the memory address that is the value in register ra will contain the value stored in register rd.
36 branch instructions set these instructions are commonly used with compare instructions. you place the branch after the com- pare, using the result of it to make the decision. opcode label - where label is the address of the code where you want to branch to. the assembler takes care of translating the label to the address. ? metaware assembler directives the assembler directives are instructions to the assembler on how to configure data and w here to put the code and data in memory. the most useful are: (a) .text C identifies where the code section starts. (b) .data C mark the start of the data section (c) .word C reserves space for a word in memory (d) .org C starting address of the following code and/or data (e) .global
We use cookies to deliver the best possible
web experience and assist with our advertising efforts. By continuing to use
this site, you consent to the use of cookies. For more information on
cookies, please take a look at our Privacy Policy.