From 183634edb316cf2f1c214d962d1b7dc212a65842 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Fri, 10 Jan 2020 22:03:02 -0500 Subject: [PATCH] Start adding the toolchain The toolchain project will include an instruction set simulator, an assembler, etc. --- toolchain/.gitignore | 1 + toolchain/Cargo.lock | 5 ++++ toolchain/Cargo.toml | 9 ++++++ toolchain/src/cpu.rs | 52 ++++++++++++++++++++++++++++++++ toolchain/src/inst.rs | 69 +++++++++++++++++++++++++++++++++++++++++++ toolchain/src/main.rs | 6 ++++ 6 files changed, 142 insertions(+) create mode 100644 toolchain/.gitignore create mode 100644 toolchain/Cargo.lock create mode 100644 toolchain/Cargo.toml create mode 100644 toolchain/src/cpu.rs create mode 100644 toolchain/src/inst.rs create mode 100644 toolchain/src/main.rs diff --git a/toolchain/.gitignore b/toolchain/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/toolchain/.gitignore @@ -0,0 +1 @@ +/target diff --git a/toolchain/Cargo.lock b/toolchain/Cargo.lock new file mode 100644 index 0000000..e24cc9f --- /dev/null +++ b/toolchain/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "toolchain" +version = "0.1.0" diff --git a/toolchain/Cargo.toml b/toolchain/Cargo.toml new file mode 100644 index 0000000..e910a73 --- /dev/null +++ b/toolchain/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "toolchain" +version = "0.1.0" +authors = ["Cassie Jones "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/toolchain/src/cpu.rs b/toolchain/src/cpu.rs new file mode 100644 index 0000000..42be7ad --- /dev/null +++ b/toolchain/src/cpu.rs @@ -0,0 +1,52 @@ +use std::ops::{Index, IndexMut}; +use crate::inst::Reg; + +struct RegFile { + values: [u8; 4], +} + +impl Index for RegFile { + type Output = u8; + fn index(&self, index: Reg) -> &u8 { + match index { + Reg::R0 => &self.values[0], + Reg::R1 => &self.values[1], + Reg::R2 => &self.values[2], + Reg::R3 => &self.values[3], + } + } +} + +impl IndexMut for RegFile { + fn index_mut(&mut self, index: Reg) -> &mut u8 { + match index { + Reg::R0 => &mut self.values[0], + Reg::R1 => &mut self.values[1], + Reg::R2 => &mut self.values[2], + Reg::R3 => &mut self.values[3], + } + } +} + +pub struct Cpu { + pc: u16, + reg: RegFile, + memory: Vec, +} + +impl Cpu { + pub fn new() -> Self { + Cpu { + reg: RegFile { values: [0; 4] }, + memory: vec![0; 1 << 16], + } + } + + pub fn mem(&self) -> &[u8] { + &self.memory[..] + } + + pub fn mem_mut(&mut self) -> &mut [u8] { + &mut self.memory[..] + } +} diff --git a/toolchain/src/inst.rs b/toolchain/src/inst.rs new file mode 100644 index 0000000..2075230 --- /dev/null +++ b/toolchain/src/inst.rs @@ -0,0 +1,69 @@ +pub enum Reg { + R0, + R1, + R2, + R3, +} + +pub enum RegPair { + R1R0, + R3R2, +} + +pub enum Op1 { + Inc, + Dec, + Neg, + Compl, +} + +pub enum OpI { + Add7(Imm7), + Sub6(Imm6), + Lsl(Imm3), + Lsr(Imm3), + Asr(Imm3), + Rol(Imm3), + And(Imm8), + Or(Imm8), + Xor(Imm8), +} + +pub enum Op2 { + Add, + Sub, + And, + Or, + Xor, + // These are preliminary + Lsl, + Rol, + Bic, +} + +pub struct Imm3(u8); +pub struct Imm5(u8); +pub struct Imm6(u8); +pub struct Imm7(u8); +pub struct Imm8(u8); + +pub enum Addr { + Imm(Imm7), + Off(Reg, Imm5), + One(Reg), + Pair(RegPair), +} + +pub enum Inst { + Mov(Reg, Reg), + CallR(Reg), + Alu1(Reg, Op1), + Load(Reg, Addr), + Store(Addr, Reg), + JumpR(Reg), + JumpI(Imm8), + CallI(Imm8), + AluI(Reg, OpI), + Skip(Reg, Reg), + Alu2(Reg, Op2, Reg), +} diff --git a/toolchain/src/main.rs b/toolchain/src/main.rs new file mode 100644 index 0000000..68e87a4 --- /dev/null +++ b/toolchain/src/main.rs @@ -0,0 +1,6 @@ +mod inst; +mod cpu; + +fn main() { + println!("Hello, world!"); +} -- 2.47.0