Skip to content

Commit

Permalink
Make macro GBIT to transfer data to gbit function
Browse files Browse the repository at this point in the history
  • Loading branch information
Rsysz committed Jan 31, 2021
1 parent 433f520 commit 953b22b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 27 deletions.
93 changes: 75 additions & 18 deletions bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "gbit/lib/tester.h"

static struct gb_s gb;
static size_t instruction_mem_size;
static uint8_t *instruction_mem;

static int num_mem_accesses;
static struct mem_access mem_accesses[16];

/* Return byte from Blarrg test ROM, written by Shay Green <gblargg@gmail.com>
* https://github.com/retrio/gb-test-roms/tree/master/cpu_instrs
Expand All @@ -42,17 +51,21 @@ void gb_cart_ram_write(struct gb_s *gb,
const uint8_t val)
{
(void) gb;
(void) addr;
(void) val;
struct mem_access *access = &mem_accesses[num_mem_accesses++];
access->type = MEM_ACCESS_WRITE;
access->addr = addr;
access->val = val;
return;
}

/* Ignore cart RAM reads */
uint8_t gb_cart_ram_read(struct gb_s *gb, const uint_fast32_t addr)
{
(void) gb;
(void) addr;
return 0xFF;
if (addr < instruction_mem_size)
return instruction_mem[addr];
/* Return 0xAA for valid memory range validation */
return 0xaa;
}

/* Ignore all errors */
Expand All @@ -64,26 +77,70 @@ void gb_error(struct gb_s *gb, const gb_error_t gb_err, const uint16_t val)
return;
}

int main(void)
static void gbz80_init(size_t tester_instruction_mem_size,
uint8_t *tester_instruction_mem)
{
const unsigned short pc_end = 0x06F1; /* Test ends when PC is this value. */
struct gb_s gb;
int ret;
instruction_mem_size = tester_instruction_mem_size;
instruction_mem = tester_instruction_mem;
gb_init(&gb, &gb_rom_read, &gb_cart_ram_read, &gb_cart_ram_write, &gb_error,
NULL);
}

PROF_START();
/* Resets the CPU state (e.g., registers) to a given state state */
static void gbz80_set_state(struct state *state)
{
gb.cpu_reg.a = state->reg8.A;
gb.cpu_reg.f = state->reg8.F;
gb.cpu_reg.bc = state->reg16.BC;
gb.cpu_reg.de = state->reg16.DE;
gb.cpu_reg.hl = state->reg16.HL;
gb.cpu_reg.sp = state->SP;
gb.cpu_reg.pc = state->PC;
gb.gb_halt = state->halted;
gb.gb_ime = state->interrupts_master_enabled;
}

ret = gb_init(&gb, &gb_rom_read, &gb_cart_ram_read, &gb_cart_ram_write,
&gb_error, NULL);
/* Query the current state of the CPU */
static void gbz80_get_state(struct state *state)
{
state->num_mem_accesses = num_mem_accesses;
memcpy(state->mem_accesses, mem_accesses, sizeof(mem_accesses));
state->reg8.A = gb.cpu_reg.a;
state->reg8.F = gb.cpu_reg.f;
state->reg16.BC = gb.cpu_reg.bc;
state->reg16.DE = gb.cpu_reg.de;
state->reg16.HL = gb.cpu_reg.hl;
state->SP = gb.cpu_reg.sp;
state->PC = gb.cpu_reg.pc;
state->halted = gb.gb_halt;
state->interrupts_master_enabled = gb.gb_ime;
num_mem_accesses = 0;
}

/* Step a single instruction of the CPU and return the amount of cycles */
static int gbz80_step(void)
{
return gb_run_frame(&gb);
}

if (ret != GB_INIT_NO_ERROR) {
printf("Error: %d\n", ret);
exit(EXIT_FAILURE);
}
static struct tester_flags flags = {
.keep_going_on_mismatch = 0,
.enable_cb_instruction_testing = 1,
.print_tested_instruction = 0,
.print_verbose_inputs = 0,
};

/* Step CPU until test is complete */
while (gb.cpu_reg.pc != pc_end)
gb_run_frame(&gb);
struct tester_operations gbz80_ops = {
.init = gbz80_init,
.set_state = gbz80_set_state,
.get_state = gbz80_get_state,
.step = gbz80_step,
};

