home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-03-13 | 3.5 KB | 173 lines | [TEXT/KAHL] |
- /************************************************************
- *
- *
- * Unit containing instruction code helping routines.
- *
- * by Adrian Bool in cooperation with Graham Cox.
- *
- * copyright © phantasm coding 1992.
- *
- *
- ************************************************************/
-
- #include "Global.h"
- #include "Code.h"
-
-
- void assembleOpcode(pdpTableRef opCode,short pass,rBlock theDestination,short *size,pHandle theProgram)
- {
- (pdp8[opCode]).itsFunction(pass,theDestination,size,theProgram);
- }
-
-
- pdpTableRef getOpcode(char *theText)
- {
- short x;
- short found;
-
- found = false;
-
- for (x=1 ; x <= noOfOpcodes ; x++)
- if (strcmp(theText,pdp8[x].mnemonic) == 0)
- {
- found = true;
- break;
- }
-
- if (found)
- return(x);
- else
- {
- AsmError = NoSuchOpcode;
- return(0);
- }
- }
-
-
- short getDataSection(char *theData , short *position , str255 result)
- {
- short x = 0;
-
- while(theData[*position] == ',') (*position)++; /* get to next data item */
-
- while(isalnum(theData[*position])) /* read in whole of item */
- {
- result[x] = theData[*position];
- (*position)++; x++;
- }
-
- result[x] = '\0'; /* terminate string */
-
- if (result[0] == '\0') return(false);
- else return(true);
- }
-
-
- void makeMemoryInstruction(pdpTableRef theInstruction,rBlock theResult,pHandle theProgram)
- {
- short anAddress;
-
- theResult[0] = pdp8[theInstruction].iNumber << 9; /* enter instuction code in top 4 bits */
- anAddress = getAddress(theProgram);
- theResult[0] |= anAddress; /* and put the address in bottom 7 bits */
- }
-
-
- addressType getAddress(pHandle theProgram)
- {
- char x;
- str255 aSegment;
- addressType anAddress,
- theAddress;
-
- sHandle sourceCode;
-
- sourceCode = (*theProgram)->sourceCode;
-
- getSection(sourceCode,aSegment);
-
- theAddress = 0;
-
- if (aSegment[0] == '*')
- {
- theAddress |= (1 << 8); /* set indirection bit */
- x = 1;
- }
- else x = 0;
-
- anAddress = decodeNumber(theProgram,aSegment,x);
-
- if (anAddress > 4095)
- AsmError = AddressOutOfBounds;
- else {
- if (anAddress < 128)
- { /* its zero page */
- theAddress |= (anAddress & 127); /* allow only bottom 6 bits through */
- }
- else
- {
- theAddress |= (1 << 7); /* set present-page bit */
- if (pageOf(anAddress) == thisPage(theProgram))
- theAddress = theAddress | (anAddress & 127);
- else
- AsmError = AddressOutOfRange;
- }
- return(theAddress);
- }
- return(0);
- }
-
-
- addressType decodeNumber(pHandle Program , char *aSegment , short offset)
- {
- wordType theValue;
-
- lHandle labelHeader;
-
- labelHeader = (*Program)->labelHeader;
-
- if (isalpha(aSegment[offset]))
- theValue = getLabelValue(labelHeader,&aSegment[offset]);
-
- if (isdigit(aSegment[offset]))
- theValue = atoi(&aSegment[offset]);
-
- if (!(isalpha(aSegment[offset]) || isdigit(aSegment[offset])))
- AsmError = SyntaxError;
-
- if (theValue >4095)
- {
- AsmError = BadNumber;
- return(0xFFFF);
- }
- else
- return(theValue);
- }
-
- int checkAddress(pHandle theProgram , addressType theAddress)
- {
- if ((theAddress < 4096) &&
- (pageOf(theAddress) == 0) || (pageOf(theAddress) == thisPage(theProgram)))
- return(true);
- else
- {
- AsmError = AddressOutOfBounds;
- return(false);
- }
- }
-
- short thisPage(pHandle theProgram)
- {
- return(pageOf((*(*theProgram)->objectCode)->address));
- }
-
-
- short pageOf(addressType anAddress)
- {
- short page;
-
- page = anAddress & 0xf80; /* mask out all except page info */
- page >>= 7; /* move to begining of the integer */
- return(page);
- }
-