Skip to main content

working_hello/
working_hello.rs

1use std::process::Command;
2use uxn_tal::{Assembler, AssemblerError};
3
4fn main() -> Result<(), AssemblerError> {
5    let tal_source = r#"
6        ( Simple working hello world )
7        #48 #18 DEO  ( H )
8        #65 #18 DEO  ( e )
9        #6c #18 DEO  ( l )
10        #6c #18 DEO  ( l )
11        #6f #18 DEO  ( o )
12        #20 #18 DEO  ( space )
13        #57 #18 DEO  ( W )
14        #6f #18 DEO  ( o )
15        #72 #18 DEO  ( r )
16        #6c #18 DEO  ( l )
17        #64 #18 DEO  ( d )
18        #21 #18 DEO  ( ! )
19        #0a #18 DEO  ( newline )
20        BRK
21    "#;
22
23    let mut assembler = Assembler::new();
24    match assembler.assemble(tal_source, Some("working_hello.rs".to_owned())) {
25        Ok(rom) => {
26            println!("Successfully assembled {} bytes", rom.len());
27
28            // Save to file
29            std::fs::write("working_hello.rom", &rom)?;
30            println!("ROM saved to working_hello.rom");
31        }
32        Err(e) => {
33            eprintln!("Assembly failed: {}", e);
34        }
35    }
36    // Write the TAL source to a temporary file
37    let temp_path = "working_hello.tal";
38    std::fs::write(temp_path, tal_source)?;
39    println!("TAL source written to {:?}", temp_path);
40
41    // Run WSL uxnasm on the TAL source file
42    let output_path = "working_hello_wsl.rom";
43    let status = Command::new("wsl")
44        .arg("uxnasm")
45        .arg(temp_path)
46        .arg(output_path)
47        .status()?;
48
49    if status.success() {
50        println!("WSL uxnasm succeeded, output at {:?}", output_path);
51    } else {
52        eprintln!("WSL uxnasm failed with status: {}", status);
53    }
54
55    // Compare the two ROM files byte-by-byte
56    let rust_rom = std::fs::read("working_hello.rom")?;
57    let wsl_rom = std::fs::read(output_path)?;
58
59    if rust_rom == wsl_rom {
60        println!("ROM outputs are identical.");
61    } else {
62        println!("ROM outputs differ:");
63        let min_len = rust_rom.len().min(wsl_rom.len());
64        for i in 0..min_len {
65            if rust_rom[i] == wsl_rom[i] {
66                println!("Byte {}: equal ({:02x})", i, rust_rom[i]);
67            } else {
68                println!(
69                    "Byte {}: Rust = {:02x}, WSL = {:02x}",
70                    i, rust_rom[i], wsl_rom[i]
71                );
72            }
73        }
74        let min_len = rust_rom.len().min(wsl_rom.len());
75        for i in 0..min_len {
76            if rust_rom[i] != wsl_rom[i] {
77                println!(
78                    "Byte {}: Rust = {:02x}, WSL = {:02x}",
79                    i, rust_rom[i], wsl_rom[i]
80                );
81            }
82        }
83        if rust_rom.len() != wsl_rom.len() {
84            println!(
85                "ROM sizes differ: Rust = {}, WSL = {}",
86                rust_rom.len(),
87                wsl_rom.len()
88            );
89            if rust_rom.len() > wsl_rom.len() {
90                println!("Extra bytes in Rust ROM:");
91                for (i, b) in rust_rom.iter().enumerate().skip(wsl_rom.len()) {
92                    println!("Byte {}: {:02x}", i, b);
93                }
94            } else {
95                println!("Extra bytes in WSL ROM:");
96                for (i, b) in wsl_rom.iter().enumerate().skip(rust_rom.len()) {
97                    println!("Byte {}: {:02x}", i, b);
98                }
99            }
100        }
101    }
102
103    // Run both ROMs using uxncli via WSL and compare their outputs
104    let run_rom = |rom_path: &str| -> Result<String, std::io::Error> {
105        let output = Command::new("wsl").arg("uxncli").arg(rom_path).output()?;
106        Ok(String::from_utf8_lossy(&output.stdout).to_string())
107    };
108
109    let rust_output = run_rom("working_hello.rom")?;
110    let wsl_output = run_rom(output_path)?;
111
112    println!("--- Rust ROM output ---\n{}", rust_output);
113    println!("--- WSL ROM output ---\n{}", wsl_output);
114
115    if rust_output == wsl_output {
116        println!("ROM runtime outputs are identical.");
117    } else {
118        println!("ROM runtime outputs differ.");
119    }
120    Ok(())
121}