[−][src]Crate imperative_rs
This crate provides the InstructionSet
-trait and corresponding error types, as well as
a procedural macro automatically derive the trait for enum
s. A type implementing
InstructionSet
provides fn InstructionSet::decode(...) -> {...}
to decode instructions from a &[u8]
and fn InstructionSet::encode(...) -> {...}
to encode and write an instruction into a &[u8]
.
use imperative::InstructionSet; #[derive(InstructionSet)] enum Is { //constant opcode #[opcode = "0x0000"] Nop, //hex opcode with split variable x #[opcode = "0x1x0x"] Inc{x:u8}, //hex opcode with three variables #[ opcode = "0x2xxyyzz" ] Add{x:u8, y:u8, z:u8}, //bin opcode with two variables and underscores for readability #[ opcode = "0b100000000_xxxxyyyyy_xyxyxyxy" ] Mov{x:u8, y:i8}, } fn main() { let mut mem = [0u8; 1024]; let (num_bytes, Is::Nop) = Is::decode(&mem).unwrap(); let instruction = Is::Add{x:0xab, y:0xcd, z:0xef}; assert_eq!(4, instruction.encode(&mut mem[100..]).unwrap()); assert_eq!([0x2a, 0xbc, 0xde, 0xf0], mem[100..104]); }
Enums
DecodeError | This type is returned by |
EncodeError | This Type is returned by `fn InstructionSet::encode(...) -> {...} when the instruction could not be encoded. |
Traits
InstructionSet | This |