int main(void)
{
PROF_START();
tester_run(&flags, &gbz80_ops);
PROF_STDOUT();

return 0;
Expand Down
16 changes: 12 additions & 4 deletions cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static uint8_t __gb_execute_cb(struct gb_s *gb)
}

/* Internal function used to step the CPU */
static void __gb_step_cpu(struct gb_s *gb)
static uint8_t __gb_step_cpu(struct gb_s *gb)
{
uint8_t opcode, inst_cycles;
static const uint8_t op_cycles[0x100] = {
Expand Down Expand Up @@ -1954,6 +1954,9 @@ static void __gb_step_cpu(struct gb_s *gb)
(gb->gb_error)(gb, GB_INVALID_OPCODE, opcode);
}

#ifdef GBIT
gb->gb_frame = 1;
#else
/* DIV register timing */
gb->counter.div_count += inst_cycles;

Expand Down Expand Up @@ -2025,7 +2028,7 @@ static void __gb_step_cpu(struct gb_s *gb)
/* If LCD is off, don't update LCD state.
*/
if ((gb->gb_reg.LCDC & LCDC_ENABLE) == 0)
return;
return inst_cycles;

/* LCD Timing */
gb->counter.lcd_count += inst_cycles;
Expand Down Expand Up @@ -2101,12 +2104,17 @@ static void __gb_step_cpu(struct gb_s *gb)
__gb_draw_line(gb);
#endif
}
#endif
return inst_cycles;
}

void gb_run_frame(struct gb_s *gb)
uint8_t gb_run_frame(struct gb_s *gb)
{
uint8_t cycles = 0;
gb->gb_frame = 0;

while (!gb->gb_frame)
__gb_step_cpu(gb);
cycles = __gb_step_cpu(gb);
return cycles;
}

3 changes: 2 additions & 1 deletion cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "core.h"

void gb_run_frame(struct gb_s *gb);
uint8_t gb_run_frame(struct gb_s *gb);

18 changes: 14 additions & 4 deletions gameboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ void gb_set_rtc(struct gb_s *gb, const struct tm *const time)
/* Internal function used to read bytes. */
uint8_t __gb_read(struct gb_s *gb, const uint_fast16_t addr)
{
#ifdef GBIT
return gb->gb_cart_ram_read(gb, addr);
#else
switch (addr >> 12) {
case 0x0:

Expand Down Expand Up @@ -223,13 +226,15 @@ uint8_t __gb_read(struct gb_s *gb, const uint_fast16_t addr)

(gb->gb_error)(gb, GB_INVALID_READ, addr);
return 0xFF;
#endif
}

/* Internal function used to write bytes */
void __gb_write(struct gb_s *gb,
const uint_fast16_t addr,
const uint8_t val)
void __gb_write(struct gb_s *gb, const uint_fast16_t addr, const uint8_t val)
{
#ifdef GBIT
gb->gb_cart_ram_write(gb, addr, val);
#else
switch (addr >> 12) {
case 0x0:
case 0x1:
Expand Down Expand Up @@ -496,6 +501,7 @@ void __gb_write(struct gb_s *gb,
}

(gb->gb_error)(gb, GB_INVALID_WRITE, addr);
#endif
}

#if ENABLE_LCD
Expand Down Expand Up @@ -817,10 +823,14 @@ void gb_reset(struct gb_s *gb)
gb->gb_reg.SC = 0x7E;
gb->gb_reg.STAT = 0;
gb->gb_reg.LY = 0;

/* Avoid using __gb_write when instruction tests, since __gb_write() is called to
* write data to shared memory with gbit functions.
*/
#ifndef GBIT
__gb_write(gb, 0xFF47, 0xFC); /* BGP */
__gb_write(gb, 0xFF48, 0xFF); /* OBJP0 */
__gb_write(gb, 0xFF49, 0x0F); /* OBJP1 */
#endif
gb->gb_reg.WY = 0x00;
gb->gb_reg.WX = 0x00;
gb->gb_reg.IE = 0x00;
Expand Down

0 comments on commit 953b22b

Please sign in to comment.