Skip to content

Commit 3b021e0

Browse files
E2Helper & Text Editor Module Support (#63)
* Support for E2Helper & Text Editor Modules * Use the right name for the instruction hook * Use Editor.Location instead of editor name+chip * Change HCOMP to use editor's file location * Don't try to update highlights in wrong editor.
1 parent cb94feb commit 3b021e0

File tree

7 files changed

+176
-22
lines changed

7 files changed

+176
-22
lines changed

lua/wire/client/hlzasm/hc_compiler.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ function HCOMP:SetSpecialLabels()
670670
self:SetLabel("__DATE_MINUTE__",tonumber(os.date("%M")))
671671
self:SetLabel("__DATE_SECOND__",tonumber(os.date("%S")))
672672

673-
if self.Settings.CurrentPlatform == "GPU" then
673+
if string.match(self.Settings.CurrentPlatform, "GPU$") then
674674
self:SetLabel("regClk", 65535)
675675
self:SetLabel("regReset", 65534)
676676
self:SetLabel("regHWClear", 65533)

lua/wire/client/hlzasm/hc_preprocess.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
--------------------------------------------------------------------------------
1010
-- Load file
1111
function HCOMP:LoadFile(filename)
12-
return file.Read("data/"..self.Settings.CurrentPlatform.."Chip/"..filename, "GAME") -- So we also get /addons/wire/data/
12+
return file.Read("data/"..self.Location.."/"..filename, "GAME") -- So we also get /addons/wire/data/
1313
end
1414

1515
-- Save file
1616
function HCOMP:SaveFile(filename,text)
17-
file.Write(self.Settings.CurrentPlatform.."Chip/"..filename,text)
17+
file.Write(self.Location.."/"..filename,text)
1818
end
1919

2020
-- Trim spaces at string sides

lua/wire/client/text_editor/modes/zcpu.lua

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ local string_gmatch = string.gmatch
33
local string_gsub = string.gsub
44
local draw_WordBox = draw.WordBox
55

6-
local EDITOR = {}
6+
local EDITOR = {
7+
UseValidator = true,
8+
Validator = CPULib.Validate,
9+
E2HelperCategory = "ZASM", -- As an override, makes GPU and SPU use the ZCPU helper too
10+
}
711

812
-- CPU hint box
913
local oldpos, haschecked = {0,0}, false
@@ -151,7 +155,11 @@ end
151155

152156
function EDITOR:ShowContextHelp(word)
153157
E2Helper.Show()
154-
E2Helper.UseCPU(self:GetParent().EditorType)
158+
if E2Helper.Modes then
159+
E2Helper:SetMode("ZASM")
160+
else
161+
E2Helper.UseCPU(self:GetParent().EditorType)
162+
end
155163
E2Helper.Show(word)
156164
end
157165

@@ -362,3 +370,16 @@ function EDITOR:Paint()
362370
end
363371

364372
WireTextEditor.Modes.ZCPU = EDITOR
373+
WireTextEditor.Modes.ZGPU = EDITOR
374+
local ZSPU = {
375+
UseSoundBrowser = true
376+
}
377+
378+
-- Proxy everything else from the ZCPU editor.
379+
local ZSPU_Meta = {
380+
__index = function(self,key) return EDITOR[key] end
381+
}
382+
383+
setmetatable(ZSPU,ZSPU_Meta)
384+
385+
WireTextEditor.Modes.ZSPU = ZSPU

lua/wire/cpulib.lua

Lines changed: 141 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if CLIENT or TESTING then
4545

4646
------------------------------------------------------------------------------
4747
-- Request compiling specific sourcecode
48-
function CPULib.Compile(source,fileName,successCallback,errorCallback,targetPlatform)
48+
function CPULib.Compile(source,fileName,successCallback,errorCallback,targetPlatform,sourceDirectory)
4949
-- Stop any compile/upload process that is running right now
5050
timer.Remove("cpulib_compile")
5151
timer.Remove("cpulib_upload")
@@ -72,6 +72,7 @@ if CLIENT or TESTING then
7272

7373
-- Start compiling the sourcecode
7474
HCOMP.Settings.CurrentPlatform = targetPlatform or "CPU"
75+
HCOMP.Location = sourceDirectory or HCOMP.Settings.CurrentPlatform .. "chip"
7576
print("=== HL-ZASM High Level Assembly Compiler Output ==")
7677

7778
-- Initialize callbacks
@@ -88,10 +89,11 @@ if CLIENT or TESTING then
8889
function CPULib.SelectTab(editor,fileName,dontForceChange)
8990
if not editor then return end
9091
local editorType = string.lower(editor.EditorType)
91-
local fullFileName = editorType.."chip\\"..fileName
92-
93-
if string.sub(fileName,1,7) == editorType.."chip" then
92+
local fullFileName
93+
if string.match(fileName,"^"..editor.Location) then
9494
fullFileName = fileName
95+
else
96+
fullFileName = editor.Location.."/"..fileName
9597
end
9698

9799
local currentTab = editor:GetActiveTabIndex()
@@ -143,7 +145,7 @@ if CLIENT or TESTING then
143145
end
144146

145147
editor.C.Val:Update({{message = issue, line = line, char = char}}, nil, issue, Color(128, 20, 50))
146-
end,editor.EditorType)
148+
end,editor.EditorType,editor.Location)
147149
end
148150

