From b7da7e7ccd688a6b808dfd239f5a034f8168ab25 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Mon, 15 Jul 2024 14:40:49 -0500 Subject: [PATCH 1/4] Custom ram/rom memory model --- lua/entities/gmod_wire_cpu.lua | 7 ++++++- lua/wire/stools/cpu.lua | 30 ++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lua/entities/gmod_wire_cpu.lua b/lua/entities/gmod_wire_cpu.lua index 853d1fa..b9b0b26 100644 --- a/lua/entities/gmod_wire_cpu.lua +++ b/lua/entities/gmod_wire_cpu.lua @@ -140,7 +140,12 @@ local memoryModels = { ["flat"] = { 0, 0 }, } -function ENT:SetMemoryModel(model) +function ENT:SetMemoryModel(model,cram,crom) + if model == "custom" then + self.VM.RAMSize = (tonumber(cram) or 512)*128 -- 65536 + self.VM.ROMSize = (tonumber(crom) or 512)*128 -- 65536 + return + end self.VM.RAMSize = memoryModels[model][1] or 65536 self.VM.ROMSize = memoryModels[model][2] or 65536 end diff --git a/lua/wire/stools/cpu.lua b/lua/wire/stools/cpu.lua index 2d0aa7e..118c89f 100644 --- a/lua/wire/stools/cpu.lua +++ b/lua/wire/stools/cpu.lua @@ -19,7 +19,9 @@ TOOL.ClientConVar = { model = "models/cheeze/wires/cpu.mdl", filename = "", memorymodel = "64krom", - extensions = "" + extensions = "", + customram = 0, + customrom = 0 } if CLIENT then @@ -46,7 +48,7 @@ if SERVER then if player:KeyDown(IN_SPEED) then if (trace.Entity:IsValid()) and (trace.Entity:GetClass() == "gmod_wire_cpu") then - trace.Entity:SetMemoryModel(self:GetClientInfo("memorymodel")) + trace.Entity:SetMemoryModel(self:GetClientInfo("memorymodel"),self:GetClientInfo("customram"),self:GetClientInfo("customrom")) trace.Entity:FlashData({}) net.Start("CPULib.InvalidateDebugger") net.WriteUInt(0,2) net.Send(player) end @@ -76,7 +78,7 @@ if SERVER then end function TOOL:MakeEnt(ply, model, Ang, trace) local ent = WireLib.MakeWireEnt(ply, {Class = self.WireClass, Pos=trace.HitPos, Angle=Ang, Model=model}) - ent:SetMemoryModel(self:GetClientInfo("memorymodel")) + ent:SetMemoryModel(self:GetClientInfo("memorymodel"),self:GetClientInfo("customram"),self:GetClientInfo("customrom")) ent:SetExtensionLoadOrder(self:GetClientInfo("extensions")) self:LeftClick_Update(trace) return ent @@ -181,7 +183,7 @@ if CLIENT then ---------------------------------------------------------------------------- - panel:AddControl("ComboBox", { + local memPanel = panel:AddControl("ComboBox", { Label = "Memory model", Options = { ["128 bytes ROM only"] = {wire_cpu_memorymodel = "128rom"}, @@ -194,9 +196,29 @@ if CLIENT then ["8KB RAM only"] = {wire_cpu_memorymodel = "8k"}, ["128KB RAM/ROM"] = {wire_cpu_memorymodel = "128krom"}, ["No internal RAM/ROM"] = {wire_cpu_memorymodel = "flat"}, + ["Custom RAM/ROM"] = {wire_cpu_memorymodel = "custom"}, } }) panel:AddControl("Label", {Text = "Sets the processor memory model (determines interaction with the external devices)"}) + local customMemPanel = { + panel:AddControl("Label", {Text = "Custom memory size for RAM and ROM is in pages(128 bytes)"}), + panel:AddControl("Slider", {Label = "RAM size", Command = "wire_cpu_customram", Min = 0, Max = 1024}), + panel:AddControl("Slider", {Label = "ROM size", Command = "wire_cpu_customrom", Min = 0, Max = 1024}), + } + local memoryModel = GetConVar("wire_cpu_memorymodel") + function memPanel:OnSelect(index, value, data) + if data.wire_cpu_memorymodel == "custom" then + for _,i in ipairs(customMemPanel) do + i:Show() + end + else + for _,i in ipairs(customMemPanel) do + i:Hide() + end + end + -- Overriding this turns off the automatic convar write, so we have to do it ourselves. + memoryModel:SetString(data.wire_cpu_memorymodel) + end local enabledExtensionOrder = {} local enabledExtensionLookup = {} From 1e1721f4be74b859a6b39a9e6688b2521b487b74 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Mon, 15 Jul 2024 17:48:55 -0500 Subject: [PATCH 2/4] Clamp and floor handling for custom ram/rom --- lua/entities/gmod_wire_cpu.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/entities/gmod_wire_cpu.lua b/lua/entities/gmod_wire_cpu.lua index b9b0b26..16eb1b7 100644 --- a/lua/entities/gmod_wire_cpu.lua +++ b/lua/entities/gmod_wire_cpu.lua @@ -142,8 +142,8 @@ local memoryModels = { function ENT:SetMemoryModel(model,cram,crom) if model == "custom" then - self.VM.RAMSize = (tonumber(cram) or 512)*128 -- 65536 - self.VM.ROMSize = (tonumber(crom) or 512)*128 -- 65536 + self.VM.RAMSize = math.floor(math.Clamp((tonumber(cram) or 512),0,1024))*128 -- 65536 + self.VM.ROMSize = math.floor(math.Clamp((tonumber(crom) or 512),0,1024))*128 -- 65536 return end self.VM.RAMSize = memoryModels[model][1] or 65536 From 7b08c916b681ac41e0de2b37e231dae0942d08b5 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Mon, 15 Jul 2024 17:50:01 -0500 Subject: [PATCH 3/4] Rebuild layout on select & show correct status on first build --- lua/wire/stools/cpu.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/wire/stools/cpu.lua b/lua/wire/stools/cpu.lua index 118c89f..77efaa5 100644 --- a/lua/wire/stools/cpu.lua +++ b/lua/wire/stools/cpu.lua @@ -218,8 +218,11 @@ if CLIENT then end -- Overriding this turns off the automatic convar write, so we have to do it ourselves. memoryModel:SetString(data.wire_cpu_memorymodel) + -- Rebuild the layout to prevent an unsightly gap + panel:InvalidateLayout() + panel:InvalidateChildren(true) end - + memPanel:OnSelect(0,0,{wire_cpu_memorymodel = memoryModel:GetString()}) -- Simulate one on-select to correct the show/hide status local enabledExtensionOrder = {} local enabledExtensionLookup = {} local extensionConvar = GetConVar("wire_cpu_extensions") From 671ddd53495690eb4b5e08d566f96103b407b8e5 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Tue, 16 Jul 2024 09:45:34 -0500 Subject: [PATCH 4/4] Now displays RAM/ROM size in KB and B --- lua/wire/stools/cpu.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lua/wire/stools/cpu.lua b/lua/wire/stools/cpu.lua index 77efaa5..ce79c96 100644 --- a/lua/wire/stools/cpu.lua +++ b/lua/wire/stools/cpu.lua @@ -205,6 +205,20 @@ if CLIENT then panel:AddControl("Slider", {Label = "RAM size", Command = "wire_cpu_customram", Min = 0, Max = 1024}), panel:AddControl("Slider", {Label = "ROM size", Command = "wire_cpu_customrom", Min = 0, Max = 1024}), } + local function formatPageSizeString(pages) + if pages >= 8 then + return string.format("KB %g",pages/8) -- padding with spaces won't help because the font isn't monospaced + else + return string.format(" B %g",pages*128) + end + end + customMemPanel[2].OnValueChanged = function(self,pages) + self:SetText("RAM size "..formatPageSizeString(pages)) + end + customMemPanel[3].OnValueChanged = function(self,pages) + self:SetText("ROM size "..formatPageSizeString(pages)) + end + local memoryModel = GetConVar("wire_cpu_memorymodel") function memPanel:OnSelect(index, value, data) if data.wire_cpu_memorymodel == "custom" then