Use psram for cache of ROM file, if available
This commit is contained in:
parent
c4734c7fc7
commit
7079e001e1
1 changed files with 41 additions and 39 deletions
|
|
@ -10,24 +10,51 @@
|
||||||
#include "hardware/sync.h"
|
#include "hardware/sync.h"
|
||||||
#include "emuapi.h"
|
#include "emuapi.h"
|
||||||
|
|
||||||
|
extern size_t _psram_size;
|
||||||
|
|
||||||
|
static bool using_psram;
|
||||||
|
|
||||||
|
#define PSRAM_BASE (0x11000000)
|
||||||
|
// #define PSRAM_BASE (0x15000000) // there's no reason to use uncached access
|
||||||
|
|
||||||
unsigned char * flash_start = (unsigned char *)(XIP_BASE + HW_FLASH_STORAGE_BASE);
|
unsigned char * flash_start = (unsigned char *)(XIP_BASE + HW_FLASH_STORAGE_BASE);
|
||||||
unsigned char * flash_end = (unsigned char *)(XIP_BASE + HW_FLASH_STORAGE_TOP);
|
unsigned char * flash_end = (unsigned char *)(XIP_BASE + HW_FLASH_STORAGE_TOP);
|
||||||
|
|
||||||
static uint8_t cache[FLASH_SECTOR_SIZE];
|
static uint8_t cache[FLASH_SECTOR_SIZE];
|
||||||
|
|
||||||
int flash_load(const char * filename)
|
static void flash_setup() {
|
||||||
|
if (_psram_size) {
|
||||||
|
using_psram = true;
|
||||||
|
flash_start = (unsigned char*)PSRAM_BASE;
|
||||||
|
flash_end = flash_start + _psram_size;
|
||||||
|
} else {
|
||||||
|
using_psram = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int flash_load_common(const char * filename, bool do_bswap)
|
||||||
{
|
{
|
||||||
uint32_t offset = HW_FLASH_STORAGE_BASE;
|
flash_setup();
|
||||||
|
|
||||||
|
uint8_t *dest = flash_start;
|
||||||
int n;
|
int n;
|
||||||
int size = 0;
|
int size = 0;
|
||||||
emu_printf("flash_load...");
|
emu_printf("flash_load...");
|
||||||
int f = emu_FileOpen(filename,"r+b");
|
int f = emu_FileOpen(filename,"r+b");
|
||||||
if (f) {
|
if (f) {
|
||||||
while ( (offset < (HW_FLASH_STORAGE_TOP-FLASH_SECTOR_SIZE)) && (n = emu_FileRead(cache,FLASH_SECTOR_SIZE,f) ) ) {
|
while ( (dest < (flash_end-FLASH_SECTOR_SIZE)) && (n = emu_FileRead(cache,FLASH_SECTOR_SIZE,f) ) ) {
|
||||||
//memcpy(cache, (unsigned char *)offset, n);
|
if (do_bswap) {
|
||||||
if (memcmp(cache, (unsigned char*)XIP_BASE + offset, n)) {
|
for (int i=0;i<n; i+=2) {
|
||||||
|
uint8_t k = cache[i];
|
||||||
|
cache[i]=cache[i+1];
|
||||||
|
cache[i+1] = k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (using_psram) {
|
||||||
|
memcpy(dest, cache, n);
|
||||||
|
} else if (memcmp(cache, dest, n)) {
|
||||||
uint32_t ints = save_and_disable_interrupts();
|
uint32_t ints = save_and_disable_interrupts();
|
||||||
|
uint32_t offset = dest - (uint8_t*)XIP_BASE;
|
||||||
flash_range_erase(offset, FLASH_SECTOR_SIZE);
|
flash_range_erase(offset, FLASH_SECTOR_SIZE);
|
||||||
flash_range_program(offset, (const uint8_t *)&cache[0], FLASH_SECTOR_SIZE);
|
flash_range_program(offset, (const uint8_t *)&cache[0], FLASH_SECTOR_SIZE);
|
||||||
restore_interrupts(ints);
|
restore_interrupts(ints);
|
||||||
|
|
@ -39,7 +66,7 @@ int flash_load(const char * filename)
|
||||||
//emu_printi(pt[2]);
|
//emu_printi(pt[2]);
|
||||||
//emu_printi(pt[3]);
|
//emu_printi(pt[3]);
|
||||||
}
|
}
|
||||||
offset += FLASH_SECTOR_SIZE;
|
dest += FLASH_SECTOR_SIZE;
|
||||||
size += n;
|
size += n;
|
||||||
}
|
}
|
||||||
emu_FileClose(f);
|
emu_FileClose(f);
|
||||||
|
|
@ -49,45 +76,20 @@ int flash_load(const char * filename)
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_load_bswap(const char * filename)
|
int flash_load(const char * filename)
|
||||||
{
|
{
|
||||||
uint32_t offset = HW_FLASH_STORAGE_BASE;
|
return flash_load_common(filename, false);
|
||||||
int n;
|
}
|
||||||
int size = 0;
|
|
||||||
emu_printf("flash_load...");
|
|
||||||
int f = emu_FileOpen(filename,"r+b");
|
|
||||||
if (f) {
|
|
||||||
while ( (offset < (HW_FLASH_STORAGE_TOP-FLASH_SECTOR_SIZE)) && (n = emu_FileRead(cache,FLASH_SECTOR_SIZE,f) ) ) {
|
|
||||||
for (int i=0;i<n; i+=2) {
|
|
||||||
uint8_t k = cache[i];
|
|
||||||
cache[i]=cache[i+1];
|
|
||||||
cache[i+1] = k;
|
|
||||||
}
|
|
||||||
//memcpy(cache, (unsigned char *)offset, n);
|
|
||||||
uint32_t ints = save_and_disable_interrupts();
|
|
||||||
flash_range_erase(offset, FLASH_SECTOR_SIZE);
|
|
||||||
flash_range_program(offset, (const uint8_t *)&cache[0], FLASH_SECTOR_SIZE);
|
|
||||||
restore_interrupts(ints);
|
|
||||||
emu_printi(n);
|
|
||||||
emu_printi(offset);
|
|
||||||
//uint8_t * pt = (uint8_t*)(XIP_BASE + offset);
|
|
||||||
//emu_printi(pt[0]);
|
|
||||||
//emu_printi(pt[1]);
|
|
||||||
//emu_printi(pt[2]);
|
|
||||||
//emu_printi(pt[3]);
|
|
||||||
offset += FLASH_SECTOR_SIZE;
|
|
||||||
size += n;
|
|
||||||
}
|
|
||||||
emu_FileClose(f);
|
|
||||||
emu_printf("flash_load OK.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return size;
|
int flash_load_bswap(const char * filename)
|
||||||
|
{
|
||||||
|
return flash_load_common(filename, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_verify(unsigned char * buf, int size)
|
int flash_verify(unsigned char * buf, int size)
|
||||||
{
|
{
|
||||||
unsigned char * datapt = (unsigned char *)(XIP_BASE + HW_FLASH_STORAGE_BASE);
|
flash_setup();
|
||||||
|
unsigned char * datapt = flash_start;
|
||||||
emu_printf("flash_verify...");
|
emu_printf("flash_verify...");
|
||||||
int count = size;
|
int count = size;
|
||||||
while (count++ < size) {
|
while (count++ < size) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue