tama_p1.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. uint8_t layout_mode = 0; // 3: portrait => 4: portrait <=
  13. // 0: landscape (small) 1: landscape (big) 2: landscape (full)
  14. bool in_menu = false;
  15. uint8_t speed = 1;
  16. const uint8_t speed_options[] = {1, 2, 4};
  17. const uint8_t min_speed = 1;
  18. const uint8_t max_speed = 4;
  19. const uint8_t speed_options_size = 3;
  20. // = sizeof(speed_options) / sizeof(speed_options[0]);
  21. uint8_t menu_cursor = 3; // 0: A+C; 1: layout mode; 2: speed
  22. const uint8_t menu_items = 4; // 3: Close menu, Save & Exit
  23. uint8_t sub_menu_default = 0;
  24. static const Icon* icons_list[] = {
  25. &I_icon_0,
  26. &I_icon_1,
  27. &I_icon_2,
  28. &I_icon_3,
  29. &I_icon_4,
  30. &I_icon_5,
  31. &I_icon_6,
  32. &I_icon_7,
  33. };
  34. // static void draw_landscape(Canvas* const canvas, void* cb_ctx)
  35. static void draw_landscape(Canvas* const canvas, uint8_t scale) {
  36. // FURI_LOG_D(TAG, "Drawing frame");
  37. // Calculate positioning
  38. uint16_t canv_width = canvas_width(canvas);
  39. uint16_t canv_height = canvas_height(canvas);
  40. uint16_t lcd_matrix_scaled_width = 32 * scale;
  41. uint16_t lcd_matrix_scaled_height = 16 * scale;
  42. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2; // 0
  43. uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  44. // uint16_t lcd_icon_upper_top = lcd_matrix_top - TAMA_LCD_ICON_SIZE - TAMA_LCD_ICON_MARGIN;
  45. // uint16_t lcd_icon_lower_top = lcd_matrix_top + lcd_matrix_scaled_height + TAMA_LCD_ICON_MARGIN;
  46. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  47. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  48. uint16_t lcd_icon_spacing_horiz =
  49. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  50. uint16_t y = lcd_matrix_top;
  51. for(uint8_t row = 0; row < 16; ++row) {
  52. uint16_t x = lcd_matrix_left;
  53. uint32_t row_pixels = g_ctx->framebuffer[row];
  54. for(uint8_t col = 0; col < 32; ++col) {
  55. if(row_pixels & 1) {
  56. canvas_draw_box(canvas, x, y, scale, scale);
  57. }
  58. x += scale;
  59. row_pixels >>= 1;
  60. }
  61. y += scale;
  62. }
  63. // Start drawing icons
  64. uint8_t lcd_icons = g_ctx->icons;
  65. // Draw top icons
  66. y = 0;
  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. // if(scale == 3) {
  74. // y += 16;
  75. // } else {
  76. x_ic += lcd_icon_spacing_horiz;
  77. // }
  78. lcd_icons >>= 1;
  79. }
  80. // Draw bottom icons
  81. y = 64 - TAMA_LCD_ICON_SIZE;
  82. // if(scale == 3) {
  83. // y = 0;
  84. // x_ic = 128 - TAMA_LCD_ICON_SIZE;
  85. // x_ic = 0;
  86. // } else {
  87. // y = 64 - TAMA_LCD_ICON_SIZE;
  88. x_ic = lcd_icon_lower_left;
  89. // }
  90. for(uint8_t i = 4; i < 8; ++i) {
  91. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  92. if(lcd_icons & 1) {
  93. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  94. }
  95. // if(scale == 3) {
  96. // y += 16;
  97. // } else {
  98. x_ic += lcd_icon_spacing_horiz;
  99. // }
  100. lcd_icons >>= 1;
  101. }
  102. }
  103. // static void draw_portrait_right(Canvas* const canvas, void* cb_ctx)
  104. static void draw_portrait_right(Canvas* const canvas) {
  105. // FURI_LOG_D(TAG, "Drawing frame");
  106. // Calculate positioning
  107. // uint16_t canv_width = canvas_width(canvas);
  108. uint16_t canv_height = canvas_height(canvas);
  109. uint16_t lcd_matrix_scaled_width = 32 * TAMA_SCREEN_SCALE_FACTOR;
  110. uint16_t lcd_matrix_scaled_height = 16 * TAMA_SCREEN_SCALE_FACTOR;
  111. // uint16_t lcd_matrix_top = 0;
  112. uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  113. // uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  114. uint16_t lcd_matrix_left = 64 - TAMA_LCD_ICON_SIZE;
  115. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  116. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  117. uint16_t lcd_icon_spacing_horiz =
  118. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  119. uint16_t y = lcd_matrix_top; // 64
  120. for(uint8_t row = 0; row < 16; ++row) {
  121. uint16_t x = 128; // lcd_matrix_left
  122. uint32_t row_pixels = g_ctx->framebuffer[row];
  123. for(uint8_t col = 0; col < 32; ++col) {
  124. if(row_pixels & 1) {
  125. canvas_draw_box(
  126. canvas, y + 32, x - 66, TAMA_SCREEN_SCALE_FACTOR, TAMA_SCREEN_SCALE_FACTOR);
  127. }
  128. x -= TAMA_SCREEN_SCALE_FACTOR;
  129. row_pixels >>= 1;
  130. }
  131. y += TAMA_SCREEN_SCALE_FACTOR;
  132. }
  133. // Start drawing icons
  134. uint8_t lcd_icons = g_ctx->icons;
  135. // Draw top icons
  136. // y = lcd_icon_upper_top;
  137. y = 30;
  138. // y = 64 - TAMA_LCD_ICON_SIZE;
  139. uint16_t x_ic = lcd_icon_upper_left;
  140. // uint16_t x_ic = 64 - TAMA_LCD_ICON_SIZE;
  141. for(uint8_t i = 0; i < 4; ++i) {
  142. if(lcd_icons & 1) {
  143. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  144. }
  145. x_ic -= lcd_icon_spacing_horiz; // TAMA_LCD_ICON_SIZE + 4;
  146. lcd_icons >>= 1;
  147. }
  148. // Draw bottom icons
  149. y = 84; // lcd_icon_lower_top
  150. x_ic = lcd_icon_lower_left; // 64 - TAMA_LCD_ICON_SIZE
  151. for(uint8_t i = 4; i < 8; ++i) {
  152. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  153. if(lcd_icons & 1) {
  154. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  155. }
  156. x_ic -= lcd_icon_spacing_horiz;
  157. lcd_icons >>= 1;
  158. }
  159. }
  160. static void draw_portrait_left(Canvas* const canvas) {
  161. // FURI_LOG_D(TAG, "Drawing frame");
  162. // Calculate positioning
  163. // uint16_t canv_width = canvas_width(canvas);
  164. // uint16_t canv_height = canvas_height(canvas);
  165. uint16_t lcd_matrix_scaled_width = 32 * TAMA_SCREEN_SCALE_FACTOR;
  166. // uint16_t lcd_matrix_scaled_height = 16 * TAMA_SCREEN_SCALE_FACTOR;
  167. // uint16_t lcd_matrix_top = 0;
  168. // uint16_t lcd_matrix_top = (canv_height - lcd_matrix_scaled_height) / 2;
  169. // uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  170. uint16_t lcd_matrix_left = 0;
  171. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  172. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  173. uint16_t lcd_icon_spacing_horiz =
  174. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  175. // uint16_t y = 64 + lcd_matrix_top;
  176. uint16_t y = 64;
  177. for(uint8_t row = 0; row < 16; ++row) {
  178. uint16_t x = 0; // lcd_matrix_left
  179. uint32_t row_pixels = g_ctx->framebuffer[row];
  180. for(uint8_t col = 0; col < 32; ++col) {
  181. if(row_pixels & 1) {
  182. canvas_draw_box(canvas, y, x, TAMA_SCREEN_SCALE_FACTOR, TAMA_SCREEN_SCALE_FACTOR);
  183. }
  184. x += TAMA_SCREEN_SCALE_FACTOR;
  185. row_pixels >>= 1;
  186. }
  187. y -= TAMA_SCREEN_SCALE_FACTOR;
  188. }
  189. // Start drawing icons
  190. uint8_t lcd_icons = g_ctx->icons;
  191. // Draw top icons
  192. // y = lcd_icon_upper_top;
  193. y = 70;
  194. // y = 64 - TAMA_LCD_ICON_SIZE;
  195. uint16_t x_ic = lcd_icon_upper_left;
  196. // uint16_t x_ic = 64 - TAMA_LCD_ICON_SIZE;
  197. for(uint8_t i = 0; i < 4; ++i) {
  198. if(lcd_icons & 1) {
  199. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  200. }
  201. x_ic += lcd_icon_spacing_horiz; // TAMA_LCD_ICON_SIZE + 4;
  202. lcd_icons >>= 1;
  203. }
  204. // Draw bottom icons
  205. y = 16; // lcd_icon_lower_top
  206. x_ic = lcd_icon_lower_left; // 64 - TAMA_LCD_ICON_SIZE
  207. for(uint8_t i = 4; i < 8; ++i) {
  208. // canvas_draw_frame(canvas, x_ic, y, TAMA_LCD_ICON_SIZE, TAMA_LCD_ICON_SIZE);
  209. if(lcd_icons & 1) {
  210. canvas_draw_icon(canvas, y, x_ic, icons_list[i]);
  211. }
  212. x_ic += lcd_icon_spacing_horiz;
  213. lcd_icons >>= 1;
  214. }
  215. }
  216. static void draw_mini(Canvas* const canvas) {
  217. // Calculate positioning
  218. uint16_t y = 34;
  219. for(uint8_t row = 0; row < 16; ++row) {
  220. uint16_t x = 84;
  221. uint32_t row_pixels = g_ctx->framebuffer[row];
  222. for(uint8_t col = 0; col < 32; ++col) {
  223. if(row_pixels & 1) {
  224. canvas_draw_dot(canvas, x, y);
  225. }
  226. x += 1;
  227. row_pixels >>= 1;
  228. }
  229. y += 1;
  230. }
  231. }
  232. // static void draw_menu_portrait(Canvas* const canvas, void* cb_ctx) {}
  233. // static void draw_menu_landscape(Canvas* const canvas)
  234. static void draw_menu(Canvas* const canvas) {
  235. canvas_draw_frame(canvas, 0, 0, 128, 64);
  236. canvas_draw_str_aligned(canvas, 64, 6, AlignCenter, AlignCenter, "Menu");
  237. canvas_draw_line(canvas, 0, 10, 128, 10);
  238. draw_mini(canvas);
  239. switch(menu_cursor) {
  240. case 0:
  241. canvas_draw_triangle(canvas, 4, 16, 6, 6, CanvasDirectionLeftToRight);
  242. break;
  243. case 1:
  244. canvas_draw_triangle(canvas, 4, 26, 6, 6, CanvasDirectionLeftToRight);
  245. break;
  246. case 2:
  247. canvas_draw_triangle(canvas, 4, 36, 6, 6, CanvasDirectionLeftToRight);
  248. break;
  249. case menu_items - 1:
  250. if(sub_menu_default == 0) {
  251. canvas_draw_triangle(canvas, 4, 56, 6, 6, CanvasDirectionLeftToRight);
  252. } else if(sub_menu_default == 1) {
  253. canvas_draw_triangle(canvas, 36, 56, 6, 6, CanvasDirectionLeftToRight);
  254. } else if(sub_menu_default == 2) {
  255. canvas_draw_triangle(canvas, 67, 56, 6, 6, CanvasDirectionLeftToRight);
  256. }
  257. break;
  258. }
  259. canvas_draw_str(canvas, 12, 20, "A+C (mute/change time)");
  260. switch(layout_mode) {
  261. case 0:
  262. canvas_draw_str(canvas, 12, 30, "Layout: Landscape (small)");
  263. break;
  264. case 1:
  265. canvas_draw_str(canvas, 12, 30, "Layout: Landscape (big)");
  266. break;
  267. case 2:
  268. canvas_draw_str(canvas, 12, 30, "Layout: Landscape (full)");
  269. break;
  270. case 3:
  271. canvas_draw_str(canvas, 12, 30, "Layout: Portrait =>");
  272. break;
  273. case 4:
  274. canvas_draw_str(canvas, 12, 30, "Layout: Portrait <=");
  275. break;
  276. default:
  277. canvas_draw_str(canvas, 12, 30, "Layout: ???");
  278. break;
  279. }
  280. switch(speed) { // match with speed_options
  281. case 0: // freeze menu too
  282. canvas_draw_str(canvas, 12, 40, "Speed: 0x");
  283. break;
  284. case 1:
  285. canvas_draw_str(canvas, 12, 40, "Speed: 1x");
  286. break;
  287. case 2:
  288. canvas_draw_str(canvas, 12, 40, "Speed: 2x");
  289. break;
  290. case 3:
  291. canvas_draw_str(canvas, 12, 40, "Speed: 3x");
  292. break;
  293. case 4:
  294. canvas_draw_str(canvas, 12, 40, "Speed: 4x (max)");
  295. break;
  296. // case 8: // can't handle 8x
  297. // canvas_draw_str(canvas, 12, 40, "Speed: 8x (max)");
  298. // break;
  299. default:
  300. canvas_draw_str(canvas, 12, 40, "Speed ??x");
  301. break;
  302. }
  303. canvas_draw_str(canvas, 12, 60, "Close");
  304. canvas_draw_str(canvas, 44, 60, "Save");
  305. canvas_draw_str(canvas, 75, 60, "Save & Exit");
  306. }
  307. static void speed_up() {
  308. switch(speed) {
  309. case max_speed:
  310. speed = speed_options[0];
  311. break;
  312. default:
  313. for(uint8_t i = 0; i < speed_options_size - 1; i++) {
  314. if(speed == speed_options[i]) {
  315. speed = speed_options[i + 1];
  316. break;
  317. }
  318. }
  319. break;
  320. }
  321. tamalib_set_speed(speed);
  322. }
  323. static void speed_down() {
  324. switch(speed) {
  325. case min_speed:
  326. speed = speed_options[speed_options_size - 1];
  327. break;
  328. default:
  329. for(uint8_t i = speed_options_size - 1; i > 0; i--) {
  330. if(speed == speed_options[i]) {
  331. speed = speed_options[i - 1];
  332. break;
  333. }
  334. }
  335. break;
  336. }
  337. tamalib_set_speed(speed);
  338. }
  339. static void tama_p1_draw_callback(Canvas* const canvas, void* cb_ctx) {
  340. furi_assert(cb_ctx);
  341. FuriMutex* const mutex = cb_ctx;
  342. if(furi_mutex_acquire(mutex, 25) != FuriStatusOk) return;
  343. if(g_ctx->rom == NULL) {
  344. canvas_set_font(canvas, FontPrimary);
  345. canvas_draw_str(canvas, 30, 30, "No ROM");
  346. } else if(g_ctx->halted) {
  347. canvas_set_font(canvas, FontPrimary);
  348. canvas_draw_str(canvas, 30, 30, "Halted");
  349. } else {
  350. if(in_menu) {
  351. // switch(layout_mode)
  352. // draw_menu_landscape(canvas);
  353. draw_menu(canvas);
  354. } else {
  355. switch(layout_mode) {
  356. case 0:
  357. draw_landscape(canvas, TAMA_SCREEN_SCALE_FACTOR); // 2
  358. break;
  359. case 1:
  360. draw_landscape(canvas, 3);
  361. break;
  362. case 2:
  363. draw_landscape(canvas, 4);
  364. break;
  365. case 3:
  366. draw_portrait_right(canvas);
  367. break;
  368. case 4:
  369. draw_portrait_left(canvas);
  370. break;
  371. default:
  372. break;
  373. }
  374. }
  375. }
  376. furi_mutex_release(mutex);
  377. }
  378. static void tama_p1_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  379. furi_assert(event_queue);
  380. TamaEvent event = {.type = EventTypeInput, .input = *input_event};
  381. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  382. }
  383. static void tama_p1_update_timer_callback(FuriMessageQueue* event_queue) {
  384. furi_assert(event_queue);
  385. TamaEvent event = {.type = EventTypeTick};
  386. furi_message_queue_put(event_queue, &event, 0);
  387. }
  388. static void tama_p1_load_state() {
  389. state_t* state;
  390. uint8_t buf[4];
  391. bool error = false;
  392. state = tamalib_get_state();
  393. Storage* storage = furi_record_open(RECORD_STORAGE);
  394. File* file = storage_file_alloc(storage);
  395. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  396. storage_file_read(file, &buf, 4);
  397. if(buf[0] != (uint8_t)STATE_FILE_MAGIC[0] || buf[1] != (uint8_t)STATE_FILE_MAGIC[1] ||
  398. buf[2] != (uint8_t)STATE_FILE_MAGIC[2] || buf[3] != (uint8_t)STATE_FILE_MAGIC[3]) {
  399. FURI_LOG_E(TAG, "FATAL: Wrong state file magic in \"%s\" !\n", TAMA_SAVE_PATH);
  400. error = true;
  401. }
  402. storage_file_read(file, &buf, 1);
  403. if(buf[0] != STATE_FILE_VERSION) {
  404. FURI_LOG_E(TAG, "FATAL: Unsupported version");
  405. error = true;
  406. }
  407. if(!error) {
  408. FURI_LOG_D(TAG, "Reading save.bin");
  409. storage_file_read(file, &buf, 2);
  410. *(state->pc) = buf[0] | ((buf[1] & 0x1F) << 8);
  411. storage_file_read(file, &buf, 2);
  412. *(state->x) = buf[0] | ((buf[1] & 0xF) << 8);
  413. storage_file_read(file, &buf, 2);
  414. *(state->y) = buf[0] | ((buf[1] & 0xF) << 8);
  415. storage_file_read(file, &buf, 1);
  416. *(state->a) = buf[0] & 0xF;
  417. storage_file_read(file, &buf, 1);
  418. *(state->b) = buf[0] & 0xF;
  419. storage_file_read(file, &buf, 1);
  420. *(state->np) = buf[0] & 0x1F;
  421. storage_file_read(file, &buf, 1);
  422. *(state->sp) = buf[0];
  423. storage_file_read(file, &buf, 1);
  424. *(state->flags) = buf[0] & 0xF;
  425. storage_file_read(file, &buf, 4);
  426. *(state->tick_counter) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  427. storage_file_read(file, &buf, 4);
  428. *(state->clk_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  429. (buf[3] << 24);
  430. storage_file_read(file, &buf, 4);
  431. *(state->prog_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) |
  432. (buf[3] << 24);
  433. storage_file_read(file, &buf, 1);
  434. *(state->prog_timer_enabled) = buf[0] & 0x1;
  435. storage_file_read(file, &buf, 1);
  436. *(state->prog_timer_data) = buf[0];
  437. storage_file_read(file, &buf, 1);
  438. *(state->prog_timer_rld) = buf[0];
  439. storage_file_read(file, &buf, 4);
  440. *(state->call_depth) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  441. FURI_LOG_D(TAG, "Restoring Interupts");
  442. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  443. storage_file_read(file, &buf, 1);
  444. state->interrupts[i].factor_flag_reg = buf[0] & 0xF;
  445. storage_file_read(file, &buf, 1);
  446. state->interrupts[i].mask_reg = buf[0] & 0xF;
  447. storage_file_read(file, &buf, 1);
  448. state->interrupts[i].triggered = buf[0] & 0x1;
  449. }
  450. /* First 640 half bytes correspond to the RAM */
  451. FURI_LOG_D(TAG, "Restoring RAM");
  452. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  453. storage_file_read(file, &buf, 1);
  454. SET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR, buf[0] & 0xF);
  455. }
  456. /* I/Os are from 0xF00 to 0xF7F */
  457. FURI_LOG_D(TAG, "Restoring I/O");
  458. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  459. storage_file_read(file, &buf, 1);
  460. SET_IO_MEMORY(state->memory, i + MEM_IO_ADDR, buf[0] & 0xF);
  461. }
  462. FURI_LOG_D(TAG, "Refreshing Hardware");
  463. tamalib_refresh_hw();
  464. }
  465. }
  466. storage_file_close(file);
  467. storage_file_free(file);
  468. furi_record_close(RECORD_STORAGE);
  469. }
  470. static void tama_p1_save_state() {
  471. // Saving state
  472. FURI_LOG_D(TAG, "Saving Gamestate");
  473. uint8_t buf[4];
  474. state_t* state;
  475. uint32_t offset = 0;
  476. state = tamalib_get_state();
  477. Storage* storage = furi_record_open(RECORD_STORAGE);
  478. File* file = storage_file_alloc(storage);
  479. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  480. buf[0] = (uint8_t)STATE_FILE_MAGIC[0];
  481. buf[1] = (uint8_t)STATE_FILE_MAGIC[1];
  482. buf[2] = (uint8_t)STATE_FILE_MAGIC[2];
  483. buf[3] = (uint8_t)STATE_FILE_MAGIC[3];
  484. offset += storage_file_write(file, &buf, sizeof(buf));
  485. buf[0] = STATE_FILE_VERSION & 0xFF;
  486. offset += storage_file_write(file, &buf, 1);
  487. buf[0] = *(state->pc) & 0xFF;
  488. buf[1] = (*(state->pc) >> 8) & 0x1F;
  489. offset += storage_file_write(file, &buf, 2);
  490. buf[0] = *(state->x) & 0xFF;
  491. buf[1] = (*(state->x) >> 8) & 0xF;
  492. offset += storage_file_write(file, &buf, 2);
  493. buf[0] = *(state->y) & 0xFF;
  494. buf[1] = (*(state->y) >> 8) & 0xF;
  495. offset += storage_file_write(file, &buf, 2);
  496. buf[0] = *(state->a) & 0xF;
  497. offset += storage_file_write(file, &buf, 1);
  498. buf[0] = *(state->b) & 0xF;
  499. offset += storage_file_write(file, &buf, 1);
  500. buf[0] = *(state->np) & 0x1F;
  501. offset += storage_file_write(file, &buf, 1);
  502. buf[0] = *(state->sp) & 0xFF;
  503. offset += storage_file_write(file, &buf, 1);
  504. buf[0] = *(state->flags) & 0xF;
  505. offset += storage_file_write(file, &buf, 1);
  506. buf[0] = *(state->tick_counter) & 0xFF;
  507. buf[1] = (*(state->tick_counter) >> 8) & 0xFF;
  508. buf[2] = (*(state->tick_counter) >> 16) & 0xFF;
  509. buf[3] = (*(state->tick_counter) >> 24) & 0xFF;
  510. offset += storage_file_write(file, &buf, sizeof(buf));
  511. buf[0] = *(state->clk_timer_timestamp) & 0xFF;
  512. buf[1] = (*(state->clk_timer_timestamp) >> 8) & 0xFF;
  513. buf[2] = (*(state->clk_timer_timestamp) >> 16) & 0xFF;
  514. buf[3] = (*(state->clk_timer_timestamp) >> 24) & 0xFF;
  515. offset += storage_file_write(file, &buf, sizeof(buf));
  516. buf[0] = *(state->prog_timer_timestamp) & 0xFF;
  517. buf[1] = (*(state->prog_timer_timestamp) >> 8) & 0xFF;
  518. buf[2] = (*(state->prog_timer_timestamp) >> 16) & 0xFF;
  519. buf[3] = (*(state->prog_timer_timestamp) >> 24) & 0xFF;
  520. offset += storage_file_write(file, &buf, sizeof(buf));
  521. buf[0] = *(state->prog_timer_enabled) & 0x1;
  522. offset += storage_file_write(file, &buf, 1);
  523. buf[0] = *(state->prog_timer_data) & 0xFF;
  524. offset += storage_file_write(file, &buf, 1);
  525. buf[0] = *(state->prog_timer_rld) & 0xFF;
  526. offset += storage_file_write(file, &buf, 1);
  527. buf[0] = *(state->call_depth) & 0xFF;
  528. buf[1] = (*(state->call_depth) >> 8) & 0xFF;
  529. buf[2] = (*(state->call_depth) >> 16) & 0xFF;
  530. buf[3] = (*(state->call_depth) >> 24) & 0xFF;
  531. offset += storage_file_write(file, &buf, sizeof(buf));
  532. for(uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  533. buf[0] = state->interrupts[i].factor_flag_reg & 0xF;
  534. offset += storage_file_write(file, &buf, 1);
  535. buf[0] = state->interrupts[i].mask_reg & 0xF;
  536. offset += storage_file_write(file, &buf, 1);
  537. buf[0] = state->interrupts[i].triggered & 0x1;
  538. offset += storage_file_write(file, &buf, 1);
  539. }
  540. /* First 640 half bytes correspond to the RAM */
  541. for(uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  542. buf[0] = GET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR) & 0xF;
  543. offset += storage_file_write(file, &buf, 1);
  544. }
  545. /* I/Os are from 0xF00 to 0xF7F */
  546. for(uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  547. buf[0] = GET_IO_MEMORY(state->memory, i + MEM_IO_ADDR) & 0xF;
  548. offset += storage_file_write(file, &buf, 1);
  549. }
  550. }
  551. storage_file_close(file);
  552. storage_file_free(file);
  553. furi_record_close(RECORD_STORAGE);
  554. FURI_LOG_D(TAG, "Finished Writing %lu", offset);
  555. }
  556. static int32_t tama_p1_worker(void* context) {
  557. bool running = true;
  558. FuriMutex* mutex = context;
  559. while(furi_mutex_acquire(mutex, FuriWaitForever) != FuriStatusOk) furi_delay_tick(1);
  560. cpu_sync_ref_timestamp();
  561. LL_TIM_EnableCounter(TIM2);
  562. tama_p1_load_state();
  563. while(running) {
  564. if(furi_thread_flags_get()) {
  565. running = false;
  566. } else {
  567. // FURI_LOG_D(TAG, "Stepping");
  568. // for (int i = 0; i < 100; ++i)
  569. tamalib_step();
  570. }
  571. }
  572. LL_TIM_DisableCounter(TIM2);
  573. furi_mutex_release(mutex);
  574. return 0;
  575. }
  576. static void tama_p1_init(TamaApp* const ctx) {
  577. g_ctx = ctx;
  578. memset(ctx, 0, sizeof(TamaApp));
  579. tama_p1_hal_init(&ctx->hal);
  580. // Load ROM
  581. Storage* storage = furi_record_open(RECORD_STORAGE);
  582. FileInfo fi;
  583. if(storage_common_stat(storage, TAMA_ROM_PATH, &fi) == FSE_OK) {
  584. File* rom_file = storage_file_alloc(storage);
  585. if(storage_file_open(rom_file, TAMA_ROM_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  586. ctx->rom = malloc((size_t)fi.size);
  587. uint8_t* buf_ptr = ctx->rom;
  588. size_t read = 0;
  589. while(read < fi.size) {
  590. size_t to_read = fi.size - read;
  591. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  592. uint16_t now_read = storage_file_read(rom_file, buf_ptr, (uint16_t)to_read);
  593. read += now_read;
  594. buf_ptr += now_read;
  595. }
  596. // Reorder endianess of ROM
  597. for(size_t i = 0; i < fi.size; i += 2) {
  598. uint8_t b = ctx->rom[i];
  599. ctx->rom[i] = ctx->rom[i + 1];
  600. ctx->rom[i + 1] = b & 0xF;
  601. }
  602. }
  603. storage_file_close(rom_file);
  604. storage_file_free(rom_file);
  605. }
  606. furi_record_close(RECORD_STORAGE);
  607. if(ctx->rom != NULL) {
  608. // Init TIM2
  609. // 64KHz
  610. LL_TIM_InitTypeDef tim_init = {
  611. .Prescaler = 999,
  612. .CounterMode = LL_TIM_COUNTERMODE_UP,
  613. .Autoreload = 0xFFFFFFFF,
  614. };
  615. LL_TIM_Init(TIM2, &tim_init);
  616. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  617. LL_TIM_DisableCounter(TIM2);
  618. LL_TIM_SetCounter(TIM2, 0);
  619. // Init TamaLIB
  620. tamalib_register_hal(&ctx->hal);
  621. tamalib_init((u12_t*)ctx->rom, NULL, 64000);
  622. tamalib_set_speed(speed);
  623. // TODO: implement fast forwarding
  624. ctx->fast_forward_done = true;
  625. // Start stepping thread
  626. ctx->thread = furi_thread_alloc();
  627. furi_thread_set_name(ctx->thread, "TamaLIB");
  628. furi_thread_set_stack_size(ctx->thread, 1024);
  629. furi_thread_set_callback(ctx->thread, tama_p1_worker);
  630. furi_thread_set_context(ctx->thread, g_state_mutex);
  631. furi_thread_start(ctx->thread);
  632. }
  633. }
  634. static void tama_p1_deinit(TamaApp* const ctx) {
  635. if(ctx->rom != NULL) {
  636. tamalib_release();
  637. furi_thread_free(ctx->thread);
  638. free(ctx->rom);
  639. }
  640. }
  641. int32_t tama_p1_app(void* p) {
  642. UNUSED(p);
  643. TamaApp* ctx = malloc(sizeof(TamaApp));
  644. g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  645. tama_p1_init(ctx);
  646. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TamaEvent));
  647. ViewPort* view_port = view_port_alloc();
  648. view_port_draw_callback_set(view_port, tama_p1_draw_callback, g_state_mutex);
  649. view_port_input_callback_set(view_port, tama_p1_input_callback, event_queue);
  650. Gui* gui = furi_record_open(RECORD_GUI);
  651. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  652. FuriTimer* timer =
  653. furi_timer_alloc(tama_p1_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  654. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  655. // in_menu = false;
  656. // menu_cursor = 2;
  657. for(bool running = true; running;) {
  658. TamaEvent event;
  659. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  660. if(event_status == FuriStatusOk) {
  661. // Local override with acquired context
  662. if(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk) continue;
  663. if(event.type == EventTypeTick) {
  664. // FURI_LOG_D(TAG, "EventTypeTick");
  665. view_port_update(view_port);
  666. } else if(event.type == EventTypeInput) {
  667. FURI_LOG_D(
  668. TAG,
  669. "EventTypeInput: %ld %d %d",
  670. event.input.sequence,
  671. event.input.key,
  672. event.input.type);
  673. InputType input_type = event.input.type;
  674. btn_state_t tama_btn_state = 0;
  675. if(in_menu) {
  676. if(event.input.key == InputKeyBack) {
  677. in_menu = false;
  678. } else if(event.input.key == InputKeyUp && event.input.type == InputTypePress) {
  679. if(menu_cursor > 0) {
  680. menu_cursor -= 1;
  681. } else {
  682. sub_menu_default = 0;
  683. menu_cursor = menu_items - 1;
  684. }
  685. } else if(event.input.key == InputKeyDown && event.input.type == InputTypePress) {
  686. if(menu_cursor < menu_items - 1) {
  687. sub_menu_default = 0;
  688. menu_cursor += 1;
  689. } else {
  690. menu_cursor = 0;
  691. }
  692. } else if(event.input.key == InputKeyLeft && event.input.type == InputTypePress) {
  693. switch(menu_cursor) {
  694. case 1:
  695. switch(layout_mode) {
  696. case 0:
  697. layout_mode = 4;
  698. break;
  699. case 1:
  700. case 2:
  701. case 3:
  702. case 4:
  703. layout_mode -= 1;
  704. break;
  705. }
  706. break;
  707. case 2:
  708. speed_down();
  709. break;
  710. case menu_items - 1:
  711. switch(sub_menu_default) {
  712. case 0:
  713. sub_menu_default = 2;
  714. break;
  715. case 1:
  716. case 2:
  717. // sub_menu_default = 0;
  718. sub_menu_default -= 1;
  719. break;
  720. default:
  721. break;
  722. }
  723. break;
  724. default:
  725. break;
  726. }
  727. } else if(event.input.key == InputKeyRight && event.input.type == InputTypePress) {
  728. switch(menu_cursor) {
  729. case 1:
  730. switch(layout_mode) {
  731. case 0:
  732. case 1:
  733. case 2:
  734. case 3:
  735. layout_mode += 1;
  736. break;
  737. case 4:
  738. layout_mode = 0;
  739. break;
  740. }
  741. break;
  742. case 2:
  743. speed_up();
  744. break;
  745. case menu_items - 1:
  746. switch(sub_menu_default) {
  747. case 0:
  748. case 1:
  749. // sub_menu_default = 2;
  750. sub_menu_default += 1;
  751. break;
  752. case 2:
  753. sub_menu_default = 0;
  754. break;
  755. default:
  756. break;
  757. }
  758. break;
  759. default:
  760. break;
  761. }
  762. } else if(event.input.key == InputKeyOk) {
  763. switch(menu_cursor) {
  764. case 0:
  765. // mute tamagotchi
  766. if(input_type == InputTypePress)
  767. tama_btn_state = BTN_STATE_PRESSED;
  768. else if(input_type == InputTypeRelease)
  769. tama_btn_state = BTN_STATE_RELEASED;
  770. tamalib_set_button(BTN_LEFT, tama_btn_state);
  771. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  772. break;
  773. case 1:
  774. if(event.input.type == InputTypePress) {
  775. switch(layout_mode) {
  776. case 0:
  777. case 1:
  778. case 2:
  779. layout_mode += 1;
  780. break;
  781. case 3:
  782. layout_mode = 0;
  783. break;
  784. }
  785. }
  786. break;
  787. case 2:
  788. if(event.input.type == InputTypePress) {
  789. speed_up();
  790. }
  791. break;
  792. case menu_items - 1:
  793. default:
  794. switch(sub_menu_default) {
  795. case 0:
  796. in_menu = false;
  797. break;
  798. case 1:
  799. if(speed != 1) {
  800. uint8_t temp = speed;
  801. speed = 1;
  802. tamalib_set_speed(speed);
  803. speed = temp;
  804. tamalib_set_speed(speed);
  805. }
  806. tama_p1_save_state();
  807. in_menu = false;
  808. break;
  809. case 2:
  810. if(speed != 1) {
  811. speed = 1;
  812. tamalib_set_speed(speed);
  813. }
  814. furi_timer_stop(timer);
  815. tama_p1_save_state();
  816. running = false;
  817. break;
  818. default:
  819. break;
  820. }
  821. break;
  822. }
  823. }
  824. } else { // out of menu
  825. if(input_type == InputTypePress)
  826. tama_btn_state = BTN_STATE_PRESSED;
  827. else if(input_type == InputTypeRelease)
  828. tama_btn_state = BTN_STATE_RELEASED;
  829. if(input_type == InputTypePress || input_type == InputTypeRelease) {
  830. if(event.input.key == InputKeyOk) {
  831. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  832. }
  833. switch(layout_mode) {
  834. case 0:
  835. case 1:
  836. case 2:
  837. switch(event.input.key) {
  838. case InputKeyLeft:
  839. tamalib_set_button(BTN_LEFT, tama_btn_state);
  840. break;
  841. case InputKeyDown:
  842. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  843. break;
  844. case InputKeyRight:
  845. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  846. break;
  847. case InputKeyUp:
  848. sub_menu_default = 0;
  849. in_menu = true;
  850. break;
  851. default:
  852. break;
  853. }
  854. break;
  855. case 3:
  856. switch(event.input.key) {
  857. case InputKeyDown:
  858. tamalib_set_button(BTN_LEFT, tama_btn_state);
  859. break;
  860. case InputKeyRight:
  861. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  862. break;
  863. case InputKeyUp:
  864. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  865. break;
  866. case InputKeyLeft:
  867. sub_menu_default = 0;
  868. in_menu = true;
  869. break;
  870. default:
  871. break;
  872. }
  873. break;
  874. case 4:
  875. switch(event.input.key) {
  876. case InputKeyUp:
  877. tamalib_set_button(BTN_LEFT, tama_btn_state);
  878. break;
  879. case InputKeyLeft:
  880. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  881. break;
  882. case InputKeyDown:
  883. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  884. break;
  885. case InputKeyRight:
  886. sub_menu_default = 0;
  887. in_menu = true;
  888. break;
  889. default:
  890. break;
  891. }
  892. break;
  893. default:
  894. break;
  895. }
  896. }
  897. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) {
  898. if(speed != 1) {
  899. speed = 1;
  900. tamalib_set_speed(speed);
  901. }
  902. furi_timer_stop(timer);
  903. tama_p1_save_state();
  904. running = false;
  905. }
  906. }
  907. }
  908. furi_mutex_release(g_state_mutex);
  909. }
  910. // else {
  911. // // Timeout
  912. // // FURI_LOG_D(TAG, "Timed out");
  913. // }
  914. }
  915. if(ctx->rom != NULL) {
  916. furi_thread_flags_set(furi_thread_get_id(ctx->thread), 1);
  917. furi_thread_join(ctx->thread);
  918. }
  919. furi_timer_free(timer);
  920. view_port_enabled_set(view_port, false);
  921. gui_remove_view_port(gui, view_port);
  922. furi_record_close(RECORD_GUI);
  923. view_port_free(view_port);
  924. furi_message_queue_free(event_queue);
  925. furi_mutex_free(g_state_mutex);
  926. tama_p1_deinit(ctx);
  927. free(ctx);
  928. return 0;
  929. }