149151
------------------------------------------------------------------------------
@@ -372,8 +374,12 @@ if CLIENT or TESTING then
372374
if ZCPU_Editor then
373375
-- Highlight current position
374376
local currentPosition = CPULib.Debugger.PositionByPointer[CPULib.Debugger.Variables[CPULib.Debugger.PreferredTracker or "IP"]]
375-
376377
if currentPosition then
378+
-- Check if currentposition is even in the correct editor first, when switching editors CPULib's debugging info switches
379+
-- to the file in the new editor.
380+
if not currentPosition.File:sub(1,9):match("cpuchip/") then
381+
return
382+
end
377383
-- Clear all highlighted lines
378384
for tab=1,ZCPU_Editor:GetNumTabs() do
379385
ZCPU_Editor:GetEditor(tab):ClearHighlightedLines()
@@ -824,6 +830,128 @@ end
824830
--
825831
-- INT: 48-bit signed integer
826832

833+
if CLIENT then
834+
timer.Simple(1,function()
835+
if E2Helper.Modes then
836+
-- If the version of wiremod is too old for E2Helper modules we should be fine since it's probably
837+
-- the one with the hardcoded ZCPU support, we just miss out on seeing extension stuff.
838+
E2Helper.Modes.ZASM = nil -- Erase existing mode
839+
CPULib.E2HelperMode = E2Helper:RegisterMode("ZASM")
840+
local helperModule = CPULib.E2HelperMode
841+
842+
local helperDesc = helperModule.Descriptions
843+
local helperInst = helperModule.Items
844+
helperModule.ModeSetup = function(panel)
845+
panel.FunctionColumn:SetName("Instruction")
846+
panel.FunctionColumn:SetWidth(126)
847+
panel.FromColumn:SetName("From") -- This can be the extension name now
848+
panel.FromColumn:SetWidth(80)
849+
panel.TakesColumn:SetName("Takes")
850+
panel.TakesColumn:SetWidth(60)
851+
panel.ReturnsColumn:SetName("Platform")
852+
panel.ReturnsColumn:SetWidth(60)
853+
panel.CostColumn:SetName("Opcode")
854+
panel.CostColumn:SetWidth(40)
855+
end
856+
-- Divran's instruction generator from the original e2helper implementation.
857+
local function AddCPUDesc(FuncName, Args, Desc, Platform, Type)
858+
table.insert(helperInst, { [1] = FuncName, [2] = "ZASM", [3] = Args, [4] = Platform, [5] = Type })
859+
helperDesc[FuncName] = Desc
860+
end
861+
862+
-- Add help on all opcodes
863+
for _, instruction in ipairs(CPULib.InstructionTable) do
864+
if (instruction.Mnemonic ~= "RESERVED") and
865+
(not instruction.Obsolete) then
866+
local instructionArgs = instruction.Operand1
867+
if instruction.Operand2 ~= "" then
868+
instructionArgs = instructionArgs .. ", " .. instruction.Operand2
869+
end
870+
871+
AddCPUDesc(instruction.Mnemonic,
872+
instructionArgs,
873+
instruction.Reference,
874+
instruction.Set,
875+
instruction.Opcode)
876+
end
877+
end
878+
-- Create and destroy hooks for extended instruction support.
879+
local function helperCreateInstructionHook(indexes)
880+
for _,k in ipairs(indexes) do
881+
local instr = CPULib.InstructionTable[k]
882+
local instructionArgs = instr.Operand1
883+
if instr.Operand2 ~= "" then
884+
instructionArgs = instructionArgs .. ", " .. instr.Operand2
885+
end
886+
table.insert(helperInst,{
887+
[1] = instr.Mnemonic,
888+
[2] = instr.Extension,
889+
[3] = instructionArgs,
890+
[4] = instr.Set,
891+
[5] = instr.Opcode
892+
})
893+
helperDesc[instr.Mnemonic] = instr.Reference
894+
end
895+
end
896+
local function helperDestroyInstructionHook(indexes)
897+
for _,k in ipairs(indexes) do
898+
helperDesc[helperInst[k][1]] = nil
899+
helperInst[k] = nil
900+
end
901+
end
902+
table.insert(CPULib.CreateInstructionHooks,helperCreateInstructionHook)
903+
table.insert(CPULib.RemoveInstructionHooks,helperDestroyInstructionHook)
904+
end
905+
end)
906+
end
907+
908+
function CPULib.SetupEditor(editor,title,location,name)
909+
-- Modes support removes the original handling for the debug buttons, which are added back here.
910+
-- These were also moved a bit to make them not overlap the sound browser on the spu
911+
if E2Helper.Modes then
912+
-- Add "step forward" button
913+
local DebugForward = editor:addComponent(vgui.Create("Button", editor), -366, 31, -286, 20)
914+
DebugForward:SetText("Step Forward")
915+
DebugForward.Font = "E2SmallFont"
916+
DebugForward.DoClick = function()
917+
local currentPosition = CPULib.Debugger.PositionByPointer[CPULib.Debugger.Variables.IP]
918+
if currentPosition then
919+
local linePointers = CPULib.Debugger.PointersByLine[currentPosition.Line .. ":" .. currentPosition.File]
920+
if linePointers then -- Run till end of line
921+
RunConsoleCommand("wire_cpulib_debugstep", linePointers[2])
922+
else -- Run just once
923+
RunConsoleCommand("wire_cpulib_debugstep")
924+
end
925+
else -- Run just once
926+
RunConsoleCommand("wire_cpulib_debugstep")
927+
end
928+
-- Reset interrupt text
929+
CPULib.InterruptText = nil
930+
end
931+
editor.C.DebugForward = DebugForward
932+
933+
-- Add "reset" button
934+
local DebugReset = editor:addComponent(vgui.Create("Button", editor), -406, 31, -366, 20)
935+
DebugReset:SetText("Reset")
936+
DebugReset.DoClick = function()
937+
RunConsoleCommand("wire_cpulib_debugreset")
938+
-- Reset interrupt text
939+
CPULib.InterruptText = nil
940+
end
941+
editor.C.DebugReset = DebugReset
942+
943+
-- Add "run" button
944+
local DebugRun = editor:addComponent(vgui.Create("Button", editor), -441, 31, -406, 20)
945+
DebugRun:SetText("Run")
946+
DebugRun.DoClick = function() RunConsoleCommand("wire_cpulib_debugrun") end
947+
editor.C.DebugRun = DebugRun
948+
editor:Setup(title,location,"Z"..name)
949+
else
950+
editor:Setup(title,location,name)
951+
-- ZCPU will be fine without the new version, because the old version is hardcoded to support us.
952+
end
953+
end
954+
827955
CPULib.InstructionTable = {}
828956
local W1,R0,OB,UB,CB,TR,OL,BL,PR = 1,2,4,8,16,32,64,128,256
829957

