risc8asm.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import argparse
  2. import sys
  3. import math
  4. from os import path, mkdir
  5. import asm_compiler as compiler
  6. asmc = compiler.Compiler(byte_order='big')
  7. asmc.add_reg('r0', 0)
  8. asmc.add_reg('r1', 1)
  9. asmc.add_reg('r2', 2)
  10. asmc.add_reg('r3', 3)
  11. asmc.add_instr(compiler.Instruction('CPY0 ', '0000_0000', 1, alias=['COPY0']))
  12. asmc.add_instr(compiler.Instruction('CPY1 ', '0000_0101', 1, alias=['COPY1']))
  13. asmc.add_instr(compiler.Instruction('CPY2 ', '0000_1010', 1, alias=['COPY2']))
  14. asmc.add_instr(compiler.Instruction('CPY3 ', '0000_1111', 1, alias=['COPY3']))
  15. asmc.add_instr(compiler.Instruction('MOVE ', '0000_????', alias=['MOV']))
  16. asmc.add_instr(compiler.Instruction('ADD ', '0001_????'))
  17. asmc.add_instr(compiler.Instruction('SUB ', '0010_????'))
  18. asmc.add_instr(compiler.Instruction('AND ', '0011_????'))
  19. asmc.add_instr(compiler.Instruction('OR ', '0100_????'))
  20. asmc.add_instr(compiler.Instruction('XOR ', '0101_????'))
  21. asmc.add_instr(compiler.Instruction('MUL ', '0110_????'))
  22. asmc.add_instr(compiler.Instruction('DIV ', '0111_????'))
  23. asmc.add_instr(compiler.Instruction('CI0 ', '1000_??00'))
  24. asmc.add_instr(compiler.Instruction('CI1 ', '1000_??01'))
  25. asmc.add_instr(compiler.Instruction('CI2 ', '1000_??10'))
  26. asmc.add_instr(compiler.Instruction('ADDC ', '1000_??11'))
  27. asmc.add_instr(compiler.Instruction('ADDI', '1110_??00', 1))
  28. asmc.add_instr(compiler.Instruction('SUBI ', '1110_??01', 1))
  29. asmc.add_instr(compiler.Instruction('ANDI ', '1110_??10', 1))
  30. asmc.add_instr(compiler.Instruction('ORI ', '1110_??11', 1))
  31. asmc.add_instr(compiler.Instruction('XORI ', '1100_??11', 1))
  32. asmc.add_instr(compiler.Instruction('SLL ', '1001_??00', 1))
  33. asmc.add_instr(compiler.Instruction('SRL ', '1001_??01', 1))
  34. asmc.add_instr(compiler.Instruction('SRA ', '1001_??10', 1))
  35. asmc.add_instr(compiler.Instruction('SUBC ', '1001_??11'))
  36. asmc.add_instr(compiler.Instruction('LWHI ', '1010_??00', 3))
  37. asmc.add_instr(compiler.Instruction('SWHI ', '1010_??01'))
  38. asmc.add_instr(compiler.Instruction('LWLO ', '1010_??10', 3))
  39. asmc.add_instr(compiler.Instruction('SWLO ', '1010_??11', 3))
  40. asmc.add_instr(compiler.Instruction('INC ', '1011_??00'))
  41. asmc.add_instr(compiler.Instruction('DEC ', '1011_??01'))
  42. asmc.add_instr(compiler.Instruction('GETAH', '1011_??10', alias=['AH']))
  43. asmc.add_instr(compiler.Instruction('GETIF', '1011_??11'))
  44. asmc.add_instr(compiler.Instruction('PUSH ', '1100_??00'))
  45. asmc.add_instr(compiler.Instruction('POP ', '1100_??01'))
  46. asmc.add_instr(compiler.Instruction('COM ', '1100_??10', 1))
  47. asmc.add_instr(compiler.Instruction('SETI ', '1100_??11'))
  48. asmc.add_instr(compiler.Instruction('BEQ ', '1101_??00', 3))
  49. asmc.add_instr(compiler.Instruction('BGT ', '1101_??01', 3))
  50. asmc.add_instr(compiler.Instruction('BGE ', '1101_??10', 3))
  51. asmc.add_instr(compiler.Instruction('BZ ', '1101_??11', 2))
  52. asmc.add_instr(compiler.Instruction('CALL ', '1111_0000', 2))
  53. asmc.add_instr(compiler.Instruction('RET ', '1111_0001'))
  54. asmc.add_instr(compiler.Instruction('JUMP ', '1111_0010', 2))
  55. asmc.add_instr(compiler.Instruction('INTRE', '1111_1110', 2))
  56. asmc.add_instr(compiler.Instruction('RETI ', '1111_0011'))
  57. asmc.add_instr(compiler.Instruction('CLC ', '1111_0100'))
  58. asmc.add_instr(compiler.Instruction('SETC ', '1111_0101'))
  59. asmc.add_instr(compiler.Instruction('CLS ', '1111_0110'))
  60. asmc.add_instr(compiler.Instruction('SETS ', '1111_0111'))
  61. asmc.add_instr(compiler.Instruction('SSETS', '1111_1000'))
  62. asmc.add_instr(compiler.Instruction('CLN ', '1111_1001'))
  63. asmc.add_instr(compiler.Instruction('SETN ', '1111_1010'))
  64. asmc.add_instr(compiler.Instruction('SSETN', '1111_1011'))
  65. asmc.add_instr(compiler.Instruction('RJUMP', '1111_1100', 2))
  66. asmc.add_instr(compiler.Instruction('RBWI ', '1111_1101'))
  67. if __name__ == '__main__':
  68. compiler.main(asmc)
  69. # parser = argparse.ArgumentParser(description='Assembly compiler', add_help=True)
  70. # parser.add_argument('file', help='Files to compile')
  71. # parser.add_argument('-t', '--output_type', choices=['bin', 'mem', 'binary', 'mif', 'uhex'], default='mem',
  72. # help='Output type')
  73. # parser.add_argument('-S', '--slice', default=-1, type=int, help='Slice output for section')
  74. # parser.add_argument('-o', '--output', help='Output directory')
  75. # parser.add_argument('-f', '--force', action='store_true', help='Force override output file')
  76. # parser.add_argument('-s', '--stdout', action='store_true', help='Print to stdout')
  77. # parser.add_argument('-D', '--decompile', action='store_true', help='Print decompiled')
  78. # parser.add_argument('section', help='Section')
  79. # args = parser.parse_args(sys.argv[1:])
  80. # if not path.isfile(args.file):
  81. # print(f'No file {args.file}!')
  82. # sys.exit(1)
  83. #
  84. # output_dir = args.output or path.dirname(args.file)
  85. # if not path.exists(output_dir):
  86. # mkdir(output_dir)
  87. #
  88. # if args.output_type == 'mem':
  89. # ext = '.mem'
  90. # elif args.output_type == 'bin':
  91. # ext = '.bin'
  92. # elif args.output_type == 'mif':
  93. # ext = '.mif'
  94. # elif args.output_type == 'uhex':
  95. # ext = '.uhex'
  96. # else:
  97. # ext = '.out'
  98. # bname = path.basename(args.file).rsplit('.', 1)[0]
  99. #
  100. # sformat = f'01d'
  101. # outputs = []
  102. # if args.slice > 0:
  103. # sformat = f'0{int(math.log10(args.slice)) + 1}d'
  104. # for i in range(0, args.slice):
  105. # outputs.append(path.join(output_dir,f'{bname}{args.section}_{format(i, sformat)}{ext}'))
  106. # else:
  107. # outputs = [path.join(output_dir, bname + args.section + ext)]
  108. # if not args.stdout and not args.force:
  109. # for output in outputs:
  110. # if path.isfile(output):
  111. # print(f'Output file already exists {output}!')
  112. # sys.exit(1)
  113. #
  114. # data = asmc.compile_file(args.file)
  115. # if data is not None:
  116. # section = args.section
  117. # if section in data:
  118. # width, length, size, bdata = data[section]
  119. # asize = len(bdata)
  120. # if size > 0:
  121. # bdataf = bdata + (size - len(bdata)) * bytearray(b'\x00')
  122. # else:
  123. # bdataf = bdata
  124. #
  125. # for i, output in enumerate(outputs):
  126. #
  127. # y = bdataf[i::len(outputs)]
  128. # if args.output_type == 'binary':
  129. # x = compiler.convert_to_binary(y)
  130. # elif args.output_type == 'mem':
  131. # x = compiler.convert_to_mem(y, width=width)
  132. # elif args.output_type == 'mif':
  133. # x = compiler.convert_to_mif(y, width=width, depth=len(y)/width)
  134. # elif args.output_type == 'uhex':
  135. # x = compiler.convert_to_mem(y, width=width, uhex=True)
  136. # else:
  137. # x = bytes(y)
  138. #
  139. # op = 'Printing' if args.stdout else 'Saving'
  140. # print(f"{op} {args.output_type} {section} data '{output}' [Size: {len(y)}B Slice: {format(i + 1, sformat)}/{len(outputs)}]")
  141. # if args.stdout:
  142. # if args.decompile:
  143. # print(asmc.decompile(bdata))
  144. # else:
  145. # print(x.decode())
  146. # else:
  147. # with open(output, 'wb') as of:
  148. # of.write(x)
  149. #
  150. # print(f"Total {section} size: {len(bdata)/len(bdataf)*100:.1f}% [{len(bdata)}B/{len(bdataf)}B]")
  151. # else:
  152. # print(f'No such section {section}!')
  153. # else:
  154. # print(f'Failed to compile {args.file}!')
  155. # sys.exit(1)
  156. # sys.exit(0)