Skip to content

Commit

Permalink
Output rework WIP
Browse files Browse the repository at this point in the history
Add more unit tests
  • Loading branch information
BlockoS committed Jan 2, 2025
1 parent c6fdbdb commit 4d86257
Show file tree
Hide file tree
Showing 10 changed files with 1,048 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ set(etripator_SRC
rom.c
cd.c
ipl.c
output.c
# wla_dx.c
)

set(etripator_HDR
Expand All @@ -69,6 +71,8 @@ set(etripator_HDR
rom.h
cd.h
ipl.h
output.h
# wla_dx.h
)

add_library(etripator STATIC ${etripator_SRC} ${etripator_HDR})
Expand Down
2 changes: 1 addition & 1 deletion cli/etripator.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ static bool code_extract(FILE *out, SectionArray *arr, int index, MemoryMap *map
if(current->size <= 0) {
current->size = compute_size(arr, index, arr->count, map);
}
if (!label_extract(current, map, labels)) {
if (!label_extract(labels, map, current)) {
// ...
} else {
/* Process opcodes */
Expand Down
50 changes: 24 additions & 26 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,36 @@ static void print_label(FILE *out, Label *label) {
}

/* Finds any jump address from the current section. */
bool label_extract(Section *section, MemoryMap *map, LabelRepository *repository) {
bool label_extract(LabelRepository *labels, MemoryMap *map, Section *section) {
assert(labels != NULL);
assert(map != NULL);
assert(section != NULL);

int i;
uint8_t inst;
uint8_t data[6];

char buffer[32];

uint16_t logical;
uint8_t page;

const Opcode *opcode;
const size_t begin = section->logical;
const size_t end = begin + section->size;
size_t logical;
bool ret = true;

if (section->type != SECTION_TYPE_CODE) {
return 1;
}

/* Walk along section */
for (logical = section->logical; logical < (section->logical + section->size); logical += opcode->size) {
ret = false;
} else for (logical = begin; ret && (logical < end); ) {
/* Read instruction */
inst = memory_map_read(map, logical);
opcode = opcode_get(inst);
uint8_t inst = memory_map_read(map, logical);
const Opcode *opcode = opcode_get(inst);

uint8_t data[6] = {0};
uint16_t jump = 0;
/* Read data (if any) */
for (i = 0; i < (opcode->size - 1); i++) {
data[i] = memory_map_read(map, logical + i + 1);
}

if (opcode_is_local_jump(inst)) {
uint16_t jump;
int delta;
/* For BBR* and BBS* displacement is stored in the 2nd byte */
i = (((inst)&0x0F) == 0x0F) ? 1 : 0;
Expand All @@ -136,27 +139,22 @@ bool label_extract(Section *section, MemoryMap *map, LabelRepository *repository
jump = logical + delta;
page = memory_map_page(map, jump);
/* Create label name */
snprintf(buffer, 32, "l%04x_%02d", jump, page);

snprintf(buffer, sizeof(buffer), "l%04x_%02d", jump, page);
/* Insert offset to repository */
if (!label_repository_add(repository, buffer, jump, page, NULL)) {
return 0;
}
ret = label_repository_add(labels, buffer, jump, page, NULL);
INFO_MSG("%04x short jump to %04x (%02x)", logical, jump, page);
} else if (opcode_is_far_jump(inst)) {
uint16_t jump = data[0] | (data[1] << 8);
jump = data[0] | (data[1] << 8);
page = memory_map_page(map, jump);
/* Create label name */
snprintf(buffer, 32, "l%04x_%02d", jump, page);
snprintf(buffer, sizeof(buffer), "l%04x_%02d", jump, page);
/* Insert offset to repository */
if (!label_repository_add(repository, buffer, jump, page, NULL)) {
return 0;
}

ret = label_repository_add(labels, buffer, jump, page, NULL);
INFO_MSG("%04x long jump to %04x (%02x)", logical, jump, page);
}
logical += opcode->size;
}
return 1;
return ret;
}

static int data_extract_binary(FILE *out, Section *section, MemoryMap *map, LabelRepository *repository) {
Expand Down
8 changes: 5 additions & 3 deletions decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
#include "memory_map.h"
#include "comment.h"

// [todo] decoder struct

/// Finds any jump address from the current section.
/// \param [in] section Current section.
/// \param [in out] labels Label repository.
/// \param [in] map Memory map.
/// \param [in out] repository Label repository.
/// \param [in] section Current section.
/// \return true upon success.
/// \return false if an error occured.
bool label_extract(Section *section, MemoryMap *map, LabelRepository *repository);
bool label_extract(LabelRepository *labels, MemoryMap *map, Section *section);

/// Process data section. The result will be output has a binary file or an asm file containing hex values or strings.
/// \param [out] out File output.
Expand Down
3 changes: 3 additions & 0 deletions label.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ typedef struct {
uint16_t logical; //< Logical address
uint8_t page; //< Memory page
char* description; //< Description (optional)
uint32_t file_id; //< Id of the file in the output registry where the label was written
size_t line; //< Current line.
size_t column; //< Cursor position in the current line.
} Label;

/// Label repository.
Expand Down
Loading

0 comments on commit 4d86257

Please sign in to comment.