Skip to content

Instantly share code, notes, and snippets.

@Galaxtone
Created May 26, 2020 01:07
Show Gist options
  • Save Galaxtone/bfe26d936c254de1b796b922817561c5 to your computer and use it in GitHub Desktop.
Save Galaxtone/bfe26d936c254de1b796b922817561c5 to your computer and use it in GitHub Desktop.
TIS-100-3D
-- Configurable options
local inputAddresses = {"d28f","4c41","2dea","a359"}
local outputAddresses = {"7b48","1ce0","ebca","1d7d"}
local numInputValues = 100
local inputGens = {}
inputGens[2] = function()
return math.random(10, 99)
end
local outputGens = {}
outputGens[3] = function(inputSets, i)
return inputSets[2][i] * 2
end
-- End of configurable options
local component = require("component")
local os = require("os")
-- Grabbing input and output serial ports
local inputs = {}
for i = 1, #inputAddresses do
local address = inputAddresses[i]
local fullAddress = component.get(address)
if not fullAddress then
error("Failed to resolve address " .. address)
end
local input = component.proxy(fullAddress)
if not input or input.type ~= "serial_port" then
error("Failed to get serial port from address " .. fullAddress)
end
input.setReading(false)
table.insert(inputs, input)
end
local outputs = {}
for i = 1, #outputAddresses do
local address = outputAddresses[i]
local fullAddress = component.get(address)
if not fullAddress then
error("Failed to resolve address " .. address)
end
local output = component.proxy(fullAddress)
if not output or output.type ~= "serial_port" then
error("Failed to get serial port from address " .. fullAddress)
end
output.setReading(false)
table.insert(outputs, output)
end
local numInputs = #inputs
local numOutputs = #outputs
-- Generating input and output sets
local inputSets = {}
for i = 1, numInputs do
local inputGen = inputGens[i]
if inputGen then
if not inputs[i] then
error("Found input gen for non-existant input #" .. i)
end
local inputSet = {}
inputSets[i] = inputSet
for j = 1, numInputValues do
table.insert(inputSet, inputGen())
end
end
end
local outputSets = {}
for i = 1, numOutputs do
local outputGen = outputGens[i]
if outputGen then
if not outputs[i] then
error("Found output gen for non-existant output #" .. i)
end
local outputSet = {}
outputSets[i] = outputSet
for j = 1, numInputValues do
local output = outputGen(inputSets, j)
if output then
if type(output) == "table" then
for k = 1, #output do
table.insert(outputSet, output[k])
end
else
table.insert(outputSet, output)
end
end
end
end
end
-- Sending input sets and receiving output sets
local receivedSets = {}
for i = 1, numOutputs do
if outputSets[i] then
receivedSets[i] = {}
outputs[i].setReading(true)
end
end
local function receive()
local enoughReceived = true
for j = 1, numOutputs do
local receivedSet = receivedSets[j]
if receivedSet then
local received = outputs[j].read()
while received ~= nil do
table.insert(receivedSet, received)
received = outputs[j].read()
end
if #receivedSet < #outputSets[j] then
enoughReceived = false
end
end
end
return enoughReceived
end
local inputWritten = 0
print("Transmitting input sets and receiving...")
local enoughReceived
for i = 1, numInputValues do
for j = 1, numInputs do
local inputSet = inputSets[j]
if inputSet then
inputs[j].write(inputSet[i])
end
end
inputWritten = inputWritten + 1
print(inputWritten .. "...")
enoughReceived = receive()
end
if not enoughReceived then
print("Waiting to receive more data...")
while not receive() do
os.sleep(0.2)
end
end
-- Comparing received sets against output sets
print()
print("Comparing received sets to output sets:")
local totalCorrect = 0
local total = 0
for i = 1, numOutputs do
local outputSet = outputSets[i]
if outputSet then
local receivedSet = receivedSets[i]
local receivedCorrect = 0
total = total + #outputSet
for j = 1, #outputSet do
if receivedSet[j] == outputSet[j] then
receivedCorrect = receivedCorrect + 1
totalCorrect = totalCorrect + 1
end
end
local receivedStatus = "FAIL"
if receivedCorrect == #outputSet then
receivedStatus = "SUCCESS"
end
print(string.format(" Output #%d: %.2f%% - %s", i, (receivedCorrect / #outputSet) * 100, receivedStatus))
end
end
print()
local totalStatus = "FAIL"
if totalCorrect == total then
totalStatus = "SUCCESS"
end
print(string.format("Overall %.2f%% - %s", (totalCorrect / total) * 100, totalStatus))
local component = require("component")
for address in component.list("serial_port") do
component.invoke(address, "write", tonumber(address:sub(1, 4), 16))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment