Skip to content

Commit

Permalink
Section update cont'd
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Dec 23, 2024
1 parent 358a2f9 commit b167e74
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 188 deletions.
110 changes: 53 additions & 57 deletions cli/etripator.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,11 @@ int main(int argc, const char **argv) {

MemoryMap map = {0};

Section *section = NULL;
int section_count = 0;

SectionArray section_arr = {0};

failure = 1;
section_count = 0;
section = NULL;

section_array_reset(&section_arr);

/* Extract command line options */
ret = cli_opt_get(&options, argc, argv);
Expand All @@ -164,7 +162,7 @@ int main(int argc, const char **argv) {

/* Read configuration file */
if (options.cfg_filename) {
ret = section_load(&section, &section_count, options.cfg_filename);
ret = section_load(&section_arr, options.cfg_filename);
if (!ret) {
ERROR_MSG("Unable to read %s", options.cfg_filename);
goto error_1;
Expand All @@ -186,7 +184,7 @@ int main(int argc, const char **argv) {

/* Get irq offsets */
if (options.extract_irq) {
ret = irq_read(&map, &section, &section_count);
ret = irq_read(&map, &section_arr);
if (!ret) {
ERROR_MSG("An error occured while reading irq vector offsets");
goto error_2;
Expand All @@ -201,7 +199,7 @@ int main(int argc, const char **argv) {
if (options.extract_irq) {
IPL ipl;
ret = ipl_read(&ipl, options.rom_filename);
ret = ret && ipl_sections(&ipl, &section, &section_count);
ret = ret && ipl_sections(&ipl, &section_arr);
if (!ret) {
ERROR_MSG("An error occured while setting up sections from IPL data.");
goto error_2;
Expand All @@ -210,7 +208,7 @@ int main(int argc, const char **argv) {
/* Data will be loaded during section disassembly */
}

section_sort(section, section_count);
// [todo] section_sort(section, section_count);

CommentRepository *comments_repository = comment_repository_create();
/* Load comments */
Expand All @@ -237,100 +235,103 @@ int main(int argc, const char **argv) {
}

/* For each section reset every existing files */
for (int i = 0; i < section_count; ++i) {
out = fopen(section[i].output, "wb");
for (int i = 0; i < section_arr.count; ++i) {
Section *section = &section_arr.data[i];
out = fopen(section->output, "wb");
if (NULL == out) {
ERROR_MSG("Can't open %s : %s", section[i].output, strerror(errno));
ERROR_MSG("Can't open %s : %s", section->output, strerror(errno));
goto error_4;
}
fclose(out);
}

/* Add section name to label repository. */
for (int i = 0; i < section_count; ++i) {
ret = label_repository_add(repository, section[i].name, section[i].logical, section[i].page, section[i].description);
for (int i = 0; i < section_arr.count; ++i) {
Section *section = &section_arr.data[i];
ret = label_repository_add(repository, section->name, section->logical, section->page, section->description);
if (!ret) {
ERROR_MSG("Failed to add section name (%s) to labels", section[i].name);
ERROR_MSG("Failed to add section name (%s) to labels", section->name);
goto error_4;
}
}

/* Disassemble and output */
for (int i = 0; i < section_count; ++i) {
out = fopen(section[i].output, "ab");
Section *previous = NULL;
Section *current = NULL;
for (int i = 0; i < section_arr.count; ++i, previous=current) {
current = &section_arr.data[i];
out = fopen(current->output, "ab");
if (!out) {
ERROR_MSG("Can't open %s : %s", section[i].output, strerror(errno));
ERROR_MSG("Can't open %s : %s", current->output, strerror(errno));
goto error_4;
}

if (options.cdrom || (section[i].offset != ((section[i].page << 13) | (section[i].logical & 0x1fff)))) {
size_t offset = section[i].offset;
if (options.cdrom || (current->offset != ((current->page << 13) | (current->logical & 0x1fff)))) {
size_t offset = current->offset;
/* Copy CDROM data */
ret = cd_load(options.rom_filename, section[i].offset, section[i].size, options.sector_size, section[i].page, section[i].logical, &map);
ret = cd_load(options.rom_filename, current->offset, current->size, options.sector_size, current->page, current->logical, &map);
if (0 == ret) {
ERROR_MSG("Failed to load CD data (section %d)", i);
goto error_4;
}
}

if((i > 0) && (section[i].logical < (section[i-1].logical + section[i-1].size))
&& (section[i].page == section[i-1].page)
&& (section[i].type != section[i-1].type)) {
WARNING_MSG("Section %s and %s overlaps! %x %x.%x", section[i].name, section[i-1].name);
if((previous != NULL) && (current->logical < (previous->logical + previous->size))
&& (current->page == previous->page)
&& (current->type != previous->type)) {
WARNING_MSG("Section %s and %s overlaps! %x %x.%x", current->name, previous->name);
}

if((i > 0) && (0 == strcmp(section[i].output, section[i-1].output))
&& (section[i].page == section[i-1].page)
&& (section[i].logical <= (section[i-1].logical + section[i-1].size))) {
if((previous != NULL) && (0 == strcmp(current->output, previous->output))
&& (current->page == previous->page)
&& (current->logical <= (previous->logical + previous->size))) {
// "Merge" sections and adjust size if necessary.
if(section[i].size > 0) {
uint32_t end0 = section[i-1].logical + section[i-1].size;
uint32_t end1 = section[i].logical + section[i].size;
if(current->size > 0) {
uint32_t end0 = previous->logical + previous->size;
uint32_t end1 = current->logical + current->size;
if(end1 > end0) {
section[i].size = end1 - end0;
section[i].logical = end0;
INFO_MSG("Section %s has been merged with %s!", section[i].name, section[i-1].name);
current->size = end1 - end0;
current->logical = end0;
INFO_MSG("Section %s has been merged with %s!", current->name, previous->name);
}
else {
// The previous section overlaps the current one.
// We skip it as it has already been processed.
fclose(out);
continue;
}
} else {
current->logical = previous->logical + previous->size;
INFO_MSG("Section %s has been merged with %s!", current->name, previous->name);
}
else {
section[i].logical = section[i-1].logical + section[i-1].size;
INFO_MSG("Section %s has been merged with %s!", section[i].name, section[i-1].name);
}
}
else if((section[i].type != SECTION_TYPE_DATA) || (section[i].data.type != DATA_TYPE_BINARY)) {
} else if((current->type != SECTION_TYPE_DATA) || (current->data.type != DATA_TYPE_BINARY)) {
/* Print header */
fprintf(out, "\t.%s\n"
"\t.bank $%03x\n"
"\t.org $%04x\n",
(section[i].type == SECTION_TYPE_CODE) ? "code" : "data", section[i].page, section[i].logical);
(current->type == SECTION_TYPE_CODE) ? "code" : "data", current->page, current->logical);
}

memory_map_mpr(&map, section[i].mpr);
memory_map_mpr(&map, current->mpr);

if (section[i].type == SECTION_TYPE_CODE) {
if(section[i].size <= 0) {
section[i].size = compute_size(section, i, section_count, &map);
if (current->type == SECTION_TYPE_CODE) {
if(current->size <= 0) {
current->size = compute_size(current, i, section_arr.count, &map);
}

/* Extract labels */
ret = label_extract(&section[i], &map, repository);
ret = label_extract(current, &map, repository);
if (!ret) {
goto error_4;
}
/* Process opcodes */
uint16_t logical = section[i].logical;
uint16_t logical = current->logical;
do {
(void)decode(out, &logical, &section[i], &map, repository, comments_repository, options.address);
} while (logical < (section[i].logical+section[i].size));
(void)decode(out, &logical, current, &map, repository, comments_repository, options.address);
} while (logical < (current->logical+current->size));
fputc('\n', out);
} else {
ret = data_extract(out, &section[i], &map, repository, comments_repository, options.address);
ret = data_extract(out, current, &map, repository, comments_repository, options.address);
if (!ret) {
// [todo]
}
Expand All @@ -351,7 +352,7 @@ int main(int argc, const char **argv) {
if (!options.cdrom && options.extract_irq) {
fprintf(main_file, "\n\t.data\n\t.bank 0\n\t.org $FFF6\n");
for (int i = 0; i < 5; ++i) {
fprintf(main_file, "\t.dw $%04x\n", section[i].logical);
fprintf(main_file, "\t.dw $%04x\n", current->logical);
}
}

Expand All @@ -372,12 +373,7 @@ int main(int argc, const char **argv) {
error_1:
cli_opt_release(&options);

for (int i = 0; i < section_count; ++i) {
free(section[i].name);
free(section[i].output);
section[i].name = NULL;
}
free(section);
section_array_delete(&section_arr);

return failure;
}
74 changes: 37 additions & 37 deletions ipl.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ bool ipl_read(IPL *out, const char *filename) {
}

// Get irq code offsets from IPL.
bool ipl_sections(IPL *in, Section **out, int *count) {
bool ipl_sections(IPL *in, SectionArray *out) {
assert(in != NULL);
assert(out != NULL);
assert(out->data != NULL);

static const char *section_name[2] = { "cd_start", "gfx_start" };
static const char *section_filename[2] = { "cd_start.asm", "gfx_start.bin" };

Expand All @@ -167,50 +171,46 @@ bool ipl_sections(IPL *in, Section **out, int *count) {
if(extra == 0) {
INFO_MSG("No section found from IPL data.");
} else {
int j = *count;
Section *section = (Section*)realloc(*out, (j+extra) * sizeof(Section));
if(section == NULL) {
ERROR_MSG("Failed to add extra sections.");
ret = false;
} else {
*count += extra;
*out = section;

memset(&section[j], 0, extra * sizeof(Section));

for(int k=0; k<extra; k++) {
section[j+k].mpr[0] = 0xff;
section[j+k].mpr[1] = 0xf8;
section[j+k].mpr[7] = 0x00;
for(int i=0; i<5; i++) {
section[j+k].mpr[2+i] = 0x80 + in->mpr[i];
}
size_t j = out->count;
for(int k=0; ret && (k<extra); k++) {
Section tmp = {0};
tmp.mpr[0] = 0xff;
tmp.mpr[1] = 0xf8;
tmp.mpr[7] = 0x00;
for(int i=0; i<5; i++) {
tmp.mpr[2+i] = 0x80 + in->mpr[i];
}
if(section_array_add(out, &tmp) < 0) {
ret = false;
}
}
if(ret) {
// "CD boot"
if(in->load_sector_count) {
Section *section = &out->data[j++];
uint32_t record = (in->load_start_record[0] << 16) | (in->load_start_record[1] << 8) | in->load_start_record[2];
section[j].name = strdup(section_name[0]);
section[j].type = SECTION_TYPE_CODE;
section[j].page = section[j].mpr[in->load_exec_address[1]>>5];
section[j].logical = (in->load_exec_address[1] << 8) | in->load_exec_address[0];
section[j].offset = record * 2048;
section[j].size = in->load_sector_count * 2048;
section[j].output = strdup(section_filename[0]);
j++;
section->name = strdup(section_name[0]);
section->type = SECTION_TYPE_CODE;
section->page = section->mpr[in->load_exec_address[1]>>5];
section->logical = (in->load_exec_address[1] << 8) | in->load_exec_address[0];
section->offset = record * 2048;
section->size = in->load_sector_count * 2048;
section->output = strdup(section_filename[0]);
}
// "GFX"
if(in->opening_gfx_sector_count) {
Section *section = &out->data[j++];
uint32_t record = (in->opening_gfx_record[0] << 16) | (in->opening_gfx_record[1] << 8) | in->opening_gfx_record[2];
section[j].name = strdup(section_name[1]);
section[j].type = SECTION_TYPE_DATA;
section[j].page = section[j].mpr[in->opening_gfx_read_address[1]>>5];
section[j].logical = (in->opening_gfx_read_address[1] << 8) | in->opening_gfx_read_address[0];
section[j].offset = record * 2048;
section[j].size = in->opening_gfx_sector_count * 2048;
section[j].output = strdup(section_filename[1]);
section[j].data.type = DATA_TYPE_BINARY;
section[j].data.element_size = 1;
section[j].data.elements_per_line = 16;
section->name = strdup(section_name[1]);
section->type = SECTION_TYPE_DATA;
section->page = section->mpr[in->opening_gfx_read_address[1]>>5];
section->logical = (in->opening_gfx_read_address[1] << 8) | in->opening_gfx_read_address[0];
section->offset = record * 2048;
section->size = in->opening_gfx_sector_count * 2048;
section->output = strdup(section_filename[1]);
section->data.type = DATA_TYPE_BINARY;
section->data.element_size = 1;
section->data.elements_per_line = 16;
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions ipl.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ bool ipl_read(IPL *out, const char *filename);

/// Get irq code offsets from IPL.
/// \param [in] in IPL infos.
/// \param [out] section Sections.
/// \param [out] count Section count.
/// \param [out] out Sections.
/// \return 0 on error, 1 otherwise.
bool ipl_sections(IPL *in, Section **out, int *count);
bool ipl_sections(IPL *in, SectionArray *out);

#endif // IPL_H
Loading

0 comments on commit b167e74

Please sign in to comment.