Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: [C related] Structs, records, pointers...

  1. #1

    [C related] Structs, records, pointers...

    I'm a bit in stuck... I need to convert following code to pascal:

    Code:
    #define OAM_SUB       ((uint16*)0x07000400)
    
    SpriteEntry OAMCopySub[128]; 
    void updateOAM(void) {
    	unsigned int i;
    	for&#40;i = 0; i < 128 * sizeof&#40;SpriteEntry&#41; / 4 ; i++&#41;
    	&#123;
    		&#40;&#40;uint32*&#41;OAM_SUB&#41;&#91;i&#93; = &#40;&#40;uint32*&#41;OAMCopySub&#41;&#91;i&#93;;
    	&#125;
    &#125;
    where SpriteEntry is a struct. OAM_SUB is translated as
    [pascal]const
    OAM_SUB : puint16 = pointer($07000400);[/pascal]

    The problem is that I can't figure a way to typecast hte array of record to OAM_SUB :scratch:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  2. #2

    [C related] Structs, records, pointers...

    You can do something like this:

    Code:
    type
      PInt32 = ^integer;
    var
      Data &#58; array&#91;0..10&#93; of TMyRecord;
    begin
      PInt32&#40;@Data&#41;^ &#58;= 42;
    end;
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  3. #3

    Re: [C related] Structs, records, pointers...

    Quote Originally Posted by Legolas
    I'm a bit in stuck... I need to convert following code to pascal:

    Code:
    #define OAM_SUB       &#40;&#40;uint16*&#41;0x07000400&#41;
    
    SpriteEntry OAMCopySub&#91;128&#93;; 
    void updateOAM&#40;void&#41; &#123;
    	unsigned int i;
    	for&#40;i = 0; i < 128 * sizeof&#40;SpriteEntry&#41; / 4 ; i++&#41;
    	&#123;
    		&#40;&#40;uint32*&#41;OAM_SUB&#41;&#91;i&#93; = &#40;&#40;uint32*&#41;OAMCopySub&#41;&#91;i&#93;;
    	&#125;
    &#125;
    where SpriteEntry is a struct. OAM_SUB is translated as
    [pascal]const
    OAM_SUB : puint16 = pointer($07000400);[/pascal]

    The problem is that I can't figure a way to typecast hte array of record to OAM_SUB :scratch:
    declare stuff like this:

    Code:
    OAMCopySub = packed record or something
    
    POAMCopySub = ^OAMCopySub; // a pointer variant
    then use POAMCopySub in variable and set it to that pointer.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  4. #4

    [C related] Structs, records, pointers...

    Thanks to all!
    Now I'm @ work, so I'll try later
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  5. #5

    [C related] Structs, records, pointers...

    Ok, I have found a solution that works, or at least that compiles :roll:
    I have tried this "monster":
    [pascal] for i := 0 to (128 * sizeof(SpriteEntry) div 4) - 1 do
    pu32((@OAM_SUB[i])^) := pu32((@OAMCopySub[i])^);[/pascal]

    As I have said before, it compiles, though it does not work as expected, because I get corrupted sprites. Hopefully it should be a wrong (or missing) typecast somewhere...
    [size=9px]Bug hunting time[/size] :rambo:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  6. #6

    [C related] Structs, records, pointers...

    pointer wrestling in C is bad enough, translating such code is one of worst tasks you can get, good luck :/
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  7. #7

    [C related] Structs, records, pointers...

    I'm not familiar with GBA (?) types, so I'll use the regular Delphi ones....

    Code:
    var
      OAMCopySub&#58; array&#91;0..127&#93; of SpriteEntry;
    
    ...
    
    procedure UpdateOAM;
    type
      PLongs ^TLongs;
      TLongs = array&#91;0..MAXWORD-1&#93; of Longword;
    begin
      for i &#58;= 0 to &#40;128 * SizeOf&#40;SpriteEntry&#41; div SizeOf&#40;Longword&#41;&#41; -1 do
      begin
        PLongs&#40;OAM_SUB&#41;^&#91;i&#93; &#58;= PLongs&#40;@OAMCopySub&#41;^&#91;i&#93;;  
      end;
    end;
    if pu32 is ^Longword, then u32 should be a Longword and thus all you have to do is replace Longword by u32

    on the other hand if you have a "Move" procedure at your disposal you could as well use it.....

    Code:
    Move&#40;OAMCopySub, OAM_SUB^, SizeOf&#40;OAMCopySub&#41;&#41;;
    I think this is the easiest solution if it can be done...

  8. #8

    [C related] Structs, records, pointers...

    Yes, in fact it is NDS code :mrgreen:

    Move could be a solution, but it works only on Dualis emulator; on real hardware the memory is not copied at all. To tell short, I have decided to replace this loop by a dma copy, that takes care to copy the right amount of bytes at time.
    [pascal]dmaCopy(@OAMCopySub, OAM_SUB, sizeof(OAMCopySub));[/pascal]
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  9. #9

    [C related] Structs, records, pointers...

    I'm resuming this old thread because I need some other help
    Well, I have converted a struct to pascal but I have some alignment problems. The original code is this:
    Code:
    typedef struct sTransferRegion &#123;
      uint32 heartbeat;         
    	int16 touchX,   touchY;   
    	int16 touchXpx, touchYpx; 
    	int16 touchZ1,  touchZ2;  
    	uint16 tdiode1,  tdiode2; 
    	uint32 temperature;       
    	uint16 buttons;           
    	union &#123;
    		uint8 curtime&#91;8&#93;;       
        struct &#123;
    			u8 rtc_command;
    			u8 rtc_year;          
    			u8 rtc_month;         
    			u8 rtc_day;           
    
    			u8 rtc_incr;
    			u8 rtc_hours;         
    			u8 rtc_minutes;       
    			u8 rtc_seconds;       
        &#125;;
      &#125;;
    	uint16 battery;           
    	uint16 aux;               
    	pTransferSound soundData;
      vuint32 mailAddr;
      vuint32 mailData;
      vuint8 mailRead;
      vuint8 mailBusy;
      vuint32 mailSize;
    &#125; TransferRegion, * pTransferRegion;
    And this is my (bad) translation
    [pascal] sTransferRegion = record
    heartbeat: cuint32;
    touchX, touchY: cint16;
    touchXpx, touchYpx: cint16;
    touchZ1, touchZ2: cint16;
    tdiode1, tdiode2: cuint16;
    temperature: cuint32;
    buttons: cuint16;
    union: record
    case integer of
    0: (curtime: array [0..7] of cuint;
    1: (
    rtc_command: cuint8;
    rtc_year: cuint8;
    rtc_month: cuint8;
    rtc_day: cuint8;
    rtc_incr: cuint8;
    rtc_hours: cuint8;
    rtc_minutes: cuint8;
    rtc_seconds: cuint8;
    );
    end;
    battery: cuint16;
    aux: cuint16;
    soundData: pTransferSound;
    mailAddr: cuint32;
    mailData: cuint32;
    mailRead: cuint8;
    mailBusy: cuint8;
    mailSize: cuint32;
    end;
    TransferRegion = sTransferRegion;
    pTransferRegion = ^sTransferRegion;
    [/pascal]


    I think that the problem is in the union part... the nested record I have used is an ugly trick to avoid the limitation that forces to put variant parts as the last field in a record.
    Any better solution? :scratch:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  10. #10

    [C related] Structs, records, pointers...

    my first idea was that it should be a packed record but having a second look it seems to align just fine...

    edit: or wait a minute. won't the last field be scewed into the last 4 padded bytes?
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •