working on moving the 'TestCompression' code into its own crate

This commit is contained in:
tmerritt committed 2025-06-01 14:12:48 -04:00
1 parent 21f6e492f6
commit c022b15465
16 files changed
+254 -147

No files matched your search

+2 -2
View File
@@ -118,8 +118,8 @@
<body>
<div id="root"></div>
<script>
var data = {"files":[{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","bin","speedtest.rs"],"content":"use gemma::chip8::computer_manager::Chip8ComputerManager;\nuse std::fs::File;\nuse std::io::Read;\nuse std::ops::Add;\nuse std::path::Path;\nuse std::time::{Duration, Instant};\n\nconst ROM_ROOT: \u0026str = \"resources/roms\";\n\nfn main() {\n let mut x = Chip8ComputerManager::new();\n\n let mut buffer = Vec::new();\n let mut input_file =\n File::open(Path::new(\u0026(ROM_ROOT.to_string() + \"/mandelbrot_bench.ch8\"))).expect(\"derp\");\n input_file\n .read_to_end(\u0026mut buffer)\n .expect(\"unable to read file\");\n x.load_new_program_to_system_memory((\u0026*buffer).into());\n\n for _ in 0..10 {\n let start_time = Instant::now();\n let mut num_cycles = 0;\n // how many instructions in 10s.\n let end_time = start_time.add(Duration::from_secs(10));\n while Instant::now().le(\u0026end_time) {\n num_cycles += 1;\n x.step();\n }\n\n let num = num_cycles\n .to_string()\n .as_bytes()\n .rchunks(3)\n .rev()\n .map(std::str::from_utf8)\n .collect::\u003cResult\u003cVec\u003c\u0026str\u003e, _\u003e\u003e()\n .unwrap()\n .join(\",\"); // separator\n\n let num_ips = num_cycles / 1000000;\n println!(\"Completed at {num_ips} Mips. ({num})\");\n }\n}\n","traces":[{"line":10,"address":[458832,460780,460845],"length":1,"stats":{"Line":0}},{"line":11,"address":[458839],"length":1,"stats":{"Line":0}},{"line":13,"address":[458856],"length":1,"stats":{"Line":0}},{"line":14,"address":[458919,458991],"length":1,"stats":{"Line":0}},{"line":16,"address":[459251],"length":1,"stats":{"Line":0}},{"line":19,"address":[459336],"length":1,"stats":{"Line":0}},{"line":21,"address":[459436],"length":1,"stats":{"Line":0}},{"line":22,"address":[459589,459671],"length":1,"stats":{"Line":0}},{"line":23,"address":[459700],"length":1,"stats":{"Line":0}},{"line":25,"address":[459716],"length":1,"stats":{"Line":0}},{"line":26,"address":[459794],"length":1,"stats":{"Line":0}},{"line":27,"address":[460819,459898,460793],"length":1,"stats":{"Line":0}},{"line":28,"address":[460800,460840],"length":1,"stats":{"Line":0}},{"line":31,"address":[459891,460323,459933,460024,460390],"length":1,"stats":{"Line":0}},{"line":41,"address":[460537,460626],"length":1,"stats":{"Line":0}},{"line":42,"address":[460599,460646],"length":1,"stats":{"Line":0}}],"covered":0,"coverable":16},{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","bin","testcompression.rs"],"content":"use std::fs;\nuse std::fs::File;\nuse clap::{Parser, Subcommand};\nuse flate2::{write::{GzEncoder, GzDecoder}, Compression};\nuse std::io::{Cursor, Read, Write};\nuse std::path::PathBuf;\nuse std::io::prelude::*;\n\n#[derive(Parser)]\n#[command(author, version, about = \"Compress or decompress a string\", long_about = None)]\nstruct Cli {\n /// Subcommand: either 'compress' or 'decompress'\n #[command(subcommand)]\n command: Commands,\n}\n\n#[derive(Subcommand)]\nenum Commands {\n /// Compress the input string\n Compress {\n /// The string to compress\n input: String,\n },\n /// Decompress the input string (must be compressed format)\n Decompress {\n /// The compressed string to decompress, in hex format\n input: String,\n },\n CompressFile {\n input: PathBuf\n },\n DecompressFile {\n input: PathBuf\n }\n}\n\nfn compress_string(input: \u0026str) -\u003e Vec\u003cu8\u003e {\n let mut encoder = GzEncoder::new(Vec::new(), Compression::default());\n encoder.write_all(input.as_bytes()).expect(\"Compression failed\");\n encoder.finish().expect(\"Failed to finish compression\")\n}\n\nfn decompress_string(input: \u0026[u8]) -\u003e String {\n let mut decoder = GzDecoder::new(Vec::new());\n decoder.write_all(input).expect(\"Decompression failed\");\n let decompressed_data = decoder.finish().expect(\"Failed to finish decompression\");\n String::from_utf8(decompressed_data).expect(\"Invalid UTF-8\")\n}\n\nfn compress_file(input_path: \u0026PathBuf) {\n let target_file_name = format!(\"{}.tflt\", input_path.display());\n println!(\"TARGET_FILE_NAME: {}\", target_file_name);\n let input_data = fs::read(input_path).expect(\"Failed to read input file\");\n let mut encoder = GzEncoder::new(Vec::new(), Compression::default());\n encoder.write_all(\u0026input_data).expect(\"Compression failed\");\n let compressed_data = encoder.finish().expect(\"Failed to finish compression\");\n let target_file = File::create(target_file_name);\n target_file.unwrap().write_all(\u0026compressed_data).unwrap()\n}\n\nfn decompress_file(input_path: \u0026PathBuf) {\n let target_file_name = format!(\"{}.uncompressed\", input_path.dispLine truncated
var previousData = {"files":[{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","bin","speedtest.rs"],"content":"use gemma::chip8::computer_manager::Chip8ComputerManager;\nuse std::fs::File;\nuse std::io::Read;\nuse std::ops::Add;\nuse std::path::Path;\nuse std::time::{Duration, Instant};\n\nconst ROM_ROOT: \u0026str = \"resources/roms\";\n\nfn main() {\n let mut x = Chip8ComputerManager::new();\n\n let mut buffer = Vec::new();\n let mut input_file =\n File::open(Path::new(\u0026(ROM_ROOT.to_string() + \"/mandelbrot_bench.ch8\"))).expect(\"derp\");\n input_file\n .read_to_end(\u0026mut buffer)\n .expect(\"unable to read file\");\n x.load_new_program_to_system_memory((\u0026*buffer).into());\n\n for _ in 0..10 {\n let start_time = Instant::now();\n let mut num_cycles = 0;\n // how many instructions in 10s.\n let end_time = start_time.add(Duration::from_secs(10));\n while Instant::now().le(\u0026end_time) {\n num_cycles += 1;\n x.step();\n }\n\n let num = num_cycles\n .to_string()\n .as_bytes()\n .rchunks(3)\n .rev()\n .map(std::str::from_utf8)\n .collect::\u003cResult\u003cVec\u003c\u0026str\u003e, _\u003e\u003e()\n .unwrap()\n .join(\",\"); // separator\n\n let num_ips = num_cycles / 1000000;\n println!(\"Completed at {num_ips} Mips. ({num})\");\n }\n}\n","traces":[{"line":10,"address":[460780,460845,458832],"length":1,"stats":{"Line":0}},{"line":11,"address":[458839],"length":1,"stats":{"Line":0}},{"line":13,"address":[458856],"length":1,"stats":{"Line":0}},{"line":14,"address":[458919,458991],"length":1,"stats":{"Line":0}},{"line":16,"address":[459251],"length":1,"stats":{"Line":0}},{"line":19,"address":[459336],"length":1,"stats":{"Line":0}},{"line":21,"address":[459436],"length":1,"stats":{"Line":0}},{"line":22,"address":[459589,459671],"length":1,"stats":{"Line":0}},{"line":23,"address":[459700],"length":1,"stats":{"Line":0}},{"line":25,"address":[459716],"length":1,"stats":{"Line":0}},{"line":26,"address":[459794],"length":1,"stats":{"Line":0}},{"line":27,"address":[459898,460793,460819],"length":1,"stats":{"Line":0}},{"line":28,"address":[460800,460840],"length":1,"stats":{"Line":0}},{"line":31,"address":[459933,460390,459891,460323,460024],"length":1,"stats":{"Line":0}},{"line":41,"address":[460537,460626],"length":1,"stats":{"Line":0}},{"line":42,"address":[460599,460646],"length":1,"stats":{"Line":0}}],"covered":0,"coverable":16},{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","bin","testcompression.rs"],"content":"use std::fs;\nuse std::fs::File;\nuse clap::{Parser, Subcommand};\nuse flate2::{write::{GzEncoder, GzDecoder}, Compression};\nuse std::io::{Cursor, Read, Write};\nuse std::path::PathBuf;\nuse std::io::prelude::*;\n\n#[derive(Parser)]\n#[command(author, version, about = \"Compress or decompress a string\", long_about = None)]\nstruct Cli {\n /// Subcommand: either 'compress' or 'decompress'\n #[command(subcommand)]\n command: Commands,\n}\n\n#[derive(Subcommand)]\nenum Commands {\n /// Compress the input string\n Compress {\n /// The string to compress\n input: String,\n },\n /// Decompress the input string (must be compressed format)\n Decompress {\n /// The compressed string to decompress, in hex format\n input: String,\n },\n CompressFile {\n input: PathBuf\n },\n DecompressFile {\n input: PathBuf\n }\n}\n\nfn compress_string(input: \u0026str) -\u003e Vec\u003cu8\u003e {\n let mut encoder = GzEncoder::new(Vec::new(), Compression::default());\n encoder.write_all(input.as_bytes()).expect(\"Compression failed\");\n encoder.finish().expect(\"Failed to finish compression\")\n}\n\nfn decompress_string(input: \u0026[u8]) -\u003e String {\n let mut decoder = GzDecoder::new(Vec::new());\n decoder.write_all(input).expect(\"Decompression failed\");\n let decompressed_data = decoder.finish().expect(\"Failed to finish decompression\");\n String::from_utf8(decompressed_data).expect(\"Invalid UTF-8\")\n}\n\nfn compress_file(input_path: \u0026PathBuf) {\n let target_file_name = format!(\"{}.tflt\", input_path.display());\n println!(\"TARGET_FILE_NAME: {}\", target_file_name);\n let input_data = fs::read(input_path).expect(\"Failed to read input file\");\n let mut encoder = GzEncoder::new(Vec::new(), Compression::default());\n encoder.write_all(\u0026input_data).expect(\"Compression failed\");\n let compressed_data = encoder.finish().expect(\"Failed to finish compression\");\n let target_file = File::create(target_file_name);\n target_file.unwrap().write_all(\u0026compressed_data).unwrap()\n}\n\nfn decompress_file(input_path: \u0026PathBuf) {\n let target_file_name = format!(\"{}.uncompressed\", input_pLine truncated
var data = {"files":[{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","bin","speedtest.rs"],"content":"use gemma::chip8::computer_manager::Chip8ComputerManager;\nuse std::fs::File;\nuse std::io::Read;\nuse std::ops::Add;\nuse std::path::Path;\nuse std::time::{Duration, Instant};\n\nconst ROM_ROOT: \u0026str = \"resources/roms\";\n\nfn main() {\n let mut x = Chip8ComputerManager::new();\n\n let mut buffer = Vec::new();\n let mut input_file =\n File::open(Path::new(\u0026(ROM_ROOT.to_string() + \"/mandelbrot_bench.ch8\"))).expect(\"derp\");\n input_file\n .read_to_end(\u0026mut buffer)\n .expect(\"unable to read file\");\n x.load_new_program_to_system_memory((\u0026*buffer).into());\n\n for _ in 0..10 {\n let start_time = Instant::now();\n let mut num_cycles = 0;\n // how many instructions in 10s.\n let end_time = start_time.add(Duration::from_secs(10));\n while Instant::now().le(\u0026end_time) {\n num_cycles += 1;\n x.step();\n }\n\n let num = num_cycles\n .to_string()\n .as_bytes()\n .rchunks(3)\n .rev()\n .map(std::str::from_utf8)\n .collect::\u003cResult\u003cVec\u003c\u0026str\u003e, _\u003e\u003e()\n .unwrap()\n .join(\",\"); // separator\n\n let num_ips = num_cycles / 1000000;\n println!(\"Completed at {num_ips} Mips. ({num})\");\n }\n}\n","traces":[{"line":10,"address":[880112,881936,881871],"length":1,"stats":{"Line":0}},{"line":11,"address":[880119],"length":1,"stats":{"Line":0}},{"line":13,"address":[880136],"length":1,"stats":{"Line":0}},{"line":14,"address":[880271,880199],"length":1,"stats":{"Line":0}},{"line":16,"address":[880531],"length":1,"stats":{"Line":0}},{"line":19,"address":[880616],"length":1,"stats":{"Line":0}},{"line":21,"address":[880716],"length":1,"stats":{"Line":0}},{"line":22,"address":[880869,880939],"length":1,"stats":{"Line":0}},{"line":23,"address":[880968],"length":1,"stats":{"Line":0}},{"line":25,"address":[880984],"length":1,"stats":{"Line":0}},{"line":26,"address":[881062],"length":1,"stats":{"Line":0}},{"line":27,"address":[881166,881910,881884],"length":1,"stats":{"Line":0}},{"line":28,"address":[881891,881931],"length":1,"stats":{"Line":0}},{"line":31,"address":[881477,881272,881159,881201],"length":1,"stats":{"Line":0}},{"line":41,"address":[881628,881717],"length":1,"stats":{"Line":0}},{"line":42,"address":[881690,881737],"length":1,"stats":{"Line":0}}],"covered":0,"coverable":16},{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","bin","testcompression.rs"],"content":"/// TestCompression\n///\n/// Utility for [de]compressing tests for Gemma\n///\n/// Usage: testcompression \u003cCOMMAND\u003e\n//\n// Commands:\n// compress Compress the input string\n// decompress Decompress the input string (must be compressed format)\n// help Print this message or the help of the given subcommand(s)\n//\n// Options:\n// -h, --help Print help\n// -V, --version Print version\n///\n/// Limitations:\n/// - Uncompressed files must only contain printable ASCII characters\n/// - Stuff I don't know about yet\n\n\nuse std::fs;\nuse std::fs::File;\nuse clap::{Parser, Subcommand};\nuse std::path::{Path, PathBuf};\nuse std::io::prelude::*;\nuse std::io::Read;\nuse std::io::Write;\nuse flate2::Compression;\nuse flate2::write::{DeflateEncoder, GzEncoder, ZlibEncoder};\nuse gemma::constants::{TESTS_ROOT, TEST_ROM_ROOT};\nuse flate2::read::{DeflateDecoder};\nuse std::io::prelude::*;\nuse std::io;\nuse std::ptr::write;\nuse log::{debug, info};\n\n#[derive(Parser)]\n#[command(author, version, about = \"Compress or decompress a string\", long_about = None)]\nstruct Cli {\n /// Subcommand: either 'compress' or 'decompress'\n #[command(subcommand)]\n command: Commands,\n}\n\n#[derive(Subcommand)]\nenum Commands {\n /// Compress the input file\n Compress {\n /// The string to compress, in plain text\n input: PathBuf,\n },\n /// Decompress the input file\n Decompress {\n /// The compressed string to decompress, in hex format\n input: PathBuf,\n }\n}\n\nfn compress_file(input_path: \u0026PathBuf) -\u003e io::Result\u003c()\u003e {\n let compressed_bytes= compress_file_to_array(input_path);\n let output_filename = input_path.with_extension(\"deflated\");\n let mut output_file = File::create(\u0026output_filename)?;\n output_file.write_all(\u0026compressed_bytes.unwrap())?;\n Ok(())\n}\n\nfn compress_file_to_array(input_path: \u0026Path) -\u003e Result\u003cVec\u003cu8\u003e, String\u003e {\n let mut file = File::open(input_path)\n .map_err(|e| std::io::Error::new(e.kind(), format!(\"Unable to open input path [{}]: {}\", input_path.display(), e)));\n\n let mut read_buffer = Vec::new();\n let mut write_buffer = Vec::new();\Line truncated
var previousData = {"files":[{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","bin","speedtest.rs"],"content":"use gemma::chip8::computer_manager::Chip8ComputerManager;\nuse std::fs::File;\nuse std::io::Read;\nuse std::ops::Add;\nuse std::path::Path;\nuse std::time::{Duration, Instant};\n\nconst ROM_ROOT: \u0026str = \"resources/roms\";\n\nfn main() {\n let mut x = Chip8ComputerManager::new();\n\n let mut buffer = Vec::new();\n let mut input_file =\n File::open(Path::new(\u0026(ROM_ROOT.to_string() + \"/mandelbrot_bench.ch8\"))).expect(\"derp\");\n input_file\n .read_to_end(\u0026mut buffer)\n .expect(\"unable to read file\");\n x.load_new_program_to_system_memory((\u0026*buffer).into());\n\n for _ in 0..10 {\n let start_time = Instant::now();\n let mut num_cycles = 0;\n // how many instructions in 10s.\n let end_time = start_time.add(Duration::from_secs(10));\n while Instant::now().le(\u0026end_time) {\n num_cycles += 1;\n x.step();\n }\n\n let num = num_cycles\n .to_string()\n .as_bytes()\n .rchunks(3)\n .rev()\n .map(std::str::from_utf8)\n .collect::\u003cResult\u003cVec\u003c\u0026str\u003e, _\u003e\u003e()\n .unwrap()\n .join(\",\"); // separator\n\n let num_ips = num_cycles / 1000000;\n println!(\"Completed at {num_ips} Mips. ({num})\");\n }\n}\n","traces":[{"line":11,"address":[254592,256682,256747],"length":1,"stats":{"Line":0}},{"line":12,"address":[254599],"length":1,"stats":{"Line":0}},{"line":14,"address":[254616],"length":1,"stats":{"Line":0}},{"line":15,"address":[254747,254679],"length":1,"stats":{"Line":0}},{"line":17,"address":[254999],"length":1,"stats":{"Line":0}},{"line":20,"address":[255084],"length":1,"stats":{"Line":0}},{"line":22,"address":[255184],"length":1,"stats":{"Line":0}},{"line":23,"address":[255415,255333],"length":1,"stats":{"Line":0}},{"line":24,"address":[255444],"length":1,"stats":{"Line":0}},{"line":26,"address":[255460],"length":1,"stats":{"Line":0}},{"line":27,"address":[255538],"length":1,"stats":{"Line":0}},{"line":28,"address":[256695,255642,256721],"length":1,"stats":{"Line":0}},{"line":29,"address":[256702,256742],"length":1,"stats":{"Line":0}},{"line":32,"address":[255635,256067,256134,255677,255768],"length":1,"stats":{"Line":0}},{"line":42,"address":[256281,256445],"length":1,"stats":{"Line":0}},{"line":43,"address":[256575],"length":1,"stats":{"Line":0}}],"covered":0,"coverable":16},{"path":["/","home","tmerritt","Projects","trevors_chip8_toy","gemma","src","chip8","computer.rs"],"content":"use crate::chip8::delay_timer::DelayTimer;\nuse crate::chip8::keypad::Keypad;\nuse crate::chip8::quirk_modes::QuirkMode;\nuse crate::chip8::registers::Chip8Registers;\nuse crate::chip8::sound_timer::SoundTimer;\nuse crate::chip8::stack::Chip8Stack;\nuse log::debug;\nuse serde::{Deserialize, Serialize};\nuse super::{\n cpu_states::Chip8CpuStates, instructions::Chip8CpuInstructions,\n system_memory::Chip8SystemMemory, video::Chip8Video,\n};\n\n#[derive(Clone, Serialize, Deserialize, Debug)]\npub struct Chip8Computer {\n pub num_cycles: i32,\n pub memory: Chip8SystemMemory,\n pub registers: Chip8Registers,\n pub sound_timer: SoundTimer,\n pub delay_timer: DelayTimer,\n pub video_memory: Chip8Video,\n pub state: Chip8CpuStates,\n pub keypad: Keypad,\n pub stack: Chip8Stack,\n pub quirk_mode: QuirkMode,\n}\n\nimpl PartialEq for Chip8Computer {\n fn eq(\u0026self, other: \u0026Self) -\u003e bool {\n self.video_memory.format_as_string() == other.video_memory.format_as_string() \u0026\u0026\n self.keypad.format_as_string() == other.keypad.format_as_string() \u0026\u0026\n self.num_cycles == other.num_cycles \u0026\u0026\n self.memory == other.memory\n }\n}\n\nimpl Default for Chip8Computer {\n fn default() -\u003e Self {\n Self {\n num_cycles: 0,\n memory: Chip8SystemMemory::default(),\n video_memory: Chip8Video::default(),\n registers: Chip8Registers::default(),\n sound_timer: SoundTimer::new(),\n delay_timer: DelayTimer::new(),\n state: Chip8CpuStates::default(),\n keypad: Keypad::default(),\n stack: Chip8Stack::default(),\n quirk_mode: QuirkMode::default(),\n }\n }\n}\n\nimpl Chip8Computer {\n pub fn reset(\u0026mut self, quirk_mode: QuirkMode) {\n self.video_memory.reset();\n self.num_cycles = 0;\n self.registers.reset();\n self.delay_timer.reset();\n self.sound_timer.reset();\n self.stack.reset();\n self.memory.reset(quirk_mode);\n self.quirk_mode = quirk_mode;\n }\n\n pub fn dump_keypad_to_string(\u0026self) -\u003e String {\n self.keypad.format_as_sLine truncated
</script>
<script crossorigin>/** @license React v16.13.1
* react.production.min.js