Skip to content

Commit

Permalink
feat(li) Implemented decode.
Browse files Browse the repository at this point in the history
  • Loading branch information
x4exr committed Aug 13, 2024
1 parent 8c861e0 commit 51cf4df
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
10 changes: 5 additions & 5 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use arrseq_lightning::num::MaskedU8;

fn main() {
let c_max = 10u16;

let program = [
Instruction::LoadImmediate { destination: RegisterCode::new(0), segment: MaskedU8::new(0), immediate: c_max }, // li r0, c_max
Instruction::DualSource { operation: operation::DualSource::Compare, sources: [RegisterCode::new(0), RegisterCode::new(1)] }, // cmp r0, r1
Instruction::Branch { hint: None, condition: Flag::Zero, address: Address::Immediate { immediate: address::Immediate::new(8), mode: address::Mode::Relative }}, // jz pc+8
Instruction::Memory { operation: operation::Memory::Branch, address: Address::Immediate { immediate: address::Immediate::new(4), mode: address::Mode::Absolute }}, // jmp 0
Instruction::WaitForInterrupt // hlt

// pseudo code:
//
//
// ```
// let count = c_max;
// loop {
Expand All @@ -25,6 +25,6 @@ fn main() {
// }
// ```
];
dbg!(Instruction::decode(0b1111111111111111_00_0000001));

dbg!(Instruction::decode(0b1111111111111111_00_1111_0000001));
}
45 changes: 42 additions & 3 deletions src/instruction/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
use crate::instruction::Instruction;
use crate::instruction::{Format, Instruction, operation, RegisterCode, SegmentCode};

pub const OPERATION_MASK: u32 = 0x0000007F;

pub const LOAD_IMMEDIATE_DESTINATION: u32 = 0b00000000_00000000_00000000_00001111;
pub const LOAD_IMMEDIATE_SEGMENT : (u32, u32) = (4, 0b00000000_00000000_00000000_00110000);
pub const LOAD_IMMEDIATE_IMMEDIATE : (u32, u32) = (6, 0b00000000_00111111_11111111_11000000);

impl Instruction {
pub fn decode(encoded: u32) {
const fn decode_load_immediate(operands: u32) -> (RegisterCode, SegmentCode, u16) {
let destination = RegisterCode::new((operands & LOAD_IMMEDIATE_DESTINATION) as u8);
let segment = SegmentCode::new(((operands & LOAD_IMMEDIATE_SEGMENT.1) >> LOAD_IMMEDIATE_SEGMENT.0) as u8);
let immediate = ((operands & LOAD_IMMEDIATE_IMMEDIATE.1) >> LOAD_IMMEDIATE_IMMEDIATE.0) as u16;
(destination, segment, immediate)
}

pub fn decode(encoded: u32) -> Self {
let operation = encoded & OPERATION_MASK;
dbg!(operation);

let format = operation::MAPPINGS
.get(operation as usize)
.unwrap_or(&operation::MAPPINGS[0])
.format;

let operands = (encoded & !OPERATION_MASK) >> 7;

match format {
Format::WaitForInterrupt => Self::WaitForInterrupt,
Format::LoadImmediate => {
let (destination, segment, immediate) = Self::decode_load_immediate(operands);
Self::LoadImmediate { destination, segment, immediate }
},
Format::LoadVectorComponents => todo!(),
Format::ExtractVectorComponents => todo!(),
Format::FlagVectorComponents => todo!(),
Format::MapVector => todo!(),
Format::Branch => todo!(),
Format::DualSource => todo!(),
Format::Destination => todo!(),
Format::DestinationSource => todo!(),
Format::DestinationDualSource => todo!(),
Format::DestinationTripleSource => todo!(),
Format::DualDestinationDualSource => todo!(),
Format::Memory => todo!(),
Format::SourceMemory => todo!(),
Format::DestinationMemory => todo!()
}
}
}
4 changes: 2 additions & 2 deletions src/instruction/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl DestinationDualSource {
pub enum DestinationTripleSource {
MultiplyAndAdd,
AddAndMultiply

// todo: Consider neccessity and operation space
}

Expand All @@ -86,7 +86,7 @@ pub enum DualDestinationDualSource {

impl DualDestinationDualSource {
pub const MAPPINGS: [(Code, DualDestinationDualSource); 4] = [
(Code::new(21), Self::DivideWithRemainder), (Code::new(22), Self::DivideFloatingWithRemainder),
(Code::new(21), Self::DivideWithRemainder), (Code::new(22), Self::DivideFloatWithRemainder),
(Code::new(23), Self::DivideVectorWithRemainder), (Code::new(24), Self::DivideFloatVectorWithRemainder),
];
}
Expand Down

0 comments on commit 51cf4df

Please sign in to comment.