cpu.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * TamaLIB - A hardware agnostic Tamagotchi P1 emulation library
  3. *
  4. * Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #ifndef _CPU_H_
  21. #define _CPU_H_
  22. #include "hal.h"
  23. #define MEMORY_SIZE 4096 // 4096 x 4 bits (640 x 4 bits of RAM)
  24. #define MEM_RAM_ADDR 0x000
  25. #define MEM_RAM_SIZE 0x280
  26. #define MEM_DISPLAY1_ADDR 0xE00
  27. #define MEM_DISPLAY1_SIZE 0x050
  28. #define MEM_DISPLAY2_ADDR 0xE80
  29. #define MEM_DISPLAY2_SIZE 0x050
  30. #define MEM_IO_ADDR 0xF00
  31. #define MEM_IO_SIZE 0x080
  32. /* Define this if you want to reduce the footprint of the memory buffer from 4096 u4_t (most likely bytes)
  33. * to 464 u8_t (bytes for sure), while increasing slightly the number of operations needed to read/write from/to it.
  34. */
  35. #define LOW_FOOTPRINT
  36. #ifdef LOW_FOOTPRINT
  37. /* Invalid memory areas are not buffered to reduce the footprint of the library in memory */
  38. #define MEM_BUFFER_SIZE (MEM_RAM_SIZE + MEM_DISPLAY1_SIZE + MEM_DISPLAY2_SIZE + MEM_IO_SIZE) / 2
  39. /* Maps the CPU memory to the memory buffer */
  40. #define RAM_TO_MEMORY(n) ((n - MEM_RAM_ADDR) / 2)
  41. #define DISP1_TO_MEMORY(n) ((n - MEM_DISPLAY1_ADDR + MEM_RAM_SIZE) / 2)
  42. #define DISP2_TO_MEMORY(n) ((n - MEM_DISPLAY2_ADDR + MEM_RAM_SIZE + MEM_DISPLAY1_SIZE) / 2)
  43. #define IO_TO_MEMORY(n) \
  44. ((n - MEM_IO_ADDR + MEM_RAM_SIZE + MEM_DISPLAY1_SIZE + MEM_DISPLAY2_SIZE) / 2)
  45. #define SET_RAM_MEMORY(buffer, n, v) \
  46. { \
  47. buffer[RAM_TO_MEMORY(n)] = (buffer[RAM_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | \
  48. ((v)&0xF) << (((n) % 2) << 2); \
  49. }
  50. #define SET_DISP1_MEMORY(buffer, n, v) \
  51. { \
  52. buffer[DISP1_TO_MEMORY(n)] = (buffer[DISP1_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | \
  53. ((v)&0xF) << (((n) % 2) << 2); \
  54. }
  55. #define SET_DISP2_MEMORY(buffer, n, v) \
  56. { \
  57. buffer[DISP2_TO_MEMORY(n)] = (buffer[DISP2_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | \
  58. ((v)&0xF) << (((n) % 2) << 2); \
  59. }
  60. #define SET_IO_MEMORY(buffer, n, v) \
  61. { \
  62. buffer[IO_TO_MEMORY(n)] = (buffer[IO_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | \
  63. ((v)&0xF) << (((n) % 2) << 2); \
  64. }
  65. #define SET_MEMORY(buffer, n, v) \
  66. { \
  67. if((n) < (MEM_RAM_ADDR + MEM_RAM_SIZE)) { \
  68. SET_RAM_MEMORY(buffer, n, v); \
  69. } else if((n) < MEM_DISPLAY1_ADDR) { \
  70. /* INVALID_MEMORY */ \
  71. } else if((n) < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) { \
  72. SET_DISP1_MEMORY(buffer, n, v); \
  73. } else if((n) < MEM_DISPLAY2_ADDR) { \
  74. /* INVALID_MEMORY */ \
  75. } else if((n) < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) { \
  76. SET_DISP2_MEMORY(buffer, n, v); \
  77. } else if((n) < MEM_IO_ADDR) { \
  78. /* INVALID_MEMORY */ \
  79. } else if((n) < (MEM_IO_ADDR + MEM_IO_SIZE)) { \
  80. SET_IO_MEMORY(buffer, n, v); \
  81. } else { \
  82. /* INVALID_MEMORY */ \
  83. } \
  84. }
  85. #define GET_RAM_MEMORY(buffer, n) ((buffer[RAM_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
  86. #define GET_DISP1_MEMORY(buffer, n) ((buffer[DISP1_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
  87. #define GET_DISP2_MEMORY(buffer, n) ((buffer[DISP2_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
  88. #define GET_IO_MEMORY(buffer, n) ((buffer[IO_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
  89. #define GET_MEMORY(buffer, n) \
  90. ((buffer \
  91. [((n) < (MEM_RAM_ADDR + MEM_RAM_SIZE)) ? RAM_TO_MEMORY(n) : \
  92. ((n) < MEM_DISPLAY1_ADDR) ? 0 : \
  93. ((n) < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) ? DISP1_TO_MEMORY(n) : \
  94. ((n) < MEM_DISPLAY2_ADDR) ? 0 : \
  95. ((n) < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) ? DISP2_TO_MEMORY(n) : \
  96. ((n) < MEM_IO_ADDR) ? 0 : \
  97. ((n) < (MEM_IO_ADDR + MEM_IO_SIZE)) ? IO_TO_MEMORY(n) : \
  98. 0] >> \
  99. (((n) % 2) << 2)) & \
  100. 0xF)
  101. #define MEM_BUFFER_TYPE u8_t
  102. #else
  103. #define MEM_BUFFER_SIZE MEMORY_SIZE
  104. #define SET_MEMORY(buffer, n, v) \
  105. { buffer[n] = v; }
  106. #define SET_RAM_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
  107. #define SET_DISP1_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
  108. #define SET_DISP2_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
  109. #define SET_IO_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
  110. #define GET_MEMORY(buffer, n) (buffer[n])
  111. #define GET_RAM_MEMORY(buffer, n) GET_MEMORY(buffer, n)
  112. #define GET_DISP1_MEMORY(buffer, n) GET_MEMORY(buffer, n)
  113. #define GET_DISP2_MEMORY(buffer, n) GET_MEMORY(buffer, n)
  114. #define GET_IO_MEMORY(buffer, n) GET_MEMORY(buffer, n)
  115. #define MEM_BUFFER_TYPE u4_t
  116. #endif
  117. typedef struct breakpoint {
  118. u13_t addr;
  119. struct breakpoint* next;
  120. } breakpoint_t;
  121. /* Pins (TODO: add other pins) */
  122. typedef enum {
  123. PIN_K00 = 0x0,
  124. PIN_K01 = 0x1,
  125. PIN_K02 = 0x2,
  126. PIN_K03 = 0x3,
  127. PIN_K10 = 0X4,
  128. PIN_K11 = 0X5,
  129. PIN_K12 = 0X6,
  130. PIN_K13 = 0X7,
  131. } pin_t;
  132. typedef enum {
  133. PIN_STATE_LOW = 0,
  134. PIN_STATE_HIGH = 1,
  135. } pin_state_t;
  136. typedef enum {
  137. INT_PROG_TIMER_SLOT = 0,
  138. INT_SERIAL_SLOT = 1,
  139. INT_K10_K13_SLOT = 2,
  140. INT_K00_K03_SLOT = 3,
  141. INT_STOPWATCH_SLOT = 4,
  142. INT_CLOCK_TIMER_SLOT = 5,
  143. INT_SLOT_NUM,
  144. } int_slot_t;
  145. typedef struct {
  146. u4_t factor_flag_reg;
  147. u4_t mask_reg;
  148. bool_t triggered; /* 1 if triggered, 0 otherwise */
  149. u8_t vector;
  150. } interrupt_t;
  151. typedef struct {
  152. u13_t* pc;
  153. u12_t* x;
  154. u12_t* y;
  155. u4_t* a;
  156. u4_t* b;
  157. u5_t* np;
  158. u8_t* sp;
  159. u4_t* flags;
  160. u32_t* tick_counter;
  161. u32_t* clk_timer_timestamp;
  162. u32_t* prog_timer_timestamp;
  163. bool_t* prog_timer_enabled;
  164. u8_t* prog_timer_data;
  165. u8_t* prog_timer_rld;
  166. u32_t* call_depth;
  167. interrupt_t* interrupts;
  168. MEM_BUFFER_TYPE* memory;
  169. } state_t;
  170. void cpu_add_bp(breakpoint_t** list, u13_t addr);
  171. void cpu_free_bp(breakpoint_t** list);
  172. void cpu_set_speed(u8_t speed);
  173. state_t* cpu_get_state(void);
  174. u32_t cpu_get_depth(void);
  175. void cpu_set_input_pin(pin_t pin, pin_state_t state);
  176. void cpu_sync_ref_timestamp(void);
  177. void cpu_refresh_hw(void);
  178. void cpu_reset(void);
  179. bool_t cpu_init(const u12_t* program, breakpoint_t* breakpoints, u32_t freq);
  180. void cpu_release(void);
  181. int cpu_step(void);
  182. #endif /* _CPU_H_ */