From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: Is there a better (Ada) way?
Date: Thu, 23 Oct 2003 03:12:59 GMT
Date: 2003-10-23T03:12:59+00:00 [thread overview]
Message-ID: <%GHlb.1106$mZ5.9467@attbi_s54> (raw)
In-Reply-To: 20619edc.0310221056.4c92d10c@posting.google.com
I don't know if its really more "Ada like" but here is the approach I would
take.
Rougly following your conventions I would add a type definition:
type SReg16_T is range -2**15..2**15-1;
for SReg16_T'size use 16;
Then use a type conversion for a sign extend:
SReg16_T( PB.Offset5 )
And an instance of unchecked conversion to convert to a SReg16_T to a
Reg16_T:
function To_Reg16_T is new Ada.Unchecked_Conversion( SReg16_T, Reg16_T );
Then the expression in your example would read:
R16 := R16 + To_Reg16_T( SReg16_T( PB.Offset5 ) );
Another approach would be to use a lookup table indexed by the offset giving
the amount to change the index. Something like:
type RegOff_T is array( Offset5_T ) of Reg16_T;
Reg_Offset : constant RegOff_T :=
(
-16 => 16#FFF0#,
-15 => 16#FFF1#,
-14 => 16#FFF2#,
-13 => 16#FFF3#,
-12 => 16#FFF4#,
-11 => 16#FFF5#,
-10 => 16#FFF6#,
-9 => 16#FFF7#,
-8 => 16#FFF8#,
-7 => 16#FFF9#,
-6 => 16#FFFA#,
-5 => 16#FFFB#,
-4 => 16#FFFC#,
-3 => 16#FFFD#,
-2 => 16#FFFE#,
-1 => 16#FFFF#,
0 => 16#0000#,
1 => 16#0001#,
2 => 16#0002#,
3 => 16#0003#,
4 => 16#0004#,
5 => 16#0005#,
6 => 16#0006#,
7 => 16#0007#,
8 => 16#0008#,
9 => 16#0009#,
10 => 16#000A#,
11 => 16#000B#,
12 => 16#000C#,
13 => 16#000D#,
14 => 16#000E#,
15 => 16#000F#
);
Then the expression in your example would read:
R16 := R16 + Reg_Offset( PB.Offset5 );
If you actually want to try to run the simulator, I'd try a few approaches
and pick the one that is faster.
BTW: There is a nice module built in to Ada95 called "Interfaces" it
defines stuff like Unsigned_16, etc. I prefer to use that over defining my
own similar types.
Steve
(The Duck)
"Mike Silva" <snarflemike@yahoo.com> wrote in message
news:20619edc.0310221056.4c92d10c@posting.google.com...
> I've started writing a little CPU simulator, just for fun and to be
> doing *something* in Ada, and I want to make sure I'm not writing "C
> in Ada" when it comes to all the bit, bitfield and register
> manipulations.
>
[snip]
> ....
> R16 := R16 + Reg16_t( Integer( PB.offset5 ) mod Reg16_t'Modulus );
> -- the above line works, but is it good Ada, or C-in-Ada?
next prev parent reply other threads:[~2003-10-23 3:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-10-22 18:56 Is there a better (Ada) way? Mike Silva
2003-10-22 19:33 ` sk
2003-10-23 18:33 ` Mike Silva
2003-10-22 20:50 ` Robert I. Eachus
2003-10-23 16:57 ` Mike Silva
2003-10-23 21:42 ` Robert I. Eachus
2003-10-24 1:42 ` Mike Silva
2003-10-23 3:12 ` Steve [this message]
2003-10-23 18:30 ` Mike Silva
2003-10-24 22:20 ` Nick Roberts
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox