📄 ghwlib.c
字号:
list_p = list; while (1) { uint32_t d; /* Read delta to next signal. */ if (ghw_read_uleb128 (h, &d) < 0) return -1; if (d == 0) { /* Last signal reached. */ break; } /* Find next signal. */ while (d > 0) { i++; if (h->sigs[i].type != NULL) d--; } if (ghw_read_signal_value (h, &h->sigs[i]) < 0) return -1; if (list_p) *list_p++ = i; } if (list_p) *list_p = 0; return 0;}intghw_read_cycle_next (struct ghw_handler *h){ int64_t d_time; if (ghw_read_lsleb128 (h, &d_time) < 0) return -1; if (d_time == -1) return 0; h->snap_time += d_time; return 1;}intghw_read_cycle_end (struct ghw_handler *h){ char hdr[4]; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) return -1; if (memcmp (hdr, "ECY", 4)) return -1; return 0;}voidghw_disp_value (union ghw_val *val, union ghw_type *type){ switch (ghw_get_base_type (type)->kind) { case ghdl_rtik_type_b2: printf ("%s (%d)", type->en.lits[val->b2], val->b2); break; case ghdl_rtik_type_e8: printf ("%s (%d)", type->en.lits[val->e8], val->e8); break; case ghdl_rtik_type_i32: printf ("%d", val->i32); break; case ghdl_rtik_type_p64: printf ("%lld", val->i64); break; case ghdl_rtik_type_f64: printf ("%g", val->f64); break; default: fprintf (stderr, "ghw_disp_value: cannot handle type %d\n", type->kind); abort (); }}/* Put the ASCII representation of VAL into BUF, whose size if LEN. A NUL is always written to BUF.*/voidghw_get_value (char *buf, int len, union ghw_val *val, union ghw_type *type){ switch (ghw_get_base_type (type)->kind) { case ghdl_rtik_type_b2: if (val->b2 <= 1) { strncpy (buf, type->en.lits[val->b2], len - 1); buf[len - 1] = 0; } else { snprintf (buf, len, "?%d", val->b2); } break; case ghdl_rtik_type_e8: if (val->b2 <= type->en.nbr) { strncpy (buf, type->en.lits[val->e8], len - 1); buf[len - 1] = 0; } else { snprintf (buf, len, "?%d", val->e8); } break; case ghdl_rtik_type_i32: snprintf (buf, len, "%d", val->i32); break; case ghdl_rtik_type_p64: snprintf (buf, len, "%lld", val->i64); break; case ghdl_rtik_type_f64: snprintf (buf, len, "%g", val->f64); break; default: snprintf (buf, len, "?bad type %d?", type->kind); }}voidghw_disp_values (struct ghw_handler *h){ int i; for (i = 0; i < h->nbr_sigs; i++) { struct ghw_sig *s = &h->sigs[i]; if (s->type != NULL) { printf ("#%d: ", i); ghw_disp_value (s->val, s->type); printf ("\n"); } }}intghw_read_directory (struct ghw_handler *h){ unsigned char hdr[8]; int nbr_entries; int i; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) return -1; nbr_entries = ghw_get_i32 (h, &hdr[4]); if (h->flag_verbose) printf ("Directory (%d entries):\n", nbr_entries); for (i = 0; i < nbr_entries; i++) { unsigned char ent[8]; int pos; if (fread (ent, sizeof (ent), 1, h->stream) != 1) return -1; pos = ghw_get_i32 (h, &ent[4]); if (h->flag_verbose) printf (" %s at %d\n", ent, pos); } if (fread (hdr, 4, 1, h->stream) != 1) return -1; if (memcmp (hdr, "EOD", 4)) return -1; return 0;}intghw_read_tailer (struct ghw_handler *h){ unsigned char hdr[8]; int pos; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) return -1; pos = ghw_get_i32 (h, &hdr[4]); if (h->flag_verbose) printf ("Tailer: directory at %d\n", pos); return 0;}enum ghw_resghw_read_sm_hdr (struct ghw_handler *h, int *list){ unsigned char hdr[4]; int res; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) { if (feof (h->stream)) return ghw_res_eof; else return ghw_res_error; } if (memcmp (hdr, "SNP", 4) == 0) { res = ghw_read_snapshot (h); if (res < 0) return res; return ghw_res_snapshot; } else if (memcmp (hdr, "CYC", 4) == 0) { res = ghw_read_cycle_start (h); if (res < 0) return res; res = ghw_read_cycle_cont (h, list); if (res < 0) return res; return ghw_res_cycle; } else if (memcmp (hdr, "DIR", 4) == 0) { res = ghw_read_directory (h); } else if (memcmp (hdr, "TAI", 4) == 0) { res = ghw_read_tailer (h); } else { fprintf (stderr, "unknown GHW section %c%c%c%c\n", hdr[0], hdr[1], hdr[2], hdr[3]); return -1; } if (res != 0) return res; return ghw_res_other;}intghw_read_sm (struct ghw_handler *h, enum ghw_sm_type *sm){ int res; while (1) { /* printf ("sm: state = %d\n", *sm); */ switch (*sm) { case ghw_sm_init: case ghw_sm_sect: res = ghw_read_sm_hdr (h, NULL); switch (res) { case ghw_res_other: break; case ghw_res_snapshot: *sm = ghw_sm_sect; return res; case ghw_res_cycle: *sm = ghw_sm_cycle; return res; default: return res; } break; case ghw_sm_cycle: if (0) printf ("Time is %lld fs\n", h->snap_time); if (0) ghw_disp_values (h); res = ghw_read_cycle_next (h); if (res < 0) return res; if (res == 1) { res = ghw_read_cycle_cont (h, NULL); if (res < 0) return res; return ghw_res_cycle; } res = ghw_read_cycle_end (h); if (res < 0) return res; *sm = ghw_sm_sect; break; } }}intghw_read_cycle (struct ghw_handler *h){ int res; res = ghw_read_cycle_start (h); if (res < 0) return res; while (1) { res = ghw_read_cycle_cont (h, NULL); if (res < 0) return res; if (0) printf ("Time is %lld fs\n", h->snap_time); if (0) ghw_disp_values (h); res = ghw_read_cycle_next (h); if (res < 0) return res; if (res == 0) break; } res = ghw_read_cycle_end (h); return res;}intghw_read_dump (struct ghw_handler *h){ unsigned char hdr[4]; int res; while (1) { if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) { if (feof (h->stream)) return 0; else return -1; } if (memcmp (hdr, "SNP", 4) == 0) { res = ghw_read_snapshot (h); if (0 && res >= 0) ghw_disp_values (h); } else if (memcmp (hdr, "CYC", 4) == 0) { res = ghw_read_cycle (h); } else if (memcmp (hdr, "DIR", 4) == 0) { res = ghw_read_directory (h); } else if (memcmp (hdr, "TAI", 4) == 0) { res = ghw_read_tailer (h); } else { fprintf (stderr, "unknown GHW section %c%c%c%c\n", hdr[0], hdr[1], hdr[2], hdr[3]); return -1; } if (res != 0) return res; }}struct ghw_section ghw_sections[] = { { "\0\0\0", NULL }, { "STR", ghw_read_str }, { "HIE", ghw_read_hie }, { "TYP", ghw_read_type }, { "WKT", ghw_read_wk_types }, { "EOH", ghw_read_eoh }, { "SNP", ghw_read_snapshot }, { "CYC", ghw_read_cycle }, { "DIR", ghw_read_directory }, { "TAI", ghw_read_tailer }};intghw_read_section (struct ghw_handler *h){ unsigned char hdr[4]; int i; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) { if (feof (h->stream)) return -2; else return -1; } for (i = 1; i < sizeof (ghw_sections) / sizeof (*ghw_sections); i++) if (memcmp (hdr, ghw_sections[i].name, 4) == 0) return i; fprintf (stderr, "ghw_read_section: unknown GHW section %c%c%c%c\n", hdr[0], hdr[1], hdr[2], hdr[3]); return 0;}voidghw_close (struct ghw_handler *h){ if (h->stream) { fclose (h->stream); h->stream = NULL; }}const char *ghw_get_dir (int is_downto){ return is_downto ? "downto" : "to";}voidghw_disp_range (union ghw_range *rng){ switch (rng->kind) { case ghdl_rtik_type_i32: case ghdl_rtik_type_p32: printf ("%d %s %d", rng->i32.left, ghw_get_dir (rng->i32.dir), rng->i32.right); break; case ghdl_rtik_type_i64: case ghdl_rtik_type_p64: printf ("%lld %s %lld", rng->i64.left, ghw_get_dir (rng->i64.dir), rng->i64.right); break; case ghdl_rtik_type_f64: printf ("%g %s %g", rng->f64.left, ghw_get_dir (rng->f64.dir), rng->f64.right); break; default: printf ("?(%d)", rng->kind); }}voidghw_disp_type (struct ghw_handler *h, union ghw_type *t){ switch (t->kind) { case ghdl_rtik_type_b2: case ghdl_rtik_type_e8: { struct ghw_type_enum *e = &t->en; int i; printf ("type %s is (", e->name); for (i = 0; i < e->nbr; i++) { if (i != 0) printf (", "); printf ("%s", e->lits[i]); } printf (");"); if (e->wkt != ghw_wkt_unknown) printf (" -- WKT:%d", e->wkt); printf ("\n"); } break; case ghdl_rtik_type_i32: case ghdl_rtik_type_f64: { struct ghw_type_scalar *s = &t->sc; printf ("type %s is range <>;\n", s->name); } break; case ghdl_rtik_type_p32: case ghdl_rtik_type_p64: { int i; struct ghw_type_physical *p = &t->ph; printf ("type %s is range <> units\n", p->name); for (i = 0; i < p->nbr_units; i++) { struct ghw_unit *u = &p->units[i]; printf (" %s = %lld %s;\n", u->name, u->val, p->units[0].name); } printf ("end units\n"); } break; case ghdl_rtik_subtype_scalar: { struct ghw_subtype_scalar *s = &t->ss; printf ("subtype %s is ", s->name); ghw_disp_typename (h, s->base); printf (" range "); ghw_disp_range (s->rng); printf (";\n"); } break; case ghdl_rtik_type_array: { struct ghw_type_array *a = &t->ar; int i; printf ("type %s is array (", a->name); for (i = 0; i < a->nbr_dim; i++) { if (i != 0) printf (", "); ghw_disp_typename (h, a->dims[i]); printf (" range <>"); } printf (") of "); ghw_disp_typename (h, a->el); printf (";\n"); } break; case ghdl_rtik_subtype_array: case ghdl_rtik_subtype_array_ptr: { struct ghw_subtype_array *a = &t->sa; int i; printf ("subtype %s is ", a->name); ghw_disp_typename (h, (union ghw_type *)a->base); printf (" ("); for (i = 0; i < a->base->nbr_dim; i++) { if (i != 0) printf (", "); ghw_disp_range (a->rngs[i]); } printf (");\n"); } break; case ghdl_rtik_type_record: { struct ghw_type_record *r = &t->rec; int i; printf ("type %s is record\n", r->name); for (i = 0; i < r->nbr_fields; i++) { printf (" %s: ", r->el[i].name); ghw_disp_typename (h, r->el[i].type); printf ("\n"); } printf ("end record;\n"); } break; default: printf ("ghw_disp_type: unhandled type kind %d\n", t->kind); }}voidghw_disp_types (struct ghw_handler *h){ int i; for (i = 0; i < h->nbr_types; i++) ghw_disp_type (h, h->types[i]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -