tama_p1.c 18 KB

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