Skip to content

Commit

Permalink
IOBus execution test + Fixed IOBus/Membus readcell
Browse files Browse the repository at this point in the history
  • Loading branch information
DerelictDrone committed Nov 16, 2023
1 parent ccd3087 commit 61c10a9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
55 changes: 55 additions & 0 deletions lua/wire/zvm/tests/execute_from_iobus.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
CPUTest = {}

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("MOV R0,6 ADD R0,R0 MUL R0,2",nil,CPUTest.CompileNext,CPUTest.CompileError)
-- end result of the above code should be R0 = 24
end

function CPUTest.CompileNext()
local buff = CPUTest.TestSuite.GetCompileBuffer()
local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(#buff+1) -- create an IOBus large enough to hold this code
PrintTable(buff)
CPUTest.IOBus = IOBus
-- reverse the compiled code, the CPU will read them in reverse if it's in the IOBus
-- because CS will be negative, and IP only increments
-- ipairs won't index 0 and the cpu compile buffer uses 0
for i=0,#buff do
IOBus.InPorts[#buff-i] = buff[i]
end
IOBus.OldReadCell = IOBus.ReadCell
IOBus.AccessLog = {}
function IOBus:ReadCell(address)
IOBus.AccessLog[#IOBus.AccessLog+1] = {"read address"..tostring(address),self:OldReadCell(address) or "no value"}
return self:OldReadCell(address)
end
-- JMPF jumps to 0 IP, CS = (code length+1)*-1 because first index of IOBus is "cell -1" of extern read/write
local generatedcode = "CMP R0,0 JNER -3 JMPF 0,"..(#buff+1)*-1
print(generatedcode)
CPUTest.TestSuite.Compile(generatedcode,nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.TestSuite.Initialize(CPUTest.VM,nil,CPUTest.IOBus) -- reinitialize the CPU with the IOBus
CPUTest.VM.Clk = 1
for i=0,32 do
CPUTest.VM:RunStep()
end

-- False = no error, True = error
if CPUTest.VM.R0 == 24 then
CPUTest.TestSuite.FinishTest(false)
else
PrintTable(CPUTest.IOBus)
print("R0 != 24, R0 = "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError()
print('hit a compile time error')
CPUTest.TestSuite.FinishTest(true)
end

27 changes: 22 additions & 5 deletions lua/wire/zvm/zvm_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ function ZVMTestSuite.RunNextTest()
end

function ZVMTestSuite.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform)
ZVMTestSuite.CompileArgs = {
SourceCode = SourceCode,
FileName = FileName,
SuccessCallback = SuccessCallback,
ErrorCallback = ErrorCallback,
TargetPlatform = TargetPlatform
}
-- Needs to delay next compile otherwise it'll halt, this will make it easier.
timer.Simple(0.125,ZVMTestSuite.StartCompileInternal)
end

function ZVMTestSuite.StartCompileInternal()
local SourceCode = ZVMTestSuite.CompileArgs.SourceCode
local FileName = ZVMTestSuite.CompileArgs.FileName
local SuccessCallback = ZVMTestSuite.CompileArgs.SuccessCallback
local ErrorCallback = ZVMTestSuite.CompileArgs.ErrorCallback
local TargetPlatform = ZVMTestSuite.CompileArgs.TargetPlatform
CPULib.Compile(SourceCode,FileName,SuccessCallback,ErrorCallback,TargetPlatform)
end

Expand All @@ -72,12 +89,12 @@ end
function ZVMTestSuite.CreateVirtualMemBus(MembusSize)
local virtualMemBus = {Size = MembusSize}
function virtualMemBus:ReadCell(Address)
if Address < self.Size and Address > -1 then
if Address <= self.Size and Address > -1 then
return virtualMemBus[Address]
end
end
function virtualMemBus:WriteCell(Address,Value)
if Address < self.Size and Address > -1 then
if Address <= self.Size and Address > -1 then
virtualMemBus[Address] = Value
return true
end
Expand All @@ -87,14 +104,14 @@ function ZVMTestSuite.CreateVirtualMemBus(MembusSize)
end

function ZVMTestSuite.CreateVirtualIOBus(IOBusSize)
local virtualIOBus = {InPorts = {},OutPorts = {}, Size = IOBusSize}
local virtualIOBus = {InPorts = {},OutPorts = {}, Size = IOBusSize-1}
function virtualIOBus:ReadCell(Address)
if Address < self.Size and Address > -1 then
if Address <= self.Size and Address > -1 then
return self.InPorts[Address]
end
end
function virtualIOBus:WriteCell(Address,Value)
if Address < self.Size and Address > -1 then
if Address <= self.Size and Address > -1 then
self.OutPorts[Address] = Value
return true
end
Expand Down

0 comments on commit 61c10a9

Please sign in to comment.