Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZCPU Automated Testing console command & framework #18

Merged
merged 22 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lua/autorun/cpu_load.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ AddCSLuaFile("wire/zvm/zvm_features.lua")
AddCSLuaFile("wire/zvm/zvm_opcodes.lua")
AddCSLuaFile("wire/zvm/zvm_data.lua")

if game.SinglePlayer() then
AddCSLuaFile("wire/zvm/zvm_tests.lua")
include("wire/zvm/zvm_tests.lua")
end

AddCSLuaFile("wire/cpulib.lua")
include("wire/cpulib.lua")

Expand All @@ -30,4 +35,5 @@ AddCSLuaFile("wire/cpu_default_data_files.lua")

if CLIENT then
include("wire/client/hlzasm/hc_compiler.lua")
end
end

9 changes: 5 additions & 4 deletions lua/wire/cpulib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
local INVALID_BREAKPOINT_IP = 2e7

CPULib = CPULib or {}
if CLIENT then
if CLIENT or TESTING then
-- Sourcecode available as compiled binary
CPULib.Source = ""
-- Compiled binary
Expand Down Expand Up @@ -34,6 +34,7 @@
CPULib.Debugger.PointersByLine = {}

CPULib.Debugger.Breakpoint = {}
CPULib.print = print
DerelictDrone marked this conversation as resolved.
Show resolved Hide resolved

-- Convars to control CPULib
local wire_cpu_upload_speed = CreateClientConVar("wire_cpu_upload_speed",1000,false,false)
Expand Down Expand Up @@ -69,14 +70,14 @@
-- Start compiling the sourcecode
HCOMP:StartCompile(source,fileName or "source",CPULib.OnWriteByte,nil)
HCOMP.Settings.CurrentPlatform = targetPlatform or "CPU"
print("=== HL-ZASM High Level Assembly Compiler Output ==")
CPULib.print("=== HL-ZASM High Level Assembly Compiler Output ==")

-- Initialize callbacks
CPULib.SuccessCallback = successCallback
CPULib.ErrorCallback = errorCallback

-- Run the timer
timer.Create("cpulib_compile",1/60,0,CPULib.OnCompileTimer)

Check warning on line 80 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator
CPULib.Compiling = true
end

Expand All @@ -85,14 +86,14 @@
function CPULib.SelectTab(editor,fileName)
if not editor then return end
local editorType = string.lower(editor.EditorType)
local fullFileName = editorType.."chip\\"..fileName

Check warning on line 89 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator

Check warning on line 89 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator

if string.sub(fileName,1,7) == editorType.."chip" then

Check warning on line 91 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator
fullFileName = fileName
end

local sourceTab
for tab=1,editor:GetNumTabs() do

