This repository has been archived on 2025-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
corten/src/instruction.rs
Anthony Foxclaw 6dd33396f0 Added Jump opcode
- And fixed a oversight in the match statement while I was at it.
2020-02-06 17:42:51 -05:00

54 lines
974 B
Rust

use crate::instruction::Opcode::LOAD;
#[derive(Debug, PartialEq)]
pub enum Opcode {
HLT,
IGL,
LOAD,
ADD,
MUL,
DIV,
JMP,
}
impl From<u8> for Opcode {
fn from(vm: u8) -> Self {
match vm {
0 => Opcode::HLT,
1 => Opcode::LOAD,
2 => Opcode::JMP,
3 => Opcode::ADD,
4 => Opcode::MUL,
5 => Opcode::DIV,
_ => Opcode::IGL,
}
}
}
#[derive(Debug, PartialEq)]
pub struct Instruction {
opcode: Opcode,
}
impl Instruction {
pub fn new(opcode: Opcode) -> Self {
Instruction { opcode }
}
}
#[cfg(test)]
mod instruction_tests {
use crate::instruction::*;
#[test]
fn test_crate_hlt() {
let opcode = Opcode::HLT;
assert_eq!(opcode, Opcode::HLT);
}
#[test]
fn test_create_instruction() {
let inst = Instruction::new(Opcode::HLT);
assert_eq!(inst.opcode, Opcode::HLT);
}
}