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.

70 lines
1.8 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. #define BITS(x, start, len) (((x) >> (start)) & ((1 << (len)) - 1))
  8. #define OP(insn) BITS(insn, 26, 6)
  9. #define RS(insn) BITS(insn, 21, 5)
  10. #define RT(insn) BITS(insn, 16, 5)
  11. #define RD(insn) BITS(insn, 11, 5)
  12. #define COMMENT(insn) BITS(insn, 6, 20)
  13. #define IMM5(insn) BITS(insn, 6, 5)
  14. #define OP2(insn) BITS(insn, 0, 6)
  15. #define IMM(insn) BITS(insn, 0, 16)
  16. static cpu_insn_handler primary_insn_handler[TABLE_SIZE];
  17. static cpu_insn_handler secondary_insn_handler[TABLE_SIZE];
  18. void insn_execute(cpu_t *cpu, uint32_t insn) {
  19. const op_primary_t op = OP(insn);
  20. if (op == SPECIAL) {
  21. const op_secondary_t op2 = OP2(insn);
  22. if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL) {
  23. fatal("Unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn,
  24. op, op2);
  25. }
  26. secondary_insn_handler[op2](cpu, insn);
  27. }
  28. if (op > TABLE_SIZE || primary_insn_handler[op] == NULL) {
  29. fatal("Unsupported instruction: insn=%08x, op=%02x", insn, op);
  30. }
  31. primary_insn_handler[op](cpu, insn);
  32. }
  33. void insn_lw(cpu_t *cpu, uint32_t insn) {
  34. const uint8_t rt = RT(insn);
  35. const uint8_t rs = RS(insn);
  36. const uint16_t imm = IMM(insn);
  37. debug("LW %s, [%s + %x]", REG_NAMES[rt], REG_NAMES[rs], imm);
  38. cpu->regs[rt] = cpu_read32(cpu, cpu->regs[rs] + imm);
  39. }
  40. void insn_srl(cpu_t *cpu, uint32_t insn) {
  41. const uint8_t rd = RT(insn);
  42. const uint8_t rt = RT(insn);
  43. const uint8_t imm5 = IMM5(insn);
  44. debug("SRL %s, %s, %u", REG_NAMES[rd], REG_NAMES[rt], imm5);
  45. cpu->regs[rd] = cpu->regs[rt] >> imm5;
  46. }
  47. static cpu_insn_handler primary_insn_handler[TABLE_SIZE] = {
  48. [LW] = insn_lw,
  49. };
  50. static cpu_insn_handler secondary_insn_handler[TABLE_SIZE] = {
  51. [SRL] = insn_srl,
  52. };