Advertisement
DOGGYWOOF

DOGGY CORP single boot BIOS for Opencomputers

Apr 27th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. local init
  2. do
  3. local component_invoke = component.invoke
  4. local function boot_invoke(address, method, ...)
  5. local result = table.pack(pcall(component_invoke, address, method, ...))
  6. if not result[1] then
  7. return nil, result[2]
  8. else
  9. return table.unpack(result, 2, result.n)
  10. end
  11. end
  12.  
  13. local function drawCenteredText(text)
  14. local gpu = component.list("gpu")()
  15. local w, h = component.invoke(gpu, "getResolution")
  16. local x = math.floor((w - #text) / 2)
  17. local y = math.floor(h / 2)
  18. component.invoke(gpu, "set", x, y, text)
  19. end
  20.  
  21. local function setResolution(width, height)
  22. local gpu = component.list("gpu")()
  23. component.invoke(gpu, "setResolution", width, height)
  24. end
  25.  
  26. local function delay(seconds)
  27. local deadline = computer.uptime() + seconds
  28. repeat until computer.uptime() >= deadline
  29. end
  30.  
  31. -- backwards compatibility, may remove later
  32. local eeprom = component.list("eeprom")()
  33. computer.getBootAddress = function()
  34. return boot_invoke(eeprom, "getData")
  35. end
  36. computer.setBootAddress = function(address)
  37. return boot_invoke(eeprom, "setData", address)
  38. end
  39.  
  40. do
  41. local screen = component.list("screen")()
  42. local gpu = component.list("gpu")()
  43. if gpu and screen then
  44. boot_invoke(gpu, "bind", screen)
  45. setResolution(80, 25) -- Adjust resolution for larger text
  46. end
  47. end
  48.  
  49. local function tryLoadFrom(address)
  50. local handle, reason = boot_invoke(address, "open", "/init.lua")
  51. if not handle then
  52. return nil, reason
  53. end
  54. local buffer = ""
  55. repeat
  56. local data, reason = boot_invoke(address, "read", handle, math.maxinteger or math.huge)
  57. if not data and reason then
  58. return nil, reason
  59. end
  60. buffer = buffer .. (data or "")
  61. until not data
  62. boot_invoke(address, "close", handle)
  63. return load(buffer, "=init")
  64. end
  65.  
  66. local function drawBootMessage()
  67. local gpu = component.list("gpu")()
  68. local w, h = component.invoke(gpu, "getResolution")
  69. component.invoke(gpu, "fill", 1, 1, w, h, " ") -- Clear the screen
  70. component.invoke(gpu, "setBackground", 0x000000) -- Set background to black
  71. drawCenteredText("DOGGY CORP")
  72. end
  73.  
  74. local function drawErrorMessage()
  75. local gpu = component.list("gpu")()
  76. local w, h = component.invoke(gpu, "getResolution")
  77. component.invoke(gpu, "fill", 1, 1, w, h, " ") -- Clear the screen
  78. component.invoke(gpu, "setBackground", 0x000000) -- Set background to black
  79. drawCenteredText("BIOS failed to locate boot drive!")
  80. end
  81.  
  82. local function runWithoutHalting()
  83. drawErrorMessage()
  84. computer.beep(1500, 0.5) -- Longer beep
  85. delay(0.5) -- Delay after the beep
  86. for _ = 1, 2 do
  87. computer.beep(1500, 0.10) -- Beep 2 times quickly
  88. delay(0.2) -- Delay between beeps
  89. end
  90. while true do
  91. computer.pullSignal(0.5) -- Adjust the delay time as needed
  92. drawErrorMessage()
  93. end
  94. end
  95.  
  96. local reason
  97. drawBootMessage()
  98. delay(2) -- Wait 2 seconds
  99. if computer.getBootAddress() then
  100. init, reason = tryLoadFrom(computer.getBootAddress())
  101. end
  102. if not init then
  103. computer.setBootAddress()
  104. for address in component.list("filesystem") do
  105. init, reason = tryLoadFrom(address)
  106. if init then
  107. computer.setBootAddress(address)
  108. break
  109. end
  110. end
  111. end
  112. if not init then
  113. runWithoutHalting() -- Run the error message without halting the computer
  114. return
  115. end
  116. computer.beep(1000, 0.10)
  117. end
  118. return init()
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement