Skip to content

Commit d2fa01e

Browse files
committed
move lcd struct and bgdup into shared memory for gbc
1 parent 9d5c714 commit d2fa01e

File tree

7 files changed

+51
-68
lines changed

7 files changed

+51
-68
lines changed

components/gbc/gnuboy/include/gnuboy/lcd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct lcd
4444
byte pal[128];
4545
};
4646

47-
extern struct lcd lcd;
47+
extern struct lcd *lcd;
4848
extern struct gbc_scan *scan;
4949

5050

components/gbc/gnuboy/src/hw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void hw_dma(byte b)
5454

5555
a = ((addr)b) << 8;
5656
for (i = 0; i < 160; i++, a++)
57-
lcd.oam.mem[i] = readb(a);
57+
lcd->oam.mem[i] = readb(a);
5858
}
5959

6060

components/gbc/gnuboy/src/lcd.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "make_color.h"
2121

22-
struct lcd lcd;
22+
struct lcd *lcd = NULL;
2323

2424
struct gbc_scan *scan = NULL;
2525

@@ -83,7 +83,7 @@ static const byte* get_patpix(int i, int x)
8383

8484
int j;
8585
int a, c;
86-
const byte* const vram = &lcd.vbank[0];
86+
const byte* const vram = &lcd->vbank[0];
8787

