Skip to content

Instantly share code, notes, and snippets.

@pskrgag
Last active April 18, 2025 20:49
Show Gist options
  • Save pskrgag/1fc7fb9c9b1da56dc9559c4e74ef4c0e to your computer and use it in GitHub Desktop.
Save pskrgag/1fc7fb9c9b1da56dc9559c4e74ef4c0e to your computer and use it in GitHub Desktop.
use json::parse;
use std::fs::read_to_string;
fn generate_array(arr: [u16; 256], name: &str) {
println!("const {name} = [_]u8 {{");
for i in 0..256 / 16 {
print!(" ");
for j in 0..16 {
print!("{}, ", arr[i * 16 + j]);
}
println!("");
}
println!("}};");
}
fn main() {
let data = read_to_string("data.json").unwrap();
let json = parse(data.as_str()).unwrap();
let mut max_opcode = 0;
let mut single = [0u16; 256];
let mut double = [0u16; 256];
for i in json.members() {
let mut opcode = 0;
let mut cycles = 0;
for (s, val) in i.entries() {
match s {
"opCode" => {
opcode = u16::from_str_radix(val.as_str().unwrap(), 16).unwrap();
max_opcode = max_opcode.max(opcode);
}
"cycles" => {
let val = val.as_str().unwrap();
cycles = u16::from_str_radix(&val[0..1], 16).unwrap();
}
_ => {}
};
}
if opcode < 256 {
single[opcode as usize] = cycles;
} else {
assert!(opcode & 0xFF00 == 0xCB00);
double[opcode as usize & 0xFF] = cycles;
}
}
generate_array(single, "single_cycles");
generate_array(double, "double_cycles");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment