checksmix 0.2.23

A simplified MIX simulator - Donald Knuth's hypothetical computer for teaching and learning assembly language programming.
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
//! MMIX Object (MMO) file format support
//!
//! This module handles generation and parsing of MMIX object files (.mmo format).
//! The MMO format consists of records (not instructions) that specify how to load
//! code and data into memory at arbitrary 64-bit addresses.
//!
//! ## Format Specification (from MMOTYPE)
//!
//! ### Record Structure
//! - All records begin with MM escape code (0x98)
//! - Format: `MM lopcode Y Z [data...]`
//! - YZ = (Y << 8) | Z (16-bit value)
//! - All multi-byte values are big-endian
//!
//! ### Normal Data Tetrabytes
//! Any tetrabyte NOT preceded by MM (0x98) is loaded as data at cur_loc,
//! then cur_loc += 4. This is the primary way to load instruction/data bytes.
//!
//! ### Lopcodes (verified against mmotype.pdf)
//! - 0x00 (lop_quote): Quote single tetrabyte (YZ must = 1)
//! - 0x01 (lop_loc): Set current location
//! - 0x02 (lop_skip): Skip YZ bytes forward
//! - 0x03 (lop_fixo): Absolute fixup
//! - 0x04 (lop_fixr): Relative fixup
//! - 0x05 (lop_fixrx): Extended relative fixup
//! - 0x06 (lop_file): File name
//! - 0x07 (lop_line): Source line number
//! - 0x08 (lop_spec): Special data
//! - 0x09 (lop_pre): Preamble (Y=version, typically 1)
//! - 0x0A (lop_post): Postamble (Y=0, Z>=32)
//! - 0x0B (lop_stab): Symbol table
//! - 0x0C (lop_end): End of file (YZ=symbol table length)
//!
//! Reference: MMIXWARE documentation by Donald Knuth, mmotype.pdf

use crate::mmixal::MMixInstruction;
use std::collections::HashMap;
use tracing::debug;

use crate::encode::encode_instruction_bytes;

/// MMO escape code - all MMO files must start with this
pub const MM: u8 = 0x98;

/// MMO record types (lopcodes) as defined in the MMIXAL specification
/// Reference: MMIXWARE documentation, Section on MMO Format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
#[allow(clippy::enum_variant_names)]
pub enum MmoRecordType {
    /// lop_quote (0): Literal data to load at current address
    LopQuote = 0,
    /// lop_loc (1): Set loading address
    LopLoc = 1,
    /// lop_skip (2): Advance current address
    LopSkip = 2,
    /// lop_fixo (3): Octabyte-fix lopcode
    LopFixo = 3,
    /// lop_fixr (4): Relative-fix lopcode
    LopFixr = 4,
    /// lop_fixrx (5): Extended relative-fix lopcode
    LopFixrx = 5,
    /// lop_file (6): File name lopcode
    LopFile = 6,
    /// lop_line (7): File position lopcode
    LopLine = 7,
    /// lop_spec (8): Special hook lopcode
    LopSpec = 8,
    /// lop_pre (9): Preamble lopcode
    LopPre = 9,
    /// lop_post (10): Postamble lopcode
    LopPost = 10,
    /// lop_stab (11): Symbol table lopcode
    LopStab = 11,
    /// lop_end (12): End-it-all lopcode
    LopEnd = 12,
}

impl TryFrom<u8> for MmoRecordType {
    type Error = String;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0x0 => Ok(MmoRecordType::LopQuote),
            0x1 => Ok(MmoRecordType::LopLoc),
            0x2 => Ok(MmoRecordType::LopSkip),
            0x3 => Ok(MmoRecordType::LopFixo),
            0x4 => Ok(MmoRecordType::LopFixr),
            0x5 => Ok(MmoRecordType::LopFixrx),
            0x6 => Ok(MmoRecordType::LopFile),
            0x7 => Ok(MmoRecordType::LopLine),
            0x8 => Ok(MmoRecordType::LopSpec),
            0x9 => Ok(MmoRecordType::LopPre),
            0xa => Ok(MmoRecordType::LopPost),
            0xb => Ok(MmoRecordType::LopStab),
            0xc => Ok(MmoRecordType::LopEnd),
            _ => Err(format!("Unknown MMO lopcode: 0x{:02X}", value)),
        }
    }
}

/// MMO file generator
pub struct MmoGenerator {
    /// Instructions to encode, sorted by address
    instructions: Vec<(u64, MMixInstruction)>,
    /// Symbol table (labels)
    labels: HashMap<String, u64>,
}

impl MmoGenerator {
    /// Create a new MMO generator
    pub fn new(instructions: Vec<(u64, MMixInstruction)>, labels: HashMap<String, u64>) -> Self {
        Self {
            instructions,
            labels,
        }
    }

    /// Generate MMIX object code in .mmo format
    /// The format uses records (lopcodes) preceded by the MM escape code (0x98).
    /// Each record has the format: MM YZ X Z where YZ is a 16-bit value
    /// - lop_pre (9): Preamble
    /// - lop_loc (1): Set loading address (followed by 2 tetras for 64-bit addr)
    /// - lop_quote (0): Literal data bytes to load at current address
    /// - lop_post (10): Postamble
    pub fn generate(&self) -> Vec<u8> {
        debug!("Generating MMIX object code (.mmo format)");
        let mut mmo = Vec::new();

        // Write preamble: lop_pre
        // Format: MM lop_pre YZ (version in YZ, typically 1)
        self.emit_simple_record(&mut mmo, MmoRecordType::LopPre, 1);

        // Group instructions by contiguous address ranges
        let mut sorted_instructions: Vec<_> = self.instructions.iter().collect();
        sorted_instructions.sort_by_key(|(addr, _)| *addr);

        let mut current_loc: Option<u64> = None;
        let mut pending_bytes = Vec::new();

        for (addr, instruction) in sorted_instructions {
            let addr = *addr;
            // Encode a single instruction to bytes
            let bytes = encode_instruction_bytes(instruction);

            // Check if we need to emit a new lop_loc directive
            let need_new_loc = match current_loc {
                None => true,
                Some(loc) => {
                    // Need new LOC if not contiguous or pending buffer would be too large
                    addr != loc || pending_bytes.len() + bytes.len() > 252
                }
            };

            if need_new_loc {
                // Flush any pending bytes first
                if !pending_bytes.is_empty() {
                    self.emit_lop_quote(&mut mmo, &pending_bytes);
                    pending_bytes.clear();
                }

                // Emit lop_loc to set new address
                self.emit_lop_loc(&mut mmo, addr);
            }

            // Add bytes to pending buffer
            pending_bytes.extend_from_slice(&bytes);
            current_loc = Some(addr + bytes.len() as u64);
        }

        // Flush any remaining bytes
        if !pending_bytes.is_empty() {
            self.emit_lop_quote(&mut mmo, &pending_bytes);
        }

        // Write postamble: lop_post with entry point
        // Find Main label or use first instruction address
        let entry_point = self
            .labels
            .get("Main")
            .or_else(|| {
                self.instructions
                    .iter()
                    .find(|(addr, _)| *addr < 0x2000000000000000)
                    .map(|(addr, _)| addr)
            })
            .copied()
            .unwrap_or(0x100);

        self.emit_lop_post(&mut mmo, entry_point);

        debug!("Generated {} bytes of .mmo object code", mmo.len());
        mmo
    }

    /// Emit a simple record with just YZ value (no additional data)
    /// Format: MM lopcode YZ (4 bytes total)
    fn emit_simple_record(&self, mmo: &mut Vec<u8>, lopcode: MmoRecordType, yz: u16) {
        mmo.push(MM); // MM escape code
        mmo.push(lopcode as u8); // lopcode
        mmo.push((yz >> 8) as u8); // Y
        mmo.push((yz & 0xFF) as u8); // Z
    }

    /// Emit lop_quote: literal bytes to load at current address
    /// Format: MM lop_quote YZ X, followed by YZ tetras of data (padded to tetra boundary)
    /// where YZ is a 16-bit count of tetras (not bytes)
    fn emit_lop_quote(&self, mmo: &mut Vec<u8>, bytes: &[u8]) {
        // Split into chunks of max 255 tetras (1020 bytes)
        for chunk in bytes.chunks(1020) {
            let chunk_tetras = chunk.len().div_ceil(4);
            let yz = chunk_tetras as u16;

            mmo.push(MM); // MM escape code
            mmo.push(MmoRecordType::LopQuote as u8); // lop_quote
            mmo.push((yz >> 8) as u8); // Y
            mmo.push((yz & 0xFF) as u8); // Z

            // Write data, padding to tetra boundary
            mmo.extend_from_slice(chunk);
            let padding = (4 - (chunk.len() % 4)) % 4;
            for _ in 0..padding {
                mmo.push(0);
            }
        }
    }

