Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MOVE 0
  5. #define ADDA 1
  6. #define SUB 2
  7. #define BRA 3
  8. #define CMP 4
  9. #define BEQ 5
  10. #define BNE 6
  11. #define EXG 7
  12. #define ADD 9
  13. #define STOP 15
  14.  
  15. void main(void) {
  16. unsigned short int PC = 0; // program counter
  17. unsigned short int D0 = 1; // data register 0
  18. unsigned short int D1 = 2; // data register 1
  19. unsigned short int A0 = 5; // adress register
  20. unsigned short int CCR = 0; // condition code register
  21. unsigned short int MAR; // memory address register
  22. unsigned short int MBR; // memory buffer register
  23. unsigned short int IR; // Instruction register
  24. unsigned short int operand; // the 8-bit operand from the IR
  25. unsigned short int source; // source operand
  26. unsigned short int destination; // destination value
  27. unsigned short int opcode; // the 4-bit op-code from the IR
  28. unsigned short int amode; // the 2-bit addressing mode
  29. unsigned short int direction; // the 1-bit direction flag
  30. unsigned short int memory[256]={0}; // the memory
  31. unsigned short int run = 1; // excution of program
  32. unsigned short int x=0;
  33. unsigned short int l=0;
  34. unsigned short int z=0;
  35. /* Instruction format:
  36. /* 7 6 5 4 3 2 1 0
  37. /* Bit 1 and 0 : 2-bit address mode
  38. 00 address mode = absolute
  39. 01 address mode = literal
  40. 10 address mode = indexed
  41. 11 address mode = relative
  42. /* Bit 2 : 1-bit direction (source/operand)
  43. /* Bit 3 : not used
  44. /* Bit 4 to 7 : 4-bit instruction code
  45. /* main loop */
  46. while (run)
  47. {
  48. memory[0]=0b00010001;
  49. memory[1]=2;
  50. memory[2]=10;
  51. //memory[3]=2;
  52. memory[5]=6;
  53. memory[8]=16;
  54. MAR = PC; // PC to MAR
  55. PC = PC + 1; // Increment PC
  56. MBR = memory[MAR]; // get next instruction
  57. IR = MBR; // copy MBR to IR
  58. opcode = IR; // store the op-code bits
  59.  
  60. MAR = PC; // PC to MAR
  61. PC = PC + 1; // Increment PC
  62. MBR = memory[MAR]; // get the operand
  63. IR = MBR; // copy MBR to IR
  64. operand = IR; // store the operand bits
  65.  
  66. amode = opcode & 0x03; // extract the address mode bits
  67. direction = (opcode & 0x04) >> 2;
  68. /* get data direction
  69. 0 = register to memory
  70. 1 = memory to register */
  71. opcode = opcode >> 4 ;
  72. run = 0;
  73. }// behery touch
  74. // get the 4-bit instruction code
  75. x=memory[A0];
  76. l=memory[D0];
  77. z=memory[x+l];
  78. printf("Opcode = %d\n",opcode);
  79. printf("amode = %d\n",amode);
  80. printf("Direction = %d\n",direction);
  81. /* use the address mode to get the source operand */
  82. printf("z before overwrite =%d\n",z);
  83. //printf("Memory[D1] before overwrite = %d\n",memory[D1]);
  84. switch (amode) {
  85. case 0: {
  86. source = memory[operand];
  87. break;
  88. } // Absolute
  89. case 1: {
  90. source = operand;
  91. break;
  92. } // Literal
  93. case 2: {
  94. source = memory[A0 + operand];
  95. break;
  96. } // Indexed
  97. case 3: {
  98. source = memory[PC + operand];
  99. break;
  100. } // PC relative
  101. }
  102.  
  103. /* now execute the instruction */
  104. switch (opcode) {
  105. case MOVE: {
  106. if (direction == 0)
  107. {z=x+4;
  108. printf("The Result of Move = %d\n",destination);}
  109. else
  110. D0 = source;
  111. if (D0 == 0)
  112. CCR = 1;
  113. else
  114. CCR = 0;
  115.  
  116. printf("The CCR = %d\n",CCR);
  117. break;
  118. }
  119. case ADDA: {
  120. if (direction == 0) {
  121. z=x+4 ;
  122. printf("The Result of z after overwrite = %d\n",z);
  123. if (destination == 0)
  124. CCR = 1;
  125. else
  126. CCR = 0;
  127. } else {
  128. D0 = l + source;
  129. printf("The Result of Add of D0 = %d\n",D0);
  130. if (D0 == 0)
  131. CCR = 1;
  132. else
  133. CCR = 0;
  134. }
  135. printf("The CCR = %d\n",CCR);
  136. break;
  137. }
  138.  
  139. case SUB: {
  140. if (direction == 0) {
  141. destination = D0 - source;
  142. printf("The Result of Sub = %d\n",destination);
  143. if (destination == 0)
  144. CCR = 1;
  145. else
  146. CCR = 0;
  147. } else {
  148. D0 = D0 - source;
  149. printf("The Result of Sub of D0 = %d\n",D0);
  150. if (D0 == 0)
  151. CCR = 1;
  152. else
  153. CCR = 0;
  154. }
  155. printf("The CCR = %d\n",CCR);
  156. break;
  157. }
  158.  
  159. case BRA: {
  160. if (amode == 0)
  161. {PC = operand;
  162. printf("The Result of BRA = %d\n",PC);}
  163. if (amode == 1)
  164. {PC = PC + operand;
  165. printf("The Result of BRA = %d\n",PC);}
  166. break;
  167. }
  168.  
  169. case CMP: {
  170. MBR = D0 - source;
  171. printf("The result of CMP = %d\n",MBR);
  172. if (MBR == 0)
  173. CCR = 1;
  174. else
  175. CCR = 0;
  176.  
  177. printf("The CCR = %d\n",CCR);
  178. break;
  179. }
  180. case BEQ: {
  181. if (CCR == 1) {
  182. printf("There is CCR\n");
  183. if (amode == 0)
  184. {PC = operand;
  185. printf("PC = %d",PC);}
  186. if (amode == 1)
  187. {PC = PC + operand;
  188. printf("PC = %d",PC);}
  189. }
  190. break;
  191. }
  192.  
  193. case BNE: {
  194. if (CCR != 1) {
  195. printf("There is no CCR\n");
  196. if (amode == 0)
  197. {PC = operand;
  198. printf("PC = %d",PC);}
  199. if (amode == 1)
  200. {PC = PC + operand;
  201. printf("PC = %d",PC);}
  202. }
  203. break;
  204. }
  205.  
  206. case EXG: {
  207. MBR = D0;
  208. D0 = A0;
  209. A0 = MBR;
  210. printf("A0 = %d\n",A0);
  211. break;
  212. }
  213. case ADD: {
  214. if (direction == 0) {
  215. memory[D1] = memory[D1] + memory[l] ;
  216. printf("The Result of Add of memory[D1] after overwrite = %d\n",memory[D1]);
  217. if (memory[D1] == 0)
  218. CCR = 1;
  219. else
  220. CCR = 0;
  221. } else {
  222. memory[l] = memory[l] + source;
  223. printf("The Result of Add of D0 = %d\n",D0);
  224. if (D0 == 0)
  225. CCR = 1;
  226. else
  227. CCR = 0;
  228. }
  229. printf("The CCR = %d\n",CCR);
  230. break;
  231. }
  232.  
  233. case STOP: {
  234. printf("STOPPED");
  235. run = 0;
  236. break;
  237. }
  238. }
  239. /* save result in memory if register to memory */
  240. if (direction == 0)
  241. switch (amode) {
  242. case 0: {
  243. memory[operand] = destination;
  244. //printf("%d",destination);
  245. break;
  246. } // Absolute
  247. case 1: {
  248. //printf("%d",destination);
  249. break;
  250. } // Literal
  251. case 2: {
  252. memory[A0 + operand] = destination;
  253. break;
  254. } // Indexed
  255. case 3: {
  256. memory[PC + operand] = destination;
  257. break;
  258. } // PC relative
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement