From: Jim Rogers <jrogers@omnitech.com>
Subject: Re: C/C++ programmer giving Ada95 a chance -- writing an emulator.
Date: 2000/03/28
Date: 2000-03-28T00:00:00+00:00 [thread overview]
Message-ID: <8bqoeh$9gs$1@nnrp1.deja.com> (raw)
In-Reply-To: 38E05E84.BB5507FC@gmx.net
In article <38E05E84.BB5507FC@gmx.net>,
> 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?
>
There are some differences in the Ada approach and the C++ approach.
What you are asking is if Ada can be C++. The answer is that Ada is
only moderately good at being C++.
Some differences:
Ada does not have an equivalent of a ++ operator.
All your C++ functions would be implemented as Ada procedures.
You can build a similar table of access to procedures in Ada. This
would not be what Ada describes as a dispatching operation. Ada uses
the term dispatching to refer to what in C++ would be calling a
virtual function through the vtbl.
The Ada specification for the procedures and access types would look
something like:
type Proc_Ptr is access procedure;
type OpCode is range 16#00#..16#FF#;
type Opcode_Table is array(OpCode) of Proc_Ptr;
procedure Op_00;
procedure Op_01;
procedure Op_02;
...
OpTable : Opcode_Table := (
16#00# => Op_00'Access,
16#02# => Op_01'Access,
16#03# => Op_02'Access,
... );
IR : OpCode;
function Core(Item : in System.Address) return OpCode;
Now get the opcode, advance the program address, execute the opcode
IR := Core(PA);
PA := PA + 1;
OpTable(IR).all;
--
Jim Rogers
Colorado Springs, Colorado USA
Sent via Deja.com http://www.deja.com/
Before you buy.
next prev parent reply other threads:[~2000-03-28 0:00 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <38e148e2.5089627@news.shreve.net>
2000-03-28 0:00 ` C/C++ programmer giving Ada95 a chance -- writing an emulator Geoff Bull
2000-03-28 0:00 ` Jean-Marc Bourguet
2000-03-28 0:00 ` Juergen Pfeifer
2000-03-28 0:00 ` Jim Rogers [this message]
2000-03-29 0:00 ` Ed Falis
2000-03-29 0:00 ` James S. Rogers
2000-03-29 0:00 ` Robert Dewar
2000-03-29 0:00 ` Jean-Marc Bourguet
2000-03-30 0:00 ` Geoff Bull
2000-03-30 0:00 ` tmoran
2000-04-01 0:00 ` Robert Dewar
2000-03-28 0:00 ` Ken Garlington
2000-03-30 0:00 ` Geoff Bull
[not found] ` <38e7e951.8384503@news.shreve.net>
2000-04-02 0:00 ` Jean-Pierre Rosen
2000-04-02 0:00 ` Robert Dewar
2000-04-03 0:00 ` Paul Graham
2000-04-06 0:00 ` Robert Dewar
2000-04-06 0:00 ` Larry Kilgallen
2000-04-06 0:00 ` Robert Dewar
2000-04-06 0:00 ` Gautier
2000-04-07 0:00 ` Robert Dewar
2000-04-07 0:00 ` Gautier
[not found] ` <38e19656.17008608@news.shreve.net>
2000-03-29 0:00 ` Marc A. Criley
2000-03-29 0:00 ` David Starner
2000-03-29 0:00 ` Robert Dewar
2000-03-29 0:00 ` Jean-Marc Bourguet
2000-03-29 0:00 ` Robert Dewar
2000-03-30 0:00 ` Jean-Marc Bourguet
2000-04-01 0:00 ` Robert Dewar
2000-03-29 0:00 ` Marin D. Condic
2000-03-29 0:00 ` Robert A Duff
2000-03-29 0:00 ` Marin D. Condic
2000-03-30 0:00 ` Geoff Bull
2000-04-01 0:00 ` Robert Dewar
2000-04-02 0:00 ` Geoff Bull
2000-04-02 0:00 ` swhalen
2000-04-02 0:00 ` Robert Dewar
2000-03-29 0:00 ` Robert A Duff
2000-03-30 0:00 ` Geoff Bull
2000-04-01 0:00 ` Robert Dewar
2000-03-29 0:00 ` Marin D. Condic
2000-03-29 0:00 ` swhalen
2000-03-29 0:00 ` Robert Dewar
2000-03-30 0:00 ` swhalen
2000-03-30 0:00 ` Ken Garlington
2000-03-30 0:00 ` Samuel T. Harris
2000-04-01 0:00 ` Robert Dewar
2000-04-05 0:00 ` Robert A Duff
[not found] <38E3DBD7.27F5B246@acenet.com.au>
2000-03-31 0:00 ` tmoran
2000-03-31 0:00 ` Geoff Bull
2000-04-01 0:00 ` Tucker Taft
2000-04-02 0:00 ` Robert Dewar
2000-04-02 0:00 ` Geoff Bull
2000-04-02 0:00 ` Geoff Bull
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox