Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.15)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

Expand Down
1 change: 1 addition & 0 deletions components/box-emu/include/box-emu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class BoxEmu : public espp::BaseComponent {
/////////////////////////////////////////////////////////////////////////////

bool initialize_video();
void clear_screen();
void display_size(size_t width, size_t height);
void native_size(size_t width, size_t height, int pitch = -1);
void palette(const uint16_t *palette, size_t size = 256);
Expand Down
27 changes: 24 additions & 3 deletions components/box-emu/src/box-emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ bool BoxEmu::initialize_video() {
return true;
}

void BoxEmu::clear_screen() {
static int buffer = 0;
xQueueSend(video_queue_, &buffer, 10);
}

void BoxEmu::display_size(size_t width, size_t height) {
display_width_ = width;
display_height_ = height;
Expand Down Expand Up @@ -717,13 +722,29 @@ bool BoxEmu::video_task_callback(std::mutex &m, std::condition_variable& cv, boo
return false;
}
static constexpr int num_lines_to_write = num_rows_in_framebuffer;
auto &box = Bsp::get();
static auto &box = Bsp::get();
static const uint16_t *vram0 = (uint16_t*)box.vram0();
static const uint16_t *vram1 = (uint16_t*)box.vram1();
static uint16_t vram_index = 0; // has to be static so that it persists between calls
const int _x_offset = x_offset();
const int _y_offset = y_offset();
const uint16_t* _palette = palette();
uint16_t *vram0 = (uint16_t*)box.vram0();
uint16_t *vram1 = (uint16_t*)box.vram1();
// special case: if _frame_ptr is null, then we ignore all palette, scaling,
// etc. and simply fill the screen with 0
if (_frame_ptr == nullptr) {
for (int y=0; y<lcd_height(); y+= num_lines_to_write) {
Pixel* _buf = (Pixel*)((uint32_t)vram0 * (vram_index ^ 0x01) + (uint32_t)vram1 * vram_index);
vram_index = vram_index ^ 0x01;
int num_lines = std::min<int>(num_lines_to_write, lcd_height()-y);
// memset the buffer to 0
memset(_buf, 0, lcd_width() * num_lines * sizeof(Pixel));
box.write_lcd_frame(0, y + _y_offset, lcd_width(), num_lines, (uint8_t*)&_buf[0]);
}

// now return
return false;
}

if (is_native()) {
if (has_palette()) {
for (int y=0; y<display_height_; y+= num_lines_to_write) {
Expand Down
2 changes: 1 addition & 1 deletion components/gbc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ idf_component_register(
SRC_DIRS "src" "gnuboy/src"
PRIV_INCLUDE_DIRS "gnuboy/include"
# LDFRAGMENTS "linker.lf"
REQUIRES "box-emu" "statistics"
REQUIRES "box-emu" "statistics" "shared_memory"
)

target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-unused-function -Wno-unused-variable -Wno-discarded-qualifiers)
Expand Down
2 changes: 1 addition & 1 deletion components/gbc/gnuboy/include/gnuboy/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct cpu
int snd;
};

extern struct cpu cpu;
extern struct cpu *cpu;


void cpu_timers(int cnt);
Expand Down
54 changes: 27 additions & 27 deletions components/gbc/gnuboy/include/gnuboy/cpuregs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@
#define W(r) ((r).w[LO])
#define DW(r) ((r).d)

#define A HB(cpu.af)
#define F LB(cpu.af)
#define B HB(cpu.bc)
#define C LB(cpu.bc)
#define D HB(cpu.de)
#define E LB(cpu.de)
#define H HB(cpu.hl)
#define L LB(cpu.hl)

#define AF W(cpu.af)
#define BC W(cpu.bc)
#define DE W(cpu.de)
#define HL W(cpu.hl)

#define PC W(cpu.pc)
#define SP W(cpu.sp)

#define xAF DW(cpu.af)
#define xBC DW(cpu.bc)
#define xDE DW(cpu.de)
#define xHL DW(cpu.hl)

#define xPC DW(cpu.pc)
#define xSP DW(cpu.sp)

#define IMA cpu.ima
#define IME cpu.ime
#define A HB(cpu->af)
#define F LB(cpu->af)
#define B HB(cpu->bc)
#define C LB(cpu->bc)
#define D HB(cpu->de)
#define E LB(cpu->de)
#define H HB(cpu->hl)
#define L LB(cpu->hl)

#define AF W(cpu->af)
#define BC W(cpu->bc)
#define DE W(cpu->de)
#define HL W(cpu->hl)

#define PC W(cpu->pc)
#define SP W(cpu->sp)

#define xAF DW(cpu->af)
#define xBC DW(cpu->bc)
#define xDE DW(cpu->de)
#define xHL DW(cpu->hl)

#define xPC DW(cpu->pc)
#define xSP DW(cpu->sp)

#define IMA cpu->ima
#define IME cpu->ime
#define IF R_IF
#define IE R_IE

Expand Down
4 changes: 2 additions & 2 deletions components/gbc/gnuboy/include/gnuboy/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct vissprite
//byte pad[2]; //6
};

struct scan
struct gbc_scan
{
int bg[64];
int wnd[64];
Expand Down Expand Up @@ -45,7 +45,7 @@ struct lcd
};

extern struct lcd lcd;
extern struct scan scan;
extern struct gbc_scan *scan;


void lcd_begin();
Expand Down
2 changes: 1 addition & 1 deletion components/gbc/gnuboy/include/gnuboy/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ int sram_save();
void state_load(int n);
void state_save(int n);


extern unsigned char *gbc_filebuf;

#endif
62 changes: 28 additions & 34 deletions components/gbc/gnuboy/src/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include "gnuboy/asm.h"
#endif


struct cpu cpu;




#define ZFLAG(n) ( (n) ? 0 : FZ )
#define HFLAG(n) ( (n) ? 0 : FH )
#define CFLAG(n) ( (n) ? 0 : FC )
Expand Down Expand Up @@ -231,7 +225,7 @@ label: op(b); break;
#define RET ( POP(PC) )

#define EI ( IMA = 1 )
#define DI ( cpu.halt = IMA = IME = 0 )
#define DI ( cpu->halt = IMA = IME = 0 )



Expand All @@ -251,13 +245,13 @@ label: op(b); break;

void cpu_reset()
{
cpu.speed = 0;
cpu.halt = 0;
cpu.div = 0;
cpu.tim = 0;
cpu->speed = 0;
cpu->halt = 0;
cpu->div = 0;
cpu->tim = 0;
/* set lcdc ahead of cpu by 19us; see A */
/* FIXME: leave value at 0, use lcdc_trans() to actually send lcdc ahead */
cpu.lcdc = 40;
cpu->lcdc = 40;

IME = 0;
IMA = 0;
Expand All @@ -281,11 +275,11 @@ void cpu_reset()
handle differences in place */
void div_advance(int cnt)
{
cpu.div += (cnt<<1);
if (cpu.div >= 256)
cpu->div += (cnt<<1);
if (cpu->div >= 256)
{
R_DIV += (cpu.div >> 8);
cpu.div &= 0xff;
R_DIV += (cpu->div >> 8);
cpu->div &= 0xff;
}
}

Expand All @@ -302,12 +296,12 @@ void timer_advance(int cnt)
if (!(R_TAC & 0x04)) return;

unit = ((-R_TAC) & 3) << 1;
cpu.tim += (cnt<<unit);
cpu->tim += (cnt<<unit);

if (cpu.tim >= 512)
if (cpu->tim >= 512)
{
tima = R_TIMA + (cpu.tim >> 9);
cpu.tim &= 0x1ff;
tima = R_TIMA + (cpu->tim >> 9);
cpu->tim &= 0x1ff;
if (tima >= 256)
{
hw_interrupt(IF_TIMER, IF_TIMER);
Expand All @@ -325,21 +319,21 @@ void timer_advance(int cnt)
*/
inline void lcdc_advance(int cnt)
{
cpu.lcdc -= cnt;
if (cpu.lcdc <= 0) lcdc_trans();
cpu->lcdc -= cnt;
if (cpu->lcdc <= 0) lcdc_trans();
}

/* cnt - time to emulate, expressed in 2MHz units */
inline void sound_advance(int cnt)
{
cpu.snd += cnt;
cpu->snd += cnt;
}

/* cnt - time to emulate, expressed in 2MHz units */
void cpu_timers(int cnt)
{
div_advance(cnt << cpu.speed);
timer_advance(cnt << cpu.speed);
div_advance(cnt << cpu->speed);
timer_advance(cnt << cpu->speed);
lcdc_advance(cnt);
sound_advance(cnt);
}
Expand All @@ -356,16 +350,16 @@ int cpu_idle(int max)
int cnt, unit;


if (!(cpu.halt && IME)) return 0;
if (!(cpu->halt && IME)) return 0;
if (R_IF & R_IE)
{
cpu.halt = 0;
cpu->halt = 0;
return 0;
}

/* Make sure we don't miss lcdc status events! */
if ((R_IE & (IF_VBLANK | IF_STAT)) && (max > cpu.lcdc))
max = cpu.lcdc;
if ((R_IE & (IF_VBLANK | IF_STAT)) && (max > cpu->lcdc))
max = cpu->lcdc;

/* If timer interrupt cannot happen, this is very simple! */
if (!((R_IE & IF_TIMER) && (R_TAC & 0x04)))
Expand All @@ -376,7 +370,7 @@ int cpu_idle(int max)

/* Figure out when the next timer interrupt will happen */
unit = ((-R_TAC) & 3) << 1;
cnt = (511 - cpu.tim + (1<<unit)) >> unit;
cnt = (511 - cpu->tim + (1<<unit)) >> unit;
cnt += (255 - R_TIMA) << (9 - unit);

if (max < cnt)
Expand Down Expand Up @@ -909,15 +903,15 @@ int cpu_emulate(int cycles)
PC++;
if (R_KEY1 & 1)
{
cpu.speed = cpu.speed ^ 1;
R_KEY1 = (R_KEY1 & 0x7E) | (cpu.speed << 7);
cpu->speed = cpu->speed ^ 1;
R_KEY1 = (R_KEY1 & 0x7E) | (cpu->speed << 7);
break;
}
/* NOTE - we do not implement dmg STOP whatsoever */
break;

case 0x76: /* HALT */
cpu.halt = 1;
cpu->halt = 1;
break;

case 0xCB: /* CB prefix */
Expand Down Expand Up @@ -956,7 +950,7 @@ int cpu_emulate(int cycles)
clen <<= 1;
div_advance(clen);
timer_advance(clen);
clen >>= cpu.speed;
clen >>= cpu->speed;
lcdc_advance(clen);
sound_advance(clen);

Expand Down
2 changes: 1 addition & 1 deletion components/gbc/gnuboy/src/emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void emu_reset()
*/
void emu_step()
{
cpu_emulate(cpu.lcdc);
cpu_emulate(cpu->lcdc);
}


Expand Down
6 changes: 3 additions & 3 deletions components/gbc/gnuboy/src/hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void hw_interrupt(byte i, byte mask)
R_IF |= i & (hw.ilines ^ i);

/* FIXME - is this correct? not sure the docs understand... */
if ((R_IF & (R_IF ^ oldif) & R_IE) && cpu.ime) cpu.halt = 0;
/* if ((i & (hw.ilines ^ i) & R_IE) && cpu.ime) cpu.halt = 0; */
/* if ((i & R_IE) && cpu.ime) cpu.halt = 0; */
if ((R_IF & (R_IF ^ oldif) & R_IE) && cpu->ime) cpu->halt = 0;
/* if ((i & (hw.ilines ^ i) & R_IE) && cpu->ime) cpu->halt = 0; */
/* if ((i & R_IE) && cpu->ime) cpu->halt = 0; */

hw.ilines &= ~mask;
hw.ilines |= i;
Expand Down
Loading