Skip to content

Commit

Permalink
Code gardening and documentation update.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Oct 16, 2023
1 parent 9e2adfc commit ee5c794
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 147 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ set(etripator_HDR
jsonhelpers.h
decode.h
section.h
section/load.h
section/save.h
opcodes.h
comment.h
label.h
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ The supported fields are :
* **mpr** : an array containing the page value for each memory page register.

* **data** : an object with 2 entries :
* **type** : **binary**, **hex** or **string**.
* **element_size** *(default value: 1)* : element size in bytes. The only supported values are 1 or 2.
* **type** : **binary**, **hex**, **string** or **jumptable**.
* **element_size** *(default value: 1 or 2 for **jumptable**)* : element size in bytes. The only supported values are 1 or 2.
* **elements_per_line** *(default value: 16)* : number of elements per line.

* **description** *(optional)*: description as a string or an array of strings for multiline description.

There must be only one occurence of each field per section.
Expand Down
1 change: 0 additions & 1 deletion cli/etripator.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <rom.h>
#include <ipl.h>
#include <section.h>
#include <section/load.h>
#include <comment.h>

#include "options.h"
Expand Down
145 changes: 77 additions & 68 deletions rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,91 @@
* You should have received a copy of the GNU General Public License
* along with Etripator. If not, see <http://www.gnu.org/licenses/>.
*/
#include "message.h"
#include "rom.h"
#include "message.h"

static int rom_load_data(const char *filename, memmap_t *map) {
int ret = 0;
size_t size = 0;
FILE *in;

/* Load ROM from file. */
int rom_load(const char* filename, memmap_t* map) {
FILE *in;
int i;
size_t size;
size_t count, nread;
/* Open file */
in = fopen(filename, "rb");
if(!in) {
if (in == NULL) {
ERROR_MSG("Unable to open %s : %s", filename, strerror(errno));
return 0;
}
/* Compute file size. */
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
size -= ftell(in);
/* Check size */
if(!size) {
ERROR_MSG("Empty file: %s", filename);
goto err_0;
}
/* Check for possible header */
if(size & 0x200) {
/* Jump header */
size &= ~0x200;
if(fseek(in, 0x200, SEEK_SET)) {
ERROR_MSG("Failed to jump rom header in %s: %s", filename, strerror(errno));
goto err_0;
}
}
/* Allocate rom storage */
if(!mem_create(&map->mem[PCE_MEM_ROM], (size + 0x1fff) & ~0x1fff)) {
ERROR_MSG("Failed to allocate ROM storage : %s", strerror(errno));
goto err_0;
}
/* Fill rom with 0xff */
memset(map->mem[PCE_MEM_ROM].data, 0xff, map->mem[PCE_MEM_ROM].len);
/* Read ROM data */
count = (size < map->mem[PCE_MEM_ROM].len) ? size : map->mem[PCE_MEM_ROM].len;
nread = fread(map->mem[PCE_MEM_ROM].data, 1, count, in);
if(nread != count) {
ERROR_MSG("Failed to read ROM data from %s : %s", filename, strerror(errno));
goto err_1;
}
fclose(in);
/* Initialize ROM pages. */
if(map->mem[PCE_MEM_ROM].len == 0x60000) {
for(i=0; i<64; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[(i & 0x1f) * 8192];
}
for(i=64; i<128; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[((i & 0x0f) + 32) * 8192];
}
}
else if(map->mem[PCE_MEM_ROM].len == 0x80000) {
for(i=0; i<64; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[(i & 0x3f) * 8192];
} else {
/* Compute file size. */
struct stat infos;
int fd = fileno(in);
if (fd < 0) {
ERROR_MSG("Failed to retrieve file descriptior for %s : %s", filename, strerror(errno));
} else if (fstat(fd, &infos) < 0) {
ERROR_MSG("Failed to retrieve file informations of %s : %s", filename, strerror(errno));
} else if (infos.st_size == 0) {
ERROR_MSG("Empty file: %s", filename);
} else {
size = infos.st_size;
/* Check for possible header */
if (size & 0x200) {
/* Jump header */
size &= ~0x200;
if (fseek(in, 0x200, SEEK_SET)) {
ERROR_MSG("Failed to jump rom header in %s: %s", filename, strerror(errno));
}
}
}
for(i=64; i<128; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[((i & 0x1f) + 32) * 8192];
if(size) {
/* Allocate rom storage */
if (!mem_create(&map->mem[PCE_MEM_ROM], (size + 0x1fff) & ~0x1fff)) {
ERROR_MSG("Failed to allocate ROM storage : %s", strerror(errno));
} else {
/* Fill rom with 0xff */
memset(map->mem[PCE_MEM_ROM].data, 0xff, map->mem[PCE_MEM_ROM].len);
/* Read ROM data */
size_t count = (size < map->mem[PCE_MEM_ROM].len) ? size : map->mem[PCE_MEM_ROM].len;
size_t nread = fread(map->mem[PCE_MEM_ROM].data, 1, count, in);
if (nread != count) {
ERROR_MSG("Failed to read ROM data from %s : %s", filename, strerror(errno));
} else {
ret = 1;
}
}
}
fclose(in);
}
else {
for(i=0; i<128; i++) {
uint8_t bank = (uint8_t)(i % (map->mem[PCE_MEM_ROM].len / 8192));
map->page[i] = &map->mem[PCE_MEM_ROM].data[bank * 8192];
return ret;
}

/* Load ROM from file. */
int rom_load(const char *filename, memmap_t *map) {
FILE *in;
int ret = 0;
if(rom_load_data(filename, map) == 0) {
mem_destroy(&map->mem[PCE_MEM_ROM]);
} else {
unsigned int i;
/* Initialize ROM pages. */
if (map->mem[PCE_MEM_ROM].len == 0x60000) {
for (i = 0; i < 64; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[(i & 0x1f) * 8192];
}
for (i = 64; i < 128; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[((i & 0x0f) + 32) * 8192];
}
} else if (map->mem[PCE_MEM_ROM].len == 0x80000) {
for (i = 0; i < 64; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[(i & 0x3f) * 8192];
}
for (i = 64; i < 128; i++) {
map->page[i] = &map->mem[PCE_MEM_ROM].data[((i & 0x1f) + 32) * 8192];
}
} else {
for (i = 0; i < 128; i++) {
uint8_t bank = (uint8_t)(i % (map->mem[PCE_MEM_ROM].len / 8192));
map->page[i] = &map->mem[PCE_MEM_ROM].data[bank * 8192];
}
}
ret = 1;
}
return 1;
err_1:
mem_destroy(&map->mem[PCE_MEM_ROM]);
err_0:
fclose(in);
return 0;
return ret;
}
20 changes: 20 additions & 0 deletions section.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,24 @@ void section_sort(section_t *ptr, size_t n);
*/
void section_delete(section_t *ptr, int n);

/**
* Load sections from a JSON file.
* \param [in] filename Input filename.
* \param [out] out Loaded sections.
* \param [out] n Number of loaded sections.
* \return 1 if the sections contained in the file were succesfully loaded.
* 0 if an error occured.
*/
int section_load(const char *filename, section_t **out, int *n);

/**
* Save sections to a JSON file.
* \param [in] filename Output filename.
* \param [in] ptr Sections to be saved.
* \param [in] count Number of sections.
* \return 1 if the sections were succesfully saved.
* 0 if an error occured.
*/
int section_save(const char *filename, section_t *ptr, int n);

#endif // ETRIPATOR_SECTION_H
2 changes: 1 addition & 1 deletion section/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with Etripator. If not, see <http://www.gnu.org/licenses/>.
*/
#include "load.h"
#include "../section.h"

#include <errno.h>
#include <stdlib.h>
Expand Down
33 changes: 0 additions & 33 deletions section/load.h

This file was deleted.

2 changes: 1 addition & 1 deletion section/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with Etripator. If not, see <http://www.gnu.org/licenses/>.
*/
#include "save.h"
#include "../section.h"

#include "../jsonhelpers.h"
#include "../message.h"
Expand Down
33 changes: 0 additions & 33 deletions section/save.h

This file was deleted.

5 changes: 2 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_test(NAME section_tests
COMMAND $<TARGET_FILE:section_tests>
WORKING_DIRECTORY $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>)
set_target_properties(section_tests PROPERTIES C_STANDARD 11)
add_custom_command(TARGET section_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/data $<TARGET_FILE_DIR:section_tests>/data)

#[[
add_executable(label_tests label.c)
Expand All @@ -20,6 +22,3 @@ add_test(NAME label_tests
COMMAND $<TARGET_FILE:label_tests>)
set_target_properties(label_tests PROPERTIES C_STANDARD 11)
#]]

add_custom_command(TARGET section_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/data $<TARGET_FILE_DIR:section_tests>/data)
2 changes: 0 additions & 2 deletions test/section.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <munit.h>
#include "section.h"
#include "section/load.h"
#include "section/save.h"
#include "message.h"
#include "message/console.h"

Expand Down

0 comments on commit ee5c794

Please sign in to comment.