From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1ea19776e3073a96 X-Google-Attributes: gid103376,public From: Juergen Pfeifer Subject: Re: C/C++ programmer giving Ada95 a chance -- writing an emulator. Date: 2000/03/28 Message-ID: <38E05E84.BB5507FC@gmx.net>#1/1 X-Deja-AN: 603306988 Content-Transfer-Encoding: 7bit References: <38e148e2.5089627@news.shreve.net> X-Accept-Language: de-DE, en X-Sender: 340028232866-0001@t-dialin.net Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@t-online.de X-Trace: news07.btx.dtag.de 954228367 1013 340028232866-0001 000328 07:26:07 Organization: Familie Pfeifer Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-03-28T00:00:00+00:00 List-Id: jross wrote: > My first question is how do you define and write a dispatching > mechanism as you would in C++ using function pointers in a table for > execution speed efficiency, like this: > > --------------------------------- > -- the .h file > --------------------------------- > > class TACpu : public TCpu > { > ? define registers and much stuff (not shown) > > struct OpStruct // define the op code jump table structure > { > char Byte; // the opcode byte for reference only > void (TACpu::*Func)(); // executes opcode > }; > > static OpStruct OpTable[]; // the table in the .cpp file > > void Op_U(); // undefined > void Op_00(); // functions that execute the opcodes > void Op_01(); > void Op_02(); > void Op_03(); > // ? thru Op_FF > }; > > --------------------------------- > -- in the .cpp file > --------------------------------- > > TACpu::OpStruct TACpu::OpTable[] = { // opcode table > { 0x00, Op_00 }, > { 0x01, Op_01 }, > { 0x02, Op_U }, > { 0x03, Op_03 }, > // ? thru Op_FF > }; > > ... the Op_xx functions would go here > > now the code for calling the opcodes > > IR = Core[PA++]; // get the opcode, advance the program address > (this->*OpTable[IR].Func)(); // execute it > > ---------------- > > This is very efficient C++ code (could have been just as easy in plain > old C without the class encapsulation?) How would you do this in Ada > without using a Case Statement? > Ada95 allows the definition of "access to function" or "access to procedure" types. You obviously can then use these types to build arrays of "function pointers" or to use it in records. I recommend Norman H. Cohens "Ada as a second language" (ISBN 0-07-011607-5). It is an excellent Ada95 textbook for programmers coming from C/C++ or similar languages. This would have answered your question easily. Cheers Juergen