apps,litenes,cpu: optimize memory accessing

This commit is contained in:
Zihao Yu 2019-04-24 09:57:38 +08:00
parent 963be42f60
commit 6270a141ee
2 changed files with 39 additions and 24 deletions

View File

@ -21,22 +21,18 @@ static inline uint32_t memory_readb(uint32_t address)
static inline uint32_t instr_fetch(uint32_t address) {
extern byte memory[0x10000]; // mmc
extern byte CPU_RAM[0x8000]; // CPU Memory
//extern byte CPU_RAM[0x8000]; // CPU Memory
if ((address >> 15) == 0) {
return CPU_RAM[address & 0x7FF];
}
else {
// for super mairo, all fetch are from mmc
return memory[address];
}
}
static inline void memory_writeb(uint32_t address, uint32_t byte_data)
{
switch (address >> 13) {
case 0:
case 3: return cpu_ram_write(address, byte_data);
case 1: return ppu_io_write(address, byte_data);
case 3: cpu_ram_write(address, byte_data); break;
case 1: ppu_io_write(address, byte_data); break;
case 2:
if (address == 0x4014) {
// DMA transfer
@ -46,8 +42,8 @@ static inline void memory_writeb(uint32_t address, uint32_t byte_data)
}
return;
}
return psg_io_write(address, byte_data);
default: return mmc_write(address, byte_data);
psg_io_write(address, byte_data); break;
// for super mario, it does not write to mmc
}
}

View File

@ -117,16 +117,19 @@ static inline void cpu_address_indirect() {
}
static inline void cpu_address_indirect_x() {
uint32_t arg_addr = instr_fetch(cpu.PC);
uint32_t arg_addr = (instr_fetch(cpu.PC) + cpu.X) & 0xFF;
//op_address = (memory_readb((arg_addr + cpu.X + 1) & 0xFF) << 8) | memory_readb((arg_addr + cpu.X) & 0xFF);
op_address = memory_readw(arg_addr + cpu.X);
// op_address = memory_readw((arg_addr + cpu.X) & 0xFF);
assert(0);
op_address = (CPU_RAM[arg_addr + 1] << 8) | CPU_RAM[arg_addr];
op_value = memory_readb(op_address);
cpu.PC++;
}
static inline void cpu_address_indirect_y() {
uint32_t arg_addr = instr_fetch(cpu.PC);
op_address = (((memory_readb((arg_addr + 1) & 0xFF) << 8) | memory_readb(arg_addr)) + (cpu.Y & 0xff)) & 0xFFFF;
uint32_t temp = (CPU_RAM[arg_addr + 1] << 8) | CPU_RAM[arg_addr];
op_address = (temp + (cpu.Y & 0xff)) & 0xFFFF;
op_value = memory_readb(op_address);
cpu.PC++;
@ -718,6 +721,31 @@ void cpu_reset()
cpu.P[interrupt_bp] = 1;
}
static void display_statistic() {
int i;
int total = 0;
for (i = 0; i < 256; i += 8) {
total += cpu_op_cnts[i + 0] + cpu_op_cnts[i + 1] + cpu_op_cnts[i + 2] + cpu_op_cnts[i + 3] +
cpu_op_cnts[i + 4] + cpu_op_cnts[i + 5] + cpu_op_cnts[i + 6] + cpu_op_cnts[i + 7];
}
for (i = 0; i < 256; i ++) {
cpu_op_cnts[i] = (cpu_op_cnts[i] * 1000) / total;
}
for (i = 0; i < 256; i += 8) {
printf("0x%02x: %8d %8d %8d %8d %8d %8d %8d %8d\n", i,
cpu_op_cnts[i + 0], cpu_op_cnts[i + 1], cpu_op_cnts[i + 2], cpu_op_cnts[i + 3],
cpu_op_cnts[i + 4], cpu_op_cnts[i + 5], cpu_op_cnts[i + 6], cpu_op_cnts[i + 7]);
}
for (i = 0; i < 256; i ++) {
cpu_op_cnts[i] = 0;
}
printf("========== Total = %d ===========\n", total);
}
void cpu_interrupt()
{
// if (ppu_in_vblank()) {
@ -983,17 +1011,8 @@ void cpu_run(long cycles)
static int num = 0;
if (num == 10000) {
display_statistic();
num = 0;
int i;
int total = 0;
for (i = 0; i < 256; i += 8) {
printf("0x%02x: %8d %8d %8d %8d %8d %8d %8d %8d\n", i,
cpu_op_cnts[i + 0], cpu_op_cnts[i + 1], cpu_op_cnts[i + 2], cpu_op_cnts[i + 3],
cpu_op_cnts[i + 4], cpu_op_cnts[i + 5], cpu_op_cnts[i + 6], cpu_op_cnts[i + 7]);
total += cpu_op_cnts[i + 0] + cpu_op_cnts[i + 1] + cpu_op_cnts[i + 2] + cpu_op_cnts[i + 3] +
cpu_op_cnts[i + 4] + cpu_op_cnts[i + 5] + cpu_op_cnts[i + 6] + cpu_op_cnts[i + 7];
}
printf("========== Total = %d ===========\n", total);
}
num ++;
}