tama_p1.c 33 KB

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