From 6a6df6398003cb9f1bc3b5b4293342e63a3af4ca Mon Sep 17 00:00:00 2001 From: Daniel Dybing Date: Sat, 7 Feb 2026 10:12:25 +0100 Subject: [PATCH] feat: Add CRC checksum calculation and display --- src/teletext/models.py | 95 +++++++++++++++++++++++++++++++++++------- src/teletext/ui.py | 38 +++++++++++++++++ 2 files changed, 117 insertions(+), 16 deletions(-) diff --git a/src/teletext/models.py b/src/teletext/models.py index 80eab31..f5509b4 100644 --- a/src/teletext/models.py +++ b/src/teletext/models.py @@ -1,6 +1,13 @@ from dataclasses import dataclass, field from typing import List, Optional +def decode_hamming_8_4(byte_val): + # Extract data bits: bits 1, 3, 5, 7 + return ((byte_val >> 1) & 1) | \ + (((byte_val >> 3) & 1) << 1) | \ + (((byte_val >> 5) & 1) << 2) | \ + (((byte_val >> 7) & 1) << 3) + @dataclass class Packet: """ @@ -27,22 +34,6 @@ class Packet: b2 = self.original_data[1] # De-interleave Hamming bits to get M (3 bits) and R (5 bits) - # This is the "basic" interpretation. - # For a robust editor we assume the input T42 is valid or we just store bytes. - # But we need Mag/Row to organize pages. - - # Decode Hamming 8/4 logic is complex to implementation from scratch correctly - # without a reference, but usually D1, D2, D3, D4 are at bit positions 1, 3, 5, 7 - # (0-indexed, where 0 is LSB). - # Let's perform a simple extraction assuming no bit errors for now. - - def decode_hamming_8_4(byte_val): - # Extract data bits: bits 1, 3, 5, 7 - return ((byte_val >> 1) & 1) | \ - (((byte_val >> 3) & 1) << 1) | \ - (((byte_val >> 5) & 1) << 2) | \ - (((byte_val >> 7) & 1) << 3) - d1 = decode_hamming_8_4(b1) d2 = decode_hamming_8_4(b2) @@ -84,6 +75,78 @@ class Page: # Format as Hex to support A-F pages return f"{self.magazine}{self.page_number:02X}" + def calculate_crc(self) -> int: + """ + Calculates the CRC-16 (CCITT) checksum for the page. + Covers Rows 0 to 23. + Row 0: Skips first 8 bytes (Header/Control). Uses bytes 8-39. + Rows 1-23: Uses all 40 bytes. + Data is 7-bit (stripped parity). + """ + crc = 0xFFFF + poly = 0x1021 + + # Helper to update CRC with a byte + def update_crc(c, val): + v = (val << 8) & 0xFFFF + for _ in range(8): + if (c ^ v) & 0x8000: + c = (c << 1) ^ poly + else: + c = c << 1 + v <<= 1 + c &= 0xFFFF + return c + + # Organize packets by row + rows = {} + for p in self.packets: + rows[p.row] = p + + for r in range(24): # 0 to 23 + if r in rows: + data = rows[r].data + start_col = 8 if r == 0 else 0 + + for i in range(start_col, 40): + byte_val = data[i] & 0x7F # Strip parity + crc = update_crc(crc, byte_val) + else: + # Missing row? Usually treated as spaces (0x20) + start_col = 8 if r == 0 else 0 + for i in range(start_col, 40): + crc = update_crc(crc, 0x20) + + return crc + + def get_stored_crc(self) -> Optional[int]: + """ + Attempts to retrieve the stored CRC from Packet 27/0 if present. + Returns None if not found. + """ + # Look for Packet 27 + for p in self.packets: + if p.row == 27: + # Check Designation Code (Byte 0) + try: + b0 = p.data[0] + # Decode Hamming 8/4 + designation = decode_hamming_8_4(b0) + + if designation == 0: + # Packet 27/0 + # Checksum is in bytes 38 and 39 + if len(p.data) >= 40: + hi = p.data[38] + lo = p.data[39] + + # Strip parity + crc = ((hi & 0x7F) << 8) | (lo & 0x7F) + return crc + except: + pass + return None + @dataclass class TeletextService: """ diff --git a/src/teletext/ui.py b/src/teletext/ui.py index baf276a..f1294c8 100644 --- a/src/teletext/ui.py +++ b/src/teletext/ui.py @@ -319,6 +319,16 @@ class MainWindow(QMainWindow): lang_layout.addWidget(self.lang_combo) lang_layout.addWidget(btn_set_lang) right_layout.addLayout(lang_layout) + + right_layout.addSpacing(10) + + # CRC Checksum + crc_label = QLabel("CRC Checksum:") + right_layout.addWidget(crc_label) + + self.lbl_crc_info = QLabel("Page: ----\nCalc: ----") + self.lbl_crc_info.setStyleSheet("font-family: monospace; font-weight: bold;") + right_layout.addWidget(self.lbl_crc_info) right_layout.addStretch() @@ -346,6 +356,30 @@ class MainWindow(QMainWindow): # Menus self.create_menus() + def update_crc_display(self): + if not self.current_page: + self.lbl_crc_info.setText("Page: ----\nCalc: ----") + self.lbl_crc_info.setStyleSheet("font-family: monospace; font-weight: bold;") + return + + calc_crc = self.current_page.calculate_crc() + stored_crc = self.current_page.get_stored_crc() + + stored_str = f"{stored_crc:04X}" if stored_crc is not None else "----" + calc_str = f"{calc_crc:04X}" + + # Highlight if match + if stored_crc is not None: + if stored_crc == calc_crc: + style = "font-family: monospace; font-weight: bold; color: green;" + else: + style = "font-family: monospace; font-weight: bold; color: red;" + else: + style = "font-family: monospace; font-weight: bold;" + + self.lbl_crc_info.setStyleSheet(style) + self.lbl_crc_info.setText(f"Page: {stored_str}\nCalc: {calc_str}") + def update_language_label(self): idx = self.canvas.subset_idx if 0 <= idx < len(self.language_names): @@ -712,6 +746,7 @@ class MainWindow(QMainWindow): # Force redraw self.canvas.redraw() self.canvas.update() + self.update_crc_display() self.status_label.setText(f"Pasted {modified_count} rows.") self.push_undo_state() # Push state after paste? NO, before! # Wait, usually we push before modifying. @@ -815,6 +850,7 @@ class MainWindow(QMainWindow): self.canvas.set_page(self.current_page) self.canvas.redraw() self.canvas.update() + self.update_crc_display() def populate_list(self): self.page_list.clear() @@ -875,6 +911,7 @@ class MainWindow(QMainWindow): self.canvas.update() self.update_language_label() + self.update_crc_display() self.canvas.setFocus() def insert_char(self, char_code): @@ -899,6 +936,7 @@ class MainWindow(QMainWindow): self.hex_input.setText(f"{val:02X}") mode_str = "Graphics" if is_graphics else "Text" self.mode_label.setText(f"Mode: {mode_str}") + self.update_crc_display() def on_hex_entered(self): text = self.hex_input.text()