Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 1. Dependency Management
- local filename = "cpu6502.lua"
- local url = "https://files.catbox.moe/4vl2gj.lua"
- if not fs.exists(filename) then
- print("Downloading 6502 library from Catbox...")
- local response = http.get(url)
- if response then
- local file = fs.open(filename, "w")
- file.write(response.readAll())
- file.close()
- response.close()
- print("Download successful.")
- else
- error("Failed to download library. Is HTTP enabled in the config?")
- end
- end
- -- Force the script to look in the local directory
- package.path = package.path .. ";./?.lua"
- local cpu6502 = require("cpu6502")
- -- 2. Memory and Hardware Emulation
- local mem = {}
- for i = 0, 0xFFFF do mem[i] = 0 end
- local function read_byte(addr)
- if addr == 0xD010 then -- Keyboard Input
- local event, char = os.pullEvent("char")
- return string.byte(char:upper()) + 0x80
- elseif addr == 0xD011 then -- Status
- return 0x80
- end
- return mem[addr] or 0
- end
- local function write_byte(addr, val)
- if addr == 0xD012 then -- Display
- local char_code = bit32.band(val, 0x7F)
- if char_code == 0x0D then print() else write(string.char(char_code)) end
- else
- mem[addr] = bit32.band(val, 0xFF)
- end
- end
- -- 3. Load Woz Monitor Machine Code
- -- (Contains the FF00-FFFF block including vectors)
- local woz_bin = {
- [0xFF00]=0xD8,0x58,0xA0,0x7F,0x8D,0x12,0xD0,0xA9,0xA7,0x8D,0x11,0xD0,0x8D,0x13,0xD0,0xC9,
- [0xFF10]=0xDF,0xF0,0x12,0xC9,0x9B,0xF0,0x03,0xC8,0x10,0x1F,0xA9,0xDC,0x20,0xEF,0xFF,0xA9,
- [0xFF20]=0x8D,0x20,0xEF,0xFF,0xA0,0x01,0x88,0x30,0xF6,0xAD,0x11,0xD0,0x10,0xFB,0xAD,0x10,
- [0xFF30]=0xD0,0x99,0x00,0x02,0x20,0xEF,0xFF,0xC9,0x8D,0xD0,0xBB,0xA0,0xFF,0xA9,0x00,0xAA,
- [0xFF40]=0x0A,0x85,0x2B,0xC8,0xB9,0x00,0x02,0xC9,0x8D,0xF0,0x34,0xC9,0xAE,0x90,0xF4,0xF0,
- [0xFF50]=0xF0,0xC9,0xBA,0xF0,0xEB,0xC9,0xD2,0xF0,0x3F,0x86,0x28,0x86,0x29,0x84,0x2A,0xB9,
- [0xFF60]=0x00,0x02,0x49,0xB0,0xC9,0x0A,0x90,0x06,0x69,0x88,0xC9,0xFA,0x90,0x13,0x0A,0x0A,
- [0xFF70]=0x0A,0x0A,0xA2,0x04,0x0A,0x26,0x28,0x26,0x29,0xCA,0xD0,0xF7,0xC8,0xD0,0xE1,0xC4,
- [0xFF80]=0x2A,0xF0,0x9C,0x24,0x2B,0x50,0x10,0xA5,0x28,0x81,0x26,0xE6,0x26,0xD0,0x02,0xE6,
- [0xFF90]=0x27,0x4C,0x44,0xFF,0x6C,0x24,0x00,0x30,0x11,0xA2,0x02,0xB5,0x27,0x95,0x25,0x95,
- [0xFFA0]=0x23,0xCA,0xD0,0xF7,0xD0,0x17,0xA9,0x8D,0x20,0xEF,0xFF,0xA5,0x25,0x20,0xE5,0xFF,
- [0xFFB0]=0xA5,0x24,0x20,0xE5,0xFF,0xA9,0xBA,0x20,0xEF,0xFF,0xA9,0xA0,0x20,0xEF,0xFF,0xA1,
- [0xFFC0]=0x24,0x20,0xE5,0xFF,0x86,0x2B,0xA5,0x24,0xC5,0x28,0xA5,0x25,0xE5,0x29,0xB0,0xC7,
- [0xFFD0]=0xE6,0x24,0xD0,0x02,0xE6,0x25,0xA5,0x24,0x29,0x07,0x10,0xC8,0x48,0x4A,0x4A,0x4A,
- [0xFFE0]=0x4A,0x20,0xF9,0xFF,0x68,0x29,0x0F,0x09,0xB0,0xC9,0xBA,0x90,0x02,0x69,0x06,0x24,
- [0xFFF0]=0x12,0x30,0xFB,0x8D,0x12,0xD0,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0xFF,
- [0xFFFF]=0x00
- }
- for addr, val in pairs(woz_bin) do mem[addr] = val end
- -- 4. Start
- local cpu = cpu6502.new({read = read_byte, write = write_byte})
- term.clear()
- term.setCursorPos(1,1)
- print("Apple-1 WozMon [6502]")
- print("--------------------")
- cpu:reset()
- while true do
- cpu:step()
- end
Add Comment
Please, Sign In to add comment