feat: Add CRC checksum calculation and display
All checks were successful
Build Linux / Build Linux (push) Successful in 1m34s
Build Windows / Build Windows (push) Successful in 4m42s

This commit is contained in:
2026-02-07 10:12:25 +01:00
parent 06107a3d78
commit 6a6df63980
2 changed files with 117 additions and 16 deletions

View File

@@ -1,6 +1,13 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import List, Optional 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 @dataclass
class Packet: class Packet:
""" """
@@ -27,22 +34,6 @@ class Packet:
b2 = self.original_data[1] b2 = self.original_data[1]
# De-interleave Hamming bits to get M (3 bits) and R (5 bits) # 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) d1 = decode_hamming_8_4(b1)
d2 = decode_hamming_8_4(b2) d2 = decode_hamming_8_4(b2)
@@ -84,6 +75,78 @@ class Page:
# Format as Hex to support A-F pages # Format as Hex to support A-F pages
return f"{self.magazine}{self.page_number:02X}" 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 @dataclass
class TeletextService: class TeletextService:
""" """

View File

@@ -320,6 +320,16 @@ class MainWindow(QMainWindow):
lang_layout.addWidget(btn_set_lang) lang_layout.addWidget(btn_set_lang)
right_layout.addLayout(lang_layout) 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() right_layout.addStretch()
self.layout.addLayout(center_layout, 1) self.layout.addLayout(center_layout, 1)
@@ -346,6 +356,30 @@ class MainWindow(QMainWindow):
# Menus # Menus
self.create_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): def update_language_label(self):
idx = self.canvas.subset_idx idx = self.canvas.subset_idx
if 0 <= idx < len(self.language_names): if 0 <= idx < len(self.language_names):
@@ -712,6 +746,7 @@ class MainWindow(QMainWindow):
# Force redraw # Force redraw
self.canvas.redraw() self.canvas.redraw()
self.canvas.update() self.canvas.update()
self.update_crc_display()
self.status_label.setText(f"Pasted {modified_count} rows.") self.status_label.setText(f"Pasted {modified_count} rows.")
self.push_undo_state() # Push state after paste? NO, before! self.push_undo_state() # Push state after paste? NO, before!
# Wait, usually we push before modifying. # Wait, usually we push before modifying.
@@ -815,6 +850,7 @@ class MainWindow(QMainWindow):
self.canvas.set_page(self.current_page) self.canvas.set_page(self.current_page)
self.canvas.redraw() self.canvas.redraw()
self.canvas.update() self.canvas.update()
self.update_crc_display()
def populate_list(self): def populate_list(self):
self.page_list.clear() self.page_list.clear()
@@ -875,6 +911,7 @@ class MainWindow(QMainWindow):
self.canvas.update() self.canvas.update()
self.update_language_label() self.update_language_label()
self.update_crc_display()
self.canvas.setFocus() self.canvas.setFocus()
def insert_char(self, char_code): def insert_char(self, char_code):
@@ -899,6 +936,7 @@ class MainWindow(QMainWindow):
self.hex_input.setText(f"{val:02X}") self.hex_input.setText(f"{val:02X}")
mode_str = "Graphics" if is_graphics else "Text" mode_str = "Graphics" if is_graphics else "Text"
self.mode_label.setText(f"Mode: {mode_str}") self.mode_label.setText(f"Mode: {mode_str}")
self.update_crc_display()
def on_hex_entered(self): def on_hex_entered(self):
text = self.hex_input.text() text = self.hex_input.text()