tama_p1.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. static const Icon* icons_list[] = {
  13. &I_icon_0,
  14. &I_icon_1,
  15. &I_icon_2,
  16. &I_icon_3,
  17. &I_icon_4,
  18. &I_icon_5,
  19. &I_icon_6,
  20. &I_icon_7,
  21. };
  22. static void tama_p1_draw_callback(Canvas* const canvas, void* cb_ctx) {
  23. furi_assert(cb_ctx);
  24. FuriMutex* const mutex = cb_ctx;
  25. if(furi_mutex_acquire(mutex, 25) != FuriStatusOk) return;
  26. if(g_ctx->rom == NULL) {
  27. canvas_set_font(canvas, FontPrimary);
  28. canvas_draw_str(canvas, 30, 30, "No ROM");
  29. } else if(g_ctx->halted) {
  30. canvas_set_font(canvas, FontPrimary);
  31. canvas_draw_str(canvas, 30, 30, "Halted");
  32. } else {
  33. // FURI_LOG_D(TAG, "Drawing frame");
  34. // Calculate positioning
  35. uint16_t canv_width = canvas_width(canvas);
  36. uint16_t canv_height = canvas_height(canvas);
  37. uint16_t lcd_matrix_scaled_width = 32 * TAMA_SCREEN_SCALE_FACTOR;
  38. uint16_t lcd_matrix_scaled_height = 16 * TAMA_SCREEN_SCALE_FACTOR;
  39. uint16_t lcd_matrix_top = 0;
  40. uint16_t lcd_matrix_left = (canv_width - lcd_matrix_scaled_width) / 2;
  41. uint16_t lcd_icon_upper_top = lcd_matrix_top - TAMA_LCD_ICON_SIZE - TAMA_LCD_ICON_MARGIN;
  42. uint16_t lcd_icon_upper_left = lcd_matrix_left;
  43. uint16_t lcd_icon_lower_top =
  44. lcd_matrix_top + lcd_matrix_scaled_height + TAMA_LCD_ICON_MARGIN;
  45. uint16_t lcd_icon_lower_left = lcd_matrix_left;
  46. uint16_t lcd_icon_spacing_horiz =
  47. (lcd_matrix_scaled_width - (4 * TAMA_LCD_ICON_SIZE)) / 3 + TAMA_LCD_ICON_SIZE;
  48. uint16_t y = lcd_matrix_top;
  49. for(uint8_t row = 0; row < 16; ++row) {
  50. uint16_t x = lcd_matrix_left;
  51. uint32_t row_pixels = g_ctx->framebuffer[row];
  52. for(uint8_t col = 0; col < 32; ++col) {
  53. if(row_pixels & 1) {
  54. canvas_draw_box(
  55. 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. // Draw Icons on bottom
  63. uint8_t lcd_icons = g_ctx->icons;
  64. uint16_t x_ic = 0;
  65. y = 64 - TAMA_LCD_ICON_SIZE;
  66. for(uint8_t i = 0; i < 7; ++i) {
  67. if(lcd_icons & 1) {
  68. canvas_draw_icon(canvas, x_ic, y, icons_list[i]);
  69. }
  70. x_ic += TAMA_LCD_ICON_SIZE + 4;
  71. lcd_icons >>= 1;
  72. }
  73. if (lcd_icons & 7) {
  74. canvas_draw_icon(canvas, 128 - TAMA_LCD_ICON_SIZE, 0, icons_list[7]);
  75. }
  76. }
  77. furi_mutex_release(mutex);
  78. }
  79. static void tama_p1_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  80. furi_assert(event_queue);
  81. TamaEvent event = {.type = EventTypeInput, .input = *input_event};
  82. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  83. }
  84. static void tama_p1_update_timer_callback(FuriMessageQueue* event_queue) {
  85. furi_assert(event_queue);
  86. TamaEvent event = {.type = EventTypeTick};
  87. furi_message_queue_put(event_queue, &event, 0);
  88. }
  89. static void tama_p1_load_state() {
  90. state_t *state;
  91. uint8_t buf[4];
  92. bool error = false;
  93. state = tamalib_get_state();
  94. Storage* storage = furi_record_open(RECORD_STORAGE);
  95. File* file = storage_file_alloc(storage);
  96. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  97. storage_file_read(file, &buf, 4);
  98. if (buf[0] != (uint8_t) STATE_FILE_MAGIC[0] || buf[1] != (uint8_t) STATE_FILE_MAGIC[1] ||
  99. buf[2] != (uint8_t) STATE_FILE_MAGIC[2] || buf[3] != (uint8_t) STATE_FILE_MAGIC[3]) {
  100. FURI_LOG_E(TAG, "FATAL: Wrong state file magic in \"%s\" !\n", TAMA_SAVE_PATH);
  101. error = true;
  102. }
  103. storage_file_read(file, &buf, 1);
  104. if (buf[0] != STATE_FILE_VERSION) {
  105. FURI_LOG_E(TAG, "FATAL: Unsupported version");
  106. error = true;
  107. }
  108. if (!error) {
  109. FURI_LOG_D(TAG, "Reading save.bin");
  110. storage_file_read(file, &buf, 2);
  111. *(state->pc) = buf[0] | ((buf[1] & 0x1F) << 8);
  112. storage_file_read(file, &buf, 2);
  113. *(state->x) = buf[0] | ((buf[1] & 0xF) << 8);
  114. storage_file_read(file, &buf, 2);
  115. *(state->y) = buf[0] | ((buf[1] & 0xF) << 8);
  116. storage_file_read(file, &buf, 1);
  117. *(state->a) = buf[0] & 0xF;
  118. storage_file_read(file, &buf, 1);
  119. *(state->b) = buf[0] & 0xF;
  120. storage_file_read(file, &buf, 1);
  121. *(state->np) = buf[0] & 0x1F;
  122. storage_file_read(file, &buf, 1);
  123. *(state->sp) = buf[0];
  124. storage_file_read(file, &buf, 1);
  125. *(state->flags) = buf[0] & 0xF;
  126. storage_file_read(file, &buf, 4);
  127. *(state->tick_counter) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  128. storage_file_read(file, &buf, 4);
  129. *(state->clk_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  130. storage_file_read(file, &buf, 4);
  131. *(state->prog_timer_timestamp) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  132. storage_file_read(file, &buf, 1);
  133. *(state->prog_timer_enabled) = buf[0] & 0x1;
  134. storage_file_read(file, &buf, 1);
  135. *(state->prog_timer_data) = buf[0];
  136. storage_file_read(file, &buf, 1);
  137. *(state->prog_timer_rld) = buf[0];
  138. storage_file_read(file, &buf, 4);
  139. *(state->call_depth) = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  140. FURI_LOG_D(TAG, "Restoring Interupts");
  141. for (uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  142. storage_file_read(file, &buf, 1);
  143. state->interrupts[i].factor_flag_reg = buf[0] & 0xF;
  144. storage_file_read(file, &buf, 1);
  145. state->interrupts[i].mask_reg = buf[0] & 0xF;
  146. storage_file_read(file, &buf, 1);
  147. state->interrupts[i].triggered = buf[0] & 0x1;
  148. }
  149. /* First 640 half bytes correspond to the RAM */
  150. FURI_LOG_D(TAG, "Restoring RAM");
  151. for (uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  152. storage_file_read(file, &buf, 1);
  153. SET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR, buf[0] & 0xF);
  154. }
  155. /* I/Os are from 0xF00 to 0xF7F */
  156. FURI_LOG_D(TAG, "Restoring I/O");
  157. for (uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  158. storage_file_read(file, &buf, 1);
  159. SET_IO_MEMORY(state->memory, i + MEM_IO_ADDR, buf[0] & 0xF);
  160. }
  161. FURI_LOG_D(TAG, "Refreshing Hardware");
  162. tamalib_refresh_hw();
  163. }
  164. }
  165. storage_file_close(file);
  166. storage_file_free(file);
  167. furi_record_close(RECORD_STORAGE);
  168. }
  169. static void tama_p1_save_state() {
  170. // Saving state
  171. FURI_LOG_D(TAG, "Saving Gamestate");
  172. uint8_t buf[4];
  173. state_t *state;
  174. uint32_t offset = 0;
  175. state = tamalib_get_state();
  176. Storage* storage = furi_record_open(RECORD_STORAGE);
  177. File* file = storage_file_alloc(storage);
  178. if(storage_file_open(file, TAMA_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  179. buf[0] = (uint8_t) STATE_FILE_MAGIC[0];
  180. buf[1] = (uint8_t) STATE_FILE_MAGIC[1];
  181. buf[2] = (uint8_t) STATE_FILE_MAGIC[2];
  182. buf[3] = (uint8_t) STATE_FILE_MAGIC[3];
  183. offset += storage_file_write(file, &buf, sizeof(buf));
  184. buf[0] = STATE_FILE_VERSION & 0xFF;
  185. offset += storage_file_write(file, &buf, 1);
  186. buf[0] = *(state->pc) & 0xFF;
  187. buf[1] = (*(state->pc) >> 8) & 0x1F;
  188. offset += storage_file_write(file, &buf, 2);
  189. buf[0] = *(state->x) & 0xFF;
  190. buf[1] = (*(state->x) >> 8) & 0xF;
  191. offset += storage_file_write(file, &buf, 2);
  192. buf[0] = *(state->y) & 0xFF;
  193. buf[1] = (*(state->y) >> 8) & 0xF;
  194. offset += storage_file_write(file, &buf, 2);
  195. buf[0] = *(state->a) & 0xF;
  196. offset += storage_file_write(file, &buf, 1);
  197. buf[0] = *(state->b) & 0xF;
  198. offset += storage_file_write(file, &buf, 1);
  199. buf[0] = *(state->np) & 0x1F;
  200. offset += storage_file_write(file, &buf, 1);
  201. buf[0] = *(state->sp) & 0xFF;
  202. offset += storage_file_write(file, &buf, 1);
  203. buf[0] = *(state->flags) & 0xF;
  204. offset += storage_file_write(file, &buf, 1);
  205. buf[0] = *(state->tick_counter) & 0xFF;
  206. buf[1] = (*(state->tick_counter) >> 8) & 0xFF;
  207. buf[2] = (*(state->tick_counter) >> 16) & 0xFF;
  208. buf[3] = (*(state->tick_counter) >> 24) & 0xFF;
  209. offset += storage_file_write(file, &buf, sizeof(buf));
  210. buf[0] = *(state->clk_timer_timestamp) & 0xFF;
  211. buf[1] = (*(state->clk_timer_timestamp) >> 8) & 0xFF;
  212. buf[2] = (*(state->clk_timer_timestamp) >> 16) & 0xFF;
  213. buf[3] = (*(state->clk_timer_timestamp) >> 24) & 0xFF;
  214. offset += storage_file_write(file, &buf, sizeof(buf));
  215. buf[0] = *(state->prog_timer_timestamp) & 0xFF;
  216. buf[1] = (*(state->prog_timer_timestamp) >> 8) & 0xFF;
  217. buf[2] = (*(state->prog_timer_timestamp) >> 16) & 0xFF;
  218. buf[3] = (*(state->prog_timer_timestamp) >> 24) & 0xFF;
  219. offset += storage_file_write(file, &buf, sizeof(buf));
  220. buf[0] = *(state->prog_timer_enabled) & 0x1;
  221. offset += storage_file_write(file, &buf, 1);
  222. buf[0] = *(state->prog_timer_data) & 0xFF;
  223. offset += storage_file_write(file, &buf, 1);
  224. buf[0] = *(state->prog_timer_rld) & 0xFF;
  225. offset += storage_file_write(file, &buf, 1);
  226. buf[0] = *(state->call_depth) & 0xFF;
  227. buf[1] = (*(state->call_depth) >> 8) & 0xFF;
  228. buf[2] = (*(state->call_depth) >> 16) & 0xFF;
  229. buf[3] = (*(state->call_depth) >> 24) & 0xFF;
  230. offset += storage_file_write(file, &buf, sizeof(buf));
  231. for (uint32_t i = 0; i < INT_SLOT_NUM; i++) {
  232. buf[0] = state->interrupts[i].factor_flag_reg & 0xF;
  233. offset += storage_file_write(file, &buf, 1);
  234. buf[0] = state->interrupts[i].mask_reg & 0xF;
  235. offset += storage_file_write(file, &buf, 1);
  236. buf[0] = state->interrupts[i].triggered & 0x1;
  237. offset += storage_file_write(file, &buf, 1);
  238. }
  239. /* First 640 half bytes correspond to the RAM */
  240. for (uint32_t i = 0; i < MEM_RAM_SIZE; i++) {
  241. buf[0] = GET_RAM_MEMORY(state->memory, i + MEM_RAM_ADDR) & 0xF;
  242. offset += storage_file_write(file, &buf, 1);
  243. }
  244. /* I/Os are from 0xF00 to 0xF7F */
  245. for (uint32_t i = 0; i < MEM_IO_SIZE; i++) {
  246. buf[0] = GET_IO_MEMORY(state->memory, i + MEM_IO_ADDR) & 0xF;
  247. offset += storage_file_write(file, &buf, 1);
  248. }
  249. }
  250. storage_file_close(file);
  251. storage_file_free(file);
  252. furi_record_close(RECORD_STORAGE);
  253. FURI_LOG_D(TAG, "Finished Writing %lu", offset);
  254. }
  255. static int32_t tama_p1_worker(void* context) {
  256. bool running = true;
  257. FuriMutex* mutex = context;
  258. while(furi_mutex_acquire(mutex, FuriWaitForever) != FuriStatusOk) furi_delay_tick(1);
  259. cpu_sync_ref_timestamp();
  260. LL_TIM_EnableCounter(TIM2);
  261. tama_p1_load_state();
  262. while(running) {
  263. if(furi_thread_flags_get()) {
  264. running = false;
  265. } else {
  266. // FURI_LOG_D(TAG, "Stepping");
  267. // for (int i = 0; i < 100; ++i)
  268. tamalib_step();
  269. }
  270. }
  271. LL_TIM_DisableCounter(TIM2);
  272. furi_mutex_release(mutex);
  273. return 0;
  274. }
  275. static void tama_p1_init(TamaApp* const ctx) {
  276. g_ctx = ctx;
  277. memset(ctx, 0, sizeof(TamaApp));
  278. tama_p1_hal_init(&ctx->hal);
  279. // Load ROM
  280. Storage* storage = furi_record_open(RECORD_STORAGE);
  281. FileInfo fi;
  282. if(storage_common_stat(storage, TAMA_ROM_PATH, &fi) == FSE_OK) {
  283. File* rom_file = storage_file_alloc(storage);
  284. if(storage_file_open(rom_file, TAMA_ROM_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  285. ctx->rom = malloc((size_t)fi.size);
  286. uint8_t* buf_ptr = ctx->rom;
  287. size_t read = 0;
  288. while(read < fi.size) {
  289. size_t to_read = fi.size - read;
  290. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  291. uint16_t now_read = storage_file_read(rom_file, buf_ptr, (uint16_t)to_read);
  292. read += now_read;
  293. buf_ptr += now_read;
  294. }
  295. // Reorder endianess of ROM
  296. for(size_t i = 0; i < fi.size; i += 2) {
  297. uint8_t b = ctx->rom[i];
  298. ctx->rom[i] = ctx->rom[i + 1];
  299. ctx->rom[i + 1] = b & 0xF;
  300. }
  301. }
  302. storage_file_close(rom_file);
  303. storage_file_free(rom_file);
  304. }
  305. furi_record_close(RECORD_STORAGE);
  306. if(ctx->rom != NULL) {
  307. // Init TIM2
  308. // 64KHz
  309. LL_TIM_InitTypeDef tim_init = {
  310. .Prescaler = 999,
  311. .CounterMode = LL_TIM_COUNTERMODE_UP,
  312. .Autoreload = 0xFFFFFFFF,
  313. };
  314. LL_TIM_Init(TIM2, &tim_init);
  315. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  316. LL_TIM_DisableCounter(TIM2);
  317. LL_TIM_SetCounter(TIM2, 0);
  318. // Init TamaLIB
  319. tamalib_register_hal(&ctx->hal);
  320. tamalib_init((u12_t*)ctx->rom, NULL, 64000);
  321. tamalib_set_speed(1);
  322. // TODO: implement fast forwarding
  323. ctx->fast_forward_done = true;
  324. // Start stepping thread
  325. ctx->thread = furi_thread_alloc();
  326. furi_thread_set_name(ctx->thread, "TamaLIB");
  327. furi_thread_set_stack_size(ctx->thread, 1024);
  328. furi_thread_set_callback(ctx->thread, tama_p1_worker);
  329. furi_thread_set_context(ctx->thread, g_state_mutex);
  330. furi_thread_start(ctx->thread);
  331. }
  332. }
  333. static void tama_p1_deinit(TamaApp* const ctx) {
  334. if(ctx->rom != NULL) {
  335. tamalib_release();
  336. furi_thread_free(ctx->thread);
  337. free(ctx->rom);
  338. }
  339. }
  340. int32_t tama_p1_app(void* p) {
  341. UNUSED(p);
  342. TamaApp* ctx = malloc(sizeof(TamaApp));
  343. g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  344. tama_p1_init(ctx);
  345. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TamaEvent));
  346. ViewPort* view_port = view_port_alloc();
  347. view_port_draw_callback_set(view_port, tama_p1_draw_callback, g_state_mutex);
  348. view_port_input_callback_set(view_port, tama_p1_input_callback, event_queue);
  349. Gui* gui = furi_record_open(RECORD_GUI);
  350. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  351. FuriTimer* timer =
  352. furi_timer_alloc(tama_p1_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  353. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  354. for(bool running = true; running;) {
  355. TamaEvent event;
  356. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  357. if(event_status == FuriStatusOk) {
  358. // Local override with acquired context
  359. if(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk) continue;
  360. if(event.type == EventTypeTick) {
  361. // FURI_LOG_D(TAG, "EventTypeTick");
  362. view_port_update(view_port);
  363. } else if(event.type == EventTypeInput) {
  364. FURI_LOG_D(
  365. TAG,
  366. "EventTypeInput: %ld %d %d",
  367. event.input.sequence,
  368. event.input.key,
  369. event.input.type);
  370. InputType input_type = event.input.type;
  371. if(input_type == InputTypePress || input_type == InputTypeRelease) {
  372. btn_state_t tama_btn_state = 0;
  373. if(input_type == InputTypePress)
  374. tama_btn_state = BTN_STATE_PRESSED;
  375. else if(input_type == InputTypeRelease)
  376. tama_btn_state = BTN_STATE_RELEASED;
  377. if(event.input.key == InputKeyLeft) {
  378. tamalib_set_button(BTN_LEFT, tama_btn_state);
  379. } else if(event.input.key == InputKeyOk) {
  380. tamalib_set_button(BTN_MIDDLE, tama_btn_state);
  381. } else if(event.input.key == InputKeyRight) {
  382. tamalib_set_button(BTN_RIGHT, tama_btn_state);
  383. }
  384. }
  385. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) {
  386. furi_timer_stop(timer);
  387. running = false;
  388. tama_p1_save_state();
  389. }
  390. }
  391. furi_mutex_release(g_state_mutex);
  392. } else {
  393. // Timeout
  394. // FURI_LOG_D(TAG, "Timed out");
  395. }
  396. }
  397. if(ctx->rom != NULL) {
  398. furi_thread_flags_set(furi_thread_get_id(ctx->thread), 1);
  399. furi_thread_join(ctx->thread);
  400. }
  401. furi_timer_free(timer);
  402. view_port_enabled_set(view_port, false);
  403. gui_remove_view_port(gui, view_port);
  404. furi_record_close(RECORD_GUI);
  405. view_port_free(view_port);
  406. furi_message_queue_free(event_queue);
  407. furi_mutex_free(g_state_mutex);
  408. tama_p1_deinit(ctx);
  409. free(ctx);
  410. return 0;
  411. }