Skip to content

Instantly share code, notes, and snippets.

@cz172638
Created March 13, 2018 11:49
Show Gist options
  • Select an option

  • Save cz172638/473e675c4713473e077db2d99682a189 to your computer and use it in GitHub Desktop.

Select an option

Save cz172638/473e675c4713473e077db2d99682a189 to your computer and use it in GitHub Desktop.
playing with packed structure
#include <stdint.h>
#include <stdio.h>
/*
* TODO
* use _BYTE_ORDER_ macro for compile time directive
* to properly set structure and test it
*/
union idcode {
struct idcode_generic {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
uint8_t ver:4;
uint32_t pn:16; /* don't use uint16_t or int, must ask @ #tools */
uint8_t jep106parity:1;
uint8_t jep106cc:3;
uint8_t jep106mfg:7;
uint8_t jep106rao:1;
#else
uint8_t jep106rao:1;
uint8_t jep106mfg:7;
uint8_t jep106cc:3;
uint8_t jep106parity:1;
uint32_t pn:16; /* don't use uint16_t or int, must ask @ #tools */
uint8_t ver:4;
#endif
} fields;
uint32_t raw;
};
union cfg {
struct cfg_v5 {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
uint32_t rsvd:29;
uint8_t le:1;
uint8_t la:1;
uint8_t be:1;
#else
uint8_t be:1;
uint8_t la:1;
uint8_t le:1;
uint32_t rsvd:29;
#endif
} v5_fields;
struct cfg_v6 {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
uint16_t rsvd2:12;
uint8_t tarinc:4;
uint8_t rsvd1:4;
uint8_t err:4;
uint8_t darsize:4;
uint8_t rsvd0:1;
uint8_t le:1;
uint8_t la:1;
uint8_t be:1;
#else
uint8_t be:1;
uint8_t la:1;
uint8_t le:1;
uint8_t rsvd0:1;
uint8_t darsize:4;
uint8_t err:4;
uint8_t rsvd1:4;
uint8_t tarinc:4;
uint16_t rsvd2:12;
#endif
} v6_fields;
uint32_t raw;
};
struct jtag_ap_csw {
uint8_t srst_out:1;
uint8_t trst_out:1;
uint8_t srstconnected:1;
uint8_t portconnected:1;
uint32_t rsvd0:20;
uint8_t rfifocnt:3;
uint8_t rsvd1:1;
uint8_t wfifocnt:3;
uint8_t seract:1;
};
struct mem_ap_csw {
uint8_t size:3;
uint8_t rsvd0:1;
uint8_t addrinc:2;
uint8_t deviceen:1;
uint8_t trinprog:1;
uint8_t mode:4;
uint8_t type:4;
uint8_t errnpass:1;
uint8_t errstop:1;
uint8_t rsvd1:5;
uint8_t sdeviceen:1;
uint8_t prot:7;
uint8_t dbgswenable:1;
};
struct ahb3_ap_csw {
uint8_t size:3;
uint8_t rsvd0:1;
uint8_t addrinc:2;
uint8_t deviceen:1;
uint8_t trinprog:1;
uint8_t mode:4;
uint8_t type:4;
uint8_t errnpass:1;
uint8_t errstop:1;
uint8_t rsvd1:5;
uint8_t sdeviceen:1;
uint8_t hprot:3;
uint8_t hprot_allocate:1;
uint8_t hprot_mastertype:1;
uint8_t sbo:1;
uint8_t dbgswenable:1;
};
struct axi4_ap_csw {
uint8_t size:3;
uint8_t rsvd0:1;
uint8_t addrinc:2;
uint8_t deviceen:1;
uint8_t trinprog:1;
uint8_t mode:4;
uint8_t type:4;
uint8_t errnpass:1;
uint8_t errstop:1;
uint8_t rsvd1:5;
uint8_t sdeviceen:1;
uint8_t cache:4;
uint8_t prot_priv:1;
uint8_t prot_nonsec:1;
uint8_t prot_instr:1;
uint8_t dbgswenable:1;
};
union csw {
struct mem_ap_csw mem_fields;
struct jtag_ap_csw jtag_fields;
struct axi4_ap_csw axi4_fields;
struct ahb3_ap_csw ahb3_fields;
uint32_t raw;
};
int main()
{
union csw csw = { .raw = 0x00000001 };
union idcode idcode = { .raw = 0x5ba00477 };
union cfg cfg = { .raw = 0x00000006 };
printf("IDCODE is 0x%08x\n", idcode.raw);
printf("IDCODE.JEP106MFG is 0x%02x\n", idcode.fields.jep106mfg);
printf("IDCODE.JEP106CC is 0x%0x\n", idcode.fields.jep106cc);
printf("IDCODE.PART is 0x%0x\n", idcode.fields.pn);
printf("IDCODE.VER is 0x%0x\n", idcode.fields.ver);
printf("sizeof(idcode.fields) = %zu\n", sizeof(idcode.fields));
printf("CFG is 0x%08x\n", cfg.raw);
printf("CFG.BE is 0x%0x\n", cfg.v6_fields.be);
printf("CFG.LE is 0x%0x\n", cfg.v6_fields.le);
printf("CFG.LA is 0x%0x\n", cfg.v6_fields.la);
printf("sizeof(cfg.v6_fields) = %zu\n", sizeof(cfg.v6_fields));
printf("sizeof(uint32_t) = %zu\n", sizeof(uint32_t));
printf("sizeof(uint16_t) = %zu\n", sizeof(uint16_t));
printf("sizeof(uint8_t) = %zu\n", sizeof(uint8_t));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment