#define __STDC_FORMAT_MACROS #include #include #include const char *yaml_def = "#schemaversion 3.0.0\n" "root:\n" " class: MemDev\n" " size: 32\n" " children:\n" " mmio:\n" " at: { nelms: 1 }\n" " class: MMIODev\n" " size: 32\n" " children:\n" " dut:\n" " class: GmpZField\n" " at: { offset: 0 }\n" " sizeBits: 32\n" " duts:\n" " class: GmpZField\n" " at: { offset: 0 }\n" " sizeBits: 32\n" " isSigned: true\n" " raw:\n" " class: IntField\n" " sizeBits: 32\n" " at: { offset: 0 }\n" ; class TestFailed {}; static void pr(mpz_t *m, int32_t v) { fprintf(stderr,"(MPZ: 0x"); mpz_out_str(stderr, 16, *m); fprintf(stderr," != 0x%"PRIx32")\n", v); } static void testRead(int32_t v, ScalVal raw, GmpZVal gmpu, GmpZVal gmps) { MPZ val; raw->setVal( v ); gmpu->getVal( &val.m_, 1 ); if ( (int32_t)mpz_get_si( val.m_ ) != v ) { pr( &val.m_, v ); throw TestFailed(); } gmps->getVal( &val.m_, 1 ); if ( (int32_t)mpz_get_si( val.m_ ) != v ) { fprintf(stderr,"comparison (signed) failed (%"PRId32")\n", v); throw TestFailed(); } } static void testWrite(int32_t v, ScalVal raw, GmpZVal gmpu, GmpZVal gmps) { MPZ val; int32_t rval; mpz_set_si( val.m_, v ); gmpu->setVal( &val.m_, 1 ); raw->getVal( (uint32_t*)&rval, 1 ); if ( v != rval ) { fprintf(stderr,"readback (unsigned) mismatch (exp 0x%"PRIx32", got 0x%"PRIx32"\n", v, rval); pr( &val.m_, v ); throw TestFailed(); } } int main() { Path r = IPath::loadYamlStream(yaml_def); ScalVal raw = IScalVal::create( r->findByName("mmio/raw") ); GmpZVal gmpu = IGmpZVal::create( r->findByName("mmio/dut") ); GmpZVal gmps = IGmpZVal::create( r->findByName("mmio/duts") ); int nelms = raw->getNelms(); uint32_t bits[nelms]; mpz_t theVal; mpz_init( theVal ); mpz_set_si( theVal, 2 ); bits[0] = 2; raw->setVal(bits, nelms); testRead( 2, raw, gmpu, gmps ); testRead(-2, raw, gmpu, gmps ); testWrite(33, raw, gmpu, gmps ); testWrite(-33, raw, gmpu, gmps ); }