tama_p1.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <storage/storage.h>
  5. #include <stdlib.h>
  6. #include <stm32wbxx_ll_tim.h>
  7. #include "tamalib/tamalib.h"
  8. #include "tama.h"
  9. #include "compiled/assets_icons.h"
  10. TamaApp* g_ctx;
  11. FuriMutex* g_state_mutex;
  12. bool portrait_mode = false;
  13. bool in_menu = false;
  14. int speed = 1;
  15. const int speed_options[] = {1, 2, 4};
  16. const int min_speed = 1;
  17. const int max_speed = 4;
  18. const int speed_options_size = 3;
  19. // = sizeof(speed_options) / sizeof(speed_options[0]);
  20. int menu_cursor = 0;
  21. const int menu_items = 4;
  22. static const Icon* icons_list[] = {
  23. &I_icon_0,
  24. &I_icon_1,
  25. &I_icon_2,
  26. &I_icon_3,
  27. &I_icon_4,
  28. &I_icon_5,
  29. &I_icon_6,
  30. &I_icon_7,
  31. };
  32. // static void draw_landscape(Canvas* const canvas, void* cb_ctx)
  33. static void draw_landscape(Canvas* const canvas) {
  34. // FURI_LOG_D(TAG, "Drawing frame");
  35. // Calculate positioning
  36. uint16_t canv_width = canvas_width(canvas);
  37. uint16_t canv_height = canvas_height(canvas);
  38. uint16_t lcd_matrix_scaled_width = 32 * TAMA_SCREEN_SCALE_FACTOR;
  39. uint16_t lcd_matrix_scaled_height = 16 * TAMA_SCREEN_SCALE_FACTOR;
  40. // uint16_t lcd_matrix_top = 0;
  41. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  42. uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  43. uint16_t lcd_icon_upper_top = lcd_matrix_top - TAMA_LCD_ICON_SIZE - TAMA_LCD_ICON_MARGIN;
  44. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  45. uint16_t lcd_icon_lower_top = lcd_matrix_top + lcd_matrix_scaled_height + TAMA_LCD_ICON_MARGIN;
  46. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  47. uint16_t lcd_icon_spacing_horiz =
  48. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  49. uint16_t y = lcd_matrix_top;
  50. for(uint8_t row = 0; row < 16; ++row) {
  51. uint16_t x = lcd_matrix_left;
  52. uint32_t row_pixels = g_ctx->framebuffer[row];
  53. for(uint8_t col = 0; col < 32; ++col) {
  54. if(row_pixels & 1) {
  55. canvas_draw_box(canvas, x, y, TAMA_SCREEN_SCALE_FACTOR, TAMA_SCREEN_SCALE_FACTOR);
  56. }
  57. x += TAMA_SCREEN_SCALE_FACTOR;
  58. row_pixels >>= 1;
  59. }
  60. y += TAMA_SCREEN_SCALE_FACTOR;
  61. }
  62. // Start drawing icons
  63. uint8_t lcd_icons = g_ctx->icons;
  64. // Draw top icons
  65. y = lcd_icon_upper_top;
  66. // y = 64 - TAMA_LCD_ICON_SIZE;
  67. uint16_t x_ic = lcd_icon_upper_left;
  68. for(uint8_t i = 0; i < 4; ++i) {
  69. if(lcd_icons & 1) {
  70. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  71. }
  72. // x_ic += TAMA_LCD_ICON_SIZE + 4;
  73. x_ic += lcd_icon_spacing_horiz;
  74. lcd_icons >>= 1;
  75. }
  76. // Draw bottom icons
  77. y = lcd_icon_lower_top;
  78. x_ic = lcd_icon_lower_left;
  79. for(uint8_t i = 4; i < 8; ++i) {
  80. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  81. if(lcd_icons & 1) {
  82. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  83. }
  84. x_ic += lcd_icon_spacing_horiz;
  85. lcd_icons >>= 1;
  86. }
  87. }
  88. // static void draw_portrait(Canvas* const canvas, void* cb_ctx)
  89. static void draw_portrait(Canvas* const canvas) {
  90. // FURI_LOG_D(TAG, "Drawing frame");
  91. // Calculate positioning
  92. // uint16_t canv_width = canvas_width(canvas);
  93. uint16_t canv_height = canvas_height(canvas);
  94. uint16_t lcd_matrix_scaled_width = 32 * TAMA_SCREEN_SCALE_FACTOR;
  95. uint16_t lcd_matrix_scaled_height = 16 * TAMA_SCREEN_SCALE_FACTOR;
  96. // uint16_t lcd_matrix_top = 0;
  97. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  98. // uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  99. uint16_t lcd_matrix_left = 64 - TAMA_LCD_ICON_SIZE;
  100. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  101. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  102. uint16_t lcd_icon_spacing_horiz =
  103. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  104. uint16_t y = lcd_matrix_top; // 64
  105. for(uint8_t row = 0; row < 16; ++row) {
  106. uint16_t x = 128; // lcd_matrix_left
  107. uint32_t row_pixels = g_ctx->framebuffer[row];
  108. for(uint8_t col = 0; col < 32; ++col) {
  109. if(row_pixels & 1) {
  110. canvas_draw_box(
  111. canvas, y + 32, x - 66, TAMA_SCREEN_SCALE_FACTOR, TAMA_SCREEN_SCALE_FACTOR);
  112. }
  113. x -= TAMA_SCREEN_SCALE_FACTOR;
  114. row_pixels >>= 1;
  115. }
  116. y += TAMA_SCREEN_SCALE_FACTOR;
  117. }
  118. // Start drawing icons
  119. uint8_t lcd_icons = g_ctx->icons;
  120. // Draw top icons
  121. // y = lcd_icon_upper_top;
  122. y = 30;
  123. // y = 64 - TAMA_LCD_ICON_SIZE;
  124. uint16_t x_ic = lcd_icon_upper_left;
  125. // uint16_t x_ic = 64 - TAMA_LCD_ICON_SIZE;
  126. for(uint8_t i = 0; i < 4; ++i) {
  127. if(lcd_icons & 1) {
  128. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  129. }
  130. x_ic -= lcd_icon_spacing_horiz; // TAMA_LCD_ICON_SIZE + 4;
  131. lcd_icons >>= 1;
  132. }
  133. // Draw bottom icons
  134. y = 84; // lcd_icon_lower_top
  135. x_ic = lcd_icon_lower_left; // 64 - TAMA_LCD_ICON_SIZE
  136. for(uint8_t i = 4; i < 8; ++i) {
  137. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  138. if(lcd_icons & 1) {
  139. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  140. }
  141. x_ic -= lcd_icon_spacing_horiz;
  142. lcd_icons >>= 1;
  143. }
  144. }
  145. // static void draw_menu_portrait(Canvas* const canvas, void* cb_ctx) {}
  146. static void draw_menu_landscape(Canvas* const canvas) {
  147. canvas_draw_frame(canvas, 0, 0, 128, 64);
  148. canvas_draw_str_aligned(canvas, 64, 6, AlignCenter, AlignCenter, "Menu");
  149. canvas_draw_line(canvas, 0, 10, 128, 10);
  150. switch(menu_cursor) {
  151. case 0:
  152. canvas_draw_triangle(canvas, 4, 15, 6, 6, CanvasDirectionLeftToRight);
  153. break;
  154. case 1:
  155. canvas_draw_triangle(canvas, 4, 25, 6, 6, CanvasDirectionLeftToRight);
  156. break;
  157. case 2:
  158. canvas_draw_triangle(canvas, 4, 35, 6, 6, CanvasDirectionLeftToRight);
  159. break;
  160. case menu_items - 1:
  161. canvas_draw_triangle(canvas, 4, 55, 6, 6, CanvasDirectionLeftToRight);
  162. break;
  163. }
  164. canvas_draw_str(canvas, 12, 20, "A+C (mute/change time)");
  165. if(portrait_mode) {
  166. canvas_draw_str(canvas, 12, 30, "Orientation: Portrait");
  167. } else {
  168. canvas_draw_str(canvas, 12, 30, "Orientation: Landscape");
  169. }
  170. switch(speed) { // match with speed_options
  171. case 0: // freeze menu too
  172. canvas_draw_str(canvas, 12, 40, "Speed: 0x");
  173. break;
  174. case 1:
  175. canvas_draw_str(canvas, 12, 40, "Speed: 1x");
  176. break;
  177. case 2:
  178. canvas_draw_str(canvas, 12, 40, "Speed: 2x");
  179. break;
  180. case 3:
  181. canvas_draw_str(canvas, 12, 40, "Speed: 3x");
  182. break;
  183. case 4:
  184. canvas_draw_str(canvas, 12, 40, "Speed: 4x (max)");
  185. break;
  186. // case 8: // can't handle 8x
  187. // canvas_draw_str(canvas, 12, 40, "Speed: 8x (max)");
  188. // break;
  189. default:
  190. canvas_draw_str(canvas, 12, 40, "Speed ??x");
  191. break;
  192. }
  193. canvas_draw_str(canvas, 12, 60, "Close menu");
  194. }
  195. static void speed_up() {
  196. switch(speed) {
  197. case max_speed:
  198. speed = speed_options[0];
  199. break;
  200. default:
  201. for(int i = 0; i < speed_options_size - 1; i++) {
  202. if(speed == speed_options[i]) {
  203. speed = speed_options[i + 1];
  204. break;
  205. }
  206. }
  207. break;
  208. }
  209. tamalib_set_speed(speed);
  210. }
  211. static void speed_down() {
  212. switch(speed) {
  213. case min_speed:
  214. speed = speed_options[speed_options_size - 1];
  215. break;
  216. default:
  217. for(int i = speed_options_size - 1; i > 0; i--) {
  218. if(speed == speed_options[i]) {
  219. speed = speed_options[i - 1];
  220. break;
  221. }
  222. }
  223. break;
  224. }
  225. tamalib_set_speed(speed);
  226. }
  227. static void tama_p1_draw_callback(Canvas* const canvas, void* cb_ctx) {
  228. furi_assert(cb_ctx);
  229. FuriMutex* const mutex = cb_ctx;
  230. if(furi_mutex_acquire(mutex, 25) != FuriStatusOk) return;
  231. if(g_ctx->rom == NULL) {
  232. canvas_set_font(canvas, FontPrimary);
  233. canvas_draw_str(canvas, 30, 30, "No ROM");
  234. } else if(g_ctx->halted) {
  235. canvas_set_font(canvas, FontPrimary);
  236. canvas_draw_str(canvas, 30, 30, "Halted");
  237. } else {
  238. if(in_menu) {
  239. if(portrait_mode) {
  240. // draw_menu_portrait(canvas);
  241. draw_menu_landscape(canvas);
  242. } else {
  243. draw_menu_landscape(canvas);
  244. }
  245. } else if(portrait_mode) {
  246. draw_portrait(canvas);
  247. } else {
  248. draw_landscape(canvas);
  249. }
  250. }
  251. furi_mutex_release(mutex);
  252. }
  253. static void tama_p1_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  254. furi_assert(event_queue);
  255. TamaEvent event = {.type = EventTypeInput, .input = *input_event};
  256. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  257. }
  258. static void tama_p1_update_timer_callback(FuriMessageQueue* event_queue) {
  259. furi_assert(event_queue);
  260. TamaEvent event = {.type = EventTypeTick};
  261. furi_message_queue_put(event_queue, &event, 0);
  262. }
  263. static void tama_p1_load_state() {
  264. state_t* state;
  265. uint8_t buf[4];
  266. bool error = false;
  267. state = tamalib_get_state();
  268. Storage* storage = furi_record_open(RECORD_STORAGE);
  269. File* file = storage_file_alloc(storage);
  270. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  271. storage_file_read(file, &buf, 4);
  272. if(buf[0] != (uint8_t)STATE_FILE_MAGIC[0] || buf[1] != (uint8_t)STATE_FILE_MAGIC[1] ||
  273. buf[2] != (uint8_t)STATE_FILE_MAGIC[2] || buf[3] != (uint8_t)STATE_FILE_MAGIC[3]) {
  274. FURI_LOG_E(TAG, "FATAL: Wrong state file magic in \"%s\" !\n", TAMA_SAVE_PATH);
  275. error = true;
  276. }
  277. storage_file_read(file, &buf, 1);
  278. if(buf[0] != STATE_FILE_VERSION) {
  279. FURI_LOG_E(TAG, "FATAL: Unsupported version");
  280. error = true;
  281. }
  282. if(!error) {
  283. FURI_LOG_D(TAG, "Reading save.bin");
  284. storage_file_read(file, &buf, 2);
  285. *(state->pc) = buf[0] | ((buf[1] & 0x1F) << 8);
  286. storage_file_read(file, &buf, 2);
  287. *(state->x) = buf[0] | ((buf[1] & 0xF) << 8);
  288. storage_file_read(file, &buf, 2);
  289. *(state->y) = buf[0] | ((buf[1] & 0xF) << 8);
  290. storage_file_read(file, &buf, 1);
  291. *(state->a) = buf[0] & 0xF;
  292. storage_file_read(file, &buf, 1);
  293. *(state->b) = buf[0] & 0xF;
  294. storage_file_read(file, &buf, 1);
  295. *(state->np) = buf[0] & 0x1F;
  296. storage_file_read(file, &buf, 1);
  297. *(state->sp) = buf[0];
  298. storage_file_read(file, &buf, 1);
  299. *(state->flags) = buf[0] & 0xF;
  300. storage_file_read(file, &buf, 4);
  301. *(state->tick_counter) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  302. storage_file_read(file, &buf, 4);
  303. *(state->clk_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  304. (buf[3] << 24);
  305. storage_file_read(file, &buf, 4);
  306. *(state->prog_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  307. (buf[3] << 24);
  308. storage_file_read(file, &buf, 1);
  309. *(state->prog_timer_enabled) = buf[0] & 0x1;
  310. storage_file_read(file, &buf, 1);
  311. *(state->prog_timer_data) = buf[0];
  312. storage_file_read(file, &buf, 1);
  313. *(state->prog_timer_rld) = buf[0];
  314. storage_file_read(file, &buf, 4);
  315. *(state->call_depth) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  316. FURI_LOG_D(TAG, "Restoring Interupts");
  317. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  318. storage_file_read(file, &buf, 1);
  319. state->interrupts[i].factor_flag_reg = buf[0] & 0xF;
  320. storage_file_read(file, &buf, 1);
  321. state->interrupts[i].mask_reg = buf[0] & 0xF;
  322. storage_file_read(file, &buf, 1);
  323. state->interrupts[i].triggered = buf[0] & 0x1;
  324. }
  325. /* First 640 half bytes correspond to the RAM */
  326. FURI_LOG_D(TAG, "Restoring RAM");
  327. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  328. storage_file_read(file, &buf, 1);
  329. SET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR, buf[0] & 0xF);
  330. }
  331. /* I/Os are from 0xF00 to 0xF7F */
  332. FURI_LOG_D(TAG, "Restoring I/O");
  333. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  334. storage_file_read(file, &buf, 1);
  335. SET_IO_MEMORY(state->memory, i + MEM_IO_ADDR, buf[0] & 0xF);
  336. }
  337. FURI_LOG_D(TAG, "Refreshing Hardware");
  338. tamalib_refresh_hw();
  339. }
  340. }
  341. storage_file_close(file);
  342. storage_file_free(file);
  343. furi_record_close(RECORD_STORAGE);
  344. }
  345. static void tama_p1_save_state() {
  346. // Saving state
  347. FURI_LOG_D(TAG, "Saving Gamestate");
  348. uint8_t buf[4];
  349. state_t* state;
  350. uint32_t offset = 0;
  351. state = tamalib_get_state();
  352. Storage* storage = furi_record_open(RECORD_STORAGE);
  353. File* file = storage_file_alloc(storage);
  354. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  355. buf[0] = (uint8_t)STATE_FILE_MAGIC[0];
  356. buf[1] = (uint8_t)STATE_FILE_MAGIC[1];
  357. buf[2] = (uint8_t)STATE_FILE_MAGIC[2];
  358. buf[3] = (uint8_t)STATE_FILE_MAGIC[3];
  359. offset += storage_file_write(file, &buf, sizeof(buf));
  360. buf[0] = STATE_FILE_VERSION & 0xFF;
  361. offset += storage_file_write(file, &buf, 1);
  362. buf[0] = *(state->pc) & 0xFF;
  363. buf[1] = (*(state->pc) >> 8) & 0x1F;
  364. offset += storage_file_write(file, &buf, 2);
  365. buf[0] = *(state->x) & 0xFF;
  366. buf[1] = (*(state->x) >> 8) & 0xF;
  367. offset += storage_file_write(file, &buf, 2);
  368. buf[0] = *(state->y) & 0xFF;
  369. buf[1] = (*(state->y) >> 8) & 0xF;
  370. offset += storage_file_write(file, &buf, 2);
  371. buf[0] = *(state->a) & 0xF;
  372. offset += storage_file_write(file, &buf, 1);
  373. buf[0] = *(state->b) & 0xF;
  374. offset += storage_file_write(file, &buf, 1);
  375. buf[0] = *(state->np) & 0x1F;
  376. offset += storage_file_write(file, &buf, 1);
  377. buf[0] = *(state->sp) & 0xFF;
  378. offset += storage_file_write(file, &buf, 1);
  379. buf[0] = *(state->flags) & 0xF;
  380. offset += storage_file_write(file, &buf, 1);
  381. buf[0] = *(state->tick_counter) & 0xFF;
  382. buf[1] = (*(state->tick_counter) >> 8) & 0xFF;
  383. buf[2] = (*(state->tick_counter) >> 16) & 0xFF;
  384. buf[3] = (*(state->tick_counter) >> 24) & 0xFF;
  385. offset += storage_file_write(file, &buf, sizeof(buf));
  386. buf[0] = *(state->clk_timer_timestamp) & 0xFF;
  387. buf[1] = (*(state->clk_timer_timestamp) >> 8) & 0xFF;
  388. buf[2] = (*(state->clk_timer_timestamp) >> 16) & 0xFF;
  389. buf[3] = (*(state->clk_timer_timestamp) >> 24) & 0xFF;
  390. offset += storage_file_write(file, &buf, sizeof(buf));
  391. buf[0] = *(state->prog_timer_timestamp) & 0xFF;
  392. buf[1] = (*(state->prog_timer_timestamp) >> 8) & 0xFF;
  393. buf[2] = (*(state->prog_timer_timestamp) >> 16) & 0xFF;
  394. buf[3] = (*(state->prog_timer_timestamp) >> 24) & 0xFF;
  395. offset += storage_file_write(file, &buf, sizeof(buf));
  396. buf[0] = *(state->prog_timer_enabled) & 0x1;
  397. offset += storage_file_write(file, &buf, 1);
  398. buf[0] = *(state->prog_timer_data) & 0xFF;
  399. offset += storage_file_write(file, &buf, 1);
  400. buf[0] = *(state->prog_timer_rld) & 0xFF;
  401. offset += storage_file_write(file, &buf, 1);
  402. buf[0] = *(state->call_depth) & 0xFF;
  403. buf[1] = (*(state->call_depth) >> 8) & 0xFF;
  404. buf[2] = (*(state->call_depth) >> 16) & 0xFF;
  405. buf[3] = (*(state->call_depth) >> 24) & 0xFF;
  406. offset += storage_file_write(file, &buf, sizeof(buf));
  407. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  408. buf[0] = state->interrupts[i].factor_flag_reg & 0xF;
  409. offset += storage_file_write(file, &buf, 1);
  410. buf[0] = state->interrupts[i].mask_reg & 0xF;
  411. offset += storage_file_write(file, &buf, 1);
  412. buf[0] = state->interrupts[i].triggered & 0x1;
  413. offset += storage_file_write(file, &buf, 1);
  414. }
  415. /* First 640 half bytes correspond to the RAM */
  416. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  417. buf[0] = GET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR) & 0xF;
  418. offset += storage_file_write(file, &buf, 1);
  419. }
  420. /* I/Os are from 0xF00 to 0xF7F */
  421. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  422. buf[0] = GET_IO_MEMORY(state->memory, i + MEM_IO_ADDR) & 0xF;
  423. offset += storage_file_write(file, &buf, 1);
  424. }
  425. }
  426. storage_file_close(file);
  427. storage_file_free(file);
  428. furi_record_close(RECORD_STORAGE);
  429. FURI_LOG_D(TAG, "Finished Writing %lu", offset);
  430. }
  431. static int32_t tama_p1_worker(void* context) {
  432. bool running = true;
  433. FuriMutex* mutex = context;
  434. while(furi_mutex_acquire(mutex, FuriWaitForever) != FuriStatusOk) furi_delay_tick(1);
  435. cpu_sync_ref_timestamp();
  436. LL_TIM_EnableCounter(TIM2);
  437. tama_p1_load_state();
  438. while(running) {
  439. if(furi_thread_flags_get()) {
  440. running = false;
  441. } else {
  442. // FURI_LOG_D(TAG, "Stepping");
  443. // for (int i = 0; i < 100; ++i)
  444. tamalib_step();
  445. }
  446. }
  447. LL_TIM_DisableCounter(TIM2);
  448. furi_mutex_release(mutex);
  449. return 0;
  450. }
  451. static void tama_p1_init(TamaApp* const ctx) {
  452. g_ctx = ctx;
  453. memset(ctx, 0, sizeof(TamaApp));
  454. tama_p1_hal_init(&ctx->hal);
  455. // Load ROM
  456. Storage* storage = furi_record_open(RECORD_STORAGE);
  457. FileInfo fi;
  458. if(storage_common_stat(storage, TAMA_ROM_PATH, &fi) == FSE_OK) {
  459. File* rom_file = storage_file_alloc(storage);
  460. if(storage_file_open(rom_file, TAMA_ROM_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  461. ctx->rom = malloc((size_t)fi.size);
  462. uint8_t* buf_ptr = ctx->rom;
  463. size_t read = 0;
  464. while(read < fi.size) {
  465. size_t to_read = fi.size - read;
  466. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  467. uint16_t now_read = storage_file_read(rom_file, buf_ptr, (uint16_t)to_read);
  468. read += now_read;
  469. buf_ptr += now_read;
  470. }
  471. // Reorder endianess of ROM
  472. for(size_t i = 0; i < fi.size; i += 2) {
  473. uint8_t b = ctx->rom[i];
  474. ctx->rom[i] = ctx->rom[i + 1];
  475. ctx->rom[i + 1] = b & 0xF;
  476. }
  477. }
  478. storage_file_close(rom_file);
  479. storage_file_free(rom_file);
  480. }
  481. furi_record_close(RECORD_STORAGE);
  482. if(ctx->rom != NULL) {
  483. // Init TIM2
  484. // 64KHz
  485. LL_TIM_InitTypeDef tim_init = {
  486. .Prescaler = 999,
  487. .CounterMode = LL_TIM_COUNTERMODE_UP,
  488. .Autoreload = 0xFFFFFFFF,
  489. };
  490. LL_TIM_Init(TIM2, &tim_init);
  491. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  492. LL_TIM_DisableCounter(TIM2);
  493. LL_TIM_SetCounter(TIM2, 0);
  494. // Init TamaLIB
  495. tamalib_register_hal(&ctx->hal);
  496. tamalib_init((u12_t*)ctx->rom, NULL, 64000);
  497. tamalib_set_speed(speed);
  498. // TODO: implement fast forwarding
  499. ctx->fast_forward_done = true;
  500. // Start stepping thread
  501. ctx->thread = furi_thread_alloc();
  502. furi_thread_set_name(ctx->thread, "TamaLIB");
  503. furi_thread_set_stack_size(ctx->thread, 1024);
  504. furi_thread_set_callback(ctx->thread, tama_p1_worker);
  505. furi_thread_set_context(ctx->thread, g_state_mutex);
  506. furi_thread_start(ctx->thread);
  507. }
  508. }
  509. static void tama_p1_deinit(TamaApp* const ctx) {
  510. if(ctx->rom != NULL) {
  511. tamalib_release();
  512. furi_thread_free(ctx->thread);
  513. free(ctx->rom);
  514. }
  515. }
  516. int32_t tama_p1_app(void* p) {
  517. UNUSED(p);
  518. TamaApp* ctx = malloc(sizeof(TamaApp));
  519. g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  520. tama_p1_init(ctx);
  521. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TamaEvent));
  522. ViewPort* view_port = view_port_alloc();
  523. view_port_draw_callback_set(view_port, tama_p1_draw_callback, g_state_mutex);
  524. view_port_input_callback_set(view_port, tama_p1_input_callback, event_queue);
  525. Gui* gui = furi_record_open(RECORD_GUI);
  526. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  527. FuriTimer* timer =
  528. furi_timer_alloc(tama_p1_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  529. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  530. // portrait_mode = false;
  531. // in_menu = false;
  532. // menu_cursor = 2;
  533. for(bool running = true; running;) {
  534. TamaEvent event;
  535. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  536. if(event_status == FuriStatusOk) {
  537. // Local override with acquired context
  538. if(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk) continue;
  539. if(event.type == EventTypeTick) {
  540. // FURI_LOG_D(TAG, "EventTypeTick");
  541. view_port_update(view_port);
  542. } else if(event.type == EventTypeInput) {
  543. FURI_LOG_D(
  544. TAG,
  545. "EventTypeInput: %ld %d %d",
  546. event.input.sequence,
  547. event.input.key,
  548. event.input.type);
  549. InputType input_type = event.input.type;
  550. btn_state_t tama_btn_state = 0;
  551. if(input_type == InputTypePress)
  552. tama_btn_state = BTN_STATE_PRESSED;
  553. else if(input_type == InputTypeRelease)
  554. tama_btn_state = BTN_STATE_RELEASED;
  555. if(in_menu) {
  556. if(event.input.key == InputKeyUp && event.input.type == InputTypePress) {
  557. if(menu_cursor > 0) {
  558. menu_cursor -= 1;
  559. } else {
  560. menu_cursor = menu_items - 1;
  561. }
  562. } else if(event.input.key == InputKeyDown && event.input.type == InputTypePress) {
  563. if(menu_cursor < menu_items - 1) {
  564. menu_cursor += 1;
  565. } else {
  566. menu_cursor = 0;
  567. }
  568. } else if(event.input.key == InputKeyLeft && event.input.type == InputTypePress) {
  569. switch(menu_cursor) {
  570. case 1:
  571. portrait_mode = !portrait_mode;
  572. break;
  573. case 2:
  574. speed_down();
  575. break;
  576. default:
  577. break;
  578. }
  579. } else if(event.input.key == InputKeyRight && event.input.type == InputTypePress) {
  580. switch(menu_cursor) {
  581. case 1:
  582. portrait_mode = !portrait_mode;
  583. break;
  584. case 2:
  585. speed_up();
  586. break;
  587. default:
  588. break;
  589. }
  590. } else if(event.input.key == InputKeyOk) {
  591. switch(menu_cursor) {
  592. case 0:
  593. // mute tamagotchi
  594. tamalib_set_button(BTN_LEFT, tama_btn_state);
  595. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  596. break;
  597. case 1:
  598. // portrait_mode = true;
  599. if(event.input.type == InputTypePress) portrait_mode = !portrait_mode;
  600. break;
  601. case 2:
  602. if(event.input.type == InputTypePress) {
  603. speed_up();
  604. }
  605. break;
  606. case menu_items - 1:
  607. default:
  608. in_menu = false;
  609. break;
  610. }
  611. } else if(event.input.key == InputKeyBack) {
  612. in_menu = false;
  613. }
  614. } else { // out of menu
  615. if(input_type == InputTypePress || input_type == InputTypeRelease) {
  616. if(portrait_mode) {
  617. if(event.input.key == InputKeyDown) {
  618. tamalib_set_button(BTN_LEFT, tama_btn_state);
  619. } else if(event.input.key == InputKeyOk) {
  620. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  621. } else if(event.input.key == InputKeyRight) {
  622. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  623. } else if(event.input.key == InputKeyUp) {
  624. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  625. } else if(event.input.key == InputKeyLeft) {
  626. in_menu = true;
  627. } else if(
  628. event.input.key == InputKeyBack &&
  629. event.input.type == InputTypeShort) {
  630. tama_p1_save_state();
  631. }
  632. } else {
  633. if(event.input.key == InputKeyLeft) {
  634. tamalib_set_button(BTN_LEFT, tama_btn_state);
  635. } else if(event.input.key == InputKeyOk) {
  636. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  637. } else if(event.input.key == InputKeyDown) {
  638. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  639. } else if(event.input.key == InputKeyRight) {
  640. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  641. } else if(event.input.key == InputKeyUp) {
  642. in_menu = true;
  643. } else if(
  644. event.input.key == InputKeyBack &&
  645. event.input.type == InputTypePress) {
  646. if(speed != 1) {
  647. speed = 1;
  648. tamalib_set_speed(speed);
  649. }
  650. tama_p1_save_state();
  651. }
  652. }
  653. }
  654. }
  655. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong &&
  656. !in_menu) {
  657. if(speed != 1) {
  658. speed = 1;
  659. tamalib_set_speed(speed);
  660. }
  661. furi_timer_stop(timer);
  662. running = false;
  663. tama_p1_save_state();
  664. }
  665. }
  666. furi_mutex_release(g_state_mutex);
  667. } else {
  668. // Timeout
  669. // FURI_LOG_D(TAG, "Timed out");
  670. }
  671. }
  672. if(ctx->rom != NULL) {
  673. furi_thread_flags_set(furi_thread_get_id(ctx->thread), 1);
  674. furi_thread_join(ctx->thread);
  675. }
  676. furi_timer_free(timer);
  677. view_port_enabled_set(view_port, false);
  678. gui_remove_view_port(gui, view_port);
  679. furi_record_close(RECORD_GUI);
  680. view_port_free(view_port);
  681. furi_message_queue_free(event_queue);
  682. furi_mutex_free(g_state_mutex);
  683. tama_p1_deinit(ctx);
  684. free(ctx);
  685. return 0;
  686. }