From a2e7382d8c8dbd55bba1ee7f428ecabd06f5d7d5 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 14 Nov 2024 18:45:47 +0100 Subject: [PATCH] core: improve Debug implementation --- src/core/core.rs | 24 +++++++++++++++++++++++- src/lib.rs | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/core/core.rs b/src/core/core.rs index dec0957..7c34c42 100644 --- a/src/core/core.rs +++ b/src/core/core.rs @@ -20,12 +20,14 @@ // or implied. See the License for the specific language governing permissions and limitations under // the License. +use core::fmt::{self, Debug, Formatter}; + use aluvm::{CoreExt, Register}; use amplify::num::u4; use crate::fe128; -#[derive(Clone, Eq, PartialEq, Debug)] +#[derive(Copy, Clone, Eq, PartialEq)] pub struct GfaCore { pub(super) fq: u128, pub(super) e: [Option; 16], @@ -65,6 +67,26 @@ impl CoreExt for GfaCore { fn reset(&mut self) { self.e = [None; 16]; } } +impl Debug for GfaCore { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let (sect, reg, val, reset) = + if f.alternate() { ("\x1B[0;4;1m", "\x1B[0;1m", "\x1B[0;32m", "\x1B[0m") } else { ("", "", "", "") }; + + writeln!(f)?; + writeln!(f, "{reg}FQ{reset} {val}{:X}{reset}#h", self.fq)?; + writeln!(f, "{sect}E-regs:{reset}")?; + for (no, item) in self.e.iter().enumerate() { + write!(f, "{reg}{}{reset} ", RegE::from(u4::with(no as u8)))?; + if let Some(e) = item { + writeln!(f, "{val}{e}{reset}#h")?; + } else { + writeln!(f, "~")?; + } + } + writeln!(f) + } +} + #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)] #[display(uppercase)] #[repr(u8)] diff --git a/src/lib.rs b/src/lib.rs index a6b5f33..cbbbbc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,5 +45,5 @@ pub use self::core::{GfaCore, RegE}; #[allow(non_camel_case_types)] #[derive(Copy, Clone, Eq, PartialEq, Debug, Display)] -#[display(inner)] +#[display("{0:X}")] pub struct fe128(pub u128);