Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ogamespec committed Jul 15, 2024
1 parent dc8a81f commit 85cd7cd
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 166 deletions.
199 changes: 99 additions & 100 deletions apu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct
unsigned outfreq,ratelo,ratehi,z0;
sndchan ch[4];
uint8_t wave[16];
} so;
} apu;

const static uint8_t dmgwave[16] =
{
Expand Down Expand Up @@ -50,7 +50,7 @@ const static int divtab[8] =
14
};

so snd;
apu snd;

static uint8_t noise7[16];
static uint8_t noise15[4096];
Expand Down Expand Up @@ -91,7 +91,7 @@ void apu_shutdown()
// **********************************************************************

/*
Questionable aspects of SO terminal emulation:
Questionable aspects of APU emulation:
NR51 contains updated "sound enable" flags
Frequency registers can update if sweep is operating
Expand Down Expand Up @@ -333,103 +333,102 @@ void apu_write(uint8_t r, uint8_t b)
apu_mix();
switch (r)
{
case RI_NR10:
R_NR10 = b;
S1.swfreq = 2047&*(unsigned short*)&R_NR13;
S1.swcnt = 0; // TODO? Is it true?
break;
case RI_NR11:
R_NR11 = b;
S1.cnt = 64-(b&63);
break;
case RI_NR12:
R_NR12 = b;
S1.envol = R_NR12 >> 4;
if((b&0xF8) == 0) apu_1off(); // Forced OFF mode(as stated in patent docs) if 0 and down
//S1.endir = (R_NR12>>3) & 1;
//S1.endir |= S1.endir - 1;
break;
case RI_NR13:
R_NR13 = b;
s1_freq();
break;
case RI_NR14:
R_NR14 = b;
s1_freq();
if (b & 128) s1_init();
break;
case RI_NR21:
R_NR21 = b;
S2.cnt = 64-(b&63);
break;
case RI_NR22:
R_NR22 = b;
if((b&0xF8) == 0) apu_2off(); // Forced OFF mode(as stated in patent docs) if 0 and down
S2.envol = R_NR22 >> 4;
//S2.endir = (R_NR22>>3) & 1;
//S2.endir |= S2.endir - 1;
break;
case RI_NR23:
R_NR23 = b;
s2_freq();
break;
case RI_NR24:
R_NR24 = b;
s2_freq();
if (b & 128) s2_init();
break;
case RI_NR30:
R_NR30 = b;
if (!(b & 128)) apu_3off();
break;
case RI_NR31:
R_NR31 = b;
S3.cnt = 256-b;
break;
case RI_NR32:
R_NR32 = b;

break;
case RI_NR33:
R_NR33 = b;
s3_freq();
break;
case RI_NR34:
R_NR34 = b;
s3_freq();
if (b & 128) s3_init();
break;
case RI_NR41:
R_NR41 = b;
S4.cnt = 64-(b&63);
break;
case RI_NR42:
R_NR42 = b;
S4.envol = R_NR42 >> 4;
if((b&0xF8) == 0) apu_4off(); // Forced OFF mode(as stated in patent docs) if 0 and down
break;
case RI_NR43:
R_NR43 = b;
s4_freq();
break;
case RI_NR44:
R_NR44 = b;
s4_freq();
if (b & 128) s4_init();
break;
case RI_NR50:
R_NR50 = b;
break;
case RI_NR51:
R_NR51 = b;
break;
case RI_NR52:
R_NR52 = b;
if (!(R_NR52 & 128))
apu_off();
break;
default:
return;
case RI_NR10:
R_NR10 = b;
S1.swfreq = 2047&*(unsigned short*)&R_NR13;
S1.swcnt = 0; // TODO? Is it true?
break;
case RI_NR11:
R_NR11 = b;
S1.cnt = 64-(b&63);
break;
case RI_NR12:
R_NR12 = b;
S1.envol = R_NR12 >> 4;
if((b&0xF8) == 0) apu_1off(); // Forced OFF mode(as stated in patent docs) if 0 and down
//S1.endir = (R_NR12>>3) & 1;
//S1.endir |= S1.endir - 1;
break;
case RI_NR13:
R_NR13 = b;
s1_freq();
break;
case RI_NR14:
R_NR14 = b;
s1_freq();
if (b & 128) s1_init();
break;
case RI_NR21:
R_NR21 = b;
S2.cnt = 64-(b&63);
break;
case RI_NR22:
R_NR22 = b;
if((b&0xF8) == 0) apu_2off(); // Forced OFF mode(as stated in patent docs) if 0 and down
S2.envol = R_NR22 >> 4;
//S2.endir = (R_NR22>>3) & 1;
//S2.endir |= S2.endir - 1;
break;
case RI_NR23:
R_NR23 = b;
s2_freq();
break;
case RI_NR24:
R_NR24 = b;
s2_freq();
if (b & 128) s2_init();
break;
case RI_NR30:
R_NR30 = b;
if (!(b & 128)) apu_3off();
break;
case RI_NR31:
R_NR31 = b;
S3.cnt = 256-b;
break;
case RI_NR32:
R_NR32 = b;
break;
case RI_NR33:
R_NR33 = b;
s3_freq();
break;
case RI_NR34:
R_NR34 = b;
s3_freq();
if (b & 128) s3_init();
break;
case RI_NR41:
R_NR41 = b;
S4.cnt = 64-(b&63);
break;
case RI_NR42:
R_NR42 = b;
S4.envol = R_NR42 >> 4;
if((b&0xF8) == 0) apu_4off(); // Forced OFF mode(as stated in patent docs) if 0 and down
break;
case RI_NR43:
R_NR43 = b;
s4_freq();
break;
case RI_NR44:
R_NR44 = b;
s4_freq();
if (b & 128) s4_init();
break;
case RI_NR50:
R_NR50 = b;
break;
case RI_NR51:
R_NR51 = b;
break;
case RI_NR52:
R_NR52 = b;
if (!(R_NR52 & 128))
apu_off();
break;
default:
return;
}
}

Expand Down
16 changes: 8 additions & 8 deletions gb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void gb_init()
{
// log_init("gbemu.log");
mem_init();
gbz80_init();
sm83_init();
pad_init();
ppu_init();
apu_init(44100);
Expand Down Expand Up @@ -38,7 +38,7 @@ void check4LCDint(unsigned mode) { // Also called from mem.c!!
//unsigned lcd_int_on_new=; // LYC is not processed here
if(R_STAT&stat2LCDflg[mode]/* !lcd_int_on && */) {// check if interrupt is requested already
R_IF|=INT_LCDSTAT;
check4int(); // do int if possible
sm83_check4int(); // do int if possible
}
//lcd_int_on = lcd_int_on_new;
}
Expand All @@ -49,7 +49,7 @@ void check4LYC(void) { // Also called from mem.c!!
if(R_STAT < stnew)
if(stnew&0x40) {// check if interrupt allowed
R_IF|=INT_LCDSTAT;
check4int(); // do int if possible
sm83_check4int(); // do int if possible
}
}
R_STAT=stnew;
Expand All @@ -74,12 +74,12 @@ void gb_reload_tima(unsigned data) { // will only contain byte value
static void execute(unsigned long n) {
gb_eventclk+=n; // timerclk = MAXULONG means that timer interrupt is off
/*while(gb_eventclk>gb_timerclk) {
gbz80_execute_until(gb_timerclk);
sm83_execute_until(gb_timerclk);
gb_reload_tima(R_TMA);
R_IF|=INT_TIMER; // request timer interrupt
check4int();
sm83_check4int();
}*/
gbz80_execute_until(gb_eventclk);
sm83_execute_until(gb_eventclk);
}

// **********************************************************************
Expand All @@ -105,7 +105,7 @@ void start()
/* LCD during H-Blank */
//STAT_MODE(0);
//gb_eventclk+=204;
//gbz80_execute_until(gb_eventclk);
//sm83_execute_until(gb_eventclk);

STAT_MODE(2);
ppu_enumsprites();
Expand All @@ -125,7 +125,7 @@ void start()
/* LCD during V-Blank (10 "empty" lines) */
//if(R_STAT & 0x10) // questionable
R_IF|=INT_VBLANK; // Queue V-blank int
check4int();
sm83_check4int();
STAT_MODE(1);
ppu_vsync();
for(i=10;i!=0;i--) {
Expand Down
10 changes: 10 additions & 0 deletions gb.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#pragma once

/* Interrupt flags*/
#define INT_NONE 0
#define INT_VBLANK 1
#define INT_LCDSTAT 2
#define INT_TIMER 4
#define INT_SERIAL 8
#define INT_PAD 0x10
#define INT_ALL 0x1F


void gb_init(void);
void gb_shutdown(void);
void start(void);
Expand Down
49 changes: 27 additions & 22 deletions lcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ unsigned benchmark_sound, benchmark_gfx;
static HDC main_hdc, hdcc;
static HBITMAP DIB_section;
static HGDIOBJ old_obj;
//static RGBQUAD *pbuf;

RGBQUAD* pbuf;
static RGBQUAD *pbuf;

/* milk to cofee */
RGBQUAD dib_pal[] = {
Expand Down Expand Up @@ -90,8 +88,8 @@ void win32_win_init(int width, int height)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
char title[128];
WNDCLASS wc;
RECT rect;
WNDCLASS wc{};
RECT rect{};
int w, h;

sprintf(title, "GameBoy - %s", romhdr->title);
Expand Down Expand Up @@ -127,8 +125,10 @@ void win32_win_init(int width, int height)
NULL, NULL,
hInstance, NULL);

if (!main_hwnd)
if (!main_hwnd) {
sys_error("couldn't create main window.");
return;
}

ShowWindow(main_hwnd, SW_NORMAL);
UpdateWindow(main_hwnd);
Expand Down Expand Up @@ -185,32 +185,37 @@ void WIN_Center(HWND hwnd)
void win32_dib_init(int width, int height)
{
HDC hdc;
BITMAPINFO* bmi;
BITMAPINFO bmi{};
void* DIB_base;

bmi = (BITMAPINFO*)calloc(sizeof(BITMAPINFO) + 16 * 4, 1);
main_hdc = hdc = GetDC(main_hwnd);

//memset(&(bmi->bmiHeader), 0, sizeof(BITMAPINFOHEADER));

bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
dib_width = bmi->bmiHeader.biWidth = width;
dib_height = bmi->bmiHeader.biHeight = -height;
bmi->bmiHeader.biPlanes = 1;
bmi->bmiHeader.biBitCount = 32;
bmi->bmiHeader.biCompression = BI_RGB;
//bmi->bmiHeader.

DIB_section = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &DIB_base, NULL, 0);
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
dib_width = bmi.bmiHeader.biWidth = width;
dib_height = bmi.bmiHeader.biHeight = -height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;

DIB_section = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &DIB_base, NULL, 0);
if (!DIB_section) {
sys_error("CreateDIBSection() failed!");
return;
}
pbuf = (RGBQUAD*)DIB_base;

hdcc = CreateCompatibleDC(hdc);
if (!hdcc)
if (!hdcc) {
DeleteObject(DIB_section);
sys_error("CreateCompatibleDC() failed!");
return;
}

if (!(old_obj = SelectObject(hdcc, DIB_section)))
if (!(old_obj = SelectObject(hdcc, DIB_section))) {
DeleteDC(hdcc);
DeleteObject(DIB_section);
sys_error("SelectObject() failed!");
free(bmi);
}
}

void win32_dib_shutdown()
Expand Down
Loading

0 comments on commit 85cd7cd

Please sign in to comment.