tama_p1.c 34 KB

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