96 lines
2.6 KiB
Python
96 lines
2.6 KiB
Python
from tea3.pretty_print import pretty_print
|
||
from tea3.tea3model import Tea3Model
|
||
from tea3.variable_search import run_exhaustive
|
||
|
||
|
||
def prompt_int(message: str, lo: int, hi: int) -> int:
|
||
while True:
|
||
raw = input(message).strip()
|
||
try:
|
||
value = int(raw)
|
||
except ValueError:
|
||
print("Please enter a number.")
|
||
continue
|
||
if lo <= value <= hi:
|
||
return value
|
||
print(f"Value must be between {lo} and {hi} (inclusive).")
|
||
|
||
|
||
def prompt_choice(message: str, choices: set[int]) -> int:
|
||
choices_str = ", ".join(map(str, sorted(choices)))
|
||
while True:
|
||
raw = input(message).strip()
|
||
try:
|
||
value = int(raw)
|
||
except ValueError:
|
||
print("Please enter a number.")
|
||
continue
|
||
if value in choices:
|
||
return value
|
||
print(f"Value must be one of: {choices_str}")
|
||
|
||
|
||
def run_classic_cli():
|
||
print("\nR registers are indexed 0–7; bits within each register are 0–7.")
|
||
print("Enter -1 to print all registers, or all bits.")
|
||
|
||
steps = prompt_int("How many steps do you want to run? (1–100): ", 1, 100)
|
||
reg = prompt_int("Which R register do you want to inspect? (-1 or 0–7): ", -1, 7)
|
||
bit = prompt_int("Which bit of that register? (-1 or 0–7): ", -1, 7)
|
||
|
||
print("-" * 50)
|
||
|
||
model = Tea3Model()
|
||
|
||
for i in range(steps):
|
||
model.step()
|
||
print(f"\n[Step {i + 1}]")
|
||
|
||
regs = range(8) if reg == -1 else [reg]
|
||
bits = range(8) if bit == -1 else [bit]
|
||
|
||
for r in regs:
|
||
for b in bits:
|
||
poly = model.R_bits[r][b]
|
||
print(f"R_bits[{r}][{b}] =")
|
||
print(pretty_print(poly))
|
||
print()
|
||
|
||
print("\n" + "=" * 50)
|
||
print("Done.")
|
||
|
||
def run_exhaustive_cli():
|
||
print("\nR registers are indexed 0–7; bits within each register are 0–7.")
|
||
print("Enter -1 to print all bits in the chosen register.")
|
||
|
||
steps = prompt_int("How many steps? (1–100): ", 1, 100)
|
||
target_reg = prompt_int("Target register (0–7): ", 0, 7)
|
||
target_bit = prompt_int("Target bit (-1 or 0–7): ", -1, 7)
|
||
|
||
print("-" * 50)
|
||
run_exhaustive(steps, target_reg, target_bit)
|
||
|
||
print("\n" + "=" * 50)
|
||
print("Done.")
|
||
|
||
|
||
|
||
def main():
|
||
print("=" * 50)
|
||
print(" Tea3 Model ")
|
||
print("=" * 50)
|
||
|
||
print("\nChoose a mode:")
|
||
print(" 1) Classic inspection")
|
||
print(" 2) Exhaustive variable-change search")
|
||
|
||
mode = prompt_choice("Your choice (1 or 2): ", {1, 2})
|
||
|
||
if mode == 1:
|
||
run_classic_cli()
|
||
else:
|
||
run_exhaustive_cli()
|
||
|
||
|
||
main()
|