Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to convert a hex represented decimal value to a hexadecimal value
- // -----------------------------------------------------------------------------
- int DecHex (unsigned int Decimal)
- {
- int Hexadecimal = 0x00000000, DecimalDigit, HexadecimalPlaceLoc = 0x00000000;
- int HexadecimalPlace [] = { 1,
- 10,
- 100,
- 1000,
- 10000,
- 100000,
- 1000000,
- 10000000,
- 0x00 };
- while (Decimal > 0x99999999)
- {
- Decimal = 0x99999999;
- }
- while (HexadecimalPlace [HexadecimalPlaceLoc] != 0x00)
- {
- DecimalDigit = Decimal & 0x0F;
- Decimal >>= 0x04;
- while (DecimalDigit != 0x00)
- {
- Hexadecimal += HexadecimalPlace [HexadecimalPlaceLoc];
- DecimalDigit--;
- }
- HexadecimalPlaceLoc++;
- }
- return (Hexadecimal);
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to load a line of text from the code
- // -----------------------------------------------------------------------------
- void LoadLine (char *Data, int &DataLoc, int DataSize, char *Line, int &LineSize)
- {
- int Character = Data [DataLoc] & 0xFF;
- LineSize = 0x00;
- while (Character != 0x0D && Character != 0x0A && Character != ';' && DataLoc < DataSize)
- {
- Line [LineSize++] = Character;
- Character = Data [++DataLoc] & 0xFF;
- }
- while (Character != 0x0A && DataLoc < DataSize)
- {
- Character = Data [DataLoc++] & 0xFF;
- }
- Line [LineSize] = 0x00;
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to load an opcode
- // -----------------------------------------------------------------------------
- void LoadOpcode (char *Line, int &LineLoc, int LineSize, char *String, int &StringSize)
- {
- int Character = Line [LineLoc] & 0xFF;
- StringSize = 0x00;
- if (Character == '-')
- {
- String [StringSize++] = Character;
- Character = Line [++LineLoc] & 0xFF;
- }
- while (Character >= '!' && Character <= '~' && Character != ',' && Character != ':' && Character != '.' && Character != '-' && LineLoc < LineSize)
- {
- if (Character >= 'a' && Character <= 'z')
- {
- Character -= 'a' - 'A';
- }
- String [StringSize++] = Character;
- Character = Line [++LineLoc] & 0xFF;
- }
- if (Character == ':' || Character == '.')
- {
- LineLoc++;
- }
- String [StringSize] = 0x00;
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to find an opcode
- // -----------------------------------------------------------------------------
- void FindOpcode (char *Line, int &LineLoc, int LineSize)
- {
- int Character = Line [LineLoc] & 0xFF;
- while (Character != 0x00 && Character < '!' || Character > '~' || Character == ',' && LineLoc < LineSize)
- {
- Character = Line [++LineLoc] & 0xFF;
- }
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to save a lable's string and binary address
- // -----------------------------------------------------------------------------
- void SaveLable (char *String, int StringLoc, int StringSize, char **Lables, int &LablesLoc, int BinaryLoc)
- {
- char *Lable = (char*) malloc (StringSize + 0x01);
- if (Lable == NULL)
- {
- printf (" Error; could not store lable (memory allocation)\n");
- return;
- }
- for (StringLoc = 0x00; StringLoc < StringSize + 0x01; StringLoc++)
- {
- Lable [StringLoc] = String [StringLoc];
- }
- Lables [LablesLoc++] = Lable;
- Lables [LablesLoc++] = (char*)BinaryLoc;
- Lables [LablesLoc] = NULL;
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to clear all lables in a lable list
- // -----------------------------------------------------------------------------
- void ClearLables (char **Lables, int Skip)
- {
- int LablesLoc = 0x00;
- char *Lable = Lables [LablesLoc++];
- while (Lable != NULL)
- {
- free (Lable);
- LablesLoc += Skip; // Skip address/size
- Lable = Lables [LablesLoc++];
- }
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to read an ASCII string as hex values
- // -----------------------------------------------------------------------------
- int GetHex (char *String, int &StringLoc, int &Value, int Range, int LineCount)
- {
- Value = 0x00;
- int Character = String [++StringLoc] & 0xFF;
- while (Character != 0x00 && Character >= '0' && Character <= '9' || Character >= 'A' && Character <= 'F' || Character >= 'a' && Character <= 'f')
- {
- if (Character > 'F')
- {
- Character -= 'a' - 'A';
- }
- if (Character > '9')
- {
- Character -= 'A' - ('9' + 0x01);
- }
- Character -= '0';
- Value <<= 0x04;
- Value |= Character & 0x0F;
- Character = String [++StringLoc] & 0xFF;
- }
- if (Character != 0x00)
- {
- printf (" Error; line %d, the dc value is not hexadecimal:\n %s\n", LineCount, String);
- return (0xFF);
- }
- if (Value > Range && Range != 0xFFFFFFFF)
- {
- printf (" Error; line %d, the dc value is too large for opcode:\n %s\n", LineCount, String);
- return (0xFF);
- }
- return (0x00);
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to read an ASCII string as binary values
- // -----------------------------------------------------------------------------
- int GetBin (char *String, int &StringLoc, int &Value, int Range, int LineCount)
- {
- Value = 0x00;
- int Character = String [++StringLoc] & 0xFF;
- while (Character != 0x00 && Character >= '0' && Character <= '1')
- {
- Character -= '0';
- Value <<= 0x01;
- Value |= Character & 0x01;
- Character = String [++StringLoc] & 0xFF;
- }
- if (Character != 0x00)
- {
- printf (" Error; line %d, the dc value is not binary:\n %s\n", LineCount, String);
- return (0xFF);
- }
- if (Value > Range && Range != 0xFFFFFFFF)
- {
- printf (" Error; line %d, the dc value is too large for opcode:\n %s\n", LineCount, String);
- return (0xFF);
- }
- return (0x00);
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to read an ASCII string as decimal values (or lable reserved)
- // -----------------------------------------------------------------------------
- int GetDec (char *String, int &StringLoc, int &Value, int Range, int LineCount, int StringSize, int Size, char **LablesIn, int &LablesInLoc, int BinaryLoc)
- {
- Value = 0x00;
- int Character = String [StringLoc] & 0xFF;
- while (Character != 0x00 && Character >= '0' && Character <= '9')
- {
- Character -= '0';
- Value <<= 0x04;
- Value |= Character & 0x0F;
- Character = String [++StringLoc] & 0xFF;
- }
- Value = DecHex (Value);
- if (Character != 0x00)
- {
- // --- Lable ---
- SaveLable (String, StringLoc, StringSize, LablesIn, LablesInLoc, BinaryLoc);
- LablesIn [LablesInLoc++] = (char*) Size;
- LablesIn [LablesInLoc] = NULL;
- Value = 0x00; // force value to 00 for now...
- }
- else if (Value > Range && Range != 0xFFFFFFFF)
- {
- printf (" Error; line %d, the dc value is too large for opcode:\n %s\n", LineCount, String);
- return (0xFF);
- }
- return (0x00);
- }
- // =============================================================================
- // -----------------------------------------------------------------------------
- // Subroutine to assemble dc code
- // -----------------------------------------------------------------------------
- bool AssembleCode (char *&Data, int &DataSize)
- {
- char *LablesIn [0x1000]; // Lables inside opcodes (Using lables)
- char *LablesOut [0x1000]; // Lables outside opcodes (Actual lables)
- int LablesInLoc = 0x00;
- int LablesOutLoc = 0x00;
- LablesIn [LablesInLoc] = NULL;
- LablesOut [LablesOutLoc] = NULL;
- char Line [0x1000]; int LineSize, LineLoc;
- char String [0x0400]; int StringSize, StringLoc;
- int BinarySize = 0x1000;
- int BinaryLoc = 0x00;
- char *Binary = (char*) malloc (BinarySize);
- if (Binary == NULL)
- {
- printf (" Error; could not allocate memory for binary storage\n");
- return (TRUE);
- }
- int DataLoc = 0x00;
- int LineCount = 0x00;
- while (DataLoc < DataSize)
- {
- if (BinaryLoc >= (BinarySize - 0x0800))
- {
- BinarySize += 0x0800;
- char *BinaryNew = (char*) realloc (Binary, BinarySize);
- if (BinaryNew == NULL)
- {
- printf (" Error; could not reallocate binary storage\n");
- free (Binary);
- ClearLables (LablesIn, 0x02);
- ClearLables (LablesOut, 0x01);
- return (TRUE);
- }
- Binary = BinaryNew;
- }
- LineCount++;
- LoadLine (Data, DataLoc, DataSize, Line, LineSize);
- int LineLoc = 0x00;
- int Character = Line [LineLoc];
- if (Character >= '!' && Character <= '~')
- {
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- // lable in String
- SaveLable (String, StringLoc, StringSize, LablesOut, LablesOutLoc, BinaryLoc);
- }
- FindOpcode (Line, LineLoc, LineSize);
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- if ((String [StringSize - 0x01] & 0xFF) == ':')
- {
- // lable in String
- SaveLable (String, StringLoc, StringSize, LablesOut, LablesOutLoc, BinaryLoc);
- FindOpcode (Line, LineLoc, LineSize);
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- }
- if ((String [0x00] & 0xFF) != 0x00)
- {
- if (strcmp (String, "DC") == 0x00)
- {
- FindOpcode (Line, LineLoc, LineSize);
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- int Range = 0xFFFFFFFF;
- int Size = 0x04;
- if (strcmp (String, "B") == 0x00)
- {
- Range = 0xFF;
- Size = 0x01;
- FindOpcode (Line, LineLoc, LineSize);
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- }
- else if (strcmp (String, "W") == 0x00)
- {
- Range = 0xFFFF;
- Size = 0x02;
- FindOpcode (Line, LineLoc, LineSize);
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- }
- else if (strcmp (String, "L") == 0x00)
- {
- Range = 0xFFFFFFFF;
- Size = 0x04;
- FindOpcode (Line, LineLoc, LineSize);
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- }
- while ((String [0x00] & 0xFF) != 0x00)
- {
- if ((String [0x00] & 0xFF) != 0x00)
- {
- StringLoc = 0x00;
- int Value;
- switch (String [StringLoc] & 0xFF)
- {
- case '$': // hex
- {
- if (GetHex (String, StringLoc, Value, Range, LineCount) != 0x00)
- {
- free (Binary);
- ClearLables (LablesIn, 0x02);
- ClearLables (LablesOut, 0x01);
- return (TRUE);
- }
- int SizeCount = 0x00;
- while (SizeCount < Size)
- {
- SizeCount++;
- Binary [BinaryLoc++] = Value >> ((Size - SizeCount) * 0x08);
- }
- }
- break;
- case '%': // binary
- {
- if (GetBin (String, StringLoc, Value, Range, LineCount) != 0x00)
- {
- free (Binary);
- ClearLables (LablesIn, 0x02);
- ClearLables (LablesOut, 0x01);
- return (TRUE);
- }
- int SizeCount = 0x00;
- while (SizeCount < Size)
- {
- SizeCount++;
- Binary [BinaryLoc++] = Value >> ((Size - SizeCount) * 0x08);
- }
- }
- break;
- case '-': // Lables beginning with a subtract symbol
- {
- // Do nothing (Ignoring all -Lables and assuming 00 offset)
- }
- break;
- default: // decimal/lable
- {
- if (GetDec (String, StringLoc, Value, Range, LineCount, StringSize, Size, LablesIn, LablesInLoc, BinaryLoc) != 0x00)
- {
- free (Binary);
- ClearLables (LablesIn, 0x02);
- ClearLables (LablesOut, 0x01);
- return (TRUE);
- }
- int SizeCount = 0x00;
- while (SizeCount < Size)
- {
- SizeCount++;
- Binary [BinaryLoc++] = Value >> ((Size - SizeCount) * 0x08);
- }
- }
- break;
- }
- }
- FindOpcode (Line, LineLoc, LineSize);
- LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
- }
- }
- else if (strcmp (String, "EVEN") == 0x00)
- {
- if ((BinaryLoc & 0x01) != 0x00)
- {
- Binary [BinaryLoc++] = 0x00;
- }
- }
- else
- {
- printf (" Error; line %d, opcode not recognised:\n %s\n", LineCount, String);
- free (Binary);
- ClearLables (LablesIn, 0x02);
- ClearLables (LablesOut, 0x01);
- return (TRUE);
- }
- }
- }
- BinarySize = BinaryLoc;
- LablesInLoc = 0x00;
- char *InLable; int InAddress, InSize;
- char *OutLable; int OutAddress;
- do
- {
- InLable = LablesIn [LablesInLoc++];
- if (InLable != NULL)
- {
- InAddress = (int) LablesIn [LablesInLoc++];
- InSize = (int) LablesIn [LablesInLoc++];
- LablesOutLoc = 0x00;
- do
- {
- OutLable = LablesOut [LablesOutLoc++];
- if (OutLable != NULL)
- {
- OutAddress = (int) LablesOut [LablesOutLoc++];
- if (strcmp (InLable, OutLable) == 0x00)
- {
- switch (InSize)
- {
- case 0x01: // Byte
- {
- Binary [InAddress] = OutAddress;
- }
- break;
- case 0x02: // Word
- {
- Binary [InAddress++] = OutAddress >> 0x08;
- Binary [InAddress] = OutAddress;
- }
- break;
- case 0x04: // Long-word
- {
- Binary [InAddress++] = OutAddress >> 0x18;
- Binary [InAddress++] = OutAddress >> 0x10;
- Binary [InAddress++] = OutAddress >> 0x08;
- Binary [InAddress] = OutAddress;
- }
- break;
- }
- OutLable = NULL;
- }
- }
- }
- while (OutLable != NULL);
- }
- }
- while (InLable != NULL);
- ClearLables (LablesIn, 0x02);
- ClearLables (LablesOut, 0x01);
- free (Data);
- Data = Binary;
- DataSize = BinarySize;
- return (FALSE);
- }
- // =============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement