Advertisement
karelvysinka

z80

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