cpu.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033
  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. #include "cpu.h"
  21. #include "hw.h"
  22. #include "hal.h"
  23. #define TICK_FREQUENCY 32768 // Hz
  24. #define TIMER_1HZ_PERIOD 32768 // in ticks
  25. #define TIMER_256HZ_PERIOD 128 // in ticks
  26. #define MASK_4B 0xF00
  27. #define MASK_6B 0xFC0
  28. #define MASK_7B 0xFE0
  29. #define MASK_8B 0xFF0
  30. #define MASK_10B 0xFFC
  31. #define MASK_12B 0xFFF
  32. #define PCS (pc & 0xFF)
  33. #define PCSL (pc & 0xF)
  34. #define PCSH ((pc >> 4) & 0xF)
  35. #define PCP ((pc >> 8) & 0xF)
  36. #define PCB ((pc >> 12) & 0x1)
  37. #define TO_PC(bank, page, step) ((step & 0xFF) | ((page & 0xF) << 8) | (bank & 0x1) << 12)
  38. #define NBP ((np >> 4) & 0x1)
  39. #define NPP (np & 0xF)
  40. #define TO_NP(bank, page) ((page & 0xF) | (bank & 0x1) << 4)
  41. #define XHL (x & 0xFF)
  42. #define XL (x & 0xF)
  43. #define XH ((x >> 4) & 0xF)
  44. #define XP ((x >> 8) & 0xF)
  45. #define YHL (y & 0xFF)
  46. #define YL (y & 0xF)
  47. #define YH ((y >> 4) & 0xF)
  48. #define YP ((y >> 8) & 0xF)
  49. #define M(n) get_memory(n)
  50. #define SET_M(n, v) set_memory(n, v)
  51. #define RQ(i) get_rq(i)
  52. #define SET_RQ(i, v) set_rq(i, v)
  53. #define SPL (sp & 0xF)
  54. #define SPH ((sp >> 4) & 0xF)
  55. #define FLAG_C (0x1 << 0)
  56. #define FLAG_Z (0x1 << 1)
  57. #define FLAG_D (0x1 << 2)
  58. #define FLAG_I (0x1 << 3)
  59. #define C !!(flags & FLAG_C)
  60. #define Z !!(flags & FLAG_Z)
  61. #define D !!(flags & FLAG_D)
  62. #define I !!(flags & FLAG_I)
  63. #define SET_C() \
  64. { flags |= FLAG_C; }
  65. #define CLEAR_C() \
  66. { flags &= ~FLAG_C; }
  67. #define SET_Z() \
  68. { flags |= FLAG_Z; }
  69. #define CLEAR_Z() \
  70. { flags &= ~FLAG_Z; }
  71. #define SET_D() \
  72. { flags |= FLAG_D; }
  73. #define CLEAR_D() \
  74. { flags &= ~FLAG_D; }
  75. #define SET_I() \
  76. { flags |= FLAG_I; }
  77. #define CLEAR_I() \
  78. { flags &= ~FLAG_I; }
  79. #define REG_CLK_INT_FACTOR_FLAGS 0xF00
  80. #define REG_SW_INT_FACTOR_FLAGS 0xF01
  81. #define REG_PROG_INT_FACTOR_FLAGS 0xF02
  82. #define REG_SERIAL_INT_FACTOR_FLAGS 0xF03
  83. #define REG_K00_K03_INT_FACTOR_FLAGS 0xF04
  84. #define REG_K10_K13_INT_FACTOR_FLAGS 0xF05
  85. #define REG_CLOCK_INT_MASKS 0xF10
  86. #define REG_SW_INT_MASKS 0xF11
  87. #define REG_PROG_INT_MASKS 0xF12
  88. #define REG_SERIAL_INT_MASKS 0xF13
  89. #define REG_K00_K03_INT_MASKS 0xF14
  90. #define REG_K10_K13_INT_MASKS 0xF15
  91. #define REG_PROG_TIMER_DATA_L 0xF24
  92. #define REG_PROG_TIMER_DATA_H 0xF25
  93. #define REG_PROG_TIMER_RELOAD_DATA_L 0xF26
  94. #define REG_PROG_TIMER_RELOAD_DATA_H 0xF27
  95. #define REG_K00_K03_INPUT_PORT 0xF40
  96. #define REG_K10_K13_INPUT_PORT 0xF42
  97. #define REG_K40_K43_BZ_OUTPUT_PORT 0xF54
  98. #define REG_CPU_OSC3_CTRL 0xF70
  99. #define REG_LCD_CTRL 0xF71
  100. #define REG_LCD_CONTRAST 0xF72
  101. #define REG_SVD_CTRL 0xF73
  102. #define REG_BUZZER_CTRL1 0xF74
  103. #define REG_BUZZER_CTRL2 0xF75
  104. #define REG_CLK_WD_TIMER_CTRL 0xF76
  105. #define REG_SW_TIMER_CTRL 0xF77
  106. #define REG_PROG_TIMER_CTRL 0xF78
  107. #define REG_PROG_TIMER_CLK_SEL 0xF79
  108. #define INPUT_PORT_NUM 2
  109. typedef struct {
  110. char* log;
  111. u12_t code;
  112. u12_t mask;
  113. u12_t shift_arg0;
  114. u12_t mask_arg0; // != 0 only if there are two arguments
  115. u8_t cycles;
  116. void (*cb)(u8_t arg0, u8_t arg1);
  117. } op_t;
  118. typedef struct {
  119. u4_t states;
  120. } input_port_t;
  121. /* Registers */
  122. static u13_t pc, next_pc;
  123. static u12_t x, y;
  124. static u4_t a, b;
  125. static u5_t np;
  126. static u8_t sp;
  127. /* Flags */
  128. static u4_t flags;
  129. static const u12_t* g_program = NULL;
  130. static MEM_BUFFER_TYPE memory[MEM_BUFFER_SIZE];
  131. static input_port_t inputs[INPUT_PORT_NUM] = {{0}};
  132. /* Interrupts (in priority order) */
  133. static interrupt_t interrupts[INT_SLOT_NUM] = {
  134. {0x0, 0x0, 0, 0x0C}, // Prog timer
  135. {0x0, 0x0, 0, 0x0A}, // Serial interface
  136. {0x0, 0x0, 0, 0x08}, // Input (K10-K13)
  137. {0x0, 0x0, 0, 0x06}, // Input (K00-K03)
  138. {0x0, 0x0, 0, 0x04}, // Stopwatch timer
  139. {0x0, 0x0, 0, 0x02}, // Clock timer
  140. };
  141. static breakpoint_t* g_breakpoints = NULL;
  142. static u32_t call_depth = 0;
  143. static u32_t clk_timer_timestamp = 0; // in ticks
  144. static u32_t prog_timer_timestamp = 0; // in ticks
  145. static bool_t prog_timer_enabled = 0;
  146. static u8_t prog_timer_data = 0;
  147. static u8_t prog_timer_rld = 0;
  148. static u32_t tick_counter = 0;
  149. static u32_t ts_freq;
  150. static u8_t speed_ratio = 1;
  151. static timestamp_t ref_ts;
  152. static state_t cpu_state = {
  153. .pc = &pc,
  154. .x = &x,
  155. .y = &y,
  156. .a = &a,
  157. .b = &b,
  158. .np = &np,
  159. .sp = &sp,
  160. .flags = &flags,
  161. .tick_counter = &tick_counter,
  162. .clk_timer_timestamp = &clk_timer_timestamp,
  163. .prog_timer_timestamp = &prog_timer_timestamp,
  164. .prog_timer_enabled = &prog_timer_enabled,
  165. .prog_timer_data = &prog_timer_data,
  166. .prog_timer_rld = &prog_timer_rld,
  167. .call_depth = &call_depth,
  168. .interrupts = interrupts,
  169. .memory = memory,
  170. };
  171. void cpu_add_bp(breakpoint_t** list, u13_t addr) {
  172. breakpoint_t* bp;
  173. bp = (breakpoint_t*)g_hal->malloc(sizeof(breakpoint_t));
  174. if(!bp) {
  175. g_hal->log(LOG_ERROR, "Cannot allocate memory for breakpoint 0x%04X!\n", addr);
  176. return;
  177. }
  178. bp->addr = addr;
  179. if(*list != NULL) {
  180. bp->next = *list;
  181. } else {
  182. /* List is empty */
  183. bp->next = NULL;
  184. }
  185. *list = bp;
  186. }
  187. void cpu_free_bp(breakpoint_t** list) {
  188. breakpoint_t *bp = *list, *tmp;
  189. while(bp != NULL) {
  190. tmp = bp->next;
  191. g_hal->free(bp);
  192. bp = tmp;
  193. }
  194. *list = NULL;
  195. }
  196. void cpu_set_speed(u8_t speed) {
  197. speed_ratio = speed;
  198. }
  199. state_t* cpu_get_state(void) {
  200. return &cpu_state;
  201. }
  202. u32_t cpu_get_depth(void) {
  203. return call_depth;
  204. }
  205. static void generate_interrupt(int_slot_t slot, u8_t bit) {
  206. /* Set the factor flag no matter what */
  207. interrupts[slot].factor_flag_reg = interrupts[slot].factor_flag_reg | (0x1 << bit);
  208. /* Trigger the INT only if not masked */
  209. if(interrupts[slot].mask_reg & (0x1 << bit)) {
  210. interrupts[slot].triggered = 1;
  211. }
  212. }
  213. void cpu_set_input_pin(pin_t pin, pin_state_t state) {
  214. /* Set the I/O */
  215. inputs[pin & 0x4].states = (inputs[pin & 0x4].states & ~(0x1 << (pin & 0x3))) |
  216. (state << (pin & 0x3));
  217. /* Trigger the interrupt (TODO: handle relation register) */
  218. if(state == PIN_STATE_LOW) {
  219. switch((pin & 0x4) >> 2) {
  220. case 0:
  221. generate_interrupt(INT_K00_K03_SLOT, pin & 0x3);
  222. break;
  223. case 1:
  224. generate_interrupt(INT_K10_K13_SLOT, pin & 0x3);
  225. break;
  226. }
  227. }
  228. }
  229. void cpu_sync_ref_timestamp(void) {
  230. ref_ts = g_hal->get_timestamp();
  231. }
  232. static u4_t get_io(u12_t n) {
  233. u4_t tmp;
  234. switch(n) {
  235. case REG_CLK_INT_FACTOR_FLAGS:
  236. /* Interrupt factor flags (clock timer) */
  237. tmp = interrupts[INT_CLOCK_TIMER_SLOT].factor_flag_reg;
  238. interrupts[INT_CLOCK_TIMER_SLOT].factor_flag_reg = 0;
  239. return tmp;
  240. case REG_SW_INT_FACTOR_FLAGS:
  241. /* Interrupt factor flags (stopwatch) */
  242. tmp = interrupts[INT_STOPWATCH_SLOT].factor_flag_reg;
  243. interrupts[INT_STOPWATCH_SLOT].factor_flag_reg = 0;
  244. return tmp;
  245. case REG_PROG_INT_FACTOR_FLAGS:
  246. /* Interrupt factor flags (prog timer) */
  247. tmp = interrupts[INT_PROG_TIMER_SLOT].factor_flag_reg;
  248. interrupts[INT_PROG_TIMER_SLOT].factor_flag_reg = 0;
  249. return tmp;
  250. case REG_SERIAL_INT_FACTOR_FLAGS:
  251. /* Interrupt factor flags (serial) */
  252. tmp = interrupts[INT_SERIAL_SLOT].factor_flag_reg;
  253. interrupts[INT_SERIAL_SLOT].factor_flag_reg = 0;
  254. return tmp;
  255. case REG_K00_K03_INT_FACTOR_FLAGS:
  256. /* Interrupt factor flags (K00-K03) */
  257. tmp = interrupts[INT_K00_K03_SLOT].factor_flag_reg;
  258. interrupts[INT_K00_K03_SLOT].factor_flag_reg = 0;
  259. return tmp;
  260. case REG_K10_K13_INT_FACTOR_FLAGS:
  261. /* Interrupt factor flags (K10-K13) */
  262. tmp = interrupts[INT_K10_K13_SLOT].factor_flag_reg;
  263. interrupts[INT_K10_K13_SLOT].factor_flag_reg = 0;
  264. return tmp;
  265. case REG_CLOCK_INT_MASKS:
  266. /* Clock timer interrupt masks */
  267. return interrupts[INT_CLOCK_TIMER_SLOT].mask_reg;
  268. case REG_SW_INT_MASKS:
  269. /* Stopwatch interrupt masks */
  270. return interrupts[INT_STOPWATCH_SLOT].mask_reg & 0x3;
  271. case REG_PROG_INT_MASKS:
  272. /* Prog timer interrupt masks */
  273. return interrupts[INT_PROG_TIMER_SLOT].mask_reg & 0x1;
  274. case REG_SERIAL_INT_MASKS:
  275. /* Serial interface interrupt masks */
  276. return interrupts[INT_SERIAL_SLOT].mask_reg & 0x1;
  277. case REG_K00_K03_INT_MASKS:
  278. /* Input (K00-K03) interrupt masks */
  279. return interrupts[INT_K00_K03_SLOT].mask_reg;
  280. case REG_K10_K13_INT_MASKS:
  281. /* Input (K10-K13) interrupt masks */
  282. return interrupts[INT_K10_K13_SLOT].mask_reg;
  283. case REG_PROG_TIMER_DATA_L:
  284. /* Prog timer data (low) */
  285. return prog_timer_data & 0xF;
  286. case REG_PROG_TIMER_DATA_H:
  287. /* Prog timer data (high) */
  288. return (prog_timer_data >> 4) & 0xF;
  289. case REG_PROG_TIMER_RELOAD_DATA_L:
  290. /* Prog timer reload data (low) */
  291. return prog_timer_rld & 0xF;
  292. case REG_PROG_TIMER_RELOAD_DATA_H:
  293. /* Prog timer reload data (high) */
  294. return (prog_timer_rld >> 4) & 0xF;
  295. case REG_K00_K03_INPUT_PORT:
  296. /* Input port (K00-K03) */
  297. return inputs[0].states;
  298. case REG_K10_K13_INPUT_PORT:
  299. /* Input port (K10-K13) */
  300. return inputs[1].states;
  301. case REG_K40_K43_BZ_OUTPUT_PORT:
  302. /* Output port (R40-R43) */
  303. return GET_IO_MEMORY(memory, n);
  304. case REG_CPU_OSC3_CTRL:
  305. /* CPU/OSC3 clocks switch, CPU voltage switch */
  306. return GET_IO_MEMORY(memory, n);
  307. case REG_LCD_CTRL:
  308. /* LCD control */
  309. return GET_IO_MEMORY(memory, n);
  310. case REG_LCD_CONTRAST:
  311. /* LCD contrast */
  312. break;
  313. case REG_SVD_CTRL:
  314. /* SVD */
  315. return GET_IO_MEMORY(memory, n) & 0x7; // Voltage always OK
  316. case REG_BUZZER_CTRL1:
  317. /* Buzzer config 1 */
  318. return GET_IO_MEMORY(memory, n);
  319. case REG_BUZZER_CTRL2:
  320. /* Buzzer config 2 */
  321. return GET_IO_MEMORY(memory, n) & 0x3; // Buzzer ready
  322. case REG_CLK_WD_TIMER_CTRL:
  323. /* Clock/Watchdog timer reset */
  324. break;
  325. case REG_SW_TIMER_CTRL:
  326. /* Stopwatch stop/run/reset */
  327. break;
  328. case REG_PROG_TIMER_CTRL:
  329. /* Prog timer stop/run/reset */
  330. return !!prog_timer_enabled;
  331. case REG_PROG_TIMER_CLK_SEL:
  332. /* Prog timer clock selection */
  333. break;
  334. default:
  335. g_hal->log(LOG_ERROR, "Read from unimplemented I/O 0x%03X - PC = 0x%04X\n", n, pc);
  336. }
  337. return 0;
  338. }
  339. static void set_io(u12_t n, u4_t v) {
  340. switch(n) {
  341. case REG_CLOCK_INT_MASKS:
  342. /* Clock timer interrupt masks */
  343. /* Assume 1Hz timer INT enabled (0x8) */
  344. interrupts[INT_CLOCK_TIMER_SLOT].mask_reg = v;
  345. break;
  346. case REG_SW_INT_MASKS:
  347. /* Stopwatch interrupt masks */
  348. /* Assume all INT disabled */
  349. interrupts[INT_STOPWATCH_SLOT].mask_reg = v;
  350. break;
  351. case REG_PROG_INT_MASKS:
  352. /* Prog timer interrupt masks */
  353. /* Assume Prog timer INT enabled (0x1) */
  354. interrupts[INT_PROG_TIMER_SLOT].mask_reg = v;
  355. break;
  356. case REG_SERIAL_INT_MASKS:
  357. /* Serial interface interrupt masks */
  358. /* Assume all INT disabled */
  359. interrupts[INT_K10_K13_SLOT].mask_reg = v;
  360. break;
  361. case REG_K00_K03_INT_MASKS:
  362. /* Input (K00-K03) interrupt masks */
  363. /* Assume all INT disabled */
  364. interrupts[INT_SERIAL_SLOT].mask_reg = v;
  365. break;
  366. case REG_K10_K13_INT_MASKS:
  367. /* Input (K10-K13) interrupt masks */
  368. /* Assume all INT disabled */
  369. interrupts[INT_K10_K13_SLOT].mask_reg = v;
  370. break;
  371. case REG_PROG_TIMER_RELOAD_DATA_L:
  372. /* Prog timer reload data (low) */
  373. prog_timer_rld = v | (prog_timer_rld & 0xF0);
  374. break;
  375. case REG_PROG_TIMER_RELOAD_DATA_H:
  376. /* Prog timer reload data (high) */
  377. prog_timer_rld = (prog_timer_rld & 0xF) | (v << 4);
  378. break;
  379. case REG_K00_K03_INPUT_PORT:
  380. /* Input port (K00-K03) */
  381. /* Write not allowed */
  382. break;
  383. case REG_K40_K43_BZ_OUTPUT_PORT:
  384. /* Output port (R40-R43) */
  385. //g_hal->log(LOG_INFO, "Output/Buzzer: 0x%X\n", v);
  386. hw_enable_buzzer(!(v & 0x8));
  387. break;
  388. case REG_CPU_OSC3_CTRL:
  389. /* CPU/OSC3 clocks switch, CPU voltage switch */
  390. /* Assume 32,768 OSC1 selected, OSC3 off, battery >= 3,1V (0x1) */
  391. break;
  392. case REG_LCD_CTRL:
  393. /* LCD control */
  394. break;
  395. case REG_LCD_CONTRAST:
  396. /* LCD contrast */
  397. /* Assume medium contrast (0x8) */
  398. break;
  399. case REG_SVD_CTRL:
  400. /* SVD */
  401. /* Assume battery voltage always OK (0x6) */
  402. break;
  403. case REG_BUZZER_CTRL1:
  404. /* Buzzer config 1 */
  405. hw_set_buzzer_freq(v & 0x7);
  406. break;
  407. case REG_BUZZER_CTRL2:
  408. /* Buzzer config 2 */
  409. break;
  410. case REG_CLK_WD_TIMER_CTRL:
  411. /* Clock/Watchdog timer reset */
  412. /* Ignore watchdog */
  413. break;
  414. case REG_SW_TIMER_CTRL:
  415. /* Stopwatch stop/run/reset */
  416. break;
  417. case REG_PROG_TIMER_CTRL:
  418. /* Prog timer stop/run/reset */
  419. if(v & 0x2) {
  420. prog_timer_data = prog_timer_rld;
  421. }
  422. if((v & 0x1) && !prog_timer_enabled) {
  423. prog_timer_timestamp = tick_counter;
  424. }
  425. prog_timer_enabled = v & 0x1;
  426. break;
  427. case REG_PROG_TIMER_CLK_SEL:
  428. /* Prog timer clock selection */
  429. /* Assume 256Hz, output disabled */
  430. break;
  431. default:
  432. g_hal->log(LOG_ERROR, "Write 0x%X to unimplemented I/O 0x%03X - PC = 0x%04X\n", v, n, pc);
  433. }
  434. }
  435. static void set_lcd(u12_t n, u4_t v) {
  436. u8_t i;
  437. u8_t seg, com0;
  438. seg = ((n & 0x7F) >> 1);
  439. com0 = (((n & 0x80) >> 7) * 8 + (n & 0x1) * 4);
  440. for(i = 0; i < 4; i++) {
  441. hw_set_lcd_pin(seg, com0 + i, (v >> i) & 0x1);
  442. }
  443. }
  444. static u4_t get_memory(u12_t n) {
  445. u4_t res = 0;
  446. if(n < MEM_RAM_SIZE) {
  447. /* RAM */
  448. g_hal->log(LOG_MEMORY, "RAM - ");
  449. res = GET_RAM_MEMORY(memory, n);
  450. } else if(n >= MEM_DISPLAY1_ADDR && n < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) {
  451. /* Display Memory 1 */
  452. g_hal->log(LOG_MEMORY, "Display Memory 1 - ");
  453. res = GET_DISP1_MEMORY(memory, n);
  454. } else if(n >= MEM_DISPLAY2_ADDR && n < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) {
  455. /* Display Memory 2 */
  456. g_hal->log(LOG_MEMORY, "Display Memory 2 - ");
  457. res = GET_DISP2_MEMORY(memory, n);
  458. } else if(n >= MEM_IO_ADDR && n < (MEM_IO_ADDR + MEM_IO_SIZE)) {
  459. /* I/O Memory */
  460. g_hal->log(LOG_MEMORY, "I/O - ");
  461. res = get_io(n);
  462. } else {
  463. g_hal->log(LOG_ERROR, "Read from invalid memory address 0x%03X - PC = 0x%04X\n", n, pc);
  464. return 0;
  465. }
  466. g_hal->log(LOG_MEMORY, "Read 0x%X - Address 0x%03X - PC = 0x%04X\n", res, n, pc);
  467. return res;
  468. }
  469. static void set_memory(u12_t n, u4_t v) {
  470. /* Cache any data written to a valid address, and process it */
  471. if(n < MEM_RAM_SIZE) {
  472. /* RAM */
  473. SET_RAM_MEMORY(memory, n, v);
  474. g_hal->log(LOG_MEMORY, "RAM - ");
  475. } else if(n >= MEM_DISPLAY1_ADDR && n < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) {
  476. /* Display Memory 1 */
  477. SET_DISP1_MEMORY(memory, n, v);
  478. set_lcd(n, v);
  479. g_hal->log(LOG_MEMORY, "Display Memory 1 - ");
  480. } else if(n >= MEM_DISPLAY2_ADDR && n < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) {
  481. /* Display Memory 2 */
  482. SET_DISP2_MEMORY(memory, n, v);
  483. set_lcd(n, v);
  484. g_hal->log(LOG_MEMORY, "Display Memory 2 - ");
  485. } else if(n >= MEM_IO_ADDR && n < (MEM_IO_ADDR + MEM_IO_SIZE)) {
  486. /* I/O Memory */
  487. SET_IO_MEMORY(memory, n, v);
  488. set_io(n, v);
  489. g_hal->log(LOG_MEMORY, "I/O - ");
  490. } else {
  491. g_hal->log(
  492. LOG_ERROR, "Write 0x%X to invalid memory address 0x%03X - PC = 0x%04X\n", v, n, pc);
  493. return;
  494. }
  495. g_hal->log(LOG_MEMORY, "Write 0x%X - Address 0x%03X - PC = 0x%04X\n", v, n, pc);
  496. }
  497. void cpu_refresh_hw(void) {
  498. static const struct range {
  499. u12_t addr;
  500. u12_t size;
  501. } refresh_locs[] = {
  502. {MEM_DISPLAY1_ADDR, MEM_DISPLAY1_SIZE}, /* Display Memory 1 */
  503. {MEM_DISPLAY2_ADDR, MEM_DISPLAY2_SIZE}, /* Display Memory 2 */
  504. {REG_BUZZER_CTRL1, 1}, /* Buzzer frequency */
  505. {REG_K40_K43_BZ_OUTPUT_PORT, 1}, /* Buzzer enabled */
  506. {0, 0}, // end of list
  507. };
  508. for(int i = 0; refresh_locs[i].size != 0; i++) {
  509. for(u12_t n = refresh_locs[i].addr; n < (refresh_locs[i].addr + refresh_locs[i].size);
  510. n++) {
  511. set_memory(n, GET_MEMORY(memory, n));
  512. }
  513. }
  514. }
  515. static u4_t get_rq(u12_t rq) {
  516. switch(rq & 0x3) {
  517. case 0x0:
  518. return a;
  519. case 0x1:
  520. return b;
  521. case 0x2:
  522. return M(x);
  523. case 0x3:
  524. return M(y);
  525. }
  526. return 0;
  527. }
  528. static void set_rq(u12_t rq, u4_t v) {
  529. switch(rq & 0x3) {
  530. case 0x0:
  531. a = v;
  532. break;
  533. case 0x1:
  534. b = v;
  535. break;
  536. case 0x2:
  537. SET_M(x, v);
  538. break;
  539. case 0x3:
  540. SET_M(y, v);
  541. break;
  542. }
  543. }
  544. /* Instructions */
  545. static void op_pset_cb(u8_t arg0, u8_t arg1) {
  546. UNUSED(arg1);
  547. np = arg0;
  548. }
  549. static void op_jp_cb(u8_t arg0, u8_t arg1) {
  550. UNUSED(arg1);
  551. next_pc = arg0 | (np << 8);
  552. }
  553. static void op_jp_c_cb(u8_t arg0, u8_t arg1) {
  554. UNUSED(arg1);
  555. if(flags & FLAG_C) {
  556. next_pc = arg0 | (np << 8);
  557. }
  558. }
  559. static void op_jp_nc_cb(u8_t arg0, u8_t arg1) {
  560. UNUSED(arg1);
  561. if(!(flags & FLAG_C)) {
  562. next_pc = arg0 | (np << 8);
  563. }
  564. }
  565. static void op_jp_z_cb(u8_t arg0, u8_t arg1) {
  566. UNUSED(arg1);
  567. if(flags & FLAG_Z) {
  568. next_pc = arg0 | (np << 8);
  569. }
  570. }
  571. static void op_jp_nz_cb(u8_t arg0, u8_t arg1) {
  572. UNUSED(arg1);
  573. if(!(flags & FLAG_Z)) {
  574. next_pc = arg0 | (np << 8);
  575. }
  576. }
  577. static void op_jpba_cb(u8_t arg0, u8_t arg1) {
  578. UNUSED(arg0);
  579. UNUSED(arg1);
  580. next_pc = a | (b << 4) | (np << 8);
  581. }
  582. static void op_call_cb(u8_t arg0, u8_t arg1) {
  583. UNUSED(arg1);
  584. pc = (pc + 1) & 0x1FFF; // This does not actually change the PC register
  585. SET_M(sp - 1, PCP);
  586. SET_M(sp - 2, PCSH);
  587. SET_M(sp - 3, PCSL);
  588. sp = (sp - 3) & 0xFF;
  589. next_pc = TO_PC(PCB, NPP, arg0);
  590. call_depth++;
  591. }
  592. static void op_calz_cb(u8_t arg0, u8_t arg1) {
  593. UNUSED(arg1);
  594. pc = (pc + 1) & 0x1FFF; // This does not actually change the PC register
  595. SET_M(sp - 1, PCP);
  596. SET_M(sp - 2, PCSH);
  597. SET_M(sp - 3, PCSL);
  598. sp = (sp - 3) & 0xFF;
  599. next_pc = TO_PC(PCB, 0, arg0);
  600. call_depth++;
  601. }
  602. static void op_ret_cb(u8_t arg0, u8_t arg1) {
  603. UNUSED(arg0);
  604. UNUSED(arg1);
  605. next_pc = M(sp) | (M(sp + 1) << 4) | (M(sp + 2) << 8) | (PCB << 12);
  606. sp = (sp + 3) & 0xFF;
  607. call_depth--;
  608. }
  609. static void op_rets_cb(u8_t arg0, u8_t arg1) {
  610. UNUSED(arg0);
  611. UNUSED(arg1);
  612. next_pc = M(sp) | (M(sp + 1) << 4) | (M(sp + 2) << 8) | (PCB << 12);
  613. sp = (sp + 3) & 0xFF;
  614. next_pc = (pc + 1) & 0x1FFF;
  615. call_depth--;
  616. }
  617. static void op_retd_cb(u8_t arg0, u8_t arg1) {
  618. UNUSED(arg1);
  619. next_pc = M(sp) | (M(sp + 1) << 4) | (M(sp + 2) << 8) | (PCB << 12);
  620. sp = (sp + 3) & 0xFF;
  621. SET_M(x, arg0 & 0xF);
  622. SET_M(x + 1, (arg0 >> 4) & 0xF);
  623. x = ((x + 2) & 0xFF) | (XP << 8);
  624. call_depth--;
  625. }
  626. static void op_nop5_cb(u8_t arg0, u8_t arg1) {
  627. UNUSED(arg0);
  628. UNUSED(arg1);
  629. }
  630. static void op_nop7_cb(u8_t arg0, u8_t arg1) {
  631. UNUSED(arg0);
  632. UNUSED(arg1);
  633. }
  634. static void op_halt_cb(u8_t arg0, u8_t arg1) {
  635. UNUSED(arg0);
  636. UNUSED(arg1);
  637. g_hal->halt();
  638. }
  639. static void op_inc_x_cb(u8_t arg0, u8_t arg1) {
  640. UNUSED(arg0);
  641. UNUSED(arg1);
  642. x = ((x + 1) & 0xFF) | (XP << 8);
  643. }
  644. static void op_inc_y_cb(u8_t arg0, u8_t arg1) {
  645. UNUSED(arg0);
  646. UNUSED(arg1);
  647. y = ((y + 1) & 0xFF) | (YP << 8);
  648. }
  649. static void op_ld_x_cb(u8_t arg0, u8_t arg1) {
  650. UNUSED(arg1);
  651. x = arg0 | (XP << 8);
  652. }
  653. static void op_ld_y_cb(u8_t arg0, u8_t arg1) {
  654. UNUSED(arg1);
  655. y = arg0 | (YP << 8);
  656. }
  657. static void op_ld_xp_r_cb(u8_t arg0, u8_t arg1) {
  658. UNUSED(arg1);
  659. x = XHL | (RQ(arg0) << 8);
  660. }
  661. static void op_ld_xh_r_cb(u8_t arg0, u8_t arg1) {
  662. UNUSED(arg1);
  663. x = XL | (RQ(arg0) << 4) | (XP << 8);
  664. }
  665. static void op_ld_xl_r_cb(u8_t arg0, u8_t arg1) {
  666. UNUSED(arg1);
  667. x = RQ(arg0) | (XH << 4) | (XP << 8);
  668. }
  669. static void op_ld_yp_r_cb(u8_t arg0, u8_t arg1) {
  670. UNUSED(arg1);
  671. y = YHL | (RQ(arg0) << 8);
  672. }
  673. static void op_ld_yh_r_cb(u8_t arg0, u8_t arg1) {
  674. UNUSED(arg1);
  675. y = YL | (RQ(arg0) << 4) | (YP << 8);
  676. }
  677. static void op_ld_yl_r_cb(u8_t arg0, u8_t arg1) {
  678. UNUSED(arg1);
  679. y = RQ(arg0) | (YH << 4) | (YP << 8);
  680. }
  681. static void op_ld_r_xp_cb(u8_t arg0, u8_t arg1) {
  682. UNUSED(arg1);
  683. SET_RQ(arg0, XP);
  684. }
  685. static void op_ld_r_xh_cb(u8_t arg0, u8_t arg1) {
  686. UNUSED(arg1);
  687. SET_RQ(arg0, XH);
  688. }
  689. static void op_ld_r_xl_cb(u8_t arg0, u8_t arg1) {
  690. UNUSED(arg1);
  691. SET_RQ(arg0, XL);
  692. }
  693. static void op_ld_r_yp_cb(u8_t arg0, u8_t arg1) {
  694. UNUSED(arg1);
  695. SET_RQ(arg0, YP);
  696. }
  697. static void op_ld_r_yh_cb(u8_t arg0, u8_t arg1) {
  698. UNUSED(arg1);
  699. SET_RQ(arg0, YH);
  700. }
  701. static void op_ld_r_yl_cb(u8_t arg0, u8_t arg1) {
  702. UNUSED(arg1);
  703. SET_RQ(arg0, YL);
  704. }
  705. static void op_adc_xh_cb(u8_t arg0, u8_t arg1) {
  706. UNUSED(arg1);
  707. u8_t tmp;
  708. tmp = XH + arg0 + C;
  709. x = XL | ((tmp & 0xF) << 4) | (XP << 8);
  710. if(tmp >> 4) {
  711. SET_C();
  712. } else {
  713. CLEAR_C();
  714. }
  715. if(!(tmp & 0xF)) {
  716. SET_Z();
  717. } else {
  718. CLEAR_Z();
  719. }
  720. }
  721. static void op_adc_xl_cb(u8_t arg0, u8_t arg1) {
  722. UNUSED(arg1);
  723. u8_t tmp;
  724. tmp = XL + arg0 + C;
  725. x = (tmp & 0xF) | (XH << 4) | (XP << 8);
  726. if(tmp >> 4) {
  727. SET_C();
  728. } else {
  729. CLEAR_C();
  730. }
  731. if(!(tmp & 0xF)) {
  732. SET_Z();
  733. } else {
  734. CLEAR_Z();
  735. }
  736. }
  737. static void op_adc_yh_cb(u8_t arg0, u8_t arg1) {
  738. UNUSED(arg1);
  739. u8_t tmp;
  740. tmp = YH + arg0 + C;
  741. y = YL | ((tmp & 0xF) << 4) | (YP << 8);
  742. if(tmp >> 4) {
  743. SET_C();
  744. } else {
  745. CLEAR_C();
  746. }
  747. if(!(tmp & 0xF)) {
  748. SET_Z();
  749. } else {
  750. CLEAR_Z();
  751. }
  752. }
  753. static void op_adc_yl_cb(u8_t arg0, u8_t arg1) {
  754. UNUSED(arg1);
  755. u8_t tmp;
  756. tmp = YL + arg0 + C;
  757. y = (tmp & 0xF) | (YH << 4) | (YP << 8);
  758. if(tmp >> 4) {
  759. SET_C();
  760. } else {
  761. CLEAR_C();
  762. }
  763. if(!(tmp & 0xF)) {
  764. SET_Z();
  765. } else {
  766. CLEAR_Z();
  767. }
  768. }
  769. static void op_cp_xh_cb(u8_t arg0, u8_t arg1) {
  770. UNUSED(arg1);
  771. if(XH < arg0) {
  772. SET_C();
  773. } else {
  774. CLEAR_C();
  775. }
  776. if(XH == arg0) {
  777. SET_Z();
  778. } else {
  779. CLEAR_Z();
  780. }
  781. }
  782. static void op_cp_xl_cb(u8_t arg0, u8_t arg1) {
  783. UNUSED(arg1);
  784. if(XL < arg0) {
  785. SET_C();
  786. } else {
  787. CLEAR_C();
  788. }
  789. if(XL == arg0) {
  790. SET_Z();
  791. } else {
  792. CLEAR_Z();
  793. }
  794. }
  795. static void op_cp_yh_cb(u8_t arg0, u8_t arg1) {
  796. UNUSED(arg1);
  797. if(YH < arg0) {
  798. SET_C();
  799. } else {
  800. CLEAR_C();
  801. }
  802. if(YH == arg0) {
  803. SET_Z();
  804. } else {
  805. CLEAR_Z();
  806. }
  807. }
  808. static void op_cp_yl_cb(u8_t arg0, u8_t arg1) {
  809. UNUSED(arg1);
  810. if(YL < arg0) {
  811. SET_C();
  812. } else {
  813. CLEAR_C();
  814. }
  815. if(YL == arg0) {
  816. SET_Z();
  817. } else {
  818. CLEAR_Z();
  819. }
  820. }
  821. static void op_ld_r_i_cb(u8_t arg0, u8_t arg1) {
  822. SET_RQ(arg0, arg1);
  823. }
  824. static void op_ld_r_q_cb(u8_t arg0, u8_t arg1) {
  825. SET_RQ(arg0, RQ(arg1));
  826. }
  827. static void op_ld_a_mn_cb(u8_t arg0, u8_t arg1) {
  828. UNUSED(arg1);
  829. a = M(arg0);
  830. }
  831. static void op_ld_b_mn_cb(u8_t arg0, u8_t arg1) {
  832. UNUSED(arg1);
  833. b = M(arg0);
  834. }
  835. static void op_ld_mn_a_cb(u8_t arg0, u8_t arg1) {
  836. UNUSED(arg1);
  837. SET_M(arg0, a);
  838. }
  839. static void op_ld_mn_b_cb(u8_t arg0, u8_t arg1) {
  840. UNUSED(arg1);
  841. SET_M(arg0, b);
  842. }
  843. static void op_ldpx_mx_cb(u8_t arg0, u8_t arg1) {
  844. UNUSED(arg1);
  845. SET_M(x, arg0);
  846. x = ((x + 1) & 0xFF) | (XP << 8);
  847. }
  848. static void op_ldpx_r_cb(u8_t arg0, u8_t arg1) {
  849. SET_RQ(arg0, RQ(arg1));
  850. x = ((x + 1) & 0xFF) | (XP << 8);
  851. }
  852. static void op_ldpy_my_cb(u8_t arg0, u8_t arg1) {
  853. UNUSED(arg1);
  854. SET_M(y, arg0);
  855. y = ((y + 1) & 0xFF) | (YP << 8);
  856. }
  857. static void op_ldpy_r_cb(u8_t arg0, u8_t arg1) {
  858. SET_RQ(arg0, RQ(arg1));
  859. y = ((y + 1) & 0xFF) | (YP << 8);
  860. }
  861. static void op_lbpx_cb(u8_t arg0, u8_t arg1) {
  862. UNUSED(arg1);
  863. SET_M(x, arg0 & 0xF);
  864. SET_M(x + 1, (arg0 >> 4) & 0xF);
  865. x = ((x + 2) & 0xFF) | (XP << 8);
  866. }
  867. static void op_set_cb(u8_t arg0, u8_t arg1) {
  868. UNUSED(arg1);
  869. flags |= arg0;
  870. }
  871. static void op_rst_cb(u8_t arg0, u8_t arg1) {
  872. UNUSED(arg1);
  873. flags &= arg0;
  874. }
  875. static void op_scf_cb(u8_t arg0, u8_t arg1) {
  876. UNUSED(arg0);
  877. UNUSED(arg1);
  878. SET_C();
  879. }
  880. static void op_rcf_cb(u8_t arg0, u8_t arg1) {
  881. UNUSED(arg0);
  882. UNUSED(arg1);
  883. CLEAR_C();
  884. }
  885. static void op_szf_cb(u8_t arg0, u8_t arg1) {
  886. UNUSED(arg0);
  887. UNUSED(arg1);
  888. SET_Z();
  889. }
  890. static void op_rzf_cb(u8_t arg0, u8_t arg1) {
  891. UNUSED(arg0);
  892. UNUSED(arg1);
  893. CLEAR_Z();
  894. }
  895. static void op_sdf_cb(u8_t arg0, u8_t arg1) {
  896. UNUSED(arg0);
  897. UNUSED(arg1);
  898. SET_D();
  899. }
  900. static void op_rdf_cb(u8_t arg0, u8_t arg1) {
  901. UNUSED(arg0);
  902. UNUSED(arg1);
  903. CLEAR_D();
  904. }
  905. static void op_ei_cb(u8_t arg0, u8_t arg1) {
  906. UNUSED(arg0);
  907. UNUSED(arg1);
  908. SET_I();
  909. }
  910. static void op_di_cb(u8_t arg0, u8_t arg1) {
  911. UNUSED(arg0);
  912. UNUSED(arg1);
  913. CLEAR_I();
  914. }
  915. static void op_inc_sp_cb(u8_t arg0, u8_t arg1) {
  916. UNUSED(arg0);
  917. UNUSED(arg1);
  918. sp = (sp + 1) & 0xFF;
  919. }
  920. static void op_dec_sp_cb(u8_t arg0, u8_t arg1) {
  921. UNUSED(arg0);
  922. UNUSED(arg1);
  923. sp = (sp - 1) & 0xFF;
  924. }
  925. static void op_push_r_cb(u8_t arg0, u8_t arg1) {
  926. UNUSED(arg0);
  927. UNUSED(arg1);
  928. sp = (sp - 1) & 0xFF;
  929. SET_M(sp, RQ(arg0));
  930. }
  931. static void op_push_xp_cb(u8_t arg0, u8_t arg1) {
  932. UNUSED(arg0);
  933. UNUSED(arg1);
  934. sp = (sp - 1) & 0xFF;
  935. SET_M(sp, XP);
  936. }
  937. static void op_push_xh_cb(u8_t arg0, u8_t arg1) {
  938. UNUSED(arg0);
  939. UNUSED(arg1);
  940. sp = (sp - 1) & 0xFF;
  941. SET_M(sp, XH);
  942. }
  943. static void op_push_xl_cb(u8_t arg0, u8_t arg1) {
  944. UNUSED(arg0);
  945. UNUSED(arg1);
  946. sp = (sp - 1) & 0xFF;
  947. SET_M(sp, XL);
  948. }
  949. static void op_push_yp_cb(u8_t arg0, u8_t arg1) {
  950. UNUSED(arg0);
  951. UNUSED(arg1);
  952. sp = (sp - 1) & 0xFF;
  953. SET_M(sp, YP);
  954. }
  955. static void op_push_yh_cb(u8_t arg0, u8_t arg1) {
  956. UNUSED(arg0);
  957. UNUSED(arg1);
  958. sp = (sp - 1) & 0xFF;
  959. SET_M(sp, YH);
  960. }
  961. static void op_push_yl_cb(u8_t arg0, u8_t arg1) {
  962. UNUSED(arg0);
  963. UNUSED(arg1);
  964. sp = (sp - 1) & 0xFF;
  965. SET_M(sp, YL);
  966. }
  967. static void op_push_f_cb(u8_t arg0, u8_t arg1) {
  968. UNUSED(arg0);
  969. UNUSED(arg1);
  970. sp = (sp - 1) & 0xFF;
  971. SET_M(sp, flags);
  972. }
  973. static void op_pop_r_cb(u8_t arg0, u8_t arg1) {
  974. UNUSED(arg1);
  975. SET_RQ(arg0, M(sp));
  976. sp = (sp + 1) & 0xFF;
  977. }
  978. static void op_pop_xp_cb(u8_t arg0, u8_t arg1) {
  979. UNUSED(arg0);
  980. UNUSED(arg1);
  981. x = XL | (XH << 4) | (M(sp) << 8);
  982. sp = (sp + 1) & 0xFF;
  983. }
  984. static void op_pop_xh_cb(u8_t arg0, u8_t arg1) {
  985. UNUSED(arg0);
  986. UNUSED(arg1);
  987. x = XL | (M(sp) << 4) | (XP << 8);
  988. sp = (sp + 1) & 0xFF;
  989. }
  990. static void op_pop_xl_cb(u8_t arg0, u8_t arg1) {
  991. UNUSED(arg0);
  992. UNUSED(arg1);
  993. x = M(sp) | (XH << 4) | (XP << 8);
  994. sp = (sp + 1) & 0xFF;
  995. }
  996. static void op_pop_yp_cb(u8_t arg0, u8_t arg1) {
  997. UNUSED(arg0);
  998. UNUSED(arg1);
  999. y = YL | (YH << 4) | (M(sp) << 8);
  1000. sp = (sp + 1) & 0xFF;
  1001. }
  1002. static void op_pop_yh_cb(u8_t arg0, u8_t arg1) {
  1003. UNUSED(arg0);
  1004. UNUSED(arg1);
  1005. y = YL | (M(sp) << 4) | (YP << 8);
  1006. sp = (sp + 1) & 0xFF;
  1007. }
  1008. static void op_pop_yl_cb(u8_t arg0, u8_t arg1) {
  1009. UNUSED(arg0);
  1010. UNUSED(arg1);
  1011. y = M(sp) | (YH << 4) | (YP << 8);
  1012. sp = (sp + 1) & 0xFF;
  1013. }
  1014. static void op_pop_f_cb(u8_t arg0, u8_t arg1) {
  1015. UNUSED(arg0);
  1016. UNUSED(arg1);
  1017. flags = M(sp);
  1018. sp = (sp + 1) & 0xFF;
  1019. }
  1020. static void op_ld_sph_r_cb(u8_t arg0, u8_t arg1) {
  1021. UNUSED(arg1);
  1022. sp = SPL | (RQ(arg0) << 4);
  1023. }
  1024. static void op_ld_spl_r_cb(u8_t arg0, u8_t arg1) {
  1025. UNUSED(arg1);
  1026. sp = RQ(arg0) | (SPH << 4);
  1027. }
  1028. static void op_ld_r_sph_cb(u8_t arg0, u8_t arg1) {
  1029. UNUSED(arg1);
  1030. SET_RQ(arg0, SPH);
  1031. }
  1032. static void op_ld_r_spl_cb(u8_t arg0, u8_t arg1) {
  1033. UNUSED(arg1);
  1034. SET_RQ(arg0, SPL);
  1035. }
  1036. static void op_add_r_i_cb(u8_t arg0, u8_t arg1) {
  1037. u8_t tmp;
  1038. tmp = RQ(arg0) + arg1;
  1039. if(D) {
  1040. if(tmp >= 10) {
  1041. SET_RQ(arg0, (tmp - 10) & 0xF);
  1042. SET_C();
  1043. } else {
  1044. SET_RQ(arg0, tmp);
  1045. CLEAR_C();
  1046. }
  1047. } else {
  1048. SET_RQ(arg0, tmp & 0xF);
  1049. if(tmp >> 4) {
  1050. SET_C();
  1051. } else {
  1052. CLEAR_C();
  1053. }
  1054. }
  1055. if(!RQ(arg0)) {
  1056. SET_Z();
  1057. } else {
  1058. CLEAR_Z();
  1059. }
  1060. }
  1061. static void op_add_r_q_cb(u8_t arg0, u8_t arg1) {
  1062. u8_t tmp;
  1063. tmp = RQ(arg0) + RQ(arg1);
  1064. if(D) {
  1065. if(tmp >= 10) {
  1066. SET_RQ(arg0, (tmp - 10) & 0xF);
  1067. SET_C();
  1068. } else {
  1069. SET_RQ(arg0, tmp);
  1070. CLEAR_C();
  1071. }
  1072. } else {
  1073. SET_RQ(arg0, tmp & 0xF);
  1074. if(tmp >> 4) {
  1075. SET_C();
  1076. } else {
  1077. CLEAR_C();
  1078. }
  1079. }
  1080. if(!RQ(arg0)) {
  1081. SET_Z();
  1082. } else {
  1083. CLEAR_Z();
  1084. }
  1085. }
  1086. static void op_adc_r_i_cb(u8_t arg0, u8_t arg1) {
  1087. u8_t tmp;
  1088. tmp = RQ(arg0) + arg1 + C;
  1089. if(D) {
  1090. if(tmp >= 10) {
  1091. SET_RQ(arg0, (tmp - 10) & 0xF);
  1092. SET_C();
  1093. } else {
  1094. SET_RQ(arg0, tmp);
  1095. CLEAR_C();
  1096. }
  1097. } else {
  1098. SET_RQ(arg0, tmp & 0xF);
  1099. if(tmp >> 4) {
  1100. SET_C();
  1101. } else {
  1102. CLEAR_C();
  1103. }
  1104. }
  1105. if(!RQ(arg0)) {
  1106. SET_Z();
  1107. } else {
  1108. CLEAR_Z();
  1109. }
  1110. }
  1111. static void op_adc_r_q_cb(u8_t arg0, u8_t arg1) {
  1112. u8_t tmp;
  1113. tmp = RQ(arg0) + RQ(arg1) + C;
  1114. if(D) {
  1115. if(tmp >= 10) {
  1116. SET_RQ(arg0, (tmp - 10) & 0xF);
  1117. SET_C();
  1118. } else {
  1119. SET_RQ(arg0, tmp);
  1120. CLEAR_C();
  1121. }
  1122. } else {
  1123. SET_RQ(arg0, tmp & 0xF);
  1124. if(tmp >> 4) {
  1125. SET_C();
  1126. } else {
  1127. CLEAR_C();
  1128. }
  1129. }
  1130. if(!RQ(arg0)) {
  1131. SET_Z();
  1132. } else {
  1133. CLEAR_Z();
  1134. }
  1135. }
  1136. static void op_sub_cb(u8_t arg0, u8_t arg1) {
  1137. u8_t tmp;
  1138. tmp = RQ(arg0) - RQ(arg1);
  1139. if(D) {
  1140. if(tmp >> 4) {
  1141. SET_RQ(arg0, (tmp - 6) & 0xF);
  1142. } else {
  1143. SET_RQ(arg0, tmp);
  1144. }
  1145. } else {
  1146. SET_RQ(arg0, tmp & 0xF);
  1147. }
  1148. if(tmp >> 4) {
  1149. SET_C();
  1150. } else {
  1151. CLEAR_C();
  1152. }
  1153. if(!RQ(arg0)) {
  1154. SET_Z();
  1155. } else {
  1156. CLEAR_Z();
  1157. }
  1158. }
  1159. static void op_sbc_r_i_cb(u8_t arg0, u8_t arg1) {
  1160. u8_t tmp;
  1161. tmp = RQ(arg0) - arg1 - C;
  1162. if(D) {
  1163. if(tmp >> 4) {
  1164. SET_RQ(arg0, (tmp - 6) & 0xF);
  1165. } else {
  1166. SET_RQ(arg0, tmp);
  1167. }
  1168. } else {
  1169. SET_RQ(arg0, tmp & 0xF);
  1170. }
  1171. if(tmp >> 4) {
  1172. SET_C();
  1173. } else {
  1174. CLEAR_C();
  1175. }
  1176. if(!RQ(arg0)) {
  1177. SET_Z();
  1178. } else {
  1179. CLEAR_Z();
  1180. }
  1181. }
  1182. static void op_sbc_r_q_cb(u8_t arg0, u8_t arg1) {
  1183. u8_t tmp;
  1184. tmp = RQ(arg0) - RQ(arg1) - C;
  1185. if(D) {
  1186. if(tmp >> 4) {
  1187. SET_RQ(arg0, (tmp - 6) & 0xF);
  1188. } else {
  1189. SET_RQ(arg0, tmp);
  1190. }
  1191. } else {
  1192. SET_RQ(arg0, tmp & 0xF);
  1193. }
  1194. if(tmp >> 4) {
  1195. SET_C();
  1196. } else {
  1197. CLEAR_C();
  1198. }
  1199. if(!RQ(arg0)) {
  1200. SET_Z();
  1201. } else {
  1202. CLEAR_Z();
  1203. }
  1204. }
  1205. static void op_and_r_i_cb(u8_t arg0, u8_t arg1) {
  1206. SET_RQ(arg0, RQ(arg0) & arg1);
  1207. if(!RQ(arg0)) {
  1208. SET_Z();
  1209. } else {
  1210. CLEAR_Z();
  1211. }
  1212. }
  1213. static void op_and_r_q_cb(u8_t arg0, u8_t arg1) {
  1214. SET_RQ(arg0, RQ(arg0) & RQ(arg1));
  1215. if(!RQ(arg0)) {
  1216. SET_Z();
  1217. } else {
  1218. CLEAR_Z();
  1219. }
  1220. }
  1221. static void op_or_r_i_cb(u8_t arg0, u8_t arg1) {
  1222. SET_RQ(arg0, RQ(arg0) | arg1);
  1223. if(!RQ(arg0)) {
  1224. SET_Z();
  1225. } else {
  1226. CLEAR_Z();
  1227. }
  1228. }
  1229. static void op_or_r_q_cb(u8_t arg0, u8_t arg1) {
  1230. SET_RQ(arg0, RQ(arg0) | RQ(arg1));
  1231. if(!RQ(arg0)) {
  1232. SET_Z();
  1233. } else {
  1234. CLEAR_Z();
  1235. }
  1236. }
  1237. static void op_xor_r_i_cb(u8_t arg0, u8_t arg1) {
  1238. SET_RQ(arg0, RQ(arg0) ^ arg1);
  1239. if(!RQ(arg0)) {
  1240. SET_Z();
  1241. } else {
  1242. CLEAR_Z();
  1243. }
  1244. }
  1245. static void op_xor_r_q_cb(u8_t arg0, u8_t arg1) {
  1246. SET_RQ(arg0, RQ(arg0) ^ RQ(arg1));
  1247. if(!RQ(arg0)) {
  1248. SET_Z();
  1249. } else {
  1250. CLEAR_Z();
  1251. }
  1252. }
  1253. static void op_cp_r_i_cb(u8_t arg0, u8_t arg1) {
  1254. if(RQ(arg0) < arg1) {
  1255. SET_C();
  1256. } else {
  1257. CLEAR_C();
  1258. }
  1259. if(RQ(arg0) == arg1) {
  1260. SET_Z();
  1261. } else {
  1262. CLEAR_Z();
  1263. }
  1264. }
  1265. static void op_cp_r_q_cb(u8_t arg0, u8_t arg1) {
  1266. if(RQ(arg0) < RQ(arg1)) {
  1267. SET_C();
  1268. } else {
  1269. CLEAR_C();
  1270. }
  1271. if(RQ(arg0) == RQ(arg1)) {
  1272. SET_Z();
  1273. } else {
  1274. CLEAR_Z();
  1275. }
  1276. }
  1277. static void op_fan_r_i_cb(u8_t arg0, u8_t arg1) {
  1278. if(!(RQ(arg0) & arg1)) {
  1279. SET_Z();
  1280. } else {
  1281. CLEAR_Z();
  1282. }
  1283. }
  1284. static void op_fan_r_q_cb(u8_t arg0, u8_t arg1) {
  1285. if(!(RQ(arg0) & RQ(arg1))) {
  1286. SET_Z();
  1287. } else {
  1288. CLEAR_Z();
  1289. }
  1290. }
  1291. static void op_rlc_cb(u8_t arg0, u8_t arg1) {
  1292. UNUSED(arg1);
  1293. u8_t tmp;
  1294. tmp = (RQ(arg0) << 1) | C;
  1295. if(RQ(arg0) & 0x8) {
  1296. SET_C();
  1297. } else {
  1298. CLEAR_C();
  1299. }
  1300. SET_RQ(arg0, tmp & 0xF);
  1301. /* No need to set Z (issue in DS) */
  1302. }
  1303. static void op_rrc_cb(u8_t arg0, u8_t arg1) {
  1304. UNUSED(arg1);
  1305. u8_t tmp;
  1306. tmp = (RQ(arg0) >> 1) | (C << 3);
  1307. if(RQ(arg0) & 0x1) {
  1308. SET_C();
  1309. } else {
  1310. CLEAR_C();
  1311. }
  1312. SET_RQ(arg0, tmp & 0xF);
  1313. /* No need to set Z (issue in DS) */
  1314. }
  1315. static void op_inc_mn_cb(u8_t arg0, u8_t arg1) {
  1316. UNUSED(arg1);
  1317. u8_t tmp;
  1318. tmp = M(arg0) + 1;
  1319. SET_M(arg0, tmp & 0xF);
  1320. if(tmp >> 4) {
  1321. SET_C();
  1322. } else {
  1323. CLEAR_C();
  1324. }
  1325. if(!M(arg0)) {
  1326. SET_Z();
  1327. } else {
  1328. CLEAR_Z();
  1329. }
  1330. }
  1331. static void op_dec_mn_cb(u8_t arg0, u8_t arg1) {
  1332. UNUSED(arg1);
  1333. u8_t tmp;
  1334. tmp = M(arg0) - 1;
  1335. SET_M(arg0, tmp & 0xF);
  1336. if(tmp >> 4) {
  1337. SET_C();
  1338. } else {
  1339. CLEAR_C();
  1340. }
  1341. if(!M(arg0)) {
  1342. SET_Z();
  1343. } else {
  1344. CLEAR_Z();
  1345. }
  1346. }
  1347. static void op_acpx_cb(u8_t arg0, u8_t arg1) {
  1348. UNUSED(arg1);
  1349. u8_t tmp;
  1350. tmp = M(x) + RQ(arg0) + C;
  1351. if(D) {
  1352. if(tmp >= 10) {
  1353. SET_M(x, (tmp - 10) & 0xF);
  1354. SET_C();
  1355. } else {
  1356. SET_M(x, tmp);
  1357. CLEAR_C();
  1358. }
  1359. } else {
  1360. SET_M(x, tmp & 0xF);
  1361. if(tmp >> 4) {
  1362. SET_C();
  1363. } else {
  1364. CLEAR_C();
  1365. }
  1366. }
  1367. if(!M(x)) {
  1368. SET_Z();
  1369. } else {
  1370. CLEAR_Z();
  1371. }
  1372. x = ((x + 1) & 0xFF) | (XP << 8);
  1373. }
  1374. static void op_acpy_cb(u8_t arg0, u8_t arg1) {
  1375. UNUSED(arg1);
  1376. u8_t tmp;
  1377. tmp = M(y) + RQ(arg0) + C;
  1378. if(D) {
  1379. if(tmp >= 10) {
  1380. SET_M(y, (tmp - 10) & 0xF);
  1381. SET_C();
  1382. } else {
  1383. SET_M(y, tmp);
  1384. CLEAR_C();
  1385. }
  1386. } else {
  1387. SET_M(y, tmp & 0xF);
  1388. if(tmp >> 4) {
  1389. SET_C();
  1390. } else {
  1391. CLEAR_C();
  1392. }
  1393. }
  1394. if(!M(y)) {
  1395. SET_Z();
  1396. } else {
  1397. CLEAR_Z();
  1398. }
  1399. y = ((y + 1) & 0xFF) | (YP << 8);
  1400. }
  1401. static void op_scpx_cb(u8_t arg0, u8_t arg1) {
  1402. UNUSED(arg1);
  1403. u8_t tmp;
  1404. tmp = M(x) - RQ(arg0) - C;
  1405. if(D) {
  1406. if(tmp >> 4) {
  1407. SET_M(x, (tmp - 6) & 0xF);
  1408. } else {
  1409. SET_M(x, tmp);
  1410. }
  1411. } else {
  1412. SET_M(x, tmp & 0xF);
  1413. }
  1414. if(tmp >> 4) {
  1415. SET_C();
  1416. } else {
  1417. CLEAR_C();
  1418. }
  1419. if(!M(x)) {
  1420. SET_Z();
  1421. } else {
  1422. CLEAR_Z();
  1423. }
  1424. x = ((x + 1) & 0xFF) | (XP << 8);
  1425. }
  1426. static void op_scpy_cb(u8_t arg0, u8_t arg1) {
  1427. UNUSED(arg1);
  1428. u8_t tmp;
  1429. tmp = M(y) - RQ(arg0) - C;
  1430. if(D) {
  1431. if(tmp >> 4) {
  1432. SET_M(y, (tmp - 6) & 0xF);
  1433. } else {
  1434. SET_M(y, tmp);
  1435. }
  1436. } else {
  1437. SET_M(y, tmp & 0xF);
  1438. }
  1439. if(tmp >> 4) {
  1440. SET_C();
  1441. } else {
  1442. CLEAR_C();
  1443. }
  1444. if(!M(y)) {
  1445. SET_Z();
  1446. } else {
  1447. CLEAR_Z();
  1448. }
  1449. y = ((y + 1) & 0xFF) | (YP << 8);
  1450. }
  1451. static void op_not_cb(u8_t arg0, u8_t arg1) {
  1452. UNUSED(arg1);
  1453. SET_RQ(arg0, ~RQ(arg0) & 0xF);
  1454. if(!RQ(arg0)) {
  1455. SET_Z();
  1456. } else {
  1457. CLEAR_Z();
  1458. }
  1459. }
  1460. /* The E0C6S46 supported instructions */
  1461. static const op_t ops[] = {
  1462. {"PSET #0x%02X ", 0xE40, MASK_7B, 0, 0, 5, &op_pset_cb}, // PSET
  1463. {"JP #0x%02X ", 0x000, MASK_4B, 0, 0, 5, &op_jp_cb}, // JP
  1464. {"JP C #0x%02X ", 0x200, MASK_4B, 0, 0, 5, &op_jp_c_cb}, // JP_C
  1465. {"JP NC #0x%02X ", 0x300, MASK_4B, 0, 0, 5, &op_jp_nc_cb}, // JP_NC
  1466. {"JP Z #0x%02X ", 0x600, MASK_4B, 0, 0, 5, &op_jp_z_cb}, // JP_Z
  1467. {"JP NZ #0x%02X ", 0x700, MASK_4B, 0, 0, 5, &op_jp_nz_cb}, // JP_NZ
  1468. {"JPBA ", 0xFE8, MASK_12B, 0, 0, 5, &op_jpba_cb}, // JPBA
  1469. {"CALL #0x%02X ", 0x400, MASK_4B, 0, 0, 7, &op_call_cb}, // CALL
  1470. {"CALZ #0x%02X ", 0x500, MASK_4B, 0, 0, 7, &op_calz_cb}, // CALZ
  1471. {"RET ", 0xFDF, MASK_12B, 0, 0, 7, &op_ret_cb}, // RET
  1472. {"RETS ", 0xFDE, MASK_12B, 0, 0, 12, &op_rets_cb}, // RETS
  1473. {"RETD #0x%02X ", 0x100, MASK_4B, 0, 0, 12, &op_retd_cb}, // RETD
  1474. {"NOP5 ", 0xFFB, MASK_12B, 0, 0, 5, &op_nop5_cb}, // NOP5
  1475. {"NOP7 ", 0xFFF, MASK_12B, 0, 0, 7, &op_nop7_cb}, // NOP7
  1476. {"HALT ", 0xFF8, MASK_12B, 0, 0, 5, &op_halt_cb}, // HALT
  1477. {"INC X #0x%02X ", 0xEE0, MASK_12B, 0, 0, 5, &op_inc_x_cb}, // INC_X
  1478. {"INC Y #0x%02X ", 0xEF0, MASK_12B, 0, 0, 5, &op_inc_y_cb}, // INC_Y
  1479. {"LD X #0x%02X ", 0xB00, MASK_4B, 0, 0, 5, &op_ld_x_cb}, // LD_X
  1480. {"LD Y #0x%02X ", 0x800, MASK_4B, 0, 0, 5, &op_ld_y_cb}, // LD_Y
  1481. {"LD XP R(#0x%02X) ", 0xE80, MASK_10B, 0, 0, 5, &op_ld_xp_r_cb}, // LD_XP_R
  1482. {"LD XH R(#0x%02X) ", 0xE84, MASK_10B, 0, 0, 5, &op_ld_xh_r_cb}, // LD_XH_R
  1483. {"LD XL R(#0x%02X) ", 0xE88, MASK_10B, 0, 0, 5, &op_ld_xl_r_cb}, // LD_XL_R
  1484. {"LD YP R(#0x%02X) ", 0xE90, MASK_10B, 0, 0, 5, &op_ld_yp_r_cb}, // LD_YP_R
  1485. {"LD YH R(#0x%02X) ", 0xE94, MASK_10B, 0, 0, 5, &op_ld_yh_r_cb}, // LD_YH_R
  1486. {"LD YL R(#0x%02X) ", 0xE98, MASK_10B, 0, 0, 5, &op_ld_yl_r_cb}, // LD_YL_R
  1487. {"LD R(#0x%02X) XP ", 0xEA0, MASK_10B, 0, 0, 5, &op_ld_r_xp_cb}, // LD_R_XP
  1488. {"LD R(#0x%02X) XH ", 0xEA4, MASK_10B, 0, 0, 5, &op_ld_r_xh_cb}, // LD_R_XH
  1489. {"LD R(#0x%02X) XL ", 0xEA8, MASK_10B, 0, 0, 5, &op_ld_r_xl_cb}, // LD_R_XL
  1490. {"LD R(#0x%02X) YP ", 0xEB0, MASK_10B, 0, 0, 5, &op_ld_r_yp_cb}, // LD_R_YP
  1491. {"LD R(#0x%02X) YH ", 0xEB4, MASK_10B, 0, 0, 5, &op_ld_r_yh_cb}, // LD_R_YH
  1492. {"LD R(#0x%02X) YL ", 0xEB8, MASK_10B, 0, 0, 5, &op_ld_r_yl_cb}, // LD_R_YL
  1493. {"ADC XH #0x%02X ", 0xA00, MASK_8B, 0, 0, 7, &op_adc_xh_cb}, // ADC_XH
  1494. {"ADC XL #0x%02X ", 0xA10, MASK_8B, 0, 0, 7, &op_adc_xl_cb}, // ADC_XL
  1495. {"ADC YH #0x%02X ", 0xA20, MASK_8B, 0, 0, 7, &op_adc_yh_cb}, // ADC_YH
  1496. {"ADC YL #0x%02X ", 0xA30, MASK_8B, 0, 0, 7, &op_adc_yl_cb}, // ADC_YL
  1497. {"CP XH #0x%02X ", 0xA40, MASK_8B, 0, 0, 7, &op_cp_xh_cb}, // CP_XH
  1498. {"CP XL #0x%02X ", 0xA50, MASK_8B, 0, 0, 7, &op_cp_xl_cb}, // CP_XL
  1499. {"CP YH #0x%02X ", 0xA60, MASK_8B, 0, 0, 7, &op_cp_yh_cb}, // CP_YH
  1500. {"CP YL #0x%02X ", 0xA70, MASK_8B, 0, 0, 7, &op_cp_yl_cb}, // CP_YL
  1501. {"LD R(#0x%02X) #0x%02X ", 0xE00, MASK_6B, 4, 0x030, 5, &op_ld_r_i_cb}, // LD_R_I
  1502. {"LD R(#0x%02X) Q(#0x%02X)", 0xEC0, MASK_8B, 2, 0x00C, 5, &op_ld_r_q_cb}, // LD_R_Q
  1503. {"LD A M(#0x%02X) ", 0xFA0, MASK_8B, 0, 0, 5, &op_ld_a_mn_cb}, // LD_A_MN
  1504. {"LD B M(#0x%02X) ", 0xFB0, MASK_8B, 0, 0, 5, &op_ld_b_mn_cb}, // LD_B_MN
  1505. {"LD M(#0x%02X) A ", 0xF80, MASK_8B, 0, 0, 5, &op_ld_mn_a_cb}, // LD_MN_A
  1506. {"LD M(#0x%02X) B ", 0xF90, MASK_8B, 0, 0, 5, &op_ld_mn_b_cb}, // LD_MN_B
  1507. {"LDPX MX #0x%02X ", 0xE60, MASK_8B, 0, 0, 5, &op_ldpx_mx_cb}, // LDPX_MX
  1508. {"LDPX R(#0x%02X) Q(#0x%02X)", 0xEE0, MASK_8B, 2, 0x00C, 5, &op_ldpx_r_cb}, // LDPX_R
  1509. {"LDPY MY #0x%02X ", 0xE70, MASK_8B, 0, 0, 5, &op_ldpy_my_cb}, // LDPY_MY
  1510. {"LDPY R(#0x%02X) Q(#0x%02X)", 0xEF0, MASK_8B, 2, 0x00C, 5, &op_ldpy_r_cb}, // LDPY_R
  1511. {"LBPX #0x%02X ", 0x900, MASK_4B, 0, 0, 5, &op_lbpx_cb}, // LBPX
  1512. {"SET #0x%02X ", 0xF40, MASK_8B, 0, 0, 7, &op_set_cb}, // SET
  1513. {"RST #0x%02X ", 0xF50, MASK_8B, 0, 0, 7, &op_rst_cb}, // RST
  1514. {"SCF ", 0xF41, MASK_12B, 0, 0, 7, &op_scf_cb}, // SCF
  1515. {"RCF ", 0xF5E, MASK_12B, 0, 0, 7, &op_rcf_cb}, // RCF
  1516. {"SZF ", 0xF42, MASK_12B, 0, 0, 7, &op_szf_cb}, // SZF
  1517. {"RZF ", 0xF5D, MASK_12B, 0, 0, 7, &op_rzf_cb}, // RZF
  1518. {"SDF ", 0xF44, MASK_12B, 0, 0, 7, &op_sdf_cb}, // SDF
  1519. {"RDF ", 0xF5B, MASK_12B, 0, 0, 7, &op_rdf_cb}, // RDF
  1520. {"EI ", 0xF48, MASK_12B, 0, 0, 7, &op_ei_cb}, // EI
  1521. {"DI ", 0xF57, MASK_12B, 0, 0, 7, &op_di_cb}, // DI
  1522. {"INC SP ", 0xFDB, MASK_12B, 0, 0, 5, &op_inc_sp_cb}, // INC_SP
  1523. {"DEC SP ", 0xFCB, MASK_12B, 0, 0, 5, &op_dec_sp_cb}, // DEC_SP
  1524. {"PUSH R(#0x%02X) ", 0xFC0, MASK_10B, 0, 0, 5, &op_push_r_cb}, // PUSH_R
  1525. {"PUSH XP ", 0xFC4, MASK_12B, 0, 0, 5, &op_push_xp_cb}, // PUSH_XP
  1526. {"PUSH XH ", 0xFC5, MASK_12B, 0, 0, 5, &op_push_xh_cb}, // PUSH_XH
  1527. {"PUSH XL ", 0xFC6, MASK_12B, 0, 0, 5, &op_push_xl_cb}, // PUSH_XL
  1528. {"PUSH YP ", 0xFC7, MASK_12B, 0, 0, 5, &op_push_yp_cb}, // PUSH_YP
  1529. {"PUSH YH ", 0xFC8, MASK_12B, 0, 0, 5, &op_push_yh_cb}, // PUSH_YH
  1530. {"PUSH YL ", 0xFC9, MASK_12B, 0, 0, 5, &op_push_yl_cb}, // PUSH_YL
  1531. {"PUSH F ", 0xFCA, MASK_12B, 0, 0, 5, &op_push_f_cb}, // PUSH_F
  1532. {"POP R(#0x%02X) ", 0xFD0, MASK_10B, 0, 0, 5, &op_pop_r_cb}, // POP_R
  1533. {"POP XP ", 0xFD4, MASK_12B, 0, 0, 5, &op_pop_xp_cb}, // POP_XP
  1534. {"POP XH ", 0xFD5, MASK_12B, 0, 0, 5, &op_pop_xh_cb}, // POP_XH
  1535. {"POP XL ", 0xFD6, MASK_12B, 0, 0, 5, &op_pop_xl_cb}, // POP_XL
  1536. {"POP YP ", 0xFD7, MASK_12B, 0, 0, 5, &op_pop_yp_cb}, // POP_YP
  1537. {"POP YH ", 0xFD8, MASK_12B, 0, 0, 5, &op_pop_yh_cb}, // POP_YH
  1538. {"POP YL ", 0xFD9, MASK_12B, 0, 0, 5, &op_pop_yl_cb}, // POP_YL
  1539. {"POP F ", 0xFDA, MASK_12B, 0, 0, 5, &op_pop_f_cb}, // POP_F
  1540. {"LD SPH R(#0x%02X) ", 0xFE0, MASK_10B, 0, 0, 5, &op_ld_sph_r_cb}, // LD_SPH_R
  1541. {"LD SPL R(#0x%02X) ", 0xFF0, MASK_10B, 0, 0, 5, &op_ld_spl_r_cb}, // LD_SPL_R
  1542. {"LD R(#0x%02X) SPH ", 0xFE4, MASK_10B, 0, 0, 5, &op_ld_r_sph_cb}, // LD_R_SPH
  1543. {"LD R(#0x%02X) SPL ", 0xFF4, MASK_10B, 0, 0, 5, &op_ld_r_spl_cb}, // LD_R_SPL
  1544. {"ADD R(#0x%02X) #0x%02X ", 0xC00, MASK_6B, 4, 0x030, 7, &op_add_r_i_cb}, // ADD_R_I
  1545. {"ADD R(#0x%02X) Q(#0x%02X)", 0xA80, MASK_8B, 2, 0x00C, 7, &op_add_r_q_cb}, // ADD_R_Q
  1546. {"ADC R(#0x%02X) #0x%02X ", 0xC40, MASK_6B, 4, 0x030, 7, &op_adc_r_i_cb}, // ADC_R_I
  1547. {"ADC R(#0x%02X) Q(#0x%02X)", 0xA90, MASK_8B, 2, 0x00C, 7, &op_adc_r_q_cb}, // ADC_R_Q
  1548. {"SUB R(#0x%02X) Q(#0x%02X)", 0xAA0, MASK_8B, 2, 0x00C, 7, &op_sub_cb}, // SUB
  1549. {"SBC R(#0x%02X) #0x%02X ", 0xB40, MASK_6B, 4, 0x030, 7, &op_sbc_r_i_cb}, // SBC_R_I
  1550. {"SBC R(#0x%02X) Q(#0x%02X)", 0xAB0, MASK_8B, 2, 0x00C, 7, &op_sbc_r_q_cb}, // SBC_R_Q
  1551. {"AND R(#0x%02X) #0x%02X ", 0xC80, MASK_6B, 4, 0x030, 7, &op_and_r_i_cb}, // AND_R_I
  1552. {"AND R(#0x%02X) Q(#0x%02X)", 0xAC0, MASK_8B, 2, 0x00C, 7, &op_and_r_q_cb}, // AND_R_Q
  1553. {"OR R(#0x%02X) #0x%02X ", 0xCC0, MASK_6B, 4, 0x030, 7, &op_or_r_i_cb}, // OR_R_I
  1554. {"OR R(#0x%02X) Q(#0x%02X)", 0xAD0, MASK_8B, 2, 0x00C, 7, &op_or_r_q_cb}, // OR_R_Q
  1555. {"XOR R(#0x%02X) #0x%02X ", 0xD00, MASK_6B, 4, 0x030, 7, &op_xor_r_i_cb}, // XOR_R_I
  1556. {"XOR R(#0x%02X) Q(#0x%02X)", 0xAE0, MASK_8B, 2, 0x00C, 7, &op_xor_r_q_cb}, // XOR_R_Q
  1557. {"CP R(#0x%02X) #0x%02X ", 0xDC0, MASK_6B, 4, 0x030, 7, &op_cp_r_i_cb}, // CP_R_I
  1558. {"CP R(#0x%02X) Q(#0x%02X)", 0xF00, MASK_8B, 2, 0x00C, 7, &op_cp_r_q_cb}, // CP_R_Q
  1559. {"FAN R(#0x%02X) #0x%02X ", 0xD80, MASK_6B, 4, 0x030, 7, &op_fan_r_i_cb}, // FAN_R_I
  1560. {"FAN R(#0x%02X) Q(#0x%02X)", 0xF10, MASK_8B, 2, 0x00C, 7, &op_fan_r_q_cb}, // FAN_R_Q
  1561. {"RLC R(#0x%02X) ", 0xAF0, MASK_8B, 0, 0, 7, &op_rlc_cb}, // RLC
  1562. {"RRC R(#0x%02X) ", 0xE8C, MASK_10B, 0, 0, 5, &op_rrc_cb}, // RRC
  1563. {"INC M(#0x%02X) ", 0xF60, MASK_8B, 0, 0, 7, &op_inc_mn_cb}, // INC_MN
  1564. {"DEC M(#0x%02X) ", 0xF70, MASK_8B, 0, 0, 7, &op_dec_mn_cb}, // DEC_MN
  1565. {"ACPX R(#0x%02X) ", 0xF28, MASK_10B, 0, 0, 7, &op_acpx_cb}, // ACPX
  1566. {"ACPY R(#0x%02X) ", 0xF2C, MASK_10B, 0, 0, 7, &op_acpy_cb}, // ACPY
  1567. {"SCPX R(#0x%02X) ", 0xF38, MASK_10B, 0, 0, 7, &op_scpx_cb}, // SCPX
  1568. {"SCPY R(#0x%02X) ", 0xF3C, MASK_10B, 0, 0, 7, &op_scpy_cb}, // SCPY
  1569. {"NOT R(#0x%02X) ", 0xD0F, 0xFCF, 4, 0, 7, &op_not_cb}, // NOT
  1570. {NULL, 0, 0, 0, 0, 0, NULL},
  1571. };
  1572. static timestamp_t wait_for_cycles(timestamp_t since, u8_t cycles) {
  1573. timestamp_t deadline;
  1574. tick_counter += cycles;
  1575. if(speed_ratio == 0) {
  1576. /* Emulation will be as fast as possible */
  1577. return g_hal->get_timestamp();
  1578. }
  1579. deadline = since + (cycles * ts_freq) / (TICK_FREQUENCY * speed_ratio);
  1580. g_hal->sleep_until(deadline);
  1581. return deadline;
  1582. }
  1583. static void process_interrupts(void) {
  1584. u8_t i;
  1585. /* Process interrupts in priority order */
  1586. for(i = 0; i < INT_SLOT_NUM; i++) {
  1587. if(interrupts[i].triggered) {
  1588. //printf("IT %u !\n", i);
  1589. SET_M(sp - 1, PCP);
  1590. SET_M(sp - 2, PCSH);
  1591. SET_M(sp - 3, PCSL);
  1592. sp = (sp - 3) & 0xFF;
  1593. CLEAR_I();
  1594. np = TO_NP(NBP, 1);
  1595. pc = TO_PC(PCB, 1, interrupts[i].vector);
  1596. call_depth++;
  1597. ref_ts = wait_for_cycles(ref_ts, 12);
  1598. interrupts[i].triggered = 0;
  1599. }
  1600. }
  1601. }
  1602. static void print_state(u8_t op_num, u12_t op, u13_t addr) {
  1603. u8_t i;
  1604. if(!g_hal->is_log_enabled(LOG_CPU)) {
  1605. return;
  1606. }
  1607. g_hal->log(LOG_CPU, "0x%04X: ", addr);
  1608. for(i = 0; i < call_depth; i++) {
  1609. g_hal->log(LOG_CPU, " ");
  1610. }
  1611. if(ops[op_num].mask_arg0 != 0) {
  1612. /* Two arguments */
  1613. g_hal->log(
  1614. LOG_CPU,
  1615. ops[op_num].log,
  1616. (op & ops[op_num].mask_arg0) >> ops[op_num].shift_arg0,
  1617. op & ~(ops[op_num].mask | ops[op_num].mask_arg0));
  1618. } else {
  1619. /* One argument */
  1620. g_hal->log(LOG_CPU, ops[op_num].log, (op & ~ops[op_num].mask) >> ops[op_num].shift_arg0);
  1621. }
  1622. if(call_depth < 10) {
  1623. for(i = 0; i < (10 - call_depth); i++) {
  1624. g_hal->log(LOG_CPU, " ");
  1625. }
  1626. }
  1627. g_hal->log(LOG_CPU, " ; 0x%03X - ", op);
  1628. for(i = 0; i < 12; i++) {
  1629. g_hal->log(LOG_CPU, "%s", ((op >> (11 - i)) & 0x1) ? "1" : "0");
  1630. }
  1631. g_hal->log(
  1632. LOG_CPU,
  1633. " - PC = 0x%04X, SP = 0x%02X, NP = 0x%02X, X = 0x%03X, Y = 0x%03X, A = 0x%X, B = 0x%X, F = 0x%X\n",
  1634. pc,
  1635. sp,
  1636. np,
  1637. x,
  1638. y,
  1639. a,
  1640. b,
  1641. flags);
  1642. }
  1643. void cpu_reset(void) {
  1644. u13_t i;
  1645. /* Registers and variables init */
  1646. pc = TO_PC(0, 1, 0x00); // PC starts at bank 0, page 1, step 0
  1647. np = TO_NP(0, 1); // NP starts at page 1
  1648. a = 0; // undef
  1649. b = 0; // undef
  1650. x = 0; // undef
  1651. y = 0; // undef
  1652. sp = 0; // undef
  1653. flags = 0;
  1654. /* Init RAM to zeros */
  1655. for(i = 0; i < MEM_BUFFER_SIZE; i++) {
  1656. memory[i] = 0;
  1657. }
  1658. SET_IO_MEMORY(memory, REG_K40_K43_BZ_OUTPUT_PORT, 0xF); // Output port (R40-R43)
  1659. SET_IO_MEMORY(memory, REG_LCD_CTRL, 0x8); // LCD control
  1660. /* TODO: Input relation register */
  1661. cpu_sync_ref_timestamp();
  1662. }
  1663. bool_t cpu_init(const u12_t* program, breakpoint_t* breakpoints, u32_t freq) {
  1664. g_program = program;
  1665. g_breakpoints = breakpoints;
  1666. ts_freq = freq;
  1667. cpu_reset();
  1668. return 0;
  1669. }
  1670. void cpu_release(void) {
  1671. }
  1672. int cpu_step(void) {
  1673. u12_t op;
  1674. u8_t i;
  1675. breakpoint_t* bp = g_breakpoints;
  1676. static u8_t previous_cycles = 0;
  1677. op = g_program[pc];
  1678. /* Lookup the OP code */
  1679. for(i = 0; ops[i].log != NULL; i++) {
  1680. if((op & ops[i].mask) == ops[i].code) {
  1681. break;
  1682. }
  1683. }
  1684. if(ops[i].log == NULL) {
  1685. g_hal->log(LOG_ERROR, "Unknown op-code 0x%X (pc = 0x%04X)\n", op, pc);
  1686. return 1;
  1687. }
  1688. next_pc = (pc + 1) & 0x1FFF;
  1689. /* Display the operation along with the current state of the processor */
  1690. print_state(i, op, pc);
  1691. /* Match the speed of the real processor
  1692. * NOTE: For better accuracy, the final wait should happen here, however
  1693. * the downside is that all interrupts will likely be delayed by one OP
  1694. */
  1695. ref_ts = wait_for_cycles(ref_ts, previous_cycles);
  1696. /* Process the OP code */
  1697. if(ops[i].cb != NULL) {
  1698. if(ops[i].mask_arg0 != 0) {
  1699. /* Two arguments */
  1700. ops[i].cb(
  1701. (op & ops[i].mask_arg0) >> ops[i].shift_arg0,
  1702. op & ~(ops[i].mask | ops[i].mask_arg0));
  1703. } else {
  1704. /* One arguments */
  1705. ops[i].cb((op & ~ops[i].mask) >> ops[i].shift_arg0, 0);
  1706. }
  1707. }
  1708. /* Prepare for the next instruction */
  1709. pc = next_pc;
  1710. previous_cycles = ops[i].cycles;
  1711. if(i > 0) {
  1712. /* OP code is not PSET, reset NP */
  1713. np = (pc >> 8) & 0x1F;
  1714. }
  1715. /* Handle timers using the internal tick counter */
  1716. if(tick_counter - clk_timer_timestamp >= TIMER_1HZ_PERIOD) {
  1717. do {
  1718. clk_timer_timestamp += TIMER_1HZ_PERIOD;
  1719. } while(tick_counter - clk_timer_timestamp >= TIMER_1HZ_PERIOD);
  1720. generate_interrupt(INT_CLOCK_TIMER_SLOT, 3);
  1721. }
  1722. if(prog_timer_enabled && tick_counter - prog_timer_timestamp >= TIMER_256HZ_PERIOD) {
  1723. do {
  1724. prog_timer_timestamp += TIMER_256HZ_PERIOD;
  1725. prog_timer_data--;
  1726. if(prog_timer_data == 0) {
  1727. prog_timer_data = prog_timer_rld;
  1728. generate_interrupt(INT_PROG_TIMER_SLOT, 0);
  1729. }
  1730. } while(tick_counter - prog_timer_timestamp >= TIMER_256HZ_PERIOD);
  1731. }
  1732. /* Check if there is any pending interrupt */
  1733. if(I && i > 0) { // Do not process interrupts after a PSET operation
  1734. process_interrupts();
  1735. }
  1736. /* Check if we could pause the execution */
  1737. while(bp != NULL) {
  1738. if(bp->addr == pc) {
  1739. return 1;
  1740. }
  1741. bp = bp->next;
  1742. }
  1743. return 0;
  1744. }