Guest User

Untitled

a guest
Feb 8th, 2019
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --bit = bit32
  2.  
  3. MemIoOps = {}
  4.  
  5. setmetatable(MemIoOps, {
  6. __call = function (class, ...)
  7. numArgs = select("#", ...)
  8. if numArgs == 0 then
  9. return class.newArg0(class)
  10. elseif numArgs == 2 then
  11. return class.newArg2(class, ...)
  12. end
  13. end,
  14. })
  15.  
  16. MemIoOps.z80Ram = {}
  17. MemIoOps.z80Ports = {}
  18. MemIoOps.tstates = 0;
  19.  
  20. MemIoOps.newArg0 = function(self)
  21. for i=0, 0x10000 - 1 do
  22. self.z80Ram[i] = 0x0;
  23. end
  24.  
  25. for i=0, 0x10000 - 1 do
  26. self.z80Ports[i] = 0x0;
  27. end
  28.  
  29. return self
  30. end
  31.  
  32. MemIoOps.newArg2 = function(self, ramSize, portSize)
  33. if ramSize < 0 or ramSize > 0x10000 then
  34. error("ramSize Out of Range [0x0000 - 0x10000]")
  35. end
  36. if ramSize > 0 then
  37. for i=0, ramSize - 1 do
  38. self.z80Ram[i] = 0x0;
  39. end
  40. end
  41.  
  42. if portSize < 0 or portSize > 0x10000 then
  43. error("portSize Out of Range [0x0000 - 0x10000]")
  44. end
  45.  
  46. if portSize > 0 then
  47. for i=0, portSize - 1 do
  48. self.z80Ports[i] = 0x0;
  49. end
  50. end
  51.  
  52. return self
  53. end
  54.  
  55. MemIoOps.setRam = function(self, ram)
  56. self.z80Ram = ram;
  57. end
  58.  
  59. MemIoOps.setPorts = function(self, ports)
  60. self.z80Ports = ports;
  61. end
  62.  
  63. MemIoOps.fetchOpcode = function(self, address)
  64. -- 3 clocks to fetch opcode from RAM and 1 execution clock
  65. self.tstates = self.tstates + 4;
  66. return bit.band(self.z80Ram[address], 0xff);
  67. end
  68.  
  69. MemIoOps.peek8 = function(self, address)
  70. --print("Peeking at: "..string.format("0x%x", address))
  71. if address > #self.z80Ram then
  72. error("Peek8 - Address out of bounds: 0x"..string.format("%x", address))
  73. else
  74. self.tstates = self.tstates + 3; -- 3 clocks for read byte from RAM
  75. return bit.band(self.z80Ram[address], 0xff);
  76. end
  77. end
  78.  
  79. MemIoOps.poke8 = function(self, address, value)
  80. ---print("Poking at: "..string.format("0x%x", address))
  81. if address > #self.z80Ram then
  82. error("Poke8 - Address out of bounds: 0x"..string.format("%x", address))
  83. else
  84. self.tstates = self.tstates + 3; -- 3 clocks for write byte to RAM
  85. --self.z80Ram[address] = bit.band(value, 0xFF);
  86. rawset(self.z80Ram, address, bit.band(value, 0xFF))
  87. end
  88. end
  89.  
  90. MemIoOps.peek16 = function(self, address)
  91. lsb = self:peek8(address);
  92. msb = self:peek8(address + 1);
  93. return bit.bor(bit.blshift(msb, 8), lsb);
  94. end
  95.  
  96. MemIoOps.poke16 = function(self, address, word)
  97. self:poke8(address, word);
  98. self:poke8(address + 1, bit.brshift(word, 8));
  99. end
  100.  
  101. MemIoOps.inPort = function(self, port)
  102. print(port)
  103. self.tstates = self.tstates + 4; -- 4 clocks for read byte from bus
  104. return bit.band(self.z80Ports[port], 0xff);
  105. end
  106.  
  107. MemIoOps.outPort = function(self, port, value)
  108. self.tstates = self.tstates + 4; -- 4 clocks for write byte to bus
  109. self.z80Ports[port] = bit.band(value, 0xFF);
  110. end
  111.  
  112. MemIoOps.addressOnBus = function(self, address, tstates)
  113. -- Additional clocks to be added on some instructions
  114. -- Not to be changed, really.
  115. self.tstates = self.tstates + tstates;
  116. end
  117.  
  118. MemIoOps.interruptHandlingTime = function(self, tstates)
  119. -- Additional clocks to be added on INT & NMI
  120. self.tstates = self.tstates + tstates;
  121. end
  122.  
  123. MemIoOps.isActiveINT = function(self)
  124. return false;
  125. end
  126.  
  127. MemIoOps.getTstates = function(self)
  128. return self.tstates;
  129. end
  130.  
  131. MemIoOps.reset = function(self)
  132. self.tstates = 0;
  133. end
  134.  
  135. MemIoOps.dumpMemory = function(self)
  136. buf = ""
  137. for i=0,#self.z80Ram do
  138. buf = buf .. string.char(self.z80Ram[i])
  139. end
  140.  
  141. for i=1,math.ceil(#buf/16) * 16 do
  142. if (i-1) % 16 == 0 then io.write(string.format('%08X ', i-1)) end
  143. io.write( i > #buf and ' ' or string.format('%02X ', buf:byte(i)) )
  144. if i % 8 == 0 then io.write(' ') end
  145. if i % 16 == 0 then io.write( buf:sub(i-16+1, i):gsub('%c','.'), '\n' ) end
  146. end
  147. end
  148.  
  149. Z80 = {}
  150.  
  151. setmetatable(
  152. Z80,
  153. {
  154. __call = function(class, ...)
  155. return class.new(class, ...)
  156. end
  157. }
  158. )
  159.  
  160. Z80.a = 0
  161. Z80.b = 0
  162. Z80.c = 0
  163. Z80.d = 0
  164. Z80.e = 0
  165. Z80.h = 0
  166. Z80.l = 0
  167. Z80.a_prime = 0
  168. Z80.b_prime = 0
  169. Z80.c_prime = 0
  170. Z80.d_prime = 0
  171. Z80.e_prime = 0
  172. Z80.h_prime = 0
  173. Z80.l_prime = 0
  174. Z80.ix = 0
  175. Z80.iy = 0
  176. Z80.i = 0
  177. Z80.r = 0
  178. Z80.sp = 57328
  179. Z80.pc = 0
  180. Z80.flags = {
  181. S = false,
  182. Z = false,
  183. Y = false,
  184. H = false,
  185. X = false,
  186. P = false,
  187. N = false,
  188. C = false
  189. }
  190. Z80.flags_prime = {
  191. S = false,
  192. Z = false,
  193. Y = false,
  194. H = false,
  195. X = false,
  196. P = false,
  197. N = false,
  198. C = false
  199. }
  200. Z80.imode = 0
  201. Z80.iff1 = 0
  202. Z80.iff2 = 0
  203. Z80.halted = false
  204. Z80.do_delayed_di = false
  205. Z80.do_delayed_ei = false
  206. Z80.cycle_counter = 0
  207.  
  208. Z80.new = function(self, core)
  209. if
  210. (((not core or (type(core.mem_read) ~= "function")) or (type(core.mem_write) ~= "function")) or
  211. (type(core.io_read) ~= "function")) or
  212. (type(core.io_write) ~= "function")
  213. then
  214. error("Z80: Core object is missing required functions.")
  215. end
  216. self.core = core
  217.  
  218. return self
  219. end
  220.  
  221. function Z80.getState(self)
  222. return {
  223. b = self.b,
  224. a = self.a,
  225. c = self.c,
  226. d = self.d,
  227. e = self.e,
  228. h = self.h,
  229. l = self.l,
  230. a_prime = self.a_prime,
  231. b_prime = self.b_prime,
  232. c_prime = self.c_prime,
  233. d_prime = self.d_prime,
  234. e_prime = self.e_prime,
  235. h_prime = self.h_prime,
  236. l_prime = self.l_prime,
  237. ix = self.ix,
  238. iy = self.iy,
  239. i = self.i,
  240. r = self.r,
  241. sp = self.sp,
  242. pc = self.pc,
  243. flags = {
  244. S = self.flags.S,
  245. Z = self.flags.Z,
  246. Y = self.flags.Y,
  247. H = self.flags.H,
  248. X = self.flags.X,
  249. P = self.flags.P,
  250. N = self.flags.N,
  251. C = self.flags.C
  252. },
  253. flags_prime = {
  254. S = self.flags_prime.S,
  255. Z = self.flags_prime.Z,
  256. Y = self.flags_prime.Y,
  257. H = self.flags_prime.H,
  258. X = self.flags_prime.X,
  259. P = self.flags_prime.P,
  260. N = self.flags_prime.N,
  261. C = self.flags_prime.C
  262. },
  263. imode = self.imode,
  264. iff1 = self.iff1,
  265. iff2 = self.iff2,
  266. halted = self.halted,
  267. do_delayed_di = self.do_delayed_di,
  268. do_delayed_ei = self.do_delayed_ei,
  269. cycle_counter = self.cycle_counter
  270. }
  271. end
  272. function Z80.setState(self, state)
  273. self.b = state.b
  274. self.a = state.a
  275. self.c = state.c
  276. self.d = state.d
  277. self.e = state.e
  278. self.h = state.h
  279. self.l = state.l
  280. self.a_prime = state.a_prime
  281. self.b_prime = state.b_prime
  282. self.c_prime = state.c_prime
  283. self.d_prime = state.d_prime
  284. self.e_prime = state.e_prime
  285. self.h_prime = state.h_prime
  286. self.l_prime = state.l_prime
  287. self.ix = state.ix
  288. self.iy = state.iy
  289. self.i = state.i
  290. self.r = state.r
  291. self.sp = state.sp
  292. self.pc = state.pc
  293. self.flags.S = state.flags.S
  294. self.flags.Z = state.flags.Z
  295. self.flags.Y = state.flags.Y
  296. self.flags.H = state.flags.H
  297. self.flags.X = state.flags.X
  298. self.flags.P = state.flags.P
  299. self.flags.N = state.flags.N
  300. self.flags.C = state.flags.C
  301. self.flags_prime.S = state.flags_prime.S
  302. self.flags_prime.Z = state.flags_prime.Z
  303. self.flags_prime.Y = state.flags_prime.Y
  304. self.flags_prime.H = state.flags_prime.H
  305. self.flags_prime.X = state.flags_prime.X
  306. self.flags_prime.P = state.flags_prime.P
  307. self.flags_prime.N = state.flags_prime.N
  308. self.flags_prime.C = state.flags_prime.C
  309. self.imode = state.imode
  310. self.iff1 = state.iff1
  311. self.iff2 = state.iff2
  312. self.halted = state.halted
  313. self.do_delayed_di = state.do_delayed_di
  314. self.do_delayed_ei = state.do_delayed_ei
  315. self.cycle_counter = state.cycle_counter
  316. end
  317. Z80.reset = function(self)
  318. self.sp = 57328
  319. self.pc = 0
  320. self.a = 0
  321. self.r = 0
  322. self:set_flags_register(0)
  323. self.imode = 0
  324. self.iff1 = 0
  325. self.iff2 = 0
  326. self.halted = false
  327. self.do_delayed_di = false
  328. self.do_delayed_ei = false
  329. self.cycle_counter = 0
  330. end
  331. Z80.run_instruction = function(self)
  332. if not self.halted then
  333. local doing_delayed_di, doing_delayed_ei = false, false
  334. if self.do_delayed_di then
  335. self.do_delayed_di = false
  336. doing_delayed_di = true
  337. elseif self.do_delayed_ei then
  338. self.do_delayed_ei = false
  339. doing_delayed_ei = true
  340. end
  341. self.r = bit.bor(bit.band(self.r, 128), bit.band((bit.band(self.r, 127) + 1), 127))
  342. local opcode = self.core.mem_read(self, self.pc)
  343. self:decode_instruction(opcode)
  344. self.pc = bit.band((self.pc + 1), 65535)
  345. if doing_delayed_di then
  346. self.iff1 = 0
  347. self.iff2 = 0
  348. elseif doing_delayed_ei then
  349. self.iff1 = 1
  350. self.iff2 = 1
  351. end
  352. local retval = self.cycle_counter
  353. --self.cycle_counter = 0
  354. return retval
  355. else
  356. return 1
  357. end
  358. end
  359. Z80.interrupt = function(self, non_maskable, data)
  360. if non_maskable then
  361. self.r = bit.bor(bit.band(self.r, 128), bit.band((bit.band(self.r, 127) + 1), 127))
  362. self.halted = false
  363. self.iff2 = self.iff1
  364. self.iff1 = 0
  365. self:push_word(self.pc)
  366. self.pc = 102
  367. self.cycle_counter = self.cycle_counter + 11
  368. elseif self.iff1 then
  369. self.r = bit.bor(bit.band(self.r, 128), bit.band((bit.band(self.r, 127) + 1), 127))
  370. self.halted = false
  371. self.iff1 = 0
  372. self.iff2 = 0
  373. if self.imode == 0 then
  374. self:decode_instruction(data)
  375. self.cycle_counter = self.cycle_counter + 2
  376. elseif self.imode == 1 then
  377. self:push_word(self.pc)
  378. self.pc = 56
  379. self.cycle_counter = self.cycle_counter + 13
  380. elseif self.imode == 2 then
  381. self:push_word(self.pc)
  382. local vector_address = bit.bor(bit.blshift(i, 8), data)
  383. self.pc =
  384. bit.bor(
  385. self.core.mem_read(self, vector_address),
  386. bit.blshift(self.core.mem_read(self, bit.band((vector_address + 1), 65535)), 8)
  387. )
  388. self.cycle_counter = self.cycle_counter + 19
  389. end
  390. end
  391. end
  392. Z80.decode_instruction = function(self, opcode)
  393. local get_operand = function(opcode)
  394. return (bit.band(opcode, 7) == 0) and self.b or (bit.band(opcode, 7) == 1) and self.c or
  395. (bit.band(opcode, 7) == 2) and self.d or
  396. (bit.band(opcode, 7) == 3) and self.e or
  397. (bit.band(opcode, 7) == 4) and self.h or
  398. (bit.band(opcode, 7) == 5) and self.l or
  399. (bit.band(opcode, 7) == 6) and self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8))) or
  400. self.a
  401. end
  402. if opcode == 118 then
  403. self.halted = true
  404. elseif (opcode >= 64) and (opcode < 128) then
  405. local operand = get_operand(opcode)
  406. if bit.brshift(bit.band(opcode, 56), 3) == 0 then
  407. self.b = operand
  408. elseif bit.brshift(bit.band(opcode, 56), 3) == 1 then
  409. self.c = operand
  410. elseif bit.brshift(bit.band(opcode, 56), 3) == 2 then
  411. self.d = operand
  412. elseif bit.brshift(bit.band(opcode, 56), 3) == 3 then
  413. self.e = operand
  414. elseif bit.brshift(bit.band(opcode, 56), 3) == 4 then
  415. self.h = operand
  416. elseif bit.brshift(bit.band(opcode, 56), 3) == 5 then
  417. self.l = operand
  418. elseif bit.brshift(bit.band(opcode, 56), 3) == 6 then
  419. self.core.mem_write(self, bit.bor(self.l, bit.blshift(self.h, 8)), operand)
  420. elseif bit.brshift(bit.band(opcode, 56), 3) == 7 then
  421. self.a = operand
  422. end
  423. elseif (opcode >= 128) and (opcode < 192) then
  424. local operand, op_array =
  425. get_operand(opcode),
  426. {self.do_add, self.do_adc, self.do_sub, self.do_sbc, self.do_and, self.do_xor, self.do_or, self.do_cp}
  427. --print(bit.brshift((opcode & 56), 3) + 1)
  428. op_array[bit.brshift(bit.band(opcode, 56), 3) + 1](self, operand)
  429. else
  430. local func = self.instructions[opcode]
  431. func(self)
  432. end
  433. self.cycle_counter = self.cycle_counter + self.cycle_counts[opcode]
  434. end
  435. Z80.get_signed_offset_byte = function(self, value)
  436. value = bit.band(value, 255)
  437. if bit.band(value, 128) ~= 0 then
  438. value = -(bit.band(255, bit.bnot(value)) + 1)
  439. end
  440. return value
  441. end
  442. Z80.get_flags_register = function(self)
  443. local flagS, flagZ, flagY, flagH, flagX, flagP, flagN, flagC = 0, 0, 0, 0, 0, 0, 0, 0
  444. if (self.flags.S == true) then
  445. flagS = 1
  446. else
  447. flagS = 0
  448. end
  449. if (self.flags.Z == true) then
  450. flagZ = 1
  451. else
  452. flagZ = 0
  453. end
  454. if (self.flags.Y == true) then
  455. flagY = 1
  456. else
  457. flagY = 0
  458. end
  459. if (self.flags.H == true) then
  460. flagH = 1
  461. else
  462. flagH = 0
  463. end
  464. if (self.flags.X == true) then
  465. flagX = 1
  466. else
  467. flagX = 0
  468. end
  469. if (self.flags.P == true) then
  470. flagP = 1
  471. else
  472. flagP = 0
  473. end
  474. if (self.flags.N == true) then
  475. flagN = 1
  476. else
  477. flagN = 0
  478. end
  479. if (self.flags.C == true) then
  480. flagC = 1
  481. else
  482. flagC = 0
  483. end
  484. local ret =
  485. bit.bor(
  486. bit.bor(
  487. bit.bor(
  488. bit.bor(
  489. bit.bor(bit.bor(bit.bor(bit.blshift(flagS, 7), bit.blshift(flagZ, 6)), bit.blshift(flagY, 5)), bit.blshift(flagH, 4)),
  490. bit.blshift(flagX, 3)
  491. ),
  492. bit.blshift(flagP, 2)
  493. ),
  494. bit.blshift(flagN, 1)
  495. ),
  496. flagC
  497. )
  498. return ret
  499. end
  500. Z80.get_flags_prime = function(self)
  501. --return (((((((self.flags_prime.S << 7) | (self.flags_prime.Z << 6)) | (self.flags_prime.Y << 5)) | (self.flags_prime.H << 4)) | (self.flags_prime.X << 3)) | (self.flags_prime.P << 2)) | (self.flags_prime.N << 1)) | self.flags_prime.C
  502.  
  503. local flag_primeS, flag_primeZ, flag_primeY, flag_primeH, flag_primeX, flag_primeP, flag_primeN, flag_primeC =
  504. 0, 0, 0, 0, 0, 0, 0, 0
  505. if (self.flags_prime.S == true) then
  506. flag_primeS = 1
  507. else
  508. flag_primeS = 0
  509. end
  510. if (self.flags_prime.Z == true) then
  511. flag_primeZ = 1
  512. else
  513. flag_primeZ = 0
  514. end
  515. if (self.flags_prime.Y == true) then
  516. flag_primeY = 1
  517. else
  518. flag_primeY = 0
  519. end
  520. if (self.flags_prime.H == true) then
  521. flag_primeH = 1
  522. else
  523. flag_primeH = 0
  524. end
  525. if (self.flags_prime.X == true) then
  526. flag_primeX = 1
  527. else
  528. flag_primeX = 0
  529. end
  530. if (self.flags_prime.P == true) then
  531. flag_primeP = 1
  532. else
  533. flag_primeP = 0
  534. end
  535. if (self.flags_prime.N == true) then
  536. flag_primeN = 1
  537. else
  538. flag_primeN = 0
  539. end
  540. if (self.flags_prime.C == true) then
  541. flag_primeC = 1
  542. else
  543. flag_primeC = 0
  544. end
  545. local ret =
  546. bit.bor(
  547. bit.bor(
  548. bit.bor(
  549. bit.bor(
  550. bit.bor(
  551. bit.bor(bit.bor(bit.blshift(flag_primeS, 7), bit.blshift(flag_primeZ, 6)), bit.blshift(flag_primeY, 5)),
  552. bit.blshift(flag_primeH, 4)
  553. ),
  554. bit.blshift(flag_primeX, 3)
  555. ),
  556. bit.blshift(flag_primeP, 2)
  557. ),
  558. bit.blshift(flag_primeN, 1)
  559. ),
  560. flag_primeC
  561. )
  562. return ret
  563. end
  564. Z80.set_flags_register = function(self, operand)
  565. if (bit.brshift(bit.band(operand, 128), 7)) == 1 then
  566. self.flags.S = true
  567. else
  568. self.flags.S = false
  569. end
  570. if (bit.brshift(bit.band(operand, 64), 6)) == 1 then
  571. self.flags.Z = true
  572. else
  573. self.flags.Z = false
  574. end
  575. if (bit.brshift(bit.band(operand, 32), 5)) == 1 then
  576. self.flags.Y = true
  577. else
  578. self.flags.Y = false
  579. end
  580. if (bit.brshift(bit.band(operand, 16), 4)) == 1 then
  581. self.flags.H = true
  582. else
  583. self.flags.H = false
  584. end
  585. if (bit.brshift(bit.band(operand, 8), 3)) == 1 then
  586. self.flags.X = true
  587. else
  588. self.flags.X = false
  589. end
  590. if (bit.brshift(bit.band(operand, 4), 2)) == 1 then
  591. self.flags.P = true
  592. else
  593. self.flags.P = false
  594. end
  595. if (bit.brshift(bit.band(operand, 2), 1)) == 1 then
  596. self.flags.N = true
  597. else
  598. self.flags.N = false
  599. end
  600. if bit.band(operand, 1) == 1 then
  601. self.flags.C = true
  602. else
  603. self.flags.C = false
  604. end
  605. end
  606. Z80.set_flags_prime = function(self, operand)
  607. if (bit.brshift(bit.band(operand, 128), 7)) == 1 then
  608. self.flags_prime.S = true
  609. else
  610. self.flags_prime.S = false
  611. end
  612. if (bit.brshift(bit.band(operand, 64), 6)) == 1 then
  613. self.flags_prime.Z = true
  614. else
  615. self.flags_prime.Z = false
  616. end
  617. if (bit.brshift(bit.band(operand, 32), 5)) == 1 then
  618. self.flags_prime.Y = true
  619. else
  620. self.flags_prime.Y = false
  621. end
  622. if (bit.brshift(bit.band(operand, 16), 4)) == 1 then
  623. self.flags_prime.H = true
  624. else
  625. self.flags_prime.H = false
  626. end
  627. if (bit.brshift(bit.band(operand, 8), 3)) == 1 then
  628. self.flags_prime.X = true
  629. else
  630. self.flags_prime.X = false
  631. end
  632. if (bit.brshift(bit.band(operand, 4), 2)) == 1 then
  633. self.flags_prime.P = true
  634. else
  635. self.flags_prime.P = false
  636. end
  637. if (bit.brshift(bit.band(operand, 2), 1)) == 1 then
  638. self.flags_prime.N = true
  639. else
  640. self.flags_prime.N = false
  641. end
  642. if bit.band(operand, 1) == 1 then
  643. self.flags_prime.C = true
  644. else
  645. self.flags_prime.C = false
  646. end
  647. end
  648. Z80.update_xy_flags = function(self, result)
  649. if (bit.brshift(bit.band(result, 32), 5) > 0) then
  650. self.flags.Y = true
  651. else
  652. self.flags.Y = false
  653. end
  654. if (bit.brshift(bit.band(result, 8), 3) > 0) then
  655. self.flags.X = true
  656. else
  657. self.flags.X = false
  658. end
  659. end
  660. Z80.get_parity = function(self, value)
  661. local parity_bits = {
  662. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
  663. }
  664. if (parity_bits[value + 1] == 1) then
  665. return true
  666. else
  667. return false
  668. end
  669. end
  670. Z80.push_word = function(self, operand)
  671. self.sp = bit.band((self.sp - 1), 65535)
  672. self.core.mem_write(self, self.sp, bit.brshift(bit.band(operand, 65280), 8))
  673. self.sp = bit.band((self.sp - 1), 65535)
  674. self.core.mem_write(self, self.sp, bit.band(operand, 255))
  675. end
  676. Z80.pop_word = function(self)
  677. local retval = bit.band(self.core.mem_read(self, self.sp), 255)
  678. self.sp = bit.band((self.sp + 1), 65535)
  679. retval = bit.bor(retval, bit.blshift(self.core.mem_read(self, self.sp), 8))
  680. self.sp = bit.band((self.sp + 1), 65535)
  681. return retval
  682. end
  683. Z80.do_conditional_absolute_jump = function(self, condition)
  684. if condition then
  685. self.pc =
  686. bit.bor(
  687. self.core.mem_read(self, bit.band((self.pc + 1), 0xffff)),
  688. bit.blshift(self.core.mem_read(self, bit.band((self.pc + 2), 0xffff)), 8)
  689. )
  690. self.pc = bit.band((self.pc - 1), 65535)
  691. else
  692. self.pc = bit.band((self.pc + 2), 65535)
  693. end
  694. end
  695. Z80.do_conditional_relative_jump = function(self, condition)
  696. if condition then
  697. self.cycle_counter = self.cycle_counter + 5
  698. local offset = self:get_signed_offset_byte(self.core.mem_read(self, bit.band((self.pc + 1), 65535)))
  699. self.pc = bit.band(((self.pc + offset) + 1), 65535)
  700. else
  701. self.pc = bit.band((self.pc + 1), 65535)
  702. end
  703. end
  704. Z80.do_conditional_call = function(self, condition)
  705. if condition then
  706. self.cycle_counter = self.cycle_counter + 7
  707. self:push_word(bit.band((self.pc + 3), 65535))
  708. self.pc =
  709. bit.bor(
  710. self.core.mem_read(self, bit.band((self.pc + 1), 65535)),
  711. bit.blshift(self.core.mem_read(self, bit.band((self.pc + 2), 65535)), 8)
  712. )
  713. self.pc = bit.band((self.pc - 1), 65535)
  714. else
  715. self.pc = bit.band((self.pc + 2), 65535)
  716. end
  717. end
  718. Z80.do_conditional_return = function(self, condition)
  719. if condition then
  720. self.cycle_counter = self.cycle_counter + 6
  721. self.pc = bit.band((self:pop_word() - 1), 65535)
  722. end
  723. end
  724. Z80.do_reset = function(self, address)
  725. self:push_word(bit.band((self.pc + 1), 65535))
  726. self.pc = bit.band((address - 1), 65535)
  727. end
  728. Z80.do_add = function(self, operand)
  729. local result = self.a + operand
  730. if (bit.band(result, 128) > 0) then
  731. self.flags.S = true
  732. else
  733. self.flags.S = false
  734. end
  735. if (bit.band(result, 255) == 0) then
  736. self.flags.Z = true
  737. else
  738. self.flags.Z = false
  739. end
  740. if (bit.band((bit.band(operand, 15) + bit.band(self.a, 15)), 16) ~= 0) then
  741. self.flags.H = true
  742. else
  743. self.flags.H = false
  744. end
  745. if ((bit.band(self.a, 128) == bit.band(operand, 128)) and (bit.band(self.a, 128) ~= bit.band(result, 128))) then
  746. self.flags.P = true
  747. else
  748. self.flags.P = false
  749. end
  750. self.flags.N = false
  751. if (bit.band(result, 256) > 0) then
  752. self.flags.C = true
  753. else
  754. self.flags.C = false
  755. end
  756. self.a = bit.band(result, 255)
  757. self:update_xy_flags(self.a)
  758. end
  759. Z80.do_adc = function(self, operand)
  760. local flagC = 0
  761. if (self.flags.C) then
  762. flagC = 1
  763. end
  764. local result = (self.a + operand) + flagC
  765. if (bit.band(result, 128) > 0) then
  766. self.flags.S = true
  767. else
  768. self.flags.S = false
  769. end
  770. if (bit.band(result, 255) == 0) then
  771. self.flags.Z = true
  772. else
  773. self.flags.Z = false
  774. end
  775. if (bit.band(((bit.band(operand, 15) + bit.band(self.a, 15)) + flagC), 16) ~= 0) then
  776. self.flags.H = true
  777. else
  778. self.flags.H = false
  779. end
  780. if ((bit.band(self.a, 128) == bit.band(operand, 128)) and (bit.band(self.a, 128) ~= bit.band(result, 128))) then
  781. self.flags.P = true
  782. else
  783. self.flags.P = false
  784. end
  785. self.flags.N = false
  786. if (bit.band(result, 256) > 0) then
  787. self.flags.C = true
  788. else
  789. self.flags.C = false
  790. end
  791. self.a = bit.band(result, 255)
  792. self:update_xy_flags(self.a)
  793. end
  794.  
  795. Z80.do_sub = function(self, operand)
  796. local result = self.a - operand
  797. if (bit.band(result, 128) > 0) then
  798. self.flags.S = true
  799. else
  800. self.flags.S = false
  801. end
  802. if (bit.band(result, 255) == 0) then
  803. self.flags.Z = true
  804. else
  805. self.flags.Z = false
  806. end
  807. if (bit.band((bit.band(self.a, 15) - bit.band(operand, 15)), 16) ~= 0) then
  808. self.flags.H = true
  809. else
  810. self.flags.H = false
  811. end
  812. if ((bit.band(self.a, 128) ~= bit.band(operand, 128)) and (bit.band(self.a, 128) ~= bit.band(result, 128))) then
  813. self.flags.P = true
  814. else
  815. self.flags.P = false
  816. end
  817. self.flags.N = true
  818. if (bit.band(result, 256) > 0) then
  819. self.flags.C = true
  820. else
  821. self.flags.C = false
  822. end
  823. self.a = bit.band(result, 255)
  824. self:update_xy_flags(self.a)
  825. end
  826. Z80.do_sbc = function(self, operand)
  827. local flagC = 0
  828. if (self.flags.C) then
  829. flagC = 1
  830. end
  831. local result = (self.a - operand) - flagC
  832. if (bit.band(result, 128) > 0) then
  833. self.flags.S = true
  834. else
  835. self.flags.S = false
  836. end
  837. if (bit.band(result, 255) == 0) then
  838. self.flags.Z = true
  839. else
  840. self.flags.Z = false
  841. end
  842. if (bit.band(((bit.band(self.a, 15) - bit.band(operand, 15)) - flagC), 16) ~= 0) then
  843. self.flags.H = true
  844. else
  845. self.flags.H = false
  846. end
  847. if ((bit.band(self.a, 128) ~= bit.band(operand, 128)) and (bit.band(self.a, 128) ~= bit.band(result, 128))) then
  848. self.flags.P = true
  849. else
  850. self.flags.P = false
  851. end
  852. self.flags.N = true
  853. if (bit.band(result, 256) > 0) then
  854. self.flags.C = true
  855. else
  856. self.flags.C = false
  857. end
  858. self.a = bit.band(result, 255)
  859. self:update_xy_flags(self.a)
  860. end
  861. Z80.do_cp = function(self, operand)
  862. local temp = self.a
  863. self:do_sub(operand)
  864. self.a = temp
  865. self:update_xy_flags(operand)
  866. end
  867. Z80.do_and = function(self, operand)
  868. self.a = bit.band(bit.band(self.a, operand), 255)
  869. if (bit.band(self.a, 128) > 0) then
  870. self.flags.S = true
  871. else
  872. self.flags.S = false
  873. end
  874. if (self.a == 0) then
  875. self.flags.Z = true
  876. else
  877. self.flags.Z = false
  878. end
  879. --print(self.flags.Z)
  880. self.flags.H = true
  881. self.flags.P = self:get_parity(self.a)
  882. self.flags.N = false
  883. self.flags.C = false
  884. self:update_xy_flags(self.a)
  885. end
  886. Z80.do_or = function(self, operand)
  887. self.a = bit.band(bit.bor(operand, self.a), 255)
  888.  
  889. if (bit.band(self.a, 128) > 0) then
  890. self.flags.S = true
  891. else
  892. self.flags.S = false
  893. end
  894. if (self.a == 0) then
  895. self.flags.Z = true
  896. else
  897. self.flags.Z = false
  898. end
  899. self.flags.H = false
  900. self.flags.P = self:get_parity(self.a)
  901. self.flags.N = false
  902. self.flags.C = false
  903. self:update_xy_flags(self.a)
  904. end
  905. Z80.do_xor = function(self, operand)
  906. self.a = bit.band(bit.bxor(operand, self.a), 255)
  907. if (bit.band(self.a, 128) > 0) then
  908. self.flags.S = true
  909. else
  910. self.flags.S = false
  911. end
  912.  
  913. if (self.a == 0) then
  914. self.flags.Z = true
  915. else
  916. self.flags.Z = false
  917. end
  918. self.flags.H = false
  919. self.flags.P = self:get_parity(self.a)
  920. self.flags.N = false
  921. self.flags.C = false
  922. self:update_xy_flags(self.a)
  923. end
  924. Z80.do_inc = function(self, operand)
  925. local result = operand + 1
  926. if (bit.band(result, 128) > 0) then
  927. self.flags.S = true
  928. else
  929. self.flags.S = false
  930. end
  931. if (bit.band(result, 255) == 0) then
  932. self.flags.Z = true
  933. else
  934. self.flags.Z = false
  935. end
  936. if ((bit.band(operand, 15) == 15) == true) then
  937. self.flags.H = true
  938. else
  939. self.flags.H = false
  940. end
  941. if ((operand == 127) == true) then
  942. self.flags.P = true
  943. else
  944. self.flags.P = false
  945. end
  946. self.flags.N = false
  947. result = bit.band(result, 255)
  948. self:update_xy_flags(result)
  949. return result
  950. end
  951. Z80.do_dec = function(self, operand)
  952. local result = operand - 1
  953. if (bit.band(result, 128) > 0) then
  954. self.flags.S = true
  955. else
  956. self.flags.S = false
  957. end
  958. if (bit.band(result, 255) == 0) then
  959. self.flags.Z = true
  960. else
  961. self.flags.Z = false
  962. end
  963. if ((bit.band(operand, 15) == 0) == true) then
  964. self.flags.H = true
  965. else
  966. self.flags.H = false
  967. end
  968. if ((operand == 128) == true) then
  969. self.flags.P = true
  970. else
  971. self.flags.P = false
  972. end
  973. self.flags.N = true
  974. result = bit.band(result, 255)
  975. self:update_xy_flags(result)
  976. return result
  977. end
  978. Z80.do_hl_add = function(self, operand)
  979. local hl = bit.bor(self.l, bit.blshift(self.h, 8))
  980. local result = hl + operand
  981. self.flags.N = false
  982. if (bit.band(result, 65536) ~= 0) then
  983. self.flags.C = true
  984. else
  985. self.flags.C = false
  986. end
  987. if (bit.band((bit.band(hl, 4095) + bit.band(operand, 4095)), 4096) ~= 0) then
  988. self.flags.H = true
  989. else
  990. self.flags.H = false
  991. end
  992. self.l = bit.band(result, 255)
  993. self.h = bit.brshift(bit.band(result, 65280), 8)
  994. self:update_xy_flags(self.h)
  995. end
  996. Z80.do_hl_adc = function(self, operand)
  997. local flagC = 0
  998. if (self.flags.C) then
  999. flagC = 1
  1000. end
  1001. operand = operand + flagC
  1002. local hl = bit.bor(self.l, bit.blshift(self.h, 8))
  1003. local result = hl + operand
  1004. if (bit.band(result, 32768) ~= 0) then
  1005. self.flags.S = true
  1006. else
  1007. self.flags.S = false
  1008. end
  1009. if (bit.band(result, 65535) == 0) then
  1010. self.flags.Z = true
  1011. else
  1012. self.flags.Z = false
  1013. end
  1014. if (bit.band((bit.band(hl, 4095) + bit.band(operand, 4095)), 4096) ~= 0) then
  1015. self.flags.H = true
  1016. else
  1017. self.flags.H = false
  1018. end
  1019. if (((bit.band(hl, 32768) == bit.band(operand, 32768)) and (bit.band(result, 32768) ~= bit.band(hl, 32768))) == true) then
  1020. self.flags.P = true
  1021. else
  1022. self.flags.P = false
  1023. end
  1024. self.flags.N = false
  1025. if (bit.band(result, 65536) > 0) then
  1026. self.flags.C = true
  1027. else
  1028. self.flags.C = false
  1029. end
  1030. self.l = bit.band(result, 255)
  1031. self.h = bit.band(bit.brshift(result, 8), 255)
  1032. self:update_xy_flags(self.h)
  1033. end
  1034. Z80.do_hl_sbc = function(self, operand)
  1035. --print(operand)
  1036. local flagC = 0
  1037. if (self.flags.C) then
  1038. flagC = 1
  1039. end
  1040. operand = operand + flagC
  1041. local hl = bit.bor(self.l, bit.blshift(self.h, 8))
  1042. local result = hl - operand
  1043. if (bit.band(result, 32768) ~= 0) then
  1044. self.flags.S = true
  1045. else
  1046. self.flags.S = false
  1047. end
  1048. if (bit.band(result, 65535) == 0) then
  1049. self.flags.Z = true
  1050. else
  1051. self.flags.Z = false
  1052. end
  1053. if (bit.band((bit.band(hl, 4095) - bit.band(operand, 4095)), 4096) ~= 0) then
  1054. self.flags.H = true
  1055. else
  1056. self.flags.H = false
  1057. end
  1058. if (((bit.band(hl, 32768) ~= bit.band(operand, 32768)) and (bit.band(result, 32768) ~= bit.band(hl, 32768))) == true) then
  1059. self.flags.P = true
  1060. else
  1061. self.flags.P = false
  1062. end
  1063. self.flags.N = true
  1064. if (bit.band(result, 65536) > 0) then
  1065. self.flags.C = true
  1066. else
  1067. self.flags.C = false
  1068. end
  1069. self.l = bit.band(result, 255)
  1070. self.h = bit.band(bit.brshift(result, 8), 255)
  1071. self:update_xy_flags(self.h)
  1072. end
  1073. Z80.do_in = function(self, port)
  1074. local result = self.core.io_read(self, port)
  1075. if (bit.band(result, 128) > 0) then
  1076. self.flags.S = true
  1077. else
  1078. self.flags.S = false
  1079. end
  1080. if (result ~= 0) then
  1081. self.flags.Z = false
  1082. else
  1083. self.flags.Z = true
  1084. end
  1085. self.flags.H = false
  1086. if (self:get_parity(result) == true) then
  1087. self.flags.P = true
  1088. else
  1089. self.flags.P = false
  1090. end
  1091. self.flags.N = false
  1092. self:update_xy_flags(result)
  1093. return result
  1094. end
  1095. Z80.do_neg = function(self)
  1096. if self.a ~= 128 then
  1097. self.a = self:get_signed_offset_byte(self.a)
  1098. self.a = bit.band(-self.a, 255)
  1099. end
  1100. if (bit.band(self.a, 128) > 0) then
  1101. self.flags.S = true
  1102. else
  1103. self.flags.S = false
  1104. end
  1105. if (self.a == 0) then
  1106. self.flags.Z = true
  1107. else
  1108. self.flags.Z = false
  1109. end
  1110. if ((bit.band(-self.a, 15) > 0) == true) then
  1111. self.flags.H = true
  1112. else
  1113. self.flags.H = false
  1114. end
  1115. if ((self.a == 128) == true) then
  1116. self.flags.P = true
  1117. else
  1118. self.flags.P = false
  1119. end
  1120. self.flags.N = true
  1121. if (self.a ~= 0) then
  1122. self.flags.C = true
  1123. else
  1124. self.flags.C = false
  1125. end
  1126. self:update_xy_flags(self.a)
  1127. end
  1128. Z80.do_ldi = function(self)
  1129. local read_value = self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8)))
  1130. self.core.mem_write(self, bit.bor(self.e, bit.blshift(self.d, 8)), read_value)
  1131. local result = bit.bor(self.e, bit.blshift(self.d, 8)) + 1
  1132. self.e = bit.band(result, 255)
  1133. self.d = bit.brshift(bit.band(result, 65280), 8)
  1134. result = bit.bor(self.l, bit.blshift(self.h, 8)) + 1
  1135. self.l = bit.band(result, 255)
  1136. self.h = bit.brshift(bit.band(result, 65280), 8)
  1137. result = bit.bor(self.c, bit.blshift(self.b, 8)) - 1
  1138. self.c = bit.band(result, 255)
  1139. self.b = bit.brshift(bit.band(result, 65280), 8)
  1140. self.flags.H = false
  1141. if ((self.c or self.b) ~= 0) then
  1142. self.flags.P = true
  1143. else
  1144. self.flags.P = false
  1145. end
  1146. self.flags.N = false
  1147. if (bit.brshift(bit.band((self.a + read_value), 2), 1) == 1) then
  1148. self.flags.Y = true
  1149. else
  1150. self.flags.Y = false
  1151. end
  1152. if (bit.brshift(bit.band((self.a + read_value), 8), 3) == 1) then
  1153. self.flags.X = true
  1154. else
  1155. self.flags.X = false
  1156. end
  1157. end
  1158. Z80.do_cpi = function(self)
  1159. local temp_carry = self.flags.C
  1160. local read_value = self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8)))
  1161. self:do_cp(read_value)
  1162. self.flags.C = temp_carry
  1163. local flagH = 0
  1164. if self.flags.H then
  1165. flagH = 1
  1166. end
  1167. if (bit.brshift(bit.band(((self.a - read_value) - flagH), 2), 1) == 1) then
  1168. self.flags.Y = true
  1169. else
  1170. self.flags.Y = false
  1171. end
  1172. if (bit.brshift(bit.band(((self.a - read_value) - flagH), 8), 3) == 1) then
  1173. self.flags.X = true
  1174. else
  1175. self.flags.X = false
  1176. end
  1177. local result = bit.bor(self.l, bit.blshift(self.h, 8)) + 1
  1178. self.l = bit.band(result, 255)
  1179. self.h = bit.brshift(bit.band(result, 65280), 8)
  1180. result = bit.bor(self.c, bit.blshift(self.b, 8)) - 1
  1181. self.c = bit.band(result, 255)
  1182. self.b = bit.brshift(bit.band(result, 65280), 8)
  1183. if (result ~= 0) then
  1184. self.flags.P = true
  1185. else
  1186. self.flags.P = false
  1187. end
  1188. end
  1189. Z80.do_ini = function(self)
  1190. self.b = self:do_dec(self.b)
  1191. self.core.mem_write(self, bit.bor(self.l, bit.blshift(self.h, 8)), self.core.io_read(self, bit.bor(bit.blshift(self.b, 8), self.c)))
  1192. local result = bit.bor(self.l, bit.blshift(self.h, 8)) + 1
  1193. self.l = bit.band(result, 255)
  1194. self.h = bit.brshift(bit.band(result, 65280), 8)
  1195. self.flags.N = true
  1196. end
  1197. Z80.do_outi = function(self)
  1198. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8))))
  1199. local result = bit.bor(self.l, bit.blshift(self.h, 8)) + 1
  1200. self.l = bit.band(result, 255)
  1201. self.h = bit.brshift(bit.band(result, 65280), 8)
  1202. self.b = self:do_dec(self.b)
  1203. self.flags.N = true
  1204. end
  1205. Z80.do_ldd = function(self)
  1206. self.flags.N = false
  1207. self.flags.H = false
  1208. local read_value = self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8)))
  1209. self.core.mem_write(self, bit.bor(self.e, bit.blshift(self.d, 8)), read_value)
  1210. local result = bit.bor(self.e, bit.blshift(self.d, 8)) - 1
  1211. self.e = bit.band(result, 255)
  1212. self.d = bit.brshift(bit.band(result, 65280), 8)
  1213. result = bit.bor(self.l, bit.blshift(self.h, 8)) - 1
  1214. self.l = bit.band(result, 255)
  1215. self.h = bit.brshift(bit.band(result, 65280), 8)
  1216. result = bit.bor(self.c, bit.blshift(self.b, 8)) - 1
  1217. self.c = bit.band(result, 255)
  1218. self.b = bit.brshift(bit.band(result, 65280), 8)
  1219. if ((self.c ~= 0) or (self.b ~= 0)) then
  1220. self.flags.P = true
  1221. else
  1222. self.flags.P = false
  1223. end
  1224. if (bit.brshift(bit.band((self.a + read_value), 2), 1) == 1) then
  1225. self.flags.Y = true
  1226. else
  1227. self.flags.Y = false
  1228. end
  1229. if (bit.brshift(bit.band((self.a + read_value), 8), 3) == 1) then
  1230. self.flags.X = true
  1231. else
  1232. self.flags.X = false
  1233. end
  1234. end
  1235. Z80.do_cpd = function(self)
  1236. local temp_carry = self.flags.C
  1237. local read_value = self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8)))
  1238. self:do_cp(read_value)
  1239. self.flags.C = temp_carry
  1240. local flagH = 0
  1241. if self.flags.H then
  1242. flagH = 1
  1243. end
  1244. if (bit.brshift(bit.band(((self.a - read_value) - flagH), 2), 1) == 1) then
  1245. self.flags.Y = true
  1246. else
  1247. self.flags.Y = false
  1248. end
  1249. if (bit.brshift(bit.band(((self.a - read_value) - flagH), 8), 3) == 1) then
  1250. self.flags.X = true
  1251. else
  1252. self.flags.X = false
  1253. end
  1254. local result = bit.bor(self.l, bit.blshift(self.h, 8)) - 1
  1255. self.l = bit.band(result, 255)
  1256. self.h = bit.brshift(bit.band(result, 65280), 8)
  1257. result = bit.bor(self.c, bit.blshift(self.b, 8)) - 1
  1258. self.c = bit.band(result, 255)
  1259. self.b = bit.brshift(bit.band(result, 65280), 8)
  1260. if (result ~= 0) then
  1261. self.flags.P = true
  1262. else
  1263. self.flags.P = false
  1264. end
  1265. end
  1266. Z80.do_ind = function(self)
  1267. self.b = self:do_dec(self.b)
  1268. self.core.mem_write(self, bit.bor(self.l, bit.blshift(self.h, 8)), self.core.io_read(self, bit.bor(bit.blshift(self.b, 8), self.c)))
  1269. local result = bit.bor(self.l, bit.blshift(self.h, 8)) - 1
  1270. self.l = bit.band(result, 255)
  1271. self.h = bit.brshift(bit.band(result, 65280), 8)
  1272. self.flags.N = true
  1273. end
  1274. Z80.do_outd = function(self)
  1275. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8))))
  1276. local result = bit.bor(self.l, bit.blshift(self.h, 8)) - 1
  1277. self.l = bit.band(result, 255)
  1278. self.h = bit.brshift(bit.band(result, 65280), 8)
  1279. self.b = self:do_dec(self.b)
  1280. self.flags.N = true
  1281. end
  1282. Z80.do_rlc = function(self, operand)
  1283. self.flags.N = false
  1284. self.flags.H = false
  1285. if (bit.brshift(bit.band(operand, 128), 7) ~= 0) then
  1286. self.flags.C = true
  1287. else
  1288. self.flags.C = false
  1289. end
  1290. local flagC = 0
  1291. if self.flags.C == true then
  1292. flagC = 1
  1293. end
  1294. operand = bit.band(bit.bor(bit.blshift(operand, 1), flagC), 255)
  1295.  
  1296. if (operand ~= 0) then
  1297. self.flags.Z = false
  1298. else
  1299. self.flags.Z = true
  1300. end
  1301.  
  1302. self.flags.P = self:get_parity(operand)
  1303.  
  1304. if (bit.band(operand, 128) > 0) then
  1305. self.flags.S = true
  1306. else
  1307. self.flags.S = false
  1308. end
  1309. self:update_xy_flags(operand)
  1310.  
  1311. return operand
  1312. end
  1313. Z80.do_rrc = function(self, operand)
  1314. self.flags.N = false
  1315. self.flags.H = false
  1316. if (bit.band(operand, 1) ~= 0) then
  1317. self.flags.C = true
  1318. else
  1319. self.flags.C = false
  1320. end
  1321. local flagC = 0
  1322. if self.flags.C == true then
  1323. flagC = 1
  1324. end
  1325. operand = bit.bor(bit.band(bit.brshift(operand, 1), 127), bit.blshift(flagC, 7))
  1326. if (bit.band(operand, 255) ~= 0) then
  1327. self.flags.Z = false
  1328. else
  1329. self.flags.Z = true
  1330. end
  1331. self.flags.P = self:get_parity(operand)
  1332. if (bit.band(operand, 128) > 0) then
  1333. self.flags.S = true
  1334. else
  1335. self.flags.S = false
  1336. end
  1337. self:update_xy_flags(operand)
  1338.  
  1339. return bit.band(operand, 255)
  1340. end
  1341. Z80.do_rl = function(self, operand)
  1342. self.flags.N = false
  1343. self.flags.H = false
  1344. local temp = 0
  1345. if self.flags.C then temp = 1 end
  1346. if (bit.brshift(bit.band(operand, 128), 7) ~= 0) then
  1347. self.flags.C = true
  1348. else
  1349. self.flags.C = false
  1350. end
  1351. operand = bit.band(bit.bor(bit.blshift(operand, 1), temp), 255)
  1352. if (operand ~= 0) then
  1353. self.flags.Z = false
  1354. else
  1355. self.flags.Z = true
  1356. end
  1357. self.flags.P = self:get_parity(operand)
  1358. if (bit.band(operand, 128) > 0) then
  1359. self.flags.S = true
  1360. else
  1361. self.flags.S = false
  1362. end
  1363. self:update_xy_flags(operand)
  1364.  
  1365. return operand
  1366. end
  1367. Z80.do_rr = function(self, operand)
  1368. self.flags.N = false
  1369. self.flags.H = false
  1370. local temp = 0
  1371. if self.flags.C then temp = 1 end
  1372. if (bit.band(operand, 1) ~= 0) then
  1373. self.flags.C = true
  1374. else
  1375. self.flags.C = false
  1376. end
  1377. operand = bit.bor(bit.band(bit.brshift(operand, 1), 127), bit.blshift(temp, 7))
  1378. if (operand ~= 0) then
  1379. self.flags.Z = false
  1380. else
  1381. self.flags.Z = true
  1382. end
  1383. self.flags.P = self:get_parity(operand)
  1384. if (bit.band(operand, 128) > 0) then
  1385. self.flags.S = true
  1386. else
  1387. self.flags.S = false
  1388. end
  1389. self:update_xy_flags(operand)
  1390.  
  1391. --os.exit()
  1392. return operand
  1393. end
  1394. Z80.do_sla = function(self, operand)
  1395. self.flags.N = false
  1396. self.flags.H = false
  1397. if (bit.brshift(bit.band(operand, 128), 7) ~= 0) then
  1398. self.flags.C = true
  1399. else
  1400. self.flags.C = false
  1401. end
  1402. operand = bit.band(bit.blshift(operand, 1), 255)
  1403. if (not (operand > 0)) then
  1404. self.flags.Z = true
  1405. else
  1406. self.flags.Z = false
  1407. end
  1408. self.flags.P = self:get_parity(operand)
  1409. if (bit.band(operand, 128) > 0) then
  1410. self.flags.S = true
  1411. else
  1412. self.flags.S = false
  1413. end
  1414. self:update_xy_flags(operand)
  1415. return operand
  1416. end
  1417. Z80.do_sra = function(self, operand)
  1418. self.flags.N = false
  1419. self.flags.H = false
  1420. if (bit.band(operand, 1) ~= 0) then
  1421. self.flags.C = true
  1422. else
  1423. self.flags.C = false
  1424. end
  1425. operand = bit.bor(bit.band(bit.brshift(operand, 1), 127), bit.band(operand, 128))
  1426. if (not (operand > 0)) then
  1427. self.flags.Z = true
  1428. else
  1429. self.flags.Z = false
  1430. end
  1431. self.flags.P = self:get_parity(operand)
  1432. if (bit.band(operand, 128) > 0) then
  1433. self.flags.S = true
  1434. else
  1435. self.flags.S = false
  1436. end
  1437. self:update_xy_flags(operand)
  1438. return operand
  1439. end
  1440. Z80.do_sll = function(self, operand)
  1441. self.flags.N = false
  1442. self.flags.H = false
  1443. if (bit.brshift(bit.band(operand, 128), 7) ~= 0) then
  1444. self.flags.C = true
  1445. else
  1446. self.flags.C = false
  1447. end
  1448. operand = bit.bor(bit.band(bit.blshift(operand, 1), 255), 1)
  1449. if (not (operand > 0)) then
  1450. self.flags.Z = true
  1451. else
  1452. self.flags.Z = false
  1453. end
  1454. self.flags.P = self:get_parity(operand)
  1455. if (bit.band(operand, 128) > 0) then
  1456. self.flags.S = true
  1457. else
  1458. self.flags.S = false
  1459. end
  1460. self:update_xy_flags(operand)
  1461. return operand
  1462. end
  1463. Z80.do_srl = function(self, operand)
  1464. self.flags.N = false
  1465. self.flags.H = false
  1466. if (bit.band(operand, 1) ~= 0) then
  1467. self.flags.C = true
  1468. else
  1469. self.flags.C = false
  1470. end
  1471. operand = bit.band(bit.brshift(operand, 1), 127)
  1472. if (not (operand > 0)) then
  1473. self.flags.Z = true
  1474. else
  1475. self.flags.Z = false
  1476. end
  1477. self.flags.P = self:get_parity(operand)
  1478. self.flags.S = 0
  1479. self:update_xy_flags(operand)
  1480. return operand
  1481. end
  1482. Z80.do_ix_add = function(self, operand)
  1483. self.flags.N = false
  1484. local result = self.ix + operand
  1485. if (bit.band(result, 65536) ~= 0) then
  1486. self.flags.C = true
  1487. else
  1488. self.flags.C = false
  1489. end
  1490. if (bit.band((bit.band(self.ix, 4095) + bit.band(operand, 4095)), 4096) ~= 0) then
  1491. self.flags.H = true
  1492. else
  1493. self.flags.H = false
  1494. end
  1495. self:update_xy_flags(bit.brshift(bit.band(result, 65280), 8))
  1496. self.ix = result
  1497. end
  1498. Z80.instructions = {}
  1499. Z80.instructions[0] = function(self)
  1500. end
  1501. Z80.instructions[1] = function(self)
  1502. self.pc = bit.band((self.pc + 1), 65535)
  1503. self.c = self.core.mem_read(self, self.pc)
  1504. self.pc = bit.band((self.pc + 1), 65535)
  1505. self.b = self.core.mem_read(self, self.pc)
  1506. end
  1507. Z80.instructions[2] = function(self)
  1508. self.core.mem_write(self, bit.bor(self.c, bit.blshift(self.b, 8)), self.a)
  1509. end
  1510. Z80.instructions[3] = function(self)
  1511. local result = bit.bor(self.c, bit.blshift(self.b, 8))
  1512. result = result + 1
  1513. self.c = bit.band(result, 255)
  1514. self.b = bit.brshift(bit.band(result, 65280), 8)
  1515. end
  1516. Z80.instructions[4] = function(self)
  1517. self.b = self:do_inc(self.b)
  1518. end
  1519. Z80.instructions[5] = function(self)
  1520. self.b = self:do_dec(self.b)
  1521. end
  1522. Z80.instructions[6] = function(self)
  1523. self.pc = bit.band((self.pc + 1), 65535)
  1524. self.b = self.core.mem_read(self, self.pc)
  1525. end
  1526. Z80.instructions[7] = function(self)
  1527. local temp_s = self.flags.S
  1528. temp_z = self.flags.Z
  1529. temp_p = self.flags.P
  1530. self.a = self:do_rlc(self.a)
  1531. self.flags.S = temp_s
  1532. self.flags.Z = temp_z
  1533. self.flags.P = temp_p
  1534. end
  1535. Z80.instructions[8] = function(self)
  1536. local temp = self.a
  1537. self.a = self.a_prime
  1538. self.a_prime = temp
  1539. temp = self:get_flags_register()
  1540. self:set_flags_register(self:get_flags_prime())
  1541. self:set_flags_prime(temp)
  1542. end
  1543. Z80.instructions[9] = function(self)
  1544. self:do_hl_add(bit.bor(self.c, bit.blshift(self.b, 8)))
  1545. end
  1546. Z80.instructions[10] = function(self)
  1547. self.a = self.core.mem_read(self, bit.bor(self.c, bit.blshift(self.b, 8)))
  1548. end
  1549. Z80.instructions[11] = function(self)
  1550. local result = bit.bor(self.c, bit.blshift(self.b, 8))
  1551. result = result - 1
  1552. self.c = bit.band(result, 255)
  1553. self.b = bit.brshift(bit.band(result, 65280), 8)
  1554. end
  1555. Z80.instructions[12] = function(self)
  1556. self.c = self:do_inc(self.c)
  1557. end
  1558. Z80.instructions[13] = function(self)
  1559. self.c = self:do_dec(self.c)
  1560. end
  1561. Z80.instructions[14] = function(self)
  1562. self.pc = bit.band((self.pc + 1), 65535)
  1563. self.c = self.core.mem_read(self, self.pc)
  1564. end
  1565. Z80.instructions[15] = function(self)
  1566. local temp_s, temp_z, temp_p = self.flags.S, self.flags.Z, self.flags.P
  1567. self.a = self:do_rrc(self.a)
  1568. self.flags.S = temp_s
  1569. self.flags.Z = temp_z
  1570. self.flags.P = temp_p
  1571. end
  1572. Z80.instructions[16] = function(self)
  1573. self.b = bit.band((self.b - 1), 255)
  1574. self:do_conditional_relative_jump((self.b ~= 0))
  1575. end
  1576. Z80.instructions[17] = function(self)
  1577. self.pc = bit.band((self.pc + 1), 65535)
  1578. self.e = self.core.mem_read(self, self.pc)
  1579. self.pc = bit.band((self.pc + 1), 65535)
  1580. self.d = self.core.mem_read(self, self.pc)
  1581. end
  1582. Z80.instructions[18] = function(self)
  1583. self.core.mem_write(self, bit.bor(self.e, bit.blshift(self.d, 8)), self.a)
  1584. end
  1585. Z80.instructions[19] = function(self)
  1586. local result = bit.bor(self.e, bit.blshift(self.d, 8))
  1587. result = result + 1
  1588. self.e = bit.band(result, 255)
  1589. self.d = bit.brshift(bit.band(result, 65280), 8)
  1590. end
  1591. Z80.instructions[20] = function(self)
  1592. self.d = self:do_inc(self.d)
  1593. end
  1594. Z80.instructions[21] = function(self)
  1595. self.d = self:do_dec(self.d)
  1596. end
  1597. Z80.instructions[22] = function(self)
  1598. self.pc = bit.band((self.pc + 1), 65535)
  1599. self.d = self.core.mem_read(self, self.pc)
  1600. end
  1601. Z80.instructions[23] = function(self)
  1602. local temp_s, temp_z, temp_p = self.flags.S, self.flags.Z, self.flags.P
  1603. self.a = self:do_rl(self.a)
  1604. self.flags.S = temp_s
  1605. self.flags.Z = temp_z
  1606. self.flags.P = temp_p
  1607. end
  1608. Z80.instructions[24] = function(self)
  1609. local offset = self:get_signed_offset_byte(self.core.mem_read(self, bit.band((self.pc + 1), 65535)))
  1610. self.pc = bit.band(((self.pc + offset) + 1), 65535)
  1611. end
  1612. Z80.instructions[25] = function(self)
  1613. self:do_hl_add(bit.bor(self.e, bit.blshift(self.d, 8)))
  1614. end
  1615. Z80.instructions[26] = function(self)
  1616. self.a = self.core.mem_read(self, bit.bor(self.e, bit.blshift(self.d, 8)))
  1617. end
  1618. Z80.instructions[27] = function(self)
  1619. local result = bit.bor(self.e, bit.blshift(self.d, 8))
  1620. result = result - 1
  1621. self.e = bit.band(result, 255)
  1622. self.d = bit.brshift(bit.band(result, 65280), 8)
  1623. end
  1624. Z80.instructions[28] = function(self)
  1625. self.e = self:do_inc(self.e)
  1626. end
  1627. Z80.instructions[29] = function(self)
  1628. self.e = self:do_dec(self.e)
  1629. end
  1630. Z80.instructions[30] = function(self)
  1631. self.pc = bit.band((self.pc + 1), 65535)
  1632. self.e = self.core.mem_read(self, self.pc)
  1633. end
  1634. Z80.instructions[31] = function(self)
  1635. local temp_s, temp_z, temp_p = self.flags.S, self.flags.Z, self.flags.P
  1636. self.a = self:do_rr(self.a)
  1637. self.flags.S = temp_s
  1638. self.flags.Z = temp_z
  1639. self.flags.P = temp_p
  1640. end
  1641. Z80.instructions[32] = function(self)
  1642. self:do_conditional_relative_jump(not self.flags.Z)
  1643. end
  1644. Z80.instructions[33] = function(self)
  1645. self.pc = bit.band((self.pc + 1), 65535)
  1646. self.l = self.core.mem_read(self, self.pc)
  1647. self.pc = bit.band((self.pc + 1), 65535)
  1648. self.h = self.core.mem_read(self, self.pc)
  1649. end
  1650. Z80.instructions[34] = function(self)
  1651. self.pc = bit.band((self.pc + 1), 65535)
  1652. local address = self.core.mem_read(self, self.pc)
  1653. self.pc = bit.band((self.pc + 1), 65535)
  1654. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  1655. self.core.mem_write(self, address, self.l)
  1656. self.core.mem_write(self, bit.band((address + 1), 65535), self.h)
  1657. end
  1658. Z80.instructions[35] = function(self)
  1659. local result = bit.bor(self.l, bit.blshift(self.h, 8))
  1660. result = result + 1
  1661. self.l = bit.band(result, 255)
  1662. self.h = bit.brshift(bit.band(result, 65280), 8)
  1663. end
  1664. Z80.instructions[36] = function(self)
  1665. self.h = self:do_inc(self.h)
  1666. end
  1667. Z80.instructions[37] = function(self)
  1668. self.h = self:do_dec(self.h)
  1669. end
  1670. Z80.instructions[38] = function(self)
  1671. self.pc = bit.band((self.pc + 1), 65535)
  1672. self.h = self.core.mem_read(self, self.pc)
  1673. end
  1674. Z80.instructions[39] = function(self)
  1675. local temp = self.a
  1676. if not self.flags.N then
  1677. if self.flags.H or (bit.band(self.a, 15) > 9) then
  1678. temp = temp + 6
  1679. end
  1680. if self.flags.C or (self.a > 153) then
  1681. temp = temp + 96
  1682. end
  1683. else
  1684. if self.flags.H or (bit.band(self.a, 15) > 9) then
  1685. temp = temp - 6
  1686. end
  1687. if self.flags.C or (self.a > 153) then
  1688. temp = temp - 96
  1689. end
  1690. end
  1691. if (bit.band(temp, 128) > 0) then
  1692. self.flags.S = true
  1693. else
  1694. self.flags.S = false
  1695. end
  1696. if ((bit.band(temp, 255) == 0)) then
  1697. self.flags.Z = true
  1698. else
  1699. self.flags.Z = false
  1700. end
  1701. if (bit.bxor(bit.band(self.a, 16), bit.band(temp, 16)) ~= 0) then
  1702. self.flags.H = true
  1703. else
  1704. self.flags.H = false
  1705. end
  1706. self.flags.P = self:get_parity(bit.band(temp, 255))
  1707. if ((self.flags.C or (self.a > 153)) == true) then
  1708. self.flags.C = true
  1709. else
  1710. self.flags.C = false
  1711. end
  1712. self.a = bit.band(temp, 255)
  1713. self:update_xy_flags(self.a)
  1714. end
  1715. Z80.instructions[40] = function(self)
  1716. self:do_conditional_relative_jump(self.flags.Z)
  1717. end
  1718. Z80.instructions[41] = function(self)
  1719. self:do_hl_add(bit.bor(self.l, bit.blshift(self.h, 8)))
  1720. end
  1721. Z80.instructions[42] = function(self)
  1722. self.pc = bit.band((self.pc + 1), 65535)
  1723. local address = self.core.mem_read(self, self.pc)
  1724. self.pc = bit.band((self.pc + 1), 65535)
  1725. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  1726. self.l = self.core.mem_read(self, address)
  1727. self.h = self.core.mem_read(self, bit.band((address + 1), 65535))
  1728. end
  1729. Z80.instructions[43] = function(self)
  1730. local result = bit.bor(self.l, bit.blshift(self.h, 8))
  1731. result = result - 1
  1732. self.l = bit.band(result, 255)
  1733. self.h = bit.brshift(bit.band(result, 65280), 8)
  1734. end
  1735. Z80.instructions[44] = function(self)
  1736. self.l = self:do_inc(self.l)
  1737. end
  1738. Z80.instructions[45] = function(self)
  1739. self.l = self:do_dec(self.l)
  1740. end
  1741. Z80.instructions[46] = function(self)
  1742. self.pc = bit.band((self.pc + 1), 65535)
  1743. self.l = self.core.mem_read(self, self.pc)
  1744. end
  1745. Z80.instructions[47] = function(self)
  1746. self.a = bit.band(bit.bnot(self.a), 255)
  1747. self.flags.N = true
  1748. self.flags.H = true
  1749. self:update_xy_flags(self.a)
  1750. end
  1751. Z80.instructions[48] = function(self)
  1752. self:do_conditional_relative_jump(not self.flags.C)
  1753. end
  1754. Z80.instructions[49] = function(self)
  1755. self.sp =
  1756. bit.bor(
  1757. self.core.mem_read(self, bit.band((self.pc + 1), 65535)),
  1758. bit.blshift(self.core.mem_read(self, bit.band((self.pc + 2), 65535)), 8)
  1759. )
  1760. self.pc = bit.band((self.pc + 2), 65535)
  1761. end
  1762. Z80.instructions[50] = function(self)
  1763. self.pc = bit.band((self.pc + 1), 65535)
  1764. local address = self.core.mem_read(self, self.pc)
  1765. self.pc = bit.band((self.pc + 1), 65535)
  1766. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  1767. self.core.mem_write(self, address, self.a)
  1768. end
  1769. Z80.instructions[51] = function(self)
  1770. self.sp = bit.band((self.sp + 1), 65535)
  1771. end
  1772. Z80.instructions[52] = function(self)
  1773. local address = bit.bor(self.l, bit.blshift(self.h, 8))
  1774. self.core.mem_write(self, address, self:do_inc(self.core.mem_read(self, address)))
  1775. end
  1776. Z80.instructions[53] = function(self)
  1777. local address = bit.bor(self.l, bit.blshift(self.h, 8))
  1778. self.core.mem_write(self, address, self:do_dec(self.core.mem_read(self, address)))
  1779. end
  1780. Z80.instructions[54] = function(self)
  1781. self.pc = bit.band((self.pc + 1), 65535)
  1782. self.core.mem_write(self, bit.bor(self.l, bit.blshift(self.h, 8)), self.core.mem_read(self, self.pc))
  1783. end
  1784. Z80.instructions[55] = function(self)
  1785. self.flags.N = false
  1786. self.flags.H = false
  1787. self.flags.C = true
  1788. self:update_xy_flags(self.a)
  1789. end
  1790. Z80.instructions[56] = function(self)
  1791. self:do_conditional_relative_jump(self.flags.C)
  1792. end
  1793. Z80.instructions[57] = function(self)
  1794. self:do_hl_add(self.sp)
  1795. end
  1796. Z80.instructions[58] = function(self)
  1797. self.pc = bit.band((self.pc + 1), 65535)
  1798. local address = self.core.mem_read(self, self.pc)
  1799. self.pc = bit.band((self.pc + 1), 65535)
  1800. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  1801. self.a = self.core.mem_read(self, address)
  1802. end
  1803. Z80.instructions[59] = function(self)
  1804. self.sp = bit.band((self.sp - 1), 65535)
  1805. end
  1806. Z80.instructions[60] = function(self)
  1807. self.a = self:do_inc(self.a)
  1808. end
  1809. Z80.instructions[61] = function(self)
  1810. self.a = self:do_dec(self.a)
  1811. end
  1812. Z80.instructions[62] = function(self)
  1813. self.a = self.core.mem_read(self, bit.band((self.pc + 1), 65535))
  1814. self.pc = bit.band((self.pc + 1), 65535)
  1815. end
  1816. Z80.instructions[63] = function(self)
  1817. self.flags.N = false
  1818. self.flags.H = self.flags.C
  1819. if (self.flags.C == true) then
  1820. self.flags.C = false
  1821. else
  1822. self.flags.C = true
  1823. end
  1824. self:update_xy_flags(self.a)
  1825. end
  1826. Z80.instructions[192] = function(self)
  1827. self:do_conditional_return(not self.flags.Z)
  1828. end
  1829. Z80.instructions[193] = function(self)
  1830. local result = self:pop_word()
  1831. self.c = bit.band(result, 255)
  1832. self.b = bit.brshift(bit.band(result, 65280), 8)
  1833. end
  1834. Z80.instructions[194] = function(self)
  1835. self:do_conditional_absolute_jump(not self.flags.Z)
  1836. end
  1837. Z80.instructions[195] = function(self)
  1838. self.pc =
  1839. bit.bor(
  1840. self.core.mem_read(self, bit.band((self.pc + 1), 65535)),
  1841. bit.blshift(self.core.mem_read(self, bit.band((self.pc + 2), 65535)), 8)
  1842. )
  1843. self.pc = bit.band((self.pc - 1), 65535)
  1844. end
  1845. Z80.instructions[196] = function(self)
  1846. self:do_conditional_call(not self.flags.Z)
  1847. end
  1848. Z80.instructions[197] = function(self)
  1849. self:push_word(bit.bor(self.c, bit.blshift(self.b, 8)))
  1850. end
  1851. Z80.instructions[198] = function(self)
  1852. self.pc = bit.band((self.pc + 1), 65535)
  1853. self:do_add(self.core.mem_read(self, self.pc))
  1854. end
  1855. Z80.instructions[199] = function(self)
  1856. self:do_reset(0)
  1857. end
  1858. Z80.instructions[200] = function(self)
  1859. self:do_conditional_return(self.flags.Z)
  1860. end
  1861. Z80.instructions[201] = function(self)
  1862. self.pc = bit.band((self:pop_word() - 1), 65535)
  1863. end
  1864. Z80.instructions[202] = function(self)
  1865. self:do_conditional_absolute_jump(self.flags.Z)
  1866. end
  1867. Z80.instructions[203] = function(self)
  1868. self.r = bit.bor(bit.band(self.r, 128), bit.band((bit.band(self.r, 127) + 1), 127))
  1869. self.pc = bit.band((self.pc + 1), 65535)
  1870. local opcode = self.core.mem_read(self, self.pc)
  1871. local bit_number = bit.brshift(bit.band(opcode, 56), 3)
  1872. local reg_code = bit.band(opcode, 7)
  1873. if opcode < 64 then
  1874. local op_array = {
  1875. self.do_rlc,
  1876. self.do_rrc,
  1877. self.do_rl,
  1878. self.do_rr,
  1879. self.do_sla,
  1880. self.do_sra,
  1881. self.do_sll,
  1882. self.do_srl
  1883. }
  1884. if reg_code == 0 then
  1885. self.b = op_array[bit_number + 1](self, self.b)
  1886. elseif reg_code == 1 then
  1887. self.c = op_array[bit_number + 1](self, self.c)
  1888. elseif reg_code == 2 then
  1889. self.d = op_array[bit_number + 1](self, self.d)
  1890. elseif reg_code == 3 then
  1891. self.e = op_array[bit_number + 1](self, self.e)
  1892. elseif reg_code == 4 then
  1893. self.h = op_array[bit_number + 1](self, self.h)
  1894. elseif reg_code == 5 then
  1895. self.l = op_array[bit_number + 1](self, self.l)
  1896. elseif reg_code == 6 then
  1897. self.core.mem_write(
  1898. self,
  1899. bit.bor(self.l, bit.blshift(self.h, 8)),
  1900. op_array[bit_number + 1](self, self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8))))
  1901. )
  1902. elseif reg_code == 7 then
  1903. self.a = op_array[bit_number + 1](self, self.a)
  1904. end
  1905. elseif opcode < 128 then
  1906.  
  1907. if reg_code == 0 then
  1908. if (not (bit.band(self.b, bit.blshift(1, bit_number)) ~= 0)) then
  1909. self.flags.Z = true
  1910. else
  1911. self.flags.Z = false
  1912. end
  1913. elseif reg_code == 1 then
  1914. if (not (bit.band(self.c, bit.blshift(1, bit_number)) ~= 0)) then
  1915. self.flags.Z = true
  1916. else
  1917. self.flags.Z = false
  1918. end
  1919. elseif reg_code == 2 then
  1920. if (not (bit.band(self.d, bit.blshift(1, bit_number)) ~= 0)) then
  1921. self.flags.Z = true
  1922. else
  1923. self.flags.Z = false
  1924. end
  1925. elseif reg_code == 3 then
  1926. if (not (bit.band(self.e, bit.blshift(1, bit_number)) ~= 0)) then
  1927. self.flags.Z = true
  1928. else
  1929. self.flags.Z = false
  1930. end
  1931. elseif reg_code == 4 then
  1932. if (not (bit.band(self.h, bit.blshift(1, bit_number)) ~= 0)) then
  1933. self.flags.Z = true
  1934. else
  1935. self.flags.Z = false
  1936. end
  1937. elseif reg_code == 5 then
  1938. if (not (bit.band(self.l, bit.blshift(1, bit_number)) ~= 0)) then
  1939. self.flags.Z = true
  1940. else
  1941. self.flags.Z = false
  1942. end
  1943. elseif reg_code == 6 then
  1944. if (not (bit.band(self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8))), bit.blshift(1, bit_number)) ~= 0)) then
  1945. self.flags.Z = true
  1946. else
  1947. self.flags.Z = false
  1948. end
  1949. elseif reg_code == 7 then
  1950. if (not (bit.band(self.a, bit.blshift(1, bit_number)) ~= 0)) then
  1951. self.flags.Z = true
  1952. else
  1953. self.flags.Z = false
  1954. end
  1955. end
  1956. self.flags.N = false
  1957. self.flags.H = true
  1958. self.flags.P = self.flags.Z
  1959. if (((bit_number == 7) and not self.flags.Z)) then
  1960. self.flags.S = true
  1961. else
  1962. self.flags.S = false
  1963. end
  1964. if (((bit_number == 5) and not self.flags.Z)) then
  1965. self.flags.Y = true
  1966. else
  1967. self.flags.Y = false
  1968. end
  1969. if (((bit_number == 3) and not self.flags.Z)) then
  1970. self.flags.X = true
  1971. else
  1972. self.flags.X = false
  1973. end
  1974. elseif opcode < 192 then
  1975. if reg_code == 0 then
  1976. self.b = bit.band(bit.band(self.b, 255), bit.bnot(bit.blshift(1, bit_number)))
  1977. elseif reg_code == 1 then
  1978. self.c = bit.band(bit.band(self.c, 255), bit.bnot(bit.blshift(1, bit_number)))
  1979. elseif reg_code == 2 then
  1980. self.d = bit.band(bit.band(self.d, 255), bit.bnot(bit.blshift(1, bit_number)))
  1981. elseif reg_code == 3 then
  1982. self.e = bit.band(bit.band(self.e, 255), bit.bnot(bit.blshift(1, bit_number)))
  1983. elseif reg_code == 4 then
  1984. self.h = bit.band(bit.band(self.h, 255), bit.bnot(bit.blshift(1, bit_number)))
  1985. elseif reg_code == 5 then
  1986. self.l = bit.band(bit.band(self.l, 255), bit.bnot(bit.blshift(1, bit_number)))
  1987. elseif reg_code == 6 then
  1988. self.core.mem_write(
  1989. self,
  1990. bit.bor(self.l, bit.blshift(self.h, 8)),
  1991. bit.band(self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8))), bit.bnot(bit.blshift(1, bit_number)))
  1992. )
  1993. elseif reg_code == 7 then
  1994. self.a = bit.band(bit.band(self.a, 255), bit.bnot(bit.blshift(1, bit_number)))
  1995. end
  1996. else
  1997. if reg_code == 0 then
  1998. self.b = bit.bor(self.b, bit.blshift(1, bit_number))
  1999. elseif reg_code == 1 then
  2000. self.c = bit.bor(self.c, bit.blshift(1, bit_number))
  2001. elseif reg_code == 2 then
  2002. self.d = bit.bor(self.d, bit.blshift(1, bit_number))
  2003. elseif reg_code == 3 then
  2004. self.e = bit.bor(self.e, bit.blshift(1, bit_number))
  2005. elseif reg_code == 4 then
  2006. self.h = bit.bor(self.h, bit.blshift(1, bit_number))
  2007. elseif reg_code == 5 then
  2008. self.l = bit.bor(self.l, bit.blshift(1, bit_number))
  2009. elseif reg_code == 6 then
  2010. self.core.mem_write(
  2011. self,
  2012. bit.bor(self.l, bit.blshift(self.h, 8)),
  2013. bit.bor(self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8))), bit.blshift(1, bit_number))
  2014. )
  2015. elseif reg_code == 7 then
  2016. self.a = bit.bor(self.a, bit.blshift(1, bit_number))
  2017. end
  2018. end
  2019. self.cycle_counter = self.cycle_counter + self.cycle_counts_cb[opcode]
  2020. end
  2021. Z80.instructions[204] = function(self)
  2022. self:do_conditional_call(self.flags.Z)
  2023. end
  2024. Z80.instructions[205] = function(self)
  2025. self:push_word(bit.band((self.pc + 3), 65535))
  2026. self.pc =
  2027. bit.bor(
  2028. self.core.mem_read(self, bit.band((self.pc + 1), 65535)),
  2029. bit.blshift(self.core.mem_read(self, bit.band((self.pc + 2), 65535)), 8)
  2030. )
  2031. self.pc = bit.band((self.pc - 1), 65535)
  2032. end
  2033. Z80.instructions[206] = function(self)
  2034. self.pc = bit.band((self.pc + 1), 65535)
  2035. self:do_adc(self.core.mem_read(self, self.pc))
  2036. end
  2037. Z80.instructions[207] = function(self)
  2038. self:do_reset(8)
  2039. end
  2040. Z80.instructions[208] = function(self)
  2041. self:do_conditional_return(not self.flags.C)
  2042. end
  2043. Z80.instructions[209] = function(self)
  2044. local result = self:pop_word()
  2045. self.e = bit.band(result, 255)
  2046. self.d = bit.brshift(bit.band(result, 65280), 8)
  2047. end
  2048. Z80.instructions[210] = function(self)
  2049. self:do_conditional_absolute_jump(not self.flags.C)
  2050. end
  2051. Z80.instructions[211] = function(self)
  2052. self.pc = bit.band((self.pc + 1), 65535)
  2053. self.core.io_write(self, bit.bor(bit.blshift(self.a, 8), self.core.mem_read(self, self.pc)), self.a)
  2054. end
  2055. Z80.instructions[212] = function(self)
  2056. self:do_conditional_call(not self.flags.C)
  2057. end
  2058. Z80.instructions[213] = function(self)
  2059. self:push_word(bit.bor(self.e, bit.blshift(self.d, 8)))
  2060. end
  2061. Z80.instructions[214] = function(self)
  2062. self.pc = bit.band((self.pc + 1), 65535)
  2063. self:do_sub(self.core.mem_read(self, self.pc))
  2064. end
  2065. Z80.instructions[215] = function(self)
  2066. self:do_reset(16)
  2067. end
  2068. Z80.instructions[216] = function(self)
  2069. self:do_conditional_return(self.flags.C)
  2070. end
  2071. Z80.instructions[217] = function(self)
  2072. local temp = self.b
  2073. self.b = self.b_prime
  2074. self.b_prime = temp
  2075. temp = self.c
  2076. self.c = self.c_prime
  2077. self.c_prime = temp
  2078. temp = self.d
  2079. self.d = self.d_prime
  2080. self.d_prime = temp
  2081. temp = self.e
  2082. self.e = self.e_prime
  2083. self.e_prime = temp
  2084. temp = self.h
  2085. self.h = self.h_prime
  2086. self.h_prime = temp
  2087. temp = self.l
  2088. self.l = self.l_prime
  2089. self.l_prime = temp
  2090. end
  2091. Z80.instructions[218] = function(self)
  2092. self:do_conditional_absolute_jump(self.flags.C)
  2093. end
  2094. Z80.instructions[219] = function(self)
  2095. self.pc = bit.band((self.pc + 1), 65535)
  2096. self.a = self.core.io_read(self, bit.bor(bit.blshift(self.a, 8), self.core.mem_read(self, self.pc)))
  2097. end
  2098. Z80.instructions[220] = function(self)
  2099. self:do_conditional_call(self.flags.C)
  2100. end
  2101. Z80.instructions[221] = function(self)
  2102. self.r = bit.bor(bit.band(self.r, 128), bit.band((bit.band(self.r, 127) + 1), 127))
  2103. self.pc = bit.band((self.pc + 1), 65535)
  2104. local opcode = self.core.mem_read(self, self.pc)
  2105. local func = self.dd_instructions[opcode]
  2106. if func then
  2107. func(self)
  2108. self.cycle_counter = self.cycle_counter + self.cycle_counts_dd[opcode]
  2109. else
  2110. self.pc = bit.band((self.pc - 1), 65535)
  2111. self.cycle_counter = self.cycle_counter + self.cycle_counts[0]
  2112. end
  2113. end
  2114. Z80.instructions[222] = function(self)
  2115. self.pc = bit.band((self.pc + 1), 65535)
  2116. self:do_sbc(self.core.mem_read(self, self.pc))
  2117. end
  2118. Z80.instructions[223] = function(self)
  2119. self:do_reset(24)
  2120. end
  2121. Z80.instructions[224] = function(self)
  2122. self:do_conditional_return(not self.flags.P)
  2123. end
  2124. Z80.instructions[225] = function(self)
  2125. local result = self:pop_word()
  2126. self.l = bit.band(result, 255)
  2127. self.h = bit.brshift(bit.band(result, 65280), 8)
  2128. end
  2129. Z80.instructions[226] = function(self)
  2130. self:do_conditional_absolute_jump(not self.flags.P)
  2131. end
  2132. Z80.instructions[227] = function(self)
  2133. local temp = self.core.mem_read(self, self.sp)
  2134. self.core.mem_write(self, self.sp, self.l)
  2135. self.l = temp
  2136. temp = self.core.mem_read(self, bit.band((self.sp + 1), 65535))
  2137. self.core.mem_write(self, bit.band((self.sp + 1), 65535), self.h)
  2138. self.h = temp
  2139. end
  2140. Z80.instructions[228] = function(self)
  2141. self:do_conditional_call(not self.flags.P)
  2142. end
  2143. Z80.instructions[229] = function(self)
  2144. self:push_word(bit.bor(self.l, bit.blshift(self.h, 8)))
  2145. end
  2146. Z80.instructions[230] = function(self)
  2147. self.pc = bit.band((self.pc + 1), 65535)
  2148. self:do_and(self.core.mem_read(self, self.pc))
  2149. end
  2150. Z80.instructions[231] = function(self)
  2151. self:do_reset(32)
  2152. end
  2153. Z80.instructions[232] = function(self)
  2154. self:do_conditional_return(self.flags.P)
  2155. end
  2156. Z80.instructions[233] = function(self)
  2157. self.pc = bit.bor(self.l, bit.blshift(self.h, 8))
  2158. self.pc = bit.band((self.pc - 1), 65535)
  2159. end
  2160. Z80.instructions[234] = function(self)
  2161. self:do_conditional_absolute_jump(self.flags.P)
  2162. end
  2163. Z80.instructions[235] = function(self)
  2164. local temp = self.d
  2165. self.d = self.h
  2166. self.h = temp
  2167. temp = self.e
  2168. self.e = self.l
  2169. self.l = temp
  2170. end
  2171. Z80.instructions[236] = function(self)
  2172. self:do_conditional_call(self.flags.P)
  2173. end
  2174. Z80.instructions[237] = function(self)
  2175. self.r = bit.bor(bit.band(self.r, 128), bit.band((bit.band(self.r, 127) + 1), 127))
  2176. self.pc = bit.band((self.pc + 1), 65535)
  2177. local opcode = self.core.mem_read(self, self.pc)
  2178. --print("OPCODE: "..opcode)
  2179. local func = self.ed_instructions[opcode]
  2180. if func then
  2181. func(self)
  2182. self.cycle_counter = self.cycle_counter + self.cycle_counts_ed[opcode]
  2183. else
  2184. self.cycle_counter = self.cycle_counter + self.cycle_counts[0]
  2185. end
  2186. end
  2187. Z80.instructions[238] = function(self)
  2188. self.pc = bit.band((self.pc + 1), 65535)
  2189. self:do_xor(self.core.mem_read(self, self.pc))
  2190. end
  2191. Z80.instructions[239] = function(self)
  2192. self:do_reset(40)
  2193. end
  2194. Z80.instructions[240] = function(self)
  2195. self:do_conditional_return(not self.flags.S)
  2196. end
  2197. Z80.instructions[241] = function(self)
  2198. local result = self:pop_word()
  2199. self:set_flags_register(bit.band(result, 255))
  2200. self.a = bit.brshift(bit.band(result, 65280), 8)
  2201. end
  2202. Z80.instructions[242] = function(self)
  2203. self:do_conditional_absolute_jump(not self.flags.S)
  2204. end
  2205. Z80.instructions[243] = function(self)
  2206. self.do_delayed_di = true
  2207. end
  2208. Z80.instructions[244] = function(self)
  2209. self:do_conditional_call(not self.flags.S)
  2210. end
  2211. Z80.instructions[245] = function(self)
  2212. self:push_word(bit.bor(self:get_flags_register(), bit.blshift(self.a, 8)))
  2213. end
  2214. Z80.instructions[246] = function(self)
  2215. self.pc = bit.band((self.pc + 1), 65535)
  2216. self:do_or(self.core.mem_read(self, self.pc))
  2217. end
  2218. Z80.instructions[247] = function(self)
  2219. self:do_reset(48)
  2220. end
  2221. Z80.instructions[248] = function(self)
  2222. self:do_conditional_return(self.flags.S)
  2223. end
  2224. Z80.instructions[249] = function(self)
  2225. self.sp = bit.bor(self.l, bit.blshift(self.h, 8))
  2226. end
  2227. Z80.instructions[250] = function(self)
  2228. self:do_conditional_absolute_jump(self.flags.S)
  2229. end
  2230. Z80.instructions[251] = function(self)
  2231. self.do_delayed_ei = true
  2232. end
  2233. Z80.instructions[252] = function(self)
  2234. self:do_conditional_call(self.flags.S)
  2235. end
  2236. Z80.instructions[253] = function(self)
  2237. self.r = bit.bor(bit.band(self.r, 128), bit.band((bit.band(self.r, 127) + 1), 127))
  2238. self.pc = bit.band((self.pc + 1), 65535)
  2239. local opcode = self.core.mem_read(self, self.pc)
  2240. local func = self.dd_instructions[opcode]
  2241. if func then
  2242. local temp = self.ix
  2243. self.ix = iy
  2244. func(self)
  2245. iy = self.ix
  2246. self.ix = temp
  2247. self.cycle_counter = self.cycle_counter + self.cycle_counts_dd[opcode]
  2248. else
  2249. self.pc = bit.band((self.pc - 1), 65535)
  2250. self.cycle_counter = self.cycle_counter + self.cycle_counts[0]
  2251. end
  2252. end
  2253. Z80.instructions[254] = function(self)
  2254. self.pc = bit.band((self.pc + 1), 65535)
  2255. self:do_cp(self.core.mem_read(self, self.pc))
  2256. end
  2257. Z80.instructions[255] = function(self)
  2258. self:do_reset(56)
  2259. end
  2260. Z80.ed_instructions = {}
  2261. Z80.ed_instructions[64] = function(self)
  2262. self.b = self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2263. end
  2264. Z80.ed_instructions[65] = function(self)
  2265. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.b)
  2266. end
  2267. Z80.ed_instructions[66] = function(self)
  2268. self:do_hl_sbc(bit.bor(self.c, bit.blshift(self.b, 8)))
  2269. end
  2270. Z80.ed_instructions[67] = function(self)
  2271. self.pc = bit.band((self.pc + 1), 65535)
  2272. local address = self.core.mem_read(self, self.pc)
  2273. self.pc = bit.band((self.pc + 1), 65535)
  2274. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2275. self.core.mem_write(self, address, self.c)
  2276. self.core.mem_write(self, bit.band((address + 1), 65535), self.b)
  2277. end
  2278. Z80.ed_instructions[68] = function(self)
  2279. self:do_neg()
  2280. end
  2281. Z80.ed_instructions[69] = function(self)
  2282. self.pc = bit.band((self:pop_word() - 1), 65535)
  2283. self.iff1 = self.iff2
  2284. end
  2285. Z80.ed_instructions[70] = function(self)
  2286. self.imode = 0
  2287. end
  2288. Z80.ed_instructions[71] = function(self)
  2289. i = self.a
  2290. end
  2291. Z80.ed_instructions[72] = function(self)
  2292. self.c = self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2293. end
  2294. Z80.ed_instructions[73] = function(self)
  2295. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.c)
  2296. end
  2297. Z80.ed_instructions[74] = function(self)
  2298. self:do_hl_adc(bit.bor(self.c, bit.blshift(self.b, 8)))
  2299. end
  2300. Z80.ed_instructions[75] = function(self)
  2301. self.pc = bit.band((self.pc + 1), 65535)
  2302. local address = self.core.mem_read(self, self.pc)
  2303. self.pc = bit.band((self.pc + 1), 65535)
  2304. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2305. self.c = self.core.mem_read(self, address)
  2306. self.b = self.core.mem_read(self, bit.band((address + 1), 65535))
  2307. end
  2308. Z80.ed_instructions[76] = function(self)
  2309. self:do_neg()
  2310. end
  2311. Z80.ed_instructions[77] = function(self)
  2312. self.pc = bit.band((self:pop_word() - 1), 65535)
  2313. end
  2314. Z80.ed_instructions[78] = function(self)
  2315. self.imode = 0
  2316. end
  2317. Z80.ed_instructions[79] = function(self)
  2318. self.r = self.a
  2319. end
  2320. Z80.ed_instructions[80] = function(self)
  2321. self.d = self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2322. end
  2323. Z80.ed_instructions[81] = function(self)
  2324. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.d)
  2325. end
  2326. Z80.ed_instructions[82] = function(self)
  2327. self:do_hl_sbc(bit.bor(self.e, bit.blshift(self.d, 8)))
  2328. end
  2329. Z80.ed_instructions[83] = function(self)
  2330. self.pc = bit.band((self.pc + 1), 65535)
  2331. local address = self.core.mem_read(self, self.pc)
  2332. self.pc = bit.band((self.pc + 1), 65535)
  2333. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2334. self.core.mem_write(self, address, self.e)
  2335. self.core.mem_write(self, bit.band((address + 1), 65535), self.d)
  2336. end
  2337. Z80.ed_instructions[84] = function(self)
  2338. self:do_neg()
  2339. end
  2340. Z80.ed_instructions[85] = function(self)
  2341. self.pc = bit.band((self:pop_word() - 1), 65535)
  2342. self.iff1 = self.iff2
  2343. end
  2344. Z80.ed_instructions[86] = function(self)
  2345. self.imode = 1
  2346. end
  2347. Z80.ed_instructions[87] = function(self)
  2348. self.a = i
  2349. if (bit.band(self.a, 128) > 0) then
  2350. self.flags.S = true
  2351. else
  2352. self.flags.S = false
  2353. end
  2354. if (self.a == 1) then
  2355. self.flags.Z = false
  2356. else
  2357. self.flags.Z = true
  2358. end
  2359. self.flags.H = false
  2360. self.flags.P = self.iff2
  2361. self.flags.N = false
  2362. self:update_xy_flags(self.a)
  2363. end
  2364. Z80.ed_instructions[88] = function(self)
  2365. self.e = self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2366. end
  2367. Z80.ed_instructions[89] = function(self)
  2368. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.e)
  2369. end
  2370. Z80.ed_instructions[90] = function(self)
  2371. self:do_hl_adc(bit.bor(self.e, bit.blshift(self.d, 8)))
  2372. end
  2373. Z80.ed_instructions[91] = function(self)
  2374. self.pc = bit.band((self.pc + 1), 65535)
  2375. local address = self.core.mem_read(self, self.pc)
  2376. self.pc = bit.band((self.pc + 1), 65535)
  2377. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2378. self.e = self.core.mem_read(self, address)
  2379. self.d = self.core.mem_read(self, bit.band((address + 1), 65535))
  2380. end
  2381. Z80.ed_instructions[92] = function(self)
  2382. self:do_neg()
  2383. end
  2384. Z80.ed_instructions[93] = function(self)
  2385. self.pc = bit.band((self:pop_word() - 1), 65535)
  2386. self.iff1 = self.iff2
  2387. end
  2388. Z80.ed_instructions[94] = function(self)
  2389. self.imode = 2
  2390. end
  2391. Z80.ed_instructions[95] = function(self)
  2392. self.a = self.r
  2393. if (bit.band(self.a, 128) > 0) then
  2394. self.flags.S = true
  2395. else
  2396. self.flags.S = false
  2397. end
  2398. if (self.a == 1) then
  2399. self.flags.Z = false
  2400. else
  2401. self.flags.Z = true
  2402. end
  2403. self.flags.H = false
  2404. self.flags.P = self.iff2
  2405. self.flags.N = false
  2406. self:update_xy_flags(self.a)
  2407. end
  2408. Z80.ed_instructions[96] = function(self)
  2409. self.h = self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2410. end
  2411. Z80.ed_instructions[97] = function(self)
  2412. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.h)
  2413. end
  2414. Z80.ed_instructions[98] = function(self)
  2415. self:do_hl_sbc(bit.bor(self.l, bit.blshift(self.h, 8)))
  2416. end
  2417. Z80.ed_instructions[99] = function(self)
  2418. self.pc = bit.band((self.pc + 1), 65535)
  2419. local address = self.core.mem_read(self, self.pc)
  2420. self.pc = bit.band((self.pc + 1), 65535)
  2421. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2422. self.core.mem_write(self, address, self.l)
  2423. self.core.mem_write(self, bit.band((address + 1), 65535), self.h)
  2424. end
  2425. Z80.ed_instructions[100] = function(self)
  2426. self:do_neg()
  2427. end
  2428. Z80.ed_instructions[101] = function(self)
  2429. self.pc = bit.band((self:pop_word() - 1), 65535)
  2430. self.iff1 = self.iff2
  2431. end
  2432. Z80.ed_instructions[102] = function(self)
  2433. self.imode = 0
  2434. end
  2435. Z80.ed_instructions[103] = function(self)
  2436. local hl_value = self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8)))
  2437. local temp1, temp2 = bit.band(hl_value, 15), bit.band(self.a, 15)
  2438. hl_value = bit.bor(bit.brshift(bit.band(hl_value, 240), 4), bit.blshift(temp2, 4))
  2439. self.a = bit.bor(bit.band(self.a, 240), temp1)
  2440. self.core.mem_write(self, bit.bor(self.l, bit.blshift(self.h, 8)), hl_value)
  2441. if (bit.band(self.a, 128) > 0) then
  2442. self.flags.S = true
  2443. else
  2444. self.flags.S = false
  2445. end
  2446. if (self.a > 0) then
  2447. self.flags.Z = false
  2448. else
  2449. self.flags.Z = true
  2450. end
  2451. self.flags.H = false
  2452. if (self:get_parity(self.a) == true) then
  2453. self.flags.P = true
  2454. else
  2455. self.flags.P = false
  2456. end
  2457. self.flags.N = false
  2458. self:update_xy_flags(self.a)
  2459. end
  2460. Z80.ed_instructions[104] = function(self)
  2461. self.l = self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2462. end
  2463. Z80.ed_instructions[105] = function(self)
  2464. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.l)
  2465. end
  2466. Z80.ed_instructions[106] = function(self)
  2467. self:do_hl_adc(bit.bor(self.l, bit.blshift(self.h, 8)))
  2468. end
  2469. Z80.ed_instructions[107] = function(self)
  2470. self.pc = bit.band((self.pc + 1), 65535)
  2471. local address = self.core.mem_read(self, self.pc)
  2472. self.pc = bit.band((self.pc + 1), 65535)
  2473. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2474. self.l = self.core.mem_read(self, address)
  2475. self.h = self.core.mem_read(self, bit.band((address + 1), 65535))
  2476. end
  2477. Z80.ed_instructions[108] = function(self)
  2478. self:do_neg()
  2479. end
  2480. Z80.ed_instructions[109] = function(self)
  2481. self.pc = bit.band((self:pop_word() - 1), 65535)
  2482. self.iff1 = self.iff2
  2483. end
  2484. Z80.ed_instructions[110] = function(self)
  2485. self.imode = 0
  2486. end
  2487. Z80.ed_instructions[111] = function(self)
  2488. local hl_value = self.core.mem_read(self, bit.bor(self.l, bit.blshift(self.h, 8)))
  2489. local temp1, temp2 = bit.band(hl_value, 240), bit.band(self.a, 15)
  2490. hl_value = bit.bor(bit.blshift(bit.band(hl_value, 15), 4), temp2)
  2491. self.a = bit.bor(bit.band(self.a, 240), bit.brshift(temp1, 4))
  2492. self.core.mem_write(self, bit.bor(self.l, bit.blshift(self.h, 8)), hl_value)
  2493. if (bit.band(self.a, 128) > 0) then
  2494. self.flags.S = true
  2495. else
  2496. self.flags.S = false
  2497. end
  2498. if (self.a ~= 0) then
  2499. self.flags.Z = false
  2500. else
  2501. self.flags.Z = true
  2502. end
  2503. self.flags.H = false
  2504. if (self:get_parity(self.a) == true) then
  2505. self.flags.P = true
  2506. else
  2507. self.flags.P = false
  2508. end
  2509. self.flags.N = false
  2510. self:update_xy_flags(self.a)
  2511. end
  2512. Z80.ed_instructions[112] = function(self)
  2513. self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2514. end
  2515. Z80.ed_instructions[113] = function(self)
  2516. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), 0)
  2517. end
  2518. Z80.ed_instructions[114] = function(self)
  2519. self:do_hl_sbc(self.sp)
  2520. end
  2521. Z80.ed_instructions[115] = function(self)
  2522. self.pc = bit.band((self.pc + 1), 65535)
  2523. local address = self.core.mem_read(self, self.pc)
  2524. self.pc = bit.band((self.pc + 1), 65535)
  2525. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2526. self.core.mem_write(self, address, bit.band(self.sp, 255))
  2527. self.core.mem_write(self, bit.band((address + 1), 65535), bit.band(bit.brshift(self.sp, 8), 255))
  2528. end
  2529. Z80.ed_instructions[116] = function(self)
  2530. self:do_neg()
  2531. end
  2532. Z80.ed_instructions[117] = function(self)
  2533. self.pc = bit.band((self:pop_word() - 1), 65535)
  2534. self.iff1 = self.iff2
  2535. end
  2536. Z80.ed_instructions[118] = function(self)
  2537. self.imode = 1
  2538. end
  2539. Z80.ed_instructions[120] = function(self)
  2540. self.a = self:do_in(bit.bor(bit.blshift(self.b, 8), self.c))
  2541. end
  2542. Z80.ed_instructions[121] = function(self)
  2543. self.core.io_write(self, bit.bor(bit.blshift(self.b, 8), self.c), self.a)
  2544. end
  2545. Z80.ed_instructions[122] = function(self)
  2546. self:do_hl_adc(self.sp)
  2547. end
  2548. Z80.ed_instructions[123] = function(self)
  2549. self.pc = bit.band((self.pc + 1), 65535)
  2550. local address = self.core.mem_read(self, self.pc)
  2551. self.pc = bit.band((self.pc + 1), 65535)
  2552. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2553. self.sp = self.core.mem_read(self, address)
  2554. self.sp = bit.bor(self.sp, bit.blshift(self.core.mem_read(self, bit.band((address + 1), 65535)), 8))
  2555. end
  2556. Z80.ed_instructions[124] = function(self)
  2557. self:do_neg()
  2558. end
  2559. Z80.ed_instructions[125] = function(self)
  2560. self.pc = bit.band((self:pop_word() - 1), 65535)
  2561. self.iff1 = self.iff2
  2562. end
  2563. Z80.ed_instructions[126] = function(self)
  2564. self.imode = 2
  2565. end
  2566. Z80.ed_instructions[160] = function(self)
  2567. self:do_ldi()
  2568. end
  2569. Z80.ed_instructions[161] = function(self)
  2570. self:do_cpi()
  2571. end
  2572. Z80.ed_instructions[162] = function(self)
  2573. self:do_ini()
  2574. end
  2575. Z80.ed_instructions[163] = function(self)
  2576. self:do_outi()
  2577. end
  2578. Z80.ed_instructions[168] = function(self)
  2579. self:do_ldd()
  2580. end
  2581. Z80.ed_instructions[169] = function(self)
  2582. self:do_cpd()
  2583. end
  2584. Z80.ed_instructions[170] = function(self)
  2585. self:do_ind()
  2586. end
  2587. Z80.ed_instructions[171] = function(self)
  2588. self:do_outd()
  2589. end
  2590. Z80.ed_instructions[176] = function(self)
  2591. self:do_ldi()
  2592. if self.b ~= 0 or self.c ~= 0 then
  2593. self.cycle_counter = self.cycle_counter + 5
  2594. self.pc = bit.band((self.pc - 2), 65535)
  2595. end
  2596. end
  2597. Z80.ed_instructions[177] = function(self)
  2598. self:do_cpi()
  2599. if not self.flags.Z and (self.b ~= 0 or self.c ~= 0) then
  2600. self.cycle_counter = self.cycle_counter + 5
  2601. self.pc = bit.band((self.pc - 2), 65535)
  2602. end
  2603. end
  2604. Z80.ed_instructions[178] = function(self)
  2605. self:do_ini()
  2606. if self.b ~= 0 then
  2607. self.cycle_counter = self.cycle_counter + 5
  2608. self.pc = bit.band((self.pc - 2), 65535)
  2609. end
  2610. end
  2611. Z80.ed_instructions[179] = function(self)
  2612. self:do_outi()
  2613. if self.b ~= 0 then
  2614. self.cycle_counter = self.cycle_counter + 5
  2615. self.pc = bit.band((self.pc - 2), 65535)
  2616. end
  2617. end
  2618. Z80.ed_instructions[184] = function(self)
  2619. self:do_ldd()
  2620. if self.b ~= 0 or self.c ~= 0 then
  2621. self.cycle_counter = self.cycle_counter + 5
  2622. self.pc = bit.band((self.pc - 2), 65535)
  2623. end
  2624. end
  2625. Z80.ed_instructions[185] = function(self)
  2626. self:do_cpd()
  2627. if not self.flags.Z and (self.b ~= 0 or self.c ~= 0) then
  2628. self.cycle_counter = self.cycle_counter + 5
  2629. self.pc = bit.band((self.pc - 2), 65535)
  2630. end
  2631. end
  2632. Z80.ed_instructions[186] = function(self)
  2633. self:do_ind()
  2634. if self.b ~= 0 then
  2635. self.cycle_counter = self.cycle_counter + 5
  2636. self.pc = bit.band((self.pc - 2), 65535)
  2637. end
  2638. end
  2639. Z80.ed_instructions[187] = function(self)
  2640. self:do_outd()
  2641. if self.b ~= 0 then
  2642. self.cycle_counter = self.cycle_counter + 5
  2643. self.pc = bit.band((self.pc - 2), 65535)
  2644. end
  2645. end
  2646. Z80.dd_instructions = {}
  2647. Z80.dd_instructions[9] = function(self)
  2648. self:do_ix_add(bit.bor(self.c, bit.blshift(self.b, 8)))
  2649. end
  2650. Z80.dd_instructions[25] = function(self)
  2651. self:do_ix_add(bit.bor(self.e, bit.blshift(self.d, 8)))
  2652. end
  2653. Z80.dd_instructions[33] = function(self)
  2654. self.pc = bit.band((self.pc + 1), 65535)
  2655. self.ix = self.core.mem_read(self, self.pc)
  2656. self.pc = bit.band((self.pc + 1), 65535)
  2657. self.ix = bit.bor(self.ix, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2658. end
  2659. Z80.dd_instructions[34] = function(self)
  2660. self.pc = bit.band((self.pc + 1), 65535)
  2661. local address = self.core.mem_read(self, self.pc)
  2662. self.pc = bit.band((self.pc + 1), 65535)
  2663. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2664. self.core.mem_write(self, address, bit.band(self.ix, 255))
  2665. self.core.mem_write(self, bit.band((address + 1), 65535), bit.band(bit.brshift(self.ix, 8), 255))
  2666. end
  2667. Z80.dd_instructions[35] = function(self)
  2668. self.ix = bit.band((self.ix + 1), 65535)
  2669. end
  2670. Z80.dd_instructions[36] = function(self)
  2671. self.ix = bit.bor(bit.blshift(self:do_inc(bit.brshift(self.ix, 8)), 8), bit.band(self.ix, 255))
  2672. end
  2673. Z80.dd_instructions[37] = function(self)
  2674. self.ix = bit.bor(bit.blshift(self:do_dec(bit.brshift(self.ix, 8)), 8), bit.band(self.ix, 255))
  2675. end
  2676. Z80.dd_instructions[38] = function(self)
  2677. self.pc = bit.band((self.pc + 1), 65535)
  2678. self.ix = bit.bor(bit.blshift(self.core.mem_read(self, self.pc), 8), bit.band(self.ix, 255))
  2679. end
  2680. Z80.dd_instructions[41] = function(self)
  2681. self:do_ix_add(self.ix)
  2682. end
  2683. Z80.dd_instructions[42] = function(self)
  2684. self.pc = bit.band((self.pc + 1), 65535)
  2685. local address = self.core.mem_read(self, self.pc)
  2686. self.pc = bit.band((self.pc + 1), 65535)
  2687. address = bit.bor(address, bit.blshift(self.core.mem_read(self, self.pc), 8))
  2688. self.ix = self.core.mem_read(self, address)
  2689. self.ix = bit.bor(self.ix, bit.blshift(self.core.mem_read(self, bit.band((address + 1), 65535)), 8))
  2690. end
  2691. Z80.dd_instructions[43] = function(self)
  2692. self.ix = bit.band((self.ix - 1), 65535)
  2693. end
  2694. Z80.dd_instructions[44] = function(self)
  2695. self.ix = bit.bor(self:do_inc(bit.band(self.ix, 255)), bit.band(self.ix, 65280))
  2696. end
  2697. Z80.dd_instructions[45] = function(self)
  2698. self.ix = bit.bor(self:do_dec(bit.band(self.ix, 255)), bit.band(self.ix, 65280))
  2699. end
  2700. Z80.dd_instructions[46] = function(self)
  2701. self.pc = bit.band((self.pc + 1), 65535)
  2702. self.ix = bit.bor(bit.band(self.core.mem_read(self, self.pc), 255), bit.band(self.ix, 65280))
  2703. end
  2704. Z80.dd_instructions[52] = function(self)
  2705. self.pc = bit.band((self.pc + 1), 65535)
  2706. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2707. local value = self.core.mem_read(self, bit.band((offset + self.ix), 65535))
  2708. self.core.mem_write(self, bit.band((offset + self.ix), 65535), self:do_inc(value))
  2709. end
  2710. Z80.dd_instructions[53] = function(self)
  2711. self.pc = bit.band((self.pc + 1), 65535)
  2712. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2713. local value = self.core.mem_read(self, bit.band((offset + self.ix), 65535))
  2714. self.core.mem_write(self, bit.band((offset + self.ix), 65535), self:do_dec(value))
  2715. end
  2716. Z80.dd_instructions[54] = function(self)
  2717. self.pc = bit.band((self.pc + 1), 65535)
  2718. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2719. self.pc = bit.band((self.pc + 1), 65535)
  2720. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.core.mem_read(self, self.pc))
  2721. end
  2722. Z80.dd_instructions[57] = function(self)
  2723. self:do_ix_add(self.sp)
  2724. end
  2725. Z80.dd_instructions[68] = function(self)
  2726. self.b = bit.band(bit.brshift(self.ix, 8), 255)
  2727. end
  2728. Z80.dd_instructions[69] = function(self)
  2729. self.b = bit.band(self.ix, 255)
  2730. end
  2731. Z80.dd_instructions[70] = function(self)
  2732. self.pc = bit.band((self.pc + 1), 65535)
  2733. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2734. self.b = self.core.mem_read(self, bit.band((self.ix + offset), 65535))
  2735. end
  2736. Z80.dd_instructions[76] = function(self)
  2737. self.c = bit.band(bit.brshift(self.ix, 8), 255)
  2738. end
  2739. Z80.dd_instructions[77] = function(self)
  2740. self.c = bit.band(self.ix, 255)
  2741. end
  2742. Z80.dd_instructions[78] = function(self)
  2743. self.pc = bit.band((self.pc + 1), 65535)
  2744. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2745. self.c = self.core.mem_read(self, bit.band((self.ix + offset), 65535))
  2746. end
  2747. Z80.dd_instructions[84] = function(self)
  2748. self.d = bit.band(bit.brshift(self.ix, 8), 255)
  2749. end
  2750. Z80.dd_instructions[85] = function(self)
  2751. self.d = bit.band(self.ix, 255)
  2752. end
  2753. Z80.dd_instructions[86] = function(self)
  2754. self.pc = bit.band((self.pc + 1), 65535)
  2755. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2756. self.d = self.core.mem_read(self, bit.band((self.ix + offset), 65535))
  2757. end
  2758. Z80.dd_instructions[92] = function(self)
  2759. self.e = bit.band(bit.brshift(self.ix, 8), 255)
  2760. end
  2761. Z80.dd_instructions[93] = function(self)
  2762. self.e = bit.band(self.ix, 255)
  2763. end
  2764. Z80.dd_instructions[94] = function(self)
  2765. self.pc = bit.band((self.pc + 1), 65535)
  2766. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2767. self.e = self.core.mem_read(self, bit.band((self.ix + offset), 65535))
  2768. end
  2769. Z80.dd_instructions[96] = function(self)
  2770. self.ix = bit.bor(bit.band(self.ix, 255), bit.blshift(self.b, 8))
  2771. end
  2772. Z80.dd_instructions[97] = function(self)
  2773. self.ix = bit.bor(bit.band(self.ix, 255), bit.blshift(self.c, 8))
  2774. end
  2775. Z80.dd_instructions[98] = function(self)
  2776. self.ix = bit.bor(bit.band(self.ix, 255), bit.blshift(self.d, 8))
  2777. end
  2778. Z80.dd_instructions[99] = function(self)
  2779. self.ix = bit.bor(bit.band(self.ix, 255), bit.blshift(self.e, 8))
  2780. end
  2781. Z80.dd_instructions[100] = function(self)
  2782. end
  2783. Z80.dd_instructions[101] = function(self)
  2784. self.ix = bit.bor(bit.band(self.ix, 255), bit.blshift(bit.band(self.ix, 255), 8))
  2785. end
  2786. Z80.dd_instructions[102] = function(self)
  2787. self.pc = bit.band((self.pc + 1), 65535)
  2788. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2789. self.h = self.core.mem_read(self, bit.band((self.ix + offset), 65535))
  2790. end
  2791. Z80.dd_instructions[103] = function(self)
  2792. self.ix = bit.bor(bit.band(self.ix, 255), bit.blshift(self.a, 8))
  2793. end
  2794. Z80.dd_instructions[104] = function(self)
  2795. self.ix = bit.bor(bit.band(self.ix, 65280), self.b)
  2796. end
  2797. Z80.dd_instructions[105] = function(self)
  2798. self.ix = bit.bor(bit.band(self.ix, 65280), self.c)
  2799. end
  2800. Z80.dd_instructions[106] = function(self)
  2801. self.ix = bit.bor(bit.band(self.ix, 65280), self.d)
  2802. end
  2803. Z80.dd_instructions[107] = function(self)
  2804. self.ix = bit.bor(bit.band(self.ix, 65280), self.e)
  2805. end
  2806. Z80.dd_instructions[108] = function(self)
  2807. self.ix = bit.bor(bit.band(self.ix, 65280), bit.brshift(self.ix, 8))
  2808. end
  2809. Z80.dd_instructions[109] = function(self)
  2810. end
  2811. Z80.dd_instructions[110] = function(self)
  2812. self.pc = bit.band((self.pc + 1), 65535)
  2813. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2814. self.l = self.core.mem_read(self, bit.band((self.ix + offset), 65535))
  2815. end
  2816. Z80.dd_instructions[111] = function(self)
  2817. self.ix = bit.bor(bit.band(self.ix, 65280), self.a)
  2818. end
  2819. Z80.dd_instructions[112] = function(self)
  2820. self.pc = bit.band((self.pc + 1), 65535)
  2821. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2822. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.b)
  2823. end
  2824. Z80.dd_instructions[113] = function(self)
  2825. self.pc = bit.band((self.pc + 1), 65535)
  2826. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2827. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.c)
  2828. end
  2829. Z80.dd_instructions[114] = function(self)
  2830. self.pc = bit.band((self.pc + 1), 65535)
  2831. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2832. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.d)
  2833. end
  2834. Z80.dd_instructions[115] = function(self)
  2835. self.pc = bit.band((self.pc + 1), 65535)
  2836. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2837. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.e)
  2838. end
  2839. Z80.dd_instructions[116] = function(self)
  2840. self.pc = bit.band((self.pc + 1), 65535)
  2841. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2842. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.h)
  2843. end
  2844. Z80.dd_instructions[117] = function(self)
  2845. self.pc = bit.band((self.pc + 1), 65535)
  2846. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2847. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.l)
  2848. end
  2849. Z80.dd_instructions[119] = function(self)
  2850. self.pc = bit.band((self.pc + 1), 65535)
  2851. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2852. self.core.mem_write(self, bit.band((self.ix + offset), 65535), self.a)
  2853. end
  2854. Z80.dd_instructions[124] = function(self)
  2855. self.a = bit.band(bit.brshift(self.ix, 8), 255)
  2856. end
  2857. Z80.dd_instructions[125] = function(self)
  2858. self.a = bit.band(self.ix, 255)
  2859. end
  2860. Z80.dd_instructions[126] = function(self)
  2861. self.pc = bit.band((self.pc + 1), 65535)
  2862. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2863. self.a = self.core.mem_read(self, bit.band((self.ix + offset), 65535))
  2864. end
  2865. Z80.dd_instructions[132] = function(self)
  2866. self:do_add(bit.band(bit.brshift(self.ix, 8), 255))
  2867. end
  2868. Z80.dd_instructions[133] = function(self)
  2869. self:do_add(bit.band(self.ix, 255))
  2870. end
  2871. Z80.dd_instructions[134] = function(self)
  2872. self.pc = bit.band((self.pc + 1), 65535)
  2873. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2874. self:do_add(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2875. end
  2876. Z80.dd_instructions[140] = function(self)
  2877. self:do_adc(bit.band(bit.brshift(self.ix, 8), 255))
  2878. end
  2879. Z80.dd_instructions[141] = function(self)
  2880. self:do_adc(bit.band(self.ix, 255))
  2881. end
  2882. Z80.dd_instructions[142] = function(self)
  2883. self.pc = bit.band((self.pc + 1), 65535)
  2884. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2885. self:do_adc(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2886. end
  2887. Z80.dd_instructions[148] = function(self)
  2888. self:do_sub(bit.band(bit.brshift(self.ix, 8), 255))
  2889. end
  2890. Z80.dd_instructions[149] = function(self)
  2891. self:do_sub(bit.band(self.ix, 255))
  2892. end
  2893. Z80.dd_instructions[150] = function(self)
  2894. self.pc = bit.band((self.pc + 1), 65535)
  2895. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2896. self:do_sub(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2897. end
  2898. Z80.dd_instructions[156] = function(self)
  2899. self:do_sbc(bit.band(bit.brshift(self.ix, 8), 255))
  2900. end
  2901. Z80.dd_instructions[157] = function(self)
  2902. self:do_sbc(bit.band(self.ix, 255))
  2903. end
  2904. Z80.dd_instructions[158] = function(self)
  2905. self.pc = bit.band((self.pc + 1), 65535)
  2906. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2907. self:do_sbc(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2908. end
  2909. Z80.dd_instructions[164] = function(self)
  2910. self:do_and(bit.band(bit.brshift(self.ix, 8), 255))
  2911. end
  2912. Z80.dd_instructions[165] = function(self)
  2913. self:do_and(bit.band(self.ix, 255))
  2914. end
  2915. Z80.dd_instructions[166] = function(self)
  2916. self.pc = bit.band((self.pc + 1), 65535)
  2917. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2918. self:do_and(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2919. end
  2920. Z80.dd_instructions[172] = function(self)
  2921. self:do_xor(bit.band(bit.brshift(self.ix, 8), 255))
  2922. end
  2923. Z80.dd_instructions[173] = function(self)
  2924. self:do_xor(bit.band(self.ix, 255))
  2925. end
  2926. Z80.dd_instructions[174] = function(self)
  2927. self.pc = bit.band((self.pc + 1), 65535)
  2928. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2929. self:do_xor(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2930. end
  2931. Z80.dd_instructions[180] = function(self)
  2932. self:do_or(bit.band(bit.brshift(self.ix, 8), 255))
  2933. end
  2934. Z80.dd_instructions[181] = function(self)
  2935. self:do_or(bit.band(self.ix, 255))
  2936. end
  2937. Z80.dd_instructions[182] = function(self)
  2938. self.pc = bit.band((self.pc + 1), 65535)
  2939.  
  2940. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2941.  
  2942. self:do_or(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2943. end
  2944. Z80.dd_instructions[188] = function(self)
  2945. self:do_cp(bit.band(bit.brshift(self.ix, 8), 255))
  2946. end
  2947. Z80.dd_instructions[189] = function(self)
  2948. self:do_cp(bit.band(self.ix, 255))
  2949. end
  2950. Z80.dd_instructions[190] = function(self)
  2951. self.pc = bit.band((self.pc + 1), 65535)
  2952. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2953. self:do_cp(self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2954. end
  2955. Z80.dd_instructions[203] = function(self)
  2956.  
  2957. self.pc = bit.band((self.pc + 1), 65535)
  2958. local offset = self:get_signed_offset_byte(self.core.mem_read(self, self.pc))
  2959. self.pc = bit.band((self.pc + 1), 65535)
  2960. local opcode, value = self.core.mem_read(self, self.pc), nil
  2961. if opcode < 64 then
  2962. local ddcb_functions = {
  2963. self.do_rlc,
  2964. self.do_rrc,
  2965. self.do_rl,
  2966. self.do_rr,
  2967. self.do_sla,
  2968. self.do_sra,
  2969. self.do_sll,
  2970. self.do_srl
  2971. }
  2972. local func = ddcb_functions[bit.brshift(bit.band(opcode, 56), 3) + 1]
  2973. value = func(self, self.core.mem_read(self, bit.band((self.ix + offset), 65535)))
  2974. self.core.mem_write(self, bit.band((self.ix + offset), 65535), value)
  2975. else
  2976. local bit_number = bit.brshift(bit.band(opcode, 56), 3)
  2977. if opcode < 128 then
  2978. self.flags.N = false
  2979. self.flags.H = true
  2980. if (not (bit.band(self.core.mem_read(self, bit.band((self.ix + offset), 65535)), bit.blshift(1, bit_number)) ~= 0)) then
  2981. self.flags.Z = true
  2982. else
  2983. self.flags.Z = false
  2984. end
  2985. self.flags.P = self.flags.Z
  2986. if (((bit_number == 7) and not self.flags.Z)) then
  2987. self.flags.S = true
  2988. else
  2989. self.flags.S = false
  2990. end
  2991. elseif opcode < 192 then
  2992. value =
  2993. bit.band(
  2994. bit.band(self.core.mem_read(self, bit.band((self.ix + offset), 65535)), bit.bnot(bit.blshift(1, bit_number))),
  2995. 255
  2996. )
  2997. self.core.mem_write(self, bit.band((self.ix + offset), 65535), value)
  2998. else
  2999. value = bit.bor(self.core.mem_read(self, bit.band((self.ix + offset), 65535)), bit.blshift(1, bit_number))
  3000. self.core.mem_write(self, bit.band((self.ix + offset), 65535), value)
  3001. end
  3002. end
  3003. if value ~= nil then
  3004. if bit.band(opcode, 7) == 0 then
  3005. self.b = value
  3006. elseif bit.band(opcode, 7) == 1 then
  3007. self.c = value
  3008. elseif bit.band(opcode, 7) == 2 then
  3009. self.d = value
  3010. elseif bit.band(opcode, 7) == 3 then
  3011. self.e = value
  3012. elseif bit.band(opcode, 7) == 4 then
  3013. self.h = value
  3014. elseif bit.band(opcode, 7) == 5 then
  3015. self.l = value
  3016. elseif bit.band(opcode, 7) == 7 then
  3017. self.a = value
  3018. end
  3019. end
  3020. self.cycle_counter = self.cycle_counter + self.cycle_counts_cb[opcode] + 8
  3021. end
  3022. Z80.dd_instructions[225] = function(self)
  3023. self.ix = self:pop_word()
  3024. end
  3025. Z80.dd_instructions[227] = function(self)
  3026. local temp = self.ix
  3027. self.ix = self.core.mem_read(self, self.sp)
  3028. self.ix = bit.bor(self.ix, bit.blshift(self.core.mem_read(self, bit.band((self.sp + 1), 65535)), 8))
  3029. self.core.mem_write(self, self.sp, bit.band(temp, 255))
  3030. self.core.mem_write(self, bit.band((self.sp + 1), 65535), bit.band(bit.brshift(temp, 8), 255))
  3031. end
  3032. Z80.dd_instructions[229] = function(self)
  3033. self:push_word(self.ix)
  3034. end
  3035. Z80.dd_instructions[233] = function(self)
  3036. self.pc = bit.band((self.ix - 1), 65535)
  3037. end
  3038. Z80.dd_instructions[249] = function(self)
  3039. self.sp = self.ix
  3040. end
  3041. Z80.cycle_counts_oneBased = {
  3042. 4, 10, 7, 6, 4, 4, 7, 4, 4, 11, 7, 6, 4, 4, 7, 4, 8, 10, 7, 6, 4, 4, 7, 4, 12, 11, 7, 6, 4, 4, 7, 4, 7, 10, 16, 6, 4, 4, 7, 4, 7, 11, 16, 6, 4, 4, 7, 4, 7, 10, 13, 6, 11, 11, 10, 4, 7, 11, 13, 6, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 7, 7, 7, 7, 7, 7, 4, 7, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 5, 10, 10, 10, 10, 11, 7, 11, 5, 10, 10, 0, 10, 17, 7, 11, 5, 10, 10, 11, 10, 11, 7, 11, 5, 4, 10, 11, 10, 0, 7, 11, 5, 10, 10, 19, 10, 11, 7, 11, 5, 4, 10, 4, 10, 0, 7, 11, 5, 10, 10, 4, 10, 11, 7, 11, 5, 6, 10, 4, 10, 0, 7, 11
  3043. }
  3044. Z80.cycle_counts_ed_oneBased = {
  3045. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 15, 20, 8, 14, 8, 9, 12, 12, 15, 20, 8, 14, 8, 9, 12, 12, 15, 20, 8, 14, 8, 9, 12, 12, 15, 20, 8, 14, 8, 9, 12, 12, 15, 20, 8, 14, 8, 18, 12, 12, 15, 20, 8, 14, 8, 18, 12, 12, 15, 20, 8, 14, 8, 0, 12, 12, 15, 20, 8, 14, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16, 16, 16, 0, 0, 0, 0, 16, 16, 16, 16, 0, 0, 0, 0, 16, 16, 16, 16, 0, 0, 0, 0, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  3046. }
  3047. Z80.cycle_counts_cb_oneBased = {
  3048. 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8
  3049. }
  3050. Z80.cycle_counts_dd_oneBased = {
  3051. 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 14, 20, 10, 8, 8, 11, 0, 0, 15, 20, 10, 8, 8, 11, 0, 0, 0, 0, 0, 23, 23, 19, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8, 8, 8, 8, 8, 19, 8, 19, 19, 19, 19, 19, 19, 0, 19, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 23, 0, 15, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0
  3052. }
  3053. Z80.cycle_counts = {}
  3054. Z80.cycle_counts_ed = {}
  3055. Z80.cycle_counts_cb = {}
  3056. Z80.cycle_counts_dd = {}
  3057.  
  3058. for i = 0, #Z80.cycle_counts_oneBased - 1 do
  3059. Z80.cycle_counts[i] = Z80.cycle_counts_oneBased[i + 1]
  3060. end
  3061. for i = 0, #Z80.cycle_counts_ed_oneBased - 1 do
  3062. Z80.cycle_counts_ed[i] = Z80.cycle_counts_ed_oneBased[i + 1]
  3063. end
  3064. for i = 0, #Z80.cycle_counts_cb_oneBased - 1 do
  3065. Z80.cycle_counts_cb[i] = Z80.cycle_counts_cb_oneBased[i + 1]
  3066. end
  3067. for i = 0, #Z80.cycle_counts_dd_oneBased - 1 do
  3068. Z80.cycle_counts_dd[i] = Z80.cycle_counts_dd_oneBased[i + 1]
  3069. end
  3070.  
  3071. m = MemIoOps()
  3072.  
  3073. core = {
  3074. mem_read = function(self, addr)
  3075. return m:peek8(addr)
  3076. end,
  3077. mem_write = function(self, addr, val)
  3078. m:poke8(addr, val)
  3079. end,
  3080. io_read = function(self, addr)
  3081. return m:inPort(addr)
  3082. end,
  3083. io_write = function(self, addr, val)
  3084. m:outPort(addr, val)
  3085. end
  3086. }
  3087.  
  3088. x = Z80(core)
  3089. t = Z80:getState()
  3090.  
  3091. x:setState(t)
  3092.  
  3093. local clock = os.clock
  3094. function sleep(n) -- seconds
  3095. local t0 = clock()
  3096. while clock() - t0 <= n do
  3097. end
  3098. end
  3099.  
  3100. function loadBin(path, offset)
  3101. local jsString = "var rom = {"
  3102. local count = 0
  3103. local input = io.open(path, "rb")
  3104. while true do
  3105. local x = input:read(1)
  3106. if x == nil then
  3107. break
  3108. end
  3109. m:poke8(count + offset, string.byte(x))
  3110. jsString = jsString .. string.byte(x) .. ", "
  3111. count = count + 1
  3112. end
  3113. jsString = jsString .. "}"
  3114. return jsString
  3115. end
  3116.  
  3117. js = loadBin("zexdoc.ams", 0x100)
  3118. --rom = {195, 19, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 6, 0, 249, 17, 218, 29, 14, 9, 205, 206, 29, 33, 58, 1, 126, 35, 182, 202, 47, 1, 43, 205, 226, 26, 195, 34, 1, 17, 246, 29, 14, 9, 205, 206, 29, 195, 0, 0, 194, 1, 34, 2, 130, 2, 226, 2, 66, 3, 162, 3, 2, 4, 98, 4, 194, 4, 34, 5, 130, 5, 226, 5, 66, 6, 162, 6, 2, 7, 98, 7, 194, 7, 34, 8, 130, 8, 226, 8, 66, 9, 162, 9, 2, 10, 98, 10, 194, 10, 34, 11, 130, 11, 226, 11, 66, 12, 162, 12, 2, 13, 98, 13, 194, 13, 34, 14, 130, 14, 226, 14, 66, 15, 162, 15, 2, 16, 98, 16, 194, 16, 34, 17, 130, 17, 226, 17, 66, 18, 162, 18, 2, 19, 98, 19, 194, 19, 34, 20, 130, 20, 226, 20, 66, 21, 162, 21, 2, 22, 98, 22, 194, 22, 34, 23, 130, 23, 226, 23, 66, 24, 162, 24, 2, 25, 98, 25, 194, 25, 34, 26, 130, 26, 0, 0, 199, 237, 66, 0, 0, 44, 131, 136, 79, 43, 242, 57, 179, 31, 126, 99, 21, 211, 137, 94, 70, 0, 56, 0, 0, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 215, 0, 255, 255, 248, 180, 234, 169, 60, 97, 100, 99, 44, 115, 98, 99, 62, 32, 104, 108, 44, 60, 98, 99, 44, 100, 101, 44, 104, 108, 44, 115, 112, 62, 46, 46, 46, 46, 36, 199, 9, 0, 0, 0, 165, 196, 199, 196, 38, 210, 80, 160, 234, 88, 102, 133, 198, 222, 201, 155, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 215, 0, 255, 255, 137, 253, 182, 53, 97, 100, 100, 32, 104, 108, 44, 60, 98, 99, 44, 100, 101, 44, 104, 108, 44, 115, 112, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 199, 221, 9, 0, 0, 172, 221, 148, 194, 91, 99, 211, 51, 118, 106, 32, 250, 148, 104, 245, 54, 0, 48, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 215, 0, 255, 255, 193, 51, 121, 11, 97, 100, 100, 32, 105, 120, 44, 60, 98, 99, 44, 100, 101, 44, 105, 120, 44, 115, 112, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 199, 253, 9, 0, 0, 194, 199, 7, 244, 193, 81, 150, 62, 244, 11, 15, 81, 146, 30, 234, 113, 0, 48, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 215, 0, 255, 255, 232, 129, 123, 158, 97, 100, 100, 32, 105, 121, 44, 60, 98, 99, 44, 100, 101, 44, 105, 121, 44, 115, 112, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 198, 0, 0, 0, 64, 145, 60, 126, 103, 122, 109, 223, 97, 91, 41, 11, 16, 102, 178, 133, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 72, 121, 147, 96, 97, 108, 117, 111, 112, 32, 97, 44, 110, 110, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 128, 0, 0, 0, 62, 197, 58, 87, 77, 76, 3, 1, 9, 227, 102, 166, 208, 59, 187, 173, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 215, 0, 0, 0, 254, 67, 176, 22, 97, 108, 117, 111, 112, 32, 97, 44, 60, 98, 44, 99, 44, 100, 44, 101, 44, 104, 44, 108, 44, 40, 104, 108, 41, 44, 97, 62, 46, 46, 36, 215, 221, 132, 0, 0, 247, 214, 110, 199, 207, 172, 71, 40, 221, 34, 53, 192, 197, 56, 75, 35, 32, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 215, 0, 0, 0, 164, 2, 109, 90, 97, 108, 117, 111, 112, 32, 97, 44, 60, 105, 120, 104, 44, 105, 120, 108, 44, 105, 121, 104, 44, 105, 121, 108, 62, 46, 46, 46, 46, 46, 36, 215, 221, 134, 1, 0, 183, 144, 2, 1, 2, 1, 253, 50, 110, 64, 220, 193, 69, 110, 250, 229, 32, 56, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 232, 73, 103, 110, 97, 108, 117, 111, 112, 32, 97, 44, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 83, 221, 203, 1, 70, 117, 32, 2, 1, 2, 1, 252, 60, 154, 167, 116, 61, 81, 39, 20, 202, 32, 0, 0, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 238, 8, 103, 98, 105, 116, 32, 110, 44, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 83, 203, 64, 0, 0, 241, 62, 252, 157, 204, 122, 3, 1, 97, 190, 134, 122, 80, 36, 152, 25, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 0, 255, 0, 0, 123, 85, 230, 200, 98, 105, 116, 32, 110, 44, 60, 98, 44, 99, 44, 100, 44, 101, 44, 104, 44, 108, 44, 40, 104, 108, 41, 44, 97, 62, 46, 46, 46, 46, 36, 215, 237, 169, 0, 0, 182, 199, 180, 114, 246, 24, 20, 1, 189, 141, 1, 0, 192, 48, 163, 148, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 168, 126, 108, 250, 99, 112, 100, 60, 114, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 161, 0, 0, 72, 77, 74, 175, 107, 144, 3, 1, 113, 78, 1, 0, 147, 106, 124, 144, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 6, 222, 179, 86, 99, 112, 105, 60, 114, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 39, 0, 0, 0, 65, 33, 250, 9, 96, 29, 89, 165, 91, 141, 121, 144, 4, 142, 157, 41, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155, 75, 166, 117, 60, 100, 97, 97, 44, 99, 112, 108, 44, 115, 99, 102, 44, 99, 99, 102, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 60, 0, 0, 0, 223, 74, 216, 213, 152, 229, 43, 138, 176, 167, 27, 67, 68, 90, 48, 208, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 209, 136, 21, 164, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 97, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 4, 0, 0, 0, 35, 214, 45, 67, 97, 122, 128, 129, 134, 90, 133, 30, 134, 88, 187, 155, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 95, 104, 34, 100, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 98, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 3, 0, 0, 0, 151, 205, 171, 68, 201, 141, 227, 227, 204, 17, 164, 232, 2, 73, 77, 42, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 210, 174, 59, 236, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 98, 99, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 12, 0, 0, 0, 137, 215, 53, 9, 91, 5, 133, 159, 39, 139, 8, 210, 149, 5, 96, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 194, 132, 85, 76, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 99, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 20, 0, 0, 0, 234, 160, 186, 95, 251, 101, 28, 152, 204, 56, 188, 222, 67, 92, 189, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 69, 35, 222, 16, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 100, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 19, 0, 0, 0, 46, 52, 29, 19, 201, 40, 202, 10, 103, 153, 46, 58, 146, 246, 84, 157, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 174, 198, 212, 44, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 100, 101, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 28, 0, 0, 0, 47, 96, 13, 76, 2, 36, 245, 226, 244, 160, 10, 161, 19, 50, 37, 89, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 225, 117, 175, 204, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 101, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 36, 0, 0, 0, 6, 21, 235, 242, 221, 232, 43, 38, 166, 17, 26, 188, 23, 6, 24, 40, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 28, 237, 132, 125, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 104, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 35, 0, 0, 0, 244, 195, 165, 7, 109, 27, 4, 79, 194, 226, 42, 130, 87, 224, 225, 195, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 252, 13, 109, 74, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 104, 108, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 35, 0, 0, 60, 188, 155, 13, 129, 224, 253, 173, 127, 154, 229, 150, 19, 133, 226, 11, 0, 8, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 165, 77, 190, 49, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 105, 120, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 253, 35, 0, 0, 2, 148, 122, 99, 130, 49, 90, 198, 233, 178, 180, 171, 22, 242, 5, 109, 0, 8, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 80, 93, 81, 163, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 105, 121, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 44, 0, 0, 0, 49, 128, 32, 165, 86, 67, 9, 180, 193, 244, 162, 223, 209, 60, 162, 62, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 86, 205, 6, 243, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 108, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 52, 0, 0, 0, 86, 184, 124, 12, 62, 229, 3, 1, 126, 135, 88, 218, 21, 92, 55, 31, 1, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 184, 58, 220, 239, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 40, 104, 108, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 51, 0, 0, 0, 111, 52, 130, 212, 105, 209, 182, 222, 148, 164, 118, 244, 83, 2, 91, 133, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 93, 172, 213, 39, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 115, 112, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 52, 1, 0, 110, 250, 2, 1, 2, 1, 40, 44, 148, 136, 87, 80, 22, 51, 111, 40, 32, 1, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 32, 88, 20, 112, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 36, 0, 0, 56, 184, 108, 49, 212, 198, 1, 62, 88, 131, 180, 21, 129, 222, 89, 66, 0, 1, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 111, 70, 54, 98, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 105, 120, 104, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 44, 0, 0, 20, 77, 96, 116, 212, 118, 231, 6, 162, 50, 60, 33, 214, 215, 165, 153, 0, 1, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 2, 123, 239, 44, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 105, 120, 108, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 36, 0, 0, 54, 40, 111, 159, 22, 145, 185, 97, 203, 130, 25, 226, 146, 115, 140, 169, 0, 1, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 45, 150, 108, 243, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 105, 121, 104, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 44, 0, 0, 198, 215, 213, 98, 158, 160, 57, 112, 126, 62, 18, 159, 144, 217, 15, 34, 0, 1, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 251, 203, 186, 149, 60, 105, 110, 99, 44, 100, 101, 99, 62, 32, 105, 121, 108, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 75, 3, 1, 168, 249, 89, 245, 164, 147, 237, 245, 150, 111, 104, 217, 134, 230, 216, 75, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 69, 169, 172, 108, 100, 32, 60, 98, 99, 44, 100, 101, 62, 44, 40, 110, 110, 110, 110, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 42, 3, 1, 0, 99, 152, 48, 120, 119, 32, 254, 177, 250, 185, 184, 171, 4, 6, 21, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 151, 36, 135, 108, 100, 32, 104, 108, 44, 40, 110, 110, 110, 110, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 123, 3, 1, 252, 141, 215, 87, 97, 33, 24, 202, 133, 193, 218, 39, 131, 30, 96, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122, 206, 161, 27, 108, 100, 32, 115, 112, 44, 40, 110, 110, 110, 110, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 42, 3, 1, 215, 222, 250, 166, 128, 247, 76, 36, 222, 135, 194, 188, 22, 99, 150, 76, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 139, 241, 109, 108, 100, 32, 60, 105, 120, 44, 105, 121, 62, 44, 40, 110, 110, 110, 110, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 67, 3, 1, 152, 31, 77, 132, 172, 232, 237, 201, 93, 201, 97, 143, 128, 63, 191, 199, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 100, 30, 135, 21, 108, 100, 32, 40, 110, 110, 110, 110, 41, 44, 60, 98, 99, 44, 100, 101, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 34, 3, 1, 0, 3, 208, 114, 119, 83, 127, 114, 63, 234, 100, 128, 225, 16, 45, 233, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 163, 96, 139, 71, 108, 100, 32, 40, 110, 110, 110, 110, 41, 44, 104, 108, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 115, 3, 1, 220, 192, 214, 209, 90, 237, 86, 243, 218, 175, 167, 108, 68, 159, 10, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 22, 88, 95, 215, 108, 100, 32, 40, 110, 110, 110, 110, 41, 44, 115, 112, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 34, 3, 1, 195, 108, 145, 13, 0, 105, 248, 142, 214, 227, 247, 195, 198, 217, 223, 194, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 186, 16, 42, 107, 108, 100, 32, 40, 110, 110, 110, 110, 41, 44, 60, 105, 120, 44, 105, 121, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 1, 0, 0, 0, 28, 92, 70, 45, 185, 142, 120, 96, 177, 116, 14, 179, 70, 209, 204, 48, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 222, 57, 25, 105, 108, 100, 32, 60, 98, 99, 44, 100, 101, 44, 104, 108, 44, 115, 112, 62, 44, 110, 110, 110, 110, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 33, 0, 0, 232, 135, 6, 32, 18, 189, 155, 182, 83, 114, 229, 161, 81, 19, 189, 241, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 125, 213, 37, 108, 100, 32, 60, 105, 120, 44, 105, 121, 62, 44, 110, 110, 110, 110, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 10, 0, 0, 0, 168, 179, 42, 29, 142, 127, 172, 66, 3, 1, 3, 1, 198, 177, 142, 239, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 255, 0, 0, 176, 129, 137, 53, 108, 100, 32, 97, 44, 60, 40, 98, 99, 41, 44, 40, 100, 101, 41, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 6, 0, 0, 0, 7, 196, 157, 244, 61, 209, 57, 3, 137, 222, 85, 116, 83, 192, 9, 85, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 241, 218, 181, 86, 108, 100, 32, 60, 98, 44, 99, 44, 100, 44, 101, 44, 104, 44, 108, 44, 40, 104, 108, 41, 44, 97, 62, 44, 110, 110, 46, 46, 46, 46, 36, 215, 221, 54, 1, 0, 69, 27, 2, 1, 2, 1, 193, 213, 199, 97, 196, 189, 192, 133, 22, 205, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 38, 219, 71, 126, 108, 100, 32, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 44, 110, 110, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 70, 1, 0, 22, 208, 2, 1, 2, 1, 96, 66, 57, 127, 4, 4, 151, 74, 133, 208, 32, 24, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 17, 6, 168, 108, 100, 32, 60, 98, 44, 99, 44, 100, 44, 101, 62, 44, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 36, 215, 221, 102, 1, 0, 224, 132, 2, 1, 2, 1, 82, 156, 153, 167, 182, 73, 147, 0, 173, 238, 32, 8, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 250, 42, 77, 3, 108, 100, 32, 60, 104, 44, 108, 62, 44, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 126, 1, 0, 182, 216, 2, 1, 2, 1, 18, 198, 7, 223, 208, 156, 67, 166, 229, 160, 32, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 233, 172, 100, 108, 100, 32, 97, 44, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 38, 0, 0, 83, 60, 64, 70, 121, 225, 17, 119, 7, 193, 250, 26, 129, 173, 155, 93, 32, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 36, 232, 130, 139, 108, 100, 32, 60, 105, 120, 104, 44, 105, 120, 108, 44, 105, 121, 104, 44, 105, 121, 108, 62, 44, 110, 110, 46, 46, 46, 46, 46, 46, 46, 36, 215, 64, 0, 0, 0, 164, 114, 36, 160, 172, 97, 3, 1, 199, 130, 143, 113, 151, 143, 142, 239, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 215, 255, 0, 0, 116, 75, 1, 24, 108, 100, 32, 60, 98, 99, 100, 101, 104, 108, 97, 62, 44, 60, 98, 99, 100, 101, 104, 108, 97, 62, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 64, 0, 0, 197, 188, 3, 1, 3, 1, 3, 1, 194, 47, 192, 152, 131, 31, 205, 59, 32, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 215, 255, 0, 0, 71, 139, 163, 107, 108, 100, 32, 60, 98, 99, 100, 101, 120, 121, 97, 62, 44, 60, 98, 99, 100, 101, 120, 121, 97, 62, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 50, 3, 1, 0, 104, 253, 236, 244, 160, 68, 67, 181, 83, 6, 186, 205, 210, 79, 216, 31, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 255, 0, 0, 201, 38, 45, 229, 108, 100, 32, 97, 44, 40, 110, 110, 110, 110, 41, 32, 47, 32, 108, 100, 32, 40, 110, 110, 110, 110, 41, 44, 97, 46, 46, 46, 46, 46, 36, 215, 237, 168, 0, 0, 82, 152, 250, 104, 161, 102, 6, 1, 4, 1, 1, 0, 193, 104, 183, 32, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 148, 244, 39, 105, 108, 100, 100, 60, 114, 62, 32, 40, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 168, 0, 0, 46, 241, 42, 235, 186, 213, 6, 1, 4, 1, 2, 0, 71, 255, 228, 251, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 90, 144, 126, 212, 108, 100, 100, 60, 114, 62, 32, 40, 50, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 160, 0, 0, 48, 254, 205, 3, 88, 96, 5, 1, 3, 1, 1, 0, 4, 96, 136, 38, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 154, 189, 246, 181, 108, 100, 105, 60, 114, 62, 32, 40, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 160, 0, 0, 206, 74, 110, 194, 136, 177, 5, 1, 3, 1, 2, 0, 20, 45, 159, 163, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 235, 89, 137, 27, 108, 100, 105, 60, 114, 62, 32, 40, 50, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 68, 0, 0, 162, 56, 107, 95, 52, 217, 228, 87, 214, 210, 66, 70, 67, 90, 204, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 106, 60, 59, 189, 110, 101, 103, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 237, 103, 0, 0, 203, 145, 139, 196, 98, 250, 3, 1, 32, 231, 121, 180, 64, 6, 226, 138, 0, 8, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 255, 0, 0, 149, 91, 163, 38, 60, 114, 114, 100, 44, 114, 108, 100, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 7, 0, 0, 0, 146, 203, 67, 109, 144, 10, 132, 194, 83, 12, 14, 245, 145, 235, 252, 64, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 37, 19, 48, 174, 60, 114, 108, 99, 97, 44, 114, 114, 99, 97, 44, 114, 108, 97, 44, 114, 114, 97, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 203, 1, 6, 175, 221, 2, 1, 2, 1, 60, 255, 246, 219, 244, 148, 130, 128, 217, 97, 32, 0, 0, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 87, 0, 0, 0, 113, 58, 205, 129, 115, 104, 102, 47, 114, 111, 116, 32, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 203, 0, 0, 0, 235, 204, 74, 93, 7, 224, 3, 1, 149, 19, 238, 48, 67, 120, 173, 61, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 87, 255, 0, 0, 235, 96, 77, 88, 115, 104, 102, 47, 114, 111, 116, 32, 60, 98, 44, 99, 44, 100, 44, 101, 44, 104, 44, 108, 44, 40, 104, 108, 41, 44, 97, 62, 46, 46, 36, 215, 203, 128, 0, 0, 213, 44, 171, 151, 255, 57, 3, 1, 75, 209, 178, 106, 83, 39, 56, 181, 0, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 215, 255, 0, 0, 139, 87, 240, 8, 60, 115, 101, 116, 44, 114, 101, 115, 62, 32, 110, 44, 60, 98, 99, 100, 101, 104, 108, 40, 104, 108, 41, 97, 62, 46, 46, 46, 46, 46, 36, 215, 221, 203, 1, 134, 68, 251, 2, 1, 2, 1, 9, 186, 190, 104, 216, 50, 16, 94, 103, 168, 32, 0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 204, 99, 249, 138, 60, 115, 101, 116, 44, 114, 101, 115, 62, 32, 110, 44, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 112, 1, 0, 13, 39, 2, 1, 2, 1, 58, 183, 123, 136, 238, 153, 134, 112, 7, 202, 32, 3, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 4, 98, 106, 191, 108, 100, 32, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 44, 60, 98, 44, 99, 44, 100, 44, 101, 62, 46, 46, 46, 46, 46, 46, 36, 215, 221, 116, 1, 0, 100, 182, 2, 1, 2, 1, 172, 232, 245, 181, 254, 170, 18, 16, 102, 149, 32, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 106, 26, 136, 49, 108, 100, 32, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 44, 60, 104, 44, 108, 62, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 221, 119, 1, 0, 175, 103, 2, 1, 2, 1, 19, 79, 68, 6, 215, 188, 80, 172, 175, 95, 32, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 204, 190, 90, 150, 108, 100, 32, 40, 60, 105, 120, 44, 105, 121, 62, 43, 49, 41, 44, 97, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 215, 2, 0, 0, 0, 59, 12, 146, 181, 255, 108, 158, 149, 3, 1, 4, 1, 193, 33, 231, 189, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 122, 76, 17, 79, 108, 100, 32, 40, 60, 98, 99, 44, 100, 101, 62, 41, 44, 97, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 36, 229, 126, 35, 102, 111, 126, 50, 101, 29, 35, 229, 17, 20, 0, 25, 17, 218, 28, 205, 73, 28, 225, 229, 17, 40, 0, 25, 17, 2, 29, 205, 73, 28, 33, 2, 29, 54, 1, 225, 229, 17, 66, 29, 1, 4, 0, 237, 176, 17, 3, 1, 1, 16, 0, 237, 176, 17, 44, 0, 25, 235, 14, 9, 205, 206, 29, 205, 113, 30, 58, 66, 29, 254, 118, 202, 62, 27, 230, 223, 254, 221, 194, 59, 27, 58, 67, 29, 254, 118, 196, 42, 29, 205, 137, 28, 196, 173, 28, 225, 202, 122, 27, 17, 60, 0, 25, 205, 50, 30, 17, 5, 30, 202, 113, 27, 17, 12, 30, 14, 9, 205, 206, 29, 205, 153, 29, 17, 39, 30, 14, 9, 205, 206, 29, 33, 133, 30, 205, 153, 29, 17, 47, 30, 14, 9, 205, 206, 29, 225, 35, 35, 201, 229, 62, 1, 50, 240, 27, 50, 20, 28, 33, 218, 28, 34, 241, 27, 33, 2, 29, 34, 21, 28, 6, 4, 225, 229, 17, 66, 29, 205, 164, 27, 6, 16, 17, 3, 1, 205, 164, 27, 195, 39, 27, 205, 173, 27, 35, 5, 194, 164, 27, 201, 197, 213, 229, 78, 17, 20, 0, 25, 126, 254, 0, 202, 206, 27, 6, 8, 15, 245, 62, 0, 220, 243, 27, 169, 15, 79, 241, 5, 194, 189, 27, 6, 8, 17, 20, 0, 25, 126, 254, 0, 202, 233, 27, 6, 8, 15, 245, 62, 0, 220, 23, 28, 169, 15, 79, 241, 5, 194, 218, 27, 225, 209, 121, 18, 19, 193, 201, 0, 0, 0, 197, 229, 42, 241, 27, 70, 33, 240, 27, 126, 79, 7, 119, 254, 1, 194, 12, 28, 42, 241, 27, 35, 34, 241, 27, 120, 161, 225, 193, 200, 62, 1, 201, 0, 0, 0, 197, 229, 42, 21, 28, 70, 33, 20, 28, 126, 79, 7, 119, 254, 1, 194, 48, 28, 42, 21, 28, 35, 34, 21, 28, 120, 161, 225, 193, 200, 62, 1, 201, 245, 197, 213, 229, 54, 0, 84, 93, 19, 11, 237, 176, 225, 209, 193, 241, 201, 213, 235, 1, 40, 0, 205, 56, 28, 235, 6, 20, 14, 1, 22, 0, 94, 123, 161, 202, 95, 28, 20, 121, 7, 79, 254, 1, 194, 89, 28, 35, 5, 194, 88, 28, 122, 230, 248, 15, 15, 15, 111, 38, 0, 122, 230, 7, 60, 71, 62, 128, 7, 5, 194, 124, 28, 209, 25, 17, 20, 0, 25, 119, 201, 197, 213, 229, 33, 218, 28, 17, 20, 0, 235, 25, 235, 52, 126, 254, 0, 202, 168, 28, 71, 26, 160, 202, 164, 28, 54, 0, 193, 209, 225, 201, 35, 19, 195, 149, 28, 197, 213, 229, 33, 2, 29, 17, 20, 0, 235, 25, 235, 126, 183, 202, 213, 28, 71, 26, 160, 194, 209, 28, 120, 7, 254, 1, 194, 207, 28, 54, 0, 35, 19, 119, 175, 225, 209, 193, 201, 35, 19, 195, 185, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 245, 197, 213, 229, 243, 237, 115, 141, 29, 49, 5, 1, 253, 225, 221, 225, 225, 209, 193, 241, 237, 123, 17, 1, 0, 0, 0, 0, 237, 115, 139, 29, 49, 139, 29, 245, 197, 213, 229, 221, 229, 253, 229, 237, 123, 141, 29, 251, 42, 3, 1, 34, 125, 29, 33, 137, 29, 126, 230, 215, 119, 6, 16, 17, 125, 29, 33, 133, 30, 26, 19, 205, 73, 30, 5, 194, 111, 29, 225, 209, 193, 241, 201, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 126, 205, 171, 29, 35, 5, 194, 143, 29, 201, 245, 197, 229, 6, 4, 126, 205, 171, 29, 35, 5, 194, 158, 29, 225, 193, 241, 201, 245, 15, 15, 15, 15, 205, 180, 29, 241, 245, 197, 213, 229, 230, 15, 254, 10, 218, 193, 29, 198, 39, 198, 48, 95, 14, 2, 205, 206, 29, 225, 209, 193, 241, 201, 245, 197, 213, 229, 205, 5, 0, 225, 209, 193, 241, 201, 90, 56, 48, 32, 105, 110, 115, 116, 114, 117, 99, 116, 105, 111, 110, 32, 101, 120, 101, 114, 99, 105, 115, 101, 114, 10, 13, 36, 84, 101, 115, 116, 115, 32, 99, 111, 109, 112, 108, 101, 116, 101, 36, 32, 32, 79, 75, 10, 13, 36, 32, 32, 69, 82, 82, 79, 82, 32, 42, 42, 42, 42, 32, 99, 114, 99, 32, 101, 120, 112, 101, 99, 116, 101, 100, 58, 36, 32, 102, 111, 117, 110, 100, 58, 36, 10, 13, 36, 197, 213, 229, 17, 133, 30, 6, 4, 26, 190, 194, 69, 30, 35, 19, 5, 194, 58, 30, 225, 209, 193, 201, 245, 197, 213, 229, 229, 17, 3, 0, 25, 174, 111, 38, 0, 41, 41, 235, 33, 137, 30, 25, 235, 225, 1, 4, 0, 26, 168, 70, 119, 19, 35, 13, 194, 98, 30, 225, 209, 193, 241, 201, 245, 197, 229, 33, 133, 30, 62, 255, 6, 4, 119, 35, 5, 194, 123, 30, 225, 193, 241, 201, 0, 0, 0, 0, 0, 0, 0, 0, 119, 7, 48, 150, 238, 14, 97, 44, 153, 9, 81, 186, 7, 109, 196, 25, 112, 106, 244, 143, 233, 99, 165, 53, 158, 100, 149, 163, 14, 219, 136, 50, 121, 220, 184, 164, 224, 213, 233, 30, 151, 210, 217, 136, 9, 182, 76, 43, 126, 177, 124, 189, 231, 184, 45, 7, 144, 191, 29, 145, 29, 183, 16, 100, 106, 176, 32, 242, 243, 185, 113, 72, 132, 190, 65, 222, 26, 218, 212, 125, 109, 221, 228, 235, 244, 212, 181, 81, 131, 211, 133, 199, 19, 108, 152, 86, 100, 107, 168, 192, 253, 98, 249, 122, 138, 101, 201, 236, 20, 1, 92, 79, 99, 6, 108, 217, 250, 15, 61, 99, 141, 8, 13, 245, 59, 110, 32, 200, 76, 105, 16, 94, 213, 96, 65, 228, 162, 103, 113, 114, 60, 3, 228, 209, 75, 4, 212, 71, 210, 13, 133, 253, 165, 10, 181, 107, 53, 181, 168, 250, 66, 178, 152, 108, 219, 187, 201, 214, 172, 188, 249, 64, 50, 216, 108, 227, 69, 223, 92, 117, 220, 214, 13, 207, 171, 209, 61, 89, 38, 217, 48, 172, 81, 222, 0, 58, 200, 215, 81, 128, 191, 208, 97, 22, 33, 180, 244, 181, 86, 179, 196, 35, 207, 186, 149, 153, 184, 189, 165, 15, 40, 2, 184, 158, 95, 5, 136, 8, 198, 12, 217, 178, 177, 11, 233, 36, 47, 111, 124, 135, 88, 104, 76, 17, 193, 97, 29, 171, 182, 102, 45, 61, 118, 220, 65, 144, 1, 219, 113, 6, 152, 210, 32, 188, 239, 213, 16, 42, 113, 177, 133, 137, 6, 182, 181, 31, 159, 191, 228, 165, 232, 184, 212, 51, 120, 7, 201, 162, 15, 0, 249, 52, 150, 9, 168, 142, 225, 14, 152, 24, 127, 106, 13, 187, 8, 109, 61, 45, 145, 100, 108, 151, 230, 99, 92, 1, 107, 107, 81, 244, 28, 108, 97, 98, 133, 101, 48, 216, 242, 98, 0, 78, 108, 6, 149, 237, 27, 1, 165, 123, 130, 8, 244, 193, 245, 15, 196, 87, 101, 176, 217, 198, 18, 183, 233, 80, 139, 190, 184, 234, 252, 185, 136, 124, 98, 221, 29, 223, 21, 218, 45, 73, 140, 211, 124, 243, 251, 212, 76, 101, 77, 178, 97, 88, 58, 181, 81, 206, 163, 188, 0, 116, 212, 187, 48, 226, 74, 223, 165, 65, 61, 216, 149, 215, 164, 209, 196, 109, 211, 214, 244, 251, 67, 105, 233, 106, 52, 110, 217, 252, 173, 103, 136, 70, 218, 96, 184, 208, 68, 4, 45, 115, 51, 3, 29, 229, 170, 10, 76, 95, 221, 13, 124, 201, 80, 5, 113, 60, 39, 2, 65, 170, 190, 11, 16, 16, 201, 12, 32, 134, 87, 104, 181, 37, 32, 111, 133, 179, 185, 102, 212, 9, 206, 97, 228, 159, 94, 222, 249, 14, 41, 217, 201, 152, 176, 208, 152, 34, 199, 215, 168, 180, 89, 179, 61, 23, 46, 180, 13, 129, 183, 189, 92, 59, 192, 186, 108, 173, 237, 184, 131, 32, 154, 191, 179, 182, 3, 182, 226, 12, 116, 177, 210, 154, 234, 213, 71, 57, 157, 210, 119, 175, 4, 219, 38, 21, 115, 220, 22, 131, 227, 99, 11, 18, 148, 100, 59, 132, 13, 109, 106, 62, 122, 106, 90, 168, 228, 14, 207, 11, 147, 9, 255, 157, 10, 0, 174, 39, 125, 7, 158, 177, 240, 15, 147, 68, 135, 8, 163, 210, 30, 1, 242, 104, 105, 6, 194, 254, 247, 98, 87, 93, 128, 101, 103, 203, 25, 108, 54, 113, 110, 107, 6, 231, 254, 212, 27, 118, 137, 211, 43, 224, 16, 218, 122, 90, 103, 221, 74, 204, 249, 185, 223, 111, 142, 190, 239, 249, 23, 183, 190, 67, 96, 176, 142, 213, 214, 214, 163, 232, 161, 209, 147, 126, 56, 216, 194, 196, 79, 223, 242, 82, 209, 187, 103, 241, 166, 188, 87, 103, 63, 181, 6, 221, 72, 178, 54, 75, 216, 13, 43, 218, 175, 10, 27, 76, 54, 3, 74, 246, 65, 4, 122, 96, 223, 96, 239, 195, 168, 103, 223, 85, 49, 110, 142, 239, 70, 105, 190, 121, 203, 97, 179, 140, 188, 102, 131, 26, 37, 111, 210, 160, 82, 104, 226, 54, 204, 12, 119, 149, 187, 11, 71, 3, 34, 2, 22, 185, 85, 5, 38, 47, 197, 186, 59, 190, 178, 189, 11, 40, 43, 180, 90, 146, 92, 179, 106, 4, 194, 215, 255, 167, 181, 208, 207, 49, 44, 217, 158, 139, 91, 222, 174, 29, 155, 100, 194, 176, 236, 99, 242, 38, 117, 106, 163, 156, 2, 109, 147, 10, 156, 9, 6, 169, 235, 14, 54, 63, 114, 7, 103, 133, 5, 0, 87, 19, 149, 191, 74, 130, 226, 184, 122, 20, 123, 177, 43, 174, 12, 182, 27, 56, 146, 210, 142, 155, 229, 213, 190, 13, 124, 220, 239, 183, 11, 219, 223, 33, 134, 211, 210, 212, 241, 212, 226, 66, 104, 221, 179, 248, 31, 218, 131, 110, 129, 190, 22, 205, 246, 185, 38, 91, 111, 176, 119, 225, 24, 183, 71, 119, 136, 8, 90, 230, 255, 15, 106, 112, 102, 6, 59, 202, 17, 1, 11, 92, 143, 101, 158, 255, 248, 98, 174, 105, 97, 107, 255, 211, 22, 108, 207, 69, 160, 10, 226, 120, 215, 13, 210, 238, 78, 4, 131, 84, 57, 3, 179, 194, 167, 103, 38, 97, 208, 96, 22, 247, 73, 105, 71, 77, 62, 110, 119, 219, 174, 209, 106, 74, 217, 214, 90, 220, 64, 223, 11, 102, 55, 216, 59, 240, 169, 188, 174, 83, 222, 187, 158, 197, 71, 178, 207, 127, 48, 181, 255, 233, 189, 189, 242, 28, 202, 186, 194, 138, 83, 179, 147, 48, 36, 180, 163, 166, 186, 208, 54, 5, 205, 215, 6, 147, 84, 222, 87, 41, 35, 217, 103, 191, 179, 102, 122, 46, 196, 97, 74, 184, 93, 104, 27, 2, 42, 111, 43, 148, 180, 11, 190, 55, 195, 12, 142, 161, 90, 5, 223, 27, 45, 2, 239, 141, 205, 43, 201, 205, 168, 61, 195, 239, 60, 221, 203, 40, 198, 18, 19, 126, 35, 18, 19, 254, 13, 40, 16, 254, 39, 32, 244, 235, 205, 192, 57, 235, 221, 203, 40, 70, 200, 24, 232, 43, 27, 201, 225, 209, 27, 26, 254, 38, 40, 1, 19, 213, 233, 14, 1, 221, 70, 20, 42, 53, 65, 26, 254, 13, 40, 33, 205, 63, 60, 40, 15, 190, 32, 12, 43, 27, 16, 239, 26, 205, 63, 60, 32, 2, 183, 201, 26, 27, 254, 13, 40, 7, 254, 44, 32, 246, 12, 24, 212, 55, 201, 58, 63, 65, 183, 40, 41, 71, 237, 91, 59, 65, 33, 78, 66, 58, 173, 21, 183}
  3119.  
  3120. -- for i=0,#rom - 1 do
  3121. -- m:poke8(i + 0x100, rom[i+1])
  3122. -- end
  3123.  
  3124.  
  3125. m:poke8(0, 0xC3)
  3126. m:poke8(1, 0x00)
  3127. m:poke8(2, 0x01)
  3128. m:poke8(5, 0xC9)
  3129.  
  3130. function toBits(num)
  3131. local t = {}
  3132. while num > 0 do
  3133. rest = num % 2
  3134. table.insert(t, 1, math.floor(rest))
  3135. num = (num - rest) / 2
  3136. end
  3137. return table.concat(t)
  3138. end
  3139.  
  3140. function yield()
  3141. os.queueEvent( "sleep" )
  3142. coroutine.yield( "sleep" )
  3143. end
  3144.  
  3145. -- x.a = 255
  3146. -- x.b = 255
  3147. -- x.c = 255
  3148. -- x.d = 255
  3149. -- x.e = 255
  3150. -- x.h = 255
  3151. -- x.l = 255
  3152.  
  3153. --x.flags.Y = false
  3154.  
  3155. file = io.open("output.txt", "w")
  3156. --io.output(file)
  3157.  
  3158. running = true
  3159. --print(toBits(x.get_flags_register(x)))
  3160. while running do
  3161. --print("PC: "..x.pc.." - "..string.format("0x%x", m:peek8(x.pc)).." "..m:peek8(x.pc).." A:"..x.a.." B:"..x.b.. " C:"..x.c.." D:"..x.d.." E:"..x.e.." H:"..x.h.." L:"..x.l.." Flags: S:"..tostring(x.flags.S).." Z:"..tostring(x.flags.Z).." Y:"..tostring(x.flags.Y).." H:"..tostring(x.flags.H).." X:"..tostring(x.flags.X).." P:"..tostring(x.flags.P).." N:"..tostring(x.flags.N).." C:"..tostring(x.flags.C))
  3162. for i = 1, 2000000 do
  3163. --print("PC: "..x.pc.." - "..string.format("0x%x", m:peek8(x.pc)).." "..m:peek8(x.pc).." A:"..x.a.." B:"..x.b.. " C:"..x.c.." D:"..x.d.." E:"..x.e.." H:"..x.h.." L:"..x.l.." Flags: S:"..tostring(x.flags.S).." Z:"..tostring(x.flags.Z).." Y:"..tostring(x.flags.Y).." H:"..tostring(x.flags.H).." X:"..tostring(x.flags.X).." P:"..tostring(x.flags.P).." N:"..tostring(x.flags.N).." C:"..tostring(x.flags.C))
  3164. bits = ""
  3165.  
  3166. if (x.flags.C == true) then
  3167. bits = bits .. "1"
  3168. else
  3169. bits = bits .. "0"
  3170. end
  3171. if (x.flags.H == true) then
  3172. bits = bits .. "1"
  3173. else
  3174. bits = bits .. "0"
  3175. end
  3176. if (x.flags.N == true) then
  3177. bits = bits .. "1"
  3178. else
  3179. bits = bits .. "0"
  3180. end
  3181. if (x.flags.P == true) then
  3182. bits = bits .. "1"
  3183. else
  3184. bits = bits .. "0"
  3185. end
  3186. if (x.flags.S == true) then
  3187. bits = bits .. "1"
  3188. else
  3189. bits = bits .. "0"
  3190. end
  3191. if (x.flags.X == true) then
  3192. bits = bits .. "1"
  3193. else
  3194. bits = bits .. "0"
  3195. end
  3196. if (x.flags.Y == true) then
  3197. bits = bits .. "1"
  3198. else
  3199. bits = bits .. "0"
  3200. end
  3201. if (x.flags.Z == true) then
  3202. bits = bits .. "1"
  3203. else
  3204. bits = bits .. "0"
  3205. end
  3206. --io.write("I: "..(i-1).." PC: "..x.pc.." - "..m:peek8(x.pc).." A: "..x.a.." B: "..x.b.. " C: "..x.c.." D: "..x.d.." E: "..x.e.." H: "..x.h.." L: "..x.l.." F: "..bits.."\n")
  3207. x:run_instruction()
  3208.  
  3209. if (x.pc == 0x05) then
  3210. if (x.c == 0) then
  3211. print("Z80 reset after " .. x.cycle_counter .. " t-states")
  3212. running = false
  3213. break
  3214. end
  3215.  
  3216. if (x.c == 2) then
  3217. io.write(string.format("%2x", x.e))
  3218. end
  3219.  
  3220. if (x.c == 9) then
  3221. local de = bit.bor(bit.blshift(x.d, 8), x.e)
  3222. local strAddr = de
  3223. while (string.char(m:peek8(strAddr)) ~= "$") do
  3224. io.write(string.char(m:peek8(strAddr)))
  3225. strAddr = strAddr + 1
  3226. end
  3227.  
  3228. end
  3229. --print()
  3230. end
  3231. end
  3232.  
  3233. --io.write("+")
  3234. io.flush()
  3235. yield()
  3236. end
  3237.  
  3238. io.close(file)
  3239. --print("done after: "..cycles.." cycles.")
  3240. -- do_\
RAW Paste Data