@@ -849,6 +977,7 @@ local function Entry(Set,Opc,Mnemonic,Ops,Version,Flags,Op1,Op2,Reference)
849977
BlockPrefix = Bit(Flags,BL),
850978
PrivilegedRequester = Bit(Flags,PR)
851979
})
980+
852981
end
853982
local function CPU(...) Entry("CPU",...) end
854983
local function GPU(...) Entry("GPU",...) end
@@ -921,7 +1050,8 @@ function CPULib:RegisterExtension(name, extension)
9211050
OpFunc = opfunc,
9221051
Flags = flags,
9231052
Op1Name = "",
924-
Op2Name = ""
1053+
Op2Name = "",
1054+
Description = docs.Description
9251055
}
9261056
if not docs then
9271057
instruction.Description = "Instruction "..name.." created by extension "..self.Name
@@ -1002,7 +1132,10 @@ function CPULib:RebuildExtendedInstructions()
10021132
for _,extension in ipairs(platform) do
10031133
for _,i in ipairs(self.Extensions[platname][extension].Instructions) do
10041134
Entry(platname,curOpcode,i.Name,i.Operands,i.Version,self:ParseFlagArray(i.Flags),i.Op1Name,i.Op2Name,i.Description)
1005-
table.insert(self.ExtendedInstructions,#CPULib.InstructionTable)
1135+
-- Store extension name on the instruction
1136+
local lastIndex = #CPULib.InstructionTable
1137+
CPULib.InstructionTable[lastIndex].Extension = extension
1138+
table.insert(self.ExtendedInstructions,lastIndex)
10061139
curOpcode = curOpcode - 1
10071140
end
10081141
end

lua/wire/stools/cpu.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ if CLIENT then
112112
------------------------------------------------------------------------------
113113
function ZCPU_RequestCode()
114114
if ZCPU_Editor then
115-
CPULib.Compile(ZCPU_Editor:GetCode(),ZCPU_Editor:GetChosenFile(),compile_success,compile_error)
115+
CPULib.Compile(ZCPU_Editor:GetCode(),ZCPU_Editor:GetChosenFile(),compile_success,compile_error,"CPU",ZCPU_Editor.Location)
116116
end
117117
end
118118
net.Receive("ZCPU_RequestCode", ZCPU_RequestCode)
@@ -123,7 +123,7 @@ if CLIENT then
123123
function ZCPU_OpenEditor()
124124
if not ZCPU_Editor then
125125
ZCPU_Editor = vgui.Create("Expression2EditorFrame")
126-
ZCPU_Editor:Setup("ZCPU Editor", "cpuchip", "CPU")
126+
CPULib.SetupEditor(ZCPU_Editor,"ZCPU Editor", "cpuchip", "CPU")
127127
end
128128
ZCPU_Editor:Open()
129129
end
@@ -148,7 +148,7 @@ if CLIENT then
148148
function FileBrowser:OnFileOpen(filepath, newtab)
149149
if not ZCPU_Editor then
150150
ZCPU_Editor = vgui.Create("Expression2EditorFrame")
151-
ZCPU_Editor:Setup("ZCPU Editor", "cpuchip", "CPU")
151+
CPULib.SetupEditor(ZCPU_Editor,"ZCPU Editor", "cpuchip", "CPU")
152152
end
153153
ZCPU_Editor:Open(filepath, nil, newtab)
154154
end

lua/wire/stools/gpu.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ if CLIENT then
9191
function ZGPU_RequestCode()
9292
if ZGPU_Editor then
9393
CPULib.Debugger.SourceTab = ZGPU_Editor:GetActiveTab()
94-
CPULib.Compile(ZGPU_Editor:GetCode(),ZGPU_Editor:GetChosenFile(),compile_success,compile_error,"GPU")
94+
CPULib.Compile(ZGPU_Editor:GetCode(),ZGPU_Editor:GetChosenFile(),compile_success,compile_error,"GPU",ZGPU_Editor.Location)
9595
end
9696
end
9797
net.Receive("ZGPU_RequestCode", ZGPU_RequestCode)
@@ -102,7 +102,7 @@ if CLIENT then
102102
function ZGPU_OpenEditor()
103103
if not ZGPU_Editor then
104104
ZGPU_Editor = vgui.Create("Expression2EditorFrame")
105-
ZGPU_Editor:Setup("ZGPU Editor", "gpuchip", "GPU")
105+
CPULib.SetupEditor(ZGPU_Editor,"ZGPU Editor", "gpuchip", "GPU")
106106
end
107107
ZGPU_Editor:Open()
108108
end
@@ -127,7 +127,7 @@ if CLIENT then
127127
function FileBrowser:OnFileOpen(filepath, newtab)
128128
if not ZGPU_Editor then
129129
ZGPU_Editor = vgui.Create("Expression2EditorFrame")
130-
ZGPU_Editor:Setup("ZGPU Editor", "gpuchip", "GPU")
130+
CPULib.SetupEditor(ZGPU_Editor,"ZGPU Editor", "gpuchip", "GPU")
131131
end
132132
ZGPU_Editor:Open(filepath, nil, newtab)
133133
end

lua/wire/stools/spu.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ if CLIENT then
8989
function ZSPU_RequestCode()
9090
if ZSPU_Editor then
9191
CPULib.Debugger.SourceTab = ZSPU_Editor:GetActiveTab()
92-
CPULib.Compile(ZSPU_Editor:GetCode(),ZSPU_Editor:GetChosenFile(),compile_success,compile_error)
92+
CPULib.Compile(ZSPU_Editor:GetCode(),ZSPU_Editor:GetChosenFile(),compile_success,compile_error,"SPU",ZSPU_Editor.Location)
9393
end
9494
end
9595
net.Receive("ZSPU_RequestCode", ZSPU_RequestCode)
@@ -100,7 +100,7 @@ if CLIENT then
100100
function ZSPU_OpenEditor()
101101
if not ZSPU_Editor then
102102
ZSPU_Editor = vgui.Create("Expression2EditorFrame")
103-
ZSPU_Editor:Setup("ZSPU Editor", "spuchip", "SPU")
103+
CPULib.SetupEditor(ZSPU_Editor,"ZSPU Editor", "spuchip", "SPU")
104104
end
105105
ZSPU_Editor:Open()
106106
end
@@ -132,7 +132,7 @@ if CLIENT then
132132
function FileBrowser:OnFileOpen(filepath, newtab)
133133
if not ZSPU_Editor then
134134
ZSPU_Editor = vgui.Create("Expression2EditorFrame")
135-
ZSPU_Editor:Setup("ZSPU Editor", "spuchip", "SPU")
135+
CPULib.SetupEditor(ZSPU_Editor,"ZSPU Editor", "spuchip", "SPU")
136136
end
137137
ZSPU_Editor:Open(filepath, nil, newtab)
138138
end

0 commit comments

Comments
 (0)