/* * Output in ascii-hex file format. */ #include #include #include #include #include "rasm.h" #include "output.h" struct hole_entry *holes = NULL; void clear_hole_list() { struct hole_entry *e = holes; struct hole_entry *t; while (e) { t = e->next; free(e); e = t; } holes = NULL; } void add_hole(int start, int end) { struct hole_entry *n; n = (struct hole_entry *)malloc(sizeof(struct hole_entry)); n->start = start; n->end = end; n->next = holes; holes = n; } int is_hole(int from, int to) { /* Assume holes are disjoint */ struct hole_entry *e = holes; while (e) { if (from >= e->start && to <= e->end) return 1; e = e->next; } return 0; } void write_memory_to_obj(char *outfile, unsigned char *data, int data_size) { int fd, addr = 0; unsigned char cksum; FILE *out; fd = open(outfile, O_WRONLY | O_CREAT, 0666); if (fd < 0) { perror("opening output file"); exit(1); } ftruncate(fd, 0); out = fdopen(fd, "w"); if (out == NULL) { perror("opening output file stream"); exit(1); } while (addr < data_size) { int i, bytes_to_write = MIN(16, data_size - addr); int line_end = addr + bytes_to_write - 1; if (is_hole(addr, line_end)) { addr += bytes_to_write; continue; } fprintf(out, ":%02X", bytes_to_write); cksum = bytes_to_write; fprintf(out, "%04X00", addr); cksum += (addr & 0xff) + ((addr & 0xff00) >> 8); for (i=0; i