changeset 36:b60cbf3fa894

Refactoring 'Code' & VM logic
author koryspansel
date Wed, 21 Sep 2011 12:18:15 -0700
parents 4750819f465d
children 58a16d529d95
files LightClone/LightClone.vcproj LightClone/Source/Program.cpp LightClone/Source/Program.h LightClone/Source/VirtualMachine.cpp LightClone/Source/World.cpp LightClone/Source/World.h
diffstat 6 files changed, 180 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/LightClone/LightClone.vcproj	Wed Sep 21 10:26:48 2011 -0700
+++ b/LightClone/LightClone.vcproj	Wed Sep 21 12:18:15 2011 -0700
@@ -213,6 +213,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\Program.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\Tower.cpp"
 				>
 			</File>
@@ -359,6 +363,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\Program.h"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\Tower.h"
 				>
 			</File>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/Program.cpp	Wed Sep 21 12:18:15 2011 -0700
@@ -0,0 +1,90 @@
+/*
+ * Program
+ */
+
+#include "Program.h"
+
+/*
+ * CalculateSize
+ */
+uint32 Program::Function::CalculateSize()
+{
+	uint32 nSize = 0;
+
+	for(uint32 i = 0; i < MaximumInstructionCount; ++i)
+	{
+		if(nInstruction[i] > 0)
+		{
+			++nSize;
+		}
+	}
+
+	return nSize;
+}
+
+/*
+ * Program
+ */
+Program::Program()
+{
+}
+
+/*
+ * Upload
+ */
+ErrorCode Program::Upload(VirtualMachine& kMachine)
+{
+	ErrorCode eCode = Error_Success;
+
+	kMachine.Reset();
+	kMachine.ClearMemory();
+	kMachine.RemoveAllFunctions();
+
+	for(uint32 i = 0; i < nFunctionCount && eCode == Error_Success; ++i)
+	{
+		kMachine.AddFunction(i, kFunction[i].CalculateSize());
+
+		uint32 nSize = kMachine.GetFunctionSize(i);
+		uint8* pData = kMachine.GetFunctionMemory(i);
+
+		eCode = Compile(i, pData, nSize);
+	}
+
+	return eCode;
+}
+
+/*
+ * Compile
+ */
+ErrorCode Program::Compile(uint32 nIndex, uint8* pData, uint32 nSize)
+{
+	for(uint32 i = 0; i < Function::MaximumInstructionCount; ++i)
+	{
+		const uint32 nAction = kFunction[nIndex].nInstruction[i];
+
+		if(Action_Forward <= nAction && nAction <= Action_Light)
+		{
+			if(nSize < 2)
+				return Error_Fail;
+
+			*pData++ = Instruction_Action;
+			*pData++ = nAction;
+
+			nSize -= 2;
+		}
+		else
+
+		if(Action_FunctionA <= nAction && nAction <= Action_FunctionB)
+		{
+			if(nSize < 2)
+				return Error_Fail;
+
+			*pData++ = Instruction_Call;
+			*pData++ = nAction - Action_FunctionA + 1;
+
+			nSize -= 2;
+		}
+	}
+
+	return Error_Success;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/Program.h	Wed Sep 21 12:18:15 2011 -0700
@@ -0,0 +1,73 @@
+/*
+ * Program
+ */
+
+#ifndef __PROGRAM_H__
+#define __PROGRAM_H__
+
+#include "Core.h"
+#include "VirtualMachine.h"
+
+/*
+ * Program
+ */
+class Program
+{
+	/*
+	 * MaximumFunctionCount
+	 */
+	static const uint32 MaximumFunctionCount = 3;
+
+	/*
+	 * Function
+	 */
+	struct Function
+	{
+		/*
+		 * MaximumInstructionCount
+		 */
+		static const uint32 MaximumInstructionCount = 16;
+
+		/*
+		 * nInstruction
+		 */
+		uint32 nInstruction[MaximumInstructionCount];
+
+		/*
+		 * CalculateSize
+		 */
+		uint32 CalculateSize();
+
+	};
+
+	/*
+	 * kFunction
+	 */
+	Function kFunction[MaximumFunctionCount];
+
+	/*
+	 * nFunctionCount
+	 */
+	uint32 nFunctionCount;
+
+public:
+
+	/*
+	 * Program
+	 */
+	Program();
+
+	/*
+	 * Upload
+	 */
+	ErrorCode Upload(VirtualMachine& kMachine);
+
+private:
+
+	/*
+	 * Compile
+	 */
+	ErrorCode Compile(uint32 nFunction, uint8* pData, uint32 nSize);
+};
+
+#endif //__PROGRAM_H__
--- a/LightClone/Source/VirtualMachine.cpp	Wed Sep 21 10:26:48 2011 -0700
+++ b/LightClone/Source/VirtualMachine.cpp	Wed Sep 21 12:18:15 2011 -0700
@@ -152,7 +152,7 @@
 	if(nFunctionCount + 1 < MaximumFunctionCount)
 	{
 		kFunction[nFunctionCount].nAddress	= nFunctionCount * FunctionStride;
-		kFunction[nFunctionCount].nSize		= nSize;
+		kFunction[nFunctionCount].nSize		= nSize * 2;
 
 		++nFunctionCount;
 
--- a/LightClone/Source/World.cpp	Wed Sep 21 10:26:48 2011 -0700
+++ b/LightClone/Source/World.cpp	Wed Sep 21 12:18:15 2011 -0700
@@ -16,7 +16,6 @@
 	nGameState				= GameState_Active;
 	nSimulationState		= SimulationState_Idle;
 	pFunction				= 0;
-	nCurrentFunction		= 0;
 	nCurrentLevel			= 0;
 }
 
@@ -122,7 +121,7 @@
 			kBot.kPosition	= kLoader.GetInitialPosition();
 			kBot.kDirection	= kLoader.GetInitialDirection();
 
-			const uint32 nCount = kLoader.GetFunctionCount();
+			const uint32 nCount = 2;//kLoader.GetFunctionCount();
 
 			pFunction = new Code[nCount + 1];
 			pFunction[0].Initialize(MainFunctionLength);
@@ -135,6 +134,12 @@
 				pFunction[i + 1].Initialize(nLength);
 				pFunction[i + 1].Clear();
 			}
+
+			for(uint32 i = 0; i < nCount; ++i)
+			{
+				//kProgram.Initialize(i, kLoader.GetFunctionLength(i));
+				//kProgram.Clear(i);
+			}
 		}
 	}
 
@@ -292,6 +297,7 @@
 		pFunctionA->Initialize(pResourceManager);
 		pFunctionA->SetTexture("Data\\Textures\\PanelA.png");
 		pFunctionA->SetPosition(16.0f, 360.0f);
+		//pFunctionA->Upload(
 
 		GuiLabel* pFunctionBLabel = new GuiLabel();
 		pFunctionBLabel->Initialize(pResourceManager);
--- a/LightClone/Source/World.h	Wed Sep 21 10:26:48 2011 -0700
+++ b/LightClone/Source/World.h	Wed Sep 21 12:18:15 2011 -0700
@@ -71,11 +71,6 @@
 	uint32 nCurrentLevel;
 
 	/*
-	 * nCurrentFunction
-	 */
-	uint32 nCurrentFunction;
-
-	/*
 	 * kCameraController
 	 */
 	CameraController kCameraController;