Advertisement
Redxone

[Love2D] 6502 Emulator (WIP)

Sep 27th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.68 KB | None | 0 0
  1. -- DO NOT REMOVE THESE LINES --
  2. -- Original Emulation template created by: Lewisk3 (Redxone)
  3. -- Original template designed for: 6502ASM (8bit)
  4.  
  5.  
  6. io.stdout:setvbuf("no")
  7.  
  8.  
  9. -- notes for branching
  10. -- Init mem
  11. _RAM = ""
  12. _REGS = {}
  13. _FLAGS = 0x20
  14. _ERRS = {}
  15. takingInput = true
  16. chrBuff = ""
  17. code = {}
  18.  
  19. --[0xFF] = function() -- AAA AA
  20. --end,
  21.  
  22.  
  23. OpCodes = {
  24.  
  25.  
  26.   ---------- LDA: 1 ---------------
  27.   [0xA9] = function() -- LDA IMM
  28.       writeReg('A', readNext())
  29.       checkMemFlags(readReg('A'))
  30.       stepPC(1)
  31.   end,
  32.  
  33.   [0xA5] = function() -- LDA ZP
  34.       writeReg('A', readByte(readNext()))
  35.       checkMemFlags(readReg('A'))
  36.       stepPC(1)
  37.   end,
  38.  
  39.   [0xB5] = function() -- LDA ZPX
  40.       writeReg('A', readByte(readNext()+readReg('X')))
  41.       checkMemFlags(readReg('A'))
  42.       stepPC(1)
  43.   end,
  44.    
  45.   [0xAD] = function() -- LDA AB
  46.       -- Take 2 next bytes concatenate them then take the data from that location.
  47.       writeReg('A', readByte(byteConcate(readNext(1),readNext())))
  48.       checkMemFlags(readReg('A'))
  49.       stepPC(2)
  50.   end,
  51.  
  52.   [0xBD] = function() -- LDA ABX
  53.       writeReg('A', readByte( byteConcate(readNext(1),readNext())+readReg('X') ))
  54.       checkMemFlags(readReg('A'))
  55.       stepPC(2)
  56.   end,
  57.  
  58.   [0xB9] = function() -- LDA ABY
  59.       writeReg('A', readByte( byteConcate(readNext(1),readNext())+readReg('Y') ))
  60.       checkMemFlags(readReg('A'))
  61.       stepPC(2)
  62.   end,
  63.  
  64.   [0xA1] = function() -- LDA INX
  65.       writeReg('A', readIndirect(readNext(),'X'))
  66.       checkMemFlags(readReg('A'))
  67.       stepPC(1)
  68.   end,
  69.  
  70.   [0xB1] = function() -- LDA INY
  71.       writeReg('A', readIndirect(readNext(),'Y'))
  72.       checkMemFlags(readReg('A'))
  73.       stepPC(1)
  74.   end,
  75.   ---------- LDX: 2 ---------------
  76.   [0xA2] = function() -- LDX IMM
  77.       writeReg('X', readNext())
  78.       checkMemFlags(readReg('X'))
  79.       stepPC(1)
  80.   end,
  81.  
  82.   [0xA6] = function() -- LDX ZP
  83.       writeReg('X', readByte(readNext()))
  84.       checkMemFlags(readReg('X'))
  85.       stepPC(1)
  86.   end,
  87.  
  88.   [0xB6] = function() -- LDX ZPY
  89.       writeReg('X', readByte(readNext()+readReg('Y')))
  90.       checkMemFlags(readReg('X'))
  91.       stepPC(1)
  92.   end,
  93.    
  94.   [0xAE] = function() -- LDX AB
  95.       -- Take 2 next bytes concatenate them then take the data from that location.
  96.       writeReg('X', readByte(byteConcate(readNext(1),readNext())))
  97.       checkMemFlags(readReg('X'))
  98.       stepPC(2)
  99.   end,
  100.  
  101.   [0xBE] = function() -- LDX ABY
  102.       writeReg('X', readByte( byteConcate(readNext(1),readNext())+readReg('Y') ))
  103.       checkMemFlags(readReg('X'))
  104.       stepPC(2)
  105.   end,
  106.   ---------- LDY: 3 ---------------
  107.   [0xA0] = function() -- LDY IMM
  108.       writeReg('Y', readNext())
  109.       checkMemFlags(readReg('Y'))
  110.       stepPC(1)
  111.   end,
  112.  
  113.   [0xA4] = function() -- LDY ZP
  114.       writeReg('Y', readByte(readNext()))
  115.       checkMemFlags(readReg('Y'))
  116.       stepPC(1)
  117.   end,
  118.  
  119.   [0xB4] = function() -- LDY ZPX
  120.       writeReg('Y', readByte(readNext()+readReg('X')))
  121.       checkMemFlags(readReg('Y'))
  122.       stepPC(1)
  123.   end,
  124.    
  125.   [0xAC] = function() -- LDY AB
  126.       -- Take 2 next bytes concatenate them then take the data from that location.
  127.       writeReg('Y', readByte(byteConcate(readNext(1),readNext())))
  128.       checkMemFlags(readReg('Y'))
  129.       stepPC(2)
  130.   end,
  131.  
  132.   [0xBC] = function() -- LDY ABX
  133.       writeReg('Y', readByte( byteConcate(readNext(1),readNext())+readReg('X') ))
  134.       checkMemFlags(readReg('Y'))
  135.       stepPC(2)
  136.   end,
  137.   ---------- STA: 4 ---------------
  138.   [0x85] = function() -- STA ZP
  139.       -- Take A and put it on the ZP
  140.       writeByte(readNext(),readReg('A'))
  141.       stepPC(1)
  142.   end,
  143.   [0x95] = function() -- STA ZPX
  144.       -- Take A and put it on the ZP+X
  145.       writeByte(readNext()+readReg('X'),readReg('A'))
  146.       stepPC(1)
  147.   end,
  148.   [0x8D] = function() -- STA AB
  149.       writeByte(byteConcate(readNext(1), readNext()),readReg('A'))
  150.       stepPC(2)
  151.   end,
  152.   [0x9D] = function() -- STA ABX
  153.       writeByte(byteConcate(readNext(1), readNext())+readReg('X'),readReg('A'))
  154.       stepPC(2)
  155.   end,
  156.   [0x99] = function() -- STA ABY
  157.       writeByte(byteConcate(readNext(1), readNext())+readReg('Y'),readReg('A'))
  158.       stepPC(2)
  159.   end,
  160.   [0x81] = function() -- STA INX
  161.       writeByte(readIndirect(readNext(),'X'),readReg('A'))
  162.       stepPC(1)
  163.   end,
  164.   [0x91] = function() -- STA INY
  165.       writeByte(readIndirect(readNext(),'Y'),readReg('A'))
  166.       stepPC(1)
  167.   end,
  168.   ---------- STX: 5 ---------------
  169.   [0x86] = function() -- STX ZP
  170.       -- Take A and put it on the ZP
  171.       writeByte(readNext(),readReg('X'))
  172.       stepPC(1)
  173.   end,
  174.   [0x96] = function() -- STX ZPY
  175.       -- Take A and put it on the ZP+Y
  176.       writeByte(readNext()+readReg('Y'),readReg('X'))
  177.       stepPC(1)
  178.   end,
  179.   [0x8E] = function() -- STX AB
  180.       writeByte(byteConcate(readNext(1), readNext()),readReg('X'))
  181.       stepPC(2)
  182.   end,
  183.   ---------- STY: 6 --------------
  184.   [0x84] = function() -- STY ZP
  185.       -- Take A and put it on the ZP
  186.       writeByte(readNext(),readReg('Y'))
  187.       stepPC(1)
  188.   end,
  189.   [0x94] = function() -- STY ZPX
  190.       -- Take A and put it on the ZP+Y
  191.       writeByte(readNext()+readReg('X'),readReg('Y'))
  192.       stepPC(1)
  193.   end,
  194.   [0x8C] = function() -- STY AB
  195.       writeByte(byteConcate(readNext(1), readNext()),readReg('Y'))
  196.       stepPC(2)
  197.   end,
  198.   ------------ TAX: 7  --------------
  199.   [0xAA] = function() -- TAX IMP
  200.        writeReg('X', readReg('A'))
  201.   end,
  202.   ------------ TAY: 8  --------------
  203.   [0xA8] = function() -- TAX IMP
  204.        writeReg('Y', readReg('A'))
  205.   end,
  206.   ------------ TXA: 9  --------------
  207.   [0x8A] = function() -- TXA IMP
  208.        writeReg('A', readReg('X'))
  209.   end,
  210.   ------------ TYA: A  --------------
  211.   [0x98] = function() -- TYA IMP
  212.        writeReg('A', readReg('Y'))
  213.   end,
  214.   ------------ TSX: B  --------------
  215.   [0x9A] = function() -- TSX IMP
  216.        writeReg('X', readReg('SP'))
  217.   end,
  218.   ------------ TXS: C  --------------
  219.   [0xBA] = function() -- TXS IMP
  220.        writeReg('SP', readReg('X'))
  221.   end,
  222.   ------------ ADC: D  --------------
  223.   ------------ SBC: E  --------------
  224.   ------------ AND: F  --------------
  225.   ------------ ASL: 10 --------------
  226.   ------------ BCC: 11 --------------
  227.   ------------ BCS: 12 --------------
  228.   ------------ BEQ: 13 --------------
  229.   ------------ BIT: 14 --------------
  230.   ------------ BMI: 15 --------------
  231.   ------------ BNE: 16 --------------
  232.   ------------ BPL: 17 --------------
  233.   ------------ BRK: 18 --------------
  234.   ------------ BVC: 19 --------------
  235.   ------------ BVS: 1A --------------
  236.   ------------ CLC: 1B --------------
  237.   ------------ CLD: 1C --------------
  238.   ------------ CLI: 1D --------------
  239.   ------------ CLV: 1E --------------
  240.   ------------ CMP: 1F --------------
  241.   ------------ CPX: 20 --------------
  242.   ------------ CPY: 21 --------------
  243.   ------------ DEC: 22 --------------
  244.   ------------ DEX: 23 --------------
  245.   ------------ DEY: 24 --------------
  246.   ------------ EOR: 25 --------------
  247.   ------------ INC: 26 --------------
  248.   ------------ INX: 27 --------------
  249.   ------------ INY: 28 --------------
  250.   ------------ JMP: 29 --------------
  251.   ------------ JSR: 2A --------------
  252.   ------------ LSR: 2B --------------
  253.   ------------ NOP: 2C --------------
  254.   ------------ ORA: 2D --------------
  255.   ------------ PHA: 2E --------------
  256.   ------------ PHP: 2F --------------
  257.   ------------ PLA: 30 --------------
  258.   ------------ PLP: 31 --------------
  259.   ------------ ROL: 32 --------------
  260.   ------------ ROR: 33 --------------
  261.   ------------ RTI: 34 --------------
  262.   ------------ RTS: 35 --------------
  263.   ------------ SEC: 36 --------------
  264.   ------------ SED: 37 --------------
  265.   ------------ SEI: 38 --------------
  266. }
  267.  
  268.  
  269. local colors = {
  270.   nes = {
  271.      -- (https://wiki.nesdev.com/w/index.php/PPU_palettes)
  272.      [0x00] = {84,84,84},  
  273.      [0x01] = {0,30,116},
  274.      [0x02] = {8,16,144},
  275.      [0x03] = {48,0,136},
  276.      [0x04] = {68,0,100},
  277.      [0x05] = {92,0,48},
  278.      [0x06] = {84,4,0},
  279.      [0x07] = {60,24,0},
  280.      [0x08] = {32,42,0},
  281.      [0x09] = {8,58,0},
  282.      [0x0A] = {0,64,0},
  283.      [0x0B] = {0,60,0},
  284.      [0x0C] = {0,50,60},
  285.      [0x0D] = {0,0,0},
  286.      [0x0E] = {0,0,0},
  287.      [0x0F] = {0,0,0},
  288.      
  289.      [0x10] = {152,150,152},  
  290.      [0x11] = {8,76,196},
  291.      [0x12] = {48,50,236},
  292.      [0x13] = {92,30,228},
  293.      [0x14] = {136,20,176},
  294.      [0x15] = {160,20,100},
  295.      [0x16] = {152,34,32},
  296.      [0x17] = {120,60,0},
  297.      [0x18] = {84,90,0},
  298.      [0x19] = {40,114,0},
  299.      [0x1A] = {8,124,0},
  300.      [0x1B] = {0,118,40},
  301.      [0x1C] = {0,102,120},
  302.      [0x1D] = {0,0,0},
  303.      [0x1E] = {0,0,0},
  304.      [0x1F] = {0,0,0},
  305.      
  306.      [0x20] = {236,238,236},  
  307.      [0x21] = {76,154,236},
  308.      [0x22] = {120,124,236},
  309.      [0x23] = {176,98,236},
  310.      [0x24] = {228,84,236},
  311.      [0x25] = {236,88,180},
  312.      [0x26] = {236,106,100},
  313.      [0x27] = {212,136,32},
  314.      [0x28] = {160,170,0},
  315.      [0x29] = {116,196,0},
  316.      [0x2A] = {76,208,32},
  317.      [0x2B] = {56,204,108},
  318.      [0x2C] = {56,180,204},
  319.      [0x2D] = {60,60,60},
  320.      [0x2E] = {0,0,0},
  321.      [0x2F] = {0,0,0},
  322.      
  323.      [0x30] = {236,238,236},
  324.      [0x31] = {168,204,236},
  325.      [0x32] = {188,188,236},
  326.      [0x33] = {212,178,236},
  327.      [0x34] = {236,174,236},
  328.      [0x35] = {236,174,212},
  329.      [0x36] = {236,180,176},
  330.      [0x37] = {228,196,144},
  331.      [0x38] = {204,210,120},
  332.      [0x39] = {180,222,120},
  333.      [0x3A] = {168,226,144},
  334.      [0x3B] = {152,226,180},
  335.      [0x3C] = {160,214,228},
  336.      [0x3D] = {160,162,160},
  337.      [0x3E] = {0,0,0},
  338.      [0x3F] = {0,0,0},
  339.   },
  340.  
  341. }
  342.  
  343. -- Because Love2D is freaking annoying with prints...
  344. function sprint(str,x,y)
  345.    x = x or 0
  346.    y = y or 0
  347.    love.graphics.print(tostring(str),x,y)
  348. end
  349.  
  350.  
  351. function lerror(msg)
  352.   _ERRS[#_ERRS+1] = msg
  353.   print("Error called: " .. msg)
  354. end
  355.  
  356. -- Instruction Execution
  357. function execute(op)
  358.     if(op == 0x00)then
  359.         lerror("Program ended @PC=" .. toHex(readPC()))
  360.     end
  361.     local func = OpCodes[op]
  362.     -- this is data not code.
  363.     if(func == nil)then
  364.       lerror("No such instruction: " .. op)
  365.       return false
  366.     end
  367.     func()
  368.     return true
  369. end
  370.  
  371. -- Setup functions
  372.  
  373. function init_mem()
  374.    -- idk why but i just like to have these :P
  375.    local _RAMSIZE = 64*1024
  376.    _RAM = string.rep(string.char(0x00), _RAMSIZE)
  377.    _FLAGS = 0x00
  378.    _REGS = {
  379.       ['A']  =  0x00,
  380.       ['X']  =  0x00,
  381.       ['Y']  =  0x00,
  382.       ['PC'] =  0x0600,
  383.       ['SP'] =  0xFF,
  384.     }
  385.     print("Mem set.")
  386. end
  387.  
  388.  
  389. -- Memory
  390. function readIndirect(byte1,reg)
  391.    -- Take the zeropaged byte and add the register
  392.     local nib = byte1 + readReg(reg)
  393.   -- Take 2 sections of the zeropage example: 0x00 + x = 0x01, b1 = 0x02 and b2 = 0x01.
  394.     local b1 = readByte(nib+1)
  395.     local b2 = readByte(nib)
  396.   -- Take these locations and put them together for the new read addr. 0x02 = 05, 0x01 = 06
  397.   -- Shift 06 by 8, 0600 or 05 = 0605.
  398.     local fByte = byteConcate(b1,b2)
  399.     print("Indirection result of: " .. byte1 .. "+" .. reg .. " :> " .. fByte)
  400.     return fByte
  401. end
  402.  
  403. function checkMemFlags(val)
  404.     if(val == 0)then setFlag("ZR",1) else setFlag("ZR",0) end
  405.     if(val >= 128)then
  406.       setFlag("NEG",1)
  407.     else
  408.       setFlag("NEG",0)
  409.     end
  410. end
  411.  
  412. function toBin(val)
  413.     local bits = ""
  414.     local oval = val
  415.     while val > 0 do
  416.         left = val%2
  417.         bits = left .. bits
  418.         val = (val - left)/2
  419.     end
  420.     -- Now for forced 8bits!
  421.     if(#bits < 8)then
  422.       bits = string.rep('0',8-#bits) .. bits
  423.     end
  424.    -- print(oval .. " BIN " .. bits)
  425.     return bits
  426. end
  427.  
  428. function toHex(val)
  429.     if(val < 0)then
  430.         local num = 256 + val
  431.     end
  432.     if(val == 0)then
  433.         return "00"
  434.     end
  435.     local oval = val
  436.     local hex = ""
  437.     local hexkey = "0123456789ABCDEF"
  438.     local rim
  439.     while val > 0 do
  440.         rim = math.floor(val%16)
  441.         hex = hexkey:sub(rim+1,rim+1) .. hex
  442.         val = math.floor(val/16)
  443.     end
  444.     if(#hex == 1)then
  445.         hex = "0" .. hex
  446.     end
  447.     --print(oval .. " HEX " .. hex)
  448.     return hex
  449. end
  450.  
  451. function readByte(ind)
  452.     return string.byte(_RAM:sub(ind,ind))
  453. end
  454.  
  455. function writeByte(ind,valByte)
  456.    _RAM = _RAM:sub(1,ind-1) .. string.char(valByte) .. _RAM:sub(ind+1)
  457.    print(toHex(ind) .. " :> " .. toHex(valByte))
  458. end
  459.  
  460. function readReg(reg)
  461.     return _REGS[reg]
  462. end
  463.  
  464. function readPC()
  465.     return _REGS['PC']
  466. end
  467.  
  468. function byteConcate(byte1, byte2)
  469.      print("Concating: " .. byte1 .. ", " .. byte2)
  470.      if(byte1 <= 15)then -- Nibble value
  471.         byte1 = bit.rshift(bit.lshift(byte1,4), 4)
  472.      end
  473.      if(byte2 <= 15)then -- Nibble value
  474.         byte2 = bit.rshift(bit.lshift(byte2,4), 4)
  475.      end
  476.      local b1 = bit.lshift(byte1,8)
  477.      print("Concated result: " .. bit.bor(b1,byte2))
  478.      return bit.bor(b1,byte2)
  479. end
  480.  
  481. function setPC(val2Byte)
  482.     _REGS['PC'] = val2Byte
  483. end
  484.  
  485. function stepPC(valByte)
  486.    _REGS['PC'] = _REGS['PC'] + valByte
  487. end
  488.  
  489. function readNext(by)
  490.    by = by or 0
  491.    return readByte(readPC()+by+1)
  492. end
  493.  
  494.  
  495. function writeReg(reg,valByte)
  496.     _REGS[reg] = valByte
  497. end
  498.  
  499. function setFlags(fByte)
  500.     _FLAGS = fByte
  501. end
  502.  
  503. function stringFlags()
  504.     return toBin(_FLAGS)
  505. end
  506.  
  507. function setFlag(flg,stat)
  508.     local flgs = {
  509.         ['CRY'] = 8,
  510.         ['ZR'] =  7,
  511.         ['IND'] = 6,
  512.         ['DEC'] = 5,
  513.         ['BRK'] = 4,
  514.         ['NIL'] = 3,
  515.         ['OVR'] = 2,
  516.         ['NEG'] = 1,
  517.     }
  518.      ind = flgs[flg]
  519.      stat = tostring(stat)
  520.      local fBin = toBin(_FLAGS)
  521.      if(fBin:sub(ind,ind) ~= stat)then
  522.         fBin = fBin:sub(1,ind-1) .. stat .. fBin:sub(ind+1)
  523.      end
  524.      _FLAGS = tonumber(fBin,2)
  525. end
  526.  
  527. function getFlag(flg)
  528.       local flgs = {
  529.         ['CRY'] = 8,
  530.         ['ZR'] =  7,
  531.         ['IND'] = 6,
  532.         ['DEC'] = 5,
  533.         ['BRK'] = 4,
  534.         ['NIL'] = 3,
  535.         ['OVR'] = 2,
  536.         ['NEG'] = 1,
  537.     }
  538.     bFlgs = toBin(_FLAGS)
  539.     return tonumber(bFlgs:sub(flgs[flg],flgs[flg]))
  540. end
  541.  
  542. -- Math
  543.  
  544. function checkMathFlags(val)
  545.     if(val == 0)then
  546.       setFlag("ZR",1)
  547.     else
  548.       setFlag("ZR",0)
  549.     end
  550.     if(val > 255)then
  551.       val = val - 256
  552.       setFlag("CRY",1)
  553.       print("FLAGWARN: Result of arithmetic too large (" .. val+256 .. " < 255)")
  554.     end
  555.     if(val < 0)then
  556.       val = val + 255
  557.       setFlag("CRY",0)
  558.       print("FLAGWARN: Result of arithmetic too small (" .. val-255 .. " > 0)")
  559.     end
  560.     if(val < -127)then
  561.       setFlag("OVR",1)
  562.       print("FLAGWARN: Arithmetic triggered signed bit (" .. val .. ")")
  563.     elseif(val > 127)then
  564.       setFlag("OVR",1)
  565.        print("FLAGWARN: Arithmetic triggered signed bit (" .. val .. ")")
  566.     else
  567.       setFlag("OVR",0)
  568.     end
  569.     if(val >= 128)then
  570.       setFlag("NEG",1)
  571.       print("FLAGWARN: Signed arithmetic is negative (" .. val .. ")")
  572.     else
  573.       setFlag("NEG",0)
  574.     end
  575.    
  576.     return val
  577. end
  578.  
  579. function signedAdd(b1,b2)
  580.     local bf = b1 + b2
  581.     bf = checkMathFlags(bf)
  582.     return bf
  583. end
  584.  
  585. function signedSub(b1,b2)
  586.     local bf = b1 - b2
  587.     bf = checkMathFlags(bf)
  588.     return bf
  589. end
  590.  
  591.  
  592. -- Assembly
  593.  
  594. function compileProgram()
  595. end
  596.  
  597.  
  598.  
  599. function interpretInstr()
  600.    -- just going to code up a simple machine code input thingy
  601.    
  602. end
  603.  
  604. function getProcStatus()
  605.     return "A: " .. toHex(readReg('A')) .. " X: " .. toHex(readReg('X')) .. " Y: " .. toHex(readReg('Y')) .. "\nPC: " ..
  606.     toHex(readPC()) .. " SP: " .. toHex(readReg('SP')) .. "\nNV-BDIZC\n" .. toBin(_FLAGS)
  607. end
  608.  
  609. function bytesToStr(bytes)
  610.     str = ""
  611.     for i = 1, #bytes do
  612.         str = str .. string.char(bytes[i])
  613.     end
  614.     print("Bytes converted to string: " .. unpack(bytes) .. " to \"" .. str .. "\"")
  615.     return str
  616. end
  617.  
  618. function loadProgram(bytes,clr)
  619.     -- load bytes into PC location
  620.     clr = clr or false
  621.     print("Loading program... ")
  622.     if(clr)then _RAM = string.rep(string.char(0x00),64*1024) end
  623.     for i = 1, #bytes do
  624.          writeByte(readPC()+(i-1), string.byte(bytes:sub(i,i)))
  625.         -- io.write(readPC()+(i-1) .. ":" .. string.byte(bytes:sub(i,i)) .. " ")
  626.     end
  627.     print("Program loaded.")
  628. end
  629.  
  630. function executeProgram()
  631.     while readByte(readPC()) ~= 0x00 do
  632.          inst = execute(readNext(-1))
  633.          if(not inst)then
  634.            print("Unknown instruction: " .. readNext(-1))
  635.            break
  636.          end
  637.          print("Executed: " .. readNext(-1))
  638.          stepPC(1)
  639.     end
  640.     if(readByte(readPC()) == 0x00)then
  641.       lerror("Program ended @PC=" .. toHex(readPC()))
  642.     end
  643.     setPC(0x0600)
  644.     return true
  645. end
  646.  
  647.  
  648. function drawPixel(x,y,cr,cg,cb)
  649.    love.graphics.setColor(cr,cg,cb)
  650.    love.graphics.point(x,y)
  651. end
  652.  
  653. function love.textinput(t)
  654.    if(takingInput)then
  655.       chrBuff = chrBuff .. t
  656.       if(#chrBuff >= 2)then
  657.           code[#code+1] = tonumber(chrBuff,16)
  658.           chrBuff = ""
  659.       end
  660.       if(t == 'r')then
  661.           chrBuff = ""
  662.           -- Run machineCode
  663.           loadProgram(bytesToStr(code) .. string.char(0x00))
  664.           executeProgram()
  665.           code = {}
  666.       end
  667.    end
  668. end
  669.  
  670.  
  671. function love.load()
  672.     print("Love2D initalized.")
  673. end
  674.  
  675. function love.update()
  676. end
  677.  
  678. init_mem()
  679. loadProgram(bytesToStr({0xA9, 0x80, 0xA9, 0x32}))
  680. signedSub(0x01,0x02)
  681. signedSub(0x01,0xFF)
  682. executeProgram()
  683.  
  684. function love.draw()
  685.   if(#_ERRS > 0)then
  686.       for i = 1, #_ERRS do
  687.           love.graphics.setColor(colors.nes[0x06])
  688.           love.graphics.print(_ERRS[i],0,60)
  689.           love.graphics.setColor(colors.nes[0x30])
  690.       end
  691.       if(i == #_ERRS)then ERRS = {} end
  692.   end
  693.   sprint(getProcStatus())
  694.  
  695.    local pcode = ""
  696.    for i = 1, #code do
  697.       pcode = pcode .. " " .. toHex(code[i])
  698.    end
  699.    sprint("Code: " .. pcode:upper(),0,100)
  700.    sprint("Inst: " .. chrBuff,0,80)
  701. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement