monsoon-cli 0.2.2

Headless command-line interface for the Monsoon NES emulator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# CLI Interface Design Document

## Table of Contents

1. [Overview]#overview
2. [Design Philosophy]#design-philosophy
3. [Architecture]#architecture
4. [Command Line Arguments]#command-line-arguments
5. [ROM Loading]#rom-loading
6. [Savestate Management]#savestate-management
7. [Memory Operations]#memory-operations
8. [Power Control]#power-control
9. [Palette Configuration]#palette-configuration
10. [Video Export]#video-export
11. [Execution Control]#execution-control
12. [Output Formats]#output-formats
13. [Example Workflows]#example-workflows
14. [Implementation Notes]#implementation-notes

---

## Overview

This document describes a standardized CLI interface for the NES emulator that enables programmatic automation of emulation tasks. The interface is designed to support complex multi-step behaviors driven entirely by command-line arguments,
making it suitable for:

- Automated testing and verification
- Tool-assisted speedrun (TAS) development
- Memory inspection and debugging
- Batch processing and screenshot/video generation
- Integration with external tools and scripts

The CLI interface builds upon the existing message-based architecture (`FrontendMessage`/`EmulatorMessage`) and extends the current headless mode with comprehensive control capabilities.

---

## Design Philosophy

### Principles

1. **Composability**: Individual operations should be combinable to create complex workflows
2. **Reproducibility**: Given the same inputs, the emulator should produce identical outputs
3. **Discoverability**: Options should be self-documenting with sensible defaults
4. **Safety**: Destructive operations should require explicit confirmation
5. **Integration**: Seamlessly integrate with the existing message-based architecture

### Consistency with Existing Code

The CLI interface should map directly to existing `FrontendMessage` variants where possible:

| CLI Operation    | Existing Message                                 |
|------------------|--------------------------------------------------|
| Load ROM         | `FrontendMessage::LoadRom(PathBuf)`              |
| Reset            | `FrontendMessage::Reset`                         |
| Power On         | `FrontendMessage::Power`                         |
| Power Off        | `FrontendMessage::PowerOff`                      |
| Set Palette      | `FrontendMessage::SetPalette(Box<RgbPalette>)`   |
| Write CPU Memory | `FrontendMessage::WriteCpu(u16, u8)`             |
| Write PPU Memory | `FrontendMessage::WritePpu(u16, u8)`             |
| Load Savestate   | `FrontendMessage::LoadSaveState(Box<SaveState>)` |
| Create Savestate | `FrontendMessage::CreateSaveState`               |

---

## Architecture

### High-Level Architecture

```
┌─────────────────────────────────────────────────────────────────────┐
│                        CLI Entry Point                              │
│                   (core/src/bin/main.rs)                            │
└──────────────────────────┬──────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────┐
│                     Argument Parser (clap)                          │
│                                                                     │
│  • Parses and validates all CLI arguments                           │
│  • Builds CliConfig struct with all options                         │
│  • Handles argument groups and conflicts                            │
└──────────────────────────┬──────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────┐
│                      CLI Execution Engine                           │
│                                                                     │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────────────────┐   │
│  │   Phase 1:   │  │   Phase 2:   │  │        Phase 3:          │   │
│  │    Setup     │─▶│  Initialize  │─▶│        Execute          │   │
│  │              │  │              │  │                          │   │
│  │ • Load ROM   │  │ • Init Memory│  │ • Run until condition    │   │
│  │ • Load State │  │ • Set Palette│  │ • Handle stop triggers   │   │
│  │ • Power On   │  │ • Init Regs  │  │ • Capture outputs        │   │
│  └──────────────┘  └──────────────┘  └──────────────────────────┘   │
└──────────────────────────┬──────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────┐
│                     Output Handler                                  │
│                                                                     │
│  • Memory dumps (hex/binary/JSON/toml)                                   │
│  • Screenshots (PNG)                                                │
│  • Video files (using external encoder)                             │
│  • Savestates (rkyv serialized)                                     │
│  • Debug viewer exports                                             │
└─────────────────────────────────────────────────────────────────────┘
```

### Integration with Existing Code

The CLI engine should leverage the existing `Nes` struct and its methods:

```rust
// From core/src/emulation/nes.rs
impl Nes {
    pub fn power(&mut self);           // Power on the console
    pub fn power_off(&mut self);       // Power off the console
    pub fn reset(&mut self);           // Reset the console
    pub fn load_rom<T>(&mut self, rom_get: &T);  // Load a ROM
    pub fn save_state(&self) -> SaveState;       // Create savestate
    pub fn load_state(&mut self, state: SaveState);  // Load savestate
    pub fn run_until(&mut self, last_cycle: u128);   // Run until cycle
    pub fn step_frame(&mut self);      // Run one frame
    pub fn get_memory_debug(&self, range) -> Vec<Vec<u8>>;  // Memory dump
}
```

### Proposed New Module Structure

```
core/src/
├── bin/
│   └── main.rs              # Entry point (updated)
├── cli/
│   ├── mod.rs               # CLI module root
│   ├── args.rs              # Argument definitions (clap derive)
│   ├── config.rs            # CliConfig struct
│   ├── engine.rs            # CLI execution engine
│   ├── memory_ops.rs        # Memory read/write operations
│   ├── output.rs            # Output formatting/export
│   └── stop_conditions.rs   # Execution stop conditions
└── ...
```

---

## Command Line Arguments

### Argument Organization

Arguments are organized into logical groups:

#### Global Options

| Flag        | Long Form    | Description               | Type    | Default |
|-------------|--------------|---------------------------|---------|---------|
| `-H`        | `--headless` | Run without GUI           | bool    | false   |
| `-q`        | `--quiet`    | Suppress non-error output | bool    | false   |
| `-v`        | `--verbose`  | Enable verbose output     | bool    | false   |
| `--version` |              | Print version and exit    |         |         |
| `--help`    |              | Print help information    |         |         |
| `-c`        | `--config`   | Load config from file     | PathBuf | false   |

#### ROM Loading

| Flag | Long Form    | Description                    | Type    |
|------|--------------|--------------------------------|---------|
| `-r` | `--rom`      | Path to ROM file               | PathBuf |
|      | `--rom-info` | Print ROM information and exit | bool    |

#### Savestate Operations

| Flag | Long Form         | Description                       | Type    |
|------|-------------------|-----------------------------------|---------|
| `-l` | `--load-state`    | Load savestate from file          | PathBuf |
| `-s` | `--save-state`    | Save state to file on exit        | PathBuf |
|      | `--state-stdin`   | Read savestate from stdin         | bool    |
|      | `--state-stdout`  | Write savestate to stdout on exit | bool    |
|      | `--save-state-on` | When to save state (see below)    | String  |

**`--save-state-on` Options:**

- `exit` - Save when emulator exits normally
- `stop` - Save when any stop condition is triggered

#### Memory Operations

| Flag | Long Form           | Description                | Type    |
|------|---------------------|----------------------------|---------|
|      | `--read-cpu`        | Read CPU memory range      | String  |
|      | `--read-ppu`        | Read PPU memory range      | String  |
|      | `--dump-oam`        | Dump OAM (sprite) memory   | bool    |
|      | `--dump-nametables` | Dump Nametables            | bool    |
|      | `--init-cpu`        | Initialize CPU memory      | String  |
|      | `--init-ppu`        | Initialize PPU memory      | String  |
|      | `--init-oam`        | Initialize OAM             | String  |
|      | `--init-file`       | Load init values from file | PathBuf |

**Memory Range Format:** `START-END` or `START:LENGTH` (hex addresses)

- Examples: `0x0000-0x07FF`, `0x6000:0x2000`, `0x2000-0x3FFF`

**Memory Init Format:** `ADDR=VALUE` or `ADDR=VALUE1,VALUE2,...` (hex)

- Examples: `0x0000=0xFF`, `0x6000=0x01,0x02,0x03,0x04`

**Memory Init from File:** Configure memory init in file with json/toml/binary format

#### Power Control

