generate.py 1.0 KB

123456789101112131415161718192021222324
  1. rom = [0xea] * 1024 # Initializing ROM with NOP instructions (0xEA)
  2. last_written_addr = 0x0000
  3. print(len(rom))
  4. def write(string):
  5. global last_written_addr # Define last_written_addr as a global variable
  6. string_array = string.encode("ascii")
  7. for i in range(len(string_array)):
  8. rom[last_written_addr] = 0xA9 # LDA opcode
  9. rom[last_written_addr + 1] = string_array[i] # ASCII byte
  10. rom[last_written_addr + 2] = 0x8D # STA opcode
  11. # Calculate memory address with little-endian ordering
  12. mem_address = (0x2000 + i) # Assuming 4 bytes per instruction and address
  13. rom[last_written_addr + 3] = mem_address & 0xFF # Low byte of memory address
  14. rom[last_written_addr + 4] = (mem_address >> 8) & 0xFF # High byte of memory address
  15. # Increment the last_written_addr by 5 for each instruction and memory address (5 bytes)
  16. last_written_addr += 5
  17. write("Hello world!")
  18. rom_file = open("generated.bin", "wb")
  19. rom_file.write(bytearray(rom))
  20. rom_file.close() # Don't forget to close the file