psx emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

  1. #include "insn.h"
  2. #include "cpu.h"
  3. #include "util.h"
  4. #include <stddef.h>
  5. #include <stdio.h>
  6. #define TABLE_SIZE 0x34
  7. void insn_lw(cpu_t *cpu, uint32_t insn);
  8. static cpu_insn_handler primary_insn_handler[TABLE_SIZE] = {
  9. [LW] = insn_lw,
  10. };
  11. static cpu_insn_handler secondary_insn_handler[TABLE_SIZE] = {NULL};
  12. #define BITS(x, start, len) (((x) >> (start)) & ((1 << (len)) - 1))
  13. #define OP(insn) BITS(insn, 26, 6)
  14. #define RS(insn) BITS(insn, 21, 5)
  15. #define RT(insn) BITS(insn, 16, 5)
  16. #define RD(insn) BITS(insn, 11, 5)
  17. #define COMMENT(insn) BITS(insn, 6, 20)
  18. #define IMM5(insn) BITS(insn, 6, 5)
  19. #define OP2(insn) BITS(insn, 0, 6)
  20. #define IMM(insn) BITS(insn, 0, 16)
  21. void insn_execute(cpu_t *cpu, uint32_t insn)
  22. {
  23. const op_primary_t op = OP(insn);
  24. if (op == SPECIAL)
  25. {
  26. const op_secondary_t op2 = OP2(insn);
  27. if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL)
  28. {
  29. fatal("unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn,
  30. op, op2);
  31. }
  32. secondary_insn_handler[op2](cpu, insn);
  33. }
  34. if (op > TABLE_SIZE || primary_insn_handler[op] == NULL)
  35. {
  36. fatal("unsupported instruction: insn=%08x, op=%02x", insn, op);
  37. }
  38. primary_insn_handler[op](cpu, insn);
  39. }
  40. void insn_lw(cpu_t *cpu, uint32_t insn)
  41. {
  42. const uint8_t rs = RS(insn);
  43. const uint8_t rt = RT(insn);
  44. const uint16_t imm = IMM(insn);
  45. printf("LW R%u, [R%u + %x]\n", rt, rs, imm);
  46. cpu->regs[rt] = cpu_read32(cpu, cpu->regs[rs] + imm);
  47. }