cfd16_lib/
parse.rs

1use crate::{CondCode, OpCode};
2
3impl OpCode {
4    /// Parses a mnemonic into an opcode-condition code pair. If no
5    /// possible combination is found for an input value, then the
6    /// method will return None.
7    pub fn parse(mnem: &str) -> Option<(OpCode, bool, CondCode)> {
8        use OpCode::*;
9
10        let mchars = mnem.chars().collect::<Vec<char>>();
11
12        let (mut mnem, cond) = match mchars.as_slice() {
13            [.., 'n', 'z'] => (mnem.strip_suffix("nz"), CondCode::NotZero),
14            [.., 'z'] => (mnem.strip_suffix("z"), CondCode::Zero),
15            ['s', 'c'] => (Some(mnem), CondCode::Always),
16            ['s', 'c', 'c'] => (Some("sc"), CondCode::Carry),
17            [.., 'c'] => (mnem.strip_suffix("c"), CondCode::Carry),
18            [.., 'g', 't'] => (mnem.strip_suffix("gt"), CondCode::Greater),
19            [.., 'g', 'e'] => (mnem.strip_suffix("ge"), CondCode::GreaterEq),
20            [.., 'l', 't'] => (mnem.strip_suffix("lt"), CondCode::Lesser),
21            [.., 'l', 'e'] => (mnem.strip_suffix("le"), CondCode::LesserEq),
22            _ => (Some(mnem), CondCode::Always),
23        };
24
25        let mut sbit = false;
26        if mnem.unwrap().ends_with('s') {
27            sbit = true;
28            mnem = mnem.unwrap().strip_suffix("s");
29        }
30
31        let op = match mnem {
32            Some("add") => Add,
33            Some("and") => And,
34            Some("ashr") => Ashr,
35            Some("br") => Br,
36            Some("cmp") => Cmp,
37            Some("jsr") => Jsr,
38            Some("ldb") => Ldb,
39            Some("ldr") => Ldr,
40            Some("mfhi") => Mfhi,
41            Some("mov") => Mov,
42            Some("movr") => Movr,
43            Some("movt") => Movt,
44            Some("mul") => Mul,
45            Some("not") => Not,
46            Some("or") => Or,
47            Some("pop") => Pop,
48            Some("push") => Push,
49            Some("ret") => Ret,
50            Some("ror") => Ror,
51            Some("sc") => Sc,
52            Some("seg") => Seg,
53            Some("shl") => Shl,
54            Some("shr") => Shr,
55            Some("stb") => Stb,
56            Some("str") => Str,
57            Some("sub") => Sub,
58            Some("uadd") => Uadd,
59            Some("ucmp") => Ucmp,
60            Some("umul") => Umul,
61            Some("usub") => Usub,
62            Some("xor") => Xor,
63            _ => return None,
64        };
65
66        Some((op, sbit, cond))
67    }
68}