Advertisement
Guest User

MrJester

a guest
Feb 20th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.38 KB | None | 0 0
  1. // =============================================================================
  2. // -----------------------------------------------------------------------------
  3. // Subroutine to convert a hex represented decimal value to a hexadecimal value
  4. // -----------------------------------------------------------------------------
  5.  
  6. int DecHex (unsigned int Decimal)
  7.  
  8. {
  9. int Hexadecimal = 0x00000000, DecimalDigit, HexadecimalPlaceLoc = 0x00000000;
  10. int HexadecimalPlace [] = { 1,
  11. 10,
  12. 100,
  13. 1000,
  14. 10000,
  15. 100000,
  16. 1000000,
  17. 10000000,
  18. 0x00 };
  19. while (Decimal > 0x99999999)
  20. {
  21. Decimal = 0x99999999;
  22. }
  23. while (HexadecimalPlace [HexadecimalPlaceLoc] != 0x00)
  24. {
  25. DecimalDigit = Decimal & 0x0F;
  26. Decimal >>= 0x04;
  27. while (DecimalDigit != 0x00)
  28. {
  29. Hexadecimal += HexadecimalPlace [HexadecimalPlaceLoc];
  30. DecimalDigit--;
  31. }
  32. HexadecimalPlaceLoc++;
  33. }
  34. return (Hexadecimal);
  35. }
  36.  
  37. // =============================================================================
  38. // -----------------------------------------------------------------------------
  39. // Subroutine to load a line of text from the code
  40. // -----------------------------------------------------------------------------
  41.  
  42. void LoadLine (char *Data, int &DataLoc, int DataSize, char *Line, int &LineSize)
  43.  
  44. {
  45. int Character = Data [DataLoc] & 0xFF;
  46. LineSize = 0x00;
  47. while (Character != 0x0D && Character != 0x0A && Character != ';' && DataLoc < DataSize)
  48. {
  49. Line [LineSize++] = Character;
  50. Character = Data [++DataLoc] & 0xFF;
  51. }
  52. while (Character != 0x0A && DataLoc < DataSize)
  53. {
  54. Character = Data [DataLoc++] & 0xFF;
  55. }
  56. Line [LineSize] = 0x00;
  57. }
  58.  
  59. // =============================================================================
  60. // -----------------------------------------------------------------------------
  61. // Subroutine to load an opcode
  62. // -----------------------------------------------------------------------------
  63.  
  64. void LoadOpcode (char *Line, int &LineLoc, int LineSize, char *String, int &StringSize)
  65.  
  66. {
  67. int Character = Line [LineLoc] & 0xFF;
  68. StringSize = 0x00;
  69. if (Character == '-')
  70. {
  71. String [StringSize++] = Character;
  72. Character = Line [++LineLoc] & 0xFF;
  73. }
  74. while (Character >= '!' && Character <= '~' && Character != ',' && Character != ':' && Character != '.' && Character != '-' && LineLoc < LineSize)
  75. {
  76. if (Character >= 'a' && Character <= 'z')
  77. {
  78. Character -= 'a' - 'A';
  79. }
  80. String [StringSize++] = Character;
  81. Character = Line [++LineLoc] & 0xFF;
  82. }
  83. if (Character == ':' || Character == '.')
  84. {
  85. LineLoc++;
  86. }
  87. String [StringSize] = 0x00;
  88. }
  89.  
  90. // =============================================================================
  91. // -----------------------------------------------------------------------------
  92. // Subroutine to find an opcode
  93. // -----------------------------------------------------------------------------
  94.  
  95. void FindOpcode (char *Line, int &LineLoc, int LineSize)
  96.  
  97. {
  98. int Character = Line [LineLoc] & 0xFF;
  99. while (Character != 0x00 && Character < '!' || Character > '~' || Character == ',' && LineLoc < LineSize)
  100. {
  101. Character = Line [++LineLoc] & 0xFF;
  102. }
  103. }
  104.  
  105. // =============================================================================
  106. // -----------------------------------------------------------------------------
  107. // Subroutine to save a lable's string and binary address
  108. // -----------------------------------------------------------------------------
  109.  
  110. void SaveLable (char *String, int StringLoc, int StringSize, char **Lables, int &LablesLoc, int BinaryLoc)
  111.  
  112. {
  113. char *Lable = (char*) malloc (StringSize + 0x01);
  114. if (Lable == NULL)
  115. {
  116. printf (" Error; could not store lable (memory allocation)\n");
  117. return;
  118. }
  119. for (StringLoc = 0x00; StringLoc < StringSize + 0x01; StringLoc++)
  120. {
  121. Lable [StringLoc] = String [StringLoc];
  122. }
  123. Lables [LablesLoc++] = Lable;
  124. Lables [LablesLoc++] = (char*)BinaryLoc;
  125. Lables [LablesLoc] = NULL;
  126. }
  127.  
  128. // =============================================================================
  129. // -----------------------------------------------------------------------------
  130. // Subroutine to clear all lables in a lable list
  131. // -----------------------------------------------------------------------------
  132.  
  133. void ClearLables (char **Lables, int Skip)
  134.  
  135. {
  136. int LablesLoc = 0x00;
  137. char *Lable = Lables [LablesLoc++];
  138. while (Lable != NULL)
  139. {
  140. free (Lable);
  141. LablesLoc += Skip; // Skip address/size
  142. Lable = Lables [LablesLoc++];
  143. }
  144. }
  145.  
  146. // =============================================================================
  147. // -----------------------------------------------------------------------------
  148. // Subroutine to read an ASCII string as hex values
  149. // -----------------------------------------------------------------------------
  150.  
  151. int GetHex (char *String, int &StringLoc, int &Value, int Range, int LineCount)
  152.  
  153. {
  154. Value = 0x00;
  155. int Character = String [++StringLoc] & 0xFF;
  156. while (Character != 0x00 && Character >= '0' && Character <= '9' || Character >= 'A' && Character <= 'F' || Character >= 'a' && Character <= 'f')
  157. {
  158. if (Character > 'F')
  159. {
  160. Character -= 'a' - 'A';
  161. }
  162. if (Character > '9')
  163. {
  164. Character -= 'A' - ('9' + 0x01);
  165. }
  166. Character -= '0';
  167. Value <<= 0x04;
  168. Value |= Character & 0x0F;
  169. Character = String [++StringLoc] & 0xFF;
  170. }
  171. if (Character != 0x00)
  172. {
  173. printf (" Error; line %d, the dc value is not hexadecimal:\n %s\n", LineCount, String);
  174. return (0xFF);
  175. }
  176. if (Value > Range && Range != 0xFFFFFFFF)
  177. {
  178. printf (" Error; line %d, the dc value is too large for opcode:\n %s\n", LineCount, String);
  179. return (0xFF);
  180. }
  181. return (0x00);
  182. }
  183.  
  184. // =============================================================================
  185. // -----------------------------------------------------------------------------
  186. // Subroutine to read an ASCII string as binary values
  187. // -----------------------------------------------------------------------------
  188.  
  189. int GetBin (char *String, int &StringLoc, int &Value, int Range, int LineCount)
  190.  
  191. {
  192. Value = 0x00;
  193. int Character = String [++StringLoc] & 0xFF;
  194. while (Character != 0x00 && Character >= '0' && Character <= '1')
  195. {
  196. Character -= '0';
  197. Value <<= 0x01;
  198. Value |= Character & 0x01;
  199. Character = String [++StringLoc] & 0xFF;
  200. }
  201. if (Character != 0x00)
  202. {
  203. printf (" Error; line %d, the dc value is not binary:\n %s\n", LineCount, String);
  204. return (0xFF);
  205. }
  206. if (Value > Range && Range != 0xFFFFFFFF)
  207. {
  208. printf (" Error; line %d, the dc value is too large for opcode:\n %s\n", LineCount, String);
  209. return (0xFF);
  210. }
  211. return (0x00);
  212. }
  213.  
  214. // =============================================================================
  215. // -----------------------------------------------------------------------------
  216. // Subroutine to read an ASCII string as decimal values (or lable reserved)
  217. // -----------------------------------------------------------------------------
  218.  
  219. int GetDec (char *String, int &StringLoc, int &Value, int Range, int LineCount, int StringSize, int Size, char **LablesIn, int &LablesInLoc, int BinaryLoc)
  220.  
  221. {
  222. Value = 0x00;
  223. int Character = String [StringLoc] & 0xFF;
  224. while (Character != 0x00 && Character >= '0' && Character <= '9')
  225. {
  226. Character -= '0';
  227. Value <<= 0x04;
  228. Value |= Character & 0x0F;
  229. Character = String [++StringLoc] & 0xFF;
  230. }
  231. Value = DecHex (Value);
  232. if (Character != 0x00)
  233. {
  234. // --- Lable ---
  235.  
  236. SaveLable (String, StringLoc, StringSize, LablesIn, LablesInLoc, BinaryLoc);
  237. LablesIn [LablesInLoc++] = (char*) Size;
  238. LablesIn [LablesInLoc] = NULL;
  239. Value = 0x00; // force value to 00 for now...
  240. }
  241. else if (Value > Range && Range != 0xFFFFFFFF)
  242. {
  243. printf (" Error; line %d, the dc value is too large for opcode:\n %s\n", LineCount, String);
  244. return (0xFF);
  245. }
  246. return (0x00);
  247. }
  248.  
  249. // =============================================================================
  250. // -----------------------------------------------------------------------------
  251. // Subroutine to assemble dc code
  252. // -----------------------------------------------------------------------------
  253.  
  254. bool AssembleCode (char *&Data, int &DataSize)
  255.  
  256. {
  257. char *LablesIn [0x1000]; // Lables inside opcodes (Using lables)
  258. char *LablesOut [0x1000]; // Lables outside opcodes (Actual lables)
  259. int LablesInLoc = 0x00;
  260. int LablesOutLoc = 0x00;
  261. LablesIn [LablesInLoc] = NULL;
  262. LablesOut [LablesOutLoc] = NULL;
  263. char Line [0x1000]; int LineSize, LineLoc;
  264. char String [0x0400]; int StringSize, StringLoc;
  265. int BinarySize = 0x1000;
  266. int BinaryLoc = 0x00;
  267. char *Binary = (char*) malloc (BinarySize);
  268. if (Binary == NULL)
  269. {
  270. printf (" Error; could not allocate memory for binary storage\n");
  271. return (TRUE);
  272. }
  273. int DataLoc = 0x00;
  274. int LineCount = 0x00;
  275. while (DataLoc < DataSize)
  276. {
  277. if (BinaryLoc >= (BinarySize - 0x0800))
  278. {
  279. BinarySize += 0x0800;
  280. char *BinaryNew = (char*) realloc (Binary, BinarySize);
  281. if (BinaryNew == NULL)
  282. {
  283. printf (" Error; could not reallocate binary storage\n");
  284. free (Binary);
  285. ClearLables (LablesIn, 0x02);
  286. ClearLables (LablesOut, 0x01);
  287. return (TRUE);
  288. }
  289. Binary = BinaryNew;
  290. }
  291. LineCount++;
  292. LoadLine (Data, DataLoc, DataSize, Line, LineSize);
  293. int LineLoc = 0x00;
  294. int Character = Line [LineLoc];
  295. if (Character >= '!' && Character <= '~')
  296. {
  297. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  298. // lable in String
  299. SaveLable (String, StringLoc, StringSize, LablesOut, LablesOutLoc, BinaryLoc);
  300. }
  301. FindOpcode (Line, LineLoc, LineSize);
  302. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  303. if ((String [StringSize - 0x01] & 0xFF) == ':')
  304. {
  305. // lable in String
  306. SaveLable (String, StringLoc, StringSize, LablesOut, LablesOutLoc, BinaryLoc);
  307.  
  308. FindOpcode (Line, LineLoc, LineSize);
  309. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  310. }
  311. if ((String [0x00] & 0xFF) != 0x00)
  312. {
  313. if (strcmp (String, "DC") == 0x00)
  314. {
  315. FindOpcode (Line, LineLoc, LineSize);
  316. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  317. int Range = 0xFFFFFFFF;
  318. int Size = 0x04;
  319. if (strcmp (String, "B") == 0x00)
  320. {
  321. Range = 0xFF;
  322. Size = 0x01;
  323. FindOpcode (Line, LineLoc, LineSize);
  324. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  325. }
  326. else if (strcmp (String, "W") == 0x00)
  327. {
  328. Range = 0xFFFF;
  329. Size = 0x02;
  330. FindOpcode (Line, LineLoc, LineSize);
  331. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  332. }
  333. else if (strcmp (String, "L") == 0x00)
  334. {
  335. Range = 0xFFFFFFFF;
  336. Size = 0x04;
  337. FindOpcode (Line, LineLoc, LineSize);
  338. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  339. }
  340. while ((String [0x00] & 0xFF) != 0x00)
  341. {
  342. if ((String [0x00] & 0xFF) != 0x00)
  343. {
  344. StringLoc = 0x00;
  345. int Value;
  346. switch (String [StringLoc] & 0xFF)
  347. {
  348. case '$': // hex
  349. {
  350. if (GetHex (String, StringLoc, Value, Range, LineCount) != 0x00)
  351. {
  352. free (Binary);
  353. ClearLables (LablesIn, 0x02);
  354. ClearLables (LablesOut, 0x01);
  355. return (TRUE);
  356. }
  357. int SizeCount = 0x00;
  358. while (SizeCount < Size)
  359. {
  360. SizeCount++;
  361. Binary [BinaryLoc++] = Value >> ((Size - SizeCount) * 0x08);
  362. }
  363. }
  364. break;
  365. case '%': // binary
  366. {
  367. if (GetBin (String, StringLoc, Value, Range, LineCount) != 0x00)
  368. {
  369. free (Binary);
  370. ClearLables (LablesIn, 0x02);
  371. ClearLables (LablesOut, 0x01);
  372. return (TRUE);
  373. }
  374. int SizeCount = 0x00;
  375. while (SizeCount < Size)
  376. {
  377. SizeCount++;
  378. Binary [BinaryLoc++] = Value >> ((Size - SizeCount) * 0x08);
  379. }
  380. }
  381. break;
  382. case '-': // Lables beginning with a subtract symbol
  383. {
  384. // Do nothing (Ignoring all -Lables and assuming 00 offset)
  385. }
  386. break;
  387. default: // decimal/lable
  388. {
  389. if (GetDec (String, StringLoc, Value, Range, LineCount, StringSize, Size, LablesIn, LablesInLoc, BinaryLoc) != 0x00)
  390. {
  391. free (Binary);
  392. ClearLables (LablesIn, 0x02);
  393. ClearLables (LablesOut, 0x01);
  394. return (TRUE);
  395. }
  396. int SizeCount = 0x00;
  397. while (SizeCount < Size)
  398. {
  399. SizeCount++;
  400. Binary [BinaryLoc++] = Value >> ((Size - SizeCount) * 0x08);
  401. }
  402. }
  403. break;
  404. }
  405. }
  406. FindOpcode (Line, LineLoc, LineSize);
  407. LoadOpcode (Line, LineLoc, LineSize, String, StringSize);
  408. }
  409. }
  410. else if (strcmp (String, "EVEN") == 0x00)
  411. {
  412. if ((BinaryLoc & 0x01) != 0x00)
  413. {
  414. Binary [BinaryLoc++] = 0x00;
  415. }
  416. }
  417. else
  418. {
  419. printf (" Error; line %d, opcode not recognised:\n %s\n", LineCount, String);
  420. free (Binary);
  421. ClearLables (LablesIn, 0x02);
  422. ClearLables (LablesOut, 0x01);
  423. return (TRUE);
  424. }
  425. }
  426. }
  427. BinarySize = BinaryLoc;
  428. LablesInLoc = 0x00;
  429. char *InLable; int InAddress, InSize;
  430. char *OutLable; int OutAddress;
  431. do
  432. {
  433. InLable = LablesIn [LablesInLoc++];
  434. if (InLable != NULL)
  435. {
  436. InAddress = (int) LablesIn [LablesInLoc++];
  437. InSize = (int) LablesIn [LablesInLoc++];
  438. LablesOutLoc = 0x00;
  439. do
  440. {
  441. OutLable = LablesOut [LablesOutLoc++];
  442. if (OutLable != NULL)
  443. {
  444. OutAddress = (int) LablesOut [LablesOutLoc++];
  445. if (strcmp (InLable, OutLable) == 0x00)
  446. {
  447. switch (InSize)
  448. {
  449. case 0x01: // Byte
  450. {
  451. Binary [InAddress] = OutAddress;
  452. }
  453. break;
  454. case 0x02: // Word
  455. {
  456. Binary [InAddress++] = OutAddress >> 0x08;
  457. Binary [InAddress] = OutAddress;
  458. }
  459. break;
  460. case 0x04: // Long-word
  461. {
  462. Binary [InAddress++] = OutAddress >> 0x18;
  463. Binary [InAddress++] = OutAddress >> 0x10;
  464. Binary [InAddress++] = OutAddress >> 0x08;
  465. Binary [InAddress] = OutAddress;
  466. }
  467. break;
  468. }
  469. OutLable = NULL;
  470. }
  471. }
  472. }
  473. while (OutLable != NULL);
  474. }
  475. }
  476. while (InLable != NULL);
  477. ClearLables (LablesIn, 0x02);
  478. ClearLables (LablesOut, 0x01);
  479. free (Data);
  480. Data = Binary;
  481. DataSize = BinarySize;
  482. return (FALSE);
  483. }
  484.  
  485. // =============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement