comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Creating several types from a base type and conversion
Date: Sun, 19 Jan 2020 02:07:33 +0200
Date: 2020-01-19T02:07:33+02:00	[thread overview]
Message-ID: <h8hku5Fk6v0U1@mid.individual.net> (raw)
In-Reply-To: <536e9961-f454-44d9-95a6-aecaaee1addf@googlegroups.com>

On 2020-01-19 0:16, Ken Roberts wrote:
> <code>
> 
> package core is
> 
>      --
>      -- 30-bit word size (CP642/A/B)
>      --      Data : 0 .. 2**29
>      -- Half-word
>      --      Upper : at 0 range 15 .. 29
>      --      Lower : at 0 range 00 .. 14

This means that you (and/or the documentation of the original computer) 
are using little-endian bit numbering, in which the less-significant 15 
bits are numbered 0 .. 14, and the more significant are numbered 15 .. 29.

Note, again, that the bit-numbering scheme can be different in different 
Ada compilers or for different target computers, which means that if you 
Unchecked_Convert a record type with a representation clause from or to 
an integer or modular type (wuth the same size) you must use the 
Bit_Order aspect to ensure portability across Ada compilers.

>      WORD_BITS       : constant := 30;
>      WORD_MASK       : constant := 8#77777_77777#;
>      HALF_WORD_BITS  : constant := 15;
>      HALF_WORD_MASK  : constant := 8#77777#;

Most Ada programmers do not use all-caps for constants (or named 
numbers, as here), because they are not preprocessor macros as in C, but 
are more like ordinary objects or variables in Ada. You can of course 
define your own style, this is just a remark.

>      for DataWord use record
>          for Upper use HALF_WORD_BITS .. (WORD_BITS - 1);
>          for Lower use 00 .. (HALF_WORD_BITS - 1);
>      end record;
>      pragma pack (DataWord);

Pragma Pack has two uses: first, to reduce the amount of memory used for 
data, which is probably not relevant for your emulator; second, to 
squeeze records like this into a "word" of a certain size, usually to 
make it possible to Unchecked_Convert it to/from some other type of the 
same size, and this is probably your goal here. But note that pragma 
Pack, even when accepted by the compiler, does _not_ mean that the 
compiler uses a size that is the sum of the widths of the bit-ranges in 
the representation clause; the compiler could, for whatever reason, add 
some space. Therefore it is good practice, IMO, to always add a Size 
clause if you want a certain size. Therefore I would add, to every 
pragma Pack that is meant to produce 30 bits, the clause "for 
the_type'Size use 30;".

(But the errors in the type declarations must first be corrected, of 
course.)

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


  parent reply	other threads:[~2020-01-19  0:07 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-18  7:32 Creating several types from a base type and conversion Ken Roberts
2020-01-18 12:16 ` Simon Wright
2020-01-18 12:49   ` Ken Roberts
2020-01-18 14:56     ` Bill Findlay
2020-01-18 16:13       ` Jeffrey R. Carter
2020-01-18 18:20         ` Bill Findlay
2020-01-18 18:32           ` Jeffrey R. Carter
2020-01-18 20:34             ` Simon Wright
2020-01-20 16:38               ` Bill Findlay
2020-01-18 22:20       ` Ken Roberts
2020-01-18 15:09     ` Simon Wright
2020-01-18 22:16       ` Ken Roberts
2020-01-18 22:35         ` Simon Wright
2020-01-18 23:03           ` Ken Roberts
2020-01-18 23:38             ` Simon Wright
2020-01-19  0:12               ` Ken Roberts
2020-01-19  9:37                 ` Simon Wright
2020-01-19 11:48                   ` AdaMagica
2020-01-19 14:51                     ` Simon Wright
2020-01-19 15:24                       ` Niklas Holsti
2020-01-19 16:11                     ` Optikos
2020-01-19  0:33               ` Ken Roberts
2020-01-19  0:07         ` Niklas Holsti [this message]
2020-01-18 15:47     ` Simon Wright
2020-01-21 21:35     ` Shark8
2020-01-21 23:06       ` Niklas Holsti
2020-01-22  1:08         ` Ken Roberts
2020-01-22 14:18           ` Ken Roberts
2020-01-22  8:37       ` Simon Wright
2020-01-22 14:32         ` Shark8
2020-01-22 15:40           ` Simon Wright
2020-01-18 14:17   ` Optikos
2020-01-18 17:57 ` Niklas Holsti
2020-01-18 22:59   ` Ken Roberts
2020-01-19  0:30     ` Niklas Holsti
2020-01-19  1:07       ` Ken Roberts
2020-01-19  3:37 ` Ken Roberts
2020-01-23 21:39 ` Optikos
2020-01-24  9:35   ` Ken Roberts
2020-01-24 10:04     ` AdaMagica
2020-01-24 12:38     ` Optikos
2020-01-24 15:01       ` Ken Roberts
2020-01-24 15:22         ` Simon Wright
2020-01-24 15:40           ` Ken Roberts
2020-01-24 15:54   ` Simon Wright
2020-01-25 10:37 ` Ken Roberts
2020-01-25 10:44   ` Ken Roberts
2020-01-25 20:26   ` Shark8
2020-01-27 14:10 ` Ken 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