gameboy superoptimizer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.1 KiB

3 years ago
3 years ago
  1. from gbso.program.mutate import create_random_program
  2. from time import time
  3. from gbso.cpu.state import CPUState
  4. from gbso.program.test_case import TestCase
  5. from gbso.cpu.insn import *
  6. from gbso.cpu.regs import R8
  7. from gbso.optimize import OptimizationParameters, cost, optimize
  8. from gbso.program.program import Program
  9. prgm = Program(
  10. insns=[
  11. INC_R(R8.A),
  12. INC_R(R8.A),
  13. INC_R(R8.A),
  14. INC_R(R8.A),
  15. ],
  16. )
  17. outputs = [R8.A]
  18. test_cases = [TestCase(), TestCase(state=CPUState(carry=1))]
  19. params = OptimizationParameters(max_size=4)
  20. print("Program to optimize:")
  21. prgm.display()
  22. print(f"Cost: 0.0")
  23. print(f"Cycles: {prgm.perf()}")
  24. start_time = time()
  25. optimized_prgm = optimize(
  26. prgm,
  27. test_cases,
  28. outputs,
  29. params,
  30. )
  31. end_time = time()
  32. print("Optimized result:")
  33. optimized_prgm.display()
  34. optimized_cost = cost(prgm, test_cases, outputs, optimized_prgm)
  35. optimized_cycles = optimized_prgm.perf()
  36. print(f"Cost: {optimized_cost[0]}")
  37. print(f"Cycles: {optimized_cycles}")
  38. print(f"Took {round(end_time - start_time, 3)} seconds")