bitpacked record like c/c++type bit = 0..1;BitsInaByte = bitpacked record bit0 : bit; bit1 : bit; bit2 : bit; bit3 : bit; bit4 : bit; bit5 : bit; bit6 : bit; bit7 : bit; end;
Could you, ttomas, or someone else give a short sample code to explain the use of this.
QuoteCould you, ttomas, or someone else give a short sample code to explain the use of this.This is mostly useful for low level access (for example, if you access certain hardware or writing a kernel) or data exchange that requires exact bit structure. With this, you don't need to play with bitwise operators in your code.
And, can you give a very short example-code?
const PageDirVirtAddr = $FFBFF000; PageTableVirtAddr = $FFC00000;type UBit3 = 0..(1 shl 3) - 1; UBit20 = 0..(1 shl 20) - 1; PPageTableEntry = ^TPageTableEntry; TPageTableEntry = bitpacked record Present, Writable, UserMode, WriteThrough, NotCacheable, Accessed, Dirty, AttrIndex, GlobalPage: Boolean; Avail: UBit3; FrameAddr: UBit20; end; PPageDirEntry = ^TPageDirEntry; TPageDirEntry = bitpacked record Present, Writable, UserMode, WriteThrough, NotCacheable, Accessed, Reserved, PageSize, GlobalPage: Boolean; Avail: UBit3; TableAddr: UBit20; end; PPageTable = ^TPageTable; TPageTable = array [0..1023] of TPageTableEntry; PPageDir = ^TPageDir; TPageDir = array [0..1023] of TPageDirEntry;...var PageDir: PPageDir = PPageDir(PageDirVirtAddr); PageTables: PPageTable = PPageTable(PageTableVirtAddr);...procedure Map(const va, pa: LongWord; const IsPresent, IsWritable, IsUserMode: Boolean);var VirtPage, ptIndex: LongWord;begin {$ifdef debug} WriteStrLn('Map $' + HexStr(va, 8) + ' to $' + HexStr(pa, 8)); {$endif} VirtPage := va div PageSize; ptIndex := PageDirIndex(VirtPage); // Find the appropriate page table for 'va' if PageDir^[ptIndex].TableAddr shl 12 = 0 then begin // The page table holding this page has not been created yet with PageDir^[ptIndex] do begin TableAddr := AllocPage shr 12; Present := True; Writable := True; end; FillByte(PageTables^[ptIndex * 1024], PageSize, 0); end; // Now that the page table definately exists, we can update the PTE with PageTables^[VirtPage] do begin FrameAddr := Align(pa, PageSize) shr 12; Present := IsPresent; Writable := IsWritable; UserMode := IsUserMode; end;end;
typedef union { struct { Bits queue :4; /* Network interface message queue */ /* Use value of type 'NI_Queue' */ Bits q_cmd :4; /* Network interface command with queue */ /* Use value of type 'NI_QueueCmd' */ Bits length :8; /* Length of the buffer to follow */ } q; /* Queue option */ struct { Byte cmd; /* Network interface command w/o queue */ /* Use value of type 'NI_NoQueueCmd' */ Byte length; /* Length of the buffer to follow */ } noq; /* No queue option */ } NI_Hdr; typedef struct { Bits tag :4; /* Message tag for implicit addressing */ /* Magic cookie for explicit addressing */ Bits auth :1; /* 1 => Authenticated */ Bits st :2; /* Service Type - see 'ServiceType' */ Bits msg_type :1; /* 0 => explicit message */ /* or unprocessed NV */ /*--------------------------------------------------------------------------*/ Bits response :1; /* 1 => Response, 0 => Other */ Bits pool :1; /* 0 => Outgoing */ Bits alt_path :1; /* 1 => Use path specified in 'path' */ /* 0 => Use default path */ Bits addr_mode :1; /* 1 => Explicit addressing, */ /* 0 => Implicit */ /* Outgoing buffers only */ Bits cmpl_code :2; /* Completion Code - see 'ComplType' */ Bits path :1; /* 1 => Use alternate path, */ /* 0 => Use primary path */ /* (if 'alt_path' is set) */ Bits priority :1; /* 1 => Priority message */ /*--------------------------------------------------------------------------*/ Byte length; /* Length of msg or NV to follow */ /* not including any explicit address */ /* field, includes code Byte or */ /* selector Bytes */ } ExpMsgHdr;
It was nightmare to convert to Delphi