{ "version": 3, "sources": ["../src/platform/vectrex.ts"], "sourcesContent": ["\nimport { Platform, BaseZ80Platform, Base6502Platform, Base6809Platform } from \"../common/baseplatform\";\nimport { PLATFORMS, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, VectorVideo, Keys, makeKeycodeMap } from \"../common/emu\";\nimport { hex, lzgmini, stringToByteArray, safe_extend } from \"../common/util\";\nimport { MasterAudio, AY38910_Audio } from \"../common/audio\";\nimport { ProbeRecorder } from \"../common/probe\";\nimport { NullProbe, Probeable, ProbeAll } from \"../common/devices\";\n\n// emulator from https://github.com/raz0red/jsvecx\n// https://roadsidethoughts.com/vectrex/vectrex-memory-map.htm\n// http://www.playvectrex.com/designit/chrissalo/memorymap.htm\n// http://vectrexmuseum.com/share/coder/other/TEXT/VECTREX/INTERNAL.TXT\n// http://vide.malban.de/help/vectrex-tutorial-ii-starting-with-bios\n// http://www.playvectrex.com/designit/chrissalo/bios.asm\n// https://www.6809.org.uk/asm6809/doc/asm6809.shtml\n// http://www.playvectrex.com/\n// http://vectrexmuseum.com/vectrexhistory.php\n\nvar VECTREX_PRESETS = [\n { id: 'hello.xasm', name: 'Hello World (ASM)' },\n { id: 'hello.c', name: 'Hello World (CMOC)' },\n { id: 'joystick.c', name: 'Joystick Test (CMOC)' },\n { id: 'threed.c', name: '3D Transformations (CMOC)' },\n]\n\n// TODO: player 2\nvar VECTREX_KEYCODE_MAP = makeKeycodeMap([\n [Keys.LEFT, 0, 0x01],\n [Keys.RIGHT, 0, 0x02],\n [Keys.DOWN, 0, 0x04],\n [Keys.UP, 0, 0x08],\n [Keys.GP_B, 2, 0x01],\n [Keys.GP_A, 2, 0x02],\n [Keys.GP_D, 2, 0x04],\n [Keys.GP_C, 2, 0x08],\n\n [Keys.P2_LEFT, 1, 0x01],\n [Keys.P2_RIGHT, 1, 0x02],\n [Keys.P2_DOWN, 1, 0x04],\n [Keys.P2_UP, 1, 0x08],\n [Keys.P2_GP_B, 2, 0x10],\n [Keys.P2_GP_A, 2, 0x20],\n [Keys.P2_GP_D, 2, 0x40],\n [Keys.P2_GP_C, 2, 0x80],\n]);\n\n//\n\nclass VIA6522 {\n vectrex: VectrexPlatform;\n constructor(vectrex) {\n this.vectrex = vectrex;\n }\n\n //static unsigned via_ora;\n ora = 0;\n //static unsigned via_orb;\n orb = 0;\n //static unsigned via_ddra;\n ddra = 0;\n //static unsigned via_ddrb;\n ddrb = 0;\n //static unsigned via_t1on; /* is timer 1 on? */\n t1on = 0;\n //static unsigned via_t1int; /* are timer 1 interrupts allowed? */\n t1int = 0;\n //static unsigned via_t1c;\n t1c = 0;\n //static unsigned via_t1ll;\n t1ll = 0;\n //static unsigned via_t1lh;\n t1lh = 0;\n //static unsigned via_t1pb7; /* timer 1 controlled version of pb7 */\n t1pb7 = 0;\n //static unsigned via_t2on; /* is timer 2 on? */\n t2on = 0;\n //static unsigned via_t2int; /* are timer 2 interrupts allowed? */\n t2int = 0;\n //static unsigned via_t2c;\n t2c = 0;\n //static unsigned via_t2ll;\n t2ll = 0;\n //static unsigned via_sr;\n sr = 0;\n //static unsigned via_srb; /* number of bits shifted so far */\n srb = 0;\n //static unsigned via_src; /* shift counter */\n src = 0;\n //static unsigned via_srclk;\n srclk = 0;\n //static unsigned via_acr;\n acr = 0;\n //static unsigned via_pcr;\n pcr = 0;\n //static unsigned via_ifr;\n ifr = 0;\n //static unsigned via_ier;\n ier = 0;\n //static unsigned via_ca2;\n ca2 = 0;\n //static unsigned via_cb2h; /* basic handshake version of cb2 */\n cb2h = 0;\n //static unsigned via_cb2s; /* version of cb2 controlled by the shift register */\n cb2s = 0;\n\n reset() {\n // http://archive.6502.org/datasheets/mos_6522_preliminary_nov_1977.pdf\n // \"Reset sets all registers to zero except t1 t2 and sr\"\n this.ora = 0;\n this.orb = 0;\n this.ddra = 0;\n this.ddrb = 0;\n this.t1on = 0;\n this.t1int = 0;\n this.t1c = 0;\n this.t1ll = 0;\n this.t1lh = 0;\n this.t1pb7 = 0x80;\n this.t2on = 0;\n this.t2int = 0;\n this.t2c = 0;\n this.t2ll = 0;\n this.sr = 0;\n this.srb = 8;\n this.src = 0;\n this.srclk = 0;\n this.acr = 0;\n this.pcr = 0;\n this.ifr = 0;\n this.ier = 0;\n this.ca2 = 1;\n this.cb2h = 1;\n this.cb2s = 0;\n };\n\n int_update() {\n if ((this.ifr & 0x7f) & (this.ier & 0x7f)) {\n this.ifr |= 0x80;\n }\n else {\n this.ifr &= 0x7f;\n }\n }\n\n step0() {\n var t2shift = 0;\n if (this.t1on) {\n this.t1c = (this.t1c > 0 ? this.t1c - 1 : 0xffff);\n if ((this.t1c & 0xffff) == 0xffff) {\n /* counter just rolled over */\n if (this.acr & 0x40) {\n /* continuous interrupt mode */\n this.ifr |= 0x40;\n this.int_update();\n this.t1pb7 ^= 0x80;\n /* reload counter */\n this.t1c = (this.t1lh << 8) | this.t1ll;\n }\n else {\n /* one shot mode */\n if (this.t1int) {\n this.ifr |= 0x40;\n this.int_update();\n this.t1pb7 = 0x80;\n this.t1int = 0;\n }\n }\n }\n }\n\n if (this.t2on && (this.acr & 0x20) == 0x00) {\n this.t2c = (this.t2c > 0 ? this.t2c - 1 : 0xffff);\n if ((this.t2c & 0xffff) == 0xffff) {\n /* one shot mode */\n if (this.t2int) {\n this.ifr |= 0x20;\n this.int_update();\n this.t2int = 0;\n }\n }\n }\n\n /* shift counter */\n this.src = (this.src > 0 ? this.src - 1 : 0xff); // raz was 0xffffffff\n if ((this.src & 0xff) == 0xff) {\n this.src = this.t2ll;\n if (this.srclk) {\n t2shift = 1;\n this.srclk = 0;\n }\n else {\n t2shift = 0;\n this.srclk = 1;\n }\n }\n else {\n t2shift = 0;\n }\n\n if (this.srb < 8) {\n switch (this.acr & 0x1c) {\n case 0x00:\n /* disabled */\n break;\n case 0x04:\n /* shift in under control of t2 */\n if (t2shift) {\n /* shifting in 0s since cb2 is always an output */\n this.sr <<= 1;\n this.srb++;\n }\n break;\n case 0x08:\n /* shift in under system clk control */\n this.sr <<= 1;\n this.srb++;\n break;\n case 0x0c:\n /* shift in under cb1 control */\n break;\n case 0x10:\n /* shift out under t2 control (free run) */\n if (t2shift) {\n this.cb2s = (this.sr >> 7) & 1;\n this.sr <<= 1;\n this.sr |= this.cb2s;\n }\n break;\n case 0x14:\n /* shift out under t2 control */\n if (t2shift) {\n this.cb2s = (this.sr >> 7) & 1;\n this.sr <<= 1;\n this.sr |= this.cb2s;\n this.srb++;\n }\n break;\n case 0x18:\n /* shift out under system clock control */\n this.cb2s = (this.sr >> 7) & 1;\n this.sr <<= 1;\n this.sr |= this.cb2s;\n this.srb++;\n break;\n case 0x1c:\n /* shift out under cb1 control */\n break;\n }\n\n if (this.srb == 8) {\n this.ifr |= 0x04;\n this.int_update();\n }\n }\n }\n\n step1() {\n if ((this.pcr & 0x0e) == 0x0a) {\n /* if ca2 is in pulse mode, then make sure\n * it gets restored to '1' after the pulse.\n */\n this.ca2 = 1;\n }\n if ((this.pcr & 0xe0) == 0xa0) {\n /* if cb2 is in pulse mode, then make sure\n * it gets restored to '1' after the pulse.\n */\n this.cb2h = 1;\n }\n }\n\n read(address) {\n var data;\n /* io */\n switch (address & 0xf) {\n case 0x0:\n /* compare signal is an input so the value does not come from\n * orb.\n */\n if (this.acr & 0x80) {\n /* timer 1 has control of bit 7 */\n data = ((this.orb & 0x5f) | this.t1pb7 | this.vectrex.alg.compare);\n }\n else {\n /* bit 7 is being driven by orb */\n data = ((this.orb & 0xdf) | this.vectrex.alg.compare);\n }\n return data & 0xff;\n case 0x1:\n /* register 1 also performs handshakes if necessary */\n if ((this.pcr & 0x0e) == 0x08) {\n /* if ca2 is in pulse mode or handshake mode, then it\n * goes low whenever ira is read.\n */\n this.ca2 = 0;\n }\n /* fall through */\n case 0xf:\n if ((this.orb & 0x18) == 0x08) {\n /* the snd chip is driving port a */\n data = this.vectrex.psg.readData();\n //console.log(this.vectrex.psg.currentRegister(), data);\n }\n else {\n data = this.ora;\n }\n return data & 0xff;\n case 0x2:\n return this.ddrb & 0xff;\n case 0x3:\n return this.ddra & 0xff;\n case 0x4:\n /* T1 low order counter */\n data = this.t1c;\n this.ifr &= 0xbf; /* remove timer 1 interrupt flag */\n this.t1on = 0; /* timer 1 is stopped */\n this.t1int = 0;\n this.t1pb7 = 0x80;\n this.int_update();\n return data & 0xff;\n case 0x5:\n /* T1 high order counter */\n return (this.t1c >> 8) & 0xff;\n case 0x6:\n /* T1 low order latch */\n return this.t1ll & 0xff;\n case 0x7:\n /* T1 high order latch */\n return this.t1lh & 0xff;\n case 0x8:\n /* T2 low order counter */\n data = this.t2c;\n this.ifr &= 0xdf; /* remove timer 2 interrupt flag */\n this.t2on = 0; /* timer 2 is stopped */\n this.t2int = 0;\n this.int_update();\n return data & 0xff;\n case 0x9:\n /* T2 high order counter */\n return (this.t2c >> 8);\n case 0xa:\n data = this.sr;\n this.ifr &= 0xfb; /* remove shift register interrupt flag */\n this.srb = 0;\n this.srclk = 1;\n this.int_update();\n return data & 0xff;\n case 0xb:\n return this.acr & 0xff;\n case 0xc:\n return this.pcr & 0xff;\n case 0xd:\n /* interrupt flag register */\n return this.ifr & 0xff;\n case 0xe:\n /* interrupt enable register */\n return (this.ier | 0x80) & 0xff;\n }\n }\n\n write(address, data) {\n switch (address & 0xf) {\n case 0x0:\n this.orb = data;\n this.vectrex.snd_update();\n this.vectrex.alg.update();\n if ((this.pcr & 0xe0) == 0x80) {\n /* if cb2 is in pulse mode or handshake mode, then it\n * goes low whenever orb is written.\n */\n this.cb2h = 0;\n }\n break;\n case 0x1:\n /* register 1 also performs handshakes if necessary */\n if ((this.pcr & 0x0e) == 0x08) {\n /* if ca2 is in pulse mode or handshake mode, then it\n * goes low whenever ora is written.\n */\n this.ca2 = 0;\n }\n /* fall through */\n case 0xf:\n this.ora = data;\n this.vectrex.snd_update();\n /* output of port a feeds directly into the dac which then\n * feeds the x axis sample and hold.\n */\n this.vectrex.alg.xsh = data ^ 0x80;\n this.vectrex.alg.update();\n break;\n case 0x2:\n this.ddrb = data;\n break;\n case 0x3:\n this.ddra = data;\n break;\n case 0x4:\n /* T1 low order counter */\n this.t1ll = data;\n break;\n case 0x5:\n /* T1 high order counter */\n this.t1lh = data;\n this.t1c = (this.t1lh << 8) | this.t1ll;\n this.ifr &= 0xbf; /* remove timer 1 interrupt flag */\n this.t1on = 1; /* timer 1 starts running */\n this.t1int = 1;\n this.t1pb7 = 0;\n this.int_update();\n break;\n case 0x6:\n /* T1 low order latch */\n this.t1ll = data;\n break;\n case 0x7:\n /* T1 high order latch */\n this.t1lh = data;\n break;\n case 0x8:\n /* T2 low order latch */\n this.t2ll = data;\n break;\n case 0x9:\n /* T2 high order latch/counter */\n this.t2c = (data << 8) | this.t2ll;\n this.ifr &= 0xdf;\n this.t2on = 1; /* timer 2 starts running */\n this.t2int = 1;\n this.int_update();\n break;\n case 0xa:\n this.sr = data;\n this.ifr &= 0xfb; /* remove shift register interrupt flag */\n this.srb = 0;\n this.srclk = 1;\n this.int_update();\n break;\n case 0xb:\n this.acr = data;\n break;\n case 0xc:\n this.pcr = data;\n if ((this.pcr & 0x0e) == 0x0c) {\n /* ca2 is outputting low */\n this.ca2 = 0;\n }\n else {\n /* ca2 is disabled or in pulse mode or is\n * outputting high.\n */\n this.ca2 = 1;\n }\n if ((this.pcr & 0xe0) == 0xc0) {\n /* cb2 is outputting low */\n this.cb2h = 0;\n }\n else {\n /* cb2 is disabled or is in pulse mode or is\n * outputting high.\n */\n this.cb2h = 1;\n }\n break;\n case 0xd:\n /* interrupt flag register */\n this.ifr &= (~(data & 0x7f)); // & 0xffff ); // raz\n this.int_update();\n break;\n case 0xe:\n /* interrupt enable register */\n if (data & 0x80) {\n this.ier |= data & 0x7f;\n }\n else {\n this.ier &= (~(data & 0x7f)); // & 0xffff ); // raz\n }\n this.int_update();\n break;\n }\n }\n saveState() {\n return safe_extend(null, {}, this);\n }\n loadState(state) {\n safe_extend(null, this, state);\n }\n toLongString(state) {\n var s = \"\";\n for (var key in state) {\n s += key + \": \" + hex(state[key]) + \"\\n\";\n }\n return s;\n }\n};\n\nconst Globals =\n{\n VECTREX_MHZ: 1500000, /* speed of the vectrex being emulated */\n VECTREX_COLORS: 128, /* number of possible colors ... grayscale */\n ALG_MAX_X: 33000,\n ALG_MAX_Y: 41000,\n //VECTREX_PDECAY: 30, /* phosphor decay rate */\n //VECTOR_HASH: 65521,\n SCREEN_X_DEFAULT: 900,\n SCREEN_Y_DEFAULT: 1100,\n BOUNDS_MIN_X: 0,\n BOUNDS_MAX_X: 30000,\n BOUNDS_MIN_Y: 41000,\n BOUNDS_MAX_Y: 0,\n};\n\nclass VectrexAnalog {\n vectrex: VectrexPlatform;\n constructor(vectrex) {\n this.vectrex = vectrex;\n }\n videoEnabled = true;\n //static unsigned rsh; /* zero ref sample and hold */\n rsh = 0;\n //static unsigned xsh; /* x sample and hold */\n xsh = 0;\n //static unsigned ysh; /* y sample and hold */\n ysh = 0;\n //static unsigned zsh; /* z sample and hold */\n zsh = 0;\n //unsigned jch0;\t\t /* joystick direction channel 0 */\n jch0 = 0;\n //unsigned jch1;\t\t /* joystick direction channel 1 */\n jch1 = 0;\n //unsigned jch2;\t\t /* joystick direction channel 2 */\n jch2 = 0;\n //unsigned jch3;\t\t /* joystick direction channel 3 */\n jch3 = 0;\n //static unsigned jsh; /* joystick sample and hold */\n jsh = 0;\n //static unsigned compare;\n compare = 0;\n //static long dx; /* delta x */\n dx = 0;\n //static long dy; /* delta y */\n dy = 0;\n //static long curr_x; /* current x position */\n curr_x = 0;\n //static long curr_y; /* current y position */\n curr_y = 0;\n\n max_x = Globals.ALG_MAX_X >> 1;\n max_y = Globals.ALG_MAX_Y >> 1;\n\n //static unsigned vectoring; /* are we drawing a vector right now? */\n vectoring = false;\n //static long vector_x0;\n vector_x0 = 0;\n //static long vector_y0;\n vector_y0 = 0;\n //static long vector_x1;\n vector_x1 = 0;\n //static long vector_y1;\n vector_y1 = 0;\n //static long vector_dx;\n vector_dx = 0;\n //static long vector_dy;\n vector_dy = 0;\n //static unsigned char vector_color;\n vector_color = 0;\n\n reset() {\n this.rsh = 128;\n this.xsh = 128;\n this.ysh = 128;\n this.zsh = 0;\n this.jch0 = 128;\n this.jch1 = 128;\n this.jch2 = 128;\n this.jch3 = 128;\n this.jsh = 128;\n this.compare = 0;\n /* check this */\n this.dx = 0;\n this.dy = 0;\n this.curr_x = Globals.ALG_MAX_X >> 1;\n this.curr_y = Globals.ALG_MAX_Y >> 1;\n this.vectoring = false;\n }\n\n update() {\n var via = this.vectrex.via;\n switch (via.orb & 0x06) {\n case 0x00:\n this.jsh = this.jch0;\n if ((via.orb & 0x01) == 0x00) {\n /* demultiplexor is on */\n this.ysh = this.xsh;\n }\n break;\n case 0x02:\n this.jsh = this.jch1;\n if ((via.orb & 0x01) == 0x00) {\n /* demultiplexor is on */\n this.rsh = this.xsh;\n }\n break;\n case 0x04:\n this.jsh = this.jch2;\n if ((via.orb & 0x01) == 0x00) {\n /* demultiplexor is on */\n if (this.xsh > 0x80) {\n this.zsh = this.xsh - 0x80;\n } else {\n this.zsh = 0;\n }\n }\n break;\n case 0x06:\n /* sound output line */\n this.jsh = this.jch3;\n break;\n }\n\n /* compare the current joystick direction with a reference */\n if (this.jsh > this.xsh) {\n this.compare = 0x20;\n } else {\n this.compare = 0;\n }\n\n /* compute the new \"deltas\" */\n this.dx = this.xsh - this.rsh;\n this.dy = this.rsh - this.ysh;\n }\n\n step() {\n var via = this.vectrex.via;\n var sig_dx = 0;\n var sig_dy = 0;\n var sig_ramp = 0;\n var sig_blank = 0;\n\n if (via.acr & 0x10) {\n sig_blank = via.cb2s;\n }\n else {\n sig_blank = via.cb2h;\n }\n\n if (via.ca2 == 0)\n {\n /* need to force the current point to the 'orgin' so just\n * calculate distance to origin and use that as dx,dy.\n */\n sig_dx = this.max_x - this.curr_x;\n sig_dy = this.max_y - this.curr_y;\n }\n else {\n if (via.acr & 0x80) {\n sig_ramp = via.t1pb7;\n }\n else {\n sig_ramp = via.orb & 0x80;\n }\n\n if (sig_ramp == 0) {\n sig_dx = this.dx;\n sig_dy = this.dy;\n }\n else {\n sig_dx = 0;\n sig_dy = 0;\n }\n }\n //if (sig_dx || sig_dy) console.log(via.ca2, this.curr_x, this.curr_y, this.dx, this.dy, sig_dx, sig_dy, sig_ramp, sig_blank);\n\n if (!this.vectoring) {\n if (sig_blank == 1 &&\n this.curr_x >= 0 && this.curr_x < Globals.ALG_MAX_X &&\n this.curr_y >= 0 && this.curr_y < Globals.ALG_MAX_Y) {\n /* start a new vector */\n this.vectoring = true;\n this.vector_x0 = this.curr_x;\n this.vector_y0 = this.curr_y;\n this.vector_x1 = this.curr_x;\n this.vector_y1 = this.curr_y;\n this.vector_dx = sig_dx;\n this.vector_dy = sig_dy;\n this.vector_color = this.zsh & 0xff;\n }\n }\n else {\n /* already drawing a vector ... check if we need to turn it off */\n if (sig_blank == 0) {\n /* blank just went on, vectoring turns off, and we've got a\n * new line.\n */\n this.vectoring = false;\n\n this.addline(this.vector_x0, this.vector_y0,\n this.vector_x1, this.vector_y1,\n this.vector_color);\n }\n else if (sig_dx != this.vector_dx ||\n sig_dy != this.vector_dy ||\n (this.zsh & 0xff) != this.vector_color) {\n\n /* the parameters of the vectoring processing has changed.\n * so end the current line.\n */\n this.addline(this.vector_x0, this.vector_y0,\n this.vector_x1, this.vector_y1,\n this.vector_color);\n\n /* we continue vectoring with a new set of parameters if the\n * current point is not out of limits.\n */\n\n if (this.curr_x >= 0 && this.curr_x < Globals.ALG_MAX_X &&\n this.curr_y >= 0 && this.curr_y < Globals.ALG_MAX_Y) {\n this.vector_x0 = this.curr_x;\n this.vector_y0 = this.curr_y;\n this.vector_x1 = this.curr_x;\n this.vector_y1 = this.curr_y;\n this.vector_dx = sig_dx;\n this.vector_dy = sig_dy;\n this.vector_color = this.zsh & 0xff;\n }\n else {\n this.vectoring = false;\n }\n }\n }\n\n this.curr_x += sig_dx;\n this.curr_y += sig_dy;\n\n if (this.vectoring &&\n this.curr_x >= 0 && this.curr_x < Globals.ALG_MAX_X &&\n this.curr_y >= 0 && this.curr_y < Globals.ALG_MAX_Y) {\n /* we're vectoring ... current point is still within limits so\n * extend the current vector.\n */\n this.vector_x1 = this.curr_x;\n this.vector_y1 = this.curr_y;\n }\n }\n\n addline(x0, y0, x1, y1, color) {\n if (!this.videoEnabled) return;\n // TODO\n //console.log(x0, y0, x1, y1, color);\n x0 = (x0 - Globals.BOUNDS_MIN_X) / (Globals.BOUNDS_MAX_X - Globals.BOUNDS_MIN_X) * Globals.SCREEN_X_DEFAULT;\n x1 = (x1 - Globals.BOUNDS_MIN_X) / (Globals.BOUNDS_MAX_X - Globals.BOUNDS_MIN_X) * Globals.SCREEN_X_DEFAULT;\n y0 = (y0 - Globals.BOUNDS_MIN_Y) / (Globals.BOUNDS_MAX_Y - Globals.BOUNDS_MIN_Y) * Globals.SCREEN_Y_DEFAULT;\n y1 = (y1 - Globals.BOUNDS_MIN_Y) / (Globals.BOUNDS_MAX_Y - Globals.BOUNDS_MIN_Y) * Globals.SCREEN_Y_DEFAULT;\n this.vectrex.video.drawLine(x0, y0, x1, y1, color, 7);\n }\n\n saveState() {\n return safe_extend(null, {}, this);\n }\n loadState(state) {\n safe_extend(null, this, state);\n }\n toLongString(state) {\n var s = \"\";\n for (var key in state) {\n s += key + \": \" + state[key] + \"\\n\";\n }\n return s;\n }\n}\n\n//\n\nclass VectrexPlatform extends Base6809Platform {\n\n mainElement;\n via: VIA6522;\n alg: VectrexAnalog;\n ram: Uint8Array;\n rom: Uint8Array;\n bios: Uint8Array;\n inputs: Uint8Array;\n bus;\n video: VectorVideo;\n psg: AY38910_Audio;\n audio;\n timer: AnimationTimer;\n\n constructor(mainElement) {\n super();\n this.mainElement = mainElement;\n }\n\n getPresets() {\n return VECTREX_PRESETS;\n }\n\n start() {\n this.via = new VIA6522(this);\n this.alg = new VectrexAnalog(this);\n this.bios = padBytes(new lzgmini().decode(stringToByteArray(atob(VECTREX_FASTROM_LZG))), 0x2000);\n this.ram = new Uint8Array(0x400);\n this.inputs = new Uint8Array(4);\n var mbus = {\n read: newAddressDecoder([\n [0x0000, 0x7fff, 0, (a) => { return this.rom && this.rom[a]; }],\n [0xc800, 0xcfff, 0x3ff, (a) => { return this.ram[a]; }],\n [0xd000, 0xdfff, 0xf, (a) => { return this.via.read(a); }],\n [0xe000, 0xffff, 0x1fff, (a) => { return this.bios && this.bios[a]; }],\n ]),\n\n write: newAddressDecoder([\n [0xc800, 0xcfff, 0x3ff, (a, v) => { this.ram[a] = v; }],\n [0xd000, 0xd7ff, 0x3ff, (a, v) => { this.via.write(a & 0xf, v); }],\n [0xd800, 0xdfff, 0x3ff, (a, v) => { this.ram[a] = v; this.via.write(a & 0xf, v); }],\n ])\n };\n this.bus = {\n read: (a) => { var v = mbus.read(a); this.probe.logRead(a,v); return v; },\n write: (a,v) => { this.probe.logWrite(a,v); mbus.write(a,v); }\n };\n this._cpu = this.newCPU(this.bus);\n // create video/audio\n this.video = new VectorVideo(this.mainElement, Globals.SCREEN_X_DEFAULT, Globals.SCREEN_Y_DEFAULT);\n this.video.persistenceAlpha = 0.2;\n this.audio = new MasterAudio();\n this.psg = new AY38910_Audio(this.audio);\n this.video.create();\n this.timer = new AnimationTimer(60, this.nextFrame.bind(this));\n setKeyboardFromMap(this.video, this.inputs, VECTREX_KEYCODE_MAP); // true = always send function);\n }\n\n // TODO: loadControlsState\n updateControls() {\n // joystick (analog simulation)\n this.alg.jch0 = (this.inputs[0] & 0x1) ? 0x00 : (this.inputs[0] & 0x2) ? 0xff : 0x80;\n this.alg.jch1 = (this.inputs[0] & 0x4) ? 0x00 : (this.inputs[0] & 0x8) ? 0xff : 0x80;\n this.alg.jch2 = (this.inputs[1] & 0x1) ? 0x00 : (this.inputs[1] & 0x2) ? 0xff : 0x80;\n this.alg.jch3 = (this.inputs[1] & 0x4) ? 0x00 : (this.inputs[1] & 0x8) ? 0xff : 0x80;\n // buttons (digital)\n this.psg.psg.register[14] = ~this.inputs[2];\n }\n\n advance(novideo:boolean) : number {\n if (!novideo) this.video.clear();\n this.alg.videoEnabled = !novideo;\n this.updateControls();\n this.probe.logNewFrame();\n var frameCycles = 1500000 / 60;\n var cycles = 0;\n while (cycles < frameCycles) {\n cycles += this.nextCycle();\n }\n return cycles;\n }\n\n nextCycle() {\n this.probe.logExecute(this.getPC(), this.getSP());\n if (this.via.ifr & 0x80) {\n this._cpu.interrupt();\n }\n var n = this.runCPU(this._cpu, 1);\n if (n == 0) n = 1; // TODO?\n this.probe.logClocks(n);\n for (var i=0; i