<commit_before>/*
*  6502 assembler and simulator in Javascript
*  (C)2006-2010 <NAME> - www.6502asm.com
*
*  Adapted by <NAME>
*  https://github.com/skilldrick/6502js
*
*  Released under the GNU General Public License
*  see http://gnu.org/licenses/gpl.html
*/


function SimulatorWidget(node) {
  var $node = $(node);
  var ui = UI();
  var display = Display();
  var memory = Memory();
  var labels = Labels();
  var simulator = Simulator();
  var assembler = Assembler();

  function initialize() {
    stripText();
    ui.initialize();
    display.initialize();
    simulator.reset();

    $node.find('.assembleButton').click(function () {
      assembler.assembleCode();
    });
    $node.find('.runButton').click(simulator.runBinary);
    $node.find('.runButton').click(simulator.stopDebugger);
    $node.find('.resetButton').click(simulator.reset);
    $node.find('.hexdumpButton').click(assembler.hexdump);
    $node.find('.disassembleButton').click(assembler.disassemble);
    $node.find('.debug').change(function () {
      var debug = $(this).is(':checked');
      if (debug) {
        ui.debugOn();
        simulator.enableDebugger();
      } else {
        ui.debugOff();
        simulator.stopDebugger();
      }
    });
    $node.find('.monitoring').change(function () {
      ui.toggleMonitor();
      simulator.toggleMonitor();
    });
    $node.find('.stepButton').click(simulator.debugExec);
    $node.find('.gotoButton').click(simulator.gotoAddr);
    $node.find('.notesButton').click(ui.showNotes);
    $node.find('.code').keypress(simulator.stop);
    $node.find('.code').keypress(ui.initialize);
    $(document).keypress(memory.storeKeypress);
  }

  function stripText() {
    //Remove leading and trailing space in textarea
    var text = $node.find('.code').val();
    text = text.replace(/^\n+/, '').replace(/\s+$/, '');
    $node.find('.code').val(text);
  }

  function UI() {
    var currentState;

    var start = {
      assemble: true,
      run: [false, 'Run'],
      reset: false,
      hexdump: false,
      disassemble: false,
      debug: [false, false]
    };
    var assembled = {
      assemble: false,
      run: [true, 'Run'],
      reset: true,
      hexdump: true,
      disassemble: true,
      debug: [true, false]
    };
    var running = {
      assemble: false,
      run: [true, 'Stop'],
      reset: true,
      hexdump: false,
      disassemble: false,
      debug: [true, false]
    };
    var debugging = {
      assemble: false,
      reset: true,
      hexdump: true,
      disassemble: true,
      debug: [true, true]
    };
    var postDebugging = {
      assemble: false,
      reset: true,
      hexdump: true,
      disassemble: true,
      debug: [true, false]
    };


    function setState(state) {
      $node.find('.assembleButton').attr('disabled', !state.assemble);
      if (state.run) {
        $node.find('.runButton').attr('disabled', !state.run[0]);
        $node.find('.runButton').val(state.run[1]);
      }
      $node.find('.resetButton').attr('disabled', !state.reset);
      $node.find('.hexdumpButton').attr('disabled', !state.hexdump);
      $node.find('.disassembleButton').attr('disabled', !state.disassemble);
      $node.find('.debug').attr('disabled', !state.debug[0]);
      $node.find('.debug').attr('checked', state.debug[1]);
      $node.find('.stepButton').attr('disabled', !state.debug[1]);
      $node.find('.gotoButton').attr('disabled', !state.debug[1]);
      currentState = state;
    }

    function initialize() {
      setState(start);
    }

    function play() {
      setState(running);
    }

    function stop() {
      setState(assembled);
    }

    function debugOn() {
      setState(debugging);
    }

    function debugOff() {
      setState(postDebugging);
    }

    function assembleSuccess() {
      setState(assembled);
    }

    function toggleMonitor() {
      $node.find('.monitor').toggle();
    }

    function showNotes() {
      $node.find('.messages code').html($node.find('.notes').html());
    }

    return {
      initialize: initialize,
      play: play,
      stop: stop,
      assembleSuccess: assembleSuccess,
      debugOn: debugOn,
      debugOff: debugOff,
      toggleMonitor: toggleMonitor,
      showNotes: showNotes
    };
  }


  function Display() {
    var displayArray = [];
    var palette = [
      "#000000", // 0
      "#00aa00", // 1
      "#ff0000", // 2
      "#ffaa00", // 3
      "#005500", // 4
      "#00ff00", // 5
      "#ff5500", // 6
      "#ffff00", // 7
      "#0000ff", // 8
      "#00aaff", // 9
      "#ff00ff", // 10
      "#ffaaff", // 11
      "#0055ff", // 12
      "#00ffff", // 13
      "#ff55ff", // 14
      "#ffffff"  // 15
    ];
    var ctx;
    var width;
    var height;
    var pixelSize;
    var pixelHiResSize
    var numX = 128;
    var numY = 128;

    function initialize() {
      var canvas = $node.find('.screen')[0];
      width = canvas.width;
      height = canvas.height;
      pixelSize = width / numX;
      pixelHiResSize = width / (2 * numX);
      ctx = canvas.getContext('2d');
      reset();
    }

    function reset() {
      ctx.fillStyle = "black";
      ctx.fillRect(0, 0, width, height);
    }

    /**
     * Draw supplied memory address in screen.
     */
    function updatePixel(addr) {
      // Read video mode [HiRes Invert S1 S0    RGB LED NC NC]
      var videoModeReg = memory.get(0xdf80);
      
      // Flags
      var hiRes = (videoModeReg & 0x80)>>7;      
      var invert = (videoModeReg & 0x40)>>6;
      
      if(hiRes == 0) {
        drawColorPixel(addr);
      }
      else if(hiRes == 1) {
        drawHiResPixel(addr);
      }      
    }
    
    function toHexValue(value) {
      var stringValue = value.toString(16);
      if(stringValue.length<2) {
        stringValue = '0'+stringValue;
      }
      return stringValue;
    }
    
    /** 
     * Get rgb color to display pixel.
     */
    function getColor(index) {
      // Color components
      var red=0, green=0, blue=0;

      // Durango palette
      switch(index) {
        case 0x00: red = 0x00; green = 0x00; blue = 0x00; break; // 0
        case 0x01: red = 0x00; green = 0xaa; blue = 0x00; break; // 1
        case 0x02: red = 0xff; green = 0x00; blue = 0x00; break; // 2
        case 0x03: red = 0xff; green = 0xaa; blue = 0x00; break; // 3
        case 0x04: red = 0x00; green = 0x55; blue = 0x00; break; // 4
        case 0x05: red = 0x00; green = 0xff; blue = 0x00; break; // 5
        case 0x06: red = 0xff; green = 0x55; blue = 0x00; break; // 6
        case 0x07: red = 0xff; green = 0xff; blue = 0x00; break; // 7
        case 0x08: red = 0x00; green = 0x00; blue = 0xff; break; // 8
        case 0x09: red = 0x00; green = 0xaa; blue = 0xff; break; // 9
        case 0x0a: red = 0xff; green = 0x00; blue = 0xff; break; // 10
        case 0x0b: red = 0xff; green = 0xaa; blue = 0xff; break; // 11
        case 0x0c: red = 0x00; green = 0x55; blue = 0xff; break; // 12
        case 0x0d: red = 0x00; green = 0xff; blue = 0xff; break; // 13
        case 0x0e: red = 0xff; green = 0x55; blue = 0xff; break; // 14
        case 0x0f: red = 0xff; green = 0xff; blue = 0xff; break; // 15
      }
	
      // Process invert flag
      if((memory.get(0xdf80) & 0x40)>>6 == 1) {
        red = 0xff-red;
        green = 0xff - green;
        blue = 0xff - blue;
      }
	
      // Process RGB flag
      if((memory.get(0xdf80) & 0x08)>>3 == 0) {
        red = (red + green + blue) / 3;
        green = red;
        blue = green;
      }
    
      return '#'+toHexValue(red)+toHexValue(green)+toHexValue(blue);
    }
    
    /* Set current color in SDL HiRes mode */
    function getHiresColor(color_index) {
      var color = color_index == 0 ? 0x00 : 0xff;

      // Process invert flag
      if((memory.get(0xdf80) & 0x40)>>6 == 1) {
        color = 0xff-color;
      }
      
      htmlColor = toHexValue(color);
      htmlColor = '#'+htmlColor+htmlColor+htmlColor;

      return htmlColor;
    }<commit_msg>optimise graphics on 6502js<commit_after>/*
*  6502 assembler and simulator in Javascript
*  (C)2006-2010 <NAME> - www.6502asm.com
*
*  Adapted by <NAME>
*  https://github.com/skilldrick/6502js
*
*  Released under the GNU General Public License
*  see http://gnu.org/licenses/gpl.html
*/


function SimulatorWidget(node) {
  var $node = $(node);
  var ui = UI();
  var display = Display();
  var memory = Memory();
  var labels = Labels();
  var simulator = Simulator();
  var assembler = Assembler();

  function initialize() {
    stripText();
    ui.initialize();
    display.initialize();
    simulator.reset();

    $node.find('.assembleButton').click(function () {
      assembler.assembleCode();
    });
    $node.find('.runButton').click(simulator.runBinary);
    $node.find('.runButton').click(simulator.stopDebugger);
    $node.find('.resetButton').click(simulator.reset);
    $node.find('.hexdumpButton').click(assembler.hexdump);
    $node.find('.disassembleButton').click(assembler.disassemble);
    $node.find('.debug').change(function () {
      var debug = $(this).is(':checked');
      if (debug) {
        ui.debugOn();
        simulator.enableDebugger();
      } else {
        ui.debugOff();
        simulator.stopDebugger();
      }
    });
    $node.find('.monitoring').change(function () {
      ui.toggleMonitor();
      simulator.toggleMonitor();
    });
    $node.find('.stepButton').click(simulator.debugExec);
    $node.find('.gotoButton').click(simulator.gotoAddr);
    $node.find('.notesButton').click(ui.showNotes);
    $node.find('.code').keypress(simulator.stop);
    $node.find('.code').keypress(ui.initialize);
    $(document).keypress(memory.storeKeypress);
  }

  function stripText() {
    //Remove leading and trailing space in textarea
    var text = $node.find('.code').val();
    text = text.replace(/^\n+/, '').replace(/\s+$/, '');
    $node.find('.code').val(text);
  }

  function UI() {
    var currentState;

    var start = {
      assemble: true,
      run: [false, 'Run'],
      reset: false,
      hexdump: false,
      disassemble: false,
      debug: [false, false]
    };
    var assembled = {
      assemble: false,
      run: [true, 'Run'],
      reset: true,
      hexdump: true,
      disassemble: true,
      debug: [true, false]
    };
    var running = {
      assemble: false,
      run: [true, 'Stop'],
      reset: true,
      hexdump: false,
      disassemble: false,
      debug: [true, false]
    };
    var debugging = {
      assemble: false,
      reset: true,
      hexdump: true,
      disassemble: true,
      debug: [true, true]
    };
    var postDebugging = {
      assemble: false,
      reset: true,
      hexdump: true,
      disassemble: true,
      debug: [true, false]
    };


    function setState(state) {
      $node.find('.assembleButton').attr('disabled', !state.assemble);
      if (state.run) {
        $node.find('.runButton').attr('disabled', !state.run[0]);
        $node.find('.runButton').val(state.run[1]);
      }
      $node.find('.resetButton').attr('disabled', !state.reset);
      $node.find('.hexdumpButton').attr('disabled', !state.hexdump);
      $node.find('.disassembleButton').attr('disabled', !state.disassemble);
      $node.find('.debug').attr('disabled', !state.debug[0]);
      $node.find('.debug').attr('checked', state.debug[1]);
      $node.find('.stepButton').attr('disabled', !state.debug[1]);
      $node.find('.gotoButton').attr('disabled', !state.debug[1]);
      currentState = state;
    }

    function initialize() {
      setState(start);
    }

    function play() {
      setState(running);
    }

    function stop() {
      setState(assembled);
    }

    function debugOn() {
      setState(debugging);
    }

    function debugOff() {
      setState(postDebugging);
    }

    function assembleSuccess() {
      setState(assembled);
    }

    function toggleMonitor() {
      $node.find('.monitor').toggle();
    }

    function showNotes() {
      $node.find('.messages code').html($node.find('.notes').html());
    }

    return {
      initialize: initialize,
      play: play,
      stop: stop,
      assembleSuccess: assembleSuccess,
      debugOn: debugOn,
      debugOff: debugOff,
      toggleMonitor: toggleMonitor,
      showNotes: showNotes
    };
  }


  function Display() {
    var displayArray = [];
    var palette = [
      "#000000", // 0
      "#00aa00", // 1
      "#ff0000", // 2
      "#ffaa00", // 3
      "#005500", // 4
      "#00ff00", // 5
      "#ff5500", // 6
      "#ffff00", // 7
      "#0000ff", // 8
      "#00aaff", // 9
      "#ff00ff", // 10
      "#ffaaff", // 11
      "#0055ff", // 12
      "#00ffff", // 13
      "#ff55ff", // 14
      "#ffffff"  // 15
    ];
    var ctx;
    var width;
    var height;
    var pixelSize;
    var pixelHiResSize
    var numX = 128;
    var numY = 128;

    function initialize() {
      var canvas = $node.find('.screen')[0];
      width = canvas.width;
      height = canvas.height;
      pixelSize = width / numX;
      pixelHiResSize = width / (2 * numX);
      ctx = canvas.getContext('2d');
      reset();
    }

    function reset() {
      ctx.fillStyle = "black";
      ctx.fillRect(0, 0, width, height);
    }

    /**
     * Draw supplied memory address in screen.
     */
    function updatePixel(addr) {
      // Read video mode [HiRes Invert S1 S0    RGB LED NC NC]
      var videoModeReg = memory.get(0xdf80);
      
      // Flags
      var hiRes  = videoModeReg & 0x80;      
      var invert = videoModeReg & 0x40;
      
      if(hiRes) {
        drawHiResPixel(addr);
      }
      else {
        drawColorPixel(addr);
      }      
    }
    
    function toHexValue(value) {
      var stringValue = value.toString(16);
      if(stringValue.length<2) {
        stringValue = '0'+stringValue;
      }
      return stringValue;
    }
    
    /** 
     * Get rgb color to display pixel.
     */
    function getColor(c) {
      // Color components
      var red=0, green=0, blue=0;

      // Process invert flag
      if(memory.get(0xdf80) & 0x40) {
          c ~= c;
      }

      // Durango palette
      switch(c) {
        case 0x00: red = 0x00; green = 0x00; blue = 0x00; break; // 0
        case 0x01: red = 0x00; green = 0xaa; blue = 0x00; break; // 1
        case 0x02: red = 0xff; green = 0x00; blue = 0x00; break; // 2
        case 0x03: red = 0xff; green = 0xaa; blue = 0x00; break; // 3
        case 0x04: red = 0x00; green = 0x55; blue = 0x00; break; // 4
        case 0x05: red = 0x00; green = 0xff; blue = 0x00; break; // 5
        case 0x06: red = 0xff; green = 0x55; blue = 0x00; break; // 6
        case 0x07: red = 0xff; green = 0xff; blue = 0x00; break; // 7
        case 0x08: red = 0x00; green = 0x00; blue = 0xff; break; // 8
        case 0x09: red = 0x00; green = 0xaa; blue = 0xff; break; // 9
        case 0x0a: red = 0xff; green = 0x00; blue = 0xff; break; // 10
        case 0x0b: red = 0xff; green = 0xaa; blue = 0xff; break; // 11
        case 0x0c: red = 0x00; green = 0x55; blue = 0xff; break; // 12
        case 0x0d: red = 0x00; green = 0xff; blue = 0xff; break; // 13
        case 0x0e: red = 0xff; green = 0x55; blue = 0xff; break; // 14
        case 0x0f: red = 0xff; green = 0xff; blue = 0xff; break; // 15
      }
	
	
      // Process RGB flag
      if(!(memory.get(0xdf80) & 0x08)) {
        red = green = blue = ((c&1)?0x88:0) | ((c&2)?0x44:0) | ((c&4)?0x22:0) | ((c&8)?0x11:0);
      }
    
      return '#'+toHexValue(red)+toHexValue(green)+toHexValue(blue);
    }
    
    /* Set current color in SDL HiRes mode */
    function getHiresColor(color_index) {
      var color = color_index == 0 ? 0x00 : 0xff;

      // Process invert flag
      if(memory.get(0xdf80) & 0x40) {
        color = ~color;
      }
      
      htmlColor = toHexValue(color);
      htmlColor = '#'+htmlColor+htmlColor+htmlColor;

      return htmlColor;
    }
{'fraction_non_alphanumeric': 0.16096794468887493, 'fraction_numerical': 0.04116907605279698, 'mean_word_length': 2.1607071911005167, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2259205', 'n_tokens_mistral': 5837, 'n_tokens_neox': 5313, 'n_words': 1201}