import { Platform, Base6502Platform, BaseMAMEPlatform, getOpcodeMetadata_6502, cpuStateToLongString_6502, getToolForFilename_6502, dumpStackToString } from "../common/baseplatform";
import { PLATFORMS, RAM, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, RasterVideo, Keys, makeKeycodeMap, dumpRAM, KeyFlags, EmuHalt, ControllerPoller } from "../common/emu";
import { hex, lpad, lzgmini, byteArrayToString } from "../common/util";
import { CodeAnalyzer_nes } from "../common/analysis";
import { SampleAudio } from "../common/audio";
import { ProbeRecorder } from "../common/recorder";
import { NullProbe, Probeable, ProbeAll } from "../common/devices";
declare var jsnes : any;
declare var Mousetrap;
const JSNES_PRESETS = [
{id:'hello.c', name:'Hello World'},
{id:'attributes.c', name:'Attribute Table'},
{id:'scroll.c', name:'Scrolling'},
{id:'sprites.c', name:'Sprites'},
{id:'metasprites.c', name:'Metasprites'},
{id:'flicker.c', name:'Flickering Sprites'},
{id:'metacursor.c', name:'Controllers'},
{id:'vrambuffer.c', name:'VRAM Buffer'},
{id:'statusbar.c', name:'Split Status Bar'},
{id:'siegegame.c', name:'Siege Game'},
{id:'tint.c', name:'Color Emphasis'},
{id:'rletitle.c', name:'Title Screen RLE'},
{id:'aputest.c', name:'Sound Tester'},
{id:'music.c', name:'Music Player'},
{id:'horizscroll.c', name:'Offscreen Scrolling'},
{id:'monobitmap.c', name:'Monochrome Bitmap'},
{id:'fami.c', name:'Famitone Demo'},
{id:'shoot2.c', name:'Solarian Game'},
{id:'climber.c', name:'Climber Game'},
{id:'bankswitch.c', name:'Bank Switching'},
{id:'irq.c', name:'IRQ Scanline Counter'},
{id:'ex0.dasm', name:'Initialization (ASM)'},
{id:'ex1.dasm', name:'Hello World (ASM)'},
{id:'ex2.dasm', name:'Scrolling Demo (ASM)'},
{id:'ex3.dasm', name:'Sprite Demo (ASM)'},
{id:'ex4.dasm', name:'Controller Demo (ASM)'},
{id:'musicdemo.dasm', name:'Famitone Demo (ASM)'},
{id:'xyscroll.dasm', name:'XY Split Scrolling (ASM)'},
// {id:'scrollrt.dasm', name:'Line-by-line Scrolling (ASM)'},
{id:'road.dasm', name:'3-D Road Demo (ASM)'},
{id:'chase/game.c', name:'Shiru\'s Chase Game'},
{id:'hello.wiz', name:'Hello (Wiz)'},
];
/// JSNES
const JSNES_KEYCODE_MAP = makeKeycodeMap([
[Keys.A, 0, 0],
[Keys.B, 0, 1],
[Keys.SELECT, 0, 2],
[Keys.START, 0, 3],
[Keys.UP, 0, 4],
[Keys.DOWN, 0, 5],
[Keys.LEFT, 0, 6],
[Keys.RIGHT, 0, 7],
[Keys.P2_A, 1, 0],
[Keys.P2_B, 1, 1],
[Keys.P2_SELECT, 1, 2],
[Keys.P2_START, 1, 3],
[Keys.P2_UP, 1, 4],
[Keys.P2_DOWN, 1, 5],
[Keys.P2_LEFT, 1, 6],
[Keys.P2_RIGHT, 1, 7],
]);
class JSNESPlatform extends Base6502Platform implements Platform, Probeable {
mainElement;
nes;
video;
audio;
timer;
poller : ControllerPoller;
audioFrequency = 44030; //44100
frameindex = 0;
ntvideo;
ntlastbuf;
machine = { cpuCyclesPerLine: 114 }; // TODO: hack for width of probe scope
constructor(mainElement) {
super();
this.mainElement = mainElement;
}
getPresets() { return JSNES_PRESETS; }
start() {
this.debugPCDelta = 1;
var debugbar = $("
").appendTo(this.mainElement);
this.audio = new SampleAudio(this.audioFrequency);
this.video = new RasterVideo(this.mainElement,256,224,{overscan:true});
this.video.create();
// debugging view
this.ntvideo = new RasterVideo(this.mainElement,512,480,{overscan:false});
this.ntvideo.create();
$(this.ntvideo.canvas).hide();
this.ntlastbuf = new Uint32Array(0x1000);
Mousetrap.bind('ctrl+shift+alt+n', () => {
$(this.video.canvas).toggle()
$(this.ntvideo.canvas).toggle()
});
// toggle buttons (TODO)
/*
$('