Check warning on line 96 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator
if editor:GetEditor(tab).chosenfile == fullFileName then
sourceTab = tab
end
Expand All @@ -114,9 +115,9 @@
CPULib.Compile(source,fileName,
function()
editor.C.Val:Update(nil, nil, " Success, "..(HCOMP.WritePointer or "?").." bytes compiled.", Color(50, 128, 20))
end,

Check warning on line 118 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator

Check warning on line 118 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator

Check warning on line 118 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator

Check warning on line 118 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace after ')'
function(error,errorPos)
local issue = (error or "unknown error")

Check warning on line 120 in lua/wire/cpulib.lua

View workflow job for this annotation

GitHub Actions / lint

"Whitespace style"

Style: Please put some whitespace before the operator

local line, char = 0, 0

Expand Down Expand Up @@ -147,14 +148,14 @@
for _ = 1, compile_speed do
local status,result = pcall(HCOMP.Compile,HCOMP)
if not status then
print("==================================================")
CPULib.print("==================================================")
if CPULib.ErrorCallback then CPULib.ErrorCallback(HCOMP.ErrorMessage or ("Internal error: "..result),HCOMP.ErrorPosition) end
timer.Remove("cpulib_compile")
CPULib.Compiling = false

return
elseif not result then
print("==================================================")
CPULib.print("==================================================")
CPULib.Source = CPULib.CurrentSource
CPULib.Buffer = CPULib.CurrentBuffer

Expand Down
27 changes: 27 additions & 0 deletions lua/wire/zvm/tests/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CPUTest = {}

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("x: INC R0 JMP x",nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,4096 do
CPUTest.VM:RunStep()
end
-- False = no error, True = error
if CPUTest.VM.R0 == 4096 then
CPUTest.TestSuite.FinishTest(false)
else
CPUTest.TestSuite.Error("R0 is not 4096! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError(msg)
CPUTest.TestSuite.Error('hit a compile time error '..msg)
CPUTest.TestSuite.FinishTest(true)
end
52 changes: 52 additions & 0 deletions lua/wire/zvm/tests/execute_from_iobus.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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
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
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)
CPUTest.TestSuite.Error("R0 != 24, R0 = "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError(msg)
CPUTest.TestSuite.Error('hit a compile time error '..msg)
DerelictDrone marked this conversation as resolved.
Show resolved Hide resolved
CPUTest.TestSuite.FinishTest(true)
end
29 changes: 29 additions & 0 deletions lua/wire/zvm/tests/file_example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CPUTest = {}
DerelictDrone marked this conversation as resolved.
Show resolved Hide resolved

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
-- Loads a file from the testing directory and returns it as a str
local src = TestSuite:LoadFile("file_example.txt")
TestSuite.Compile(src,nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,4096 do
CPUTest.VM:RunStep()
end
-- False = no error, True = error
if CPUTest.VM.R0 == 4096 then
CPUTest.TestSuite.FinishTest(false)
else
print("R0 is not 4096! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError(msg)
CPUTest.TestSuite.Error('hit a compile time error '..msg)
CPUTest.TestSuite.FinishTest(true)
end
3 changes: 3 additions & 0 deletions lua/wire/zvm/tests/file_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x:
INC R0
JMP x
86 changes: 86 additions & 0 deletions lua/wire/zvm/tests/ifdefs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
CPUTest = {}

--not x and y = 14, name "Test Y"
-- x and y = 11, name "Test Y"
-- x and not y = 1, name "Test Y"

-- culling update
-- not x and y = 12, name "Test Y"
-- x and y = 11, name "Test X and Y"
-- x and not y = 1, name "Test X"


CPUTest.ExpectedVariations1 = {"X","Y","X and Y","Y","Y","Y"} -- CPU Name vars
CPUTest.ExpectedVariations2 = {1,12,11,1,14,11}
CPUTest.ResultVariations1 = {}
CPUTest.ResultVariations2 = {}

CPUTest.Variations1 = {"true","false"}
CPUTest.Variations2 = {"#define x\n","#define y\n","#define x\n#define y\n"}
CPUTest.Variation1Index = 1
CPUTest.Variation2Index = 1

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
-- Loads a file from the testing directory and returns it as a str
CPUTest.Src = TestSuite:LoadFile("ifdefs.txt")
CPUTest.CompileNext()
end

function CPUTest.CompileNext()
local cursrc
if CPUTest.Variation1Index <= #CPUTest.Variations1 then
cursrc = "#pragma set NewIfDefs "..CPUTest.Variations1[CPUTest.Variation1Index].."\n"
else
return CPUTest.CompareResults()
end
if CPUTest.Variation2Index <= #CPUTest.Variations2 then
cursrc = cursrc..CPUTest.Variations2[CPUTest.Variation2Index].."\n"..CPUTest.Src
CPUTest.TestSuite.Compile(cursrc,nil,CPUTest.LogResults,CPUTest.CompileError)
else
CPUTest.Variation1Index = CPUTest.Variation1Index + 1
CPUTest.Variation2Index = 1
CPUTest.CompileNext()
end
end

function CPUTest.LogResults()
CPUTest.ResultVariations1[CPUTest.Variation2Index+#CPUTest.Variations2*(CPUTest.Variation1Index-1)] = CPUTest.TestSuite.GetCPUName() or "ERROR"
CPUTest.ResultVariations2[CPUTest.Variation2Index+#CPUTest.Variations2*(CPUTest.Variation1Index-1)] = #CPUTest.TestSuite.GetCompileBuffer()+1 or "ERROR"
CPUTest.Variation2Index = CPUTest.Variation2Index + 1
CPUTest.CompileNext()
end

function CPUTest.CompareResults()
local fail,results1,results2 = false,{},{}
for ind,i in ipairs(CPUTest.ExpectedVariations1) do
if CPUTest.ResultVariations1[ind] == "Test "..i then
results1[ind] = true
else
fail = true
results1[ind] = false
end
end
for ind,i in ipairs(CPUTest.ExpectedVariations2) do
if CPUTest.ResultVariations2[ind] == i then
results2[ind] = true
else
fail = true
results2[ind] = false
end
end
if fail then
CPUTest.TestSuite.Error('Unexpected test results!')
PrintTable({CPUTest.ResultVariations1,results1,CPUTest.ResultVariations2,results2})
CPUTest.TestSuite.FinishTest(true)
else
CPUTest.TestSuite.FinishTest(false)
end
end

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

38 changes: 38 additions & 0 deletions lua/wire/zvm/tests/ifdefs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*defs will be added programatically*/

#ifdef x
#define z
#pragma cpuname Test X
ALLOC 1
#ifdef y
#ifdef x
#pragma cpuname Test X and Y
#endif
ALLOC 2
#endif
//not x
#else
ALLOC 4
#endif

#ifdef y
#ifndef x
#pragma cpuname Test Y
#endif
ALLOC 8
#endif
/*
#ifdef y
ALLOC 8
#endif
*/
//above comment intentional for making sure ifdef handler doesn't skip into the middle of a comment

// not x and y = 14, name "Test Y"
// x and y = 11, name "Test Y"
// x and not y = 1, name "Test Y"

// culling update
// not x and y = 12, name "Test Y"
// x and y = 11, name "Test X and Y"
// x and not y = 1, name "Test X"
29 changes: 29 additions & 0 deletions lua/wire/zvm/tests/includes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CPUTest = {}

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
-- Loads a file from the testing directory and returns it as a str
local src = TestSuite:LoadFile("includes_1.txt")
TestSuite.Compile(src,nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,16 do
CPUTest.VM:RunStep()
end
-- False = no error, True = error
if CPUTest.VM.R0 == 2 then
CPUTest.TestSuite.FinishTest(false)
else
CPUTest.TestSuite.Error("R0 is not 2! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError(msg)
CPUTest.TestSuite.Error('hit a compile time error '..msg)
CPUTest.TestSuite.FinishTest(true)
end
2 changes: 2 additions & 0 deletions lua/wire/zvm/tests/includes_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <includes_2.txt>
ADD R0,1
1 change: 1 addition & 0 deletions lua/wire/zvm/tests/includes_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MOV R0,1
16 changes: 16 additions & 0 deletions lua/wire/zvm/tests/intentional_compile_error.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CPUTest = {}

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("MOV R0,",nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.Error('Compiler did not error when it should have!')
CPUTest.TestSuite.FinishTest(true)
end

function CPUTest.CompileError()
CPUTest.TestSuite.FinishTest(false)
end
27 changes: 27 additions & 0 deletions lua/wire/zvm/tests/intentional_failed_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CPUTest = {}

function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("x: INC R0 JMP x",nil,CPUTest.RunCPU,CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,4096 do
CPUTest.VM:RunStep()
end
-- False = no error, True = error
if CPUTest.VM.R0 == 4095 then
CPUTest.TestSuite.FinishTest(false)
else
CPUTest.TestSuite.Error("R0 is not 4095! R0 is "..tostring(CPUTest.VM.R0))
CPUTest.TestSuite.FinishTest(true)
end
end

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