base/quarters.rs
1//! Code for identifying quarters and bits of TX-2 words using the
2//! notations from the TX-2 documentation.
3//!
4//! In this notation, "bit 2.3" is bit 3 (counting from 1 as the least
5//! significant) of quarter 2 (counting from 1 as the least
6//! significant). Bit 2.3 would have the value 02000 (octal).
7use std::fmt::{Display, Write};
8
9/// `Quarter` identifies a quarter within a TX-2 word. Q1 is the
10/// least-significant 9 bits and Q4 is the most-significant 9 bits.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub enum Quarter {
13 Q1 = 0,
14 Q2 = 1,
15 Q3 = 2,
16 Q4 = 3,
17}
18
19/// Render the quarter ("q") part of the bit selector ("q.b").
20impl Display for Quarter {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 f.write_char(match self {
23 Quarter::Q1 => '1',
24 Quarter::Q2 => '2',
25 Quarter::Q3 => '3',
26 Quarter::Q4 => '4',
27 })
28 }
29}
30
31/// Convert the `Quarter` enumeration value into the position of that
32/// quarter (counting from the least-significant end of the 36-bit
33/// word).
34impl From<Quarter> for u8 {
35 fn from(q: Quarter) -> u8 {
36 match q {
37 Quarter::Q1 => 0,
38 Quarter::Q2 => 1,
39 Quarter::Q3 => 2,
40 Quarter::Q4 => 3,
41 }
42 }
43}