From ea25619c7818009552cea6ab32f04b8bf2cfbeca Mon Sep 17 00:00:00 2001 From: Martin Haye Date: Mon, 11 Sep 2017 11:16:10 -0700 Subject: [PATCH] Added code to display % heap memory in use. --- .../src/org/badvision/A2PackPartitions.groovy | 2 +- Platform/Apple/virtual/src/font/fontEngine.s | 26 +++++ .../Apple/virtual/src/include/fontEngine.i | 3 +- Platform/Apple/virtual/src/plasma/diskops.pla | 9 +- Platform/Apple/virtual/src/plasma/gamelib.plh | 1 + .../Apple/virtual/src/plasma/gameloop.pla | 94 ++++++++++++++----- Platform/Apple/virtual/src/plasma/godmode.pla | 1 + Platform/Apple/virtual/src/plasma/party.pla | 3 +- Platform/Apple/virtual/src/plasma/store.pla | 18 ++-- 9 files changed, 120 insertions(+), 37 deletions(-) diff --git a/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy b/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy index 5e6d2873..61602e7a 100644 --- a/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy +++ b/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy @@ -1588,7 +1588,7 @@ class A2PackPartitions def hourCode = (char) (97 + hour) // 'a'=0, 'b'=1, etc. def engineCode = String.format("%d%c%02d%c", yearCode, monthCode, day, hourCode) - def offset = (int) ((scenarioStamp - engineStamp) / (1000 * 60 * 60)) + def offset = Math.max(-99, Math.min(99, (int) ((scenarioStamp - engineStamp) / (1000 * 60 * 60)))) return String.format("%s%s%d", engineCode, offset < 0 ? "-" : ".", Math.abs(offset)) } diff --git a/Platform/Apple/virtual/src/font/fontEngine.s b/Platform/Apple/virtual/src/font/fontEngine.s index fe0a73bb..4054d50f 100644 --- a/Platform/Apple/virtual/src/font/fontEngine.s +++ b/Platform/Apple/virtual/src/font/fontEngine.s @@ -107,6 +107,9 @@ SetFont JMP DoSetFont ;API call address ;Set the window boundaries (byte-oriented bounds) SetWindow JMP SetWnd ;API call address +;Set the window boundaries (byte-oriented bounds) +GetWindow JMP GetWnd ;API call address + ;Clear the window ClearWindow JMP ClrHome ;API call address @@ -613,6 +616,29 @@ SetWnd LDA evalStkL+SW_TOP,X ;get top coord STA WrdWdth RTS +;Routine: Get the cursor position (relative to the current window) +GetWnd DEX + LDY #0 + LDA CursY ; top + STA evalStkL,X + TYA + STA evalStkH,X + DEX ; bottom + STA evalStkH,X + LDA CursYb ; bottom + STA evalStkL,X + DEX + LDA CursXl ; left + STA evalStkL,X + LDA CursXh + STA evalStkH,X + DEX + LDA CursXrl ; right + STA evalStkL,X + LDA CursXrh + STA evalStkH,X + RTS + ;Routine: Scroll screen up 1 character line ;This routine scrolls a window defined by ;Left, Right, Top, Bottom - Margin parameters. diff --git a/Platform/Apple/virtual/src/include/fontEngine.i b/Platform/Apple/virtual/src/include/fontEngine.i index 10ee8a32..37d74e24 100644 --- a/Platform/Apple/virtual/src/include/fontEngine.i +++ b/Platform/Apple/virtual/src/include/fontEngine.i @@ -37,7 +37,8 @@ fontEngine = $EC00 fontEngineLen = $F00 ; maximum (allows for some debug code) SetFont = fontEngine SetWindow = SetFont+3 -ClearWindow = SetWindow+3 +GetWindow = SetWindow+3 +ClearWindow = GetWindow+3 CopyWindow = ClearWindow+3 DisplayChar = CopyWindow+3 DisplayStr = DisplayChar+3 diff --git a/Platform/Apple/virtual/src/plasma/diskops.pla b/Platform/Apple/virtual/src/plasma/diskops.pla index 0092ac1f..22c20d8c 100644 --- a/Platform/Apple/virtual/src/plasma/diskops.pla +++ b/Platform/Apple/virtual/src/plasma/diskops.pla @@ -225,11 +225,9 @@ end /////////////////////////////////////////////////////////////////////////////////////////////////// def _saveGame()#1 - // Never save corrupted heap - mmgr(CHECK_MEM, 0) - // Perform garbage collection and record the size of the heap so we can restore it correctly - global=>w_heapSize = mmgr(HEAP_COLLECT, 0) - HEAP_BOTTOM + // (also does a CHECK_MEM to be sure we never save corrupted heap) + heapCollect() // Copy data to main memory, and write it out. memcpy(HEAP_BOTTOM, LOAD_SAVE_BUF, HEAP_SIZE) // LC to low mem @@ -253,7 +251,7 @@ def loadInternal()#1 fin memcpy(LOAD_SAVE_BUF, HEAP_BOTTOM, HEAP_SIZE) // low mem to LC initHeap(p_loaded=>w_heapSize) - mmgr(CHECK_MEM, 0) // make sure heap is valid + heapCollect() // make sure heap is valid, and record final size return TRUE end @@ -412,6 +410,7 @@ def newGame()#0 if global=>p_players->b_skillPoints partyModule()=>party_showPlayerSheet(0) fin + heapCollect() end /////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/Platform/Apple/virtual/src/plasma/gamelib.plh b/Platform/Apple/virtual/src/plasma/gamelib.plh index 17279a65..68ae8650 100644 --- a/Platform/Apple/virtual/src/plasma/gamelib.plh +++ b/Platform/Apple/virtual/src/plasma/gamelib.plh @@ -61,6 +61,7 @@ import gamelib predef getYN()#1 predef girdPlayer(player)#0 predef giveItemToPlayer(p_player, itemFuncNum)#0 + predef heapCollect()#0 predef hisHerTheir(c_gender)#1 predef initHeap(loadedSize)#0 predef initPlayerXP(player)#0 diff --git a/Platform/Apple/virtual/src/plasma/gameloop.pla b/Platform/Apple/virtual/src/plasma/gameloop.pla index 814576a9..15b0d0b4 100644 --- a/Platform/Apple/virtual/src/plasma/gameloop.pla +++ b/Platform/Apple/virtual/src/plasma/gameloop.pla @@ -98,6 +98,7 @@ word pGlobalTileset = NULL byte curMapPartition = 0 export word pGodModule = NULL export word typeHash = 0 +word curHeapPct = 0 // Queue setMap / teleport / start_encounter, since otherwise script might be replaced while executing byte q_mapIs3D = 0 @@ -797,6 +798,14 @@ export asm setWindow(top, bottom, left, right)#0 jmp SetWindow end +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Get the font engine's current text window +// Returns: top, bottom, left, right +export asm getWindow()#4 + bit setLcRW+lcBank2 + jmp GetWindow +end + /////////////////////////////////////////////////////////////////////////////////////////////////// // Get the cursor position - returns X, Y export asm getCursor()#2 @@ -1358,33 +1367,75 @@ export def setGround(num)#0 end /////////////////////////////////////////////////////////////////////////////////////////////////// -def printVersion()#0 - word p, len, cv, ch - if !pResourceIndex; return; fin +def printAtBottom(str, textX, botLeft, botRight, strX)#0 + byte cv + word origX, origY, origTop, origBottom, origLeft, origRight + + // Save text cursor, and print at bottom of text window cv = ^$25 ^$23 = 24 // full height window ^$25 = 22 crout() - ^$24 = 25 - puts("V") - setWindow(183, 192, 161, 261) - clearWindow() - setWindow(183, 192, 168, 252) - rawDisplayStr("^YV") - p = pResourceIndex - len = readAuxByte(p) - while len - p++ - ch = readAuxByte(p) - printChar(ch) - displayChar(ch) - len-- - loop - rawDisplayStr("^N") + ^$24 = textX + puts(str) ^$23 = 23 // shrink window to protect version num ^$25 = cv-1 crout() - setWindow2() + + // Save graphics cursor and window + origX, origY = getCursor() + origTop, origBottom, origLeft, origRight = getWindow() + + // Establish new graphics window, clear it, and display the string + setWindow(183, 192, botLeft, botRight) + clearWindow() + rawDisplayStr("^T") // do not use printf variants, since it might overwrite str + rawDisplayStr(convert3Dec(strX)) + rawDisplayStr(str) + if mapIs3D and texturesLoaded; copyWindow(); fin + + // Restore original graphics cursor and window + setWindow(origTop, origBottom, origLeft, origRight) + setCursor(origX, origY) +end + +/////////////////////////////////////////////////////////////////////////////////////////////////// +def printHeapPct()#0 + word str, len + str = sprintf1("%d%%", curHeapPct) + len = calcWidth(str) + printAtBottom(str, 36, 240, 268, (28-len) >> 1) // center +end + +/////////////////////////////////////////////////////////////////////////////////////////////////// +def printVersion()#0 + word p + byte len + if !pResourceIndex; return; fin + + buildString(@addToString) + printChar('V') + p = pResourceIndex + len = readAuxByte(p) + while len + p++; printChar(readAuxByte(p)); len-- + loop + p = finishString(FALSE) + len = calcWidth(p) + printAtBottom(p, 25, 154, 154 + len + 8, 3) + printHeapPct() +end + +/////////////////////////////////////////////////////////////////////////////////////////////////// +export def heapCollect()#0 + word pct + mmgr(CHECK_MEM, 0) + global=>w_heapSize = mmgr(HEAP_COLLECT, 0) - HEAP_BOTTOM + pct = min(99, max(0, ((global=>w_heapSize / 10) * 100) / 307)) + if pct <> curHeapPct + curHeapPct = pct + printHeapPct() + fin end /////////////////////////////////////////////////////////////////////////////////////////////////// @@ -2463,8 +2514,7 @@ def doCombat(mapCode, backUpOnFlee)#1 // Handled in a separate module. Clear enemies out of the heap when finished. result = loadEngine(MOD_COMBAT)=>combat_zoneEncounter(mapCode) global=>p_enemyGroups = NULL - mmgr(CHECK_MEM, 0) - mmgr(HEAP_COLLECT, 0) + heapCollect() if (result == -99) playerDeath() diff --git a/Platform/Apple/virtual/src/plasma/godmode.pla b/Platform/Apple/virtual/src/plasma/godmode.pla index 9f321835..b4f5bd3a 100644 --- a/Platform/Apple/virtual/src/plasma/godmode.pla +++ b/Platform/Apple/virtual/src/plasma/godmode.pla @@ -279,6 +279,7 @@ def selectThing(moduleNum, nThings, nSkip, prompt)#1 mmgr(FREE_MEMORY, pModule) ^$c050 + heapCollect() return nFunc end diff --git a/Platform/Apple/virtual/src/plasma/party.pla b/Platform/Apple/virtual/src/plasma/party.pla index aa1267e1..26d574f6 100644 --- a/Platform/Apple/virtual/src/plasma/party.pla +++ b/Platform/Apple/virtual/src/plasma/party.pla @@ -778,8 +778,7 @@ def _showPlayerSheet(player_num)#1 // funcTbl functions always have to return a fin break is $1B // Esc - mmgr(CHECK_MEM, 0) - mmgr(HEAP_COLLECT, 0) + heapCollect() return NULL otherwise if sel == 'X' and mode <> 'I' // switch from stats to inv diff --git a/Platform/Apple/virtual/src/plasma/store.pla b/Platform/Apple/virtual/src/plasma/store.pla index 14552bfd..014505fb 100644 --- a/Platform/Apple/virtual/src/plasma/store.pla +++ b/Platform/Apple/virtual/src/plasma/store.pla @@ -107,8 +107,7 @@ def displayBuyPage(pItemTbl, markupRatio, pageNum, nPages)#1 word pFunc, pItem // Clear stuff from previous page - mmgr(CHECK_MEM, 0) - mmgr(HEAP_COLLECT, 0) + heapCollect() displayTitle("buying", "Browse", pageNum, nPages) pFunc = pItemTbl + ((pageNum*PAGE_SIZE) << 1) @@ -255,6 +254,13 @@ def _buyFromStore(storeCode, profitPercent)#1 while TRUE if redisplay nItemsOnPage = displayBuyPage(pItemTbl, ratio, pageNum, nPages) + flipToPage1 // FOO + ^$c051 + mmgr(DEBUG_MEM, 0) + rdkey + auxMmgr(DEBUG_MEM, 0) + rdkey + ^$c050 fin choice = getUpperKey() redisplay = TRUE @@ -276,8 +282,8 @@ def _buyFromStore(storeCode, profitPercent)#1 loop unloadExtraModules() - mmgr(CHECK_MEM, 0) - return mmgr(HEAP_COLLECT, 0) + heapCollect() + return 0 end /////////////////////////////////////////////////////////////////////////////////////////////////// @@ -381,8 +387,8 @@ def _sellToStore(profitPercent)#1 loop unloadExtraModules() - mmgr(CHECK_MEM, 0) - return mmgr(HEAP_COLLECT, 0) + heapCollect() + return 0 end ///////////////////////////////////////////////////////////////////////////////////////////////////