Browse Source

remove slow hashmap, some linting cleanup

jmelesky 4 years ago
parent
commit
62c4ef53f9
1 changed files with 23 additions and 32 deletions
  1. 23 32
      src/main.rs

+ 23 - 32
src/main.rs

@@ -85,7 +85,7 @@ impl fmt::Display for UMOp {
                         reg(*b),
                         reg(*c)),
             UMOp::Halt =>
-                format!("Halt!"),
+                "Halt!".to_string(),
             UMOp::Allocate(b, c) =>
                 format!("Alloc #{} <- [#{}]",
                         reg(*b),
@@ -112,20 +112,18 @@ impl fmt::Display for UMOp {
     }
 }
 
-fn num_extract(masks_map: &HashMap<&str, (u32,u32)>,
-               num: u32, mask_name: &str) -> u32 {
-    let masktup = masks_map.get(mask_name).unwrap();
-    (num & masktup.0) >> masktup.1
+fn num_extract(num: u32, mask: u32, shiftcount: u32) -> u32 {
+    (num & mask) >> shiftcount
 }
 
-fn parse_instr(masks_map: &HashMap<&str, (u32,u32)>,
-               instr: u32) -> UMOp {
-    let opcode = num_extract(masks_map, instr, "op_mask");
-    let rega   = num_extract(masks_map, instr, "rega_mask") as u8;
-    let regb   = num_extract(masks_map, instr, "regb_mask") as u8;
-    let regc   = num_extract(masks_map, instr, "regc_mask") as u8;
-    let alta   = num_extract(masks_map, instr, "alta_mask") as u8;
-    let val    = num_extract(masks_map, instr, "val_mask");
+fn parse_instr(instr: u32) -> UMOp {
+
+    let opcode = num_extract(instr, 0b11110000000000000000000000000000, 28);
+    let rega   = num_extract(instr, 0b00000000000000000000000111000000,  6) as u8;
+    let regb   = num_extract(instr, 0b00000000000000000000000000111000,  3) as u8;
+    let regc   = num_extract(instr, 0b00000000000000000000000000000111,  0) as u8;
+    let alta   = num_extract(instr, 0b00001110000000000000000000000000, 25) as u8;
+    let val    = num_extract(instr, 0b00000001111111111111111111111111,  0);
     match opcode {
         0  => UMOp::ConditionalMove(rega, regb, regc),
         1  => UMOp::ArrayIndex(rega, regb, regc),
@@ -192,14 +190,17 @@ fn um_step(um: &mut UMState, op: UMOp) {
         },
         UMOp::Input(c) => {
             let input: String = read!();
-            um.regs[c as usize] = input.chars().nth(0).unwrap() as u32 as u8 as u32;
+            um.regs[c as usize] = input.chars().next().unwrap() as u32 as u8 as u32;
             if um.regs[c as usize] == 4 {
                 um.regs[c as usize] = 255
             }
         },
         UMOp::Load(b, c) => {
-            let new_arr = um.mem[um.arrmap[&um.regs[b as usize]]].clone();
-            um.mem[0] = new_arr;
+            let new_arr_num = um.regs[b as usize] as usize;
+            if new_arr_num != 0 {
+                let new_arr = um.mem[new_arr_num].clone();
+                um.mem[0] = new_arr;
+            }
             um.pc = um.regs[c as usize] as usize;
         },
         UMOp::Value(a, val) => {
@@ -208,14 +209,13 @@ fn um_step(um: &mut UMState, op: UMOp) {
     }
 }
 
-fn um_run(masks: HashMap<&str, (u32,u32)>,
-          um: &mut UMState) {
+fn um_run(um: &mut UMState) {
     loop {
-        let instr = um.mem[0][um.pc];
-        let full_op = parse_instr(&masks, instr);
+        let instr = um.mem[um.arrmap[&0]][um.pc];
+        let full_op = parse_instr(instr);
         //println!("{}", full_op);
 
-        um.pc = um.pc+1;
+        um.pc += 1;
 
         um_step(um, full_op);
     }
@@ -224,15 +224,6 @@ fn um_run(masks: HashMap<&str, (u32,u32)>,
 fn main() {
     let args: Vec<String> = env::args().collect();
 
-    let mut masks_map: HashMap<&str, (u32,u32)> = HashMap::new();
-    masks_map.insert("op_mask",   (0b11110000000000000000000000000000, 28));
-    masks_map.insert("rega_mask", (0b00000000000000000000000111000000,  6));
-    masks_map.insert("regb_mask", (0b00000000000000000000000000111000,  3));
-    masks_map.insert("regc_mask", (0b00000000000000000000000000000111,  0));
-    masks_map.insert("alta_mask", (0b00001110000000000000000000000000, 25));
-    masks_map.insert("val_mask",  (0b00000001111111111111111111111111,  0));
-
-
     let codex8 = &fs::read(&args[1]).unwrap();
 
     let mut codex = vec![0u32; codex8.len() / 4];
@@ -250,12 +241,12 @@ fn main() {
 
     let mut um = UMState {
         pc: 0,
-        arrmap: arrmap,
+        arrmap,
         regs: [0,0,0,0,0,0,0,0],
         mem: vec![codex]
     };
 
-    um_run(masks_map, &mut um);
+    um_run(&mut um);
 }