| Flag | Long Form    | Description                        | Type | Default |
|------|--------------|------------------------------------|------|---------|
|      | `--no-power` | Don't auto-power on after ROM load | bool | false   |
|      | `--reset`    | Reset after loading                | bool | false   |

#### Palette Configuration

| Flag | Long Form           | Description                   | Type    |
|------|---------------------|-------------------------------|---------|
| `-p` | `--palette`         | Path to .pal RGB palette file | PathBuf |
|      | `--palette-builtin` | Use built-in palette by name  | String  |

**Built-in Palettes:**

- `2C02G` (default) - Standard 2C02G palette
- `composite` - NTSC composite simulation

#### Video/Screenshot Export

| Flag | Long Form                       | Description                                      | Type    |
|------|---------------------------------|--------------------------------------------------|---------|
|      | `--screenshot`                  | Save screenshot on exit                          | PathBuf |
|      | `--screenshot-on`               | When to capture (same as save-state-on)          | String  |
|      | `--video-path`                  | Record video to file                             | PathBuf |
|      | `--video-format`                | Video output format                              | String  |
|      | `--video-fps`                   | Video frame rate (multiplier like "2x" or fixed) | String  |
|      | `--video-mode`                  | Video export mode (accurate or smooth)           | String  |
|      | `--video-scale`                 | Video output resolution                          | String  |

**Video Formats:**

- `raw` - Raw RGBA frames (for piping to FFmpeg)
- `ppm` - PPM image sequence
- `png` - PNG image sequence
- `mp4` - MP4 video

**Video Export Modes:**

- `accurate` - Encode at exact NES framerate (60.0988 fps or its multiple). This is the default mode and preserves the exact timing of the original hardware.
- `smooth` - Encode at exactly 60 fps (or its multiple), accepting slight timing drift. This produces videos that are more compatible with standard video players and avoid visual artifacts on displays that expect standard framerates.

**Video FPS:**

The `--video-fps` option accepts either:
- Multipliers like `1x`, `2x`, `3x` (default is `1x`)
- Fixed values like `60`, `120`, `180`

When using multipliers greater than 1x, the emulator captures the framebuffer more frequently, inserting "half-finished" frames between complete PPU frames. This allows for smoother slow-motion playback or higher framerate output.

Examples:
- `--video-fps 1x --video-mode accurate`: 60.0988 fps (exact NES timing)
- `--video-fps 1x --video-mode smooth`: 60.0 fps (standard timing)
- `--video-fps 2x --video-mode accurate`: 120.1976 fps
- `--video-fps 2x --video-mode smooth`: 120.0 fps
- `--video-fps 120`: Converted to 2x multiplier based on mode

#### Execution Control

| Flag | Long Form        | Description                           | Type     |
|------|------------------|---------------------------------------|----------|
| `-c` | `--cycles`       | Run for N master cycles               | u128     |
| `-f` | `--frames`       | Run for N frames                      | u64      |
|      | `--until-opcode` | Run until specific opcode executes    | u8       |
|      | `--until-mem`    | Run until memory condition            | String   |
|      | `--until-hlt`    | Run until HLT instruction             | bool     |
|      | `--trace`        | Enable instruction trace              | PathBuf  |
|      | `--breakpoint`   | Set breakpoint at PC address          | Vec<u16> |
|      | `--watch-mem`    | Watch memory for access (read/write)  | Vec<String> |

**Memory Condition Format:** `ADDR==VALUE`, `ADDR!=VALUE`, `ADDR&MASK==VALUE`

- Examples: `0x6000==0x80`, `0x2002&0x80!=0x00`

**Memory Watch Format:** `ADDR` or `ADDR:MODE` where MODE is `r` (read), `w` (write), or `rw` (both)

- Examples: `0x2002` (any access), `0x2002:r` (reads only), `0x4016:w` (writes only)

#### Output Control

| Flag | Long Form         | Description                  | Type    | Default |
|------|-------------------|------------------------------|---------|---------|
| `-o` | `--output`        | Output file for memory dumps | PathBuf | stdout  |
|      | `--output-format` | Output format                | String  | hex     |
|      | `--json`          | Output in JSON format        | bool    | false   |
|      | `--toml`          | Output in TOML format        | bool    | false   |
|      | `--binary`        | Output in binary format      | bool    | false   |

---

## ROM Loading

### Basic ROM Loading

```bash
# Load and run ROM
nes_main --headless --rom game.nes --frames 100

# Load ROM without auto-power
nes_main --headless --rom game.nes --no-power

# Print ROM information only
nes_main --rom game.nes --rom-info
```

### ROM Information Output

The `--rom-info` flag should output:

```
ROM Information:
  File: game.nes
  Name: Super Mario Bros.
  Format: iNES 2.0
  Mapper: 0 (NROM)
  PRG ROM: 32 KB
  CHR ROM: 8 KB
  PRG RAM: 8 KB (battery-backed: no)
  Mirroring: Horizontal
  Console Type: NES/Famicom
  Checksum (SHA-256): abc123...
```

---

## Savestate Management

### Basic Savestate Operations

```bash
# Save state after running
nes_main -H --rom game.nes --frames 100 --save-state state.sav

# Load existing state
nes_main -H --rom game.nes --load-state state.sav --frames 100

# Chain operations: load state, run, save new state
nes_main -H --rom game.nes -l input.sav --frames 60 -s output.sav
```

### Pipe-Based Savestates (Streaming Workflows)

For multistep automation pipelines, savestates can be read from stdin and written to stdout:

```bash
# Single step pipeline
nes_main -H --rom game.nes --frames 100 --state-stdout | \
nes_main -H --rom game.nes --state-stdin --frames 50 --state-stdout | \
nes_main -H --rom game.nes --state-stdin --frames 25 --save-state final.sav
```

### Savestate Format

Savestates use the existing `rkyv` serialization format from `savestate.rs`. The structure includes:

```rust
pub struct SaveState {
    pub cpu: CpuState,           // CPU registers and RAM
    pub ppu: PpuState,           // PPU state and VRAM
    pub rom_file: RomFile,       // ROM metadata (for verification)
    pub version: u16,            // Savestate format version
    pub total_cycles: u128,      // Total elapsed cycles
    pub cycle: u8,               // Current sub-cycle
    pub ppu_cycle_counter: u8,   // PPU cycle position
    pub cpu_cycle_counter: u8,   // CPU cycle position
}
```

### Conditional Savestates

```bash
# Save at specific cycle
nes_main -H --rom game.nes --save-state-on cycle:1000000 -s milestone.sav

# Save when PC reaches address (e.g., level complete routine)
nes_main -H --rom game.nes --save-state-on pc:0x8500 -s level_complete.sav

# Save on any stop condition
nes_main -H --rom game.nes --until-pc 0x8500 --save-state-on stop -s stopped.sav
```

---

## Memory Operations

### Reading Memory

#### CPU Memory (Addresses 0x0000-0xFFFF)

```bash
# Read zero page
nes_main -H --rom game.nes --frames 100 --read-cpu 0x0000-0x00FF

# Read RAM (with mirrors)
nes_main -H --rom game.nes --frames 100 --read-cpu 0x0000-0x07FF

# Read PRG RAM (save data area)
nes_main -H --rom game.nes --frames 100 --read-cpu 0x6000-0x7FFF

# Read specific range with length
nes_main -H --rom game.nes --frames 100 --read-cpu 0x6000:0x100

# Output to file
nes_main -H --rom game.nes --frames 100 --read-cpu 0x0000-0x07FF -o ram.bin --binary
```

#### PPU Memory (Addresses 0x0000-0x3FFF)

```bash
# Read pattern tables (CHR ROM/RAM)
nes_main -H --rom game.nes --frames 100 --read-ppu 0x0000-0x1FFF

# Read nametables
nes_main -H --rom game.nes --frames 100 --read-ppu 0x2000-0x2FFF

# Read palette RAM
nes_main -H --rom game.nes --frames 100 --dump-palette
```

#### OAM (Sprite Memory)

```bash
# Dump full OAM (256 bytes, 64 sprites)
nes_main -H --rom game.nes --frames 100 --dump-oam

# JSON format with sprite interpretation
nes_main -H --rom game.nes --frames 100 --dump-oam --json
```

### Initializing Memory

#### CPU Memory Initialization

```bash
# Set single byte
nes_main -H --rom game.nes --init-cpu 0x0050=0xFF --frames 100

# Set multiple bytes
nes_main -H --rom game.nes --init-cpu 0x0050=0x01,0x02,0x03,0x04 --frames 100

# Multiple init operations
nes_main -H --rom game.nes \
  --init-cpu 0x0050=0xFF \
  --init-cpu 0x0060=0x01,0x02 \
  --frames 100
```

#### PPU Memory Initialization

```bash
# Initialize VRAM
nes_main -H --rom game.nes --init-ppu 0x2000=0x20,0x20,0x20 --frames 100

# Initialize palette RAM
nes_main -H --rom game.nes --init-ppu 0x3F00=0x0F,0x00,0x10,0x20 --frames 100
```

#### Initialization from File

```bash
# Init file format (JSON):
# {
#   "cpu": {"0x0050": [1, 2, 3, 4], "0x0060": [255]},
#   "ppu": {"0x3F00": [15, 0, 16, 32]},
# }
nes_main -H --rom game.nes --init-file init.json --frames 100
```

### Memory Access Timing

Memory initialization happens:

1. **After ROM loading** - ROM is loaded and mapped
2. **After power-on** - CPU/PPU are in initialized state
3. **After savestate load** - If loading a savestate
4. **Before execution** - Just before running cycles/frames

This ensures that initialized values are present when execution begins.

---

## Power Control

### Power Sequence

```bash
# Normal: Load ROM → Power On → Execute
nes_main -H --rom game.nes --frames 100

# Manual power control
nes_main -H --rom game.nes --no-power  # ROM loaded but not powered
```

### Reset Operations

```bash
# Power on then immediately reset (mimics physical reset)
nes_main -H --rom game.nes --reset --frames 100

# Multiple resets (for testing reset behavior)
# First run to state, then reset
nes_main -H --rom game.nes --frames 100 -s pre_reset.sav
nes_main -H --rom game.nes -l pre_reset.sav --reset --frames 100 -s post_reset.sav
```

---

## Palette Configuration

### Loading Custom Palettes

```bash
# Load custom .pal file (192-byte or 1536-byte format)
nes_main -H --rom game.nes --palette custom.pal --frames 100

# Use built-in palette
nes_main -H --rom game.nes --palette-builtin 2C02G --frames 100
```

### Palette File Format

The emulator supports the standard NES palette format:

**192-byte format (single palette):**

- 64 colors × 3 bytes (RGB) = 192 bytes
- Colors are in NES palette order (0x00-0x3F)

**1536-byte format (8 emphasis variants):**

- 8 emphasis modes × 64 colors × 3 bytes = 1536 bytes
- Emphasis modes: Normal, R, G, RG, B, RB, GB, RGB

### Palette Emphasis

When using the 1536-byte format, the correct emphasis palette is automatically selected based on the PPU mask register bits.

---

## Video Export

### Screenshots

```bash
# Screenshot on exit
nes_main -H --rom game.nes --frames 100 --screenshot frame100.png

# Screenshot at specific frame
nes_main -H --rom game.nes --screenshot-on frame:100 --screenshot shot.png

# Screenshot when reaching specific address
nes_main -H --rom game.nes --screenshot-on pc:0x8500 --screenshot level_end.png
```

### Video Recording

```bash
# Record MP4 with accurate NES timing (60.0988 fps)
nes_main -H --rom game.nes --frames 600 --video-path video.mp4 --video-format mp4 --video-mode accurate

# Record MP4 with smooth 60fps timing (avoids player artifacts)
nes_main -H --rom game.nes --frames 600 --video-path video.mp4 --video-format mp4 --video-mode smooth

# Record at 2x framerate (120fps) - captures mid-frame states
nes_main -H --rom game.nes --frames 600 --video-path video_2x.mp4 --video-format mp4 --video-fps 2x

# Record at 2x smooth framerate (exactly 120fps)
nes_main -H --rom game.nes --frames 600 --video-path video_2x_smooth.mp4 --video-format mp4 --video-fps 2x --video-mode smooth

# Record raw frames (pipe to ffmpeg)
nes_main -H --rom game.nes --frames 600 --video-format raw --video-path - | \
  ffmpeg -f rawvideo -pixel_format rgba -video_size 256x240 \
         -framerate 60 -i - output.mp4

# Record PNG sequence
nes_main -H --rom game.nes --frames 600 --video-path frames.png --video-format png

# Record PPM sequence (faster, larger files)
nes_main -H --rom game.nes --frames 600 --video-path frames.ppm --video-format ppm
```

---

## Execution Control

### Cycle-Based Execution

```bash
# Run for exact number of master cycles
nes_main -H --rom game.nes --cycles 1000000

# Run for exact number of frames
nes_main -H --rom game.nes --frames 60
```

### Conditional Stop

```bash
# Stop when PC reaches address (use --breakpoint instead of deprecated --until-pc)
nes_main -H --rom game.nes --breakpoint 0x8500

# Stop when specific opcode executes (0x02 is KIL, an illegal "halt" opcode)
nes_main -H --rom game.nes --until-opcode 0x02  # Stop on KIL (illegal halt)

# Stop when memory condition is met
nes_main -H --rom game.nes --until-mem "0x6000==0x80"

# Combined conditions (stops on first match)
nes_main -H --rom game.nes --frames 3600 --breakpoint 0x8500 --until-mem "0x6000==0x80"
```

### Breakpoints

```bash
# Set PC breakpoints (execution stops when PC reaches these addresses)
nes_main -H --rom game.nes --breakpoint 0x8000 --breakpoint 0x8500 --trace trace.log
```

### Memory Watchpoints

Stop execution when the CPU accesses a specific memory address:

```bash
# Watch for any access (read or write) to address
nes_main -H --rom game.nes --watch-mem 0x2002 --frames 3600

# Watch for reads only (e.g., PPU status register)
nes_main -H --rom game.nes --watch-mem 0x2002:r --frames 3600

# Watch for writes only (e.g., controller port)
nes_main -H --rom game.nes --watch-mem 0x4016:w --frames 3600

# Multiple watchpoints
nes_main -H --rom game.nes --watch-mem 0x2002:r --watch-mem 0x2007:rw --frames 3600
```

### Instruction Tracing

The trace format should be compatible with existing trace systems:

```bash
# Enable tracing to file
nes_main -H --rom game.nes --trace execution.log --frames 10
```

**Trace Format:**

```
C000  78        SEI            A:00 X:00 Y:00 P:04 SP:FD CYC:7
C001  D8        CLD            A:00 X:00 Y:00 P:04 SP:FD CYC:9
C002  A9 10     LDA #$10       A:00 X:00 Y:00 P:04 SP:FD CYC:11
...
```

---

## Output Formats

### Hexadecimal (Default)

```
0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...
```

### Binary

Raw binary data written to file or stdout.

### JSON

```json
{
	"memory_dump": {
		"type": "cpu",
		"start": "0x0000",
		"end": "0x07FF",
		"data": "AAAAAAAAAAAAAAAA..."
	},
	"registers": {
		"pc": "0xC000",
		"a": "0x00",
		"x": "0x00",
		"y": "0x00",
		"sp": "0xFD",
		"p": "0x04"
	},
	"cycles": 1000000,
	"frames": 16
}
```

### OAM Interpret Format (Json example)

```json
{
	"sprites": [
		{
			"index": 0,
			"y": 64,
			"tile": 1,
			"attributes": {
				"palette": 0,
				"priority": false,
				"flip_h": false,
				"flip_v": false
			},
			"x": 128
		},
		...
	],
	"raw": "base64..."
}
```

---

## Example Workflows

### Workflow 1: Automated ROM Testing

```bash
#!/bin/bash
# Test ROM runs without crashing for 1 minute of game time

nes_main --headless \
  --rom "$1" \
  --frames 3600 \
  --quiet

if [ $? -eq 0 ]; then
  echo "PASS: $1"
else
  echo "FAIL: $1"
fi
```

### Workflow 2: Memory Comparison Tool

```bash
#!/bin/bash
# Compare RAM state before and after running

nes_main -H --rom game.nes --frames 100 --read-cpu 0x0000-0x07FF -o before.bin --binary
nes_main -H --rom game.nes --frames 200 --read-cpu 0x0000-0x07FF -o after.bin --binary
diff before.bin after.bin && echo "No changes" || echo "Memory changed"
```

### Workflow 3: TAS Input Testing

```bash
#!/bin/bash
# Test a sequence of inputs using savestate chains

# Frame 0: Start state
nes_main -H --rom game.nes --frames 1 --state-stdout > state0.sav

# Apply input A, run 10 frames
cat state0.sav | nes_main -H --rom game.nes --state-stdin \
  --init-cpu 0x4016=0x01 --frames 10 --state-stdout > state1.sav

# Apply input B, run 10 frames  
cat state1.sav | nes_main -H --rom game.nes --state-stdin \
  --init-cpu 0x4016=0x02 --frames 10 --state-stdout > state2.sav

# Check final state
cat state2.sav | nes_main -H --rom game.nes --state-stdin \
  --read-cpu 0x0050-0x0060 --json
```

### Workflow 4: Sprite Extraction

```bash
#!/bin/bash
# Extract sprite data at specific game moment

nes_main -H --rom game.nes \
  --until-pc 0x8500 \
  --read-oam --json \
  --export-sprites sprites.png
```

### Workflow 5: Screenshot Generation Pipeline

```bash
#!/bin/bash
# Generate screenshots at regular intervals

for frame in 100 200 300 400 500; do
  nes_main -H --rom game.nes \
    --frames $frame \
    --screenshot "frame_$frame.png"
done
```

### Workflow 6: Conditional Execution with Fallback

```bash
#!/bin/bash
# Run until condition, but timeout after 1 hour of game time

nes_main -H --rom game.nes \
  --frames 216000 \
  --until-mem "0x6000==0x80" \
  --save-state-on stop \
  --save-state result.sav

if [ $? -eq 0 ]; then
  echo "Condition met, state saved"
else
  echo "Timeout reached"
fi
```

---

## Implementation Notes

### Integration with Existing Architecture

#### Leveraging ChannelEmulator

The CLI can optionally use the `ChannelEmulator` infrastructure for consistency:

```rust
// Option 1: Direct Nes manipulation (current headless approach)
let mut emu = Nes::default ();
emu.load_rom( & rom_path);
emu.power();
emu.run_until(target_cycles);

// Option 2: Message-based approach (more consistent with GUI)
let ( mut channel_emu, tx, rx) = ChannelEmulator::new(Nes::default ());
tx.send(FrontendMessage::LoadRom(rom_path));
tx.send(FrontendMessage::Power);
// Process messages in a loop
```

For the CLI, **Option 1 (direct manipulation)** is recommended for:

- Lower overhead
- Simpler control flow
- Easier cycle-accurate timing

However, option 2 should be used when features require it (e.g., debug data that goes through the message system).

### Memory Init Implementation

Memory initialization should map to existing memory write operations. The method naming
uses `init` to indicate this happens before execution begins, distinguishing it from
runtime memory writes:

```rust
// In the CLI engine
fn apply_memory_init(nes: &mut Nes, cpu_inits: &[(u16, Vec<u8>)], ppu_inits: &[(u16, Vec<u8>)]) {
    for (addr, bytes) in cpu_inits {
        for (i, byte) in bytes.iter().enumerate() {
            // Uses the existing init method which bypasses normal bus behavior
            nes.cpu.memory.init(*addr + i as u16, *byte);
        }
    }

    for (addr, bytes) in ppu_inits {
        for (i, byte) in bytes.iter().enumerate() {
            // Uses mem_init for direct PPU memory initialization
            nes.ppu.borrow_mut().mem_init(*addr + i as u16, *byte);
        }
    }
}
```

### Stop Condition Implementation