    /// Emit lop_loc: set current loading address
    /// Format: MM lop_loc YZ X (4 bytes), followed by 2 tetras (8 bytes) for 64-bit address
    /// Total: 12 bytes
    fn emit_lop_loc(&self, mmo: &mut Vec<u8>, addr: u64) {
        // Record header: MM lop_loc with YZ=2 (two tetras of address data follow)
        mmo.push(MM); // MM escape code
        mmo.push(MmoRecordType::LopLoc as u8); // lop_loc
        mmo.push(0x00); // Y
        mmo.push(0x02); // Z = 2 (two tetras follow)

        // Tetra 1: high 32 bits
        let high = (addr >> 32) as u32;
        mmo.extend_from_slice(&high.to_be_bytes());

        // Tetra 2: low 32 bits
        let low = (addr & 0xFFFFFFFF) as u32;
        mmo.extend_from_slice(&low.to_be_bytes());
    }

    /// Emit lop_post: postamble, storing entry point in register 255
    fn emit_lop_post(&self, mmo: &mut Vec<u8>, entry_point: u64) {
        mmo.push(MM); // MM escape code
        mmo.push(MmoRecordType::LopPost as u8); // lop_post
        mmo.push(0x00); // Y must be 0
        mmo.push(0xFF); // Z = 255 (start writing from g[255])

        let high = (entry_point >> 32) as u32;
        let low = (entry_point & 0xFFFF_FFFF) as u32;
        mmo.extend_from_slice(&high.to_be_bytes());
        mmo.extend_from_slice(&low.to_be_bytes());
    }
}

/// MMO file decoder
pub struct MmoDecoder {
    data: Vec<u8>,
}

impl MmoDecoder {
    /// Create a new MMO decoder
    pub fn new(data: Vec<u8>) -> Self {
        Self { data }
    }

    /// Decode MMO format and load into memory
    /// Returns the entry point address and a callback is invoked for each byte to write
    /// MMO format: each record starts with MM (0x98) followed by lopcode and data
    pub fn decode<F>(&self, mut write_byte: F) -> u64
    where
        F: FnMut(u64, u8),
    {
        debug!("Decoding MMIX object code (.mmo format)");
        let mut entry_point = 0x100u64; // Default entry point
        let mut i = 0;
        let mut current_addr = 0u64;

        while i < self.data.len() {
            // Check for MM escape code
            if i >= self.data.len() {
                break;
            }

            if self.data[i] != MM {
                // All records should start with MM in our MMO files
                debug!(
                    "Unexpected byte (not MM escape) at offset 0x{:X}: 0x{:02X}",
                    i, self.data[i]
                );
                i += 1;
                continue;
            }

            // We have MM, now get the lopcode
            i += 1;
            if i >= self.data.len() {
                break;
            }

            let lopcode_byte = self.data[i];
            i += 1;

            // Try to parse as a known lopcode
            match MmoRecordType::try_from(lopcode_byte) {
                Ok(MmoRecordType::LopQuote) => {
                    // lop_quote: YZ tetras of literal data follow
                    if i + 2 > self.data.len() {
                        break;
                    }
                    let yz = ((self.data[i] as usize) << 8) | (self.data[i + 1] as usize);
                    i += 2; // Skip YZ

                    // Load yz tetras (4*yz bytes) at current_addr
                    let byte_count = yz * 4;
                    debug!(
                        "lop_quote: loading {} bytes at 0x{:X}",
                        byte_count, current_addr
                    );
                    for offset in 0..byte_count {
                        if i + offset < self.data.len() {
                            write_byte(current_addr + offset as u64, self.data[i + offset]);
                        }
                    }
                    current_addr += byte_count as u64;
                    i += byte_count;
                }
                Ok(MmoRecordType::LopLoc) => {
                    // lop_loc: Set loading address
                    // Format: MM lop_loc YZ (lopcode already read)
                    // Followed by 2 tetras (8 bytes) for address
                    if i + 2 > self.data.len() {
                        break;
                    }
                    let _yz = ((self.data[i] as u16) << 8) | (self.data[i + 1] as u16);
                    i += 2; // Skip YZ

                    if i + 8 > self.data.len() {
                        break;
                    }

                    let high = u32::from_be_bytes([
                        self.data[i],
                        self.data[i + 1],
                        self.data[i + 2],
                        self.data[i + 3],
                    ]);
                    let low = u32::from_be_bytes([
                        self.data[i + 4],
                        self.data[i + 5],
                        self.data[i + 6],
                        self.data[i + 7],
                    ]);
                    current_addr = ((high as u64) << 32) | (low as u64);
                    debug!("lop_loc: set address to 0x{:X}", current_addr);
                    i += 8;
                }
                Ok(MmoRecordType::LopPre) => {
                    // lop_pre: Preamble (just YZ, no data)
                    if i + 2 > self.data.len() {
                        break;
                    }
                    i += 2; // Skip YZ
                }
                Ok(MmoRecordType::LopPost) => {
                    if i + 2 > self.data.len() {
                        break;
                    }
                    let _y = self.data[i];
                    let start = self.data[i + 1] as usize;
                    i += 2;
                    let mut reg_index = start;
                    while reg_index <= 255 {
                        if i + 8 > self.data.len() {
                            i = self.data.len();
                            break;
                        }
                        let high = u32::from_be_bytes([
                            self.data[i],
                            self.data[i + 1],
                            self.data[i + 2],
                            self.data[i + 3],
                        ]);
                        let low = u32::from_be_bytes([
                            self.data[i + 4],
                            self.data[i + 5],
                            self.data[i + 6],
                            self.data[i + 7],
                        ]);
                        let value = ((high as u64) << 32) | (low as u64);
                        if reg_index == 255 {
                            entry_point = value;
                        }
                        i += 8;
                        reg_index += 1;
                    }
                }
                Ok(MmoRecordType::LopSkip) => {
                    // lop_skip: Advance current address by YZ tetras
                    if i + 2 > self.data.len() {
                        break;
                    }
                    let yz = ((self.data[i] as u64) << 8) | (self.data[i + 1] as u64);
                    current_addr += yz * 4; // Skip YZ tetras
                    i += 2;
                }
                Ok(MmoRecordType::LopEnd) => {
                    // lop_end: End of file
                    break;
                }
                Ok(_) => {
                    // Other lopcodes we don't handle yet - skip the YZ bytes
                    debug!(
                        "Unhandled lopcode: {:?}",
                        MmoRecordType::try_from(lopcode_byte)
                    );
                    if i + 2 <= self.data.len() {
                        i += 2;
                    } else {
                        break;
                    }
                }
                Err(e) => {
                    // Unknown lopcode
                    debug!("Error: {}", e);
                    // Try to skip - assume YZ follows
                    if i + 2 <= self.data.len() {
                        i += 2;
                    } else {
                        break;
                    }
                }
            }
        }

        debug!("Decoded .mmo file, entry point: 0x{:X}", entry_point);
        entry_point
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mmixal::MMixInstruction;

    #[test]
    fn test_record_type_enum() {
        // Test conversion from u8 to MmoRecordType
        assert_eq!(MmoRecordType::try_from(0).unwrap(), MmoRecordType::LopQuote);
        assert_eq!(MmoRecordType::try_from(1).unwrap(), MmoRecordType::LopLoc);
        assert_eq!(MmoRecordType::try_from(2).unwrap(), MmoRecordType::LopSkip);
        assert_eq!(MmoRecordType::try_from(9).unwrap(), MmoRecordType::LopPre);
        assert_eq!(MmoRecordType::try_from(10).unwrap(), MmoRecordType::LopPost);
        assert_eq!(MmoRecordType::try_from(12).unwrap(), MmoRecordType::LopEnd);

        // Test invalid record type
        assert!(MmoRecordType::try_from(13).is_err());
        assert!(MmoRecordType::try_from(255).is_err());
    }

    #[test]
    fn test_mm_escape_code() {
        // Verify MM constant is correct
        assert_eq!(MM, 0x98);
    }

    #[test]
    fn test_mmo_format_debug() {
        // Debug test to see the actual bytes generated
        let instructions = vec![(0x100, MMixInstruction::SET(1, 42))];
        let labels = HashMap::new();

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        // Print the raw bytes
        println!("Generated MMO bytes ({} total):", mmo_data.len());
        for (i, &byte) in mmo_data.iter().enumerate() {
            print!("{:02X} ", byte);
            if (i + 1) % 16 == 0 {
                println!();
            }
        }
        println!();

        // Decode and print what we got
        let decoder = MmoDecoder::new(mmo_data);
        let mut memory = HashMap::new();
        let entry_point = decoder.decode(|addr, byte| {
            println!("Loaded byte 0x{:02X} at address 0x{:X}", byte, addr);
            memory.insert(addr, byte);
        });

        println!("Entry point: 0x{:X}", entry_point);
        println!("Memory size: {}", memory.len());
    }

    #[test]
    fn test_mmo_encode_decode_simple() {
        // Create a simple program with one instruction
        // Note: SET expands to 4 instructions (SETH, SETMH, SETML, SETL) = 16 bytes
        let instructions = vec![(0x100, MMixInstruction::SETL(1, 42))];
        let labels = HashMap::new();

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        // Verify the MMO data starts with MM escape code and preamble
        assert_eq!(mmo_data[0], MM);
        assert_eq!(mmo_data[1], MmoRecordType::LopPre as u8);

        // Decode and verify
        let decoder = MmoDecoder::new(mmo_data);
        let mut memory = HashMap::new();
        let entry_point = decoder.decode(|addr, byte| {
            memory.insert(addr, byte);
        });

        // Verify entry point
        assert_eq!(entry_point, 0x100);

        // Verify instruction was loaded at 0x100
        // SETL $1, 42 = E3 01 00 2A
        assert_eq!(memory.get(&0x100), Some(&0xE3));
        assert_eq!(memory.get(&0x101), Some(&0x01));
        assert_eq!(memory.get(&0x102), Some(&0x00));
        assert_eq!(memory.get(&0x103), Some(&0x2A));
    }

    #[test]
    fn test_mmo_encode_decode_multiple_instructions() {
        // Create a program with multiple contiguous instructions
        let instructions = vec![
            (0x100, MMixInstruction::SETL(1, 10)),
            (0x104, MMixInstruction::SETL(2, 20)),
            (0x108, MMixInstruction::ADD(3, 1, 2)),
        ];
        let labels = HashMap::new();

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        // Decode
        let decoder = MmoDecoder::new(mmo_data);
        let mut memory = HashMap::new();
        decoder.decode(|addr, byte| {
            memory.insert(addr, byte);
        });

        // Verify all three instructions are loaded
        assert_eq!(memory.len(), 12); // 3 instructions * 4 bytes each
        assert!(memory.contains_key(&0x100));
        assert!(memory.contains_key(&0x104));
        assert!(memory.contains_key(&0x108));
    }

    #[test]
    fn test_mmo_encode_decode_non_contiguous() {
        // Create a program with non-contiguous instructions (should emit multiple LOC records)
        let instructions = vec![
            (0x100, MMixInstruction::SET(1, 1)),
            (0x200, MMixInstruction::SET(2, 2)),
            (0x300, MMixInstruction::SET(3, 3)),
        ];
        let labels = HashMap::new();

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        // Count LOC records - each should be preceded by MM
        let loc_count = mmo_data
            .windows(2)
            .filter(|w| w[0] == MM && w[1] == MmoRecordType::LopLoc as u8)
            .count();
        assert_eq!(loc_count, 3); // Should have 3 LOC records for 3 non-contiguous addresses

        // Decode and verify
        let decoder = MmoDecoder::new(mmo_data);
        let mut memory = HashMap::new();
        decoder.decode(|addr, byte| {
            memory.insert(addr, byte);
        });

        // Verify all instructions are at correct addresses
        assert!(memory.contains_key(&0x100));
        assert!(memory.contains_key(&0x200));
        assert!(memory.contains_key(&0x300));
    }

    #[test]
    fn test_mmo_encode_decode_with_main_label() {
        // Create a program with Main label
        let instructions = vec![(0x1000, MMixInstruction::SET(1, 99))];
        let mut labels = HashMap::new();
        labels.insert("Main".to_string(), 0x1000);

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        let decoder = MmoDecoder::new(mmo_data);
        let mut memory = HashMap::new();
        let entry_point = decoder.decode(|addr, byte| {
            memory.insert(addr, byte);
        });

        // Entry point should be Main label address
        assert_eq!(entry_point, 0x1000);
    }

    #[test]
    fn test_mmo_encode_decode_64bit_addresses() {
        // Test with high addresses (data segment)
        let instructions = vec![
            (0x100, MMixInstruction::SET(1, 1)),
            (0x2000000000000000, MMixInstruction::BYTE(65)), // 'A' in data segment
        ];
        let labels = HashMap::new();

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        let decoder = MmoDecoder::new(mmo_data);
        let mut memory = HashMap::new();
        decoder.decode(|addr, byte| {
            memory.insert(addr, byte);
        });

        // Verify both low and high addresses are loaded
        assert!(memory.contains_key(&0x100));
        assert!(memory.contains_key(&0x2000000000000000));
        assert_eq!(memory.get(&0x2000000000000000), Some(&65));
    }

    #[test]
    fn test_mmo_lop_loc_format() {
        // Verify LOC record format is correct (12 bytes total: MM + lopcode + YZ + 8 bytes address)
        let instructions = vec![(0x123456789ABCDEF0, MMixInstruction::SET(1, 1))];
        let labels = HashMap::new();

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        // Find the LOC record (MM followed by lop_loc)
        let loc_pos = mmo_data
            .windows(2)
            .position(|w| w[0] == MM && w[1] == MmoRecordType::LopLoc as u8)
            .expect("Should have LOC record");

        // Verify LOC record structure (12 bytes):
        // Byte 0: MM (0x98)
        // Byte 1: lop_loc (1)
        // Bytes 2-3: YZ (should be 2 for two tetras)
        // Bytes 4-7: high 32 bits
        // Bytes 8-11: low 32 bits
        assert_eq!(mmo_data[loc_pos], MM);
        assert_eq!(mmo_data[loc_pos + 1], MmoRecordType::LopLoc as u8);
        assert_eq!(mmo_data[loc_pos + 2], 0x00); // Y
        assert_eq!(mmo_data[loc_pos + 3], 0x02); // Z = 2 (two tetras follow)

        // Verify the address is stored correctly (big-endian)
        let high = u32::from_be_bytes([
            mmo_data[loc_pos + 4],
            mmo_data[loc_pos + 5],
            mmo_data[loc_pos + 6],
            mmo_data[loc_pos + 7],
        ]);
        let low = u32::from_be_bytes([
            mmo_data[loc_pos + 8],
            mmo_data[loc_pos + 9],
            mmo_data[loc_pos + 10],
            mmo_data[loc_pos + 11],
        ]);
        let reconstructed_addr = ((high as u64) << 32) | (low as u64);
        assert_eq!(reconstructed_addr, 0x123456789ABCDEF0);
    }

    #[test]
    fn test_mmo_lop_quote_format() {
        // Test that lop_quote pads data to tetra boundary
        let instructions = vec![
            (0x100, MMixInstruction::BYTE(1)),
            (0x101, MMixInstruction::BYTE(2)),
            (0x102, MMixInstruction::BYTE(3)),
            // 3 bytes should be padded to 4
        ];
        let labels = HashMap::new();

        let generator = MmoGenerator::new(instructions, labels);
        let mmo_data = generator.generate();

        // Find lop_quote record (MM followed by lop_quote)
        let quote_pos = mmo_data
            .windows(2)
            .position(|w| w[0] == MM && w[1] == MmoRecordType::LopQuote as u8)
            .expect("Should have QUOTE record");

        // Byte 0: MM (0x98)
        // Byte 1: lop_quote (0)
        // Bytes 2-3: YZ (tetra count)
        // Bytes 4+: data (padded to tetra boundary)
        assert_eq!(mmo_data[quote_pos], MM);
        assert_eq!(mmo_data[quote_pos + 1], MmoRecordType::LopQuote as u8);

        // YZ should be 1 (1 tetra = 4 bytes, even though we only have 3 bytes of data)
        let yz = ((mmo_data[quote_pos + 2] as u16) << 8) | (mmo_data[quote_pos + 3] as u16);
        assert_eq!(yz, 1);
    }

    #[test]
    fn test_mmo_roundtrip() {
        // Test that encode -> decode produces the same memory layout
        let instructions = vec![
            (0x100, MMixInstruction::SETL(1, 42)),
            (0x104, MMixInstruction::ADD(2, 1, 1)),
            (0x108, MMixInstruction::TRAP(0, 0, 0)),
        ];
        let labels = HashMap::new();

        // Encode
        let generator = MmoGenerator::new(instructions.clone(), labels);
        let mmo_data = generator.generate();

        // Decode
        let decoder = MmoDecoder::new(mmo_data);
        let mut memory = HashMap::new();
        decoder.decode(|addr, byte| {
            memory.insert(addr, byte);
        });

        // Verify each instruction
        for (addr, inst) in &instructions {
            let bytes = encode_instruction_bytes(inst);
            for (offset, &expected_byte) in bytes.iter().enumerate() {
                assert_eq!(
                    memory.get(&(addr + offset as u64)),
                    Some(&expected_byte),
                    "Mismatch at address 0x{:X} offset {}",
                    addr,
                    offset
                );
            }
        }
    }
}