**This is an old revision of the document!**

编写自制操作系统

从 OpenComputers 1.3 开始,计算机需要一个“操作系统”才能运行-——OpenOS 不再内置。 从1.4(.2?)开始,您甚至需要在计算机中安装EEPROM,以充当机器的 BIOS。 虽然这使得组装计算机麻烦了一点,但这也为想要编写自己的操作系统的人提供了更多可能;本文的主题就是编写操作系统!

引导

现在唯一内置的是启动 BIOS 的“机器”。这个“机器”包装器是一个不可编辑的 Lua 脚本,负责设置沙箱和与“主机”端的事物(即 Java / Scala)的超低级交互。打开计算机电源后,将加载并运行该脚本。在稳定沙盒环境后,它会查找 EEPROM,并尝试将其数据作为 Lua 脚本运行。如果失败,计算机将崩溃。否则,将运行该 BIOS。对于提供的 Lua BIOS(使用 EEPROM +手册制作),它将执行以前由机器本身完成的操作:

  • 1.它查找文件系统并通过 computer.setBootAddress 设置地址(通过computer.getBootAddress读取)。
  • 2.在该文件系统上,它尝试加载并执行名为 init.lua 的文件。
  • 3.如果成功了,现在由用户脚本控制。如果失败,它将遍历所有当前文件系统,再次执行步骤2,直到成功为止。
  • 4.如果没有找到可用的 init.lua 脚本。计算机将崩溃并显示 no bootable medium found 错误.

但是,由于您可以随心所欲地对 EEPROM 进行编程,因此您也可以从网络设备启动。

什么可用?

Wiki 上记录了一些库,它们实际上是 OpenOS 的一部分,所以当你实现自己的操作系统时,这些库不会由你使用。最值得注意的是,这包括 iopackagefilesystem 库。有关 init 脚本中可用内容的明确参考,请检查 [[https://github.com/MightyPirates/OpenComputers/blob/master-MC1.12/src/main/resources/assets/opencomputers/lua/machine.lua|machine.lua script for your current OpenComputers version](英文)。 以下列表不能保证完整或最新!

snippet.lua
_G
_VERSION
 
assert
error
getmetatable
ipairs
load
next
pairs
pcall
rawequal
rawget
rawlen
rawset
select
setmetatable
tonumber
tostring
type
xpcall
 
bit32.arshift
bit32.band
bit32.bnot
bit32.bor
bit32.btest
bit32.bxor
bit32.extract
bit32.lrotate
bit32.lshift
bit32.replace
bit32.rrotate
bit32.rshift
 
coroutine.create
coroutine.resume
coroutine.running
coroutine.status
coroutine.wrap
coroutine.yield
 
debug.getinfo
debug.traceback
debug.getlocal
debug.getupvalue
 
math.abs
math.acos
math.asin
math.atan
math.atan2
math.ceil
math.cos
math.cosh
math.deg
math.exp
math.floor
math.fmod
math.frexp
math.huge
math.ldexp
math.log
math.max
math.min
math.modf
math.pi
math.pow
math.rad
math.random
math.randomseed
math.sin
math.sinh
math.sqrt
math.tan
math.tanh
 
os.clock
os.date
os.difftime
os.time
 
string.byte
string.char
string.dump
string.find
string.format
string.gmatch
string.gsub
string.len
string.lower
string.match
string.rep
string.reverse
string.sub
string.upper
 
table.concat
table.insert
table.pack
table.remove
table.sort
table.unpack
 
checkArg
 
component.doc
component.fields
component.invoke
component.list
component.methods
component.proxy
component.slot
component.type
 
computer.address
computer.addUser
computer.beep
computer.energy
computer.freeMemory
computer.getArchitectures
computer.getArchitecture
computer.getBootAddress
computer.getDeviceInfo
computer.getProgramLocations
computer.isRobot
computer.maxEnergy
computer.pullSignal
computer.pushSignal
computer.removeUser
computer.setArchitecture
computer.setBootAddress
computer.shutdown
computer.tmpAddress
computer.totalMemory
computer.uptime
computer.users
 
unicode.char
unicode.charWidth
unicode.isWide
unicode.len
unicode.lower
unicode.reverse
unicode.sub
unicode.upper
unicode.wlen
unicode.wtrunc
 
-- 仅Lua 5.3:
coroutine.isyieldable
 
string.pack
string.unpack
string.packsize
 
table.move
 
math.maxinteger
math.mininteger
math.tointeger
math.type
math.ult
 
utf8.char
utf8.charpattern
utf8.codes
utf8.codepoint
utf8.len
utf8.offset

在 BIOS 中访问设备

与 OpenOS 不同,您在 BIOS 中没有组件的主系统。这意味着您不能只编写 component.redstone.setOutput,您必须首先手动为组件创建代理。出于多种原因,代理系统是通过机器本身提供的,因此您仍然可以使用它。若要获取代理,请使用 component.proxy 方法。它需要组件的地址来转换。 您可以手动写下来,或者用 component.list 来获得组件的列表。

比如说,要获得第一个红石组件,您可以执行以下操作:

snippet.lua
local r = component.proxy(component.list("redstone")())

这起作用是因为 component.list 返回一个 Lua 迭代器:一个可用重复调用来获得下一个元素的函数(没有元素的时候返回一次 nil)。

其他东西

init.lua 返回的时候,计算机将会关机,所以在这里你需要设置一个“主循环”。无论您如何构建操作系统。信号必须使用 computer.pullSignal 处理,您可以自由使用任何模块将其提供给在操作系统中运行的程序——例如,这就是 OpenOS 中 event.pull 的作用。

您还需要在操作系统的启动例程中设置附加组件,例如将 GPU 绑定到屏幕。

除此之外,疯狂,发挥创造力!