nonogamer9

ezsqdf

Jan 24th, 2026 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. -- 1. Dependency Management
  2. local filename = "cpu6502.lua"
  3. local url = "https://files.catbox.moe/4vl2gj.lua"
  4.  
  5. if not fs.exists(filename) then
  6. print("Downloading 6502 library from Catbox...")
  7. local response = http.get(url)
  8. if response then
  9. local file = fs.open(filename, "w")
  10. file.write(response.readAll())
  11. file.close()
  12. response.close()
  13. print("Download successful.")
  14. else
  15. error("Failed to download library. Is HTTP enabled in the config?")
  16. end
  17. end
  18.  
  19. -- Force the script to look in the local directory
  20. package.path = package.path .. ";./?.lua"
  21. local cpu6502 = require("cpu6502")
  22.  
  23. -- 2. Memory and Hardware Emulation
  24. local mem = {}
  25. for i = 0, 0xFFFF do mem[i] = 0 end
  26.  
  27. local function read_byte(addr)
  28. if addr == 0xD010 then -- Keyboard Input
  29. local event, char = os.pullEvent("char")
  30. return string.byte(char:upper()) + 0x80
  31. elseif addr == 0xD011 then -- Status
  32. return 0x80
  33. end
  34. return mem[addr] or 0
  35. end
  36.  
  37. local function write_byte(addr, val)
  38. if addr == 0xD012 then -- Display
  39. local char_code = bit32.band(val, 0x7F)
  40. if char_code == 0x0D then print() else write(string.char(char_code)) end
  41. else
  42. mem[addr] = bit32.band(val, 0xFF)
  43. end
  44. end
  45.  
  46. -- 3. Load Woz Monitor Machine Code
  47. -- (Contains the FF00-FFFF block including vectors)
  48. local woz_bin = {
  49. [0xFF00]=0xD8,0x58,0xA0,0x7F,0x8D,0x12,0xD0,0xA9,0xA7,0x8D,0x11,0xD0,0x8D,0x13,0xD0,0xC9,
  50. [0xFF10]=0xDF,0xF0,0x12,0xC9,0x9B,0xF0,0x03,0xC8,0x10,0x1F,0xA9,0xDC,0x20,0xEF,0xFF,0xA9,
  51. [0xFF20]=0x8D,0x20,0xEF,0xFF,0xA0,0x01,0x88,0x30,0xF6,0xAD,0x11,0xD0,0x10,0xFB,0xAD,0x10,
  52. [0xFF30]=0xD0,0x99,0x00,0x02,0x20,0xEF,0xFF,0xC9,0x8D,0xD0,0xBB,0xA0,0xFF,0xA9,0x00,0xAA,
  53. [0xFF40]=0x0A,0x85,0x2B,0xC8,0xB9,0x00,0x02,0xC9,0x8D,0xF0,0x34,0xC9,0xAE,0x90,0xF4,0xF0,
  54. [0xFF50]=0xF0,0xC9,0xBA,0xF0,0xEB,0xC9,0xD2,0xF0,0x3F,0x86,0x28,0x86,0x29,0x84,0x2A,0xB9,
  55. [0xFF60]=0x00,0x02,0x49,0xB0,0xC9,0x0A,0x90,0x06,0x69,0x88,0xC9,0xFA,0x90,0x13,0x0A,0x0A,
  56. [0xFF70]=0x0A,0x0A,0xA2,0x04,0x0A,0x26,0x28,0x26,0x29,0xCA,0xD0,0xF7,0xC8,0xD0,0xE1,0xC4,
  57. [0xFF80]=0x2A,0xF0,0x9C,0x24,0x2B,0x50,0x10,0xA5,0x28,0x81,0x26,0xE6,0x26,0xD0,0x02,0xE6,
  58. [0xFF90]=0x27,0x4C,0x44,0xFF,0x6C,0x24,0x00,0x30,0x11,0xA2,0x02,0xB5,0x27,0x95,0x25,0x95,
  59. [0xFFA0]=0x23,0xCA,0xD0,0xF7,0xD0,0x17,0xA9,0x8D,0x20,0xEF,0xFF,0xA5,0x25,0x20,0xE5,0xFF,
  60. [0xFFB0]=0xA5,0x24,0x20,0xE5,0xFF,0xA9,0xBA,0x20,0xEF,0xFF,0xA9,0xA0,0x20,0xEF,0xFF,0xA1,
  61. [0xFFC0]=0x24,0x20,0xE5,0xFF,0x86,0x2B,0xA5,0x24,0xC5,0x28,0xA5,0x25,0xE5,0x29,0xB0,0xC7,
  62. [0xFFD0]=0xE6,0x24,0xD0,0x02,0xE6,0x25,0xA5,0x24,0x29,0x07,0x10,0xC8,0x48,0x4A,0x4A,0x4A,
  63. [0xFFE0]=0x4A,0x20,0xF9,0xFF,0x68,0x29,0x0F,0x09,0xB0,0xC9,0xBA,0x90,0x02,0x69,0x06,0x24,
  64. [0xFFF0]=0x12,0x30,0xFB,0x8D,0x12,0xD0,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0xFF,
  65. [0xFFFF]=0x00
  66. }
  67.  
  68. for addr, val in pairs(woz_bin) do mem[addr] = val end
  69.  
  70. -- 4. Start
  71. local cpu = cpu6502.new({read = read_byte, write = write_byte})
  72. term.clear()
  73. term.setCursorPos(1,1)
  74. print("Apple-1 WozMon [6502]")
  75. print("--------------------")
  76. cpu:reset()
  77.  
  78. while true do
  79. cpu:step()
  80. end
Add Comment
Please, Sign In to add comment