{"action":"reopened","issue":{"url":"https://api.github.com/repos/bittwiddler1/dNES/issues/8","labels_url":"https://api.github.com/repos/bittwiddler1/dNES/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/bittwiddler1/dNES/issues/8/comments","events_url":"https://api.github.com/repos/bittwiddler1/dNES/issues/8/events","html_url":"https://github.com/bittwiddler1/dNES/issues/8","id":70701374,"number":8,"title":"CPU cycle","user":{"login":"bobomb","id":1750895,"avatar_url":"https://avatars.githubusercontent.com/u/1750895?v=3","gravatar_id":"","url":"https://api.github.com/users/bobomb","html_url":"https://github.com/bobomb","followers_url":"https://api.github.com/users/bobomb/followers","following_url":"https://api.github.com/users/bobomb/following{/other_user}","gists_url":"https://api.github.com/users/bobomb/gists{/gist_id}","starred_url":"https://api.github.com/users/bobomb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bobomb/subscriptions","organizations_url":"https://api.github.com/users/bobomb/orgs","repos_url":"https://api.github.com/users/bobomb/repos","events_url":"https://api.github.com/users/bobomb/events{/privacy}","received_events_url":"https://api.github.com/users/bobomb/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/bittwiddler1/dNES/labels/cpu","name":"cpu","color":"000666"},{"url":"https://api.github.com/repos/bittwiddler1/dNES/labels/feature","name":"feature","color":"207de5"},{"url":"https://api.github.com/repos/bittwiddler1/dNES/labels/task","name":"task","color":"5319e7"}],"state":"open","locked":false,"assignee":{"login":"bobomb","id":1750895,"avatar_url":"https://avatars.githubusercontent.com/u/1750895?v=3","gravatar_id":"","url":"https://api.github.com/users/bobomb","html_url":"https://github.com/bobomb","followers_url":"https://api.github.com/users/bobomb/followers","following_url":"https://api.github.com/users/bobomb/following{/other_user}","gists_url":"https://api.github.com/users/bobomb/gists{/gist_id}","starred_url":"https://api.github.com/users/bobomb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bobomb/subscriptions","organizations_url":"https://api.github.com/users/bobomb/orgs","repos_url":"https://api.github.com/users/bobomb/repos","events_url":"https://api.github.com/users/bobomb/events{/privacy}","received_events_url":"https://api.github.com/users/bobomb/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/bittwiddler1/dNES/milestones/1","html_url":"https://github.com/bittwiddler1/dNES/milestones/CPU%20instruction%20set","labels_url":"https://api.github.com/repos/bittwiddler1/dNES/milestones/1/labels","id":1073428,"number":1,"title":"CPU instruction set","description":"","creator":{"login":"bittwiddler1","id":2100425,"avatar_url":"https://avatars.githubusercontent.com/u/2100425?v=3","gravatar_id":"","url":"https://api.github.com/users/bittwiddler1","html_url":"https://github.com/bittwiddler1","followers_url":"https://api.github.com/users/bittwiddler1/followers","following_url":"https://api.github.com/users/bittwiddler1/following{/other_user}","gists_url":"https://api.github.com/users/bittwiddler1/gists{/gist_id}","starred_url":"https://api.github.com/users/bittwiddler1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bittwiddler1/subscriptions","organizations_url":"https://api.github.com/users/bittwiddler1/orgs","repos_url":"https://api.github.com/users/bittwiddler1/repos","events_url":"https://api.github.com/users/bittwiddler1/events{/privacy}","received_events_url":"https://api.github.com/users/bittwiddler1/received_events","type":"User","site_admin":false},"open_issues":4,"closed_issues":1,"state":"open","created_at":"2015-04-18T17:48:55Z","updated_at":"2015-04-24T14:47:33Z","due_on":"2015-04-30T04:00:00Z","closed_at":null},"comments":1,"created_at":"2015-04-24T14:17:41Z","updated_at":"2015-04-24T14:47:33Z","closed_at":null,"body":"Some thoughts on implementing the CPU cycle:\r\n```\r\nstruct instruction_info\r\n{\r\n *fn(...) instrFuncPtr\r\n ubyte [] decodedArgs\r\nubyte cyclecount;\r\n}\r\nvoid instruction_add_immediate(ubyte[] args)\r\n{\r\nresult =   cpu.a +args[0];\r\ncpu.a = result;\r\n/* set overflow etc etc if needed */\r\n}\r\n\r\nvoid instruction_add_absolute(ubyte[] args)\r\n{\r\nresult = cpu.a + console.ram.read(args[0]);\r\n}\r\n```\r\n\r\nCpu Cycle loop:\r\nopcode = fetch()\r\ninstruction_info = decode(opcode);\r\ninstruction_info.execute()\r\n\r\n\r\nfetch() will read a byte from the address indicated in the pc register, increment pc by 1 and then return the fetched byte, which is the opcode for an instruction\r\n\r\ndecode() will take in the opcode and go through a massive case statement to determine which instruction and addressing mode.\r\n\r\nExample:\r\nImmediate  |  AND #$44 | $29 | 2 |  2\r\nZero Page  AND $44 |  |  $25 | 2 |  3\r\nZero Page,X  |  AND $44,X |  $35 | 2 |  4\r\nAbsolute  |  AND $4400 |  $2D | 3 |  4\r\nAbsolute,X | AND $4400,X |  $3D | 3 |  4+\r\nAbsolute,Y | AND $4400,Y |  $39 | 3 |  4+\r\nIndirect,X | AND ($44,X) |  $21 | 2 |  6\r\nIndirect,Y | AND ($44),Y |  $31 | 2 |  5+\r\n\r\n0x44 = And operation\r\nNext byte is the immediate address.\r\nFetch would get 0x44 as the opcode, increment pc by 1\r\ndecode(opcode) would interpret the 0x44 as AND IMMEDIATE (big switch statement) and read the next byte and increment pc by 1 (or 2 or 3 based on the length of the instruction), run it through the address decoder (determine the memory address) and return it as as a structure containing the function(possibly as a pointer)and the decoded memory address.\r\n\r\nThen execute would call the fp and pass in the args.\r\nDecode would basically read in the parameters based on the instruction and put them into an array and increment the PC by the right amount so it's set to the address of the next instruction in memory, ready for the next cycle."}}