Lecture 10: a New Interrupt invoking function Software Engineering: pointers and structures Embedded Systems: A new interrupt invoking function Page 1 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie Pointers 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; Page 2 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie Pointers 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 &my_int name and value of the int address of the int Page 3 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie Define a struct in Turbo C++ 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 Page 4 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie An Example: struct student { • char name[20]; • long id; }; //name for student //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; Page 5 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie Pointer to struct 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 Page 6 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie Pointer to struct 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’; Page 7 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie A Function to get a key and its key number struct CharInt {char x; int y;}; //define a struct type CharInt get_key_number () { char a; int b; CharInt tmp; _AH=0x0; geninterrupt(0x16); a=_AL; b=_AH; tmp.x=a; tmp.y=b; return tmp; } //return type of the function is CharInt //service number 0x00 //interrupt 0x16 //_AL is the key and _AH is the keynumber //assign them to a variable of type CharInt //return the value Page 8 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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. Page 9 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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. Page 10 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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. Page 11 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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. Page 12 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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 Page 13 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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); } Page 14 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie Revised Function to get a key and its key number struct CharInt {char x; int y;}; //define a struct type CharInt get_key_number () { REGS*in; REGS*out; CharInt tmp; REGS.h->ah=0x0; int86(0x16,in,out); tmp.x=out->h.al; tmp.y=out->h.ah; return tmp; } //return type of the function is CharInt //service number 0x00 //interrupt 0x16 //_AL is the key and _AH is the keynumber //assign them to a variable of type CharInt //return the value Page 15 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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 } Page 16 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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;} Page 17 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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; } } 2016/5/29 Page 18 CS3369 Real Time Control Software/DENG Xiaotie 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. Page 19 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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. Page 20 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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 Page 21 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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. Page 22 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie 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.) Page 23 2016/5/29 CS3369 Real Time Control Software/DENG Xiaotie