Advertisement
nonogamer9

teest

Feb 3rd, 2025 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local keyboard = require("keyboard")
  4. local computer = require("computer")
  5. local bit32 = require("bit32")
  6.  
  7. -- Constants
  8. local MEMORY_SIZE = 16 * 1024 * 1024 -- 16 MiB
  9. local BANK_SIZE = 1 * 1024 * 1024 -- 1 MiB per bank
  10. local NUM_BANKS = 16
  11. local SCREEN_WIDTH = 256
  12. local SCREEN_HEIGHT = 256
  13. local CYCLES_PER_FRAME = 65536
  14.  
  15. -- Initialize memory banks
  16. local memoryBanks = {}
  17. for i = 1, NUM_BANKS do
  18. memoryBanks[i] = {}
  19. for j = 1, BANK_SIZE do
  20. memoryBanks[i][j] = 0
  21. end
  22. end
  23.  
  24. local currentBank = 1
  25.  
  26. -- Memory access with bank switching
  27. local function memoryRead(address)
  28. local bank = bit32.rshift(address - 1, 20) + 1
  29. local offset = bit32.band(address - 1, BANK_SIZE - 1) + 1
  30. return memoryBanks[bank][offset]
  31. end
  32.  
  33. local function memoryWrite(address, value)
  34. local bank = bit32.rshift(address - 1, 20) + 1
  35. local offset = bit32.band(address - 1, BANK_SIZE - 1) + 1
  36. memoryBanks[bank][offset] = value
  37. end
  38.  
  39. -- CPU implementation
  40. local function cpu_cycle()
  41. local pc = bit32.bor(bit32.lshift(memoryRead(3), 16), bit32.lshift(memoryRead(4), 8), memoryRead(5))
  42. local a = bit32.bor(bit32.lshift(memoryRead(pc + 1), 16), bit32.lshift(memoryRead(pc + 2), 8), memoryRead(pc + 3))
  43. local b = bit32.bor(bit32.lshift(memoryRead(pc + 4), 16), bit32.lshift(memoryRead(pc + 5), 8), memoryRead(pc + 6))
  44. memoryWrite(a, memoryRead(b))
  45. return a + 3
  46. end
  47.  
  48. -- Color palette initialization
  49. local colorPalette = {}
  50. for i = 0, 215 do
  51. local r = bit32.rshift(i, 4) * 51
  52. local g = bit32.band(bit32.rshift(i, 2), 3) * 51
  53. local b = bit32.band(i, 3) * 51
  54. colorPalette[i] = {r / 255, g / 255, b / 255}
  55. end
  56.  
  57. -- Main emulation loop
  58. local function emulate()
  59. -- Set up screen
  60. gpu.setResolution(SCREEN_WIDTH, SCREEN_HEIGHT)
  61.  
  62. local lastTime = computer.uptime()
  63. while true do
  64. -- Handle input
  65. local keyState = 0
  66. for i = 0, 15 do
  67. if keyboard.isKeyDown(tostring(i)) then
  68. keyState = bit32.bor(keyState, bit32.lshift(1, i))
  69. end
  70. end
  71. memoryWrite(1, bit32.rshift(keyState, 8))
  72. memoryWrite(2, bit32.band(keyState, 0xFF))
  73.  
  74. -- Run CPU cycles
  75. for _ = 1, CYCLES_PER_FRAME do
  76. local newPC = cpu_cycle()
  77. memoryWrite(3, bit32.rshift(newPC, 16))
  78. memoryWrite(4, bit32.band(bit32.rshift(newPC, 8), 0xFF))
  79. memoryWrite(5, bit32.band(newPC, 0xFF))
  80. end
  81.  
  82. -- Update screen
  83. local screenPtr = bit32.lshift(memoryRead(6), 16)
  84. for y = 1, SCREEN_HEIGHT do
  85. for x = 1, SCREEN_WIDTH do
  86. local color = memoryRead(screenPtr + (y-1)*SCREEN_WIDTH + x)
  87. local r, g, b = table.unpack(colorPalette[color])
  88. gpu.setForeground(gpu.getPaletteColor(gpu.setPaletteColor(1, r, g, b)))
  89. gpu.set(x, y, "▀")
  90. end
  91. end
  92.  
  93. -- Frame timing
  94. local currentTime = computer.uptime()
  95. local sleepTime = (1/60) - (currentTime - lastTime)
  96. if sleepTime > 0 then
  97. os.sleep(sleepTime)
  98. end
  99. lastTime = currentTime
  100.  
  101. -- Check for exit condition
  102. if keyboard.isKeyDown("q") then
  103. break
  104. end
  105. end
  106. end
  107.  
  108. -- Load ROM from /home/ directory
  109. local function loadROMFromHome(filename)
  110. local file = io.open("/home/" .. filename, "rb")
  111. if file then
  112. local content = file:read("*all")
  113. file:close()
  114. for i = 1, #content do
  115. memoryWrite(i, string.byte(content, i))
  116. end
  117. return true
  118. end
  119. return false
  120. end
  121.  
  122. -- Main
  123. print("BytePusher Emulator for OpenComputers")
  124. print("Enter the name of your ROM file (located in /home/):")
  125. local romName = io.read()
  126.  
  127. if loadROMFromHome(romName) then
  128. print("ROM loaded successfully from /home/")
  129. print("Emulation starting. Press 'Q' to quit.")
  130. emulate()
  131. print("Emulation ended.")
  132. else
  133. print("Failed to load ROM from /home/. Make sure the file exists and is readable.")
  134. end
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement