1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-09 09:18:01 +00:00

M4+M5: Optimizer passes, type casting, bank switching, math runtime

Milestone 4 — Optimization & Polish:
- Strength reduction: multiply by power-of-2 → shift left
- Zero-page promotion analysis: rank variables by access frequency
- `as` type casting expression in parser/AST/analyzer
- `scroll(x, y)` statement
- `--asm-dump` flag for viewing generated assembly
- Extended optimizer tests (strength reduction, frequency analysis)

Milestone 5 — Bank Switching & Release:
- Mapper support: MMC1 (1), UxROM (2), MMC3 (4) in parser and ROM builder
- Bank declarations: `bank Name: prg` / `bank Name: chr`
- Linker::with_mapper for mapper-aware ROM generation
- Software multiply (8x8→16, shift-and-add algorithm)
- Software divide (8÷8→8, restoring division algorithm)
- ROM tests for mapper encoding round-trip
- Integration test for MMC1 compilation

210 tests total (18 new), all pre-commit checks pass.

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 00:22:11 +00:00
parent 058f7a87b6
commit 5434dda114
No known key found for this signature in database
15 changed files with 865 additions and 13 deletions

View file

@ -10,6 +10,7 @@ pub struct Program {
pub sprites: Vec<SpriteDecl>,
pub palettes: Vec<PaletteDecl>,
pub backgrounds: Vec<BackgroundDecl>,
pub banks: Vec<BankDecl>,
pub start_state: String,
pub span: Span,
}
@ -53,6 +54,9 @@ pub struct GameDecl {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mapper {
NROM,
MMC1,
UxROM,
MMC3,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -61,6 +65,19 @@ pub enum Mirroring {
Vertical,
}
#[derive(Debug, Clone)]
pub struct BankDecl {
pub name: String,
pub bank_type: BankType,
pub span: Span,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BankType {
Prg,
Chr,
}
#[derive(Debug, Clone)]
pub struct StateDecl {
pub name: String,
@ -150,6 +167,7 @@ pub enum Expr {
Call(String, Vec<Expr>, Span),
ButtonRead(Option<Player>, String, Span),
ArrayLiteral(Vec<Expr>, Span),
Cast(Box<Expr>, NesType, Span),
}
impl Expr {
@ -163,7 +181,8 @@ impl Expr {
| Self::UnaryOp(_, _, s)
| Self::Call(_, _, s)
| Self::ButtonRead(_, _, s)
| Self::ArrayLiteral(_, s) => *s,
| Self::ArrayLiteral(_, s)
| Self::Cast(_, _, s) => *s,
}
}
}
@ -221,6 +240,7 @@ pub enum Statement {
Call(String, Vec<Expr>, Span),
LoadBackground(String, Span),
SetPalette(String, Span),
Scroll(Expr, Expr, Span),
}
#[derive(Debug, Clone)]