tama_p1.c 36 KB

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