cpu.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <stdio.h>
  2. unsigned int registers[16];
  3. unsigned int ir[5];
  4. unsigned int pc = 0x0000;
  5. #define OP(x) ((x>>26)&0x3F)
  6. #define FUNC(x) ((x>>0)&0x3F)
  7. int isWrite(unsigned int instruction)
  8. {
  9. switch(OP(instruction))
  10. {
  11. case 0x00: //maths
  12. case 0x08: //addi
  13. case 0x09: //addiu
  14. case 0x0A: //slti
  15. case 0x0B: //sltiu
  16. case 0x0C: //andi
  17. case 0x0D: //ori
  18. case 0x0F: //lui
  19. case 0x10: //mfc0
  20. case 0x20: //lb
  21. case 0x23: //lw
  22. case 0x24: //lbu
  23. case 0x25: //lhu
  24. return 1;
  25. }
  26. return 0;
  27. }
  28. int isMem(unsigned int instruction)
  29. {
  30. switch(OP(instruction))
  31. {
  32. case 0x20: //lb
  33. case 0x23: //lw
  34. case 0x24: //lbu
  35. case 0x25: //lhu
  36. case 0x28: //sb
  37. case 0x29: //sh
  38. case 0x2B: //sw
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. int isMath(unsigned int instruction)
  44. {
  45. switch(OP(instruction))
  46. {
  47. case 0x00: //maths
  48. case 0x08: //addi
  49. case 0x09: //addiu
  50. case 0x0A: //slti
  51. case 0x0B: //sltiu
  52. case 0x0C: //andi
  53. case 0x0D: //ori
  54. case 0x0F: //lui (maybe??)
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. int isRead(unsigned int instruction)
  60. {
  61. switch(OP(instruction))
  62. {
  63. case 0x00: //maths
  64. case 0x08: //addi
  65. case 0x09: //addiu
  66. case 0x0A: //slti
  67. case 0x0B: //sltiu
  68. case 0x0C: //andi
  69. case 0x0D: //ori
  70. case 0x28: //sb
  71. case 0x29: //sh
  72. case 0x2B: //sw
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. int isFetch(unsigned int instruction)
  78. {
  79. return 1;
  80. }
  81. void step()
  82. {
  83. int i;
  84. for(i=0;i<4;i++)
  85. {
  86. ir[i+1] = ir[i];
  87. }
  88. if(isWrite(ir[4])
  89. {
  90. //register write
  91. }
  92. if(isMem(ir[3])
  93. {
  94. //memory stuff
  95. }
  96. if(isMath(ir[2])
  97. {
  98. //do math
  99. }
  100. if(isRead(ir[1])
  101. {
  102. //register read
  103. }
  104. if(isFetch(ir[0])
  105. {
  106. //fetch
  107. ir[0] = memRead(pc);
  108. pc += 4;
  109. }
  110. }