Skip to content

Commit

Permalink
[NES] Move aligned buffers to .aligned_buffers at 0x200
Browse files Browse the repository at this point in the history
The NES typically has noinit buffers of high alignment; given its
paucity of RAM, this risks wasting a ton of space. Typically these
buffers are placed immediately after the hard stack at 0x200, so this
change change creates an `.aligned_buffers` section there. OAM_BUF,
VRAM_BUF, and PAL_BUF in the SDK are placed there, with alignments 256,
128, and 32, respectively. The linker already sorts by decreasing
alignment, so no space is then ever wasted for these buffers.

Fixes #219
Fixes #229
  • Loading branch information
mysterymath committed Oct 15, 2023
1 parent e7ce558 commit b3c6a27
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions mos-platform/nes/nes.ld
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ SECTIONS {
*(.nmi_end)
} >c_readonly
INCLUDE rodata.ld
/* A naturally page-aligned place (0x200) to place noinit buffers with high
* alignment requirement (e.g. OAM) */
.aligned (NOLOAD) : { *(.aligned_buffers .aligned_buffers.*) } >ram
INCLUDE data.ld
INCLUDE zp.ld
INCLUDE bss.ld
Expand Down
2 changes: 1 addition & 1 deletion mos-platform/nes/nesdoug/vram_buffer.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

.zeropage VRAM_INDEX

.section .noinit.vram_buf,"aw",@nobits
.section .aligned.vram_buf,"aw",@nobits
.weak VRAM_BUF
.balign 128
VRAM_BUF:
Expand Down
2 changes: 1 addition & 1 deletion mos-platform/nes/neslib/oam_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// information.

__attribute__((section(".zp.sprid"))) unsigned SPRID;
__attribute__((weak, aligned(256), section(".noinit.oam_buf"))) char OAM_BUF[256];
__attribute__((weak, aligned(256), section(".aligned.oam_buf"))) char OAM_BUF[256];

void oam_set(char index) { SPRID = index & 0xfc; }

Expand Down
2 changes: 1 addition & 1 deletion mos-platform/nes/neslib/pal_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// information.

__attribute__((section(".zp.pal_update"))) volatile char PAL_UPDATE;
__attribute__((weak, aligned(32), section(".noinit.pal_buf"))) volatile char PAL_BUF[32];
__attribute__((weak, aligned(32), section(".aligned.pal_buf"))) volatile char PAL_BUF[32];

void pal_col(char index, char color) {
PAL_BUF[index & 0x1f] = color;
Expand Down

0 comments on commit b3c6a27

Please sign in to comment.