From ce6cd97b118f48343a2f6b58eae6b05f0e493fb7 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Thu, 16 Jan 2020 02:11:40 -0500 Subject: [PATCH] Modify the Rust instruction definition for v0.2 --- toolchain/src/inst.rs | 109 +++++++++++++++++++++++++++--------------- toolchain/src/lib.rs | 2 + toolchain/src/main.rs | 3 -- 3 files changed, 72 insertions(+), 42 deletions(-) create mode 100644 toolchain/src/lib.rs diff --git a/toolchain/src/inst.rs b/toolchain/src/inst.rs index f67d0cb..ff0788b 100644 --- a/toolchain/src/inst.rs +++ b/toolchain/src/inst.rs @@ -1,6 +1,8 @@ mod decode; mod encode; +use std::cmp::PartialEq; + #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)] pub enum Reg { R0, @@ -10,9 +12,10 @@ pub enum Reg { } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum RegPair { - R1R0, - R3R2, +#[repr(u8)] +pub enum LdSt { + Ld, + St, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -25,15 +28,11 @@ pub enum Op1 { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum OpI { - Add7(Imm7), - Sub6(Imm6), - Lsl(Imm3), - Lsr(Imm3), - Asr(Imm3), - Rol(Imm3), - And(Imm8), - Or(Imm8), - Xor(Imm8), + Add(I8), + Lsl(U4), + Lsr(U4), + Asr(U4), + Rol(U4), } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -50,48 +49,80 @@ pub enum Op2 { } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Imm3(u8); +pub struct U4(i8); +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct I5(i8); #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Imm5(u8); +pub struct I7(i8); #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Imm6(u8); +pub struct U7(u8); #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Imm7(u8); +pub struct U8(u8); #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Imm8(u8); +pub struct I8(i8); #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Addr { - Imm(Imm7), - Off(Reg, Imm5), - One(Reg), - Pair(RegPair), +#[repr(u8)] +pub enum Size { + Byte, + Word, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(u8)] +pub enum Half { + Low, + High, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Addr { + Fixed(U7), + Reg(Reg), + Extended{ + base: Reg, + offset: I5, + stack: bool, + size: Size, + }, +} + +#[derive(Clone, Copy, Debug, Eq)] pub enum Cond { - Eq, - Ne, - Test, - TestNot, - Lt, - Ult, + Eq(Reg, Reg), + Ne(Reg, Reg), + Test(Reg ,Reg), + TestNot(Reg, Reg), + Lt(Reg, Reg), + Ult(Reg, Reg), } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Inst { + Halt, + Nope, + Alu2(Reg, Op2, Reg), + Jalr(Reg), Move(Reg, Reg), - CallR(Reg), Alu1(Reg, Op1), - Load(Reg, Addr), - Store(Addr, Reg), - JumpR(Reg), - JumpI(Imm8), - CallI(Imm8), - // @TODO: Implement the PartialEq for Cond symmetric in the registers for - // Eq, Ne, Test, and TestNot. - Branch(Cond, Reg, Reg), + Mem(LdSt, Reg, Addr), + Branch(Cond, I7), + JumpI(I8), AluI(Reg, OpI), - Skip(Reg, Reg), - Alu2(Reg, Op2, Reg), + LdImm(Half, Reg, U8), +} + +impl PartialEq for Cond { + fn eq(&self, other: &Cond) -> bool { + use Cond::*; + match (self, other) { + (Eq(a1, b1), Eq(a2, b2)) => (a1, b1) == (a2, b2) || (a1, b1) == (b2, a2), + (Ne(a1, b1), Ne(a2, b2)) => (a1, b1) == (a2, b2) || (a1, b1) == (b2, a2), + (Test(a1, b1), Test(a2, b2)) => (a1, b1) == (a2, b2) || (a1, b1) == (b2, a2), + (TestNot(a1, b1), TestNot(a2, b2)) => (a1, b1) == (a2, b2) || (a1, b1) == (b2, a2), + (Lt(a1, b1), Lt(a2, b2)) => (a1, b1) == (a2, b2), + (Ult(a1, b1), Ult(a2, b2)) => (a1, b1) == (a2, b2), + _ => false, + } + } } diff --git a/toolchain/src/lib.rs b/toolchain/src/lib.rs new file mode 100644 index 0000000..dda0093 --- /dev/null +++ b/toolchain/src/lib.rs @@ -0,0 +1,2 @@ +pub mod inst; +pub mod cpu; diff --git a/toolchain/src/main.rs b/toolchain/src/main.rs index 69a3e2c..e7a11a9 100644 --- a/toolchain/src/main.rs +++ b/toolchain/src/main.rs @@ -1,6 +1,3 @@ -mod cpu; -mod inst; - fn main() { println!("Hello, world!"); } -- 2.47.0