luat_lib_nes.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. @module nes
  3. @summary nes模拟器
  4. @version 1.0
  5. @date 2023.4.10
  6. @tag LUAT_USE_NES
  7. */
  8. #include "luat_base.h"
  9. #include "luat_rtos.h"
  10. #include "luat_lcd.h"
  11. #include "luat_mem.h"
  12. #include "luat_rtos.h"
  13. #include "nes.h"
  14. #include "nes_port.h"
  15. #define LUAT_LOG_TAG "nes"
  16. #include "luat_log.h"
  17. enum {
  18. Up = 1,
  19. Down,
  20. Left,
  21. Right,
  22. A,
  23. B,
  24. Start,
  25. Select
  26. };
  27. static luat_rtos_queue_t nes_thread;
  28. static nes_t* nes = NULL;
  29. void nes_task(void *param){
  30. nes_run(nes);
  31. }
  32. /*
  33. nes模拟器初始化
  34. @api nes.init(file_path)
  35. @string file_path 文件路径
  36. @return bool 成功返回true,否则返回false
  37. @usage
  38. nes.init("/luadb/super_mario.nes")
  39. */
  40. static int l_nes_init(lua_State *L) {
  41. size_t nes_rom_len;
  42. const char* nes_rom = luaL_checklstring(L, 1, &nes_rom_len);
  43. nes = nes_load_file(nes_rom);
  44. if (!nes){
  45. return 0;
  46. }
  47. if (luat_rtos_task_create(&nes_thread, 4*1024, 50, "nes", nes_task, nes, 0)){
  48. return 0;
  49. }
  50. lua_pushboolean(L, 1);
  51. return 1;
  52. }
  53. /*
  54. nes模拟器初始化
  55. @api nes.key(key,val)
  56. @number key 按键
  57. @number val 状态 1按下 0抬起
  58. @return bool 成功返回true,否则返回false
  59. @usage
  60. nes.init("/luadb/super_mario.nes")
  61. */
  62. static int l_nes_key(lua_State *L) {
  63. int key = luaL_checkinteger(L, 1);
  64. int val = luaL_checkinteger(L, 2);
  65. switch (key){
  66. case Up:
  67. nes->nes_cpu.joypad.U1 = val;
  68. break;
  69. case Down:
  70. nes->nes_cpu.joypad.D1 = val;
  71. break;
  72. case Left:
  73. nes->nes_cpu.joypad.L1 = val;
  74. break;
  75. case Right:
  76. nes->nes_cpu.joypad.R1 = val;
  77. break;
  78. case A:
  79. nes->nes_cpu.joypad.A1 = val;
  80. break;
  81. case B:
  82. nes->nes_cpu.joypad.B1 = val;
  83. break;
  84. case Start:
  85. nes->nes_cpu.joypad.ST1 = val;
  86. break;
  87. case Select:
  88. nes->nes_cpu.joypad.SE1 = val;
  89. break;
  90. default:
  91. break;
  92. }
  93. return 0;
  94. }
  95. #include "rotable2.h"
  96. static const rotable_Reg_t reg_nes[] =
  97. {
  98. {"init", ROREG_FUNC(l_nes_init) },
  99. {"key", ROREG_FUNC(l_nes_key) },
  100. //@const Up 按键上
  101. { "Up", ROREG_INT(Up) },
  102. //@const Down 按键下
  103. { "Down", ROREG_INT(Down) },
  104. //@const Left 按键左
  105. { "Left", ROREG_INT(Left) },
  106. //@const Right 按键右
  107. { "Right", ROREG_INT(Right) },
  108. //@const A 按键A
  109. { "A", ROREG_INT(A) },
  110. //@const B 按键B
  111. { "B", ROREG_INT(B) },
  112. //@const Start 按键开始
  113. { "Start", ROREG_INT(Start) },
  114. //@const Select 按键选择
  115. { "Select", ROREG_INT(Select) },
  116. { NULL, ROREG_INT(0)}
  117. };
  118. LUAMOD_API int luaopen_nes( lua_State *L ) {
  119. luat_newlib2(L, reg_nes);
  120. return 1;
  121. }