jt.py 904 B

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