Skip to content

Commit

Permalink
Space operators / minor formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DerelictDrone committed Dec 12, 2023
1 parent c203bb2 commit 31de8fe
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 90 deletions.
3 changes: 1 addition & 2 deletions lua/autorun/cpu_load.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ AddCSLuaFile("wire/cpu_default_data_files.lua")

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

end
8 changes: 4 additions & 4 deletions lua/wire/zvm/tests/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ CPUTest = {}
function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("x: INC R0 JMP x",nil,CPUTest.RunCPU,CPUTest.CompileError)
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.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,4096 do
CPUTest.VM:RunStep()
Expand All @@ -16,12 +16,12 @@ function CPUTest.RunCPU()
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.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.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end
23 changes: 12 additions & 11 deletions lua/wire/zvm/tests/execute_from_iobus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,37 @@ 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)
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
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
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"}
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)
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.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
for i = 0, 32 do
CPUTest.VM:RunStep()
end

Expand All @@ -41,12 +42,12 @@ function CPUTest.RunCPU()
CPUTest.TestSuite.FinishTest(false)
else
PrintTable(CPUTest.IOBus)
CPUTest.TestSuite.Error("R0 != 24, R0 = "..tostring(CPUTest.VM.R0))
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)
CPUTest.TestSuite.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end
6 changes: 3 additions & 3 deletions lua/wire/zvm/tests/file_example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ 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
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.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.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end
28 changes: 14 additions & 14 deletions lua/wire/zvm/tests/ifdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ CPUTest = {}
-- 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.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.Variations1 = {"true", "false"}
CPUTest.Variations2 = {"#define x\n", "#define y\n", "#define x\n#define y\n"}
CPUTest.Variation1Index = 1
CPUTest.Variation2Index = 1

Expand All @@ -31,12 +31,12 @@ end
function CPUTest.CompileNext()
local cursrc
if CPUTest.Variation1Index <= #CPUTest.Variations1 then
cursrc = "#pragma set NewIfDefs "..CPUTest.Variations1[CPUTest.Variation1Index].."\n"
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
cursrc = cursrc .. CPUTest.Variations2[CPUTest.Variation2Index] .. "\n" .. CPUTest.Src
CPUTest.TestSuite.Compile(cursrc,nil,CPUTest.LogResults,CPUTest.CompileError)
else
CPUTest.Variation1Index = CPUTest.Variation1Index + 1
Expand All @@ -46,23 +46,23 @@ function CPUTest.CompileNext()
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.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
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
for ind, i in ipairs(CPUTest.ExpectedVariations2) do
if CPUTest.ResultVariations2[ind] == i then
results2[ind] = true
else
Expand All @@ -72,15 +72,15 @@ function CPUTest.CompareResults()
end
if fail then
CPUTest.TestSuite.Error("Unexpected test results!")
PrintTable({CPUTest.ResultVariations1,results1,CPUTest.ResultVariations2,results2})
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.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end

10 changes: 5 additions & 5 deletions lua/wire/zvm/tests/includes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ function CPUTest:RunTest(VM,TestSuite)
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)
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.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,16 do
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.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.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end
2 changes: 1 addition & 1 deletion lua/wire/zvm/tests/intentional_compile_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CPUTest = {}
function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("MOV R0,",nil,CPUTest.RunCPU,CPUTest.CompileError)
TestSuite.Compile("MOV R0,", nil, CPUTest.RunCPU, CPUTest.CompileError)
end

function CPUTest.RunCPU()
Expand Down
10 changes: 5 additions & 5 deletions lua/wire/zvm/tests/intentional_failed_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ CPUTest = {}
function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("x: INC R0 JMP x",nil,CPUTest.RunCPU,CPUTest.CompileError)
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.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.VM.Clk = 1
for i=0,4096 do
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.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.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end
12 changes: 6 additions & 6 deletions lua/wire/zvm/tests/no_internal_mem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ CPUTest = {}
function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("MOV R0,1",nil,CPUTest.RunCPU,CPUTest.CompileError)
TestSuite.Compile("MOV R0,1", nil, CPUTest.RunCPU, CPUTest.CompileError)
end

function CPUTest.RunCPU()
local buff = CPUTest.TestSuite.GetCompileBuffer()
local bus = CPUTest.TestSuite.CreateVirtualMemBus(#buff) -- get external ram device large enough to hold program
CPUTest.TestSuite.FlashData(bus,buff) -- upload compiled to membus
CPUTest.TestSuite.FlashData(bus, buff) -- upload compiled to membus
CPUTest.VM.RAMSize = 0
CPUTest.VM.ROMSize = 0
CPUTest.TestSuite.Initialize(CPUTest.VM,bus,nil) -- reinitialize the CPU with the membus
CPUTest.TestSuite.Initialize(CPUTest.VM, bus, nil) -- reinitialize the CPU with the membus
CPUTest.VM.Clk = 1
for i=0,16 do
for i = 0, 16 do
CPUTest.VM:RunStep()
end

-- False = no error, True = error
if CPUTest.VM.R0 == 1 then
CPUTest.TestSuite.FinishTest(false)
else
CPUTest.TestSuite.Error("CPU with no ram/rom failed to execute code from bus! R0 = "..CPUTest.VM.R0)
CPUTest.TestSuite.Error("CPU with no ram/rom failed to execute code from bus! R0 = " .. CPUTest.VM.R0)
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError(msg)
CPUTest.TestSuite.Error("hit a compile time error "..msg)
CPUTest.TestSuite.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end
16 changes: 8 additions & 8 deletions lua/wire/zvm/tests/virtualiobus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ CPUTest = {}
function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("MOV PORT0,1 MOV R0,PORT0",nil,CPUTest.RunCPU,CPUTest.CompileError)
TestSuite.Compile("MOV PORT0,1 MOV R0,PORT0", nil, CPUTest.RunCPU, CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
local IOBus = CPUTest.TestSuite.CreateVirtualIOBus(4) -- get external IO device of size 4
CPUTest.TestSuite.Initialize(CPUTest.VM,nil,IOBus) -- reinitialize the CPU with the IOBus
CPUTest.TestSuite.Initialize(CPUTest.VM, nil, IOBus) -- reinitialize the CPU with the IOBus
IOBus.InPorts[0] = 24
CPUTest.VM.Clk = 1
for i=0,16 do
for i = 0, 16 do
CPUTest.VM:RunStep()
end

Expand All @@ -22,19 +22,19 @@ function CPUTest.RunCPU()
if CPUTest.VM.R0 == 24 then
CPUTest.TestSuite.FinishTest(false)
else
CPUTest.TestSuite.Error("CPU failed to read input port! R0 = "..CPUTest.VM.R0)
CPUTest.TestSuite.Error("CPU failed to read input port! R0 = " .. CPUTest.VM.R0)
CPUTest.TestSuite.FinishTest(true)
end
else
CPUTest.TestSuite.Error("CPU failed to write to output port! Port0 = "..IOBus.OutPorts[0])
CPUTest.TestSuite.Error("CPU failed to write to output port! Port0 = " .. IOBus.OutPorts[0])
end
else
CPUTest.TestSuite.Error("CPU wrote to input ports! "..tostring(IOBus:ReadCell(0)))
CPUTest.TestSuite.Error("CPU wrote to input ports! " .. tostring(IOBus:ReadCell(0)))
CPUTest.TestSuite.FinishTest(true)
end
end

function CPUTest.CompileError(msg)
CPUTest.TestSuite.Error("hit a compile time error "..msg)
CPUTest.TestSuite.Error("hit a compile time error " .. msg)
CPUTest.TestSuite.FinishTest(true)
end
8 changes: 4 additions & 4 deletions lua/wire/zvm/tests/virtualmembus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ CPUTest = {}
function CPUTest:RunTest(VM,TestSuite)
CPUTest.VM = VM
CPUTest.TestSuite = TestSuite
TestSuite.Compile("CPUGET R0,43 MOV [R0],1 MOV R1,[R0]",nil,CPUTest.RunCPU,CPUTest.CompileError)
TestSuite.Compile("CPUGET R0,43 MOV [R0],1 MOV R1,[R0]", nil, CPUTest.RunCPU, CPUTest.CompileError)
end

function CPUTest.RunCPU()
CPUTest.TestSuite.FlashData(CPUTest.VM,CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
CPUTest.TestSuite.FlashData(CPUTest.VM, CPUTest.TestSuite.GetCompileBuffer()) -- upload compiled to virtual cpu
local bus = CPUTest.TestSuite.CreateVirtualMemBus(4) -- get external ram device of size 4
CPUTest.TestSuite.Initialize(CPUTest.VM,bus,nil) -- reinitialize the CPU with the membus
CPUTest.TestSuite.Initialize(CPUTest.VM, bus, nil) -- reinitialize the CPU with the membus
CPUTest.VM.Clk = 1
for i=0,16 do
for i = 0, 16 do
CPUTest.VM:RunStep()
end

Expand Down
Loading

0 comments on commit 31de8fe

Please sign in to comment.