Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local gpu = component.gpu
- local keyboard = require("keyboard")
- local computer = require("computer")
- local bit32 = require("bit32")
- -- Constants
- local MEMORY_SIZE = 16 * 1024 * 1024 -- 16 MiB
- local BANK_SIZE = 1 * 1024 * 1024 -- 1 MiB per bank
- local NUM_BANKS = 16
- local SCREEN_WIDTH = 256
- local SCREEN_HEIGHT = 256
- local CYCLES_PER_FRAME = 65536
- -- Initialize memory banks
- local memoryBanks = {}
- for i = 1, NUM_BANKS do
- memoryBanks[i] = {}
- for j = 1, BANK_SIZE do
- memoryBanks[i][j] = 0
- end
- end
- local currentBank = 1
- -- Memory access with bank switching
- local function memoryRead(address)
- local bank = bit32.rshift(address - 1, 20) + 1
- local offset = bit32.band(address - 1, BANK_SIZE - 1) + 1
- return memoryBanks[bank][offset]
- end
- local function memoryWrite(address, value)
- local bank = bit32.rshift(address - 1, 20) + 1
- local offset = bit32.band(address - 1, BANK_SIZE - 1) + 1
- memoryBanks[bank][offset] = value
- end
- -- CPU implementation
- local function cpu_cycle()
- local pc = bit32.bor(bit32.lshift(memoryRead(3), 16), bit32.lshift(memoryRead(4), 8), memoryRead(5))
- local a = bit32.bor(bit32.lshift(memoryRead(pc + 1), 16), bit32.lshift(memoryRead(pc + 2), 8), memoryRead(pc + 3))
- local b = bit32.bor(bit32.lshift(memoryRead(pc + 4), 16), bit32.lshift(memoryRead(pc + 5), 8), memoryRead(pc + 6))
- memoryWrite(a, memoryRead(b))
- return a + 3
- end
- -- Color palette initialization
- local colorPalette = {}
- for i = 0, 215 do
- local r = bit32.rshift(i, 4) * 51
- local g = bit32.band(bit32.rshift(i, 2), 3) * 51
- local b = bit32.band(i, 3) * 51
- colorPalette[i] = {r / 255, g / 255, b / 255}
- end
- -- Main emulation loop
- local function emulate()
- -- Set up screen
- gpu.setResolution(SCREEN_WIDTH, SCREEN_HEIGHT)
- local lastTime = computer.uptime()
- while true do
- -- Handle input
- local keyState = 0
- for i = 0, 15 do
- if keyboard.isKeyDown(tostring(i)) then
- keyState = bit32.bor(keyState, bit32.lshift(1, i))
- end
- end
- memoryWrite(1, bit32.rshift(keyState, 8))
- memoryWrite(2, bit32.band(keyState, 0xFF))
- -- Run CPU cycles
- for _ = 1, CYCLES_PER_FRAME do
- local newPC = cpu_cycle()
- memoryWrite(3, bit32.rshift(newPC, 16))
- memoryWrite(4, bit32.band(bit32.rshift(newPC, 8), 0xFF))
- memoryWrite(5, bit32.band(newPC, 0xFF))
- end
- -- Update screen
- local screenPtr = bit32.lshift(memoryRead(6), 16)
- for y = 1, SCREEN_HEIGHT do
- for x = 1, SCREEN_WIDTH do
- local color = memoryRead(screenPtr + (y-1)*SCREEN_WIDTH + x)
- local r, g, b = table.unpack(colorPalette[color])
- gpu.setForeground(gpu.getPaletteColor(gpu.setPaletteColor(1, r, g, b)))
- gpu.set(x, y, "▀")
- end
- end
- -- Frame timing
- local currentTime = computer.uptime()
- local sleepTime = (1/60) - (currentTime - lastTime)
- if sleepTime > 0 then
- os.sleep(sleepTime)
- end
- lastTime = currentTime
- -- Check for exit condition
- if keyboard.isKeyDown("q") then
- break
- end
- end
- end
- -- Load ROM from /home/ directory
- local function loadROMFromHome(filename)
- local file = io.open("/home/" .. filename, "rb")
- if file then
- local content = file:read("*all")
- file:close()
- for i = 1, #content do
- memoryWrite(i, string.byte(content, i))
- end
- return true
- end
- return false
- end
- -- Main
- print("BytePusher Emulator for OpenComputers")
- print("Enter the name of your ROM file (located in /home/):")
- local romName = io.read()
- if loadROMFromHome(romName) then
- print("ROM loaded successfully from /home/")
- print("Emulation starting. Press 'Q' to quit.")
- emulate()
- print("Emulation ended.")
- else
- print("Failed to load ROM from /home/. Make sure the file exists and is readable.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement