This addin is a Chip 8 Emulator.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*****************************************************************/
  2. /* */
  3. /* CASIO fx-9860G SDK Library */
  4. /* */
  5. /* File name : [ProjectName].c */
  6. /* */
  7. /* Copyright (c) 1206 CASIO COMPUTER CO., LTD. */
  8. /* */
  9. /*****************************************************************/
  10. #include "fxlib.h"
  11. #include "MonochromeLib.h"
  12. #include "Machine.h"
  13. #define FILENAME_SIZE 13
  14. void Run_Program (const FONTCHARACTER *fileName);
  15. void PrintLine_FONTCHARACTER (const FONTCHARACTER *text);
  16. //*****************
  17. // AddIn_main
  18. //*****************
  19. int AddIn_main(int isAppli, unsigned short OptionNum)
  20. {
  21. FONTCHARACTER path[]={'\\', '\\', 'f', 'l', 's', '0', '\\', '*', '.', 'c', 'h', '8', 0};
  22. int file;
  23. FONTCHARACTER actual_name[FILENAME_SIZE];
  24. FONTCHARACTER *names;
  25. FILE_INFO info;
  26. int r, i;
  27. unsigned int key;
  28. int selected_program = 0, nb_programs;
  29. unsigned char arrow[]={0xE6, 0x9B, 0x00};
  30. // Check if there's any ROM in memory
  31. if (Bfile_FindFirst(path, &file, &actual_name[0], &info) == 0) {
  32. nb_programs = 0;
  33. names = (FONTCHARACTER*) malloc(FILENAME_SIZE * sizeof(FONTCHARACTER));
  34. // Put every available ROMs into the list
  35. do{
  36. nb_programs ++;
  37. names = (FONTCHARACTER*) realloc(names, FILENAME_SIZE * nb_programs * sizeof(FONTCHARACTER));
  38. memcpy((names + (FILENAME_SIZE * (nb_programs - 1))), actual_name, FILENAME_SIZE * sizeof(FONTCHARACTER));
  39. }while(Bfile_FindNext(file, &actual_name[0], &info) == 0);
  40. // Main loop
  41. while(key != KEY_CTRL_MENU){
  42. /* The key part */
  43. if(key == KEY_CTRL_EXE)
  44. Run_Program(names + selected_program * FILENAME_SIZE);
  45. if(selected_program > 0)
  46. selected_program -= (key == KEY_CTRL_UP);
  47. if(selected_program < nb_programs-1)
  48. selected_program += (key == KEY_CTRL_DOWN);
  49. /* Screen's part */
  50. ML_clear_vram();
  51. locate(4, 1);
  52. Print((unsigned char*)"Chip 8 Emulator");
  53. // Print the ROMS
  54. for(i = 0; i < 7; i++){
  55. locate(2, 2 + i);
  56. PrintLine_FONTCHARACTER((names + FILENAME_SIZE * (i + (selected_program / 7 * 7))));
  57. if((i + (selected_program / 7 * 7) + 1) >= nb_programs) i = 7;
  58. }
  59. // Print the arrow
  60. locate(1, 2 + (selected_program % 7));
  61. Print(arrow);
  62. // Refresh the screen
  63. ML_display_vram();
  64. GetKey(&key);
  65. }
  66. }else{
  67. // If there's no programs
  68. ML_clear_vram();
  69. locate(4, 1);
  70. Print((unsigned char*)"Chip 8 Emulator");
  71. locate(3, 4);
  72. Print((unsigned char*)"No program found");
  73. ML_display_vram();
  74. while(!IsKeyDown(KEY_CTRL_MENU));
  75. }
  76. return 1;
  77. }
  78. void PrintLine_FONTCHARACTER(const FONTCHARACTER *text){
  79. unsigned char characters[] = {0x00, 0x00, 0x00};
  80. int i;
  81. while(*text != 0){
  82. characters[0] = (unsigned char)(*text >> 8);
  83. characters[1] = (unsigned char)(*text & 0x0FF);
  84. if(characters[0] == 0)
  85. Print(characters + 1);
  86. else
  87. Print(characters);
  88. ML_display_vram();
  89. text ++;
  90. }
  91. }
  92. void Run_Program(const FONTCHARACTER *fileName){
  93. int file, i, time_to_wait = 5;
  94. Machine_State state;
  95. FONTCHARACTER fn[FILENAME_SIZE + 9];
  96. // Make the complete path
  97. fn[0] = '\\';
  98. fn[1] = '\\';
  99. fn[2] = 'f';
  100. fn[3] = 'l';
  101. fn[4] = 's';
  102. fn[5] = '0';
  103. fn[6] = '\\';
  104. fn[FILENAME_SIZE + 8] = 0;
  105. for(i = 0; i < FILENAME_SIZE; i ++)
  106. fn[7 + i] = fileName[i];
  107. /* File's part */
  108. file = Bfile_OpenFile(fn, _OPENMODE_READ);
  109. // If we can't load the file
  110. if(file < 0)
  111. return;
  112. // Initialize the machine and put the program into memory
  113. Machine_Init(&state);
  114. Bfile_ReadFile(file, state.Memory + 0x200, sizeof(state.Memory) - 0x200, 0);
  115. // Clear the screen
  116. ML_clear_vram();
  117. ML_display_vram();
  118. while(!IsKeyDown(KEY_CTRL_EXIT)){
  119. // The emulator's speed control
  120. if(time_to_wait > 0)
  121. time_to_wait -= IsKeyDown(KEY_CTRL_UP);
  122. time_to_wait += IsKeyDown(KEY_CTRL_DOWN);
  123. Sleep(time_to_wait);
  124. Machine_Update(&state);
  125. }
  126. Bfile_CloseFile(file);
  127. }
  128. //****************************************************************************
  129. //************** ****************
  130. //************** Notice! ****************
  131. //************** ****************
  132. //************** Please do not change the following source. ****************
  133. //************** ****************
  134. //****************************************************************************
  135. #pragma section _BR_Size
  136. unsigned long BR_Size;
  137. #pragma section
  138. #pragma section _TOP
  139. //****************************************************************************
  140. // InitializeSystem
  141. //
  142. // param : isAppli : 1 = Application / 0 = eActivity
  143. // OptionNum : Option Number (only eActivity)
  144. //
  145. // retval : 1 = No error / 0 = Error
  146. //
  147. //****************************************************************************
  148. int InitializeSystem(int isAppli, unsigned short OptionNum)
  149. {
  150. return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  151. }
  152. #pragma section