```rust
pub enum StopCondition {
    Cycles(u128),
    Frames(u64),
    ProgramCounter(u16),
    Opcode(u8),
    MemoryCondition {
        address: u16,
        mask: u8,
        operation: CompareOp,
        value: u8,
    },
    Halt,
}

pub enum CompareOp {
    Equal,
    NotEqual,
}

impl StopCondition {
    fn is_met(&self, nes: &Nes, current_cycles: u128, current_frames: u64) -> bool {
        match self {
            StopCondition::Cycles(target) => current_cycles >= *target,
            StopCondition::Frames(target) => current_frames >= *target,
            StopCondition::ProgramCounter(addr) => nes.cpu.program_counter == *addr,
            StopCondition::Opcode(op) => {
                nes.cpu.current_opcode.map(|o| o.opcode == *op).unwrap_or(false)
            }
            StopCondition::MemoryCondition { address, mask, operation, value } => {
                let mem_val = nes.cpu.memory.mem_read(*address) & mask;
                match operation {
                    CompareOp::Equal => mem_val == *value,
                    CompareOp::NotEqual => mem_val != *value,
                }
            }
            StopCondition::Halt => nes.cpu.is_halted,
        }
    }
}
```

### Video Export Implementation

For raw frame export:

```rust
fn export_frame(frame: &[u32], output: &mut impl Write, format: VideoFormat) -> io::Result<()> {
    match format {
        VideoFormat::Raw => {
            // RGBA format, 4 bytes per pixel
            for pixel in frame {
                output.write_all(&pixel.to_le_bytes())?;
            }
        }
        VideoFormat::Ppm => {
            writeln!(output, "P6")?;
            writeln!(output, "256 240")?;
            writeln!(output, "255")?;
            for pixel in frame {
                // Extract RGB, ignore alpha
                output.write_all(&[
                    ((pixel >> 16) & 0xFF) as u8,
                    ((pixel >> 8) & 0xFF) as u8,
                    (pixel & 0xFF) as u8,
                ])?;
            }
        }
        // PNG would use image crate
    }
    Ok(())
}
```

### Pipe-Based Savestate Serialization

```rust
// Write savestate to stdout
fn write_state_stdout(state: &SaveState) -> io::Result<()> {
    let bytes = rkyv::to_bytes::<rkyv::rancor::BoxedError>(state)
        .map_err(|e| io::Error::other(e))?;
    io::stdout().write_all(&bytes)
}

// Read savestate from stdin  
fn read_state_stdin() -> io::Result<SaveState> {
    let mut bytes = Vec::new();
    io::stdin().read_to_end(&mut bytes)?;
    rkyv::from_bytes::<SaveState, rkyv::rancor::BoxedError>(&bytes)
        .map_err(|e| io::Error::other(e))
}
```

### Error Handling

The CLI should use standard exit codes:

| Code | Meaning                        |
|------|--------------------------------|
| 0    | Success                        |
| 1    | General error                  |
| 2    | Invalid arguments              |
| 3    | ROM load failed                |
| 4    | Savestate load failed          |
| 5    | I/O error                      |
| 6    | Timeout/stop condition not met |

### Configuration File Support (Future)

Consider supporting a configuration file for complex setups:

```toml
# nes_cli.toml
[rom]
path = "game.nes"

[savestate]
load = "initial.sav"
save = "final.sav"
save_on = "stop"

[memory.init.cpu]
"0x0050" = [0xFF, 0x00, 0x10]
"0x0060" = [0x01]

[execution]
stop_conditions = ["pc:0x8500", "frames:3600"]

[output]
format = "json"
screenshot = "final.png"
```

---

## Summary

This CLI interface design provides:

1. **Complete control** over the emulator via command-line arguments
2. **Composable operations** that can be chained together
3. **Reproducible results** with deterministic execution
4. **Integration** with the existing message-based architecture
5. **Extensibility** for future features

The design prioritizes:

- Minimal changes to existing code
- Reuse of existing infrastructure (messages, savestate format, etc.)
- Clear separation between CLI parsing, execution, and output
- Compatibility with shell scripting and pipeline workflows

Implementation should proceed in phases:

1. Basic ROM loading and cycle-count execution
2. Savestate load/save with pipe support
3. Memory read/write operations
4. Stop conditions and breakpoints
5. Video/screenshot export