Advertisement
Guest User

Untitled

a guest
May 26th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define TO_HEX(i) (i <= 9 ? '0' + i : 'a' - 10 + i)
  5. //char * toHex(int val);
  6. //void print_format(int * registers[], int regcc);
  7. //int set_cc(int value);
  8.  
  9. int main(int argc, char**argv) {
  10. short *buffer;
  11. unsigned long fileLength;
  12. int regs[11] = {0};
  13. int rego;
  14. int opcode;
  15. int pc = 1;
  16. int regcc = 0;
  17. unsigned short offset;
  18. printf("Initial state\n");
  19. print_format(regs, regcc);
  20. printf("==================\n");
  21. printf("after executing instruction");
  22. FILE*f;
  23. f = fopen(argv[1], "r");
  24. fseek(f, 0, SEEK_END);
  25. fileLength = ftell(f);
  26. fseek(f, 0, SEEK_SET);
  27. buffer = malloc(fileLength + 1);
  28. fread(buffer, fileLength, 1, f);
  29. fclose(f);
  30. int i = 0;
  31. for (i = 0; i < sizeof (buffer); i++) {
  32. buffer[i] = ((buffer[i] & 0x00FF) << 8 | ((buffer[i] & 0xFF00) >> 8));
  33. }
  34. regs[8] = buffer[0];
  35. for (i = 1; i < sizeof (buffer); i++) {
  36. pc++;
  37. opcode = (buffer[i] & 0xf000) >> 12;
  38. if (opcode != 15) {
  39. if (opcode == 2) {
  40. regs[8] = regs[8] + 1;
  41. rego = (buffer[i] & 14) >> 12;
  42. offset = buffer[i] & 0x01ff;
  43. regs[rego] = buffer[pc + offset];
  44. regs[9] = buffer[i];
  45. regcc = set_cc(regs[rego]);
  46. break;
  47. }
  48. }
  49. }
  50. print_format(regs, regcc);
  51. printf("==================\n");
  52. }
  53.  
  54. int set_cc(int value) {
  55. if (value < 0) {
  56. return -1;
  57. }
  58. if (value == 0x0000) {
  59. return 0;
  60. }
  61. if (value > 1) {
  62. return 1;
  63. }
  64. }
  65.  
  66. char * toHex(int val) {
  67. int x = val;
  68. char *res = (char *) malloc(sizeof (char) * 5);
  69. if (x <= 0xFFFF) {
  70. res[0] = TO_HEX(((x & 0xF000) >> 12));
  71. res[1] = TO_HEX(((x & 0x0F00) >> 8));
  72. res[2] = TO_HEX(((x & 0x00F0) >> 4));
  73. res[3] = TO_HEX((x & 0x000F));
  74. res[4] = '\0';
  75. }
  76. return res;
  77. }
  78.  
  79. void print_format(int * registers[], int regcc) {
  80. int i = 0;
  81. for (i; i < 11; i++) {
  82. if (i == 8) {
  83. printf("PC\t%s\n", toHex(registers[8]));
  84. } else if (i == 9) {
  85. printf("IR\t%s\n", toHex(registers[9]));
  86. } else if (i == 10) {
  87. if (regcc == 0x0000) {
  88. printf("CC\t%s\n", "Z");
  89. }
  90. if (regcc < 0) {
  91. printf("CC\t%s\n", "N");
  92. }
  93. if (regcc > 0) {
  94. printf("CC\t%s\n", "P");
  95. }
  96. } else {
  97. printf("R%d\t0x%s\n", i, toHex(registers[i]));
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement