Skip to content

Commit

Permalink
Emit & Dyn_emit now wrap string.format + Vector instruction segment fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DerelictDrone committed Nov 26, 2023
1 parent a00b47a commit f79d6b8
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 121 deletions.
8 changes: 4 additions & 4 deletions lua/entities/gmod_wire_cpu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ function ENT:Run()
self:Dyn_EmitState()
self:Emit("VM.CPUIF.OnBreakpointInstruction(VM.IP)")
self:Emit("VM.CPUIF.VMStopped = true")
self:Emit("VM.TMR = VM.TMR + "..self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + "..self.PrecompileBytes)
self:Emit("VM.TMR = VM.TMR + %d",self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + %d",self.PrecompileBytes)
self:Emit("if true then return end")
self:Emit("end")
self:Emit("if VM.CPUIF.LastInstruction and ((VM.IP > VM.CPUIF.LastInstruction) or VM.CPUIF.ForceLastInstruction) then")
self:Dyn_EmitState()
self:Emit("VM.CPUIF.ForceLastInstruction = nil")
self:Emit("VM.CPUIF.OnLastInstruction()")
self:Emit("VM.CPUIF.VMStopped = true")
self:Emit("VM.TMR = VM.TMR + "..self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + "..self.PrecompileBytes)
self:Emit("VM.TMR = VM.TMR + %d",self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + %d",self.PrecompileBytes)
self:Emit("if true then return end")
self:Emit("end")
end)
Expand Down
48 changes: 24 additions & 24 deletions lua/wire/zvm/zvm_core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ if ZVM.MicrocodeDebug then -- Debug microcode generator
then pad = pad + 1 end
end
else
function ZVM:Emit(text)
self.EmitBlock = self.EmitBlock..text.."\n"
function ZVM:Emit(...)
self.EmitBlock = self.EmitBlock..string.format(...).."\n"
end
end

Expand Down Expand Up @@ -155,8 +155,8 @@ end

--------------------------------------------------------------------------------
-- Emit preprocessed text
function ZVM:Dyn_Emit(text)
self:Emit(self:Dyn_PreprocessEmit(text))
function ZVM:Dyn_Emit(...)
self:Emit(self:Dyn_PreprocessEmit(string.format(...)))
end


Expand Down Expand Up @@ -196,11 +196,11 @@ end
--------------------------------------------------------------------------------
-- Emit forced block return
function ZVM:Dyn_EmitBreak(emitIP)
self:Emit("VM.TMR = VM.TMR + "..self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + "..self.PrecompileBytes)
self:Emit("VM.TMR = VM.TMR + %d",self.PrecompileInstruction)
self:Emit("VM.CODEBYTES = VM.CODEBYTES + %d",self.PrecompileBytes)
if emitIP then
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
end
if self.ExtraEmitFunction then self.ExtraEmitFunction(self) end
self:Emit("if true then return end")
Expand Down Expand Up @@ -265,9 +265,9 @@ end
-- Emit interrupt call
function ZVM:Dyn_EmitInterrupt(intNo,intParam)
self:Dyn_EmitState()
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Dyn_Emit("VM:Interrupt("..intNo..","..intParam..")")
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Dyn_Emit("VM:Interrupt(%d,%d)",intNo,intParam)
self:Dyn_EmitBreak()
end

Expand All @@ -280,23 +280,23 @@ function ZVM:Dyn_EmitInterruptCheck()
if self.RQCAP == 1 then
self:Emit("if VM.MEMRQ > 0 then") -- Extended memory request
self:Emit("if VM.MEMRQ == 1 then") -- Delayed request
self:Emit("VM.IP = "..self.PrecompileStartIP)
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = %d",self.PrecompileStartIP)
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IDLE = 1")
self:Dyn_EmitState(true)
self:Dyn_EmitBreak()
self:Emit("elseif VM.MEMRQ == 2 then") -- Reading
self:Dyn_EmitState(true)
self:Emit("VM.MEMRQ = 4")
self:Emit("VM.IP = "..self.PrecompileStartIP)
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = %d",self.PrecompileStartIP)
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM:Interrupt(28,VM.LADD)")
self:Dyn_EmitBreak()
self:Emit("elseif VM.MEMRQ == 3 then") -- Writing
self:Dyn_EmitState(true)
self:Emit("VM.MEMRQ = 5")
self:Emit("VM.IP = "..self.PrecompileStartIP)
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = %d",self.PrecompileStartIP)
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self:Emit("VM:Interrupt(29,VM.LADD)")
self:Dyn_EmitBreak()
self:Emit("end")
Expand Down Expand Up @@ -406,12 +406,12 @@ function ZVM:Precompile_Step()

-- Check if we crossed the page boundary, if so - repeat the check
if math.floor(self.PrecompileXEIP / 128) ~= self.PrecompilePreviousPage then
self:Emit("VM:SetCurrentPage("..math.floor(self.PrecompileXEIP/128)..")")
self:Emit("VM:SetCurrentPage(%d)",math.floor(self.PrecompileXEIP/128))
self:Emit("if (VM.PCAP == 1) and (VM.CurrentPage.Execute == 0) and")
self:Emit(" (VM.PreviousPage.RunLevel ~= 0) then")
self:Dyn_EmitInterrupt("14",self.PrecompileIP)
self:Emit("end")
self:Emit("VM:SetPreviousPage("..math.floor(self.PrecompileXEIP/128)..")")
self:Emit("VM:SetPreviousPage(%d)",math.floor(self.PrecompileXEIP/128))

self.PrecompilePreviousPage = math.floor(self.PrecompileXEIP / 128)
end
Expand Down Expand Up @@ -442,7 +442,7 @@ function ZVM:Precompile_Step()

-- Check opcode runlevel
if self.OpcodeRunLevel[Opcode] then
self:Emit("if (VM.PCAP == 1) and (VM.CurrentPage.RunLevel > "..self.OpcodeRunLevel[Opcode]..") then")
self:Emit("if (VM.PCAP == 1) and (VM.CurrentPage.RunLevel > %d) then",self.OpcodeRunLevel[Opcode])
self:Dyn_EmitInterrupt("13",Opcode)
self:Emit("end")
end
Expand Down Expand Up @@ -536,8 +536,8 @@ function ZVM:Precompile_Step()

-- Emit interrupt check prefix
if self.EmitNeedInterruptCheck then
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
end

-- Emit opcode
Expand Down Expand Up @@ -600,8 +600,8 @@ function ZVM:Step(overrideSteps,extraEmitFunction)
local instruction = 1
while (instruction <= overrideSteps) and self:Precompile_Step() do
if self.ExtraEmitFunction then
self:Emit("VM.IP = "..(self.PrecompileIP or 0))
self:Emit("VM.XEIP = "..(self.PrecompileTrueXEIP or 0))
self:Emit("VM.IP = %d",(self.PrecompileIP or 0))
self:Emit("VM.XEIP = %d",(self.PrecompileTrueXEIP or 0))
self.ExtraEmitFunction(self)
end
instruction = instruction + 1
Expand Down
Loading

0 comments on commit f79d6b8

Please sign in to comment.