1 / 23

Lecture 10: a New Interrupt invoking function

This lecture covers the new interrupt invoking function in real-time control software, including pointers, structures, and embedded systems.

tammyhayes
Download Presentation

Lecture 10: a New Interrupt invoking function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10: a New Interrupt invoking function • Software Engineering: • pointers and structures • Embedded Systems: • A new interrupt invoking function CS3369 Real Time Control Software/DENG Xiaotie

  2. Syntax data_type * pointer_name; An example: int * my_pointer; //my_pointer is a pointer to an integer char * another_pointer //another_pointer is a pointer to a char //it can also used as an array of char; another_pointer=“Deng”; // cout << another_pointer << endl; //it is equivalent to cout<<“Deng”<<endl; Pointers CS3369 Real Time Control Software/DENG Xiaotie

  3. int * my_pointer; my_pointer name of the pointer &my_pointer address of the pointer *my_pointer value of object pointed to by my_pointer (an int here). In comparison int my_int; my_int name and value of the int &my_int address of the int Pointers CS3369 Real Time Control Software/DENG Xiaotie

  4. Syntax struct struct_type { variable-type variable_name; variable-type variable_name; ……… variable-type variable_name; }; //don’t forget this “;” Declare a variable of user defined struct-type struct_type struct_name; //after struct_type is defined Define a struct in Turbo C++ CS3369 Real Time Control Software/DENG Xiaotie

  5. struct student { char name[20]; //name for student long id; //id for student }; //don’t forget this “;” student stud1; //define variable stud1 to be type student stud1.name[0]=‘J’; stud1.name[1]=‘a’; stud1.name[2]=‘c’; stud1.name[3]=‘k’; stud1.id=88888888; cout << “The ID of” << stud1.name <<“ is “ << stud1.id<<endl; An Example: CS3369 Real Time Control Software/DENG Xiaotie

  6. Syntax structure_type * pointer_name; An example: student * stud1P; //define variable stud1P to be pointer to student stud1P->name[0]=‘J’; // stud1P->name[1]=‘a’; stud1P->name[2]=‘c’; stud1P->name[3]=‘k’; stud1P->id=88888888; cout << “The ID of” << stud1P->name <<“ is “ << stud1P->id<<endl; int * my_pointer; //my_pointer is a pointer to an integer Pointer to struct CS3369 Real Time Control Software/DENG Xiaotie

  7. student * stud1P; //define variable stud1P to be pointer to student stud1P the pointer *stud1P the struct pointed to by the pointer (*stud1P).id the variable id for the struct pointed to by the pointer stud1P->id simplified version of (*stud1P).id the followings are similarly interpreted. stud1P->name[0]=‘J’; stud1P->name[1]=‘a’; stud1P->name[2]=‘c’; stud1P->name[3]=‘k’; Pointer to struct CS3369 Real Time Control Software/DENG Xiaotie

  8. struct CharInt {char x; int y;}; //define a struct type CharInt get_key_number () { //return type of the function is CharInt char a; int b; CharInt tmp; _AH=0x0; //service number 0x00 geninterrupt(0x16); //interrupt 0x16 a=_AL; b=_AH; //_AL is the key and _AH is the keynumber tmp.x=a; tmp.y=b; //assign them to a variable of type CharInt return tmp; //return the value } A Function to get a key and its key number CS3369 Real Time Control Software/DENG Xiaotie

  9. Problems caused by Pseudo-variables Pseudo-variables refer to CPU registers which are used by other programs which may run at the same time. One must assign values right before using them and read values right after obtaining them, especially when we program in C. Function “geninterrupt” has no direct provision for register manipulation so we have to use pseudo-variables. Therefore, there is no way to guarantee that pseudo-variables will retain their values during a succession of assignment operations before/after a “geninterrupt” invocation. CS3369 Real Time Control Software/DENG Xiaotie

  10. More Reliable Access to Registers Segments registers can be obtained more reliably by the following approach: • struct SREGS {unsigned int es,cs,ss,ds;} • void segread (struct SREGS *SegReg) The function segread simply copies the values of the four segment registers into the corresponding structure SegReg items. CS3369 Real Time Control Software/DENG Xiaotie

  11. Three More Structures • struct WORDREGS { unsigned int ax,bx,cx,dx,si,di,cflags,flags;} • struct BYTEREGS {unsigned char al,ah,bl,bh,cl,ch,dl,dh;} • union REGS {struct WORDREGS x; struct BYTEREGS h;} The item “cflags” of the WORDREGS structure reflects the value of the carry flag in item “flags”: “cflag” is zero when this flag is not set. DOS frequently sets this flag to indicate an error condition. CS3369 Real Time Control Software/DENG Xiaotie

  12. An Alternative Function for Invoking Interrupts • int int86(int N,union REGS *in, union REGS *out) • //precondition: N is assigned to be the interrupt number to invoke. The relevant service number and the data are assigned in the REGS *in • //postcondition: the interrupt with the specified service is invoked and the output data are in the REGS*out. NOTE:Function int86 is used to invoke interrupt when the request for the desired interrupt service does not use special settings of the segment registers. Its source code consists of a call to “segread” to load the structure “SegReg”, and then make a call to invoke interrupt. CS3369 Real Time Control Software/DENG Xiaotie

  13. Input/Output of int86 From structure To registers • In • SegReg • AX,BX,CX,DX,SI,DI • DS,ES From registers To structure • AX,BX,CX,DX,SI,DI,Flag • DS,ES • Out • SegReg CS3369 Real Time Control Software/DENG Xiaotie

  14. Revised Function for Output a Char • void output_a_char(int x); //prototype • //precondition: x is the ascii code of a character • //postcondition: the character is output to screen • void output_a_char(int x) //definition • {REGS*in; REGS*out; • in->h.ah=0x0E; • in->h.al=x; • int86(0x10,in,out); • } CS3369 Real Time Control Software/DENG Xiaotie

  15. struct CharInt {char x; int y;}; //define a struct type CharInt get_key_number () { //return type of the function is CharInt REGS*in; REGS*out; CharInt tmp; REGS.h->ah=0x0; //service number 0x00 int86(0x16,in,out); //interrupt 0x16 tmp.x=out->h.al; //_AL is the key and _AH is the keynumber tmp.y=out->h.ah; //assign them to a variable of type CharInt return tmp; //return the value } Revised Function to get a key and its key number CS3369 Real Time Control Software/DENG Xiaotie

  16. Check any key is hit (for control) • int key_ready() {//return 1 if a key is ready, 0 otherwise • long int x; • REGS in, out; //page 358 of chapter12 of the handout • REGS.h->ah=0x1; //service number 0x00 • int86(0x16, &in, &out); //interrupt 0x16 • x=out.x.flags; //get flag register • if (x&(0x40)==0) {return 1;} //if ZF==0 a key is ready • else return 0; //else no key • } CS3369 Real Time Control Software/DENG Xiaotie

  17. Write a function to read a key • char getch() { //return char for the key hit • { • REGS in; REGS out; • //page 358 of chapter12 of the handout • in.h.ah=0x0; //service number 0x00 • int86(0x16,in,out); //interrupt 0x16 • return out.h.al; //AL is the key • } • //alternatively, one may simply do: • //{char t; cin >> t; return t;} CS3369 Real Time Control Software/DENG Xiaotie

  18. Control by typing keys • char k; • If (key_ready()) • { • k=getch(); • switch k • { • case ‘l’: do_something; break; • case ‘r’: do_something_else; break; • case ‘u’: do_another_thing; break; • case ‘d’: do_different_thing; break; • } • } CS3369 Real Time Control Software/DENG Xiaotie

  19. More about assignment 2 • Some suggestions are given below: • add a constructor in the class Elevator. This will reduce the length of the main function and makes the program clear. • Write a function for “pressing buttons”. Recall that we have 24 buttons to be pressed. CS3369 Real Time Control Software/DENG Xiaotie

  20. More about assignment 2 • How to open a door? • Draw a door by drawing 50 vertical lines. • Use a member variable door to control the door. • Door is an integer, the value is 25 --3. • 25-- the door is closed • 3-- the door is open. • Open the door • just draw black lines from 25 to door. • Close the door • draw more and more yellow lines. CS3369 Real Time Control Software/DENG Xiaotie

  21. More about assignment 2 • When to open the door? • After your door REALLY stops. • You have to study the function stop(). • When to close the door • before the lift moves, i.e., before x.k is changed. • However to show buttons inside the elevator • modify the member function selevator() to show bottons according to array[]. • if (array[i]==1) print a dot/circle CS3369 Real Time Control Software/DENG Xiaotie

  22. More about assignment 2 • How to show buttons outside the elevator • do it according to the two arrays corresponding to the buttons. Let up[] and down are the arrays. • A ordinary function can be defined. • Call the function in main(). • if (up[i]==1) print red square else print green square • if (down[i]==1) print read circles else print green circles. • Repeat the above for different i’s. CS3369 Real Time Control Software/DENG Xiaotie

  23. More about assignment 2 • How to test if the lift should be stopped? • Very hard • First, just consider the buttons inside the elevator • two subcases should be considered • lift is going up (look at x.sign) • lift is going down • When buttons outside the elevators are considered, we have to decide that which elevator stops. (This is the last step to do.) CS3369 Real Time Control Software/DENG Xiaotie

More Related