From 0584dca3adfe687b33e48f8f69e6e349fad33d99 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Mon, 21 Oct 2024 23:44:14 +0200 Subject: [PATCH] isa: encode ALU instructions --- src/isa/alu/bytecode.rs | 83 +++++++++++++++++++++++++--- src/isa/alu/exec.rs | 4 +- src/isa/bytecode.rs | 68 +++++++++++------------ src/isa/gfa/bytecode.rs | 50 ++++++++--------- src/library/marshaller.rs | 110 +++++++++++++++++++------------------- 5 files changed, 192 insertions(+), 123 deletions(-) diff --git a/src/isa/alu/bytecode.rs b/src/isa/alu/bytecode.rs index ff9f456..5cbf49e 100644 --- a/src/isa/alu/bytecode.rs +++ b/src/isa/alu/bytecode.rs @@ -24,19 +24,34 @@ use core::ops::RangeInclusive; -use super::{CtrlInstr, RegInstr}; -use crate::core::SiteId; +use super::{CtrlInstr, MaybeU128, RegInstr}; +use crate::core::{SiteId, A}; use crate::isa::bytecode::CodeEofError; use crate::isa::{Bytecode, BytecodeRead, BytecodeWrite, Instr, InstructionSet, ReservedInstr}; +use crate::Site; -impl> Bytecode for Instr { +impl + Bytecode> Bytecode for Instr { fn op_range() -> RangeInclusive { todo!() } - fn opcode_byte(&self) -> u8 { todo!() } + fn opcode_byte(&self) -> u8 { + match self { + Instr::Ctrl(instr) => instr.opcode_byte(), + Instr::Reg(instr) => Bytecode::::opcode_byte(instr), + Instr::GFqA(instr) => Bytecode::::opcode_byte(instr), + Instr::Reserved(instr) => Bytecode::::opcode_byte(instr), + Instr::Ext(instr) => instr.opcode_byte(), + } + } fn encode_operands(&self, writer: &mut W) -> Result<(), W::Error> where W: BytecodeWrite { - todo!() + match self { + Instr::Ctrl(instr) => instr.encode_operands(writer), + Instr::Reg(instr) => instr.encode_operands(writer), + Instr::GFqA(instr) => instr.encode_operands(writer), + Instr::Reserved(instr) => instr.encode_operands(writer), + Instr::Ext(instr) => instr.encode_operands(writer), + } } fn decode_operands(reader: &mut R, opcode: u8) -> Result @@ -74,7 +89,28 @@ impl Bytecode for CtrlInstr { fn encode_operands(&self, writer: &mut W) -> Result<(), W::Error> where W: BytecodeWrite { - todo!() + match *self { + CtrlInstr::Nop + | CtrlInstr::Chk + | CtrlInstr::FailCk + | CtrlInstr::RsetCk + | CtrlInstr::NotCo + | CtrlInstr::Ret + | CtrlInstr::Stop => {} + + CtrlInstr::Jmp { pos } | CtrlInstr::JifCo { pos } | CtrlInstr::JifCk { pos } | CtrlInstr::Fn { pos } => { + writer.write_fixed(pos.to_le_bytes())? + } + CtrlInstr::Shift { shift } | CtrlInstr::ShIfCo { shift } | CtrlInstr::ShIfCk { shift } => { + writer.write_byte(shift.to_le_bytes()[0])? + } + CtrlInstr::Call { site } | CtrlInstr::Exec { site } => { + let site = Site::new(site.prog_id, site.offset); + writer.write_ref(site.prog_id)?; + writer.write_word(site.offset)?; + } + } + Ok(()) } fn decode_operands(reader: &mut R, opcode: u8) -> Result @@ -93,7 +129,40 @@ impl Bytecode for RegInstr { fn encode_operands(&self, writer: &mut W) -> Result<(), W::Error> where W: BytecodeWrite { - todo!() + match *self { + RegInstr::Clr { dst } => { + writer.write_byte(dst.to_u8())?; + } + RegInstr::Put { dst, val } | RegInstr::Pif { dst, val } => { + writer.write_byte(dst.to_u8())?; + let MaybeU128::U128(val) = val else { + panic!("an attempt to serialize program with missed data"); + }; + match dst.a() { + A::A8 => writer.write_byte(val as u8)?, + A::A16 => writer.write_word(val as u16)?, + A::A32 => writer.write_fixed(val.to_le_bytes())?, + A::A64 => writer.write_fixed(val.to_le_bytes())?, + A::A128 => writer.write_fixed(val.to_le_bytes())?, + } + } + RegInstr::Test { src } => { + writer.write_byte(src.to_u8())?; + } + RegInstr::Cpy { dst, src } => { + writer.write_byte(dst.to_u8())?; + writer.write_byte(src.to_u8())?; + } + RegInstr::Swp { src_dst1, src_dst2 } => { + writer.write_byte(src_dst1.to_u8())?; + writer.write_byte(src_dst2.to_u8())?; + } + RegInstr::Eq { src1, src2 } => { + writer.write_byte(src1.to_u8())?; + writer.write_byte(src2.to_u8())?; + } + } + Ok(()) } fn decode_operands(reader: &mut R, opcode: u8) -> Result diff --git a/src/isa/alu/exec.rs b/src/isa/alu/exec.rs index 10ccc7b..3cb1503 100644 --- a/src/isa/alu/exec.rs +++ b/src/isa/alu/exec.rs @@ -106,9 +106,9 @@ impl Instruction for ReservedInstr { impl Instruction for CtrlInstr { type Context<'ctx> = (); - fn src_regs(&self) -> BTreeSet { todo!() } + fn src_regs(&self) -> BTreeSet { none!() } - fn dst_regs(&self) -> BTreeSet { todo!() } + fn dst_regs(&self) -> BTreeSet { none!() } fn op_data_bytes(&self) -> u16 { todo!() } diff --git a/src/isa/bytecode.rs b/src/isa/bytecode.rs index e3f2495..f4668ce 100644 --- a/src/isa/bytecode.rs +++ b/src/isa/bytecode.rs @@ -50,7 +50,7 @@ pub trait Bytecode { /// Write an instruction as bytecode. fn encode_instr(&self, writer: &mut W) -> Result<(), W::Error> where W: BytecodeWrite { - writer.write_u8(self.opcode_byte())?; + writer.write_byte(self.opcode_byte())?; self.encode_operands(writer)?; writer.check_aligned(); Ok(()) @@ -66,7 +66,7 @@ pub trait Bytecode { Self: Sized, R: BytecodeRead, { - let opcode = reader.read_u8()?; + let opcode = reader.read_byte()?; let instr = Self::decode_operands(reader, opcode)?; reader.check_aligned(); Ok(instr) @@ -85,7 +85,7 @@ pub trait Bytecode { pub struct CodeEofError; /// Reader from a bytecode for instruction deserialization. -pub trait BytecodeRead { +pub trait BytecodeRead { /// Return current byte offset of the cursor. Does not account for bits. /// If the position is exactly at EOF, returns `None`. fn pos(&self) -> u16; @@ -102,38 +102,38 @@ pub trait BytecodeRead { fn peek_byte(&self) -> Result; fn read_reg_a(&mut self) -> Result { - let a = A::from(self.read_u3()?); - let idx = IdxA::from(self.read_u5()?); + let a = A::from(self.read_3bits()?); + let idx = IdxA::from(self.read_5bits()?); Ok(RegA::with(a, idx)) } fn read_pair_a(&mut self) -> Result<(A, IdxA, IdxA), CodeEofError> { - let a = A::from(self.read_u3()?); - let idx1 = IdxA::from(self.read_u5()?); - let idx2 = IdxA::from(self.read_u5()?); + let a = A::from(self.read_3bits()?); + let idx1 = IdxA::from(self.read_5bits()?); + let idx2 = IdxA::from(self.read_5bits()?); Ok((a, idx1, idx2)) } /// Read single bit as a bool value. - fn read_bool(&mut self) -> Result { Ok(self.read_u1()? == u1::ONE) } + fn read_bool(&mut self) -> Result { Ok(self.read_1bit()? == u1::ONE) } /// Read single bit. - fn read_u1(&mut self) -> Result; + fn read_1bit(&mut self) -> Result; /// Read two bits. - fn read_u2(&mut self) -> Result; + fn read_2bits(&mut self) -> Result; /// Read three bits. - fn read_u3(&mut self) -> Result; + fn read_3bits(&mut self) -> Result; /// Read four bits. - fn read_u4(&mut self) -> Result; + fn read_4bits(&mut self) -> Result; /// Read five bits. - fn read_u5(&mut self) -> Result; + fn read_5bits(&mut self) -> Result; /// Read six bits. - fn read_u6(&mut self) -> Result; + fn read_6bits(&mut self) -> Result; /// Read seven bits. - fn read_u7(&mut self) -> Result; + fn read_7bits(&mut self) -> Result; - /// Read unsigned 8-bit integer. - fn read_u8(&mut self) -> Result; - /// Read unsigned 16-bit integer. - fn read_u16(&mut self) -> Result; + /// Read byte. + fn read_byte(&mut self) -> Result; + /// Read word. + fn read_word(&mut self) -> Result; /// Read fixed number of bytes and convert it into a result type. /// @@ -164,36 +164,36 @@ pub trait BytecodeRead { } /// Writer converting instructions into a bytecode. -pub trait BytecodeWrite { +pub trait BytecodeWrite { type Error: Error; /// Write a single bit from a bool value. fn write_bool(&mut self, data: bool) -> Result<(), Self::Error> { - self.write_u1(if data { u1::ONE } else { u1::ZERO }) + self.write_1bit(if data { u1::ONE } else { u1::ZERO }) } /// Write a single bit. - fn write_u1(&mut self, data: impl Into) -> Result<(), Self::Error>; + fn write_1bit(&mut self, data: impl Into) -> Result<(), Self::Error>; /// Write two bits. - fn write_u2(&mut self, data: impl Into) -> Result<(), Self::Error>; + fn write_2bits(&mut self, data: impl Into) -> Result<(), Self::Error>; /// Write three bits. - fn write_u3(&mut self, data: impl Into) -> Result<(), Self::Error>; + fn write_3bits(&mut self, data: impl Into) -> Result<(), Self::Error>; /// Write four bits. - fn write_u4(&mut self, data: impl Into) -> Result<(), Self::Error>; + fn write_4bits(&mut self, data: impl Into) -> Result<(), Self::Error>; /// Write five bits. - fn write_u5(&mut self, data: impl Into) -> Result<(), Self::Error>; + fn write_5bits(&mut self, data: impl Into) -> Result<(), Self::Error>; /// Write six bits. - fn write_u6(&mut self, data: impl Into) -> Result<(), Self::Error>; + fn write_6bits(&mut self, data: impl Into) -> Result<(), Self::Error>; /// Write seven bits. - fn write_u7(&mut self, data: impl Into) -> Result<(), Self::Error>; + fn write_7bits(&mut self, data: impl Into) -> Result<(), Self::Error>; - /// Write unsigned 8-bit integer. - fn write_u8(&mut self, data: u8) -> Result<(), Self::Error>; - /// Write unsigned 16-bit integer. - fn write_u16(&mut self, data: u16) -> Result<(), Self::Error>; + /// Write byte. + fn write_byte(&mut self, data: u8) -> Result<(), Self::Error>; + /// Write word. + fn write_word(&mut self, data: u16) -> Result<(), Self::Error>; /// Write data representable as a fixed-length byte array. - fn write_fixed(&mut self, data: [u8; LEN]) -> Result<(), Self::Error>; + fn write_fixed(&mut self, data: [u8; LEN]) -> Result<(), Self::Error>; /// Write variable-length byte string. fn write_bytes(&mut self, data: &[u8]) -> Result<(), Self::Error>; diff --git a/src/isa/gfa/bytecode.rs b/src/isa/gfa/bytecode.rs index dcbc859..affa807 100644 --- a/src/isa/gfa/bytecode.rs +++ b/src/isa/gfa/bytecode.rs @@ -56,29 +56,29 @@ impl Bytecode for FieldInstr { where W: BytecodeWrite { match *self { FieldInstr::IncMod { src_dst, val } => { - writer.write_u8(src_dst.to_u8())?; - writer.write_u8(val)?; + writer.write_byte(src_dst.to_u8())?; + writer.write_byte(val)?; } FieldInstr::DecMod { src_dst, val } => { - writer.write_u8(src_dst.to_u8())?; - writer.write_u8(val)?; + writer.write_byte(src_dst.to_u8())?; + writer.write_byte(val)?; } FieldInstr::NegMod { src_dst } => { - writer.write_u8(src_dst.to_u8())?; + writer.write_byte(src_dst.to_u8())?; } FieldInstr::AddMod { reg, dst, src1, src2 } => { - writer.write_u1(u1::ZERO)?; - writer.write_u3(reg.to_u3())?; - writer.write_u4(dst.to_u4())?; - writer.write_u4(src1.to_u4())?; - writer.write_u4(src2.to_u4())?; + writer.write_1bit(u1::ZERO)?; + writer.write_3bits(reg.to_u3())?; + writer.write_4bits(dst.to_u4())?; + writer.write_4bits(src1.to_u4())?; + writer.write_4bits(src2.to_u4())?; } FieldInstr::MulMod { reg, dst, src1, src2 } => { - writer.write_u1(u1::ONE)?; - writer.write_u3(reg.to_u3())?; - writer.write_u4(dst.to_u4())?; - writer.write_u4(src1.to_u4())?; - writer.write_u4(src2.to_u4())?; + writer.write_1bit(u1::ONE)?; + writer.write_3bits(reg.to_u3())?; + writer.write_4bits(dst.to_u4())?; + writer.write_4bits(src1.to_u4())?; + writer.write_4bits(src2.to_u4())?; } } Ok(()) @@ -91,25 +91,25 @@ impl Bytecode for FieldInstr { { Ok(match opcode - Self::START { Self::INC_MOD => { - let src_dst = RegA::from(reader.read_u8()?); - let val = reader.read_u8()?; + let src_dst = RegA::from(reader.read_byte()?); + let val = reader.read_byte()?; FieldInstr::IncMod { src_dst, val } } Self::DEC_MOD => { - let src_dst = RegA::from(reader.read_u8()?); - let val = reader.read_u8()?; + let src_dst = RegA::from(reader.read_byte()?); + let val = reader.read_byte()?; FieldInstr::IncMod { src_dst, val } } Self::NEG_MOD => { - let src_dst = RegA::from(reader.read_u8()?); + let src_dst = RegA::from(reader.read_byte()?); FieldInstr::NegMod { src_dst } } Self::ADD_MUL => { - let subop = reader.read_u1()?; - let reg = A::from(reader.read_u3()?); - let dst = IdxAl::from(reader.read_u4()?); - let src1 = IdxAl::from(reader.read_u4()?); - let src2 = IdxAl::from(reader.read_u4()?); + let subop = reader.read_1bit()?; + let reg = A::from(reader.read_3bits()?); + let dst = IdxAl::from(reader.read_4bits()?); + let src1 = IdxAl::from(reader.read_4bits()?); + let src2 = IdxAl::from(reader.read_4bits()?); match subop { u1::ZERO => FieldInstr::AddMod { reg, dst, src1, src2 }, u1::ONE => FieldInstr::MulMod { reg, dst, src1, src2 }, diff --git a/src/library/marshaller.rs b/src/library/marshaller.rs index 7ab5401..d77c849 100644 --- a/src/library/marshaller.rs +++ b/src/library/marshaller.rs @@ -272,53 +272,53 @@ where fn read_bool(&mut self) -> Result { Ok(self.read(u5::with(1))? == 0x01) } - fn read_u1(&mut self) -> Result { + fn read_1bit(&mut self) -> Result { let res = self.read(u5::with(1))? as u8; Ok(res.try_into().expect("bit extractor failure")) } - fn read_u2(&mut self) -> Result { + fn read_2bits(&mut self) -> Result { let res = self.read(u5::with(2))? as u8; Ok(res.try_into().expect("bit extractor failure")) } - fn read_u3(&mut self) -> Result { + fn read_3bits(&mut self) -> Result { let res = self.read(u5::with(3))? as u8; Ok(res.try_into().expect("bit extractor failure")) } - fn read_u4(&mut self) -> Result { + fn read_4bits(&mut self) -> Result { let res = self.read(u5::with(4))? as u8; Ok(res.try_into().expect("bit extractor failure")) } - fn read_u5(&mut self) -> Result { + fn read_5bits(&mut self) -> Result { let res = self.read(u5::with(5))? as u8; Ok(res.try_into().expect("bit extractor failure")) } - fn read_u6(&mut self) -> Result { + fn read_6bits(&mut self) -> Result { let res = self.read(u5::with(6))? as u8; Ok(res.try_into().expect("bit extractor failure")) } - fn read_u7(&mut self) -> Result { + fn read_7bits(&mut self) -> Result { let res = self.read(u5::with(7))? as u8; Ok(res.try_into().expect("bit extractor failure")) } - fn read_u8(&mut self) -> Result { + fn read_byte(&mut self) -> Result { let res = self.read(u5::with(8))? as u8; Ok(res) } - fn read_u16(&mut self) -> Result { + fn read_word(&mut self) -> Result { let res = self.read(u5::with(16))? as u16; Ok(res) } fn read_fixed(&mut self, f: impl FnOnce([u8; LEN]) -> N) -> Result { - let pos = self.read_u16()? as usize; + let pos = self.read_word()? as usize; let end = pos + LEN; if end >= self.data.as_ref().len() { return Err(CodeEofError); @@ -329,8 +329,8 @@ where } fn read_bytes(&mut self) -> Result<(SmallBlob, bool), CodeEofError> { - let pos = self.read_u16()? as usize; - let end = pos + self.read_u16()? as usize; + let pos = self.read_word()? as usize; + let end = pos + self.read_word()? as usize; let ck = end >= self.data.as_ref().len(); let data = &self.data.as_ref()[pos.min(0xFF)..end.min(0xFF)]; Ok((SmallBlob::from_slice_checked(data), ck)) @@ -338,7 +338,7 @@ where fn read_ref(&mut self) -> Result where LibId: Sized { - let pos = self.read_u8()? as usize; + let pos = self.read_byte()? as usize; Ok(self.libs.iter().nth(pos).copied().unwrap_or_default()) } @@ -353,57 +353,57 @@ where { type Error = MarshallError; - fn write_u1(&mut self, data: impl Into) -> Result<(), MarshallError> { + fn write_1bit(&mut self, data: impl Into) -> Result<(), MarshallError> { self.write(data.into().into_u8() as u32, u5::with(1)) .map_err(MarshallError::from) } - fn write_u2(&mut self, data: impl Into) -> Result<(), MarshallError> { + fn write_2bits(&mut self, data: impl Into) -> Result<(), MarshallError> { self.write(data.into().to_u8() as u32, u5::with(2)) .map_err(MarshallError::from) } - fn write_u3(&mut self, data: impl Into) -> Result<(), MarshallError> { + fn write_3bits(&mut self, data: impl Into) -> Result<(), MarshallError> { self.write(data.into().to_u8() as u32, u5::with(3)) .map_err(MarshallError::from) } - fn write_u4(&mut self, data: impl Into) -> Result<(), MarshallError> { + fn write_4bits(&mut self, data: impl Into) -> Result<(), MarshallError> { self.write(data.into().to_u8() as u32, u5::with(4)) .map_err(MarshallError::from) } - fn write_u5(&mut self, data: impl Into) -> Result<(), MarshallError> { + fn write_5bits(&mut self, data: impl Into) -> Result<(), MarshallError> { self.write(data.into().to_u8() as u32, u5::with(5)) .map_err(MarshallError::from) } - fn write_u6(&mut self, data: impl Into) -> Result<(), MarshallError> { + fn write_6bits(&mut self, data: impl Into) -> Result<(), MarshallError> { self.write(data.into().to_u8() as u32, u5::with(6)) .map_err(MarshallError::from) } - fn write_u7(&mut self, data: impl Into) -> Result<(), MarshallError> { + fn write_7bits(&mut self, data: impl Into) -> Result<(), MarshallError> { self.write(data.into().to_u8() as u32, u5::with(7)) .map_err(MarshallError::from) } - fn write_u8(&mut self, data: u8) -> Result<(), MarshallError> { + fn write_byte(&mut self, data: u8) -> Result<(), MarshallError> { self.write(data as u32, u5::with(8)) .map_err(MarshallError::from) } - fn write_u16(&mut self, data: u16) -> Result<(), MarshallError> { + fn write_word(&mut self, data: u16) -> Result<(), MarshallError> { self.write(data as u32, u5::with(16)) .map_err(MarshallError::from) } - fn write_fixed(&mut self, data: [u8; LEN]) -> Result<(), Self::Error> { + fn write_fixed(&mut self, data: [u8; LEN]) -> Result<(), Self::Error> { if LEN >= u16::MAX as usize { return Err(MarshallError::DataExceedsLimit(LEN)); } let offset = self.write_unique(&data)?; - self.write_u16(offset) + self.write_word(offset) } fn write_bytes(&mut self, data: &[u8]) -> Result<(), Self::Error> { @@ -412,8 +412,8 @@ where return Err(MarshallError::DataExceedsLimit(len)); } let offset = self.write_unique(&data)?; - self.write_u16(offset)?; - self.write_u16(len as u16) + self.write_word(offset)?; + self.write_word(len as u16) } fn write_ref(&mut self, id: LibId) -> Result<(), Self::Error> { @@ -422,7 +422,7 @@ where .iter() .position(|lib| *lib == id) .ok_or(MarshallError::LibAbsent(id))?; - self.write_u8(pos as u8) + self.write_byte(pos as u8) } fn check_aligned(&self) { debug_assert_eq!(self.bit_pos, u3::ZERO, "not all instruction operands are written") } @@ -436,19 +436,19 @@ mod tests { fn read() { let libseg = LibsSeg::default(); let mut marshaller = Marshaller::with([0b01010111, 0b00001001], [], &libseg); - assert_eq!(marshaller.read_u2().unwrap().to_u8(), 0b00000011); - assert_eq!(marshaller.read_u2().unwrap().to_u8(), 0b00000001); - assert_eq!(marshaller.read_u8().unwrap(), 0b10010101); + assert_eq!(marshaller.read_2bits().unwrap().to_u8(), 0b00000011); + assert_eq!(marshaller.read_2bits().unwrap().to_u8(), 0b00000001); + assert_eq!(marshaller.read_byte().unwrap(), 0b10010101); let mut marshaller = Marshaller::with([0b01010111, 0b00001001], [], &libseg); - assert_eq!(marshaller.read_u2().unwrap().to_u8(), 0b00000011); - assert_eq!(marshaller.read_u3().unwrap().to_u8(), 0b00000101); - assert_eq!(marshaller.read_u8().unwrap(), 0b01001010); + assert_eq!(marshaller.read_2bits().unwrap().to_u8(), 0b00000011); + assert_eq!(marshaller.read_3bits().unwrap().to_u8(), 0b00000101); + assert_eq!(marshaller.read_byte().unwrap(), 0b01001010); let mut marshaller = Marshaller::with([0b01110111, 0b00001111], [], &libseg); - assert_eq!(marshaller.read_u8().unwrap(), 0b01110111); - assert_eq!(marshaller.read_u3().unwrap().to_u8(), 0b00000111); - assert_eq!(marshaller.read_u5().unwrap().to_u8(), 0b00000001); + assert_eq!(marshaller.read_byte().unwrap(), 0b01110111); + assert_eq!(marshaller.read_3bits().unwrap().to_u8(), 0b00000111); + assert_eq!(marshaller.read_5bits().unwrap().to_u8(), 0b00000001); let bytes = 0b11101011_11110000_01110111; let mut marshaller = Marshaller::with(u32::to_le_bytes(bytes), [], &libseg); @@ -459,9 +459,9 @@ mod tests { fn read_eof() { let libseg = LibsSeg::default(); let mut marshaller = Marshaller::with([0b01010111], [], &libseg); - assert_eq!(marshaller.read_u2().unwrap().to_u8(), 0b00000011); - assert_eq!(marshaller.read_u2().unwrap().to_u8(), 0b00000001); - assert!(marshaller.read_u8().is_err()); + assert_eq!(marshaller.read_2bits().unwrap().to_u8(), 0b00000011); + assert_eq!(marshaller.read_2bits().unwrap().to_u8(), 0b00000001); + assert!(marshaller.read_byte().is_err()); } #[test] @@ -469,26 +469,26 @@ mod tests { let libseg = LibsSeg::default(); let mut code = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let mut marshaller = Marshaller::with(&mut code, [], &libseg); - marshaller.write_u2(u2::with(0b00000011)).unwrap(); - marshaller.write_u3(u3::with(0b00000101)).unwrap(); - marshaller.write_u7(u7::with(0b01011111)).unwrap(); - marshaller.write_u8(0b11100111).unwrap(); + marshaller.write_2bits(u2::with(0b00000011)).unwrap(); + marshaller.write_3bits(u3::with(0b00000101)).unwrap(); + marshaller.write_7bits(u7::with(0b01011111)).unwrap(); + marshaller.write_byte(0b11100111).unwrap(); marshaller.write_bool(true).unwrap(); - marshaller.write_u3(u3::with(0b00000110)).unwrap(); + marshaller.write_3bits(u3::with(0b00000110)).unwrap(); let two_bytes = 0b11110000_10101010u16; - marshaller.write_u16(two_bytes).unwrap(); + marshaller.write_word(two_bytes).unwrap(); let number = 255u8; marshaller.write_fixed(255u8.to_le_bytes()).unwrap(); let data = marshaller.data; let mut marshaller = Marshaller::with(code, data, &libseg); - assert_eq!(marshaller.read_u2().unwrap().to_u8(), 0b00000011); - assert_eq!(marshaller.read_u3().unwrap().to_u8(), 0b00000101); - assert_eq!(marshaller.read_u7().unwrap().to_u8(), 0b01011111); - assert_eq!(marshaller.read_u8().unwrap(), 0b11100111); + assert_eq!(marshaller.read_2bits().unwrap().to_u8(), 0b00000011); + assert_eq!(marshaller.read_3bits().unwrap().to_u8(), 0b00000101); + assert_eq!(marshaller.read_7bits().unwrap().to_u8(), 0b01011111); + assert_eq!(marshaller.read_byte().unwrap(), 0b11100111); assert!(marshaller.read_bool().unwrap()); - assert_eq!(marshaller.read_u3().unwrap().to_u8(), 0b00000110); - assert_eq!(marshaller.read_u16().unwrap(), two_bytes); + assert_eq!(marshaller.read_3bits().unwrap().to_u8(), 0b00000110); + assert_eq!(marshaller.read_word().unwrap(), two_bytes); assert_eq!(marshaller.read_fixed(u8::from_le_bytes).unwrap(), number); } @@ -506,9 +506,9 @@ mod tests { let libseg = LibsSeg::default(); let mut code = [0, 0]; let mut marshaller = Marshaller::with(&mut code, [], &libseg); - marshaller.write_u2(u2::with(0b00000011)).unwrap(); - marshaller.write_u3(u3::with(0b00000101)).unwrap(); - marshaller.write_u7(u7::with(0b01011111)).unwrap(); - assert!(marshaller.write_u8(0b11100111).is_err()); + marshaller.write_2bits(u2::with(0b00000011)).unwrap(); + marshaller.write_3bits(u3::with(0b00000101)).unwrap(); + marshaller.write_7bits(u7::with(0b01011111)).unwrap(); + assert!(marshaller.write_byte(0b11100111).is_err()); } }