home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / SimUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-25  |  9.5 KB  |  356 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "Conversions.h"
  14. #include    "FabLibResIDs.h"
  15. #include    "SimResIDs.h"
  16.  
  17. #include    "Globals.h"
  18. #include    "Animation.h"
  19. #include    "ControlStore.h"
  20. #include    "Dump.h"
  21. #include    "Disasm.h"
  22. #include    "Input.h"
  23. #include    "Main.h"
  24. #include    "Preferences.h"
  25. #include    "Registers.h"
  26. #include    "SimUtils.h"
  27.  
  28. #if defined(FabSystem7orlater)
  29.  
  30. #define    LOWSHORTADDR(l)    (((short *)(&l))+1)
  31.  
  32. #pragma segment Main
  33.  
  34.  
  35. /* FromStringToNum: common code for translating a string with a specified
  36. format to a number */
  37.  
  38. void FromStringToNum(ConstStr255Param sourceStr, long *destN, short whichFormat)
  39. {
  40.  
  41. switch( whichFormat ) {
  42.     case kPOP_DEC:    StringToNum(sourceStr, destN);
  43.                     break;
  44.     case kPOP_HEX:    HexStringToShort(sourceStr, LOWSHORTADDR(*destN));
  45.                     break;
  46.     case kPOP_OCT:    OctStringToShort(sourceStr, LOWSHORTADDR(*destN));
  47.                     break;
  48.     case kPOP_BIN:    BinStringToShort(sourceStr, LOWSHORTADDR(*destN));
  49.                     break;
  50.     case kPOP_TEXT:    *(char *)LOWSHORTADDR(*destN) = sourceStr[1];
  51.                     *((char *)LOWSHORTADDR(*destN) + 1) = sourceStr[2];
  52.                     break;
  53.     }
  54. }
  55.  
  56. /* FromNumToString: common code for translating a number into a
  57. string with a specified format */
  58.  
  59. void FromNumToString(Str255 destStr, short sourceN, short whichFormat)
  60. {
  61.  
  62. switch( whichFormat ) {
  63.     case kPOP_DEC:    MyNumToString(sourceN, destStr);
  64.                     break;
  65.     case kPOP_HEX:    ShortToHexString(sourceN, destStr);
  66.                     break;
  67.     case kPOP_OCT:    ShortToOctString(sourceN, destStr);
  68.                     break;
  69.     case kPOP_BIN:    ShortToBinString(sourceN, destStr);
  70.                     break;
  71.     case kPOP_TEXT:    destStr[0] = 2;
  72.                     destStr[1] = *((char *)&sourceN);
  73.                     destStr[2] = *((char *)&sourceN + 1);
  74.                     break;
  75.     }
  76. }
  77.  
  78. /* MyGrowZone: the (old) Grow Zone proc! */
  79.  
  80.  
  81. /*
  82. pascal long MyGrowZone(Size )
  83. {
  84. //#pragma unused    (qNeeded)
  85. ParamBlockRec    myPB;
  86. Str63    tempFName;
  87. EventRecord    dummyEv;
  88. FSSpec    thisFSSpec;
  89. short    state[kNUMOFSTATEPARTS];    // the registers & editable parts of my machine
  90. register Handle    dangerousHandle = GZSaveHnd();
  91. register Handle    readyBuffer;
  92. register long    tempA5;
  93. long    thisDirID;
  94. short    thisVRefNum, tmpFRefN;
  95. register OSErr    err;
  96. register Boolean    somethingInTheTrash = false;
  97.  
  98. tempA5 = SetCurrentA5();
  99. InitCursor();
  100. if (gWPtr_Animation)
  101.     if (offScr) {
  102.         if (offScr->portBits.baseAddr)
  103.             DisposePtr(offScr->portBits.baseAddr);
  104.         if (offScr->visRgn && offScr->clipRgn)
  105.             ClosePort(offScr);
  106.         DisposePtr((Ptr)offScr);
  107.         }
  108. if (gWPtr_Animation &&
  109.     gWPtr_Registers &&
  110. //    Ctrl_Base &&
  111.     gWPtr_IO &&
  112.     gWPtr_Microprogram_Ed &&
  113.     gWPtr_Dump &&
  114. //    dumpVScroll &&
  115.     gWPtr_Disasm &&
  116. //    disasmVScroll
  117.     )
  118.         SavePreferencesFile();
  119. if (gWPtr_Disasm) {
  120.     DisposeWindow(gWPtr_Disasm);
  121.     gWPtr_Disasm = nil;
  122.     }
  123. if (gWPtr_Dump) {
  124.     DisposeWindow(gWPtr_Dump);
  125.     gWPtr_Dump = nil;
  126.     }
  127. if (gWPtr_IO) {
  128.     if (gIODoc.docTE != (TEHandle)dangerousHandle)
  129.         if (gIODoc.docTE) {
  130.             TEDispose(gIODoc.docTE);
  131.             gIODoc.docTE = nil;
  132.             }
  133.     DisposeWindow(gWPtr_IO);
  134.     gWPtr_IO = nil;
  135.     }
  136. if (gWPtr_Registers) {
  137.     DisposeWindow(gWPtr_Registers);
  138.     gWPtr_Registers = nil;
  139.     }
  140. if (gWPtr_Animation) {
  141.     DisposeWindow(gWPtr_Animation);
  142.     gWPtr_Animation = nil;
  143.     }
  144. // we should have freed some memory
  145. if (gMMemory) {
  146.     if ((err = FindFolder(kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
  147.                             &thisVRefNum, &thisDirID)) == noErr) {
  148.         MyNumToString(TickCount(), tempFName);
  149.         (void)FSMakeFSSpecCompat(thisVRefNum, thisDirID, tempFName, &thisFSSpec);
  150.         if ((err = FSpCreateCompat(&thisFSSpec, kFCR_MINE, kFTY_RAM,
  151.                             smSystemScript)) == noErr)
  152.             if ((err = FSpOpenDFCompat(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  153.                 myPB.ioParam.ioCompletion = nil;
  154.                 myPB.ioParam.ioBuffer = gMMemory;
  155.                 myPB.ioParam.ioReqCount = kSIZE_RAM;
  156.                 myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
  157.                 myPB.ioParam.ioRefNum = tmpFRefN;
  158.                 myPB.ioParam.ioPosOffset = 0L;
  159.                 (void)PBWriteAsync(&myPB);    // in temporary file
  160.                 while (myPB.ioParam.ioResult > 0) {
  161.                     SystemTask();
  162.                     (void)EventAvail(everyEvent, &dummyEv);
  163.                     }
  164.                 (void)FSClose(tmpFRefN);
  165.                 DisposePtr(gMMemory);
  166.                 gMMemory = nil;
  167.                 if ((err = myPB.ioParam.ioResult) == noErr) {
  168.                     somethingInTheTrash = true;
  169.                     err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE, kFTY_RAM,
  170.                                         kSTR_ApplicationName, smSystemScript);
  171.                     }
  172.                 }
  173.         MyNumToString(TickCount() + 1, tempFName);
  174.         (void)FSMakeFSSpecCompat(thisVRefNum, thisDirID, tempFName, &thisFSSpec);
  175.         if ((err = FSpCreateCompat(&thisFSSpec, kFCR_MINE, kFTY_REG,
  176.                             smSystemScript)) == noErr)
  177.             if ((err = FSpOpenDFCompat(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  178.                 BlockMoveData((Ptr)&gParts, (Ptr)&state, sizeof(gParts));
  179.                 BlockMoveData((Ptr)&gRegs, (Ptr)(state + kNUMOFPARTS), sizeof(state) - sizeof(gParts));
  180.                 myPB.ioParam.ioCompletion = nil;
  181.                 myPB.ioParam.ioBuffer = (Ptr)&state;
  182.                 myPB.ioParam.ioReqCount = sizeof(state);
  183.                 myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
  184.                 myPB.ioParam.ioRefNum = tmpFRefN;
  185.                 myPB.ioParam.ioPosOffset = 0L;
  186.                 (void)PBWriteAsync(&myPB);    // in temporary file
  187.                 while (myPB.ioParam.ioResult > 0) {
  188.                     SystemTask();
  189.                     (void)EventAvail(everyEvent, &dummyEv);
  190.                     }
  191.                 (void)FSClose(tmpFRefN);
  192.                 if ((err = myPB.ioParam.ioResult) == noErr) {
  193.                     somethingInTheTrash = true;
  194.                     err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE, kFTY_REG,
  195.                                         kSTR_ApplicationName, smSystemScript);
  196.                     }
  197.                 }
  198.         readyBuffer = PrepareBufferFromList();
  199.         UnloadSeg(PrepareBufferFromList);
  200.         if (readyBuffer) {
  201.             MyNumToString(TickCount() + 2, tempFName);
  202.             (void)FSMakeFSSpecCompat(thisVRefNum, thisDirID, (ConstStr255Param)&tempFName, &thisFSSpec);
  203.             if ((err = FSpCreateCompat(&thisFSSpec, kFCR_MINE, kFTY_CSTORE,
  204.                                 smSystemScript)) == noErr)
  205.                 if ((err = FSpOpenDFCompat(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  206.                     err = WriteMicroprogramData(readyBuffer, &thisFSSpec, kFTY_CSTORE, tmpFRefN,
  207.                                                 smSystemScript);
  208.                     UnloadSeg(WriteMicroprogramData);
  209.                     (void)FSClose(tmpFRefN);
  210.                     if (err == noErr)
  211.                         err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE, kFTY_CSTORE,
  212.                                             kSTR_ApplicationName, smSystemScript);
  213.                     }
  214.             }
  215.         }
  216.     }
  217. if (gMMemory) {
  218.     DisposePtr(gMMemory);
  219.     gMMemory = nil;
  220.     }
  221. if (somethingInTheTrash)
  222.     (void)StopAlert_UPP(kALRT_MEMTOOFULL, nil);
  223. tempA5 = SetA5(tempA5);
  224. ExitToShell();
  225. return 0;
  226. }
  227. */
  228.  
  229. /* SetupVertScrollBar: adjusts the vertical scrollbar to the right border
  230. of the window */
  231.  
  232. void SetupVertScrollBar(WindowPtr wind, ControlHandle scrollB)
  233. {
  234. MoveControl(scrollB, PRCT_R(wind) - kScrollbarAdjust, PRCT_T(wind) - 1);
  235. SizeControl(scrollB, kScrollbarWidth, PRCT_B(wind) - PRCT_T(wind) - 13);
  236. }
  237.  
  238. /* SetupHorzScrollBar: adjusts the horizontal scrollbar to the bottom border
  239. of the window */
  240.  
  241. void SetupHorzScrollBar(WindowPtr wind, ControlHandle scrollB)
  242. {
  243. MoveControl(scrollB, PRCT_L(wind) - 1, PRCT_B(wind) - kScrollbarAdjust);
  244. SizeControl(scrollB, PRCT_R(wind) - PRCT_L(wind) - 13, kScrollbarWidth);
  245. }
  246.  
  247. /* myStdFilterProc: standard filter proc for all alerts & simple dialogs
  248. WITH a cancel button */
  249.  
  250. pascal Boolean myStdFilterProc(DialogPtr theD, EventRecord *thEv, short *iHit)
  251. {
  252. GrafPtr    savePort;
  253. Boolean    retVal;
  254.  
  255. GetPort(&savePort);
  256. SetPort(theD);
  257. switch (thEv->what) {
  258.     case updateEvt:
  259.         if (theD != (DialogPtr)thEv->message) {
  260.             DoUpdate(thEv);
  261.             thEv->what = nullEvent;
  262.             return false;
  263.             }
  264.         break;
  265.     case activateEvt:
  266.         if (thEv->modifiers & activeFlag) {
  267.             (void)SetDialogDefaultItem(theD, ((DialogPeek)theD)->aDefItem);
  268.             (void)SetDialogCancelItem(theD, cancel);
  269.             (void)SetDialogTracksCursor(theD, false);
  270.             }
  271.         if (theD != (DialogPtr)thEv->message) {
  272.             DoActivate(thEv);
  273.             thEv->what = nullEvent;
  274.             return false;
  275.             }
  276.         break;
  277.     }
  278. retVal = StdFilterProc(theD, thEv, iHit);
  279. SetPort(savePort);
  280. return retVal;
  281. }
  282.  
  283. /* myStdFilterProcNoCancel: standard filter proc for all alerts & simple dialogs
  284. WITHOUT a cancel button */
  285.  
  286. pascal Boolean myStdFilterProcNoCancel(DialogPtr theD, EventRecord *thEv, short *iHit)
  287. {
  288. GrafPtr    savePort;
  289. register Boolean    retVal;
  290.  
  291. GetPort(&savePort);
  292. SetPort(theD);
  293. switch (thEv->what) {
  294.     case updateEvt:
  295.         if (theD != (DialogPtr)thEv->message) {
  296.             DoUpdate(thEv);
  297.             thEv->what = nullEvent;
  298.             return false;
  299.             }
  300.         break;
  301.     case activateEvt:
  302.         if (thEv->modifiers & activeFlag) {
  303.             (void)SetDialogDefaultItem(theD, ((DialogPeek)theD)->aDefItem);
  304.             (void)SetDialogTracksCursor(theD, false);
  305.             }
  306.         if (theD != (DialogPtr)thEv->message) {
  307.             DoActivate(thEv);
  308.             thEv->what = nullEvent;
  309.             return false;
  310.             }
  311.         break;
  312.     }
  313. retVal = StdFilterProc(theD, thEv, iHit);
  314. SetPort(savePort);
  315. return(retVal);
  316. }
  317.  
  318. /* general error alert displaying error code */
  319.  
  320. void ErrorAlert(OSErr reason)
  321. {
  322. Str255    tempS;
  323.  
  324. MyNumToString(reason, tempS);
  325. ParamText(tempS, nil, nil, nil);
  326. (void)StopAlert_UPP(kALRT_GENERICERROR, myStdFilterProcNoCancel);
  327. }
  328.  
  329. #pragma segment Rare
  330.  
  331. /* ResetRegisters: sets the main registers to their default values */
  332.  
  333. void ResetRegisters(void)
  334. {
  335. gRegs[kREG_PC] = gPrefs.DefPCValue;
  336. gRegs[kREG_SP] = gPrefs.DefSPValue;
  337. gParts[kP_MPC - kFIRST_PICT] = 0;
  338. }
  339.  
  340. /* ResetMemory: sets some important "System" memory locations */
  341.  
  342. void ResetMemory(void)
  343. {
  344. register Handle IllHandler = Get1Resource(krHandlers, kIllegalHandlerCode);
  345.  
  346. ((short *)gMMemory)[gPrefs.DefSPValue] = kPCExitToShell;
  347. ((short *)gMMemory)[kIllegalVector] = kIllegalHandler;
  348. BlockMoveData(*IllHandler, (Ptr)((short *)gMMemory + kIllegalHandler),
  349.             InlineGetHandleSize(IllHandler));
  350. }
  351.  
  352. #pragma segment Main
  353.  
  354. #endif
  355.  
  356.