Skip to content

Add set_chr_mode() API for MMC3 CHR bank switching#307

Merged
jonathanpeppers merged 6 commits intomainfrom
copilot/support-mmc3-mapper-chr-switching
Mar 21, 2026
Merged

Add set_chr_mode() API for MMC3 CHR bank switching#307
jonathanpeppers merged 6 commits intomainfrom
copilot/support-mmc3-mapper-chr-switching

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 21, 2026

Add set_chr_mode(byte reg, byte bank) API for MMC3 CHR bank switching, following nesdoug's set_chr_mode_X() naming convention but with a register parameter instead of 6 separate functions.

// Set MMC3 register R2 to CHR bank 9
set_chr_mode(0x02, 0x09);

Emits inline 6502: LDA #reg, STA $8000, LDA #bank, STA $8001

  • NESLib.cs -- set_chr_mode(byte reg, byte bank) stub with MMC3 register docs
  • IL2NESWriter.ILDispatch.cs -- Two-param inline dispatch; reg must be constant, bank supports constants and locals
  • PublicAPI.Unshipped.txt -- New API entry
  • RoslynTests.cs -- Tests for constant pairs and local variable bank argument

Fixes #306

Copilot AI and others added 2 commits March 21, 2026 17:26
Copilot AI changed the title [WIP] Add support for MMC3 mapper CHR bank switching Add mmc3_set_chr_bank() API for MMC3 CHR bank switching Mar 21, 2026
Copilot AI requested a review from jonathanpeppers March 21, 2026 17:30
@jonathanpeppers jonathanpeppers marked this pull request as ready for review March 21, 2026 18:21
Copilot AI review requested due to automatic review settings March 21, 2026 18:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a first-class mmc3_set_chr_bank(byte reg, byte bank) API to neslib and wires it through the transpiler so MMC3 (mapper 4) CHR bank switching can be emitted without manual poke() sequences.

Changes:

  • Added NESLib.mmc3_set_chr_bank(reg, bank) public API stub (with MMC3 CHR register docs).
  • Implemented IL dispatcher support to emit $8000/$8001 writes for MMC3 bank select/data.
  • Added a Roslyn-based test asserting the expected 6502 byte sequence and updated PublicAPI.Unshipped.txt.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/neslib/PublicAPI.Unshipped.txt Declares the new public API surface for mmc3_set_chr_bank.
src/neslib/NESLib.cs Adds the mmc3_set_chr_bank stub and XML documentation.
src/dotnes.tasks/Utilities/IL2NESWriter.ILDispatch.cs Adds call-dispatch lowering that emits the MMC3 bank select/data writes.
src/dotnes.tests/RoslynTests.cs Adds a regression test validating emitted bytes for constant reg/bank pairs.

Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.ILDispatch.cs
Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.ILDispatch.cs Outdated
Comment thread src/neslib/NESLib.cs Outdated
Comment thread src/dotnes.tests/RoslynTests.cs
jonathanpeppers and others added 2 commits March 21, 2026 13:27
Follows nesdoug's set_chr_mode_X() convention but uses a register
parameter instead of 6 separate functions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…/tests

- Verify reg is a compile-time constant (LDA immediate); throw TranspileException otherwise
- Remove dead bankIsStaticField branch (_lastStaticFieldAddress is never assigned)
- Update XML docs to document full bank-select byte including mode bits
- Add SetChrMode_SupportsLocalBankArg test for local variable bank argument

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers changed the title Add mmc3_set_chr_bank() API for MMC3 CHR bank switching Add set_chr_mode() API for MMC3 CHR bank switching Mar 21, 2026
Copy link
Copy Markdown
Owner

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All 4 review comments addressed. API renamed from mmc3_set_chr_bank to set_chr_mode (nesdoug-style). All 569 tests pass.

@jonathanpeppers jonathanpeppers requested a review from Copilot March 21, 2026 18:35
@jonathanpeppers jonathanpeppers merged commit e5c2806 into main Mar 21, 2026
4 of 5 checks passed
@jonathanpeppers jonathanpeppers deleted the copilot/support-mmc3-mapper-chr-switching branch March 21, 2026 18:38
Copilot AI review requested due to automatic review settings March 23, 2026 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support MMC3 mapper for CHR bank switching (crypto sample)

3 participants