UNPKG

fma-snes65816

Version:
450 lines (410 loc) 24.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _InstructionType = require('./InstructionType'); var _InstructionType2 = _interopRequireDefault(_InstructionType); var _Instruction = require('./Instruction'); var _Instruction2 = _interopRequireDefault(_Instruction); var _InstructionTickCounter = require('./InstructionTickCounter'); var _InstructionTickCounter2 = _interopRequireDefault(_InstructionTickCounter); var _fma = require('fma'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var InstructionCollection = function () { function InstructionCollection() { (0, _classCallCheck3.default)(this, InstructionCollection); this.instructions = {}; } (0, _createClass3.default)(InstructionCollection, [{ key: 'add', value: function add(name, opcode, type) { var callback = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; name = name.toUpperCase(); if (!this.instructions.hasOwnProperty(name)) { this.instructions[name] = new _Instruction2.default(name); } this.instructions[name].add(type, opcode, callback); } }, { key: 'compile', value: function compile(project, interpreter) { var root = interpreter.getRoot(); for (var name in this.instructions) { root.setMember(name, this.instructions[name].build()); } } }]); return InstructionCollection; }(); ; var InstructionCompiler = function () { function InstructionCompiler() { (0, _classCallCheck3.default)(this, InstructionCompiler); } (0, _createClass3.default)(InstructionCompiler, [{ key: 'setReturnOpcode', value: function setReturnOpcode(scope, opcode, p1, p2, context) { scope.setMember('is_return_opcode', new _fma.BooleanObject(true)); } }, { key: 'setSizeHint', value: function setSizeHint(scope, opcode, p1, p2, context) { this.setReturnOpcode(scope, opcode, p1, p2, context); switch (p1.rawValue.getClassName()) { case 'Function': case 'Label': break; default: console.log('Size hint on non-function: ' + p1.rawValue.getClassName()); return; } var func = p1.rawValue; var sizeA = scope.getMember('register_size_A'); var sizeXY = scope.getMember('register_size_XY'); func.setMember('register_size_A', sizeA); func.setMember('register_size_XY', sizeXY); } }, { key: 'createNumber', value: function createNumber(context, number) { var obj = context.getRoot().resolveChild('Number').getObject(); var value = new _fma.InternalValue(number); return obj.getMember('new').callWithParameters(context, value); } }, { key: 'implementTo', value: function implementTo(project, interpreter) { var _this = this; var collection = new InstructionCollection(); collection.add("BRK", 0x00, _InstructionType2.default.brk()); collection.add("ora", 0x01, _InstructionType2.default.idpx()); collection.add("cop", 0x02, _InstructionType2.default.im()); collection.add("ora", 0x03, _InstructionType2.default.sr()); collection.add("tsb", 0x04, _InstructionType2.default.dp()); collection.add("ora", 0x05, _InstructionType2.default.dp()); collection.add("asl", 0x06, _InstructionType2.default.dp()); collection.add("ora", 0x07, _InstructionType2.default.ildp()); collection.add("php", 0x08, _InstructionType2.default.impl()); collection.add("ora", 0x09, _InstructionType2.default.im_a()); collection.add("asl", 0x0a, _InstructionType2.default.a()); collection.add("asl", 0x0a, _InstructionType2.default.impl()); collection.add("phd", 0x0b, _InstructionType2.default.impl()); collection.add("tsb", 0x0c, _InstructionType2.default.addr()); collection.add("ora", 0x0d, _InstructionType2.default.addr()); collection.add("asl", 0x0e, _InstructionType2.default.addr()); collection.add("ora", 0x0f, _InstructionType2.default.long()); collection.add("bpl", 0x10, _InstructionType2.default.relb()); collection.add("ora", 0x11, _InstructionType2.default.idpy()); collection.add("ora", 0x12, _InstructionType2.default.idp()); collection.add("ora", 0x13, _InstructionType2.default.isry()); collection.add("trb", 0x14, _InstructionType2.default.dp()); collection.add("ora", 0x15, _InstructionType2.default.dpx()); collection.add("asl", 0x16, _InstructionType2.default.dpx()); collection.add("ora", 0x17, _InstructionType2.default.ildpy()); collection.add("clc", 0x18, _InstructionType2.default.impl()); collection.add("ora", 0x19, _InstructionType2.default.addry()); collection.add("inc", 0x1a, _InstructionType2.default.a()); collection.add("inc", 0x1a, _InstructionType2.default.impl()); collection.add("tcs", 0x1b, _InstructionType2.default.impl()); collection.add("trb", 0x1c, _InstructionType2.default.addr()); collection.add("ora", 0x1d, _InstructionType2.default.addrx()); collection.add("asl", 0x1e, _InstructionType2.default.addrx()); collection.add("ora", 0x1f, _InstructionType2.default.longx()); collection.add("jsr", 0x20, _InstructionType2.default.addr_pc()); collection.add("and", 0x21, _InstructionType2.default.idpx()); collection.add("jsl", 0x22, _InstructionType2.default.alias(_InstructionType2.default.addr(), _InstructionType2.default.long()), function () { _this.setSizeHint.apply(_this, arguments); }); collection.add("jsl", 0x22, _InstructionType2.default.long(), function () { _this.setSizeHint.apply(_this, arguments); }); collection.add("and", 0x23, _InstructionType2.default.sr()); collection.add("bit", 0x24, _InstructionType2.default.dp()); collection.add("and", 0x25, _InstructionType2.default.dp()); collection.add("rol", 0x26, _InstructionType2.default.dp()); collection.add("and", 0x27, _InstructionType2.default.ildp()); collection.add("plp", 0x28, _InstructionType2.default.impl()); collection.add("AND", 0x29, _InstructionType2.default.im_a()); collection.add("rol", 0x2a, _InstructionType2.default.impl()); collection.add("pld", 0x2b, _InstructionType2.default.impl()); collection.add("bit", 0x2c, _InstructionType2.default.addr()); collection.add("and", 0x2d, _InstructionType2.default.addr()); collection.add("rol", 0x2e, _InstructionType2.default.addr()); collection.add("and", 0x2f, _InstructionType2.default.long()); collection.add("bmi", 0x30, _InstructionType2.default.relb()); collection.add("and", 0x31, _InstructionType2.default.idpy()); collection.add("and", 0x32, _InstructionType2.default.idp()); collection.add("and", 0x33, _InstructionType2.default.isry()); collection.add("bit", 0x34, _InstructionType2.default.dpx()); collection.add("and", 0x35, _InstructionType2.default.dpx()); collection.add("rol", 0x36, _InstructionType2.default.dpx()); collection.add("and", 0x37, _InstructionType2.default.ildpy()); collection.add("sec", 0x38, _InstructionType2.default.impl()); collection.add("and", 0x39, _InstructionType2.default.addry()); collection.add("dec", 0x3a, _InstructionType2.default.a()); collection.add("dec", 0x3a, _InstructionType2.default.impl()); collection.add("tsc", 0x3b, _InstructionType2.default.impl()); collection.add("bit", 0x3c, _InstructionType2.default.addrx()); collection.add("and", 0x3d, _InstructionType2.default.addrx()); collection.add("rol", 0x3e, _InstructionType2.default.addrx()); collection.add("and", 0x3f, _InstructionType2.default.longx()); collection.add("rti", 0x40, _InstructionType2.default.impl(), function () { _this.setReturnOpcode.apply(_this, arguments); }); collection.add("eor", 0x41, _InstructionType2.default.idpx()); collection.add("wdm", 0x42, _InstructionType2.default.im()); collection.add("eor", 0x43, _InstructionType2.default.sr()); collection.add("mvp", 0x44, _InstructionType2.default.mv()); collection.add("eor", 0x45, _InstructionType2.default.dp()); collection.add("lsr", 0x46, _InstructionType2.default.dp()); collection.add("eor", 0x47, _InstructionType2.default.ildp()); collection.add("pha", 0x48, _InstructionType2.default.impl()); collection.add("eor", 0x49, _InstructionType2.default.im_a()); collection.add("lsr", 0x4a, _InstructionType2.default.a()); collection.add("lsr", 0x4a, _InstructionType2.default.impl()); collection.add("phk", 0x4b, _InstructionType2.default.impl()); collection.add("JMP", 0x4c, _InstructionType2.default.addr(), function () { _this.setSizeHint.apply(_this, arguments); }); collection.add("eor", 0x4d, _InstructionType2.default.addr()); collection.add("lsr", 0x4e, _InstructionType2.default.addr()); collection.add("eor", 0x4f, _InstructionType2.default.long()); collection.add("bvc", 0x50, _InstructionType2.default.relb()); collection.add("eor", 0x51, _InstructionType2.default.idpy()); collection.add("eor", 0x52, _InstructionType2.default.idp()); collection.add("eor", 0x53, _InstructionType2.default.isry()); collection.add("mvn", 0x54, _InstructionType2.default.mv()); collection.add("eor", 0x55, _InstructionType2.default.dpx()); collection.add("lsr", 0x56, _InstructionType2.default.dpx()); collection.add("eor", 0x57, _InstructionType2.default.ildpy()); collection.add("cli", 0x58, _InstructionType2.default.impl()); collection.add("eor", 0x59, _InstructionType2.default.addry()); collection.add("phy", 0x5a, _InstructionType2.default.impl()); collection.add("tcd", 0x5b, _InstructionType2.default.impl()); collection.add("JML", 0x5c, _InstructionType2.default.alias(_InstructionType2.default.addr(), _InstructionType2.default.long()), function () { _this.setSizeHint.apply(_this, arguments); }); collection.add("JML", 0x5c, _InstructionType2.default.long(), function () { _this.setSizeHint.apply(_this, arguments); }); collection.add("eor", 0x5d, _InstructionType2.default.addrx()); collection.add("lsr", 0x5e, _InstructionType2.default.addrx()); collection.add("eor", 0x5f, _InstructionType2.default.longx()); collection.add("RTS", 0x60, _InstructionType2.default.impl(), function () { _this.setReturnOpcode.apply(_this, arguments); }); collection.add("adc", 0x61, _InstructionType2.default.idpx()); collection.add("per", 0x62, _InstructionType2.default.relw()); collection.add("adc", 0x63, _InstructionType2.default.sr()); collection.add("stz", 0x64, _InstructionType2.default.dp()); collection.add("adc", 0x65, _InstructionType2.default.dp()); collection.add("ror", 0x66, _InstructionType2.default.dp()); collection.add("adc", 0x67, _InstructionType2.default.ildp()); collection.add("pla", 0x68, _InstructionType2.default.impl()); collection.add("ADC", 0x69, _InstructionType2.default.im_a()); collection.add("ror", 0x6a, _InstructionType2.default.impl()); collection.add("rtl", 0x6b, _InstructionType2.default.impl(), function () { _this.setReturnOpcode.apply(_this, arguments); }); collection.add("adc", 0x6d, _InstructionType2.default.addr()); collection.add("ror", 0x6e, _InstructionType2.default.addr()); collection.add("adc", 0x6f, _InstructionType2.default.long()); collection.add("bvs", 0x70, _InstructionType2.default.relb()); collection.add("adc", 0x71, _InstructionType2.default.idpy()); collection.add("adc", 0x72, _InstructionType2.default.idp()); collection.add("adc", 0x73, _InstructionType2.default.isry()); collection.add("stz", 0x74, _InstructionType2.default.dpx()); collection.add("adc", 0x75, _InstructionType2.default.dpx()); collection.add("ror", 0x76, _InstructionType2.default.dpx()); collection.add("adc", 0x77, _InstructionType2.default.ildpy()); collection.add("sei", 0x78, _InstructionType2.default.impl()); collection.add("adc", 0x79, _InstructionType2.default.addry()); collection.add("ply", 0x7a, _InstructionType2.default.impl()); collection.add("tdc", 0x7b, _InstructionType2.default.impl()); collection.add("jmp", 0x7c, _InstructionType2.default.iaddrx()); collection.add("adc", 0x7d, _InstructionType2.default.addrx()); collection.add("ror", 0x7e, _InstructionType2.default.addrx()); collection.add("adc", 0x7f, _InstructionType2.default.longx()); collection.add("bra", 0x80, _InstructionType2.default.relb(), function () { _this.setReturnOpcode.apply(_this, arguments); }); collection.add("sta", 0x81, _InstructionType2.default.idpx()); collection.add("brl", 0x82, _InstructionType2.default.relw(), function () { _this.setReturnOpcode.apply(_this, arguments); }); collection.add("sta", 0x83, _InstructionType2.default.sr()); collection.add("sty", 0x84, _InstructionType2.default.dp()); collection.add("sta", 0x85, _InstructionType2.default.dp()); collection.add("stx", 0x86, _InstructionType2.default.dp()); collection.add("sta", 0x87, _InstructionType2.default.ildp()); collection.add("dey", 0x88, _InstructionType2.default.impl()); collection.add("bit", 0x89, _InstructionType2.default.im_a()); collection.add("txa", 0x8a, _InstructionType2.default.impl()); collection.add("phb", 0x8b, _InstructionType2.default.impl()); collection.add("sty", 0x8c, _InstructionType2.default.addr()); collection.add("sta", 0x8d, _InstructionType2.default.addr()); collection.add("stx", 0x8e, _InstructionType2.default.addr()); collection.add("sta", 0x8f, _InstructionType2.default.long()); collection.add("bcc", 0x90, _InstructionType2.default.relb()); collection.add("blt", 0x90, _InstructionType2.default.relb()); collection.add("sta", 0x91, _InstructionType2.default.idpy()); collection.add("sta", 0x92, _InstructionType2.default.idp()); collection.add("sta", 0x93, _InstructionType2.default.isry()); collection.add("sty", 0x94, _InstructionType2.default.dpx()); collection.add("sta", 0x95, _InstructionType2.default.dpx()); collection.add("stx", 0x96, _InstructionType2.default.dpy()); collection.add("sta", 0x97, _InstructionType2.default.ildpy()); collection.add("tya", 0x98, _InstructionType2.default.impl()); collection.add("sta", 0x99, _InstructionType2.default.addry()); collection.add("txs", 0x9a, _InstructionType2.default.impl()); collection.add("txy", 0x9b, _InstructionType2.default.impl()); collection.add("stz", 0x9c, _InstructionType2.default.addr()); collection.add("sta", 0x9d, _InstructionType2.default.addrx()); collection.add("stz", 0x9e, _InstructionType2.default.addrx()); collection.add("sta", 0x9f, _InstructionType2.default.longx()); collection.add("LDY", 0xa0, _InstructionType2.default.im_y()); collection.add("lda", 0xa1, _InstructionType2.default.idpx()); collection.add("LDX", 0xa2, _InstructionType2.default.im_x()); collection.add("lda", 0xa3, _InstructionType2.default.sr()); collection.add("ldy", 0xa4, _InstructionType2.default.dp()); collection.add("lda", 0xa5, _InstructionType2.default.dp()); collection.add("ldx", 0xa6, _InstructionType2.default.dp()); collection.add("lda", 0xa7, _InstructionType2.default.ildp()); collection.add("tay", 0xa8, _InstructionType2.default.impl()); collection.add("LDA", 0xa9, _InstructionType2.default.im_a()); collection.add("tax", 0xaa, _InstructionType2.default.impl()); collection.add("plb", 0xab, _InstructionType2.default.impl()); collection.add("ldy", 0xac, _InstructionType2.default.addr()); collection.add("lda", 0xad, _InstructionType2.default.addr()); collection.add("ldx", 0xae, _InstructionType2.default.addr()); collection.add("lda", 0xaf, _InstructionType2.default.long()); collection.add("bcs", 0xb0, _InstructionType2.default.relb()); collection.add("bge", 0xb0, _InstructionType2.default.relb()); collection.add("lda", 0xb1, _InstructionType2.default.idpy()); collection.add("lda", 0xb2, _InstructionType2.default.idp()); collection.add("lda", 0xb3, _InstructionType2.default.isry()); collection.add("ldy", 0xb4, _InstructionType2.default.dpx()); collection.add("lda", 0xb5, _InstructionType2.default.dpx()); collection.add("ldx", 0xb6, _InstructionType2.default.dpy()); collection.add("lda", 0xb7, _InstructionType2.default.ildpy()); collection.add("clv", 0xb8, _InstructionType2.default.impl()); collection.add("lda", 0xb9, _InstructionType2.default.addry()); collection.add("tsx", 0xba, _InstructionType2.default.impl()); collection.add("tyx", 0xbb, _InstructionType2.default.impl()); collection.add("ldy", 0xbc, _InstructionType2.default.addrx()); collection.add("lda", 0xbd, _InstructionType2.default.addrx()); collection.add("ldx", 0xbe, _InstructionType2.default.addry()); collection.add("lda", 0xbf, _InstructionType2.default.longx()); collection.add("CPY", 0xc0, _InstructionType2.default.im_y()); collection.add("cmp", 0xc1, _InstructionType2.default.idpx()); collection.add("REP", 0xc2, _InstructionType2.default.im(), function (scope, opcode, p1, p2, context) { var value = p1.value.getMember('__value').value; if (value & 0x10) { scope.setMember('register_size_XY', _this.createNumber(context, 16)); } if (value & 0x20) { scope.setMember('register_size_A', _this.createNumber(context, 16)); } }); collection.add("cmp", 0xc3, _InstructionType2.default.sr()); collection.add("cpy", 0xc4, _InstructionType2.default.dp()); collection.add("cmp", 0xc5, _InstructionType2.default.dp()); collection.add("dec", 0xc6, _InstructionType2.default.dp()); collection.add("cmp", 0xc7, _InstructionType2.default.ildp()); collection.add("iny", 0xc8, _InstructionType2.default.impl()); collection.add("cmp", 0xc9, _InstructionType2.default.im_a()); collection.add("dex", 0xca, _InstructionType2.default.impl()); collection.add("wai", 0xcb, _InstructionType2.default.impl()); collection.add("cpy", 0xcc, _InstructionType2.default.addr()); collection.add("cmp", 0xcd, _InstructionType2.default.addr()); collection.add("dec", 0xce, _InstructionType2.default.addr()); collection.add("cmp", 0xcf, _InstructionType2.default.long()); collection.add("bne", 0xd0, _InstructionType2.default.relb()); collection.add("bnz", 0xd0, _InstructionType2.default.relb()); collection.add("cmp", 0xd1, _InstructionType2.default.idpy()); collection.add("cmp", 0xd2, _InstructionType2.default.idp()); collection.add("cmp", 0xd3, _InstructionType2.default.isry()); collection.add("pei", 0xd4, _InstructionType2.default.dp()); collection.add("cmp", 0xd5, _InstructionType2.default.dpx()); collection.add("dec", 0xd6, _InstructionType2.default.dpx()); collection.add("cmp", 0xd7, _InstructionType2.default.ildpy()); collection.add("cld", 0xd8, _InstructionType2.default.impl()); collection.add("cmp", 0xd9, _InstructionType2.default.addry()); collection.add("phx", 0xda, _InstructionType2.default.impl()); collection.add("stp", 0xdb, _InstructionType2.default.impl()); collection.add("cmp", 0xdd, _InstructionType2.default.addrx()); collection.add("dec", 0xde, _InstructionType2.default.addrx()); collection.add("cmp", 0xdf, _InstructionType2.default.longx()); collection.add("cpx", 0xe0, _InstructionType2.default.im_x()); collection.add("sbc", 0xe1, _InstructionType2.default.idpx()); collection.add("sep", 0xe2, _InstructionType2.default.im(), function (scope, opcode, p1, p2, context) { var value = p1.value.getMember('__value').value; if (value & 0x10) { scope.setMember('register_size_XY', _this.createNumber(context, 8)); } if (value & 0x20) { scope.setMember('register_size_A', _this.createNumber(context, 8)); } }); collection.add("sbc", 0xe3, _InstructionType2.default.sr()); collection.add("cpx", 0xe4, _InstructionType2.default.dp()); collection.add("sbc", 0xe5, _InstructionType2.default.dp()); collection.add("inc", 0xe6, _InstructionType2.default.dp()); collection.add("sbc", 0xe7, _InstructionType2.default.ildp()); collection.add("inx", 0xe8, _InstructionType2.default.impl()); collection.add("sbc", 0xe9, _InstructionType2.default.im_a()); collection.add("nop", 0xea, _InstructionType2.default.impl()); collection.add("xba", 0xeb, _InstructionType2.default.impl()); collection.add("cpx", 0xec, _InstructionType2.default.addr()); collection.add("sbc", 0xed, _InstructionType2.default.addr()); collection.add("inc", 0xee, _InstructionType2.default.addr()); collection.add("sbc", 0xef, _InstructionType2.default.long()); collection.add("beq", 0xf0, _InstructionType2.default.relb()); collection.add("bze", 0xf0, _InstructionType2.default.relb()); collection.add("sbc", 0xf1, _InstructionType2.default.idpy()); collection.add("sbc", 0xf2, _InstructionType2.default.idp()); collection.add("sbc", 0xf3, _InstructionType2.default.isry()); collection.add("pea", 0xf4, _InstructionType2.default.im_16()); collection.add("sbc", 0xf5, _InstructionType2.default.dpx()); collection.add("inc", 0xf6, _InstructionType2.default.dpx()); collection.add("sbc", 0xf7, _InstructionType2.default.ildpy()); collection.add("sed", 0xf8, _InstructionType2.default.impl()); collection.add("sbc", 0xf9, _InstructionType2.default.addry()); collection.add("plx", 0xfa, _InstructionType2.default.impl()); collection.add("xce", 0xfb, _InstructionType2.default.impl()); collection.add("jsr", 0xfc, _InstructionType2.default.iaddrx()); collection.add("sbc", 0xfd, _InstructionType2.default.addrx()); collection.add("inc", 0xfe, _InstructionType2.default.addrx()); collection.add("sbc", 0xff, _InstructionType2.default.longx()); collection.compile(project, interpreter); this.createCallback(interpreter, 'SNES65816_start_instruction_counter', [], function (context, self) { _Instruction2.default.pushCounter(new _InstructionTickCounter2.default()); }); this.createCallback(interpreter, 'SNES65816_end_instruction_counter', [], function (context, self) { var oldCounter = _Instruction2.default.popCounter(); var counter = _Instruction2.default.getCounter(); if (counter) { counter.addDirect(counter.get()); } return _this.createNumber(context, oldCounter.get()); }); } }, { key: 'createCallback', value: function createCallback(interpreter, name, argList, callback) { var macro = new _fma.MacroObject(name); var args = new _fma.ArgumentList(); args.buildFromStringList(argList); macro.setArguments(args); macro.setCallback(callback); interpreter.getRoot().setMember(name, macro); } }]); return InstructionCompiler; }(); exports.default = InstructionCompiler; //# sourceMappingURL=InstructionCompiler.js.map