2008-09-16 17:05:56 +00:00
TOOL.Category = " Wire - Control "
TOOL.Name = " Chip - CPU "
TOOL.Command = nil
TOOL.ConfigName = " "
if ( CLIENT ) then
language.Add ( " Tool_wire_cpu_name " , " CPU Tool (Wire) " )
language.Add ( " Tool_wire_cpu_desc " , " Spawns a central processing unit " )
language.Add ( " Tool_wire_cpu_0 " , " Primary: Create empty CPU / Upload current program to it " ) // ; Secondary : Debug the CPU
language.Add ( " sboxlimit_wire_cpu " , " You've hit CPU limit! " )
language.Add ( " undone_wirecpu " , " Undone the wire CPU " )
2008-06-14 00:55:20 +00:00
end
if ( SERVER ) then
CreateConVar ( ' sbox_maxwire_cpus ' , 20 )
end
2008-09-16 17:05:56 +00:00
TOOL.ClientConVar [ " model " ] = " models/cheeze/wires/cpu.mdl "
TOOL.ClientConVar [ " filename " ] = " "
TOOL.ClientConVar [ " packet_bandwidth " ] = 400
TOOL.ClientConVar [ " packet_rate_sp " ] = 0.05
TOOL.ClientConVar [ " packet_rate_mp " ] = 0.4
TOOL.ClientConVar [ " compile_rate " ] = 0.05
TOOL.ClientConVar [ " compile_bandwidth " ] = 200
TOOL.ClientConVar [ " rom " ] = 1
TOOL.ClientConVar [ " rom_present " ] = 1
TOOL.ClientConVar [ " dump_data " ] = 0
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
cleanup.Register ( " wire_cpus " )
2008-06-14 00:55:20 +00:00
//=============================================================================
2008-09-16 17:05:56 +00:00
if ( SERVER ) then
SourceCode = { }
local function AddSourceLine ( pl , command , args )
SourceCode [ tonumber ( args [ 1 ] ) ] = tostring ( args [ 2 ] )
end
concommand.Add ( " wire_cpu_addsrc " , AddSourceLine )
local function ClearSource ( pl , command , args )
SourceCode = { }
end
concommand.Add ( " wire_cpu_clearsrc " , ClearSource )
end
2008-06-14 00:55:20 +00:00
//=============================================================================
local function CPUStool_Version ( )
2008-09-16 17:05:56 +00:00
local SVNString = " $Revision$ "
local rev = tonumber ( string.sub ( SVNString , 12 , 14 ) )
if ( rev ) then
return rev
else
return 0
end
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
//=============================================================================
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local function CompileProgram_Timer ( tool , firstpass )
if ( firstpass && tool.FirstPassDone ) then return end
if ( ! firstpass && tool.SecondPassDone ) then return end
if ( ! tool : GetOwner ( ) ) then return end
if ( ! tool.LineNumber ) then return end
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local SendLinesMax = tool.LineNumber + tool : GetOwner ( ) : GetInfo ( " wire_cpu_compile_bandwidth " )
2008-06-14 00:55:20 +00:00
if ( SendLinesMax > table.Count ( SourceCode ) ) then SendLinesMax = table.Count ( SourceCode ) end
local Rate = 0
2008-09-16 17:05:56 +00:00
if ( SourceCode [ tostring ( tool.LineNumber ) ] ) then
if ( string.len ( SourceCode [ tostring ( tool.LineNumber ) ] ) > 256 ) then
SendLinesMax = tool.LineNumber
2008-06-14 00:55:20 +00:00
end
end
2008-09-16 17:05:56 +00:00
while ( tool.LineNumber <= SendLinesMax ) and ( tool.CPU_Entity ) do
local line = SourceCode [ tonumber ( tool.LineNumber ) ]
if ( line ) then
2008-06-14 00:55:20 +00:00
if ( string.len ( line ) > 254 ) then
2008-09-16 17:05:56 +00:00
tool : GetOwner ( ) : PrintMessage ( HUD_PRINTCONSOLE , " -> ZyeliosASM: Line " .. tool.LineNumber .. " too long! I compile it, but it may trigger infinite loop thing. \n " )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
if ( tool.CPU_Entity . ParseProgram_ASM ) then
tool.CPU_Entity : ParseProgram_ASM ( line , tool.LineNumber )
2008-06-26 18:35:36 +00:00
end
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
tool.LineNumber = tool.LineNumber + 1
2008-06-14 00:55:20 +00:00
Rate = Rate + 1
end
2008-09-16 17:05:56 +00:00
local TimeLeft = ( table.Count ( SourceCode ) * 2 - tool.LineNumber ) / Rate
if ( not firstpass ) then
TimeLeft = ( table.Count ( SourceCode ) - tool.LineNumber ) / Rate
end
tool.PrevRate = ( tool.PrevRate * 1.5 + TimeLeft * 0.5 ) / 2
TimeLeft = math.floor ( tool.PrevRate / 10 )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local TempPercent = ( ( tool.LineNumber - 1 ) / table.Count ( SourceCode ) ) * 100
2008-06-14 00:55:20 +00:00
if ( firstpass ) then
2008-09-16 17:05:56 +00:00
if ( ! tool.FirstPassDone ) then
tool : GetOwner ( ) : ConCommand ( ' wire_cpu_vgui_status "Compiling ( ' .. TimeLeft .. ' seconds left), ' .. tool.LineNumber .. ' lines processed" ' )
tool : GetOwner ( ) : ConCommand ( ' wire_cpu_vgui_progress " ' .. math.floor ( TempPercent / 2 ) .. ' " ' )
2008-06-14 00:55:20 +00:00
end
else
2008-09-16 17:05:56 +00:00
if ( ! tool.SecondPassDone ) then
tool : GetOwner ( ) : ConCommand ( ' wire_cpu_vgui_status "Compiling ( ' .. TimeLeft .. ' seconds left), ' .. tool.LineNumber .. ' lines processed" ' )
tool : GetOwner ( ) : ConCommand ( ' wire_cpu_vgui_progress " ' .. math.floor ( 50 + TempPercent / 2 ) .. ' " ' )
2008-06-14 00:55:20 +00:00
end
end
2008-09-16 17:05:56 +00:00
if ( tool.LineNumber > table.Count ( SourceCode ) ) || ( TempPercent >= 100 ) then
if ( ! tool.FirstPassDone ) then
tool.FirstPassDone = true
tool : Compile_Pass2 ( )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
if ( ! firstpass ) && ( ! tool.SecondPassDone ) then
tool.SecondPassDone = true
tool : Compile_End ( )
2008-06-14 00:55:20 +00:00
end
end
2008-09-16 17:05:56 +00:00
if ( tool.CPU_Entity . FatalError == true ) then
timer.Destroy ( " CPUCompileTimer1 " )
timer.Destroy ( " CPUCompileTimer2 " )
tool : Compile_End ( )
end
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
//=============================================================================
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
function TOOL : StartCompile ( pl )
local ent = self.CPU_Entity
if table.Count ( SourceCode ) == 0 then return end
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
pl : PrintMessage ( HUD_PRINTCONSOLE , " ----> ZyeliosASM compiler - Version 2.0 (SVN REV " .. CPUStool_Version ( ) .. " / " .. ent : CPUID_Version ( ) .. " ) <---- \n " )
pl : PrintMessage ( HUD_PRINTCONSOLE , " -> ZyeliosASM: Compiling... \n " )
2008-06-14 00:55:20 +00:00
pl : ConCommand ( ' wire_cpu_vgui_open ' )
pl : ConCommand ( ' wire_cpu_vgui_title "ZyeliosASM - Compiling" ' )
pl : ConCommand ( ' wire_cpu_vgui_status "Initializing" ' )
pl : ConCommand ( ' wire_cpu_vgui_progress "0" ' )
2008-09-16 17:05:56 +00:00
if ( self : GetClientInfo ( " userom " ) == " 1 " ) then ent.UseROM = true
else ent.UseROM = false
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
if ( self : GetClientInfo ( " dump_data " ) == " 1 " ) then
2008-06-14 00:55:20 +00:00
ent.MakeDump = true
ent.Dump = " Code listing: \n "
else
ent.MakeDump = false
end
2008-09-16 17:05:56 +00:00
[ADDED] NMIINT opcode (similar to inputting value into NMI input)
[ADDED] IDLE opcode (makes CPU skip some cycles, causes less lag, but also a small delay)
[ADDED] Better debugger. It shows code flow now. To enable it, change line 16 of init.lua
[FIXED] Critical bug with DB compiler macro
[FIXED] Faster compile
[FIXED] One-byte opcodes not precompiled
[CHANGED] Internal change, PF and IF are now values rather than booleans
[CHANGED] Interrupts #0 and #1 will not be handled by interrupt handler if interrupt #1 has property magic value set to "96"
[FIXED] CPU ignoring IF flag
[CHANGED] STI now does not affect next opcode (interrupts will be enabled after next executed opcode, not right after STI was executed)
[ADDED] Missing bitwise operations (BXOR, BOR, BAND, BSHL, BSHR). Still missing BNOT.
[ADDED] Missing TBIT opcode
[FIXED] SBIT, CBIT, BIT operations now use new bitwise logic
[FIXED] XCHG opcode now works (I think)
[FIXED] NMIRET now pops 2 values off stack, to simulate IRET opcode behaviour
[FIXED] Limited input value to 32..255 range on NMI input
[FIXED] CPU internal VM state was not completly reset inbetween ZASM compiles
2008-07-02 21:01:29 +00:00
2008-06-14 00:55:20 +00:00
self.FirstPassDone = false
self.SecondPassDone = false
timer.Destroy ( " CPUCompileTimer1 " )
timer.Destroy ( " CPUCompileTimer2 " )
2008-09-16 17:05:56 +00:00
ent : Compiler_Stage0 ( pl )
2008-06-14 00:55:20 +00:00
self : Compile_Pass1 ( )
end
function TOOL : Compile_Pass1 ( )
2008-09-16 17:05:56 +00:00
if ( ! self : GetOwner ( ) ) then return end
2008-06-14 00:55:20 +00:00
self : GetOwner ( ) : PrintMessage ( HUD_PRINTCONSOLE , " -> ZyeliosASM: Pass 1 \n " )
self.Compiling = true
2008-09-16 17:05:56 +00:00
self.CPU_Entity : Compiler_Stage1 ( )
2008-06-14 00:55:20 +00:00
self.LineNumber = 1
self.PrevRate = 0
2008-09-16 17:05:56 +00:00
timer.Create ( " CPUCompileTimer1 " , self : GetOwner ( ) : GetInfo ( " wire_cpu_compile_rate " ) , 0 , CompileProgram_Timer , self , true )
2008-06-14 00:55:20 +00:00
end
function TOOL : Compile_Pass2 ( )
2008-09-16 17:05:56 +00:00
if ( ! self : GetOwner ( ) ) then return end
2008-06-14 00:55:20 +00:00
self : GetOwner ( ) : PrintMessage ( HUD_PRINTCONSOLE , " -> ZyeliosASM: Pass 2 \n " )
self.Compiling = true
2008-09-16 17:05:56 +00:00
self.CPU_Entity : Compiler_Stage2 ( )
2008-06-14 00:55:20 +00:00
self.LineNumber = 1
2008-09-16 17:05:56 +00:00
timer.Create ( " CPUCompileTimer2 " , self : GetOwner ( ) : GetInfo ( " wire_cpu_compile_rate " ) , 0 , CompileProgram_Timer , self , false )
2008-06-14 00:55:20 +00:00
end
function TOOL : Compile_End ( )
local pl = self : GetOwner ( )
2008-09-16 17:05:56 +00:00
local ent = self.CPU_Entity
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
if ( ent.FatalError ) then pl : PrintMessage ( HUD_PRINTCONSOLE , " -> ZyeliosASM: Compile aborted: fatal error has occured \n " )
else pl : PrintMessage ( HUD_PRINTCONSOLE , " -> ZyeliosASM: Compile succeded! " .. ( table.Count ( SourceCode ) - 1 ) .. " lines, " .. ent.WIP .. " bytes, " .. table.Count ( ent.Labels ) .. " definitions. \n " )
2008-06-14 00:55:20 +00:00
end
pl : ConCommand ( ' wire_cpu_vgui_close ' )
2008-09-16 17:05:56 +00:00
if ( self : GetClientInfo ( " dump_data " ) == " 1 " ) then
2008-06-14 00:55:20 +00:00
pl : PrintMessage ( HUD_PRINTCONSOLE , " ZyeliosASM: Dumping data \n " )
local codedump = " Count: " .. ent.WIP .. " \n "
2008-09-16 17:05:56 +00:00
// local pointerdump = " Count: " .. table.Count ( ent.Labels ) .. " \n "
2008-06-14 00:55:20 +00:00
for i = 0 , ent.WIP do
2008-06-26 18:35:36 +00:00
if ( ent.Memory [ i ] ) then
codedump = codedump .. " [ " .. i .. " ] " .. " = " .. ent.Memory [ i ] .. " \n "
end
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
/* for k , v in pairs ( ent.Labels ) do
2008-06-14 00:55:20 +00:00
pointerdump = pointerdump .. " #pointer " .. k .. " " .. v .. " \n "
2008-09-16 17:05:56 +00:00
end */
2008-06-14 00:55:20 +00:00
file.Write ( " cdump.txt " , codedump )
file.Write ( " ldump.txt " , ent.Dump )
2008-09-16 17:05:56 +00:00
// file.Write ( " pdump.txt " , pointerdump )
2008-06-14 00:55:20 +00:00
pl : PrintMessage ( HUD_PRINTCONSOLE , " ZyeliosASM: Dumped! \n " )
end
ent : Reset ( )
ent.Compiling = false
end
//=============================================================================
2008-09-16 17:05:56 +00:00
function TOOL : LeftClick ( trace )
2008-06-14 00:55:20 +00:00
if trace.Entity : IsPlayer ( ) then return false end
2008-09-16 17:05:56 +00:00
if ( CLIENT ) then return true end
2008-06-14 00:55:20 +00:00
local ply = self : GetOwner ( )
2008-09-16 17:05:56 +00:00
self.CPU_Entity = trace.Entity
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
if ( trace.Entity : IsValid ( ) && trace.Entity : GetClass ( ) == " gmod_wire_cpu " && trace.Entity . pl == ply ) then
self : StartCompile ( ply )
2008-06-14 00:55:20 +00:00
return true
end
2008-09-16 17:05:56 +00:00
if ( ! self : GetSWEP ( ) : CheckLimit ( " wire_cpus " ) ) then return false end
if ( not util.IsValidModel ( self : GetClientInfo ( " model " ) ) ) then return false end
if ( not util.IsValidProp ( self : GetClientInfo ( " model " ) ) ) then return false end
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local ang = trace.HitNormal : Angle ( )
local model = self : GetClientInfo ( " model " )
ang.pitch = ang.pitch + 90
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
wire_cpu = MakeWireCpu ( ply , ang , trace.HitPos , model )
2008-06-14 00:55:20 +00:00
local min = wire_cpu : OBBMins ( )
2008-09-16 17:05:56 +00:00
wire_cpu : SetPos ( trace.HitPos - trace.HitNormal * min.z )
2008-06-14 00:55:20 +00:00
local const = WireLib.Weld ( wire_cpu , trace.Entity , trace.PhysicsBone , true )
undo.Create ( " WireCpu " )
2008-09-16 17:05:56 +00:00
undo.AddEntity ( wire_cpu )
undo.AddEntity ( const )
undo.SetPlayer ( ply )
2008-06-14 00:55:20 +00:00
undo.Finish ( )
2008-09-16 17:05:56 +00:00
ply : AddCleanup ( " wire_cpus " , wire_cpu )
ply : AddCleanup ( " wire_cpus " , const )
2008-06-14 00:55:20 +00:00
return true
end
if ( SERVER ) then
2008-09-16 17:05:56 +00:00
function MakeWireCpu ( pl , ang , pos , model )
if ( ! pl : CheckLimit ( " wire_cpus " ) ) then return false end
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local wire_cpu = ents.Create ( " gmod_wire_cpu " )
2008-06-14 00:55:20 +00:00
if ( ! wire_cpu : IsValid ( ) ) then return false end
2008-09-16 17:05:56 +00:00
wire_cpu : SetModel ( model )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
wire_cpu : SetAngles ( ang )
wire_cpu : SetPos ( pos )
2008-06-14 00:55:20 +00:00
wire_cpu : Spawn ( )
2008-09-16 17:05:56 +00:00
2008-06-14 00:55:20 +00:00
wire_cpu : SetPlayer ( pl )
2008-09-16 17:05:56 +00:00
2008-06-14 00:55:20 +00:00
local ttable = {
pl = pl ,
2008-09-16 17:05:56 +00:00
model = model ,
2008-06-14 00:55:20 +00:00
}
2008-09-16 17:05:56 +00:00
table.Merge ( wire_cpu : GetTable ( ) , ttable )
pl : AddCount ( " wire_cpus " , wire_cpu )
2008-06-14 00:55:20 +00:00
return wire_cpu
end
2008-09-16 17:05:56 +00:00
duplicator.RegisterEntityClass ( " gmod_wire_cpu " , MakeWireCpu , " ang " , " pos " , " model " )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
function TOOL : UpdateGhostWireCpu ( ent , player )
if ( ! ent ) then return end
if ( ! ent : IsValid ( ) ) then return end
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local tr = utilx.GetPlayerTrace ( player , player : GetCursorAimVector ( ) )
local trace = util.TraceLine ( tr )
2008-06-14 00:55:20 +00:00
if ( ! trace.Hit ) then return end
if ( trace.Entity && trace.Entity : GetClass ( ) == " gmod_wire_cpu " || trace.Entity : IsPlayer ( ) ) then
2008-09-16 17:05:56 +00:00
ent : SetNoDraw ( true )
2008-06-14 00:55:20 +00:00
return
end
2008-09-16 17:05:56 +00:00
local ang = trace.HitNormal : Angle ( )
ang.pitch = ang.pitch + 90
2008-06-14 00:55:20 +00:00
local min = ent : OBBMins ( )
2008-09-16 17:05:56 +00:00
ent : SetPos ( trace.HitPos - trace.HitNormal * min.z )
ent : SetAngles ( ang )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
ent : SetNoDraw ( false )
2008-06-14 00:55:20 +00:00
end
function TOOL : Think ( )
2008-09-16 17:05:56 +00:00
if ( ! self.GhostEntity || ! self.GhostEntity : IsValid ( ) || self.GhostEntity : GetModel ( ) ! = self : GetClientInfo ( " model " ) || ( not self.GhostEntity : GetModel ( ) ) ) then
self : MakeGhostEntity ( self : GetClientInfo ( " model " ) , Vector ( 0 , 0 , 0 ) , Angle ( 0 , 0 , 0 ) )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
self : UpdateGhostWireCpu ( self.GhostEntity , self : GetOwner ( ) )
2008-06-14 00:55:20 +00:00
end
//=============================================================================
// Code sending
//=============================================================================
if ( CLIENT ) then
local Frame
local StatusLabel
local PLabel
local ProgressBar
local BGBar
2008-09-16 17:05:56 +00:00
local function VGUI_Open ( pl , command , args )
2008-06-14 00:55:20 +00:00
if ( Frame ) then
Frame : SetVisible ( false )
end
Frame = vgui.Create ( " Panel " )
Frame : SetSize ( 400 , 50 )
Frame : SetPos ( 150 , 150 )
Frame : SetVisible ( true )
BGBar = vgui.Create ( " ProgressBar " , Frame )
BGBar : SetVisible ( true )
BGBar : SetSize ( 400 , 100 )
BGBar : SetPos ( 0 , 0 )
StatusLabel = vgui.Create ( " Label " , Frame )
StatusLabel : SetSize ( 380 , 30 )
StatusLabel : SetPos ( 10 , 10 )
StatusLabel : SetVisible ( true )
PLabel = vgui.Create ( " Label " , Frame )
PLabel : SetSize ( 30 , 30 )
PLabel : SetPos ( 360 , 10 )
PLabel : SetVisible ( true )
ProgressBar = vgui.Create ( " ProgressBar " , Frame )
ProgressBar : SetSize ( 280 , 30 )
ProgressBar : SetPos ( 10 , 60 )
ProgressBar : SetVisible ( false )
end
2008-09-16 17:05:56 +00:00
concommand.Add ( " wire_cpu_vgui_open " , VGUI_Open )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local function VGUI_Close ( pl , command , args )
Frame : SetVisible ( false ) ;
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
concommand.Add ( " wire_cpu_vgui_close " , VGUI_Close )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local function VGUI_Title ( pl , command , args )
2008-06-14 00:55:20 +00:00
Frame : PostMessage ( " SetTitle " , " text " , args [ 1 ] ) ;
end
2008-09-16 17:05:56 +00:00
concommand.Add ( " wire_cpu_vgui_title " , VGUI_Title )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local function VGUI_Status ( pl , command , args )
2008-06-14 00:55:20 +00:00
StatusLabel : PostMessage ( " SetText " , " text " , args [ 1 ] ) ;
end
2008-09-16 17:05:56 +00:00
concommand.Add ( " wire_cpu_vgui_status " , VGUI_Status )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local function VGUI_Progress ( pl , command , args )
2008-06-14 00:55:20 +00:00
if ( args [ 1 ] ) then
2008-09-16 17:05:56 +00:00
ProgressBar : PostMessage ( " SetValue " , " Float " , tonumber ( args [ 1 ] ) / 100 ) ;
2008-06-14 00:55:20 +00:00
PLabel : PostMessage ( " SetText " , " text " , args [ 1 ] .. " % " ) ;
end
end
2008-09-16 17:05:56 +00:00
concommand.Add ( " wire_cpu_vgui_progress " , VGUI_Progress )
2008-06-14 00:55:20 +00:00
end
// if ( CLIENT ) then
2008-09-16 17:05:56 +00:00
SourceLines = { }
SourceLineNumbers = { }
SourceLinesSent = 0
SourcePrevCharRate = 0
SourceTotalChars = 0
SourceLoadedChars = 0
local function UploadProgram ( pl )
local SendLinesMax = SourceLinesSent + pl : GetInfo ( " wire_cpu_packet_bandwidth " )
local TotalChars = 0
if SendLinesMax > table.Count ( SourceLines ) then
SendLinesMax = table.Count ( SourceLines )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
while ( SourceLinesSent <= SendLinesMax ) && ( TotalChars < 1024 ) do
SourceLinesSent = SourceLinesSent + 1
local line = SourceLines [ SourceLinesSent ]
local linen = SourceLinesSent
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
if ( line ) && ( line ~= " \n " ) && ( string.gsub ( line , " \n " , " " ) ~= " " ) then
RunConsoleCommand ( " wire_cpu_addsrc " , linen , string.gsub ( line , " \n " , " " ) )
TotalChars = TotalChars + string.len ( line )
2008-06-14 00:55:20 +00:00
else
2008-09-16 17:05:56 +00:00
RunConsoleCommand ( " wire_cpu_addsrc " , linen , " " )
end
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
SourceLoadedChars = SourceLoadedChars + TotalChars
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local CharRate = ( SourcePrevCharRate * 1.95 + TotalChars * 0.05 ) / 2
SourcePrevCharRate = CharRate
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
if SinglePlayer ( ) then CharRate = CharRate / pl : GetInfo ( " wire_cpu_packet_rate_sp " )
else CharRate = CharRate / pl : GetInfo ( " wire_cpu_packet_rate_mp " )
end
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local TimeLeft = math.floor ( ( SourceTotalChars - SourceLoadedChars ) / CharRate )
local TempPercent = math.floor ( ( ( SourceLinesSent - 1 ) / table.Count ( SourceLines ) ) * 100 )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
pl : ConCommand ( ' wire_cpu_vgui_status "Uploading @ ' .. math.floor ( CharRate / 1024 ) .. ' kb/sec, avg. ' .. TimeLeft .. ' sec left, ' .. SourceLinesSent .. ' lines sent" ' )
pl : ConCommand ( ' wire_cpu_vgui_progress " ' .. TempPercent .. ' " ' )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
if ( SourceLinesSent > table.Count ( SourceLines ) ) then
pl : ConCommand ( ' wire_cpu_vgui_close ' )
timer.Remove ( " CPUSendTimer " )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
end
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local function LoadProgram ( pl , command , args )
local fname = " CPUChip \\ " .. pl : GetInfo ( " wire_cpu_filename " ) ;
if ( ! file.Exists ( fname ) ) then
fname = " CPUChip \\ " .. pl : GetInfo ( " wire_cpu_filename " ) .. " .txt " ;
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
if ( ! file.Exists ( fname ) ) then
pl : PrintMessage ( HUD_PRINTTALK , " CPU -> Sorry! Requested file was not found \n " )
return
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
pl : ConCommand ( ' wire_cpu_clearsrc ' )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local filedata = file.Read ( fname )
if ( ! filedata ) then
pl : PrintMessage ( HUD_PRINTTALK , " CPU -> Sorry! File was found, but leprechauns prevented it from getting read! \n " ) // This message occurs rarely enough to put something fun here
return
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
SourceLines = string.Explode ( " \n " , filedata )
SourceLinesSent = 0
SourceTotalChars = string.len ( filedata )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
SourcePrevCharRate = string.len ( SourceLines [ 1 ] )
SourceLoadedChars = 0
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
pl : ConCommand ( ' wire_cpu_vgui_open ' )
pl : ConCommand ( ' wire_cpu_vgui_title "CPU - Uploading program" ' )
pl : ConCommand ( ' wire_cpu_vgui_status "Initializing" ' )
pl : ConCommand ( ' wire_cpu_vgui_progress "0" ' )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
// Send 50 lines
if ( SinglePlayer ( ) ) then timer.Create ( " CPUSendTimer " , pl : GetInfo ( " wire_cpu_packet_rate_sp " ) , 0 , UploadProgram , pl , false )
else timer.Create ( " CPUSendTimer " , pl : GetInfo ( " wire_cpu_packet_rate_mp " ) , 0 , UploadProgram , pl , false )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
end
concommand.Add ( " wire_cpu_load " , LoadProgram )
2008-06-14 00:55:20 +00:00
2008-09-16 17:05:56 +00:00
local function ClearProgram ( pl , command , args )
pl : ConCommand ( ' wire_cpu_clearsrc ' )
2008-06-14 00:55:20 +00:00
end
2008-09-16 17:05:56 +00:00
concommand.Add ( " wire_cpu_clear " , ClearProgram )
// end
2008-06-14 00:55:20 +00:00
//=============================================================================
function TOOL . BuildCPanel ( panel )
panel : AddControl ( " Header " , { Text = " #Tool_wire_cpu_name " , Description = " #Tool_wire_cpu_desc " } )
panel : AddControl ( " TextBox " , {
2008-09-16 17:05:56 +00:00
Label = " Source code file name " ,
2008-06-14 00:55:20 +00:00
Command = " wire_cpu_filename " ,
MaxLength = " 128 "
} )
panel : AddControl ( " Button " , {
Text = " Quick Load " ,
Name = " Load " ,
Command = " wire_cpu_load "
} )
2008-09-16 17:05:56 +00:00
/* panel : AddControl ( " Button " , {
2008-06-14 00:55:20 +00:00
Text = " Clear " ,
Name = " Clear " ,
Command = " wire_cpu_clear "
2008-09-16 17:05:56 +00:00
} ) */
panel : AddControl ( " Label " , {
Text = " "
} )
panel : AddControl ( " Label " , {
Text = " CPU settings: "
} )
panel : AddControl ( " CheckBox " , {
Label = " Use CPU ROM " ,
Command = " wire_cpu_rom "
} )
panel : AddControl ( " Label " , {
Text = " ROM data is saved with advanced duplicator and is stored between CPU resets "
} )
panel : AddControl ( " Label " , {
Text = " "
} )
panel : AddControl ( " Label " , {
Text = " These do not work yet: "
2008-06-14 00:55:20 +00:00
} )
panel : AddControl ( " CheckBox " , {
2008-09-16 17:05:56 +00:00
Label = " CPU ROM Present " ,
Command = " wire_cpu_rom_present "
} )
panel : AddControl ( " Label " , {
Text = " CPU can be without internal ROM/RAM (you need to attach RAM/ROM manually) "
2008-06-14 00:55:20 +00:00
} )
2008-09-16 17:05:56 +00:00
2008-06-18 09:16:54 +00:00
panel : AddControl ( " CheckBox " , {
2008-09-16 17:05:56 +00:00
Label = " Dump CPU data " ,
Command = " wire_cpu_dump_data "
} )
panel : AddControl ( " Label " , {
Text = " Dumps CPU information and compiled code to pdump/cdump/ldump files in DATA folder (server host, or singleplayer only) "
2008-06-18 09:16:54 +00:00
} )
2008-09-16 17:05:56 +00:00
2008-06-14 00:55:20 +00:00
panel : AddControl ( " Button " , {
2008-09-16 17:05:56 +00:00
Text = " Code editor "
2008-06-14 00:55:20 +00:00
} )
2008-09-16 17:05:56 +00:00
panel : AddControl ( " Label " , {
Text = " Opens code editor (ZASM) "
} )
panel : AddControl ( " Label " , {
Text = " Can be used for ZC code (requires ZCK addon) "
} )
panel : AddControl ( " Button " , {
Text = " ZCPU documentation (online) "
} )
panel : AddControl ( " Label " , {
Text = " Loads online CPU documentation and tutorials "
} )
end