Skip to content

Commit

Permalink
core: improve Debug implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Nov 14, 2024
1 parent 901e6fd commit a2e7382
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/core/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<fe128>; 16],
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit a2e7382

Please sign in to comment.