Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Oct 12, 2024
1 parent 30591e3 commit 15ebcba
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 432 deletions.
8 changes: 5 additions & 3 deletions comment/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ bool comment_repository_load(CommentRepository* repository, const char* filename
ERROR_MSG("Invalid or missing page.");
} else {
// text (same format as section/label description)
char* text = json_load_description (value, "text");
if(text == NULL) {
ERROR_MSG("Invalid or missing text.");
char* text = NULL;
if(json_load_description (value, "text", &text) != true) {
ERROR_MSG("Faile to retrieve text.");
} else if(text == NULL) {
ERROR_MSG("Empty text string");
} else if((num < 0) || (num > 0xFF)) {
ERROR_MSG("Page value out of range.");
} else if(comment_repository_add(repository, logical, (uint8_t)num, text)) {
Expand Down
4 changes: 2 additions & 2 deletions comment/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ bool comment_repository_save(CommentRepository* repository, const char* filename

bool ret = false;

int i, count = comment_repository_size(repository);
FILE *stream = fopen(filename, "wb");
if(stream == NULL) {
ERROR_MSG("Failed to open %s: %s", filename, strerror(errno));
} else {
int count = comment_repository_size(repository);
fprintf(stream, "[\n");
for(i=0; i<count; i++) {
for(int i=0; i<count; i++) {
Comment entry;
if(comment_repository_get(repository, i, &entry)) {
fprintf(stream, "\t{ \"logical\":\"%04x\", \"page\":\"%02x\", \"text\": ", entry.logical, entry.page);
Expand Down
92 changes: 52 additions & 40 deletions irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "irq.h"
#include "message.h"

#define PCE_IRQ_TABLE 0xfff6
#define PCE_IRQ_TABLE 0xFFF6U
#define PCE_IRQ_COUNT 5

static char* g_irq_names[PCE_IRQ_COUNT] = {
Expand All @@ -47,46 +47,58 @@ static char* g_irq_names[PCE_IRQ_COUNT] = {
"irq_reset"
};

/* Get irq code offsets from rom. */
int irq_read(memmap_t* map, section_t **section, int *count) {
int i;
uint8_t addr[2];
size_t filename_len;

uint16_t offset = PCE_IRQ_TABLE;
int j = *count;
section_t *tmp = (section_t*)realloc(*section, (j+PCE_IRQ_COUNT) * sizeof(section_t));
if(NULL == tmp) {
ERROR_MSG("Failed to allocate extra IRQ sections.");
return 0;
}
*section = tmp;
*count += PCE_IRQ_COUNT;

for(i=0; i<PCE_IRQ_COUNT; ++i) {
/* Read offset */
addr[0] = memmap_read(map, offset++);
addr[1] = memmap_read(map, offset++);
/* Initialize section */
tmp[j+i].name = strdup(g_irq_names[i]);
tmp[j+i].type = Code;
tmp[j+i].page = 0;
tmp[j+i].logical = (addr[1] << 8) | addr[0];
tmp[j+i].offset = 0;
tmp[j+i].size = 0;
memset(tmp[j+i].mpr, 0, 8);
tmp[j+i].mpr[0] = 0xff;
tmp[j+i].mpr[1] = 0xf8;

filename_len = strlen(g_irq_names[i]) + 5;
tmp[j+i].output = (char*)malloc(filename_len);
snprintf(tmp[j+i].output, filename_len, "%s.asm", g_irq_names[i]);
// [todo] add an extra section for the address table
// Get irq code offsets from rom.
bool irq_read(MemoryMap* map, Section **section, int *count) {
assert(map != NULL);
assert(section != NULL);
assert(count != NULL);

bool ret = false;
if(map->memory[PCE_MEMORY_ROM].data == NULL) {
ERROR_MSG("No ROM in memory map.");
} else if(map->memory[PCE_MEMORY_ROM].length < (PCE_IRQ_TABLE + PCE_IRQ_COUNT)) {
ERROR_MSG("ROM is abnormally small.");
} else {
Section *tmp = (Section*)realloc(*section, (*count+PCE_IRQ_COUNT) * sizeof(Section));
if(tmp == NULL) {
ERROR_MSG("Failed to allocate extra IRQ sections.");
} else {
int j = *count;
*section = tmp;
*count += PCE_IRQ_COUNT;

for(size_t i=0; i<PCE_IRQ_COUNT; i++, j++) {
// IRQ name.
const char *name = g_irq_names[i];
// Read offset.
uint8_t lo = memmap_read(map, offset++);
uint8_t hi = memmap_read(map, offset++);

// [todo] code_add....
// Initialize section
tmp[j].name = strdup(name);
tmp[j].type = Code;
tmp[j].page = 0;
tmp[j].logical = (hi << 8) | lo;
tmp[j].size = 0;

tmp[j+1].description = NULL;
tmp[j].mpr[0] = 0xFFU; // I/O
tmp[j].mpr[1] = 0xF8U; // RAM
for(int k=2; k<PCE_MPR_COUNT; k++) {
tmp[j].mpr[k] = 0x00; // ROM
}

INFO_MSG("%s found at %04x", tmp[j+i].name, tmp[j+i].logical);
filename_len = strlen(name) + 5U; // .asm\0
tmp[j].output = (char*)malloc(filename_len);
snprintf(tmp[j].output, filename_len, "%s.asm", name);

tmp[j].description = NULL;

INFO_MSG("%s found at %04x", tmp[j].name, tmp[j].logical);
}
}
}
return 1;

return ret;
}
41 changes: 32 additions & 9 deletions irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,39 @@
#define ETRIPATOR_IRQ_H

#include "config.h"
#include "section.h"
//#include "code.h"
#include "memorymap.h"

/**
* Get irq code offsets from rom.
* \param [in] map Memory map.
* \param [out] section Sections.
* \param [out] count Section count.
* \return 0 on error, 1 otherwise.
*/
int irq_read(memmap_t* map, section_t **section, int *count);
/// Section.
typedef struct Section {
struct Section *previous; ///< Previous section.
struct Section *next; ///< Next section.
uint8_t page; ///< Memory page.
uint16_t logical; ///< Logical address.
uint8_t mpr[8]; ///< MPR registers value.
int32_t size; ///< Size (in bytes).
char *output; ///< Output filename.
// [todo] function pointers
// [todo] bool decode(???)
} Section;

/*
status_code decode(Section *section, MemoryMap *map, LabelRepository *repository, CommentRepository *comments
, in: uint16_t current
, out: uint16_t *out
, in: void *payload
);
Section *section, MemoryMap *map, LabelRepository *repository, CommentRepository *comments => struct (opaque pointer)
status_code:
ERROR
OK
*/

/// Get irq code offsets from rom.
/// \param [in] map Memory map.
/// \param [out] section Section list.
/// \return true if the IRQ vectors where successfully extradcted.
/// \return false otherwise (missing ROM, or offsets out of range).
bool irq_read(MemoryMap* map, Section *section);

#endif // ETRIPATOR_IRQ_H
70 changes: 45 additions & 25 deletions jsonhelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
#include <errno.h>
#include <string.h>

int json_validate_int(const json_t* obj, int* out) {
int ret = 0;
bool json_validate_int(const json_t* obj, int* out) {
assert(obj != NULL);
assert(out != NULL);
bool ret = false;
if(json_is_string(obj)) {
const char *str = json_string_value(obj);
for(; (*str!='\0') && isspace(*str); str++) {
Expand All @@ -50,44 +52,62 @@ int json_validate_int(const json_t* obj, int* out) {
errno = 0;
*out = strtoul(str, NULL, 16);
if(errno == 0) {
ret = 1;
ret = true;
}
} else if(json_is_integer(obj)) {
*out = (int)json_integer_value(obj);
ret = 1;
ret = true;
}
return ret;
}

char* json_load_description(const json_t* obj, const char *key) {
json_t *tmp = json_object_get(obj, key);
char *out = NULL;
if(json_is_string(tmp)) {
out = strdup(json_string_value(tmp));
} else if (json_is_array(tmp)) {
int index;
json_t* value;
bool json_load_description(const json_t* obj, const char *key, char **out) {
assert(out != NULL);
assert(out && (*out == NULL));

size_t len = 0;
json_array_foreach(tmp, index, value) {
if(json_is_string(value)) {
const char *str = json_string_value(value);
if(out) {
out[len-1] = '\n';
bool ret = false;
char *buffer = NULL;
json_t *tmp = json_object_get(obj, key);

if(tmp == NULL) {
ret = true;
} else {
if(json_is_string(tmp)) {
buffer = strdup(json_string_value(tmp));
} else if (json_is_array(tmp)) {
int index;
json_t* value;
size_t len = 0;
json_array_foreach(tmp, index, value) {
if(json_is_string(value)) {
const char *str = json_string_value(value);
if(buffer != NULL) {
buffer[len-1] = '\n';
}
size_t n = len + strlen(str) + 1;
char *ptr = realloc(buffer, n);
if(ptr == NULL) {
free(buffer);
buffer = NULL;
break;
}
memcpy(ptr+len, str, strlen(str));
ptr[n-1] = '\0';
buffer = ptr;
len = n;
}
size_t n = len + strlen(str) + 1;
char *ptr = realloc(out, n);
memcpy(ptr+len, str, strlen(str));
ptr[n-1] = '\0';
out = ptr;
len = n;
}
}
ret = (buffer != NULL);
}
return out;
*out = buffer;
return ret;
}

void json_print_description(FILE *out, const char *key, const char *str) {
assert(out != NULL);
assert(key != NULL);
assert(str != NULL);
fprintf(out, "\"%s\":[", key);
while(*str) {
fprintf(out, "\n\t\t\t\"");
Expand Down
4 changes: 2 additions & 2 deletions jsonhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

#include <jansson.h>

int json_validate_int(const json_t* obj, int* out);
char* json_load_description(const json_t* obj, const char *key);
bool json_validate_int(const json_t* obj, int* out);
bool json_load_description(const json_t* obj, const char *key, char **out);
void json_print_description(FILE *out, const char *key, const char *str);

#endif // ETRIPATOR_JSON_HELPERS_H
6 changes: 4 additions & 2 deletions label/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ bool label_repository_load(LabelRepository* repository, const char* filename) {
ERROR_MSG("Invalid or missing page.");
} else {
// description
char* description = json_load_description(value, "description");
if((num < 0) || (num > 0xff)) {
char* description = NULL;
if(json_load_description(value, "description", &description) != true) {
ERROR_MSG("Failed to load label description");
} else if((num < 0) || (num > 0xff)) {
ERROR_MSG("Page value out of range.");
} else if(label_repository_add(repository, key, logical, (uint8_t)num, description)) {
ret = true;
Expand Down
37 changes: 20 additions & 17 deletions label/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,30 @@
#include "../jsonhelpers.h"
#include "../label.h"

/* Save labels to file. */
int label_repository_save(const char* filename, label_repository_t* repository) {
// Save labels to file
bool label_repository_save(LabelRepository* repository, const char* filename) {
bool ret = false;
FILE *stream = fopen(filename, "wb");
int i, count = label_repository_size(repository);
if(stream == NULL) {
ERROR_MSG("Failed to open %s: %s", filename, strerror(errno));
return 0;
}
fprintf(stream, "[\n");
for(i=0; i<count; i++) {
label_t label;
if(label_repository_get(repository, i, &label)) {
fprintf(stream, "\t{ \"name\":\"%s\", \"logical\":\"%04x\", \"page\":\"%02x\"", label.name, label.logical, label.page);
if(label.description) {
fputc(',', stream);
json_print_description(stream, "description", label.description);
} else {
int count = label_repository_size(repository);

fprintf(stream, "[\n");
for(i=0; i<count; i++) {
label_t label;
if(label_repository_get(repository, i, &label)) {
fprintf(stream, "\t{ \"name\":\"%s\", \"logical\":\"%04x\", \"page\":\"%02x\"", label.name, label.logical, label.page);
if(label.description) {
fputc(',', stream);
json_print_description(stream, "description", label.description);
}
fprintf(stream,"}%c\n", (i<(count-1)) ? ',' : ' ');
}
fprintf(stream,"}%c\n", (i<(count-1)) ? ',' : ' ');
}
fprintf(stream, "]\n");
fclose(stream);
ret = true;
}
fprintf(stream, "]\n");
fclose(stream);
return 1;
return ret;
}
Loading

0 comments on commit 15ebcba

Please sign in to comment.