add the memory source

This commit is contained in:
Lephenixnoir 2022-06-24 20:26:26 +01:00
parent 9f1d806af8
commit 5c834580c1
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
6 changed files with 83 additions and 4 deletions

View File

@ -240,7 +240,7 @@ static void heditor_poly_render(void *e0, int x, int y)
for(int i = 0; i < e->visible_lines; i++) {
int line_y = y + line_height * i;
dprint(x, line_y, C_BLACK, "%08X:", offset);
dprint(x, line_y, C_BLACK, "%08X:", offset + s->address_base);
int bytes_x = x + 85;
int ascii_x = x + 250;

View File

@ -399,6 +399,7 @@ void hex_view(void)
char const *str = jinput_value(goto_input);
int mode = (*str == '+') ? 1 : (*str == '-') ? -1 : 0;
int target = strtoul(str + (mode != 0), NULL, 16);
target -= app.source->address_base;
heditor_move_to(mem, mode ? mem->cursor + target * mode : target);
}
if(e.type == JINPUT_VALIDATED || e.type == JINPUT_CANCELED) {

View File

@ -104,6 +104,7 @@ source_t *source_lazy_file_open(char const *path)
if(!source) goto fail;
source->cap = SOURCE_WRITE;
source->address_base = 0;
return source;
fail:

View File

@ -155,6 +155,7 @@ source_t *source_loaded_file_new(void)
source->no_origin = true;
source->cap = SOURCE_WRITE | SOURCE_RESIZE | SOURCE_SAVEAS;
source->address_base = 0;
return source;
fail:

View File

@ -1,5 +1,8 @@
#include "source.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gint/defs/util.h>
static memory_region_t REGION_ROM = {
.start = 0x80000000,
@ -10,7 +13,7 @@ static memory_region_t REGION_ROM = {
static memory_region_t REGION_RAM = {
.start = 0x8c000000,
.end = 0x8c800000,
.writable = false,
.writable = true,
.name = "RAM",
};
static memory_region_t REGION_ILRAM = {
@ -28,7 +31,7 @@ static memory_region_t REGION_XYRAM = {
static memory_region_t REGION_RS = {
.start = 0xfd800000,
.end = 0xfd800800,
.writable = true,
.writable = false,
.name = "RS",
};
@ -43,7 +46,78 @@ memory_region_t *memory_all_regions[] = {
// TODO: Add offset to source + heditor view so we can see real memory addresses
static ssize_t mr_read(void *_cookie, void *buf, off_t offset, size_t size)
{
memory_region_t *mr = _cookie;
int mr_size = mr->end - mr->start;
if(offset < 0 || offset >= mr_size)
return -1;
size = min(size, mr_size - offset);
memcpy(buf, (void *)mr->start + offset, size);
return size;
}
static bool mr_write(void *_cookie, void *data, size_t data_size, off_t offset,
size_t segment_size)
{
memory_region_t *mr = _cookie;
int mr_size = mr->end - mr->start;
if(data_size != segment_size)
return false;
if(offset < 0 || offset >= mr_size)
return -1;
data_size = min(data_size, mr_size - offset);
memcpy((void *)mr->start + offset, data, data_size);
return true;
}
static size_t mr_size(void *_cookie)
{
memory_region_t *mr = _cookie;
return mr->end - mr->start;
}
static bool mr_dirty(void *_cookie)
{
(void)_cookie;
return false;
}
static bool mr_save(void *_cookie, char const *outfile)
{
(void)_cookie;
return (outfile == NULL);
}
static void mr_close(void *_cookie)
{
(void)_cookie;
}
static source_intf_t mr_intf = {
.name = "Memory",
.close_on_return_to_menu = false,
.read = mr_read,
.write = mr_write,
.size = mr_size,
.dirty = mr_dirty,
.save = mr_save,
.close = mr_close,
};
source_t *source_memory_open(memory_region_t *region)
{
return NULL;
char path[128];
sprintf(path, "%s (%08X)", region->name, region->start);
source_t *source = source_open(&mr_intf, region, path);
if(!source)
return NULL;
source->cap = region->writable ? SOURCE_WRITE : 0;
source->address_base = region->start;
return source;
}

View File

@ -55,6 +55,8 @@ typedef struct {
int cap;
/* Source name. Can be any string: file path, "memory at X", etc. */
char *origin;
/* Starting offset for display and addressing within the source */
uint32_t address_base;
/* If set, there is no origin and the "SAVE" button is disabled (only
useful if "SAVE AS" is enabled) */
bool no_origin;