<commit_before>#include "rom.h"

#include "memmap.h"
#include "core.h"

#include "../state.h"

#define ADDR_TO_IDX(_addr, _bot) ((_addr - _bot) >> 2)
static uint32_t rom[ROMSIZE >> 2] = {0};

EXPORT void flash_ROM(void *image, uint32_t nbytes) {
	memcpy(rom, image, nbytes);
	INFO("Flashed %d bytes to ROM\n", nbytes);
}

static bool rom_read(uint32_t addr, uint32_t *val) {
#ifdef DEBUG1
	assert((addr >= ROMBOT) && (addr < ROMTOP) && "CORE_rom_read");
#endif
	if ((addr >= ROMBOT) && (addr < ROMTOP) && (0 == (addr & 0x3))) {
		*val = SR(&rom[ADDR_TO_IDX(addr, ROMBOT)]);
	} else {
		CORE_ERR_invalid_addr(false, addr);
	}

	return true;
}

#ifdef WRITEABLE_ROM
static void rom_write(uint32_t addr, uint32_t val) {
	DBG2("ROM Write request addr %x (idx: %d)\n", addr, ADDR_TO_IDX(addr, ROMBOT));
#ifdef DEBUG1
	assert((addr >= ROMBOT) && (addr < ROMTOP) && "CORE_rom_write");
#endif
	if ((addr >= ROMBOT) && (addr < ROMTOP) && (0 == (addr & 0x3))) {
		SW(&rom[ADDR_TO_IDX(addr, ROMBOT)],val);
	} else {
		CORE_ERR_invalid_addr(true, addr);
	}
}
#endif

__attribute__ ((constructor))
void register_memmap_rom(void) {
#if (ROMBOT > ROMTOP)
#error "ROMBOT cannot be larger than ROMTOP"
#elif (ROMBOT == ROMTOP)
#pragma message ( "ROM of size 0. ROM has been disabled" )
#else
	union memmap_fn mem_fn;

	mem_fn.R_fn32 = rom_read;
	register_memmap(false, 4, mem_fn, ROMBOT, ROMTOP);
#ifdef WRITEABLE_ROM
	register_memmap(true, 4, rom_write, ROMBOT, ROMTOP);
#endif
#endif
}<commit_msg>rom: Fix union change for writeable rom case<commit_after>#include "rom.h"

#include "memmap.h"
#include "core.h"

#include "../state.h"

#define ADDR_TO_IDX(_addr, _bot) ((_addr - _bot) >> 2)
static uint32_t rom[ROMSIZE >> 2] = {0};

EXPORT void flash_ROM(void *image, uint32_t nbytes) {
	memcpy(rom, image, nbytes);
	INFO("Flashed %d bytes to ROM\n", nbytes);
}

static bool rom_read(uint32_t addr, uint32_t *val) {
#ifdef DEBUG1
	assert((addr >= ROMBOT) && (addr < ROMTOP) && "CORE_rom_read");
#endif
	if ((addr >= ROMBOT) && (addr < ROMTOP) && (0 == (addr & 0x3))) {
		*val = SR(&rom[ADDR_TO_IDX(addr, ROMBOT)]);
	} else {
		CORE_ERR_invalid_addr(false, addr);
	}

	return true;
}

#ifdef WRITEABLE_ROM
static void rom_write(uint32_t addr, uint32_t val) {
	DBG2("ROM Write request addr %x (idx: %d)\n", addr, ADDR_TO_IDX(addr, ROMBOT));
#ifdef DEBUG1
	assert((addr >= ROMBOT) && (addr < ROMTOP) && "CORE_rom_write");
#endif
	if ((addr >= ROMBOT) && (addr < ROMTOP) && (0 == (addr & 0x3))) {
		SW(&rom[ADDR_TO_IDX(addr, ROMBOT)],val);
	} else {
		CORE_ERR_invalid_addr(true, addr);
	}
}
#endif

__attribute__ ((constructor))
void register_memmap_rom(void) {
#if (ROMBOT > ROMTOP)
#error "ROMBOT cannot be larger than ROMTOP"
#elif (ROMBOT == ROMTOP)
#pragma message ( "ROM of size 0. ROM has been disabled" )
#else
	union memmap_fn mem_fn;

	mem_fn.R_fn32 = rom_read;
	register_memmap(false, 4, mem_fn, ROMBOT, ROMTOP);
#ifdef WRITEABLE_ROM
	mem_fn.W_fn32 = rom_write;
	register_memmap(true, 4, mem_fn, ROMBOT, ROMTOP);
#endif
#endif
}
{'fraction_non_alphanumeric': 0.1675496688741722, 'fraction_numerical': 0.019867549668874173, 'mean_word_length': 5.1030303030303035, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4752034', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1239, 'n_words': 313}