1
0
Fork 0

cargo fmt

main
Andrew Coleman 2019-01-09 12:28:51 -05:00
parent 921e892ca8
commit 6bd5db6251
1 changed files with 115 additions and 52 deletions

View File

@ -45,7 +45,7 @@ fn read_samples_from_file(filename: &str) -> Vec<OpSample> {
samples.push(OpSample { samples.push(OpSample {
before, before,
instruction, instruction,
after after,
}); });
} }
samples samples
@ -61,71 +61,109 @@ fn read_instructions_from_file(filename: &str) -> Vec<Instruction> {
fn addr(input_registers: Registers, instruction: Instruction) -> Registers { fn addr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] + input_registers[instruction[2] as usize]; output_registers[instruction[3] as usize] =
debug!("addr: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] + input_registers[instruction[2] as usize];
debug!(
"addr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn addi(input_registers: Registers, instruction: Instruction) -> Registers { fn addi(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] + instruction[2]; output_registers[instruction[3] as usize] =
debug!("addi: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] + instruction[2];
debug!(
"addi: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn mulr(input_registers: Registers, instruction: Instruction) -> Registers { fn mulr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] * input_registers[instruction[2] as usize]; output_registers[instruction[3] as usize] =
debug!("mulr: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] * input_registers[instruction[2] as usize];
debug!(
"mulr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn muli(input_registers: Registers, instruction: Instruction) -> Registers { fn muli(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] * instruction[2]; output_registers[instruction[3] as usize] =
debug!("muli: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] * instruction[2];
debug!(
"muli: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn banr(input_registers: Registers, instruction: Instruction) -> Registers { fn banr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] & input_registers[instruction[2] as usize]; output_registers[instruction[3] as usize] =
debug!("banr: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] & input_registers[instruction[2] as usize];
debug!(
"banr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn bani(input_registers: Registers, instruction: Instruction) -> Registers { fn bani(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] & instruction[2]; output_registers[instruction[3] as usize] =
debug!("bani: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] & instruction[2];
debug!(
"bani: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn borr(input_registers: Registers, instruction: Instruction) -> Registers { fn borr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] | input_registers[instruction[2] as usize]; output_registers[instruction[3] as usize] =
debug!("borr: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] | input_registers[instruction[2] as usize];
debug!(
"borr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn bori(input_registers: Registers, instruction: Instruction) -> Registers { fn bori(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] | instruction[2]; output_registers[instruction[3] as usize] =
debug!("bori: input {:?} output {:?}", input_registers, output_registers); input_registers[instruction[1] as usize] | instruction[2];
debug!(
"bori: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn setr(input_registers: Registers, instruction: Instruction) -> Registers { fn setr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize]; output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize];
debug!("setr: input {:?} output {:?}", input_registers, output_registers); debug!(
"setr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
fn seti(input_registers: Registers, instruction: Instruction) -> Registers { fn seti(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone(); let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = instruction[1]; output_registers[instruction[3] as usize] = instruction[1];
debug!("seti: input {:?} output {:?}", input_registers, output_registers); debug!(
"seti: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
@ -136,7 +174,10 @@ fn gtir(input_registers: Registers, instruction: Instruction) -> Registers {
} else { } else {
output_registers[instruction[3] as usize] = 0; output_registers[instruction[3] as usize] = 0;
} }
debug!("gtir: input {:?} output {:?}", input_registers, output_registers); debug!(
"gtir: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
@ -147,7 +188,10 @@ fn gtri(input_registers: Registers, instruction: Instruction) -> Registers {
} else { } else {
output_registers[instruction[3] as usize] = 0; output_registers[instruction[3] as usize] = 0;
} }
debug!("gtri: input {:?} output {:?}", input_registers, output_registers); debug!(
"gtri: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
@ -158,7 +202,10 @@ fn gtrr(input_registers: Registers, instruction: Instruction) -> Registers {
} else { } else {
output_registers[instruction[3] as usize] = 0; output_registers[instruction[3] as usize] = 0;
} }
debug!("gtrr: input {:?} output {:?}", input_registers, output_registers); debug!(
"gtrr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
@ -169,7 +216,10 @@ fn eqir(input_registers: Registers, instruction: Instruction) -> Registers {
} else { } else {
output_registers[instruction[3] as usize] = 0; output_registers[instruction[3] as usize] = 0;
} }
debug!("eqir: input {:?} output {:?}", input_registers, output_registers); debug!(
"eqir: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
@ -180,7 +230,10 @@ fn eqri(input_registers: Registers, instruction: Instruction) -> Registers {
} else { } else {
output_registers[instruction[3] as usize] = 0; output_registers[instruction[3] as usize] = 0;
} }
debug!("eqri: input {:?} output {:?}", input_registers, output_registers); debug!(
"eqri: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
@ -191,7 +244,10 @@ fn eqrr(input_registers: Registers, instruction: Instruction) -> Registers {
} else { } else {
output_registers[instruction[3] as usize] = 0; output_registers[instruction[3] as usize] = 0;
} }
debug!("eqrr: input {:?} output {:?}", input_registers, output_registers); debug!(
"eqrr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers output_registers
} }
@ -277,15 +333,17 @@ fn reduce_decode_map(map: &mut HashMap<u64, HashSet<&str>>) -> DecodeTable {
} }
removed_one = true; removed_one = true;
let confirmed_instruction = confirmed_instructions.iter().nth(0).unwrap(); let confirmed_instruction = confirmed_instructions.iter().nth(0).unwrap();
results.entry(*confirmed_op).or_insert(confirmed_instruction.to_string()); results
.entry(*confirmed_op)
.or_insert(confirmed_instruction.to_string());
this_pass_found_opcodes.insert(*confirmed_op); this_pass_found_opcodes.insert(*confirmed_op);
this_pass_found_instr.insert(*confirmed_instruction); this_pass_found_instr.insert(*confirmed_instruction);
} }
for op in this_pass_found_opcodes.iter() { for op in this_pass_found_opcodes.iter() {
map.retain(|k,_| k != op); map.retain(|k, _| k != op);
} }
for instr in this_pass_found_instr.iter() { for instr in this_pass_found_instr.iter() {
for (_k,v) in map.iter_mut() { for (_k, v) in map.iter_mut() {
v.retain(|i| i != instr); v.retain(|i| i != instr);
} }
} }
@ -301,12 +359,19 @@ fn opcodes_part_one(samples: &Vec<OpSample>) -> (u16, DecodeTable) {
if matched_ops.len() >= 3 { if matched_ops.len() >= 3 {
i += 1; i += 1;
} }
debug!("{:?} matches {} opcodes", sample.instruction, matched_ops.len()); debug!(
"{:?} matches {} opcodes",
sample.instruction,
matched_ops.len()
);
let instruction = sample.instruction[0]; let instruction = sample.instruction[0];
for op in matched_ops.iter() { for op in matched_ops.iter() {
decode_map.entry(instruction).and_modify(|m| { decode_map
m.insert(op); .entry(instruction)
}).or_insert(HashSet::<&str>::new()); .and_modify(|m| {
m.insert(op);
})
.or_insert(HashSet::<&str>::new());
} }
} }
let final_decode_map = reduce_decode_map(&mut decode_map); let final_decode_map = reduce_decode_map(&mut decode_map);
@ -321,26 +386,24 @@ fn execute_program(decode_table: &DecodeTable, instructions: &Vec<Instruction>)
for instruction in instructions.iter() { for instruction in instructions.iter() {
debug!("{:?}", instruction); debug!("{:?}", instruction);
registers = match decode_table.get(&instruction[0]) { registers = match decode_table.get(&instruction[0]) {
Some(x) => { Some(x) => match x.as_str() {
match x.as_str() { "addr" => addr(registers, *instruction),
"addr" => addr(registers, *instruction), "addi" => addi(registers, *instruction),
"addi" => addi(registers, *instruction), "mulr" => mulr(registers, *instruction),
"mulr" => mulr(registers, *instruction), "muli" => muli(registers, *instruction),
"muli" => muli(registers, *instruction), "banr" => banr(registers, *instruction),
"banr" => banr(registers, *instruction), "bani" => bani(registers, *instruction),
"bani" => bani(registers, *instruction), "borr" => borr(registers, *instruction),
"borr" => borr(registers, *instruction), "bori" => bori(registers, *instruction),
"bori" => bori(registers, *instruction), "setr" => setr(registers, *instruction),
"setr" => setr(registers, *instruction), "seti" => seti(registers, *instruction),
"seti" => seti(registers, *instruction), "gtir" => gtir(registers, *instruction),
"gtir" => gtir(registers, *instruction), "gtri" => gtri(registers, *instruction),
"gtri" => gtri(registers, *instruction), "gtrr" => gtrr(registers, *instruction),
"gtrr" => gtrr(registers, *instruction), "eqir" => eqir(registers, *instruction),
"eqir" => eqir(registers, *instruction), "eqri" => eqri(registers, *instruction),
"eqri" => eqri(registers, *instruction), "eqrr" => eqrr(registers, *instruction),
"eqrr" => eqrr(registers, *instruction), _ => panic!("No decoded instruction available for {}", x),
_ => panic!("No decoded instruction available for {}", x),
}
}, },
None => panic!("Unmapped instruction!"), None => panic!("Unmapped instruction!"),
} }