8888
switch (rotation)
8989
{
@@ -161,8 +161,8 @@ static void tilebuf()
161161

162162

163163
base = ((R_LCDC&0x08)?0x1C00:0x1800) + (T<<5) + S;
164-
tilemap = &lcd.vbank[0] + base;
165-
attrmap = &lcd.vbank[8192] + base;
164+
tilemap = &lcd->vbank[0] + base;
165+
attrmap = &lcd->vbank[8192] + base;
166166
tilebuf = BG;
167167
wrap = wraptable + S;
168168
cnt = ((WX + 7) >> 3) + 1;
@@ -209,8 +209,8 @@ static void tilebuf()
209209
if (WX >= 160) return;
210210

211211
base = ((R_LCDC&0x40)?0x1C00:0x1800) + (WT<<5);
212-
tilemap = &lcd.vbank[0] + base;
213-
attrmap = &lcd.vbank[8192] + base;
212+
tilemap = &lcd->vbank[0] + base;
213+
attrmap = &lcd->vbank[8192] + base;
214214
tilebuf = WND;
215215
cnt = ((160 - WX) >> 3) + 1;
216216

@@ -361,7 +361,7 @@ static void bg_scan_pri()
361361
i = S;
362362
cnt = WX;
363363
dest = PRI;
364-
src = &lcd.vbank[8192] + ((R_LCDC&0x08)?0x1C00:0x1800) + (T<<5);
364+
src = &lcd->vbank[8192] + ((R_LCDC&0x08)?0x1C00:0x1800) + (T<<5);
365365

366366
if (!priused(src))
367367
{
@@ -391,7 +391,7 @@ static void wnd_scan_pri()
391391
i = 0;
392392
cnt = 160 - WX;
393393
dest = PRI + WX;
394-
src = &lcd.vbank[8192] + ((R_LCDC&0x40)?0x1C00:0x1800) + (WT<<5);
394+
src = &lcd->vbank[8192] + ((R_LCDC&0x40)?0x1C00:0x1800) + (WT<<5);
395395

396396
if (!priused(src))
397397
{
@@ -472,7 +472,7 @@ static void spr_count()
472472
NS = 0;
473473
if (!(R_LCDC & 0x02)) return;
474474

475-
o = lcd.oam.obj;
475+
o = lcd->oam.obj;
476476

477477
for (i = 40; i; i--, o++)
478478
{
@@ -497,7 +497,7 @@ static void spr_enum()
497497
NS = 0;
498498
if (!(R_LCDC & 0x02)) return;
499499

500-
o = lcd.oam.obj;
500+
o = lcd->oam.obj;
501501

502502
for (i = 40; i; i--, o++)
503503
{
@@ -567,7 +567,7 @@ static void spr_enum()
567567
}
568568

569569

570-
static byte bgdup[256];
570+
byte *bgdup = NULL; // [256];
571571

572572
static void spr_scan()
573573
{
@@ -724,8 +724,8 @@ inline static void updatepalette(int i)
724724
short c;
725725
short r, g, b; //, y, u, v, rr, gg;
726726

727-
short low = lcd.pal[i << 1];
728-
short high = lcd.pal[(i << 1) | 1];
727+
short low = lcd->pal[i << 1];
728+
short high = lcd->pal[(i << 1) | 1];
729729

730730
c = (low | (high << 8)) & 0x7fff;
731731

@@ -744,9 +744,9 @@ inline static void updatepalette(int i)
744744

745745
inline void pal_write(int i, byte b)
746746
{
747-
if (lcd.pal[i] != b)
747+
if (lcd->pal[i] != b)
748748
{
749-
lcd.pal[i] = b;
749+
lcd->pal[i] = b;
750750
updatepalette(i>>1);
751751
}
752752
}
@@ -778,11 +778,11 @@ void pal_write_dmg(int i, int mapnum, byte d)
778778

779779
inline void vram_write(int a, byte b)
780780
{
781-
// byte (*vbank_arry)[2][8192] = (byte (*)[2][8192])lcd.vbank;
782-
//if (lcd.vbank[R_VBK&1][a] != b)
781+
// byte (*vbank_arry)[2][8192] = (byte (*)[2][8192])lcd->vbank;
782+
//if (lcd->vbank[R_VBK&1][a] != b)
783783
{
784-
// lcd.vbank[R_VBK&1][a] = b;
785-
lcd.vbank[(R_VBK&1) * 8192 + a] = b;
784+
// lcd->vbank[R_VBK&1][a] = b;
785+
lcd->vbank[(R_VBK&1) * 8192 + a] = b;
786786
// (*vbank_arry)[R_VBK&1][a] = b;
787787
if (a >= 0x1800) return;
788788
}
@@ -814,10 +814,10 @@ void pal_dirty()
814814
void lcd_reset()
815815
{
816816
// save the vbank pointer
817-
byte *vbank = lcd.vbank;
818-
memset(&lcd, 0, sizeof lcd);
817+
byte *vbank = lcd->vbank;
818+
memset(&lcd, 0, sizeof(struct lcd));
819819
// restore the vbank pointer
820-
lcd.vbank = vbank;
820+
lcd->vbank = vbank;
821821

822822
lcd_begin();
823823
vram_dirty();

components/gbc/gnuboy/src/mem.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ void mem_updatemap()
6060
//}
6161
//else
6262
//{
63-
// map[0x8] = lcd.vbank[R_VBK & 1] - 0x8000;
64-
// map[0x9] = lcd.vbank[R_VBK & 1] - 0x8000;
63+
// map[0x8] = lcd->vbank[R_VBK & 1] - 0x8000;
64+
// map[0x9] = lcd->vbank[R_VBK & 1] - 0x8000;
6565
//}
6666

6767
// if (mbc.enableram && !(rtc.sel&8))
@@ -210,11 +210,11 @@ void ioreg_write(byte r, byte b)
210210
break;
211211
case RI_BCPS:
212212
R_BCPS = b & 0xBF;
213-
R_BCPD = lcd.pal[b & 0x3F];
213+
R_BCPD = lcd->pal[b & 0x3F];
214214
break;
215215
case RI_OCPS:
216216
R_OCPS = b & 0xBF;
217-
R_OCPD = lcd.pal[64 + (b & 0x3F)];
217+
R_OCPD = lcd->pal[64 + (b & 0x3F)];
218218
break;
219219
case RI_BCPD:
220220
R_BCPD = b;
@@ -543,7 +543,7 @@ void mem_write(int a, byte b)
543543
if ((a & 0xFF00) == 0xFE00)
544544
{
545545
/* if (R_STAT & 0x02) break; */
546-
if (a < 0xFEA0) lcd.oam.mem[a & 0xFF] = b;
546+
if (a < 0xFEA0) lcd->oam.mem[a & 0xFF] = b;
547547
break;
548548
}
549549
/* return writehi(a & 0xFF, b); */
@@ -589,7 +589,7 @@ byte mem_read(int a)
589589
return rom.bank[mbc.rombank][a & 0x3FFF];
590590
case 0x8:
591591
/* if ((R_STAT & 0x03) == 0x03) return 0xFF; */
592-
return lcd.vbank[(R_VBK&1)*8192 + (a & 0x1FFF)];
592+
return lcd->vbank[(R_VBK&1)*8192 + (a & 0x1FFF)];
593593
case 0xA:
594594
if (!mbc.enableram && mbc.type == MBC_HUC3)
595595
return 0x01;
@@ -615,7 +615,7 @@ byte mem_read(int a)
615615
if ((a & 0xFF00) == 0xFE00)
616616
{
617617
/* if (R_STAT & 0x02) return 0xFF; */
618-
if (a < 0xFEA0) return lcd.oam.mem[a & 0xFF];
618+
if (a < 0xFEA0) return lcd->oam.mem[a & 0xFF];
619619
return 0xFF;
620620
}
621621
/* return readhi(a & 0xFF); */

components/gbc/gnuboy/src/save.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ void loadstate(FILE *f)
198198
if (hramofs) memcpy(ram.hi+128, gbc_filebuf+hramofs, 127);
199199

200200
if (hiofs) memcpy(ram.hi, gbc_filebuf+hiofs, sizeof ram.hi);
201-
if (palofs) memcpy(lcd.pal, gbc_filebuf+palofs, sizeof lcd.pal);
202-
if (oamofs) memcpy(lcd.oam.mem, gbc_filebuf+oamofs, sizeof lcd.oam);
201+
if (palofs) memcpy(lcd->pal, gbc_filebuf+palofs, sizeof lcd->pal);
202+
if (oamofs) memcpy(lcd->oam.mem, gbc_filebuf+oamofs, sizeof lcd->oam);
203203

204204
if (wavofs) memcpy(snd.wave, gbc_filebuf+wavofs, sizeof snd.wave);
205205
else memcpy(snd.wave, ram.hi+0x30, 16); /* patch data from older files */
@@ -212,7 +212,7 @@ void loadstate(FILE *f)
212212
fread(ram.ibank, 4096, irl, f);
213213

214214
fseek(f, vramblock<<12, SEEK_SET);
215-
fread(lcd.vbank, 4096, vrl, f);
215+
fread(lcd->vbank, 4096, vrl, f);
216216

217217
fseek(f, sramblock<<12, SEEK_SET);
218218

@@ -377,8 +377,8 @@ struct svar svars[] =
377377
header[i][0] = header[i][1] = 0;
378378

379379
memcpy(gbc_filebuf+hiofs, ram.hi, sizeof ram.hi);
380-
memcpy(gbc_filebuf+palofs, lcd.pal, sizeof lcd.pal);
381-
memcpy(gbc_filebuf+oamofs, lcd.oam.mem, sizeof lcd.oam);
380+
memcpy(gbc_filebuf+palofs, lcd->pal, sizeof lcd->pal);
381+
memcpy(gbc_filebuf+oamofs, lcd->oam.mem, sizeof lcd->oam);
382382
memcpy(gbc_filebuf+wavofs, snd.wave, sizeof snd.wave);
383383

384384
fseek(f, 0, SEEK_SET);
@@ -388,7 +388,7 @@ struct svar svars[] =
388388
fwrite(ram.ibank, 4096, irl, f);
389389

390390
fseek(f, vramblock<<12, SEEK_SET);
391-
fwrite(lcd.vbank, 4096, vrl, f);
391+
fwrite(lcd->vbank, 4096, vrl, f);
392392

393393
fseek(f, sramblock<<12, SEEK_SET);
394394

components/gbc/src/gameboy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void init_gameboy(const std::string& rom_filename, uint8_t *romdata, size_t rom_
113113
gbc_get_memory_regions(&vram, &wram, &audio);
114114

115115
// Use shared memory regions
116-
lcd.vbank = vram;
116+
lcd->vbank = vram;
117117
ram.ibank = wram;
118118
pcm.buf = (int16_t*)audio;
119119
static constexpr int GBC_AUDIO_BUFFER_SIZE = GAMEBOY_AUDIO_SAMPLE_RATE * 2 * 2 / 5; // TODO: 5 is a hack to make it work

components/gbc/src/gbc_shared_memory.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,63 +31,46 @@ static uint8_t* wram_ptr = nullptr;
3131
static uint8_t* audio_ptr = nullptr;
3232
struct cpu *cpu = nullptr;
3333

34+
// from lcd.c
35+
extern byte *bgdup; // [256]
36+
3437
void gbc_init_shared_memory(void) {
3538
// Allocate VRAM in shared memory
36-
shared_mem_request_t vram_request = {
37-
.size = GBC_VRAM_SIZE,
38-
.region = SHARED_MEM_DEFAULT
39-
};
40-
vram_ptr = (uint8_t*)shared_mem_allocate(&vram_request);
39+
vram_ptr = (uint8_t*)shared_malloc(GBC_VRAM_SIZE);
4140
if (!vram_ptr) {
4241
ESP_LOGE(TAG, "Failed to allocate VRAM");
4342
return;
4443
}
4544
memset(vram_ptr, 0, GBC_VRAM_SIZE);
4645

4746
// Allocate WRAM in shared memory
48-
shared_mem_request_t wram_request = {
49-
.size = GBC_WRAM_SIZE,
50-
.region = SHARED_MEM_DEFAULT
51-
};
52-
wram_ptr = (uint8_t*)shared_mem_allocate(&wram_request);
47+
wram_ptr = (uint8_t*)shared_malloc(GBC_WRAM_SIZE);
5348
if (!wram_ptr) {
5449
ESP_LOGE(TAG, "Failed to allocate WRAM");
5550
return;
5651
}
5752
memset(wram_ptr, 0, GBC_WRAM_SIZE);
5853

5954
// allocate gbc scan object in shared memory
60-
shared_mem_request_t gbc_scan_request = {
61-
.size = sizeof(struct gbc_scan),
62-
.region = SHARED_MEM_DEFAULT
63-
};
64-
scan = (struct gbc_scan*)shared_mem_allocate(&gbc_scan_request);
55+
scan = (struct gbc_scan*)shared_malloc(sizeof(struct gbc_scan));
6556

6657
// allocate gbc file buf in shared memory
67-
shared_mem_request_t gbc_file_request = {
68-
.size = 4096,
69-
.region = SHARED_MEM_DEFAULT
70-
};
71-
gbc_filebuf = (uint8_t*)shared_mem_allocate(&gbc_file_request);
58+
gbc_filebuf = (uint8_t*)shared_malloc(4096);
7259

7360
// allocate CPU structure in shared memory
74-
shared_mem_request_t cpu_request = {
75-
.size = sizeof(struct cpu),
76-
.region = SHARED_MEM_DEFAULT
77-
};
78-
cpu = (struct cpu*)shared_mem_allocate(&cpu_request);
61+
cpu = (struct cpu*)shared_malloc(sizeof(struct cpu));
62+
63+
lcd = (struct lcd*)shared_malloc(sizeof(struct lcd));
7964

8065
// Allocate audio buffer in shared memory
81-
shared_mem_request_t audio_request = {
82-
.size = GBC_AUDIO_BUFFER_SIZE,
83-
.region = SHARED_MEM_DEFAULT
84-
};
85-
audio_ptr = (uint8_t*)shared_mem_allocate(&audio_request);
66+
audio_ptr = (uint8_t*)shared_malloc(GBC_AUDIO_BUFFER_SIZE);
8667
if (!audio_ptr) {
8768
ESP_LOGE(TAG, "Failed to allocate audio buffer");
8869
return;
8970
}
9071
memset(audio_ptr, 0, GBC_AUDIO_BUFFER_SIZE);
72+
73+
bgdup = (byte*)shared_malloc(256);
9174
}
9275

9376
void gbc_free_shared_memory(void) {

0 commit comments

Comments
 (0)