瀏覽代碼

parsing from file

jmelesky 4 年之前
父節點
當前提交
9f32f23980
共有 1 個文件被更改,包括 23 次插入22 次删除
  1. 23 22
      src/main.rs

+ 23 - 22
src/main.rs

@@ -3,13 +3,12 @@ use std::fmt;
 use std::fs;
 
 struct UMState {
-    program_counter: u32,
-    regs: [u32; 8],
-    mem: Vec<u32>
+    pc: usize,       // program counter
+    regs: [u32; 8],  // register contents
+    mem: Vec<u32>    // memory contents
 }
 
 
-
 enum UMOp {
     ConditionalMove(u8, u8, u8),
     ArrayIndex(u8, u8, u8),
@@ -29,7 +28,6 @@ enum UMOp {
     Value(u8, u32),
 }
 
-
 fn reg(i:u8) -> &'static str {
     match i {
         0 => "A",
@@ -96,7 +94,7 @@ impl fmt::Display for UMOp {
                 format!("Output #{}",
                         reg(*c)),
             UMOp::Input(c) =>
-                format!("Output #{} <-",
+                format!("Input #{} <-",
                         reg(*c)),
             UMOp::Load(b, c) =>
                 format!("Load <- #{}[#{}]",
@@ -117,14 +115,14 @@ fn num_extract(masks_map: &HashMap<&str, (u32,u32)>,
     (num & masktup.0) >> masktup.1
 }
 
-fn parse_instr(masks_map: HashMap<&str, (u32,u32)>,
+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");
+    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");
     match opcode {
         0  => UMOp::ConditionalMove(rega, regb, regc),
         1  => UMOp::ArrayIndex(rega, regb, regc),
@@ -156,7 +154,7 @@ fn main() {
     masks_map.insert("val_mask",  (0b00000001111111111111111111111111,  0));
 
 
-    let codex8 = &fs::read("data/codex.umz").unwrap();
+    let codex8 = &fs::read("data/sandmark.umz").unwrap();
 
     let mut codex = vec![0u32; codex8.len() / 4];
 
@@ -168,19 +166,22 @@ fn main() {
             codex8[i*4+3] ]);
     }
 
-
-    let instr = codex[0];
-
+    let mut um = UMState {
+        pc: 0,
+        regs: [0,0,0,0,0,0,0,0],
+        mem: codex
+    };
 
 
+    loop {
 
-    
+        let instr = um.mem[um.pc];
+        let full_op = parse_instr(&masks_map, instr);
+        println!("{}", full_op);
 
 
-    let full_op = parse_instr(masks_map, instr);
-
-
-    println!("Hello, world! - {}", full_op);
+        um.pc = um.pc+1;
+    }
 }