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.

58 lines
1.5 KiB

  1. #include "insn.h"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include "cpu.h"
  5. #include "log.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. const op_primary_t op = OP(insn);
  23. if (op == SPECIAL) {
  24. const op_secondary_t op2 = OP2(insn);
  25. if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL) {
  26. fatal("Unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn,
  27. op, op2);
  28. }
  29. secondary_insn_handler[op2](cpu, insn);
  30. }
  31. if (op > TABLE_SIZE || primary_insn_handler[op] == NULL) {
  32. fatal("Unsupported instruction: insn=%08x, op=%02x", insn, op);
  33. }
  34. primary_insn_handler[op](cpu, insn);
  35. }
  36. void insn_lw(cpu_t *cpu, uint32_t insn) {
  37. const uint8_t rs = RS(insn);
  38. const uint8_t rt = RT(insn);
  39. const uint16_t imm = IMM(insn);
  40. debug("LW %s, [%s + %x]", REG_NAMES[rt], REG_NAMES[rs], imm);
  41. cpu->regs[rt] = cpu_read32(cpu, cpu->regs[rs] + imm);
  42. }