Skip to content

Commit a6f2e36

Browse files
Samuel OrtizSamuel Ortiz
Samuel Ortiz
authored and
Samuel Ortiz
committed
do-core1: Move Instruction to the do-core crate
Signed-off-by: Samuel Ortiz <[email protected]>
1 parent 73df1a7 commit a6f2e36

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

do-core/src/instruction.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Error;
1+
use crate::{Error, MAX_REGISTER_INDEX};
22

33
#[allow(dead_code)]
44
#[derive(Clone, Debug, PartialEq)]
@@ -20,3 +20,34 @@ impl OpCode {
2020
}
2121
}
2222
}
23+
24+
#[derive(Debug)]
25+
pub struct Instruction {
26+
opcode: OpCode,
27+
op0: u8,
28+
op1: u8,
29+
}
30+
31+
impl Instruction {
32+
// Instruction constructor, a.k.a. disassembler.
33+
pub fn disassemble(insn: u32) -> Result<Instruction, Error> {
34+
// Keep the first 6 bits only
35+
let opcode = OpCode::from_u8((insn & 0x3f) as u8)?;
36+
37+
// Shift right by 6, keep only the first 5 bits.
38+
let op0 = ((insn >> 6) & 0x1f) as u8;
39+
40+
// Shift right by 11, keep only the first 5 bits.
41+
let op1: u8 = ((insn >> 11) & 0x1f) as u8;
42+
43+
if op0 > MAX_REGISTER_INDEX {
44+
return Err(Error::Op0OutOfRange);
45+
}
46+
47+
if op1 > MAX_REGISTER_INDEX {
48+
return Err(Error::Op1OutOfRange);
49+
}
50+
51+
Ok(Instruction { opcode, op0, op1 })
52+
}
53+
}

do-core/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ pub enum Error {
66
AdditionOverflow(u32, u32),
77
}
88

9+
// do-core1 register indexes range from 0 to 31.
10+
pub const MAX_REGISTER_INDEX: u8 = 31;
11+
912
pub mod instruction;

src/main.rs

+2-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clap::Parser;
2-
use do_core::instruction::OpCode;
3-
use do_core::Error;
2+
use do_core::instruction::{Instruction, OpCode};
3+
use do_core::{Error, MAX_REGISTER_INDEX};
44

55
#[derive(Parser)]
66
#[clap(version, author)]
@@ -10,40 +10,6 @@ struct DoCoreOpts {
1010
insn: String,
1111
}
1212

13-
#[derive(Debug)]
14-
struct Instruction {
15-
opcode: OpCode,
16-
op0: u8,
17-
op1: u8,
18-
}
19-
20-
// do-core1 register indexes range from 0 to 31.
21-
const MAX_REGISTER_INDEX: u8 = 31;
22-
23-
impl Instruction {
24-
// Instruction constructor, a.k.a. disassembler.
25-
fn disassemble(insn: u32) -> Result<Instruction, Error> {
26-
// Keep the first 6 bits only
27-
let opcode = OpCode::from_u8((insn & 0x3f) as u8)?;
28-
29-
// Shift right by 6, keep only the first 5 bits.
30-
let op0 = ((insn >> 6) & 0x1f) as u8;
31-
32-
// Shift right by 11, keep only the first 5 bits.
33-
let op1: u8 = ((insn >> 11) & 0x1f) as u8;
34-
35-
if op0 > MAX_REGISTER_INDEX {
36-
return Err(Error::Op0OutOfRange);
37-
}
38-
39-
if op1 > MAX_REGISTER_INDEX {
40-
return Err(Error::Op1OutOfRange);
41-
}
42-
43-
Ok(Instruction { opcode, op0, op1 })
44-
}
45-
}
46-
4713
fn add(op0: u32, op1: u32) -> Result<u32, Error> {
4814
op0.checked_add(op1)
4915
.ok_or(Error::AdditionOverflow(op0, op1))

0 commit comments

Comments
 (0)