@@ -67,7 +67,11 @@ void VirtualMachine::SetProgram(std::vector<uint8> bytecode)
67
67
m_HeapBase = m_FirstSegmentPtr+sizeof (uint32);
68
68
Pack<uint32>(m_FirstSegmentPtr, m_HeapBase);
69
69
Pack<uint32>(m_HeapBase, MAX_RAM - m_HeapBase);
70
- Pack<uint32>(m_HeapBase+sizeof (uint32), 0 );
70
+ Pack<uint32>(m_HeapBase + sizeof (uint32), 0 );
71
+
72
+ #ifdef VM_DEBUG_HEAP
73
+ PrintHeap ();
74
+ #endif
71
75
72
76
ProgramLoaded = true ;
73
77
}
@@ -87,7 +91,7 @@ void VirtualMachine::Interpret()
87
91
88
92
Opcode operation = static_cast <Opcode>(m_RAM[m_ProgramCounter]);
89
93
90
- // std::cout << "[DBG] operation: " << GetOpString(operation) << std::endl;
94
+ std::cout << " [DBG] operation: " << GetOpString (operation) << std::endl;
91
95
92
96
switch (operation)
93
97
{
@@ -200,6 +204,11 @@ void VirtualMachine::Interpret()
200
204
Pack<uint32>(prevNextPtr, Unpack<uint32>(bestFitPtr+sizeof (uint32)));// Link the previous segment to next segment
201
205
}
202
206
Push (bestFitPtr+sizeof (uint32));
207
+ ++m_ProgramCounter;
208
+
209
+ #ifdef VM_DEBUG_HEAP
210
+ PrintHeap ();
211
+ #endif
203
212
}
204
213
continue ;
205
214
// Mark the space at (a) as unused
@@ -210,7 +219,7 @@ void VirtualMachine::Interpret()
210
219
211
220
uint32 existingNextPtr = m_FirstSegmentPtr;
212
221
uint32 nextSegment = Unpack<uint32>(existingNextPtr);
213
- uint32 existingSegment;
222
+ uint32 existingSegment = existingNextPtr ;
214
223
215
224
bool earlyOut = false ;
216
225
while (nextSegment != 0 && !earlyOut)
@@ -219,7 +228,7 @@ void VirtualMachine::Interpret()
219
228
{
220
229
// insert
221
230
bool standalone = true ;
222
- if ((existingNextPtr != m_FirstSegmentPtr) && (existingSegment + Unpack<uint32>(existingSegment) == segmentPtr))// Merge EXISTING+INSERTED
231
+ if ((nextSegment != m_FirstSegmentPtr) && (existingSegment + Unpack<uint32>(existingSegment) == segmentPtr))// Merge EXISTING+INSERTED
223
232
{
224
233
// simply expand the existing segment to accomodate our size
225
234
segmentPtr = existingSegment;
@@ -248,7 +257,13 @@ void VirtualMachine::Interpret()
248
257
if (!earlyOut)
249
258
{
250
259
std::cerr << " [VM] Failed to free memory at " << segmentPtr << " ; " << segmentSize << " bytes" << std::endl;
260
+ return ;
251
261
}
262
+ ++m_ProgramCounter;
263
+
264
+ #ifdef VM_DEBUG_HEAP
265
+ PrintHeap ();
266
+ #endif
252
267
}
253
268
continue ;
254
269
@@ -464,3 +479,18 @@ void VirtualMachine::Pack(uint32 address, T value)
464
479
m_RAM[address+3 ] = n & 0xFF ;
465
480
#endif
466
481
}
482
+
483
+ void VirtualMachine::PrintHeap (bool baseOffset)
484
+ {
485
+ uint32 offset = baseOffset ? m_HeapBase : 0 ;
486
+ uint32 nextSegment = Unpack<uint32>(m_FirstSegmentPtr);
487
+
488
+ std::cout << " [DBG Heap]: " << m_FirstSegmentPtr-offset << " first: " << nextSegment-offset << " \t " ;
489
+ while (nextSegment != 0 )
490
+ {
491
+ std::cout << " @" << nextSegment-offset << " {s: " << Unpack<uint32>(nextSegment);
492
+ nextSegment = Unpack<uint32>(nextSegment + sizeof (uint32));
493
+ std::cout << " ; n: " << nextSegment-offset << " } " ;
494
+ }
495
+ std::cout << std::endl;
496
+ }
0 commit comments