# MUD64 Native Compile (MUD64CMP)

> Compile `.mud` **on the Commodore 64** — same IR as [Code Designer Flight Deck](../MUD64-SPEC.md#54-dual-compile--code-designer-or-native-c64).

## Overview

| Component | Role |
|-----------|------|
| **MUD64CMP** | On-disk 6510 compiler (`LOAD"MUD64CMP",8,1`) |
| **MUD64.LIB** | Runtime: frame router, PETSCII plotter, raycast engine, optional sprite mux |
| **MODULE.MUD** | Your source (plain PETSCII text, 40-col safe) |
| **MODULE.PRG** | Output — autostart or `SYS` entry |

Code Designer and native compile share **MUD64-IR**: frame graph, `++ c64.*` manifests, PETSCII plot opcode stream, **raycast opcode stream**.

## Quick start (real hardware)

```
LOAD"MUD64CMP",8,1
RUN
INPUT"MODULE NAME";N$
OPEN 1,8,0,N$+".MUD":REM read source
... compiles to MODULE.PRG ...
SAVE"MODULE",8:REM or autostart D64
```

## Memory map (default PAL)

| Region | Address | Contents |
|--------|---------|----------|
| BASIC load | `$0801` | Generated PRG |
| Plotter | `$0C00` | `PLOT_*` routines from `++ c64.petscii` |
| **Raycast engine** | `$1000` | `RAYCAST_*` cast + blit from `++ c64.raycast` |
| Frame table | `$2000` | Branch vectors + text pointers |
| **Maze grids** | `$2800` | Level map bytes (max 16×16 × 4 levels typical) |
| Screen | `$0400` | PETSCII semigraphics output (40×25) |
| **Z-buffer / color LUT** | `$2A00` | Per-column depth + VIC fg/bg indices |
| **Trig LUT** | `$C000` | sin/cos 256-entry 8.8 fixed (shared with `++ c64.math`) |
| Mesh LUT | `$C200` | Optional REU / expanded RAM for 3D precalc |

## PETSCII plot opcodes (native)

Same opcodes CD emits — see MUD64-SPEC §5.4:

- `PLOT_CLEAR` · `PLOT_LINE` · `PLOT_CIRCLE` · `PLOT_TUNNEL` · `PLOT_MESH`

## Raycast opcodes (native)

Same opcodes CD emits — see MUD64-SPEC §10.6:

| Opcode | Args | Notes |
|--------|------|-------|
| `RAYCAST_INIT` | cols, rows, fov | Viewport + LUT ptr |
| `RAYCAST_MAP` | level_id, w, h, data_ptr | Maze @ `$2800` |
| `RAYCAST_TEXTURE` | wall_type, pattern_ptr | Stone / metal / hell patterns |
| `RAYCAST_SPAWN` | level_id, px, py, pa | 8.8 fixed player vector |
| `RAYCAST_SPRITE` | id, x, y, art_ptr | Billboard imp |
| `RAYCAST_WALK` | mode, speed, turn | `auto` or input IRQ |
| `RAYCAST_FRAME` | frame_id, scene_name | Frame graph bind |

**Compile include:** add `++ c64.raycast` to `++ c64.compile` `cd:` / `native:` manifest (alongside `++ c64.petscii`).

Reference module: [`examples/mud-doom-demo.mud`](../examples/mud-doom-demo.mud) → `out/MUDDOOM.PRG`.

## Verify dual build (Team DC QA)

When `++ c64.compile target=both`:

1. Export from Flight Deck → `out/MODULE.PRG`
2. Compile on C64 → `MODULE.PRG` on disk
3. Compare SHA256 in covenant manifest (optional `VERIFY both` flag)

Mismatch → check `machine: c64pal` vs `c64ntsc` raster tables.

## Roadmap

- [ ] MUD64CMP v0.1 tokenizer (6510)
- [ ] PETSCII plotter ROM @ `$0C00`
- [ ] **Raycast engine ROM @ `$1000`** (fixed-point cast + PETSCII blit)
- [ ] Flight Deck export parity test suite (plot + raycast IR)
- [ ] `.D64` with MUD64CMP + MUDDOOM module for demo parties

**FutureVision Labs · Team DC · [homebrewz.live](https://homebrewz.live)**
