jt.py 861 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import sys
  2. #This reads the jumptable.z80 and generates an include file
  3. hexd="0123456789ABCDEF"
  4. def hexify(x,n):
  5. s=''
  6. while n>0:
  7. s=hexd[x&15]+s
  8. x>>=4
  9. n-=1
  10. return s
  11. def makejt(l):
  12. s=''
  13. base=0
  14. for i in l.split('\n'):
  15. if i[0:7]==";start=":
  16. base=int(i[7:],16)
  17. i=i.strip(" ").strip("\t")
  18. if i[0:3]=="jp ":
  19. if base==0:
  20. print("WARNING! Jumptable has ';start=0'")
  21. t=i[3:]
  22. s+=t+" "*(20-len(t))+"= $"+hexify(base,4)+"\n"
  23. base+=3
  24. return s
  25. if len(sys.argv)<3:
  26. print("Too few arguments!\n python "+sys.argv[0]+" <input file> <output file>")
  27. else:
  28. f=open(sys.argv[1],'r')
  29. j=f.read()
  30. f.close()
  31. f=open(sys.argv[2],'r')
  32. s = f.read()
  33. f.close()
  34. t = "#ifndef NO_JUMP_TABLE\n"
  35. s = s.split(t)[0]
  36. s += t
  37. s += makejt(j)
  38. s += "#endif"
  39. f=open(sys.argv[2],'w')
  40. f.write(s)
  41. f.close()